zoukankan      html  css  js  c++  java
  • 使用 ww.cad 完成dwg文件转shp(包含所有属性)

    单纯使用ArcEngine提供的接口读取dwg数据转shp存在众多属性无法读取的情况(最直观的 南方cass生产的dwg文件有SOUTH这一字段,为目标要素的类型)

    private void ConvertDwgToShp()
            {
                
                DwgReader pDwgReader = new DwgReader(@"D:appCAD	est.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

  • 相关阅读:
    数论基础(维诺格拉多夫著,裘光明译) 勘误
    微观经济学现代观点(Hal R. Varian) 复习题 1.1
    微分学里的中值定理
    数论基础(维诺格拉多夫著,裘光明译) 勘误
    分数的一种分拆方法
    C++正则表达式的初步使用
    如何用消息系统避免分布式事务
    阿里云表格存储全面升级,打造一站式物联网存储新方案
    探究 Java 应用的启动速度优化
    技术干货|基于Apache Hudi 的CDC数据入湖「内附干货PPT下载渠道」
  • 原文地址:https://www.cnblogs.com/clgis/p/12539930.html
Copyright © 2011-2022 走看看