For each in sub assembly

For each in sub assembly

jaimievandamme1
Participant Participant
277 Views
4 Replies
Message 1 of 5

For each in sub assembly

jaimievandamme1
Participant
Participant

Hello everyone,

I am unable to have all parts and sub-assembly as objects in an assembly.

see code. I always get a wood notification on the 2nd For Each.

can anyone help me?

 



'   Dim naam_project As String = InputBox("naam project", "geef de naam van het project", "")

   
Dim asmDoc As AssemblyDocument
asmDoc = ThisApplication.ActiveDocument

If asmDoc.DocumentType = kAssemblyDocumentObject Then



    Dim PTN_TRADEMARK As String
   
    Dim naam_project As String
    naam_project = InputBox("naam project", "geef de naam van het project", "")

    Dim oFolderPath As String
    oFolderPath = ("C:\$WorkingFolder\PDF_AND_STEP\" & naam_project & "\")
    'MessageBox.Show("oFolderPath" & oFolderPath, "Title")

   
    Dim oVaultPath As String
   
    Dim oFileDownloadedSuccessfully As Boolean
   
    Dim oRuleArguments As Inventor.NameValueMap
   
    'Dim foutmeldingen As New ArrayList


     'Begin met de hoofdassemblage
    Dim asmDef As AssemblyComponentDefinition
    asmDef = asmDoc.ComponentDefinition
   
    Dim Occ As ComponentOccurrence
    For Each Occ In asmDef.Occurrences
       

        Dim subOcc As ComponentOccurrence
        For Each subOcc In Occ.SubOccurrences
           
            Dim subsubOcc As ComponentOccurrence
            For Each subsubOcc In subOcc.SubOccurrences
               
               
                Dim subsubsubOcc As ComponentOccurrence
                For Each subsubsubOcc In subsubOcc.SubOccurrences
                   

 
                Next
 
             Next

        Next

    Next
   
    'MessageBox.Show(foutmeldingen, "Title")

   
   
Else
    MessageBox.Show("Het actieve document is geen samenstelling (assembly).", "error")
End If
0 Likes
278 Views
4 Replies
Replies (4)
Message 2 of 5

Frederick_Law
Mentor
Mentor

I use recursive loop to go through all sub-assemblies:

Sub SaveAsmThumbnail()

Dim oDoc As Inventor.Document
Dim oRefDoc As Inventor.Document
Dim oCompDoc As Inventor.Document
Set oDoc = ThisApplication.ActiveDocument
SaveThumbnail
For Each oRefDoc In oDoc.ReferencedDocuments
    If oRefDoc.IsModifiable Then
        Set oCompDoc = ThisApplication.Documents.Open(oRefDoc.FullFileName)
        SaveThumbnail
        If oCompDoc.ReferencedDocuments.Count > 0 Then
            Call procAllThumb(oRefDoc)
        End If
        oCompDoc.Close
    End If
Next
End Sub

Private Sub procAllThumb(ByVal oSubDoc As Inventor.Document)
    
    Dim oCompDoc As Inventor.Document
    Dim oRefDoc As Inventor.Document
    For Each oRefDoc In oSubDoc.ReferencedDocuments
        If oRefDoc.ReferencedDocuments.Count > 0 Then
            Call procAllThumb(oRefDoc)
        End If
'Do what you need here
        If oRefDoc.IsModifiable Then
        Set oCompDoc = ThisApplication.Documents.Open(oRefDoc.FullFileName)
        SaveThumbnail
        oCompDoc.Close
'Replace above code
        End If
    Next
End Sub
Message 3 of 5

WCrihfield
Mentor
Mentor

Here is another example of a recursive Sub routine that can be used in an iLogic rule to iterate down through all levels of assembly components.  You just need to fill in some code for what you want to do with each component down within that custom Sub routine, where indicated.  I left a few lines in there, but commented out, just in case they may be useful to you.

Sub Main
	If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then Exit Sub
	Dim oADoc As AssemblyDocument = ThisDoc.Document
	Dim oOccs As ComponentOccurrences = oADoc.ComponentDefinition.Occurrences
	RecurseComponents(oOccs)
	If oADoc.RequiresUpdate Then oADoc.Update2(True)
	'If oADoc.Dirty Then oADoc.Save2(True)
End Sub

Sub RecurseComponents(oComps As ComponentOccurrences)
	If oComps Is Nothing OrElse oComps.Count = 0 Then Exit Sub
	For Each oComp As ComponentOccurrence In oComps
		If oComp.Suppressed Then Continue For 'if suppressed, you can not do much with it without error
		'If TypeOf oComp.Definition Is VirtualComponentDefinition Then Continue For 'virtual component
		'If TypeOf oComp.Definition Is WeldsComponentDefinition Then Continue For 'welds in Weldment type assembly
		
		'<<< add code here for doing something with this component >>>
		
		If oComp.Suppressed = False AndAlso _ 'can not access the Definition of a suppressed component
			oComp.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
			'use this next line when proxy connection to top assembly is not important
			RecurseComponents(oComp.Definition.Occurrences)
			'or (not also) use this next line instead to help maintain proxy connection to top assembly
			'RecurseComponents(oComp.SubOccurrences)
		End If
	Next
End Sub

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 4 of 5

jaimievandamme1
Participant
Participant

tnx, its works. but i need to have acces to the assembly object where the part is in.

 

so oComps is a object whith multiple objects. but i need also the object of te assem where all te oComps are in.

0 Likes
Message 5 of 5

Frederick_Law
Mentor
Mentor

 

        If oRefDoc.ReferencedDocuments.Count > 0 Then
'This oRefDoc could be assembly or derived
'Check file type
            Call procAllThumb(oRefDoc)
        End If

 

0 Likes