Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Find Occurrences including assemblies.

6 REPLIES 6
Reply
Message 1 of 7
shastu
2150 Views, 6 Replies

Find Occurrences including assemblies.

I have the code below which works great if you are looking for a part file:

 

Sub FindOcc()
Dim oDoc As Document
Set oDoc = ThisApplication.ActiveDocument
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
           If oOcc.Name = "8545-13:1" Then
        MsgBox ("wait")
        End If
    Next
End Sub

 

If 8545-13 is a part file then as soon as it would find the occurrence 8545-13:1 it would display the Msgbox.  However if 8545-13 was an assembly, it would never display the Msgbox because oOcc.Name never was 8545-13:1.

 

Is there any way to include both part files and assembly files in the cycle of occurrences?  Any help would be greatly appreciated.

 

thanks so much.

6 REPLIES 6
Message 2 of 7
Owner2229
in reply to: shastu

Hey, I guess this should do:

 

For Each oOcc In oLeafOccs
     Dim oName As String
     oName = oOcc.Name
     Dim iFP As Integer
     iFP = InStrRev(oName, ":", -1)
     If iFP > 1 Then
          oName = Left(oName, iFP - 1)
     End If
     If oName = "8545-13" Then 'Without the ":1"
          MsgBox ("wait")
     End If
Next
Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
Message 3 of 7
BrandonBG
in reply to: shastu

Can you clarify your assembly structure? Do you have parts and sub-assemblies with the same name, or parts within sub-assemblies with the same names?

 

In the API help file, search for "Traverse an Assembly API Sample". It may be of help.

 

Brandon

Message 4 of 7
chandra.shekar.g
in reply to: shastu

Hi shastu,

 

LeafOccurances will return all unique leaf occurrences (optionally filtered to the specified definition) relative to the context. Please go through the example explained in the following link. In this, leafoccurances are used to detect ipart.

 

https://forums.autodesk.com/t5/inventor-customization/ilogic-detect-ipart/td-p/5557485

 

Meanwhile, an example is explained in the current post. There are many methods to retrieve occurance.name. Out of that, here only two methods are explained in this example.

 

  1. Using AllLeafOccurances property
  2. Using Occurances property

 

 

Assembly_tree.JPG

 

The above image shows sample tree view of an assembly.

 

1.Using AllLeafOccurnaces property,

 

	Dim mApp As Inventor.Application = Nothing
        mApp = System.Runtime.InteropServices.Marshal.GetActiveObject("Inventor.Application")
        Dim assyDoc As AssemblyDocument
        assyDoc = mApp.ActiveDocument

        Dim leafOccc As ComponentOccurrencesEnumerator
        leafOccc = assyDoc.ComponentDefinition.Occurrences.AllLeafOccurrences

        For Each occ As ComponentOccurrence In leafOccc
            Console.WriteLine(occ.Name)
        Next

On execution of above code, the result would appear in the following image.

 

LeafOccurances_Result.JPG

2. Using Occurances Property, the differences are highlighted in red colour.

 

	Dim mApp As Inventor.Application = Nothing
        mApp = System.Runtime.InteropServices.Marshal.GetActiveObject("Inventor.Application")
        Dim assyDoc As AssemblyDocument
        assyDoc = mApp.ActiveDocument

        Dim leafOccc As ComponentOccurrences
        leafOccc = assyDoc.ComponentDefinition.Occurrences

        For Each occ As ComponentOccurrence In leafOccc
            Console.WriteLine(occ.Name)
        Next

On running the above code, the result would appear in the following image

 

Occurances_Result.JPG

 

Please feel free to contact if there is any doubt

 

 


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



Message 5 of 7
shastu
in reply to: Owner2229

Same problem.  If 8545-13 is a part then it works, but if it is an iam file then it doesn't.

Message 6 of 7
shastu
in reply to: chandra.shekar.g

I am using VBA so I will see if I can adapt your code and use it.  Thanks for the post.

Message 7 of 7
shastu
in reply to: chandra.shekar.g

Thanks so much for your detailed help.  I was able to translate it for VBA into this:

 

Sub FindOcc2()
Dim AssyDoc As AssemblyDocument
Set AssyDoc = ThisApplication.ActiveDocument
Dim LeafOcc As ComponentOccurrences
Set LeafOcc = AssyDoc.ComponentDefinition.Occurrences
    Dim oOccs As ComponentOccurrences
    Set oOccs = AssyDoc.ComponentDefinition.Occurrences

    For Each oOcc In oOccs
 MsgBox oOcc.Name
 

Next

End Sub

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report