I need to create a new VObject which clone another VObject Rotation Angle, and Location.
Was trying with
Dim Trans As System.Drawing.Drawing2D.Matrix = CType(orgObj.Transform.Clone, System.Drawing.Drawing2D.Matrix)
Dim newObj As Aurigma.GraphicsMill.WinControls.ImageVObject = New Aurigma.GraphicsMill.WinControls.ImageVObject(image, True, 0, 0)
newObj.Transform = Trans
This code work perfect if the orgObj is not being resized. As I only want the new VObject copy the old VObject roration angle, and also the location , but not any others. Please advice.
I manage to copy the location by
Dim newObj As Aurigma.GraphicsMill.WinControls.ImageVObject = New Aurigma.GraphicsMill.WinControls.ImageVObject(image, True, Trans.X, Trans.Y)
I'm afraid that you will need more code to implement this... 😉 Here is sample which calculates rotation angle and dimensions of the object.
First of all main code - it creates RectangleVObject which has same parameters as some another rectangular object (let it be "srcObj"):
//
// Get rotation angle and size of the srcObj
//
System.Drawing.PointF[] topSide;
GetTransformedTopSide(srcObj, out topSide);
float angle = CalcAngle(
new System.Drawing.PointF(1000, 0) /*horizontal vector*/,
new System.Drawing.PointF(/*vector of the top-side of the srcObj*/
topSide[1].X - topSide[0].X,
topSide[1].Y - topSide[0].Y));
SizeF srcObjSize = GetTransformedDimensions(srcObj);
//
//Create new RectangleVObject
//
Aurigma.GraphicsMill.WinControls.RectangleVObject newObj =
new Aurigma.GraphicsMill.WinControls.RectangleVObject(
0,
0,
dstSize.Width,
dstSize.Height);
newObj.Transform.Rotate(
angle,
System.Drawing.Drawing2D.MatrixOrder.Append);
newObj.Transform.Translate(
topSide[0].X,
topSide[0].Y,
System.Drawing.Drawing2D.MatrixOrder.Append);
//
// Add created object to the viewer control
//
multiLayerViewer.CurrentLayer.VObjects.Add(newObj);
The code above uses folowing methods:
//
// Calculates angle between 2 specified vectors.
//
private static float CalcAngle(
System.Drawing.PointF v0,
System.Drawing.PointF v1)
{
float dotProduct = v0.X * v1.X + v0.Y + v1.Y;
float vLengthProduct = CalcLength(v0) * CalcLength(v1);
if (vLengthProduct < 0.01)
throw new ApplicationException("Ups, div by zero error...");
float result =
(float)(Math.Acos(dotProduct / vLengthProduct) / Math.PI * 180);
float vecProduct = v0.X * v1.Y - v0.Y * v1.X;
if (vecProduct < 0)
result = 360 - result;
return result;
}
//
// Returns coordinates of the top side of the specified rectangular
// object. side[0] - is the coordinates of the left-top corner in
// original image, side[1] - right-top corner.
//
private static void GetTransformedTopSide(
Aurigma.GraphicsMill.WinControls.IVObject obj,
out System.Drawing.PointF[] side)
{
System.Drawing.RectangleF baseRect = obj.GetVObjectBounds();
System.Drawing.PointF[] points = new System.Drawing.PointF[2];
points[0] = baseRect.Location;
points[1] =
new System.Drawing.PointF(baseRect.Right, baseRect.Top);
obj.Transform.TransformPoints(points);
side = points;
}
//
// Returns dimensions (width and height) of the
// transformed object.
//
private static System.Drawing.SizeF GetTransformedDimensions(
Aurigma.GraphicsMill.WinControls.IVObject obj)
{
System.Drawing.RectangleF baseRect = obj.GetVObjectBounds();
System.Drawing.PointF[] cornerPnts = new System.Drawing.PointF[3];
cornerPnts[0] = baseRect.Location;
cornerPnts[1] =
new System.Drawing.PointF(baseRect.Right, baseRect.Top);
cornerPnts[2] =
new System.Drawing.PointF(baseRect.Left, baseRect.Bottom);
obj.Transform.TransformPoints(cornerPnts);
return new System.Drawing.SizeF(
CalcDistance(cornerPnts[0], cornerPnts[1]),
CalcDistance(cornerPnts[0], cornerPnts[2]));
}
//
// Calculates distance between 2 points.
//
private static float CalcDistance(
System.Drawing.PointF p0,
System.Drawing.PointF p1)
{
float dx = p0.X - p1.X;
float dy = p0.Y - p1.Y;
return (float)Math.Sqrt(dx * dx + dy * dy);
}
//
// Calculates length of the specified vector.
//
private static float CalcLength(System.Drawing.PointF v0)
{
return (float)Math.Sqrt(v0.X * v0.X + v0.Y * v0.Y);
}
How useful was for me and I had to translate to VB.NET, then put my code here and can be more useful to someone.
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
''
'' Get rotation angle and size of the srcObj
''
Dim topSide As System.Drawing.PointF()
topSide = GetTransformedTopSide(RetornObjetoSelecionado)
Dim angle As Single = CalcAngle(New System.Drawing.PointF(1000, 0), New System.Drawing.PointF( _
topSide(1).X - topSide(0).X, _
topSide(1).Y - topSide(0).Y))
Dim srcObjSize As SizeF = GetTransformedDimensions(RetornObjetoSelecionado)
''
''Create new RectangleVObject
''
Dim newObj As Aurigma.GraphicsMill.WinControls.RectangleVObject = _
New Aurigma.GraphicsMill.WinControls.RectangleVObject(0, 0, srcObjSize.Width, srcObjSize.Height)
newObj.Transform.Rotate(angle, System.Drawing.Drawing2D.MatrixOrder.Append)
newObj.Transform.Translate(topSide(0).X, topSide(0).Y, System.Drawing.Drawing2D.MatrixOrder.Append)
''
'' Add created object to the viewer control
''
canvas.CurrentLayer.VObjects.Add(newObj)
End Sub
''' <summary>
''' Calculates distance between 2 points.
''' </summary>
''' <param name="p0"></param>
''' <param name="p1"></param>
''' <returns></returns>
''' <remarks></remarks>
Private Function CalcDistance( _
ByVal p0 As System.Drawing.PointF, _
ByVal p1 As System.Drawing.PointF) As Single
Dim dx As Single = p0.X - p1.X
Dim dy As Single = p0.Y - p1.Y
Return Math.Sqrt(dx * dx + dy * dy)
End Function
''' <summary>
''' Calculates length of the specified vector.
''' </summary>
''' <param name="v0"></param>
''' <returns></returns>
''' <remarks></remarks>
Private Function CalcLength(ByVal v0 As System.Drawing.PointF) As Single
Return Math.Sqrt(v0.X * v0.X + v0.Y * v0.Y)
End Function
''' <summary>
''' Returns dimensions (width and height) of the
''' transformed object.
''' </summary>
''' <param name="obj"></param>
''' <returns></returns>
''' <remarks></remarks>
Private Function GetTransformedDimensions(ByVal obj As Aurigma.GraphicsMill.WinControls.IVObject) As System.Drawing.SizeF
Dim baseRect As System.Drawing.RectangleF = obj.GetVObjectBounds()
Dim cornerPnts As System.Drawing.PointF() = New System.Drawing.PointF(2) {}
cornerPnts(0) = baseRect.Location
cornerPnts(1) = New System.Drawing.PointF(baseRect.Right, baseRect.Top)
cornerPnts(2) = New System.Drawing.PointF(baseRect.Left, baseRect.Bottom)
obj.Transform.TransformPoints(cornerPnts)
Return New System.Drawing.SizeF(CalcDistance(cornerPnts(0), cornerPnts(1)), CalcDistance(cornerPnts(0), cornerPnts(2)))
End Function
''' <summary>
''' Returns coordinates of the top side of the specified rectangular
''' object. side[0] - is the coordinates of the left-top corner in
''' original image, side[1] - right-top corner.
''' </summary>
''' <param name="obj"></param>
''' <returns></returns>
''' <remarks></remarks>
Private Function GetTransformedTopSide(ByVal obj As Aurigma.GraphicsMill.WinControls.IVObject) As System.Drawing.PointF()
Dim baseRect As System.Drawing.RectangleF = obj.GetVObjectBounds()
Dim points As System.Drawing.PointF() = New System.Drawing.PointF(1) {}
points(0) = baseRect.Location
points(1) = New System.Drawing.PointF(baseRect.Right, baseRect.Top)
obj.Transform.TransformPoints(points)
Return points
End Function
''' <summary>
''' Calculates angle between 2 specified vectors.
''' </summary>
''' <param name="v0"></param>
''' <param name="v1"></param>
''' <returns></returns>
''' <remarks></remarks>
Private Function CalcAngle(ByVal v0 As System.Drawing.PointF, ByVal v1 As System.Drawing.PointF) As Single
Dim dotProduct As Single = v0.X * v1.X + v0.Y + v1.Y
Dim vLengthProduct As Single = CalcLength(v0) * CalcLength(v1)
If (vLengthProduct < 0.01) Then Throw New ApplicationException("Ups, div by zero error...")
Dim result As Single = (Math.Acos(dotProduct / vLengthProduct) / Math.PI * 180)
Dim vecProduct As Single = v0.X * v1.Y - v0.Y * v1.X
If (vecProduct < 0) Then result = 360 - result
Return result
End Function