ArcGIS Simple Server Object Extension
ArcGIS_Simple_Server_Object_Extension_VBNet\SimpleSOEWebApp_VBNet\App_Code\SOETool.vb
' 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.
' 

Imports Microsoft.VisualBasic
Imports System
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports ESRI.ArcGIS.ADF.Web.UI.WebControls.Tools
Imports ESRI.ArcGIS.ADF.Web.UI.WebControls
Imports ESRI.ArcGIS.ADF.Web.DataSources
Imports ESRI.ArcGIS.esriSystem
Imports ESRI.ArcGIS.Server
Imports ESRI.ArcGIS.Geometry
Imports ESRI.ArcGIS.Display
Imports ESRI.ArcGIS.Carto
Imports System.Collections
Imports SimpleSOEInterfaces_VBNet

Public Class SOETool
  Implements IMapServerToolAction

  #Region "IMapServerToolAction Members"

    Public Sub ServerAction(ByVal args As ToolEventArgs) Implements IMapServerToolAction.ServerAction
        Dim mapctrl As ESRI.ArcGIS.ADF.Web.UI.WebControls.Map = CType(args.Control, ESRI.ArcGIS.ADF.Web.UI.WebControls.Map)

        Dim pntargs As PointEventArgs = CType(args, PointEventArgs)
        Dim mypnt As System.Drawing.Point = pntargs.ScreenPoint

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

        Dim imageDisp As IImageDisplay = CType(sc.CreateObject("esriCarto.ImageDisplay"), ImageDisplay)
        imageDisp.Height = CInt(Fix(mapctrl.Height.Value))
        imageDisp.Width = CInt(Fix(mapctrl.Width.Value))
        imageDisp.DeviceResolution = mf.DisplaySettings.ImageDescriptor.Dpi

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

        ' Server Object Extension client code
        Dim soexm As IServerObjectExtensionManager = CType(ms, IServerObjectExtensionManager)
        Dim soext As IServerObjectExtension = soexm.FindExtensionByTypeName("SimpleSOE_VBNet")
        Dim utilsoe As IUtilSOE_VBNet = CType(soext, IUtilSOE_VBNet)

        Dim radius As Double = mapctrl.Extent.Width * 0.1
        Dim circle As IPolygon = CType(utilsoe.ReturnCircle(pnt, radius), IPolygon)
        circle.Densify(0.1, 0.01)

        Dim gfc As IEnumerable = mapctrl.GetFunctionalities()
        Dim gResource As ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapResource = Nothing
        For Each gfunc As IGISFunctionality In gfc
            If TypeOf gfunc.Resource Is ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapResource Then
                gResource = CType(gfunc.Resource, ESRI.ArcGIS.ADF.Web.DataSources.Graphics.MapResource)
                Exit For
            End If
        Next gfunc

        If gResource Is Nothing Then
            Return
        End If

        Dim glayer As ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer = Nothing

        For Each dt As System.Data.DataTable In gResource.Graphics.Tables
            If TypeOf dt Is ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer Then
                glayer = CType(dt, ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer)
                Exit For
            End If
        Next dt

        If glayer Is Nothing Then
            glayer = New ESRI.ArcGIS.ADF.Web.Display.Graphics.ElementGraphicsLayer()
            gResource.Graphics.Tables.Add(glayer)
        End If
        glayer.Clear()

        Dim adf_parray() As ESRI.ArcGIS.ADF.Web.Geometry.Point = ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.Converter.FromIPointCollection(CType(circle, ESRI.ArcGIS.Geometry.IPointCollection))
        Dim adf_pc As New ESRI.ArcGIS.ADF.Web.Geometry.PointCollection()
        For i As Integer = 0 To adf_parray.Length - 2
            adf_pc.Add(adf_parray(i))
        Next i
        Dim adf_ring As New ESRI.ArcGIS.ADF.Web.Geometry.Ring()
        adf_ring.Points = adf_pc
        Dim adf_rcol As New ESRI.ArcGIS.ADF.Web.Geometry.RingCollection()
        adf_rcol.Add(adf_ring)
        Dim adf_pg As New ESRI.ArcGIS.ADF.Web.Geometry.Polygon()
        adf_pg.Rings = adf_rcol

        Dim ge As 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 Then
            mapctrl.Refresh()
        ElseIf mapctrl.ImageBlendingMode = ImageBlendingMode.Browser Then
            mapctrl.RefreshResource(gResource.Name)
        End If
    End Sub

  #End Region
End Class