VB.NET to C#

VB.NET to C#

Daan_M
Collaborator Collaborator
1,132 Views
3 Replies
Message 1 of 4

VB.NET to C#

Daan_M
Collaborator
Collaborator

Hi forum,

 

I made a VB.NET code for Inventor last week (with help from forum members), which runs fine by the way!

I tried translating it to C# but i cant solve the errors i am getting, since im new to coding i find it diffecult to interpret the error messages.

 

 

If anyone can help me out it's greatly appreciated

.

Below here is a part the code in C#, below that the error messages, its about CS1955 and CS1061

 

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Inventor;
using System.ComponentModel.Design.Serialization;
using Autodesk.iLogic.Interfaces;


        public void rulerunner()
        {

            {
                string MyRuleName = "cmdRead";
                string iLogicAddinGuid = "{3BDD8D79-2179-4B11-8A5A-257B1C0263AC}";
                ApplicationAddIn iLogicAddin;
                object iLogicAutomation = null; // As IiLogicAutomation = Nothing
                try
                {
                    iLogicAddin = _invApp.ApplicationAddIns.ItemById(iLogicAddinGuid); //doesnt work?
                    iLogicAutomation = iLogicAddin.Automation;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Unable to access iLogic addin " + ex.Message);
                }

                try
                {
                    iLogicAutomation.RunRule(oDoc, MyRuleName); //doesntwork?
                }
                catch
                {
                    MessageBox.Show("There is no rule called " + MyRuleName);
                }
            }
        }




       

 

 

forumdonderdag.JPG

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

Michael.Navara
Advisor
Advisor
Accepted solution

This is because ItemById is not a function but property with argument

Change

_invApp.ApplicationAddIns.ItemById(iLogicAddinGuid);

to

_invApp.ApplicationAddIns.ItemById[iLogicAddinGuid];

That's all 🙂 

Message 3 of 4

Daan_M
Collaborator
Collaborator

Thanks! that solves a part of 🙂

 

i still need help with the following lines:

 

 try
            {
                _invApp = (Inventor.Application)Marshal.GetActiveObject("Inventor.Application");

            }


Error CS0117 'Marshal' does not contain a definition for 'GetActiveObject' WindowsFormsApp1 C:\Users\daanm\Desktop\vrijdag\WindowsFormsApp1\Form1.cs 38 Active

 

 

try
                {
                    Autodesk.iLogic.Interfaces.IiLogicAutomation.RunRule(oDoc, MyRuleName);
                }

Error CS0120 An object reference is required for the non-static field, method, or property 'IiLogicAutomation.RunRule(Document, string)' WindowsFormsApp1 C:\Users\daanm\Desktop\vrijdag\WindowsFormsApp1\Form1.cs 88 Active

 

 

0 Likes
Message 4 of 4

Michael.Navara
Advisor
Advisor

I don't know what the Marshal is in your context. I'm using this piece of code to get active Inventor.Application object

 //Get running inventor
private const string InventorProgID = "Inventor.Application";
result = System.Runtime.InteropServices.Marshal.GetActiveObject(InventorProgID) as Inventor.Application;

 

in the second example you try to call method on object definition but you mast call on object instance. Similar to this: 

iLogicAddin = _invApp.ApplicationAddIns.ItemById(iLogicAddinGuid);
iLogicAutomation = iLogicAddin.Automation;
iLogicAutomation.RunRule(oDoc, MyRuleName);

0 Likes