RhinoScript importing custom file formats
From KokkugiaWiki
simple rhinoScripts can be used to import custom file formats into rhino. the following example enables points, lines, points with extra object data, splines or vectors(leaders) to be imported. this example is an extension of a script available on the rhino wiki - importPoints
a sample file to import here
the file format for the input files needs to contain information regarding each element to import on one line per object. formatted as follows(using floats for integers):
points
x,y,z per line
5,7,8
points with object data
where 22 is a value for object data and 'dataExampleName' is the name of that data
5,7,7 dataExampleName 22
lines x,y,z for the start and end point of the line separated by a space
5,7,8 10,3,22
splines x,y,z for each control point separated by a space
5,7,8 10,3,22 13,1,30 18,0,35
vectors x,y,z for the start and end point of the leader separated by a space
5,7,8 10,3,22
Option Explicit
' script written by roland snooks | www.kokkugia.com
' based on the import points script from the rhinoWiki
Call kImport()
Sub kImport()
' Prompt the user for a file to import
Dim strFilter, strFileName, agents()
strFilter = "Text File (*.txt)|*.txt|All Files (*.*)|*.*||"
strFileName = Rhino.OpenFileName("Open Point File", strFilter)
If IsNull(strFileName) Then Exit Sub
Dim arrNames(4), strName
arrNames(0) = "points"
arrNames(1) = "pointsData"
arrNames(2) = "lines"
arrNames(3) = "splines"
arrNames(4) = "vectors"
strName = Rhino.GetString("import what sort of custom file format", , arrNames)
' The the file system object
Dim objFSO, objFile
Set objFSO = CreateObject("Scripting.FileSystemObject")
' Try opening the text file
On Error Resume Next
Set objFile = objFSO.OpenTextFile(strFileName, 1)
If Err Then
MsgBox Err.Description
Exit Sub
End If
Rhino.EnableRedraw False
' Read each line from the file and pass to the appropriate build function
Dim strLine, arrPoint, counter, fileType
counter = 0
Do While objFile.AtEndOfStream <> True
strLine = objFile.ReadLine
If Not IsNull(strLine) Then
' Remove any double-quote characters
strLine = Replace(strLine, Chr(34), "", 1)
Select Case strName
Case "pointsData"
obj = buildPointsData(strLine) 'adds points with object data
Case "lines"
obj = buildLines(strLine) 'adds lines
Case "splines"
obj = buildSplines(strLine) 'adds line
Case "vectors"
obj = buildVectors(strLine) 'adds lines
Case Else
obj = buildPoints(strLine) 'adds points
End Select
' add to an array
ReDim Preserve agents(counter - 1)
agents(counter - 1) = obj
End If
counter = counter + 1
Loop
Rhino.EnableRedraw True
objFile.Close
Set objFile = Nothing
Set objFSO = Nothing
End Sub
Function buildPoints(strLine)
Dim arrPoint, pt, i, arrTokens
' convert the first string to a 3D point
arrPoint = Rhino.Str2Pt(strLine)
' Add the point to Rhino
If IsArray(arrPoint) Then
pt = Rhino.AddPoint(arrPoint)
End If
buildPoints = pt
End Function
Function buildPointsData(strLine)
Dim arrPoint, pt, i, arrTokens
arrTokens = Rhino.Strtok(strLine)
' convert the first string to a 3D point
arrPoint = Rhino.Str2Pt(arrTokens(0))
' Add the point to Rhino
If IsArray(arrPoint) Then
pt = Rhino.AddPoint(arrPoint)
' add object data to point
For i = 1 To UBound(arrTokens)
Rhino.SetObjectData pt, "agentAttribute", arrTokens(i*2 - 1), arrTokens(i*2)
Next
End If
buildPointsData = pt
End Function
Function buildLines(strLine)
Dim arrPoint1, arrPoint2, line, arrTokens
arrTokens = Rhino.Strtok(strLine)
' convert the first string to a 3D point
arrPoint1 = Rhino.Str2Pt(arrTokens(0))
arrPoint2 = Rhino.Str2Pt(arrTokens(1))
' Add a line to Rhino
line = Rhino.AddLine(arrPoint1, arrPoint2)
buildLines = line
End Function
Function buildSplines(strLine)
Dim arrPoint1, arrPoint2, line, arrTokens, arrPoints(), i
arrTokens = Rhino.Strtok(strLine)
' loop to convert to points
For i = 0 To UBound(arrTokens)
ReDim Preserve arrPoints(i)
arrPoints(i) = Rhino.Str2Pt(arrTokens(i))
Next
' Add a spline to Rhino
line = Rhino.AddCurve(arrPoints)
buildSplines = line
End Function
Function buildVectors(strLine)
Dim arrPoint1, arrPoint2, line, arrTokens
arrTokens = Rhino.Strtok(strLine)
' convert the first string to a 3D point
arrPoint1 = Rhino.Str2Pt(arrTokens(0))
arrPoint2 = Rhino.Str2Pt(arrTokens(1))
' Add the leader to Rhino
line = Rhino.AddLeader(Array(arrPoint1, arrPoint2))
buildVectors = line
End Function
for information on exporting custom file formats from processing see: processing output
for information on how to generate meshes from point information, check out the meshing resources page.
