How to create a layer file


This tip demonstrates how to create a layer file (in this case pointing to a CAD file) based on the selected item.

How to use

  1. Paste this macro into VBA.
  2. Select a CAD Drawing in the right window of ArcCatalog (the contents view).
  3. Run this macro.
[VBA]
Public Sub CreateLayerFile()
    Dim pLayerFactory As ILayerFactory, pApp As IGxApplication, pGxObject As IGxObject
    Dim pName As IName
    Set pLayerFactory = New CadLayerFactory
    Set pApp = Application
    Set pGxObject = pApp.SelectedObject
    'Use GetObjectFromFullName if you want to specify a path to a file on disk
    Set pName = pGxObject.InternalObjectName
    If Not pLayerFactory.CanCreate(pName) Then
        MsgBox "Cannot create layer"
        Exit Sub
    End If
    
    Dim pEnum As IEnumLayer, pLayer As ILayer, pGxLayer As IGxLayer, pFile As IGxFile
    Set pEnum = pLayerFactory.Create(pName)
    Set pGxLayer = New GxLayer
    Set pFile = pGxLayer
    pFile.Path = "C:\temp\mylayer.lyr"
    Set pGxLayer.Layer = pEnum.Next
    pFile.Save
End Sub