Site Tools


Differences

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

Link to this comparison view

developer:scriptsamples:importtext [2015/09/14] (current)
Line 1: Line 1:
 +====== Importing Text from a File ======
 +====== RhinoScript ======
 +> **Version:​** //4.0//
 +> **Summary:​** //How to import text from a file.//
 +
 +=====Question=====
 +Is there a [[developer:​rhinoscript|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 [[developer:​rhinoscript|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.
 +
 +
 +<code vb>
 + 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
 +
 +</​code>​
 +
 +
 +{{tag>​Developer RhinoScript}}
  
developer/scriptsamples/importtext.txt ยท Last modified: 2015/09/14 (external edit)