I have group1
and I made a box(group2) in group1
Next, I want to take the box out of group1
Like “past in place”
Couldn`t find example
I have group1
and I made a box(group2) in group1
Next, I want to take the box out of group1
Like “past in place”
Couldn`t find example
Something like this I would try…:
group2_copy = group2.parent.parent.entities.add_instance(group2.definition,group2.transformation)
or:
group2_copy = group1.parent.entities.add_instance(group2.definition,group2.transformation)
then optionally:
group2.erase!
… except that group2.transformation
will be relative to group1.transformation
if the user is not within the group1
editing context. So …
group2_copy = group1.parent.entities.add_instance(
group2.definition, group2.transformation * group1.transformation
)
If the model.active_entities
are group1.entities
then the transformation will be in global model coordinates.
It may be necessary to use the model.edit_transform
to get the group properly placed.
model = Sketchup.active_model
entities = model.active_entities
selection = model.selection
faces = selection.grep(Sketchup::Face)
faces.each{|face|
group = entities.add_group(face)
group2 = group.copy
entities2 = group2.entities
before = entities2.grep(Sketchup::Face)
before[0].pushpull( 3000.mm )
new_faces = entities2.grep(Sketchup::Face) - before
upper_face = new_faces.find {|f| f.normal == before[0].normal.reverse }
unless upper_face.nil?
upper_face.material = "Red"
start_point = upper_face.bounds.center
offset_vector = upper_face.normal
offset_vector.length = 3000.mm
end_point = start_point.offset(offset_vector)
end
edge = group2.entities.add_line(start_point, end_point)
#model = Sketchup.active_model
#entities = model.active_entities
group2_copy = group2.parent.entities.add_instance(group2.definition,group2.transformation)
group2.erase!
}
when I use “group2.parent.ent”, group2 successfully copied but still in group1
group2.parent.entities
and not working with “group2.parent.parent.ent”
group2.parent.parent.entities
It says "undefined method ‘entities’ for Sketchup::DefinitionList:0x0002727d77ba68
The problem is that “parent” doesn’t do quite what you expect, particularly when applied recursively.
Parent returns the object whose entities collection contains the specified Group or ComponentInstance. The parent may be the model or a ComponentDefinition.
If the Group is nested in another one, the parent will be the enclosing Group’s ComponentDefinition, not a specific enclosing Group or ComponentInstance. This happens because Groups are actually a special case of Component, and they use a ComponentDefinition to manage their entities. The direct method group.entities is just a code shortcut for group.definition.entities. Both Groups and Components work that way so that multiple copies (instances) can share the same geometric template.
The parent of a ComponentDefinition is the model’s DefinitionList, both for Groups and Components.
So, the heart of your solution will be “paste in place in what?”. Once you understand that, you will need to figure out how to get hold of that object’s Entities. As you have seen, parent is the answer only if you want to paste into whatever contains the original Group.
model = Sketchup.active_model
entities = model.active_entities
selection = model.selection
faces = selection.grep(Sketchup::Face)
faces.each{|face|
group = entities.add_group(face)
group2 = group.copy
entities2 = group2.entities
before = entities2.grep(Sketchup::Face)
before[0].pushpull( 3000.mm )
new_faces = entities2.grep(Sketchup::Face) - before
upper_face = new_faces.find {|f| f.normal == before[0].normal.reverse }
unless upper_face.nil?
upper_face.material = "Red"
start_point = upper_face.bounds.center
offset_vector = upper_face.normal
offset_vector.length = 3000.mm
end_point = start_point.offset(offset_vector)
end
edge = group2.entities.add_line(start_point, end_point)
#model = Sketchup.active_model
#entities = model.active_entities
if false #if faces are not in group
group2_copy = group2.parent.entities.add_instance(group2.definition,group2.transformation)
group2.erase!
else #if faces are in group
group2_copy = group2.parent.parent.parent.entities.add_instance(group2.definition,group2.transformation)
group2.erase!
end
}
It works but one more question
How to divides cases 1,2
1.if the faces are in group
2.if the faces are not in group
How to define “if in group”?
.
if model.active_entities != model.entities
# Then the user has clicked into some level of editing.
# Ie, they are not at the top level model entities collection
end
To get the actual editing path, see Sketchup::Model#active_path
, so you can also do:
if !model.active_path.nil?
instance = model.active_path.last
definition = instance.definition
# Other code ...
end