基于C#的AutoCAD二次开发之获取用户输入信息、选择集、访问CAD内部命令
基于C#的AutoCAD二次开发之获取用户输入信息、选择集、访问CAD内部命令 在CAD创建图形对象时,经常需要和用户进行交互,例如:直线创建时需要用户输入起点和终点(或长度)信息,复制对象时需要指定源对象等。我的开发环境为Visual Studio 2017 & AutoCAD 2014 & AutoCAD 2020。点拾取实现方法方
·
基于C#的AutoCAD二次开发之获取用户输入信息、选择集、访问CAD内部命令
在CAD创建图形对象时,经常需要和用户进行交互,例如:直线创建时需要用户输入起点和终点(或长度)信息,复制对象时需要指定源对象等。
我的开发环境为Visual Studio 2017 & AutoCAD 2014 & AutoCAD 2020。
- 点拾取
- 实现方法
方式一
PromptPointResult retStPoint = ed.GetPoint(("请输入点"));
方式二
PromptPointOptions ppo = new PromptPointOptions("请输入点");
PromptPointResult retStPoint = ed.GetPoint(ppo);
- 效果展示

- 实例代码(获取手动交互的某点坐标,并标注)
类库
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.DatabaseServices;
代码
[CommandMethod("PointCreated")]
public void ComputePointCreate()
{
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.
DocumentManager.MdiActiveDocument.Editor;
PromptPointResult retStPoint = ed.GetPoint(("获得起点坐标"));
if (retStPoint.Status != PromptStatus.OK)
return;
DBPoint pt = new DBPoint(retStPoint.Value);
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
BlockTable bt = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord btr = trans.GetObject(bt[BlockTableRecord.ModelSpace],
OpenMode.ForWrite) as BlockTableRecord;
btr.AppendEntity(pt);
trans.AddNewlyCreatedDBObject(pt, true);
trans.Commit();
}
}
- 其他要素
其它信息获取方法

- 选择集(读取鼠标框选选择集实体)
- 实现方法
//获取用户框选的选择集对象
PromptSelectionResult pers = ed.GetSelection();
- 实例代码(获取框选区的,线和多段线的数量)
类库
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.DatabaseServices;
代码
public Entity getEntityByObjId(Database db, ObjectId id)
{
using (Transaction trans = db.TransactionManager.StartTransaction())
{
Entity dbo = trans.GetObject(id, OpenMode.ForRead) as Entity;
trans.Commit();
return dbo;
}
return null;
}
[CommandMethod("ModelCreated")]
public void ComputeModelCreated()
{
Database db = HostApplicationServices.WorkingDatabase;
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
//获取用户框选的选择集对象
PromptSelectionResult pers = ed.GetSelection();
if (pers.Status != PromptStatus.OK)//判断选择状态
return;
if (pers.Value == null)//判断选择集是否为空
return;
/*
PromptEntityResult downEntity = ed.GetEntity("请选择起始线");
if (downEntity.Status != PromptStatus.OK)
return;
*/
List<DBText> textArray = new List<DBText>();//注记
List<Line> lineArray = new List<Line>();//线段
List<Polyline> polylineArray = new List<Polyline>();//轻量)多段线
List<Polyline2d> poly2dArray = new List<Polyline2d>();//拟合曲线
List<Spline> splineArray = new List<Spline>();//曲线
List<Point3d> pointArray = new List<Point3d>();//存储线段的点位置
//获取输入对象的id或实体
ObjectId[] oids = pers.Value.GetObjectIds();
for (int ii = 0; ii < oids.Length; ii++)
{
ObjectId oid = oids[ii];
Entity ent = getEntityByObjId(db, oid);
if (ent == null)
continue;
if (ent is DBText)
{
textArray.Add(ent as DBText);
}
else if (ent is Line)
{
lineArray.Add(ent as Line);
}
else if (ent is Polyline)
{
polylineArray.Add(ent as Polyline);
}
else if (ent is Polyline2d)
{
poly2dArray.Add(ent as Polyline2d);
}
else if (ent is Spline)
{
splineArray.Add(ent as Spline);
}
}
MessageBox.Show("线段共有" + lineArray.Count + "多段线有" + polylineArray.Count);
}
- 效果展示

- 访问CAD内部命令
-
实现方式
1)SendStringToExecute(.Net)
2)SendCommand(COM方式)
3)acedCommand( P/Invoke非托管C++函数) -
实现代码
[CommandMethod("CadCmdExecute")]
public void CadCmdExecute()
{
//命令行动态执行语句
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.
DocumentManager.MdiActiveDocument;
//创建一个圆形圆形,并强制缩放视图至图形
doc.SendStringToExecute("circle\n2,2,0\n4\n", true, false, true);
doc.SendStringToExecute("zoom e\n", true, false, true);
}
- 总结
喜欢的朋友们可以点个关注,后续将持续更新,精彩无限^ - ^
更多推荐
所有评论(0)