# encoding: UTF-8
m = Sketchup.active_model
MM = 25.4
def mmv(p); [(p.x*25.4).round(1),(p.y*25.4).round(1),(p.z*25.4).round(1)]; end

g33 = m.entities.grep(Sketchup::Group).find{|g| b=g.bounds; (b.min.z*25.4).round(1) == -129.8 }
puts "== G33 =="
if g33
  g33.entities.each_with_index do |e,i|
    next unless e.respond_to?(:bounds)
    b=e.bounds
    puts "  #{e.class} name=#{(e.respond_to?(:name) ? e.name : '')} bbox #{mmv(b.min)} .. #{mmv(b.max)}"
  end
end

["RJ102-0604_LH_20260918","RJ102-0602_RH_20260918"].each do |nm|
  g = m.entities.grep(Sketchup::Group).find{|x| x.name==nm}
  next unless g
  puts "== #{nm} =="
  tr = g.transformation
  # collect faces whose normal has significant -Z, in X window
  cands = []
  g.entities.grep(Sketchup::Face).each do |f|
    n = f.normal.transform(tr)
    next if n.z > -0.2
    ctr = f.bounds.center.transform(tr)
    cx = ctr.x*25.4
    next unless cx > 3700 && cx < 4700
    cands << [f, n, ctr, f.area*25.4*25.4]
  end
  cands.sort_by!{|c| -c[3]}
  cands[0,8].each do |f,n,ctr,a|
    ang = Math.acos([[-n.z,-1.0].max,1.0].min)*180/Math::PI
    puts "  face area=#{(a/100).round(1)}cm2 ctr=#{mmv(ctr)} n=(#{n.x.round(4)},#{n.y.round(4)},#{n.z.round(4)}) tiltdeg=#{ang.round(2)}"
    pts = f.outer_loop.vertices.map{|v| mmv(v.position.transform(tr))}
    puts "     pts=#{pts.inspect}" if pts.length <= 8
  end
end
"SURVEY2"
