# encoding: UTF-8
begin
  puts "###DZW-STATUS-COVER-20260920"
  m = Sketchup.active_model
  puts "PATH=#{m.path.to_s.tr("\\", "/")}"
  sel = m.selection.to_a
  puts "선택 #{sel.length}개"
  sel.each_with_index do |e, i|
    nm = (e.respond_to?(:name) ? e.name.to_s : '')
    nm = e.definition.name.to_s if nm.empty? && e.is_a?(Sketchup::ComponentInstance)
    b = e.bounds
    puts "  SEL[#{i}] #{e.class} '#{nm}' bounds(부모계)=min#{b.min.to_a.map { |v| v.to_mm.round(1) }.inspect} max#{b.max.to_a.map { |v| v.to_mm.round(1) }.inspect}"
  end
  cov = nil
  m.active_entities.each do |e|
    next unless e.is_a?(Sketchup::Group) || e.is_a?(Sketchup::ComponentInstance)
    cov = e if (e.respond_to?(:name) ? e.name.to_s : '') == '하부프레임커버'
  end
  if cov.nil?
    puts "STOP: '하부프레임커버' 미발견"
  else
    # 월드 정점 수집 (누적 변환행렬)
    wpts = []
    kids = []
    rec = nil
    rec = lambda do |ent, tr, depth, label|
      ent.grep(Sketchup::Edge).each { |ed| ed.vertices.each { |v| wpts << v.position.transform(tr) } }
      (ent.grep(Sketchup::Group) + ent.grep(Sketchup::ComponentInstance)).each do |s|
        nm = (s.respond_to?(:name) ? s.name.to_s : '')
        nm = s.definition.name.to_s if nm.empty? && s.is_a?(Sketchup::ComponentInstance)
        st = tr * s.transformation
        sp = []
        cnt = lambda do |en, t2|
          en.grep(Sketchup::Edge).each { |ed| ed.vertices.each { |v| sp << v.position.transform(t2) } }
          (en.grep(Sketchup::Group) + en.grep(Sketchup::ComponentInstance)).each { |q| cnt.call(q.entities, t2 * q.transformation) }
        end
        cnt.call(s.entities, st)
        unless sp.empty?
          xs = sp.map { |p| p.x.to_mm }
          ys = sp.map { |p| p.y.to_mm }
          zs = sp.map { |p| p.z.to_mm }
          kids << [depth, nm, s.class.to_s, xs.minmax, ys.minmax, zs.minmax, sp.length, s.transformation.xscale, s.transformation.yscale, s.transformation.zscale]
        end
        rec.call(s.entities, st, depth + 1, nm) if depth < 1
      end
    end
    rec.call(cov.entities, cov.transformation, 0, '커버')
    xs = wpts.map { |p| p.x.to_mm }
    ys = wpts.map { |p| p.y.to_mm }
    zs = wpts.map { |p| p.z.to_mm }
    puts "[월드] 커버 전체 X=[#{xs.min.round(1)},#{xs.max.round(1)}] Y=[#{ys.min.round(1)},#{ys.max.round(1)}] Z=[#{zs.min.round(1)},#{zs.max.round(1)}]"
    puts "[월드] 커버 폭(Y)=#{(ys.max - ys.min).round(1)}  (파손판정 기준: 7540.7 / 정상 1294.2)"
    puts "[월드] 커버 그룹 변환 scale=[#{cov.transformation.xscale.round(4)},#{cov.transformation.yscale.round(4)},#{cov.transformation.zscale.round(4)}] origin=#{cov.transformation.origin.to_a.map { |v| v.to_mm.round(1) }.inspect}"
    puts "-- 자식 부재 (월드 실측) --"
    kids.select { |k| k[0] == 0 }.each do |d, nm, cl, xr, yr, zr, n, sx, sy, sz|
      puts "  '#{nm}' #{cl} X=[#{xr[0].round(1)},#{xr[1].round(1)}] Y=[#{yr[0].round(1)},#{yr[1].round(1)}] Z=[#{zr[0].round(1)},#{zr[1].round(1)}] 정점#{n} scale=[#{sx.round(4)},#{sy.round(4)},#{sz.round(4)}]"
    end
  end
rescue Exception => e
  puts "EXC #{e.class}: #{e.message}"
  puts e.backtrace.first(5).join(" | ")
end
nil
