Get instance AutoCAD with c#

Get instance AutoCAD with c#

Anonymous
Not applicable
3,516 Views
4 Replies
Message 1 of 5

Get instance AutoCAD with c#

Anonymous
Not applicable
Hello,

In order to create a AutoCAD instance with c# is necessary this source code:

private Autodesk.AutoCAD.Interop.AcadApplicationClass _app = new Autodesk.AutoCAD.Interop.AcadApplicationClass();


Is it possible get an existing instance of AutoCAD with c#?

thanx
0 Likes
3,517 Views
4 Replies
Replies (4)
Message 2 of 5

Anonymous
Not applicable
System.Runtime.InteropServices.Marshal.GetActiveObject()
--
Bobby C. Jones
http://www.acadx.com


wrote in message news:5025254@discussion.autodesk.com...
Hello,

In order to create a AutoCAD instance with c# is necessary this source code:

private Autodesk.AutoCAD.Interop.AcadApplicationClass _app = new
Autodesk.AutoCAD.Interop.AcadApplicationClass();


Is it possible get an existing instance of AutoCAD with c#?

thanx
Message 3 of 5

Anonymous
Not applicable
You can start acad as a process by:

Process myProcess = new Process();
myProcess.StartInfo.FileName = acadFilePath + "acad.exe";
myProcess.StartInfo.WindowStyle = myProcessssWindowStyle.Hidden;
myProcess.Start();
object obj = null;
obj = Marshal.GetActiveObject("AutoCAD.Application");

obj now points to an autocad object

or if you want to get the process:
System.Diagnostics.Process [] p = Process.GetProcessesByName("acad");

hope this helps
0 Likes
Message 4 of 5

jbooth
Advocate
Advocate
This is just something on the side, but you CAN call some of those custom methods that VB programmers are so used to (in C#). You do so by using the Microsoft.VisualBasic namespace.

For example: you can get the Autocad application object by using
Microsoft.VisualBasic.Interaction.GetObject("", "AutoCAD.Application");

However. the solution posted previously is more correct, since that's what the Marshal class is for. This is just FYI.
0 Likes
Message 5 of 5

Anonymous
Not applicable
Hello everybody,

I used something similar to run a #net code called by an icon. That mean I need to find the existing AcadProcess and not open an new Acad instance.
But I have a problem: If different instances of Acad are opened, it always find the first Acad instance. What I try to do is finding the calling Acad instance, what's the instance where click the icon.
But how to do ?

Thanks for code, help or advices,
Jean-Marc

PS: I'm under AcadMap2004.
0 Likes