iLogic rules not running, Inventor 2020

iLogic rules not running, Inventor 2020

Scott_Stubbington
Advocate Advocate
418 Views
4 Replies
Message 1 of 5

iLogic rules not running, Inventor 2020

Scott_Stubbington
Advocate
Advocate

Hello, 🙂

I have a Skeleton Parametric assembly that I configure using VBA forms and code, there are a few rules for turning stuff on and off that are iLogic based in assemblies.  Having updated the assembly using VBA the iLogic rules which are driven from the Parameter Change event are not running, I open each assembly not displaying correctly, run the rule and life is good again.  Is it best to only run iLogic rules when the document is active?

 

Thanks in advance for your help.

 

Scott

0 Likes
419 Views
4 Replies
Replies (4)
Message 2 of 5

WCrihfield
Mentor
Mentor

To answer that last question...it depends on the code within the rule.  Some iLogic snippets will only act upon the currently 'active' document, unless you are supplying a specific document for it to act upon within the snippet.  And some document references will always point to the 'active' document too, but there are some document references that are best for use in 'local' rules, that will always refer to the 'local' document.  Here is a link to one of my contribution posts about document references that you might find informative.  It is a fairly common problem when using a lot of automation and dealing with multiple documents within a routine for other rules you are running to not be 'pointing' to the correct target document to be acting upon.  Most of the time it is fixable though, one way or another.

 

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)

0 Likes
Message 3 of 5

Scott_Stubbington
Advocate
Advocate

Hello, 🙂

Thank you for the reply, a good read.

I'm not entirely sure what I've done though as I'm not referencing a document, here is the iLogic code in an assembly document.

varOccR1 = Component.InventorComponent("Fan Guide#1:1")
varOccR2 = Component.InventorComponent("Fan Guide#2:1")
varOccL1 = Component.InventorComponent("Fan Guide#1:2")
varOccL2 = Component.InventorComponent("Fan Guide#2:2")

If Parameter("CentrePanelLeft") = 0 Then	' Right
	varOccR1.BOMStructure =  BOMStructureEnum.kReferenceBOMStructure
	varOccR1.Visible = False
	varOccR2.BOMStructure =  BOMStructureEnum.kReferenceBOMStructure
	varOccR2.Visible = False
	varOccL1.BOMStructure = BOMStructureEnum.kDefaultBOMStructure
	varOccL1.Visible = True
	varOccL2.BOMStructure = BOMStructureEnum.kDefaultBOMStructure
	varOccL2.Visible = True
Else	' Left
	varOccR1.BOMStructure =  BOMStructureEnum.kDefaultBOMStructure
	varOccR1.Visible = True
	varOccR2.BOMStructure =  BOMStructureEnum.kDefaultBOMStructure
	varOccR2.Visible = True
	varOccL1.BOMStructure = BOMStructureEnum.kReferenceBOMStructure
	varOccL1.Visible = False
	varOccL2.BOMStructure = BOMStructureEnum.kReferenceBOMStructure
	varOccL2.Visible = False
End If

As I've not made a reference to a document via code I'm assuming a document is referenced indirectly.

The worst part is, I've attached the rule to an event and I know the event has happened as I see the changes but the rule doesn't run and I'm struggling to workout how document scope impacts in this situation.

Thanks in advance for your help. 

Scott

0 Likes
Message 4 of 5

WCrihfield
Mentor
Mentor

Hi @Scott_Stubbington.  That is usually the problem when using iLogic snippets in your code, without specific document reference, in complex scenarios.  They are not always easy to recognize either.  The 'Component.InventorComponent()' line of code is an iLogic snippet, and allows you to search for a named component without specifying what assembly to look for them in, because it assumes it will be the 'active' assembly.  Seems simpler, and easier to use, but a bit sneaky.  Also the 'Parameter("paramName")' line of code is an iLogic snippet, and again, if you did not specify either a component name or document name, as part of the input variable(s), it will target the 'active' document by default.  To avoid these types of situations in complex scenarios, I often revert back to the better documented, tried & true API route of doing that task, to be sure.

 

I created a slightly alternate version of your local rule that you can try.  If this code works for you on its own, then I'm hoping it will solve the possible document reference issues and cause it to start running as usual.  By the way, maybe as an initial test, before changing the entire local rule code, try putting one or two simple messages into that local rule (maybe one at start and one at end), then run your main routine again.  If those messages show up, that will tell us that the rule is actually running, but just not targeting the right document(s), and therefore does not appear to be running.  If these measures don't fix the main problem, then there may be something else causing the problem.

 

