How to add a CAD drawing layer


This macro adds all the layers in a CAD file in one CAD drawing layer. See the How to add a CAD feature layer for an example showing how to add each layer in a CAD file as a separate feature layer.

How to use

  1. Open ArcMap and paste the code into VBA.
  2. Modify the code to point to a CAD file on your system.
  3. Run the routine from the Macros dialog.
[VBA]
Public Sub LoadCadDrawingLayer()
    Dim pMxDoc As IMxDocument
    Dim pCadLayer As ICadLayer
    Dim pCadDrawingDataset As ICadDrawingDataset
    
    Set pMxDoc = Application.Document
    Set pCadDrawingDataset = GetCadDataset("d:\data\cad", "e-51878.dwg")
    If pCadDrawingDataset Is Nothing Then Exit Sub
    Set pCadLayer = New CadLayer
    Set pCadLayer.CadDrawingDataset = pCadDrawingDataset
    pCadLayer.Name = "e-51878.dwg" 'Give the map layer a name
    pMxDoc.FocusMap.AddLayer pCadLayer
    pMxDoc.UpdateContents 'Update the TOC
End Sub

Private Function GetCadDataset(strCadWorkspacePath As String, strCadFileName As String) As ICadDrawingDataset
    Dim pName As IName
    Dim pCadDatasetName As IDatasetName
    Dim pWorkspaceName As IWorkspaceName
    
    On Error GoTo ErrorHandler
    
    'Create a WorkspaceName object
    Set pWorkspaceName = New WorkspaceName
    pWorkspaceName.WorkspaceFactoryProgID = "esriDataSourcesFile.CadWorkspaceFactory"
    pWorkspaceName.PathName = strCadWorkspacePath
    
    'Create a CadDrawingName object
    Set pCadDatasetName = New CadDrawingName
    pCadDatasetName.Name = strCadFileName
    Set pCadDatasetName.WorkspaceName = pWorkspaceName
    
    'Open the CAD drawing
    Set pName = pCadDatasetName 'QI
    Set GetCadDataset = pName.Open
    Exit Function
    
ErrorHandler:
    Set GetCadDataset = Nothing
End Function