This sample code uses the Object Loader to add features into a feature class that is being edited. This is the same functionality as the Load Objects command available in ArcMap.
The example assumes that the top most layer in the table of contents is a USA states layer that is being edited. Features satisfying a query condition are loaded from a shapefile that is not necessarily present as a layer. Note that the load operation can be undone.
How to use
- Paste the code into your ArcMap VBA application.
- Adjust the code to connect to a feature class containing compatible data with the top-most layer in the ArcMap table of contents
- Adjust the query filter in the code to correspond to your data.
- Ensure an edit session is running for the workspace containing the top-most layer in the ArcMap table of contents
- For clarity, delete any existing features that will get loaded from the other feature class.
- From the Macros dialog, run the ObjectLoader_Example macro.
Public Sub ObjectLoader_Example()
On Error GoTo EH
' Get the Editor, there must be an edit session active
Dim pEditor As IEditor
Dim pID As New UID
pID = "esriEditor.Editor"
Set pEditor = Application.FindExtensionByCLSID(pID)
' Set up the output feature classes, i.e. the destination
Dim pOutFClass As IFeatureClass
Dim pFeatLayer As IFeatureLayer
Dim pDoc As IMxDocument
Dim pMap As IMap
Set pDoc = ThisDocument
Set pMap = pDoc.FocusMap
Set pFeatLayer = pMap.Layer(0)
Set pOutFClass = pFeatLayer.FeatureClass
' Set up the input feature classe, i.e. the data source
Dim pInFClass As IFeatureClass
Dim pWSF As IWorkspaceFactory
Set pWSF = New ShapefileWorkspaceFactory
Dim pFeatWS As IFeatureWorkspace
Set pFeatWS = pWSF.OpenFromFile("C:\Program Files\ArcGIS\DeveloperKit10.0\Samples\data\usa", 0)
Set pInFClass = pFeatWS.OpenFeatureClass("states")
' Specify a subset of the input data
Dim pQueryFilter As IQueryFilter
Set pQueryFilter = New QueryFilter
pQueryFilter.SubFields = "SHAPE,STATE_NAME"
pQueryFilter.WhereClause = "STATE_NAME = 'Alaska' OR STATE_NAME = 'Hawaii'"
' OutputFields parameter needs to match sub-fields in input queryfilter
Dim pAllFields As IFields
Set pAllFields = pOutFClass.Fields
Dim pOutFields As IFields
Set pOutFields = New Fields
Dim pOutFieldsEdit As IFieldsEdit
Set pOutFieldsEdit = pOutFields
' Get the query filter sub-fields as an array
' and loop through each field in turn,
' adding it to the ouput fields
Dim sSubFields() As String
sSubFields = Split(pQueryFilter.SubFields, ",")
Dim i, j As Long
For j = LBound(sSubFields) To UBound(sSubFields)
i = pAllFields.FindField(sSubFields(j))
If i = -1 Then
MsgBox "field not found:" & sSubFields(j)
Exit Sub
End If
pOutFieldsEdit.AddField pAllFields.Field(i)
Next j
Dim pObjectLoader As IObjectLoader
Set pObjectLoader = New ObjectLoader
Dim pEnumInvalidObject As IEnumInvalidObject
pObjectLoader.LoadObjects pEditor, _
pInFClass, pQueryFilter, _
pOutFClass, pOutFields, _
False, 0, False, False, 10, _
pEnumInvalidObject
Dim pInvalidObject As IInvalidObjectInfo
Set pInvalidObject = pEnumInvalidObject.Next
If Not pInvalidObject Is Nothing Then
MsgBox "Some or all features did not load"
End If
pDoc.ActiveView.Refresh
Exit Sub
EH:
MsgBox Err.Description, vbInformation, "Object Loader Example"
End Sub