Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I am trying to get the user to draw a line(only 2 vertices) on the screen(just like the georeferencing link/spatial adjustment link). I got the below snippet from ESRI website which does give me a polyline but it requires the user to double click to finish the sketch.

public IPolyline GetPolylineFromMouseClicks(IActiveView activeView)
        {

          IScreenDisplay screenDisplay = activeView.ScreenDisplay;

          IRubberBand rubberBand = new RubberLineClass();
          IGeometry geometry = rubberBand.TrackNew(screenDisplay, null);

          IPolyline polyline = (IPolyline)geometry;

          return polyline;

        }

I would like the tool to finish the drawing as soon as two points are clicked, thus avoiding a double click. Can somebody help me on this? I seem to be making a mess of a simple situation!

share|improve this question
add comment

1 Answer 1

up vote 3 down vote accepted

I don't think you can use the IRubberBand interface. Once you start the TrackNew method you lose control of your calling tool because it requires a double-click to finish it's process.

Here is a solution using a custom tool:

    private bool _isFirstClick;
    private ESRI.ArcGIS.Display.INewLineFeedback _lineFeedback;
    private IActiveView _focusMap;
    private IApplication _application;
    private IMxDocument _mxDoc;

    public override void OnCreate(object hook)
    {
        _application = hook as IApplication;

        //Disable if it is not ArcMap
        if (hook is IMxApplication)
            base.m_enabled = true;
        else
            base.m_enabled = false;
    }

    public override void OnMouseDown(int Button, int Shift, int X, int Y)
    {
        _mxDoc = (IMxDocument) _application.Document;
        _focusMap = mxDoc.FocusMap as IActiveView;

        IPoint point = _focusMap.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y) as IPoint;

        if (_lineFeedback == null)
        {
            _lineFeedback = new ESRI.ArcGIS.Display.NewLineFeedback();
            _lineFeedback.Display = _focusMap.ScreenDisplay;
            _lineFeedback.Start(point);
            _isFirstClick = true;
        }
        else
        {
            _lineFeedback.AddPoint(point);
            _isFirstClick = false;
        }

    }
    public override void OnMouseUp(int Button, int Shift, int X, int Y)
    {

        if (_isFirstClick) return;

        IPolyline polyline = _lineFeedback.Stop();

        //Do something with the polyline here

        _lineFeedback = null;
    }

After the second click you will have a reference to your two-point polyline. You then need to create a function to draw it on the map, create a feature from it, or whatever you are trying to do.

I tested this in ArcGIS 10, not 9.3.1 so let me know if it doesn't work for you.

share|improve this answer
    
looks like this is exactly what i need!! –  vinayan May 30 '12 at 5:04
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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