Simple Set Event Trigger with Ilogic

Simple Set Event Trigger with Ilogic

AMN3161
Advocate Advocate
2,969 Views
23 Replies
Message 1 of 24

Simple Set Event Trigger with Ilogic

AMN3161
Advocate
Advocate

I have been seeing a few rather large posts about having logic set your event trigger but all of the posts are very clearly over my head

 

I have this to insert a rule into my assembly

 

Dim oRuleName As String = "Master_Parts_List_Assembly"
Dim oTxtFileName As String = "C:\_VAULT_WORKSPACE\JOBS\FAB\Ilogic Rules\External Rules\Referenced Rules\Run a Specific Rule in all parts (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

The that code will insert this into my assembly

Dim oDoc As AssemblyDocument = ThisDoc.Document

'define the ilogicAutomation
Dim iLogicAuto As Object 
iLogicAuto = iLogicVb.Automation 

Dim oRuleName As String = "Master_Parts_List" 
Dim oRule As Object 

For Each doc As Document In oDoc.AllReferencedDocuments
	On Error Resume Next
	oRule = iLogicAuto.GetRule(doc, oRuleName) 
	'run the rule
	iLogicAuto.RunRuleDirect(oRule) 
Next

 I am trying to have the other designers simply run a external rule in a assembly then another in a part to link our parts to a excel Bill of Material

 

I seen some lengthy chunks of code that revolve around something like this

("Master_Parts_List_Assembly", "BeforeDocSave0", 700)

 

I just cant figure out how to have a very simple code that just adds the Master_Parts_List_Assembly rule to a before document save. I dont need multiple runs just a simple run this before a save

 

any help would be appreciated 

0 Likes
Accepted solutions (1)
2,970 Views
23 Replies
Replies (23)
Message 2 of 24

WCrihfield
Mentor
Mentor

I think I may have a solution for you.  There are a lot of things that could potentially cause an error, so this is a preliminary code that could likely use more checks in there to help handle them, but I think it should get the job done.

 

This code is designed to:

  • iterate down through each referenced document of the active assembly
    • It searches to ensure the specified rule is found within each document
    • If found, it will then attempt to get the hidden property set that Inventor used for the Event Triggers.
    • If it finds this property set, it will then attempt to add the specified rule to the "BeforeDocSave" event.
      • It first looks to see if you already have a rule at that specific PropId location
      • If one is found, and the rule name within it matches the currently specified one, it will delete it
      • Then it will add it again

Here's the code:

Dim oADoc As AssemblyDocument = ThisAssembly.Document
Dim oAuto As IiLogicAutomation = iLogicVb.Automation
Dim oRuleName As String = "Master_Parts_List_Assembly"
Dim oRule As iLogicRule
Dim oETPropSet As PropertySet 'Event Triggers Property Set

For Each oRefDoc As Document In oADoc.AllReferencedDocuments
	'this Try checks to make sure the target rule exists within each document
	Try
		oRule = oAuto.GetRule(oRefDoc,oRuleName)
	Catch 'it didn't find that rule in that document
		MsgBox("A rule named " & oRuleName & " was not found within the following document:" & vbCrLf & _
		oRefDoc.FullFileName, vbOKOnly, " ")
	End Try
	If oRule IsNot Nothing Then
		Try
			oETPropSet = oRefDoc.PropertySets.Item("iLogicEventsRules")
		Catch
			oETPropSet = oRefDoc.PropertySets.Item("_iLogicEventsRules")
		Catch
			MsgBox("Coundn't find the iLogic Event Triggrs property set in :" & vbCrLf & _
			oRefDoc.FullFileName & vbCrLf & _
			"So the rule couldn't be added to the Event Triggers event by code.", vbOKOnly, " ")
			Continue For
		End Try
		If oETPropSet IsNot Nothing Then
			If oETPropSet.ItemByPropId(700) IsNot Nothing And
				oETPropSet.ItemByPropId(700).Value = oRuleName Then
				oETPropSet.ItemByPropId(700).Delete
			End If
			oETPropSet.Add(oRuleName,"BeforeDocSave0",700)
		End If
	End If
Next

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 24

AMN3161
Advocate
Advocate

I am not sure if i explained this or its me not completely understanding what this code does

 

Once the user runs the external rule its going to put this into the top level assembly

 

Dim oDoc As AssemblyDocument = ThisDoc.Document

'define the ilogicAutomation
Dim iLogicAuto As Object 
iLogicAuto = iLogicVb.Automation 

