您的位置:网站首页 > CAD新闻

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

时间:2011-04-21 10:47:18 来源:未知

打开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;#p#分页标题#e#

  //--定义一个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();