Inventor VBA Part Center Point

Inventor VBA Part Center Point

Anonymous
Not applicable
1,342 Views
4 Replies
Message 1 of 5

Inventor VBA Part Center Point

Anonymous
Not applicable

I am hoping somebody can help me with this VBA sub.
I want to look through a large assembly model and pick out the parts based upon their node names (got that far)

Then I want to extract the X , Y , Z values of that parts origin (or center point as it's labeled in the feature tree)

so I can use them in a string:

 

 

Public Sub TP_POINTS1()

 

    Dim qty As Integer
    qty = 1


    ' Get the active assembly.
    Dim oAsmDoc As AssemblyDocument
    Set oAsmDoc = ThisApplication.ActiveDocument

 

    ' Get the assembly component definition.
    Dim oAsmDef As AssemblyComponentDefinition
    Set oAsmDef = oAsmDoc.ComponentDefinition

  

  ' Get all of the leaf occurrences of the assembly.
    Dim oLeafOccs As ComponentOccurrencesEnumerator
    Set oLeafOccs = oAsmDef.Occurrences.AllLeafOccurrences

  

  ' Iterate through the occurrences and print the name.
    Dim oOcc As ComponentOccurrence
    For Each oOcc In oLeafOccs
       
   
        'select only the parts that have the correct name
        Dim oName As String
        Dim strOrig As String
        Dim leftstring As String
       
        strOrig = oOcc.name
        leftstring = Left(strOrig, InStr(strOrig, ":"))
        oName = Right(leftstring, 5)
   
        If oName = "(TP):" Then
       
            '==============================
       
           
           
           
            '==============================
            qty = qty + 1
           
         End If
    Next
   
End Sub

0 Likes
Accepted solutions (1)
1,343 Views
4 Replies
Replies (4)
Message 2 of 5

marcin_otręba
Advisor
Advisor

you want x,y,z values but in assembly coordinates?

Hi, maybe you want to check my apps:


DrawingTools   View&ColoringTools   MRUFolders

0 Likes
Message 3 of 5

Anonymous
Not applicable

Yes. that's right.

0 Likes
Message 4 of 5

ekinsb
Alumni
Alumni
Accepted solution

Here's a modified version of your program that I believe does what you want.  I've tried to explain what it's doing in the comments but I know that it might be a little hard to understand.

 

Public Sub GetOriginPoints()
    ' Get the active assembly.
    Dim oAsmDoc As AssemblyDocument
    Set oAsmDoc = ThisApplication.ActiveDocument

    ' Get the assembly component definition.
    Dim oAsmDef As AssemblyComponentDefinition
    Set oAsmDef = oAsmDoc.ComponentDefinition

    ' Get all of the leaf occurrences of the assembly.
    Dim oLeafOccs As ComponentOccurrencesEnumerator
    Set oLeafOccs = oAsmDef.occurrences.AllLeafOccurrences

   ' Iterate through the occurrences and print the name.
    Dim oOcc As ComponentOccurrence
    For Each oOcc In oLeafOccs
        'select only the parts that have the correct name
        Dim strOrig As String
        strOrig = oOcc.Name
        
        Dim leftstring As String
        leftstring = Left(strOrig, InStr(strOrig, ":"))
        
        Dim oName As String
        oName = Right(leftstring, 5)
   
        If oName = "(TP):" Then
            ' The occurrence is returned as a ComponentOccurrence if it's in the
            ' top level of the assembly and as a ComponentOccurrenceProxy if it's in
            ' a sub assembly.  The proxy takes care of any transforms but the work
            ' features aren't available directly from the occurrence so we need to get
            ' them directly from the part and they'll be in part space.  We can then
            ' create a proxy of them to get them in the space of the top level assembly.
            Dim partDef As PartComponentDefinition
            Set partDef = oOcc.Definition
            
            Dim originWP As WorkPoint
            Set originWP = partDef.WorkPoints.Item(1)
            
            Dim originWPProxy As WorkPointProxy
            Call oOcc.CreateGeometryProxy(originWP, originWPProxy)
            
            Debug.Print oName & ": " & originWPProxy.Point.x & ", " & originWPProxy.Point.x & ", " & originWPProxy.Point.Z
         End If
    Next
End Sub

Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
Message 5 of 5

Anonymous
Not applicable

Ekinsb,

 

That code works great!

Thanks a lot!

0 Likes