Programmatically move points

Dec 14, 2010 at 3:06 PM

Hi. I'm working with Drawing Board. I need to move a point of a PolyLine programmatically through the screen coordinates. Even if I studied the code, don't understand how.

I have these points:

 Point A: 100,100

Point B: 150,150

Point C: 100,200

Pont D: 250,250

Programmatically, I need to move Point A from 100,100 to 300,300, without moving the rest of the points. How can I do that?

Thanks in advance. Best regards, Fernando.

 

Developer
Dec 14, 2010 at 8:00 PM

hi Fernando,

I think that's quite easy, actually.

Have a look in the DrawingBoard.List property.  it is a list of all shapes on the drawingboard.

for example: loop over them, and find out which one is the polyline your're interested in.  (a PolyLine is called a PointSet in the DrawingBoard's lingo)

In this PointSet, you will find the 'points' list, which represents the individual points (of type PointWr), which you can

move by setting or adding to the .X and .Y properties.

To take into account 'screen coordinates' (versus document coordinates), you divide by the DrawingBoard's Zoom property, for the X and Y coordinate.

Don't forget to update the Drawingboard (call Invalidate() on it, just like you would refresh any other .Net control) to make your change visible.

To completely make it clean, it is a good idea to also update any selected 'handles' that might be on the active shape selection... I had a look in OnMouseUp() to figure out how to do that (it just re-creates the handles on selected shapes, and drops the old ones implictly by the .Net garbage collector).

To give you a headstart (you will have to think a bit for yourself too, how to implement exactly what you want), here's some code you can paste into the OnMouseMove() of DrawingBoard, right at the start.   Make a polyline on the drawingboard, press the MIDDLE mouse button (I needed a free mousebutton to demonstrate ;-), and move the mouse around... you will see that the points of all polylines get moved.  It should be rather easy to move only one point, and not do it with the mouse, but by another programmatic command that measures your granny's shoe size or something... I just hooked it up to some mouse movement for illustration purposes, of course, so don't get me wrong: this IS programmatic, although it uses the mouse for illustration purposes and to make it a bit interactive..

if you catch my drift ;-)

------ snippit ----

       // just replace the start of the OnMouseMove() method in DrawingBoard.cs by this chunk of a code-snippet, to see what I mean


        // some hacky member variable for this demo, to remember our 'old' mouse location in between mouse moves, so we can calculate the 'delta' movement
        private Point oldMouse;

        protected override void OnMouseMove(MouseEventArgs e)
        {
            // call the baseclass first, being a good .Net citizen
            base.OnMouseMove(e);

            /////////////////////////////
            // some demo code to show how to move points of a polyline (a 'PointSet', in Drawingboard lingo) programmatically.
            // actually, we move it by mouse (with the MIDDLE mouse button pressed), but the mouse delta movement could as well be you granny's shoesize... just demonstrating!
            if ((e.Button & MouseButtons.Middle) != 0)
            {
                bool redraw = false;                        // a flag that signals us if we need to redraw when done... quick hack

                int dx = (int) ((e.X - oldMouse.X)/Zoom);   // get the screen-relative mouse movement, from the current and precious mouse location, and the DrawingBoard's zoom.
                int dy = (int) ((e.Y - oldMouse.Y)/Zoom);

                // let's just iterate over all shapes in the _shapesCollection, and move some points of the polylines ('PoinSet' shapes, in DrawingBoard lingo)
                foreach (var v in _shapesCollection.List)
                {
                    if (v is PointSet)                      // we only want to move polylines in this demo, no other shapes
                    {
                        var ps = v as PointSet;             // cast v to a PointSet for convenience

                        // let's move all points of the polyline.  (if you want to move only the first, then... guess what.. do it on the first one only, likke illustrated below, in comments ;-)
                        foreach (var point in ps.Points)  
                        {
                            point.X += dx;                  // we add to the coordinates... do your own mojo with the coordinates
                            point.Y += dy;
                        }

                        // or.. example: to move only the first point, we could do something like:
                        // ps.Points[0].X += dx;
                        // ps.Points[0].Y += dy;


                        // since we have movement, we will want to redraw later, when done!
                        redraw = true;                     

                        ///////////////////////////////////////////////////
                        // and finally, some cosmetics:
                        //
                        // for selection handle cleanup, we just need to update the handles, if there are any, so they move along
                        if (_shapesCollection.selectedElement != null)
                        {
                           
                            if (_shapesCollection.selectedElement is PointSet)
                            {
                                ((PointSet) _shapesCollection.selectedElement).setupSize();
                                _shapesCollection.sRec = new SelPoly(_shapesCollection.selectedElement);
                            }
                           
                            if (_redimStatus != RedimStatus.None)
                                _shapesCollection.EndMove();

                            if (_shapesCollection.sRec != null)
                                _shapesCollection.sRec.endMoveRedim();
                        }

                    }
                }

                // if we needed to redraw, do it now! (otherwise our change wouldn't be visible straight away)
                if(redraw)
                    Invalidate();
            }

            // just remember our old mouse location for next time.... demo code of course
            oldMouse = e.Location;         

            // by all means: this is JUST DEMO CODE... never hardcode stuff in this way.. it's just to illustrate how to move points of a polyline programmatically.

            ////////

            // hereafter, the normal mouse handling continues...
            if ((e.Button & MouseButtons.Left) != 0)
            {
                    .... and blaaa and blaaa
           
.....

------ snippit -----

 

greetz!

frank


 

Developer
Dec 14, 2010 at 8:19 PM

hi Fernando,

me again... I included the piece of demo code in the svn repository, in the file DrawingBoard.cs.  You can activate/deactivate it by uncommenting a simple preprocessor command at the start of the file DrawingBoard.cs

         #define _DEMO_FOR_DERNANDO

it will stay in there for a while.  After some weeks, I'll rip it out again, to clean up.  This question/answer session will keep record of it after that, of course.

just for convenience ;-)

greetz

frank

PS. I have no clue how quickly it will actually be updated on the server, and be ready for download by you... maybe you could contact the main administrator of the project for this, if it doesn't... or it might go completely automagically, of course, in some nightly build or so.

Developer
Dec 14, 2010 at 8:21 PM

just checked, and it is 'immediate'!!!  great stuff, the codeplex machine is directly linked to the svn repository.

I you do another checkout or download of the code, you get the changes straight away.

ain't that sweet!

Dec 14, 2010 at 11:35 PM

Hi Frank.

Thanks for your quick response. It's perfect for what I need!

Thank you, thank you, thank you.

Best regards from Uruguay.

Fernando-