Jump to content

Get the COM object for AutoCAD 2011


Yaorderse

Recommended Posts

I have spent the past few days googling this problem and have not come up with a solution. I searched here as well, so I hope I didn't just miss it....

 

The Scenario: I am writing a C# program that I am trying to use to start and then control AutoCAD 2011.

 

The Problem: Even though AutoCAD will start every time, it will not return the COM object always, instead it will error inside the calling function. I haven't been able to determine why this is, it seems random. The error code is 8001010a.

 

Notes:

1) Sometimes, if I put a break point before the calling function and step over it, it will work better than without using a break point.

2) On other forums, people seem to say that the issue is sometimes caused by norton or antivirus programs, but I don't have an antivirus installed.

3) If I try the same thing using AutoCAD 2000, I have zero problems...

4) I'm using Visual Studio C# 2008 Express Edition, AutoCAD 2011, Windows XP, no antivirus.

 

 

The code I'm using to activate AutoCAD:

public static object StartAutoCAD_2011()
{
 string progID = "AutoCAD.Application.18.1";
 Type acType = Type.GetTypeFromProgID(progID);
 Autodesk.AutoCAD.Interop.AcadApplication ACAD = null;
 ACAD = (Autodesk.AutoCAD.Interop.AcadApplication)Activator.CreateInstance(acType);
 return ACAD;
}

 

Thanks for taking the time to look at this.

Link to comment
Share on other sites

I forgot to mention that one alternative I was looking into was obtaining the Process ID for the launched AutoCAD 2011 and then attempting to get its COM object that way. I googled how to accomplish getting the COM object from the process ID or using the Main Window Handle (which I can get from the process), but was also unsuccessful in coming up with a working solution.

Link to comment
Share on other sites

I got an idea that I could use the GetActiveObject function. By making active the instance that I want, I can use that function to get the one I want. It's a bit of a kluge, but I think it could work....If it's possible to change the active object--which I don't know how to do. Any thoughts....?

Link to comment
Share on other sites

It is a shame that a question from a new member happens to be in an area in which there isn’t much readily available advise. The only routine I’ve worked on where I’ve tried to interact with AutoCAD files from an outside executable was posted mid way through this thread:

http://www.cadtutor.net/forum/showthread.php?39166

Unfortunately I don’t think it will be any help because I wasn’t trying to interact with an active AutoCAD session.

You will find much more AutoCAD programming discussion over at http://www.theswamp.org/

Link to comment
Share on other sites

You could check theswamp.org

 

See if this works

 

Here is a example that will draw a circle

If acad is not running it will start it or if it is runnig it will grab it

 

uses

Marshal.GetActiveObject()

[color=blue]using[/color] System;
[color=blue]using[/color] System.IO;
[color=blue]using[/color] Autodesk.AutoCAD.Interop.Common;
[color=blue]using[/color] Autodesk.AutoCAD.Interop;
[color=blue]using[/color] System.Runtime.InteropServices;
[color=blue]namespace[/color] ConsoleApplication1
{
   [color=blue]class[/color] [color=#2b91af]Program[/color]
   {
       [color=blue]const[/color] [color=blue]string[/color] progIDstr = [color=#a31515]"AutoCAD.Application.18.1"[/color];
       [color=blue]static[/color] [color=blue]void[/color] Main([color=blue]string[/color][] args)
       {
           
           [color=#2b91af]AcadApplication[/color] app = [color=blue]null[/color];
           
           [color=blue]try[/color]
           {
               [color=blue]try[/color]
               {
                   app = ([color=#2b91af]AcadApplication[/color])[color=#2b91af]Marshal[/color].GetActiveObject(progIDstr);
                   app.Visible = [color=blue]true[/color];
               }
               [color=blue]catch[/color]
               {
                   app = [color=blue]new[/color] [color=#2b91af]AcadApplicationClass[/color]();
                   app.Visible = [color=blue]true[/color];
               }
           }
           [color=blue]catch[/color] ([color=#2b91af]Exception[/color] ex)
           {
               [color=#2b91af]Console[/color].WriteLine([color=#a31515]"Canot start AutoCAD: "[/color] + ex.Message);
               [color=blue]return[/color];
           }

           

           [color=#2b91af]AcadDocument[/color] doc = [color=blue]null[/color];
          
           [color=blue]if[/color] (app.Documents.Count > 1)
           {
               doc = app.ActiveDocument;
           }
           [color=blue]else[/color]
           {
               doc = app.Documents.Add([color=#a31515]"acad.dwt"[/color]);
           }

           [color=blue]if[/color] (doc == [color=blue]null[/color])
           {
               [color=#2b91af]Console[/color].WriteLine([color=#a31515]"No drawing is open"[/color]);
               [color=blue]return[/color];
           }

           [color=blue]try[/color]
           {
               doc.SendCommand([color=#a31515]"C 0,0,0 10 "[/color]);
           }
           [color=blue]catch[/color] ([color=#2b91af]Exception[/color] ex)
           {
               [color=#2b91af]Console[/color].WriteLine([color=#a31515]"Importing failed: "[/color] + ex.Message);
               [color=#2b91af]Console[/color].ReadLine();
               [color=blue]return[/color];
           }
          
       }
   }
}

Link to comment
Share on other sites

  • 1 month later...

I'm using VS2008/VB. But I use this code to start my AutoCad.

Dim acadProcess As New Process

acadProcess.StartInfo.FileName = "path/Acad.Exe"

acadProcess.StartInfo.Arguments = "file.dwg"

acadProcess.Start()

acadProcess.WaitForExit()

 

 

You can also write a script file that automatically loads and runs a VBA program. Something like the following:

Dim strProcess As String = _

" /b " & Chr(34) & CAD_Script & Chr(34) & _

" /t " & Chr(34) & CAD_Template & Chr(34)

' create the AutoCad Script Data

Dim strScript As String = _

"SAVEAS" + vbCrLf + _

"2000" + vbCrLf + _

Chr(34) & CAD_GusDwgPath & CAD_DwgNo & ".dwg" & Chr(34) + vbCrLf + _

"vbaload " & Chr(34) & CAD_VBAPath & CAD_VBCode & Chr(34) + vbCrLf + _

"vbarun " & Chr(34) & CAD_VBAPath & CAD_VBCode & _

"!Module1.MainSub" & Chr(34) + vbCrLf

This is only partially tested as I am in the process of moving from VB6/AutoCad2002/VBA to VS2008/Autocad2010/.Net.

 

Hope this is helpful.

Rose Anna

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...