Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.

Inventor API C# script to control iLogic variables

Daan_M
Collaborator

Inventor API C# script to control iLogic variables

Daan_M
Collaborator
Collaborator

Hello!

 

I managed to create the following:

I can fill in parameters in a Excelfile on my local disk, upon saving the Excelfile, my C# script tells Inventor to execute the iLogic code. The iLogic code reads out the Excel parameters, creates the according model, and saves it as STEP. So basically this allows me to create models without having to do anything in Inventor.

 

What i want to change:

Instead of changing the parameters inside the Excelfile (overwriting the existing file), i want the iLogic code to execute when a new Excel parameter file is added in the local disk folder (so every model comes from a seperate Excel file). Now i can probably put this in my C# script, but I run into a problem.

The problem:

Right now i reference to my Excelfile inside my iLogic code as:

 

Excelfile = "C:\Users\Administrator\OneDrive\EMS source.xlsx"

 

This works fine if the Excelfile gets overwritten, since this reference is always the same. But since i want to import the parameters for every model from a new Excel file, i need this reference to be dynamic. 

 

What help do i need:

I think i can solve this by changing my C# script to do the following things

 

 

1. The reference to the Excelfile should be changed everytime a Excelfile is added to the folder. So instead of 

 

Excelfile = "C:\Users\Administrator\OneDrive\EMS source.xlsx"​

 

 

a dynamic reference should be added. I think i can do this by using the Inventor API inside my C# script to change the reference inside the iLogic code, but i'm not sure. Maybe theres an easy way to solve this inside iLogic?

 

2. Instead of executing the iLogic when the Excelfile is saved, it should execute after:

Excelfile is added to the disk AND the refence  is changed correctly (point 1)

 

I'm not sure what the right path is, any tips or ways to solve the problem described above is greatly appreciated.

Also example code i can use to achieve this goal is appreciated.

 

Thanks for taking time to read this, if more reference is needed feel free to ask.

 

Daan

 

 

 

 

 

0 Likes
Reply
Accepted solutions (1)
1,207 Views
5 Replies
Replies (5)

JhoelForshav
Mentor
Mentor

Hi @Daan_M 

I don't really understand what you're trying to do. But I think think this might guide you in the right direction.

I suppose you run your iLogic rule from your script using ilogic automation. The automation has a method to run rules with arguments. So you can just give the iLogic rule the excel path directly when calling it.

 

I'll give an example:

If you create a part and add these two rules to it:

 

Rule 1:

Dim RuleArgs As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap
RuleArgs.Add("ExcelFile", "C:\Users\Administrator\OneDrive\EMS source.xlsx" ​)
iLogicVB.Automation.RunRuleWithArguments(ThisDoc.Document, "MessageBox", RuleArgs)

Rule 2: Name this rule "MessageBox"

Dim oExcelFile As String = RuleArguments("ExcelFile")
MsgBox(oExcelFile)

If you now run Rule 1 It'll run the rule MessageBox with the Excel path as an argument. This rule will then display the path in a messagebox.

 

In other words; You don't have to modify the actual ilogic rule from your script. just run it with the excel path as an argument.

 

Hope this helps!

 

Daan_M
Collaborator
Collaborator

Thank you very much for your reply, i was indeed afraid my explanation wasn't very clear, let me try to summarize it clearly;

 

My iLogic rule now does:

 

read excel document 'ems source.xlsx'

generate model according to 'ems source.xlsx' parameters

 

(it's always the same Excel document i read parameters from, i just overwrite the parameters in the file everytime i want a new model)

 

Now i want seperate Excel files for every model, so whenever i place a new Excelfile with parameters in a designated folder, it needs to read that newly placed file:

 

read excel document *newly placed excel document.xslx*

generate model according to *newly placed excel document.xlsx* parameters

 

vb2.PNG

 

(now its a dynamic, since the reference to the Excel file inside iLogic needs to change everytime i place a new Excelfile in the folder)

 

I indeed use iLogic automation to trigger the iLogic rule in my script. 

 

 I'm not very experienced so I find it diffecult to understand the solution you're offering, could you elaborate? 

I hope the goal im trying to achieve is more clear now

 

Daan

 

 

0 Likes

Daan_M
Collaborator
Collaborator

@JhoelForshav I've looked into your suggestion and understand it now, this is indeed what i am looking for.

In your example both rules are placed Inventorfile. Instead of running a rule with arguments fromout another iLogic rule, i want to run it fromout my C# script.

 

The script below is what i have now, im not a programmer so i cant fully get it to work. See my notes behind the '//' for what i think i need to do. Can you help me make the final adjustments? Thanks alot

        void AddiLogic()
        {
            string MyRuleName = textBox2.Text;
            string iLogicAddinGuid = "{3BDD8D79-2179-4B11-8A5A-257B1C0263AC}";
            ApplicationAddIn iLogicAddin;
            dynamic iLogicAutomation = null;

//declare the Rulearguments below in the correct way...          
//RuleArgs As NameValueMap = Inventor.TransientObjects.CreateNameValueMap;
//RuleArgs.Add("Excelfile", 'Some string i read earlier in my code' ​)

            try
            {
                iLogicAddin = _invApp.ApplicationAddIns.ItemById[iLogicAddinGuid];
                iLogicAutomation = iLogicAddin.Automation;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Unable to access iLogic addin " + ex.Message);
            }

           
            try
            {
                iLogicAutomation.Runrule(oDoc, MyRuleName);
//change the line above to;
//iLogicVB.Automation.RunRuleWithArguments(oDoc, MyRuleName, RuleArgs)

            }
            catch
            {
                MessageBox.Show("There is no rule called " + MyRuleName);
            }

        }

//adjust the dim for the Excelfile inside iLogic:
//Dim oExcelFile As String = RuleArguments("Excelfile")

 

 

 

0 Likes

JhoelForshav
Mentor
Mentor
Accepted solution

Hi @Daan_M 

I don't really know C# so I don't know if I can be of much help.

I guess it should look something like this though...

 

    void AddiLogic()
    {
        string MyRuleName = textBox2.Text;
        string iLogicAddinGuid = "{3BDD8D79-2179-4B11-8A5A-257B1C0263AC}";
        ApplicationAddIn iLogicAddin;
        dynamic iLogicAutomation = null;

        //declare the Rulearguments below in the correct way...    
        NameValueMap RuleArgs = _invApp.TransientObjects.CreateNameValueMap;
        RuleArgs.Add("ExcelFile", "Here you should input the excel documents full filename");
        //RuleArgs As NameValueMap = Inventor.TransientObjects.CreateNameValueMap;
        //RuleArgs.Add("Excelfile", 'Some string i read earlier in my code' ​)

        try
        {
            iLogicAddin = _invApp.ApplicationAddIns.ItemById[iLogicAddinGuid];
            iLogicAutomation = iLogicAddin.Automation;
        }
        catch (Exception ex)
        {
            MessageBox.Show("Unable to access iLogic addin " + ex.Message);
        }


        try
        {
            //iLogicAutomation.Runrule(oDoc, MyRuleName);
            //change the line above to;
            iLogicVB.Automation.RunRuleWithArguments(oDoc, MyRuleName, RuleArgs);

        }
        catch
        {
            MessageBox.Show("There is no rule called " + MyRuleName);
        }

    }

    //adjust the dim for the Excelfile inside iLogic:
    //Dim oExcelFile As String = RuleArguments("Excelfile")

Anonymous
Not applicable

I don't have any code for you, but in regards to your file system questions I found some threads that may lead you in the right direction.

 

Finding the most recent file in a directory 

 

Detect when a file is created 

 

Detect when a file is created 2