Need to run an external rule for all parts of an assembly

Need to run an external rule for all parts of an assembly

Anonymous
Not applicable
4,235 Views
10 Replies
Message 1 of 11

Need to run an external rule for all parts of an assembly

Anonymous
Not applicable

Hello

I am trying to add some custom iproperties to all the sheet metal parts in an assembly using an external rule.

 

Here's the assembly rule:

 

Dim aDoc As AssemblyDocument
aDoc = ThisApplication.ActiveDocument

Dim iDoc As Document

For Each iDoc In aDoc.AllReferencedDocuments
iLogicVb.RunExternalRule("Test")
 Next

 And here's the external rule:

 

'Assigning Sheet metal Parameters
iProperties.Value("Custom", "LENGTH") = Round(SheetMetal.FlatExtentsLength)  'LENGTH OF THE PART FOR CUTTIN LIST
iProperties.Value("Custom", "WIDTH") = Round(SheetMetal.FlatExtentsWidth)   'WIDTH OF THE PART FOR CUTTING LIST
iProperties.Value("Custom", "THICKNESS") = Thickness 'MODEL PROPERTY EQUALS TO CUSTOM IPROPERTY

 
iProperties.Value("Custom", "AREA") = Round((SheetMetal.FlatExtentsArea / 92903.04),2) 'AREA OF THE PART FOR CUTTING LIST IN SFT
 
 
iProperties.Value("Custom", "MATERIAL") = iProperties.Material
 
iLogicVb.UpdateWhenDone = True

 After running the assembly rule, there are no changes at all to any parts of the assembly. Only custom iproperties are added to assembly as 0.0

 

P.S I am a newbie in iLogic and wrote the codes after learning from forums. Please feel free to point out any silly mistakes.

 

Any help is appreciated.

Thanks,

kp

0 Likes
Accepted solutions (1)
4,236 Views
10 Replies
Replies (10)
Message 2 of 11

martin.jacko
Autodesk
Autodesk

Hi,

 

I am not sure iLogic is the right way here.

You can do it with classic VBA script.

 

There is short example how to open documents with VBA.

