# encoding: UTF-8
# rd20 — 윈치 베이스판 + 주황판(조립부원판) 실측 (읽기 전용)
def r1(v); (v * 25.4).round(1); end
def deg(a); (a * 180.0 / Math::PI).round(4); end
m = Sketchup.active_model
out = []

def ents_of(e)
  e.is_a?(Sketchup::ComponentInstance) ? e.definition.entities : e.entities
end

def collect(ent, tr, faces)
  ents_of(ent).each do |e|
    case e
    when Sketchup::Face
      pts = e.vertices.map { |v| v.position.transform(tr) }
      n = e.normal.transform(tr); n.normalize!
      faces << [pts, n, e.area * 645.16]
    when Sketchup::Group, Sketchup::ComponentInstance
      collect(e, tr * e.transformation, faces)
    end
  end
end

["WINCH_BASE_PLATE_25T_1", "조립부원판_20260919"].each do |nm|
  obj = m.entities.find { |e| (e.is_a?(Sketchup::Group) || e.is_a?(Sketchup::ComponentInstance)) && e.name == nm }
  if obj.nil?
    out << "#{nm} 없음"
    next
  end
  fs = []
  collect(obj, obj.transformation, fs)
  b = obj.bounds
  out << "=== #{nm} (#{obj.class}) 면 #{fs.length}개 ==="
  out << "  bbox (#{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=#{obj.transformation.to_a.map { |v| v.round(5) }.inspect}"
  fs.sort_by { |f| -f[2] }[0, 8].each do |pts, n, a|
    zs = pts.map { |p| r1(p.z) }
    cx = pts.map { |p| p.x }.inject(:+) / pts.length
    cy = pts.map { |p| p.y }.inject(:+) / pts.length
    out << "   A=#{a.round(0)} n=(#{n.x.round(5)},#{n.y.round(5)},#{n.z.round(5)}) 수평기울기=#{deg(Math.acos([n.z.abs, 1.0].min))}도 z #{zs.min}..#{zs.max} 중심(#{r1(cx)},#{r1(cy)})"
  end
end
puts out.join("\n")
"RD20"
