ArcGIS Simple Server Object Extension
ArcGIS_Simple_Server_Object_Extension_CSharp\SimpleSOEWebApp_CSharp\App_Code\SOETool.cs
// Copyright 2009 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.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ESRI.ArcGIS.ADF.Web.UI.WebControls.Tools;
using ESRI.ArcGIS.ADF.Web.UI.WebControls;
using ESRI.ArcGIS.ADF.Web.DataSources;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Server;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Carto;
using System.Collections;
using SimpleSOEInterfaces_CSharp;

public class SOETool: IMapServerToolAction
{

    #region IMapServerToolAction Members

    public void ServerAction(ToolEventArgs args)
    {
        ESRI.ArcGIS.ADF.Web.UI.WebControls.Map mapctrl = (ESRI.ArcGIS.ADF.Web.UI.WebControls.Map)args.Control;

        PointEventArgs pntargs = (PointEventArgs)args;
        System.Drawing.Point mypnt = pntargs.ScreenPoint;

        ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapFunctionality mf = (ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapFunctionality)mapctrl.GetFunctionality(mapctrl.MapResourceManagerInstance.ResourceItems.Count - 1);
        ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceLocal mr = (ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceLocal) mf.MapResource;
        IServerContext sc = mr.ServerContextInfo.ServerContext;
        IMapServer ms = mr.MapServer;

        IImageDisplay imageDisp = (ImageDisplay)sc.CreateObject("esriCarto.ImageDisplay");
        imageDisp.Height = (int)mapctrl.Height.Value;
        imageDisp.Width = (int)mapctrl.Width.Value;
        imageDisp.DeviceResolution = mf.DisplaySettings.ImageDescriptor.Dpi;

        ESRI.ArcGIS.Carto.MapDescription md = (ESRI.ArcGIS.Carto.MapDescription)ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.Converter.ValueObjectToComObject(mf.MapDescription, sc);
        IPoint pnt = ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.Converter.ToMapPoint(sc, ms, md, imageDisp, mypnt.X, mypnt.Y);

        // Server Object Extension client code
        IServerObjectExtensionManager soexm = (IServerObjectExtensionManager)ms;
        IServerObjectExtension soext = soexm.FindExtensionByTypeName("SimpleSOE_CSharp");
        IUtilSOE_CSharp utilsoe = (IUtilSOE_CSharp)soext;

        double radius = mapctrl.Extent.Width * 0.1;
        IPolygon circle = (IPolygon)utilsoe.ReturnCircle(ref pnt, ref radius);
        circle.Densify(0.1, 0.01);

        IEnumerable gfc = mapctrl.GetFunctionalities();
        ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapResource gResource = null;
        foreach (IGISFunctionality gfunc in gfc)
        {
            if (gfunc.Resource is ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapResource)
            {
                gResource = (ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapResource)gfunc.Resource;
                break;
            }
        }

        if (gResource == null)
            return;

        ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer glayer = null;

        foreach (System.Data.DataTable dt in gResource.Graphics.Tables)
        {
            if (dt is ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer)
            {
                glayer = (ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer)dt;
                break;
            }
        }

        if (glayer == null)
        {
            glayer = new ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer();
            gResource.Graphics.Tables.Add(glayer);
        }
        glayer.Clear();

        ESRI.ArcGIS.ADF.Web.Geometry.Point[] adf_parray = ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.Converter.FromIPointCollection((ESRI.ArcGIS.Geometry.IPointCollection)circle);
        ESRI.ArcGIS.ADF.Web.Geometry.PointCollection adf_pc = new ESRI.ArcGIS.ADF.Web.Geometry.PointCollection();
        for (int i = 0; i < adf_parray.Length - 1; i++)
        {
            adf_pc.Add(adf_parray[i]);
        }
        ESRI.ArcGIS.ADF.Web.Geometry.Ring adf_ring = new ESRI.ArcGIS.ADF.Web.Geometry.Ring();
        adf_ring.Points = adf_pc;
        ESRI.ArcGIS.ADF.Web.Geometry.RingCollection adf_rcol = new ESRI.ArcGIS.ADF.Web.Geometry.RingCollection();
        adf_rcol.Add(adf_ring);
        ESRI.ArcGIS.ADF.Web.Geometry.Polygon adf_pg = new ESRI.ArcGIS.ADF.Web.Geometry.Polygon();
        adf_pg.Rings = adf_rcol;

        ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicElement ge = new ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicElement(adf_pg, System.Drawing.Color.Yellow);
        ge.Symbol.Transparency = 70.0;
        glayer.Add(ge);

        if (mapctrl.ImageBlendingMode == ImageBlendingMode.WebTier)
        { mapctrl.Refresh(); }
        else if (mapctrl.ImageBlendingMode == ImageBlendingMode.Browser)
        { mapctrl.RefreshResource(gResource.Name); }
    }

    #endregion
}