ArcGIS ClipShip Geoprocessing
ArcGIS_ClipShip_Geoprocessing_CSharp\App_Code\PolylineTool.cs
// Copyright 2008 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.
// 

public class PolylineTool : ESRI.ArcGIS.ADF.Web.UI.WebControls.Tools.IMapServerToolAction
{
    #region IMapServerToolAction Members

    void ESRI.ArcGIS.ADF.Web.UI.WebControls.Tools.IMapServerToolAction.ServerAction(
        ESRI.ArcGIS.ADF.Web.UI.WebControls.ToolEventArgs toolEventArgs)
    {
        // Get the map control on which the tool was executed
        ESRI.ArcGIS.ADF.Web.UI.WebControls.Map adfMap =
            (ESRI.ArcGIS.ADF.Web.UI.WebControls.Map)toolEventArgs.Control;

        try
        {
            // Get the polyline drawn by the user
            ESRI.ArcGIS.ADF.Web.UI.WebControls.MapPolylineEventArgs mapPolylineEventArgs =
                toolEventArgs as ESRI.ArcGIS.ADF.Web.UI.WebControls.MapPolylineEventArgs;
            ESRI.ArcGIS.ADF.Web.Geometry.Polyline adfPolyline = mapPolylineEventArgs.MapPolyline;

            // Get the name of the graphics resource from session that will contain the 
            // user-drawn polyline
            string graphicsResourceName = (string)adfMap.Page.Session["graphicsResourceName"];

            // Get the graphics map functionality for the resource
            ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapFunctionality graphicsMapFunctionality =
                adfMap.GetFunctionality(graphicsResourceName) as
                ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapFunctionality;

            // Get the name of the graphics layer to which we will add the user-drawn polyline from
            // from session, then use this name to retrieve the graphics layer from the graphics
            // map functionality.
            string polylineGraphicsLayerName = (string)adfMap.Page.Session["polylineGraphicsLayerName"];
            ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer elementGraphicsLayer = 
                graphicsMapFunctionality.GraphicsDataSet.Tables[polylineGraphicsLayerName] as 
                ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer;

            // If the graphics layer was not found, create it
            if (elementGraphicsLayer == null)
            {
                elementGraphicsLayer = new ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer();
                elementGraphicsLayer.TableName = polylineGraphicsLayerName;
                graphicsMapFunctionality.GraphicsDataSet.Tables.Add(elementGraphicsLayer);
            }

            // Uncomment to allow only one polyline to be drawn on the map at a time
            //elementGraphicsLayer.Clear();

            // Create the symbology for the polyline
            ESRI.ArcGIS.ADF.Web.Display.Symbol.SimpleLineSymbol adfSimpleLineSymbol =
                new ESRI.ArcGIS.ADF.Web.Display.Symbol.SimpleLineSymbol();
            adfSimpleLineSymbol.Type = ESRI.ArcGIS.ADF.Web.Display.Symbol.LineType.Solid;
            adfSimpleLineSymbol.Color = System.Drawing.Color.DarkGreen;
            adfSimpleLineSymbol.Width = 1;

            // Create a graphic element based on the polyline and the symbology
            ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicElement graphicElement =
                new ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicElement(adfPolyline, adfSimpleLineSymbol);

            // Add the element to the graphics layer
            elementGraphicsLayer.Add(graphicElement);

            // Refresh the graphics resource so the newly added graphic is displayed
            adfMap.RefreshResource(graphicsResourceName); 
        }
        catch (System.Exception exception)
        {
            ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult errorCallbackResult =
                Utility.CreateErrorCallbackResult(exception);
            adfMap.CallbackResults.Add(errorCallbackResult);
        }
    }

    #endregion
}