注册 登录  
 加关注
查看详情
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

程序员驿站

淘宝店:http://shop106888457.taobao.com

 
 
 

日志

 
 

C#+ArcEngine加载CAD数据的几种方法  

2013-07-25 19:31:21|  分类: ArcEngine |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

打开cad文件的几种方法

  方法一、通过遍历CAD数据集,依次加载中点线面注记等图层,所加载的数据均为一个独立的图层。

  IWorkspaceFactory pWorkspaceFactory;
  IFeatureWorkspace pFeatureWorkspace;
  IFeatureLayer pFeatureLayer;
  IFeatureDataset pFeatureDataset;
  //打开CAD数据集
  pWorkspaceFactory = new CadWorkspaceFactoryClass();
  pFeatureWorkspace = (IFeatureWorkspace)pWorkspaceFactory.OpenFromFile(directoryPath, 0);
  //打开一个要素集
  pFeatureDataset = pFeatureWorkspace.OpenFeatureDataset(fileName);
  //IFeaturClassContainer可以管理IFeatureDataset中的每个要素类
  IFeatureClassContainer pFeatureClassContainer = (IFeatureClassContainer)pFeatureDataset;
  //对CAD文件中的要素进行遍历处理
  for (int i = 0; i < pFeatureClassContainer.ClassCount - 1; i++)
  {
  IFeatureClass pFeatureClass = pFeatureClassContainer.get_Class(i);
  if (pFeatureClass.FeatureType == esriFeatureType.esriFTAnnotation)
  {
  //如果是注记,则添加注记层
  pFeatureLayer = new CadAnnotationLayerClass();
  }
  else//如果是点、线、面,则添加要素层
  {
  pFeatureLayer = new FeatureLayerClass();
  pFeatureLayer.Name = pFeatureClass.AliasName;
  pFeatureLayer.FeatureClass = pFeatureClass;
  this.axmc_Main.Map.AddLayer(pFeatureLayer);
  this.axmc_Main.ActiveView.**();
  }

  }

  方法二、直接打开CAD数据的点线面注记等要素类,所加载的数据均为一个独立的图层。

 具体代码如下:

   CadWorkspaceFactoryClass fac=new CadWorkspaceFactoryClass ();
   String filePath="E:\\cad";
   IFeatureWorkspace space=fac.OpenFromFile(filePath,0) as IFeatureWorkspace ;
   下面是打开FeatureClass的代码:
   //线
   IFeatureClass polyline =space.OpenFeatureClass("NewCAD.dwg:Polyline");  
   IFeatureLayer layer=new CadFeatureLayerClass ();
   layer.FeatureClass =polyline;
   //点
   IFeatureClass point=space.OpenFeatureClass ("NewCAD.dwg:Point");
   layer=new CadFeatureLayerClass ();
   layer.FeatureClass =point;
   //面  
   IFeatureClass polygon=space.OpenFeatureClass ("NewCAD.dwg:Polygon");
   ayer=new CadFeatureLayerClass();
   layer.FeatureClass =polygon;
   //注记  
   IFeatureClass anno=space.OpenFeatureClass ("NewCAD.dwg:Annotation");
   layer=new CadAnnotationLayerClass();
   layer.FeatureClass =anno;
//其实在Coverage文件的开发方式与Autocad方法类似,可以参考.


  方法三、利用绘图空间直接加载CAD数据集,加载后各类型数据作为一个图层显示。

  

  IWorkspaceFactory pWorkspaceFactory = new CadWorkspaceFactoryClass();

  IWorkspace pWorkspace = pWorkspaceFactory.OpenFromFile(directoryPath, 0);

  //--定义一个CAD画图空间,并把上边打开的工作空间赋给它

  ICadDrawingWorkspace pCadDrawingWorkspace = pWorkspace as ICadDrawingWorkspace;

  //--定义一个CAD的画图数据集,并且打开上边指定的工作空间中一幅CAD图

  //--然后赋值给CAD数据集

  ICadDrawingDataset pCadDrawingDataset = pCadDrawingWorkspace.OpenCadDrawingDataset(fileName);

  //--通过ICadLayer类,把上边得到的CAD数据局赋值给ICadLayer类对象的

  //--CadDrawingDataset属性

  ICadLayer pCadLayer = new CadLayerClass();

  pCadLayer.CadDrawingDataset = pCadDrawingDataset;

  //--利用MapControl加载CAD层

  this.axmc_Main.Map.AddLayer(pCadLayer);
  this.axmc_Main.ActiveView.**();

  方法四、也是遍历CAD点线面,但是不用检查数据类型是否为注记。

  IWorkspaceFactory pWorkspaceFactory = new CadWorkspaceFactoryClass();
  IFeatureWorkspace pFeatureWorkspace = pWorkspaceFactory.OpenFromFile(directoryPath, 0) as IFeatureWorkspace;
  IFeatureDataset pFeatureDataset = pFeatureWorkspace.OpenFeatureDataset(fileName);
  IFeatureClassContainer pFeatureClassContainer = (IFeatureClassContainer)pFeatureDataset;
  for (int i = 0; i < pFeatureClassContainer.ClassCount - 1; i++)
  {
  IFeatureClass pFeatureClass = pFeatureClassContainer.get_Class(i);
  IFeatureLayer pFeatureLayer = new FeatureLayerClass();
  pFeatureLayer.FeatureClass = pFeatureClass;
  //ICadLayer pCadLayer = new CadLayerClass();
  //pCadLayer = pFeatureLayer as ICadLayer;
  this.axmc_Main.Map.AddLayer(pFeatureLayer);
  this.axmc_Main.ActiveView.**();

  }

  方法五、类似与方法三。

  //Add passed file to MapControl
  ICadDrawingDataset pCadDrawingDataset = GetCadDrawingDataset(directoryPath, fileName);
  if (pCadDrawingDataset == null) return;
  ICadLayer pCadLayer = new CadLayerClass();
  pCadLayer.CadDrawingDataset = pCadDrawingDataset;
  pCadLayer.Name = fileName;
  this.axmc_Main.AddLayer(pCadLayer, 0);

  /// <summary>
  /// 获取记录集
  /// </summary>
  /// <param name="directoryPath"></param>
  /// <param name="fileName"></param>
  /// <returns></returns>
  private ICadDrawingDataset GetCadDrawingDataset(string directoryPath, string fileName)
  {
  //Create a WorkspaceName object
  IWorkspaceName pWorkspaceName = new WorkspaceNameClass();
  pWorkspaceName.WorkspaceFactoryProgID = "esriDataSourcesFile.CadWorkspaceFactory";
  pWorkspaceName.PathName = directoryPath;
  //Create a CadDrawingName object
  IDatasetName pDatasetName = new CadDrawingNameClass();
  pDatasetName.Name = fileName;
  pDatasetName.WorkspaceName = pWorkspaceName;
  //Open the CAD drawing
  IName pName = (IName)pDatasetName;
  return (ICadDrawingDataset)pName.Open();
  }

  评论这张
 
阅读(715)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018