🤑 SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now
Ruby - find a model's maximum height
-
Hello, I have searched google tirelessly for the past two hours and have not found anything remotely like I am trying to do, so I appeal to the great minds of sketchucation.
I started ruby programming tonight so know very little. I am trying to create a ruby script which finds and saves the maximum height, length, and width of a complicated model (eg. The statue of liberty, or something) to a text file. Sounds simple, and I think it is, but I have no idea were to start.
If someone could point me in the right direction I would be very grateful.
Thaje
-
=begin (c) TIG 2010 Copy/Paste code into ../Plugins/modelXYZ.rb Usage; type 'modelXYZ' in Ruby Console =end require 'sketchup.rb' def modelXYZ() model = Sketchup.active_model bb = model.bounds ### NB; 'bounds' xyz values do NOT follow model axes logic x = bb.width.to_s y = bb.height.to_s z = bb.depth.to_s ### message in Ruby Console puts("X = " + x) puts("Y = " + y) puts("Z = " + z) ### text into File if model.path != "" ### SKP was saved filepath = File.dirname(model.path) filename = model.title + "_XYZ.txt" file = File.join(filepath, filename).tr("\\","/") f = File.new(file, "w") f.puts(x) f.puts(y) f.puts(z) f.close ### Tip; to make a 'spreadsheet' file change suffix to ".csv" or ".tsv" ### and then 'puts' one line only, thus; f.puts(x+","+y+","+z) puts("XYZ values written to file;") puts(file) else ### SKP is not yet saved UI.beep puts("Save this SKP to write these XYZ values to a file...") end#if puts() end#def
-
@tig said:
=begin > (c) TIG 2010 > Copy/Paste code into ../Plugins/modelXYZ.rb > Usage; type 'modelXYZ' in Ruby Console > =end > require 'sketchup.rb' > def modelXYZ() > model = Sketchup.active_model > bb = model.bounds > ### NB; 'bounds' xyz values do NOT follow model axes logic > x = bb.width.to_s > y = bb.height.to_s > z = bb.depth.to_s > ### message in Ruby Console > puts("X = " + x) > puts("Y = " + y) > puts("Z = " + z) > ### text into File > if model.path != "" ### SKP was saved > filepath = File.dirname(model.path) > filename = model.title + "_XYZ.txt" > file = File.join(filepath, filename).tr("\\","/") > f = File.new(file, "w") > f.puts(x) > f.puts(y) > f.puts(z) > f.close > ### Tip; to make a 'spreadsheet' file change suffix to ".csv" or ".tsv" > ### and then 'puts' one line only, thus; f.puts(x+","+y+","+z) > puts("XYZ values written to file;") > puts(file) > else ### SKP is not yet saved > UI.beep > puts("Save this SKP to write these XYZ values to a file...") > end#if > puts() > end#def >
Thanks man, this looks exactly like what I'm after. Now I just have to work through and understand it...
Advertisement