I left some comment lines in there to help you understand what I've done.  Since I was not sure if all 4 of those components were at the top level of the assembly, and I know that iLogic snippet will retrieve components from any level of the assembly, I assumed that they might not be at the top level of the assembly, and created our own Function to retrieve the named components from any level of the assembly.  Only the one I created is definitely being supplied the components from our 'local' assembly, not some other 'active' assembly.

Sub Main
	'get the 'local' document
	Dim oADoc As AssemblyDocument = ThisDoc.Document 'local document
	oOccs = oADoc.ComponentDefinition.Occurrences
	
	'if these components are all on top level, this will work
	'varOccR1 = oOccs.ItemByName("Fan Guide#1:1")
	
	'if they are not all on top level, use this to get them by name (referenced from 'local' document)
	'use our custom Function below to retrieved named components from any level of assembly
	varOccR1 = GetNamedComponent(oOccs, "Fan Guide#1:1")
	varOccR2 = GetNamedComponent(oOccs, "Fan Guide#2:1")
	varOccL1 = GetNamedComponent(oOccs, "Fan Guide#1:2")
	varOccL2 = GetNamedComponent(oOccs, "Fan Guide#2:2")

	'make sure they were found, if not let user know then exit rule
	If IsNothing(varOccR1) Or IsNothing(varOccR2) Or _
		IsNothing(varOccL1) Or IsNothing(varOccL2) Then
		MsgBox("One or more of the specified components were not found.  Exiting rule.", vbCritical, "iLogic")
		Exit Sub
	End If

	'get the parameter from this local document specifically
	'if not found, let user know then exit rule
	Dim oParam As Inventor.Parameter
	Try
		oParam = oADoc.ComponentDefinition.Parameters.Item("CentrePanelLeft")
	Catch oEx As Exception
		MsgBox("Could not fine the specified Parameter.  Exiting rule." & vbCrLf & _
		oEx.Message & vbCrLf & oEx.StackTrace, vbCritical, "iLogic")
		Exit Sub
	End Try

	'If oParam.Expression = "0" Then	' Right
	If oParam.Value = 0 Then ' Right
		varOccR1.BOMStructure =  BOMStructureEnum.kReferenceBOMStructure
		varOccR1.Visible = False
		varOccR2.BOMStructure =  BOMStructureEnum.kReferenceBOMStructure
		varOccR2.Visible = False
		varOccL1.BOMStructure = BOMStructureEnum.kDefaultBOMStructure
		varOccL1.Visible = True
		varOccL2.BOMStructure = BOMStructureEnum.kDefaultBOMStructure
		varOccL2.Visible = True
	Else	' Left
		varOccR1.BOMStructure =  BOMStructureEnum.kDefaultBOMStructure
		varOccR1.Visible = True
		varOccR2.BOMStructure =  BOMStructureEnum.kDefaultBOMStructure
		varOccR2.Visible = True
		varOccL1.BOMStructure = BOMStructureEnum.kReferenceBOMStructure
		varOccL1.Visible = False
		varOccL2.BOMStructure = BOMStructureEnum.kReferenceBOMStructure
		varOccL2.Visible = False
	End If
End Sub

Function GetNamedComponent(oComps As ComponentOccurrences, oCompnentName As String) As ComponentOccurrence
	For Each oComp As ComponentOccurrence In oComps
		If oComp.Name = oCompnentName Then
			'exit the function, while returning that component
			Return oComp
		End If
		If oComp.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
			oSComp = GetNamedComponent(oComp.Definition.Occurrences, oCompnentName)
			If Not IsNothing(oSComp) Then Return oSComp 'exit the function, while returning that component
		End If
	Next
	'if code reaches this point, the component was not found, so return Nothing
	Return Nothing
End Function

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 5 of 5

Scott_Stubbington
Advocate
Advocate

Hello, 🙂

I really don't understand why the code "trigger = CentrePanelLeft" at the top of my code changed the rule running when I have already built that association in Manage -> Event Triggers, short version, all is now good.

Long version, I tried the Sub Main as suggested being more specific with documents and that didn't work so I then plastered Msgbox statements in all the pertinent assembly iLogic rules, updated the assembly and found not one of the Msgbox's displayed, I then started exploring the trigger code and found it worked and here I am.  I am back to using snippets as using the trigger code isn't allowed when the editor when exposed to Sub Main, the editor is expecting a Class statement in this position, I think. 😁

Thank you for your help @WCrihfield., I really appreciate the effort and the code you provided. 🙂

0 Likes