ArcGIS Add graphics
ArcGIS_AddGraphics_CSharp\App_Code\TextTool.cs
// 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.
// 

using System;
public class TextTool : ESRI.ArcGIS.ADF.Web.UI.WebControls.Tools.IMapServerCommandAction, ESRI.ArcGIS.ADF.Web.UI.WebControls.Tools.IMapServerToolAction
{
  #region IServerAction Members

  public void ServerAction(ESRI.ArcGIS.ADF.Web.UI.WebControls.Tools.ToolbarItemInfo toolbarItemInfo)
  {
    // Get a reference to the Web ADF Map control that's buddied to the toolbar containing the command
    ESRI.ArcGIS.ADF.Web.UI.WebControls.Map adfMap = (ESRI.ArcGIS.ADF.Web.UI.WebControls.Map)(toolbarItemInfo.BuddyControls[0]);

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

    // Get the ArcGIS Server MapDescription for the resource
    ESRI.ArcGIS.ADF.ArcGISServer.MapDescription agsSoapMapDescription = agsMapFunctionality.MapDescription;

    // Create a Web ADF Point for the point on the map at screen coordinates (200, 200).  Note that this 
    // code assumes the map control's dimensions are at least 200 x 200.
    ESRI.ArcGIS.ADF.Web.Geometry.Point adfPoint = ESRI.ArcGIS.ADF.Web.Geometry.Point.ToMapPoint(200, 200, adfMap.GetTransformationParams (ESRI.ArcGIS.ADF.Web.Geometry.TransformationDirection.ToMap));

    // Convert the Web ADF point to its ArcGIS Server specific equivalent
    ESRI.ArcGIS.ADF.ArcGISServer.PointN agsSoapPoint = ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.Converter.FromAdfPoint(adfPoint);

    // Create a blue ArcGIS Server data source specific color object
    ESRI.ArcGIS.ADF.ArcGISServer.RgbColor agsSoapRgbColor = new ESRI.ArcGIS.ADF.ArcGISServer.RgbColor();
    agsSoapRgbColor.Red = 0;
    agsSoapRgbColor.Green = 0;
    agsSoapRgbColor.Blue = 255;

    // Initialize an ArcGIS Server data source specific text symbol
    ESRI.ArcGIS.ADF.ArcGISServer.TextSymbol agsSoapTextSymbol = new ESRI.ArcGIS.ADF.ArcGISServer.TextSymbol();
    agsSoapTextSymbol.Color = agsSoapRgbColor;
    agsSoapTextSymbol.Size = 10;
    agsSoapTextSymbol.FontName = "Arial";

    // Create an ArcGIS Server data source specific text element with the text symbol, input point, and the
    // text "COMMAND"
    ESRI.ArcGIS.ADF.ArcGISServer.TextElement agsSoapTextElement = new ESRI.ArcGIS.ADF.ArcGISServer.TextElement();
    agsSoapTextElement.TextGeometry = agsSoapPoint;
    agsSoapTextElement.Symbol = agsSoapTextSymbol;
    agsSoapTextElement.Scale = true;
    agsSoapTextElement.Text = "COMMAND";

    // If the current resource does not have any custom graphics specified, initialize this property with
    // a new array with just the newly created graphics element.  Otherwise, create a new array with both
    // the current custom graphics and teh newly created element.
    if (agsSoapMapDescription.CustomGraphics != null)
    {
      // Create a new ArcGIS Server graphic element array with a size that is one bigger than the
      // resource's current element array
      int elementCount = agsSoapMapDescription.CustomGraphics.Length;
      ESRI.ArcGIS.ADF.ArcGISServer.GraphicElement[] agsSoapGraphicElementArray = new ESRI.ArcGIS.ADF.ArcGISServer.GraphicElement[elementCount + 1];

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

      // Add the newly created text element to the array
      agsSoapGraphicElementArray[elementCount] = agsSoapTextElement;

      // 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 text element
      ESRI.ArcGIS.ADF.ArcGISServer.GraphicElement[] agsSoapGraphicElement = new ESRI.ArcGIS.ADF.ArcGISServer.GraphicElement[1];
      agsSoapGraphicElement[0] = agsSoapTextElement;

      // Update the resource's element array with the new array
      agsSoapMapDescription.CustomGraphics = agsSoapGraphicElement;
    }

    // Refresh the resource so the new point is shown
    adfMap.RefreshResource(agsMapFunctionality.Resource.Name);
  }