Dim oRuleName As String = "Master_Parts_List" 
Dim oRule As Object 

For Each doc As Document In oDoc.AllReferencedDocuments
	On Error Resume Next
	oRule = iLogicAuto.GetRule(doc, oRuleName) 
	'run the rule
	iLogicAuto.RunRuleDirect(oRule) 
Next

 

which will run a different rule in a bunch of parts all at different assembly levels to update their iproperties. I want this update to happen passively during saving so its a fire and forget kind of logic. My only problem is that I dobt want the users to have to set the trigger for that assembly rule because we have some very very novice people.

 

So what the user will do is run a external rule to insert the rule above, i just don't know how to either have it come in and have the trigger already on it, or have a different external rule to set the rule above to be triggered on save

 

I am just trying to make a simple process for people completely unfamiliar to logic

0 Likes
Message 4 of 24

WCrihfield
Mentor
Mentor

   Apparently I wasn't sure which of the two named rules was the one within the main assembly, and which one was within all the referenced documents.  I was under the impression that you wanted this new rule to iterate through all the referenced documents looking for the rule named "Master_Parts_List_Assembly" within them, then add that rule to the BeforeDocSave event of the Event Triggers within that referenced document.

 

So, just to clarify all this in my head...:

  • The rule called "Master_Parts_List" is the one that is supposed to already exist within each of the referenced documents within that main assembly, right?
    • This rule is to update the iProperties of that local part or sub-assembly, and is working OK, correct?
    • Is it possible that some of the referenced documents may not have that rule within them?
  • The rule called "Master_Parts_List_Assembly" is the one that you want to be located within the main assembly, right?
    • This is the rule that is to iterate down through all the assembly's referenced documents and run the rule found within each of them  called "Master_Parts_List", right.
    • Are you wanting this rule (the one named "Master_Parts_List_Assembly") to be added to the BeforeDocSave event of the Event Triggers of this main assembly, by code?
  • Now we are trying to create a third rule, as an external iLogic rule, that will create the local rule (within the main assembly) called "Master_Parts_List_Assembly"?
    • And this third rule is also to add the rule it creates ("Master_Parts_List_Assembly") to the BeforeDocSave Event Trigger of the main assembly?

Does all this sound right?

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 24

WCrihfield
Mentor
Mentor

If the assumptions in my last post were true, then maybe this code will work for you.

 

I've tried to comment every thing out again to help walk you through what's going on, but honestly, this is a fairly advanced task.

Here's the new code:

Dim oADoc As AssemblyDocument = ThisAssembly.Document

Dim oRuleName As String = "Master_Parts_List_Assembly"
'Specify the full path and file name (with extension) of the text file that contains the code you want to inject into the new rule.
Dim oTxtFile As String = "C:\Temp\Rule Text.txt"
Dim oRuleText As String = IO.File.ReadAllText(oTxtFile)
Dim oAuto As IiLogicAutomation = iLogicVb.Automation
Dim oRule As iLogicRule

'Check if a rule with that name already exists or not
Try
	oRule = oAuto.GetRule(oADoc, oRuleName)
	'if the above line worked, it was found
	'now ask if you want to overwrite its conents or not
	oOverWrite = MsgBox("A Rule named '" & oRuleName & "' already exists in the target document." & vbCrLf & _
	"Its Text = " & vbCrLf & vbCrLf & oRule.Text & vbCrLf & vbCrLf & _
	"Do you want to replace its text?", vbYesNo + vbQuestion," ")
	If oOverWrite = vbNo Then GoTo EventTriggerSetup 'just past where the text is reset
Catch 'it didn't find it, so create it
	oRule = oAuto.AddRule(oDoc, oRuleName, "")
End Try
'set the text of the rule to the text retrieved from the text file
oRule.Text = oRuleText

EventTriggerSetup :
'define the variable to hold the hidden Event Triggers Property Set
Dim oETPropSet As PropertySet 'Event Triggers Property Set

