0

I am using the sample code (draw a polygon on the screen) provided by Esri but it seems I cannot start the drawing from a location I want, every time it starts at wherever the function(or tool) located on map.

Anybody know how to draw a polygon as desired? Any example there?

  • The sample code you refer to is for a Tool so the first click of the mouse defines the origin. How were you expecting to define the location you want? Clicking on a map and drawing it or entering the XY coordinates? – Hornbydd Jan 24 '15 at 1:23
  • Hello Hornbydd, thanks for the comment. I want to click on the map to define the location. – Maple Li Jan 26 '15 at 1:55
1

Here's something I put together from the link you posted and another link that you will see commented out in the code. When you click on the map it will draw a square 10x10 map units from where you clicked. I work in metres so this draws a reasonably sized graphic.

Create an add-in with a tool and add this to the class.

protected override void OnMouseDown(MouseEventArgs arg)
    {

        IMap _map = ArcMap.Document.FocusMap;
        IScreenDisplay3 _display = (IScreenDisplay3)ArcMap.Document.ActiveView.ScreenDisplay;
        ESRI.ArcGIS.Geometry.IPoint _clickedPoint = _display.DisplayTransformation.ToMapPoint(arg.Location.X, arg.Location.Y);

        ESRI.ArcGIS.Display.IRgbColor rgbColor = new ESRI.ArcGIS.Display.RgbColorClass();
        rgbColor.Red = 255;

        ESRI.ArcGIS.Display.IColor color = rgbColor; // Implicit cast.
        ESRI.ArcGIS.Display.ISimpleFillSymbol simpleFillSymbol = new
            ESRI.ArcGIS.Display.SimpleFillSymbolClass();
        simpleFillSymbol.Color = color;

        ESRI.ArcGIS.Display.ISymbol symbol = simpleFillSymbol as
            ESRI.ArcGIS.Display.ISymbol; // Dynamic cast.


        ////http://help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/index.html#//0001000002wm000000

        ESRI.ArcGIS.Geometry.IPointCollection4 _poly = (ESRI.ArcGIS.Geometry.IPointCollection4)(new ESRI.ArcGIS.Geometry.Polygon());
        _poly.AddPoint(_clickedPoint, Type.Missing, Type.Missing);

        ESRI.ArcGIS.Geometry.IPoint _pnt = new ESRI.ArcGIS.Geometry.Point();
        _pnt.PutCoords(_clickedPoint.X + 10, _clickedPoint.Y);
        _poly.AddPoint(_pnt, Type.Missing, Type.Missing);
        _pnt.PutCoords(_clickedPoint.X + 10, _clickedPoint.Y + 10);
        _poly.AddPoint(_pnt, Type.Missing, Type.Missing);
        _pnt.PutCoords(_clickedPoint.X, _clickedPoint.Y + 10 );
        _poly.AddPoint(_pnt, Type.Missing, Type.Missing);

        _poly.AddPoint(_clickedPoint, Type.Missing, Type.Missing);            _display.StartDrawing(_display.hDC, (System.Int16)ESRI.ArcGIS.Display.esriScreenCache.esriNoScreenCache);
        _display.SetSymbol(symbol);
        _display.DrawPolygon(((ESRI.ArcGIS.Geometry.IPolygon)_poly));
        _display.FinishDrawing();



            base.OnMouseDown(arg);
    }
  • I got 2 errors by using the code: (1) 'OnMouseDown' is not a member of 'ESRI.ArcGIS.Desktop.AddIns.Button'. (2)sub 'OnMouseDown' cannot be declared 'Overrides' because it does not override a sub in a base class. Is there anything wrong with my code? thank you. – Maple Li Jan 28 '15 at 2:36
  • You need to create a tool control. Tools allow you to interact with the map. Like the identify or selection tool. – M.D. Jan 28 '15 at 13:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.