Convert Text to ToString on Mac

Just trying to write a tool to cover flat set of text to create a list of points which elevation is based on text string.

Cant find a way to convert text object to string and to doubles for z values?
Also how can I check if the text is a number or a random text ?

I know there are lots of tools already, what to do it myself.

   List<Point3d> ptList = new List<Point3d>();
		double x;
		double y;
		double z;

    foreach(var guid in textID)
    {
      var text = Rhino.RhinoDoc.ActiveDoc.Objects.Find(guid) as Rhino.DocObjects.TextObject;
      if(text != null)
      {

x = text.TextGeometry.Plane.OriginX;
y = text.TextGeometry.Plane.OriginY;
z = Convert.ToDouble(ToString(text));

Point3d newPoint = new Point3d(x, y, z);
        ptList.Add(newPoint);
      }
    }
    points = ptList;

TextToPoint.3dm (3.6 MB) TextToPoint.gh (7.2 KB)

Any object in C# has a .ToString() Method. Just sometimes it is not outputing the right string. You can overload for any own object individualy. You usually call it someInstance.ToString();. In your case this should be text.ToString();, eventhough this sounds odd. Don’t call a TextObject “text”…

Thank you @TomTom.
Why not call a TextObject “text” ?

Naming variables is a hard problem in computer science :slight_smile: It is easy to fall into the trap of naming a variable after the type of data rather than picking a name which represents the meaning of that data. Of course this is not a hard and fast rule, and I suspect some coders will give the opposite advice here, whilst others will recommend a composite name such as guid_string to represent both the type and the meaning.

You usually have tools built into your editor or IDE to find the object type, i.e. it is redundant information to put the type in the name.

More on naming variables here: https://hackernoon.com/the-art-of-naming-variables-52f44de00aad

1 Like

This does what you need:

Please have all your text objects in a Rhino layer, input the layer name in to this method and let the magic happen

public void GetDataFromText(Rhino.RhinoDoc doc, string layerName
    , out List<Point3d> pts, out List<double> heights)

  {

    List<Point3d> _pts = new List<Point3d>();
    List<double> _heights = new List<double>();
    
    Rhino.DocObjects.RhinoObject[] rhobjs = doc.Objects.FindByLayer(layerName);
    
    if(rhobjs.Length==0) throw new ArgumentException ("Could not find any text objects in given layer!");

    for (int i = 0; i < rhobjs.Length; i++)
    {
      GeometryBase a = rhobjs[i].Geometry;

      TextEntity text = (TextEntity) a;
      string name = text.PlainText;
      
      double result;
      if(double.TryParse(name, out result)) _heights.Add(result);
      
      else
        throw new ArgumentException ("Casting from string to double failed!");
           
      BoundingBox b = a.GetBoundingBox(true);
      _pts.Add(b.Center);
    }

    pts = _pts;
    heights = _heights;

  }

I almost there. I suppose there are more then one way…
Can’t find a way to extract text string from TextObject. API Documentation is sad to look at ((

List<Point3d> ptList = new List<Point3d>();
	double x;
	double y;
	double z;
   string textString;


foreach(var guid in textID)
{
  var text = Rhino.RhinoDoc.ActiveDoc.Objects.Find(guid) as Rhino.DocObjects.TextObject;
  if(text != null)
  {

	x = text.TextGeometry.Plane.OriginX;
	y = text.TextGeometry.Plane.OriginY;
	textString = text.DisplayText;
	z = Convert.ToDouble(textString);


	Point3d newPoint = new Point3d(x, y, z);
    ptList.Add(newPoint);
  }
}
points = ptList;

This is my problem

Did you even try to use the code I sent you?

This is what you are trying to do right?

I tried
textString = text.GetUserString();

I need to find similar for textObject
53

Yes it does what I need. Just want to make sure my code works as well and understand what I am doing wrong.

Here is your code fixed

As a note the ToString() method takes no arguments, that was one of your mistakes in your code.


 List<Point3d> ptList = new List<Point3d>();
    double x;
    double y;
    double z;

    foreach(var guid in textID)
    {
      var text = Rhino.RhinoDoc.ActiveDoc.Objects.Find(guid) as Rhino.DocObjects.TextObject;
      if(text != null)
      {

       
        x = text.TextGeometry.Plane.OriginX;
        y = text.TextGeometry.Plane.OriginY;
        
        var textEntity = text.TextGeometry as TextEntity;
        
        
        double result;
        double.TryParse(textEntity.PlainText, out result);
        z = result;

        Point3d newPoint = new Point3d(x, y, z);
        ptList.Add(newPoint);
      }
    }
    points = ptList;

In terms of robustness, I suggest you to just select objects from a layer, it gives you greater control. Just depends what you want to do further along the line

Thank you @rawitscher-torres but I still getting error ?
16

That is very weird. In my computer it works fine. Further more TextEntity inherits from AnnotationBase which is the one that actually contains the PlainText property. I By the look of your error message Your RhinoCommon version does not seem to have the TextEntity class either!

Why dont you just use the first method I sent you if you say it worked fine in your computer… I mean its also clear to understand. Perhaps you should also post that error message in the Mac forum. But in terms of Windows OS it works perfectly fine

1 Like

Hi @rawitscher-torres it was Rhino5 for Mac problem. I just tried Rhino 6 (Mac) and it works.
Can I as a question, why you have used TryParse?
The thing left to do is to exclude non numeric text (in case it selected)

List ptList = new List();

double x;
double y;
double z;
double textValue;

foreach(var guid in textID)
{
  var text = Rhino.RhinoDoc.ActiveDoc.Objects.Find(guid) as Rhino.DocObjects.TextObject;
  if(text != null)
  {


    x = text.TextGeometry.Plane.OriginX;
    y = text.TextGeometry.Plane.OriginY;
    var textEntity = text.TextGeometry as TextEntity;
    textValue = Convert.ToDouble(textEntity.PlainText);
    z = textValue;

    Point3d newPoint = new Point3d(x, y, z);
    ptList.Add(newPoint);
  }
}
points = ptList;

Also why when I move text object in Rhino its plane origin is not updated in Grasshopper?
It works with simple points.

Yes it excludes non numeric text, why would you need a letter in there or another character?. Sorry and I didn’t understand what you mean on your last question.

  1. In case CAD file with topographic survey has lots of different text objects, some of which are not contours levels.
  2. In case text object is moved from its original place (for whatever reason) script should update points position as well.
  1. So, for example if you have something like {1.2} or {1,2} or [1.2]… etc Be able to still process the text even though they have commas or other non-numeric characters? is this what you mean?

  2. If the text object is moved the script should still update accordingly, so no worries about that.

Thank you Nicholas,

  1. Nope :slight_smile: if the text is {pipe} or {drainage}
  2. Well it doesn’t :((. I have to remove id’s from component and reference them again.

R

If you use my script, where I dont input GUID’s it will update accordingly.

Also, I still dont get what you mean in 1.