ArcObjects - enumerating feature classes and datasets within a geodatabase

Tom picture Tom · Mar 24, 2010 · Viewed 7.2k times · Source

I'm trying to enumerate the contents (feature classes and feature datasets, not interested in tables, etc) of a file geodatabase using vba/arcobjects.

I have the file GDB set as an IGxDatabase object, but can't find a way of getting further in. I've had a look at the geodatabase object model and tried using IFeatureClass and IFeatureDataset but neither seem to return useful results.

Thanks in advance for any assistance

Answer

Kirk Kuykendall picture Kirk Kuykendall · Mar 31, 2010

It is much faster to enumerate the names contained in a geodatabase instead of the things that the names can open. The tricky part is looping through names in a featuredataset. While IFeatureWorkspace.Open can be used to open a featureclass without first opening the featuredataset that contains it, getting at featureclassnames within a featuredataset requires opening the featuredataset.

If you know for sure you'll need to open each featureclass, then I suppose it wouldn't hurt to use IWorkspace.Datasets, IEnumDataset, and IDataset instead of IWorkspaceDatasetNames, IEnumDatasetname and IDatasetname.


Option Explicit
Sub TestGetContents()
    Dim pGxApp As IGxApplication
    Set pGxApp = Application
    If Not TypeOf pGxApp.SelectedObject Is IGxDatabase Then
        Debug.Print "select a geodb first"
        Exit Sub
    End If
    Dim c As Collection
    Set c = GetContents(pGxApp.SelectedObject)
    Dim l As Long
    For l = 1 To c.Count
        Dim pName As IName
        Set pName = c.Item(l)
        If TypeOf pName Is IFeatureClassName Then
            Dim pFC As IFeatureClass
            Set pFC = pName.Open
            Debug.Print pFC.AliasName, pFC.FeatureCount(Nothing)
        ElseIf TypeOf pName Is IFeatureDatasetName Then
            Dim pDSName As IDatasetName
            Set pDSName = pName
            Debug.Print pDSName.name, "(featuredataset)"
        End If
    Next l
End Sub

Function GetContents(pGxDB As IGxDatabase) As Collection Dim c As New Collection Dim pEnumDSName As IEnumDatasetName Set pEnumDSName = pGxDB.Workspace.DatasetNames(esriDTAny) Dim pDSName As IDatasetName Set pDSName = pEnumDSName.Next Do Until pDSName Is Nothing If TypeOf pDSName Is IFeatureClassName Then c.Add pDSName ElseIf TypeOf pDSName Is IFeatureDatasetName Then c.Add pDSName AddSubNames pDSName, c End If Set pDSName = pEnumDSName.Next Loop Set GetContents = c End Function

Sub AddSubNames(pDSName1 As IDatasetName, c As Collection) Dim pEnumDSName As IEnumDatasetName Set pEnumDSName = pDSName1.SubsetNames pEnumDSName.Reset Dim pDSName2 As IDatasetName Set pDSName2 = pEnumDSName.Next Do Until pDSName2 Is Nothing If TypeOf pDSName2 Is IFeatureClassName Then c.Add pDSName2 End If Set pDSName2 = pEnumDSName.Next Loop End Sub