How to modify a polygon


The following code demonstrates how to modify a polygon geometry object using a user input point.
  • modifyOneVertexOfAPolygon shows how to modify a polygon vertex using Hittest.
  • modifyOneSegmentOfAPolygon shows how to modify a polygon segment using Hittest.

How to use

  1. Use this code in VBA
[VBA]
'*************************************************************************
'* NAME: modifyOneVertexOfAPolygon
'* DESCRIPTION: Modify a vertex of a multipart polygon using hittest to identify the vertex,
'* move it and set the point back in the polygon.
'* NOTE:
'*************************************************************************

Sub modifyOneVertexOfAPolygon()
    Dim pPolColl As IGeometryCollection, pPol As IPolygon, pHitTest As IHitTest, pTrans2D As ITransform2D, pPathColl As IPointCollection
    Dim pqpt As IPoint, dr As Double, pHit As IPoint, dht As Double, lPart As Long, lVertex As Long, bright As Boolean
    Set pPolColl = FunctionCreateMultiPartPolygonViaIGeometryCollection 'Here the polygon to be modified
    Set pPol = pPolColl
    Set pHitTest = pPolColl
    Set pqpt = pPol.FromPoint 'This could be an user input point (Ex: Mouse click etc.)
    dr = 1
    Set pHit = New esriGeometry.Point 'Create the output point
    pHitTest.HitTest pqpt, dr, esriGeometryPartVertex, pHit, dht, lPart, lVertex, bright
    Set pPathColl = pPolColl.Geometry(lPart) 'Get the ring where the point is
    Set pTrans2D = pPathColl.Point(lVertex) 'Get the vertex that got hit
    pTrans2D.Move -10, 0 'Move the point
    pPathColl.UpdatePoint lVertex, pTrans2D 'Update the point via IPointCollection
    pPolColl.GeometriesChanged 'To let the polygon knows its geometry changed
    'User should call ITopologicalOperator::Simplify at this point
End Sub


'*************************************************************************
'* NAME: modifyOneSegmentOfAPolygon()
'* DESCRIPTION: Modify a segment of a multipart polygon using hittest to identify the segment and
'* move it.
'* NOTE:
'*************************************************************************

Sub modifyOneSegmentOfAPolygon()
    Dim pPolColl As IGeometryCollection, pPol As IPolyline, pHitTest As IHitTest, pTrans2D As ITransform2D, pPathColl As ISegmentCollection
    Dim pqpt As IPoint, dr As Double, pHit As IPoint, dht As Double, lPart As Long, lSeg As Long, bright As Boolean
    Set pPolColl = FunctionCreateMultiPartPolygonViaIGeometryCollection 'Here the polygon to be modified
    Set pPol = pPolColl
    Set pHitTest = pPolColl
    Set pqpt = pPol.FromPoint 'This could be an user input point (Ex: Mouse click etc.)
    dr = 1
    Set pHit = New esriGeometry.Point
    pHitTest.HitTest pqpt, dr, esriGeometryPartBoundary, pHit, dht, lPart, lSeg, bright
    Set pPathColl = pPolColl.Geometry(lPart) 'Get the ring where the point is
    Set pTrans2D = pPathColl.Segment(lSeg) 'Get the segment that got hit
    pTrans2D.Move -10, 0
    'In this case the segment doesn't have to be set back in the geometry, since the segment
    'was returned by reference by pPathColl.Segment(lSeg)
    pPolColl.GeometriesChanged 'To let the polygon knows its geometry changed
    'User should call ITopologicalOperator::Simplify at this point
End Sub