
說明Line(ZwSoft.ZwCAD.DatabaseServices.Line)純粹兩點直線實體不支持線寬漸變、不能添加中間頂點老管網(wǎng)部分標準會用到Polyline多段線管網(wǎng)主流支持線寬、拐點新增枚舉選項用來控制管線實體類型生成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 /// 在模型空間創(chuàng)建簡單Line直線實體 /// /summary /// param namedb數(shù)據(jù)庫/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; // 根據(jù)配置選擇繪制交互模式 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) { // 創(chuàng)建原生Line直線 pipeEntId PipeGeomHelper.CreatePipeLine(db, startPt, endPt, targetLayerName); } else { // 創(chuàng)建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業(yè)務屬性 // 如果是PickExistPointEntity模式可把兩個管點ObjectId寫入XData做拓撲 ed.WriteMessage($\n成功繪制管線【{codeDefine.Name}】); }關鍵點說明Line實體沒有全局線寬屬性線寬要靠CAD圖層線寬控制而Polyline支持每段獨立寬度兩種實體都只做兩點版本后續(xù)如果要做多折管線只能走PolylineLine本身不支持多個頂點全部交互邏輯復用只在最后創(chuàng)建實體時根據(jù)PipeEntityKind切換調(diào)用不同工具配置存到你的標準模板庫不同管線要素可以自由選Line/Polyline、拾取屏幕點 / 拾取已有管點。小提示模式BPickExistPointEntity當前只判斷是BlockReference后續(xù)可以增強PickPipePointEntity校驗塊名防止拾取別的無關塊。如果你需要我可以把PickPipePointEntity改成重載版本同時輸出拾取到的管點ObjectId方便后面寫拓撲XData。