. NET Development AutoCAD Guide (i)

Source: Internet
Author: User
Tags define command line keyword list wrapper visual studio advantage
The current popular development of AutoCAD tools, one is Objectarx, and the other is AutoCAD with VBA (AutoCAD built-in Visual Lisp was also counted as a, but it can only develop a few small programs, so it can not be compared with the previous two tools). But both have obvious deficiencies, to VC + + based on the development of the function of Objectarx is very powerful, but to learn and master VC + + and Objectarx for ordinary people is more difficult. And what you should note is that Objectarx is based on VC + +, but the newest ObjectARX2006 can only be developed with visual stuio.net 2001来, not using the Visual Studio.NET now commonly used 2003 (Oh, of course, can not use a higher version of Visual Studio.NET). VBA, on the other hand, is very easy to get started with, but it's not easy to write efficient or complex programs. Worse, VBA has no newer version, because VB has been replaced by vb.net.
Is there a tool to develop AutoCAD that combines the advantages of Objectarx and VBA? The answer is to take advantage of powerful and easy-to-use. Net. For AutoCAD 2004 and previous versions, you can use ActiveX for AutoCAD . NET program development (I have also written this article), but the developed program is not a true sense of the. NET program, the function is not very powerful, and Objectarx or VBA compared with no advantage. By AutoCAD2005, there is a real objectarx®managed wrapper classes that can develop. NET programs, but its functionality is still limited, such as not even the most basic functions for reading the AutoCAD command line. With the release of the latest AutoCAD2006, the performance of Objectarx®managed wrapper classes has been greatly improved, Previously only Objectarx have some functions and functions (and these things are often very important for the development of AutoCAD), now objectarx®managed wrapper classes are available.

