Creating a group inside a component

Hi there folks, noob coder here so thank you in advance for any help!
I’m trying to group some geometry inside a component definition, but I’m seeing unexpected behavior. Imagine you have selected a component that contains some loose faces and edges. Then:

model = Sketchup.active_model
selection = model.selection

comp = selection.first
defi = comp.definition
ents = defi.entities

geometry = ents.select { |e| e.is_a?(Sketchup::Edge) || e.is_a?(Sketchup::Face) }

Now, if I do something like this, it creates an empty group inside the component:

group = ents.add_group

But if I do this, it creates a group with the elements from geometry outside the component:

group = ents.add_group(geometry)

Is there a way to create a group inside the component that includes the geometry?

It is a bug. I guess, it is related to similar bug reported a long time ago here:
Entities#add_group with arguments not working when not used in the active drawing context · Issue #42 · SketchUp/api-issue-tracker

The preferred way to create groups using the API is to first create an empty group and then draw the content to it. Grouping existing geometry sadly only works at the active entities.

Thank you @ene_su! I think that’s the key: using active_path. It works now!

    model = Sketchup.active_model
    selection = model.selection

    comp = selection.first
    defi = comp.definition

    model.active_path = [comp]

    ents1 = defi.entities
    ents2 = ents1.to_a

    group = ents1.add_group(ents2)

Thank you @dezmo and @ene_su for answers!
Have a nice day

Just to add a bit more info: Actually, that code from my previous post will group everything inside the component.

After some trial and error, this version only groups loose edges and faces. Everything else, like other groups or components, remains unchanged.

model = Sketchup.active_model
selection = model.selection

comp = selection.first
defi = comp.definition

model.active_path = [comp]

ents = defi.entities
geom = ents.select { |e| e.is_a?(Sketchup::Edge) || e.is_a?(Sketchup::Face) }

group = ents.add_group(geom.to_a)

Just FYI, the grep method is faster than your iteration block over is_a?

Hi @slbaumgartner, thank you for the tip! I’ve updated the code below to reflect that, in case someone else finds this helpful later on.

model = Sketchup.active_model
selection = model.selection

comp = selection.first
defi = comp.definition

model.active_path = [comp]

ents = defi.entities
geom = ents.grep(Sketchup::Edge) + ents.grep(Sketchup::Face)

group = ents.add_group(geom.to_a)

Some other thoughts:

can be done like

geom = ents.grep(Sketchup::Edge).concat( ents.grep(Sketchup::Face) )

Array#+ : Returns a new array built by concatenating the two arrays together to produce a third array.
Array#concat : Appends the elements of other_ary to self.

So, the + operator will create a new array each time it is called (which is “more expensive”), while concat only appends the new element.
Actually, I don’t think it would mean too much in your case, I just want to show some other possibilities…
__

Enumerable#grep returs Array, so the geom already is an instance of Array, therefore Array#to_a does not really make any sense here.

__

You might consider “recording” the active path before changing it and restoring it to the original when you’re done.

original_path = model.active_path
model.active_path = [comp]

# do the things

model.active_path = original_path

Thanks a lot, @dezmo!
As you pointed out, it might not make much difference in this tiny example, but it’s definitely something useful to keep in mind for larger scripts. Also, restoring the original path at the end makes the active_path change invisible for the user, that’s really smart! Thank you again!

So the updated version of the snippet would be:

model = Sketchup.active_model
selection = model.selection

comp = selection.first
defi = comp.definition

og_path = model.active_path
model.active_path = [comp]

ents = defi.entities
geom = ents.grep(Sketchup::Edge).concat(ents.grep(Sketchup::Face))

group = ents.add_group(geom)
model.active_path = og_path

One more thing :wink: : Don’t forget about Undo Stack either.

The Model#start_operation method and the Model#commit_operation method…

