What is wrong with my C# call to GetInterfaceObject?

What is wrong with my C# call to GetInterfaceObject?

oransen
Collaborator Collaborator
1,720 Views
3 Replies
Message 1 of 4

What is wrong with my C# call to GetInterfaceObject?

oransen
Collaborator
Collaborator

Here's the code...

 

 

        private void Test1()
        {
            AcadApplication acadApp; // get a running instance of AutoCAD, or start one:
            try
            {
                acadApp = (AcadApplication) Marshal.GetActiveObject("AutoCAD.Application");
            }
            catch ( Exception e )
            {
                acadApp = new AcadApplication();
            }

            string sVer = acadApp.Version; // This gives me sVer = "23.1s (LMS Tech)"

            AxDbDocument dbxDoc = (AxDbDocument) acadApp.GetInterfaceObject("AxDbDocument.23.1s");
            dbxDoc.Open(@"D:\Temp\CSHARP.DWG", string.Empty);
        }

 

 

...GetInterfaceObject fails, with or without the last "s". Here's the error:

 

  Name Value Type

  -2147221005int
 Message"Problem in loading application"string

 

See also the attached image (image insertion in the post does not work).

 

 

TIA

 

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

norman.yuan
Mentor
Mentor
Accepted solution

You did not mention, but I suppose that you are using AutoCAD 2020.

 

The correct ProgId for ObjectDBX AxDbDocument is "ObjectDBX.AxDbDocument.23". You can/should search Windows Registry for "AxDbDocument" to find the correct ProgId.

 

My computer has Acad2020 and 2021 insatlled, the ProgId for them are ObjectDBX.AxDbDocument.23" and "ObjectDBX.AxDbDocument.24".

 

Following code works OK (creating an instance of AxDbDocument) for my Acad 2021:

 

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Start AutoCAD (Y/N): ");
            var an = Console.ReadLine();

            if (an.ToUpper().Contains("Y"))
            {
                var cadApp = new AcadApplication();
                cadApp.Visible = true;

                try
                {
                    var doc = cadApp.GetInterfaceObject("ObjectDBX.AxDbDocument.24");
                    Console.WriteLine("ObjectDBX Document created!");
                }
                catch
                {
                    Console.Write("Creating ObjectDBX Document failed!");
                }

                Console.WriteLine("Press Enter to continue: ");
                Console.ReadLine();

                cadApp.Quit();
            }
        }
    }

 

 In this console EXE project, I added references to Autodesk.AutoCAD.Interop.dll and Autodesk.AutoCAD.Interop.Common.dll. I set "Copy Local" to Ture and "Embebd Interop Types" to False (i.e. the referenced Acad interop assemblies must be deployed with the EXE).

 

I believe the same code should be working with my Acad2020, if I change the references to Acad 2020 interop assemblies.

Norman Yuan

Drive CAD With Code

EESignature

Message 3 of 4

oransen
Collaborator
Collaborator
 

Apologies for formatting, I get an "invalid html" message when I try to do it properly.

 

 

"You did not mention, but I suppose that you are using AutoCAD 2020."


I did note in the sources that the version was ....

string sVer = acadApp.Version; // This gives me sVer = "23.1s (LMS Tech)"


....could that not help me?


Thanks anyway, I'll try your solution tomorrow.

 

0 Likes
Message 4 of 4

oransen
Collaborator
Collaborator

Thanks to the help of @norman.yuan I've got this test going. The most important part was that he told me where to look for the correct key: in the registry...

 

        private void Test1()
        {
            AcadApplication acadApp; // get a running instance of AutoCAD, or start one:
            try
            {
                acadApp = (AcadApplication) Marshal.GetActiveObject("AutoCAD.Application");
            }
            catch ( Exception e )
            {
                acadApp = new AcadApplication();
            }

            acadApp.Visible = true;

            string sVer = acadApp.Version; // This gives me sVer = "23.1s (LMS Tech)"
            
            // string found in the registry under the AxDbDocument key
            AxDbDocument dbxDoc = (AxDbDocument)acadApp.GetInterfaceObject("ObjectDBX.AxDbDocument.23");
            dbxDoc.Open(@"D:\Temp\CSHARP.DWG", string.Empty);
            AXDBLib.AcadDatabase db = dbxDoc.Database;

            int iNumLayers = db.Layers.Count ;
            MessageBox.Show("There are " + iNumLayers + " layers");

            acadApp.Quit();
        }

 

 

0 Likes