Hello, how can I get the automation from iLogic?
I found
https://adndevblog.typepad.com/manufacturing/2015/05/use-goexcel-outside-a-rule.html
https://adndevblog.typepad.com/manufacturing/2013/04/call-ilogic-from-net.html
but I could not do it with c#. Here is my code
using Autodesk.iLogic.Interfaces;
...
public static IiLogicAutomation GetIlogicAutomation(Inventor.Application inventorApp) { IiLogicAutomation _iLogicAutomation = null; string iLogicAddinGuid = "{3BDD8D79-2179-4B11-8A5A-257B1C0263AC}"; Inventor.ApplicationAddIn addin = null; try { addin = inventorApp.ApplicationAddIns.get_ItemById(iLogicAddinGuid); } catch { } if (addin != null) { if (!addin.Activated) addin.Activate(); _iLogicAutomation = addin.Automation as IiLogicAutomation; _iLogicAutomation = (IiLogicAutomation)addin.Automation; <- //Invalid Cast Exception! } return _iLogicAutomation; } public static Autodesk.iLogic.Interfaces.iLogicRule GetiLogicRuleByName( IiLogicAutomation _iLogicAutomation, Inventor.Document oDoc, string ruleName) { Autodesk.iLogic.Interfaces.iLogicRule myRule = null; foreach (Autodesk.iLogic.Interfaces.iLogicRule eachRule in _iLogicAutomation.get_Rules(oDoc)) { if (eachRule.Name == ruleName) { myRule = eachRule; break; } } return myRule; } }
References:
..\Program Files\Autodesk\Inventor 2018\Bin\Autodesk.iLogic.Interfaces.dll
..\Program Files\Autodesk\Inventor 2018\Bin\Public Assemblies\Autodesk.Inventor.Interop.dll
Anyone have any suggestions or things I might try?
Christoph
Solved! Go to Solution.
Solved by JamieVJohnson2. Go to Solution.
in VB.Net I use this code:
Dim iLogicAddInGUID As String = "{3BDD8D79-2179-4B11-8A5A-257B1C0263AC}" Dim addin As Inventor.ApplicationAddIn = Nothing Try addin = invApp.ApplicationAddIns.ItemById(iLogicAddInGUID) Catch ex As Exception MsgBox("Unable to load iLogic Add In", MsgBoxStyle.SystemModal) End Try If Not addin.Activated Then addin.Activate() End If Dim strRuleCalcs As String = String.Empty invApp.SilentOperation = True Dim iLogAuto As Object 'Autodesk.iLogic.Interfaces.IiLogicAutomation iLogAuto = addin.Automation iLogAuto.CallingFromOutside = True iLogAuto.EnterDelayedRuleRunningMode() Dim newRule As Object = iLogAuto.AddRule(Model, "Calcs", "") newRule.text = strRuleCalcs iLogAuto.ExitDelayedRuleRunningMode() iLogAuto.RulesOnEventsEnabled = True AssignRuleToILogicEvent(Model, "Calcs", "BeforeDocSave0", 700) invApp.SilentOperation = False Public Sub AssignRuleToILogicEvent(invDoc As Inventor.Document, ruleName As String, strEvent As String, strID As String) Dim customiPropSet As PropertySet = Nothing For Each propSet As PropertySet In invDoc.PropertySets If propSet.Name = "iLogicEventsRules" Then customiPropSet = propSet Exit For End If If propSet.Name = "_iLogicEventsRules" Then customiPropSet = propSet Exit For End If Next If customiPropSet IsNot Nothing Then If customiPropSet.InternalName <> "{2C540830-0723-455E-A8E2-891722EB4C3E}" Then customiPropSet.Delete() customiPropSet = invDoc.PropertySets.Add("iLogicEventsRules", "{2C540830-0723-455E-A8E2-891722EB4C3E}") End If End If If customiPropSet Is Nothing Then customiPropSet = invDoc.PropertySets.Add("iLogicEventsRules", "{2C540830-0723-455E-A8E2-891722EB4C3E}") End If If customiPropSet Is Nothing Then MsgBox("Unable to create event triggers for this file.",, "Event Triggers") End If If customiPropSet IsNot Nothing Then customiPropSet.Add(ruleName, strEvent, strID) End Sub
Note, I set the variable type for the automation to 'object' aka 'var' in c#, because of the Inventor API system_com reponse to all object types, I can't use option explicit.
Thanks for the sample. You gave me an important hint. var doesn't work, but dynamic does.
public static void TestIlogicAutomation(Inventor.Application inventorApp) { string iLogicAddinGuid = "{3BDD8D79-2179-4B11-8A5A-257B1C0263AC}"; Inventor.ApplicationAddIn addin = null; try { addin = inventorApp.ApplicationAddIns.get_ItemById(iLogicAddinGuid); } catch { } if (addin != null) { if (!addin.Activated) addin.Activate(); /*IiLogicAutomation*/ dynamic _iLogicAutomation = addin.Automation; _iLogicAutomation.CallingFromOutside = true; _iLogicAutomation.DeleteRule(inventorApp.ActiveDocument, "Hauptabmessungen"); //iLogicRule delRule = null; //foreach (iLogicRule eachRule in _iLogicAutomation.get_Rules(inventorApp.ActiveDocument)) //{ // if (eachRule.Name == "Hauptabmessungen") // { // delRule = eachRule; // break; // } //} //if (delRule != null) _iLogicAutomation.DeleteRule(inventorApp.ActiveDocument, "Hauptabmessungen"); } }
But there is still a lack of clarity.
this works:
_iLogicAutomation.DeleteRule(inventorApp.ActiveDocument, "Hauptabmessungen");
this throws an exception:
foreach (iLogicRule eachRule in _iLogicAutomation.get_Rules(inventorApp.ActiveDocument))
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException
System.__ComObject' does not contain a definition for get_Rules.
why?
You will probably appreciate this: get a copy of JetBrains dotPeek (its free). Install, and open. Copy the dll, then open your copy (so as not to disturb the peace): Autodesk.iLogic.Automation.dll
Expand the list, and you will find iLogicAutomation.Rules(document doc):IEnumerable. That is your way in.
Hi,
Could you please share how you solved how to do a...
foreach (Autodesk.iLogic.Interfaces.iLogicRule eachRule in _iLogicAutomation.Rules(oApp.ActiveDocument))
Because I still get an error after changing from "get_Rules" to "Rules".
Regards
Rikard
Ricard, you have to use dynamic
public static void TestIlogicAutomation(Inventor.Application inventorApp) { string iLogicAddinGuid = "{3BDD8D79-2179-4B11-8A5A-257B1C0263AC}"; Inventor.ApplicationAddIn addin = null; try { addin = inventorApp.ApplicationAddIns.get_ItemById(iLogicAddinGuid); } catch { } if (addin != null) { if (!addin.Activated) addin.Activate(); /*IiLogicAutomation*/ dynamic _iLogicAutomation = addin.Automation; _iLogicAutomation.CallingFromOutside = true; //_iLogicAutomation.DeleteRule(inventorApp.ActiveDocument, "Hauptabmessungen"); /*iLogicRule*/ dynamic delRule = null; try { foreach (/*iLogicRule*/ dynamic eachRule in _iLogicAutomation.Rules(inventorApp.ActiveDocument)) { if (eachRule.Name == "Hauptabmessungen") { delRule = eachRule; break; } } } catch { // will throw an exception when document has no rules } if (delRule != null) _iLogicAutomation.DeleteRule(inventorApp.ActiveDocument, "Hauptabmessungen"); } }