Summary: Demonstrates how to make a simple command line calculator using RhinoScript.
Over the years, we have received a number of requests for a command line calculator. That is, a feature that would allow one to perform mathematical calculations 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 be able 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 functionality with a little creativity and by using RhinoScript.
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:
_-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 )
_-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 the following:
As you can see, there are two version of our command line calculator: the left mouse button command displays a window in which you can enter your expression, and the right mouse button command allows you to enter an expression on the command line.
To test our command line calculator, run the Circle command, pick a 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 using RhinoScript to evaluate the expression, we can use all of the mathematical operators supported RhinoScript. Check the RhinoScript] help file to see what is available.
When using the right mouse button version of the command line calculator, make sure not to separate variables and operators with spaces as this will make Rhino's string parser think that you are finished typing.