Calling function for command line options


(Vincent Shen) #1

I have the below python code in which I implement a function _options that can prompt command line options. I need to call the function twice to get different objects. However, it only prompts once, and for the second time, it directly get the result from the first one without prompting anything for input.

Can anyone help me out with this? Thanks!

import rhinoscriptsyntax as rs
import Rhino

def _xx():
    def _options(msg, dft):
        """
        Use command line options as input
        args:
            msg: String.
            dft: Int. 1 or 0.
        Return:
            List of GUIDs
        """
        # Init base getter
        gi = Rhino.Input.Custom.GetObject()
        gi.SetCommandPrompt(msg)
        gi.GroupSelect = True
        gi.AlreadySelectedObjectSelect = False

        # set up the options
        boolOption = Rhino.Input.Custom.OptionToggle(bool(dft), "False", "True")
        gi.AddOptionToggle("Meshed", boolOption) # Hyphen not allowed

        # set filter based on options
        if boolOption.CurrentValue:
            gi.GeometryFilter = Rhino.DocObjects.ObjectType.Mesh
        else:
            gi.GeometryFilter = Rhino.DocObjects.ObjectType.Surface | Rhino.DocObjects.ObjectType.Brep

        while True:
            get_rc = gi.GetMultiple(1,0)
            if gi.CommandResult()!=Rhino.Commands.Result.Success:
                # return None if nothing selected
                return
            elif get_rc == Rhino.Input.GetResult.Option:
                # set filter based on options
                if boolOption.CurrentValue:
                    gi.GeometryFilter = Rhino.DocObjects.ObjectType.Mesh
                else:
                    gi.GeometryFilter = Rhino.DocObjects.ObjectType.Surface | Rhino.DocObjects.ObjectType.Brep
                continue
            break
        ids = [gi.Object(i).ObjectId for i in range(gi.ObjectCount)]
        newdft = int(boolOption.CurrentValue)
        return ids, newdft

    # read default option values
    a = 0
    b = 0

    # Get ids
    result1 = _options("select A surfaces", a)
    result2 = _options("select B surfaces", b)

    print result1
    print result2

    return

if __name__ == "__main__":
	_xx()

(Vincent Shen) #2

I also tried to do gi.Dispose() at the end of the _options function. It does not help.


(Vincent Shen) #3

Solved. You need to do:

scriptcontext.doc.Objects.UnselectAll()
scriptcontext.doc.Views.Redraw()

to prevent preselected object, so that the second prompt won’t automatically go through.

However, I don’t quite understand what .AlreadySelectedObjectSelect property is doing here.

Cheers.


(Dale Fugier) #4

If all you really want to do is have the user pick some options, then instead of using a GetObject object, use a GetOption object.

http://developer.rhino3d.com/api/RhinoCommonWin/html/T_Rhino_Input_Custom_GetOption.htm

– Dale


(Vincent Shen) #5

Thank you Dale. Indeed I do need to get object along with options. But it is good to know I can use GetOption alone.

Cheer,