I've been playing with some rules accessing the Inventor API. The rule is based on an addin I made that walks the assembly occurrences and deletes the occurrences that are invisible.
Module DeleteInvisible
Public Sub AssemblyDelete(ByVal ThisApplication As Inventor.Application)
' Set reference to active document.
' This assumes the active document is an assembly
Dim oDoc As Inventor.AssemblyDocument
oDoc = ThisApplication.ActiveDocument
' Get assembly component definition
Dim oCompDef As Inventor.ComponentDefinition
oCompDef = oDoc.ComponentDefinition
' Get all occurrences from component definition for Assembly document
Dim oCompOcc As Inventor.ComponentOccurrence
For Each oCompOcc In oCompDef.Occurrences
Call processAllSubOcc(oCompOcc) ' subassembly
Next
End Sub
' This function is called for processing sub assembly. It is called recursively
' to iterate through the entire assembly tree.
Private Sub processAllSubOcc(ByVal oCompOcc As Inventor.ComponentOccurrence)
Dim oSubCompOcc As Inventor.ComponentOccurrence
For Each oSubCompOcc In oCompOcc.SubOccurrences
If oSubCompOcc.Visible = False Then
oSubCompOcc.Delete()
End If
Next
End Sub
End Module
In the Intent rule I'm working on, I walk the assembly occurrences, but I can't seem to access the methods.
Uncached Rule DeleteInvisible As Any
Dim ivApp As Any = %%inventorapplication
Dim oDoc As Any = ivApp.ActiveDocument
Dim oCompDef As Any = oDoc.ComponentDefinition
Dim oCompOccCount As Any = oCompDef.Occurrences.Count
For i=1 To oCompOccCount
printValue(oCompDef.Occurrences.get_item(i).name)
Dim subComp As Any = oCompDef.Occurrences.get_item(i)
Dim subCompOccCount = subComp.SubOccurrences.Count
printValue(subComp.SubOccurrences.Count)
For ii=1 To subCompOccCount
If subComp.SubOccurrences.get_item(ii).visible = False
printValue(subComp.SubOccurrences.get_item(ii).name)
subComp.SubOccurrences.get_item(ii).delete()
End If
Next
Next
End Rule
The rule will out put the names of the invisible occurrences, but I get unknown method when the delete is called.
Any ideas would be appreciated.
Hi,
Please use the FOR loop with the negative step (starting from the end of collection), when deleting its elements.
Otherwise, you have to decrement the loop variable, which is not as clean.
Uncached Rule DeleteInvisible As Any Dim ivApp As Any = %%inventorapplication Dim oDoc As Any = ivApp.ActiveDocument Dim oCompDef As Any = oDoc.ComponentDefinition Dim oCompOccCount As Any = oCompDef.Occurrences.Count For i=oCompOccCount To 1 Step -1 printValue(oCompDef.Occurrences.get_item(i).name) Dim subComp As Any = oCompDef.Occurrences.get_item(i) Dim subCompOccCount = subComp.SubOccurrences.Count printValue(subComp.SubOccurrences.Count) For ii=subCompOccCount To 1 Step -1 If subComp.SubOccurrences.get_item(ii).visible Then printValue(subComp.SubOccurrences.get_item(ii).name) subComp.SubOccurrences.get_item(ii).delete() End If Next Next End Rule
Thanks Alex. That makes sense and I will make the change. The main problem is that the methods for the ComponentOccurenses class do not seem to be availble.