External Rule to Create Internal Rule

External Rule to Create Internal Rule

AMN3161
Advocate Advocate
1,579 Views
9 Replies
Message 1 of 10

External Rule to Create Internal Rule

AMN3161
Advocate
Advocate

We have been creating a rule by copy and pasting the same code again again into our files. The code is just a simple excel look up to find a row using the part number in the iproperties, then fill in the description and vendor

i = GoExcel.FindRow("P:\_MASTER_PART_LIST\MASTER_PART_LIST.xls", "MASTER_PART_LIST", "Part No.", "=", iProperties.Value("Project", "Part Number"))
If i <> -1
iProperties.Value("Project", "Description") = GoExcel.CurrentRowValue("Description")
iProperties.Value("Project", "Vendor") = GoExcel.CurrentRowValue("Manufacturer")
Else

iProperties.Value("Project", "Description") = "The entered Part Number does not exist in the Master Parts List"
iProperties.Value("Project", "Vendor") = "The entered Part Number does not exist in the Master Parts List"

End If

But this isn't going to be on every part and we want it internal because we need it to keep checking the excel through trigger upon save to the iproperties stays up to date passively. 

Adding it has been frustrating so I was wondering if there way to have a external rule to create a internal rule within the part? So the user will just run the external rule, it will create the internal rule in what ever the active part is. Then that internal rule will be triggered upon save by another rule I have in the top level assembly?

0 Likes
Accepted solutions (1)
1,580 Views
9 Replies
Replies (9)
Message 2 of 10

WCrihfield
Mentor
Mentor

Absolutely, it is possible.  Here is an example of one I have used in the past.

When I run this external rule, it creates a new local rule, in whichever document is active at the time, and sets the code within.  However, by design, this is done in a way that doesn't cause that local rule to run immediately after it is created.

The rule being created here is a simple rule: when the parameter "CUT_LENGTH" is changed, it will instantly update the drawing, then simulate clicking the view cube's home button. This ensures the whole model is maximized in the model window, at the default view angle, immediately after changing its cut length.

 

Dim oRuleName As String = "Update Doc & Home View"
Dim oRuleText As String
oRuleText = _
"oDV = CUT_LENGTH" & vbCrLf &
"InventorVb.DocumentUpdate()" & vbCrLf &
"'Return view to Home View" & vbCrLf &
"ThisApplication.CommandManager.ControlDefinitions.Item(""AppViewCubeHomeCmd"").Execute"

'oDV (above) is just a 'dummy variable', which isn't used for anything.
'It's value though is the name of a Parameter, which is local to that Document,
'so it will trigger the local rule to run any time that specific parameter changes.
'No need to add this rule to an Event Trigger to make this work.

'The document you want to put the rule into should be the active document.
Dim oDoc As Document = ThisApplication.ActiveEditDocument
Dim oRuleExists As Boolean = False
Dim iLogicAuto = 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
Catch
	oRule = iLogicAuto.AddRule(oDoc, oRuleName, "")
End Try
'Setting the rule's text at this point, instead of above,
'causes the rule to not run as soon as it is created.
'If you want the rule to run as soon as it is created,
'put oRuleText where the empty quotes are in the Catch line above.
oRule.Text = oRuleText
iLogicVb.DocumentUpdate
oDoc.Save

'Dim oRegenAllRules As ControlDefinition = ThisApplication.CommandManager.ControlDefinitions.Item("iLogic.RegenAllRules")
'oRegenAllRules.Execute2(True)

 

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click 'LIKE' 👍.

If you have time, please... Vote For My IDEAS 💡and Explore My CONTRIBUTIONS

Inventor 2020 Help | Inventor Forum | Inventor Customization Forum | Inventor Ideas Forum

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 10

WCrihfield
Mentor
Mentor

Since your code is more complex than the one in my example, and has lots of quotation marks within it, I would suggest that you put your code in a text file, then read that text file into the string variable oRuleText, that way you avoid all the complicated double quote mark edits.

 

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click 'LIKE' 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 4 of 10

WCrihfield
Mentor
Mentor
Accepted solution

Just in case you're not familiar with how to do that last suggestion, here is an example couple lines of code you could use for this situation:

Dim oTxtFileName As String = "C:\Temp\Rule Text.txt"
Dim oRuleText As String = IO.File.ReadAllText(oTxtFileName)

So the final code would look something like this:

Dim oRuleName As String = "Update Doc & Home View"
Dim oTxtFileName As String = "C:\Temp\Rule Text.txt"
Dim oRuleText As String = IO.File.ReadAllText(oTxtFileName)
Dim oDoc As Document = ThisApplication.ActiveEditDocument
Dim oRuleExists As Boolean = False
Dim iLogicAuto = 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

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click 'LIKE' 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 10

