ArcGIS Add graphics
ArcGIS_AddGraphics_VBNet\App_Code\PolylineTool.vb
' Copyright 2010 ESRI
' 
' All rights reserved under the copyright laws of the United States
' and applicable international laws, treaties, and conventions.
' 
' You may freely redistribute and use this sample code, with or
' without modification, provided you include the original copyright
' notice and use restrictions.
' 
' See the use restrictions.
' 

Imports Microsoft.VisualBasic
Imports System
Public Class PolylineTool
    Implements ESRI.ArcGIS.ADF.Web.UI.WebControls.Tools.IMapServerToolAction
  #Region "IMapServerToolAction Members"

  Private Sub ServerAction(ByVal toolEventArgs As ESRI.ArcGIS.ADF.Web.UI.WebControls.ToolEventArgs) Implements ESRI.ArcGIS.ADF.Web.UI.WebControls.Tools.IMapServerToolAction.ServerAction
    ' Get a reference to the Map control on which the tool was executed
    Dim adfMap As ESRI.ArcGIS.ADF.Web.UI.WebControls.Map = CType(toolEventArgs.Control, ESRI.ArcGIS.ADF.Web.UI.WebControls.Map)

    ' Get the polyline drawn by the user
    Dim mapPolylineEventArgs As ESRI.ArcGIS.ADF.Web.UI.WebControls.MapPolylineEventArgs = CType(toolEventArgs, ESRI.ArcGIS.ADF.Web.UI.WebControls.MapPolylineEventArgs)
    Dim adfPolyline As ESRI.ArcGIS.ADF.Web.Geometry.Polyline = mapPolylineEventArgs.MapPolyline

    ' Convert the Web ADF input polyline to its ArcGIS Server data source specific equivalent
    Dim agsSoapPolyline As ESRI.ArcGIS.ADF.ArcGISServer.PolylineN = ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.Converter.FromAdfPolyline(adfPolyline)

    ' Get an ArcGIS Server specific map functionality.  Note this code assumes the existence of an ArcGIS
    ' Server resource item with the name "MapResourceItem0"
    Dim agsMapFunctionality As ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapFunctionality = CType(adfMap.GetFunctionality("MapResourceItem0"), ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapFunctionality)

    ' Get the ArcGIS Server MapDescription for the resource
    Dim agsSoapMapDescription As ESRI.ArcGIS.ADF.ArcGISServer.MapDescription = agsMapFunctionality.MapDescription

    ' Create a blue ArcGIS Server data source specific color object
    Dim agsSoapRgbColor As ESRI.ArcGIS.ADF.ArcGISServer.RgbColor = New ESRI.ArcGIS.ADF.ArcGISServer.RgbColor()
    agsSoapRgbColor.Red = 0
    agsSoapRgbColor.Green = 0
    agsSoapRgbColor.Blue = 255

    ' Initialize an ArcGIS Server data source specific line symbol with a solid style and the color initialized above
    Dim agsSoapSimpleLineSymbol As ESRI.ArcGIS.ADF.ArcGISServer.SimpleLineSymbol = New ESRI.ArcGIS.ADF.ArcGISServer.SimpleLineSymbol()
    agsSoapSimpleLineSymbol.Color = agsSoapRgbColor
    agsSoapSimpleLineSymbol.Style = ESRI.ArcGIS.ADF.ArcGISServer.esriSimpleLineStyle.esriSLSSolid
    agsSoapSimpleLineSymbol.Width = 2

    ' Create an ArcGIS Server data source specific line element with the line symbol and input polyline
    Dim agsSoapLineElement As ESRI.ArcGIS.ADF.ArcGISServer.LineElement = New ESRI.ArcGIS.ADF.ArcGISServer.LineElement()
    agsSoapLineElement.Line = agsSoapPolyline
    agsSoapLineElement.Symbol = agsSoapSimpleLineSymbol

    ' If the current resource does not have any custom graphics specified, initialize this property with
    ' a new array with just the newly created graphic element.  Otherwise, create a new array with both
    ' the current custom graphics and teh newly created element.
    If Not agsSoapMapDescription.CustomGraphics Is Nothing Then
      ' Create a new ArcGIS Server graphic element array with a size that is one bigger than the
      ' resource's current element array
      Dim elementCount As Integer = agsSoapMapDescription.CustomGraphics.Length
      Dim agsSoapGraphicElementArray As ESRI.ArcGIS.ADF.ArcGISServer.GraphicElement() = New ESRI.ArcGIS.ADF.ArcGISServer.GraphicElement(elementCount){}

      ' Copy the resource's current element graphic array to the new array
      agsSoapMapDescription.CustomGraphics.CopyTo(agsSoapGraphicElementArray, 0)

      ' Add the newly created line element to the array
      agsSoapGraphicElementArray(elementCount) = agsSoapLineElement

      ' Update the resource's element array with the new array
      agsSoapMapDescription.CustomGraphics = agsSoapGraphicElementArray
    Else
      ' Create a new ArcGIS Server graphic element array with only the newly created line element
      Dim agsSoapGraphicElement As ESRI.ArcGIS.ADF.ArcGISServer.GraphicElement() = New ESRI.ArcGIS.ADF.ArcGISServer.GraphicElement(0){}
      agsSoapGraphicElement(0) = agsSoapLineElement

      ' Update the resource's element array with the new array
      agsSoapMapDescription.CustomGraphics = agsSoapGraphicElement
    End If

    ' Refresh the resource so the new line is shown
    adfMap.RefreshResource(agsMapFunctionality.Resource.Name)
  End Sub

  #End Region
End Class