Hi all!
I use add_instance method with a transform parameter created before as below. But it has problem with “component definitions” saved as “has a non-default scale”, specifically everything works correctly till Redraw operation is performed, rotate value are back to default. Please help me deal with this problem. tks!
mod = Sketchup.active_model
ent = mod.entities
sel = mod.selection
defs = mod.definitions
@comp_def = defs.load(@skp)
re = sel[0]
dsx_y = re.bounds.width / @comp_def.bounds.width
dsy_y = re.bounds.height / @comp_def.bounds.height
dsz_y = re.bounds.depth / @comp_def.bounds.depth
sc_tr_y = Geom::Transformation.scaling( dsx_y, dsy_y, dsz_y)
dsx_x = re.bounds.height / @comp_def.bounds.width
dsy_x = re.bounds.width / @comp_def.bounds.height
dsz_x = re.bounds.depth / @comp_def.bounds.depth
sc_tr_x = Geom::Transformation.scaling( dsx_x, dsy_x, dsz_x)
if 135<=angle_cam_corner_ent(re) && angle_cam_corner_ent(re)<225
p_tr = Geom::Transformation.new(re.bounds.corner(1))
rot_tr = Geom::Transformation.rotation(@comp_def.bounds.corner(0), Geom::Vector3d.new(0, 0, 1), 90.degrees)
sc_tr = sc_tr_x
elsif 225<=angle_cam_corner_ent(re) && angle_cam_corner_ent(re)<315
p_tr = Geom::Transformation.new(re.bounds.corner(3))
rot_tr = Geom::Transformation.rotation(@comp_def.bounds.corner(0), Geom::Vector3d.new(0, 0, 1), 180.degrees)
sc_tr = sc_tr_y
elsif 45<=angle_cam_corner_ent(re) && angle_cam_corner_ent(re)<135
p_tr = Geom::Transformation.new(re.bounds.corner(0))
rot_tr = Geom::Transformation.rotation(@comp_def.bounds.corner(0), Geom::Vector3d.new(0, 0, 1), 0.degrees)
sc_tr = sc_tr_y
else
p_tr = Geom::Transformation.new(re.bounds.corner(2))
rot_tr = Geom::Transformation.rotation(@comp_def.bounds.corner(0), Geom::Vector3d.new(0, 0, 1), 270.degrees)
sc_tr = sc_tr_x
end
trans = p_tr * rot_tr * sc_tr
new_instance = ent.add_instance(@comp_def, trans)
make_unique_comp(new_instance)
re.erase!
mod.active_path = nil
sel.add(new_instance)
Sketchup.active_model.selection.grep(Sketchup::ComponentInstance).each do |s|
$dc_observers.get_latest_class.redraw_with_undo(s)
end
def angle_cam_corner_ent(e)
mod = Sketchup.active_model
cam = mod.active_view.camera
cam_point = cam.eye
cam_vec_ent = [cam_point[0], cam_point[1], 0].vector_to(e.bounds.corner(0))
if cam_point[1]<0
angle = cam_vec_ent.angle_between(X_AXIS)/Math::PI*180
elsif cam_point[1]>0
angle = 360 - cam_vec_ent.angle_between(X_AXIS)/Math::PI*180
end
end