70002326 I would like the vdRect to have 5 grip points

Article 70002326
Type HowTo
Product Engine
Version 10
Date Added 3/17/2023 12:00:00 AM
Fixed 10.1003.0.3 (3/17/2023 12:00:00 AM)
Submitted by Peter Chanios

Summary

I would like the vdRect to have 5 grip points 4 in the corners to resize and one in the middle to move it.

Solution

In order to change the behaviour of the grip points in a vdFigure you need to implement thefollowing two events
By doing so the rect will then have 5 grip points!
doc.OnGetGripPoints += Doc_OnGetGripPoints;
doc.OnMoveGripPointsAt += Doc_OnMoveGripPointsAt;


private void Doc_OnGetGripPoints([System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.IDispatch)] object sender, gPoints gripPoints, ref bool cancel)
        {
            if (sender is vdRect)
            {
                vdRect rect = sender as vdRect;
                //Add 4 grip points on the 4 edges of the rect that will resize the rect in the OnMoveGripPointsAt event.
                //Then add a fifth grip point at the center of the rect to move the whole rect

                //So in order to add the 4 grip points we calculate the 4 edges like this

                //3------Width-----2
                //|                |
                //Height   4     Height
                //|                |
                //0------Width-----1

                //Note 0 is the insertion point!

                gPoint ins = new gPoint();
                gripPoints.Add(ins);  //0
                gripPoints.Add(new gPoint(ins.x + rect.Width, ins.y, ins.z)); //1
                gripPoints.Add(new gPoint(ins.x + rect.Width, ins.y + rect.Height, ins.z)); //2
                gripPoints.Add(new gPoint(ins.x, ins.y + rect.Height, ins.z)); //3
                gripPoints.Add(new gPoint(ins.x + rect.Width / 2.0, ins.y + rect.Height / 2.0, ins.z)); //4 mid point to move the whole rect without resize , you can remove this if you do not like it
                rect.ECSMatrix.Transform(gripPoints);
                cancel = true; //in order to cancel the default grip points of VectorDraw
            }
        }
        private void Doc_OnMoveGripPointsAt([System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.IDispatch)] object sender, Int32Array Indexes, double dx, double dy, double dz, ref bool cancel)
        {
            if (sender is vdRect)
            {
                vdRect rect = sender as vdRect;
                gPoints grips = rect.GetGripPoints();
                Matrix mat = new Matrix();
                mat.TranslateMatrix(dx, dy, dz);

                rect.ECSMatrix.GetInvertion().Transform(grips);
                if (Indexes.Count == grips.Count)
                {
                    rect.Transformby(mat);
                }
                else
                {
                    foreach (int index in Indexes)
                    {
                        switch (index)
                        {
                            case 0:
                                {
                                    if (index >= grips.Count) break;
                                    gPoint grip = new gPoint(grips[index]);
                                    grip = rect.ECSMatrix.Transform(grip);
                                    grip += new gPoint(dx, dy, dz);
                                    Matrix world2ecs = rect.ECSMatrix.GetInvertion();
                                    grip = world2ecs.Transform(grip);
                                    gPoint ins = world2ecs.Transform(rect.InsertionPoint);
                                    rect.Width += ins.x - grip.x;
                                    rect.Height += ins.y - grip.y;
                                    rect.InsertionPoint = rect.ECSMatrix.Transform(grip);
                                    break;
                                }
                            case 1:
                                {
                                    if (index >= grips.Count) break;
                                    gPoint grip = new gPoint(grips[index]);
                                    grip = rect.ECSMatrix.Transform(grip);
                                    grip += new gPoint(dx, dy, dz);
                                    Matrix world2ecs = rect.ECSMatrix.GetInvertion();
                                    grip = world2ecs.Transform(grip);
                                    gPoint ins = world2ecs.Transform(rect.InsertionPoint);
                                    rect.Width = grip.x - ins.x;
                                    rect.Height += ins.y - grip.y;
                                    rect.InsertionPoint = new gPoint(rect.InsertionPoint.x, rect.InsertionPoint.y+dy, rect.InsertionPoint.z);
                                    break;
                                }
                            case 2:
                                {
                                    if (index >= grips.Count) break;
                                    gPoint grip = new gPoint(grips[index]);
                                    grip = rect.ECSMatrix.Transform(grip);
                                    grip += new gPoint(dx, dy, dz);
                                    Matrix world2ecs = rect.ECSMatrix.GetInvertion();
                                    grip = world2ecs.Transform(grip);
                                    gPoint ins = world2ecs.Transform(rect.InsertionPoint);
                                    rect.Width = grip.x - ins.x;
                                    rect.Height = grip.y - ins.y;
                                    break;
                                }
                            case 3:
                                {
                                    if (index >= grips.Count) break;
                                    gPoint grip = new gPoint(grips[index]);
                                    grip = rect.ECSMatrix.Transform(grip);
                                    grip += new gPoint(dx, dy, dz);
                                    Matrix world2ecs = rect.ECSMatrix.GetInvertion();
                                    grip = world2ecs.Transform(grip);
                                    gPoint ins = world2ecs.Transform(rect.InsertionPoint);
                                    rect.Width += ins.x - grip.x;
                                    rect.Height = grip.y - ins.y;
                                    rect.InsertionPoint = new gPoint(rect.InsertionPoint.x + dx, rect.InsertionPoint.y, rect.InsertionPoint.z);
                                    break;
                                }
                            case 4: //midpoint
                                {
                                    rect.Transformby(mat);
                                    break;
                                }
                            default:
                                break;
                        }
                    }
                }
                rect.Update();
                cancel = true;
            }
        }

Send comments on this topic.