13590--北极燕鸥


   博采众长,信誉卓著

导航

ARC GIS 打开cad文件的几种方法

Posted on 2010-07-13 10:56  北极燕鸥  阅读(6961)  评论(1编辑  收藏  举报
收集资料如下:

  打开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.**();

  }

  }

  方法二、

  //--定义工作空间,并用CadWorkspaceFactoryClass()实例化它

  IWorkspaceFactory pWorkspaceFactory = new CadWorkspaceFactoryClass();

  //--打开相应的工作空间,并赋值给要素空间,OpenFromFile()

  //--中的参数为CAD文件夹的路径

  IFeatureWorkspace pFeatureWorkspace = pWorkspaceFactory.OpenFromFile(directoryPath, 0) as IFeatureWorkspace;

  /*--打开线要素类,如果要打开点类型的要素,需要把下边的代码该成:

  *-- IFeatureClass pFeatureClass = pFeatureWorkspace.OpenFeatureClass (fileName + ":point");

  *-- 由此可见fileName为CAD图的名字,后边加上要打开的要素类的类型,中间用冒号  

  *-- 隔开,大家可以想想多边形和标注是怎么打开的。 */

  //point polyline

  IFeatureClass pFeatureClass = pFeatureWorkspace.OpenFeatureClass(fileName + ":point");

  IFeatureLayer pFeatureLayer = new FeatureLayerClass();

  pFeatureLayer.FeatureClass = pFeatureClass;

  this.axmc_Main.Map.AddLayer(pFeatureLayer);

  this.axmc_Main.ActiveView.**();

  方法三、

  /*--下边的两行代码是定义一个CAD工作空间,然后打开它,但这次不是赋值给

  --IFeatureWorkspace对象,而是赋值给IWorkspace定义的对象*/

  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.**();

  方法四、

  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();

  }

  需要注意

  如果你的ArcGIS版本是V9.2的话,可能是因为你的文件名中含有中文字符的原因.据我所知,在ArcGIS9.2中无法显示文件名含有中文字符的CAD文件.你可以在ArcMap中直接打开试试,如果也无法显示的话,可能是文件名的问题(注:路径中也不能含有中文字符).

  GIS for Web应用开发之道

  插件式GIS应用框架的设计与实现

  public void esriDataSourcesFile_ICadDrawingDataset_Example(string nameOfFile, string nameOfCadDrawing)

  {

  //Set the Workspace Factory

  IWorkspaceFactory workspaceFactory = new ESRI.ArcGIS.DataSourcesFile.CadWorkspaceFactoryClass();

  //Open the Workspace

  IWorkspace workspace = workspaceFactory.OpenFromFile(nameOfFile, 0);

  //set the CadDrawingWorkspace

  ESRI.ArcGIS.DataSourcesFile.ICadDrawingWorkspace cadDrawingWorkspace = (ESRI.ArcGIS.DataSourcesFile.ICadDrawingWorkspace)workspace;

  //open the CadDrawingDataset

  ESRI.ArcGIS.DataSourcesFile.ICadDrawingDataset cadDrawingDataset = cadDrawingWorkspace.OpenCadDrawngDataset(nameOfCadDrawing);

  //create the CadLayer

  ESRI.ArcGIS.Carto.ICadLayer cadLayer = new ESRI.ArcGIS.Carto.CadLayerClass();

  //set the CadLayer to the CadDrawingDataset

  cadLayer.CadDrawingDataset = cadDrawingDataset;

  //list if the CadDrawingDataset exists

  Console.WriteLine("Does drawing exist: {0}" , cadLayer.CadDrawingDataset.Exists );

  //list the CadDrawingDataset FilePath

  Console.WriteLine("CAD Drawing Path: {0}" , cadLayer.CadDrawingDataset.FilePath );

  //list if the CadDrawingDataset is 2d

  Console.WriteLine("Is Drawing 2d?: {0}" , cadLayer.CadDrawingDataset.Is2d);

  //list if the CadDrawingDataset is 3d

  Console.WriteLine("Is Drawing 3d?: {0}" , cadLayer.CadDrawingDataset.Is3d );

  //list if the CadDrawingDataset is an AutoCAD drawing file

  Console.WriteLine("Is Drawing from AutoCAD?: {0}" , cadLayer.CadDrawingDataset.IsAutoCad );

  //list if the CadDrawingDataset is an MicroSation drawing file

  Console.WriteLine("Is Drawing from MicroStation?: {0}" , cadLayer.CadDrawingDataset.IsDgn); }

  中间过程实验代码:

  实验1

  public static string AddCADLayer(Map map1, Toc toc)

  {

  // 得到服务器上下文对象

  MapFunctionality agsMapFunctionlity = map1.GetFunctionality(1) as MapFunctionality;

  MapResourceLocal localMapResource = (MapResourceLocal)agsMapFunctionlity.Resource;

  ESRI.ArcGIS.Server.IServerContext mapContext = localMapResource.ServerContextInfo.ServerContext;

  //// 增加图层

  //ESRI.ArcGIS.Carto.FeatureLayer pFeatureLayer = null;

  //IFeatureLayer pFeatureLayer;

  //打开CAD数据集

  IWorkspaceFactory factory = (IWorkspaceFactory)mapContext.CreateObject("esriDataSourcesFile.CADWorkspaceFactory");

  //pWorkspaceFactory = new CadWorkspaceFactoryClass();

  //pWorkspaceFactory = new pWorkspaceFactory();

  ESRI.ArcGIS.DataSourcesFile.ICadDrawingWorkspace pFeatureWorkspace = (ESRI.ArcGIS.DataSourcesFile.ICadDrawingWorkspace)factory.OpenFromFile(@"C:\Inetpub\wwwroot\NorthAmerica", 0);

  //IFeatureWorkspace pFeatureWorkspace = (IFeatureWorkspace)factory.OpenFromFile(@"C:\Inetpub\wwwroot\NorthAmerica", 0);

  //打开一个要素集

  ESRI.ArcGIS.DataSourcesFile.ICadDrawingDataset pFeatureDataset = (ESRI.ArcGIS.DataSourcesFile.ICadDrawingDataset)pFeatureWorkspace.OpenCadDrawingDataset("New_Shapefile_ExportCAD.DWG");

  //IFeaturClassContainer可以管理IFeatureDataset中的每个要素类

  ESRI.ArcGIS.Carto.ICadLayer pFeatureClassContainer = new ESRI.ArcGIS.Carto.CadLayerClass();

  pFeatureClassContainer.CadDrawingDataset = pFeatureDataset;

  //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 = (ESRI.ArcGIS.Carto.FeatureLayer)mapContext.CreateObject("esriCarto.CadAnnotationLayerClass");

  // }

  // else//如果是点、线、面,则添加要素层

  // {

  // pFeatureLayer = (ESRI.ArcGIS.Carto.FeatureLayer)mapContext.CreateObject("esriCarto.CadFeatureLayer");

  // pFeatureLayer.Name = pFeatureClass.AliasName;

  // }

  //}

  ESRI.ArcGIS.Carto.IMapServerObjects mapServerObjects = localMapResource.MapServer as ESRI.ArcGIS.Carto.IMapServerObjects;

  ESRI.ArcGIS.Carto.IMap map = mapServerObjects.get_Map(localMapResource.DataFrame) as ESRI.ArcGIS.Carto.IMap;

  //if (pFeatureLayer != null)

  //{

  map.AddLayer(pFeatureClassContainer);

  // //map.MoveLayer(pFeatureLayer, map.LayerCount - 1);

  //}

  //刷新服务器对象,响应添加图层

  localMapResource.RefreshServerObjects();

  //Toc控件刷新

  toc.Refresh();

  map1.CallbackResults.CopyFrom(toc.CallbackResults);

  map1.Refresh();

  return map1.CallbackResults.ToString();

  }

  实验2

  public static string AddCADLayer2(Map map1, Toc toc)

  {

  // 得到服务器上下文对象

  MapFunctionality agsMapFunctionlity = map1.GetFunctionality(1) as MapFunctionality;

  MapResourceLocal localMapResource = (MapResourceLocal)agsMapFunctionlity.Resource;

  ESRI.ArcGIS.Server.IServerContext mapContext = localMapResource.ServerContextInfo.ServerContext;

  // 增加图层

  ESRI.ArcGIS.Carto.IFeatureLayer pFeatureLayer = null;

  //IFeatureLayer pFeatureLayer;

  //打开CAD数据集

  IWorkspaceFactory factory = (IWorkspaceFactory)mapContext.CreateObject("esriDataSourcesFile.CADWorkspaceFactory");

  //pWorkspaceFactory = new CadWorkspaceFactoryClass();

  //pWorkspaceFactory = new pWorkspaceFactory();

  IFeatureWorkspace pFeatureWorkspace = (IFeatureWorkspace)factory.OpenFromFile(@"C:\Inetpub\wwwroot\NorthAmerica", 0);

  //打开一个要素集

  IFeatureDataset pFeatureDataset = (IFeatureDataset)pFeatureWorkspace.OpenFeatureDataset("New_Shapefile_ExportCAD.DWG");

  //IFeaturClassContainer可以管理IFeatureDataset中的每个要素类

  IFeatureClassContainer pFeatureClassContainer = (IFeatureClassContainer)pFeatureDataset;

  //对CAD文件中的要素进行遍历处理

  for (int i = 0; i < pFeatureClassContainer.ClassCount - 3; i++)

  {

  IFeatureClass pFeatureClass = pFeatureClassContainer.get_Class(i);

  if (pFeatureClass.FeatureType == esriFeatureType.esriFTAnnotation)

  {

  //如果是注记,则添加注记层

  pFeatureLayer = (ESRI.ArcGIS.Carto.IFeatureLayer)mapContext.CreateObject("esriCarto.CadAnnotationLayerClass");

  //pFeatureLayer = new CadAnnotationLayerClass();

  }

  else//如果是点、线、面,则添加要素层

  {

  pFeatureLayer = (ESRI.ArcGIS.Carto.IFeatureLayer)mapContext.CreateObject("esriCarto.CadFeatureLayer");

  pFeatureLayer.Name = pFeatureClass.AliasName;

  pFeatureLayer.FeatureClass = pFeatureClass;

  ESRI.ArcGIS.Carto.IGeoFeatureLayer iglayer = (ESRI.ArcGIS.Carto.IGeoFeatureLayer)pFeatureLayer;

  ESRI.ArcGIS.Carto.ISimpleRenderer isr = (ESRI.ArcGIS.Carto.ISimpleRenderer)mapContext.CreateObject("esriCarto.SimpleRenderer");

  ESRI.ArcGIS.Display.IRgbColor irgbc = (ESRI.ArcGIS.Display.IRgbColor)mapContext.CreateObject("esriDisplay.RgbColor");

  irgbc.Red = 255;

  irgbc.Green = 0;

  irgbc.Blue = 0;

  ESRI.ArcGIS.Geometry.esriGeometryType featype = pFeatureLayer.FeatureClass.ShapeType;

  if (featype == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPoint)

  {

  ESRI.ArcGIS.Display.ISimpleMarkerSymbol ifs = (ESRI.ArcGIS.Display.ISimpleMarkerSymbol)mapContext.CreateObject("esriDisplay.SimpleMarkerSymbol");

  ifs.Color = (ESRI.ArcGIS.Display.IColor)irgbc;

  isr.Symbol = (ESRI.ArcGIS.Display.ISymbol)ifs;

  }

  else if (featype == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolyline)

  {

  ESRI.ArcGIS.Display.ISimpleLineSymbol ifs = (ESRI.ArcGIS.Display.ISimpleLineSymbol)mapContext.CreateObject("esriDisplay.SimpleLineSymbol");

  ifs.Color = (ESRI.ArcGIS.Display.IColor)irgbc;

  isr.Symbol = (ESRI.ArcGIS.Display.ISymbol)ifs;

  }

  else if (featype == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon)

  {

  ESRI.ArcGIS.Display.ISimpleFillSymbol ifs = (ESRI.ArcGIS.Display.ISimpleFillSymbol)mapContext.CreateObject("esriDisplay.SimpleFillSymbol");

  ifs.Color = (ESRI.ArcGIS.Display.IColor)irgbc;

  isr.Symbol = (ESRI.ArcGIS.Display.ISymbol)ifs;

  }

  else

  {

  ESRI.ArcGIS.Display.ISimpleLineSymbol ifs = (ESRI.ArcGIS.Display.ISimpleLineSymbol)mapContext.CreateObject("esriDisplay.SimpleLineSymbol");

  ifs.Color = (ESRI.ArcGIS.Display.IColor)irgbc;

  isr.Symbol = (ESRI.ArcGIS.Display.ISymbol)ifs;

  //throw new Exception("图层类型不确定!");

  }

  iglayer.Renderer = (ESRI.ArcGIS.Carto.IFeatureRenderer)isr;

  //IMapServer ms = (IMapServer)in_mapcontext.ServerObject;

  //IMapServerObjects2 mso = (IMapServerObjects2)ms;

  //string mapname = ms.DefaultMapName;

  //IMap map = mso.get_Map(mapname);

  //map.AddLayer(pFeatureLayer);

  //mso.RefreshServerObjects();

  }

  }

  //// 从上下文对象中得到服务器对象

  //IMapServer ms = (IMapServer)mapContext.ServerObject;

  //IMapServerInfo mapInfo = ms.GetServerInfo(ms.DefaultMapName);

  //IMapDescription NEWmapDesc = mapInfo.DefaultMapDescription;

  //// 释放服务器上下文对象

  //mapContext.ReleaseContext();

  ESRI.ArcGIS.Carto.IMapServerObjects mapServerObjects = localMapResource.MapServer as ESRI.ArcGIS.Carto.IMapServerObjects;

  ESRI.ArcGIS.Carto.IMap map = mapServerObjects.get_Map(localMapResource.DataFrame) as ESRI.ArcGIS.Carto.IMap;

  if (pFeatureLayer != null)

  {

  map.AddLayer(pFeatureLayer);

  //移动层到底层

  //map.MoveLayer(pFeatureLayer, map.LayerCount - 1);

  }

  //刷新服务器对象,响应添加图层

  localMapResource.RefreshServerObjects();

  //Toc控件刷新

  toc.Refresh();

  map1.CallbackResults.CopyFrom(toc.CallbackResults);

  map1.Refresh();

  return map1.CallbackResults.ToString();

  }

  最终调试正确代码:

  public static string AddCADLayer(Map map1, Toc toc)

  {

  // 得到服务器上下文对象

  MapFunctionality agsMapFunctionlity = map1.GetFunctionality(1) as MapFunctionality;

  MapResourceLocal localMapResource = (MapResourceLocal)agsMapFunctionlity.Resource;

  ESRI.ArcGIS.Server.IServerContext mapContext = localMapResource.ServerContextInfo.ServerContext;

  // 增加图层

  ESRI.ArcGIS.Carto.IFeatureLayer pFeatureLayer = null;

  //打开CAD数据集

  IWorkspaceFactory factory = (IWorkspaceFactory)mapContext.CreateObject("esriDataSourcesFile.CADWorkspaceFactory");

  IFeatureWorkspace pFeatureWorkspace = (IFeatureWorkspace)factory.OpenFromFile(@"C:\Inetpub\wwwroot\NorthAmerica", 0);

  //打开一个要素集

  IFeatureDataset pFeatureDataset = (IFeatureDataset)pFeatureWorkspace.OpenFeatureDataset("New_Shapefile_ExportCAD.DWG");

  //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 = (ESRI.ArcGIS.Carto.IFeatureLayer)mapContext.CreateObject("esriCarto.CadAnnotationLayerClass");

  }

  else//如果是点、线、面,则添加要素层

  {

  pFeatureLayer = (ESRI.ArcGIS.Carto.IFeatureLayer)mapContext.CreateObject("esriCarto.CadFeatureLayer");

  pFeatureLayer.Name = pFeatureClass.AliasName;

  pFeatureLayer.FeatureClass = pFeatureClass;

  ESRI.ArcGIS.Carto.IMapServerObjects mapServerObjects = localMapResource.MapServer as ESRI.ArcGIS.Carto.IMapServerObjects;

  ESRI.ArcGIS.Carto.IMap map = mapServerObjects.get_Map(localMapResource.DataFrame) as ESRI.ArcGIS.Carto.IMap;

  map.AddLayer(pFeatureLayer);

  //刷新服务器对象,响应添加图层

  localMapResource.RefreshServerObjects();

  }

  }

  //Toc控件刷新

  toc.Refresh();

  map1.CallbackResults.CopyFrom(toc.CallbackResults);

  map1.Refresh();

  return map1.CallbackResults.ToString();

  }