.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

connect to autocad

9 REPLIES 9
Reply
Message 1 of 10
Anonymous
917 Views, 9 Replies

connect to autocad

i have a seemingly dumb question. I have searched this ng and can't seem to
find the answer.

all of the examples show vb.net code for creating commands to implement
inside of autocad using a dll. how does one connect to autocad from a
vb.net/C# standalone application?
9 REPLIES 9
Message 2 of 10
Anonymous
in reply to: Anonymous

You could use ActiveX, or the managed wrappers with .NET Remoting. Generally, any library / exe using the ObjectARX (managed or unmanaged) has to be running inside the memory space of AutoCAD.
Message 3 of 10
Anonymous
in reply to: Anonymous

I don't know what is your goal in AutoCAD
For the simple task you can use Reflection
Here is an example:

http://discussion.autodesk.com/thread.jspa?messageID=5550511

~'J'~
Message 4 of 10
Anonymous
in reply to: Anonymous

i'm trying to write an application similar to the way i would have using
standalone vb [i.e. getobject/createobject]. also, i might want to get a
library.


wrote in message news:5628520@discussion.autodesk.com...
I don't know what is your goal in AutoCAD
For the simple task you can use Reflection
Here is an example:

http://discussion.autodesk.com/thread.jspa?messageID=5550511

~'J'~
Message 5 of 10
NikolayPoleshchuk
in reply to: Anonymous

See examples with CreateObject, GetInterfaceObject:
http://discussion.autodesk.com/thread.jspa?messageID=5220245
Nikolay Poleshchuk
http://poleshchuk.spb.ru/cad/eng.html
Message 6 of 10
Anonymous
in reply to: Anonymous

so am i to understand that in order to connect to autocad from a .net
application i have to use activex com api?


wrote in message
news:5628722@discussion.autodesk.com...
See examples with CreateObject, GetInterfaceObject:
http://discussion.autodesk.com/thread.jspa?messageID=5220245
Message 7 of 10
Anonymous
in reply to: Anonymous

>> You could use ActiveX, or the managed
>> wrappers with .NET Remoting.

AFAIK, the managed wrappers are not remotable.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2008
Supporting AutoCAD 2000 through 2008
http://www.acadxtabs.com
Message 8 of 10
Anonymous
in reply to: Anonymous

Yes, from another .NET application running as a
separate process, ActiveX is the only way.

You can always write an extension application
(DLL) that uses the Managed API and loads
into AutoCAD, and also exposes a COM API
of its own, that would allow you to talk to it
from another process.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2008
Supporting AutoCAD 2000 through 2008
http://www.acadxtabs.com

wrote in message news:5628952@discussion.autodesk.com...
so am i to understand that in order to connect to autocad from a .net
application i have to use activex com api?


wrote in message
news:5628722@discussion.autodesk.com...
See examples with CreateObject, GetInterfaceObject:
http://discussion.autodesk.com/thread.jspa?messageID=5220245
Message 9 of 10
Anonymous
in reply to: Anonymous

They don't have to be. You just implement any method you want and access this custom method through remoting. This just means that you have to add more layers to your app.
Message 10 of 10
Anonymous
in reply to: Anonymous

Sometime ago I ran across the following tutorial:


Getting Connected

by Frank Oquendo



Prerequisites: Basic knowledge of C#



Note to VB.NET programmers: while the source for this project is C#, the
compiled dll is usable in any.NET language.



There has been an increase in the number of posts requesting information
concerning .NET programming for AutoCAD. The good news is that .NET offers
excellent support for ActiveX-enabled programs like AutoCAD. This article
will demonstrate how C# interacts with AutoCAD. To keep things simple, we'll
be discussing how a C# application connects to AutoCAD since that's where
all external programs begin.



For starters, just like any other program written outside of AutoCAD you
need to reference the AutoCAD type library. In fact, unlike Visual Basic 6,
it's a requirement as C# is a strongly-typed language. This means the
compiler will require definitions for all data types in use by your program;
late-binding is not an option.



There are two ways to reference the AutoCAD type library:



You can right-click the References folder in the Solution Explorer window
and select 'Add Reference'. Next, select the COM tab and pick the 'AutoCAD
2000 Object Library' from the list of available references and press 'OK'.
This will result in a file named Interop.AutoCAD.dll. This file is a
runtime-callable wrapper; it contains definitions for all the data types,
methods and properties exposed to COM by AutoCAD.