model = Sketchup.active_model
selection = model.selection
comp = selection.first
defi = comp.definition
ents = defi.entities
geom = ents.grep(Sketchup::Edge).concat(ents.grep(Sketchup::Face))
og_path = model.active_path

model.start_operation("Loose geom to group", true)
model.active_path = [comp]
group = ents.add_group(geom)
model.active_path = og_path
model.commit_operation

Is the extension meant to group some content the user has created previously, or something the extension itself is creating?

In this case, it groups the loose geometry inside a selected component. Imagine you have a component with several elements such as subcomponents, groups, and loose faces. The extension will group the loose faces into a new group inside that component, helping to avoid mixing loose geometry with groups and components.

Thank you again! The thing is your previous answer about saving and restoring the original path made me think about the initial selection. So I save the initial selection (initial_s) in order to restore it at the end of the process. I’m not sure whether it should be inside or outside the commit operation.

model = Sketchup.active_model
selection = model.selection

initial_s = selection.to_a

comp = selection.first
defi = comp.definition
ents = defi.entities
geom = ents.grep(Sketchup::Edge).concat(ents.grep(Sketchup::Face))
og_path = model.active_path

model.start_operation("Loose geom to group", true)
model.active_path = [comp]
group = ents.add_group(geom)
model.active_path = og_path

selection.clear
selection.add(initial_s)

model.commit_operation

Okay, in that case the workaround of opening and then closing the component makes sense. If the extension was creating its own geometry, it would make more sense to add it to the group after the group was created.

:light_bulb:

I would do something like this… I’ll leave it to you the fun to discover how it works and find the bug(s) if there are any… because I haven’t really tested it. :wink:

def instances(e)
  e.grep(Sketchup::Group).concat(e.grep(Sketchup::ComponentInstance))
end

def loose_geom_to_groups
  model = Sketchup.active_model
  selection = model.selection
  initial_s = selection.to_a
  og_path = model.active_path
  processed_defs = []
  model.start_operation("Loose geom to groups", true)

  instances(selection).each{|instance|
    initial_s -= [instance]
    defi = instance.definition
    next if processed_defs.include?(defi)
    processed_defs<< defi
    model.active_path = [instance]
    ents = defi.entities
    while edge = ents.find{|e| e.is_a?(Sketchup::Edge)} 
      connected = edge.all_connected
      group = ents.add_group(connected)
      ents = defi.entities
    end
  }
  
  model.active_path = og_path
  selection.clear
  selection.add(initial_s)

  model.commit_operation
  
end
loose_geom_to_groups

That’s great! It separates each set of connected geometry into its own group. Much better than my initial idea, thank you so much!

Hi there again! After trying out the code, I made a small fix because selection.add(initial_s) wasn’t working as expected. I believe it’s due to the fact that each iteration of initial_s -= [instance] was modifying the same array intended to restore the selection so it ends up empty by the end.

To fix this, I renamed initial_s to sel_array, and created a new initial_s = sel_array.dup to preserve the original selection. This way, I can safely subtract the processed instances from sel_array without affecting the original list. That also meant changing initial_s -= [instance] to sel_array -= [instance].

def instances(e)
  e.grep(Sketchup::Group).concat(e.grep(Sketchup::ComponentInstance))
end

def loose_geom_to_groups
  model = Sketchup.active_model
  selection = model.selection
  sel_array = selection.to_a
  initial_s = sel_array.dup
  og_path = model.active_path
  processed_defs = []
  model.start_operation("Loose geom to groups", true)

  instances(selection).each{|instance|
    sel_array -= [instance]
    defi = instance.definition
    next if processed_defs.include?(defi)
    processed_defs<< defi
    model.active_path = [instance]
    ents = defi.entities
    while edge = ents.find{|e| e.is_a?(Sketchup::Edge)} 
      connected = edge.all_connected
      group = ents.add_group(connected)
      ents = defi.entities
    end
  }
  model.active_path = og_path
  selection.clear
  selection.add(initial_s)
  model.commit_operation
end

loose_geom_to_groups