iLogic rules triggered by events behaves differently than triggered manually

iLogic rules triggered by events behaves differently than triggered manually

kmcga
Contributor Contributor
484 Views
7 Replies
Message 1 of 8

iLogic rules triggered by events behaves differently than triggered manually

kmcga
Contributor
Contributor

Dear Community,

 

I need to implement routine which after event trigger “After Open Document” stores current Representation View of assembly in custom iProperty then switch to Default Representation View. Then “Before Save Document” event trigger restoring the original Representation View of the assembly.

To do so I wrote 3 iLogic rules:

 

Rule 1 – checking if custom property exists, if not creating it with empty value. Reading current Rep View and store as string in the custom property.

Dim PropertyName1 As String = "RepView"

oCustomPropertySet = ThisApplication.ActiveDocument.PropertySets.Item("Inventor User Defined Properties")



'look for custom iproperty and try to use it

Try

oProp = oCustomPropertySet.Item(PropertyName1)

Catch

' iproperty not found , create and assign value

oCustomPropertySet.Add("", PropertyName1)

End Try



Dim odoc As Document

Dim ocompdef As ComponentDefinition

Dim paramNames As String() = {"x" }

odoc = ThisApplication.ActiveDocument

ocompdef = odoc.ComponentDefinition



'current rep view read

x = ocompdef.RepresentationsManager.ActiveDesignViewRepresentation.Name

odoc.PropertySets.Item("Inventor User Defined Properties").Item("RepView").Value = x

 

 

Rule 2 – changing Rep View to Default.

Dim odoc As Document

Dim ocompdef As ComponentDefinition

odoc = ThisApplication.ActiveDocument

ocompdef = odoc.ComponentDefinition



''change rep view to Default

Try

ocompdef.RepresentationsManager.DesignViewRepresentations.Item("Default").Activate

Catch

MessageBox.Show("This viewrep does not exist", "Error")

End Try

 

Rule 3 – restoring original Rep View.

Dim odoc As Document

Dim ocompdef As ComponentDefinition

Dim paramNames As String() = {"y" }

odoc = ThisApplication.ActiveDocument

ocompdef = odoc.ComponentDefinition

''change rep view to original

y = odoc.PropertySets.Item("Inventor User Defined Properties").Item("RepView").Value

Try

ocompdef.RepresentationsManager.DesignViewRepresentations.Item(y).Activate

Catch

MessageBox.Show("This viewrep does not exist", "Error")

End Try

 

Following external rules executed manually in sequence Rule 1, Rule 2, Rule 3 work well. When I try to assign Event Trigger to each of them, so that Rule 1 and Rule 2 to be triggered “After Open Document” and Rule 3 to be triggered “Before Save Document” the routine fails. The effect is that I receive Default value in custom iProperty and assemblies don’t come back to original Rep View, just staying in Default Rep View.

I tried to bypass it somehow by creating dummy external rule which triggers the rules and combine Rule 1 and Rule 2 in single rule without success.

 

Because I am rather novice in writing iLogic rules please support me and correct if I am wrong. I would like to understand why the rules triggered manually in proper sequence works but automatically not.

Thank you in advance.

 

Best regards

Krzysztof

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

WCrihfield
Mentor
Mentor

Hi @kmcga.  There are a few things to keep in mind in a situation like this.  The first thing I would suggest is changing from using 'ThisApplication.ActiveDocument' to using 'ThisDoc.Document' for identifying the document that you want the rule to be working with.  There are various reasons/benefits to doing it this way.  Next is the actual order in which the rules are added under the event within the Event Triggers dialog.  They are supposed to run in the order in which they were added under the event name in that dialog, but if another rule is somehow automatically triggered to run while the first one is running, it may cause problems.  And yes, combining rule 1 and rule 2 into one rule is definitely the way to go here.  I have an example of how this would look below.

Rule 1:

Dim oDoc As Document = ThisDoc.Document
Dim oCProps As PropertySet = oDoc.PropertySets.Item("Inventor User Defined Properties")
Dim oRepViewProp As Inventor.Property
Dim oRepViewPropName As String = "RepView"
Try
	oRepViewProp = oCProps.Item(oRepViewName)
Catch
	oRepViewProp = oCProps.Add("", oRepViewName)
