Modify Text Object Justification (Python)

Hello,

Does anyone know how to modify Annotation Text justification without recreating the text using Python. AddText has a parameter to modify justification when creating text, but it does not appear that TextObjectJustification was created for Python.

This is my assumption because this exists:

https://developer.rhino3d.com/api/rhinoscript/geometry_methods/textobjectjustification.htm

But it is not in this list:
https://developer.rhino3d.com/api/RhinoScriptSyntax/#geometry-TextObjectText

If I am simply misunderstanding something, please let me know.

Thank You

Hi Dbodey,

There seems to be no rhinoscriptmethod to adjust justification of a text.

Below is quickly made setup to show how to do it with RhinoCommom in python:

import rhinoscriptsyntax as rs
import Rhino
import scriptcontext as sc
import System


def set_justifiction(text_id, justification):
    """
    1 = Left
    2 = Center (horizontal)
    4 = Right
    65536 = Bottom
    131072 = Middle (vertical)
    262144 = Top
    """
    
    #set new justification like with rs.AddText
    new_justification  = System.Enum.ToObject(Rhino.Geometry.TextJustification, justification)
    
    #grab geometry of the text object
    text_geometry = rs.coercegeometry(text_id)
    text_geometry.Justification = new_justification
    
    #replace geometry of the rhino object with new justification geometry
    sc.doc.Objects.Replace(text_id,text_geometry)
    
    
    
text_id = rs.GetObject('select text for new align')

set_justifiction(text_id, 1)
rs.Redraw()
set_justifiction(text_id, 2)
rs.Redraw()
set_justifiction(text_id, 4)
rs.Redraw()
set_justifiction(text_id, 65536)
rs.Redraw()
set_justifiction(text_id, 131072)
rs.Redraw()
set_justifiction(text_id, 262144)
rs.Redraw()

Does this make sense?
-Willem

2 Likes

Makes a lot of sense. Thank you!

1 Like
 #replace geometry of the rhino object with new justification geometry
    sc.doc.Objects.Replace(text_id,text_geometry)

Could I get a little more info on this line of code and what parameter types it takes? I’m thinking text_id has to be a TextObject rather than a GUID. Is that so?

I was getting this error with my own implementation using that Replace function when I passed in (GUID, modified_TextObject).
Annotation%202019-07-11%20112026

Actually, I figured out my misunderstanding. I was treating a TextObject as a TextEntity. It works great now, thanks again.

See, I wasn’t using coerce functions because I had been using:
scriptcontext.doc.Objects.FindByLayer("LAYER_WITH_TEXTBOXES")
which returns a list of Rhino.DocObjects.TextObject

So, what is the difference between TextObject and TextEntity and particularly when do I use one over the other @Willem?

Looks like TextObject.Geometry == TextEntity