[GIS] Merging features into single programmatically

arcgis-desktoparcobjectsvba

I have a shapefile with polygons.
I can merge them manually by going into edit session and, selecting a some features to be merged, and then using merge button under Editor.

How to do this programmatically (in VBA)?

I've found the analogous ways but it doesn't solve the problem:

  1. Standard Dissolve tool (from ArcToolbox) doesn't work correctly for this purpose, because it cuts off many fields and because it operates with whole FeatureClass but not with a separate features.
  2. As I know by reading some examples, using an IBasicGeoprocessor (VBA) have the same problem – it operates with whole FeatureClass, producing a new FeatureClass, but not with separate features, mearging them within the same FeatureClass.

@kenbuja, Here is my try in vba (not in vb.net) :
Using a CType causes the Compile error: Sub or Function not defined.
What do I do wrong?

Dim pMxDoc As IMxDocument
Dim pCountyLayer As IFeatureSelection
Dim pCountySelection As ISelectionSet
Dim pCountyCursor As IFeatureCursor
Dim pCountyCursor2 As IFeatureCursor
Dim pFirstFeature As IFeature
Dim pSecondFeature As IFeature
Dim pThirdFeature As IFeature

Set pMxDoc = ThisDocument
Set pCountyLayer = pMxDoc.FocusMap.Layer(1)
Set pCountySelection = pCountyLayer.SelectionSet

' Create a cursors from the selected feature in Layer (1).
pCountySelection.Search Nothing, True, pCountyCursor
pCountySelection.Search Nothing, True, pCountyCursor2
' Return the selected features from the cursor.

Set pFirstFeature = pCountyCursor.NextFeature


Set pThirdFeature = pCountyCursor2.NextFeature
Set pSecondFeature = pCountyCursor2.NextFeature


Dim pFirstGeometry As IGeometry
Set pFirstGeometry = CType(pFirstFeature.Shape, IGeometry)
Dim pTopoOperator As ITopologicalOperator
Set pTopoOperator = CType(pFirstGeometry, ITopologicalOperator)
Dim pSecondGeom As IGeometry
Set pSecondGeom = CType(pSecondFeature.Shape, IGeometry)
Dim pMergedGeom As IGeometry
Set pMergedGeom = pTopoOperator.Union(pSecondGeom)

Best Answer

You can use the ITopologicalOperator's Union method to programmatically merge two polygons together. This example (written in VB.NET) shows how that's done.

Dim pFirstGeometry As IGeometry = CType(pFirstFeature.shape, IGeometry)
Dim pTopoOperator As ITopologicalOperator = CType(pFirstGeometry, ITopologicalOperator)
Dim pSecondGeom As IGeometry = CType(pSecondFeature.shape, IGeometry)
Dim pMergedGeom As IGeometry = pTopoOperator.Union(pSecondGeom)
Related Question