Rank: Member
Groups: Guest
Joined: 12/14/2007(UTC) Posts: 2
|
Hello, I'm working on a medical application where I need to measure distances. For this purpose, I wanted to create (inherits) from a LineVObject, and add a text in the middle of the line which would be the distance (in pixels at first). I though I would just inherit from LineVObjet and override the Draw method like this : Code:
public override void Draw(Rectangle renderingRect, Graphics g, ICoordinateMapper coordinateMapper)
{
m_text.Text = ((renderingRect.Width + renderingRect.Height)/2).ToString();
RectangleF rec = m_text.TextArea;
rec.X = renderingRect.Width/2 + renderingRect.X;
m_text.TextArea = rec;
m_text.Draw(renderingRect, g, coordinateMapper);
base.Draw(renderingRect, g, coordinateMapper);
}
But the text is not printed in the middle (X) of the line and the value displayed doesn't seem to be ok (I can't get less than 50 even if the line is at its minimum). So can someone give me a hint about doing this ? I know this is maybe not the right way of doing it... Thank you Loïc Edited by user Saturday, December 15, 2007 6:11:44 AM(UTC)
| Reason: Not specified
|
|
|
|
Rank: Advanced Member
Groups: Guest
Joined: 1/31/2005(UTC) Posts: 458
Was thanked: 5 time(s) in 5 post(s)
|
Hello Loïc, It is possible to implement drawing of the measured distance as a part of the VObject rendering. But the problem is that in such case the distance text sign will change its size while zooming... I think that is not the behaviour you want. VObject itself is binded to the workspace coordinate system and if you change zoom from 100% to 200% - it should be drawn in a doubled size. Maybe it will be acceptable for you to render this sign via VObject designer? But note that in this case the sign will be visible only when line object is selected. Here is the code sample which demostrates how to extend standart edit designer of the LineVObject to display line width: Code:class MeasuringLineDesigner : Aurigma.GraphicsMill.WinControls.GenericVObjectEditDesigner
{
public MeasuringLineDesigner(MeasuringLine line)
: base(line)
{
if (line == null)
throw new ArgumentNullException("line");
_line = line;
_font = new System.Drawing.Font("Verdana", 12);
_brush = System.Drawing.Brushes.Red;
}
public override void Draw(System.Drawing.Graphics g)
{
base.Draw(g);
System.Drawing.Rectangle textBounds = GetDistanceTextRectangle();
g.DrawString(_line.GetDistanceText(), _font, _brush, textBounds.Location);
}
protected override void ObjectChangedHandler(object sender, System.EventArgs e)
{
base.ObjectChangedHandler(sender, e);
if (this.Connected)
InvalidateDesignerArea();
}
private void InvalidateDesignerArea()
{
base.InvalidateDesigner();
System.Drawing.Rectangle invalidationRect = GetDistanceTextRectangle();
base.VObjectHost.HostViewer.InvalidateViewer(
new Aurigma.GraphicsMill.WinControls.MultiLayerViewerInvalidationTarget(invalidationRect));
}
private System.Drawing.Rectangle GetDistanceTextRectangle()
{
System.Drawing.Rectangle lineBounds = base.VObjectHost.HostViewer.WorkspaceToControl(
_line.GetTransformedVObjectBounds(),
Aurigma.GraphicsMill.Unit.Point);
System.Drawing.SizeF textSize = _g.MeasureString(_line.GetDistanceText(), _font);
System.Drawing.RectangleF result = new System.Drawing.RectangleF();
result.X = lineBounds.Left + lineBounds.Width / 2 - textSize.Width / 2;
result.Y = lineBounds.Top + lineBounds.Height / 2 - textSize.Height / 2;
result.Width = textSize.Width;
result.Height = textSize.Height;
return System.Drawing.Rectangle.Ceiling(result);
}
private MeasuringLine _line;
private System.Drawing.Font _font;
private System.Drawing.Brush _brush;
private System.Drawing.Graphics _g = System.Drawing.Graphics.FromHwnd(IntPtr.Zero);
};
class MeasuringLine : Aurigma.GraphicsMill.WinControls.LineVObject
{
public MeasuringLine(float x0, float y0, float x1, float y1)
: base(x0, y0, x1, y1)
{
_designer = new MeasuringLineDesigner(this);
}
public override Aurigma.GraphicsMill.WinControls.IDesigner Designer
{
get
{
return _designer;
}
}
public string GetDistanceText()
{
System.Drawing.PointF[] pnts = new System.Drawing.PointF[2];
pnts[0] = base.Path.PathPoints[0];
pnts[1] = base.Path.PathPoints[1];
base.Transform.TransformPoints(pnts);
int distance = (int)Math.Sqrt(Math.Pow(pnts[0].X - pnts[1].X, 2) +
Math.Pow(pnts[0].Y - pnts[1].Y, 2));
return string.Format("D={0}", distance);
}
private MeasuringLineDesigner _designer;
}
|
|
|
|
|
Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.