'This hidden property set can only be found by either,
'directly specifying it by its name, then checking if the variable Is Nothing
'or by specifying it its "InternalName", then checking if the variable Is Nothing
'it is not included in the PropertySets.Count or by looping through PropertySet names.
If oRule IsNot Nothing Then
	Try
		oETPropSet = oADoc.PropertySets.Item("iLogicEventsRules")
	Catch
		oETPropSet = oADoc.PropertySets.Item("_iLogicEventsRules")
	Catch
		MsgBox("Coundn't find the iLogic Event Triggrs property set in :" & vbCrLf & _
		oADoc.FullFileName & vbCrLf & _
		"So, the new rule could not be added to the Event Triggers event by code." & vbCrLf & _
		"But you may still be able to manually.", vbOKOnly, " ")
		Return '(or Exit Sub)
	End Try
	
	'The Integer included at the end of the name is needed, because there may be several
	'rules added under this event, and they can't all have the same exact name,
	'but all the names should all contain the propper event name.
	
	'Also the PropId integer needs to be different each time, but must stay within the 700 to 799 range.
	
	'The combination of its name and its PropId range are what specify which event the rule will be added under.
	
	'So, if you are adding multiple rules under the same event, or there are already some rules listed under this event,
	'you will most likely have to create a naming scheme that adds 1 to the Integer at the end of the name,
	'and adds 1 to the PropId each time you add another rule under this event.
	
	'create a boolean (true/false) variable to use if/when I find that this rule is already listed under that event
	Dim oListed As Boolean
	'create an Integer to keep track of what the next available PropId is that we can use
	Dim oNextAvailablePropId As Integer
	'create an Integer variable as our counter as we iterate through all possible listed items under this event
	Dim i As Integer
	If oETPropSet IsNot Nothing Then 'just checking one last time that we have the Property Set
		For i = 700 To 799 'setting up the loop parameters
			If oETPropSet.ItemByPropId(i) IsNot Nothing Then 'there is something in this slot
				If oETPropSet.ItemByPropId(i).Value = oRuleName Then 'checking to see if is this rule
					'found our rule already listed so setting our Boolean variable to True
					oListed = True
					'letting the user know that it was found already listed
					MsgBox("This rule is already listed under the target event in the Event Triggers. Exiting.", vbOKOnly + vbInformation, " ")
					'exiting the rule
					Return '(or Exit Sub)
				End If
			ElseIf oETPropSet.ItemByPropId(i) Is Nothing Then 'this slot is empty (and therefore available)
				oNextAvailablePropId = i
				Exit For
			End If
			i = i + 1
		Next
		If oListed = False Then
			'define the integer to include at the end of the name - using same count as the PropId but without the 700.
			Dim oNameInteger As Integer = oNewPropId - 700
			oETPropSet.Add(oRuleName, "BeforeDocSave" & oNameInteger.ToString, oNextAvailablePropId)
		End If
	End If
End If

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 6 of 24

AMN3161
Advocate
Advocate

So i am getting 

 

Error in rule: Rule0, in document: 1129752-201.iam

Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL))

 

What i did is i have assembly with Master_Parts_List_Assembly rule already in it which is the rule to trigger a specific rule to run in some referenced documents within the assembly. I created a new rule and named it Rule0 and copy/pasted your text into that rule

 

then i modified this line

Dim oTxtFile As String = "C:\_VAULT_WORKSPACE\JOBS\FAB\Ilogic Rules\External Rules\Referenced Rules\Run a Specific Rule in all parts (REF).txt"

Am i applying this incorrectly?

0 Likes
Message 7 of 24

WCrihfield
Mentor
Mentor

So you don't need this new rule to create the "Master_Parts_List_Assembly" rule, you just want this new rule to place that rule into the Event Triggers, under the BeforeDocSave event, right?

Here is a code that should do just that for you.

Please take screen shots of any/all messages and/or error messages that pop-up while this rule is running.

And if it is a regular error message, please include the content of both tabs of the error message.  Often the second tab of the error message is the one that contains key information to understanding where in the code something went wrong.

I cut all the comments and extra spacing out this time, to clarify the actual code.  I also set up a couple more Try...Catch blocks in there, to help get better feedback to help determine where within this long code something may have gone wrong, because there is lots of potential.

If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
	MsgBox("This rule only works for Assembly Documents.",vbOKOnly, "WRONG DOCUMENT TYPE")
	Exit Sub
End If
Dim oADoc As AssemblyDocument = ThisAssembly.Document
Dim oRuleName As String = "Master_Parts_List_Assembly"
Dim oAuto As IiLogicAutomation = iLogicVb.Automation
Dim oRule As iLogicRule
Try
	oRule = oAuto.GetRule(oADoc, oRuleName)
Catch oEx As Exception
	MsgBox("The specified rule was not found in the active document." & vbCrLf & _
	"The Error Message Is As follows:" & vbCrLf & _
	oEx.Message & vbCrLf &  vbCrLf & _
	"Its 'StackTrace is as follows:" & vbCrLf & _
	oEx.StackTrace & vbCrLf & vbCrLf & _
	"Its source is as follows:" & vbCrLf & _
	oEx.Source, vbOKOnly + vbExclamation, "RULE NOT FOUND")
	Exit Sub
