单纯使用ArcEngine提供的接口读取dwg数据转shp存在众多属性无法读取的情况(最直观的 南方cass生产的dwg文件有SOUTH这一字段,为目标要素的类型)
private void ConvertDwgToShp() { DwgReader pDwgReader = new DwgReader(@"D:\app\CAD\test.dwg"); DxfModel pDxf = pDwgReader.Read(); DxfEntityCollection xx = pDxf.Entities; //为写入shp文件做准备 WriteDestShpFile wSHP = new WriteDestShpFile(); //在遍历CAD要素时 筛选唯一图层,创建shp用 List<string> pLayerList = new List<string>(); //创建shp文件时,字段集合 List<string> pDwgFieldList = new List<string>(); //写入shp时,字段与值 的键值对 Dictionary<string,string> pDic = new Dictionary<string,string>(); IWorkspaceFactory pWFactory = new ShapefileWorkspaceFactoryClass(); IFeatureWorkspace pFWorkspace = pWFactory.OpenFromFile("C:/test",0) as IFeatureWorkspace; DelegateTest delegateTest = null; foreach (DxfEntity item in xx) { //当前要素的类型(点线面) string GeoType = GetEntityType.GetitemType(item); //当前要素将要存入的图层名称 string DesLayerName = string.Format("{0}_{1}", item.Layer.Name, GeoType); //扩展属性 DxfExtendedDataCollection pDxfDataCol = item.ExtendedDataCollection; pDic.Clear(); pDwgFieldList.Clear(); foreach (DxfExtendedData ExData in pDxfDataCol) { //AppID.Name 字段名 pDwgFieldList.Add(ExData.AppId.Name); string values = ""; for (int j = 0; j < ExData.Values.Count; j++) { values += ExData.Values[j]; } pDic.Add(ExData.AppId.Name, values); } if (!pLayerList.Contains(DesLayerName)) { pLayerList.Add(DesLayerName);
//此处为根据要素类类型,名称,字段及路径的一个创建shp文件的方法(需要方法联系,下有联系方式) CreateDestShpFile.CreateShpFile(GeoType, DesLayerName, pDwgFieldList, "C:/test"); } switch (item.GetType().Name) { case "DxfLwPolyline": delegateTest = new DelegateTest((new ConvertDxfLwPolyline()).ConvertToshp); break; case "DxfLwPoint": delegateTest = new DelegateTest((new ConvertDxfPoint()).ConvertToshp); break; case "DxfCircle": delegateTest = new DelegateTest((new ConvertDxfCircle()).ConvertToshp); break; case "DxfPolyline2D": delegateTest = new DelegateTest((new ConvertDxfPolyline2D()).ConvertToshp); break; default: Console.WriteLine(item.GetType().Name); break; } IGeometry pGeometry = delegateTest.Invoke(item); IFeatureClass pFeatureClass = pFWorkspace.OpenFeatureClass(DesLayerName); wSHP.WriteDestFeature(pFeatureClass, pGeometry,pDic); } }
个人理解:CAD中没有面要素与线要素的概念,全部都是线要素,只存在图形封闭与不封闭的区分,因此封闭的时候视为面,下为CAD要素为DxfLwPolyline时的要素转换代码,其它同理。
1 public IGeometry ConvertToshp(DxfEntity item) 2 { 3 //CAD 获取点集合 4 DxfLwPolyline pPointColl = item as DxfLwPolyline; 5 6 IGeometry pGeometry = null; 7 if (pPointColl.Closed) 8 { 9 IPointCollection pPoints = new PolygonClass(); 10 foreach (var point in pPointColl.Vertices) 11 { 12 IPoint pPoint = new PointClass() { X = point.X, Y = point.Y }; 13 pPoints.AddPoint(pPoint); 14 } 15 pPoints.AddPoint(pPoints.get_Point(0)); 16 pGeometry = pPoints as IPolygon; 17 } 18 else 19 { 20 IPointCollection pPoints = new PolylineClass(); 21 foreach (var point in pPointColl.Vertices) 22 { 23 IPoint pPoint = new PointClass() { X = point.X, Y = point.Y }; 24 pPoints.AddPoint(pPoint); 25 } 26 pGeometry = pPoints as IPolyline; 27 } 28 return pGeometry; 29 }
使用的是 ww.cad 类库 版本4.0.35.21(单纯复制粘贴不可使用,非关键代码篇幅原因没展示)
联系VX:cl141545646
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 一文读懂知识蒸馏
· 终于写完轮子一部分:tcp代理 了,记录一下