ITopologicalOperator.ConstructUnion method
Union geometries using ITopologicalOperator2.ConstructUnion
Private Sub ConstructUnion()
Dim pMxDoc As IMxDocument
Set pMxDoc = ThisDocument
'Get the selected features
Dim pEnumFeature As IEnumFeature
Set pEnumFeature = pMxDoc.FocusMap.FeatureSelection
Dim pFeature As IFeature
Set pFeature = pEnumFeature.Next
If pFeature Is Nothing Then
MsgBox "You must select at least one feature ! "
Exit Sub
End If
Dim pGeoDataset As IGeoDataset
Set pGeoDataset = pFeature.Class
'Get the spatial reference of the first feature in the enumerator
Dim pSpatialRef As ISpatialReference
Set pSpatialRef = pGeoDataset.SpatialReference
Dim pGeoBag As IGeometryCollection
Set pGeoBag = New GeometryBag
'Set the spatial reference of the geometryBag via IGeometry
Dim pGeometry As IGeometry
Set pGeometry = pGeoBag 'QI
Set pGeometry.SpatialReference = pSpatialRef
Dim pGeometryTemp As IGeometry
Set pGeometryTemp = pFeature.ShapeCopy
'Set the value of the Union GeometryType
Dim lGeometryType As Long
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
Dim pTopoOp As ITopologicalOperator2
'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
End Sub
Dim pMxDoc As IMxDocument
Set pMxDoc = ThisDocument
'Get the selected features
Dim pEnumFeature As IEnumFeature
Set pEnumFeature = pMxDoc.FocusMap.FeatureSelection
Dim pFeature As IFeature
Set pFeature = pEnumFeature.Next
If pFeature Is Nothing Then
MsgBox "You must select at least one feature ! "
Exit Sub
End If
Dim pGeoDataset As IGeoDataset
Set pGeoDataset = pFeature.Class
'Get the spatial reference of the first feature in the enumerator
Dim pSpatialRef As ISpatialReference
Set pSpatialRef = pGeoDataset.SpatialReference
Dim pGeoBag As IGeometryCollection
Set pGeoBag = New GeometryBag
'Set the spatial reference of the geometryBag via IGeometry
Dim pGeometry As IGeometry
Set pGeometry = pGeoBag 'QI
Set pGeometry.SpatialReference = pSpatialRef
Dim pGeometryTemp As IGeometry
Set pGeometryTemp = pFeature.ShapeCopy
'Set the value of the Union GeometryType
Dim lGeometryType As Long
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
Dim pTopoOp As ITopologicalOperator2
'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
End Sub
-----------------------------------------------------------
佛对我说:你心里有尘。我用力的拭擦。