Using C# language, add a command named QQ for AutoCAD. Requirements: Use the Command member method of the Editor class to call the Line command of AutoCAD to draw a 45° line segment and a right triangle.

To write AutoCAD plug-ins in C#, you can use AutoCAD .NET API, the following is the code to implement the QQ command:

using  Autodesk.AutoCAD.EditorInput;
using  Autodesk.AutoCAD.DatabaseServices;
using  Autodesk.AutoCAD.Geometry;
using  Autodesk.AutoCAD.Runtime;

namespace  MyCommands
{
        public  class  Commands
        {
                [CommandMethod("QQ")]
                public  void  QQCommand()
                {
                        Document  doc  =  Application.DocumentManager.MdiActiveDocument;
                        Database  db  =  doc.Database;
                        Editor  ed  =  doc.Editor;

                        using  (Transaction  tr  =  db.TransactionManager.StartTransaction())
                        {
                                BlockTableRecord  btr  =  (BlockTableRecord)tr.GetObject(db.CurrentSpaceId,  OpenMode.ForWrite);

                                //  绘制45度直线
                                Point3d  startPoint  =  new  Point3d(0,  0,  0);
                                Point3d  endPoint  =  new  Point3d(10,  10,  0);
                                Line  line  =  new  Line(startPoint,  endPoint);
                                line.ColorIndex  =  1;
                                btr.AppendEntity(line);
                                tr.AddNewlyCreatedDBObject(line,  true);

                                //  绘制直角三角形
                                Point3d  p1  =  new  Point3d(0,  0,  0);
                                Point3d  p2  =  new  Point3d(10,  0,  0);
                                Point3d  p3  =  new  Point3d(0,  5,  0);
                                Line  l1  =  new  Line(p1,  p2);
                                Line  l2  =  new  Line(p1,  p3);
                                Line  l3  =  new  Line(p2,  p3);
                                l1.ColorIndex  =  2;
                                l2.ColorIndex  =  3;
                                l3.ColorIndex  =  4;
                                btr.AppendEntity(l1);
                                btr.AppendEntity(l2);
                                btr.AppendEntity(l3);
                                tr.AddNewlyCreatedDBObject(l1,  true);
                                tr.AddNewlyCreatedDBObject(l2,  true);
                                tr.AddNewlyCreatedDBObject(l3,  true);

                                tr.Commit();
                                ed.WriteMessage("绘制QQ图形成功!");
                        }
                }
        }
}

Note that this code needs to be run in an AutoCAD .NET API application. The easiest way to do this is to paste the above code into a new project in Visual Studio and set the project type to Class Library (DLL). Add references to AcCoreMgd.dll, AcDbMgd.dll and AcMgd.dll in project properties. Build the project to generate the DLL file. Load the DLL file in AutoCAD to use the QQ command.

Guess you like

Origin blog.csdn.net/weixin_64338372/article/details/130809006