Site Tools


Differences

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

Link to this comparison view

developer:scriptsamples:airfoil [2015/09/14] (current)
Line 1: Line 1:
 +====== Importing Airfoil Shapes ======
 +> **Developer:​** [[developer:​rhinoscript|RhinoScript]]
 +> **Summary:​** //How to read point files that describe airfoils and create interpolated curves.//
 +
 +===== Overview =====
 +The wiki already has a sample of how to read a point file, and create an interpolated curve.
 +
 +**[[developer:​scriptsamples:​importinterpcrv|Importing an Interpolated Curve]]**
 +
 +But, to import a points file that contains airfoil information requires a little more parsing. The following code sample demonstrates how to read a text file containing point coordinates in "​Selig"​ format to draw airfoil shapes.
 +
 +The following script, along with over 1500 airfoil shapes, can be downloaded here:
 +
 +**{{:​developer:​scriptsamples:​importairfoil.zip|ImportAirfoil.zip}}**
 +
 +The airfoil shapes data is from the Department of Aerospace Engineering at the University of Illinois.
 +
 +http://​www.ae.uiuc.edu/​m-selig/​ads.html
 +
 +To use this Rhino script, drag and drop the .rvb file into an open Rhino viewport and then use the command RunScript to select and run the script.  ​
 +
 +<code vb>
 + ​Option Explicit
 +
 + '​ Subroutine to import "​Selig"​ formatted airfoil shapes
 + Sub ImportAirfoil
 +
 +   '​ Local constants
 +   Const ForReading = 1
 +
 +   '​ Local variables
 +   Dim objFSO, objFile
 +   Dim strFileName,​ strAirfoil, strLine, strCurve
 +   Dim arrPt, arrPoints(),​ nCount
 +
 +   '​ Prompt for an airfoil data file
 +   ​strFileName = Rhino.OpenFileName("​Open",​ "​Airfoil Data File (*.dat)|*.dat|"​)
 +   If IsNull(strFileName) Then Exit Sub
 +
 +   '​ Create a file system object
 +   Set objFSO = CreateObject("​Scripting.FileSystemObject"​)
 +
 +   '​ Open the data file for reading
 +   On Error Resume Next
 +   Set objFile = objFSO.OpenTextFile(strFileName,​ ForReading)
 +   If Err Then
 +     ​MsgBox Err.Description
 +     Exit Sub
 +   End If  ​
 +
 +   '​ Read the name of the airfoil
 +   ​strAirfoil = objFile.ReadLine
 +
 +   '​ Read through the file looking for point coordinates
 +   ​nCount = 0 
 +   Do While objFile.AtEndOfStream <> True
 +     ​strLine = objFile.ReadLine
 +     '​ Convert the string to a point
 +     arrPt = PointFromString(strLine)
 +     If IsArray(arrPt) Then
 +       ReDim Preserve arrPoints(nCount)
 +       ​arrPoints(nCount) = arrPt
 +       ​nCount = nCount + 1
 +     End If
 +   Loop
 +
 +   '​ Close the curve
 +   ReDim Preserve arrPoints(nCount)
 +   ​arrPoints(nCount) = arrPoints(0)
 +
 +   '​ Add the named interpolated curve
 +   If IsArray(arrPoints) Then
 +     ​strCurve = Rhino.AddInterpCurveEx(arrPoints)
 +     ​Rhino.ObjectName strCurve, strAirfoil
 +   End If
 +
 +   '​ Close the file and release objects
 +   ​objFile.Close
 +   Set objFile = Nothing
 +   Set objFSO = Nothing
 +
 + End Sub
 +
 + '​ Function to generate a point from a string
 + ​Function PointFromString( strLine )
 +   Dim arrTokens, arrPoint, x, y
 +   ​PointFromString = Null
 +   If VarType(strLine) = vbString Then
 +     ​strLine = Trim(strLine)
 +     ​arrTokens = Rhino.StrTok(strLine,​ " ")
 +     If IsArray(arrTokens) And UBound(arrTokens) = 1 Then
 +       x = CDbl(arrTokens(0))
 +       y = CDbl(arrTokens(1))
 +       ​arrPoint = Array(x, y, 0.0)
 +       ​PointFromString = arrPoint
 +     End If
 +   End If 
 + End Function
 +
 +</​code>​
 +
 +\\
 +
 +{{tag>​Developer RhinoScript}}
  
developer/scriptsamples/airfoil.txt ยท Last modified: 2015/09/14 (external edit)