External iLogic rule to create a local rule / Object reference not set...

External iLogic rule to create a local rule / Object reference not set...

daniel.coteLQQM4
Explorer Explorer
1,406 Views
2 Replies
Message 1 of 3

External iLogic rule to create a local rule / Object reference not set...

daniel.coteLQQM4
Explorer
Explorer

Hi everyone,

I've created an external rule that creates a local rule based on Luke Davenport's blog:

https://inventorlogicblog.wordpress.com/2016/04/20/autodesk-inventor-ilogic-create-a-new-ilogic-rule...

 It works sometimes but most of the time I get the “Object reference not set to an instance of an object” message and the rule is not created.

I can’t find a constant pattern to help me figure out why sometimes it works and sometimes not. I tried part files, assembly files, vaulted, unvaulted, old files, recent files, etc.

We are using Inventor 2018 with Vault Workgroup 2018

 

The purpose of this rule (in case other solutions could be applied):

In our templates, I've insert some new parameters that are driven by an external rule but they need an immediate response from this rule every time they are modified (I tried Event Triggers, not good for that). In my templates, I've created a local rule with a dummy parameter that calls for the external rule to run everytime my "finish_list" parameter is modified. This works well but when we want to update an old part or assembly with the new template's standards (with our update external rule) I also have to create a local rule on those old parts. 

 

Here's my code:

SyntaxEditor Code Snippet

' External rule to create a local rule

Dim RuleName As String = "Paint_Action"
Dim RuleText As String = "'Rule to add a color to the finish when painted" & vbCrLf & _
"Paint_Dummy = finish_list" & vbCrLf & _
"iLogicVb.RunExternalRule(""Paint_Options"")"



Dim oDoc As Document = ThisApplication.ActiveEditDocument
Dim RuleAlreadyExists As Boolean = False
' Define the iLogic addin
Dim iLogicAddIn As ApplicationAddIn = ThisApplication.ApplicationAddIns.ItemById("{3bdd8d79-2179-4b11-8a5a-257b1c0263ac}")
' Get the iLogic automation object
Dim iLogic As Object = iLogicAddIn.Automation
' Get the list of iLogic rules in the current Inventor document
Dim RuleList As Object = iLogic.Rules(oDoc)

' Loop through all rules in document
For Each R As Object In RuleList
	If R.Name = RuleName Then
		' A rule with the same name already exists...
		RuleAlreadyExists = True
		Exit For
	End If
Next

If RuleAlreadyExists Then
	Dim ReplaceRule As MsgBoxResult = MsgBox("A rule called '" & RuleName & "' already exists - replace it?", 36, "Excitech iLogic")
	If ReplaceRule = vbYes Then
		' Delete the existing rule
		iLogic.DeleteRule(oDoc, RuleName)
		' Add the new rule
		iLogic.AddRule(oDoc, RuleName, RuleText)
	Else
		MsgBox("Operation cancelled...", 64, "Excitech iLogic")
		Exit Sub
	End If
Else
	' No existing rule to delete - simply add the new rule
	iLogic.AddRule(oDoc, RuleName, RuleText)
End If

' End of iLogic rule 

Any help on this would be very appreciated.

Thank you!

0 Likes
Accepted solutions (1)
1,407 Views
2 Replies
Replies (2)
Message 2 of 3

LukeDavenport
Collaborator
Collaborator
Accepted solution

Hi Daniel,

Just checking - is there a reason you can't have this 'finish update' external rule triggered on document save? Then you wouldn't need to insert an embedded iLogic rule into every document. 

 

But anyway - I've had a look at the code and it currently fails if there are no existing iLogic rules in the document. So you just need to add the two lines shown in bold below - no point looping through the rule list if there aren't any!

This seems to work fine now. 

Thanks for spotting the bug, Luke

SyntaxEditor Code Snippet

If Not IsNothing(RuleList) Then
' Loop through all rules in document
For Each R As Object In RuleList
	If R.Name = RuleName Then
		' A rule with the same name already exists...
		RuleAlreadyExists = True
		Exit For
	End If
Next
End If

 

Message 3 of 3

daniel.coteLQQM4
Explorer
Explorer

A big thank you Luke!

I figured out the problem just a few minutes before you post the solution but I didn't know how to solve it.

It's working well now!

Have a nice day!

0 Likes