Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Call iLogic from .NET c# (Inventor 2018)

8 REPLIES 8
SOLVED
Reply
Message 1 of 9
C-Hoppen
2076 Views, 8 Replies

Call iLogic from .NET c# (Inventor 2018)

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

image.pngAnyone have any suggestions or things I might try?

Christoph

 

Tags (3)
8 REPLIES 8
Message 2 of 9
JamieVJohnson2
in reply to: C-Hoppen

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.

 

jvj
Message 3 of 9
c_hoppen
in reply to: JamieVJohnson2

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?

 

Message 4 of 9
JamieVJohnson2
in reply to: c_hoppen

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.

jvj
Message 5 of 9
C-Hoppen
in reply to: JamieVJohnson2

JetBrains dotPeek made my day. Thanks a lot!

Message 6 of 9
rikard.nilsson
in reply to: C-Hoppen

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

Message 7 of 9
C-Hoppen
in reply to: rikard.nilsson

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");
            }
        }
Message 8 of 9
rikard.nilsson
in reply to: C-Hoppen

Thanks!!

That solved my problem!!

Message 9 of 9
Khoa_NguyenDang
in reply to: C-Hoppen

@C-Hoppen 

i have this problem

Khoa_NguyenDang_0-1619629575603.png

Although i have the Rule to delete in my Part

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report