Copy selection to new group?
-
I want to make a new group and copy the selected geometry to it.
What is the best way of doing it?Something like this (but this doesn't seem to work.):
new_group = ents.copy
-
If you have a selection you are in the active_entities so making a group direct shouldn't BugSplat - adding stuff to a group as you initially make it can be fatal if you cross-thread entities sets so is generally not recommended BUT here I think it'll be OK...
If the selection was made manually it's always in the one [active_]entities context, if you are assembling the 'selection' in code then you must ensure that everything in the selection has the same parent and that the parent owns themodel.active_entities
- so if themodel.active_entities==model.entities
all of the coded 'selected objects' must haveobject.parent==model
- I hope that bit's clear?
This is how I would add the selection to a new group [untested - might be typo-riddled]...model=Sketchup.active_model ents=model.active_entities ss=model.selection ### check there's something to 'group' here ssa=ss.to_a ss.clear group=ents.add_group(ssa) ### everything that was selected has been moved into the new 'group' entities copy=group.copy; copy.explode ### this copies the group and explodes it so that the original selection is ### reinstated on top of the now grouped original selection.
You should also add a
model.start_operation('group selection')
and amodel.commit_operation
to allow a one-step undo - unless of course the code is to go inside an already wider start/commit block of code... -
Thanks, I'll try if this works.
I'll rephrase what I want:I have a selection of faces and edges that might or might not be inside a group or component.
The script should make a copy of these selected entities in a new group and open the group and select them to continue working on the duplicate instead of the original. -
There is no way to open a group/component in SketchUp via the Ruby API.
-
@thomthom said:
There is no way to open a group/component in SketchUp via the Ruby API.
Ouch! Well then I guess I'll have to go the copy.explode way...
-
There is no simple API method to open a group for editing, BUT it can be done in Windows if you have a shortcut key to Group-Edit using a keystroke emulation.
require 'win32ole.so' ### you need this .so file in Plugins ### an example of access SUp tools not otherwise available... ### make a shorcut key-combo if NOT set already ### here we assume = ctrl+alt+shift+G ### check that you have just the group selected and if so do this... WIN32OLE.new("WScript.Shell").SendKeys("+^%{g}") ###
Here's the .so zipped Win32API.zip
-
A small step forward but not quite there yet:
model = Sketchup.active_model ent = model.active_entities ent_array = ent.to_a group = ent.add_group(ent_array) bounds = group.bounds #Be certain to move the copy enough so it wont touch the original v = [(bounds.width+10), (bounds.height+10), 0] tt = Geom;;Transformation.new(v) copy = group.copy copy.transform!(tt) sel = Sketchup.active_model.selection ss = sel.add copy group.explode copy.explode
I want the copy to be exploded and the entities that were inside the "copy" group selected.
It would also be great to have the copy not inside the original group (if the original was grouped). -
Once you have the group you can get a reference to its definition
defn=group.entities.parent
It acts just like a normal component definition.
So then use
group2=model.entities.add_instance(defn, group.parent.transformation)
[you must check it's not already in the model - if it is usegroup.transformation
]
then
group.erase!
So the collection is now called 'group2
' and it is inmodel.entities
...
Advertisement