Site Tools


Justifying Text Entities

Developer: .NET
Summary: Demonstrates how to justify text in Rhino 5.0.
NOTICE: The Rhino.NET SDK is deprecated in Rhino 5. This example adapted for the new RhinoCommon SDK is here

Question

I'm using the C# and the Rhino .NET SDK, and I want to draw text that is center justified. I have a method implemented whereby I calculate the bounding box of the text and then move the text plane to the middle of a diagonal vector across the bounding box. This method involves a hack; inserting the text object into the model to get a bounding box. Is there a better way to approach this?

Answer

Text justification was plumbed in Rhino 4.0, but never implemented. Text justification is implemented in Rhino 5.0. Thus, the example (below) will run in both V4 and V5. But it (text justification) will only work in V5.

Example

C#

enum eTextJustification
{
  tjUndefined    = 0,
  tjLeft         = 1 << 0,
  tjCenter       = 1 << 1,
  tjRight        = 1 << 2,
  tjBottom       = 1 << 16,
  tjMiddle       = 1 << 17,
  tjTop          = 1 << 18,
  tjBottomLeft   = tjBottom | tjLeft,
  tjBottomCenter = tjBottom | tjCenter,
  tjBottomRight  = tjBottom | tjRight,
  tjMiddleLeft   = tjMiddle | tjLeft,
  tjMiddleCenter = tjMiddle | tjCenter,
  tjMiddleRight  = tjMiddle | tjRight,
  tjTopLeft      = tjTop    | tjLeft,
  tjTopCenter    = tjTop    | tjCenter,
  tjTopRight     = tjTop    | tjRight,
}
 
public override IRhinoCommand.result RunCommand(IRhinoCommandContext context)
{
  OnTextEntity2 textEntity = new OnTextEntity2();
  textEntity.SetPlane(OnPlane.World_xy);
  textEntity.SetUserText("Hello Rhino!");
  textEntity.SetHeight(1.0);
 
  string faceName = "Arial";
  int fontIndex = context.m_doc.m_font_table.FindOrCreateFont(ref faceName);
  textEntity.SetFontIndex(fontIndex);
 
  textEntity.SetJustification((uint)eTextJustification.tjMiddleCenter);
 
  MRhinoAnnotationText textObject = new MRhinoAnnotationText();
  textObject.SetAnnotation(textEntity);
 
  if (context.m_doc.AddObject(textObject))
    context.m_doc.Redraw();
 
  return IRhinoCommand.result.success;
}


developer/textjustify.txt ยท Last modified: 2015/09/14 (external edit)