Rule for displaying a list of rules in each part in an Assembly

Rule for displaying a list of rules in each part in an Assembly

sachin_kumar6CZM2
Contributor Contributor
577 Views
7 Replies
Message 1 of 8

Rule for displaying a list of rules in each part in an Assembly

sachin_kumar6CZM2
Contributor
Contributor

Hello Everyone,

My task is to create a rule which allows me to enter a rule name and click the part in an assembly and it will run that particular rule in that assembly. I already have the rule and it is working fine. but i am looking for a little of modification like to give a list of all rules in each part in the assembly and then select the part to run that particular rule. could anyone please help me with that. below is the code for running the rule for selected part. the code will not display the list of rules and we have to manually type the rule to run.

Dim doc As AssemblyDocument

doc = ThisApplication.ActiveDocument

Dim RuleName As String

RuleName = InputBox("INPUT RULE", "ASFD-DESIGN TECHNOLOGY DEPARTMENT", "Type Here Please")


If doc.DocumentType = kPartDocumentObject Then 
	MessageBox.Show("This rule can only be run in an assembly file - exiting rule", "")
Return
End If

Dim targetOcc As ComponentOccurrence= Nothing
Do While True
	' Note the below line will prompt the user to pick an assembly occurrence (this can be a single part file or a sub-assembly of the active document...
	targetOcc = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kAssemblyOccurrenceFilter, "Select Occurrence to Modify...(Press ESC to cancel)") 
	If Not targetOcc Is Nothing Then
		Exit Do
	Else
		Dim Res As MsgBoxResult = MsgBox("No model selected - exit?", 36, "")
		If Res = vbYes Then
			Return
		Else
			' Do nothing - keep on looping...
		End If
	End If
Loop

Try
	

iLogicVb.RunRule(targetOcc.Name, RuleName)

Catch
	
MsgBox("Error - No Rule Exit with this name in the Selection", 64, "")

End Try
  

 

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

WCrihfield
Mentor
Mentor

Hi @sachin_kumar6CZM2.  That sounds a bit odd, so I am not sure I fully understand what you are trying to achieve in the end, but below is an iLogic code example that I think should do what you seem to be wanting.

Sub Main
	Dim oDoc As Inventor.Document = ThisDoc.Document
	Dim oAuto As IiLogicAutomation = iLogicVb.Automation
	For Each oRefDoc As Inventor.Document In oDoc.AllReferencedDocuments
		Dim oRules As IEnumerable = oAuto.Rules(oRefDoc)
		If oRules Is Nothing Then Continue For
		Dim oRuleNames As New List(Of String)
		For Each oRule As iLogicRule In oRules
			oRuleNames.Add(oRule.Name)
		Next
		Dim sChosenRuleName As String = InputListBox("Choose Rule.", oRuleNames, "", "Rules In " & oRefDoc.DisplayName)
		If sChosenRuleName = "" Then Continue For
		oAuto.RunRule(oRefDoc, sChosenRuleName)
		If oRefDoc.RequiresUpdate Then oRefDoc.Update2(True)
	Next
	If oDoc.RequiresUpdate Then oDoc.Update2(True)
End Sub

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

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 8

sachin_kumar6CZM2
Contributor
Contributor

Thanku very much for your support. What I am trying to achieve is, there will be many rules in a part. Assembly will have 100s of different parts. if I have to run a particular rules for a particular part, i was trying to do something without opening that part separately. if I have a rule, i can run the rule in the assembly itself and it will open a list of all rules in each part and I can select which rule , I need to run in a particular part.

The rule that you have provided works fine, but the issue is it the list prompting separately for each part. i was looking for a list that will have rules of all part in one single list. 

 

0 Likes
Message 4 of 8

Michael.Navara
Advisor
Advisor

You are looking for something like this?

 
 

2024-07-26_14-59-08.png

 

0 Likes
Message 5 of 8

WCrihfield
Mentor
Mentor

We could definitely make a single list containing the names of every 'internal' (saved within the document, not external) iLogic rule found in every referenced document.  However, a name is just a name, not the rule itself.  So, if you did choose one of the rule names, there is nothing behind that name that says which part  the rule is in, or where to find it.  If you just used a line of code from the main assembly like 'iLogicVb.RunRule(sChosenRuleName)', that would be looking for that rule within the assembly itself, not within any of the other referenced parts.  So, if a rule by that name did not exist in the main assembly document directly, it would not be found, and would not run.  Besides that, how would this one internal rule from some referenced part know which document to focus its attention on.  Should that rule only effect the referenced document that it was found in, or should it only effect the main assembly document, or some other document?

 

