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...
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better đź’—
Register LoginAdvertisement