Stopping script execution
-
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
-
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
-
Hi John,
Thanks for the suggestion. Will see what I can do with it.
Everett
-
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]
endload 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 -
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...
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 )
-
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
Advertisement