Auto group according to instance names
-
I am drawing homes in separate framing software then importing into sketchup, see attachment.
What I really need is an addon that groups all the walls. I have attached a section of the house which is actually part of the garage, it contains 3 walls and I would like to group this as three groups automatically, it is not uncommon to have upwards of 100 walls. It would be additionally helpful if the new group contained the wall #.
Any help would be much appreciated.
-
How would you expect SketchUp or an extension to identify the geometry that needs to be included in the group? What would you do, select the edges and faces that need to be grouped?
-
Here's the outline of Ruby code to do this.
Copey+paste all and run it in the Ruby Console.
All matching elements are grouped by their group's name - say 'wall96'.
It assumes that all of the grouped elements start say 'wall' and contain '-fram-'
The current context must contain the elements.
It's one step undo-able.model = Sketchup.active_model ents = model.active_entities groups = ents.grep(Sketchup;;Group).find_all{|e| e.name=~/^wall/ } walls=[] groups.each{|e| walls << e.name.split('-fram-')[0] } walls.uniq! walls.sort! model.start_operation('wall_grouper', true) walls.each{|w| puts w es = ents.find_all{|e| e.name =~ /^#{w}/ } gp = ents.add_group(es) if es[0] gp.name = w } model.commit_operation puts
-
We are so close! It seems to be confusing wall 1 with wall 1xx or wall 1x. or wall 4 with wall 4x etc.
The error I get is something like the following
wall1
wall100
Error: #<NoMethodError: undefined methodname=' for nil:NilClass> <main>:12:in
block in <main>'
<main>:8:ineach' <main>:8:in
<main>'
SketchUp:1:in `eval'I attached the entire house this time.
Thanks!
-
@dave r said:
How would you expect SketchUp or an extension to identify the geometry that needs to be included in the group? What would you do, select the edges and faces that need to be grouped?
The geometry is all named by instance and the first "wallxxx" of the name is how I would prefer it to be grouped. I could do a search for wall1 then group all that then on and on but would take a very long time.
-
A minor error: in line 12 it should have been groups, not ents:
model = Sketchup.active_model ents = model.active_entities groups = ents.grep(Sketchup;;Group).find_all{|e| e.name=~/^wall/ } walls=[] groups.each{|e| walls << e.name.split('-fram-')[0] } walls.uniq! walls.sort! model.start_operation('wall_grouper', true) walls.each{|w| puts w es = groups.find_all{|e| e.name =~ /^#{w}/ } # error was here gp = ents.add_group(es) if es[0] gp.name = w } model.commit_operation puts
-
Thanks! It works seemlessly for the basement and 2nd floor then it coppies and moves things on the 1st floor. I have tried everything that I can think of. I have no idea what is causing this and was wondering if it was something simple. I attached the 1st floor if you get a minute. Thanks for everything.
-
Another bug! Handling regular expressions can sometimes be subtle. Here it caused the operation to process any original groups with the same leading digits multiple times because it would match both wall1 and wall13 (etc.) when looking for things to group together. Processing more than once messed up the transformations that place objects in the model.
model = Sketchup.active_model ents = model.active_entities groups = ents.grep(Sketchup;;Group).find_all{|e| e.name=~/^wall/ } walls=[] groups.each{|e| walls << e.name.split('-fram-')[0] } walls.uniq! walls.sort! model.start_operation('wall_grouper', true) walls.each{|w| puts w es = groups.find_all{|e| e.name =~ /^#{w}-/ } # both errors were here gp = ents.add_group(es) if es[0] gp.name = w } res = model.commit_operation puts "operation committed = #{res}"
-
OK, now we are in business, thank you thank you, this is exactly what I need.
Advertisement