Insert existing external rules into event rules trigger

Insert existing external rules into event rules trigger

sawek14
Explorer Explorer
940 Views
7 Replies
Message 1 of 8

Insert existing external rules into event rules trigger

sawek14
Explorer
Explorer

Hi everyone

 

For several days I have been looking for a solution to add an existing external rule to event rules trigger, exactly to Before Save Document.

I searched the entire forum, tried all possible solutions, none of them worked the way I wanted.

 

I was able to do this with two rules and an external text file.

Can someone help, maybe someone already has such a solution? I only need codes in iLogic.

Below are two forum codes that I have dealt with.

 

First :

 

Imports System.Collections
Dim oRuleName As String = "test1"
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 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

 

Second :

 

Dim oDoc As Inventor.Document = ThisApplication.ActiveDocument
Dim oRuleName As String = "test1"
Dim oAuto As IiLogicAutomation = iLogicVb.Automation
Dim oRule As iLogicRule
Try
        oRule = oAuto.GetRule(oDoc,oRuleName)
Catch
        MsgBox("The specified rule was not found in the active document. Exiting.",vbOKOnly," ")
        Exit Sub
End Try

''Added to create iLogicEventRules property set if it does not exist.
Try
        customIPropSet = oDoc.PropertySets.Item("iLogicEventsRules")
Catch
End Try
Try
        If customIPropSet.InternalName <> "{2C540830-0723-455E-A8E2-891722EB4C3E}" Then
               Call customIPropSet.Delete
               customIPropSet = oDoc.PropertySets.Add("iLogicEventsRules", "{2C540830-0723-455E-A8E2-891722EB4C3E}")
        End If
Catch 
End Try
Try
        If customIPropSet Is Nothing Then
               customIPropSet = oDoc.PropertySets.Add("iLogicEventsRules", "{2C540830-0723-455E-A8E2-891722EB4C3E}")
        End If
Catch 
End Try
''End of code to create property set

Dim oETPropSet As PropertySet
Try
        oETPropSet = oDoc.PropertySets.Item("iLogicEventsRules")
Catch
        oETPropSet = oDoc.PropertySets.Item("_iLogicEventsRules")
End Try
If oETPropSet Is Nothing Then
        MsgBox("Couldn't find the Event Triggers property set. Exiting.", vbOKOnly + vbExclamation, " ")
        Exit Sub
End If

Dim oProperty As Inventor.Property
Dim oPropId As Long
For oPropId = 700 To 799
        Try
               oProperty = oETPropSet.ItemByPropId(oPropId)
               If oProperty.Value = oRuleName Then
                       MsgBox("This rule has already been added to that event trigger. Exiting.",vbOKOnly," ")
                       Exit Sub
               End If
        Catch
               oProperty = oETPropSet.Add(oRuleName, "BeforeDocSave" & oPropId, oPropId)
               Exit Sub
        End Try
        oPropId = oPropId + 1
Next

 

i using Inventor 2018

 

0 Likes
Accepted solutions (1)
941 Views
7 Replies
Replies (7)
Message 2 of 8

sawek14
Explorer
Explorer

I would like to do this with just one rule, no external file.

I forgot to add.

0 Likes
Message 3 of 8

WCrihfield
Mentor
Mentor
Accepted solution

Hi @sawek14.  This can be a tricky and challenging task.  Adding an 'external' iLogic rule to the Event Triggers by code is slightly different than doing so with a 'local' iLogic rule.  When the rule is external, it needs a bit of additional text in front of the rule's name, before adding it as the value of the special iProperty.  That additional text is "file://".  This apparently tells it that this rule can't be found in the local document, and must be searched for in the file system.  I actually wrote a few contribution posts about accessing the Event Triggers settings by code (link1, link2, link3), but I don't think I mentioned this little pointer in them anywhere back then.  At one point I also created a reference Function to check if a rule was local or external, then used a bit of code that would add the extra text in front of the rule name if it was found to be an external rule, but it's not universally fool proof.

 

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

