Dimension Aligned "DimAligned" in Rhino common

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?

2 Likes

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…

Many thanks !

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;

    }

Domanda25

If you could help me I would be very grateful.

Matteo

Hi @matteo.forlani.90,

Are you using Rhino 5 or 6?

– Dale

Rhino 5.

Hi @matteo.forlani.90,

Here is a sample that works in Rhino 6:

It may no be possible to create an aligned dimension in RhinoCommon in Rhino 5. Some of the dimension classes were incomplete.

– Dale

The method “L.Dim.create” doesn’t exist.

Yes, as I mentioned the sample works in Rhino 6.

– Dale

Hi @matteo.forlani.90, below works in Rhino 5, you may want to try to convert this from python to C#:

import Rhino
import scriptcontext

def DoSomething():
    
    p0 = Rhino.Geometry.Point3d(0,0,0)
    p1 = Rhino.Geometry.Point3d(10,5,0)
    pd = Rhino.Geometry.Point3d(11,9,0)

    plane = Rhino.Geometry.Plane(p0, p1, pd)
    if not plane.IsValid: return
    
    rc, s, t = plane.ClosestParameter(p0)
    start_pt = Rhino.Geometry.Point2d(s,t)
    rc, s, t = plane.ClosestParameter(p1)
    end_pt = Rhino.Geometry.Point2d(s,t)
    rc, s, t = plane.ClosestParameter(pd)
    on_dim_pt = Rhino.Geometry.Point2d(s,t)
    
    dim = Rhino.Geometry.LinearDimension(plane, start_pt, end_pt, on_dim_pt)
    if not dim: return 
    
    dim.Aligned = True
    
    dim_id = scriptcontext.doc.Objects.AddLinearDimension(dim)
    if not dim_id: print "Error adding aligned dimension"; return
    scriptcontext.doc.Views.Redraw()

DoSomething()

_
c.

2 Likes

It works!!! :grinning::grinning: