
說(shuō)明Line(ZwSoft.ZwCAD.DatabaseServices.Line)純粹兩點(diǎn)直線實(shí)體不支持線寬漸變、不能添加中間頂點(diǎn)老管網(wǎng)部分標(biāo)準(zhǔn)會(huì)用到Polyline多段線管網(wǎng)主流支持線寬、拐點(diǎn)新增枚舉選項(xiàng)用來(lái)控制管線實(shí)體類型生成Line還是Polyline在PipeGeomHelper增加CreatePipeLine工具方法同時(shí)改造Line分支通過(guò)配置切換生成Line / Polyline。第一步新增枚舉CommPipe命名空間namespace CommPipe { /// summary /// 管線繪制交互模式 /// /summary public enum PipeDrawMode { /// summary拾取屏幕兩點(diǎn)直接畫(huà)線/summary PickScreenPoint, /// summary拾取圖上兩個(gè)管點(diǎn)塊實(shí)體提取插入點(diǎn)畫(huà)線/summary PickExistPointEntity } /// summary /// 管線實(shí)體類型Line簡(jiǎn)單直線 / Polyline多段線 /// /summary public enum PipeEntityKind { Line, Polyline } }CodeDefine增加屬性public PipeEntityKind PipeEntityKind { get; set; }PipeGeomHelper 新增 CreatePipeLine 方法/// summary /// 在模型空間創(chuàng)建簡(jiǎn)單Line直線實(shí)體 /// /summary /// param namedb數(shù)據(jù)庫(kù)/param /// param namestartPt起點(diǎn)/param /// param nameendPt終點(diǎn)/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保留不動(dòng)。OnStartDrawElement 中 Line分支完整代碼同時(shí)支持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拾取屏幕兩點(diǎn) PromptPointResult pt1 ed.GetPoint(\n請(qǐng)拾取管線起點(diǎn)); if (pt1.Status ! PromptStatus.OK) return; PromptPointResult pt2 ed.GetPoint(\n請(qǐng)拾取管線終點(diǎn)); if (pt2.Status ! PromptStatus.OK) return; startPt pt1.Value; endPt pt2.Value; pickOk true; } else if (codeDefine.LineDrawMode CommPipe.PipeDrawMode.PickExistPointEntity) { //模式B拾取圖上已存在管點(diǎn)塊 if (!PipeGeomHelper.PickPipePointEntity(ed, \n請(qǐng)拾取起始管點(diǎn), out startPt)) { ed.WriteMessage(\n拾取起始管點(diǎn)失敗或用戶取消); return; } if (!PipeGeomHelper.PickPipePointEntity(ed, \n請(qǐng)拾取終止管點(diǎn), out endPt)) { ed.WriteMessage(\n拾取終止管點(diǎ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實(shí)體附加X(jué)Data業(yè)務(wù)屬性 // 如果是PickExistPointEntity模式可把兩個(gè)管點(diǎn)ObjectId寫(xiě)入XData做拓?fù)?ed.WriteMessage($\n成功繪制管線【{codeDefine.Name}】); }關(guān)鍵點(diǎn)說(shuō)明Line實(shí)體沒(méi)有全局線寬屬性線寬要靠CAD圖層線寬控制而Polyline支持每段獨(dú)立寬度兩種實(shí)體都只做兩點(diǎn)版本后續(xù)如果要做多折管線只能走PolylineLine本身不支持多個(gè)頂點(diǎn)全部交互邏輯復(fù)用只在最后創(chuàng)建實(shí)體時(shí)根據(jù)PipeEntityKind切換調(diào)用不同工具配置存到你的標(biāo)準(zhǔn)模板庫(kù)不同管線要素可以自由選Line/Polyline、拾取屏幕點(diǎn) / 拾取已有管點(diǎn)。小提示模式BPickExistPointEntity當(dāng)前只判斷是BlockReference后續(xù)可以增強(qiáng)PickPipePointEntity校驗(yàn)塊名防止拾取別的無(wú)關(guān)塊。如果你需要我可以把PickPipePointEntity改成重載版本同時(shí)輸出拾取到的管點(diǎn)ObjectId方便后面寫(xiě)拓?fù)鋁Data。