Method 1,
IWorkspaceFactory pWorkspaceFactory;
IFeatureWorkspace pFeatureWorkspace;
IFeatureLayer pFeatureLayer;
IFeatureDataset pFeatureDataset;
// Open the CAD Dataset
PWorkspaceFactory = new CadWorkspaceFactoryClass ();
PFeatureWorkspace = (IFeatureWorkspace) pWorkspaceFactory. OpenFromFile (directoryPath, 0 );
// Open a feature set
PFeatureDataset = pFeatureWorkspace. OpenFeatureDataset (fileName );
// IFeaturClassContainer can manage each element class in IFeatureDataset
IFeatureClassContainer pFeatureClassContainer = (IFeatureClassContainer) pFeatureDataset;
// Traverse the elements in the CAD file
For (int I = 0; I <pFeatureClassContainer. ClassCount-1; I ++)
{
IFeatureClass pFeatureClass = pFeatureClassContainer. get_Class (I );
If (pFeatureClass. FeatureType = esriFeatureType. esriFTAnnotation)
{
// If it is a note, add the note Layer
PFeatureLayer = new CadAnnotationLayerClass ();
}
Else // if it is a vertex, line, or surface, add the element layer
{
PFeatureLayer = new FeatureLayerClass ();
PFeatureLayer. Name = pFeatureClass. AliasName;
PFeatureLayer. FeatureClass = pFeatureClass;
This. axmc_Main.Map.AddLayer (pFeatureLayer );
This. axmc_Main.ActiveView .**();
}
}
Method 2,
// -- Define the workspace and instantiate it with CadWorkspaceFactoryClass ()
IWorkspaceFactory pWorkspaceFactory = new CadWorkspaceFactoryClass ();
// -- Open the corresponding workspace and assign it to the element space. OpenFromFile ()
// -- The parameter in is the path of the CAD folder
IFeatureWorkspace pFeatureWorkspace = pWorkspaceFactory. OpenFromFile (directoryPath, 0) as IFeatureWorkspace;
/* -- Open the line element class. To open the element of the point type, you need to convert the following code:
* -- IFeatureClass pFeatureClass = pFeatureWorkspace. OpenFeatureClass (fileName + ": point ");
* -- It can be seen that fileName is the name of the CAD chart, followed by the type of the element class to be opened, with a colon in the middle
* -- Separated. You can think about how polygon and annotation are opened. */
// 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 .**();
Method 3,
/* -- The following two lines of code define a CAD workspace and open it, but this time it is not assigned
-- IFeatureWorkspace object, which is assigned to the IWorkspace Defined Object */
IWorkspaceFactory pWorkspaceFactory = new CadWorkspaceFactoryClass ();
IWorkspace pWorkspace = pWorkspaceFactory. OpenFromFile (directoryPath, 0 );
// -- Define a CAD Drawing space and assign the workspace opened above to it
ICadDrawingWorkspace pCadDrawingWorkspace = pWorkspace as ICadDrawingWorkspace;
// -- Define a CAD Drawing dataset and open a CAD diagram in the specified workspace above
// -- Then assign a value to the CAD Dataset
ICadDrawingDataset pCadDrawingDataset = pCadDrawingWorkspace. OpenCadDrawingDataset (fileName );
// -- Assign the CAD data obtained above to
// -- CadDrawingDataset attributes
ICadLayer pCadLayer = new CadLayerClass ();
PCadLayer. CadDrawingDataset = pCadDrawingDataset;
// -- Use MapControl to load the CAD Layer
This. axmc_Main.Map.AddLayer (pCadLayer );
This. axmc_Main.ActiveView .**();
Method 4,
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 .**();
}
Method 5,
// 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>
/// Obtain the record set
/// </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 = "esristmcesfile. 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 ();
}
Note:
If your ArcGIS version is V9.2, it may be because your file name contains Chinese characters. as far as I know, ArcGIS9.2 cannot display CAD files with Chinese characters in file names. you can open it directly In ArcMap. If it cannot be displayed, it may be a file name problem (note: the path cannot contain Chinese characters ).
Reproduced from: http://www.gisforum.net/bbs/dispbbs.asp? Boardid = 35 & id = 145238