• Login
sketchucation logo sketchucation
  • Login
⚠️ Libfredo 15.4b | Minor release with bugfixes and improvements Update

How to pause a ruby script for 1 second

Scheduled Pinned Locked Moved Developers' Forum
25 Posts 5 Posters 5.0k Views 5 Watching
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.
  • N Offline
    N Lindenthal
    last edited by Aug 18, 2008, 12:52 PM

    Yes, TDB, that I could know myself. Excuse me, please.

    @Jim
    Your times are the same with me.
    How can I get ==> 0.5 or ==> 0.99?

    Mac OS X 10.4.11

    1 Reply Last reply Reply Quote 0
    • T Offline
      tbd
      last edited by Aug 18, 2008, 1:24 PM

      yup. my bad. I checked only visual and didnt do any time checks.

      seems that it ignores decimals (0.1...0.9 = 0, 1.1 ... 1.9 = 1) - so we need to use that. for interface like clear selection doesnt matter that much anyway

      SketchUp Ruby Consultant | Podium 1.x developer
      http://plugins.ro

      1 Reply Last reply Reply Quote 0
      • N Offline
        N Lindenthal
        last edited by Aug 18, 2008, 5:43 PM

        To pause is a workaround. Would be better to ask how long

        Morisdov.transf_get("transf2")
        
        

        works and then clear without pause/delay.

        Can ruby know how long the earlier line works?

        1 Reply Last reply Reply Quote 0
        • A Offline
          AlexMozg
          last edited by Aug 18, 2008, 7:46 PM

          Here such variant:

          
          time_limit = 0.2
          @val_time = Time.now
          @timer = UI.start_timer(0, true) do
          	if Time.now - @val_time >= time_limit
          		UI.stop_timer @timer
          		Sketchup.active_model.selection.clear
          		@val_time = @timer = nil
          	end
          end
          
          
          

          It works 😄

          1 Reply Last reply Reply Quote 0
          • N Offline
            N Lindenthal
            last edited by Aug 18, 2008, 8:33 PM

            @AlexMozg

            Unfortunately it does not work, if this line is before

            Morisdov.transf_get("transf2")
            
            

            Please try with proper_animation.rb and one selected component with Position 2

            Morisdov.transf_get("transf2")
            time_limit = 0.2
            @val_time = Time.now
            @timer = UI.start_timer(0, true) do
               if Time.now - @val_time >= time_limit
                  UI.stop_timer @timer
                  Sketchup.active_model.selection.clear
                  @val_time = @timer = nil
               end
            end
            

            The selected component will stay selected.

            1 Reply Last reply Reply Quote 0
            • T Offline
              tbd
              last edited by Aug 18, 2008, 8:40 PM

              welcome to thread synchronization fun - as you can see Morisdov.transf_get uses start_timer
              so you need to sync with that (e.g. add global variable in transf_get end for example)

              SketchUp Ruby Consultant | Podium 1.x developer
              http://plugins.ro

              1 Reply Last reply Reply Quote 0
              • N Offline
                N Lindenthal
                last edited by Aug 18, 2008, 8:49 PM

                How?

                1 Reply Last reply Reply Quote 0
                • T Offline
                  tbd
                  last edited by Aug 18, 2008, 9:00 PM

                  btw, did you increased time_limit from AlexMozg example first ? to like 5 ?

                  SketchUp Ruby Consultant | Podium 1.x developer
                  http://plugins.ro

                  1 Reply Last reply Reply Quote 0
                  • A Offline
                    AlexMozg
                    last edited by Aug 18, 2008, 10:17 PM

                    And it is possible and so:

                    
                    ...
                    ...
                    def self.timer(proc, time_limit=0.5)
                    $val_time = Time.now
                    $timer = UI.start_timer(0, true) do
                       if Time.now - $val_time >= time_limit
                          UI.stop_timer $timer
                          proc.call
                          $val_time = $timer = nil
                       end
                    end
                    end
                    ...
                    select_comp_by_name("Fahrstuhl")
                    Morisdov.transf_get("transf2")
                    self.timer(proc{Sketchup.active_model.selection.clear})
                    ...
                    
                    
                    

                    Though it is possible to do without global variables.
                    But, I do not know that Morisdov.transf_get does.
                    ;

                    1 Reply Last reply Reply Quote 0
                    • N Offline
                      N Lindenthal
                      last edited by Aug 19, 2008, 6:58 AM

                      def self.timer(proc, time_limit=0.9)
                      $val_time = Time.now
                      $timer = UI.start_timer(0, true) do
                         if Time.now - $val_time >= time_limit
                            UI.stop_timer $timer
                            proc.call
                            $val_time = $timer = nil
                         end
                      end
                      end
                      select_comp_by_name("Fahrstuhl")
                      Morisdov.transf_get("transf2")
                      self.timer(proc{Sketchup.active_model.selection.clear})
                      

                      With 0.9 it works for exactly one time, each next start lets Sketchup crash down.
                      With 0.2 SketchUp crashes immediately.

                      I think I have to ask Morisdov by PM.

                      1 Reply Last reply Reply Quote 0
                      • A Offline
                        AlexMozg
                        last edited by Aug 20, 2008, 6:32 AM

                        @n lindenthal said:

                        ...
                        I think I have to ask Morisdov by PM.

                        Ask me, and I shall try to answer.

                        1 Reply Last reply Reply Quote 0
                        • N Offline
                          N Lindenthal
                          last edited by Aug 22, 2008, 7:12 AM

                          @alexmozg said:

                          … Ask me, and I shall try to answer.

                          Here is my »Fahrstuhl«, english elevator.

                          My script selects the "Fahrstuhl“, puts it to another position (here position 1 and 2), and then clears the selection:

                          select_comp_by_name("Fahrstuhl")
                          Morisdov.transf_get("transf1")
                          UI.start_timer(1, false) {Sketchup.active_model.selection.clear}
                          

                          Position 1

                          select_comp_by_name("Fahrstuhl")
                          Morisdov.transf_get("transf2")
                          UI.start_timer(1, false) {Sketchup.active_model.selection.clear}
                          

                          Position 2

                          To get the script work, the following .rbs must be installed:

                          1. select_com_by_name.rb
                          def select_comp_by_name(comp_name)
                          	model = Sketchup.active_model
                          	sel = model.selection
                          	sel.clear
                          	if compdef = Sketchup.active_model.definitions[comp_name]
                          		sel.add(compdef.instances)
                          	else
                          		Sketchup.active_model.active_entities.each do |d|
                          			if d.typename == "ComponentInstance"
                          			sel.add d if d.name==comp_name
                          		end
                          	end
                          		if sel.count == 0
                          			UI.beep
                          		else
                          			UI.messagebox(sel.count.to_s + " gesuchte Dinger gefunden.")
                          		end
                          	end
                          end
                          
                          1. proper_animation.rb

                          My question is, how to select the "Fahrstuhl/elevator" without selection marks.

                          So I tried to use the following script:

                          def self.timer(proc, time_limit=1)
                          $val_time = Time.now
                          $timer = UI.start_timer(0, true) do
                             if Time.now - $val_time >= time_limit
                                UI.stop_timer $timer
                                proc.call
                                $val_time = $timer = nil
                             end
                          end
                          end
                          select_comp_by_name("Fahrstuhl")
                          Morisdov.transf_get("transf2")
                          self.timer(proc{Sketchup.active_model.selection.clear})
                          

                          Would be a wonder to get the arrows in model be buttons, which start the elevator scripts.


                          Fahrstuhl = Elevator

                          1 Reply Last reply Reply Quote 0
                          • A Offline
                            AlexMozg
                            last edited by Aug 23, 2008, 5:30 AM

                            @unknownuser said:

                            My question is, how to select the "Fahrstuhl/elevator" without selection marks.

                            The simplest - to make changes to a script "Morisdov".
                            Somehow here so:

                            
                            def Morisdov.transf_get(dict_key, ss=nil)
                              model=Sketchup.active_model					
                              ents=model.active_entities
                               if ss
                                 @ents = ss
                               else
                                 @ents=[]
                                 model.selection.each {|e| @ents.push(e)}
                               end											
                              @ents.freeze
                              model.selection.clear
                            ....
                            ....
                            end
                            
                            

                            And completely to get rid from selection.

                            Where I can find "following.rbs"?
                            I shall think...

                            1 Reply Last reply Reply Quote 0
                            • N Offline
                              N Lindenthal
                              last edited by Aug 23, 2008, 7:10 AM

                              @alexmozg said:

                              @unknownuser said:

                              …And completely to get rid from selection.

                              Where I can find "following.rbs"?
                              I shall think...

                              Yes, rid from selection …

                              … but how? This way?

                              select_comp_by_name("Fahrstuhl")
                              def Morisdov.transf_get(dict_key, ss=nil)
                                model=Sketchup.active_model               
                                ents=model.active_entities
                                 if ss
                                   @ents = ss
                                 else
                                   @ents=[]
                                   model.selection.each {|e| @ents.push(e)}
                                 end                                 
                                @ents.freeze
                                model.selection.clear
                              end
                              

                              (my poor english) Which "following.rbs" you need?

                              1 Reply Last reply Reply Quote 0
                              • A Offline
                                AlexMozg
                                last edited by Aug 25, 2008, 4:44 AM

                                @unknownuser said:

                                Yes, rid from selection …

                                … but how? This way?

                                
                                def select_comp_by_name(comp_name)
                                   model = Sketchup.active_model
                                   if compdef = Sketchup.active_model.definitions[comp_name]
                                      sel = compdef.instances
                                   else
                                      sel=[]
                                      Sketchup.active_model.active_entities.each do |d|
                                         if d.typename == "ComponentInstance"
                                         sel << d if d.name==comp_name
                                      end
                                   end
                                      if sel.count == 0
                                         UI.beep
                                      else
                                         UI.messagebox(sel.count.to_s + " gesuchte Dinger gefunden.")
                                      end
                                   end
                                sel
                                end
                                
                                def Morisdov.transf_get(dict_key, ss=nil)
                                  model=Sketchup.active_model               
                                  ents=model.active_entities
                                   if ss
                                     @ents = ss
                                   else
                                     @ents=[]
                                     model.selection.each {|e| @ents.push(e)}
                                   end                                 
                                  @ents.freeze
                                  model.selection.clear
                                  ...
                                  ...<<original code>>...
                                  ...
                                end
                                
                                
                                
                                
                                

                                Use:

                                1. ss = select_comp_by_name("Fahrstuhl")
                                2. Morisdov.transf_get(dict_key, ss)

                                @unknownuser said:

                                (my poor english) Which "following.rbs" you need?

                                Here this:

                                @unknownuser said:

                                ...Position 2
                                To get the script work, the following .rbs must be installed:

                                ????

                                1 Reply Last reply Reply Quote 0
                                • Chris FullmerC Offline
                                  Chris Fullmer
                                  last edited by Feb 16, 2009, 5:36 AM

                                  I know this is old, but I was needing an answer to something similar today, and this thread over-complexificated the issue for me. I just wanted to add a short .2 second delay before carrying on with my script. This is what I've come up with and it worked for me. No need to invoke the UI.timer

                                  time_mark = ( Time.now + 0.2 )
                                  while time_mark > Time.now
                                  end
                                  

                                  Hopefully thats helpful if anyone stumbles on this thread,

                                  Chris

                                  Lately you've been tan, suspicious for the winter.
                                  All my Plugins I've written

                                  1 Reply Last reply Reply Quote 0
                                  • T Offline
                                    tbd
                                    last edited by Feb 16, 2009, 7:18 AM

                                    Chris: i hope your code is called only by the user and not in an Observer or right click menus, because it will stop Ruby thread and if you call this several times it will block a lot of things.

                                    SketchUp Ruby Consultant | Podium 1.x developer
                                    http://plugins.ro

                                    1 Reply Last reply Reply Quote 0
                                    • Chris FullmerC Offline
                                      Chris Fullmer
                                      last edited by Feb 16, 2009, 7:26 AM

                                      Ahh, I see. I think its ok in this instance to stop up ruby entirely. I was having very little luck getting it to work any other way. I just kept sending SU into a deeep coma.

                                      Maybe I'll try to post my broken code using the timer and try to get that working instead then. Thanks Tavi,

                                      Chris

                                      Lately you've been tan, suspicious for the winter.
                                      All my Plugins I've written

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

                                      Advertisement