ArcFM Engine Developer Guide
DevMapScaleCommand.cs Code

Resource Center Home

Combo Box Control Developer Sample (C#)

DevMapScaleCommand.cs

 

Copy Code
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.esriSystem;
using Miner.Interop;
using Miner.Windows.ArcGIS;
using Miner.Windows.Commands;
namespace Miner.DeveloperSamples.Engine
{
 public class DevMapZoomControl : ComboBoxCommand
 {
  #region Private Declarations
  private const int CONTROL_WIDTH = 90;
  private bool _buildList = true;
  private ListDictionary _mapScaleItems = new ListDictionary();
  bool _addToList = true;
  bool _shouldExecute = false;
  #endregion Private Declarations
  #region Public Constructors/Destructors
  
  //base constructor parameters:  base(key, caption)
  //key = unique identifier
  //caption = caption of control
  public DevMapZoomControl() : base("DevMapZoomControl", "&MapZoomControl")
  {
   this.Width = CONTROL_WIDTH;
   this.DropDownStyle = DropDownStyle.DropDown;
   this.Category = "ArcFM Developer Samples";
   this.Tooltip = "Dev: MapScale";
   this.CustomizerCaption = "Dev: MapScale";
   this.CustomizerDescription = "Dev: Allows the user to set the map scale.";
   BuildListDictionary();
   MainArcEngineApp.CurrentMapChanged +=new MainArcEngineApp.CurrentMapChangedEventHandler(MainArcEngineApp_CurrentMapChanged);
  }
  #endregion Public Constructors/Destructors
  #region Command Overrides
  
  protected override void OnClick(EventArgs args)
  {
   try 
   {
    _shouldExecute = false;
    if (IsEnabled(MainArcEngineApp.FocusMap()) == false) return;
    string itemName = this.Text;    
    if (itemName.Length <= 0) return;
    AddItemToList(itemName, true);
    
    double mapScale = FormatScaleItemDouble(itemName);
    if (mapScale <= 0.0) return;
    IMap currentMap = MainArcEngineApp.FocusMap();
    if (null == currentMap) return;
    currentMap.MapScale = mapScale;
    IActiveView activeView = currentMap as IActiveView;
    activeView.Refresh();
   }
   catch (Exception exc) 
   {
    MessageBox.Show("MapZoomControl::Execute" + exc.ToString());
    return;
   }
  }
  public override bool Enabled
  {
   get
   {
    base.Enabled = IsEnabled(MainArcEngineApp.FocusMap()); 
    return base.Enabled;
   }
   set
   {
    base.Enabled = value;
   }
  }
  protected override void OnActivating(CancelableCommandEventArgs args)
  {
   //Display Message
   try 
   {
    IMMArcGISRuntimeEnvironment pRTE = new Miner.Framework.ArcGISRuntimeEnvironment();
    if (null == pRTE) return;
    
    pRTE.SetStatusBarMessage("Dev: Allows user to set map scale.");
   }
   catch 
   {
    //do nothing; we do not want to fail just because we cannot write 
    //to the status bar
   }
   base.OnActivating (args);
  }
  protected override void OnDeactivated(CommandEventArgs args)
  {
   //Clear Message
   try 
   {
    IMMArcGISRuntimeEnvironment pRTE = new Miner.Framework.ArcGISRuntimeEnvironment();
    if (null == pRTE) return;
    
    pRTE.SetStatusBarMessage("");
   }
   catch 
   {
    //do nothing; we do not want to fail just because we cannot write 
    //to the status bar
   }
   base.OnDeactivated (args);
  }
  protected override void OnDropDownOpening(CancelableCommandEventArgs args)
  {
   BuildDropDownList();
   base.OnDropDownOpening (args);
  }
  protected override void OnKeyPress(System.Windows.Forms.KeyPressEventArgs args)
  {
   //prevent overflow
   try 
   {
    int testScale = Convert.ToInt32(FormatScaleItemDouble(this.Text + Convert.ToString(args.KeyChar)));
   }
   catch (System.OverflowException)
   {
    args.Handled = true;
    return;
   }
   //prevent illegal chars
   try 
   {
    if (args.KeyChar == ':') 
    {
     string currentText = this.Text;
     int indexSeparator = currentText.IndexOf(":");
     if (indexSeparator < 0) 
     {
      if (currentText.Length > 0) 
      {
       char firstChar = currentText[0];
       if (firstChar == 49) 
       {
        return;
       }
      }
      else 
      {
       return;
      }
     }
     else 
     {
      args.Handled = true;
      return;
     }
    }
    //if (!(Char.IsDigit(args.KeyChar)) && !(Char.IsControl(args.KeyChar)) && (args.KeyChar != ':'))
    if (!(Char.IsDigit(args.KeyChar)) && !(Char.IsControl(args.KeyChar)))
    {
     args.Handled = true;
    }
    if (args.KeyChar != 13) 
    {
     _shouldExecute = true;
    }
   }
   catch (Exception exc) 
   {
    MessageBox.Show("MapZoomControl::OnKeyPress" + exc.ToString());
    return;
   }
  }
  protected override void OnTextCommitted(EventArgs args)
  {
   if (_shouldExecute == true) 
   {
    System.Diagnostics.Debug.WriteLine("Executing...");
    OnClick(new EventArgs());
   }
  }

  #endregion Command Overrides
  #region Private Methods
  
  private void BuildListDictionary() 
  {
   try 
   {
    if (null == _mapScaleItems) 
    {
     _mapScaleItems = new ListDictionary();
    }
    _mapScaleItems.Add("1:1000", "1:1,000");
    _mapScaleItems.Add("1:10000", "1:10,000");
    _mapScaleItems.Add("1:24000", "1:24,000");
    _mapScaleItems.Add("1:100000", "1:100,000");
    _mapScaleItems.Add("1:250000", "1:250,000");
    _mapScaleItems.Add("1:500000", "1:500,000");
    _mapScaleItems.Add("1:750000", "1:750,000");
    _mapScaleItems.Add("1:1000000", "1:1,000,000");
    _mapScaleItems.Add("1:3000000", "1:3,000,000");
    _mapScaleItems.Add("1:10000000", "1:10,000,000");
   }
   catch (Exception exc) 
   {
    _buildList = true;
    MessageBox.Show("MapZoomControl::BuildListDictionary" + exc.ToString());
    return;
   }
  }
  
  private void BuildDropDownList() 
  {
   if (_buildList == false) return;
   if (null == _mapScaleItems) return;
   try 
   {
    IDictionary mapScaleDict = _mapScaleItems as IDictionary;
    foreach(DictionaryEntry de in mapScaleDict)
    {
     AddItemToList(Convert.ToString(de.Key), false);
    }
    _buildList = false;
   }
   catch (Exception exc) 
   {
    _buildList = true;
    MessageBox.Show("MapZoomControl::BuildDropDownList" + exc.ToString());
    return;
   }
  }
  private void AddItemToList(string itemName, bool checkExists) 
  {
   try 
   { 
    if (_addToList == false) return;
    string formattedItemName = FormatScaleItemString(itemName);
    if (formattedItemName.Length <= 0) return;
    if (checkExists == true) 
    {
     if (IsInList(formattedItemName) == true) return;
    }
    if (this.Items.Contains(formattedItemName) == false) 
    {
     this.Items.Add(formattedItemName, formattedItemName);
    }
    if (null == _mapScaleItems) 
    {
     _mapScaleItems = new ListDictionary();
    }
    if (_mapScaleItems.Contains(itemName) == false) 
    {
     _mapScaleItems.Add(formattedItemName, formattedItemName);
    }
   }
   catch (Exception exc) 
   {
    MessageBox.Show("MapZoomControl::AddItemToList" + exc.ToString());
    return;
   }
  }
  private bool IsInList(string itemName) 
  {
   if (itemName.Length <= 0) return false;
   if (null == _mapScaleItems) return false;
   
   try 
   {
    bool isInList = _mapScaleItems.Contains(itemName);
    return isInList;
   }
   catch (Exception exc) 
   {
    MessageBox.Show("MapZoomControl::IsInList" + exc.ToString());
    return false;
   }
  }
  private bool IsEnabled(IMap pMap) 
  {
   try 
   {
    bool bEnabled = false;
   
    if (null == pMap) return bEnabled;
   
    //now do the selecting
    UID pUID = new UIDClass();
    if (null == pUID) return bEnabled;
    //UID for IGeoFeatureLayer
    pUID.Value = "{E156D7E5-22AF-11D3-9F99-00C04F6BC78E}";
   
    IEnumLayer pEnumLayers = null;
    try 
    {
     pEnumLayers = pMap.get_Layers(pUID, true);
    }
    catch 
    {
     return bEnabled;
    }
    if (null == pEnumLayers) return bEnabled;
    pEnumLayers.Reset();
    IFeatureLayer pFLayer = null;   
    ILayer pLayer = pEnumLayers.Next();
    while (null != pLayer) 
    {
     pFLayer = pLayer as IFeatureLayer;
     if (null != pFLayer) 
     {
      bEnabled = true;
      break;
     }
     pLayer = pEnumLayers.Next();
    }
    return bEnabled;
   }
   catch(Exception exc) 
   {
    MessageBox.Show("MapZoomControl::IsEnabled" + exc.ToString());
    return false;
   }
  }
  private double FormatScaleItemDouble(string itemName)
  {
   if (itemName.Length <= 0) return -1;
   try 
   {
    string formattedString = "";
    string delimStr = ":";
    char [] delimiter = delimStr.ToCharArray();
    string [] splitItemName = null;
    splitItemName = itemName.Split(delimiter);
    if (splitItemName.Length == 1) 
    {
     formattedString = itemName;
    }
    else 
    {
     formattedString = splitItemName[1];
    }
    double formattedScale = -1.0;
    try 
    {
     formattedScale = Convert.ToDouble(formattedString);
    }
    catch 
    {
     formattedScale = -1.0;
    }
    return formattedScale;
   }
   catch (Exception exc) 
   {
    MessageBox.Show("MapZoomControl::FormatScaleItemDouble" + exc.ToString());
    return -1;
   }
  }
  private string FormatScaleItemString(string itemName)
  {
   if (itemName.Length <= 0) return "";
   try 
   {
    double testScale = FormatScaleItemDouble(itemName);
    if (testScale <= 0) return "";
    string formattedString = "";
    string delimStr = ":";
    char [] delimiter = delimStr.ToCharArray();
    string [] splitItemName = null;
    splitItemName = itemName.Split(delimiter);
    if (splitItemName.Length == 2) 
    {
     if (splitItemName[0].Length > 0) 
     {
      formattedString = itemName;
     }
     else 
     {
      formattedString = "1:" + splitItemName[1];
     }
    }
    else 
    {
     formattedString = "1:" + itemName;
    }
    return formattedString;
   }
   catch (Exception exc) 
   {
    MessageBox.Show("MapZoomControl::FormatScaleItemString" + exc.ToString());
    return "";
   }
  }
    #endregion Private Methods
  #region Event Handlers
  private void MainArcEngineApp_CurrentMapChanged(object send, Miner.Windows.ArcGIS.CurrentMapChangedEventArgs e)
  {
   //_buildList = true;
   this.Text = "";
   bool checkEnabledStatus = Enabled;
   
   if (checkEnabledStatus == false) return;
   IMap currentMap = MainArcEngineApp.FocusMap();
   if (null == currentMap) return;
    
   IActiveView activeView = currentMap as IActiveView;
   if (null == activeView) return;

   IActiveViewEvents_Event _activeViewEvents = activeView as IActiveViewEvents_Event;
   _activeViewEvents.AfterDraw +=new IActiveViewEvents_AfterDrawEventHandler(activeViewEvents_AfterDraw);
  }
  private void activeViewEvents_AfterDraw(ESRI.ArcGIS.Display.IDisplay Display, esriViewDrawPhase phase)
  {
   try 
   {
    _addToList = false;
    IMap currentMap = MainArcEngineApp.FocusMap();
    if (null == currentMap) return;
    try //this is necessary when opening a new tab
    {
     double dummy = currentMap.MapScale;
    }
    catch 
    {
     return;
    }
    string formattedMapScale = FormatScaleItemString(Convert.ToString(Convert.ToInt32(currentMap.MapScale)));
    try 
    {
     this.Text = formattedMapScale;
    }
    catch 
    {
     return;
    }
   }
   catch (Exception exc) 
   {
    MessageBox.Show("MapZoomControl::activeViewEvents_AfterDraw" + exc.ToString());
    return;
   }
   finally 
   {
    _addToList = true;
   }
  }

  #endregion
 }
}

 

 


Send Comment to ArcFMdocumentation@schneider-electric.com