I'm trying to call the iLogic snippets for Component.Add and Comstraints.AddMate in a method of a custom class inside my iLogic Rule and its giving me the error "The object reference was not set to an object instance.". I feel like i'm having similar problem to Alex from this post . I already tried to inherit from ThisRule which doesn't work (but changes the Snippet color to purple). Now i tried to adapt the solution from the other thread and pass ThisRule to my custom class which looks like this:
Sub Main()
Dim P7 As New AssemblyComponent(Me)
For counter = 0 To 2
P7.addComponentToAssembly()
Next
End Sub
Public Class AssemblyComponent : Inherits ThisRule
Dim partCount As Integer = 0
Dim mateCount As Integer = 0
Dim flushCount As Integer = 0
Public Sub addComponentToAssembly()
Me.partCount += 1
Dim seperator As String = ":"
Dim occurence As String = Me.name & seperator & CStr(Me.partCount)
Dim part = Components.Add(occurence, name & ".ipt")
Dim counter As Integer = 0
For Each item In constraintOptions
If item = "Mate" Then
mateCount += 1
Constraints.AddMate("Mate" & CStr(mateCount), "", Me.references(counter), occurence, Me.references(counter),
offset:=Me.offsets(counter))
ElseIf item = "Flush" Then
flushCount += 1
Constraints.AddFlush("Flush" & CStr(flushCount), "", Me.references(counter), occurence, Me.references(counter),
offset:=Me.offsets(counter))
End If
counter += 1
Next
End Sub
Dim ThisRule As ThisRule
Dim ThisDoc As Document
Dim Parameter As IParamDynamic
Dim MultiValue As IMultiValueParam
Dim ThisAssembly As IManagedAssembly
Dim Components As IManagedComponents
Dim Constraints As IManagedConstraints
Public Sub New(oThisRule As ThisRule)
ThisRule = oThisRule
ThisDoc = ThisRule.ThisDoc.Document
Parameter = ThisRule.Parameter
MultiValue = ThisRule.MultiValue
Components = ThisAssembly.Components
Constraints = ThisAssembly.Constraints
End Sub
End Class
Maybe someone has an idea or can tell me if i'm on a completly wrong track. I didn't get a definite answer from the thread i linked, that's why i'm asking here now. Thank you.
Solved! Go to Solution.
I'm trying to call the iLogic snippets for Component.Add and Comstraints.AddMate in a method of a custom class inside my iLogic Rule and its giving me the error "The object reference was not set to an object instance.". I feel like i'm having similar problem to Alex from this post . I already tried to inherit from ThisRule which doesn't work (but changes the Snippet color to purple). Now i tried to adapt the solution from the other thread and pass ThisRule to my custom class which looks like this:
Sub Main()
Dim P7 As New AssemblyComponent(Me)
For counter = 0 To 2
P7.addComponentToAssembly()
Next
End Sub
Public Class AssemblyComponent : Inherits ThisRule
Dim partCount As Integer = 0
Dim mateCount As Integer = 0
Dim flushCount As Integer = 0
Public Sub addComponentToAssembly()
Me.partCount += 1
Dim seperator As String = ":"
Dim occurence As String = Me.name & seperator & CStr(Me.partCount)
Dim part = Components.Add(occurence, name & ".ipt")
Dim counter As Integer = 0
For Each item In constraintOptions
If item = "Mate" Then
mateCount += 1
Constraints.AddMate("Mate" & CStr(mateCount), "", Me.references(counter), occurence, Me.references(counter),
offset:=Me.offsets(counter))
ElseIf item = "Flush" Then
flushCount += 1
Constraints.AddFlush("Flush" & CStr(flushCount), "", Me.references(counter), occurence, Me.references(counter),
offset:=Me.offsets(counter))
End If
counter += 1
Next
End Sub
Dim ThisRule As ThisRule
Dim ThisDoc As Document
Dim Parameter As IParamDynamic
Dim MultiValue As IMultiValueParam
Dim ThisAssembly As IManagedAssembly
Dim Components As IManagedComponents
Dim Constraints As IManagedConstraints
Public Sub New(oThisRule As ThisRule)
ThisRule = oThisRule
ThisDoc = ThisRule.ThisDoc.Document
Parameter = ThisRule.Parameter
MultiValue = ThisRule.MultiValue
Components = ThisAssembly.Components
Constraints = ThisAssembly.Constraints
End Sub
End Class
Maybe someone has an idea or can tell me if i'm on a completly wrong track. I didn't get a definite answer from the thread i linked, that's why i'm asking here now. Thank you.
Solved! Go to Solution.
Solved by Zach.Stauffer. Go to Solution.
Hi @nils.hassler. Just looking at it (can’t run it at the moment), you get most of the variables in your Sub New from the “oThisRule” you passed in, but not the “ThisAssembly” you use to get Components and Constraints. I’m guessing this is where the null reference is coming from. Try using “oThisRule.ThisAssembly” for those and see if that clears it up.
Hi @nils.hassler. Just looking at it (can’t run it at the moment), you get most of the variables in your Sub New from the “oThisRule” you passed in, but not the “ThisAssembly” you use to get Components and Constraints. I’m guessing this is where the null reference is coming from. Try using “oThisRule.ThisAssembly” for those and see if that clears it up.
Thanks for your reply. I hope I understood you right and this is what your solution looks like:
Public Sub New(oThisRule As ThisRule)
ThisRule = oThisRule
ThisDoc = ThisRule.ThisDoc.Document
Parameter = ThisRule.Parameter
MultiValue = ThisRule.MultiValue
Components= ThisRule.ThisAssembly.Components
Constraints = ThisRule.ThisAssembly.Constraints
End Sub
I tried it like this, but it raises an error saying "This assembly is not part of ThisRule"
Sub Main()
Dim P7 As New AssemblyComponent(Me, ThisAssembly)
End Sub
Public Sub New(oThisRule As ThisRule, oThisAssembly As ThisAssembly)
ThisRule = oThisRule
ThisDoc = ThisRule.ThisDoc.Document
Parameter = ThisRule.Parameter
MultiValue = ThisRule.MultiValue
ThisAssembly = oThisAssembly
Components= ThisRule.ThisAssembly.Components
Constraints = ThisRule.ThisAssembly.Constraints
End Sub
And I also tried passing ThisAssembly to the Sub New like this which also doesn't work.
Thanks for your reply. I hope I understood you right and this is what your solution looks like:
Public Sub New(oThisRule As ThisRule)
ThisRule = oThisRule
ThisDoc = ThisRule.ThisDoc.Document
Parameter = ThisRule.Parameter
MultiValue = ThisRule.MultiValue
Components= ThisRule.ThisAssembly.Components
Constraints = ThisRule.ThisAssembly.Constraints
End Sub
I tried it like this, but it raises an error saying "This assembly is not part of ThisRule"
Sub Main()
Dim P7 As New AssemblyComponent(Me, ThisAssembly)
End Sub
Public Sub New(oThisRule As ThisRule, oThisAssembly As ThisAssembly)
ThisRule = oThisRule
ThisDoc = ThisRule.ThisDoc.Document
Parameter = ThisRule.Parameter
MultiValue = ThisRule.MultiValue
ThisAssembly = oThisAssembly
Components= ThisRule.ThisAssembly.Components
Constraints = ThisRule.ThisAssembly.Constraints
End Sub
And I also tried passing ThisAssembly to the Sub New like this which also doesn't work.
Is there a reason you need the iLogic snippets, instead of just using the direct API calls from your class?
I would just get the active assembly document and/or the active application and pass that to your class. Then you use it in your class to call the functions you need. Admittedly the code becomes more complex, but at least it works. Short example:
Sub Main()
Dim assemblyDocument = ThisApplication.ActiveDocument
Dim P7 As New AssemblyComponent(ThisApplication, assemblyDocument)
For counter = 0 To 2
P7.addComponentToAssembly()
Next
End Sub
Public Class AssemblyComponent
Dim assemblyDoc As Inventor.AssemblyDocument
Dim app As Inventor.Application
Public Sub New(_app As Inventor.Application, _assemblyDoc As Inventor.AssemblyDocument)
this.assemblyDoc = _assemblyDoc
this.app = _app;
End Sub
Public Sub addComponentToAssembly()
Dim transientGeom As TransientGeometry = app.TransientGeometry
Dim partMatrix As Matrix = transientGeom.CreateMatrix
Dim newPartOccurrence As Inventor.ComponentOccurrence = assemblyDoc.ComponentDefinition.Occurrences.Add("filename", partMatrix)
End Sub
End Class
Is there a reason you need the iLogic snippets, instead of just using the direct API calls from your class?
I would just get the active assembly document and/or the active application and pass that to your class. Then you use it in your class to call the functions you need. Admittedly the code becomes more complex, but at least it works. Short example:
Sub Main()
Dim assemblyDocument = ThisApplication.ActiveDocument
Dim P7 As New AssemblyComponent(ThisApplication, assemblyDocument)
For counter = 0 To 2
P7.addComponentToAssembly()
Next
End Sub
Public Class AssemblyComponent
Dim assemblyDoc As Inventor.AssemblyDocument
Dim app As Inventor.Application
Public Sub New(_app As Inventor.Application, _assemblyDoc As Inventor.AssemblyDocument)
this.assemblyDoc = _assemblyDoc
this.app = _app;
End Sub
Public Sub addComponentToAssembly()
Dim transientGeom As TransientGeometry = app.TransientGeometry
Dim partMatrix As Matrix = transientGeom.CreateMatrix
Dim newPartOccurrence As Inventor.ComponentOccurrence = assemblyDoc.ComponentDefinition.Occurrences.Add("filename", partMatrix)
End Sub
End Class
Thank you! That's the way to go. I thought it would be easier using iLogic Snippets, but it turns out that using the API directly is a way better approach even though it is a little harder to understand in the beginning.
Thank you! That's the way to go. I thought it would be easier using iLogic Snippets, but it turns out that using the API directly is a way better approach even though it is a little harder to understand in the beginning.
Can't find what you're looking for? Ask the community or share your knowledge.