Remove or Replace Event Listener after iLogic rule completes runtime

Remove or Replace Event Listener after iLogic rule completes runtime

Anonymous
Not applicable
1,699 Views
17 Replies
Message 1 of 18

Remove or Replace Event Listener after iLogic rule completes runtime

Anonymous
Not applicable

I'll admit I'm new to events. The iLogic code below works as expected (gives name of constraint or Joint after it is created).

 

 

 

Option Explicit

Sub Main
    Dim evtListener As New MateListener(ThisServer, Parameter)
End Sub

Public Class MateListener
	
    Private WithEvents m_AsmEvts As Inventor.AssemblyEvents = Nothing
	
    Private Parameter As Object
	
    Sub New(invServer As InventorServer, Parameter As Object)
        m_AsmEvts = invServer.AssemblyEvents
        Me.Parameter = Parameter
    End Sub

    Private Sub m_AsmEvts_OnNewRelation(ByVal DocumentObject As Inventor._AssemblyDocument, ByVal Relationship As Object, ByVal BeforeOrAfter As Inventor.EventTimingEnum, ByVal Context As Inventor.NameValueMap, ByRef HandlingCode As Inventor.HandlingCodeEnum) Handles m_AsmEvts.OnNewRelationship
        If BeforeOrAfter = Inventor.EventTimingEnum.kAfter Then
			MsgBox("new relationship:" & vbTab & Relationship.Name)
			
        End If
    End Sub
	
End Class

 

My problem is a new event listener is created each time I run the rule. How to I delete or overwrite the original listener? Can I delete all OnNewRelationship listeners?

 

The only way to remove the listener is to close inventor. I need to add a lot more code to this so this makes troubleshooting impossible.

 

I have read about using RemoveHandler but after the rule has run I don't have the object any more. Is there a list/collection of events?

RemoveHandler m_AppEvents.OnActivateDocument, AddressOf ApplicationEvents_OnActivateDocument

http://modthemachine.typepad.com/my_weblog/2013/07/inventor-events-using-net-3-examples.html

https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/removehandler-sta...

 

0 Likes
1,700 Views
17 Replies
Replies (17)
Message 2 of 18

bradeneuropeArthur
Mentor
Mentor

Sub Main
Dim evtListener As New MateListener(ThisServer, Parameter)

Evtlistener =nothing
End Sub

Regards,

Arthur Knoors

Autodesk Affiliations & Links:
blue LinkedIn LogoSquare Youtube Logo Isolated on White Background


Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:
Drawing List!|
Toggle Drawing Sheet!|
Workplane Resize!|
Drawing View Locker!|
Multi Sheet to Mono Sheet!|
Drawing Weld Symbols!|
Drawing View Label Align!|
Open From Balloon!|
Model State Lock!
Posts and Ideas:
My Ideas|
Dimension Component!|
Partlist Export!|
Derive I-properties!|
Vault Prompts Via API!|
Vault Handbook/Manual!|
Drawing Toggle Sheets!|
Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !


 


EESignature

0 Likes
Message 3 of 18

Anonymous
Not applicable

Thanks for trying but I need to delete the listener each time the rule is run, before it creates a new instance.

 

Is anyone aware of a list or collection of event listeners?

0 Likes
Message 4 of 18

bradeneuropeArthur
Mentor
Mentor

Think I solve it for you;

 

for me it is working at least:

Nothing.PNG

Option Explicit

 

Sub Main
    Dim evtListener As New MateListener(ThisServer, Parameter)
	evtListener = Nothing 
End Sub

Public Class MateListener
	
    Private WithEvents m_AsmEvts As Inventor.AssemblyEvents = Nothing
	
    Private Parameter As Object
	
    Sub New(invServer As InventorServer, Parameter As Object)
        m_AsmEvts = invServer.AssemblyEvents
        Me.Parameter = Parameter
    End Sub

    Private Sub m_AsmEvts_OnNewRelation(ByVal DocumentObject As Inventor._AssemblyDocument, ByVal Relationship As Object, ByVal BeforeOrAfter As Inventor.EventTimingEnum, ByVal Context As Inventor.NameValueMap, ByRef HandlingCode As Inventor.HandlingCodeEnum) Handles m_AsmEvts.OnNewRelationship
        If BeforeOrAfter = Inventor.EventTimingEnum.kAfter Then
			MsgBox("new relationship:" & vbTab & Relationship.Name)
			m_AsmEvts = Nothing
        End If
    End Sub
	
