C # Secondary Development of ObjectArx cad,

Source: Internet
Author: User

C # Secondary Development of ObjectArx cad,

1. Environment setup: Install the CAD and objectArx libraries. Here cad2012 and objectArx2010 are installed, and vs is 2010.

2. Create a new class library project and reference AcDbMgd. dll and AcMgd. dll in the inc folder under the objectArx installation directory. Note the differences between X86 and X64 systems.

3. Add two classes. One inherits the IExtensionApplication interface, which is the dll entry. cad loads the program from this class for initialization. The other can write custom cad commands.

Reference of 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 to facilitate fast loading. The first class is marked as the entry that inherits the IExtensionApplication interface, and the second class is a custom command.

[Assembly: ExtensionApplication (typeof (cadObjArx. CADExetensionCls)]
[Assembly: CommandClass (typeof (cadObjArx. CADCommandS)]

Code:

Here are only two simple examples. For more information, see the cad development manual .....

Namespace cadObjArx {public class CADExetensionCls: IExtensionApplication {public void Initialize () {// execute the related loading operation Editor ed = Application when loading dll. documentManager. mdiActiveDocument. editor; ed. writeMessage ("\ n load cadObjArx \ n"); load ();} public void Terminate () {// This is the execution of Document doc = Application when launching. documentManager. mdiActiveDocument; doc. lockDocument (DocumentLockMode. notLocked, "", "", false);} private void load () {// Add a toolbar and add a button to bind the following InitT command
// This is implemented by referencing the com component of cad. You need to reference Autodesk of cad. autoCAD. interop and Autodesk. autoCAD. interop. common two com components, AcadMenuGroups menugroups = (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 class CADCommandS {[CommandMethod ("InitT", CommandFlags. modal)] // feature identification, identify this is the cad command public void Init () {// here for a test, the command line outputs a sentence, the Editor ed = Application will be executed by pressing the button added above or entering the command in the command line. documentManager. mdiActiveDocument. editor; ed. writeMessage ("test init \ n");} [CommandMethod ("ListEntities")] public void ListEntities () {// This is the object in the Document opened by the current cad. // You can execute Document acDoc = Application by writing a command in the command line. documentManager. mdiActiveDocument; Database acCurDb = acDoc. database; using (Transaction acTrans = acCurDb. transactionManager. startTransaction () {BlockTable acBlkTbl; acBlkTbl = acTrans. getObject (acCurDb. blockTableId, OpenMode. forRead) as BlockTable; BlockTableRecord acBlkTblRec; acBlkTblRec = acTrans. getObject (acBlkTbl [BlockTableRecord. modelSpace], OpenMode. forRead) as BlockTableRecord; int nCnt = 0; acDoc. editor. writeMessage ("\ nModel space objects:"); foreach (ObjectId acbjid in acBlkTblRec) {acDoc. editor. writeMessage ("\ n" + aco id. objectClass. dxfName + ":" + aco id. handle. value. toString (); nCnt = nCnt + 1;} if (nCnt = 0) {acDoc. editor. writeMessage ("\ nNo objects found. ");} else {acDoc. editor. writeMessage ("\ nTotal {0} objects. ", nCnt );}}}}}

 

4. Debug: In the project properties, set debugging-> Start external programs, specify the cad startup path, and specify the working directory as the generated directory of the current program, which can be debugged.

After each debugging, you must use the netload command to load the dll after starting the cad file. You can add an lsp script file cad2012.lsp in the preceding working directory, add a statement (command "netload" "cadObjArx. dll ")

In this way, after debugging is started, cad automatically loads cadObjArx. dll under the directory.

Another problem is that two objectArx libraries are referenced above. By default, the system copies these two libraries to the generated Directory, which may cause an inexplicable loading failure, it is normal.

5. For deployment, you can create another executable program project and start cad and load dll by calling the com component, you can also use the above method to place the lsp script file and the cad file to be used in the same directory. cad automatically executes the script.

The following uses a console program to start cad and load dll

Console. writeLine ("start .. "); string sProgID =" AutoCAD. application.18.2 "; try {if (System. diagnostics. process. getProcessesByName ("acad "). count ()> 0) // judge whether there is any cad currently running {Console. writeLine ("Get Open cad"); acApp = (AcadApplication) Marshal. getActiveObject (sProgID); // get the currently running cad object} else {Console. writeLine ("open cad"); acApp = new AcadApplication (); // No acApp is directly created. visible = true ;}} catch (Exception exc ){ Console. WriteLine (exc. Message);} if (acApp! = Null) {Console. writeLine ("netload begin"); try {// load dll string dllPath = @ "D: \ workspace \ test \ cadObjArx \ bin \ Debug \ cadObjArx. dll "; string sCommand =" (command \ "netload \" \ "{0} \") \ n "; dllPath = dllPath. replace ("\", "\"); acApp. activeDocument. sendCommand (string. format (sCommand, dllPath);} catch (Exception exload) {Console. writeLine ("netload err: {0}", exload. message );}}

 

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.