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)