Page 1 of 1

rotate an entity

Posted: 28 Jun 2016, 17:01
by roman
Hi,
what is the best way to rotate an item (rechtangle / polyline / text).

thank you
Roman

Re: rotate an entity

Posted: 28 Jun 2016, 19:05
by support
Hello Roman,

Text rotation can be applied using a CADText.Rotation property. To rotate a polyline (CADLWPolyLine, CADPolyLine), you will have to rotate each vertex using a CADVertex.Point.Rotate() method.


Mikhail

Re: rotate an entity

Posted: 29 Jun 2016, 10:29
by roman
Hi,

I added the following mehtod in the edtorContolDemo (called by an context menu item).

the rotation seems to work perfectly, but the previously selected item is (dotted) still shown as before, clicking on the item the edge - points are on the new position, but the dawing is only updated after i move the entity.

thank you for your help...

Code: Select all

private void Rotate_Click(object sender, EventArgs e)
        {
            var editor = cadEditorControl;
            var img = cadEditorControl.Image;
            var conv = img.Converter;
            var pic = cadEditorControl.PictureBox;
            
              
            var selected = cadEditorControl.Image.SelectedEntities.FirstOrDefault();

            if (selected == null)
                return;

            var angle = 30;

            var center = new DPoint(cadEditorControl.Image.Center);

            foreach (CADVertex item in selected.Entities)
            {
                item.Point = DPoint.Rotate(item.Point, center, false, angle);
                
            }
           
            selected.Color = Color.Red;

            editor.ClearSelection();


            img.InvalidateSelectedEntities(pic.CreateGraphics());
            cadEditorControl.Update();
            pic.Invalidate();
            cadEditorControl.Image.RefreshCurrentLayout();
            selected.Selected = true;
        }
after rotation something is wrong
after rotation something is wrong
Capture.PNG (3.08 KiB) Viewed 31810 times

Re: rotate an entity

Posted: 04 Jul 2016, 18:54
by support
Hello Roman,

Please modify your code as shown below.

Code: Select all

using System.Linq;
...

        private void Rotate_Click(object sender, EventArgs e)
        {
            var editor = cadEditorControl;
            var img = cadEditorControl.Image;
            var pic = cadEditorControl.PictureBox;
            var selected = cadEditorControl.Image.SelectedEntities.FirstOrDefault();
            if (selected == null)
                return;
            var angle = 30;
            var center = new DPoint(cadEditorControl.Image.Center);

            foreach (CADVertex item in selected.Entities)
            {
                item.Point = DPoint.Rotate(item.Point, center, false, angle);
            }
            selected.Color = Color.Red;
            img.Converter.Loads(selected);
            img.InvalidateSelectedEntities(pic.CreateGraphics());
            editor.ClearSelection();
            editor.Update();
        }				
Mikhail

Re: rotate an entity

Posted: 05 Jul 2016, 12:03
by roman
Hello,

works perfect, thank you.

One further question, is it possible to select multiple enities and rotate them together?

I have a rectangle (Poly) and I select all containing Elements:
img.MultipleSelect(selected.Box, false, true);

If there is a text inside the box, can I rotate the text like the other Elements?

thank you

Re: rotate an entity

Posted: 05 Jul 2016, 20:02
by support
Hello Roman,

It is possible to rotate several entities simultaneously while keeping their relative position. The code example below demonstrates how to implement such rotation for polylines and texts using the origin of coordinates as a base point. Please try this code in the CADEditorControlDemo project.

Code: Select all

private void RotatePolyline(CADImage cadImage, CADPolyLine poly, double angle)
{
    foreach (CADVertex vertex in poly.Entities)
        vertex.Point = DPoint.Rotate(vertex.Point, new DPoint(0, 0, 0), false, angle);
    cadImage.Converter.Loads(poly);
}

private void RotateText(CADImage cadImage, CADText text, float angle)
{
    text.Point = DPoint.Rotate(text.Point, new DPoint(0, 0, 0), false, angle);
    text.Rotation += angle;
    cadImage.Converter.Loads(text);
}

private void Rotate_Click(object sender, EventArgs e)
{
    var editor = cadEditorControl;
    var img = cadEditorControl.Image;
    var pic = cadEditorControl.PictureBox;
    var selected = cadEditorControl.Image.SelectedEntities;
    if (selected == null)
        return;
    var angle = 30;

    foreach (CADEntity ent in selected)
    {
        switch (ent.EntType)
        {
            case EntityType.Polyline:
                RotatePolyline(img, ent as CADPolyLine, angle);
                break;
            case EntityType.LWPolyline:
                RotatePolyline(img, ent as CADPolyLine, angle);
                break;
            case EntityType.Text:
                RotateText(img, ent as CADText, angle);
                break;
        }
    }
    img.InvalidateSelectedEntities(pic.CreateGraphics());
    editor.ClearSelection();
    editor.Update();
}				
Mikhail

