Parts List for "Sub-Assemblies" & "Sub Sub-Assemblies"

Parts List for "Sub-Assemblies" & "Sub Sub-Assemblies"

isocam
Collaborator Collaborator
5,098 Views
4 Replies
Message 1 of 5

Parts List for "Sub-Assemblies" & "Sub Sub-Assemblies"

isocam
Collaborator
Collaborator

Can anybody help???

 

I have an assembly (IAM) of a machine. This machine consists of many "Sub-Assemblies". Each "Sub-Assembly" could consist of any number of "Sub Sub-Assemblies)

 

Does anybody have a macro that can list EVERY part contained in every "sub assembly" and "Sub Sub-Assembly", that can be output to a simple text file?

 

Many thanks in advance!!!

 

IsoCAM

0 Likes
5,099 Views
4 Replies
Replies (4)
Message 2 of 5

Anonymous
Not applicable

IsoCam,

 

I'm actually kind-of surprised nobody answered this question.  Personally, I'm taking it one step further and printing off the location of each part, but I'm running into some problems on my end...

 

Regardless, this code 'should' work.  I didn't test it for your case, but it should work w/ some minor tweeking.

 

in Macros, add this code to a Module:

 

'CODE START ------------

'Option Explicit
Dim partOccur As Inventor.ComponentOccurrence
Dim assemblyDoc As Inventor.AssemblyDocument
Dim fileName As String

Sub printPart()
   Dim inventDoc As Inventor.AssemblyDocument
   Set inventDoc = ThisApplication.ActiveDocument
   fileName = inventDoc.DisplayName
   printPartCoord inventDoc
End Sub

Sub printPartCoord(assemblyObj As Object)
   Dim strg As String
   If TypeName(assemblyObj) = "AssemblyDocument" Then
      Set assemblyDoc = assemblyObj
      For Each partOccur In assemblyDoc.ComponentDefinition.Occurrences
         printPartCoord partOccur
      Next
   ElseIf TypeName(assemblyObj) = "ComponentOccurrence" Then
      For Each partOccur In assemblyObj.Definition.Occurrences
         If partOccur.Definition.SurfaceBodies.Count > 0 Then
            strg =
            Open "C:\Temp\" & fileName & ".txt" For Append As #1
            Print #1, strg
            Close #1
        Else
           printPartCoord partOccur
        End If
      Next
   End If
End Sub

'CODE END ----------------------

 

It should create a file at "C:\Temp" with a list of your parts.  Let me know if it compiles.

 

-lemensk

0 Likes
Message 3 of 5

ekinsb
Alumni
Alumni

If you look in the API help there is a sample called "Traverse an Assembly API Sample" that demonstrates traversing an assembly.


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

Anonymous
Not applicable

Ekins is refering to code similiar to the following (excerpt from http://modthemachine.typepad.com/files/mathgeometry.pdf )

 

'---------Start Code in Module -------------

 

Public Sub AssemblyTraversal()
   ' Get the active document, assuming it's an assembly.
   Dim oAsmDoc As AssemblyDocument
   Set oAsmDoc = ThisApplication.ActiveDocument
   ' Begin the assembly traversal.
   Call TraverseAsm(oAsmDoc.ComponentDefinition.Occurrences, 1)
End Sub
 
' The Level argument is used to control the amount of indent for the output.
Private Sub TraverseAsm(oOccurrences As ComponentOccurrences, Level As Integer)
   ' Iterate through the current list of occurrences.
   Dim oOcc As ComponentOccurrence
   For Each oOcc In oOccurrences
   ' Print the name of the current occurence.
      Debug.Print Space(Level * 3) & oOcc.Name
      ' If the current occurrence is a subassembly then call this sub
      ' again passing
      in the collection for the current occurrence.
      If oOcc.DefinitionDocumentType = kAssemblyDocumentObject Then
         Call TraverseAsm(oOcc.SubOccurrences, Level + 1)
      End If
   Next
End Sub

 

'---------End Code in Module -------------
 
"One thing to notice is that this example is not a single sub. Traversing an assembly of any depth requires recursion.  Recursion is when a sub or function calls itself. The TraverseAsm sub does most of the work in this program by traversing the occurrences within each level of the current assembly and when it  encounters a subassembly it calls itself to traverse that subassembly. This continues until it has gone  through every level of the assembly. The AssemblyTraversal sub just kicks off the traversal process."
0 Likes
Message 5 of 5

Anonymous
Not applicable

Below is my Answer to question.  Surprised how hard it was to get such simple code:

 

'CODE START ---------------------------------

Sub printParts()
   Dim inventDoc As Inventor.AssemblyDocument
   Dim fileName, strg As String

   Dim partOccur As Inventor.ComponentOccurrence

   Set inventDoc = ThisApplication.ActiveDocument
   fileName = inventDoc.DisplayName

   For Each partOccur In inventDoc.ComponentDefinition.Occurrences.AllLeafOccurrences   'For each part included in the assembly
       strg = partOccur.Name & ";" & partOccur.RangeBox.minPoint.X & ";" & partOccur.RangeBox.minPoint.Y & _

                 ";" & partOccur.RangeBox.minPoint.Z


      Open "C:\Temp\" & fileName & ".txt" For Append As #1 'Open txt file and Append
      Print #1, strg                                                                         'Print String
      Close #1                                                                                'Close txt file
   Next
   
End Sub


 

'CODE STOP ---------------------------------

0 Likes