人间惊鸿宴

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
  28 随笔 :: 0 文章 :: 17 评论 :: 14487 阅读

单纯使用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

posted on   人间惊鸿宴  阅读(998)  评论(1编辑  收藏  举报
编辑推荐:
· 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代理 了,记录一下
点击右上角即可分享
微信分享提示