# encoding: UTF-8
m = Sketchup.active_model
def r1(v); (v*25.4).round(2); end
YMID = 8142.55
root = m.entities.grep(Sketchup::Group).find{|g| g.name=="하부마운트커버_v4_20260919"}
# 결합면 수평 검증: 최상면 z=122.0 인 상향 수평면 집계
tot=0.0; zs=[]; ys=[]
walk = lambda do |g, tr|
  g.entities.grep(Sketchup::Group).each do |s|
    t = s.transformation*tr
    if s.entities.grep(Sketchup::Group).length>0 then walk.call(s,t)
    else
      s.entities.grep(Sketchup::Face).each do |f|
        n = f.normal.transform(t)
        pts = f.vertices.map{|v| v.position.transform(t)}
        pts.each{|p| ys << (p.y*25.4).round(2)}
        next unless n.z > 0.999
        zz = pts.map{|p| (p.z*25.4).round(2)}.uniq
        if zz.length==1 && zz[0] > 115.0
          tot += f.area*645.16; zs << zz[0]
        end
      end
    end
  end
end
walk.call(root, root.transformation)
puts "윈치 결합면(상향 수평면 Z>115): Z레벨=#{zs.uniq.inspect}  총면적=#{(tot/100).round(1)}cm2"
puts "→ 수평 검증: #{zs.uniq.length==1 ? "통과 (전부 Z=#{zs.uniq[0]} 단일 평면)" : '불일치'}"
ys.uniq!
puts "좌우 대칭: uniqueY=#{ys.length} mismatch=#{ys.reject{|y| ys.any?{|o| ((2*YMID-y)-o).abs<0.05}}.length}"
# 간섭 점검
b=root.bounds
puts "== 간섭 후보 (Z 0~130, 신규 XY 영역) =="
m.entities.each do |e|
  next unless e.is_a?(Sketchup::Group)||e.is_a?(Sketchup::ComponentInstance)
  next if e.hidden?
  nm = e.name.to_s; nm = e.definition.name.to_s if nm.empty?
  next if nm == "하부마운트커버_v4_20260919"
  eb = e.bounds
  next if r1(eb.min.x)>4410 || r1(eb.max.x)<3884 || r1(eb.min.y)>8789 || r1(eb.max.y)<7496
  next if r1(eb.min.z)>122.0 || r1(eb.max.z)<10.0
  puts "  #{nm.inspect} Z#{r1(eb.min.z)}~#{r1(eb.max.z)} X#{r1(eb.min.x)}~#{r1(eb.max.x)} Y#{r1(eb.min.y)}~#{r1(eb.max.y)}"
end
m.selection.clear
m.selection.add(root)
m.active_view.zoom(root)
puts "SEL=#{m.selection.length} MODIFIED=#{m.modified?}"
puts "hidden=#{m.entities.grep(Sketchup::Group).select{|g| g.hidden?}.map{|g| g.name.empty? ? g.definition.name : g.name}.inspect}"
"V4C"
