12.9.22

Recursive Function

This rule allows you to recursively search an assembly (at all levels). 

For each occurrence, it detects its Inventor document type (assembly, part, and so on). 

Based on the document type found, it executes the specified function.


Procedure:

- detect document type

- recursively search (all subassembly levels)

- execute functions based of actual occurrence document type

Code:

Private Sub Main

Dim oInvDoc As Inventor.Document = ThisDoc.Document

If oInvDoc.DocumentType = Inventor.DocumentTypeEnum.kAssemblyDocumentObject Then 
		RecursiveFunc(ThisDoc.Document)
Else
	MessageBox.Show("This is not Assembly (this Rule work only in assembly)", "Info msg")
End If


End Sub


Private Sub RecursiveFunc(ByVal oDoc As Inventor.Document)

Dim oAssy As Inventor.AssemblyDocument 
Dim oComp As Inventor.ComponentOccurrence 
Dim oSubDoc As Inventor.Document 
Dim oPart As Inventor.PartDocument
Dim oOocFullName As String

	'assembly document testing
If oDoc.DocumentType = Inventor.DocumentTypeEnum.kAssemblyDocumentObject Then 
    oAssy = CType(oDoc, AssemblyDocument) 
    For Each oComp In oAssy.ComponentDefinition.Occurrences 		
        oSubDoc = CType(oComp.Definition.Document, Document) 
		
        'part document testing
        If oSubDoc.DocumentType = Inventor.DocumentTypeEnum.kPartDocumentObject Then 
            Dim oPartDoc As PartDocument = oComp.Definition.Document	
            PartDocumentInfo(oPartDoc)						
        End If 	
		
        'subassembly document testing
        If oSubDoc.DocumentType = Inventor.DocumentTypeEnum.kAssemblyDocumentObject Then 			
            Dim oAsmDoc As AssemblyDocument = oComp.Definition.Document				
            AssemblyDocumentInfo(oAsmDoc)
				
            'recursive calling
            Call RecursiveFunc(oSubDoc) 
        End If  
    Next oComp 
End If 	
End Sub


Function AssemblyDocumentInfo(oDoc As Document)	
	Dim oName As String = oDoc.DisplayName
	MessageBox.Show(oName & " is Assembly", "Title")
End Function

Function PartDocumentInfo(oDoc As Document)	
	Dim oName As String = oDoc.DisplayName
	MessageBox.Show(oName & " is Part", "Title")
End Function

Žádné komentáře:

Okomentovat