The second method requires the use of TlbImp.exe. While not as easy or
convenient as the IDE method, it allows for the possibility of signing the
resulting assembly with a strong name. Strong names uniquely identify an
assembly in much the same way a GUID identifies COM objects. The main reason
for signing the wrapper assembly is to allow for signing your main assembly;
the one your wrapper is supporting. If any referenced assembly is not
strongly named, you cannot assign a strong name to your main assembly. This
could prove to be a problem if you intend to distribute your assembly to the
public. For more information on strong names and TlbImp.exe, consult your
Visual Studio.NET documentation. Important note!!! While the above methods
will work for a large number of

ActiveX-enabled applications, AutoCAD requires special handling. Read this
article for more information.



Once you've referenced the AutoCAD wrapper assembly, you simply need to
indicate when you wish to use it. You can either preface your class with
'using AutoCAD;' or you can specify the full namespace to a desired object
(e.g.: AutoCAD.IAcadLine).



Let's start our exploration with how you connect to AutoCAD in a C# program.
If you wish to connect to a running instance of AutoCAD, use the
GetActiveObject method of the Marshal class. You'll find the Marshal class
in the System.Runtime.InteropServices namespace. GetActiveObject accepts a
string containing the ProgId of the object you want. It returns an Object so
you'll have to convert, or cast, the return value to a more appropriate data
type before you can use it.



To illustrate, we'll be creating a class that makes connecting to AutoCAD a
simple process. Start a new C# Class Library project named AcadExample. Add
a reference to the AutoCAD 2000 Object Library as discussed earlier. Next,
rename 'Class1.cs' to 'AutoCADConnector.cs'. Be sure to change all instances
of 'Class1' in your code window to 'AutoCADConnector' as well. Finally,
enter the following code.





using System;

using AutoCAD;

using System.Runtime.InteropServices;



namespace AcadExample

{

public class AutoCADConnector

{

public AcadApplication GetAutoCAD()

{

return

(AcadApplication)Marshal.GetActiveObject("AutoCAD.Application.15");

}

}

}





As you can see, there's not much to it; it works quite similarly to VB's
GetObject function. It's important to note that in order to test this
project you'll need another project, either console or window based, to
create an instance of the AutoCADConnector object and use its functionality.



The code shown will work great if AutoCAD is already running but will throw
an exception if AutoCAD is not present. You should allow for this
possibility by creating an AutoCAD instance if one is not available. In VB,
you'd just use CreateObject. However, there is no such function available in
C#. Luckily, this problem is quite easily solved: whenever you need to
create an instance of AutoCAD, you just create a new instance of
AutoCADApplicationClass. This class is provided for you by the wrapper
assembly you generated when you referenced the AutoCAD type library. All you
have to is catch the exception and attempt to start AutoCAD on your own.
Below is the new code for the GetAutoCAD function:



public AcadApplication GetAutoCAD()

{

try

{

return

(AcadApplication)Marshal.GetActiveObject("AutoCAD.Application.15");

}

catch

{

try

{

return new AcadApplicationClass();

}

catch

{

throw;

}

}

}





The first try/catch block attempts to retrieve a running instance of
AutoCAD. If that fails, we catch the resulting exception and attempt to
create our own AutoCAD instance. If that fails as well, we simply propagate
the exception back to the caller.



While functional, our little class isn't very effective. After all, we have
to create an instance of the object for the sake of calling its only method.
That seems like a lot of overhead so we're going to dress things up a bit.
For starters, instead of using GetActiveObject or creating a new AutoCAD
instance every time the user calls GetAutoCAD, we're going to cache our
instance as a private field. This will improve efficiency by allowing us to
totally bypass the Get/Create process once we've actually connected to
AutoCAD. In order for clients to access a private field, we must add a
public property. Here's what the AutoCADConnector class looks like with
these changes:



using System;

using AutoCAD;

using System.Runtime.InteropServices;

namespace AcadExample

