打开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数据的点线面注记等要素类,所加载的数据均为一个独立的图层。
具体代码如下:
//其实在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();
相关文章
- 2021-09-08BIM技术丛书Revit软件应用系列Autodesk Revit族详解 [
- 2021-09-08全国专业技术人员计算机应用能力考试用书 AutoCAD2004
- 2021-09-08EXCEL在工作中的应用 制表、数据处理及宏应用PDF下载
- 2021-08-30从零开始AutoCAD 2014中文版机械制图基础培训教程 [李
- 2021-08-30从零开始AutoCAD 2014中文版建筑制图基础培训教程 [朱
- 2021-08-30电气CAD实例教程AutoCAD 2010中文版 [左昉 等编著] 20
- 2021-08-30电影风暴2:Maya影像实拍与三维合成攻略PDF下载
- 2021-08-30高等院校艺术设计案例教程中文版AutoCAD 建筑设计案例
- 2021-08-29环境艺术制图AutoCAD [徐幼光 编著] 2013年PDF下载
- 2021-08-29机械AutoCAD 项目教程 第3版 [缪希伟 主编] 2012年PDF