• Login
sketchucation logo sketchucation
  • Login
🤑 SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

Stopping script execution

Scheduled Pinned Locked Moved Developers' Forum
6 Posts 2 Posters 2.6k 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.
  • F Offline
    Fancey
    last edited by 28 Dec 2017, 12:33

    I have a number of 3D lines which I can load from a file and draw in the Sketchup window with a script via the ruby console. I would like to be able to step through the lines one at a time. I suspect it's not available because of the working environment. gets, chomp, STDIN does not work.

    Code would look like this:

    Until no_more_lines
    
        ent.add_edges line
        #wait for key board activity at the console
    
     next # line
    

    Any suggestions appreciated

    1 Reply Last reply Reply Quote 0
    • D Offline
      driven
      last edited by 28 Dec 2017, 18:32

      use IO::readlines to create an array...

      create a method that uses one pair of points and then removes them from the array...

      call the method...

      use the UP arrow and return in Ruby Console to call again...

      rinse and repeat until array is empty...

      add an example text file for more explicit code...

      john

      learn from the mistakes of others, you may not live long enough to make them all yourself...

      1 Reply Last reply Reply Quote 0
      • F Offline
        Fancey
        last edited by 29 Dec 2017, 09:52

        Hi John,

         Thanks for the suggestion.  Will see what I can do with it.
        

        Everett

        1 Reply Last reply Reply Quote 0
        • F Offline
          Fancey
          last edited by 4 Jan 2018, 18:43

          Hi John,

          Got it working. Thanks for pointing me in the right direction. Had to do a bit of learning along the way. I used the CSV and JSON classes. My main script read the original data, processed it into 3d points, and produced an array of lines. After I got all the lines working, I added a block to write the array to file. The second script reads this file, draws a single line, removes it from the array, and writes the array back to file. Here's the pseudo code for the second script:

          access the entities container

          |
          |

          load required classes

          [pre:2k0pic0s]require "CSV"
          require "JSON"[/pre:2k0pic0s]

          take a working copy of the file

          check each time through

          unless File.exist? <filename>
          [pre:2k0pic0s]#take a copy[/pre:2k0pic0s]
          end

          load the lines into an array

          [pre:2k0pic0s]line_arr = CSV.read(<filename>)[/pre:2k0pic0s]

          convert points from string to array of floats

          this caused the most grief until I got it right

          [pre:2k0pic0s]line_arr.map! {|line| line.map {|pt| JSON.parse(pt)}}[/pre:2k0pic0s]
          #draw a line
          [pre:2k0pic0s]ent.add_edges line_arr[0][/pre:2k0pic0s]
          #dump the line
          [pre:2k0pic0s]line_arr.shift[/pre:2k0pic0s]
          #replace the array in the file after checking for last line
          if <last line>
          [pre:2k0pic0s]File.delete(<filename>)[/pre:2k0pic0s]
          else
          [pre:2k0pic0s]CSV.open(<filename>,"wb") do |csv|
          [pre]line_arr.each{|x| csv<< x}[/pre:2k0pic0s]
          end[/pre]
          end
          #use the up arrow to execute as many times as lines

          1 Reply Last reply Reply Quote 0
          • D Offline
            driven
            last edited by 5 Jan 2018, 18:01

            it looks a bit over complicated...

            do you really need JSON when you can use CSV with a options hash...

            @ary = CSV.read( file, { headers; false, converters; ;numeric } )
            

            here's a little experiment to step using the mouse wheel...

            you may need to 'click' to run the gif...

            use_zoom.gif

            change the path to file...

            class UseViewZooming
              require 'CSV'
              # Called when you start the tool
              def activate
                puts "activate called"
                @pt = 0
                file = '/private/tmp/csv_test/test.csv'
                @ary = CSV.read( file, { headers; false, converters; ;numeric } )
                @model = Sketchup.active_model
                @ents  = @model.entities
              end
            
            	# Called when you run out of pts, select another tool or quit SketchUp
            	def deactivate(view)
            		puts "deactivate called"
            	end
            
            	def draw(view)
            		if @pt + 1 == @ary.length
            			@ents.grep(Sketchup;;Edge).last.find_faces
            			return Sketchup.active_model.select_tool( nil)
            		end  
            		pt1 = @ary[@pt]
            		pt2 = @ary[@pt + 1]
            		@ents.add_line([pt1, pt2])
            		@pt +=1
            	 end
            end # end of class UseViewZooming
            	
            Sketchup.active_model.select_tool( UseViewZooming.new )
            

            learn from the mistakes of others, you may not live long enough to make them all yourself...

            1 Reply Last reply Reply Quote 0
            • F Offline
              Fancey
              last edited by 6 Jan 2018, 00:31

              I did think it was a bit complicated myself. Most of my programming experience was years ago on 3rd GL stuff. All this is new, but I do have fairly good programming sense.I did try the CSV modifier, but I wasn't using it correctly. That's when I stumbled on JSON. It's only a one shot deal for me, but I have to admit, I do like Ruby! Will be studying your example.
              Thanks again.

              Everett

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

              Advertisement