How to merge groups

Dear friends,

       for i in 0...tsn
         s_grp2 = s_grp.copy
         s_pt = [0, 0, (i + 1) * delta]
         tr = Geom::Transformation.new s_pt
         s_grp2.transform! tr
       end

Codes make some groups from first one. Now I wish to have all of these groups as one group. How can I do it? This is general question to merge some groups together. I can also explode all of groups also I can move then to a especial layer but I don’t know how can I add all of entities in one group? Thank you in advance for your help.

Something like this… (Not tested)

# Will merge the groups 
# Paramater (Array<Sketchup::Group>) 
#   if nothing is specified all group will
#   be merged in active context
# Returns: <Sketchup::Entities>
def merge_groups( groups = Sketchup.active_model.active_entities.grep(Sketchup::Group) )
  model = Sketchup.active_model
  ents = model.active_entities
  merged_group = ents.add_group( groups.each{|g| g.entities} )
  merged_group.entities.each{ |e| e.explode if e.respond_to?(:explode) }
end
#usage example
merge_groups
#or
merge_groups([s_grp, s_grp2])
2 Likes

Dear Dezmo,
You codes work right and at the end we will have one group just one problem. explode make codes too slow even more than 10 second for a normal usage. Can you suggest another faster solution?

Not really.
However just an idea, you can try:
If you are wrapping to undo stack with disabled UI - which you must to do anyway on a “full” code where you make modell geometry changes - maybe helps a little…
Model#start_operation-instance_method (The second parameter must be true )

# Paramater (Array<Sketchup::Group>) 
#   if nothing is specified all group will
#   be merged in active context
# Returns: <Sketchup::Entities>
def merge_groups( groups = Sketchup.active_model.active_entities.grep(Sketchup::Group) )
  model = Sketchup.active_model
  ents = model.active_entities

  model.start_operation('Merge Groups', true)

  merged_group = ents.add_group( groups.each{|g| g.entities} )
  merged_group.entities.each{ |e| e.explode if e.respond_to?(:explode) }

  model.commit_operation
end
#usage example
merge_groups
#or
merge_groups([s_grp, s_grp2])

I did it before and takes more than 10 second. Also used following code but same result.

        groups = []
        groups << s_grp
        for i in 0...tsn
          s_grp2 = s_grp.copy
          s_pt = [0, 0, (i + 1) * delta]
          tr = Geom::Transformation.new s_pt
          s_grp2.transform! tr
          groups << s_grp2
        end
        ents = Sketchup.active_model.active_entities
        merged_group = ents.add_group( groups.each{|g| g.entities} )
#        merged_group.entities.each{ |e| e.explode if e.respond_to?(:explode) }
        merged_group.entities.grep(Sketchup::Group) { |g| g.explode }

Delay for an average usage is too much even user think SU is terminated. Maybe from beginning I should make all in one group. Let me try for it. Always your solution teach me something new. Thank you so much for it.

Yea. That is the best you can do, for efficiency… :wink:

1 Like

This snippet is the same as …

merged_group = ents.add_group( groups )

… because #each just returns the receiver collection (in this case the groups array.)


The delay is probably due to the merging of geometry after each explode.

2 Likes