CAD文件有两种加载方式,一种是要素类图层的加载方式,另一种是像栅格图层一样加载。
1 要素类方式加载
IWorkspaceFactory pWorkspaceFactory = new CadWorkspaceFactoryClass();
IFeatureWorkspace pFeatureWorkspace = (IFeatureWorkspace) pWorkspaceFactory.OpenFromFile("C:\\path\\to\\CAD", 0);
CAD加载方式一:分图层加载CAD文件(包括:点、线、面和注记)
//打开一个要素集
IFeatureDataset pFeatureDataset = pFeatureWorkspace.OpenFeatureDataset("***.dwg");
IFeatureClassContainer pFeatureClassContainer = (IFeatureClassContainer) pFeatureDataset;
IGroupLayer pGroupLayer = new GroupLayerClass();
pGroupLayer.Name = pFeatureDataset.Name;
//遍历CAD文件中的每个要素
for (int i = 0; i < pFeatureClassContainer.ClassCount; i++) {
IFeatureClass pFeatureClass = pFeatureClassContainer.get_Class(i);
//加载注记图层【esriFTCoverageAnnotation】
if (pFeatureClass.FeatureType == esriFeatureType.esriFTCoverageAnnotation) {
IFeatureLayer pFeatureLayer = new CadAnnotationLayerClass();
pFeatureLayer.Name = pFeatureClass.AliasName;
pFeatureLayer.FeatureClass = pFeatureClass;
pFeatureLayer.DataSourceType = "CAD Annotation Feature Class";//设置后Annotation的默认符号化方式是注记而不是点
pGroupLayer.Add(pFeatureLayer);
}
//加载点线面图层
else {
IFeatureLayer pFeatureLayer = new FeatureLayerClass();
pFeatureLayer.Name = pFeatureClass.AliasName;
pFeatureLayer.FeatureClass = pFeatureClass;
pGroupLayer.Add(pFeatureLayer);
}
}
pMap.AddLayer(pGroupLayer);
2 像raster一样加载CAD文件
IWorkspaceFactory pWorkspaceFactory = new CadWorkspaceFactoryClass();
IFeatureWorkspace pFeatureWorkspace = (IFeatureWorkspace) pWorkspaceFactory.OpenFromFile("C:\\path\\to\\CAD", 0);
ICadDrawingWorkspace pCadDrawingWorkspace = pFeatureWorkspace as ICadDrawingWorkspace;
ICadDrawingDataset pCadDataset = pCadDrawingWorkspace.OpenCadDrawingDataset("***.dwg");
ICadLayer pCadLayer = new CadLayerClass();
pCadLayer.CadDrawingDataset = pCadDataset;
pMap.AddLayer(pCadLayer);