End Try
Dim oETPropSet As PropertySet
Try
	oETPropSet = oADoc.PropertySets.Item("iLogicEventsRules")
Catch
	oETPropSet = oADoc.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 oListed As Boolean
Dim oPropId As Integer
Try
	For oPropId = 700 To 799
		If oETPropSet.ItemByPropId(oPropId) IsNot Nothing Then
			If oETPropSet.ItemByPropId(oPropId).Value = oRuleName Then
				oListed = True
				MsgBox("This rule is already listed under the target event in the Event Triggers. Exiting.", vbOKOnly + vbInformation, " ")
				Exit Sub
			End If
		ElseIf oETPropSet.ItemByPropId(oPropId) Is Nothing Then
			Exit For
		End If
		oPropId = oPropId + 1
	Next
Catch oEx As Exception
	MsgBox("Something went wrong while trying to iterate the property set." & vbCrLf & _
	"The Error Message Is As follows:" & vbCrLf & _
	oEx.Message & vbCrLf &  vbCrLf & _
	"Its 'StackTrace is as follows:" & vbCrLf & _
	oEx.StackTrace & vbCrLf & vbCrLf & _
	"Its source is as follows:" & vbCrLf & _
	oEx.Source, vbOKOnly + vbExclamation, " ")
	Exit Sub
End Try
Try
	If oListed = False Then
		Dim oNameInteger As Integer = (oPropId - 700)
		oETPropSet.Add(oRuleName, "BeforeDocSave" & oNameInteger.ToString, oNextAvailablePropId)
	End If
Catch oEx As Exception
	MsgBox("Something went wrong while trying to Add the rule to the event." & vbCrLf & _
	"The Error Message Is As follows:" & vbCrLf & _
	oEx.Message & vbCrLf &  vbCrLf & _
	"Its 'StackTrace is as follows:" & vbCrLf & _
	oEx.StackTrace & vbCrLf & vbCrLf & _
	"Its source is as follows:" & vbCrLf & _
	oEx.Source, vbOKOnly + vbExclamation, " ")
	Exit Sub
End Try

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 8 of 24

AMN3161
Advocate
Advocate

I didnt mean to accept as answer yet

 

This is the error i am getting

0 Likes
Message 9 of 24

AMN3161
Advocate
Advocate

I basically just want to run a external rule to put a rule into the active assembly that will turn the Maser_Parts_List_Assembly to be triggered to run upon save. Once that rule is already in the assembly

0 Likes
Message 10 of 24

WCrihfield
Mentor
Mentor
Accepted solution

   This is a pretty tricky and mostly undocumented process, so please bear with me, as I'm trying to help you.  I've got this code working on my computer, so I'm hoping it will also work on your computer.  (I'm using Inventor Professional 2021.1 and Windows 10.)  This code searches within the active document and finds the rule with that specified name.  Then attempts to find that hidden property set.  Then it starts checking for properties within it that set that who's PropID property are within the range of 700 to 799 (the BeforeDocSave event range).   This step can be complex, but is important.  If it finds a property in that range, it checks to see if the value of that property is this rule's name.  If it is, it lets you know that this rule is already listed in the Event Triggers for this type of event, then exits the rule.  When it doesn't find a property, it then creates the property with this rule's name as its value, which adds that rule to the Event Trigger for the BeforeDocSave event.

 

   I have refined and simplified my code even further, and I believe I have worked out the bugs this time, because as I said above, this code is working for me after several tests.

Dim oDoc As Document = ThisApplication.ActiveDocument
Dim oRuleName As String = "Maser_Parts_List_Assembly"
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

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

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 11 of 24

AMN3161
Advocate
Advocate

This works great thank you!!

 

Appreciate the time you took to help me with this especially with how well over my head it was. I am still dumbfounded how helpful this Autodesk Inventor customization community is!

0 Likes
Message 12 of 24

AMN3161
Advocate
Advocate

Would you have any idea why this works great on my machine and yours but has issues on other designers here? 

 

ill attach the error message but i tried changing his computer to show file extensions but that didnt help

MicrosoftTeams-image (1).pngMicrosoftTeams-image (2).png

0 Likes
Message 13 of 24

WCrihfield
Mentor
Mentor

