Add CAD Drawing Layers Programmatically

Created:10/18/2000
Description:

This macro adds all the layers in a CAD file in one map drawing layer. See the 'Add Cad Layer' sample for an example showing how to add each layer in a CAD file as a separate feature layer.

How to use:
  1. 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.
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 = "esriCore.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