欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 家装 > 多段线和二维多段线的区别及顶点遍历

多段线和二维多段线的区别及顶点遍历

2025/5/5 21:25:49 来源:https://blog.csdn.net/wuchen1004/article/details/147704913  浏览:    关键词:多段线和二维多段线的区别及顶点遍历

近日项目处理中,在遍历多义线的时候,发现客户的图纸有【二维多段线】这个类型,需要做兼容处理,因此总结记录一下。

在 AutoCAD 里,LWPOLYLINE(轻量级多段线,命令是PL,下图左)和POLYLINE(多段线,本次主要讲述二维多段线,下图右)都是用来表示由多个线段或圆弧连接而成的图形对象,通过属性可以简单查看两者的内容:

1. 数据结构与存储

  • LWPOLYLINE
    • 它属于轻量级对象,存储数据时更为紧凑。相较于POLYLINELWPOLYLINE占用的内存和磁盘空间更少,这使得在处理大量多段线对象时,能有效提升性能。
    • 其顶点信息以连续的坐标对形式存储,数据结构较为简单,这也进一步减少了存储空间。
  • POLYLINE
    • 这是一种传统的多段线对象,数据结构相对复杂。它不仅包含顶点信息,还包含更多的属性和扩展数据,因此占用的存储空间更多。

2. 图形特性

  • LWPOLYLINE
    • 只能是二维对象,仅存在于当前 UCS(用户坐标系)的 XY 平面上。
    • 各顶点间的连接方式只能是直线段或圆弧,不能包含其他复杂的曲线。
  • POLYLINE
    • 既可以是二维对象,也可以是三维对象,能在三维空间中表示多段线。
    • 除了直线段和圆弧,还可以包含其他类型的曲线,如样条曲线等,具有更强的图形表达能力。

通过插件,查看两者的组码,内容如下:

从上面的两个图可见,两者组码"0",即它们的类型是不一样的。组码"0"也是从图纸筛选过滤的类型条件。

笔者通过策略模式,封装了两种模式的顶点遍历方案,核心代码如下:

// 顶点处理策略接口public interface IVertexProcessingStrategy{void ProcessVertices(Entity entity, List<CPointInfo> list);}// 顶点处理策略基类public abstract class VertexProcessingStrategyBase : IVertexProcessingStrategy{public abstract void ProcessVertices(Entity entity, List<CPointInfo> list);public CPointInfo ProcessVertex(Point3d vertex3d){System.Globalization.CultureInfo cultures = new System.Globalization.CultureInfo("en-US");Point3d ptLocation = CommonDeal.TransToLayout_3d(m_vp, vertex3d);return (new CPointInfo(){Num = (listRailCar_RedPointInfo.Count + 1).ToString(),XPos = vertex3d.X.ToString("f1", cultures),YPos = vertex3d.Y.ToString("f1", cultures),location = MathKit.Trans2XYZPoint(vertex3d)});}}// LWPOLYLINE 顶点处理策略public class LwPolylineVertexProcessingStrategy : VertexProcessingStrategyBase{public override void ProcessVertices(Entity entity, List<CPointInfo> list){var lwPoly = entity as Autodesk.AutoCAD.DatabaseServices.Polyline;if (lwPoly != null){int iNumberOfVertices = lwPoly.NumberOfVertices;for (int j = 0; j < iNumberOfVertices; j++){Point2d pt2d = lwPoly.GetPoint2dAt(j);CPointInfo processedPoint = ProcessVertex(new Point3d(pt2d.X, pt2d.Y, 0.0));list.Add(processedPoint);}}}}// POLYLINE 顶点处理策略public class PolylineVertexProcessingStrategy : VertexProcessingStrategyBase{public override void ProcessVertices(Entity entity, List<CPointInfo> list){var poly = entity as Autodesk.AutoCAD.DatabaseServices.Polyline2d;if (poly != null){Point3dCollection aaa = new Point3dCollection();poly.GetStretchPoints(aaa);int nn = aaa.Count;for (int i = 0; i < nn; i++){CPointInfo processedPoint = ProcessVertex(aaa[i]);list.Add(processedPoint);}}}}// 顶点处理上下文public class VertexProcessingContext{private IVertexProcessingStrategy _strategy;public VertexProcessingContext(IVertexProcessingStrategy strategy){_strategy = strategy;}public void SetStrategy(IVertexProcessingStrategy strategy){_strategy = strategy;}public void Process(Entity entity, List<CPointInfo> list){_strategy.ProcessVertices(entity, list);}}public void DealTwoTypePolyline(string sPLineLayer, string sLayoutName = ""){Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;// 定义筛选条件TypedValue[] values = new TypedValue[]{// 图元类型为LWPOLYLINE或POLYLINEnew TypedValue((int)DxfCode.Start, "LWPOLYLINE,POLYLINE"),// 图层为sPLineLayernew TypedValue((int)DxfCode.LayerName, sPLineLayer),// 布局名称为sLayoutNamenew TypedValue((int)DxfCode.LayoutName, sLayoutName)};SelectionFilter filter = new SelectionFilter(values);PromptSelectionResult psr;{psr = ed.SelectAll(filter);}if (psr.Status != PromptStatus.OK){return ;}Document document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;Database db = document.Database;List<CPointInfo> listCPointInfo = new List<CPointInfo>();using (Transaction tr = db.TransactionManager.StartTransaction()){BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead);var lwPolyStrategy = new LwPolylineVertexProcessingStrategy();var polyStrategy = new PolylineVertexProcessingStrategy();var context = new VertexProcessingContext(lwPolyStrategy);ObjectId[] ids = psr.Value.GetObjectIds();for (int i = 0; i < ids.Length; i++){Entity ent = (Entity)tr.GetObject(ids[i], OpenMode.ForRead);if (ent is Autodesk.AutoCAD.DatabaseServices.Polyline){context.SetStrategy(lwPolyStrategy);}else if (ent is Autodesk.AutoCAD.DatabaseServices.Polyline2d){context.SetStrategy(polyStrategy);}else{continue;}context.Process(ent, listCPointInfo);}tr.Commit();}}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词