This document is archived and information here might be outdated.  Recommended version.


How to return CAD drawing layer properties (ArcObjects .NET 10.5 SDK)

How to return CAD drawing layer properties


Summary
This topic shows how to loop through layers in a computer-aided design (CAD) file and write the layer properties to the console. This topic uses the Parcels.DWG file (installed with the ArcGIS tutorial); however, the sample code will work with any CAD file.

Returning CAD drawing layer properties

To return CAD drawing layer properties, follow these 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 IFeatureWorkspace.
  4. Get the feature classes present in the CAD file. In this example, the Polyline Feature class is obtained from IFeatureWorkspace.
  5. Get the IFeatureLayer interface and cast it to the ICadDrawingLayers interface.
  6. Get the layer count and loop through the ICadDrawingLayers interface object.
  7. Get the name and visibility property of each layer.
See the following code. This code sample can be modified to set properties of each layer.
[C#]
// Set variables for the path and filename.
String nameOfPath="C:\\data\\CAD";
String nameOfCADFile="PARCELS.DWG";
//Set the Workspace Factory.
ESRI.ArcGIS.Geodatabase.IWorkspaceFactory pWorkspaceFact=new
    ESRI.ArcGIS.DataSourcesFile.CadWorkspaceFactory();
//Open the workspace. 
ESRI.ArcGIS.Geodatabase.IWorkspace pWorkspace=pWorkspaceFact.OpenFromFile
    (nameOfPath, 0);
//Set the feature workspace. 
ESRI.ArcGIS.Geodatabase.IFeatureWorkspace pFeatureWorkspace=
    (ESRI.ArcGIS.Geodatabase.IFeatureWorkspace)pWorkspace;
//Open the Feature Class. 
ESRI.ArcGIS.Geodatabase.IFeatureClass pFeatClass =
    pFeatureWorkspace.OpenFeatureClass(System.String.Concat(nameOfCADFile, 
    ":Polyline"));
//Set the Feature Layer to the Feature Class.
ESRI.ArcGIS.Carto.IFeatureLayer pFeatLayer=new ESRI.ArcGIS.Carto.CadFeatureLayer()
    as ESRI.ArcGIS.Carto.IFeatureLayer;
pFeatLayer.FeatureClass=pFeatClass;
//Set the CadDrawingLayer to the Feature Layer. 
ESRI.ArcGIS.DataSourcesFile.ICadDrawingLayers pCadDwgLayers=
    (ESRI.ArcGIS.DataSourcesFile.ICadDrawingLayers)pFeatLayer;
//List the drawing layer count. 
System.Console.WriteLine("DrawingLayerCount={0}", pCadDwgLayers.DrawingLayerCount);
//Loop through the drawing layers.
short i;
for (i=0; i <= pCadDwgLayers.DrawingLayerCount - 1; i++)
{
    //List the drawing layer names.
    System.Console.WriteLine("DrawingLayerName={0} ",
        pCadDwgLayers.get_DrawingLayerName(i));
    //List the drawing layer's visibility.
    System.Console.WriteLine("DrawingLayerVisible={0}",
        pCadDwgLayers.get_DrawingLayerVisible(i));
    //List the drawing layer's original visibility. 
    System.Console.WriteLine("OriginalDrawingLayerVisible={0}",
        pCadDwgLayers.get_OriginalDrawingLayerVisible(i));
}
[VB.NET]
'Set variables for the path and filename.
Dim nameOfPath As String="c:\\data\\CAD"
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)
'Set the feature workspace.
Dim pFeatureWorkspace As ESRI.ArcGIS.Geodatabase.IFeatureWorkspace=DirectCast(pWorkspace, ESRI.ArcGIS.Geodatabase.IFeatureWorkspace)
'Open the Feature Class.
Dim pFeatClass As ESRI.ArcGIS.Geodatabase.IFeatureClass=pFeatureWorkspace.OpenFeatureClass(System.String.Concat(nameOfCADFile, ":Polyline"))
'Set the Feature Layer to the Feature Class.
Dim pFeatLayer As ESRI.ArcGIS.Carto.IFeatureLayer=TryCast(New ESRI.ArcGIS.Carto.CadFeatureLayer(), ESRI.ArcGIS.Carto.IFeatureLayer)
pFeatLayer.FeatureClass=pFeatClass
'Set the CadDrawingLayer to the Feature Layer.
Dim pCadDwgLayers As ESRI.ArcGIS.DataSourcesFile.ICadDrawingLayers=DirectCast(pFeatLayer, ESRI.ArcGIS.DataSourcesFile.ICadDrawingLayers)
'List the drawing layer count.
Console.WriteLine("DrawingLayerCount={0}", pCadDwgLayers.DrawingLayerCount)
'Loop through the drawing layers.
Dim i As Short
For i=0 To pCadDwgLayers.DrawingLayerCount - 1
    'List the drawing layer names.
    Console.WriteLine("DrawingLayerName={0} ", pCadDwgLayers.DrawingLayerName(i))
    'List the drawing layer's visibility.
    Console.WriteLine("DrawingLayerVisible={0}", pCadDwgLayers.DrawingLayerVisible(i))
    'List the drawing layer's original visibility.
    Console.WriteLine("OriginalDrawingLayerVisible={0}", pCadDwgLayers.OriginalDrawingLayerVisible(i))
Next






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
ArcGIS Desktop Basic ArcGIS Desktop Basic
ArcGIS Desktop Standard ArcGIS Desktop Standard
ArcGIS Desktop Advanced ArcGIS Desktop Advanced