[Plugin] Purge Folder of SKPs
-
Would there be a way to automate the Purge command to a whole folder full of SKP models? Either with an external script or from within Sketchup...
Ideas?
EDIT: by TIG
See next post in this thread ! http://forums.sketchucation.com/viewtopic.php?p=367797#p367797 -
@tig said:
Copy/paste this code into a file called '
TIG-batchpurge.rb
' in the Plugins folderAdded the file...
Copy/paste this code into a file called 'TIG-batchpurge.rb
' in the Plugins folder EDIT: 20111126 TIG...*### Using File.expand_path() rather than .tr()for attempted Unicode friendliness ???*
<span class="syntaxdefault">module TIG<br /> def self</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">batchpurge</span><span class="syntaxkeyword">()<br /></span><span class="syntaxdefault"> puts<br /> model</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_model<br /> path</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">File</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">expand_path</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">path</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">save</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">path</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> files</span><span class="syntaxkeyword">=[]<br /></span><span class="syntaxdefault"> dir</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">File</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">dirname</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">path</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> skps</span><span class="syntaxkeyword">=[]<br /></span><span class="syntaxdefault"> Dir</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">entries</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">dir</span><span class="syntaxkeyword">).</span><span class="syntaxdefault">each</span><span class="syntaxkeyword">{|</span><span class="syntaxdefault">f</span><span class="syntaxkeyword">|<br /></span><span class="syntaxdefault"> next unless File</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">extname</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">f</span><span class="syntaxkeyword">).</span><span class="syntaxdefault">downcase</span><span class="syntaxkeyword">==</span><span class="syntaxstring">".skp"<br /></span><span class="syntaxdefault"> skps </span><span class="syntaxkeyword"><<</span><span class="syntaxdefault"> File</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">expand_path</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">File</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">join</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">dir</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">f</span><span class="syntaxkeyword">))<br /></span><span class="syntaxdefault"> </span><span class="syntaxkeyword">}<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment">### by returning to the 1st one we ensure all get processed...<br /></span><span class="syntaxdefault"> skps</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">sort</span><span class="syntaxkeyword">!<br /></span><span class="syntaxdefault"> skps </span><span class="syntaxkeyword"><<</span><span class="syntaxdefault"> skps</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">]<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment">###<br /></span><span class="syntaxdefault"> temp</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">nil<br /> max</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">skps</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">length</span><span class="syntaxkeyword">-</span><span class="syntaxdefault">1<br /> skps</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">each_with_index</span><span class="syntaxkeyword">{|</span><span class="syntaxdefault">skp</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">i</span><span class="syntaxkeyword">|<br /></span><span class="syntaxdefault"> next if not Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">open_file</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">skp</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> if files</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">]<br /></span><span class="syntaxdefault"> t</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">m</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">files<br /> File</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">delete</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">m</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> if File</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">exist</span><span class="syntaxkeyword">?(</span><span class="syntaxdefault">m</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> File</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">rename</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">t</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">m</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> if File</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">exist</span><span class="syntaxkeyword">?(</span><span class="syntaxdefault">t</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> end<br /> if i </span><span class="syntaxkeyword">!=</span><span class="syntaxdefault"> max<br /> puts skp<br /> files</span><span class="syntaxkeyword">=[]<br /></span><span class="syntaxdefault"> temp</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">File</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">join</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">dir</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> </span><span class="syntaxstring">"#"</span><span class="syntaxkeyword">+</span><span class="syntaxdefault">rand</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">to_s</span><span class="syntaxkeyword">+</span><span class="syntaxstring">".skp"</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> puts </span><span class="syntaxstring">"...Purging..."<br /></span><span class="syntaxdefault"> model</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_model<br /> path</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">File</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">expand_path</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">path</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> len</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">definitions</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">length</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">to_s<br /> model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">definitions</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">purge_unused<br /> puts </span><span class="syntaxstring">"Components "</span><span class="syntaxkeyword">+</span><span class="syntaxdefault">len</span><span class="syntaxkeyword">+</span><span class="syntaxstring">" >>> "</span><span class="syntaxkeyword">+</span><span class="syntaxdefault">model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">definitions</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">length</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">to_s<br /> len</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">layers</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">length</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">to_s<br /> model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">layers</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">purge_unused<br /> puts </span><span class="syntaxstring">"Layers "</span><span class="syntaxkeyword">+</span><span class="syntaxdefault">len</span><span class="syntaxkeyword">+</span><span class="syntaxstring">" >>> "</span><span class="syntaxkeyword">+</span><span class="syntaxdefault">model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">layers</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">length</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">to_s<br /> len</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">materials</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">length</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">to_s<br /> model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">materials</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">purge_unused<br /> puts </span><span class="syntaxstring">"Materials "</span><span class="syntaxkeyword">+</span><span class="syntaxdefault">len</span><span class="syntaxkeyword">+</span><span class="syntaxstring">" >>> "</span><span class="syntaxkeyword">+</span><span class="syntaxdefault">model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">materials</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">length</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">to_s<br /> len</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">styles</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">count</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">to_s<br /> model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">styles</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">purge_unused<br /> puts </span><span class="syntaxstring">"Styles "</span><span class="syntaxkeyword">+</span><span class="syntaxdefault">len</span><span class="syntaxkeyword">+</span><span class="syntaxstring">" >>> "</span><span class="syntaxkeyword">+</span><span class="syntaxdefault">model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">styles</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">count</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">to_s<br /> puts path<br /> puts </span><span class="syntaxstring">"...Saving..."<br /></span><span class="syntaxdefault"> puts model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">save</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">temp</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> puts<br /> files </span><span class="syntaxkeyword"><<</span><span class="syntaxdefault"> temp </span><span class="syntaxkeyword"><<</span><span class="syntaxdefault"> path<br /> end</span><span class="syntaxcomment">#if<br /></span><span class="syntaxdefault"> </span><span class="syntaxkeyword">}<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment">### tidy up - should never be needed!<br /></span><span class="syntaxdefault"> File</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">delete</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">temp</span><span class="syntaxkeyword">)</span><span class="syntaxdefault">if temp and File</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">exist</span><span class="syntaxkeyword">?(</span><span class="syntaxdefault">temp</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment">###<br /></span><span class="syntaxdefault"> UI</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">beep<br /> puts </span><span class="syntaxstring">"Done."<br /></span><span class="syntaxdefault"> end<br />end</span>
To use open one of the SKPs in the folder to be processed.
TypeTIG.batchpurge
in the Ruby Console... All of the SKPs are processed in turn and purged, a report is shown for each in the Console... Use will care - it's not 'undoable'... -
TIG, could you please rewrite sketchup, multithread it and make it 64 bit capable - no hurry, whenever you have 5 mins...!!
-
very cool.I tested and found model.save not is supporting chinese filenames and path, such as:
Sketchup.open_file "/Users/liuzhenyi/Desktop/中文/测试.skp"
-> true
model = Sketchup.active_model
model.save "/Users/liuzhenyi/Desktop/中文/测试.skp"
-> nil -
Some aspects of Ruby 1.8~ that ships with Sketchup fail when presented with Unicode characters in names/paths - like File... operations, which are used here to get/set file-names/paths etc etc... So that's probably it...
-
I've recast the code to avoid using 'tr' BUT I fear it might still fail as it must use File.delete(), File.exist?() and File.rename() to work and these might fail with Unicode names/paths ??
Please get the code again and retry... -
Still not available, the Chinese path and file name.
model = Sketchup.active_model path=model.path path1=File.expand_path(model.path) path2=model.path.tr("\\","/") path.class #-> string path1.class #-> string path2.class #-> string
path,path1 and path2, all class are string, all value are "/Users/liuzhenyi/Desktop/中文/测试.skp"
so,path=path1=path2.{File.expand_path(model.path)} same as the original {model.path.tr("\","/") or model.path}
File.delete(), File.exist?() and File.rename() , I tested ,not supported chinese.
I use google search, found it difficult to support.
Thanks TIG. hope you have a good solution.
-
As I suspected the File.xxx() methods you listed require ASCII character names/paths - yours are Unicode... The methods are necessary for the tool to complete successfully, so until some future version of Sketchup ports with v1.9+ Ruby we are stumped.
-
sorry to dig up an old thread... but......
i just tried this, and it rocks, my only request/question
Is it possible to do make it include subfolders?
Thanks for the help!
Advertisement