End Try
Dim oRepsMgr As RepresentationsManager = oDoc.ComponentDefinition.RepresentationsManager
Dim oActiveRepViewName As String = oRepsMgr.ActiveDesignViewRepresentation.Name
oRepViewProp.Value = oActiveRepViewName
Try
	oRepsMgr.DesignViewRepresentations.Item("Default").Activate
Catch
	MsgBox("A viewrep named 'Default' could not be found.", vbCritical, "Default ViewRep Missing")
End Try

Rule 2: (before save)

Dim oDoc As Document = ThisDoc.Document
Dim oCProps As PropertySet = oDoc.PropertySets.Item("Inventor User Defined Properties")
Dim oRepViewProp As Inventor.Property
Dim oRepViewPropName As String = "RepView"
Try
	oRepViewProp = oCProps.Item(oRepViewName)
Catch
	MsgBox("Custom iProp 'RepView' not found.  Exiting rule.", vbCritical, "iProp Not Found")
	Exit Sub 'or Return
End Try
Dim oRepsMgr As RepresentationsManager = oDoc.ComponentDefinition.RepresentationsManager
Dim oActiveRepViewName As String = oRepsMgr.ActiveDesignViewRepresentation.Name
If oActiveRepViewName <> oRepViewProp.Value Then
	Try
		oRepsMgr.DesignViewRepresentations.Item(oRepViewProp.Value).Activate
	Catch
		MsgBox("A viewrep named '" & oRepViewProp.Value & "' could not be found/activated.", vbCritical, "ViewRep Missing")
	End Try
End If

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 8

kmcga
Contributor
Contributor

Hi @WCrihfield,

 

thank you for your prompt reply.

I ran Rule 1 proposed by you and I have two observations:

- the rule creates custom iProperty called "Inventor User Defined Properties 10",

kmcga_1-1662540770069.png

 

- another rule run creates next custom iProperty with subsequent number in this case "Inventor User Defined Properties 11".

 

I can't run Rule 2 since the custom iProperty is not recognized. I can't also find an error in the Rule 1 and understand why the custom iProperty is not called "RepView".

I think that the rule should not create next custom properties but look if "RepView" is created and store the value there.

 

Thanks to you I think I am closer to reach a target but I need your further support to find an error.

 

Best regards

Krzysztof

0 Likes
Message 4 of 8

WCrihfield
Mentor
Mentor
Accepted solution

Hi @kmcga.  I do see a mistake in the code I posted.  While creating the code, I changed the name of the one variable that holds the name of the custom iProperty from "oRepViewName" to "oRepViewPropName", but forgot to find/replace that variable in the other two locations it was being used after renaming it.  Because of that, the variable being used in the Try...Catch block had no value, which caused it to not find the existing custom iProperty on the Try side, and always create a new one on the Catch side, but without a specified name, which causes the system to generate its own new name for the new custom iProperty it creates.  That error certainly caused an interesting outcome. 🙄

Here are the corrected rule codes:

Rule 1

 

Dim oDoc As Document = ThisDoc.Document
Dim oCProps As PropertySet = oDoc.PropertySets.Item("Inventor User Defined Properties")
Dim oRepViewProp As Inventor.Property
Dim oRepViewPropName As String = "RepView"
Try
	oRepViewProp = oCProps.Item(oRepViewPropName)
Catch
	oRepViewProp = oCProps.Add("", oRepViewPropName)
End Try
Dim oRepsMgr As RepresentationsManager = oDoc.ComponentDefinition.RepresentationsManager
Dim oActiveRepViewName As String = oRepsMgr.ActiveDesignViewRepresentation.Name
oRepViewProp.Value = oActiveRepViewName
Try
	oRepsMgr.DesignViewRepresentations.Item("Default").Activate
Catch
	MsgBox("A viewrep named 'Default' could not be found.", vbCritical, "Default ViewRep Missing")
End Try

 

Rule 2

 

Dim oDoc As Document = ThisDoc.Document
Dim oCProps As PropertySet = oDoc.PropertySets.Item("Inventor User Defined Properties")
Dim oRepViewProp As Inventor.Property
Dim oRepViewPropName As String = "RepView"
Try
	oRepViewProp = oCProps.Item(oRepViewPropName)