End Class

Regards,

Arthur Knoors

Autodesk Affiliations & Links:
blue LinkedIn LogoSquare Youtube Logo Isolated on White Background


Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:
Drawing List!|
Toggle Drawing Sheet!|
Workplane Resize!|
Drawing View Locker!|
Multi Sheet to Mono Sheet!|
Drawing Weld Symbols!|
Drawing View Label Align!|
Open From Balloon!|
Model State Lock!
Posts and Ideas:
My Ideas|
Dimension Component!|
Partlist Export!|
Derive I-properties!|
Vault Prompts Via API!|
Vault Handbook/Manual!|
Drawing Toggle Sheets!|
Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !


 


EESignature

0 Likes
Message 5 of 18

Anonymous
Not applicable

Thanks again but I need clarify the problem. [the subject post is misleading]

 

I do want the listener to persist. If I run my code one time then it works perfectly. Every time I create an assembly relationship the message box appears.

 

The problem appears when I edit the iLogic rule and run it a second time. Then there are 2 OnNewRelationship event triggers and the message box appears twice. Edit and run the rule a 3rd time and you can see how this would make it impossible to test.

 

How do I clear the event triggers the next time I run the rule? Is there some way to access all active events?

 

I'm also open to writing this rule in a completely different way if that is helpful.

0 Likes
Message 6 of 18

bradeneuropeArthur
Mentor
Mentor

you could use the the "DocumentObject" to see if the document has already been done.

 

DocObj.PNG

 

You could check that with a Boolean true or false.

 

if false then the ruleevent is running otherwise not.

 

If you need help or clarify my idea, please let me know.

Regards,

Arthur Knoors

Autodesk Affiliations & Links:
blue LinkedIn LogoSquare Youtube Logo Isolated on White Background


Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:
Drawing List!|
Toggle Drawing Sheet!|
Workplane Resize!|
Drawing View Locker!|
Multi Sheet to Mono Sheet!|
Drawing Weld Symbols!|
Drawing View Label Align!|
Open From Balloon!|
Model State Lock!
Posts and Ideas:
My Ideas|
Dimension Component!|
Partlist Export!|
Derive I-properties!|
Vault Prompts Via API!|
Vault Handbook/Manual!|
Drawing Toggle Sheets!|
Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !


 


EESignature

0 Likes
Message 7 of 18

Anonymous
Not applicable

Can you help me understand better, I don't see how the DocumentObject would help. After my rule runs, the event handler persists. I can close all my documents then open a new assembly and the event handler sub still gets triggered.

 

I need to reset the event handler each time the rule is run.

 

I would also like to prevent it from being run in other assemblies but that is something I can work around.

0 Likes
Message 8 of 18

bradeneuropeArthur
Mentor
Mentor
I did the following:
I run this rule just before the other rule in the document.

How do you run this rule? Manually?

I have created a rule in the document and run this external rule (listener).


Regards,

Arthur Knoors

Autodesk Affiliations & Links:
blue LinkedIn LogoSquare Youtube Logo Isolated on White Background


Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:
Drawing List!|
Toggle Drawing Sheet!|
Workplane Resize!|
Drawing View Locker!|
Multi Sheet to Mono Sheet!|
Drawing Weld Symbols!|
Drawing View Label Align!|
Open From Balloon!|
Model State Lock!
Posts and Ideas:
My Ideas|
Dimension Component!|
Partlist Export!|
Derive I-properties!|
Vault Prompts Via API!|
Vault Handbook/Manual!|
Drawing Toggle Sheets!|
Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !


 


EESignature

0 Likes
Message 9 of 18

Anonymous
Not applicable
I'm confused, what 'other rule' are you running? I'm only working with the rule I originally posted.

Once this rule works I intend to set it as a local iLogic event trigger upon open of the main assembly.
0 Likes
Message 10 of 18

bradeneuropeArthur
Mentor
Mentor

I run this rule indirectly!

via a rule in the document!

 

everytime the rule has running it will be cleared with the ..= nothing.

 

 

So it is not running twice!

 

What is then different with you and my side is the question.

Regards,