Edit:  Also, I can't be sure because I am not familiar with your rule naming conventions, but it might be possible for there to be more than one referenced document with an internal iLogic rule in it with the same rule name.  If that were true, then this single overall list of all rules could contain two names that are the same, but the rules would be in different referenced documents, and may do different things.

Another thought did come to mind though.  We could use some sort of 2-factor list/collection to collect not only the rule's name, but the rule object itself also, as the secondary object that is associated with the primary in a pair.  Something like a NameValueMap, where duplicate rule names would not be a problem, or a List(Of KeyValuePair(Of String, Object)), because a Dictionary would not allow duplicate 'Keys'.  Then maybe use the 'iLogicVb.Automation.RunRuleDirect(oRule)' method could be used.  But even then, no guarantees about that rule only effecting the document you want it to effect.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 6 of 8

Curtis_Waguespack
Consultant
Consultant
Accepted solution

@sachin_kumar6CZM2, building upon @WCrihfield's example, maybe this would work?

Curtis_Waguespack_0-1722003014514.png

 



Sub Main

	Dim oRuleNames As New List(Of String)
	Dim oDoc As Inventor.Document = ThisDoc.Document
	Dim oAuto As IiLogicAutomation = iLogicVb.Automation
	For Each oRefDoc As Inventor.Document In oDoc.AllReferencedDocuments
		Dim oDocName As String = IO.Path.GetFileName(oRefDoc.FullFileName)
		Dim oRules As IEnumerable = oAuto.Rules(oRefDoc)
		If oRules Is Nothing Then Continue For

		For Each oRule As iLogicRule In oRules
			oRuleNames.Add(oDocName & "    |    " & oRule.Name)
		Next
		oRuleNames.Add("")
	Next
	If oDoc.RequiresUpdate Then oDoc.Update2(True)

	Dim sListLabel As String = "Document Name     |        Rule Name"
	Dim sChosenRuleName As String = InputListBox("Choose Rule.", oRuleNames, "", "iLogic", sListLabel)
	If sChosenRuleName = "" Then Exit Sub

	Dim sSplit = Split(sChosenRuleName, "|")
	Dim DocName As String = Trim(sSplit(0))
	Dim RuleName As String = Trim(sSplit(1))

	For Each oRefDoc As Inventor.Document In oDoc.AllReferencedDocuments
		If IO.Path.GetFileName(oRefDoc.FullFileName) = DocName Then
			TargetDoc = oRefDoc
			Exit For
		End If
	Next

	If Not TargetDoc Is Nothing Then oAuto.RunRule(TargetDoc, RuleName)

End Sub

 

EESignature

Message 7 of 8

WCrihfield
Mentor
Mentor
Accepted solution

Not bad @Curtis_Waguespack.  After my last edit about the potential for storing the rule object along with the rule's name in a single, two factor list, I figured that should not be that difficult to go ahead and mock-up, so here is an example like that also.  However, even though this method of storage would technically allow us to store multiple rules with the same names from different documents, it is not really handling that possibility very well after the selection has been made, because it would just get the first one by the chosen name that it comes to in the list, and try to run that one.  Perhaps more code could be added to make it go through the whole loop first, and if duplicates were present, let the user know, and if not, proceed as usual without any extra notification.

Sub Main
	Dim oDoc As Inventor.Document = ThisDoc.Document
	Dim oAuto As IiLogicAutomation = iLogicVb.Automation
	Dim oRulesList As New List(Of KeyValuePair(Of String, iLogicRule))
	Dim oRuleNames As New List(Of String)
	For Each oRefDoc As Inventor.Document In oDoc.AllReferencedDocuments
		Dim oRules As IEnumerable = oAuto.Rules(oRefDoc)
		If oRules Is Nothing Then Continue For
		For Each oRule As iLogicRule In oRules
			oRuleNames.Add(oRule.Name)
			oRulesList.Add(New KeyValuePair(Of String, iLogicRule)(oRule.Name, oRule))
		Next
	Next
	Dim sChosenRuleName As String = InputListBox("Choose Rule.", oRuleNames, "", "All Rules In References")
	If sChosenRuleName = "" Then Return
	Dim oChosenRule As iLogicRule = Nothing
	For Each oEntry In oRulesList
		If oEntry.Key = sChosenRuleName Then oChosenRule = oEntry.Value
	Next
	oAuto.RunRuleDirect(oChosenRule)
	If oDoc.RequiresUpdate Then oDoc.Update2(True)
End Sub

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

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 8 of 8

sachin_kumar6CZM2
Contributor
Contributor

@Curtis_Waguespack .Thanku for your solution, this was exactly What I am looking for. it will help me to run the rule in the part without actually opening that particular part.

@WCrihfield & @Michael.Navara , thanku for the support.

0 Likes