Stopping script execution

Stopping script execution

Postby Fancey » Thu Dec 28, 2017 1:33 pm

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
0

Fancey 
 

Re: Stopping script execution

Postby driven » Thu Dec 28, 2017 7:32 pm

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
1
learn from the mistakes of others, you may not live long enough to make them all yourself...

driven 
PluginStore Author
PluginStore Author
 

Re: Stopping script execution

Postby Fancey » Fri Dec 29, 2017 10:52 am

Hi John,

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

Everett
0

Fancey 
 

Re: Stopping script execution

Postby Fancey » Thu Jan 04, 2018 7:43 pm

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
require "CSV"
require "JSON"

# take a working copy of the file
# check each time through
unless File.exist? <filename>
#take a copy

end
# load the lines into an array
line_arr = CSV.read(<filename>)

# convert points from string to array of floats
# this caused the most grief until I got it right
line_arr.map! {|line| line.map {|pt| JSON.parse(pt)}}

#draw a line
ent.add_edges line_arr[0]

#dump the line
line_arr.shift

#replace the array in the file after checking for last line
if <last line>
File.delete(<filename>)

else
CSV.open(<filename>,"wb") do |csv|
[pre]line_arr.each{|x| csv<< x}

end[/pre]
end
#use the up arrow to execute as many times as lines
0

Fancey 
 

Re: Stopping script execution

Postby driven » Fri Jan 05, 2018 7:01 pm

it looks a bit over complicated...

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

Code: Select all
@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...

Code: Select all
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 )
0
learn from the mistakes of others, you may not live long enough to make them all yourself...

driven 
PluginStore Author
PluginStore Author
 

Re: Stopping script execution

Postby Fancey » Sat Jan 06, 2018 1:31 am

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

Fancey 
 

SketchUcation One-Liner Adverts

by Ad Machine » 5 minutes ago



Ad Machine 
Robot
 



 

Return to Developers' Forum

Who is online

Users browsing this forum: No registered users and 9 guests

Visit our sponsors: