1. 点的拾取
有时候我们需要在CAD画布上实时选取起始点和结束点,然后绘制出来一条直线。实现如下:
public void getPoint(){var doc = Application.DocumentManager.MdiActiveDocument;var editor = doc.Editor;var docDatabase = doc.Database;PromptPointOptions ppo = new PromptPointOptions("拾取线段起点:");//获取线段起点的输入PromptPointResult pointResult = editor.GetPoint(ppo);var start = pointResult.Value;//基点到光标的虚线标识ppo.BasePoint = start;ppo.UseBasePoint=true;ppo.UseDashedLine=true;//如果用户设置了起始节点,再让用户获取结束节点if (pointResult.Status == PromptStatus.OK){ppo.Message = "拾取线段终点:";pointResult = editor.GetPoint(ppo);var end = pointResult.Value;if (pointResult.Status == PromptStatus.OK){//方式一:绘制临时向量,并不存储到数据库中// editor.DrawVector(start,end,1,false);//方式二:提交到数据库docDatabase.AddEnityToModelSpace(new Line(start, end));}}}
AddEnityToModelSpace方法见我前面文章封装的工具类
效果如下:
2. 角度的拾取
public void getAngle(){var editor = Application.DocumentManager.MdiActiveDocument.Editor;PromptAngleOptions pao = new PromptAngleOptions("拾取角度:");pao.AllowNone = false;pao.UseDashedLine = true;pao.UseAngleBase=false;PromptDoubleResult startAngle = editor.GetAngle(pao);if (startAngle.Status == PromptStatus.OK){editor.WriteMessage("\n角度:{0}", startAngle.Value);}}
最终效果:
3. 单个实体对象的拾取
public void getOneEntity(){var doc = Application.DocumentManager.MdiActiveDocument;Editor editor = doc.Editor;PromptEntityResult per = editor.GetEntity("\n拾取任意实体:");if (per.Status == PromptStatus.OK){var perObjectId = per.ObjectId;using (Transaction ts = doc.Database.TransactionManager.StartTransaction()){var entity = (Entity)ts.GetObject(perObjectId, OpenMode.ForRead,true);editor.WriteMessage("\n拾取实体对象:{0},类型:{1}", entity.ToString(),entity.GetType().FullName);ts.Commit();}}}
4. 特定实体对象的拾取
利用过滤器进行选择。
public void getSpecificEntity(){var doc = Application.DocumentManager.MdiActiveDocument;Editor editor = doc.Editor;PromptEntityOptions peo = new PromptEntityOptions("\n拾取线条:");peo.SetRejectMessage("\n 选择的对象类型不是线条,请重新选择!");peo.AddAllowedClass(typeof(Line), true);PromptEntityResult per = editor.GetEntity(peo);if (per.Status == PromptStatus.OK){var perObjectId = per.ObjectId;using (Transaction ts = doc.Database.TransactionManager.StartTransaction()){var entity = (Entity)ts.GetObject(perObjectId, OpenMode.ForRead,true);editor.WriteMessage("\n拾取实体对象:{0},类型:{1}", entity.ToString(),entity.GetType().FullName);ts.Commit();}}}
5. 利用选择集拾取多个实体对象
/// <summary>/// 利用选择集选择多个实体对象/// </summary>[CommandMethod("selectEntities")]public void selectEntitiesTest(){List<Entity> entities = selectEntities();}public List<Entity> selectEntities(){var doc = Application.DocumentManager.MdiActiveDocument;Editor editor = doc.Editor;var docDatabase = doc.Database;SelectionSet selectionSet;//清空选择集var objectIds = new ObjectId[0];editor.SetImpliedSelection(objectIds);//请求从图形区域选择对象PromptSelectionResult psr = editor.GetSelection();if (psr.Status == PromptStatus.OK){selectionSet = psr.Value;var objectIdList = selectionSet.GetObjectIds().ToList();List<Entity> entities = new List<Entity>();using (Transaction tr = docDatabase.TransactionManager.StartTransaction()){foreach (var objectId in objectIdList){var entity = (Entity)objectId.GetObject(OpenMode.ForRead);entities.Add(entity);}tr.Commit();return entities;}}return null;}