Baking Text

Hi Guys,

What is the equivalent to rhinoscriptsyntax.AddText() method in RhinoCommon? I am having a hard time to find it, sorry. The final aim is to extend functionality when baking text to the screen.

Many thanks,
Miriam

Hello @Miriam,
Check out the documentation for the ObjectTable class. Specifically the AddText overloaded method.

Hi Miriam,

To find these methods, locate the rhinopython modules.

C:\Users[USER]\AppData\Roaming\McNeel\Rhinoceros\5.0\Plug-ins\IronPython (814d908a-e25c-493d-97e9-ee3861957f49)\settings\lib\rhinoscript

AddText is in geometry.py:

def AddText(text, point_or_plane, height=1.0, font="Arial", font_style=0, justification=None):
    """Adds a text string to the document
    Parameters:
      text = the text to display
      point_or_plane = a 3-D point or the plane on which the text will lie.
          The origin of the plane will be the origin point of the text
      height [opt] = the text height
      font [opt] = the text font
      font_style[opt] = any of the following flags
         0 = normal
         1 = bold
         2 = italic
         3 = bold and italic
      justification[opt] = text justification (see help for values)
    Returns:
      Guid for the object that was added to the doc on success
      None on failure
    """
    if not text: raise ValueError("text invalid")
    if not isinstance(text, str): text = str(text)
    point = rhutil.coerce3dpoint(point_or_plane)
    plane = None
    if not point: plane = rhutil.coerceplane(point_or_plane, True)
    if not plane:
        plane = scriptcontext.doc.Views.ActiveView.ActiveViewport.ConstructionPlane()
        plane.Origin = point
    bold = (1==font_style or 3==font_style)
    italic = (2==font_style or 3==font_style)
    id = None
    if justification is None:
        id = scriptcontext.doc.Objects.AddText(text, plane, height, font, bold, italic)
    else:
        just = System.Enum.ToObject(Rhino.Geometry.TextJustification, justification)
        id = scriptcontext.doc.Objects.AddText(text, plane, height, font, bold, italic, just)
    if id==System.Guid.Empty: raise ValueError("unable to add text to document")
    scriptcontext.doc.Views.Redraw()
    return id

HTH -Willem

Instead of finding a method, you can also do this:

3 Likes

Thanks a lot for the prompt responses.

Yeah, I found the rs source on the Git. But thanks any ways for the new ways to access it.

I guess Luis got to where I needed. Gosh, I never thought that would be within the ObjectTable class! Many thanks for the light!

Miriam

The Rhino Document has an Objects property which is an ObjectTable: http://developer.rhino3d.com/api/RhinoCommonWin/html/P_Rhino_RhinoDoc_Objects.htm

If I have a Rhino.RhinoDoc called RhinoDocument, you’d add text objects to its ObjectTable like so:
RhinoDocument.Objects.AddText(...)

Ok, thanks.

But like so: Rhino.RhinoDoc.ActiveDoc.Objects.AddText(…) because it is GH, right?

It will return the text’s Guid. Then I coerce?

If you are doing this in a GH c# component, you can do the following:
RhinoDocument.Objects.AddText(...)

Notice that in a c#/vb component you already have a reference to the Rhino.RhinoDoc.ActiveDoc:

I don’t believe the GH Component templates (if you are programing your own components) make a reference to a specific RhinoDoc.

Is it possible to use and bake text in Grasshopper without writing my own Python/C# code?

From ten years ago: