Object reference issue in rule

Object reference issue in rule

To_Efficiency_and_Beyond
Enthusiast Enthusiast
788 Views
6 Replies
Message 1 of 7

Object reference issue in rule

To_Efficiency_and_Beyond
Enthusiast
Enthusiast

I found a piece of code online, here is the original version, which works perfectly:

Dim oRuleName As String = "Trip Wire"
Dim oTxtFileName As String = "C:\Users\Jesse\Desktop\Sandbox\Jesse's Wonder Emporium\iLogic\Assets\Trip Wire.txt"
Dim oRuleText As String = IO.File.ReadAllText(oTxtFileName)
Dim oDoc As Document = ThisApplication.ActiveEditDocument
Dim oRuleExists As Boolean = False
Dim iLogicAuto As IiLogicAutomation = iLogicVb.Automation
iLogicAuto.RulesEnabled = True
iLogicAuto.RulesOnEventsEnabled = True
Dim oRule As iLogicRule
Try
	oRule = iLogicAuto.GetRule(oDoc, oRuleName)
	oAns = MsgBox("A Rule named '" & oRuleName & "' already exists." & vbCrLf &
	"Its Text = " & vbCrLf &
	oRule.Text & vbCrLf &
	"Do you want to replace its text?", vbYesNo + vbQuestion,"")
	If oAns = vbNo Then Return '(or Exit Sub)
Catch
	oRule = iLogicAuto.AddRule(oDoc, oRuleName, "")
End Try
oRule.Text = oRuleText
iLogicVb.DocumentUpdate
oDoc.Save

I made a few changes (commented out several lines) to fit our needs:

Dim oRuleName As String = "Trip Wire"
Dim oTxtFileName As String = "C:\Users\Jesse\Desktop\Sandbox\Jesse's Wonder Emporium\iLogic\Assets\Trip Wire.txt"
Dim oRuleText As String = IO.File.ReadAllText(oTxtFileName)
Dim oDoc As Document = ThisApplication.ActiveEditDocument
'Dim oRuleExists As Boolean = False
Dim iLogicAuto As IiLogicAutomation = iLogicVb.Automation
iLogicAuto.RulesEnabled = True
iLogicAuto.RulesOnEventsEnabled = True
Dim oRule As iLogicRule
Try
	oRule = iLogicAuto.GetRule(oDoc, oRuleName)
'	oAns = MsgBox("A Rule named '" & oRuleName & "' already exists." & vbCrLf &
'	"Its Text = " & vbCrLf &
'	oRule.Text & vbCrLf &
'	"Do you want to replace its text?", vbYesNo + vbQuestion,"")
'	If oAns = vbNo Then Return '(or Exit Sub)
Catch
	oRule = iLogicAuto.AddRule(oDoc, oRuleName, "")
End Try
oRule.Text = oRuleText
iLogicVb.DocumentUpdate
'oDoc.Save

Now I get an error "Object reference not set to an instance of an object." which points to this line:

oRule.Text = oRuleText

Where did I go wrong? I don't see any reason that removing the message box portion should affect my object definition..

0 Likes
Accepted solutions (2)
789 Views
6 Replies
Replies (6)
Message 2 of 7

WCrihfield
Mentor
Mentor
Accepted solution

Hi @To_Efficiency_and_Beyond.  Right away, the first thing I notice within these codes is how they are using the Try...Catch block of code.  In this case, I would not use the Try...Catch block, because in my experience, that 'GetRule' method will not throw an error if it does not find the specified rule, therefore it will never get to the Catch part of that block of code.  Instead, just check if IsNothing(oRule), or If oRule Is Nothing, after using that 'GetRule' method.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 7

WCrihfield
Mentor
Mentor

Hi @To_Efficiency_and_Beyond.  Just in case you did not understand my previous post, here is the version of your last code that I modified the way I specified.  I got rid of the Try...Catch block of code, and now I am simply checking to see if the variable 'oRule' was assigned a value after using the GetRule method.  If it 'Is Nothing' then I proceed to create the rule.  And if it does have a value, the rule was found, so I proceed to present the question about replacing its text.  If you answer No, it exits the rule, but if you answer Yes, it proceeds to set its Text property to oRuleText.  I also replaced the 'iLogicVb.DocumentUpdate' code with 'oDoc.Update', because that oDoc variable is already representing the 'ActiveEditDocument'.

Dim oRuleName As String = "Trip Wire"
Dim oTxtFileName As String = "C:\Users\Jesse\Desktop\Sandbox\Jesse's Wonder Emporium\iLogic\Assets\Trip Wire.txt"
Dim oRuleText As String = IO.File.ReadAllText(oTxtFileName)
Dim oDoc As Document = ThisApplication.ActiveEditDocument
Dim oRuleExists As Boolean = False
Dim iLogicAuto As IiLogicAutomation = iLogicVb.Automation
iLogicAuto.RulesEnabled = True
iLogicAuto.RulesOnEventsEnabled = True
Dim oRule As iLogicRule
'this method 'GetRule' will not Error if rule is not found
oRule = iLogicAuto.GetRule(oDoc, oRuleName)
If IsNothing(oRule) Then
	oRule = iLogicAuto.AddRule(oDoc, oRuleName, "")
	oRule.Text = oRuleText
Else
	oAns = MsgBox("A Rule named '" & oRuleName & "' already exists." & vbCrLf &
	"Its Text = " & vbCrLf &
	oRule.Text & vbCrLf &
	"Do you want to replace its text?", vbYesNo + vbQuestion,"")
	If oAns = vbNo Then Return '(or Exit Sub)
	'this next line will not run, if you answered No
	oRule.Text = oRuleText
End If
oDoc.Update
oDoc.Save

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 4 of 7

To_Efficiency_and_Beyond
Enthusiast
Enthusiast

Thanks! This worked perfectly!

0 Likes
Message 5 of 7

Tiffany_Hayden_
Collaborator
Collaborator

@WCrihfield Hey I found this thread and I'm trying to find iLogicVB within visual studio. I added the below references but still do not see the iLogicVB class. I looked int he API but all the references were for VBA and not doing it externally with visual studio via an AddIn. Do you happen to know what references that are needed? 

 

Tiffany_Hayden__0-1726757650421.png

 

Tiffany Hayden
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature

0 Likes
Message 6 of 7

WCrihfield
Mentor
Mentor
Accepted solution

Hi @Tiffany_Hayden_.

Even though the object known as "iLogicVb" in iLogic rules (ILowLevelSupport Interface) seems to be within the "Autodesk.iLogic.Interfaces.dll", I think you also need to include a reference to "Autodesk.iLogic.Types.dll" (online help link).  It can not be used within external EXE, but can be used within Inventor.ApplicationAddIn and within DLL that is meant to be for reference by iLogic rules.  Since I can not make add-ins or EXE's where I work, for various reasons, I do not have much experience trying to use the 'iLogicVb' object outside of iLogic environment though.  When I do need access to objects that are unique to the iLogic add-in, I usually just 'pass' the 'Rule Object' to my external resource when I call it, or create an instance of it.

 

Edit:  PS.  Another pretty useful object to 'pass' to an externally referenced tool is the StandardObjectFactory.  We do not normally see that specific object, because we usually bypass it by using the:

iLogicVb.CreateObjectProvider(document As Document) As IStandardObjectProvider

...method.  However, it seems that we can actually do the same with the following:

 

Dim SOP As IStandardObjectProvider = StandardObjectFactory.Create(ThisDoc.Document)

 

 ...so it must already instantiated within our iLogic rules, similar to the 'Rule Objects'.  With that object, we can get access to pretty much all of the most popular iLogic tools, and make them focus on a specific Document.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 7 of 7

Tiffany_Hayden_
Collaborator
Collaborator

That was the trick! Adding in the Autodesk.iLogic.Interfaces.dll to the references allowed me access to what I needed. Appreciate your help @WCrihfield 

 

            string selectedItemName = ExternalRules.ListItem[ExternalRules.ListIndex];

            IStandardObjectProvider SOP = StandardObjectFactory.Create(Globals.InvApp.ActiveDocument);

            SOP.iLogicVb.RunRule(Globals.InvApp.ActiveDocument, selectedItemName, context);

 

Tiffany Hayden
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature

0 Likes