Loop on all parts and then after its occurrences

Loop on all parts and then after its occurrences

Damian_Apex
Enthusiast Enthusiast
1,058 Views
7 Replies
Message 1 of 8

Loop on all parts and then after its occurrences

Damian_Apex
Enthusiast
Enthusiast

Hello,

I am looking for code which will loop for all items and then for all occurrences of this part.

 

Sub main
InvDoc = ThisDoc.Document
Dim refDocs As DocumentsEnumerator = InvDoc.AllReferencedDocuments
Dim refDoc As Document

For Each refDoc In refDocs 'loop for all files
	FileName = refDoc.FullDocumentName.Substring(refDoc.FullDocumentName.LastIndexOf("\") + 1)	
	MessageBox.Show(FileName, "Title")
	
	'Loop for all occurances in "FileName" 
	'example:
	'Part1:1
	'Part1:2
	'Part1:3
	'Part1:4

Next
End Sub

 

0 Likes
1,059 Views
7 Replies
Replies (7)
Message 2 of 8

CattabianiI
Collaborator
Collaborator

I would drop the AllReferencedDocuments approach and go for a recursive iteration over occurrences.
Check this article: https://modthemachine.typepad.com/my_weblog/2009/03/accessing-assembly-components.html
and give this code a try:

Sub Main()
    ' Get the active assembly.
    Dim oAsmDoc As AssemblyDocument
    oAsmDoc = ThisApplication.ActiveDocument
    Debug.Print oAsmDoc.DisplayName

    ' Call the function that does the recursion.
    TraverseAssembly(oAsmDoc.ComponentDefinition.Occurrences, 1)
End Sub

Private Sub TraverseAssembly(Occurrences As ComponentOccurrences, _
                             Level As Integer)
    ' Iterate through all of the occurrence in this collection.  This
    ' represents the occurrences at the top level of an assembly.
    Dim oOcc As ComponentOccurrence
    For Each oOcc In Occurrences
        ' Print the name of the current occurrence.
        Debug.Print Space(Level * 3) & oOcc.Name

        ' Check to see if this occurrence represents a subassembly
        ' and recursively call this function to traverse through it.
        If oOcc.DefinitionDocumentType = kAssemblyDocumentObject Then
            TraverseAssembly(oOcc.SubOccurrences, Level + 1)
        End If
    Next
End Sub

 

0 Likes
Message 3 of 8

Damian_Apex
Enthusiast
Enthusiast

Thank you for reply, but I am looking for something like this

example:

Part1.ipt

     Part1:1

     Part1:2

Part2.ipt

     Part2:1

     Part2:2 [...]

 

0 Likes
Message 4 of 8

CattabianiI
Collaborator
Collaborator

What is the form of your assembly? If the assembly is something like the following one, what do you expect your code does?

Asm1.iam
  part1:1
  Asm2:1
    part1:1
    part3:1
  part2:1
  part2:2

In a very non optimezed way you could iterate the AllReferencedDocuments and then traverse all assembly with the code posted above looking for the right occurrences with something like that:
if refDoc.FullDocumentName = oOcc.Definition.Document.FullDocumentName then
    ' Print the name of the current occurrence.
    Debug.Print Space(Level * 3) & oOcc.Name
end if

0 Likes
Message 5 of 8

Damian_Apex
Enthusiast
Enthusiast

I want my code to:

    Loop for all parts (part1.ipt)

           Loop for all occurrences of this part (part1:1; part1:2 [...])

 

I want this code because, I want to operate on parts and on occurrences of that part

0 Likes
Message 6 of 8

CattabianiI
Collaborator
Collaborator


Add all occurrences in a list with the TraverseAssembly of the blog post.
Loop allreferencedparts
   Loop list of occurrences 
      check if it is an occ of this part and do what you want

Those are very common API, try to write some code with these guidelines and the codelines I posted above, this is the best way to learn these things.

0 Likes
Message 7 of 8

Damian_Apex
Enthusiast
Enthusiast

I managed to do it, so sharing the code may be useful to someone as well.

 

Sub main
Dim oDoc As Document = ThisDoc.Document
Dim oDef As AssemblyComponentDefinition = oDoc.ComponentDefinition
Dim oCompOcc As Inventor.ComponentOccurrence
Dim oDocOcc As Document  
Dim FileName As String

For Each oCompOcc In oDef.Occurrences
	oDocOcc = oCompOcc.Definition.Document
	FileName = oDocOcc.FullDocumentName.Substring(oDocOcc.FullDocumentName.LastIndexOf("\") + 1)
	Trace.WriteLine("Occurance name: " & oCompOcc.Name)	
	Trace.WriteLine("FileName: " & FileName)
	Trace.WriteLine("-----------------")
Next
End Sub
0 Likes
Message 8 of 8

CattabianiI
Collaborator
Collaborator

👍 That is not exactly the same to loop documents and their occurrences but good it solves your question.
Few things:
- Keep in mind this is not recursive, Occurrences property only  returns the top-level occurrences.
oDocOcc.FullDocumentName returns the full file name concatenated with the model state / LOD name
- To get the file name I suggest to use this API: FileName = IO.Path.GetFileName(oDocOcc.FullDocumentName)