User Tools

Site Tools


wiki:sns:snsscript:solution_sampling

Sampling Example

The following short example demonstrates sampling a Scan&Solve solution on a uniform grid of points and writing the values to a text file.

  • setup (lines 6-23)
  • iteration over bounding box (lines 28-41)
  • querying solution (line 33)

For conciseness, this example contains minimal error checking.

Option Explicit

Call Main()
Sub Main()
        
        Dim objSnSPlugIn, strObject, arrBox, dblSpacing, queryPt, dblValue
        Dim objFSO, objFile, strFileLocation
        Dim x, y, z
        On Error Resume Next
    
        Set objSnSPlugIn = Rhino.GetPluginObject("SnSScript")
        objSnSPlugIn.SetOutputOnOff False
        
        'Prompt for the output filename
        strFileLocation = Rhino.SaveFileName("Output Filename", "*.txt", "", "RS_Output.txt", "txt")
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        If Not IsNull(strFileLocation) Then
                Set objFile = objFSO.CreateTextFile(strFileLocation, True)
        End If
    
        'Prompt for the solid and the sample spacing
        strObject = Rhino.GetObject("Select a solid")
        dblSpacing = Rhino.RealBox("Sample spacing", 0.1, "Enter sample spacing")
        
        If Not IsNull(strObject) Then
                arrBox = Rhino.BoundingBox(strObject)
                If IsArray(arrBox) Then
                        For z = arrBox(0)(2) To arrBox(6)(2) Step dblSpacing
                                For y = arrBox(0)(1) To arrBox(6)(1) Step dblSpacing
                                        For x = arrBox(0)(0) To arrBox(6)(0) Step dblSpacing
                                                queryPt = Array(x, y, z)
                                                'Sample the solution at the specified point and write to a file
                                                dblValue = objSnSPlugIn.QuerySolutionValue(strObject, "DSPT", queryPt)
                                                If Not isNull(objFile) Then
                                                        objFile.WriteLine x & "," & y & "," & z & ":" & dblValue
                                                Else
                                                        Rhino.Print x & "," & y & "," & z & ":" & dblValue
                                                End If
                                        Next
                                Next
                        Next
                End If
        End If
End Sub
Line #Description
1Only scripts with Option Explicit specified can be debugged in the RhinoScript editor (Monkey).
4-8Declare variables before use.
11Obtain a reference to the SnSScript plug-in, exit if an error occurs.
12Turn off SnSScript command line output.
22Prompt the user to select a solid from the Rhino document. The solid is identified by its GUID stored in strObject.
23Prompt the user for a sample spacing.
26Obtain the bounding box for the object.
28-30Three nested loops to cover the bounding box.
31Construct the query point.
32Query the solution value at the point.
34-38Write the value to a file or the command line if the file is not open.
wiki/sns/snsscript/solution_sampling.txt ยท Last modified: 2017/07/14 13:47 by claire