☰
长度检查代码
2026/10/11 1:45:24 网站建设 项目流程

改造说明

  1. b3d控制长度计算模式:
    • b3d=true:使用三维空间距离(X,Y,Z全部参与计算)
    • b3d=false:只算二维平面距离(忽略Z坐标,投影到XY平面)
  2. 增加Polyline(轻多义线,AcDbPolyline)支持,轻多义线是CAD常用多段线,注意区分:
    • Polyline= 轻多义线(ObjectClass.DxfName == “LWPOLYLINE”)✅ 本次支持
    • Polyline2d= 老式2D多段线(DxfName:“POLYLINE”,老图才会有,可选加)
  3. 原有Line逻辑保留,统一提取长度方法,减少重复代码。

重点:轻多义线的每一段都要累加长度;并且同样支持2D/3D模式。轻多义线本身顶点自带Z值,2D模式下计算时把Z置0。

完整优化后代码

public List<QCErrorResult> Run(Document doc, Dictionary<string, object> userParamDict) { var resultList = new List<QCErrorResult>(); // 读取配置参数 var selectLayers = userParamDict["Layers"] as IEnumerable<object>; var selectEntTypes = userParamDict["EntityTypes"] as IEnumerable<object>; string ErrorLevel = Convert.ToString(userParamDict["ErrorLevel"]); bool b3d = Convert.ToBoolean(userParamDict["Is3D"]); double minLength = Convert.ToDouble(userParamDict["MinLength"]); var layerNames = selectLayers.ToStringList(); var entTypeNames = selectEntTypes.ToStringList(); if (!layerNames.Any()) { doc.Editor.WriteMessage("\n未选择任何图层,管线微短检查直接跳过"); return resultList; } if (!entTypeNames.Any()) { doc.Editor.WriteMessage("\n未选择任何类型,管线微短检查直接跳过"); return resultList; } QCErrorLevel errLevel = QCErrorLevelHelper.Parse(ErrorLevel); using (var tr = doc.TransactionManager.StartTransaction()) { BlockTable bt = tr.GetObject(doc.Database.BlockTableId, OpenMode.ForRead) as BlockTable; BlockTableRecord modelBtr = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead) as BlockTableRecord; foreach (ObjectId entId in modelBtr) { Entity ent = tr.GetObject(entId, OpenMode.ForRead) as Entity; if (ent == null) continue; string dxfName = entId.ObjectClass.DxfName; if (!entTypeNames.Contains(dxfName)) continue; string entLayer = ent.Layer; if (!layerNames.Contains(entLayer)) continue; double totalLen = 0.0; #region Line 直线 if (ent is Line line) { Point3d p1 = line.StartPoint; Point3d p2 = line.EndPoint; if (!b3d) { //2D模式:XY平面距离,Z清零 p1 = new Point3d(p1.X, p1.Y, 0); p2 = new Point3d(p2.X, p2.Y, 0); } totalLen = p1.DistanceTo(p2); } #endregion #region LWPOLYLINE 轻多义线 else if (ent is Polyline pline) { for (int i = 0; i < pline.NumberOfVertices; i++) { Point3d p1 = pline.GetPoint3dAt(i); Point3d p2 = pline.GetPoint3dAt(i + 1); if (!b3d) { p1 = new Point3d(p1.X, p1.Y, 0); p2 = new Point3d(p2.X, p2.Y, 0); } totalLen += p1.DistanceTo(p2); } } #endregion // 其他实体跳过 else { continue; } if (totalLen < minLength) { resultList.Add(new QCErrorResult { CheckName = "管线微短检查", Handle = ent.Handle.ToString(),

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询