  #endregion

  #region IMapServerToolAction Members

  public void ServerAction(ESRI.ArcGIS.ADF.Web.UI.WebControls.ToolEventArgs toolEventArgs)
  {
    // Get a reference to the Map control on which the tool was executed
    ESRI.ArcGIS.ADF.Web.UI.WebControls.Map adfMap = null;
    adfMap = (ESRI.ArcGIS.ADF.Web.UI.WebControls.Map)toolEventArgs.Control;

    // Get the point clicked by the user
    ESRI.ArcGIS.ADF.Web.UI.WebControls.MapPointEventArgs mapPointEventArgs = (ESRI.ArcGIS.ADF.Web.UI.WebControls.MapPointEventArgs)toolEventArgs;
    ESRI.ArcGIS.ADF.Web.Geometry.Point adfPoint = mapPointEventArgs.MapPoint;

    // Convert the Web ADF input point to its ArcGIS Server specific equivalent
    ESRI.ArcGIS.ADF.ArcGISServer.PointN agsSoapPoint = ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.Converter.FromAdfPoint(adfPoint);

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

    // Get the ArcGIS Server MapDescription for the resource
    ESRI.ArcGIS.ADF.ArcGISServer.MapDescription agsSoapMapDescription = agsMapFunctionality.MapDescription;

    // Create a red ArcGIS Server data source specific color object
    ESRI.ArcGIS.ADF.ArcGISServer.RgbColor agsSoapRgbColor = new ESRI.ArcGIS.ADF.ArcGISServer.RgbColor();
    agsSoapRgbColor.Red = 255;
    agsSoapRgbColor.Green = 0;
    agsSoapRgbColor.Blue = 0;
    agsSoapRgbColor.AlphaValue = 255;

    // Initialize an ArcGIS Server data source specific text symbol
    ESRI.ArcGIS.ADF.ArcGISServer.TextSymbol agsSoapTextSymbol = new ESRI.ArcGIS.ADF.ArcGISServer.TextSymbol();
    agsSoapTextSymbol.Color = agsSoapRgbColor;
    agsSoapTextSymbol.Size = 10;
    agsSoapTextSymbol.FontName = "Tahoma";

    // Create an ArcGIS Server data source specific text element with the text symbol, input point, and the
    // text "TOOL"
    ESRI.ArcGIS.ADF.ArcGISServer.TextElement agsSoapTextElement = new ESRI.ArcGIS.ADF.ArcGISServer.TextElement();
    agsSoapTextElement.TextGeometry = agsSoapPoint;
    agsSoapTextElement.Symbol = agsSoapTextSymbol;
    agsSoapTextElement.Scale = true;
    agsSoapTextElement.Text = "TOOL";

    // If the current resource does not have any custom graphics specified, initialize this property with
    // a new array with just the newly created graphics element.  Otherwise, create a new array with both
    // the current custom graphics and teh newly created element.
    if (agsSoapMapDescription.CustomGraphics != null)
    {
      // Create a new ArcGIS Server graphic element array with a size that is one bigger than the
      // resource's current element array
      int elementCount = agsSoapMapDescription.CustomGraphics.Length;
      ESRI.ArcGIS.ADF.ArcGISServer.GraphicElement[] agsSoapGraphicElementArray = new ESRI.ArcGIS.ADF.ArcGISServer.GraphicElement[elementCount + 1];

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

      // Add the newly created text element to the array
      agsSoapGraphicElementArray[elementCount] = agsSoapTextElement;

      // 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 text element
      ESRI.ArcGIS.ADF.ArcGISServer.GraphicElement[] agsSoapGraphicElement = new ESRI.ArcGIS.ADF.ArcGISServer.GraphicElement[1];
      agsSoapGraphicElement[0] = agsSoapTextElement;

      // Update the resource's element array with the new array
      agsSoapMapDescription.CustomGraphics = agsSoapGraphicElement;
    }

    // Refresh the resource so the new point is shown
    adfMap.RefreshResource(agsMapFunctionality.Resource.Name);
  }

  #endregion
}