使用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命令。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值