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:
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.
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!
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.
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.
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.
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
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