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.
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
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
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.
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.
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
Please feel free to contact if there is any doubt
I am using VBA so I will see if I can adapt your code and use it. Thanks for the post.
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