Turn string from usertext into plane object

Dear all.

any suggestions how is can turn the string of a plane object into an actual plan object again?

import rhinoscriptsyntax as rs

objects = rs.GetObjects(message="Select polysurface  ", filter=16, preselect=False, select=False,)

for obj in objects:
    box = rs.BoundingBox(obj)
    plane = rs.PlaneFromPoints(box[0], box[1], box[4])
    rs.SetUserText(obj, "plane", plane)

for obj in objects:
    string = rs.GetUserText(obj, "plane")
    #What is the easiest way to turn the string into an actuall plane object?

In grasshopper i use this constructor, so deconstruct the plane to an origin & two vectors, then rebuild.

https://developer.rhino3d.com/api/RhinoCommon/html/M_Rhino_Geometry_Plane__ctor_3.htm

Are you looking for rs.coerceplane?

import rhinoscriptsyntax as rs

objects = rs.GetObjects(message="Select polysurface  ", filter=16, preselect=False, select=False,)

for obj in objects:
    box = rs.BoundingBox(obj)
    plane_id = rs.PlaneFromPoints(box[0], box[1], box[4])
    plane = rs.coerceplane(plane_id)
import rhinoscriptsyntax as rs
from operator import add
import re

objects = rs.GetObjects(message="Select polysurface  ", filter=16, preselect=False, select=False,)
for obj in objects:
    box = rs.BoundingBox(obj)
    plane = rs.PlaneFromPoints(box[0], box[1], box[4])
    rs.SetUserText(obj, "plane", plane)

for obj in objects:
    values = re.findall(r"[-\d.]+", rs.GetUserText(obj, "plane"))
    origin = [float(values[0]), float(values[1]), float(values[2])]
    x_axis = [float(values[3]), float(values[4]), float(values[5])]
    y_axis = [float(values[6]), float(values[7]), float(values[8])]
    plane = rs.PlaneFromPoints(origin, map(add, x_axis, origin), map(add, y_axis, origin))

StringToPlane.py (707 Bytes)

1 Like

This is exactly what I was looking for, also coded in a very nice way. Thank you for the lesson!

Actually, I found a little issue in the code that results into strange planes. In some cases, the numbers in the plane will include mathematical notation with E. Since \d filters out all characters, the exponent will be returned as an independent number as well. I don’t really understand all the functions of the re library, so this is my solution for the problem for now:

values = re.findall(r"Origin=(.+?),(.+?),(.+?) XAxis=(.+?),(.+?),(.+?), YAxis=(.+?),(.+?),(.+?), ZAxis=(.+?),(.+?),(.+?)",string)[0]

I guess there is a smarter way in doing this;

Why dont you just define all the neccesary data of a plane as a JObject (Json) and then you dont have to ideal with the pain of trying to do what you are attemprting? Json syntax is very robust and used everywhere.

1 Like

Thank you for the tipp. Here is my first approach using json class:

plane = rs.WorldXYPlane()

d = {
“Origin”: {“x”: plane[0][0], “y”: plane[0][1], “z”: plane[0][2]},
“XAxis”: {“x”: plane[1][0], “y”: plane[1][1], “z”: plane[1][2]},
“YAxis”: {“x”: plane[2][0], “y”: plane[2][1], “z”: plane[2][2]},
“ZAxis”: {“x”: plane[3][0], “y”: plane[3][1], “z”: plane[3][2]}
}

obj = json.dumps(d)
id = “9bf6d3ea-a896-4b11-8eb8-c4087b07b875”
rs.SetUserText(id, “plane”, obj, attach_to_geometry=False)
string = rs.GetUserText(id, “plane”)
obj = json.loads(string)
origin = [float(obj[“Origin”][“x”]),float(obj[“Origin”][“y”]),float(obj[“Origin”][“z”])]
x_axis = [float(obj[“XAxis”][“x”]),float(obj[“XAxis”][“y”]),float(obj[“XAxis”][“z”])]
y_axis = [float(obj[“YAxis”][“x”]),float(obj[“YAxis”][“y”]),float(obj[“YAxis”][“z”])]
plane = rs.PlaneFromPoints(origin, map(add, x_axis, origin), map(add, y_axis, origin))

1 Like