# Copyright 2005, TIG # # Permission to use, copy, modify, and distribute this software for # any purpose and without fee is hereby granted, provided the above # copyright notice appear in all copies. # # THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR # IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. # #------------------------------------------------------------------ # Name : Archiver # # Description : A tool to Archive Models # # Menu Item : Plugins -> Archiver # # Context Menu: None # # Author : TIG # # Usage : Pick 'Archiver' to make an archive copy of the current model. # It saves the model (in its current state) into a sub-folder # within the model's folder called 'Archives'. It's named with # a date-time prefix - #e.g. Model[05-12-18-12-34-56].skp. # If later on you don't save the main model with those changes # then that archive copy still has those changes current at the # time of the archiving. # # Date : 12/2005 # # Type : Tool # # Verison : 1.1 18/12/05 First issue. # #--------------------------------------------------------------------- require 'sketchup.rb' ### class Archiver TOO_BIG = 1024 * 1024 * 2 # 2MB ??? ###--------------------------------------- def Archiver::make model = Sketchup.active_model entities = model.entities mpath = model.path paths = model.path.split("\\") paths = paths - [paths.last] path = paths.join("\\")+"\\" title = model.title ###[7, 41, 21, 18, 12, 2005, 0, 352, false, "GMT Standard Time"] t = Time::now.to_a sfix = t[5].to_s[2..4]+"-"+t[4].to_s+"-"+t[3].to_s+"-"+t[2].to_s+"-"+t[1].to_s+"-"+t[0].to_s name = title+"["+sfix+"].skp" archive = path+"\\Archives" Dir.mkdir(archive) if not FileTest.directory?(archive) archname = archive+"\\"+name model.save name+".tmp" Archiver.copy((path+name+".tmp"),archname) File.delete(path+name+".tmp") UI.messagebox(" Archive:\n\n#{name}\n\n made in:\n\n#{archive}\\ \n\n") #----------------------- end#def # copy files def Archiver::catname (from, to) if FileTest.directory? to to + if to =~ /\\/ if to[-1,1] != '\\' then '\\' end + basename(from) else if to[-1,1] != '/' then '/' end + basename(from) end else to end end ### def Archiver::syscopy (from, to) to = Archiver.catname(from, to) fsize = File.size(from) fsize = 1024 if fsize < 512 fsize = TOO_BIG if fsize > TOO_BIG fmode = File.stat(from).mode tpath = to from = File.open(from, "r") from.binmode to = File.open(to, "w") to.binmode begin while true r = from.sysread(fsize) rsize = r.size w = 0 while w < rsize t = to.syswrite(r[w, rsize - w]) w += t end end rescue EOFError ret = true rescue ret = false ensure to.close from.close end File.chmod(fmode, tpath) ret end ### def Archiver::copy (from, to, verbose = false) $stderr.print from, " --> ", catname(from, to), "\n" if verbose Archiver.syscopy from, to end ### #----------------------- end#class #----------------------- #--------- menu ----------------------------- ###if( not file_loaded?("98-Archiver.rb") ) if( not file_loaded?("Archiver.rb") ) add_separator_to_menu("Plugins") UI.menu("Plugins").add_item("Archiver") { Archiver.make } ###$submenu9.add_item("Archiver") { Archiver.make } end#if file_loaded("Archiver.rb") ###file_loaded("98-Archiver.rb") #---------------------------------