Hi all,
I’m unable to replicate the standard push/pull behavior with the API.
I create a face on the surface of a cube and then issue the following commands:
face.pushpull(-0.5, false)
I thought that this would then intrude into the cube. But what it instead does, is create a new solid which intersects the cube. I then cannot select the new solid, probably because it’s intersecting.
This is not the behavior when I do this with the push/pull tool. What could be the problem?
This code is run when a face of a solid is selected.
Complete code located below:
def convert_to_units(elem)
converted_elem = elem.cm.to_inch
return converted_elem
end
def create_sticker
model = Sketchup.active_model
entities = model.entities
#create the sticker square: should be 5 cm by 5 cm
sticker_dim = convert_to_units(5)
pts = []
pts[0] = [-sticker_dim/2.0, -sticker_dim/2.0, 0]
pts[1] = [sticker_dim/2.0, -sticker_dim/2.0, 0]
pts[2] = [sticker_dim/2.0, sticker_dim/2.0, 0]
pts[3] = [-sticker_dim/2.0, sticker_dim/2.0, 0]
selection = model.selection
selection.each do |phys_sel|
if phys_sel.typename == 'Face'
#get the bounding box:
bounds = phys_sel.bounds
center_of_face = bounds.center
face = entities.add_face pts
target_normal = phys_sel.normal
current_normal = face.normal
#first, translation
translation = Geom::Transformation.new bounds.center
entities.transform_entities(translation, face)
#then, rotation
point = bounds.center
axis = current_normal.cross(target_normal)
angle = Math::acos(current_normal.dot(target_normal))
if axis.length > 0.0
rotation = Geom::Transformation.rotation point, axis, angle
entities.transform_entities(rotation, face)
elsif current_normal.z == -1
#in this case we need to rotate by 180 degrees
rotation = Geom::Transformation.rotation point, (Geom::Vector3d.new 1,0,0), Math::PI
entities.transform_entities(rotation, face)
end
face.pushpull(-0.5, false) #make a new face
end
end
end
# Add a menu item to launch our plugin.
UI.menu("PlugIns").add_item("Create stickers") {
create_sticker
}
