Sunday, August 31, 2008

Beginners Samples









Some useful CATIA vba samples, since v5Automation was overwhelming for newbies like me.




'opening statement /intersectionPt
'---------------------------------------
Dim partDocument1 As PartDocument
Set partDocument1 = CATIA.ActiveDocument
Dim part1 As Part
Set part1 = partDocument1.Part
'add geom set here
Dim hybridBody1 As HybridBody
Set hybridBody1 = part1.HybridBodies.Add()
hybridBody1.Name = "Geometrical Set.New"
part1.Update
'used geom set
Dim hybridBody2 As HybridBody
Set hybridBody2 = part1.HybridBodies.Item("Geometrical Set.1")
Dim hybridBody3 As HybridBody
Set hybridBody3 = part1.HybridBodies.Item("Geometrical Set.2").HybridBodies.Item("Multi Output.1 (Intersect)")
'---------------------------------------


'selection set /color
'---------------------------------------
Dim colorCount
colorCount = hybridBody1.HybridShapes.Count
For i = 1 To colorCount
Call partDocument1.Selection.Add(hybridBody1.HybridShapes.Item(i))
Dim visProperties1 As VisPropertySet
Set visProperties1 = partDocument1.Selection.VisProperties
visProperties1.SetRealColor 255, 255, 0, 1 'yellow(255,255,0)
partDocument1.Selection.Clear
Next i
'---------------------------------------


'measurable, parameter, formula
'---------------------------------------
'create line WITHOUT parameter first
Dim line1 As HybridShapeLineAngle
Set line1 = part1.HybridShapeFactory.AddNewLineAngle( , , , , , , , )
Dim angleFormula01 As Formula
Set angleFormula01 = part1.relations.CreateFormula( , , line1.Angle, )
hybridBody1.AppendHybridShape line1
part1.InWorkObject = line1

Dim TheMeasurable01 As Measurable
Set TheMeasurable01 = CATIA.ActiveDocument.GetWorkbench("SPAWorkbench").GetMeasurable( )
Dim lineMeasurable01 As Double
lineMeasurable01 = TheMeasurable01.Length
Dim MeasurableRealStOff01 As RealParam
Set MeasurableRealStOff01 = part1.Parameters.CreateReal("Real.measurable stOff", lineMeasurable01)
Dim MeasurableRealEdOff01 As RealParam
Set MeasurableRealEdOff01 = part1.Parameters.CreateReal("Real.measurable edOff", lineMeasurable01)

Dim lengthFormula011 As Formula
Set lengthFormula011 = part1.relations.CreateFormula( , , line1.BeginOffset, )
Dim lengthFormula012 As Formula
Set lengthFormula012 = part1.relations.CreateFormula( , , line1.EndOffset, )
'---------------------------------------

Saturday, August 16, 2008

CATIA scripting














Finally got hands on CATIA scripting. Most confusing thing about CATIA vba is the tree structure. Within Part Document only, CATIA has the following structure.

+-- PartDocument
+-
--- Part
+--
---- HybridShapeFactory
+---
----- HybridBodies
+----
------ HybridBody
+------
------ HybridShapes
+------
-------- HybridShape

And also, since CATIA has multiple ways to create a geometry,
"Dim ...... As ......" lines get a little tricky. Point Geometry for example, has 9 types including intersection point.

HybridShapePointBetween
HybridShapePointCenter
HybridShapePointCoord
HybridShapePointExplicit
HybridShapePointOnCurve
HybridShapePointOnPlane
HybridShapePointOnSurface
HybridShapePointTangent
HybridShapeInterSection



'Module h_planeM (THIS IS ONE MODULE ONLY !!!)

Option Explicit
'Author: YukiukiH
'Date: 10/18/2008
'compatibility: CATIA V5R18
'----------------------------------------------------------------------------------------------
Sub CATMain()
'opening statement
Dim partDocument1 As PartDocument
Set partDocument1 = CATIA.ActiveDocument
Dim part1 As Part
Set part1 = partDocument1.Part
'add geom set here
Dim hybridBody1 As HybridBody
Set hybridBody1 = part1.HybridBodies.Add()
hybridBody1.Name = "D_planeM"
part1.Update
'useg geom set
Dim hybridBody2 As HybridBody
Set hybridBody2 = part1.HybridBodies.Item("D_MidL")
Dim hybridBody3 As HybridBody
Set hybridBody3 = part1.HybridBodies.Item("D_normalM")

Dim i As Integer
Dim j As Integer
j = 0
Dim k As Integer

'Loop
'----------------------------------------------------------------------------------------------
'----------------------------------------------------------------------------------------------
Dim lineCount As Integer
lineCount = hybridBody2.HybridShapes.Count

For i = 1 To lineCount Step 9

'input
Dim line01 As HybridShapeLinePtPt
Set line01 = hybridBody2.HybridShapes.Item(i)
Dim lineNormal01 As HybridShapeLineNormal
Set lineNormal01 = hybridBody3.HybridShapes.Item(i + j)
'output
Dim plane01 As HybridShapePlane2Lines
Set plane01 = part1.HybridShapeFactory.AddNewPlane2Lines(line01, lineNormal01)
hybridBody1.AppendHybridShape plane01
part1.InWorkObject = plane01

'---------------------------------------
For k = 1 To 8
'input
Set line01 = hybridBody2.HybridShapes.Item(i + k)
Set lineNormal01 = hybridBody3.HybridShapes.Item(i + j + k)
'output
Set plane01 = part1.HybridShapeFactory.AddNewPlane2Lines(line01, lineNormal01)
hybridBody1.AppendHybridShape plane01
part1.InWorkObject = plane01
Next k
'---------------------------------------

j = j + 1
Next i
'----------------------------------------------------------------------------------------------
'----------------------------------------------------------------------------------------------
part1.Update 'has to be before SetRealColor



'color
'---------------------------------------
Dim colorCount
colorCount = hybridBody1.HybridShapes.Count
For i = 1 To colorCount
Call partDocument1.Selection.Add(hybridBody1.HybridShapes.Item(i))
Dim visProperties1 As VisPropertySet
Set visProperties1 = partDocument1.Selection.VisProperties
visProperties1.SetRealColor 0, 0, 0, 1 'blk(0, 0, 0)
partDocument1.Selection.Clear
Next i
'---------------------------------------
End Sub