Arthur Knoors

Autodesk Affiliations & Links:
blue LinkedIn LogoSquare Youtube Logo Isolated on White Background


Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:
Drawing List!|
Toggle Drawing Sheet!|
Workplane Resize!|
Drawing View Locker!|
Multi Sheet to Mono Sheet!|
Drawing Weld Symbols!|
Drawing View Label Align!|
Open From Balloon!|
Model State Lock!
Posts and Ideas:
My Ideas|
Dimension Component!|
Partlist Export!|
Derive I-properties!|
Vault Prompts Via API!|
Vault Handbook/Manual!|
Drawing Toggle Sheets!|
Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !


 


EESignature

0 Likes
Message 11 of 18

Anonymous
Not applicable
Let's confirm we see the same problem.

1. Run the rule once, placing several constraints or joints to confirm it works each time.
2. Edit the rule (I would do this a lot to as I work on the rule) and change the message box text so it is clear there was a change.
3. Run the rule again and place several constraints.

You should now see 2 message boxes for each Constraint. Each time you edit and run the rule the problem gets worse.
0 Likes
Message 12 of 18

bradeneuropeArthur
Mentor
Mentor

Ok understand.
I did not check that, but i will.
I am not at my desk right now... so later!

Regards,

Arthur Knoors

Autodesk Affiliations & Links:
blue LinkedIn LogoSquare Youtube Logo Isolated on White Background


Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:
Drawing List!|
Toggle Drawing Sheet!|
Workplane Resize!|
Drawing View Locker!|
Multi Sheet to Mono Sheet!|
Drawing Weld Symbols!|
Drawing View Label Align!|
Open From Balloon!|
Model State Lock!
Posts and Ideas:
My Ideas|
Dimension Component!|
Partlist Export!|
Derive I-properties!|
Vault Prompts Via API!|
Vault Handbook/Manual!|
Drawing Toggle Sheets!|
Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !


 


EESignature

0 Likes
Message 13 of 18

bradeneuropeArthur
Mentor
Mentor

Hi,

 

I have tested the above:

 

1. Run the rule once, placing several constraints or joints to confirm it works each time.
2. Edit the rule (I would do this a lot to as I work on the rule) and change the message box text so it is clear there was a change.
3. Run the rule again and place several constraints.

 

If I do it like I suggested I do not have 2 messages!

Why are you having them twice.

 

please put this complete code into yours, and test again.

 

Sub Main
    Dim evtListener As New MateListener(ThisServer, Parameter)
	evtListener = Nothing 
End Sub

Public Class MateListener
	
    Private WithEvents m_AsmEvts As Inventor.AssemblyEvents = Nothing
	
    Private Parameter As Object
	
    Sub New(invServer As InventorServer, Parameter As Object)
        m_AsmEvts = invServer.AssemblyEvents
        Me.Parameter = Parameter
    End Sub

    Private Sub m_AsmEvts_OnNewRelation(ByVal DocumentObject As Inventor._AssemblyDocument, ByVal Relationship As Object, ByVal BeforeOrAfter As Inventor.EventTimingEnum, ByVal Context As Inventor.NameValueMap, ByRef HandlingCode As Inventor.HandlingCodeEnum) Handles m_AsmEvts.OnNewRelationship
        If BeforeOrAfter = Inventor.EventTimingEnum.kAfter Then
			MsgBox("new relationship: Modified second time" & vbTab & Relationship.Name)
		m_AsmEvts = Nothing	
        End If
		
    End Sub
	
End Class

M-AsmEvts  is cleared everytime after the message.

 

Regards,

Arthur Knoors

Autodesk Affiliations & Links:
blue LinkedIn LogoSquare Youtube Logo Isolated on White Background


Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:
Drawing List!|
Toggle Drawing Sheet!|
Workplane Resize!|
Drawing View Locker!|
Multi Sheet to Mono Sheet!|
Drawing Weld Symbols!|
Drawing View Label Align!|
Open From Balloon!|
Model State Lock!
Posts and Ideas:
My Ideas|
Dimension Component!|
Partlist Export!|
Derive I-properties!|
Vault Prompts Via API!|
Vault Handbook/Manual!|
Drawing Toggle Sheets!|
Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !


 


EESignature

0 Likes
Message 14 of 18

Anonymous
Not applicable

