Quick and dirty finger exercise…
Tested only once at SU 2021 (Windows). Use at your own risk! No warranties!
Click on a menu in Extensions >>“Write thumbnail png beside skps”.
In a popop window select any skp file in a desired directory (folder) and click Open.
The plugin will go through ALL skp files in that directory (independently which file selected) and write its thumbnail into png file, into that directory.
The file name will be the same just the extension will be png, e.g.: model.skp >> model.png, model1.skp >> model1.png etc… )
(In the ruby console, the name of the file being written will be printed )
You can install as extension (again: it is a “dirty” one):
Dezmo_tumbstopng_a.rbz (1011 Bytes)
_
module Dezmo
module TumbsToPngA
extend self
@@loaded = false unless defined?(@@loaded)
def get_skps_directory( model )
# get the starting directory based on a current file
path = model.path
if path.nil? || path.empty?
start_dir = Dir.getwd
else
start_dir = File.dirname(path)
end
# give a chance to user to select other directory
# containing .skp file(s)
title = "Save thumbnails in a selected directory: Select any SKP File ..."
user_path = UI.openpanel(title, start_dir, "SKP|*.skp|")
return unless user_path
directory_path = File.dirname(user_path)
# UI::openpanel returns paths with backslashes, change it:
directory_path.gsub!(/\\\\|\\/,"/")
end
def inform(text)
UI.messagebox(text)
p "Job done!"
end
def create_thumbnails_pngs( model = Sketchup.active_model )
directory_path = get_skps_directory( model )
return inform("There is no skp file!") if !directory_path || !File.exist?(directory_path)
successful = 0
unsuccessful = 0
skp_filenames = []
image_filenames = []
Dir.entries(directory_path).each{|file_name|
next unless File.extname(file_name).downcase == ".skp"
skp_filenames<<File.join(directory_path, file_name)
base_name = File.basename(file_name, ".*")
image_filenames<<File.join(directory_path, base_name + ".png")
}
skp_filenames.size.times{|i|
p "Writing: #{image_filenames[i]}"
# The main task: Write the thumbs into file...
status = Sketchup.save_thumbnail(skp_filenames[i], image_filenames[i])
status ? successful += 1 : unsuccessful += 1
}
done = " #{successful.to_s} png image(s) created."
error = "\n #{unsuccessful.to_s} error(s) occurred."
inform(done + error)
end
unless @@loaded
cmd1 = UI::Command.new("Write thumbnail png beside skps"){
create_thumbnails_pngs()
}
menu = UI.menu("Plugins").add_item(cmd1)
@@loaded = true
end
end
end
References (some):
https://ruby-doc.org/core-2.7.1/Dir.html#method-c-getwd
https://ruby-doc.org/core-2.7.1/File.html#method-c-basename
https://ruby.sketchup.com/UI.html#openpanel-class_method
https://ruby.sketchup.com/Sketchup.html#save_thumbnail-class_method