Rs Airfoil
Last changed: -204.177.179.138

.
DeveloperRhinoScript
SummaryHow to read point files that describe airfoils and create interpolated curves.

The wiki already has a sample of how to read a point file, and create an interpolated curve.

http://en.wiki.mcneel.com/default.aspx/McNeel/ImportInterpCrv

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:

http://en.wiki.mcneel.com/content/upload/files/Airfoil.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

  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