Public Declare PtrSafe Function GetTickCount Lib "kernel32" () As Long
Sub Main()
    mlngStart = GetTickCount
    Dim oDoc As Document
    For i = 0 To 4
    ThisApplication.Documents.Open ("D:\tu\" & i & ".ipt")
    Next i
    EndTimer = (GetTickCount - mlngStart)
    MsgBox "open time  " & EndTimer   
    For Each oDoc In ThisApplication.Documents
        oDoc.Close (True)
    Next oDoc
End Sub

 

 

D:\tu\ folder must contains files

0.ipt 1.ipt  ..... 5.ipt

 

Martin

0 Likes
Message 3 of 11

MechMachineMan
Advisor
Advisor
Accepted solution

Hello,

 

Your current writing of the rule doesn't know the CONTEXT that the iLogic rule is supposed to be run in, so it's currently the equivalent of saying "run the rule where it is, 100 times".

 

To fix that, you need to supply the iLogic Rule call with some CONTEXT (ie; the DOCUMENT the rule is supposed to effect).

 

Dim aDoc As AssemblyDocument
aDoc = ThisApplication.ActiveDocument

Dim iDoc As Document

For Each iDoc In aDoc.AllReferencedDocuments
    iLogicVb.RunExternalRule(iDoc, "Test")
Next

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

Anonymous
Not applicable

Thanks

That helped 🙂

 

Just one more question

 

What's the difference in running the external rule like:

iLogicVb.RunExternalRule(iDoc, "Test")

 

And using automation like:

auto=iLogicVb.Automation

auto.RunExternalRule(iDoc, "Test")

 

 

0 Likes
Message 5 of 11

matthew.johnson200
Enthusiast
Enthusiast

This rule is exactly what i'm looking for also, however when i try rule this rule i get this error:

 

Unable to cast COM object of type 'System.__ComObject' to class type 'System.String'. 
Instances of types that represent COM components cannot be cast to types that do not represent COM components; however they can be cast to interfaces as long as the underlying COM component supports QueryInterface calls for the IID of the interface.
0 Likes
Message 6 of 11

Anonymous
Not applicable

Hi Matthew

 

It's been a while since I have posted this query. I am afraid I won't be able to help you with your specific error. But, below is the code that worked for me:

 

ASSEMBLY RULE

Dim aDoc As AssemblyDocument

aDoc = ThisApplication.ActiveDocument

 

Dim iDoc As Document

 

For Each iDoc In aDoc.AllReferencedDocuments

 

If iDoc.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then

auto = iLogicVb.Automation

auto.RunExternalRule(iDoc, ”Sheet Metal Parameters”)

 

iDoc.Close()

End If

 

Next

iLogicVb.UpdateWhenDone = True

 

This rule basically identifies all the sheet metal parts in the assembly, runs an external rule named "Sheet Metal Parameters" and then closes that part document.

 

Hope this helps.

 

Regards,

Kp

Message 7 of 11

matthew.johnson200
Enthusiast
Enthusiast

This worked great. 

Thanks!

0 Likes
Message 8 of 11

RoyWickrama_RWEI
Advisor
Advisor

Thanks a lot. Found a many solutions in the forum. Was about to post in the forum for help, but finally, I found your code helping me. Thanks a lot.

 

Dim aDoc As AssemblyDocument
aDoc = ThisApplication.ActiveDocument
Dim iDoc As Document
For Each iDoc In aDoc.AllReferencedDocuments
auto = iLogicVb.Automation
auto.RunExternalRule(iDoc, "D:\Vault Workspace\Projects\ExternalRules\Rules\Test")
iDoc.Close()
Next
iLogicVb.UpdateWhenDone = True

I need to tailor the rule for my needs, will revert to you for help. 

Message 9 of 11

adrian.kostilekBVRH9
Observer
Observer

Hallo, sorry for necroing

i'm triying to use this feature too.

Our company runs inventor 2014 (this may be the problem but i'm not sure)

 

so in the assembly i would run this rule

 

Dim aDoc As AssemblyDocument
aDoc = ThisApplication.ActiveDocument
Dim iDoc As Document
For Each iDoc In aDoc.AllReferencedDocuments
'lamiere
If iDoc.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then
auto = iLogicVb.Automation
auto.RunExternalRule(iDoc, "aggiungi regole materiali")
iDoc.Close()
End If
'parti
If iDoc.SubType = "{4D29B490-49B2-11D0-93C3-7E0706000000}" Then
auto = iLogicVb.Automation
auto.RunExternalRule(iDoc, "aggiungi regole materiali")
iDoc.Close()
End If
'prossimo file
Next
iLogicVb.UpdateWhenDone = True

 

which should run the external rule "aggiungi regole materiali" on each sheetmetal and part in the assembly

 

here is the rule i run

 

 
' Questa regola aggiunge una regola locale per la gestione dei materiali

Dim RuleName As String = 	"Materiale"
Dim RuleText As String = 	"' Hello - This is an automatically created iLogic rule" & vbCrLf & _
							"Dim oPartDoc As Inventor.PartDocument = ThisDoc.Document" & vbCrLf & _
							"Dim mat_norm As String = oPartDoc.ActiveMaterial.Item(""physmat_Comments"").Value" & vbCrLf & _
							"Dim stringa1 as String = """"" & vbCrLf & _
							"Dim stringa2 as String = """"" & vbCrLf & _
							"Dim stringa3 as String = """"" & vbCrLf & _
							"If mat_resi=""-"" Then" & vbCrLf & _ 
							"stringa1="""" " & vbCrLf & _ 
							"Else" & vbCrLf & _ 
"stringa1=mat_resi" & vbCrLf & _ 
"End If" & vbCrLf & _
"If mat_forn=""-"" Then" & vbCrLf & _ 
"stringa2="""" " & vbCrLf & _ 
"Else" & vbCrLf & _ 
"stringa2="" ""&mat_forn" & vbCrLf & _
"End If" & vbCrLf & _
"If mat_norm=""-"" Then" & vbCrLf & _
"stringa3="""" " & vbCrLf & _
"Else" & vbCrLf & _
"stringa3="" ""&mat_norm" & vbCrLf & _
"End If" & vbCrLf & _
"iProperties.Value(""Custom"", ""MAT_EST"")=ThisDoc.Document.ComponentDefinition.Material.Name & stringa1 & stringa2 & stringa3"& vbCrLf & _
"'non so perché ma la regola vuole questa riga commentata in fondo"

Dim oDoc As Document = ThisApplication.ActiveEditDocument                         
Dim iLogicAddIn As ApplicationAddIn = ThisApplication.ApplicationAddIns.ItemById("{3bdd8d79-2179-4b11-8a5a-257b1c0263ac}")
Dim iLogic As Object = iLogicAddIn.Automation
iLogic.DeleteRule(oDoc, RuleName)
iLogic.AddRule(oDoc, RuleName, RuleText)

SharedVariable("myRuleName") = "Materiale"                  		'Nome della regola.
SharedVariable("myTrigger") = "Any Model Parameter Change"    		'Tipo di trigger, deve essere esattamente quello che viene visualizzato nella finestra gestione trigger
SharedVariable("myAction") = "Add"                      			'il codice può essere usato per "Add" o "Remove" una regola da un trigger	
iLogicVb.RunExternalRule("EventTriggerSet")
SharedVariable.RemoveAll()
SharedVariable("myRuleName") = "Materiale"                  		'Nome della regola.
SharedVariable("myTrigger") = "Material Change"    					'Tipo di trigger, deve essere esattamente quello che viene visualizzato nella finestra gestione trigger
SharedVariable("myAction") = "Add"                      			'il codice può essere usato per "Add" o "Remove" una regola da un trigger	
iLogicVb.RunExternalRule("EventTriggerSet")
SharedVariable.RemoveAll()

oMyParameter=ThisApplication.ActiveDocument.ComponentDefinition.Parameters.UserParameters
oParameter=oMyParameter.AddByValue("mat_resi", "-", UnitsTypeEnum.kTextUnits)
oParameter=oMyParameter.AddByValue("mat_forn", "-", UnitsTypeEnum.kTextUnits)
MultiValue.SetList("mat_resi", "-", "J0", "J2", "JR", "K0", "K2", "KR", "L", "L0", "L2", "LR")
Parameter("mat_resi") = "-"
MultiValue.SetList("mat_forn", "-", "+M", "+N (TD)", "+Q", "+QT (TF)", "+T")
Parameter("mat_forn") = "-"

iLogicVb.RunRule("Materiale")

 

if i run this one on a single part or sheetmetal it works: it creates a rule, applies it to triggers and adds 2 parameters that the new rule uses, then it runs the new rule

 

if i use the assembly rule this is the error i get:

 

System.InvalidCastException: Impossibile eseguire il cast di oggetti COM di tipo 'System.__ComObject' in tipi di interfaccia 'Inventor.PartDocument'. L'operazione non è stata completata perché la chiamata QueryInterface sul componente COM per l'interfaccia con IID '{29F0D463-C114-11D2-B77F-0060B0F159EF}' non è riuscita a causa del seguente errore: Interfaccia non supportata. (Eccezione da HRESULT: 0x80004002 (E_NOINTERFACE)).
   in LmiRuleScript.Main()
   in Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
   in iLogic.RuleEvalContainer.ExecRuleEval(String execRule)

 

and then for each part of the assembly it says it could not find the parameter "mat_resi" which the rule should have added.

 

I am grateful for all the help i can get. I'm still a newbie to ilogic and i don't understand everything i do (many things come from this forums and work, i don't question them)

 

 

0 Likes
Message 10 of 11

halooween123
Explorer
Explorer
How to add a condition that if a part already has a rule, the rule will not run with that part
0 Likes
Message 11 of 11

WCrihfield
Mentor
Mentor

Hi @halooween123.

  • Do you only want the external rule to run on parts (not on sub assemblies)?
  • Can you explain "already has a rule" in more detail?
    • A specifically named rule, or just any rule?
    • If specifically named, then what is the name of the rule that it might find in the part?
      • assume spelling and capitalization are important.
  • Can you explain "the rule will not run with that part" in more detail?
    • By "the rule", do you mean the external rule, or the internal rule found within that part?
    • So, do you mean...if the part contains (a specific / or any) internal iLogic rule, then do not run this external iLogic rule on that part?

Here is an iLogic rule example that will only process parts (not sub assemblies) being referenced by the current document, and can be used either way (any internal rules in part or specific rule in part).  Just change the rule names in Lines 3 & 4, and choose which Line (Line 8 or Line 10) should be 'commented out' and which line the rule should use.

Dim oDoc As Document = ThisDoc.Document
Dim oAuto As IiLogicAutomation = iLogicVb.Automation
Dim sExtRuleName As String = "My External Rule Name"
Dim sIntRuleName As String = "Name of Internal Rule In Part"
For Each oRefDoc As Document In oDoc.AllReferencedDocuments
	If TypeOf oRefDoc Is PartDocument Then
		'next line checks for 'any' internal iLogic rules - if any found, skip this part
'		If oAuto.Rules(oRefDoc) IsNot Nothing Then Continue For
		'next line checks for 'specific' internal iLogic rule - if found, skip this part
		If oAuto.GetRule(oRefDoc, sIntRuleName) IsNot Nothing Then Continue For
		oAuto.RunExternalRule(oRefDoc, sExtRuleName)
	End If
Next oRefDoc
oDoc.Update2(True)

PS.  If the external rule is not using the iLogic code phrase 'ThisDoc.Document' to obtain a reference to the document it should be focused on, and is using 'ThisApplication.ActiveDocument' instead, then providing the referenced document to the RunExternalRule method in Line 11 will not work properly for you, and you may need to change that phrase in your external rule.

 

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)