Script to bring all components from library into SU model
-
Grrr - curses, they load in but something is going wrong Im afraid.
Copied and pasted code below - no error messages appearing but the components aren't all there and some of them are broken up / exploded it seems. I have 4 components that Im loading in this case (PLAT Carpet Amber FULL Diagonal A, B, C & D) but when I run the code I only get C and D as complete components and then I get two groups of objects that are broken up?
Ive attached a screen grab - the bottom row is the components as they appear after running the lines of code. The top row is the components loaded from the components pallet.
Ive also pasted the code from the console below...
skpfolderpath='C:\Users\sam.morris\Documents\TEST COMPS';model=Sketchup.active_model;ents=model.active_entities;defs=model.definitions;skps=[];Dir.foreach(skpfolderpath){|f|skps << File.join(skpfolderpath, f) if File.extname(f).downcase==".skp"};skps.each{|skp|defn=defs.load(skp)}
["C:\Users\sam.morris\Documents\TEST COMPS/PLAT Carpet Amber FULL Diagonal A.skp", "C:\Users\sam.morris\Documents\TEST COMPS/PLAT Carpet Amber FULL Diagonal B.skp", "C:\Users\sam.morris\Documents\TEST COMPS/PLAT Carpet Amber FULL Diagonal C.skp", "C:\Users\sam.morris\Documents\TEST COMPS/PLAT Carpet Amber FULL Diagonal D.skp"]
pt = Geom::Point3d.new(0,0,0)
Point3d(0, 0, 0)
newdefs = defs.to_a[(defs.length - skps.length)..-1]
[#Sketchup::ComponentDefinition:0xe60a9d8, #Sketchup::ComponentDefinition:0xe60a8e8, #Sketchup::ComponentDefinition:0xe60a8ac, #Sketchup::ComponentDefinition:0xe603908]
newdefs.each{|defn|tr=Geom::Transformation.new(pt);model.entities.add_instance(defn, tr);pt.x=pt.x+1.m}
[#Sketchup::ComponentDefinition:0xe60a9d8, #Sketchup::ComponentDefinition:0xe60a8e8, #Sketchup::ComponentDefinition:0xe60a8ac, #Sketchup::ComponentDefinition:0xe603908]Any further thoughts or help with this much appreciated.
Thanks -
Your imported components contain subcomponents or groups - these have definitions.
The current code simply takes the definitions list, and because definitions are added onto the end it it assumes the last 4 [say] are the 4 components you loaded, but of course there are more than that because the subcomponent/groups are all extra definitions too - so you only get the first few and not all.
We can recode so that we make an array of the definitions before the import and then an array after an find the difference, skipping groups... that way you only get new components added as instances... I've also added a sorting routine to add them in order...model=Sketchup.active_model; defs=model.definitions; olddefs=defs.to_a; skpfolderpath='C;\Users\sam.morris\Documents\TEST COMPS';model=Sketchup.active_model;ents=model.active_entities; skps=[]; Dir.foreach(skpfolderpath){|f|skps << File.join(skpfolderpath, f) if File.extname(f).downcase==".skp"}; skps.each{|skp|defn=defs.load(skp)}; newdefs = defs.to_a - olddefs; newdefsnames=[]; newdefs.each{|defn|next if defn.group? or defn.image?; newdefsnames << defn.name}; newdefsnames.sort!; pt = Geom;;Point3d.new(0,0,0); newdefsnames.each{|name|tr=Geom;;Transformation.new(pt); model.entities.add_instance(defs[name], tr); pt.x=pt.x+1.m};
Hope this helps [untested...]
-
As we say in the west country "Thats the badger mi babber!!"
Works a treat now TIG - brilliant!
Thanks again for ALL your help with this and you patience
Advertisement