Dynamically execute/add a Macro

Dynamically execute/add a Macro

fleischmannHVTJ3
Explorer Explorer
97 Views
3 Replies
Message 1 of 4

Dynamically execute/add a Macro

fleischmannHVTJ3
Explorer
Explorer

Hello, I am writing an Inventor AddIn in C#.

I want to dynamically execute a VBA script/a Macro in Inventor.

For example this simple Macro:
Sub My_Macro()
MsgBox "My First Macro"
End Sub

I know how to execute existing Macros. Therefore, my idea was to first add a macro (dynamically) and then execute it. However, I did not find any way to dynamically add macros.

I tried it via ControlDefinitions.AddMacroControlDefinition(string MacroOrProgram) but that method appears to not actually add a macro, but rather return the MacroControlDefinition object of an existing macro.

I then tried to add it via InventorVBAProjects object, where I can iterate over the different objects, and modules (InventorVBAComponents), etc. But again, there seems to be no way to actually add a macro, only iterate over existing ones.

Is it even possible to achieve this via the Inventor API?

0 Likes
Accepted solutions (1)
98 Views
3 Replies
Replies (3)
Message 2 of 4

Michael.Navara
Advisor
Advisor
Accepted solution

Hello @fleischmannHVTJ3 

In my opinion there is no simple way, how to create VBA macros dynamically.

 

But you can easily create and execute iLogic rule. See the sample 

However ask yourself if this is the best approach. Isn't it possible to do it in different way? What is the benefit of this approach?

 

My experience is when I need this, something in design of my application is not good and need to be improved or re-designed.

public override void Main()
{
    //Get iLogic Automation object

    //var iLogicAuto = iLogicVb.Automation; //Strongly typed version of the line bellow
    dynamic iLogicAuto = ThisApplication.ApplicationAddIns.ItemById["{3BDD8D79-2179-4B11-8A5A-257B1C0263AC}"].Automation;

    //Create new rule
    string ruleName = "C:\\Temp\\DynamicRule.iLogicVb";
    var ruleText = @"       
Sub Main()
   MsgBox(""Dynamically created rule"")
End Sub
";
    System.IO.File.WriteAllText(ruleName, ruleText);

    //Execute rule
    var doc = ThisApplication.ActiveEditDocument;
    iLogicAuto.RunExternalRule(doc, ruleName);
}

 

0 Likes
Message 3 of 4

fleischmannHVTJ3
Explorer
Explorer

Thanks for the reply @Michael.Navara .

it does seem that iLogic is way more practical for this use case, so I will use that instead, thank you very much.

Before marking this as the Solution, I'd like to add that iLogicAuto.RunExternalRule(doc, ruleName) threw a COMException for me (Typelib export: Type library is not registered).

I don't know what other ways there are, but I made everything work with the following code:

public override void Main()
{
    // Get iLogic Automation Object
    var iLogicAuto = ThisApplication.ApplicationAddIns.ItemById["{3BDD8D79-2179-4B11-8A5A-257B1C0263AC}"].Automation;

    // Create new rule
    string ruleName = "C:\\Temp\\DynamicRule.iLogicVb";
    string ruleText = $"Sub Main()\nMsgBox(\"Dynamically created rule\")\nEnd Sub";

    System.IO.File.WriteAllText(ruleName, ruleText);
   
    var doc = ThisApplication.ActiveEditDocument;

    // Execute rule
    iLogicAuto.GetType().InvokeMember("RunExternalRule", BindingFlags.Public | BindingFlags.InvokeMethod, null, iLogicAuto, [doc, ruleName]);
}

 

0 Likes
Message 4 of 4

Michael.Navara
Advisor
Advisor

As you can see in my example. Variable iLogicAuto is not decalared using keyword var but dynamic. In this case the variable is not strongly typed and you can use dot convention to call public members of the variable instead of use InvokeMember method.

0 Likes