# encoding: UTF-8
begin
  puts "###DZW-FIND-DJ1250-20260920"
  m = Sketchup.active_model
  ents = lambda { |s| s.is_a?(Sketchup::Group) ? s.entities : s.definition.entities }
  hits = []
  walk = nil
  walk = lambda do |ent, tr, path, depth|
    (ent.grep(Sketchup::Group) + ent.grep(Sketchup::ComponentInstance)).each do |s|
      nm = (s.respond_to?(:name) ? s.name.to_s : '')
      dn = s.is_a?(Sketchup::ComponentInstance) ? s.definition.name.to_s : ''
      lbl = nm.empty? ? "(무명)#{dn}" : nm
      st = tr * s.transformation
      hits << ["#{path}/#{lbl}", s, st, depth]
      walk.call(ents.call(s), st, "#{path}/#{lbl}", depth + 1) if depth < 3
    end
  end
  walk.call(m.entities, Geom::Transformation.new, '', 0)
  cand = hits.select { |h| h[0].include?('DJ1250') || h[0].include?('윈치') || h[0].upcase.include?('WINCH') }
  if cand.empty?
    puts "STOP: 'DJ1250-윈치받침' 및 유사 이름 미발견. 전체 그룹/컴포넌트 #{hits.length}개 중 후보 없음."
    puts "  참고 최상위 이름 목록(앞 40개): #{hits.select { |h| h[3] == 0 }.map { |h| h[0] }.first(40).inspect}"
  else
    cand.each do |path, s, st, d|
      ps = []
      col = nil
      col = lambda do |en, t|
        en.grep(Sketchup::Edge).each { |ed| ed.vertices.each { |v| ps << v.position.transform(t) } }
        (en.grep(Sketchup::Group) + en.grep(Sketchup::ComponentInstance)).each { |q| col.call(ents.call(q), t * q.transformation) }
      end
      col.call(ents.call(s), st)
      next if ps.empty?
      xs = ps.map { |p| p.x.to_mm }.minmax
      ys = ps.map { |p| p.y.to_mm }.minmax
      zs = ps.map { |p| p.z.to_mm }.minmax
      puts "== #{path} (#{s.class}) d#{d}"
      puts "   월드 X=#{xs.map { |v| v.round(1) }.inspect} Y=#{ys.map { |v| v.round(1) }.inspect} Z=#{zs.map { |v| v.round(1) }.inspect} 크기=#{(xs[1] - xs[0]).round(1)}x#{(ys[1] - ys[0]).round(1)}x#{(zs[1] - zs[0]).round(1)}"
      puts "   scale=[#{s.transformation.xscale.round(5)},#{s.transformation.yscale.round(5)},#{s.transformation.zscale.round(5)}] 정점#{ps.length}"
      fl = []
      ff = nil
      ff = lambda do |en, t|
        en.grep(Sketchup::Face).each do |fa|
          vp = fa.outer_loop.vertices.map { |v| v.position.transform(t) }
          next if vp.length < 3
          n = (vp[1] - vp[0]).cross(vp[2] - vp[0])
          next if n.length.to_f < 1.0e-12
          n.normalize!
          bb = Geom::BoundingBox.new
          vp.each { |p| bb.add(p) }
          ex = [bb.width.to_mm, bb.height.to_mm, bb.depth.to_mm].sort
          fl << [ex[1] * ex[2], n, bb]
        end
        (en.grep(Sketchup::Group) + en.grep(Sketchup::ComponentInstance)).each { |q| ff.call(ents.call(q), t * q.transformation) }
      end
      ff.call(ents.call(s), st)
      horiz = fl.select { |a, n, b| n.z.abs > 0.7 }.sort_by { |a, n, b| -a }
      puts "   수평계열 면 상위3:"
      horiz.first(3).each do |a, n, b|
        tilt = Math.acos([[n.z.abs, 1.0].min, -1.0].max) * 180.0 / Math::PI
        puts "      면적~#{a.round(0)}mm2 법선=[#{n.x.round(5)},#{n.y.round(5)},#{n.z.round(5)}] 수평기울기=#{tilt.round(4)}deg Z=[#{b.min.z.to_mm.round(1)},#{b.max.z.to_mm.round(1)}]"
      end
    end
  end
  puts "모델 수정 없음"
rescue Exception => e
  puts "EXC #{e.class}: #{e.message}"
  puts e.backtrace.first(5).join(" | ")
end
nil
