Pan and center to entity position

1
DS
3/18/2021 5:49 PM

Hi,

I'm looking to purchase a license for our company and have just one outstanding issue that I'm trying to solve.

Currently I'm basing my code on the WinFormsEditExampleCS
I'm trying to pan the viewport and center the screen to the midpoint of a DxfEntity location.

The midpoint was easy to find:

C# Code:
        private bool findMidPoint(string handle, out double midX, out double midY)
        {
            double startX = 0, startY = 0, finishX = 0, finishY = 0;

            midX = 0; midY = 0;

            if (handle.Length == 0)
                return false;

            DxfEntity obj = myModel.Document_GetEntityByHandle(handle);
            string codeName = obj.EntityType;

            switch (codeName)
            {
                case "LINE":
                    {
                        DxfLine o = (DxfLine)obj;
                        startX = o.Start.X;
                        startY = o.Start.Y;
                        finishX = o.End.X;
                        finishY = o.End.Y;

                        midX = (finishX - startX) / 2 + startX;
                        midY = (finishY - startY) / 2 + startY;
                    }
                    break;

...

C# Code:
        public void UpdateRenderTransform(DxfEntity e, Point3D panToPoint)
        {
            Matrix4D to2DTransform = Matrix4D.Identity;
            if (gdiGraphics3D != null)
            {
                if (model != null && bounds != null)
                {
                    translation = new Vector3D(panToPoint.X, panToPoint.Y, 0);

                    double halfHeight = ClientSize.Height / 2;
                    double halfWidth = ClientSize.Width / 2;
                    double margin = 5d; // 5 pixels margin on each size.
                    to2DTransform =
                        Transformation4D.Translation(translation) *
                        Transformation4D.Translation(halfHeight, halfWidth, 0) *
                        Transformation4D.Scaling(scaleFactor) *
                        Transformation4D.Translation(-halfHeight, -halfWidth, 0) *
                        DxfUtil.GetScaleTransform(
                            bounds.Corner1,
                            bounds.Corner2,
                            bounds.Center, //This also didn't work when I passed the vector to it
                            new Point3D(margin, ClientSize.Height - margin, 0d),
                            new Point3D(ClientSize.Width - margin, margin, 0d),
                            new Point3D(ClientSize.Width / 2, ClientSize.Height / 2, 0d)
                        );
                }
                gdiGraphics3D.To2DTransform = to2DTransform * modelTransform;
                dynamicGdiGraphics3D.To2DTransform = gdiGraphics3D.To2DTransform;

                Invalidate();
            }
        }

I tried to pass it into the initial translation and into the bounds.center but it doesn't appear to work.
Is there a utility function that I need to call to convert the coordinates between the model and the viewport?

Wout
3/19/2021 4:44 AM

Hi,

You got the transformation order incorrect. You are applying translation as the last translation, but it should be the first. Transformations happen in opposite order: the first matrix in the multiplication is applied last.

You can set modelTransform to be Transformation4D.Translation(translation), that's what it's intended for: translating the model before doing the transformation to display.

- Wout

1