ArcIMS Select Buffer tool
ArcIMS_SelectBufferTool_CSharp\App_Code\SelectionTool.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;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data;
using System.Drawing;

// For ArcMap services, all dynamic feature layers must have null renderers.  
// Renderers are configured in the mxd document.
public class SelectionTool : 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)
    {
        int resourceIndex = 0;
        
        ESRI.ArcGIS.ADF.Web.UI.WebControls.Map adfMap = (ESRI.ArcGIS.ADF.Web.UI.WebControls.Map)toolEventArgs.Control;

        // User provided values included in ESRIWebADFHiddenFields (see Page_PreRender in Default.aspx.cs).  
        // Get the selected value in the active layer drop down list.
        System.Collections.Specialized.NameValueCollection nameValueCollection = null;
        if (adfMap.Page.IsCallback)
        {
            string callbackArgs = adfMap.Page.Request.Params["__CALLBACKPARAM"];
            nameValueCollection = ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackUtility.
                ParseStringIntoNameValueCollection(callbackArgs);
        }
        else // if full page postback
        {
            nameValueCollection = adfMap.Page.Request.Params;
        }

        string activeLayerName = nameValueCollection["activeLayerDropDownList"];

        // Convert user entered rectangle from screen units to map units
        ESRI.ArcGIS.ADF.Web.UI.WebControls.RectangleEventArgs rectagleEventArgs =
            (ESRI.ArcGIS.ADF.Web.UI.WebControls.RectangleEventArgs)toolEventArgs;
        System.Drawing.Rectangle screenRectangle = rectagleEventArgs.ScreenExtent;

        ESRI.ArcGIS.ADF.IMS.Geometry.Envelope imsExtentEnvelope = (ESRI.ArcGIS.ADF.IMS.Geometry.Envelope)
            ESRI.ArcGIS.ADF.Web.DataSources.IMS.Converter.ToIMSGeometry(adfMap.Extent);

        ESRI.ArcGIS.ADF.IMS.Geometry.Point imsMinimumPoint = ESRI.ArcGIS.ADF.IMS.Geometry.Point.ToMapPoint
            (screenRectangle.Left, screenRectangle.Bottom,
            imsExtentEnvelope, adfMap.TilingScheme.TileWidth, adfMap.TilingScheme.TileHeight);
        ESRI.ArcGIS.ADF.IMS.Geometry.Point imsMaximumPoint = ESRI.ArcGIS.ADF.IMS.Geometry.Point.ToMapPoint
            (screenRectangle.Right, screenRectangle.Top,
            imsExtentEnvelope, adfMap.TilingScheme.TileWidth, adfMap.TilingScheme.TileHeight);

        ESRI.ArcGIS.ADF.IMS.Geometry.Envelope imsEnvelope = new ESRI.ArcGIS.ADF.IMS.Geometry.Envelope(imsMinimumPoint, 
            imsMaximumPoint);

        ESRI.ArcGIS.ADF.Web.DataSources.IMS.MapFunctionality imsMapFunctionality =
            (ESRI.ArcGIS.ADF.Web.DataSources.IMS.MapFunctionality)adfMap.GetFunctionality(resourceIndex);

        ESRI.ArcGIS.ADF.IMS.Carto.MapView mapview = imsMapFunctionality.MapView;

        // Remove all dynamic layers.  Removes all previous selections, buffers, and graphics.
        foreach (string layerName in LayerNames.GetLayerNames())
        {
            ESRI.ArcGIS.ADF.IMS.Carto.Layer.Layer layer = mapview.Layers.FindByName(layerName);
            if (layer != null)
            {
                mapview.Layers.Remove(layer);
            }
        }

        // Define a filter based on user entered selection rectangle.  
        // Store it in session to be used by logic elsewhere in the app (e.g. in Default.aspx.cs).
        ESRI.ArcGIS.ADF.IMS.Carto.Layer.FeatureLayer activeFeatureLayer = 
            (ESRI.ArcGIS.ADF.IMS.Carto.Layer.FeatureLayer)mapview.Layers.FindByName(activeLayerName);
        ESRI.ArcGIS.ADF.IMS.Carto.Layer.Filter filter = new ESRI.ArcGIS.ADF.IMS.Carto.Layer.Filter();
        filter.Geometry = imsEnvelope;

        adfMap.Page.Session["activeLayerFilter"] = filter;

        // Create a dynamic selection layer.  If ArcMap service, set the feature renderer to null.  If not, 
        // define the renderer here.
        ESRI.ArcGIS.ADF.IMS.Carto.Layer.FeatureLayer selectionFeatureLayer = null;

        if (imsMapFunctionality.MapResource.MapService.Type == ESRI.ArcGIS.ADF.IMS.ServiceType.ArcMapServer)
        {
            selectionFeatureLayer = activeFeatureLayer.CreateSelectionLayer
                (filter, null, LayerNames.SelectionToolActive);
        }
        else
        {
            ESRI.ArcGIS.ADF.IMS.Display.Renderer.SimpleRenderer activeLayerSimpleRenderer =
            new ESRI.ArcGIS.ADF.IMS.Display.Renderer.SimpleRenderer();
            ESRI.ArcGIS.ADF.IMS.Display.Symbol.FeatureSymbol activeLayerFeatureSymbol = null;

            ESRI.ArcGIS.ADF.IMS.FeatureType imsFeatureType = activeFeatureLayer.Type;
            if (imsFeatureType == ESRI.ArcGIS.ADF.IMS.FeatureType.Point)
            {
                ESRI.ArcGIS.ADF.IMS.Display.Symbol.SimpleMarkerSymbol activeLayerSimpleMarkerSymbol =
                    new ESRI.ArcGIS.ADF.IMS.Display.Symbol.SimpleMarkerSymbol();
                activeLayerSimpleMarkerSymbol.Color = System.Drawing.Color.Yellow;
                activeLayerSimpleMarkerSymbol.Width = 12;
                activeLayerFeatureSymbol = activeLayerSimpleMarkerSymbol;
            }
            else if (imsFeatureType == ESRI.ArcGIS.ADF.IMS.FeatureType.Line)
            {
                ESRI.ArcGIS.ADF.IMS.Display.Symbol.SimpleLineSymbol activeLayerSimpleLineSymbol =
                    new ESRI.ArcGIS.ADF.IMS.Display.Symbol.SimpleLineSymbol();
                activeLayerSimpleLineSymbol.Width = 2;
                activeLayerSimpleLineSymbol.Color = System.Drawing.Color.Yellow;
                activeLayerFeatureSymbol = activeLayerSimpleLineSymbol;
            }
            else if (imsFeatureType == ESRI.ArcGIS.ADF.IMS.FeatureType.Polygon)
            {
                ESRI.ArcGIS.ADF.IMS.Display.Symbol.SimpleFillSymbol activeLayerSimpleFillSymbol =
                    new ESRI.ArcGIS.ADF.IMS.Display.Symbol.SimpleFillSymbol();
                activeLayerSimpleFillSymbol.Color = System.Drawing.Color.Yellow;
                activeLayerFeatureSymbol = activeLayerSimpleFillSymbol;
            }

            if (activeLayerFeatureSymbol != null)
            {
                activeLayerFeatureSymbol.Transparency = 50.0;
                activeLayerSimpleRenderer.Symbol = activeLayerFeatureSymbol;
            }

            selectionFeatureLayer = activeFeatureLayer.CreateSelectionLayer(filter, activeLayerSimpleRenderer, LayerNames.SelectionToolActive);
        }

        selectionFeatureLayer.Name = LayerNames.SelectionToolActive;

        // *** Add dynamic selection layer to the map
        mapview.Layers.Add(selectionFeatureLayer);

        if (adfMap.ImageBlendingMode == ESRI.ArcGIS.ADF.Web.UI.WebControls.ImageBlendingMode.Browser)
        {
            adfMap.RefreshResource(imsMapFunctionality.Resource.Name);
        }
        else
        {
            adfMap.Refresh();
        }
    }

    #endregion
}