Tuesday, March 10, 2015

ArcMap tool for Panning and Zooming while editing using C#

1 Comment
Draw features in ArcMap using ArcGIS Addin tool (Arcobject's IRubberband Interface) doesn't provide ways to zoom and pan while editing. In order to implement pan and zoom functionality during edit use IDisplayFeedback Interface. Following is  the code snippets using IDisplayFeedback Interface using C#. Then, you can use ArcMap standard keys to pan (arrows-left, right, top, bottom) and zoom (mouse-scroll or Z & X).
     protected override void OnUpdate()  
     {  
       Enabled = ArcMap.Application != null;  
     }  
     protected override void OnMouseDown(ESRI.ArcGIS.Desktop.AddIns.Tool.MouseEventArgs arg)  
     {  
       DrawGeometryOnMousedown(ArcMap.Document, arg);  
     }  
     protected override void OnMouseMove(MouseEventArgs arg)  
     {  
       DrawGeometryOnMouseMove(ArcMap.Document, arg);  
     }  
     protected override void OnDoubleClick()  
     {  
       FinishGeometryDrawOnDblClick(ArcMap.Document);  
     }  
     protected override void OnRefresh(int hDC)  
     {  
       if (newPolygonFeedback != null)  
       {  
         newPolygonFeedback.Refresh(hDC);  
       }  
     }  
     #region private helpers  
     //Polygon geometry object  
     private INewPolygonFeedback newPolygonFeedback;  
     private void DrawGeometryOnMousedown(IMxDocument doc, ESRI.ArcGIS.Desktop.AddIns.Tool.MouseEventArgs arg)  
     {  
       // Get the ScreenDisplay for the the ActiveView  
       IDisplay display = doc.ActiveView.ScreenDisplay;  
       // Get the current mouse location in Map Units  
       IPoint point = display.DisplayTransformation.ToMapPoint(arg.X, arg.Y);  
       // Check that user is not using an existing feedback  
       if ((newPolygonFeedback == null))  
       {  
         // Create a new Feedback  
         newPolygonFeedback = new NewPolygonFeedback();  
         // Get the Feedback's Symbol by reference (IDisplayFeedback::Symbol)  
         var simpleLineSymbol = newPolygonFeedback.Symbol as ISimpleLineSymbol;  
         // Create a new RGBColor and set it up  
         IRgbColor rgbColor = new RgbColor();  
         rgbColor.Green = 255;  
         rgbColor.Red = 0;  
         rgbColor.Blue = 255;  
         // Set the Color and Style for the Feedback's Symbol  
         if (simpleLineSymbol != null)  
         {  
           simpleLineSymbol.Color = rgbColor;  
           simpleLineSymbol.Style = esriSimpleLineStyle.esriSLSSolid;  
         }  
         // Set the new Feedback's DISPLAY property (IDisplayFeedback::DISPLAY)  
         newPolygonFeedback.Display = (IScreenDisplay)display;  
         // Start the Feedback at the current mouse location  
         newPolygonFeedback.Start(point);  
       }  
       else  
       {  
         // Otherwise use the current mouse location to add a vertex to the current feedback  
         newPolygonFeedback.AddPoint(point);  
       }  
     }  
     private void DrawGeometryOnMouseMove(IMxDocument doc, ESRI.ArcGIS.Desktop.AddIns.Tool.MouseEventArgs arg)  
     {  
       // Get the ScreenDisplay for the the ActiveView  
       IMxDocument pMXDoc = doc;  
       IDisplay pDisp = pMXDoc.ActiveView.ScreenDisplay;  
       // Check if the user is currently using the feedback  
       if (newPolygonFeedback != null)  
       {  
         // Get the current mouse location in map units  
         IPoint pPnt = pDisp.DisplayTransformation.ToMapPoint(arg.X, arg.Y);  
         // Move the Feedback to the current mouse location (IDisplayFeedback::MoveTo)  
         newPolygonFeedback.MoveTo(pPnt);  
       }  
     }  
     private void FinishGeometryDrawOnDblClick(IMxDocument doc)  
     {  
       // Check if the user is currently using the feedback  
       if (newPolygonFeedback != null)  
       {  
         // Stop the feedback and set it to Nothing  
         IGeometry geometry = newPolygonFeedback.Stop();  
         newPolygonFeedback = null;  
         //check for valid geometery  
         if (geometry != null)  
         {  
           IScreenDisplay screenDisplay = doc.ActiveView.ScreenDisplay;  
           // Constants  
           screenDisplay.StartDrawing(screenDisplay.hDC, (System.Int16)esriScreenCache.esriNoScreenCache);  
           // Explicit Cast  
           IRgbColor rgbColor = new RgbColorClass();  
           rgbColor.Red = 255;  
           rgbColor.Transparency = 75;  
           IColor color = rgbColor; // Implicit Cast  
           ISimpleFillSymbol simpleFillSymbol = new SimpleFillSymbolClass();  
           simpleFillSymbol.Color = color;  
           //Draw geometry  
           ISymbol symbol = simpleFillSymbol as ISymbol; // Dynamic Cast  
           screenDisplay.SetSymbol(symbol);  
           screenDisplay.DrawPolygon(geometry);  
           screenDisplay.FinishDrawing();  
         }  
       }  
       ArcMap.Document.ActiveView.Refresh();  
     }  
     #endregion private helpers  

Draw features in ArcMap using ArcGIS Addin tool (Arcobject's IRubberband Interface)

Be The First To Comment
 protected override void OnMouseDown(ESRI.ArcGIS.Desktop.AddIns.Tool.MouseEventArgs arg)  
 {  
      //create geometery  
      if (ArcMap.Document != null)  
      {  
           IScreenDisplay screenDisplay = ArcMap.Document.ActiveView.ScreenDisplay;  
           // Constants  
           screenDisplay.StartDrawing(screenDisplay.hDC, (System.Int16)esriScreenCache.esriNoScreenCache);  
           // Explicit Cast  
           IRgbColor rgbColor = new RgbColorClass();  
           rgbColor.Red = 255;  
           rgbColor.Transparency = 75;  
           IColor color = rgbColor; // Implicit Cast  
           ISimpleFillSymbol simpleFillSymbol = new SimpleFillSymbolClass();  
           simpleFillSymbol.Color = color;  
           //Draw geometry  
           ISymbol symbol = simpleFillSymbol as ISymbol; // Dynamic Cast  
           IRubberBand rubberBand = new RubberPolygonClass();  
           IGeometry geometry = rubberBand.TrackNew(screenDisplay, symbol);  
           //check for valid geometery  
           if (geometry != null)  
           {  
                screenDisplay.SetSymbol(symbol);  
                screenDisplay.DrawPolygon(geometry);  
                screenDisplay.FinishDrawing();  
                //Open addattributes wpf form  
                AddAtrributesView addAtrributesView = new AddAtrributesView(geometry);  
                addAtrributesView.ShowInTaskbar = false;  
                addAtrributesView.ShowDialog();  
                ArcMap.Document.ActivatedView.Refresh();  
           }  
      }       
 }  
 

© 2011 GIS and Remote Sensing Tools, Tips and more .. ToS | Privacy Policy | Sitemap

About Me