@bradeneuropeArthur The problem with adding m_AsmEvts = Nothing into Sub m_AsmEvts_OnNewRelation is that the event listener only runs on the next constraint or joint created and then stops until you run the rule again.

 

I don't think you "placed several constraints or joints to confirm it works each time."

 

I want this to trigger [once per constraint or joint].

0 Likes
Message 15 of 18

bradeneuropeArthur
Mentor
Mentor

Hi,

 

Ok.

 

So running the code till the command "Contraint" has been exit!

Am I right?

 

I have put a rule in the event trigger from the document:

 

Event.PNG

 

Dim auto = iLogicVb.Automation 
'auto = iLogicVb.Automation
Dim a As Inventor.AssemblyDocument = ThisDoc.Document
auto.RunExternalRule(a, "P:\UIEvents.iLogicVb")

Regards,

Arthur Knoors

Autodesk Affiliations & Links:
blue LinkedIn LogoSquare Youtube Logo Isolated on White Background


Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:
Drawing List!|
Toggle Drawing Sheet!|
Workplane Resize!|
Drawing View Locker!|
Multi Sheet to Mono Sheet!|
Drawing Weld Symbols!|
Drawing View Label Align!|
Open From Balloon!|
Model State Lock!
Posts and Ideas:
My Ideas|
Dimension Component!|
Partlist Export!|
Derive I-properties!|
Vault Prompts Via API!|
Vault Handbook/Manual!|
Drawing Toggle Sheets!|
Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !


 


EESignature

0 Likes
Message 16 of 18

Anonymous
Not applicable

Sention,

 

I think this is a good time to use a shared variable. I was not able to find out how to access the objects we create in iLogic.

I have added a counter that is shared between all of the objects created from your class. On New, the counter will add 1. On Disconnect, the counter will subtract 1. If the counter shows more than one instance, the instance will be disconnected. 

 

This will only work if the ilogic rule containing the listener class is not modified between creating new instances.

I recommend putting your listener inside of it's own rule, and calling it from another rule -OR- putting the tasks to be handled on listener event in their own rule and calling from your listener. Doing both may not be a bad idea.

 

This may not be the only or best way, but hopefully it will get the wheels turning for you. I am away from my computer but I believe the following code should work or at least deliver the idea.

 

Option Explicit

Sub Main
    Dim evtListener As New MateListener(ThisServer, Parameter)
End Sub

Public Class MateListener
    Private Shared ObjCount As Integer
    Private WithEvents m_AsmEvts As Inventor.AssemblyEvents = Nothing
	
    Private Parameter As Object
	
    Sub New(invServer As InventorServer, Parameter As Object)
        ObjCount = ObjCount + 1
        m_AsmEvts = invServer.AssemblyEvents
        Me.Parameter = Parameter
    End Sub

    Private Sub m_AsmEvts_OnNewRelation(ByVal DocumentObject As Inventor._AssemblyDocument, ByVal Relationship As Object, ByVal BeforeOrAfter As Inventor.EventTimingEnum, ByVal Context As Inventor.NameValueMap, ByRef HandlingCode As Inventor.HandlingCodeEnum) Handles m_AsmEvts.OnNewRelationship
       If ObjCount > 1 Then
            Call Disconnect
            Return
       End If

        If BeforeOrAfter = Inventor.EventTimingEnum.kAfter Then
			MsgBox("new relationship:" & vbTab & Relationship.Name)
			
        End If
    End Sub
     Sub Disconnect()
         ObjCount = ObjCount-1
         m_AsmEvts = Nothing
     End Sub
          
	
End Class

 

Best Regards,
Tyler Boni
Order Automation Developer
Inventor 2016

 

Mark this as the solution if it solves your issue!

 

0 Likes
Message 17 of 18

Anonymous
Not applicable

@bradeneuropeArthur Thanks for trying to help but I had to get this done so I have the user click an update button to run my rule.

 

@Anonymous I think a shared variable would work (my manager was leading that way) but troubleshooting it would a nightmare because of the need to close Inventor every time I change the rule.

0 Likes
Message 18 of 18

Anonymous
Not applicable

@Anonymous

 

I understand completely.

 

Thanks for the reply, and please update us if you find a better solution. Currently this is the best workflow for listeners that I have found.

0 Likes