Secondary development of AutoCAD using C # (2)

zhaozj2021-02-16  50

Secondary development of AutoCAD using C # (2)

C # talent bird

Hello everyone, today I continue to introduce you to the second development of AutoCAD using C #. In this lecture, it mainly introduces the problems existing in the previous talk.

In the previous example, I used the AutoCAD 2004 Type library to communicate between C # and AutoCAD, but this method has two fatal disadvantages. The first disadvantage is that the C # must restart AutoCAD every time the debugger, if the number of times the debugging is large (such as tracking error and then debugging), then the programming efficiency is very low, because starting a CAD still takes a longer time . The second disadvantage is more decent with respect to the first disadvantage. Due to the problem of .NET itself, the interop.autocad.dll file (that is, the communication between C # and AutoCAD) has some bugs, so although your code is completely correct, the C # compiler is still throwing An inexplicable mistake. Isn't that end? I once had a stage because these two hit Dongdong gave up C # and wanted to change the ObjectARX, huh, but still luck, I occasionally read an article written by foreigners in the Internet, he specially introduced The solution for these two issues. Below to solve these two problems.

First, look at the second problem, follow the steps below:

1. Just use Visual Studio .NET to create a C # application, follow the setup in the previous article to join AutoCAD 2004 Type Library, then do not join any code, compile your program.

2. Use ILDASM.EXE in the Visual Studio .NET command line tool (this tool can be found in the Visual Studio .NET installation CD) put the interop.autocad.dll file (this file BIN / RELEASE Compiled into an intermediate language interop. AutoCAD.IL in the folder. Note: The compilation setting of the item established in step 1 is release mode.

ILDASM.EXE / SOURCE Interop.autocad.dll / Output = Interop. AutoCAD.IL

Also pay attention: put ildasm.exe, interop.autocad.dll in the same directory.

3. Open the interop. Autocad.il file in Notepad, then look for the end of "SinkHelper" and start the statement of ".class private auto ANSI SeaCad", change the private in the statement to public, then save Interop. AutoCad.il file.

4. Compile Interop.autocad.dll files using ILASM.EXE, which is also made under Visual Studio .NET command line tools.

ILASM.EXE /RESOURCE = Interop.autocad.res / dll interop.autocad.il / output = interop. AutoCAD.DLL

The interop.autocad.RES file is generated in step 1.

5. Obviously, you don't want to join Interop. AutoCad.dll through the method described in the previous article every time you write an application, it is too much trouble. You can use the following method to let the program automatically join the file: Find the C: / Program Files / Microsoft.Net / Primary Interop Assemoft.Net / Primary Interop assembly folder, then generated above

Interop.autocad.dll file copies. Ok, the second question is solved, and then look at the first one.

In VBA, the programmer can use the getObject function to get the current active autocad object, but in C #, don't, for this function I almost parallel MSDN, then go to the various C # Forum to ask everyone, the result is not Get solved, huh, huh, maybe use C # people in China. It is still seen in the foreigner forum that is the article that speaks this problem is solved. Use the following statement to get the current actoCAD object:

(Acadapplication) Marshal.getActiveObject ("AutoCad.Application.16")

(For CAD2000 and CAD2002, 16 is changed to 15)

Of course, the above statement must be used in the case of AutoCAD, otherwise an error will occur. For the case where AutoCAD is not open, the method of the previous article can be used. Complete connection of AutoCAD and C # as follows:

Using system;

Using AutoCAD;

Using system.Runtime.InteropServices;

Namespace Academplexample

{

Public Class AutoCadconnector: IDisposable

{

PRIVATE ACADAPPLICATION_Application;

Private bool_initialize;

Private bool_disposed;

Public AutoCadconnector ()

{

Try

{

// Upon Creation, Attempt to Retrieve Running Instance

_Application = (acadapplication) Marshal.getActiveObject ("AutoCad.Application.16");

}

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 IntersAl 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 create Our AutoCAD Instance, Call ITS // Quit Method to Avoid Leaking Memory.

IF (! this._disposed && _initialize)

_application.quit ();

_disposed = true;

}

}

}

Use Visual Studio.net to compile the above program into a class library, you can use it in the later program, the following example illustrates its usage. (First include the Academple class library in the project)

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 ();

}

}

}

This example is the title of the current document in AutoCAD in the C # window.

转载请注明原文地址:https://www.9cbs.com/read-22175.html

New Post(0)