Site Tools


Importing Text from a File

RhinoScript

Version: 4.0
Summary: How to import text from a file.

Question

Is there a RhinoScript method to import text from a file, similar to “Import Text” button found in the “Create Text” dialog box displayed by Rhino's Text command?

Answer

No, there is no method in RhinoScript that will import text from a file and then create a text object. But, it does not take much to write your own. The following example code demonstrates just how you might go about doing this.

 Sub ImportText
 
   Const ForReading = 1
   Dim strFile, strText
   Dim objFSO, objFile
   Dim arrOrigin
 
   strFile = Rhino.OpenFileName("Open", "Text Files (*.txt)|*.txt|")
   If IsNull(strFile) Then Exit Sub
 
   arrOrigin = Rhino.GetPoint("Start point")
   If Not IsArray(arrOrigin) Then Exit Sub
 
   Set objFSO = CreateObject("Scripting.FileSystemObject")
 
   On Error Resume Next
   Set objFile = objFSO.OpenTextFile(strFile, ForReading)
   If Err Then
     MsgBox Err.Description
     Exit Sub
   End If
 
   While Not objFile.AtEndOfStream
     strText = strText & objFile.ReadLine
     If Not objFile.AtEndOfStream Then
       strText = strText & VbCrLf
     End If
   Wend
 
   objFile.Close
 
   Set objFile = Nothing
   Set objFSO = Nothing
 
   If Len(strText) > 0 Then
     Rhino.AddText strText, arrOrigin
   End If
 
 End Sub
developer/scriptsamples/importtext.txt · Last modified: 2015/09/14 (external edit)