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;
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”…
Naming variables is a hard problem in computer science 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.
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;
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
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
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;
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.
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?
If the text object is moved the script should still update accordingly, so no worries about that.