We know the Linear dimension works as below.
If I want to Align the dimension to the object, i mean Rhino Command “DimAligned”
How to implement it in RhinoCommon. I could not find anything in rhinoCommon for DimAligned
The below code only serves as command name “Dim”
I am looking for “DimAligned” code in RhinoCommon…
protected override Result RunCommand(RhinoDoc doc, RunMode mode)
{
Point3d origin = new Point3d(1, 1, 5);
Point3d offset = new Point3d(110, 120, 90);
Line line = new Line(origin, offset);
doc.Objects.AddLine(line);
//Point3d pt = new Point3d((offset.X - origin.X) / 2, 3, 0);
Point3d pt = new Point3d((offset.X + origin.X) / 2, (offset.Y + origin.Y) / 2, 0);
Plane plane = Plane.WorldXY;
plane.Origin = origin;
double u, v;
plane.ClosestParameter(origin, out u, out v);
Point2d ext1 = new Point2d(u, v);
plane.ClosestParameter(offset, out u, out v);
Point2d ext2 = new Point2d(u, v);
plane.ClosestParameter(pt, out u, out v);
Point2d linePt = new Point2d(u, v);
LinearDimension dimension = new LinearDimension(plane, ext1, ext2, linePt);
dimension.Aligned = true;
if (doc.Objects.AddLinearDimension(dimension) != Guid.Empty)
{
doc.Views.Redraw();
return Rhino.Commands.Result.Success;
}
return Rhino.Commands.Result.Failure;
}
A DimAligned is just a Linear Dimension with an aligned Plane. What happens if you align your plane? In this image, the dim on the left is a linear dimension, the one on the right is an aligned dimension. Notice their construction planes. A linear dimension has the same plane orientation as the current viewport cplane, an aligned dimension takes on the orientation set up when the user picks the dim points. Maybe create a Plane with the x axis in the direction of the Line in your code?
Thanks a lot ! Yes, very good point. I could not think of it. So wrote myself something(custom DimAligned) by vector calculation last 30 minutes.
But if I follow your way, it will be much easier than mine…
I tried in different ways to align the dimension without succeeding.
The problem is that the dimension is rotated and is not parallel to the measured line.
{
Point3d origin = new Point3d(1, 1, 0);
Point3d offset = new Point3d(110, 120, 0);
Line line = new Line(origin, offset);
doc.Objects.AddLine(line);
//Point3d pt = new Point3d((offset.X - origin.X) / 2, 3, 0);
Point3d pt = new Point3d((offset.X + origin.X) / 2, (offset.Y + origin.Y) / 2, 0);
Plane plane = new Plane();
plane.Origin = origin;
Vector3d vx = vector3d(origin, offset);
Vector3d vy = Vector3d.CrossProduct(vx, Vector3d.ZAxis);
vx.Unitize();
vy.Unitize();
plane.XAxis = vx;
plane.YAxis = vy;
plane.ZAxis = Vector3d.ZAxis;
Point2d ext1 = new Point2d(origin.X, origin.Y);
Point2d ext2 = new Point2d(offset.X, offset.Y);
Point2d linePt = new Point2d(pt.X, pt.Y);
LinearDimension dimension = new LinearDimension(plane, ext1, ext2, linePt);
dimension.Aligned = true;
if (doc.Objects.AddLinearDimension(dimension) != Guid.Empty)
{
doc.Views.Redraw();
return Rhino.Commands.Result.Success;
}
return Result.Success;
}