• Login
sketchucation logo sketchucation
  • Login
ℹ️ GoFundMe | Our friend Gus Robatto needs some help in a challenging time Learn More

Problems with PickHelper

Scheduled Pinned Locked Moved Developers' Forum
14 Posts 3 Posters 1.0k Views
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    hsmyers
    last edited by 1 Aug 2016, 17:11

    I'm trying to allow the user to pick two objects with the intent to align the second selection with the first. I'll allow embroidery regarding axis and points later. With that in mind I thought this was a good chance to use PickHelper. So I created a small class as follows:

    
    class HSM_Picker
      def activate
        @@picks = []
        Sketchup.set_status_text('Select an object', SB_PROMPT)
      end
    
      def deactivate(view)
        view.invalidate
      end
    
      def onLButtonUp(_flags, x, y, view)
        ph = view.pick_helper(x, y)
        ph.do_pick(x, y)
        @@picks << ph.best_picked
        if @@picks.length == 2
          p 'done'
          Sketchup.active_model.select_tool(nil)
        end
      end
    
      def onCancel(_flag, _view)
        Sketchup.send_action('selectSelectionTool;')
      end
    end # class HSM_Picker
    
    

    This for reasons unknown to me does not work. In my effort to debug I wrote the following snippet in the console:

    
    x = y = 0
    view = mod.active_view
    ph = view.pick_helper(x, y)
    ph.do_pick(x, y)
    best_entity = ph.best_picked
    
    

    As you might guess this did not work either. 😞

    So what am I doing wrong? Efforts to amend my abysmal ignorance will be deeply appreciated!!

    1 Reply Last reply Reply Quote 0
    • T Offline
      thomthom
      last edited by 1 Aug 2016, 17:52

      @hsmyers said:

      This for reasons unknown to me does not work.

      Can you elaborate to what didn't work and what you expected to see happening?

      Thomas Thomassen — SketchUp Monkey & Coding addict
      List of my plugins and link to the CookieWare fund

      1 Reply Last reply Reply Quote 0
      • H Offline
        hsmyers
        last edited by 1 Aug 2016, 18:21

        @thomthom said:

        Can you elaborate to what didn't work and what you expected to see happening?

        Never having used PickHelper, I really didn't have any expectations other than a vague notion of some indication that the code was in control of the 'pick' process but I saw no such indication during testing. I noticed that what I was printing to the console didn't appear which suggests an error of some sort...which I foolishly failed to save, I clear the console with each run head slap I'm not even all that clear on how I should integrate what I need with the tool under development—roll it into the tool or separate class within?

        1 Reply Last reply Reply Quote 0
        • S Offline
          sdmitch
          last edited by 1 Aug 2016, 19:57

          @hsmyers said:

          @thomthom said:

          Can you elaborate to what didn't work and what you expected to see happening?

          Never having used PickHelper, I really didn't have any expectations other than a vague notion of some indication that the code was in control of the 'pick' process but I saw no such indication during testing. I noticed that what I was printing to the console didn't appear which suggests an error of some sort...which I foolishly failed to save, I clear the console with each run head slap I'm not even all that clear on how I should integrate what I need with the tool under development—roll it into the tool or separate class within?

          In the API the ph = view.pick_helper statement does not does not have variables passed to it. Taking off the (x,y) made it work for me.

          class HSM_Picker
            def activate
              @@picks = []
              Sketchup.set_status_text('Select an object', SB_PROMPT)
            end
          
            def deactivate(view)
              view.invalidate
            end
          
            def onLButtonUp(_flags, x, y, view)
              ph = view.pick_helper
              ph.do_pick(x, y)
              @@picks << ph.best_picked
              if @@picks.length == 2
                p 'done'
                Sketchup.active_model.select_tool(nil)
              end
            end
          
            def onCancel(_flag, _view)
              Sketchup.send_action('selectSelectionTool;')
            end
          end # class HSM_Picker
          Sketchup.active_model.select_tool HSM_Picker.new
          

          Nothing is worthless, it can always be used as a bad example.

          http://sdmitch.blogspot.com/

          1 Reply Last reply Reply Quote 0
          • H Offline
            hsmyers
            last edited by 1 Aug 2016, 21:47

            @sdmitch said:

            In the API the ph = view.pick_helper statement does not does not have variables passed to it. Taking off the (x,y) made it work for me.

            class HSM_Picker
            >   def activate
            >     @@picks = []
            >     Sketchup.set_status_text('Select an object', SB_PROMPT)
            >   end
            > 
            >   def deactivate(view)
            >     view.invalidate
            >   end
            > 
            >   def onLButtonUp(_flags, x, y, view)
            >     ph = view.pick_helper
            >     ph.do_pick(x, y)
            >     @@picks << ph.best_picked
            >     if @@picks.length == 2
            >       p 'done'
            >       Sketchup.active_model.select_tool(nil)
            >     end
            >   end
            > 
            >   def onCancel(_flag, _view)
            >     Sketchup.send_action('selectSelectionTool;')
            >   end
            > end # class HSM_Picker
            > Sketchup.active_model.select_tool HSM_Picker.new
            

            Ah! On such screw-ups as this are founded the bugs that plague us! 👍 👍 👍 😄 Much and many thanks sir.

            1 Reply Last reply Reply Quote 0
            • H Offline
              hsmyers
              last edited by 2 Aug 2016, 13:23

              Back to debugging... The following appears to run without error—or actually without doing anything other than returning "[]"!

              mod = Sketchup.active_model # Open model
              ent = mod.entities # All entities in model
              sel = mod.selection # Current selection
              
              SKETCHUP_CONSOLE.clear
              
              class HSM_Picker
              
                @@picks = []
                
                def activate
                  @@picks = []
                  Sketchup.set_status_text('Select an object', SB_PROMPT)
                end
              
                def deactivate(view)
                  view.invalidate
                end
              
                def onLButtonUp(_flags, x, y, view)
                  ph = view.pick_helper
                  ph.do_pick(x, y)
                  @@picks << ph.best_picked
                  if @@picks.length == 2
                    p 'done'
                    Sketchup.active_model.select_tool(nil)
                  end
                end
              
                def onCancel(_flag, _view)
                  Sketchup.send_action('selectSelectionTool;')
                end
                
                def picks
                  @@picks
                end
              end # class HSM_Picker
              
              pk = HSM_Picker.new()
              ets = pk.picks
              
              

              Implication clearly being that I don't know what I'm doing (certainly true!) So once again begging for clues here... 😞 In line with previous I had expected

              ph.do_pick(x, y)
              

              to politely wait for the user to make a selection (or two) and then be available for interrogation but I fear that may have been naive. My initial understanding ( or miss in this case ) is often warped by my expectations based on years of building APIs more sigh 😞 😞

              1 Reply Last reply Reply Quote 0
              • T Offline
                thomthom
                last edited by 2 Aug 2016, 14:49

                What is the value you get from .best_picked?

                That should only return an entity - so I wouldn't expect @@picks.length to work. (Unless you accidentally picked an edge - in which case you are getting the length of the edge.)

                Thomas Thomassen — SketchUp Monkey & Coding addict
                List of my plugins and link to the CookieWare fund

                1 Reply Last reply Reply Quote 0
                • H Offline
                  hsmyers
                  last edited by 2 Aug 2016, 14:59

                  @thomthom said:

                  What is the value you get from .best_picked?

                  That should only return an entity - so I wouldn't expect @@picks.length to work. (Unless you accidentally picked an edge - in which case you are getting the length of the edge.)

                  Don't know, will find out...oh the joys of print statements! 😄

                  1 Reply Last reply Reply Quote 0
                  • S Offline
                    sdmitch
                    last edited by 2 Aug 2016, 15:19

                    The class HSM_Picker works as a Tool but must be envoked by Sketchup.active_model.select_tool HSM_Picker.new. The problem is getting the two selected entities returned since the normal return is the model object.

                    Nothing is worthless, it can always be used as a bad example.

                    http://sdmitch.blogspot.com/

                    1 Reply Last reply Reply Quote 0
                    • T Offline
                      thomthom
                      last edited by 2 Aug 2016, 15:57

                      @hsmyers said:

                      @thomthom said:

                      What is the value you get from .best_picked?

                      That should only return an entity - so I wouldn't expect @@picks.length to work. (Unless you accidentally picked an edge - in which case you are getting the length of the edge.)

                      Don't know, will find out...oh the joys of print statements! 😄

                      Try the debugger: https://github.com/SketchUp/sketchup-ruby-debugger
                      I prefer RubyMine for debugging: http://forums.sketchup.com/t/please-help-me-to-setup-de-debugger-on-rubymine-for-mac/289

                      Thomas Thomassen — SketchUp Monkey & Coding addict
                      List of my plugins and link to the CookieWare fund

                      1 Reply Last reply Reply Quote 0
                      • H Offline
                        hsmyers
                        last edited by 2 Aug 2016, 19:21

                        @thomthom said:

                        @hsmyers said:

                        @thomthom said:

                        What is the value you get from .best_picked?

                        That should only return an entity - so I wouldn't expect @@picks.length to work. (Unless you accidentally picked an edge - in which case you are getting the length of the edge.)

                        Don't know, will find out...oh the joys of print statements! 😄

                        Try the debugger: https://github.com/SketchUp/sketchup-ruby-debugger
                        I prefer RubyMine for debugging: http://forums.sketchup.com/t/please-help-me-to-setup-de-debugger-on-rubymine-for-mac/289

                        Hmmm...haven't used any, could be interesting...

                        1 Reply Last reply Reply Quote 0
                        • T Offline
                          thomthom
                          last edited by 2 Aug 2016, 20:56

                          So much nicer to debug - no more editing files to add print statements and reloading.

                          Thomas Thomassen — SketchUp Monkey & Coding addict
                          List of my plugins and link to the CookieWare fund

                          1 Reply Last reply Reply Quote 0
                          • H Offline
                            hsmyers
                            last edited by 3 Aug 2016, 14:40

                            As is often the case the problem is simple—code in question works much better if properly invoked:

                            pk = HSM_Picker.new()
                            mod.select_tool(pk)
                            

                            Thanks all, sorry for the stupidity 😞

                            1 Reply Last reply Reply Quote 0
                            • T Offline
                              thomthom
                              last edited by 3 Aug 2016, 15:19

                              No worries - we all learn. Thanks for posting back your solution.

                              Thomas Thomassen — SketchUp Monkey & Coding addict
                              List of my plugins and link to the CookieWare fund

                              1 Reply Last reply Reply Quote 0
                              • 1 / 1
                              1 / 1
                              • First post
                                5/14
                                Last post
                              Buy SketchPlus
                              Buy SUbD
                              Buy WrapR
                              Buy eBook
                              Buy Modelur
                              Buy Vertex Tools
                              Buy SketchCuisine
                              Buy FormFonts

                              Advertisement