Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Using iLogic Snippets inside a Custom Class

4 REPLIES 4
SOLVED
Reply
Message 1 of 5
nils.hassler
373 Views, 4 Replies

Using iLogic Snippets inside a Custom Class

nils.hassler
Explorer
Explorer

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.

0 Likes

Using iLogic Snippets inside a Custom Class

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.

Tags (2)
Labels (1)
4 REPLIES 4
Message 2 of 5
DRoam
in reply to: nils.hassler

DRoam
Mentor
Mentor

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.

0 Likes

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.

Message 3 of 5
nils.hassler
in reply to: DRoam

nils.hassler
Explorer
Explorer

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.

 

0 Likes

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.

 

Message 4 of 5

Zach.Stauffer
Advocate
Advocate
Accepted solution

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

 

0 Likes

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

 

Message 5 of 5

nils.hassler
Explorer
Explorer

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.

Post to forums  

Autodesk Design & Make Report