C # Objectarx CAD two-time development

Source: Internet
Author: User

1 Environment: Install the CAD and Objectarx Library, installed here is cad2012 and objectArx2010, vs is 2010

2 Create a new class library project that references the AcDbMgd.dll and AcMgd.dll under the Objectarx installation Directory Inc folder, noting the differences between X86 and X64 systems

3 Add two classes, one inherits the Iextensionapplication interface, this is the DLL's entrance, CAD will do some initialization from this class loader, and another can write some custom CAD commands

References to CAD:

Using Autodesk.AutoCAD.Runtime;
Using Autodesk.AutoCAD.ApplicationServices;
Using Autodesk.AutoCAD.DatabaseServices;
Using Autodesk.AutoCAD.EditorInput;
Using Autodesk.AutoCAD.Geometry;
Using Autodesk.AutoCAD.Windows;
Using Autodesk.AutoCAD.Interop;
Using Autodesk.AutoCAD.Interop.Common;

You can add an assembly directory for quick loading, where two classes, the first class identifies as portals inherit the Iextensionapplication interface, and the second class is the custom command

[Assembly:extensionapplication (typeof (Cadobjarx.cadexetensioncls))]
[Assembly:commandclass (typeof (Cadobjarx.cadcommands))]

Code:

Here are only two simple examples, more in-depth need to see the CAD Development manual .....

namespacecadobjarx{ Public classcadexetensioncls:iextensionapplication { Public voidInitialize () {//load DLL when loading operation Editor Ed=Application.DocumentManager.MdiActiveDocument.Editor; Ed. Writemessage ("\ n Load cadobjarx\n");        Load (); }         Public voidTerminate () {//This is the implementation Document doc when launched=Application.DocumentManager.MdiActiveDocument; Doc. LockDocument (documentlockmode.notlocked,"","",false); }        Private voidload () {//Add a toolbar Here, add a button to bind the following Initt command
This is implemented by referencing a CAD COM component, which requires reference to CAD Autodesk.AutoCAD.Interop and Autodesk.AutoCAD.Interop.Common Two COM components, acadmenugroups m Enugroups=(acadmenugroups) application.menugroups; Acadtoolbar Toolbar= Menugroups. Item (0). Toolbars.add ("Test"); Acadtoolbaritem Item= toolbar. Addtoolbarbutton (toolbar. Count,"Initt","Test","initt\n"); Item. Setbitmaps ("Set 16x16.bmp","Set 32x32.bmp"); Toolbar. Dock (Actoolbardockstatus.actoolbardocktop); } } Public classcadcommands {[Commandmethod ("Initt", Commandflags.modal)] Attribute identification, which identifies this as a CAD command Public voidInit () {//do a test here, output a word in the command line, press the button added above or enter a command on the command line will be executed Editor Ed=Application.DocumentManager.MdiActiveDocument.Editor; Ed. Writemessage ("Test init\n"); } [Commandmethod ("listentities")] Public voidlistentities () {//This is the object that iterates through the current CAD open Document///command-line to execute documents Acdoc=Application.DocumentManager.MdiActiveDocument; Database Accurdb=acdoc.database; using(Transaction Actrans =acCurDb.TransactionManager.StartTransaction ()) {blocktable acblktbl; Acblktbl= Actrans.getobject (Accurdb.blocktableid, Openmode.forread) asblocktable; Blocktablerecord Acblktblrec; Acblktblrec= Actrans.getobject (Acblktbl[blocktablerecord.modelspace], openmode.forread) asBlocktablerecord; intncnt =0; AcDoc.Editor.WriteMessage ("\nmodel space objects:"); foreach(ObjectId AcobjidinchAcblktblrec) {AcDoc.Editor.WriteMessage ("\ n"+ AcObjId.ObjectClass.DxfName +":"+acObjId.Handle.Value.ToString ()); Ncnt= ncnt +1; } if(ncnt = =0) {acDoc.Editor.WriteMessage ("\nno objects found."); } Else{acDoc.Editor.WriteMessage ("\ntotal {0} objects.", ncnt); } } } }}

4 debugging, in the project properties, set debugging, start an external program, specify the CAD startup path, specify the working directory as the current program's build directory, can be debugged.

After each debugging, after starting the CAD, through the netload command to load the DLL, more trouble, you can add an LSP script file in the above working directory CAD2012.LSP, add a loaded statement (command "Netload" " CadObjArx.dll ")

This allows CAD to automatically load the CadObjArx.dll in the directory after each start of debugging.

There is also a problem, because the above reference to the two Objectarx library, the system by default will copy the two libraries into the build directory, will lead to a strange load failure, set to not output, it is normal.

5 deployment, you can build another executable program, by calling the COM component to start the CAD and load the DLL, or you can use the same way the LSP script file and the CAD file to be used in the same directory, CAD to automatically execute the script

The following starts CAD and loads the DLL through a console program

Console.WriteLine ("start.."); stringsProgId ="autocad.application.18.2"; Try            {                if(System.Diagnostics.Process.GetProcessesByName ("Acad"). Count () >0)//Determine if there is currently a cad running {Console.WriteLine ("get an open CAD"); Acapp=(acadapplication) marshal.getactiveobject (sprogid);//Get the currently running CAD object}Else                {Console.WriteLine ("Open CAD");Acapp =Newacadapplication ();//not directly built a acapp.visible=true; }            }            Catch(Exception exc) {Console.WriteLine (exc.            Message); }            if(Acapp! =NULL)            {Console.WriteLine ("Netload begin"); Try{//Load DLL                    stringDllPath =@"D:\workspace\test\cadObjArx\bin\Debug\cadObjArx.dll"; stringScommand ="(Command \ "netload\" \ "{0}\") \ n"; DllPath= Dllpath.replace ("\\","\\\\"); AcApp.ActiveDocument.SendCommand (string. Format (Scommand, DllPath));                }                Catch(Exception exload) {Console.WriteLine ("netload err:{0}", Exload.                Message); }            }

C # Objectarx CAD two-time development

Related Article

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.