ArcObjects Library Reference

Create Graphic Buffers around Selected Features Snippet

Draws graphic buffers around the selected features in the map using distance units specified.

[C#]

///<summary>Draws graphic buffers around the selected features in the map using distance units specified.</summary>
///
///<param name="activeView">An IActiveView interface.</param>
///<param name="distance">A System.Double that is the distance in map units around the select features to draw a graphic buffer. Example: 10</param>
/// 
///<remarks></remarks>
public void CreateGraphicBuffersAroundSelectedFeatures(ESRI.ArcGIS.Carto.IActiveView activeView, System.Double distance)
{
  //parameter check
  if (activeView == null || distance < 0)
  {
    return;
  }
  ESRI.ArcGIS.Carto.IMap map = activeView.FocusMap;
  // Clear any previous buffers from the screen
  ESRI.ArcGIS.Carto.IGraphicsContainer graphicsContainer = (ESRI.ArcGIS.Carto.IGraphicsContainer)map; // Explicit Cast
  graphicsContainer.DeleteAllElements();

  // Verify there is a feature(s) selected
  if (map.SelectionCount == 0)
  {
    return;
  }

  // Reset to the first selected feature
  ESRI.ArcGIS.Geodatabase.IEnumFeature enumFeature = (ESRI.ArcGIS.Geodatabase.IEnumFeature)map.FeatureSelection; // Explicit Cast
  enumFeature.Reset();
  ESRI.ArcGIS.Geodatabase.IFeature feature = enumFeature.Next();

  // Buffer all the selected features by the buffer distance and create a new polygon element from each result
  ESRI.ArcGIS.Geometry.ITopologicalOperator topologicalOperator;
  ESRI.ArcGIS.Carto.IElement element;
  while (!(feature == null))
  {
    topologicalOperator = (ESRI.ArcGIS.Geometry.ITopologicalOperator)feature.Shape; // Explicit Cast
    element = new ESRI.ArcGIS.Carto.PolygonElementClass();
    element.Geometry = topologicalOperator.Buffer(distance);
    graphicsContainer.AddElement(element, 0);
    feature = enumFeature.Next();
  }

  activeView.PartialRefresh(ESRI.ArcGIS.Carto.esriViewDrawPhase.esriViewGraphics, null, null);
}
[Visual Basic .NET]

'''<summary>Draws graphic buffers around the selected features in the map using distance units specified.</summary>
'''
'''<param name="activeView">An IActiveView interface.</param>
'''<param name="distance">A System.Double that is the distance in map units around the select features to draw a graphic buffer. Example: 10</param>
''' 
'''<remarks></remarks>
Public Sub CreateGraphicBuffersAroundSelectedFeatures(ByVal activeView As ESRI.ArcGIS.Carto.IActiveView, ByVal distance As System.Double)

  'parameter check
  If activeView Is Nothing OrElse distance < 0 Then
    Return
  End If

  Dim map As ESRI.ArcGIS.Carto.IMap = activeView.FocusMap
  ' Clear any previous buffers from the screen
  Dim graphicsContainer As ESRI.ArcGIS.Carto.IGraphicsContainer = CType(map, ESRI.ArcGIS.Carto.IGraphicsContainer) ' Explicit Cast
  graphicsContainer.DeleteAllElements()

  ' Verify there is a feature(s) selected
  If map.SelectionCount = 0 Then
    Return
  End If

  ' Reset to the first selected feature
  Dim enumFeature As ESRI.ArcGIS.Geodatabase.IEnumFeature = CType(map.FeatureSelection, ESRI.ArcGIS.Geodatabase.IEnumFeature) ' Explicit Cast
  enumFeature.Reset()
  Dim feature As ESRI.ArcGIS.Geodatabase.IFeature = enumFeature.Next()

  ' Buffer all the selected features by the buffer distance and create a new polygon element from each result
  Dim topologicalOperator As ESRI.ArcGIS.Geometry.ITopologicalOperator
  Dim element As ESRI.ArcGIS.Carto.IElement
  Do While Not (feature Is Nothing)
    topologicalOperator = CType(feature.Shape, ESRI.ArcGIS.Geometry.ITopologicalOperator) ' Explicit Cast
    element = New ESRI.ArcGIS.Carto.PolygonElementClass()
    element.Geometry = topologicalOperator.Buffer(distance)
    graphicsContainer.AddElement(element, 0)
    feature = enumFeature.Next()
  Loop

  activeView.PartialRefresh(ESRI.ArcGIS.Carto.esriViewDrawPhase.esriViewGraphics, Nothing, Nothing)

End Sub


Additional Requirements
  • The code in this document requires the following References added to the Visual Studio project:
  • ESRI.ArcGIS.Carto
  • ESRI.ArcGIS.Geodatabase
  • ESRI.ArcGIS.Geometry
  • ESRI.ArcGIS.System