Read TextEntity headless

Hello,

I have some TextEntity and I want to retrieve the text and the bounding box from it.
When I try to parse the text as text.RichText.ToString(); or text.PlainText.ToString(); it gives me a <empty> results.

What would be the best way to read the TextEntity inside gh?

parseTextEntityHeadless.gh (3.8 KB)

Thanks in advance.
Amaraa~

Hey @amaraa, you’d need to cast your input, currently you’re creating a new empty TextEntity and printing that.

// Replace
TextEntity htext = new TextEntity();
// With this
TextEntity htext = (TextEntity)x;

Hey @csykes,
Thanks for the tip, however it shows an error of : 1. Unable to cast object of type 'System.String' to type 'Rhino.Geometry.TextEntity'. (line: 57)

I’ve tried all type hint of the ‘x’ input but it’s still the same.

Have you tired it on your pc?

How are you referencing the text entity?

You should first find the RhinoObject in the document, then get its geometry as a TextEntity:

In the context of of a headless document, you should look for the object in that document and not the RhinoDocument reference we get in the script editor.

1 Like

Hello,

These texts are referenced as .dxf files in headless environment.

Okay, I will try to find the text objects in the file I am working on.
I am referencing the initial file using the dxf import according to Mr.Baer’s example, then I am trying to get the text data within grasshopper for further process.

1 Like

Ok. So in your case, you need to loop through the objects in the doc you create and check if the object geometry is a TextEntity. Something like:

2 Likes

Thank you very much Mr.Luis for your fast response, it works very well.

I will work on the bounding box of these words now,
Have a great day ahead.

Greatly appreciated,
Amaraa

1 Like

I was able to retrieve the layer names and the text itself according your suggestion.

Now I need to know the exact location - bounding boxes - of each texts within the file.

Thus I am trying to write something like this below (the logic is to make a list and add bounding boxes to it)

  using(var doc = Rhino.RhinoDoc.CreateHeadless(null))
    {
      doc.Import(path);
      var bboxList = new System.Collections.Generic.List<GeometryBase>();

      foreach(var ro in doc.Objects)
      {
        if(ro.Geometry is TextEntity)
        {
          var inputTextEntity = ro.Geometry as TextEntity;

          var tfont = "Arial";  //need to be retrieved from original text
          var tHeight = 1.5; //need to be retrieved from original text
          var tPlane = Plane.WorldXY; //need to be retrieved from original text

          var textOutlineCurves = Curve.CreateTextOutlines(inputTextEntity, tfont, tHeight, 0, true, tPlane, 1.0, 0.01);
          var bbox = new BoundingBox(textOutlineCurves);

          bboxList.Add(textOutlineCurves);
        }
      }
      Print(te.PlainText);
      textBbox = bboxList;
    }

However, I have encountered many errors shown in below image:

Could you please take a look at this Mr @fraguada?

textlocationtest.gh (8.1 KB)
sampletext1.dxf (160.1 KB)

Thanks you.
-Amaraa