AMN3161
Advocate
Advocate

thank you, i was about to ask how i would go about that

0 Likes
Message 6 of 10

AMN3161
Advocate
Advocate

This worked great, thank you!

0 Likes
Message 7 of 10

WCrihfield
Mentor
Mentor

Good to hear.  Always glad to help out where I can.

By the way, since this is a fairly hot topic, I went ahead and created a Contribution post within my profile for this subject.  I know I'm not the first, or likely the last to post about this subject, but figured it would be nice to have an organized, to the point, resource where all the variations of this scenario can be found in one place.  That post also includes the VBA Macro version of this same process (the one using the text file as input).

Here's the link.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 8 of 10

AMN3161
Advocate
Advocate

most of the forum posts about this topic i found before making a post were far too customized to allow me to use them. that was liked more my lack of understanding with logic. But either way there is not that much on the forums regarding this problem

0 Likes
Message 9 of 10

AMN3161
Advocate
Advocate

I been trying to manipulate this for a a few hours and I cant figure out something that's likely a simple thing. But how would I remove the text from the rule that displays during the "do you want to replace it"?

 

This is what i am using

 

Dim oRuleName As String = "Master_Parts_List"
Dim oTxtFileName As String = "C:\_VAULT_WORKSPACE\JOBS\FAB\Ilogic Rules\External Rules\Referenced Rules\Master Parts List Link (REF).txt"
Dim oRuleText As String = IO.File.ReadAllText(oTxtFileName)
Dim oDoc As Document = ThisApplication.ActiveEditDocument
Dim oRuleExists As Boolean = False
Dim iLogicAuto = 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 figure just deleting orule.text out of the try statement would just fix that but no matter what i do it breaks the rule and i get this error

 

"Object reference not set to an instance of an object"

 

My rule has gotten so big over the past month that the message box is so big that it is off the screen and i cant get to the yes or no button.

 

I want to fix it myself so i can learn, can you explain what is going on in this?

 

Try
	oRule = iLogicAuto.GetRule(oDoc, oRuleName)
	oAns = MsgBox("A Rule named '" & oRuleName & "' already exists." &
	"Do you want to replace its text?", vbYesNo + vbQuestion,"")
	If oAns = vbNo Then Return '(or Exit Sub)

 

0 Likes
Message 10 of 10

AMN3161
Advocate
Advocate

I tried a different approach that works great

 

Dim oRuleName As String = "Master_Parts_List"
Dim oTxtFileName As String = "C:\_VAULT_WORKSPACE\JOBS\FAB\Ilogic Rules\External Rules\Referenced Rules\Master Parts List Link (REF).txt"
Dim oRuleText As String = IO.File.ReadAllText(oTxtFileName)
Dim oDoc As Document = ThisApplication.ActiveEditDocument
Dim oRuleExists As Boolean = False
Dim iLogicAuto = iLogicVb.Automation
iLogicAuto.RulesEnabled = True
iLogicAuto.RulesOnEventsEnabled = True
Dim oRule As iLogicRule


Dim TargetRuleName As String = "Master_Parts_List"

Dim auto As IiLogicAutomation  = iLogicVb.Automation
Dim doc As Inventor.Document = ThisApplication.ActiveDocument

' Get the rules 
Dim ruleCol As System.Collections.IEnumerable = auto.Rules(doc)
Dim bRuleExists As Boolean = False	

' make sure the rules collection exists
If Not ruleCol Is Nothing Then
    ' string to hold the rule names
    Dim name As String = ""
    ' Go through each of the rules
    For Each rule As iLogicRule In ruleCol
        ' check the name of the rule
        If TargetRuleName.ToUpper = rule.Name.ToUpper Then
            bRuleExists = True
            Exit For
        End If
    Next
End If

If bRuleExists Then
	
	MessageBox.Show("The Master Parts List Rule exists in this file, i will replace it with the most current version", "Rule Exists")

	iLogicAuto.DeleteRule(oDoc, oRuleName)
	
	oRule = iLogicAuto.AddRule(oDoc, oRuleName, "")
	
	oRule.Text = oRuleText
	iLogicVb.DocumentUpdate
	oDoc.Save

Else 

oRule = iLogicAuto.AddRule(oDoc, oRuleName, "")
	
	oRule.Text = oRuleText
	iLogicVb.DocumentUpdate
	oDoc.Save

End If






0 Likes