Site Tools


Differences

This shows you the differences between two versions of the page.

Link to this comparison view

developer:scriptsamples:converttexttogeometry [2015/09/14] (current)
Line 1: Line 1:
 +====== Converting Text to Geometry ======
 +> **Developer:​** //​[[developer:​rhinoscript|RhinoScript]]//​
 +> **Summary:​** //​Demonstrates how to convert text to curves using RhinoScript.//​
 +
 +=====Question=====
 +I have many text elements that I would like to convert to text objects (geometry) for engraving. I can explode a text element and get curves that outline the text. The problem is, when I change a text element to a single stroke font, it automatically closes each letter/​number and is unreadable. The only way I have been able to make a single stroke font work is by creating geometry using Rhino'​s **TextObject** command. However, because I have so many text elements it would take forever to remake geometry for each of them. Is it possible to write a script to automate this?
 +
 +=====Answer=====
 +The following script demonstrates how to convert text elements to text objects (geometry). In this example, text objects (geometry) are created with the identical properties, such as font, height, bold, and italics, as the text element. If you want to override the font to use a single stroke font, simply modify this line:
 +
 +<code vb>
 + font = Rhino.TextObjectFont(obj)
 +</​code>​
 +
 +and replace it with something more like this:
 +
 +<code vb>
 + font = "<​single_stroke_font_name>"​
 +</​code>​
 +
 +=====Example=====
 +
 +<code vb>
 + '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''​
 + '​ ConvertTextToGeometry.rvb -- September 2008
 + '​ If this code works, it was written by Dale Fugier.
 + '​ If not, I don't know who wrote it.
 + '​ Works with Rhino 4.0.
 +
 + ​Option Explicit
 +
 + Sub ConvertTextToGeometry
 +
 +   '​ Declare local variables
 +   Dim obj_list, obj, saved_plane,​ cmd
 +   Dim font, height, plane, style, text, bold, italic
 +
 +   '​ Select annotation objects
 +   ​obj_list = Rhino.GetObjects("​Select text to convert to geometry",​ 512, True, True)
 +   If Not IsArray(obj_list) Then Exit Sub
 +
 +   '​ For speed, turn of screen redrawing
 +   Call Rhino.EnableRedraw(False)
 +
 +   '​ Save the current construction plane
 +   ​saved_plane = Rhino.ViewCPlane()
 +
 +   '​ Process each selected object
 +   For Each obj In obj_list
 +
 +     '​ Weed out just the text objects
 +     If Rhino.IsText(obj) Then
 +
 +       '​ Acquire the text parameters
 +       font = Rhino.TextObjectFont(obj)
 +       ​height = Rhino.TextObjectHeight(obj)
 +       plane = Rhino.TextObjectPlane(obj)
 +       style = Rhino.TextObjectStyle(obj)
 +       text = Rhino.TextObjectText(obj)
 +
 +       If (style And 1) Then
 +         bold = "​_Yes"​
 +       Else
 +         bold = "​_No"​
 +       End If
 +
 +       If (style And 2) Then
 +         ​italic = "​_Yes"​
 +       Else
 +         ​italic = "​_No"​
 +       End If
 +
 +       '​ Set the current construction plane
 +       Call Rhino.ViewCPlane(,​ plane)
 +
 +       '​ Add a new text object (geometry)
 +       cmd = "​_-TextObject "
 +       cmd = cmd & "​_GroupOutput=_Yes "
 +       cmd = cmd & "​_FontName="​ & font & " "
 +       cmd = cmd & "​_Italic="​ & italic & " "
 +       cmd = cmd & "​_Bold="​ & bold & " "
 +       cmd = cmd & "​_Height="​ & CStr(height) & " "
 +       cmd = cmd & "​_Output=_Curves "
 +       cmd = cmd & "​_AllowOpenCurves=_Yes "
 +       cmd = cmd & Chr(34) & text & Chr(34) & " "
 +       cmd = cmd & "​0"​
 +       Call Rhino.Command(cmd,​ 0)
 +
 +       '​ Delete the original object
 +       Call Rhino.DeleteObject(obj)
 +
 +     End If
 +   Next
 +
 +   '​ Restore the saved construction plane      ​
 +   Call Rhino.ViewCPlane(,​ saved_plane)
 +
 +   '​ Enable screen redrawing
 +   Call Rhino.EnableRedraw(True)
 +
 + End Sub
 +
 + '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''​
 + ​Rhino.AddStartupScript Rhino.LastLoadedScriptFile
 + ​Rhino.AddAlias "​ConvertTextToGeometry",​ "​_NoEcho _-RunScript (ConvertTextToGeometry)"​
 +
 +</​code>​
 +
 +\\
 +
 +{{tag>​Developer RhinoScript}}
  
developer/scriptsamples/converttexttogeometry.txt ยท Last modified: 2015/09/14 (external edit)