Custom Input

Using Python/Rhinoscript.

I am using a custom input to get multiple curves with a toggle option. If I select a curve and then change the toggle, it deselects the original curve that was selected so if I select more curves only the newly selected curves are captured. How do I preserve what was selected prior to toggling the option so when other curves are selected they are all captured?

import Rhino
import rhinoscriptsyntax as rs
import Rhino.Geometry as rg

if __name__ == '__main__':
    DrawSurfaces = True

    cmd = Rhino.Input.Custom.GetObject()
    cmd.SetCommandPrompt('Select the curves.')
    cmd.GeometryFilter = Rhino.DocObjects.ObjectType.Curve
    
    opDrawSurfaces = Rhino.Input.Custom.OptionToggle(DrawSurfaces, 'No', 'Yes')
    cmd.AddOptionToggle('DrawSurfaces', opDrawSurfaces)

    Done = False
    
    while not Done:
        result = cmd.GetMultiple(1, 0)
        
        if result == Rhino.Input.GetResult.Option:
            DrawSurfaces = opDrawSurfaces.CurrentValue

        else: Done = True

    print 'Draw Surfaces Option: ' + str(DrawSurfaces)

    for obj in cmd.Objects():
        print obj.ObjectId

Hi MIke - something like this, I think

import Rhino
import scriptcontext as sc
import rhinoscriptsyntax as rs


def test():

    ids = None
    
    go = Rhino.Input.Custom.GetObject()
    go.EnablePreSelect(False, False)
    go.GeometryFilter = Rhino.DocObjects.ObjectType.Curve

    while True:
        go.ClearCommandOptions()
        
        #Objects selected so far
        ids = [go.Object(i).ObjectId for i in range(go.ObjectCount)] 
        
        #highlight selected objects
        if ids:
            for id in ids:
                obj = sc.doc.Objects.Find(id)
                obj.Highlight(True)
            sc.doc.Views.Redraw()


        #set your default for the toggle
        DrawSurfaces = False 
        
        #use this to keep track of what the setting was last
        if "DRAW_SURFS" in sc.sticky:
            DrawSurfaces = sc.sticky["DRAW_SURFS"]
                    
        go.SetCommandPrompt("Select curves.")
        
        opDrawSurfaces = Rhino.Input.Custom.OptionToggle(DrawSurfaces, 'No', 'Yes')
        go.AddOptionToggle('DrawSurfaces', opDrawSurfaces)
        
        
        #######
        #######
        #This is key, so as not to lose the selection:
        go.EnableClearObjectsOnEntry(False)
        #######
        #######
        
        
        rc = go.GetMultiple(1,0)

        if go.CommandResult()!=Rhino.Commands.Result.Success:
            return go.CommandResult()
            
        if rc==Rhino.Input.GetResult.Object:
            ids = [go.Object(i).ObjectId for i in range(go.ObjectCount)]
            break

        elif rc==Rhino.Input.GetResult.Option:
            ids = [go.Object(i).ObjectId for i in range(go.ObjectCount)]
            
            DrawSurfaces = opDrawSurfaces.CurrentValue
            sc.sticky["DRAW_SURFS"] = DrawSurfaces
            continue
            
    print ("DrawSurfaces = " + str(DrawSurfaces))
    if not ids: return
    
    print (len(ids))
    
    #Un-highlight
    for id in ids:
        obj = sc.doc.Objects.Find(id)
        obj.Highlight(False)
        sc.doc.Views.Redraw()
        
        
    #Select   if needed
    for id in ids:
        sc.doc.Objects.Select(id)

    sc.doc.Views.Redraw()

if __name__=="__main__":
    test()

-Pascal

1 Like

@pascal - This almost works 100%. Thanks for the code. The interesting thing is that prior to changing the DrawSurfaces toggle I can deselect any object that I have selected. Once I change the toggle I can no longer deselect any of the previously selected items. If I select additional items I can deselect those until I change the toggle again. Is this a bug where if you highlight an object via code that you can not deselect it?

I did a test run by highlighting two objects via the Highlight method and discovered that the objects cannot be deselected unless you either select the highlighted object again or select all objects and then deselect it. Not even ESC works to clear the object selection. Even if you select other objects and then press ESC it clears the newly selected objects but leaves the original highlighted (via code) objects selected. Sounds like it might be a bug/issue. I would think that highlighting via code would cause the object to act the exact same way compared to the user selecting the object. Please advise.

    obj = sc.doc.Objects.Find(sys.Guid('2ba575a5-2da9-49e0-9abd-07271fbb475f'))
    obj.Highlight(True)
    obj = sc.doc.Objects.Find(sys.Guid('a037145b-bbf1-42c0-8cd6-b6bccd732431'))
    obj.Highlight(True)
    sc.doc.Views.Redraw()

I thought I would replace Highlight(True) with Select(True, True). While this works when finding objects by their Guid, it would not work with the custom input.

Scratch the issue with Highlight and Select. I rebooted and now Select works just fine.