How to access members that control the CAD drawing dataset


Summary This topic shows how to access members that control the computer-aided design (CAD) drawing dataset. This topic uses the Parcels.DWG file (installed with the ArcGIS tutorial); however, the sample code will work with any CAD file.

Accessing members that control the CAD drawing dataset

To access members that control the CAD drawing dataset, perform the following steps:
  1. Instantiate a CAD Workspace Factory.
  2. Get the file workspace by accessing the directory that contains the CAD file.
  3. Cast the IWorkspace interface to ICadDrawingWorkspace.
  4. Open the CAD drawing dataset by providing it the name of a CAD file, and set it to ICadDrawingDatatset.
  5. Print whether or not the CAD file exists and print the full path name of the CAD file by accessing the ICadDrawingDataset members.
  6. Instantiate a CadLayer.
  7. Set the CadLayer CadDrawingDataset property to ICadDrawingDataset.
  8. Print the property values, such as Is2d, Is3d, IsAutoCad and IsDGN, by accessing the members of CadLayer.
See the following code:
[C#]
// Set variables for the path and filename.
String nameOfPath = "C:\\data\\CAD";
String nameOfCADFile = "PARCELS.DWG";
//Set the workspace.
ESRI.ArcGIS.Geodatabase.IWorkspaceFactory pWorkspaceFact = new
    ESRI.ArcGIS.DataSourcesFile.CadWorkspaceFactory();
//Open the workspace.
ESRI.ArcGIS.Geodatabase.IWorkspace pWorkspace = pWorkspaceFact.OpenFromFile
    (nameOfPath, 0);
//Get the CADDrawingWorkspace.
ESRI.ArcGIS.DataSourcesFile.ICadDrawingWorkspace pCadDwgWorkspace;
pCadDwgWorkspace = (ESRI.ArcGIS.DataSourcesFile.ICadDrawingWorkspace)pWorkspace;
//Open the CadDrawingDataset.
ESRI.ArcGIS.DataSourcesFile.ICadDrawingDataset pCadDwgDataset;
pCadDwgDataset = pCadDwgWorkspace.OpenCadDrawingDataset(nameOfCADFile);
//Get and print Exists and Filepath.
System.Console.WriteLine("Exists = {0}", pCadDwgDataset.Exists);
System.Console.WriteLine("FilePath = {0}", pCadDwgDataset.FilePath);
//Open the CadLayer interface.
ESRI.ArcGIS.Carto.ICadLayer pCadLayer = new ESRI.ArcGIS.Carto.CadLayer()as
    ESRI.ArcGIS.Carto.ICadLayer;
//Get the CadDrawingDatsaset.
pCadLayer.CadDrawingDataset = pCadDwgDataset;
//Get and print the property value for IS2D.
System.Console.WriteLine("Is2d = {0}", pCadLayer.Is2d);
//Get and print the property value for IS3D.
System.Console.WriteLine("Is3d = {0}", pCadLayer.Is3d);
//Get and print the property value for IsAutoCAD.
System.Console.WriteLine("IsAutoCAD = {0}", pCadLayer.IsAutoCad);
//Get and print the property value for IsDgn.
System.Console.WriteLine("IsDgn = {0}", pCadLayer.IsDgn);
[VB.NET]
' Set variables for the filename and feature class.
Dim nameOfPath As String = "C:\\arcgis\\ArcTutor\\Editor\\ExerciseData\\EditingCAD"
Dim nameOfCADFile As String = "PARCELS.DWG"
'Set the Workspace Factory.
Dim pWorkspaceFact As ESRI.ArcGIS.Geodatabase.IWorkspaceFactory = New ESRI.ArcGIS.DataSourcesFile.CadWorkspaceFactory()
'Open the workspace.
Dim pWorkspace As ESRI.ArcGIS.Geodatabase.IWorkspace = pWorkspaceFact.OpenFromFile(nameOfPath, 0)
'Get the CADDrawingWorkspace.
Dim pCadDwgWorkspace As ESRI.ArcGIS.DataSourcesFile.ICadDrawingWorkspace
pCadDwgWorkspace = DirectCast(pWorkspace, ESRI.ArcGIS.DataSourcesFile.ICadDrawingWorkspace)
'Open the CadDrawingDataset.
Dim pCadDwgDataset As ESRI.ArcGIS.DataSourcesFile.ICadDrawingDataset
pCadDwgDataset = pCadDwgWorkspace.OpenCadDrawingDataset(nameOfCADFile)
'Get and print Exists and Filepath.
System.Console.WriteLine("Exists = {0}", pCadDwgDataset.Exists)
System.Console.WriteLine("FilePath = {0}", pCadDwgDataset.FilePath)
'Open the CadLayer interface.
Dim pCadLayer As ESRI.ArcGIS.Carto.ICadLayer = TryCast(New ESRI.ArcGIS.Carto.CadLayer(), ESRI.ArcGIS.Carto.ICadLayer)
'Get the CadDrawingDatsaset.
pCadLayer.CadDrawingDataset = pCadDwgDataset
'Get and print the property value for IS2D.
System.Console.WriteLine("Is2d = {0}", pCadLayer.Is2d)
'Get and print the property value for IS3D.
System.Console.WriteLine("Is3d = {0}", pCadLayer.Is3d)
'Get and print the property value for IsAutoCAD.
System.Console.WriteLine("IsAutoCAD = {0}", pCadLayer.IsAutoCad)
'Get and print the property value for IsDgn.
System.Console.WriteLine("IsDgn = {0}", pCadLayer.IsDgn)






To use the code in this topic, reference the following assemblies in your Visual Studio project. In the code files, you will need using (C#) or Imports (VB .NET) directives for the corresponding namespaces (given in parenthesis below if different from the assembly name):
Development licensing Deployment licensing
ArcView ArcView
ArcEditor ArcEditor
ArcInfo ArcInfo