Catch
	MsgBox("Custom iProp '" & oRepViewPropName & "' not found.  Exiting rule.", vbCritical, "iProp Not Found")
	Exit Sub 'or Return
End Try
Dim oRepsMgr As RepresentationsManager = oDoc.ComponentDefinition.RepresentationsManager
Dim oActiveRepViewName As String = oRepsMgr.ActiveDesignViewRepresentation.Name
If oActiveRepViewName <> oRepViewProp.Value Then
	Try
		oRepsMgr.DesignViewRepresentations.Item(oRepViewProp.Value).Activate
	Catch
		MsgBox("A viewrep named '" & oRepViewProp.Value & "' could not be found/activated.", vbCritical, "ViewRep Missing")
	End Try
End If

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 5 of 8

kmcga
Contributor
Contributor

Hi @WCrihfield,

 

I corrected Rule 1 and it works but it creates custom iProperty for every subassembly - I want to avoid it, only top assembly should have custom iProp created.

Dim oDoc As Document = ThisDoc.Document
Dim oCProps As PropertySet = oDoc.PropertySets.Item("Inventor User Defined Properties")
Dim oRepViewProp As Inventor.Property
Dim oRepViewPropName As String = "RepView"
Try
	oRepViewProp = oCProps.Item(oRepViewPropName)
Catch
	oRepViewProp = oCProps.Add("", oRepViewPropName)
End Try
Dim oRepsMgr As RepresentationsManager = oDoc.ComponentDefinition.RepresentationsManager
Dim oActiveRepViewName As String = oRepsMgr.ActiveDesignViewRepresentation.Name
oRepViewProp.Value = oActiveRepViewName
Try
	oRepsMgr.DesignViewRepresentations.Item("Default").Activate
Catch
	MsgBox("A viewrep named 'Default' could not be found.", vbCritical, "Default ViewRep Missing")
End Try

 

I checked Rule 2 and noticed that it try to change every subassembly's RepView so I get MsgBoxes during save that my RepView doesn't exist.

kmcga_1-1662548906681.png

kmcga_2-1662549605945.png

kmcga_3-1662549621173.png

 

I want to avoid such a behavior since my PDM system gets information about modifications and put a modification flag on each subassembly.

I assume that it's related to declaration Dim oDoc As Document = ThisDoc.Document.

Which declaration should I use then?

 

Best regards

Krzysztof

0 Likes
Message 6 of 8

kmcga
Contributor
Contributor

Hi @WCrihfield,

 

thank you, I have just posted my findings then I had seen your comment. 🙂

 

 

"I corrected Rule 1 and it works but it creates custom iProperty for every subassembly - I want to avoid it, only top assembly should have custom iProp created.

 

 

 

I checked Rule 2 and noticed that it try to change every subassembly's RepView so I get MsgBoxes during save that my RepView doesn't exist.

kmcga_1-1662548906681.png

kmcga_2-1662549605945.png

kmcga_3-1662549621173.png

 

I want to avoid such a behavior since my PDM system gets information about modifications and put a modification flag on each subassembly.

I assume that it's related to declaration Dim oDoc As Document = ThisDoc.Document.

Which declaration should I use then?"

 

Best regards

Krzysztof

0 Likes
Message 7 of 8

WCrihfield
Mentor
Mentor

Which tab of the Event Triggers dialog are you adding these rules under?  If you only want these rules to effect the top level assembly, then you would want to have that top level assembly open and the currently active document, then open the Event Triggers dialog, and put them under those two events on the 'This Document' tab.  If you put these rules under those events on the 'All Documents' or 'Assemblies' tab of the Event Triggers dialog, that will effect either all documents, or all assemblies, not just the top assembly.  If you want to be able to put something like this on the 'Assemblies' tab, where it would effect all assemblies, but you don't want the codes to treat all assemblies the same way, then you have to find a way of identifying which assemblies you want it to act upon, and which ones you don't want it to effect, or you may have to use some user interactions within the code, like asking the user a pop-up question when in doubt about how to proceed when certain condition(s) are met.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 8 of 8

kmcga
Contributor
Contributor

You are absolutely right. I put it into assemblies card.

Thank you for the support!

0 Likes