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, )
'---------------------------------------