合并要素--how to use "ConstructUnion"

 

 参考:http://forums.esri.com/Thread.asp?c=93&f=1170&t=80838

 

 The following VBA macro creates a graphic element that is the union of all selected polygons in the map. The union is constructed using a geometry bag.

 

 

Sub UnionFeatures()

Dim pMxDoc As IMxDocument

Dim pEnumFeature As IEnumFeature

Dim pFeature As IFeature

Dim pGeoBag As IGeometryBag

Dim pGeoCollection As IGeometryCollection

Dim pTopoOp As ITopologicalOperator2

Dim pPolygon As IPolygon

Dim pPolygonElement As IPolygonElement

Dim pElement As IElement

Dim pGC As IGraphicsContainer



Set pMxDoc = ThisDocument

Set pEnumFeature = pMxDoc.FocusMap.FeatureSelection

pEnumFeature.Reset

Set pGeoBag = New GeometryBag

Set pGeoCollection = pGeoBag



Set pFeature = pEnumFeature.Next

Do While Not pFeature Is Nothing

If pFeature.Shape.GeometryType = esriGeometryPolygon Then

Set pTopoOp = pFeature.ShapeCopy

pTopoOp.IsKnownSimple = False

pTopoOp.Simplify

pGeoCollection.AddGeometry pTopoOp

End If



Set pFeature = pEnumFeature.Next

Loop



Set pTopoOp = New Polygon

pTopoOp.ConstructUnion pGeoBag

Set pPolygon = pTopoOp

Set pGC = pMxDoc.FocusMap

Set pPolygonElement = New PolygonElement

Set pElement = pPolygonElement

pElement.Geometry = pPolygon

pGC.AddElement pElement, 0

pMxDoc.ActiveView.Refresh

End Sub