GetNumber() with toggle option like Circle radius/diameter

Hi all,

Looking for a smaple of how to program RhinoCommon GetNumber with a single toggle option like the Rhino command Circle has for Diameter./Radius. I know how to do this with AddOptionToggle, but it doesn’t qork quite the same way with the command-line prompts - which is what I want to figure out.

The Rhino command looks like this:

Radius <10.000> ( Diameter ...) (there are other toggles)

if you hit the “Diameter” link, it changes to “Radius”, the prompt changes to “Diameter”, and the default number doubles:

Diameter <20.000> ( Radius...)

With what I can program currently - according to various samples here and there - I always end up having something like this:

Current Value <10.000> (Value=Diameter)

The “Current Value” prompt doesn’t change and I always have the toggle that reads Value=Diameter (or Radius), whereas I would like to just have “Diameter” or “Radius” without the “Something=” part.

Thanks, --Mitch

Hi Mitch

What about this ?

def main():
  rad = 50
  radius = True
  while True:
    scriptcontext.escape_test()
    gen = Rhino.Input.Custom.GetNumber()
    gen.AcceptNothing( True )
    if radius:
      gen.SetCommandPrompt( 'Radius <%g>' % rad )
    else:
      gen.SetCommandPrompt( 'Diameter <%g>' % ( rad * 2 ) )
    # add option
    if radius:
      optind = gen.AddOption( 'Diameter' )
    else:
      optind = gen.AddOption( 'Radius' )
    # get input
    gen.Get()
    res = gen.Result()
    if res == Rhino.Input.GetResult.Cancel:
      return
    elif res == Rhino.Input.GetResult.Nothing:
      break
    elif res == Rhino.Input.GetResult.Option:
      radius = not radius
    elif res == Rhino.Input.GetResult.Number:
      num = gen.Number()
      print 'Number = %g' % num
      return

main()

Well … it does not use a toggle option, it’s just built using ‘low level’ code, so to speak.
I often like building inputs this way in my scripts, it’s very flexible and even lets you do things beyond (or simply different from) what regular Rhino commands do.

HTH Cheers

Hey, thanks for the example Emilio… I always tend to forget the fact that you can work with string options when all else fails…

Cheers and happy holidays, --Mitch

Thank you, Mitch.
Merry Christmas !

Hi Mitch,

Here is an alternative to working with strings:

import Rhino

def GetRadius(radius):
    radius_mode = True
    
    gn = Rhino.Input.Custom.GetNumber()
    gn.SetLowerLimit(0.0, True)
    gn.AcceptNothing(True)
    
    while True:
        gn.ClearCommandOptions()

        if radius_mode:
            gn.SetCommandPrompt("Radius")
            gn.SetDefaultNumber(radius)
            gn.AddOption("Diameter")
        else:
            gn.SetCommandPrompt("Diameter")
            gn.SetDefaultNumber(radius * 2.0)
            gn.AddOption("Radius")
        
        res = gn.Get()

        if res == Rhino.Input.GetResult.Option:
            radius_mode = not radius_mode
            continue
        elif res == Rhino.Input.GetResult.Nothing:
            return radius
        elif res != Rhino.Input.GetResult.Number:
            return Rhino.RhinoMath.UnsetValue
        
        value = gn.Number()
        if radius_mode:
            return value
        return value * 0.5


radius = GetRadius(50.0)
if Rhino.RhinoMath.IsValidDouble(radius):
    print radius

Cool, thanks Dale! --Mitch