# encoding: UTF-8
begin
  puts "###DZW-SCAN-BEND2-20260919"
  m = Sketchup.active_model
  targets = ['RJ102-0604_LH_20260918', 'RJ102-0602_RH_20260918', '트랙L_실형상_20260919']
  found = {}
  m.active_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 : '')
    found[nm] = e if targets.include?(nm)
  end
  targets.each do |t|
    e = found[t]
    if e.nil?
      puts "MISS '#{t}'"
      next
    end
    b = e.bounds
    puts "== '#{t}' #{e.class} 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
  g = found['RJ102-0604_LH_20260918']
  if g
    ents = g.entities
    puts "LH 엔티티수=#{ents.length} 클래스=#{ents.map { |e| e.class.to_s }.uniq.inspect}"
    tr = g.transformation
    faces = ents.grep(Sketchup::Face)
    puts "LH 면 수=#{faces.length}"
    faces.each_with_index do |f, i|
      next if i >= 30
      n = f.normal.transform(tr)
      pts = f.vertices.map { |v| v.position.transform(tr) }
      xs = pts.map { |p| p.x.to_mm }
      ys = pts.map { |p| p.y.to_mm }
      zs = pts.map { |p| p.z.to_mm }
      puts "  F[#{i}] area=#{(f.area * 0.00064516).round(1)}cm2 n=[#{n.x.round(3)},#{n.y.round(3)},#{n.z.round(3)}] 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)}] nv=#{pts.length}"
    end
    vx = []
    ents.grep(Sketchup::Edge).each do |ed|
      ed.vertices.each do |v|
        p = v.position.transform(tr)
        vx << [p.x.to_mm.round(1), p.z.to_mm.round(1)]
      end
    end
    puts "LH (X,Z) 고유 정점 #{vx.uniq.length}개: #{vx.uniq.sort.inspect}"
  end
rescue Exception => e
  puts "EXC #{e.class}: #{e.message}"
end
nil
