# encoding: UTF-8
m = Sketchup.active_model
MM = 1.0/25.4
root = m.entities.grep(Sketchup::Group).find{|g| g.name=="하부마운트커버_20260919"}
raise "no root" unless root
d = root.entities.grep(Sketchup::Group).find{|g| g.name=="상판_12T"}
bb = d.bounds
puts "deck before z=#{(bb.min.z*25.4).round(2)}..#{(bb.max.z*25.4).round(2)}"
begin
  m.start_operation("상판 위치보정", true)
  need = 170.4 - bb.max.z*25.4
  d.transform!(Geom::Transformation.translation(Geom::Vector3d.new(0,0,need*MM))) if need.abs > 0.01
  m.commit_operation
  bb2 = d.bounds
  puts "deck after  z=#{(bb2.min.z*25.4).round(2)}..#{(bb2.max.z*25.4).round(2)} solid=#{d.manifold?}"
rescue Exception => ex
  m.commit_operation rescue nil
  puts "ERR #{ex.class}: #{ex.message}"
end

rb = root.bounds
puts "ROOT BBOX mm min=(#{(rb.min.x*25.4).round(1)},#{(rb.min.y*25.4).round(1)},#{(rb.min.z*25.4).round(1)}) max=(#{(rb.max.x*25.4).round(1)},#{(rb.max.y*25.4).round(1)},#{(rb.max.z*25.4).round(1)})"

# G33 간섭량 (bbox 교집합, 근사)
g33 = m.entities.grep(Sketchup::Group).find{|g| b=g.bounds; (b.min.z*25.4).round(1) == -129.8 }
if g33
  a=root.bounds; b=g33.bounds
  ox=[a.max.x,b.max.x].min-[a.min.x,b.min.x].max
  oy=[a.max.y,b.max.y].min-[a.min.y,b.min.y].max
  oz=[a.max.z,b.max.z].min-[a.min.z,b.min.z].max
  puts "G33 overlap mm X=#{(ox*25.4).round(1)} Y=#{(oy*25.4).round(1)} Z=#{(oz*25.4).round(1)}"
end
m.selection.clear; m.selection.add(root); m.active_view.zoom(root)
puts "SEL=#{m.selection.length} MODIFIED=#{m.modified?}"
"BUILD2"
