Python raw_input OR rs.GetObject prompt

Hello everyone,

I’m working on a script to find and replace text throughout a document (we do all of our shop drawings in Rhino, so if a material spec changes, that could mean sifting through 40+ pages of layouts to make the revisions.)

I have the base functionality sorted out, and am now trying to make the script a little more robust. Part of that is allowing the user to enter a String in the command line, or simply selecting the text object they’d like to replace.
I know how to do each of these individually, but is there any way to combine these into a single command prompt? i.e.

“Enter text, or select text object?”

i know i could do this with an additional prompt level (“1 to enter text, or 2 to select text object.”), but wondering if it could be more streamlined for the user.

Hi Adam

I think you’ll need RhinoCommon for this.

http://4.rhino3d.com/5/rhinocommon/

Here is a quick sample

import Rhino

def main():
  gob = Rhino.Input.Custom.GetObject()
  gob.AcceptNothing( True )
  gob.AcceptString( True )
  gob.GeometryFilter = Rhino.DocObjects.ObjectType.Annotation
  gob.SetCommandPrompt( 'Pick text object or enter text' )
  gob.Get()
  result = gob.Result()
  if result == Rhino.Input.GetResult.Cancel:
    return
  if result == Rhino.Input.GetResult.Nothing:
    return
  elif result == Rhino.Input.GetResult.String:
    tex = gob.StringResult()
    print( 'Text has been written: %s' % tex )
  elif result == Rhino.Input.GetResult.Object:
    obref = gob.Object( 0 )
    texent = obref.TextEntity()
    tex = texent.Text
    print( 'Text object has been picked: %s' % tex )

main()

HTH, regards

Thanks Emilio,

I figured I would be able to do this through RhinoCommon, but I’m new to python and not yet sure of the RhinoCommon Python relationship and syntax. I’ll try plugging this into my script and let you know how it works. I’ll post the script/plugin once it’s ready as well.

Thanks Again,

Adam

IMO RhinoCommon input classes are not so easy to use and understand at first, but are very powerful
and let you customize user input as you like.
You can find better samples here:

About RhinoCommon and Python in general, I am no expert, but as far as I understand it’s just object oriented Python.
RhinoCommon is a collection on Python classes. Once you are comfortable with Python classes, you can use RhinoCommon without problems … well, except maybe for its not so good documentation … :wink:

Regards

Well, I’ve got this integrated into my script beautifully except for one thing. I can’t figure out how to ,make rhino accept ‘space’ as a valid character. Say i want to replace the text “two words”. as soon as i hit the space bar, it moves on to the next step in the script. i know i can enter the command in quotes to avoid this, but i’d rather not force the user into that situation, as i think it will cause confusion.

Surround text with spaces an extra set of double-quote characters.

Yeah, i’m trying to avoid having to convey that to the user, as it’s not really intuitive. I’m looking for a way to make spaces work via something along the lines of AcceptLiteralString(), while maintaining the ability to select an object.

Hi Adam,

I think it should be like below ( switch the double and single quotes’

 gob.SetCommandPrompt( '"Type or select text to replace"' )

-Willem

for some reason, it seemed like it worked initially, but upon repetition, it doesn’t change the default behavior.