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.
I created a marcro document and run it to rename a set of points as shown below but didn’t work. Would you please help me to fix it? I’d appreciated very much.
Sub CATMain()
PtString = Pt
n = 100
For i = 0 To X.HybridShapes.Count – 1
ptName = X.HybridShapes.Item(i + 1).Name
StartPos = InStr(1, ptName, “-“, vbTextCompare)
NewPtName = Left(ptName, StartPos) & PtString & i + n
X.HybridShapes.Item(i + 1).Name = NewPtName
Next
End Sub
Best Regards,
Danny Nguyen
Hi Danny. What is X? What error message and on what line? Thanks, Emmett
HI,
i needed to create dimensions on a drawing platform for a generated view
Hi,
I am trying to create a macro with geometry creation but I need an input function to select the curve(s) where the cylinder needs to be created. Could you suggest a way to get an input box?
Hi. Use SelectElement. Here’s an excerpt from my book, VB Scripting for CATIA V5:
Pre-selection requires a user to select elements in CATIA before the macro is initiated. Use SelectElement.
Selection while running: There are three different methods to allow a user to make a selection while the macro is running and each is found under the Selection object.
SelectElement2: only 1 item will be selected
SelectElement3: multiple selections are required
SelectElement4: single or multiple selections from non-active documents
Hope that helps.
SelectElement2 example to select a point:
‘create the object array ReDim strArray(0) strArray(0)=Point
‘create a message
Dim msg1 As String
Msg1=”Select a point or select UNDO to cancel.”
‘run the selection code Dim myPoint As String
myPoint=sSelection.SelectElement2(strArray, msg1, False)
If(msg1=UNDO) Or (msg1 = Cancel) Then
MsgBox You have chosen to Undo this operation.
Exit Sub
End If
‘set the object that the user selected
Set myObject = sSelection.Item(1).Value
Heck yeah this is exltcay what I needed.
Have you created any VBScripts for manufacturing?
A few. What are you looking for?
Hi dears
who could send me an example of a macro contain a loop in CATIA
Thanks
Just search this site and you will find plenty of macros that use loops.
Hello, I need help in this macro creation. Can you please help me to solve this problem.
Task – Creating a Plane which is offset from a particular surface of already created part. So In normal procedure we select option of create plane then offset from surface and then select surface from which offset plane is required and the distance. but
“I want to create a macro in which while running only it will ask an user to “select the surface from which offset plane is required” and then user will select a surface and the further macro will run automatically and will take that user selected surface as a reference in plane creation. ”
Thanks in advance.
Hi
I want to draw a hexagon but unable to proceed can you guide on it me
Thanks Amit
HI, I am looking for an example of macro where I have to input the points from excel, make a sketch and use the pad to make simple geometric shape automatically. Could any one guide me ?
hi,
how can I loop the below written code to create points at equal intervals? It has to increase only in Z-axis.
Set hybridShapePointCoord1 = hybridShapeFactory1.AddNewPointCoord(0#, 0#, 0#)
Set hybridBodies1 = part1.HybridBodies
Set hybridBody1 = hybridBodies1.Item(“Instance”)
hybridBody1.AppendHybridShape hybridShapePointCoord1
part1.InWorkObject = hybridShapePointCoord1
part1.Update
Set hybridShapePointCoord2 = hybridShapeFactory1.AddNewPointCoord(0#, 0#, 500#)
Set reference1 = part1.CreateReferenceFromObject(hybridShapePointCoord1)
hybridShapePointCoord2.PtRef = reference1
hybridBody1.AppendHybridShape hybridShapePointCoord2
part1.InWorkObject = hybridShapePointCoord2
part1.Update
Set hybridShapePointCoord3 = hybridShapeFactory1.AddNewPointCoord(0#, 0#, 500#)
Set reference2 = part1.CreateReferenceFromObject(hybridShapePointCoord2)
hybridShapePointCoord3.PtRef = reference2
hybridBody1.AppendHybridShape hybridShapePointCoord3
part1.InWorkObject = hybridShapePointCoord3
part1.Update
Set hybridShapePointCoord4 = hybridShapeFactory1.AddNewPointCoord(0#, 0#, 500#)
Set reference3 = part1.CreateReferenceFromObject(hybridShapePointCoord3)
hybridShapePointCoord4.PtRef = reference3
hybridBody1.AppendHybridShape hybridShapePointCoord4
part1.InWorkObject = hybridShapePointCoord4
part1.Update