使用C#语言,为AutoCAD添加一个名称为QQ的命令。要求:使用Editor类的Command成员方法来调用AutoCAD的Line命令,绘制一条45°的线段和一个直角三角形。

在C#中编写AutoCAD插件,可以使用AutoCAD  .NET  API,以下是实现QQ命令的代码:

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图形成功!");
                        }
                }
        }
}

注意,该代码需在AutoCAD  .NET  API应用程序中才能运行。最简单的方法是将上述代码粘贴到Visual  Studio中的新项目中,并将项目类型设置为Class  Library  (DLL)。在项目属性中添加对AcCoreMgd.dll、AcDbMgd.dll和AcMgd.dll的引用。构建项目以生成DLL文件。在AutoCAD中加载该DLL文件以使用QQ命令。

猜你喜欢

转载自blog.csdn.net/weixin_64338372/article/details/130809006