Forgot password?

Create an account!

Forum

« back

RhinoScript – Slow Exporting Script

Messages

Please log in to write a message.

  • 2. Johannes (Nov 09, 2013 19.36):

    Hey, looks like a very nice script!
    I tried it with 1000 points, 1000 lines, 1000 boxes, ... the file size is 195kb. It took 1 second to write all informations to the file! i don't know your geometry and maybe i missed one input value... Try to find out which part of your script is slow. Just write something in the command line when one part is finished (Rhino.Print("Done: 1/5")). You can also write the system time to measure it.

    In generel scripting languages like VBScipt are slower then compiled code. So if you really need a stable application with performance you should think about a .net language. But i don't see it at the moment.

    Johannes

  • 1. matthewhickey (Nov 05, 2013 04.53):

    My professor gave me this script which exports lines as springs and points as agents to import for use with a springs-based processing script. It takes a very long time to output, usually writing the file around 42 kb a minute, while my group partner had the script run hours on a similarly complex geometry (thousands of lines and points). Can anyone help me figure out a way to make this script run faster or more efficient? I am completely new to rhinoscript and my previous work was using it to make circles on a curve proportional to their radius, then coloring them.

    Option Explicit

    Call ExportKAX()
    Sub ExportKAX()

            Dim i, j, strFileName1, strFileName2, objFSO1, objFSO2, objStream1, objStream2, strFilter1
            Dim pts, springs, lines, attractors, boxes, boundary, pts_01, pts_02, pts_03, springs_01, springs_02, springs_03, isoAttractors
            Dim obstacles, emitters, targets, vecAttractors, meshAttractors, stBord, repelors
            Dim fixedPts, springConnects
           
            ' User-definable file filters
            strFilter1 = "Text File (*.kax)|*.kax|All Files (*.*)|*.*||"

            Dim arrNames(1), selType
            arrNames(0) = "user"
            arrNames(1) = "layer"
            'selType = Rhino.GetString("selection method", , arrNames)
            selType = "layer" 'layer/user
           
            If selType = "user" Then
                    ' input
                    pts = Rhino.GetObjects("select point agents to export", 1)
                    springs = Rhino.GetObjects("select springs to export", 4)
                    lines = Rhino.GetObjects("select lines to export", 4)   
                    attractors = Rhino.GetObjects("select attractors to export", 5)
                    boxes = Rhino.GetObjects("selesct boxes to export", 16)
                    boundary = Rhino.GetObject("select boundary to export", 16)            
            End If
           
            If selType = "layer" Then
                   
                    pts = Rhino.ObjectsByLayer("agents")
                    fixedPts = Rhino.ObjectsByLayer("fixed_agents")
                    lines = Rhino.ObjectsByLayer("lines")
                    repelors = rhino.ObjectsByLayer("repelors")
                    springConnects = rhino.ObjectsByLayer("spring_connection")

            End If

            strFileName1 = Rhino.SaveFileName("save multiType file as", strFilter1)
            If IsNull(strFileName1) Then Exit Sub

            ' Get the file system object
            Set objFSO1 = CreateObject("Scripting.FileSystemObject")
            On Error Resume Next

            ' Get a new text file
            Set objStream1 = objFSO1.CreateTextFile(strFileName1, True)
            If Err Then
                    MsgBox Err.Description
                    Exit Sub
            End If
           
            If isArray(repelors) Then              
                    writeRepelor repelors, objStream1              
            End If
           
            If isArray(fixedPts) Then              
                    writeFixedPoints fixedPts, springConnects, objStream1          
            End If
           
            If isArray(pts) Then           
                    writePoints pts, springConnects, objStream1            
            End If
           
            If isArray(lines) Then
                    writeLines lines, objStream1
            End If 
           
            writeCameras objStream1
            Rhino.Print "export_complete!!"
            ' close txt file
            objStream2.Close
           
    End Sub



    Function writePoints(pts, springConnects, objStream1)
           
            Dim ptsLoc, i, attributes,n, curveParam, curvePoint, dist1, coord, endPt, stPt, endDist, stDist, otherPt

            ' write type definition
            objStream1.WriteLine("type agents")
            ' loop to get point locations + point weight
            For i = 0 To UBound(pts)
                    coord = rhino.PointCoordinates(pts(i))
                   
                    objStream1.Write(coord(0) & "," & coord(1) & "," & coord(2))
                   
                    For n = 0 To UBound(springConnects)
                           
                            curveParam = Rhino.CurveClosestPoint(springConnects(n), coord)
                            curvePoint = Rhino.EvaluateCurve(springConnects(n), curveParam)
                            dist1 = rhino.Distance(curvePoint, coord)
                           
                            If dist1 < .01 Then
                                    'Rhino.SelectObject springConnects(n)
                                    endPt = Rhino.CurveEndPoint(springConnects(n))
                                    stPt = Rhino.CurveStartPoint(springConnects(n))
                                    endDist = Rhino.Distance(endPt, coord)
                                    stDist = Rhino.Distance(stPt, coord)
                                    If endDist > stDist Then
                                            otherPt = endPt
                                    Else
                                            otherPt = stPt
                                    End If
                                    objStream1.Write(",")
                                    objStream1.Write(otherPt(0) & "," & otherPt(1) & "," & otherPt(2))
                            End If
                            'rhino.SelectObject connects(n)
                    Next
            ' get point coord
            'ptsLoc = Rhino.PointCoordinates(pts(i))

            'attributes = attString(pts(i))
            objStream1.WriteLine()
            ' add to text file
                   
            'objStream1.WriteLine(ptsLoc(0) & "," & ptsLoc(1) & "," & ptsLoc(2))
                   
            Next

    End Function

    Function writeFixedPoints(fixedPts, springConnects, objStream1)
           
            Dim ptsLoc, i, attributes,n, curveParam, curvePoint, dist1, coord, endPt, stPt, endDist, stDist, otherPt
           
           
            objStream1.WriteLine("type fixedAgents")
            '
            '       
            For i = 0 To UBound(fixedPts)
                    coord = rhino.PointCoordinates(fixedPts(i))
                    '              
                    objStream1.Write(coord(0) & "," & coord(1) & "," & coord(2))
                    '              
                    For n = 0 To UBound(springConnects)
                            '                      
                            curveParam = Rhino.CurveClosestPoint(springConnects(n), coord)
                            curvePoint = Rhino.EvaluateCurve(springConnects(n), curveParam)
                            dist1 = rhino.Distance(curvePoint, coord)
                            '                      
                            If dist1 < .01 Then
                                                           
                                    endPt = Rhino.CurveEndPoint(springConnects(n))
                                    stPt = Rhino.CurveStartPoint(springConnects(n))
                                    endDist = Rhino.Distance(endPt, coord)
                                    stDist = Rhino.Distance(stPt, coord)
                                    If endDist > stDist Then
                                            otherPt = endPt
                                    Else
                                            otherPt = stPt
                                    End If
                                    objStream1.Write(",")
                                    objStream1.Write(otherPt(0) & "," & otherPt(1) & "," & otherPt(2))
                            End If
                    Next
            objStream1.WriteLine() 
            Next
           
           
    End Function

    Function writeRepelor(pts, objStream1)
           
            Dim ptsLoc, i, attributes
           
            ' write type definition
            objStream1.WriteLine("type repelor")

            ' loop to get point locations + point weight
            For i = 0 To UBound(pts)
                           
                    ' get point coord
                    ptsLoc = Rhino.PointCoordinates(pts(i))

                    'attributes = attString(pts(i))
                   
                    ' add to text file
                    objStream1.WriteLine(ptsLoc(0) & "," & ptsLoc(1) & "," & ptsLoc(2))
                    'objStream1.WriteLine(ptsLoc(0) & "," & ptsLoc(1) & "," & ptsLoc(2))
                   
            Next

    End Function


    Function writeLines(lines, objStream1)

            Dim i, j, ptsLoc, vtx, attributes, points, endPt, strPoints, p

            ' write type definition
            objStream1.WriteLine("type lines")
           
            Rhino.Print UBound(lines)
           
            For i = 0 To UBound(lines)
                    'Rhino.Print i
                    'attributes = attString(attractors(i))
                    strPoints = ""
                    ' get vertices
                    points = Rhino.CurveEditPoints(lines(i))
                    For j = 0 To UBound(points)
                            p = points(j)
                            strPoints = strPoints & p(0) & "," & p(1) & "," & p(2) & " "
                    Next
                   
                    ' add to text file
                    objStream1.WriteLine(strPoints)'
                   
            Next
           
    End Function


    Function writeCameras(objStream1)

            Dim camLens, ct, upVec
           
            ' write type definition
            objStream1.WriteLine("type cameras")   
           
            camLens = Rhino.ViewCameraLens()
            ct = Rhino.ViewCameraTarget()
            upVec = Rhino.ViewCameraup()
           
            objStream1.WriteLine(ct(0)(0) & "," & ct(0)(1) & "," & ct(0)(2) & " " & ct(1)(0) & "," & ct(1)(1) & "," & ct(1)(2) & " " & upVec(0) & "," & upVec(1) & "," & upVec(2) & " " & camLens)

    End Function
Recommend

Why are these buttons gray?