Unexpected error While running iLogic Code

Unexpected error While running iLogic Code

roelfoubert
Enthusiast Enthusiast
537 Views
5 Replies
Message 1 of 6

Unexpected error While running iLogic Code

roelfoubert
Enthusiast
Enthusiast

I get a unexpected error while running a iLogic rule in a big assembly.

 

Error on line 34 in rule: Quantity, in document: assembly.iam

Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL))

System.Runtime.InteropServices.COMException (0x80004005): Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL))
   at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
   at Inventor.PropertySet.Add(Object PropValue, Object Name, Object PropId)
   at iLogic.CadPropertiesInRule.InvPropertyInSets(PropertySets propSets, String setName, String propName, Boolean createCustom)
   at iLogic.CadPropertiesInRule.InvProperty(Document doc, String setName, String propName, Boolean createCustom)
   at iLogic.CadPropertiesInRule.set_Value(Object compoOrDocName, String setName, String propName, Object value)
   at ThisRule.PurgeBatchQTY(Document oDoc) in external rule: Quantity:line 34
   at ThisRule.Main() in external rule: Quantity:line 21
   at Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
   at iLogic.RuleEvalContainer.ExecRuleEval(String execRule)

 

This is the iLogic code I'm running:

Sub Main
    If ThisDoc.Document.DocumentType <> kAssemblyDocumentObject Then MsgBox("Rule can only run in assembly files!"): Exit Sub
    If ThisDoc.Document.FullFileName = "" Then MsgBox("Assembly not saved yet!" & vbLf & vbLf & "Aborting!"): Exit Sub
    
    Dim oGADoc As AssemblyDocument
    oGADoc = ThisDoc.Document
    
    BatchQTY = InputBox("ENTER TOTAL QTY OF ASSEMBLY", "ASSEMBLY QTY", "")
    iProperties.Value("Custom", "Aantal") = BatchQTY
    
    Dim oBOM As BOM
     oBOM = oGADoc.ComponentDefinition.BOM

    With oBOM
        .PartsOnlyViewEnabled = True
        .StructuredViewEnabled = True
        .StructuredViewFirstLevelOnly = False
    End With
    
    Call PurgeBatchQTY(oGADoc)
    
    Dim oBOMViewPO As BOMView = oBOM.BOMViews.Item("Parts Only")
    Call SetBatchPartQTYs(oBOMViewPO, BatchQTY)
    
    Dim oBOMViewStruc As BOMView = oBOM.BOMViews.Item("Structured")
    Call SetBatchAssemblyQTYs(oBOMViewStruc.BOMRows, BatchQTY)

End Sub

Sub PurgeBatchQTY(oDoc As Document)
    For Each oSubDoc In oDoc.AllReferencedDocuments
        If oSubDoc.IsModifiable = True Then
            iProperties.Value(System.IO.Path.GetFileName(oSubDoc.FullFileName), "Custom", "Aantal") = 0
        End If
    Next
End Sub

Sub SetBatchPartQTYs(ByVal oBOMViewPO As BOMView, ByVal BatchQTY As Integer)

    For Each oBOMRowPO In oBOMViewPO.BOMRows
        oCompDef = oBOMRowPO.ComponentDefinitions.Item(1)
        oRowDoc = oCompDef.Document
        
        Dim Qty As Integer = oBOMRowPO.TotalQuantity
        
        If oRowDoc.IsModifiable = True Then
            iProperties.Value(System.IO.Path.GetFileName(oRowDoc.FullFileName), "Custom", "Aantal") = Qty * BatchQTY
        End If
    Next
    
End Sub

Private Sub SetBatchAssemblyQTYs(oBOMRows As BOMRowsEnumerator, oParentQty As Integer)
    
    Dim oQty As Integer
    
    For Each oBOMRowStruct In oBOMRows
        oCompDef = oBOMRowStruct.ComponentDefinitions.Item(1)
        oRowDoc = oCompDef.Document
        
        'Single line Check + Continues:
        If Not (TypeOf oCompDef Is AssemblyComponentDefinition And oCompDef.BOMStructure = BOMStructureEnum.kNormalBOMStructure) Then Continue For
        If oRowDoc.IsModifiable = False Then Continue For
        'Resume functionality
        
        oQty = oBOMRowStruct.ItemQuantity * oParentQty
            
        iProperties.Value(System.IO.Path.GetFileName(oRowDoc.FullFileName), "Custom", "Aantal") = oQty + iProperties.Value(System.IO.Path.GetFileName(oRowDoc.FullFileName), "Custom", "Aantal")
            
        If Not (oBOMRowStruct.ChildRows Is Nothing) Then
            Call SetBatchAssemblyQTYs(oBOMRowStruct.ChildRows, oQty)
        End If
    Next
End Sub 

 The script works perfectly in small assemblies but has the error when I use it in a big assembly (+10000 occurrences)

0 Likes
538 Views
5 Replies
Replies (5)
Message 2 of 6

Michael.Navara
Advisor
Advisor

It is hard to test your code if it works on small assemblies. But internally the code fails on 

 

at Inventor.PropertySet.Add(Object PropValue, Object Name, Object PropId)

 

Which is Inventor API call for creating new iProperty.

You can rework the piece of code

 

iProperties.Value(System.IO.Path.GetFileName(oRowDoc.FullFileName), "Custom", "Aantal") = Qty * BatchQTY

 

to full Inventor API and log as much as possible when the exception raises (document, property name, property value, etc.).

 

'iProperties.Value(System.IO.Path.GetFileName(oRowDoc.FullFileName), "Custom", "Aantal") = Qty * BatchQTY

