RhinoCommon align several objects (text)

Hi all,

I have a custom legend object that I want to bake.
It contains a list of TextEntity classes that all have TextHorizontalAlignment == Right.

I want to change all those textEntities to left alignment and then offset them to the right so they physically look like they are right aligned.

I was hoping to change their alignment to Left and then call some Rhino.Transform.Align(objects, vector) but can’t find anything like that.

Theres the rhino command Align, but i cant find it in the SDK.

As a 1st altenative approach i looked into the displaypipeline measuring of text lengths but this is not (afaik) callable outside the pipeline methods.

As a 2md alternative approach, i could use the TextEntity.GetBoundingBox(); however calling this method resets my textHeight to 1.0. (this is a rhino7 bug but fixed in rhino8).


IEnumerable<(GeometryBase geo, ObjectAttributes atts)> rightHandText = geometries
    .Where(g => g.geo is TextEntity txt && txt.TextHorizontalAlignment == TextHorizontalAlignment.Right);


// make text left aligned and align it to the right (speckle compatibility)
foreach ((GeometryBase geo, ObjectAttributes atts) in rightHandText)
{
    if (geo is TextEntity txt)
    {
        txt.TextHorizontalAlignment = TextHorizontalAlignment.Left;
        // offset to the right? 
    }
        
}

image

Hi @sonderskovmathias
I ran into a similar problem with your second apporach.

The developers kindly added a GetTextCorners Method which is much more reliable for measuring text size. Maybe that will help?

https://developer.rhino3d.com/api/rhinocommon/rhino.docobjects.textobject/gettextcorners?version=7.x

1 Like

Thanks, your approach solved it for me!

foreach (var guid in textGuidsToBeAligned)
{
    RhinoObject docTextObj = RhinoDoc.ActiveDoc.Objects.FindId(guid);

    if (docTextObj == null) continue;

    if (docTextObj is TextObject to)
    {
        //  Pt0 = LL, Pt1 = LR, Pt2 = UR, Pt3 = UL
        Point3d[] corners = to.GetTextCorners(RhinoDoc.ActiveDoc.Views.ActiveView.ActiveViewport);

        to.TextGeometry.TextHorizontalAlignment = TextHorizontalAlignment.Left;

        Vector3d movement = new Vector3d(corners[0] - corners[1]);

        to.Geometry.Translate(movement);
        to.CommitChanges();


    }
}
1 Like