Retrieve size sketchup file using ruby
-
The idea is to prevent sketchup from crashing when a plugin is called on a model the size of which is too big for the computer capacity
by showing an error message before the plugin is effectively launched
i didn't find anything in "model info" SU ruby API concerning the file size
is there a way using ruby?
-
The
File.size( filePath )
returns the specified file's size in bytes.
So if your code gets the SKP model.path and it's not empty, then you can get the size of that SKP...
Then do what you want with it...model = Sketchup.active_model path = model.path unless path.empty? size = File.size( path ) UI.messagebox("This SKP is #{size} bytes !") ### or whatever... end
-
OK
i didn't find this information in
http://www.sketchup.com/intl/en/developer/docs/methods.php
nor
http://www.sketchup.com/intl/en/developer/docs/classes.phpThank you, your code works
As long as the path doesn't contains "é" or "è", or characters like that
Using french and meters while programming sketchup, makes it more difficult...
Impossible to detect problematic characters in a path, since ruby answers "Run aborted (error has occurred)" and stops...
-
The problems with accents in File paths and SketchUp's current Ruby on PCs are well know.
It is a 'PITA'Until a better version of Ruby is used by SketchUp [ ] for now you could try encoding the path - but it is an unreliable pain...
### get 'path' if $KCODE=="UTF8" ### what is yours ? begin upath = path.unpack("U*").map{|c| c.chr }.join rescue upath = path end end#if end ### Then do 'File' operation on the encoded string... exist = File.exist?( upath ) size = File.size( upath ) ### etc
-
Yes, it is UTF8
For now, i shall use the code like this
begin unless path.empty? size = File.size( path ) UI.messagebox("This SKP is #{size} bytes !") ### or whatever... end rescue UI.messagebox "problematic characters in the file path" return end
thank you
-
@glro said:
i didn't find this information in
http://www.sketchup.com/intl/en/developer/docs/methods.php
nor
http://www.sketchup.com/intl/en/developer/docs/classes.phpThose docs only cover the SketchUp API, refer to the Ruby docs for core Ruby features: http://www.ruby-doc.org/core-1.8.6/index.html
-
@tt_su said:
@glro said:
i didn't find this information in
http://www.sketchup.com/intl/en/developer/docs/methods.php
nor
http://www.sketchup.com/intl/en/developer/docs/classes.phpThose docs only cover the SketchUp API, refer to the Ruby docs for core Ruby features: http://www.ruby-doc.org/core-1.8.6/index.html
I am not a professional programmer, and sometimes, i even surprise myself when i get a result from the code i just wrote
there is intelligence in the code itself
Maybe that's why i enjoy so much writing code
Next time i don't find something in SU API, i'll look for it in Ruby doc
Thank you
Advertisement