# encoding: UTF-8
# 형상 무수정 · 위치 이동만: 삭제한 보라 브라켓 자리로 A/B 삽입
m = Sketchup.active_model
TG = { 'A' => [ (9018.65+9239.11)/2.0, 219.13 ],   # SWD_08 중심x, 플랜지상면z
       'B' => [ (9030.70+9263.70)/2.0, 227.42 ] }  # SWD_07
m.start_operation('L브라켓 프레임 삽입(이동만)', true)
mv = {}
%w[A B].each do |nm|
  g = m.entities.grep(Sketchup::Group).find{|e| e.name=="연결브라켓_L_#{nm}_20260919"}
  next if g.nil?
  b = g.bounds
  pre = [b.min.x.to_mm, b.max.x.to_mm, b.min.y.to_mm, b.max.y.to_mm, b.min.z.to_mm, b.max.z.to_mm]
  dx = TG[nm][0] - (b.min.x.to_mm + b.max.x.to_mm)/2.0
  dz = TG[nm][1] - b.max.z.to_mm
  g.transform!(Geom::Transformation.translation(Geom::Vector3d.new(dx.mm, 0, dz.mm)))
  mv[nm] = [pre, dx, dz]
end
m.commit_operation
m.save
%w[A B].each do |nm|
  g = m.entities.grep(Sketchup::Group).find{|e| e.name=="연결브라켓_L_#{nm}_20260919"}
  b = g.bounds; pre, dx, dz = mv[nm]
  puts "#{nm} 이동 dx=%.2f dz=%.2f" % [dx, dz]
  puts "   전 x %.2f..%.2f y %.2f..%.2f z %.2f..%.2f" % pre
  puts "   후 x %.2f..%.2f y %.2f..%.2f z %.2f..%.2f solid=#{g.entities.grep(Sketchup::Group).map{|s| s.manifold?}.inspect}" %
    [b.min.x.to_mm,b.max.x.to_mm,b.min.y.to_mm,b.max.y.to_mm,b.min.z.to_mm,b.max.z.to_mm]
  ov = []
  (m.entities.grep(Sketchup::Group)+m.entities.grep(Sketchup::ComponentInstance)).each do |o|
    next if o==g || o.hidden?
    ib = o.bounds.intersect(b)
    next unless ib.valid? && ib.width.to_mm>0.05 && ib.height.to_mm>0.05 && ib.depth.to_mm>0.05
    ov << "#{o.name.to_s.empty? ? '(무명)' : o.name}(%.1fx%.1fx%.1f)" % [ib.width.to_mm, ib.height.to_mm, ib.depth.to_mm]
  end
  puts "   bbox겹침: #{ov.inspect}"
end
puts "SAVED #{File.mtime(m.path)}"
nil
