Exploding text in rhinocommon

Hello everyone,
I want to translate this pythonscript code into rhinocommon:

import rhinoscriptsyntax as rs
import scriptcontext as sc
import Rhino as r

sc.doc = r.RhinoDoc.ActiveDoc

Plane = sc.doc.Views.ActiveView.ActiveViewport.ConstructionPlane()
height = 2
font = "Arial"
text = "Sample Text"
preText = rs.AddText(text, Plane, height, font, False, False)
textGeo = rs.ExplodeText(preText, True)

Here are the code that I wrote in C#:

protected override Result RunCommand(RhinoDoc doc, RunMode mode)
{
    string text = "Smample Text";
    Rhino.Geometry.Plane plane = doc.Views.ActiveView.ActiveViewport.ConstructionPlane();
    double height = 10;
    string font = "Arial";
    doc.Objects.AddText(text, plane, height, font, false, false);

    return Result.Success;
}

But I can’t figure out how to explode the text object.

Hi Mahdiyar,

The source code to rhinoscriptsyntax can be found on your system in this folder:

%APPDATA%\McNeel\Rhinoceros\6.0\Plug-ins\IronPython (814d908a-e25c-493d-97e9-ee3861957f49)\settings\lib\rhinoscript

In this folder you will find a geometry.py file that contains the definition of rs.ExplodeText, which appears to be calling RhinoObject.Geometry.Explode().

Does this help?

– Dale

1 Like

Hi, Dale.
Thank you for your reply, Sorry for my silly question, but I can’t find [Explode].

Hi Mahdiyar,

You need to do some object casting to get this to work correctly:

protected override Result RunCommand(RhinoDoc doc, RunMode mode)
{
  var str = "Hello Rhino!";
  var plane = Rhino.Geometry.Plane.WorldXY;
  var obj_id = doc.Objects.AddText(str, plane, 10, "Arial", false, false);
  if (obj_id != Guid.Empty)
  {
    var obj = doc.Objects.Find(obj_id);
    if (null != obj)
    {
      var text = obj.Geometry as Rhino.Geometry.TextEntity;
      if (null != text)
      {
        var curves = text.Explode();
        foreach (var crv in curves)
          doc.Objects.AddCurve(crv);
        doc.Objects.Delete(obj, true);
      }
    }
  }
  doc.Views.Redraw();
  return Result.Success;
}

– Dale

1 Like

Hi again and thank you. It works perfect.

1 Like