Layers to Groups
-
Hello!
I want to first say thank you to the amazing people here on SketchUcation. I have found more help here than anywhere else. Now that I have been drawing for a while I find being able to automate some tasks would help me greatly.
Seeing as sketchup has included a ruby api to all of sketchup's features this should not be too hard right? I figured out VBA what could be so hard about this?
With that in mind I set out to make a plugin that would move cad elements on the same layer to a named group, and set their layer to Layer 0. Freeing up the layers dialog but still keeping some of the inherent organisation from the original drawing.
The code below loads without error but effectively does nothing. Would any of you be willing to shed some light on what i'm missing?
#CAD_To_Groups.rb #----------------------------------------------------------------------------- require 'sketchup.rb' #----------------------------------------------------------------------------- def layerToGroup() model = Sketchup.active_model #this is the current sketchup model environemnt layers = model.layers #current set of layers ents = model.active_entities #current active entities in model sel=model.selection #current selection newGroupEnts = array.new layers.each do |clayer| sel.each do |selEnt| if selEnt.layer = clayer then newGroupEnts.add selEnt end # I would like add each to an array end group = ents.add_group(NewGroupEnts) group.name = clayer end clayer = layers.at(0) putCurrentRecursion(sel, clayer) end def putCurrentRecursion(ents, clayer) etype = ents.typename case etype when "Face" if ents.layer!=clayer then ents.layer=clayer end #if layer needs to be changed when "Edge" if ents.layer!=clayer then ents.layer=clayer end #if layer needs to be changed when "Group" if ents.layer!=clayer then ents.layer=clayer end ents.entities.each do |ent| putCurrentRecursion(ent,clayer) #recursion happens here end when "ComponentInstance" if ents.layer!=clayer then ents.layer=clayer end begin ents.definition.entities.each do |ent| putCurrentRecursion(ent,clayer) #recursion happens here end rescue => err puts "EXCEPTION; #{err}" end else begin old = ents.layer.name.to_s new = clayer.name.to_s if ents.layer!=clayer then ents.layer=clayer end puts "Old; " + old + ", New; " + new rescue => err puts "EXCEPTION; #{err}" end end end if( not file_loaded?("cadtogroups.rb") ) plugins_menu = UI.menu "Plugins" plugins_menu.add_item("Cad To Groups") { layerToGroup() } end file_loaded("cadtogroups.rb")
-
"add" is not a method name for the
Array
class.Use "
<<
" (the append method.)if selEnt.layer = clayer then newGroupEnts << selEnt
-
Too many recursion levels can be a problem (with memory I think.)
We have discussed this in other threads. Do a forum search on "recursion".
Advertisement