# encoding: UTF-8
begin
  puts "###DZW-MEASURE-OILZONE-20260920"
  m = Sketchup.active_model
  ents = lambda { |s| s.is_a?(Sketchup::Group) ? s.entities : s.definition.entities }
  rows = []
  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
      up = lbl.upcase
      if up.include?('CENTER') || up.include?('FRAME') || up.include?('WINCH') || lbl.include?('윈치') || up.include?('OIL') || lbl.include?('오일') || lbl.include?('탱크') || up.include?('TANK')
        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)
        unless ps.empty?
          rows << ["#{path}/#{lbl}", ps.map { |p| p.x.to_mm }.minmax, ps.map { |p| p.y.to_mm }.minmax, ps.map { |p| p.z.to_mm }.minmax, depth]
        end
      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 "-- 관련 부재 (월드) --"
  rows.sort_by { |r| r[1][0] }.each do |p, xr, yr, zr, d|
    puts "  #{p} X=#{xr.map { |v| v.round(1) }.inspect} Y=#{yr.map { |v| v.round(1) }.inspect} Z=#{zr.map { |v| v.round(1) }.inspect}"
  end
  # 좌우 사이드(절곡판) 기준
  lh = nil
  rh = nil
  m.entities.each do |e|
    next unless e.is_a?(Sketchup::Group) || e.is_a?(Sketchup::ComponentInstance)
    nm = (e.respond_to?(:name) ? e.name.to_s : '')
    lh = e if nm == 'RJ102-0604_LH_20260918'
    rh = e if nm == 'RJ102-0602_RH_20260918'
  end
  if lh && rh
    puts "-- 사이드 기준 --"
    puts "  LH Y=[#{lh.bounds.min.y.to_mm.round(1)},#{lh.bounds.max.y.to_mm.round(1)}] RH Y=[#{rh.bounds.min.y.to_mm.round(1)},#{rh.bounds.max.y.to_mm.round(1)}] 안쪽폭=#{(rh.bounds.min.y.to_mm - lh.bounds.max.y.to_mm).round(1)}"
  end
  puts "모델 수정 없음"
rescue Exception => e
  puts "EXC #{e.class}: #{e.message}"
  puts e.backtrace.first(5).join(" | ")
end
nil
