rotate an entity
Posted: 28 Jun 2016, 17:01
Hi,
what is the best way to rotate an item (rechtangle / polyline / text).
thank you
Roman
what is the best way to rotate an item (rechtangle / polyline / text).
thank you
Roman
CADSoftTools - AutoCAD DWG DXF HPGL (PLT) SVG CGM STEP IGES STL SAT viewers, converters and developer tools. Delphi and C# source code.
https://cadsofttools.com/forum/
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;
}
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();
}
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();
}
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