The most exciting thing is, using objectarx®managed wrapper classes, you can easily generate AutoCAD toolbars, panels and Extended multi-page dialog box (ha, English name is extend tabbed dialogs, may not be translated very accurately , people who have used CAD should know the Options dialog box, the Extended multi-page dialog box refers to this one, and even you can add your own custom page in the Options dialog, isn't it cool! , and these are often very difficult to do Objectarx and VBA.


A panel similar to a property


Extended multi-page dialog box, you can add a custom tab in the AutoCAD Options dialog, it's cool!

Oh, the cost of saying so much, or let us use a concrete example to see how to use Objectarx®managed wrapper classes (2006) to develop AutoCAD program, this example is first in AutoCAD to draw a circle, Then change the color of the circle based on the user's selection on the command line. First of all, you can use any version of Visual Studio.NET (whether the old version of 2001, or 2003, or the latest 2005) for AutoCAD. NET program development, programming languages you can use C # or vb.net. I am using Visual Studio.NET 2003, and the language is C#,AUTOCAD version 2006 English version.

Here are the specific steps (see the demo video in the attachment):

1. In Visual Studio.NET, create a class library project.

2. Add the relevant references. Please add the Acdbmgd.dll and Acmgd.dll under the AutoCAD2006 installation directory to the project.

3. In the source code file, add the following code:

Using System;
The namespaces that are added below are all used when developing AutoCAD programs
Using Autodesk.AutoCAD.DatabaseServices;
Using Autodesk.AutoCAD.Runtime;
Using Autodesk.AutoCAD.Geometry;
Using Autodesk.AutoCAD.ApplicationServices;
Using Autodesk.AutoCAD.EditorInput;
Using Dbtransman=autodesk.autocad.databaseservices.transactionmanager;



Namespace Example
{
public class Class1
{
To register the AutoCAD command, you can call the. NET program by entering this command at the command line in AutoCAD.
[Commandmethod ("Test")]
public void Test ()
{
To define the center of a circle
Point3D center=new Point3D (100,100,0);
Define the radius of a circle
Double radius=50;
Define a Circle object to represent the circle you want to generate, and the second argument you pass in is circular, which is the circle of life,//into what surface, because the AutoCAD program is generally a plane problem, so you generally define this normal vector as a//z axis direction.
Circle circle=new Circle (center,new Vector3D (0.0,0.0,1.0), radius);
Get the database where the currently active AutoCAD document resides
Database db=application.documentmanager.mdiactivedocument.database;
Get transaction manager
Dbtransman tm=db. TransactionManager;
Start a transaction, which means adding something to the CAD
using (Transaction Trans=tm. StartTransaction ())
{
Get AutoCAD block table, AutoCAD will be added to the graph of the object information are placed in this table
Blocktable bt= (blocktable)
Tm. GetObject (db. Blocktableid,openmode.forread,false);
Get block Table records
Blocktablerecord btr= (Blocktablerecord)
Tm. GetObject (Bt[blocktablerecord.modelspace],openmode.forwrite,false);
To add information about a circle to a block table record
Btr. Appendentity (circle);
Adding a circle to AutoCAD
Tm. Addnewlycreateddbobject (circle,true);
Trans.commit ();
}
}
}
}
Does it feel a little hard to look at the things above? (hehe, if you want to figure out what block table, block table records, transaction processing, you have to understand the AutoCAD inside some things, which I will speak later) is not to join a circle? To be so complicated?! In order for beginners to quickly enter the world of. NET development AutoCAD, I have compiled a library Zhfarx, you can use this library to simplify the above program (about the Zhfarx library, you can download in the attachment.) Please see my next installment for a detailed introduction to this library. Here is the code to add a circle after referencing the Zhfarx library:

Using System;
Using Autodesk.AutoCAD.DatabaseServices;
Using Autodesk.AutoCAD.Runtime;
Using Autodesk.AutoCAD.Geometry;
Using Autodesk.AutoCAD.ApplicationServices;
Using Autodesk.AutoCAD.EditorInput;
Using Dbtransman=autodesk.autocad.databaseservices.transactionmanager;
Using zhfarx;//This is the referenced Zhfarx library
Namespace Example
{
public class Class1
{
[Commandmethod ("Test")]
public void Test ()
{
Point3D center=new Point3D (100,100,0);
Double radius=50;
Circles is a class that corresponds to the circle class in the Zhfarx library
Circles Circle=new circles (Center,radius);
Entities is a class that is used in Zhfarx libraries to add graphics to AutoCAD.
Entities.addentity (circle);
}
}
}
Oh, is not a lot simpler.

The following code changes the color of a circle based on the user's choice of the command line.

Editor on behalf of AutoCAD command line
Editor Ed=entities.editor;
promptkeywordoptions defines a keyword list option
Promptkeywordoptions opt=
New Promptkeywordoptions ("Choose color [Green (G)/Blue (B)]< red (R) >");
Add Keyword List
Opt. Keywords.add ("R");
Opt. Keywords.add ("G");
Opt. Keywords.add ("B");
Get keywords for user input
Promptresult result=ed. GetKeywords (opt);
Determines whether a defined keyword is entered
if (result. Status==promptstatus.ok)
{
Change the color of a circle according to the user's chosen keyword
Switch (result. Stringresult) {
Case "R":
Putcolorindex is a function of changing object color in Zhfarx library
Entities.putcolorindex (circle,1);
Break
Case "G":
Entities.putcolorindex (circle,3);
Break
Case "B":
Entities.putcolorindex (circle,5);
Break
}
}

Compile this program and generate Example.dll files, which is the. NET program you wrote.

The complete source program is placed in the attachment.

4. Open AutoCAD and enter the netload command at the command line. In the pop-up dialog box, select the Example.dll you just generated. Next, enter the name of the command defined in the program on the command line test, so you should see a circle. At this point, the selection color [Green (G)/Blue (B)]< Red (R) >[r/g/b] appears in the command line, you can type R, the circle turns red; type G, the circle turns green; type B, and the circle turns blue.
OK, here we go today.

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.