ARTICLE DETAIL

资讯详情

深耕商务建站与企业官网运营的一线实战洞察。

绘制直线和多段线

绘制直线和多段线 说明Line(ZwSoft.ZwCAD.DatabaseServices.Line)纯粹两点直线实体不支持线宽渐变、不能添加中间顶点老管网部分标准会用到Polyline多段线管网主流支持线宽、拐点新增枚举选项用来控制管线实体类型生成Line还是Polyline在PipeGeomHelper增加CreatePipeLine工具方法同时改造Line分支通过配置切换生成Line / Polyline。第一步新增枚举CommPipe命名空间namespace CommPipe { /// summary /// 管线绘制交互模式 /// /summary public enum PipeDrawMode { /// summary拾取屏幕两点直接画线/summary PickScreenPoint, /// summary拾取图上两个管点块实体提取插入点画线/summary PickExistPointEntity } /// summary /// 管线实体类型Line简单直线 / Polyline多段线 /// /summary public enum PipeEntityKind { Line, Polyline } }CodeDefine增加属性public PipeEntityKind PipeEntityKind { get; set; }PipeGeomHelper 新增 CreatePipeLine 方法/// summary /// 在模型空间创建简单Line直线实体 /// /summary /// param namedb数据库/param /// param namestartPt起点/param /// param nameendPt终点/param /// param namelayerName图层名/param /// returnsLine的ObjectId失败返回Null/returns public static ObjectId CreatePipeLine(Database db, Point3d startPt, Point3d endPt, string layerName) { if (db null) return ObjectId.Null; try { using (var tr db.TransactionManager.StartTransaction()) { ObjectId msId SymbolUtilityServices.GetBlockModelSpaceId(db); BlockTableRecord ms tr.GetObject(msId, OpenMode.ForWrite) as BlockTableRecord; if (ms null) { tr.Abort(); return ObjectId.Null; } Line lineEnt new Line(startPt, endPt); lineEnt.Layer layerName ?? ; ms.AppendEntity(lineEnt); tr.AddNewlyCreatedDBObject(lineEnt, true); tr.Commit(); return lineEnt.ObjectId; } } catch (Exception) { return ObjectId.Null; } }原有CreatePipePolyline保留不动。OnStartDrawElement 中 Line分支完整代码同时支持Line/Polylineelse if (curPipeType CommPipe.PipeType.Line) { var layerDef codeDefine.LayerInfo; string targetLayerName layerDef.LayerKey; ZwSoft.ZwCAD.Colors.Color layerColor LayerHelper.ParseRawColor(layerDef.ColorRaw); Point3d startPt Point3d.Origin; Point3d endPt Point3d.Origin; bool pickOk false; // 根据配置选择绘制交互模式 if (codeDefine.LineDrawMode CommPipe.PipeDrawMode.PickScreenPoint) { //模式A拾取屏幕两点 PromptPointResult pt1 ed.GetPoint(\n请拾取管线起点); if (pt1.Status ! PromptStatus.OK) return; PromptPointResult pt2 ed.GetPoint(\n请拾取管线终点); if (pt2.Status ! PromptStatus.OK) return; startPt pt1.Value; endPt pt2.Value; pickOk true; } else if (codeDefine.LineDrawMode CommPipe.PipeDrawMode.PickExistPointEntity) { //模式B拾取图上已存在管点块 if (!PipeGeomHelper.PickPipePointEntity(ed, \n请拾取起始管点, out startPt)) { ed.WriteMessage(\n拾取起始管点失败或用户取消); return; } if (!PipeGeomHelper.PickPipePointEntity(ed, \n请拾取终止管点, out endPt)) { ed.WriteMessage(\n拾取终止管点失败或用户取消); return; } pickOk true; } if (!pickOk) { return; } //拾取完成之后再确保图层存在 using (var trCheckLayer db.TransactionManager.StartTransaction()) { if (!LayerHelper.IsLayerExists(trCheckLayer, db, targetLayerName)) { LayerHelper.EnsureLayerExist(trCheckLayer, db, targetLayerName, layerColor); } trCheckLayer.Commit(); } ObjectId pipeEntId ObjectId.Null; if (codeDefine.PipeEntityKind CommPipe.PipeEntityKind.Line) { // 创建原生Line直线 pipeEntId PipeGeomHelper.CreatePipeLine(db, startPt, endPt, targetLayerName); } else { // 创建Polyline多段线 Point3dCollection pts new Point3dCollection(); pts.Add(startPt); pts.Add(endPt); pipeEntId PipeGeomHelper.CreatePipePolyline(db, pts, targetLayerName); } if (pipeEntId.IsNull) { ed.WriteMessage($\n绘制管线【{codeDefine.Name}】失败); return; } // Todo给pipeEntId实体附加XData业务属性 // 如果是PickExistPointEntity模式可把两个管点ObjectId写入XData做拓扑 ed.WriteMessage($\n成功绘制管线【{codeDefine.Name}】); }关键点说明Line实体没有全局线宽属性线宽要靠CAD图层线宽控制而Polyline支持每段独立宽度两种实体都只做两点版本后续如果要做多折管线只能走PolylineLine本身不支持多个顶点全部交互逻辑复用只在最后创建实体时根据PipeEntityKind切换调用不同工具配置存到你的标准模板库不同管线要素可以自由选Line/Polyline、拾取屏幕点 / 拾取已有管点。小提示模式BPickExistPointEntity当前只判断是BlockReference后续可以增强PickPipePointEntity校验块名防止拾取别的无关块。如果你需要我可以把PickPipePointEntity改成重载版本同时输出拾取到的管点ObjectId方便后面写拓扑XData。
返回列表
PREV
查看更多资讯
NEXT
返回资讯列表