Dim oRowDoc As Document 'Defined above
Dim userDefinedPropertiesId As String = "{D5CDD505-2E9C-101B-9397-08002B2CF9AE}"
Dim propName As String = "Aantal"

Dim value As Object = Qty * BatchQTY 'Defined above
Dim qtyProp As Inventor.Property
Try
	qtyProp = oRowDoc.PropertySets(userDefinedPropertiesId)(propName)
	qtyProp.Value = value
Catch
	Try
		qtyProp = oRowDoc.PropertySets(userDefinedPropertiesId).Add(value, propName)
	Catch ex As Exception
		Logger.Error(ex.Message)
		Logger.Error(ex.ToString())
		Logger.Error(oRowDoc.DisplayName)
		Logger.Error(oRowDoc.FullFileName)
		Logger.Error(value)
	End Try
End Try
0 Likes
Message 3 of 6

roelfoubert
Enthusiast
Enthusiast

I gives the same error at the same place.

Sub Main
    If ThisDoc.Document.DocumentType <> kAssemblyDocumentObject Then MsgBox("Rule can only run in assembly files!"): Exit Sub
    If ThisDoc.Document.FullFileName = "" Then MsgBox("Assembly not saved yet!" & vbLf & vbLf & "Aborting!"): Exit Sub
    
    Dim oGADoc As AssemblyDocument
    oGADoc = ThisDoc.Document
    
    BatchQTY = InputBox("ENTER TOTAL QTY OF ASSEMBLY", "ASSEMBLY QTY", "")
    iProperties.Value("Custom", "Aantal") = BatchQTY
    
    Dim oBOM As BOM
     oBOM = oGADoc.ComponentDefinition.BOM

    With oBOM
        .PartsOnlyViewEnabled = True
        .StructuredViewEnabled = True
        .StructuredViewFirstLevelOnly = False
    End With
    
    Call PurgeBatchQTY(oGADoc)
    
    Dim oBOMViewPO As BOMView = oBOM.BOMViews.Item("Parts Only")
    Call SetBatchPartQTYs(oBOMViewPO, BatchQTY)
    
    Dim oBOMViewStruc As BOMView = oBOM.BOMViews.Item("Structured")
    Call SetBatchAssemblyQTYs(oBOMViewStruc.BOMRows, BatchQTY)

End Sub

Sub PurgeBatchQTY(oDoc As Document)
    For Each oSubDoc In oDoc.AllReferencedDocuments
        If oSubDoc.IsModifiable = True Then
            iProperties.Value(System.IO.Path.GetFileName(oSubDoc.FullFileName), "Custom", "Aantal") = 0
        End If
    Next
End Sub

Sub SetBatchPartQTYs(ByVal oBOMViewPO As BOMView, ByVal BatchQTY As Integer)

    For Each oBOMRowPO In oBOMViewPO.BOMRows
        oCompDef = oBOMRowPO.ComponentDefinitions.Item(1)
        oRowDoc = oCompDef.Document
        
        Dim Qty As Integer = oBOMRowPO.TotalQuantity
        
        If oRowDoc.IsModifiable = True Then
            'iProperties.Value(System.IO.Path.GetFileName(oRowDoc.FullFileName), "Custom", "Aantal") = Qty * BatchQTY
Dim userDefinedPropertiesId As String = "{D5CDD505-2E9C-101B-9397-08002B2CF9AE}"
Dim propName As String = "Aantal"

Dim value As Object = Qty * BatchQTY 'Defined above
Dim qtyProp As Inventor.Property
Try
	qtyProp = oRowDoc.PropertySets(userDefinedPropertiesId)(propName)
	qtyProp.Value = value
Catch
	Try
		qtyProp = oRowDoc.PropertySets(userDefinedPropertiesId).Add(value, propName)
	Catch ex As Exception
		Logger.Error(ex.Message)
		Logger.Error(ex.ToString())
		Logger.Error(oRowDoc.DisplayName)
		Logger.Error(oRowDoc.FullFileName)
		Logger.Error(value)
	End Try
End Try
        End If
    Next
    
End Sub

Private Sub SetBatchAssemblyQTYs(oBOMRows As BOMRowsEnumerator, oParentQty As Integer)
    
    Dim oQty As Integer
    
    For Each oBOMRowStruct In oBOMRows
        oCompDef = oBOMRowStruct.ComponentDefinitions.Item(1)
        oRowDoc = oCompDef.Document
        
        'Single line Check + Continues:
        If Not (TypeOf oCompDef Is AssemblyComponentDefinition And oCompDef.BOMStructure = BOMStructureEnum.kNormalBOMStructure) Then Continue For
        If oRowDoc.IsModifiable = False Then Continue For
        'Resume functionality
        
        oQty = oBOMRowStruct.ItemQuantity * oParentQty
            
        iProperties.Value(System.IO.Path.GetFileName(oRowDoc.FullFileName), "Custom", "Aantal") = oQty + iProperties.Value(System.IO.Path.GetFileName(oRowDoc.FullFileName), "Custom", "Aantal")
            
        If Not (oBOMRowStruct.ChildRows Is Nothing) Then
            Call SetBatchAssemblyQTYs(oBOMRowStruct.ChildRows, oQty)
        End If
    Next
End Sub 
0 Likes
Message 4 of 6

Michael.Navara
Advisor
Advisor
0 Likes
Message 5 of 6

roelfoubert
Enthusiast
Enthusiast

Where can I find or see the log file?

0 Likes