Centering image to a given point and rotating it around that point
Posted: 07 Jun 2020, 17:11
Hi,
My goal is to center the CadPictBox to a certain cad point given by the user and then rotate it by a given angle.
I have to use visibleArea in order to keep the initial ratio.
My problem is that i don't know which method to use in order to make sure that the cadImage.GetPoint() function will give me the updated coordinates after one iteration in the loop.
Right now i'm using .Refresh() but performances are not good.
Also i implemented a loop because i don't always manage to center the cad in one iteration when i call my center() function after having the cad rotated around a given point (see buttonRotate). But this will lead to the realscale being changed and ratio being changed also. I then have to manually call again Center().
You will then see screenshots where the angle of rotation is not respected due to the Realscale being changed imo.
After initializing, and calling Center() once:

After a 5 degree rotation

After 4 times calling for a 5 degree rotation:

My goal is to center the CadPictBox to a certain cad point given by the user and then rotate it by a given angle.
I have to use visibleArea in order to keep the initial ratio.
My problem is that i don't know which method to use in order to make sure that the cadImage.GetPoint() function will give me the updated coordinates after one iteration in the loop.
Right now i'm using .Refresh() but performances are not good.
Also i implemented a loop because i don't always manage to center the cad in one iteration when i call my center() function after having the cad rotated around a given point (see buttonRotate). But this will lead to the realscale being changed and ratio being changed also. I then have to manually call again Center().
You will then see screenshots where the angle of rotation is not respected due to the Realscale being changed imo.
Code: Select all
private void Center()
{
FPoint fpt;
Point Ptarget; //Target point given by the user via textbox
Point Pcenter;
do
{
DPoint Dtarget = new DPoint(Int32.Parse(textBox2.Text), Int32.Parse(textBox3.Text), 0);
Ptarget = cadImage.GetPoint(Dtarget);
Pcenter = new Point((int)cadPictBox.Width / 2, (int)cadPictBox.Height / 2);
fpt = new FPoint(Ptarget.X - Pcenter.X, Ptarget.Y - Pcenter.Y, 0);
LeftImagePosition -= fpt.X;
TopImagePosition -= fpt.Y;
//visibleArea = cadPictBox.Size;
//visibleArea.Width = visibleArea.Height * (float)(cadImage.AbsWidth / cadImage.AbsHeight);
//cadImage.GetExtents();
//this.cadPictBox.Invalidate();
this.cadPictBox.Refresh();
} while (Ptarget!= Pcenter);
DPoint Rect_center = GetRealPoint((int)cadPictBox.Width / 2, (int)cadPictBox.Height / 2);//rectangle that will show the center of the cadPictBox
Rect_center = GetRealPoint((int)cadPictBox.Width / 2, (int)cadPictBox.Height / 2);
AddRectangle(cadImage, Rect_center, 5.0, 5.0);
cadImage.GetExtents();
this.cadPictBox.Invalidate();
}
Code: Select all
private void buttonRotate_Click(object sender, EventArgs e)
{
double angles = Double.Parse(textBox7.Text);
Point Pcenter = new Point((int)cadPictBox.Width / 2, (int)cadPictBox.Height / 2);
DPoint Dtarget = new DPoint(Int32.Parse(textBox2.Text), Int32.Parse(textBox3.Text), 0);
Center();
RotateALL(cadImage, angles,Dtarget);
cadPictBox.Invalidate();
}
Code: Select all
private void RotateALL(CADImage cad_image, double angle, DPoint Pcenter)
{
var img = cad_image;
CADEntityCollection ents = cad_image.Converter.Entities;
foreach (CADEntity ent in ents)
{
cad_image.SelectedEntities.Add(ent);
}
var selected = cad_image.SelectedEntities;
if (selected == null)
return;
foreach (CADEntity ent in selected)
{
switch (ent.EntType)
{
case EntityType.Polyline:
RotatePolyline(img, ent as CADPolyLine, angle,Pcenter);
break;
case EntityType.LWPolyline:
RotatePolyline(img, ent as CADPolyLine, angle,Pcenter);
break;
case EntityType.Line:
RotateLine(img, ent as CADLine, angle,Pcenter);
break;
case EntityType.Circle:
RotateCircle(img, ent as CADCircle, angle,Pcenter);
break;
}
}
}

After a 5 degree rotation

After 4 times calling for a 5 degree rotation:
