C # operate CAD-Initialize and reference dll,
There are many ways to operate cad, such as C, C ++, vb, lisp (the most efficient, but the language structure is too bad) and C #, because I am waiting for personal habits and convenience, here we will explain how to use C #, and will update operation layers, extended data, drawing, and other operation steps in the future. Of course, the premise is that you can use cad programs on your computer, or you cannot debug them!
1. Create a C # solution and a new class library.
2. reference the dll Interface in cad.
Reference acdbmgd. dll and acmgd. dll, which can be easily found in the cad installation directory.
Do not forget to modify the dll attribute to copy to local.
3. Reference a namespace.
In fact, this step is dispensable, because if vs contains plug-ins such as resharp, you can only prompt reference when necessary. Write down the frequently used namespace here:
Using Autodesk. autoCAD. databaseServices; // (Database, DBPoint, Line, Spline) using Autodesk. autoCAD. geometry; // (Point3d, Line3d, Curve3d) using Autodesk. autoCAD. applicationServices; // (Application, Document) using Autodesk. autoCAD. runtime; // (CommandMethodAttribute, RXObject, CommandFlag) using Autodesk. autoCAD. editorInput; // (Editor, PromptXOptions, PromptXResult) using AcadApp = Autodesk. autoCAD. applicationServices. application;
To write code later, you can also add frequently-used classes in the class, such:
Public Document doc = AcadApp. documentManager. mdiActiveDocument; public Database db = AcadApp. documentManager. mdiActiveDocument. database; public Editor ed = AcadApp. documentManager. mdiActiveDocument. editor; 4. There are two ways to interact with the user's Foreground Data. One is to enter a custom name through the command line, and the other is to operate through the custom winform button, the principle is the same. Here we will introduce them one by one. First of all, we need to use commands to interact with the front-end, because winform also needs to be called out by command, which is a basic requirement. The method is very simple. Just add a tag to the class, for example:
[CommandMethod("myZoom")]public void Zoom() { Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; Point2d pt1 = new Point2d(0, 0); Point2d pt2 = new Point2d(2, 2); string cmd = string.Format("Zoom {0},{1} {2},{3} ", pt1.X, pt1.Y, pt2.X, pt2.Y); doc.SendStringToExecute(cmd, true, false, false);}
This is a custom image scaling command. Input myZoom to automatically scale the rectangle from (0, 0) to (2, 2.
5. Okay. A simple function is complete. How can we call it after the dll is generated?
Simply open the dwg graph with cad, enter netload in the command line (or select tool-load net program in the menu bar), and select the generated dll file.
Enter myZoom and try again to see if there is any change? If you have any questions, please leave a message and reply. Let's learn and make progress together.
In the future, pay attention to more cad operations.