CAD二次开发(C#)第二个例子

来源:互联网 发布:无线音响 知乎 编辑:程序博客网 时间:2024/05/17 21:37

设置参考第一个例子。引入必要的命名空间

第二个例子:交互提示选择一个直线的起始点和终点,然后绘制一条以选择的两个点为始终的直线

代码如下,尽量添加了注释。

using System;using System.Collections.Generic;using System.Linq;using System.Text;using Autodesk.AutoCAD.ApplicationServices;using Autodesk.AutoCAD.EditorInput;using Autodesk.AutoCAD.DatabaseServices;using Autodesk.AutoCAD.Runtime;using Autodesk.AutoCAD.Geometry;namespace FirstCAD{    public class Class1    {        [CommandMethod("CreateLine")]//设计的新命令        public void CreateLine()//CreateLine命令所调用的函数        {            //打开数据库            Database db = HostApplicationServices.WorkingDatabase;            //获取当前的编辑器输入类            Editor ed = Application .DocumentManager .MdiActiveDocument .Editor ;            //点交换输入类;提示选择起始点            PromptPointOptions ppo = new PromptPointOptions("\n请选择直线的起始点");            //点交互输入类:提示选择终点            PromptPointResult ressp = ed.GetPoint(ppo) ;            //获取点交互输入类的结果            PromptPointOptions ppe = new PromptPointOptions("\n请选择直线的终点");            PromptPointResult resep = ed.GetPoint(ppe);            //点交互类的结果转换为Point3d坐标点             Point3d startPoint = ressp .Value ;            Point3d  endPoint = resep .Value ;            //构建一个直线的实例            Line oldLine = new Line(startPoint ,endPoint );            //启动一个任务,保证操作的原子性            using (Transaction trans = db.TransactionManager.StartTransaction())            {                //打开块表,                BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);                //打开块表记录,获取模型空间的记录                BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);                //在块表记录中添加创建的直线实体                 btr.AppendEntity(oldLine);                //告知数据库新对象加入                trans.AddNewlyCreatedDBObject(oldLine, true);                //任务提交                trans.Commit();            }        }    }}

编译之后,利用NETLOAD命令,载入生成的DLL,之后输入CreateLine命令,会分别提示选择起始点和终点,选择之后,会在模型空间中绘制一条直线。

选择直线起始点

这里写图片描述

选择直线终点

这里写图片描述

最终效果

这里写图片描述

原创粉丝点击