Forgot password?

Create an account!

Forum

« back

RhinoScript – Points lost when projecting to hull

Messages

Please log in to write a message.

  • 2. Nefarius (Jun 26, 2015 09.46):

    Update,
    Mainly solved by joining Surfaces Before projecting onto them, very few Points now misses their target, tweaking the pattern to not be on the very edge of the hull helped.

  • 1. Nefarius (Jun 24, 2015 20.36):

    Hi,

    bit of a newbie to rhino scripting, but have a script cobbled together that is keeping me awake and driving me crazy.

    The intention is to Project a set of Points to a boat hull, retreive the coordinates, sort them, and later export them to a text file.

    the issue I am having is that after creating Points, and projecting them, somehow the number of Points get fewer, leaving the textfile with errors.

    I am painfully aware of the structure and commenting issues in the below code, but would be very grateful for any help.

    I believe the issues is somewhere around line 180-185 in the code.

    The issue does not show if using small numbers (under 20) for stations and waterline (columns and rows)

    Kindest regards
    Patrik

    Option Explicit
    'Script written by <insert name>
    'Script copyrighted by <insert company name>
    'Script version den 21 december 2014 13:31:55

    Call Main()
    Sub Main()
            Rhino.enableredraw(False)'stäng av visibility
            Dim intStations, intWaterlines
            intStations = 64 'give even numner as starts to count with 0 4-80  (should be 64, wich ends up 65)
            intWaterlines = 64 'give even numner as starts to count with 0  4-80 (should be 64 , wich ends up 65)
           
           
            'Välj Hull och skapa kopia
            Dim Hull,HullCopy,OriginalLayer,WorkingLayer

            Hull = Rhino.GetObject("Select Hull", 0, True, True)
            HullCopy = Rhino.CopyObject(Hull)
           
            'Kopiera till Nytt Lager
            OriginalLayer = Rhino.CurrentLayer()
            Call Rhino.AddLayer("WorkingLayer")
            Call Rhino.CurrentLayer("WorkingLayer")
            Call Rhino.ObjectLayer(HullCopy, "WorkingLayer")
            Call Rhino.ObjectLayer(Hull, "Default")

            'Gör Hydrostatics (funktion som ger array tillbaks)
            Dim ArrHydrostatics,dblLOA,dblDrafT,dblBOA,dblVolume
           
            ArrHydrostatics = Testhydrostatics(HullCopy)
            dblLOA = ArrHydrostatics(3)'LOA
            dblDrafT = ArrHydrostatics(7)'Draft
            dblBOA = ArrHydrostatics(4)'BOA(Beam ovear all of selected portion of hull     
            dblVolume = ArrHydrostatics(0)  'Volume
           
            'Skapa mittplan, Klipp bort mitten för att skapa yta att projicera mot
            Dim SplitSurface,Mittplan,HalfHull,Halvskrov
            SplitSurface = MidSurface(dblLOA, dblDrafT, dblBOA, HullCopy)
           
            Dim arrPointA, arrPointB, arrPointC,arrPointD
            Dim arrListOfPoints
            Dim test,testett
           
            arrPointA = Array(-1000, 1, 10000)  ' har lagt till 1 mm ut, för att inte få tveksamma punkter
            arrPointB = Array(dblLOA + 1000, 1, 10000)
            arrPointC = Array(dblLOA + 1000, 1, dblDrafT - 500)
            arrPointD = Array(-1000, 1, dblDrafT - 500)
            arrListOfPoints = Array(arrPointA, arrPointB, arrPointC, arrPointD)
           
            Mittplan = Rhino.AddSrfPt(arrListOfPoints)
           
            HalfHull = Rhino.SplitBrep(HullCopy, Mittplan)
            HalvSkrov = HalfHull(0)'kan behöva ändras för att få pointarna på plus
           
            'Lägg ut punkter
            Call Points(dblLOA, dblDrafT, dblBOA, intStations, intWaterlines)'Creates points, exits with points seleced
           
            'Projicera punkter     
            Dim arrProjectedPoints, arrSortedPoints
            arrProjectedPoints = ProjectPoints(HalvSkrov, SplitSurface, intWaterlines, intStations)
            Call Rhino.Print(Ubound(arrProjectedPoints) + 1 & " arrProjectedPoints")
            'Sortera punkter
            arrSortedPoints = rhino.SortPoints(arrProjectedPoints, True, 1)'sort points in XZY ascending order
            'arrSortedPoints = arrProjectedPoints
           


            'fixa ordning punkter inför textfilen  
            Dim strOutput
            strOutput = WriteTxtFile2(arrSortedPoints, intWaterlines, intStations, dblBOA)
           
            'rensa och ta bort skapade lagret
            Call Rhino.CurrentLayer("Default")
            'Call Rhino.PurgeLayer("WorkingLayer") 

            'skriv  till textfil
            Call    WriteTxtFile(strOutput, intstations, intwaterlines, dblLOA, dblDrafT, dblBOA, dblVolume)'sends info for writing to file
           
            'sätt på visibility
            Rhino.enableredraw(True)       

    End Sub

    Function TestHydrostatics(HullCopy)

            Dim arrObjs,strComm
            Dim dblVolume
            arrObjs = HullCopy ' Rhino.GetObject("Select object for hydrostatics", 8 + 16,, True, True)
           
            strComm = "_-Hydrostatics _WaterlineElevation=0 _Symmetric=_no _Longitude=X"
            strComm = strComm & " _Enter _Clipboard _Enter"

            Call Rhino.Command(strComm)
            If Rhino.LastCommandResult <> 0 Then Exit Function

            Dim strText,strResults,arrClip,arrResults(7),i
            'grab the whole clipboard text
            strText = Rhino.ClipboardText
            'split it into lines
            arrClip = Split(strText, chr(10))
            'separate the words from numbers and remove spaces
            For i=0 To 6
                    arrResults(i) = Rhino.Strtok(arrClip(i), "=")(1)
                    arrResults(i) = Replace(arrResults(i), " ", "")
            Next
            'you now have results all in one arrayNote added:
            'the results are all strings, as they come from text.
            'You will need To convert the number values To doubles With CDbl() And the points To point arrays With Rhino.Str2Pt() If you want To use them In further calculations.
            'arrResults(0)=Volume Displacement                      'arrResults(1)=Center of Buoyancy       'arrResults(2)=Wetted Surface Area              'arrResults(3)=Waterline Length
            'arrResults(4)=Maximum Waterline Beam           'arrResults(5)=Water Plane Area         'arrResults(6)=Center of Floatation             'dblVolume = cdbl(arrResults(0))
           
            Dim Draft,DraftXYZ,DraftZ
            Draft = Rhino.BoundingBox(arrObjs)
            DraftXYZ = Draft(0)
            DraftZ = DraftXYZ(2)
            arrResults(7) = DraftZ
            TestHydrostatics = Array(arrResults(0), arrResults(1), arrResults(2), arrResults(3), arrResults(4), arrResults(5), arrResults(6), arrResults(7))
    End Function

    Function MidSurface(dblLOA, dblDrafT, dblBOA, Hull)
            Dim arrPointA, arrPointB, arrPointC,arrPointD
            Dim arrListOfPoints
            Dim test,testett
           
            arrPointA = Array(-1000, 0, 10000)  
            arrPointB = Array(dblLOA + 1000, 0, 10000)
            arrPointC = Array(dblLOA + 1000, 0, dblDrafT - 500)
            arrPointD = Array(-1000, 0, dblDrafT - 500)
            arrListOfPoints = Array(arrPointA, arrPointB, arrPointC, arrPointD)
           
            MidSurface = Rhino.AddSrfPt(arrListOfPoints)

            test = Rhino.SplitBrep(MidSurface, Hull, True)
            testett = test(1)' lan behöva bytas
            Call Rhino.deleteObject(testett)
            Midsurface = test(0)'kan behöva bytas

    End Function

    Function Points(dblLOA, dblDrafT, dblBOA, intStations, intWaterlines)

            Dim arrPoint
            Dim a,b,x,y,z,i,j,LastPoint,g
           
            a = (dblLOA - 5) / intStations 'shave 5mm off end to ensure points end up on hull surface and not on transom
            b = (dblDrafT ) / intWaterlines
            y = -2 * dblBOA
            g = 0
            For i = 0 To intStations
                    For j = 0 To intWaterlines
                            x = a * i
                            z = dblDrafT - b * j
                            arrPoint = array(x, y, z)
                            LastPoint = Rhino.AddPoint(arrPoint)
                            Rhino.SelectObject(LastPoint)
                            g = g + 1
                    Next
            Next
            'Call Rhino.Print(dblDrafT & " Djup")
            Call Rhino.Print(g & " Punkter")
            Points = Rhino.SelectedObjects()
    End Function

    Function ProjectPoints(HullCopy, SplitSurface, intWaterlines, intStations)
            Dim arrObjects, arrSurface
            arrObjects = Rhino.GetObjects("Select points to project", 1,, True)'uses preseleceted points
            arrSurface = array(HullCopy, SplitSurface) 'Rhino.GetObject("Select surface to project onto", 8 + 16)
           
            Call Rhino.Print(Ubound(arrObjects) + 1 & " arrObjects")
            Dim arrPoints,i
            ReDim arrPoints(Ubound(arrObjects) )
     
            For i = 0 To (Ubound(arrObjects) )
                    arrPoints(i) = Rhino.PointCoordinates(arrObjects(i))
            Next
           
            ' Project Y+...
            Call Rhino.DeleteObjects(arrObjects)
            Call Rhino.UnselectAllObjects()

            Dim arrResults
            arrResults = Rhino.ProjectPointToSurface(arrPoints, arrSurface, Array(0, 1, 0))
            Call Rhino.Print(Ubound(arrResults) + 1 & " arrResults/projectedPoints")
            Call Rhino.AddPoints(arrResults)
            ProjectPoints = arrResults
           
    End Function

    Function WriteTxtFile2(arrSortedPoints, intWaterlines, intStations, dblBOA)
            Dim arrSimplified, i, j, k, dbla, dblb,arrY,arrStrOutput,dblc,d,e
            ReDim arrStrOutput(intStations,intWaterlines)
            Dim x,y,z,znext
            ReDim arrY(100*(intWaterlines*5)*(intStations*5)+100)
           
           
           
            arrSimplified = Rhino.SimplifyArray(arrSortedPoints)

            j = 0
            d = 0

            For i=0 To (Ubound(arrSortedPoints))'((intWaterlines + 1) * (intStations + 1) - 1) 'ändrad för att få bort o-a, kan annars vara -1
                   
                    x = (i * 3)
                    y = (i * 3 + 1)
                    arrY(d) = 2 * arrSimplified(y) / dblBOA 'y värdet genom halva beam
                    'Call rhino.Print(arrSimplified(x) & "  ,  " & arrY(d))
                   
                    d = d + 1              
            Next

            For i=0 To intwaterlines 'set all bow coordinates to 0
                    arrY(i) = 0
            Next
           
            e = 0
            For i=0 To intStations
                    For j=0 To intWaterlines
                            arrStrOutput(i, j) = arrY(e)
                            'Call rhino.Print(i & "    " & j & "    " & e & "      " & arrStrOutput(i, j))
                            e = e + 1
                    Next
            Next
                   
            Dim strOutput
            ReDim strOutput(intStations)
           
            For i=0 To intStations 'populate first column without comma
                    strOutput(i) = CStr(arrStrOutput(i, 0))
            Next
           
           
            For i=0 To intStations
                    For j=1 To intWaterlines  'populate the rest separated by comma
                            strOutput(i) = strOutput(i) & "," & CStr(arrStrOutput(i, j))
                    Next
            Next
           

           
           
            WriteTxtFile2 = strOutput

    End Function

    Function WriteTxtFile(strOutput, intstations, intwaterlines, dblLOA, dblDrafT, dblBOA, dblVolume)

            Const ForReading = 1, ForWriting = 2
            Dim fso, txt, WriteToFile, i
                           
            Set fso = CreateObject("Scripting.FileSystemObject")
            Set txt = fso.CreateTextFile("c:\Users\Spastic Sparrow\Desktop\CAD\Rhino\an example.mlt", True)         'txt-file will be safed on the local temp direction

            Dim strBeforeOffsets()
            ReDim strBeforeOffsets(48)
            '# =================== INPUT FILE TYPE AND SUBTYPE ======================
            strBeforeOffsets(0) = "0"'# Input File Type(0 = Standard)
            strBeforeOffsets(1) = "0"'# Input File Subtype(0 = Standard)
            '# =================== OUTPUT FILE TYPE AND SUBTYPE =====================
            strBeforeOffsets(2) = "0"'# Output File Type(0 = Standard)
            strBeforeOffsets(3) = "0"'# Output File Subtype(0 = Standard)
            '# ====================== COURSE AND VESSEL TYPE ========================
            strBeforeOffsets(4) = "0"'# Course Particulars(0 = None)
            strBeforeOffsets(5) = "1"'# Number of Hulls(1, 2, ..., Or 5')
            '# ======================== PHYSICAL QUANTITIES =========================
            strBeforeOffsets(6) = "9.807000" 'Gravitational Acceleration(m / sec / sec)(min 9.6, max 9.9)
            '# ========================= WATER PROPERTIES ===========================
            strBeforeOffsets(7) = "1025.00"'# Water Density(kg / cubic metre) (min 995.0, max 1030.0)
            strBeforeOffsets(8) = "1.31000"'# Water Kin.Viscosity(sq.m / sec * 10 ^ -6)(min 0.8, max 1.31)
            strBeforeOffsets(9) = "10"'# Base Eddy Kin.Viscosity(non - dimensional, min1.0)
            strBeforeOffsets(10) = "10000.0"'       # Water Depth(metres)(max=10000.0
            '# ========================== AIR PROPERTIES ============================
            strBeforeOffsets(11) = "1.26"'# Air Density(kg / cubic metre) (min 0.9, max 2.0
            strBeforeOffsets(12) = "14.4"'# Air Kin.Viscosity(sq.m / sec * 10 ^ -6)(min 10.0, max 20.0)
            strBeforeOffsets(13) = "0.0" ' #Wind speed(m/s)
            strBeforeOffsets(14) = "0.0" ' #Wind Direction(degrees)
            '# ======================= CALCULATION PARAMETERS =======================
            strBeforeOffsets(15) = "0.5"'# Minimum Speed(m / sec)(min 0.01, max 39.9)
            strBeforeOffsets(16) = "10"'    # Maximum Speed(m / sec)(max 40.0)
            strBeforeOffsets(17) = "21"'# Number of Speeds(min2, max50)
            strBeforeOffsets(18) = "0"'# Leeway Parameters(0 = None)
            strBeforeOffsets(19) = "512"'   # Wave Drag Ntheta
            strBeforeOffsets(21) = "2"'# Skin Friction Method(0 = None, 1 = ITTC1957, 2 = Grigson)
            strBeforeOffsets(22) = "3"'# Viscous Form Factor Method (0=None, 3=Dual)
            strBeforeOffsets(23) = "1.0"'# Viscous Drag Form Factor
            strBeforeOffsets(24) = "1.0"'# Wave Drag Form Factor
            strBeforeOffsets(25) = "1"'# Pressure Signature Method (0=None,1=Slender body)
            '# ==================== SHIP CALCULATION PARAMETERS =====================
            strBeforeOffsets(26) = CStr(intStations + 1)'# Number of Offset Stations(rows)(odd integer: min 5, max 81')
            strBeforeOffsets(27) = CStr(intWaterlines + 1)'# Number of Offset Waterlines(columns)(odd integer: min 5, max 81)
            strBeforeOffsets(28) = "3"'# Ship Loading Type
            strBeforeOffsets(29) = "1.0, 0.0, 0.0"'# Ship Loading Formula Parameters
            '# ===================== WAVE ELEVATION PARAMETERS ======================
            strBeforeOffsets(30) = "12.750"'Sectorial Cuts And Patches      # R0
            strBeforeOffsets(31) = "42.500"'#R1
            strBeforeOffsets(32) = "22.500"'# Beta
            strBeforeOffsets(33) = "100"'# Nr
            strBeforeOffsets(34) = "100"'# Nbeta
            strBeforeOffsets(35) = "12.750"'# Rectangular Cuts And Patches# x0
            strBeforeOffsets(36) = "42.500"' x1
            strBeforeOffsets(37) = "-24.540"'# y0
            strBeforeOffsets(38) = "24.540"'# y1
            strBeforeOffsets(39) = "100"'# Nwx
            strBeforeOffsets(40) = "100"' # Nwy
            strBeforeOffsets(41) = "200"'#Beaches And Walls# x0
            strBeforeOffsets(42) = "2200.0"'# x1
            strBeforeOffsets(43) = "500.0"'# y0
            strBeforeOffsets(44) = "-1.0"'# z0
            strBeforeOffsets(45) = "0.0"'# z1
            strBeforeOffsets(46) = "90.0"'# Slope
            strBeforeOffsets(47) = "11"'# Nbx
            strBeforeOffsets(48) = "11"'# Nbz       
            '# ============================ FIRST HULL OFFSETS==============================

            For i=0 To ubound(strBeforeOffsets)
                    txt.WriteLine strBeforeOffsets(i)
            Next   

            For i=0 To intStations
                    txt.WriteLine strOutput(i)
            Next
           
            Dim strAfterOffsets()
            ReDim strAfterOffsets(20)
            strAfterOffsets(0) = CStr(dblVolume / 1000 / 1000 / 1000)'# Displacement Volume(cubicmetres)
            strAfterOffsets(1) = CStr(dblLOA / 1000)'# Length(metres)
            strAfterOffsets(2) = CStr(dblDrafT / -1000)'# Draft(metres)
            strAfterOffsets(3) = "0.0"'# Longitudinal Separation(metres)(0.0 For a monohull)
            strAfterOffsets(4) = "0.0"'# Lateral Separation Distance(metres)(0.0 For a monohull)
            strAfterOffsets(5) = "3"'# Loading Type For this hull
            strAfterOffsets(6) = "1.0, 0.0, 0.0"'# Loading Formula Parameters
            strAfterOffsets(7) = "0"'# Trim Method
            strAfterOffsets(8) = "2"'# Trim: Number of speeds(>= 2)
            strAfterOffsets(9) = "0.0, 0.0"'# Trim: speed, angle
            strAfterOffsets(10) = "10.0, 0.0"'# Trim: speed, angle
            strAfterOffsets(11) = "0"'# Sinkage Method
            strAfterOffsets(12) = "2"'# Sinkage: Number of speeds(>= 2)
            strAfterOffsets(13) = "0.0, 0.0"'# Sinkage: speed, amount
            strAfterOffsets(14) = "10.0, 0.0"'# Sinkage: speed, amount
            strAfterOffsets(15) = "0"'# Heel Method
            strAfterOffsets(16) = "2"'# Heel: Number of speeds(>= 2)
            strAfterOffsets(17) = "0.0, 0.0"'# Heel: speed, angle
            strAfterOffsets(18) = "10.0, 0.0"'# Heel: speed, angle
            strAfterOffsets(19) = "0"'# Appendages(0 = None)
            strAfterOffsets(20) = "0"'# Other Particulars(0 = None)
           
            For i=0 To ubound(strAfterOffsets)
                    txt.WriteLine strAfterOffsets(i)
            Next
                   
            'Set txt = fso.OpenTextFile(GetTempDir & "\example.txt", ForReading)
            'WriteToFile = txt.ReadLine

    End Function

    Function GetTempDir()

            Dim WScript, WshShell, objEnv
           
            Set WshShell = CreateObject("WScript.Shell")
            Set objEnv = WshShell.Environment("PROCESS")
           
            GetTempDir = objenv("temp")
           
    End Function

    Points lost when projecting to hull
Recommend

Why are these buttons gray?