How to add a GSD cylinder to a set of points with a CATScript Macro

Here’s a real world example for you. Let’s say you’ve got two parts being fastened together (by bolts, welds, whatever). Your job, Mr. Engineer, is to check to make sure we have enough clearance around each fastening point to be able to get our tools or machines in there in order to able to weld or tighten the parts together. To do this, you may want to create a cylinder at each point and proceed to check to see if there are any intrusions inside of this cylinder. If you have hundreds of connecting point this could become a very time consuming task. That’s where macros come in handy. We can write code which will automatically create specified cylinder geometry at every fastening point.

To add a cylinder in GSD all we need is a point and an axis (or direction). In previous examples (either here or in the newsletter) I’ve shown how to select and loop through a set of points and lines. My fastening center points and axis are saved in two arrays, called aPoints and aLines respectively.

Minus the error handling, here’s the start of my CATScript code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
Dim partDocument1 As Document 
Dim part1 As Part 
Set partDocument1 = CATIA.ActiveDocument 
Set part1 = partDocument1.Part
 
For i=1 to iCount
 
Dim reference1 As Reference 
Set reference1 = part1.CreateReferenceFromObject(aPoints(i))
 
Dim reference2 As Reference 
Set reference2 = part1.CreateReferenceFromObject(aLines(i))
 
Dim hybridShapeDirection1 As HybridShapeDirection 
Set hybridShapeDirection1 = hybridShapeFactory1.AddNewDirection(reference2)
 
Dim hybridShapeCylinder1 As HybridShapeCylinder 
Set hybridShapeCylinder1 = hybridShapeFactory1.AddNewCylinder(reference1, 27.000000, 40.000000, 40.000000, hybridShapeDirection1)
 
'set whether cylinder has mirrored extents where 0=no, 1=yes 
hybridShapeCylinder1.SymmetricalExtension = 0
 
'add the newly created cylinder geometry to my geometrical set called "FASTENER DATA"
 
Dim hybridBody1 As HybridBody 
Set hybridBody1 = hybridBodies1.Item("FASTENER DATA")
 
hybridBody1.AppendHybridShape hybridShapeCylinder1
 
'update the part 
part1.Update
 
'repeat the process at the next point 
Next 'i

To figure out what values to enter where to adjust the size of the cylinder, search for “AddNewCylinder” in the CATIA Object Browser.

This is one of those examples where it can be helpful to use the record a macro function. Record yourself creating one cylinder with the dimensions you want then go back and look at the code. The main difference will be the recorded macro will only work for the one point you manually selected. To fully automate the process you will need to add a function like the “For Loop” in order to loop or scroll through all the fastening points within the CATIA part file. Below is the full CATScript for the recorded macro. Notice the difference and simplifications which can be made:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
Language="VBSCRIPT"
'code by Emmett Ross
'www.scripting4v5.com
Sub CATMain()
 
Dim partDocument1 As Document 
Set partDocument1 = CATIA.ActiveDocument
 
Dim part1 As Part 
Set part1 = partDocument1.Part
 
Dim hybridBodies1 As HybridBodies 
Set hybridBodies1 = part1.HybridBodies
 
Dim hybridBody1 As HybridBody 
Set hybridBody1 = hybridBodies1.Item("FASTENING POINTS")
 
Dim hybridShapes1 As HybridShapes 
Set hybridShapes1 = hybridBody1.HybridShapes
 
Dim hybridShapePointOnCurve1 As HybridShape 
Set hybridShapePointOnCurve1 = hybridShapes1.Item("CENTER_PT")
 
Dim reference1 As Reference 
Set reference1 = part1.CreateReferenceFromObject(hybridShapePointOnCurve1)
 
Dim hybridShapeFactory1 As Factory 
Set hybridShapeFactory1 = part1.HybridShapeFactory
 
Dim parameters1 As Parameters 
Set parameters1 = part1.Parameters
 
Dim hybridShapeLineExplicit1 As Parameter 
Set hybridShapeLineExplicit1 = parameters1.Item("CENTER LINE")
 
Dim reference2 As Reference 
Set reference2 = part1.CreateReferenceFromObject(hybridShapeLineExplicit1)
 
Dim hybridShapeDirection1 As HybridShapeDirection 
Set hybridShapeDirection1 = hybridShapeFactory1.AddNewDirection(reference2)
 
Dim hybridShapeCylinder1 As HybridShapeCylinder 
Set hybridShapeCylinder1 = hybridShapeFactory1.AddNewCylinder(reference1, 27.000000, 40.000000, 40.000000, hybridShapeDirection1)
 
hybridShapeCylinder1.SymmetricalExtension = 0
 
Dim hybridBody2 As HybridBody 
Set hybridBody2 = hybridBodies1.Item("FASTENING DATA")
 
hybridBody2.AppendHybridShape hybridShapeCylinder1
 
part1.InWorkObject = hybridShapeCylinder1
 
part1.Update
 
End Sub

Go from the CATIA Geometry Creation Macro article back to the CATIA tutorial page.

Return to read more CATIA Macro Articles.