.NET development AutoCAD2006 Guide (1)

xiaoxiao2021-03-05  22

.NET development AutoCAD2006 Guide (1)

C # talent (Email: zhf7878@sohu.com)

The time of popular development AutoCAD, one is ObjectARX and the other is the VBA comes with AutoCAD (AutoCAD's built-in Visual Lisp is also one, but it can only develop some small programs, so it cannot be related to the previous two tools ratio). However, both have obvious shortcomings. The development of VC -based ObjectARX is very powerful, but to learn and master VC and ObjectARX are more difficult for ordinary people. And everyone should notice that ObjectARX is based on VC , but the latest Objectarx2006 can only develop in Visual Stuio.NET 2001, and cannot use the currently used Visual Studio.net 2003 (Oh, Of course, you cannot use a higher version of Visual Studio.net). VBA is just the opposite, it is very easy to get started, but it is not that simple matter to write efficient or complex programs. More bad, VBA has no update version because VB has been replaced by VB.Net.

Is there a tool for developing AutoCAD in conjunction with ObjectARX and VBA advantages? The answer is to utilize powerful and easy to use .NET. For AutoCAD 2004 and previous versions, you can use ActiveX to perform AutoCAD .NET program development (I also wrote this article), but the developed procedure is not true .NET program, function is not very powerful And there is no advantage over ObjectARX or VBA. With the AutoCAD2005, there is a real Objectarx® Managed Wrapper Classes that can develop .NET programs, but its features are still very limited, and if the most basic function of reading the AutoCAD command line is not. With the release of the latest AutoCAD2006, the performance of Objectarx® Managed Wrapper Classes is greatly improved, and only some of the functarx has only some functions and features (and these stuff often is very important for AutoCAD), now Objectarx® Managed Wrapper Classes is available.

The most exciting thing is to use Objectarx® Managed Wrapper Classes, you can easily generate AutoCAD toolbars, panels, and extend multi-page dialogs (huh, English named Extend Tabed Dialogs, may translate less accurate, used CAD should know the options dialog box, the extended multi-page dialog box is this kind, even you can add your custom page in the Options dialog box, isn't it cool!), And these are often ObjectARX and VBA are very difficult.

Similar to the property of the panel

Extend the multi-page dialog, you can add a custom tab to the AutoCAD's option dialog box, it's too cool!

Oh, please, let's take a specific example to see how to use Objectarx® Managed Wrapper Classes (2006) to develop AutoCAD programs, this example is drawn a circle in AutoCAD, and then according to users The selection in the command line changes the color of the circle. First explain, you can use any version of Visual Studio.net (whether it is an old version of 2001, or 2003, or the latest 2005) to develop AutoCAD .NET program development, programming language you can use C # or VB.NET . I use Visual Studio.Net 2003, the language is C #, the AutoCAD version is 2006 English. The following is a specific step (you can see the presentation video in the attachment):

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

2. Add related references. Please add the ACDBMGD.DLL and AcmbMgd.dll in the AutoCAD2006 installation directory to the project.

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

Using system;

// The namespace added below is used when developing an AutoCAD program.

Using autodesk.autocad.DatabaseServices;

Using autodesk.autocad.runtime;

Using autodesk.autocad.geometry;

Using autodesk.autocad.ApplicationServices;

Using autodesk.autocad.editorinput;

Using dbtransman = autodesk.autocad.DatabaseManager;

Namespace Example

{

Public Class Class1

{

// Register an AutoCAD command, you can call the .NET program by entering this command in the command line of AutoCAD

[CommandMethod ("Test")]]

Public void test ()

{

/ / Define the round center

Point3D center = new point3d (100, 100, 0);

/ / Define the radius of the circle

Double Radius = 50;

/ / Define a circle object to indicate the circle you want to generate, the second parameter of the incoming parameter is a circular normal, that is, on what faces of the round, //, because the AutoCAD program is generally a flat problem, so You usually define this method to // z-axis direction.

Circle Circle = New Circle (Center, New Vector3D (0.0, 0.0, 1.0), RADIUS;

// Get the database where the current active AutoCAD document is located

Database db = Application.documentManager.mdiactiveDocument.database;

// Get a transaction manager

DBTRANSMAN TM = DB.TRANSACTIONMANAGER;

// Start transaction processing, that is, add something to CAD

Using (Transaction Trans = Tm.StartTransaction ())

{

// Get the AutoCAD block table, AutoCAD will add information in the object in the graph to this table.

BlockTable Bt = (BlockTable)

TM.GetObject (DB.BlockTableID, OpenMode.forread, False);

// Get a block record

BlockTableRecord Btr = (BlockTableRecord)

TM.GetObject (bt [blocktableRecord.modelspace], openmode.forwrite, false); // Record the circle to the block table

Btr.Appendentity (Circle);

/ / Add to AutoCAD

TM.AddNewlycreatedDbobject (circle, true);

TRANS.COMMIT ();

}

}

}

}

Is it a bit difficult to see something above? (Oh, if you want to figure out what block, block record, transaction, you have to understand some things in the inside of AutoCad, this I will tell it later) Isn't it just add a round? What should be so complicated? ! In order to enable beginners to enter the world of AutoCAD, I give you a library zhfarX, you can simplify the above programs through this library (About the zhfarx library, you can download it in the attachment. About this library detail Introduction, please see my next article). The following is to add round code after reference to 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.DatabaseManager;

Using zhfarX; // This is the reference to the 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 corresponding to the Circle class in the Zhfarx library.

Circles Circle = New Circles (Center, Radius);

// enttive is a class that is used in the zhfarx library to join the graphics to AutoCAD

Entities.Addentity (Circle);

}

}

}

Oh, it's much simpler.

The following code changes the color of the circle based on the selection of the user in the command line.

// editor represents the autocad command line

Editor ED = Entities.EDitor;

// PromptKeywordOptions Defines a list of keywords

PromptKeywordOptions OPT =

New PromptKeywordOptions ("Select Colors [Green (G) / Blue (B)] ");

// Add keyword list

Opt.keywords.add ("r");

Opt.keywords.Add ("g");

Opt.keywords.add ("b");

/ / Get the keyword entered by the user

PromptResult result = ed.getKeywords (OPT);

/ / Judgment if the defined keyword is input

IF (Result.Status == Promptstatus.ok)

{

/ / Change the color according to the keyword selected by the user

Switch (result.stringResult) {

Case "R":

// PutcolorIndex is a function of changing the color color in the zhfarx library

Entities.putColorindex (Circle, 1); Break;

Case "G":

Entities.putColorIndex (Circle, 3);

Break;

Case "B":

Entities.putColorIndex (Circle, 5);

Break;

}

}

Compile this program to generate an example.dll file, which is the .NET program you have written.

The complete source is placed in the attachment.

4. Open AutoCAD and enter the NetLoad command in the command line. Select the example.dll just generated in the pop-up dialog. Next, enter the name TEST of the command defined in the program in the command line, you should see a circle. At this time, select Color [Green (G) / Blue (B)] [R / B]] prompts, you can type R, and round it to red; type G, Round changes to green; type B, round to blue.

Ok, I will introduce here today.

annex:

Http://www.mjtd.com/bbs/viewfile.asp?boardid=33&id=18012

Demo video

http://wordmemory.net/ARX.rar

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

New Post(0)