Not really.  There are too many possibilities.  Sometimes a object (or its Methods or its Properties) being referenced within a code only became available after certain year/releases of Inventor but not available on earlier versions, and that can cause problems.  Often if you look up each type of object in the online help, it will tell you in which version that object became available in.  But I thing all these objects have been available for a really long time.  The "iLogicRule" object and the "IiLogicAutomation" objects are considered Interfaces, and their pages mention version 25 (2021) but I know they were available long before then, so I'm not sure.  Same with the GetRule method.  They're probably the two least used within this post.  I know that hidden property set we're working with has been available since at least 2012-2013, based on some previous posts I've read, so I wouldn't think they would cause any problems.

 

Maybe try replacing all the "Exit Sub" lines with "Return" (without the quotes).

Maybe try putting "Inventor." (without the quotes) in front of some of the object types, when setting the As type (for example, instead of "Dim oDoc As Document", it would say "Dim oDoc As Inventor.Document").  This is usually only necessary when you have other external references included (like when you use AddReference or Imports within the header of the rule), and there is an object defined within one of those other sources with the same object type name, but it stands for something different, so this helps clear up the confusion.

   What kinds of problems are the other users having?  Any screen captured images of error message may (or may not) be helpful. 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 14 of 24

AMN3161
Advocate
Advocate

I posted some error messages in the previous post, i just asked him what version of Inventor he has. We are all on 2020 but i know what specific release, i will also makes the changes you suggested and see what happens

 

I saw this on a previous post

"Try removing the 'Try', 'Catch' and 'End Try' lines of code. Then you'll
see the error message on his machine that is probably being suppressed. It
might be something to do with the 'Mid' statement only working on older
.Net frameworks or something like that.

Post the error message back here so someone can help you.
Luke"

 

Not sure if its related but let me see if its a version first

0 Likes
Message 15 of 24

WCrihfield
Mentor
Mentor

I know that the Try...Catch...End Try block of code is not supported within the VBA environment, but it has been a  'cornerstone' of vb.net for long time.  Before that the main way to avoid expected errors was to use those fairly familiar lines like "On Error GoTo" or "On Error Resume Next" or just plain "GoTo" (there are others).  You can try getting rid of the Try...Catch blocks if you want to, but I doubt that's where the problem is.  The Try...Catch is the newer way to deal with expected errors, and includes ways of providing custom feedback to the user in the event of errors.  Using a line like "On Error Resume Next" would definitely not show any error messages that would normally pop up.  The comment about possibly working with an older version of the ".NET Framework" may be relevant though.  I believe that usually comes into play when you're coding using Visual Studio, and attempting to create standalone solutions (outside of the iLogic or VBA Editor environments) that can run outside of Inventor, or as an Addin for Inventor.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 16 of 24

WCrihfield
Mentor
Mentor

@AMN3161  FYI:  I created a Contribution post that I'm hoping will help to serve as a set of guidelines and instructions that others can to follow and learn from to help them interact with that 'hidden' property set that the iLogic Event Triggers uses.

   Within this post is a table that lists what each event is called within the Event Triggers dialog box, the corresponding Property Name that goes with that event, the PropId range that goes with that event, and which document types that type of event can be used in.  As well as some guidelines to follow when attempting to create new properties within this set.

  This post also contains two iLogic rule codes. 

One that is a bit simper, that is simply used as an investigative tool to inspect and verify the contents of the chart based on existing rules that have been added under each event before hand using the Event Triggers dialog box.  Once you have added a simple rule to every event available within the Event Triggers dialog, then you run this rule, it will display a multi-line MsgBox for each property that exists within that property set, showing the property's Name, DisplayName, PropId, Expression, and Value.ToString, so you can verify they stay within the bounds of the charts specfications.

The second rule is almost exactly like the last one I posted here in this post, but using a different rule name (the rule I used in my investigations).

I'm hoping to keep adding information to that contribution post as more information becomes available, to sort of keep it all in one place, as a quick reference.

Here's the link's address (in case the above link fails):

https://knowledge.autodesk.com/community/article/329361 

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 17 of 24

AMN3161
Advocate
Advocate

I will review this and hopefully learn from it

 

I contacted our Autodesk support company and they said the following about the code. It now works on other designers machines. I have one more person to test but its looking likely

 

 

this is from the support company

The issue with the code is the “PropertySets.Item("iLogicEventsRules")” call.  If no event triggers were ever created in a le manually, the “iLogicEventRules” property set does not exist.  It is not created until the first trigger is created. It’s a bit of a vicious circle when trying to do this through programming. If you are trying to use this on a file that has never had any event triggers created, you need to create this property set first.  I added the code required to do this below.  Copy the text below and replace the text in “Assemby_Master_Parts_List_Trigger.iLogicVb” file. 

