Add CAD Feature Layers Programmatically


This macro adds each CAD file layer as a separate feature layer in the focus map. See the 'Add Cad Drawing Layer' sample for an example showing how to add an entire CAD file in one map drawing 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 LoadCadDrawingLayers()
  Dim pMxDoc As IMxDocument
  Dim pMap As IMap
  Dim pCadWKSFact As IWorkspaceFactory
  Dim pWorkspace As IFeatureWorkspace
  Dim pCadFeatureLayer As IFeatureLayer
  Dim pFeatureClass As IFeatureClass
  Dim pFeatureDataset As IFeatureDataset
  Dim pFeatureClassContainer As IFeatureClassContainer
  Dim Count As Integer
  Set pMxDoc = Application.Document
  Set pMap = pMxDoc.FocusMap
  Set pCadWKSFact = New CadWorkspaceFactory
  Set pWorkspace = pCadWKSFact.OpenFromFile("d:\data\cad", 0)
  Set pFeatureDataset = pWorkspace.OpenFeatureDataset("e-51878.dwg")
  Set pFeatureClassContainer = pFeatureDataset
  For Count = 0 To pFeatureClassContainer.ClassCount - 1
    Set pFeatureClass = pFeatureClassContainer.Class(Count)
    'Check for annotation
    If pFeatureClass.FeatureType = esriFTCoverageAnnotation Then
      Set pCadFeatureLayer = New CadAnnotationLayer
      Set pCadFeatureLayer = New CadFeatureLayer
    End If
    pCadFeatureLayer.Name = pFeatureClass.AliasName 'Give the map layer a name
    Set pCadFeatureLayer.FeatureClass = pFeatureClass
    pMap.AddLayer pCadFeatureLayer
  Next Count
  pMxDoc.UpdateContents 'Refresh the TOC

End Sub