Trigger an existing rule in all parts from assembly level with ilogic.

Trigger an existing rule in all parts from assembly level with ilogic.

Anonymous
Not applicable
2,780 Views
15 Replies
Message 1 of 16

Trigger an existing rule in all parts from assembly level with ilogic.

Anonymous
Not applicable

 I need a rule in all my components to trigger after I resize my adaptive assembly. The rule gets the length, width, depth extents of the parts. After resizing i currently have to edit each part individually and run the rule again to update the dimensions. Basically I want to automatically populate the "PartA:1" in this snippit: iLogicVb.RunRule("PartA:1", "ruleName") for all of the parts in the assembly document. 

 

The code for this is beyond me.

 

Would greatly appreciate any help i can get.

 

Thanks,

0 Likes
Accepted solutions (1)
2,781 Views
15 Replies
Replies (15)
Message 2 of 16

mrattray
Advisor
Advisor
Accepted solution

You could try adding the rule in each part to the iLogic event triggers list. I like to use the "before save" trigger. Is this something that could work for you?

 

If not, iterating through all of the parts is easy enough. Here's a snippet:

 

Dim oComp As ComponentOccurrence
Dim oComps As ComponentOccurrences

oComps = ThisDoc.Document.ComponentDefinition.Occurrences

For Each oComp In oComps
	iLogicVb.RunRule(oComp, "ruleName")
Next

 

Mike (not Matt) Rattray

Message 3 of 16

Anonymous
Not applicable

That did the trick! I had the other triggers, parameter, geometry, and property changes checked but not the before save. I see how that would be the one to use. Much obliged!

 

I would like to have the option to run the occurrence code as well. Does the "ruleName" in the snippit need to be an external rule? The one i entered resides in the part files and when i ran the rule it gave me a can't find "ruleName" error.

0 Likes
Message 4 of 16

mrattray
Advisor
Advisor
The opposite is true, really. That will only work for internal rules. The function RunExternalRule() is required for external rules. Can you post the code as you have it? I suspect a formatting error.
Mike (not Matt) Rattray

0 Likes
Message 5 of 16

Anonymous
Not applicable
 
 
 
Dim oComp As ComponentOccurrence 
Dim oComps As ComponentOccurrences 

oComps = ThisDoc.Document.ComponentDefinition.Occurrences 

For Each oComp In oComps 
    iLogicVb.RunRule(oComp, "UPDATE") 
Next
The rule "UPDATE" is in all the ipt files and its function is to run three other rules. There are no rules in the iam file.

RunRule("System.__ComObject", "UPDATE"): Cannot find a rule with the name: "UPDATE" is the error I get.

Thanks,

0 Likes
Message 6 of 16

mrattray
Advisor
Advisor

I have two theories:
1) Are sure the rule is named "UPDATE"? This is case sensitive, and if it doesn't match perfectly (trailing spaces, etc.) it wont find it.
2) Perhaps the code is looking at non-standard components?If you're sure that #1 isn't the problem we can modify the code to check for things like virtual components and weld beads.

Mike (not Matt) Rattray

0 Likes
Message 7 of 16

Anonymous
Not applicable
I found this code on a different thread. Courtesy (theo.bot) This did trigger the UPDATE rule in all the components. If you can tell by this one what was missing in the one you gave me let me know. I am new to iLogic wizardry and would like to learn as much as i can in these situations. 

Thanks a ton for your assistance mrrattray! Much appreciated! If I could email some beer I would.



Sub
Main auto = iLogicVb.Automation ' Get the active assembly. Dim oAsmDoc As AssemblyDocument oAsmDoc = ThisApplication.ActiveDocument ' Get all of the referenced documents. Dim oRefDocs As DocumentsEnumerator oRefDocs = oAsmDoc.AllReferencedDocuments ' Iterate through the list of documents. Dim oRefDoc As Document For Each oRefDoc In oRefDocs Dim ruleName As String ruleName = "UPDATE" Dim rule As Object rule = auto.GetRule(oRefDoc, "UPDATE") If (rule Is Nothing) Then 'Call MsgBox("No rule named " & ruleName & " was found in the document.") Else MessageBox.Show(rule.Name, oRefdoc.displayname) Dim i As Integer i = auto.RunRuleDirect(rule) End If Next End Sub
0 Likes
Message 8 of 16

mrattray
Advisor
Advisor
I think I was on the right track. I think my code was trying to run on components that aren't real documents. Since Theo's code gets the referenced documents directly, it wouldn't have that problem.
Mike (not Matt) Rattray

0 Likes
Message 9 of 16

Anonymous
Not applicable

I have a similar code which runs a rule that exists in all parts referenced in a drawing. This works fine however, i would also like it to check to see if the rule exists in the part files and, if not, create the rule and then execute it.

 

Ideally, the iLogic rule would just be created in every related file referenced in a drawing & then run.

 

Any ideas?

 