Re: rotate an entity

Posted: 06 Jul 2016, 08:53
by roman
Perfect, thank you.

If anyone needs a implementation for rotation with mouse here is my implementation with your code, also for the CADEditorControlDemo:

Code: Select all

#region rotate
        private Point dragStart;
        private DPoint center;

        /// <summary>
        /// click handler for rotate
        /// selects all entities inside the selected item
        /// rotates all entities around the center of the selected box
        /// uses mouse move for the visual rotation
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Rotate_Click(object sender, EventArgs e)
        {
            var img = cadEditorControl.Image;
            var pic = cadEditorControl.PictureBox;


            // check if there was a selection 
            if (img.SelectedEntities.Count < 1)
                return;

            // get the first selected entity
            var selected = img.SelectedEntities.FirstOrDefault();

            // select all entities inside the box
            img.MultipleSelect(selected.Box, false, true);
            // get the center
            center = selected.Box.Center;
            // get the current mouse position, start of the mouse move
            dragStart = MousePosition;

            // add a handler for the mouse movement
            pic.MouseMove += Pic_MouseMove2;
        }

        /// <summary>
        /// Handler for the mouse move action from the rotate command
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Pic_MouseMove2(object sender, MouseEventArgs e)
        {
            var pic = cadEditorControl.PictureBox;
            var selected = cadEditorControl.Image.SelectedEntities;
            
            // check for the requirements
            if (selected == null || selected.Count < 1 || dragStart == null || dragStart == Point.Empty)
            {
                // if no selection clean up
                dragStart = Point.Empty;
                pic.MouseMove -= Pic_MouseMove2;
                return;
            }

            double angle = GetAngleFromMouseMovement();
            // check if there was an angle, if not we can exit
            if (angle == 0) return;

            var editor = cadEditorControl;
            var img = cadEditorControl.Image;
           
            // check for the selected entities and rotate
            foreach (CADEntity ent in selected)
            {
                switch (ent.EntType)
                {
                    case EntityType.Polyline:
                        RotatePolyline(img, ent as CADPolyLine, angle, center);
                        break;
                    case EntityType.LWPolyline:
                        RotatePolyline(img, ent as CADPolyLine, angle, center);
                        break;
                    case EntityType.MText:
                        RotateText(img, ent as CADMText, angle * 2, center);
                        break;
                    case EntityType.Text:
                        RotateText(img, ent as CADText, angle * 2, center);
                        break;
                }
            }

            // update the display
            img.InvalidateSelectedEntities(pic.CreateGraphics());
            pic.Invalidate();
            editor.Update();
            dragStart = MousePosition;
        }

        /// <summary>
        /// Calculates the angle from the mouse delte / movement 
        /// </summary>
        /// <returns></returns>
        private double GetAngleFromMouseMovement()
        {
            Point mousePos = MousePosition;

            int deltaX = (mousePos.X - dragStart.X);
            int deltaY = (mousePos.Y - dragStart.Y);
            var angle = Math.Sqrt(Math.Pow(deltaX, 2) + Math.Pow(deltaY, 2));

            // check if mouse was moved in X or Y direction and get the right value
            if (Math.Abs(deltaY) > Math.Abs(deltaX))
            {
                if (deltaY < 0)
                {
                    angle *= -1;
                }
            }
            else if (deltaX < 0)
            {
                {
                    angle *= -1;
                }
            }

            return angle;
        }

        private void RotatePolyline(CADImage cadImage, CADPolyLine poly, double angle, DPoint center)
        {
            foreach (CADVertex vertex in poly.Entities)
                vertex.Point = DPoint.Rotate(vertex.Point, center, false, angle);
            cadImage.Converter.Loads(poly);
        }

        private void RotateText(CADImage cadImage, CADText text, double angle, DPoint center)
        {
            text.Point = DPoint.Rotate(text.Point, center, false, angle);
            text.Rotation += (float)angle;
            cadImage.Converter.Loads(text);
        }

        private void RotateText(CADImage cadImage, CADMText text, double angle, DPoint center)
        {
            text.Point = DPoint.Rotate(text.Point, center, false, angle);
            text.Angle += (float)angle;
            cadImage.Converter.Loads(text);
        }
        #endregion