Using Wscript object in rhinoscript

Hi scripters,
I’m trying to use the WScript.Timeout function to terminate the execution of a script after a certain time (in sec) but I cant figure out how to get an instance of the Wscript object.
Can someone point me in the right direction?

thanks, Tobias

HI @tobias,

you could create and use the WScript object in a RhinoScript like this:

Option Explicit

Call Main()
Sub Main()
    
    Dim objShell
    
    Set objShell = CreateObject("WScript.Shell")
    If Not IsObject(objShell) Then
        Rhino.Print "Error getting WScript"
        Exit Sub
    End If
    
    objShell.Run "Explorer.exe", 0, vbFalse
    Set objShell = Nothing

End Sub

but you can use the WScript.Timeout function only in a *.vbs script as far as i understand. To make a RhinoScript stop after a certain time you could use a Do / Loop and check the running time. eg.:

Option Explicit

Call Main()
Sub Main()
    
    Dim t, timeout_seconds, elapsed
    
    t = Timer
    timeout_seconds = 2
    
    Do 
        ' do something here
    
        ' stop the loop 
        elapsed = timer - t 
        If elapsed > timeout_seconds Then Exit Do
    Loop
    
    Rhino.Print "Loop stopped after " & elapsed & " seconds"
    
End Sub

@stevebaer formating of Python and RhinoScript code is still broken, therefore i´ve attached the second script as file below.

TimeoutLoop.rvb (324 Bytes)

c.

Hi Clement,
Thanks for your answer. I guess what I want doesnt work in Rhino script… I want a timeout of a GetString. The user has to provide a string and the script should continue after a certain time (in sec) if nothing is entered.
Don’t ask me what for… its a little funny thing, so nothin really needed for work. :wink:

greets, Tobias

Hi Tobias,

i guess you could do this in python using an input box in a WinForm dialog and set up a timeout for the whole dialog. In RhinoScript there is no WinForm access but it could be done similar using a window.Close() call in a Rhino.HtmlBox.

c.

Clement, that sounds interesting. Do you maybe have an example code of a WinForm dialog from a python script?

Hi Tobias,

there are some dialog examples here and here.

For auto closing input string, i´ve quickly hacked below together, it just closes and prints “empty” after 5 seconds if nothing has been entered, otherwise it prints what has been entered.

AutoCloseQuickTest.py (1.1 KB)

There are much better ways to do it for shure, eg. cancel the timout if something is currently entered or cancel the timeout and close if ENTER has been pressed after entering something…

c.

Hey Clement, thanks a bunch! This are great examples. Now all I need is to find some time and start playing with it. :wink:

There is a way to go this in RhinoCommon for a GetString, but that would require python instead of RhinoScript.

1 Like

Yeah… im still programming in rhinoscript. Maybe its about time to change to python once and for all.
What was stopping me up to now is that there is no script compiler for python…

The current script compiler supports python. It has some outstanding bugs that we are still trying to solve, but for the most part it works.

Oh… where to find it? Do you have a link for me?

@stevebaer, thanks for pointing to SetWaitDuration.
@tobias below is a much better python example using the commandline:

import Rhino
    
def GetStringWithTimeout(message, default, milliseconds):
    gs = Rhino.Input.Custom.GetString()
    gs.SetCommandPrompt(message)
    gs.SetDefaultString(default)
    gs.AcceptNothing(True)
    gs.SetWaitDuration(milliseconds)
    gs.Get()
    if gs.CommandResult()==Rhino.Commands.Result.Success:
        return gs.StringResult()
    else:
        return None
    
if __name__=="__main__":
    rc = GetStringWithTimeout("Enter a string", "Default", 5000)
    if rc: print rc

c.

1 Like

Thanks a lot Steve, Clememnt! Like always there is a lot to learn… Python rocks!