ArcEngine 打开AutoCAD文件的几种方法
方法一、
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();
}