Site Tools


Simple Command Line Calculator

Developer: RhinoScript
Summary: Demonstrates how to make a simple command line calculator using RhinoScript.

Over the years, we have received many requests for a command line calculator. That is, a feature that would let one perform mathematical calculation as input to Rhino commands. For example, if you want to create a circle with a radius equal to the square root of 2, you currently have to break out your calculator to determine the radius. Wouldn't it be nice to type something like Sqr(2) on the command line and have Rhino determine the radius for us? Well, we can come close to this with a little creativity and by using RhinoScript.

Example

To create a simple command line calculator, create a new toolbar button and add the following blocks of code in either the left mouse button or right mouse button command window:

Left mouse button command

 _-NoEcho
 _-RunScript (
 If IsEmpty(scr) Or IsNull(scr) Then
 scr = Rhino.StringBox("Expression to evaluate", , "Evaluate")
 Else
 scr = Rhino.StringBox("Expression to evaluate", scr, "Evaluate")
 End If
 If Not IsNull(scr) Then
 val = Eval(scr)
 If Not IsNull(val) Then
 Rhino.SendKeystrokes val
 End If
 End If
 )

Right mouse button command

 _-NoEcho
 _-RunScript (
 If IsEmpty(scr) Or IsNull(scr) Then
 scr = Rhino.GetString("Expression to evaluate")
 Else
 scr = Rhino.GetString("Expression to evaluate", scr)
 End If
 If Not IsNull(scr) Then
 val = Eval(scr)
 If Not IsNull(val) Then
 Rhino.SendKeystrokes val
 End If
 End If
 )

When you are finished, you should have something that looks like:

rseval.jpg

Use

As you can see, there are two versions of our command line calculator: The left mouse button command displays a window in which you can enter your expression. The right mouse button command lets you enter an expression on the command line.

To test our command line calculator, run the Circle command, pick an enter point, and then activate one of our scripts by picking either the left or right mouse button version. When prompted for Expression to evaluate, enter Sqr(2) and either press OK or Enter. As you can see, we now have a circle with a radius of Sqr(2), or 1.41421.

Because our command line calulator uses RhinoScript to evaluate the expression, we can use all the mathematical operators supported in RhinoScript. Check the RhinoScript] help file to see what is available.

Limitations

When using the right mouse button version of the command line calculator, do not separate variables and operators with spaces as this makes Rhino's string parser think that you are finished typing.


developer/scriptsamples/eval.txt ยท Last modified: 2016/01/12 by sandy