# -*- coding: utf-8 -*-
def f(v); (v.to_f*25.4).round(1); end
IN = 1.0/25.4
m = Sketchup.active_model
selg = m.selection.first
puts "SEL_TRANSFORM to_a:"
selg.transformation.to_a.each_slice(4){|r| puts "  #{r.map{|v| v.round(6)}.inspect}"}
xs=selg.transformation.xscale; ys=selg.transformation.yscale; zs=selg.transformation.zscale
puts "  xscale=#{xs.round(5)} yscale=#{ys.round(5)} zscale=#{zs.round(5)}"

def verts(e, tr, out)
  if e.is_a?(Sketchup::Group) || e.is_a?(Sketchup::ComponentInstance)
    t2 = tr * e.transformation
    e.definition.entities.each{|c| verts(c, t2, out)} rescue e.entities.each{|c| verts(c, t2, out)}
  elsif e.is_a?(Sketchup::Edge)
    e.vertices.each{|v| out << v.position.transform(tr)}
  end
end

puts "\n== 트랙 내측면 프로파일 (X 3800~4560 구간, Z 25mm 밴드) =="
{"트랙L_실형상_20260919"=>:L, "트랙R_실형상_20260919"=>:R}.each do |nm, side|
  g = m.entities.find{|e| (e.respond_to?(:name)? e.name.to_s : "")==nm}
  next unless g
  pts = []
  verts(g, Geom::Transformation.new, pts)
  puts "  #{nm}  verts=#{pts.size}"
  sel_pts = pts.select{|p| f(p.x) >= 3750 && f(p.x) <= 4600}
  puts "    X3750~4600 내 정점 수 = #{sel_pts.size}"
  bands = Hash.new{|h,k| h[k]=[]}
  sel_pts.each{|p| bands[(f(p.z)/25.0).floor*25] << f(p.y)}
  bands.keys.sort.each do |zb|
    ys = bands[zb]
    val = (side==:L) ? ys.max : ys.min
    puts "    Z #{zb}~#{zb+25} : n=#{ys.size} #{side==:L ? 'maxY' : 'minY'}=#{val.round(1)}"
  end
  # 전체 X에 대한 극한
  allv = (side==:L) ? pts.map{|p| f(p.y)}.max : pts.map{|p| f(p.y)}.min
  puts "    전체 #{side==:L ? 'maxY' : 'minY'} = #{allv.round(1)}"
  # X 최소(선단)
  puts "    X min/max = #{pts.map{|p| f(p.x)}.min.round(1)} / #{pts.map{|p| f(p.x)}.max.round(1)}"
end
"DONE4"
