ArcIMS Select Buffer tool
ArcIMS_SelectBufferTool_VBNet\App_Code\SelectionTool.vb
' 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.
' 




Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports System.Data
Imports System.Drawing

' For ArcMap services, all dynamic feature layers must have null renderers.  
' Renderers are configured in the mxd document.
Public Class SelectionTool
    Implements ESRI.ArcGIS.ADF.Web.UI.WebControls.Tools.IMapServerToolAction
  #Region "IMapServerToolAction Members"

  Private Sub ServerAction(ByVal toolEventArgs As ESRI.ArcGIS.ADF.Web.UI.WebControls.ToolEventArgs) Implements ESRI.ArcGIS.ADF.Web.UI.WebControls.Tools.IMapServerToolAction.ServerAction
    Dim resourceIndex As Integer = 0

    Dim adfMap As ESRI.ArcGIS.ADF.Web.UI.WebControls.Map = CType(toolEventArgs.Control, ESRI.ArcGIS.ADF.Web.UI.WebControls.Map)

    ' User provided values included in ESRIWebADFHiddenFields (see Page_PreRender in Default.aspx.cs).  
    ' Get the selected value in the active layer drop down list.
    Dim nameValueCollection As System.Collections.Specialized.NameValueCollection = Nothing
    If adfMap.Page.IsCallback Then
      Dim callbackArgs As String = 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
    End If

    Dim activeLayerName As String = nameValueCollection("activeLayerDropDownList")

    ' Convert user entered rectangle from screen units to map units
    Dim rectagleEventArgs As ESRI.ArcGIS.ADF.Web.UI.WebControls.RectangleEventArgs = CType(toolEventArgs, ESRI.ArcGIS.ADF.Web.UI.WebControls.RectangleEventArgs)
    Dim screenRectangle As System.Drawing.Rectangle = rectagleEventArgs.ScreenExtent

    Dim imsExtentEnvelope As ESRI.ArcGIS.ADF.IMS.Geometry.Envelope = CType(ESRI.ArcGIS.ADF.Web.DataSources.IMS.Converter.ToIMSGeometry(adfMap.Extent), ESRI.ArcGIS.ADF.IMS.Geometry.Envelope)

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

    Dim imsEnvelope As ESRI.ArcGIS.ADF.IMS.Geometry.Envelope = New ESRI.ArcGIS.ADF.IMS.Geometry.Envelope(imsMinimumPoint, imsMaximumPoint)

    Dim imsMapFunctionality As ESRI.ArcGIS.ADF.Web.DataSources.IMS.MapFunctionality = CType(adfMap.GetFunctionality(resourceIndex), ESRI.ArcGIS.ADF.Web.DataSources.IMS.MapFunctionality)

    Dim mapview As ESRI.ArcGIS.ADF.IMS.Carto.MapView = imsMapFunctionality.MapView

    ' Remove all dynamic layers.  Removes all previous selections, buffers, and graphics.
        For Each layerName As String In New LayerNames()
            Dim layer As ESRI.ArcGIS.ADF.IMS.Carto.Layer.Layer = mapview.Layers.FindByName(layerName)
            If Not layer Is Nothing Then
                mapview.Layers.Remove(layer)
            End If
        Next layerName

    ' 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).
    Dim activeFeatureLayer As ESRI.ArcGIS.ADF.IMS.Carto.Layer.FeatureLayer = CType(mapview.Layers.FindByName(activeLayerName), ESRI.ArcGIS.ADF.IMS.Carto.Layer.FeatureLayer)
    Dim filter As ESRI.ArcGIS.ADF.IMS.Carto.Layer.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.
    Dim selectionFeatureLayer As ESRI.ArcGIS.ADF.IMS.Carto.Layer.FeatureLayer = Nothing

    If imsMapFunctionality.MapResource.MapService.Type = ESRI.ArcGIS.ADF.IMS.ServiceType.ArcMapServer Then
      selectionFeatureLayer = activeFeatureLayer.CreateSelectionLayer (filter, Nothing, LayerNames.SelectionToolActive)
    Else
      Dim activeLayerSimpleRenderer As ESRI.ArcGIS.ADF.IMS.Display.Renderer.SimpleRenderer = New ESRI.ArcGIS.ADF.IMS.Display.Renderer.SimpleRenderer()
      Dim activeLayerFeatureSymbol As ESRI.ArcGIS.ADF.IMS.Display.Symbol.FeatureSymbol = Nothing

      Dim imsFeatureType As ESRI.ArcGIS.ADF.IMS.FeatureType = activeFeatureLayer.Type
            If imsFeatureType = ESRI.ArcGIS.ADF.IMS.FeatureType.Point Then
                Dim activeLayerSimpleMarkerSymbol As ESRI.ArcGIS.ADF.IMS.Display.Symbol.SimpleMarkerSymbol = New ESRI.ArcGIS.ADF.IMS.Display.Symbol.SimpleMarkerSymbol()
                activeLayerSimpleMarkerSymbol.Color = System.Drawing.Color.Yellow
                activeLayerSimpleMarkerSymbol.Width = 12
                activeLayerFeatureSymbol = activeLayerSimpleMarkerSymbol
            ElseIf imsFeatureType = ESRI.ArcGIS.ADF.IMS.FeatureType.Line Then
                Dim activeLayerSimpleLineSymbol As ESRI.ArcGIS.ADF.IMS.Display.Symbol.SimpleLineSymbol = New ESRI.ArcGIS.ADF.IMS.Display.Symbol.SimpleLineSymbol()
                activeLayerSimpleLineSymbol.Width = 2
                activeLayerSimpleLineSymbol.Color = System.Drawing.Color.Yellow
                activeLayerFeatureSymbol = activeLayerSimpleLineSymbol
            ElseIf imsFeatureType = ESRI.ArcGIS.ADF.IMS.FeatureType.Polygon Then
                Dim activeLayerSimpleFillSymbol As ESRI.ArcGIS.ADF.IMS.Display.Symbol.SimpleFillSymbol = New ESRI.ArcGIS.ADF.IMS.Display.Symbol.SimpleFillSymbol()
                activeLayerSimpleFillSymbol.Color = System.Drawing.Color.Yellow
                activeLayerFeatureSymbol = activeLayerSimpleFillSymbol
            End If

      If Not activeLayerFeatureSymbol Is Nothing Then
        activeLayerFeatureSymbol.Transparency = 50.0
        activeLayerSimpleRenderer.Symbol = activeLayerFeatureSymbol
      End If

      selectionFeatureLayer = activeFeatureLayer.CreateSelectionLayer(filter, activeLayerSimpleRenderer, LayerNames.SelectionToolActive)
    End If

    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 Then
      adfMap.RefreshResource(imsMapFunctionality.Resource.Name)
    Else
      adfMap.Refresh()
    End If
  End Sub

  #End Region
End Class