{

public class AutoCADConnector

{

private AcadApplication _application;

public AutoCADConnector()

{

try

{

// Upon creation, attempt to retrieve running instance

_application =
(AcadApplication)Marshal.GetActiveObject("AutoCAD.Application.15");

}

catch

{

try

{

// Create an instance

_application = new AcadApplicationClass();

}

catch

{

throw;

}

}

}

public AcadApplication Application

{

get

{

// Return our internal instance of AutoCAD

return _application;

}

}



}

}





As you can see, we have automated the connection process by moving the
appropriate code into the class constructor. It will now execute as soon as
an instance of the AutoCADConnector class is instantiated. We then make
AutoCAD accessible to the caller through our read-only Application property.



Now for a little finesse. If our object instantiates AutoCAD, the caller
will have to remember to call AutoCAD's Quit method or it will remain loaded
in memory. This is an easy thing to forget so we're going to help things
along a little by implementing the IDisposable interface in our class.
IDisposable offers a standard mechanism for releasing resources on demand.
This is an important consideration since .NET's memory management
architecture does not guarantee objects will be terminated in a timely
manner once there are no outstanding references to them. This differs
dramatically from VB memory management where you could count on
Class_Terminate to clean up your working environment. If you are unfamiliar
with garbage collection, interfaces or the IDisposable interface, be sure to
consult your .NET documentation.



The IDisposable interface only has one method: Dispose. Sounds easy to
implement, right? Well, not quite. Since callers may easily to forget to
Dispose your objects, you have to plan for that possibility. The easiest way
is to define two versions of Dispose: one for the caller and one for the
garbage collector. In addition, you have to make sure that you only call
AutoCAD's Quit method if your object is responsible for creating it; it
would not be a good idea to terminate a previously running instance. We can
solve that problem by using another private field that gets set to true only
if we instantiate AutoCAD. Our Dispose method can check the value of this
flag and call Quit as appropriate. Here's what our class looks like with
these changes:



using System;

using AutoCAD;

using System.Runtime.InteropServices;

namespace AcadExample

{

public class AutoCADConnector : IDisposable

{

private AcadApplication _application;

private bool _initialized;

private bool _disposed;

public AutoCADConnector()

{

try

{

// Upon creation, attempt to retrieve running instance

_application =
(AcadApplication)Marshal.GetActiveObject("AutoCAD.Application.15");

}

catch

{

try

{

// Create an instance and set flag to indicate this

_application = new AcadApplicationClass();

_initialized = true;

}

catch

{

throw;

}

}

}

// If the user doesn't call Dispose, the

// garbage collector will upon destruction

~AutoCADConnector()

{

Dispose(false);

}



public AcadApplication Application

{

get

{

// Return our internal instance of AutoCAD

return _application;

}

}



// This is the user-callable version of Dispose.

// It calls our internal version and removes the

// object from the garbage collector's queue.

public void Dispose()

{

Dispose(true);

GC.SuppressFinalize(this);

}



// This version of Dispose gets called by our

// destructor.

protected virtual void Dispose(bool disposing)

{

// If we created our AutoCAD instance, call its

// Quit method to avoid leaking memory.

if(!this._disposed && _initialized)

_application.Quit();



_disposed = true;

}

}

}





Now that we've done all this work, why not put it to use? Add a new console
project. Right-click the new project in the Solution Explorer window and
pick 'Set As StartUp Project'. Next, right-click the References folder for
your new project and select 'Add Reference'. Select 'AutoCAD 2000 Object
Library' from the COM tab and 'AcadExample' from the Projects tab.



Enter the following code in the Class1 module of your console application
(your namespace may differ):



using System;

using AcadExample;

using AutoCAD;

namespace ConsoleApplication6

{

class Class1

{

[STAThread]

static void Main(string[] args)

{

using (AutoCADConnector connector = new AutoCADConnector())

{

Console.WriteLine(connector.Application.ActiveDocument.Name);

}

Console.ReadLine();

}

}

}





By wrapping our code in a using block, the compiler will generate code that
automatically calls the Dispose method of our class, ensuring proper
disposal of AutoCAD. Try this program with AutoCAD running as well as
without AutoCAD running. You will see that we can connect to AutoCAD either
way and with a minimal amount of effort thanks to all the planning we put
into the design of the AutoCADConnector class.



That's all for this article. I intend to write more articles illustrating
the use of C# for AutoCAD programming but to be honest with you, I'm not
sure what I should write about next. If you have any suggestions, feel free
to email me.

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

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost