# encoding: UTF-8
# rd14 — 그룹#2 3부재 실측 (읽기 전용)
def r1(v); (v * 25.4).round(1); end
m = Sketchup.active_model
out = []
g2 = m.entities.grep(Sketchup::Group).find { |g| g.definition.name == "그룹#2" }
if g2.nil?
  out << "그룹#2 없음"
else
  out << "그룹#2 transformation=#{g2.transformation.to_a.map { |v| v.round(4) }.inspect}"
  g2.entities.grep(Sketchup::Group).each do |s|
    d = s.definition
    lb = d.bounds
    tr = s.transformation.to_a
    sx = Geom::Vector3d.new(tr[0], tr[1], tr[2]).length
    sy = Geom::Vector3d.new(tr[4], tr[5], tr[6]).length
    sz = Geom::Vector3d.new(tr[8], tr[9], tr[10]).length
    b = s.bounds
    out << "=== #{s.name} ==="
    out << "  def=#{d.name} faces=#{d.entities.grep(Sketchup::Face).length} solid=#{s.manifold?}"
    out << "  로컬 bbox (#{r1(lb.min.x)},#{r1(lb.min.y)},#{r1(lb.min.z)})..(#{r1(lb.max.x)},#{r1(lb.max.y)},#{r1(lb.max.z)})"
    out << "  scale #{sx.round(4)}/#{sy.round(4)}/#{sz.round(4)}  world (#{r1(b.min.x)},#{r1(b.min.y)},#{r1(b.min.z)})..(#{r1(b.max.x)},#{r1(b.max.y)},#{r1(b.max.z)})"
    out << "  tr=#{tr.map { |v| v.round(4) }.inspect}"
    # 로컬 좌표에서 세로단면 프로파일: 긴축 판정
    ext = [lb.width, lb.height, lb.depth]
    axis = ext.index(ext.max)  # 0=x 1=y 2=z
    out << "  긴축=#{%w[x y z][axis]} 길이=#{r1(ext.max)}"
    # 긴축을 따라 구간별 (다른 두 축) 범위
    fs = d.entities.grep(Sketchup::Face)
    pts = []
    fs.each { |f| f.vertices.each { |v| pts << v.position } }
    lo = [lb.min.x, lb.min.y, lb.min.z][axis]
    hi = [lb.max.x, lb.max.y, lb.max.z][axis]
    11.times do |i|
      a = lo + (hi - lo) * i / 10.0
      band = pts.select { |p| ((p.to_a[axis] - a).abs) < (hi - lo) / 40.0 }
      next if band.empty?
      o1 = (axis == 0 ? 1 : 0); o2 = (axis == 2 ? 1 : 2)
      v1 = band.map { |p| p.to_a[o1] }
      v2 = band.map { |p| p.to_a[o2] }
      out << "   #{%w[x y z][axis]}=#{r1(a)} : #{%w[x y z][o1]} #{r1(v1.min)}..#{r1(v1.max)} / #{%w[x y z][o2]} #{r1(v2.min)}..#{r1(v2.max)}"
    end
  end
end
puts out.join("\n")
"RD14"
