Hello.
Is there any available code to get all occurrences (ipts, iams) from all levels in top assembly?
Property "AllLeafOccurrences" exist, but it gets only all parts (ipts), without assemblies (iams).
Any ideas?
Best regards,
Rene Repina
Solved! Go to Solution.
Solved by ReneRepina. Go to Solution.
See the first rule in this article traverse assembly.
It is written in VBA so it's needs to be converted. Remove the word "Set", add sub Main to drive the sub routine and change debug.print to logger.info.
If you have any issues converting then post the problem area with the error messages.
Yes, a recursive loop.
VBA, this is used to set all parts in assembly to same Material.
Private Sub SetAllMat(ByVal oSubDoc As Inventor.Document, oMatName As String)
Dim oCompDoc As Inventor.PartDocument
Dim oRefDoc As Inventor.Document
For Each oRefDoc In oSubDoc.ReferencedDocuments
If oRefDoc.ReferencedDocuments.Count > 0 Then
Call SetAllMat(oRefDoc, oMatName)
End If
If oRefDoc.IsModifiable Then
If oRefDoc.DocumentType = kPartDocumentObject Then
Set oCompDoc = ThisApplication.Documents.Open(oRefDoc.FullFileName)
SetPartMaterial oCompDoc, oMatName
oCompDoc.Close
End If
End If
Next
End Sub
Hi @ReneRepina. Just one small additional tip... I am not sure what you are planning on doing with all of those components once you have access to them, but there is one other detail about 'how' you get them that might be important to the process. Once you have access to a true top level component (ComponentOccurrence), how you access its child components can sometimes be important, depending on your plans. If you use something like the following:
ComponentOccurrence.Definition.Occurrences
...then you are getting them from within its ComponentDefinition, which is like opening the assembly document that it references, then accessing its top level components, so they are all in the 'context' of that component's referenced AssemblyComponentDefinition. However, when you use the ComponentOccurrence.SubOccurrences to access them, they are all still essentially in the same 'context' as the component itself (top level of the assembly). By context I mean the 3D coordinate system space of a specific ComponentDefinition. If you are working with measurements, constraints, or any other inter component geometry relationships, then context is very important because both objects need to be in the same context for these types of actions to work. Also, iterating components (instead of iterating referenced documents) is the only way to control inter component context needed for those types of tasks.
Wesley Crihfield
(Not an Autodesk Employee)
Hello all.
Sorry for the late reply.
Firstly, thank you all for your suggestions.
I used a little bit simpler approach. I will explain it below.
The thing is, I also need assemblies in the collection, not just parts.
I am coloring them with specific colors, depended on which type of the custom property it has. It can be either IPT or IAM, so that why I need the whole collection.
This was the biggest challenge, on how to get whole collection from the top assembly. The struggles I encountered was that I was not able to get occurrences from sub-assemblies or I did get the occurrences, but not the assemblies.
Thank you for the explanation. I did not exacly try this as you suggested. I used a different approach explained below.
Used approach:
I did it in 2 steps. 1st step was to get all referenced documents from the top assembly, so that all IPTs and IAMs are included. When I had this collection, I iterated through all of them and filtered the needed ones out and used them later in code.
Best regards,
Rene Repina
Can't find what you're looking for? Ask the community or share your knowledge.