Hello everyone, today I will continue to introduce you to the secondary development of AutoCAD using C. This section describes the problems in the previous example.
In the previous example, I used to reference the AutoCAD 2004 Type Library for communication between C # and AutoCAD, but this method has two fatal disadvantages. The first drawback is that each debuggingProgramC # has to restart AutoCAD. If there are many times of debugging (such as tracking errors and debugging), the programming efficiency is very low, it takes a long time to start CAD once. Compared with the first disadvantage, the second one is even worse. Due to the. NET issue, there are some bugs in the InterOP. AutoCAD. dll file (which enables communication between C # and AutoCAD only through it ).CodeIt is completely correct, but the C # compiler still throws an inexplicable error. Isn't it finished? At one stage, I almost gave up C # And wanted to change to ObjectARX because of these two terrible things, I occasionally read an article written by a foreigner on the Internet.ArticleHe specifically introduced the solutions to these two problems. Here we will solve these two problems.
First, let's take a look at the second problem by taking the following steps:
1. Use Visual Studio. NET to create a C # application, add the AutoCAD 2004 Type Library according to the settings in the previous article, and then compile your program without adding any code.
2. use ildasm.exe in the Visual Studio .netcommand line tool (this tool can be used in Visual Studio.. autoCAD. DLL file (this file is in the binrelease folder of the project generated in step 1) compiled into the intermediate language InterOP. autoCAD. il. Note: The project compilation created in step 1 is set to the release mode.
Ildasm.exe/source InterOP. AutoCAD. dll/output = InterOP. AutoCAD. Il
Note: Put ildasm.exe and InterOP. AutoCAD. dll in the same directory.
3. open InterOP in notepad. autoCAD. il file, and then find "sinkhelper" and start ". class private auto ANSI sealed _ dacad statement, change the private in the statement to public, and save InterOP. autoCAD. il file.
4.use ilasm.exe to compile the InterOP. AutoCAD. Il file into an InterOP. AutoCAD. dll file, which is also carried out in Visual Studio. NET command line tool.
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, it is too troublesome for you to add InterOP. AutoCAD. dll every time you write an application. You can use the following method to automatically add the program to the file: Find the C: Program filesmicrosoft. Net Primary InterOP assemblies folder, and then
Copy the InterOP. AutoCAD. dll file.
Okay. The second problem is solved. Next, let's take a look at the first one.
In VBA, programmers can use the GetObject function to obtain the currently active AutoCAD Object, but it does not exist in C #. I almost gave msdn to this function, then I went to various C # forums and asked you experts. The results were not solved. Haha, there may be fewer people using C # in China. I still saw an article on a foreign Forum about this problem to solve this problem. Use the following statement to obtain the active AutoCAD Object:
(Acadapplication) Marshal. getactiveobject ("AutoCAD. application.16 ")
(For cad2000 and cad2002, change 16 to 15)
Of course, the preceding statements can be used only when AutoCAD is opened. Otherwise, an error may occur. If AutoCAD is not opened, you can use the method of the previous article to handle it. The complete source code for connecting AutoCAD and C # is as follows:
Using system;
USING AutoCAD;
Using system. runtime. interopservices;
Namespace acadexample
{
Public class autocadconnector: idisposable
{
Private acadapplication _ application;
Private bool _ initialized;
Private bool _ disposed;
Public autocadconne ()
{
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,
// Garbage collector will upon destruction
~ Autocadconne ()
{
Dispose (false );
}
Public acadapplication
{
Get
{
// Return our internal instance of AutoCAD
Return _ application;
}
}
// This is the user-callable version of dispose.
// It callour internal version and removes
// 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;
}
}
}
Use Visual studio.net to compile the above program into a class library, and you can use it in future programs. The following example illustrates its usage. (First, include the acadexample class library in the Project)
Using system;
Using acadexample;
USING AutoCAD;
Namespace consoleapplication6
{
Class class1
{
[Stathread]
Static void main (string [] ARGs)
{
Using (autocadconnector conne= new autocadconne ())
{
Console. writeline (connector. application. activedocument. Name );
}
Console. Readline ();
}
}
}
This example shows the title of the current document in AutoCAD in the C # window.