Wrapping Text in a 2D_box

I made a simple C# script, to deal with the wrapping text problem.

The code inside using textObj from RhinoDoc because I cannot find a way in Grasshopper.
After setting up the textEntity, I used boundingBox of text and curve(rectangle), to adjust the height of the text, by putting in a while loop to check.

Then move the center of the text to the center of the rectangle curve.
Demo

It works, but I still think there’s something I can improve within my code.
In some situation, maybe too many iterations, it freezed.

Things I think what can be improved, is that, inside the code, I got preCalcaluteForAnnation() in order to calculate the text height before even addText() in the RhinoDoc.

But the thing I did here, is just run the calculation once, and delete it…

public void preCalculateForAnnotation(TextEntity txtEnt, Curve region, double boxX, double boxY){
    Guid textGuid = Rhino.RhinoDoc.ActiveDoc.Objects.AddText(txtEnt);
    Rhino.DocObjects.TextObject annotation = (Rhino.DocObjects.TextObject) Rhino.RhinoDoc.ActiveDoc.Objects.FindId(textGuid);

    annotation.TextGeometry.TextOrientation = Rhino.DocObjects.TextOrientation.InView;
    annotation.CommitChanges();

    double annoHeight = annotation.TextGeometry.GetBoundingBox(true).Max.Y - annotation.TextGeometry.GetBoundingBox(true).Min.Y;
    while(annoHeight > boxY){
      annotation.TextGeometry.TextHeight -= 40;
      annotation.CommitChanges();
      annoHeight = annotation.TextGeometry.GetBoundingBox(true).Max.Y - annotation.TextGeometry.GetBoundingBox(true).Min.Y;
    }

    Point3d textBoxCenter = annotation.TextGeometry.GetBoundingBox(true).Center;
    Point3d realBoxCenter = region.GetBoundingBox(true).Center;


    annotation.Geometry.Transform(Transform.Translation(new Vector3d(realBoxCenter - textBoxCenter)));
    annotation.CommitChanges();

    _annBase = (AnnotationBase) annotation.AnnotationGeometry.Duplicate();
    Rhino.RhinoDoc.ActiveDoc.Objects.Delete(textGuid, true);

  }

Hope there’s a brighter way for this problem thanks.
Demo_Question.3dm (32.3 KB)
Demo_Question.gh (9.3 KB)