[Plugin] Purge Folder of SKPs

[Plugin] Purge Folder of SKPs

Postby halroach » Thu Nov 24, 2011 12:32 pm

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/viewtop ... 97#p367797
0
User avatar
halroach 
 

Re: [Plugin] Purge Folder of SKPs

Postby TIG » Thu Nov 24, 2011 4:57 pm

TIG wrote:Copy/paste this code into a file called 'TIG-batchpurge.rb' in the Plugins folder


TIG-batchpurge.rb

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 ???
Code: Select all
module TIG
    def self
.batchpurge()
      puts
      model
=Sketchup.active_model
      path
=File.expand_path(model.path)
      model.save(path)
      files=[]
      dir=File.dirname(path)
      skps=[]
      Dir.entries(dir).each{|f|
        next unless File.extname(f).downcase==".skp"
        skps << File.expand_path(File.join(dir,f))
      }
      ### by returning to the 1st one we ensure all get processed...
      skps.sort!
      skps << skps[0]
      ###
      temp=nil
      max
=skps.length-1
      skps
.each_with_index{|skp,i|
        next if not Sketchup.open_file(skp)
        if files[0]
          t,m=files
          File
.delete(m) if File.exist?(m)
          File.rename(t,m) if File.exist?(t)
        end
       if i 
!= max
        puts skp
        files
=[]
        temp=File.join(dir, "#"+rand.to_s+".skp")
        puts "...Purging..."
        model=Sketchup.active_model
        path
=File.expand_path(model.path)
        len=model.definitions.length.to_s
        model
.definitions.purge_unused
        puts 
"Components "+len+" >>> "+model.definitions.length.to_s
        len
=model.layers.length.to_s
        model
.layers.purge_unused
        puts 
"Layers "+len+" >>> "+model.layers.length.to_s
        len
=model.materials.length.to_s
        model
.materials.purge_unused
        puts 
"Materials "+len+" >>> "+model.materials.length.to_s
        len
=model.styles.count.to_s
        model
.styles.purge_unused
        puts 
"Styles "+len+" >>> "+model.styles.count.to_s
        puts path
        puts 
"...Saving..."
        puts model.save(temp)
        puts
        files 
<< temp << path
       end
#if
      }
      ### tidy up - should never be needed!
      File.delete(temp)if temp and File.exist?(temp)
      ###
      UI.beep
      puts 
"Done."
    end
end
To use open one of the SKPs in the folder to be processed.
Type TIG.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'...
0
TIG
User avatar
TIG 
Global Moderator
 

Re: [Plugin] Purge Folder of SKPs

Postby CadFather » Thu Nov 24, 2011 7:18 pm

:shock:

TIG, could you please rewrite sketchup, multithread it and make it 64 bit capable - no hurry, whenever you have 5 mins...!!
2
User avatar
CadFather 
PluginStore Author
PluginStore Author
 

Re: [Plugin] Purge Folder of SKPs

Postby ODM » Sat Nov 26, 2011 2:24 pm

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
0

ODM 
 

Re: [Plugin] Purge Folder of SKPs

Postby TIG » Sat Nov 26, 2011 3:50 pm

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...
0
TIG
User avatar
TIG 
Global Moderator
 

Re: [Plugin] Purge Folder of SKPs

Postby TIG » Sat Nov 26, 2011 3:59 pm

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...
0
TIG
User avatar
TIG 
Global Moderator
 

Re: [Plugin] Purge Folder of SKPs

Postby ODM » Sun Nov 27, 2011 11:21 am

Still not available, the Chinese path and file name.
Code: Select all
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.
0

ODM 
 

Re: [Plugin] Purge Folder of SKPs

Postby TIG » Sun Nov 27, 2011 11:43 am

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.
:cry:
1
TIG
User avatar
TIG 
Global Moderator
 

Re: [Plugin] Purge Folder of SKPs

Postby zerothreequarter » Mon Jan 16, 2017 9:52 pm

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!
1

zerothreequarter 
 

SketchUcation One-Liner Adverts

by Ad Machine » 5 minutes ago



Ad Machine 
Robot
 



 

Return to Plugins

Who is online

Users browsing this forum: Google Bot, MADDK and 7 guests

Visit our sponsors: