Page 1 of 1

Move to CADEntity

Posted: 08 Aug 2018, 18:29
by Mikes360
Does anyone have any sample code that can center a CAD Image over a given CADEntity? I dont want to change the current scale I would just like to move the view so the selected CADEnitiy is centeral.

Below is my current none working attempt. What I am trying to do here is convert the coordinate system of the CADEntity into the coordinate system of the view.

Code: Select all

        public void GotoCadEntity(CADEntity entity)
        {
            if(entity != null)
            {
                DPoint p = entity.Box.Center;

                DRect cadExtents = cadImage.Extents;

                RectangleF screenExtents = new RectangleF(0, 0, (float)cadImage.AbsWidth, (float)cadImage.AbsHeight);

                //float x = (float) RangeConvert(cadExtents.left, cadExtents.right, screenExtents.Left, screenExtents.Right, p.X);
                //float y = (float) RangeConvert(cadExtents.top, cadExtents.bottom, screenExtents.Top, screenExtents.Bottom, p.Y);

                float x = (float)RangeConvert(cadExtents.left, cadExtents.right, screenExtents.Right, screenExtents.Left, p.X);
                float y = (float)RangeConvert(cadExtents.top, cadExtents.bottom, screenExtents.Bottom, screenExtents.Top, p.Y);

                float dx = x * (float) ImageScale;
                float dy = y * (float) ImageScale;

                CenterImagePosition = new PointF(dx, dy);

            }
        }

        //------------------------------------------------------------------------------

        public static double RangeConvert(double oldmin, double oldmax, double newmin, double newmax, double value)
        {
            return (value - oldmin) / (oldmax - oldmin) * (newmax - newmin) + newmin;
        }

Re: Move to CADEntity

Posted: 09 Aug 2018, 19:30
by support
Hello Michael,

To move a CAD image which is drawn on some control so that the center point of a given entity would be displayed in the center of the view, you need to know the following:
  • The client area of this control (Control.ClientRectangle property).
  • Current position and size of the CAD image relative to the client area of the control (in other words, CAD extents projected on the client area of the control). Can be obtained by using a MainForm.ImageRectangleF property in ViewerDemo project or CADViewerControl.ImageRectangleF property.
  • Screen coordinates of the entity center point. Can be obtained by using a CADImage.GetPoint method and CADEntity.Box.Center property.
The sample code below shows how to center the CAD image drawn on a CADPictureBox control by the center point of a given CADEntity without changing the current scale. You may test this code with the ViewerDemo project.

Code: Select all

        public void GotoCadEntity(CADEntity entity)
        {
            if (entity != null)
            {
                Rectangle rect = cadPictBox.ClientRectangle;
                PointF pt = cadImage.GetPoint(entity.Box.Center);
                PointF delta = new PointF(pt.X - rect.Width / 2, pt.Y - rect.Height / 2);
                pt = new PointF(ImageRectangleF.Location.X - delta.X, ImageRectangleF.Location.Y - delta.Y);
                ImageRectangleF = new RectangleF(pt, ImageRectangleF.Size);
                cadPictBox.Invalidate();
            }
        }
Mikhail

Re: Move to CADEntity

Posted: 16 Aug 2018, 13:42
by Mikes360
Thank you for the response this works well!

Re: Move to CADEntity

Posted: 17 Dec 2019, 20:04
by Bath
Maybe I have an old version, but ImageRectangleF property cannot be set because is read only, but I can set Position instead in Viewer Demo.

How recalculate the current scale to fit rectangle to CadPictureBox ?

Code: Select all

Public Sub GotoRectangle(ByVal viewrect As DRect)

        Dim rect As Rectangle = Me.cadPictBox.ClientRectangle
        Dim pt As PointF = Me.cadImageFld.GetPoint(viewrect.Center)
        Dim delta As PointF = New PointF(pt.X - rect.Width / 2, pt.Y - rect.Height / 2)
        pt = New PointF(Me.ImageRectangleF.Location.X - delta.X, Me.ImageRectangleF.Location.Y - delta.Y)
        'Me.ImageRectangleF = New RectangleF(pt, Me.ImageRectangleF.Size) 'read only
        Me.Position = pt
        Me.cadPictBox.Invalidate()

    End Sub

Re: Move to CADEntity

Posted: 19 Dec 2019, 18:50
by support
Bath wrote:
17 Dec 2019, 20:04
Maybe I have an old version, but ImageRectangleF property cannot be set because is read only, but I can set Position instead in Viewer Demo.
If you are talking about the MainForm.ImageRectangleF property in ViewerVbDemo project, it doesn't have the set modifier, unlike the one in ViewerDemo in C#:

Code: Select all

public RectangleF ImageRectangleF
{
    get
    {
        return new RectangleF(CenterImagePosition.X - ScaledPictureSize.Width / 2,
                              CenterImagePosition.Y - ScaledPictureSize.Height / 2,
                              ScaledPictureSize.Width, ScaledPictureSize.Height);
    }
    set
    {
        ScaledPictureSize = new SizeF(value.Width, value.Height);
        CenterImagePosition = new PointF(value.Width / 2 + value.X, value.Height / 2 + value.Y);
    }
}
Mikhail

Re: Move to CADEntity

Posted: 22 Jan 2020, 10:24
by Bath
I still can't find out how to do it...
I would like move rectangle to center and fit the one to window in one.
Can you help me, please
Vladimir