If you want and have time, I would appreciate your Vote(s) for My IDEAS 💡 or you can Explore My CONTRIBUTIONS

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 4 of 8

sawek14
Explorer
Explorer

Thx for help

0 Likes
Message 5 of 8

mslosar
Advisor
Advisor

@WCrihfield 

 

I'm trying to do the same thing, but i'm running into the same issue in Inventor 2021. I made a new rule the code from (https://knowledge.autodesk.com/support/inventor/learn-explore/caas/simplecontent/content/add-ilogic-...) and it does......nothing.

 

I've set my rule up using file://name, file://name.txt, file://fullpathtoname, etc.,  and it does nothing.  I gave a non existent filename thinking it should say no such rule exists, but upon hitting save nothing happens.  I have to do this to a few hundred files, so the manual process is out.  What is it i'm missing?

 

I think what missing is that there was not a prior rule in the file(s) in question.  Using another post i added lines like so

 

' To make sure that the document has an iLogic DocumentInterest, add a temporary rule
	Dim tempRule = iLogicVb.Automation.AddRule(ThisDoc.Document, "TemporaryRule_392856A2", "")
	EventPropSet.Add("Drawing - Auto_AUTOCAD_DWG.txt", "BeforeDocSave", 700)
	iLogicVb.Automation.DeleteRule(ThisDoc.Document, tempRule.Name)

And that seems to have taken care of the issue (fingers crossed)

Message 6 of 8

WCrihfield
Mentor
Mentor

OK.  Which event were you trying to put the external iLogic rule under?  Is that external iLogic rule located within one of the directories specified within your iLogic Configuration settings > External Rule Directories?  (Tools tab > Options panel > iLogic Configuration (opens dialog))  Does the rule have the same file extension as your iLogic Configuration settings > Default Extension for External Rule Files is set to?

WCrihfield_0-1650996065554.png

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 7 of 8

Bertram_Anlagenbau
Explorer
Explorer

@WCrihfield

 

I have been using your idea of adding external rules to the event trigger in Inventor 2022. But since I have updated our System to Inventor 2023 I have a problem. For example, if I add an external rule to the event trigger "BeforeDocSave" through an iLogic rule and then save the Document the rules are not being triggered. But if I manually go into the event trigger and swap the order of the external rules in the event trigger and then save the document everything works fine.

Screenshot 2022-04-27 091820.png

 

I would appreciate any advice or ideas to why this is not working anymore.

0 Likes
Message 8 of 8

WCrihfield
Mentor
Mentor

Hi @Bertram_Anlagenbau.  I am still using Inventor 2022.2.2, so I might not be able to exactly duplicate the problem you are encountering.  I have heard others mention a fluke like this once in a while too though.  I have not encountered that problem myself yet, but also do not really utilize that code process extensively or on a regular basis, because I'm generally either doing the set-up manually as I create stuff, or am using other means to automate my solutions.  Within one or more of my posts on the subject, I warn that this is property set is 'hidden' by Autodesk by default, and that they warn against utilizing such resources, because they may change at any time, without notice.  So, I would suggest investigating the matter yourself a bit more by utilizing some code similar to the one mentioned in this link.  Then start a fresh new document from New command, making sure that the template it is drawing from has never had any local Event Triggers set-up within it.  Then create a very simple local rule within that document that just shows a simple message like "Hi there." in it.  Then have another similar, very simple external rule prepared.  Then manually open the Event Triggers dialog ad add the local rule, and the external rule under each of the events available.  Then save the document.  Now do some things that will trigger each event, and make sure both rules are running for each event.  Then use that exploratory iLogic rule to investigate the settings of that hidden property set.  My update codes simply get that set by its 'internal' name, because that is a constant, and should never be changed, while some others prefer to change its other name in order to make it visible.  You may even want to write the results of that rule out to a text file or Excel, or similar, instead of just messages.  Check the results against the data that has been posted, to see if there are any differences.  That's mostly how I looked into these matters for myself before creating those contribution posts and updates.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes