# -*- coding: utf-8 -*-
# 1+2단계: 선택 부재 정밀 실측 + 타깃 공간 실측  (mm = inch * 25.4)
def f(v); (v.to_f*25.4).round(1); end
m = Sketchup.active_model
sel = m.selection
puts "SEL_COUNT: #{sel.count}"
if sel.count == 0
  puts "STOP: 선택 0개"
else
def dump(e, ind, depth)
  pre = "  "*ind
  bb = e.bounds
  nm = (e.respond_to?(:name) ? e.name : "")
  dn = ""; begin; dn = e.definition.name; rescue; end
  puts "#{pre}#{e.class.name.split('::').last} name=#{nm.inspect} def=#{dn.inspect}"
  puts "#{pre}  X #{f(bb.min.x)}~#{f(bb.max.x)} (#{f(bb.width)})  Y #{f(bb.min.y)}~#{f(bb.max.y)} (#{f(bb.depth)})  Z #{f(bb.min.z)}~#{f(bb.max.z)} (#{f(bb.height)})"
  return if depth <= 0
  return unless e.respond_to?(:entities)
  ents = e.entities
  faces = ents.grep(Sketchup::Face)
  edges = ents.grep(Sketchup::Edge)
  puts "#{pre}  faces=#{faces.size} edges=#{edges.size} groups=#{ents.grep(Sketchup::Group).size}"
  # 구멍(원호) 집계
  curves = edges.map{|x| x.curve}.compact.uniq.select{|c| c.is_a?(Sketchup::ArcCurve)}
  if curves.size>0
    h = Hash.new(0)
    curves.each{|c| (h[f(c.radius*2).round(0)] += 1) rescue nil}
    puts "#{pre}  HOLES dia_mm=>n : #{h.sort.inspect}"
    cc = Hash.new(0)
    curves.each{|c| ctr=c.center; cc["#{f(ctr.x)},#{f(ctr.y)},#{f(ctr.z)}"]+=1 rescue nil}
    puts "#{pre}  HOLE_CENTERS(local mm, max20): #{cc.keys.first(20).inspect}"
  end
  # 판두께 추정: 최소 bbox 변
  (ents.grep(Sketchup::Group)+ents.grep(Sketchup::ComponentInstance)).each{|s| dump(s, ind+1, depth-1)}
end
  sel.each{|e| dump(e, 0, 2)}
end

puts "\n===== 타깃 공간 / 주변 부재 ====="
pat = /윈치|WINCH|하부커버|트랙|SUPPORT_WD|BASE_PLATE|프레임/i
m.entities.each do |e|
  next unless e.is_a?(Sketchup::Group) || e.is_a?(Sketchup::ComponentInstance)
  nm = (e.respond_to?(:name) ? e.name.to_s : "")
  dn = ""; begin; dn = e.definition.name.to_s; rescue; end
  next unless (nm =~ pat) || (dn =~ pat)
  bb = e.bounds
  puts "TOP name=#{nm.inspect} def=#{dn.inspect} hidden=#{e.hidden?} X #{f(bb.min.x)}~#{f(bb.max.x)} Y #{f(bb.min.y)}~#{f(bb.max.y)} Z #{f(bb.min.z)}~#{f(bb.max.z)}"
end
puts "-- 전체 상위 그룹/컴포넌트 이름 목록 --"
names = []
m.entities.each do |e|
  next unless e.is_a?(Sketchup::Group) || e.is_a?(Sketchup::ComponentInstance)
  nm = (e.respond_to?(:name) ? e.name.to_s : "")
  dn = ""; begin; dn = e.definition.name.to_s; rescue; end
  names << "#{nm}|#{dn}"
end
puts names.uniq.join("\n")
"DONE2"
