# encoding: UTF-8
# rd11 — intersect 결과(로컬 x폭 778mm) 전 모델 탐색 (읽기 전용)
def r1(v); (v * 25.4).round(1); end
m = Sketchup.active_model
out = []
out << "== 로컬 bbox 가 778x240x109 근처인 정의 =="
m.definitions.each do |d|
  b = d.bounds
  w = r1(b.width); h = r1(b.height); dp = r1(b.depth)
  next unless (w - 778.0).abs < 30.0 && (h - 240.0).abs < 30.0
  lp = 0
  d.entities.grep(Sketchup::Face).each { |f| f.loops.each { |l| lp += 1 unless l.outer? } }
  out << "  def=#{d.name.inspect} 로컬 #{w}x#{h}x#{dp} 내부루프=#{lp} faces=#{d.entities.grep(Sketchup::Face).length} 인스턴스=#{d.instances.length} group?=#{d.group?}"
  d.instances.each do |i|
    par = i.parent
    bb = i.bounds
    out << "     inst name=#{i.name.inspect} parentdef=#{(par.respond_to?(:name) ? par.name : par.class).inspect} world (#{r1(bb.min.x)},#{r1(bb.min.y)},#{r1(bb.min.z)})..(#{r1(bb.max.x)},#{r1(bb.max.y)},#{r1(bb.max.z)}) solid=#{i.manifold?}"
  end
end
out << "== 최근 생성 의심: 이름이 SWD_04 인 인스턴스 전수 =="
m.definitions.each do |d|
  d.instances.each do |i|
    out << "  #{i.name} parentdef=#{(i.parent.respond_to?(:name) ? i.parent.name : i.parent.class)} def=#{d.name}" if i.name.to_s.include?("SWD_04")
  end
end
out << "그룹#2 자식수=#{(m.entities.grep(Sketchup::Group).find { |g| g.definition.name == '그룹#2' } || Struct.new(:entities).new([])).entities.length rescue '?'}"
puts out.join("\n")
"RD11"
