Add all components with same name to new group
-
I've created some components that have the same name and I'd like to group them.
I tried this:
**mod = Sketchup.active_model
@ent = mod.active_entities
...comps = Sketchup.active_model.definitions[@C_name]
new_group = @ent.add_group comps
new_group.name = "Layout**"But I'm doing something wrong (the group isn't created). Could you help me?
-
set up a pattern match:
name=/^xxx/
That matches everything starting with 'xxx'
Then find all matching definition:
comps = mod.definitions.find_all{|d| d.name=~name }
Now you have an array of matching definitions by name-match.
Process the active_entities to find all matching instances
ins = mod.active_entities.find_all{|e| compos.include?(e.definition) }
You now have an array of all matching instances.
new_group = mod.active_entities.add_group(ins) new_group.name = "Layout"
You now have a new group made from the instances.
Note the instances must all be in the active_context... -
Thanks, TIG but...
name=/^20x/ comps = mod.definitions.find_all{|d| d.name=~name}
74 ins = mod.active_entities.find_all{|e| comps.include?(e.definition)}
new_group = mod.active_entities.add_group(ins)
new_group.name = "Layout"Returns:
Error: #<NoMethodError: undefined method
definition' for #<Sketchup::Edge:0x0000000f893280>> C:/Users/jgv/AppData/Roaming/SketchUp/SketchUp 2016/SketchUp/Plugins/Fill Surface.rb:74:in
block in fill_surface'
C:/Users/jgv/AppData/Roaming/SketchUp/SketchUp 2016/SketchUp/Plugins/Fill Surface.rb:74:ineach' C:/Users/jgv/AppData/Roaming/SketchUp/SketchUp 2016/SketchUp/Plugins/Fill Surface.rb:74:in
find_all'
C:/Users/jgv/AppData/Roaming/SketchUp/SketchUp 2016/SketchUp/Plugins/Fill Surface.rb:74:infill_surface' C:/Users/jgv/AppData/Roaming/SketchUp/SketchUp 2016/SketchUp/Plugins/Fill Surface.rb:12:in
block in <top (required)>'
SketchUp:1:in `call' -
Sorry, I assumed the active_entities only contained instances.
Here's a way to filter for just instances...
ins = mod.active_entities**.grep(Sketchup::ComponentInstance)**.find_all{|e| comps.include?(e.definition)}
Substitute this line for the similar line in the code... -
I was blind, now I can see.
Thank you, TIG.
Advertisement