2

I am new to Sketchup Ruby and am blown away this is not more simple but here goes...

I would like to copy all the groups matching a certain layer name to a new temporary group. I have basically given up on trying to copy the whole group because that appears to be fraught with peril and Bugsplats if not done in some super-anal retentive way that considers context, immediate exploding of objects, etc...

So, I have resorted to trying to loop through all matching groups entities and copying faces instead, which seems much more straight-forward. My goal here is not to become a Ruby wizard but just accomplish this one script.

I have been able to copy faces BUT the faces lose their transformation on copy and just end up at some random size at the origin rather than wherever they were at the model.

Here is the code:

SKETCHUP_CONSOLE.clear

mod = Sketchup.active_model # Open model
ent = mod.entities # All entities in model
temp_wall_primitives = ent.add_group #create a new empty temporary group

mod.definitions.each{|d|
    next if d.image? || d.group? || d.name!="WALL"
    d.entities.each{ |wall_primative_group|
        if wall_primative_group.layer.name == "WALL_PRIMITIVES"
            wall_primative_group.entities.each{ |wall_primative_group_entity|
                if wall_primative_group_entity.is_a? Sketchup::Face
                    new_face = temp_wall_primitives.entities.add_face(wall_primative_group_entity.vertices)
                end
            }
        end
    }
}

I believe I need to somehow get the transformation of each face and apply it to the new faces as they are created?

ekad
  • 14,436
  • 26
  • 44
  • 46
Hank
  • 113
  • 1
  • 11

1 Answers1

1

Instead of trying to copy the entities from an instance to another one, place a new instance;

# Lets say we have a group.
source_group = model.entities.grep(Sketchup::Group)

# We can "copy" this to another group by fetching it'd definition and adding a new one:
new_group = model.entities.add_group
new_group.entities.add_instance(source_group.definition, source_group.transformation)

In your current solution, where you re-create each face, the reason for the transformation being lost is that vertex positions are relative to their parent. You pass in the vertices you copy from directly: temp_wall_primitives.entities.add_face(wall_primative_group_entity.vertices)

But you need to apply the transformation for the instance they relate to as well.

Additionally your current solution doesn't seem to take into account nested instances. And that faces can have holes in them - in which case face.vertices would not form a valid single loop. Manually recreating faces quickly gets complicated. If you want the whole content of a group or component instance, just make a copy of the instance itself. You can explode the new instance if you want.

But I would question why you have a temporary group in the first place. (Often this turns out to not be necessary. It would help if you explained the higher level task you are trying to perform here.)

thomthom
  • 2,854
  • 1
  • 23
  • 53
  • 1
    Thank you Thom! I actually continued and elaborated on this question on another forum where I explained myself better I think. http://sketchucation.com/forums/viewtopic.php?f=180&t=67350&p=617705#p617705 – Hank Apr 05 '17 at 14:48