# encoding: UTF-8
m = Sketchup.active_model
sel = m.selection.to_a
puts "선택수=#{sel.size}"
sel.each do |e|
  b=e.bounds; nm = (e.respond_to?(:name) && !e.name.to_s.empty?) ? e.name : '(무명)'
  mt = e.material ? "#{e.material.name} #{e.material.color.to_a.inspect}" : 'nil'
  puts "  SEL #{e.class} #{nm} mat=#{mt} bbox x %.2f..%.2f y %.2f..%.2f z %.2f..%.2f" %
    [b.min.x.to_mm,b.max.x.to_mm,b.min.y.to_mm,b.max.y.to_mm,b.min.z.to_mm,b.max.z.to_mm]
end
# 보라 계열 재질 사용 그룹 탐색 (R>80, B>80, G < min(R,B)-20)
hits = []
scan = nil
scan = lambda do |ents, path, depth|
  (ents.grep(Sketchup::Group) + ents.grep(Sketchup::ComponentInstance)).each do |e|
    nm = (e.respond_to?(:name) && !e.name.to_s.empty?) ? e.name : '(무명)'
    cols = []
    cols << e.material.color if e.material
    sub = e.is_a?(Sketchup::Group) ? e.entities : e.definition.entities
    sub.grep(Sketchup::Face).each { |f| cols << f.material.color if f.material; cols << f.back_material.color if f.back_material }
    pu = cols.select { |c| c.red > 70 && c.blue > 70 && c.green < [c.red, c.blue].min - 20 }
    if !pu.empty?
      b=e.bounds
      hits << "#{path}/#{nm} 보라면=#{pu.size} #{pu.first.to_a.inspect} bbox x %.2f..%.2f y %.2f..%.2f z %.2f..%.2f" %
        [b.min.x.to_mm,b.max.x.to_mm,b.min.y.to_mm,b.max.y.to_mm,b.min.z.to_mm,b.max.z.to_mm]
    end
    scan.call(sub, "#{path}/#{nm}", depth+1) if depth < 3
  end
end
scan.call(m.entities, '', 0)
puts "보라 후보 #{hits.size}개"
hits.each { |h| puts "  #{h}" }
puts "모델 재질: " + m.materials.map{|mt| "#{mt.name}#{mt.color.to_a.inspect}"}.join(', ')
nil
