Copy entities (@last)

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:

  1. Put the entities to be copied into a temp group.
  2. Add another instance of the temp group.
  3. Transform the second instance.
  4. 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)

This is pretty much the approach I use. I haven’t extracted it as a general copy method though (at least not what I can remember). I think there’s a risk in some old SU versions of causing a crash when creating and exploding a group in the same operator when there are observers but since 2016 when observers fire after the operation has ended I think it is safer.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.