Dim oDoc As Inventor.Document = ThisApplication.ActiveDocument

Dim oRuleName As String = "Master_Parts_List_Assembly"

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

Message 18 of 24

WCrihfield
Mentor
Mentor

I've seen that block of code before, and it is a good point.  I used a similar code in another post, but laid it out a little differently.  At first I didn't understand why they were checking if the InternalName was not what they were expecting then deleted it if it wasn't, but later I believe I know why.  Just in case someone tried to recreate this property set before, but either didn't bother specifying the InternalName (because it is "Optional", and the system will create one for you), or they may have supplied a different random string for its value.  If that were the case this check would correct that error.

Here's a rule (very similar but perhaps a little more condensed code) I wrote as a standalone rule to find (or create) this property set, then 'unhide' it (if not already), so you can work with it more easily in the future.  FYI:  When the property set has the underscore "_" at the beginning of its Name property, it will be hidden.  But the Name property is a ReadOnly property, so you can't just change its value.  You have to delete it first, then recreate it, and this time don't put that underscore at the beginning of its name, but use the same InternalName.

 

Dim oDoc As Document = ThisApplication.ActiveDocument
Dim oEventsPropSet As PropertySet
Dim oSetName As String = "iLogicEventsRules"
Dim oInternalName As String = "{2C540830-0723-455E-A8E2-891722EB4C3E}"
Try
	oEventsPropSet = oDoc.PropertySets.Item(oSetName)
	If oEventsPropSet.InternalName <> oInternalName Then
		oEventsPropSet.Delete
		oEventsPropSet = oDoc.PropertySets.Add(oSetName, oInternalName)
	End If
Catch
	oEventsPropSet = oDoc.PropertySets.Item("_" & oSetName)
	oEventsPropSet.Delete
	oEventsPropSet = oDoc.PropertySets.Add(oSetName, oInternalName)
End Try
If oEventsPropSet Is Nothing Then
	oEventsPropSet = oDoc.PropertySets.Add(oSetName, oInternalName)
End If
'the following MsgBox() is just to confirm the success of the above code
'MsgBox("The PropertySet's Name  = " & oEventsPropSet.Name & vbCrLf & _
'"And its InternalName = " & oEventsPropSet.InternalName,vbOKOnly+vbInformation," ")

 

 I use this as an external iLogic rule, so I can use it on any document I want, whenever I want to work with the Event Triggers by code.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 19 of 24

WCrihfield
Mentor
Mentor

   There is one more important thing you should know about using either one of those rules on an old or existing (not new) document.  If you already have some rules set up under some events within the Event Triggers, and one of those checked for conditions is found that causes it to delete then recreate the property set...YOU WILL LOOSE ALL THOSE EXISTING SETTINGS.

   If it is important to you to avoid possibly loosing all your current settings within the Event Triggers, you will have to plan ahead for this scenario within your code.  If you find a property set by either of those two names(or by its InternalName),  before the code to delete/recreate the set, you will need to capture the data from all the existing properties within that old set.  Capture the property's Value, Name, PropId into a List(Of String) object.  Then add that List(Of String) object into a List(Of List(Of String)) object.  (You could also use Array objects, but working with Lists is usually easier) 

   Then after you've collected all the data from all the existing properties, you can run the (delete then recreate) code.

Then you can start the process of recreating all those properties within the new property set using those stored variables (or the List(Of ) object)

Here's an example of how to use those objects I mentioned:

 

Dim oSettings As New List(Of String)
Dim oListOfSettings As New List(Of List(Of String))

Dim oPropSet As Inventor.PropertySet = ' the property set will already be defined by this point in your code
Dim oProp As Inventor.Property
For Each oProp In oPropSet
	oSettings.Clear
	oSettings.Add(oProp.Value)
	oSettings.Add(oProp.Name)
	oSettings.Add(oProp.PropId)
	oListOfSettings.Add(oSettings)
Next

'Then later when your recreating those properties in the new property set
For Each oLisfOfS As List(Of String) In oListOfSettings
	oPropSet.Add(oLisfOfS(1), oLisfOfS(2), oLisfOfS(3))
Next

 

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 20 of 24

eric.smyth
Participant
Participant

This codeworks great for me, but I am trying to figure out a way to also add the rule to the "Any model parameter change" event. I cant figure out where that value or variable is in the code.

0 Likes