Hello everyone, today I continue to introduce you to use C # for AutoCAD two development. In this lecture, we mainly introduce the problems in the previous example.
In the last example, I used the AutoCAD-Type library to communicate between C # and AutoCAD, but this approach has two fatal drawbacks. The first drawback is that every time you debug the program, C # must restart AutoCAD, if the number of debugging is very much (such as tracking errors and then debugging), then the efficiency of programming is very low, because the start of a cad or take a long time. Compared with the first shortcoming, the second disadvantage is more fatal. Because. NET itself, the Interop.AutoCAD.dll file (it is through it to realize the C # and AutoCAD communication) There are some bugs, so although sometimes your code is completely correct, but the C # compiler still throws a baffling error. Isn't that a goner? I once had a stage because these two deadly dongdong almost gave up the C # and want to change to learn Objectarx, hehe, but still lucky, I occasionally read an article on the Internet, and he specifically introduced the two solutions to the problem. Here's how to solve these two problems.
First look at the second problem and follow these steps:
1. Casual use of visual Studio. NET to build a C # application, and then follow the settings in the previous article to add the AutoCAD Type Library and then not add any code to compile your program.
2. In Visual Studio. NET command line tool with Ildasm.exe (this tool can be used in visual Studio. NET installation CD), compile the Interop.AutoCAD.dll file (which is in the Bin\release folder of the project generated in step 1) into intermediate language interop. Autocad.il. Note: The compilation of the project established in step 1 is set to release mode.
Note again: Put the Ildasm.exe,interop.autocad.dll in the same directory.
3. Open Interop in Notepad. Autocad.il the file, and then look for a statement that ends with "sinkhelper" and begins with ". Class private auto ANSI sealed _DACAD", change the private in the statement to public, and then save Interop. autocad.il files.
4. Use Ilasm.exe to compile the interop. autocad.il file into a Interop.AutoCAD.dll file, also in Visual Studio. NET command line tool.
The Interop.AutoCAD.res file is generated in step 1.
5. Obviously you don't want to add interop every time you write an application by using the method that is described in the previous article. AutoCAD.dll, that's too much trouble. You can use the following method to automatically add a program to the file: Locate the C:\Program files\microsoft.net\ Primary Interop Assemblies folder, and then put the resulting
Interop.AutoCAD.dll file copy in.
OK, the second problem is solved, then the first one.
In VBA, programmers can use the GetObject function to get the current active AutoCAD object, but in C # but not, in order to this function I almost rummaged MSDN, and then go to various C # forums to ask you master, the results have not been solved, oh, may be the domestic use of C # Less people. Or in the foreigner's forum to see an article on this issue is to solve the problem. You can get the currently active AutoCAD object by using the following statement:
(For CAD2000 and CAD2002, the 16 is changed to 15)
Of course, the above statement must be in AutoCAD open the case can be used, or there will be errors, for AutoCAD did not open the situation, you can use the method of the previous article to deal with. The complete connection AutoCAD and C # source program are 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 Autocadconnector ()
{
Try
{
Upon creation, attempt to retrieve running instance
Using Visual Studio.NET to compile the above program into a class library, you can use it in future programs, and the following example illustrates its usage. (The Acadexample class library is included in the project first)
Using System;
Using Acadexample;
Using AutoCAD;
Namespace ConsoleApplication6
{
Class Class1
{
[STAThread]
static void Main (string[] args)
{
using (autocadconnector connector = new Autocadconnector ())
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.