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