ConstructUnion of geometries with same geometrytype

Created:01/24/2002
Description:

The ConstructUnion function below shows how you might union selected Geometries into only one. This function will take all the selected features in the view and union those only if they have the same geometryType than the first selected feature.

This function will works for esriGeometryPoint, esriGeometryMultiPoint, esriGeometryPolyline, esriGeometryPolygon.



How to use:
  1. Open ArcMap, open the VBA editor and paste the code below into the code window.
  2. Add one feature class or more to ArcMap. Select one feature or more, run the script. It will create a new instance of a given geometryType composed of the Union of all selected features of this type. You have to add supplementary code to Draw/Store it.
Option Explicit

'This sub Union the selected features using only the geometries with the same geometrytype than the first
'feature in the selection with a geometryType supported by ITopologicalOperator
Private Sub ConstructUnion()
   Dim pMxDoc As IMxDocument
   Dim pFeature As IFeature
   Dim pEnumFeature As IEnumFeature
   Dim pGeoDataset As IGeoDataset
   Dim pSpatialRef As ISpatialReference
   Dim pGeoBag As IGeometryCollection
   Dim pGeometry As IGeometry
   Dim pTopoOp As ITopologicalOperator2
   Dim pGeometryTemp As IGeometry
   Dim lGeometryType As Long
   Dim pTopoOpSimplify As ITopologicalOperator2
   Set pMxDoc = ThisDocument
   'Get the selected features
   Set pEnumFeature = pMxDoc.FocusMap.FeatureSelection
   Set pFeature = pEnumFeature.Next
   If pFeature Is Nothing Then
      MsgBox "You must select at least one feature ! "
      Exit Sub
   End If
   Set pGeoDataset = pFeature.Class
   'Get the spatial reference of the first feature in the enumerator
   Set pSpatialRef = pGeoDataset.SpatialReference
   Set pGeoBag = New GeometryBag
   'Set the spatial reference of the geometryBag via IGeometry
   Set pGeometry = pGeoBag 'QI
   Set pGeometry.SpatialReference = pSpatialRef
   Set pGeometryTemp = pFeature.ShapeCopy
   'Set the value of the Union GeometryType
   lGeometryType = pGeometryTemp.GeometryType
   'This loop select the first supported geometryType in one of the features
   Do While Not pFeature Is Nothing
      If lGeometryType < > esriGeometryPoint And lGeometryType < > esriGeometryMultipoint And lGeometryType < > esriGeometryPolyline And lGeometryType < > esriGeometryPolygon Then
         Set pGeometry = pFeature.ShapeCopy
         lGeometryType = pGeometryTemp.GeometryType
      Else
         Exit Do
      End If
      Set pFeature = pEnumFeature.Next
   Loop
   'Check if there is no supported type - Exit
   If lGeometryType < > esriGeometryPoint And lGeometryType < > esriGeometryMultipoint And lGeometryType < > esriGeometryPolyline And lGeometryType < > esriGeometryPolygon Then
      MsgBox "You must select at least one feature of type Point, Multipoint, Polyline or Polygon ! "
      Exit Sub
   End If
   pEnumFeature.Reset
   Set pFeature = pEnumFeature.Next
   While Not pFeature Is Nothing
   'Add only the geometry with the same geometrytype than the one determined earlier
         If pFeature.ShapeCopy.GeometryType = lGeometryType Then
            pGeoBag.AddGeometry pFeature.ShapeCopy
         End If
      Set pFeature = pEnumFeature.Next
   Wend
   'Create a new instance of a geometry
   Select Case lGeometryType
      Case esriGeometryPoint
         Set pTopoOp = New Multipoint
      Case esriGeometryMultipoint
         Set pTopoOp = New Multipoint
      Case esriGeometryPolyline
         Set pTopoOp = New Polyline
      Case esriGeometryPolygon
         Set pTopoOp = New Polygon
      Case Else
      Exit Sub
   End Select
   pTopoOp.ConstructUnion pGeoBag
   'The pTopoOp is now pointing to a geometry of type Point or Multipoint or Polyline or Polygon
   'You can use it as needed store, draw, ...
End Sub