This thread seemed pretty similar to the iLogic i was trying to create.

 

Thanks,

 

This is my code so far;

 

 

Rule that runs in Drawings level:

 

'Define the open document
Dim oDoc As Document
oDoc = ThisDoc.Document

'Look at all of the files referenced in the open document
Dim docFile As Document
For Each docFile In oDoc.AllReferencedDocuments

'open the indexed file
'true opens the file normaly
'false opens the file programatically without generating the graphics
ThisApplication.Documents.Open(docFile.FullFileName, True)


'run rule in the drawing document
auto = iLogicVb.Automation
auto.RunRule(docFile, "Remove Precision and mm Rule")

docFile.Save
docFile.Close

iLogicVb.UpdateWhenDone = True

Next

 

 

Rule that gets fired in all parts as a result of drawing level rule being run:

 

Dim oPartDoc As PartDocument
oPartDoc=ThisApplication.ActiveDocument
Dim oUserParam As UserParameter
Dim oFormat As CustomPropertyFormat
']
'*** For Each User Parameter....
'[
For Each oUserParam In oPartDoc.ComponentDefinition.Parameters.UserParameters

    oUserParamName = oUserParam.Name
If oUserParamName = "G_L" Then
 If oUserParam.ExposedAsProperty Then
 If oUserParam.Units="mm"Then
 oFormat=oUserParam.CustomPropertyFormat
 oUserParam.ExposedAsProperty=True
 oFormat.PropertyType=Inventor.CustomPropertyTypeEnum.kTextPropertyType
 oFormat.Precision=Inventor.CustomPropertyPrecisionEnum.kTwoDecimalPlacesPrecision
 oFormat.Units="mm"
 oFormat.ShowUnitsString=False
 oFormat.ShowLeadingZeros=True
 oFormat.ShowTrailingZeros=False
End If
 End If
 Else
 End If
Next oUserParam

0 Likes
Message 10 of 16

Anonymous
Not applicable

Hello there, 

 

Did anyone get anywhere with this please?

 

I would like to add an external rule which adds LENGTH=L and WIDTH=WW where LENGTH is a parameter and L is an equation produced by editing dimension property. Same applies to the WIDTH and WW. (This one is done ad part level)

 

This can solve many problems I am having with old inventor files. 

 

 

 

0 Likes
Message 11 of 16

MechMachineMan
Advisor
Advisor
Maheta, here is a snippet for you to work off of:

Dim oDecParam As String
oDecParam="DEC" & UCase(r)

oParam=oUserP.AddByExpression(oDecParam, r, UnitsTypeEnum.kInchLengthUnits)


oParam.ExposedAsProperty=True

oFormat= oParam.CustomPropertyFormat


oFormat.Precision=Inventor.CustomPropertyPrecisionEnum.kTwoDecimalPlacesPrecision
oFormat.ShowLeadingZeros=False
oFormat.ShowTrailingZeros=True
oFormat.ShowUnitsString=False

--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
Message 12 of 16

hamza_khan1
Contributor
Contributor

I ran this rule, and had 65 parts inside assembly.

I had to click "okay" on message box 65 times. Is there anyway i run this rule and it doesn't pop message and does the job. Thanks in advance.

0 Likes
Message 13 of 16

Anonymous
Not applicable

Hello Hamza.khan

 

I run the following rule that will skip over that message box for each part. A message box will prompt only if the ipt does not contain the rule specified. Hope this works for you! 

 

Sub Main
auto = iLogicVb.Automation

' Get the active assembly. 
Dim oAsmDoc As AssemblyDocument 
oAsmDoc = ThisApplication.ActiveDocument 

' Get all of the referenced documents. 
Dim oRefDocs As DocumentsEnumerator 
oRefDocs = oAsmDoc.AllReferencedDocuments 

' Iterate through the list of documents. 
Dim oRefDoc As Document 

For Each oRefDoc In oRefDocs

	Dim ruleName As String
	ruleName = "UPDATE"
	Dim rule As Object
	rule = auto.GetRule(oRefDoc, "UPDATE")
	If (rule Is Nothing) Then
	 'Call MsgBox("No rule named " & ruleName & " was found in the document.")
	Else
	 'MessageBox.Show(rule.Name, oRefdoc.displayname)
	
	Dim i As Integer
	i = auto.RunRuleDirect(rule)
	
	End If

Next 
End Su 

 

0 Likes
Message 14 of 16

hamza_khan1
Contributor
Contributor

@Anonymous  Thank you so much. 

If I have sub assemblies in the main assembly where I am running this rule, will it run through the parts of those sub assemblies as well?

 Thanks again.

 

0 Likes
Message 15 of 16

Anonymous
Not applicable
@hamza_khan1, yes it will cycle through all parts in assembly including sub assemblies.
Message 16 of 16

AMN3161
Advocate
Advocate

hello from 2020, this was really helpful thank you

0 Likes