# encoding: UTF-8
begin
  puts "###DZW-FIND-SWD04B-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
      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
      begin
        col.call(ents.call(s), st)
      rescue Exception
        next
      end
      unless ps.empty?
        xs = ps.map { |p| p.x.to_mm }
        ys = ps.map { |p| p.y.to_mm }
        zs = ps.map { |p| p.z.to_mm }
        hits << ["#{path}/#{lbl}", s.class.to_s, xs.minmax, ys.minmax, zs.minmax,
                 [s.transformation.xscale, s.transformation.yscale, s.transformation.zscale], ps.length, depth]
      end
      walk.call(ents.call(s), st, "#{path}/#{lbl}", depth + 1) if depth < 2
    end
  end
  walk.call(m.entities, Geom::Transformation.new, '', 0)
  puts "-- SWD 포함 --"
  hits.select { |h| h[0].upcase.include?('SWD') }.each do |h|
    puts "  #{h[0]} #{h[1]} X=#{h[2].map { |v| v.round(1) }.inspect} Y=#{h[3].map { |v| v.round(1) }.inspect} Z=#{h[4].map { |v| v.round(1) }.inspect} scale=[#{h[5][0].round(4)},#{h[5][1].round(4)},#{h[5][2].round(4)}] 정점#{h[6]}"
  end
  puts "-- 하부프레임커버 하위 (깊이<=2) --"
  hits.select { |h| h[0].include?('하부프레임커버') && h[7] <= 2 }.each do |h|
    puts "  d#{h[7]} #{h[0]} X=#{h[2].map { |v| v.round(1) }.inspect} Y=#{h[3].map { |v| v.round(1) }.inspect} Z=#{h[4].map { |v| v.round(1) }.inspect} scale=[#{h[5][0].round(4)},#{h[5][1].round(4)},#{h[5][2].round(4)}] 정점#{h[6]}"
  end
  puts "모델 수정 없음"
rescue Exception => e
  puts "EXC #{e.class}: #{e.message}"
  puts e.backtrace.first(5).join(" | ")
end
nil
