C # to AutoCAD two times development __c#

Source: Internet
Author: User
Tags dbx

Two development of AutoCAD can be used: Objectarx,vba,vlisp. But instead of borrowing them here, you use C # development directly.

There are two ways to class libraries and applications:

Method 1:vs2010 Develop AutoCAD 2008 class Library

Create a dynamic library, use netload from the AutoCAD command line, and then execute its method

Create a project

1, build a wxindows form program "Project", set output as "Class library"
2, add Reference--browse--From the AutoCAD2008 installation directory C:\Program FILES\AUTODESK\MDT 2008, find references Acdbmgd.dll and Acmgd.dll



3, refer to the following namespaces
Using Autodesk.AutoCAD.EditorInput;
Using Autodesk.AutoCAD.ApplicationServices;
Using Autodesk.AutoCAD.Runtime;


4, before the method name, adds the characteristic commandmethod

5, the complete code is as follows:

Using System;
Using System.Collections.Generic;
Using System.Text;
Using Autodesk.AutoCAD.EditorInput;
Using Autodesk.AutoCAD.ApplicationServices;
Using Autodesk.AutoCAD.Runtime;
Namespace ClassLibrary2
{
public class Class1
{

[Commandmethod ("HelloWorld")]
public void HelloWorld ()
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
Ed. Writemessage ("HelloWorld CAD.") ");
}
}
}

The target frame frame version of the second project
Developed in VS2010, the default version is the. NET Framework version 4.0 high, for reference to AutoCAD 2008, can not compile
In the engineering properties, the target frame is changed to the. NET Framework 2.0 or the. NET Framework 3.5.


Method settings for three-way debugging


Debugging method of VS2010 generation AutoCAD2008 class Library

Properties--debugging--External launcher:

C:\Program FILES\AUTODESK\MDT 2008\acad.exe

Four start AutoCAD 2008

1 in the VS2010 development environment, after compiling the link completes, presses the F5 key to start the debugging, waits for the automatic start AutoCAD 2008 completes,

2 at the command line input: netload, Pop-up Loaded Class Library dialog box,
Browse to find the class library that just compiled, Classlibrary2\classlibrary2\bin\debug\classlibrary2.dll

3 at the command line input: HelloWorld,
Will prompt is: unknown command, due to Acdbmgd.dll Acmgd.dll version is too high


V. Re-introducing a lower version of the dynamic library


The dynamic library Acdbmgd.dll Acmgd.dll with AutoCAD 2008
Version 17.1.0.0, runtime version v2.0.50727

A high version, in the VS2010 project referenced, although can be generated by the dynamic library, but in AutoCAD Netload can be loaded, but the implementation of the method, hint is unknown command, there are two ways:

1 The version of Acdbmgd.dll and Acmgd.dll in the installation directory referencing AutoCAD is 17.1.0.0, from its properties

Change "Copy Local" to "False", so that the form of the DLL method, in AutoCAD can be recognized, is no longer unknown command



2 can download a lower version of the web, such as I download the following version, add reference to them on it

Version 16.2.54.0
Run-time Version v1.0.3705




===================================

Method 2:c# to build a AutoCAD2008 application

First, build a project based on Windowsformapplicaton

Add reference for type two libraries

Right-click the project's references-add reference-from the COM page, locate the following two type libraries

1 AutoCAD 2008 Type Library
Reference name-----The corresponding dynamic library
AutoCAD-----Autodesk.AutoCAD.Interop.dll Embedded Interop Type False

2 Autodesk AutoCAD Mechanical 1.0 Type Library
Reference name-----The corresponding dynamic library
Acadmauto-----Interop.AcadmAuto.dll, embedding Interop type False
Axdblib-----Autodesk.AutoCAD.Interop.Common.dll
Geauto-----Interop.GEAuto.dll


The three main operational functions

1 Add statements in files used by
Using AutoCAD = Autodesk.AutoCAD.Interop;
Using System.Runtime.InteropServices;
Using DBX = Autodesk.AutoCAD.Interop.Common;


2 Note Operation code

Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Linq;
Using System.Text;
Using System.Windows.Forms;
Using System.Windows.Forms;

Using AutoCAD = Autodesk.AutoCAD.Interop;
Using DBX = Autodesk.AutoCAD.Interop.Common;


Using Smartsoft.acad;
Namespace Aotucadwinfrm
{
public partial class Form1:form
{
Public Form1 ()
{
InitializeComponent ();
}

private void Button1_Click (object sender, EventArgs e)
{

Autocadconnector acd=new Autocadconnector ()//Generate Action Class object

Dbx. Axdbdocument doc_as = ACD. Getthisdrawing ("C:\\doc_as.dwg", "");
Dbx. Axdbdocument Acddoc = ACD. Getthisdrawing ("C:\\d1.dwg", "")//Open graphics file
Dbx. Acadblockreference BRF = ACD. Getblockreference (Acddoc, "pp");
Acd. Getentityreference (Acddoc, doc_as);//delete unwanted entities
Acd. Dispose ();
}
}
}


3 Used to transform the class from the Web
Using System;
Using AutoCAD = Autodesk.AutoCAD.Interop;
Using System.Runtime.InteropServices;
Using DBX = Autodesk.AutoCAD.Interop.Common;
Namespace Smartsoft.acad
{
///
Read AutoCAD attribute Information
///
public class Autocadconnector:idisposable
{
Private Autocad.acadapplication _application;
private bool _initialized;
private bool _disposed;
Private dbx. Axdbdocument doc_as;

#region class initialization and destructor operation
///
Class, trying to get a running AutoCAD instance,
If not, a new instance is started.
///
Public Autocadconnector ()
{
Try
{
Get a running AutoCAD instance
This._application = (autocad.acadapplication) marshal.getactiveobject ("autocad.application.17");
}//end of Try
Catch
{
Try
{
A new AutoCAD instance is established and the logo has been established successfully.
_application = new Autocad.acadapplicationclass ();
_initialized = true;
}
Catch

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.