This article is based on the new. NET API introduced in AutoCAD 2006 as a tool that describes the. NET platform for AutoCAD two times the development of technology, and with the current common VBA, Objectarx for comparison. It also discusses how to compensate for some of the insufficient features of the. NET API.
The current two development tools of AutoCAD are: Visuallisp, VBA, Objectarx and so on. Among them, Visuallisp and VBA are relatively simple, especially VBA, easy to use and rapid development, but its function compared to Objectarx, especially the object-oriented function support is not good. and Objectarx based on the VC platform, under the support of C + +, its function is very powerful, can be very good use of various object-oriented technology, but its disadvantage is that the speed is relatively slow, at the same time, the ability of developers to demand higher.
. NET is Microsoft's new development platform, with many advantages. Based on. NET platform for two development of AutoCAD to make full use of. NET of various advantages, in the premise of ensuring a strong ability to greatly improve the development speed.
first, based on. NET Development
(1) Introduction to. NET APIs
In the new AutoCAD 2006, Autodesk has added the. NET API for its development. The. NET API provides a series of managed outsourced classes (Managed Wrapper Class) that enable developers to NET Framework, use any support. NET languages, such as vb.net, C #, and managed C + +, have developed AutoCAD two times. Its advantages are fully object-oriented, with the powerful features matching with C + +, with easy-to-use features, is the ideal AutoCAD two times development tools.
(2) The main difference between. NET APIs and traditional Objectarx
The difference between. NET APIs and traditional Objectarx stems from the difference between developing applications in the. NET environment and developing applications in a VC environment. First, in the VC environment, programmers need to manage their own memory application and release, and. NET uses a garbage collection mechanism, the. NET Framework to determine the timing of memory recovery and recycling, so as to solve the C + + programmer headache memory leak problem. It is also because of this feature that in the. NET environment it is not possible to use destructors to release other resources in a C + + environment, requiring programmers to explicitly release them in the program. In the. NET API, the release of resources is primarily done through the Dispose function.
Second, the various reactors in the Objectarx (Reactor) are mapped to various events in the. NET API by the outsourced class, which can respond to various operations of AutoCAD by defining the response functions of those events. The processing of error messages is also handled from the function return value to the usual exception, making it better compatible with. NET. Because languages such as vb.net and C # are completely object-oriented and have no concept of global functions, the. NET API encapsulates global functions under Objectarx as properties of some objects or objects under the. NET API. The series of global functions that interact with the user, such as Objectarx, are encapsulated as commandlineprompt classes.
(3) using the. NET API
In C #, for example, on the Microsoft Visual C # 2005 Express Edition Beta platform, start with a new class library project, Add the Acdbmgb.dll and Acmgb.dll in the AutoCAD2005 installation directory as a reference to the project. These two files contain the. NET API for all outsourced classes.
Then add the following statement in the class where you want to use the. NET API to reference the namespace of the. NET API. The following statements need to be added:
Using Autodesk.AutoCAD.ApplicationServices;
Using Autodesk.AutoCAD.DatabaseServices;
Using Autodesk.AutoCAD.Runtime;
Using Autodesk.AutoCAD.Geometry;
This makes it possible to develop with the. NET API. The following code can be registered as a command "AddLine" in AutoCAD, which adds a line starting at (0,0,0), end point (200,200,0) in the current workspace. The code is as follows:
[Commandmethod ("AddLine")]
public static void Addlinecmd ()
{
Database db = hostapplicationservices.workingdatabase;//Gets the current workspace
Blocktable BT = (blocktable) db. Blocktableid.open (Openmode.forread); Get Block table
Blocktablerecord BTR = (Blocktablerecord) bt[blocktablerecord.modelspace]. Open (Openmode.forwrite); Get block table records for model space
Line line = new Line (new Point3D (0, 0, 0), New Point3D (200, 200, 0)); Create a line
try {
Btr. Appendentity (line); Add a line to model space
Line. Close ();//Turn off the line
}
finally {
Btr. Close ();//closing Block table records
Bt. Close ();//closing block table
}
}
Thus, the above statement and under the VC development very similar, the process is to get the database first, then open the Block table, block table records, and then add entities, finally closed block table, block table records. It is important to note that the finally statement, regardless of whether the statement in the try block is an exception, is executed in the finally block, ensuring that the operation to close the block table and the block table record is executed.
After writing the code, compile, and the compilation will get a DLL file. The DLL file is selected for loading in AutoCAD 2006 through the "Netload" command, which can be executed with the "addline" command after it is successfully loaded. Unfortunately, the current. NET API version does not yet support uninstallation, and you can only turn off AutoCAD to uninstall.
(4) initialization and cleanup of. NET APIs
In Objectarx, the "acrxentrypoint" function is the loading point of the ARX program, and the initialization and cleanup of the program can be performed in the function. In the. NET API, you first need to encapsulate the initialization code in a class, and the class needs to be oppressed to implement
Iextensionapplication interface. The interface contains two functions of initialize and terminate. Where initialize is responsible for initializing the program when it is loaded, terminate is responsible for purging when uninstalling the program. The code is as follows:
Namespace Arxexample {
public class Myarx:iextensionapplication {
......
public void Initialize () {
Initialize operation
}
public void Terminate () {
Clear operation
}
......
}
}
Also, to speed up loading, add the following statement to the MyARX.cs file header:
[Assembly:extensionapplication (typeof (Arxexample.myarx))]
[Assembly:commandclass (typeof (Arxexample.myarx))]
This way, when the program is loaded, AutoCAD will initialize directly through the Initialize statement in Myarx and register the commands in Myarx. Otherwise, AutoCAD will search for all classes in the DLL to initialize the class that implements the Iextensionapplication interface, and not initialize if it cannot be found. Similarly, with the Commandclass property, AutoCAD also searches directly into the Myarx class for the command to be registered. When the program contains a large number of classes, the Extensionapplication and Commandclass properties can significantly speed up the loading of the program.
(5). NET API interacts with COM
In the current. NET API, its functionality is less than that of traditional objectarx, and there are quite a few objectarx functions that are not currently encapsulated in. NET APIs, such as GetPoint. However, you can use ActiveX in COM mode to compensate for the lack of. NET APIs.
With the addition of COM references, the program can use many of the features in VBA. For an example of an event in AutoCAD ActiveX, the following code can add a modified event for all elements in the current workspace:
Database db = Hostapplicationservices.workingdatabase;
Blocktable BT = (blocktable) db. Blocktableid.open (Openmode.forread);
Blocktablerecord BTR = (Blocktablerecord) bt[blocktablerecord.modelspace]. Open (Openmode.forwrite);
try {
Acadobject obj;
Traversing block table records
foreach (ObjectId objId in BTR) {
Acadobject object in ActiveX obtained by Objectid
obj = (acadobject) ((acaddatabase) db. Acaddatabase). Objectidtoobject (Objid.oldid);
Add response modified event for obj
Obj. Modified + = new Iacadobjectevents_modifiedeventhandler (obj_modified);
}
}
finally {
Btr. Close ();
Bt. Close ();
}
Where the event response function obj_modified is represented as follows:
public static void Obj_modified (Acadobject obj) {
Commandlineprompts.message ("Object modified!" + obj.) ObjectID + "\ n");
}
Ii. Conclusion
In this paper, C # as an example, the two-time development of AutoCAD based on. NET API is described in more detail. The. NET API is an ideal development tool with the advantages of easy-to-use VBA and the powerful features of C + + while having Objectarx power. However, the. NET API is somewhat inadequate in some ways, but as its version is updated and perfected, it will become the preferred tool for many developers.
Description: How does the. NET API work? We all know that the function of Acad is actually realized through Objectarx, so. NET API is in fact through VC + +/CLI (the original vc++.net) mixed programming to the ARX function in the form of. NET class to show. That is, using the. NET API actually uses ARX, and how much the. NET API can achieve depends on the degree of packaging, that is, how much arx is packaged. Know Arx friends know that using Arx Library, due to the characteristics of the C + + language, we often need to derive arx class. but use. NET doesn't have to, we often just have to delegate on the corresponding event, which is a whole lot simpler than that ( such as: Autodesk.AutoCAD.ApplicationServices.Application.BeginQuit and other event). Based on the description of the custom object in front of it, it is now clear that the. NET API can replace Arx in addition to some of the custom object functions.
#autocad二次开