博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#将CAD数据转成shape或mdb的代码
阅读量:6701 次
发布时间:2019-06-25

本文共 5425 字,大约阅读时间需要 18 分钟。

本文所指的CAD数据为不带空间参考和扩展数据的数据。如果CAD带了空间参考或是扩展属性数据的话,就要采用图形和属性分离的方法转CAD数据,即图形转完后需要挂接属性数据库。这里就不详细说明了.

ArcGIS和CAD对空间数据的归纳有所不同,在AutoCad中,点线面可以在同一个图层中,但是在ArcGIS中,点线面被严格的区分为了point、polyline和polygon三种类型的图层,所以直接用ArcCatalog查看CAD数据时会看到五个图层(点线面、注记和Multipatch)

下面的方法实现了点线面图层转成Shape文件

 Code [http://www.xueit.com]

 public bool CadDataToShape(string _cadFilePath)
 {        
            //工作空间         
            IWorkspaceFactory pWorkspaceFactory;
            IFeatureWorkspace pFeatureWorkspace;
            IFeatureLayer pFeatureLayer;
            IFeatureDataset pFeatureDataset;//图层对应数据集
            
            ESRI.ArcGIS.ConversionTools.FeatureClassToFeatureClass feaTofea = new FeatureClassToFeatureClass();
            ESRI.ArcGIS.Geoprocessor.Geoprocessor pGeoPro = new ESRI.ArcGIS.Geoprocessor.Geoprocessor();

            try

            {
                string strFullPath = _cadFilePath;
                int index = strFullPath.LastIndexOf("\\");
                string filePath = strFullPath.Substring(0, index);
                string fileName = strFullPath.Substring(index   1);
                string ";

                cadGISInfo = new stMdbInfo();

                cadGISInfo.mdbFeaturesName = new List<string>();
                cadGISInfo.mdbname = fileName;

                //打开cad数据集

                pWorkspaceFactory = new CadWorkspaceFactoryClass();
                pFeatureWorkspace = (IFeatureWorkspace)pWorkspaceFactory.OpenFromFile(filePath, 0);
                
                
                if (Directory.Exists(DataSaveFilePath   @"\\"   fileName))
                {                  
                    DirectoryInfo dirInfo = new DirectoryInfo(DataSaveFilePath   @"\\"   fileName);
                    foreach (FileInfo file in dirInfo.GetFiles())
                    {
                        file.Delete();
                    }
                   
                    Directory.Delete(DataSaveFilePath   @"\\"   fileName);
                }

                Directory.CreateDirectory(DataSaveFilePath   @"\\"   fileName);

                cadGISInfo.mdbfilepath = DataSaveFilePath   @"\\"   fileName;

                //打开一个要素集

                pFeatureDataset = pFeatureWorkspace.OpenFeatureDataset(fileName);

                IFeatureClassContainer pFeatureClassContainer = (IFeatureClassContainer)pFeatureDataset;

                //对CAD文件中的要素进行遍历处理
                int count = pFeatureClassContainer.ClassCount;

                for (int j = 0; j < pFeatureClassContainer.ClassCount; j  )

                {
                    IFeatureClass pFeatClass = pFeatureClassContainer.get_Class(j);
                    cadGISInfo.mdbFeaturesName.Add(pFeatClass.AliasName ".shp");
                    if (pFeatClass.FeatureType == esriFeatureType.esriFTCoverageAnnotation || pFeatClass.FeatureType == esriFeatureType.esriFTAnnotation)
                    {

                    }

                    else
                    {
                        feaTofea.in_features = strFullPath   @"\\"   pFeatClass.AliasName;
                        feaTofea.out_path = DataSaveFilePath   @"\\"   fileName;

                        feaTofea.out_feature_class = pFeatClass.AliasName;

                        feaTofea.out_name = pFeatClass.AliasName;
                        pGeoPro.Execute(feaTofea, null);
                    }
                }           
                return true;
            }
            catch
            {
                return false;
            }
            finally
            {
                pWorkspaceFactory = null;              
                pFeatureDataset = null;            
                pFeatureWorkspace = null;             
                GC.Collect();
            }

        }

下面的方法实现CAD数据转成本地的Personal Geodatbase,同时实现了注记图层的转换
public bool CadDataToGeodatabase(string _cadFilePath)
        {     
            //工作空间         
            IWorkspaceFactory pWorkspaceFactory;
            IFeatureWorkspace pFeatureWorkspace;
            IFeatureLayer pFeatureLayer;
            IFeatureDataset pFeatureDataset;//图层对应数据集

            //创建MDB工作空间

            IWorkspaceFactory pAccessWorkSpaceFactory;

            //这个conversionTools不包含注记图层的转换

            ESRI.ArcGIS.ConversionTools.FeatureClassToFeatureClass feaTofea = new FeatureClassToFeatureClass();
            ESRI.ArcGIS.Geoprocessor.Geoprocessor pGeoPro = new ESRI.ArcGIS.Geoprocessor.Geoprocessor();
            //ImportCADAnnotation功能,实现导入注记图层
            ESRI.ArcGIS.ConversionTools.ImportCADAnnotation importCADAnno = new ImportCADAnnotation();
            try
            {                
                    string strFullPath = _cadFilePath;
                    int index = strFullPath.LastIndexOf("\\");
                    string filePath = strFullPath.Substring(0, index);
                    string fileName = strFullPath.Substring(index   1);
                    string ";

                    cadGISInfo= new stMdbInfo();

                    cadGISInfo.mdbFeaturesName = new List<string>();
                    cadGISInfo.mdbname = fileName;
                    //打开cad数据集
                    pWorkspaceFactory = new CadWorkspaceFactoryClass();
                    pFeatureWorkspace = (IFeatureWorkspace)pWorkspaceFactory.OpenFromFile(filePath, 0);

                    //创建mdb(personal geodatabase)

                    pAccessWorkSpaceFactory = new AccessWorkspaceFactoryClass();
                    if (File.Exists(DataSaveFilePath   @"\\"   fileName   ".mdb"))
                    {
                        File.Delete(DataSaveFilePath   @"\\"   fileName   ".mdb");
                    }

                    cadGISInfo.mdbfilepath = DataSaveFilePath   @"\\"   fileName   ".mdb";

                    //ltMdbFilePath.Add(DataSaveFilePath   @"\\"   fileName   ".mdb");

                    pAccessWorkSpaceFactory.Create(DataSaveFilePath, fileName   ".mdb", null, 0);

                    //打开一个要素集
                    pFeatureDataset = pFeatureWorkspace.OpenFeatureDataset(fileName);

                    IFeatureClassContainer pFeatureClassContainer = (IFeatureClassContainer)pFeatureDataset;

                    //对CAD文件中的要素进行遍历处理
                    int count = pFeatureClassContainer.ClassCount;

                    for (int j = 0; j < pFeatureClassContainer.ClassCount; j  )

                    {

                        IFeatureClass pFeatClass = pFeatureClassContainer.get_Class(j);

                        cadGISInfo.mdbFeaturesName.Add(pFeatClass.AliasName);
                        if (pFeatClass.FeatureType == esriFeatureType.esriFTCoverageAnnotation || pFeatClass.FeatureType == esriFeatureType.esriFTAnnotation)
                        {
                            pFeatureLayer = new CadAnnotationLayerClass();//注记图层
                            importCADAnno.input_features = strFullPath   @"\\"   pFeatClass.AliasName;
                            importCADAnno.output_featureclass = DataSaveFilePath   @"\\"   fileName   ".mdb"   @"\\"   pFeatClass.AliasName;
                            importCADAnno.reference_scale = 1000;
                            pGeoPro.Execute(importCADAnno, null);
                        }
                        else
                        {
                            feaTofea.in_features = strFullPath   @"\\"   pFeatClass.AliasName;
                            feaTofea.out_path = DataSaveFilePath   @"\\"   fileName   ".mdb";

                            feaTofea.out_feature_class = pFeatClass.AliasName;

                            feaTofea.out_name = pFeatClass.AliasName;
                            pGeoPro.Execute(feaTofea, null);
                        }
                    }                 
                return true;
            }
            catch(Exception ex)
            {
                return false;
            }
            finally
            {                 
                GC.Collect();           
            }            
        }
从上面的代码可以看出,实际C#调用了Geoprocessor 执行了ConversionTools.FeatureClass to FeatureClass工具,这是不是有点像用arctoolbox中的功能!这里就可以扩展用Geoprocessor 实现其他arctoolbox中能够先实现的功能!

这里也可以将上面的方法里的参数改为多个cad数据路径,实现批量转数据!

转载于:https://www.cnblogs.com/linhugh/archive/2012/04/06/2435259.html

你可能感兴趣的文章
JadClipse eclipse反编译插件
查看>>
struts2中文件上传
查看>>
男性早孕-从软件与程序的区别说起
查看>>
Windows 恢复环境(Windows RE模式)
查看>>
2018-2019 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2018) - 4.28
查看>>
SAP OBYC自动记账的实例说明 +VALUE STRING
查看>>
node.js 实现扫码二维码登录
查看>>
liunx常用命令笔记
查看>>
基础总结
查看>>
oracle-25031安装错误分析解决
查看>>
统一沟通-技巧-5-Lync 2010 for iPhone iPad–配置-手册
查看>>
基于Annotation的输入校验
查看>>
Kinect for windows开发准备
查看>>
SQL Server 2012 官方培训课程体系
查看>>
为什么恍然大悟与知识管理的几个感触:人艰不拆
查看>>
Microsoft Azure Site Recovery (1) 安装服务器代理
查看>>
SCAC连接MicrosoftAzure
查看>>
Vmware view 5.0 POC环境搭建参考v1.0
查看>>
MySQL数据库备份之复制
查看>>
好消息:我最新制作的Java视频教程在51cto网站和Java爱好者们见面了!
查看>>