I’ve long been aware that the API had methods to transform entities:
entities.transform_entities(transform, entarray)
but not to copy them(??!!).
The usual advice is to add new entities identical to the old ones, which requires a potentially long process of making sure to include all the attributes and features of the particular class from dimensions to faces to curves etc. Heaven help you if you forget something.
But reading some recent posts re group definitions and that you can use add_instance on a group (not mentioned in the API) leads me to a generally purpose copy_entities method:
- Put the entities to be copied into a temp group.
- Add another instance of the temp group.
- Transform the second instance.
- Explode both.
Here’s my general purpose copy_entities method:
def self.copy_entities(tr, entarray)
# Copies and transforms an array of entities
mod = Sketchup.active_model # Open model
ent = mod.entities # All entities in model
if entarray.length == 0
UI.messagebox "nothing to copy"
return
end
tempgroup = ent.add_group entarray
temptrans = tempgroup.transformation
tempdef = tempgroup.definition
newgroup = ent.add_instance tempdef, temptrans
ent.transform_entities(tr, newgroup)
newgroup.explode
tempgroup.explode
end #def
#test using selection entities
mod = Sketchup.active_model # Open model
ent = mod.entities # All entities in model
sel = mod.selection # Current selection
# define some delta transform
vector = Geom::Vector3d.new(80, 40, 0)
tr = Geom::Transformation.translation(vector)
result = self.copy_entities(tr, sel)