VBA
Discuss AutoCAD ActiveX and VBA (Visual Basic for Applications) questions here.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

AutoCAD COM interop without referenced DLL

4 REPLIES 4
Reply
Message 1 of 5
a_minet
362 Views, 4 Replies

AutoCAD COM interop without referenced DLL

Hello I have a problem, I want to use the AutoCAD COM interop without referencing the "Interop.AutoCAD" dll. This code works fine when I reference the "Interop.AutoCAD" dll in the project.

 

    class Program
    {
        static dynamic acApp;
        static dynamic doc;
        static dynamic modelSpace;

        static void Main(string[] args)
        {
            if (AcadConnection())
            {
                object pickedObject = null;
                object pickPoint = new object[3];

                doc.Utility.GetEntity(out pickedObject, out pickPoint, "Sélectionnez un objet texte :");

                if (pickedObject != null)
                {
                    // TODO
                }
            }
        }

        static bool AcadConnection()
        {
            const string strProgId = "AutoCAD.Application";
            try
            {
                acApp = (dynamic)ExMarshal.GetActiveObject(strProgId);
            }
            catch
            {
                try
                {
                    acApp = (dynamic)Activator.CreateInstance(Type.GetTypeFromProgID(strProgId), true);
                }
                catch
                {
                    Console.WriteLine("CannotCreateAnInstanceAutoCADApplication");
                    return false;
                }
            }
            if (acApp != null)
            {
                string version = acApp.Version;
                try
                {
                    while (true)
                    {
                        try { acApp.Visible = true; break; }
                        catch { }
                    }

                    doc = acApp.ActiveDocument;
                    modelSpace = doc.ModelSpace;

                    return true;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    return false;
                }
            }

            return false;
        }
    }

 

But at this row:

 

doc.Utility.GetEntity(out pickedObject, out pickPoint, "Sélectionnez un objet texte :");

 

I get this error: "Could not convert argument 2 for call to GetEntity."

 

a_minet_0-1727455764384.png

 

Could you help me, thank you.

 

4 REPLIES 4
Message 2 of 5
norman.yuan
in reply to: a_minet

Firstly, in your AcadConnection() method, after getting the instance of an AcadApplication, you may want to test if its ActiveDocument propery is null or not. For example, when the code creates a new instance of running AutoCAD in the "catch..." clause, there could be no document is opened in the AcadApplication instance. If you you may want to let your code to open one (either a new drawing, or an existing drawing file).

 

As for the issue in hand,  try to pass in "dynamic" as the arguments, not "object". You also want to handle the case when user hit Esc to cancel the picking, which raises an exception. So, you may want to try this:

 

dynamic pickedObject;

dynamic pickPoint;

try

{

  doc.Utility.GetEntity(out pickedObject, out pickPoint, "\nSelect xxxxx:");

}

catch

{

   pickedObject = null;

}

if (pickedObject == null) 

{

  // user cancelled the picking, you want to exit here

  return;

}

// Do something with the picked entity ...

 

Norman Yuan

Drive CAD With Code

EESignature

Message 3 of 5
minet.axel
in reply to: a_minet

Sorry

Message 4 of 5
a_minet
in reply to: norman.yuan

That's not the problem, as I was saying, it's the GetEntity row that's giving me an error. If I reference the Interop.AutoCAD dll, this code works, but if I use dynamic without referencing the dll, it doesn't work. I'll provide an example of 2 projects, one with a reference and one without.

 

In my example, I have an open AutoCAD instance and an open document.

Message 5 of 5
ed57gmc
in reply to: a_minet

You've declared pickPoint as an object, but you assigned an array to it. To set values of an array, you have to set each element separately, but the Utility.GetEntity method attempts to assign a 3D point to a variant. So, in this case, it isn't necessary to assign a value when you declare the variable. Just leave it empty like you did for pickedObject. The GetEntity method will take care of setting the variable.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

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

Post to forums  

AutoCAD Inside the Factory


Autodesk Design & Make Report