How to get the origin of a hole
|Welcome to my new blog where I’ll be sharing more of my CATIA macro tips and tricks! To start things off I’ll show you how to get the origin of a hole in a CATPart.
Assumptions: a single CATPart is the ActiveDocument
To test this macro, setup your own example part:
- Create a new CATPart in PartDesign.
- Create a pad.
- Place a hole in the pad.
Now maybe you’re thinking, just getting the origin of a hole isn’t very useful. But doing this exercise will also show you how to set up a selection filter and an example of how to use SelectElement2.Plus, once you get the hole origin maybe there are additional things you can do with it. Follow along in the CATScript code with my comments.
Sub CATMain() 'get the selection object Dim sel Set sel = CATIA.ActiveDocument.Selection 'set up a selection filter Dim fil(0) fil(0) = "Hole" 'perform the selection Dim ans ans = sel.SelectElement2(fil, "Select a hole", False) 'check that selection was not cancelled If ans <> "Normal" Then Exit Sub 'get the hole Dim theHole As Hole Set theHole = sel.Item(1).Value Msgbox "Hole Name = " & theHole.Name 'need a variant object in order to use method returning array Dim vHole As Variant Set vHole = theHole 'get the origin of the hole using the variant object Dim origin(2) vHole.GetOrigin (origin) Msgbox "Origin = " & origin(0) & ", " & origin(1) & ", " & origin(2) End Sub
End Result:
Homework/Exercise:
Create a new point at the location of the hole origin with a macro.
One Comment
Hey Emmett. Thanks for the post. When I run this, I don’t get any errors, however, I can get the hole name and diameter to print out, but when I try to print the direction or origin, it prints blank strings. Here is my code and the output:
Public CATIA As Object
Sub Macro1()
Set CATIA = GetObject(, “CATIA.Application”)
Dim sel
Set sel = CATIA.ActiveDocument.Selection
Dim theHole As hole
Set theHole = sel.Item(1).Value
Debug.Print “Hole name ” & theHole.name
Debug.Print “Hole Diameter ” & theHole.Diameter.Value
Dim vHole As Variant
Set vHole = theHole
Dim origin(2)
vHole.GetOrigin (origin)
Debug.Print “Origin = ” & origin(0) & “, ” & origin(1) & “, ” & origin(2)
End Sub
Output:
Hole name Hole.1
Hole Diameter 10
Origin = , ,
Any help would be much appreciated!!! I am using Excel VBA for this.