- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
The follow error gets thrown immediately when I try to run the rule.
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.NullReferenceException: Object reference not set to an instance of an object.
at ThisRule..ctor()
--- End of inner exception stack trace ---
at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
at System.Activator.CreateInstance(Type type, Boolean nonPublic)
at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, StackCrawlMark& stackMark)
at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
at System.Reflection.Assembly.CreateInstance(String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
at Autodesk.iLogic.Exec.AppDomExec.CreateObjectWithInterface(Assembly a, String interfaceName)
at Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
at iLogic.RuleEvalContainer.ExecRuleEval(String execRule)
The code I try to run is as follows. Can someone help to debug this?
'Sub Routing to Define Sub Run Order
Sub Main()
CreateEmptyDictionary()
CreateFastenerDictionary()
CreateVirtualParts()
End Sub
'Public Declarations for use in all functions
Public Dim PartPropertiesFileAddr As String = "04_Part_iProperties.xlsm"
Public Dim FastenerPropertyFileAddr As String = "C:\Downloads\Fastener_Project_Properties.xlsm"
Public Dim oPartDictionary As New Dictionary(Of String, Dictionary(Of String, Integer))
Public Dim oAssy As AssemblyDocument = ThisDoc.Document
Public Dim oAsmCompDef As AssemblyComponentDefinition = oAssy.ComponentDefinition
'Prepopulates the oPartDictionary from an Excel Sheet master
Sub CreateEmptyDictionary
'Find Max Iterations in Parts Sheet
GoExcel.Open(PartPropertiesFileAddr,"Parts")
Dim PartsMaxIterations As Integer = GoExcel.CellValue("F1")
For Rows = 4 To PartsMaxIterations
'Compare the Material Setting to weed out non-screwed/fastened pieces
If GoExcel.CellValue("D" & Rows) = "Aluminum" Then
Dim PartName As String = GoExcel.CellValue("A" & Rows)
Dim EmptyDictionary As New Dictionary(Of String, Integer)
oPartDictionary.Add(PartName, EmptyDictionary)
End If
Next
GoExcel.Close
End Sub
Public Dim oFastDictionary As New Dictionary(Of String, Integer)
Sub CreateFastenerDictionary()
GoExcel.Open(FastenerPropertyFileAddr, "Fasteners")
oNumFast = GoExcel.CellValue("J" & 1)
oNumExtr = GoExcel.CellValue("F" & 1)
For extrusionID = 4 To NumExtr
' Holds name of Extrusion in Each Iteration
UnitExtrusion = GoExcel.CellValue("A" & extrusionID)
' If the unit has the extrusion checked in excel sheet, then fill out fastener dictionary
If oPartDictionary.Contains(UnitExtrusion) Then
For fastenerID = 2 To oNumFast
'Changes the Character for Clumn
FastenerCol = "A" + fastenerID - 1
FastQuantity = GoExcel.CellValue(FastenerCol & fastenerID)
If FastQuantity <> 0 Then
' Grab ID of every Fastener that does have a # of parts
Dim ofastenerName As String = GoExcel.CellValue(FastenerCol & 3)
oFastDictionary.Add(ofastenerName,FastQuantity)
End If
Next
'Skip to next fastener
End If
'End searching for the part
Next
GoExcel.Close
End Sub
'Loop through the assembly to check how many virtual parts are to be made
Sub CreateVirtualParts
'Once the Level 1 Dictionary is created, the FastenerDictionary needs to be generated
Dim i As Integer = 0 ' Holding Index Variable
Dim identity As Matrix = ThisApplication.TransientGeometry.CreateMatrix
' Loop through all the Parts
For Each oOcc As ComponentOccurrence In oAsmCompDef.Occurrences
'Only look to check if the part itself is not already virtual
If Not TypeOf oOcc.Definition Is VirtualComponentDefinition Then
On Error Resume Next
oDocFile = oOcc.Definition.Document
oDocFileName = oDocFile.displayname
'Set your delimiter
Delimiter = InStr(oDocFileName, "-")
Description = Left(oDocFileName, Delimiter - 1)
' Check if Part Dictionary contains the part for the model
If oPartDictionary.ContainsKey(Description) Then
' If it does have the Part, then get the Fastener dictionary and create the number of virtual parts
' This for loop uses the oOcc to find keys in the map instead of looping through the map
For Each oFastenerType As String In oPartDictionary(Description).Keys
For i = 1 To oPartDictionary(Description)(oFastenerType)
' Loop through each fastener type within the Fastener dictionary ^
' Virtual Part Parameters
FastenerName = oFastenerType & ":" & i
'oProp1 = oFastenerType
'Create a new component occurrence
Dim oNewOcc As ComponentOccurrence = oAsmCompDef.Occurences
virtOcc = oNewOcc.AddVirtual(FastenerName, identity)
'Create Virtual Component Definition
Dim oCVirtualCompDef As VirtualComponentDefinition
oCVirtualCompDef = oNewOcc.Definition
'iProperties.Value(FastenerName & ":1", "Project", "Description") = oProp1
i = i + 1
Next
Next
End If
End If
Next
End Sub
The excel document has the following template:
Solved! Go to Solution.