# encoding: UTF-8
# rd10 — intersect 결과 그룹 위치 추적 (읽기 전용)
def r1(v); (v * 25.4).round(1); end
m = Sketchup.active_model
out = []
out << "== 최상위 그룹/컴포넌트 =="
m.entities.each do |e|
  next unless e.is_a?(Sketchup::Group) || e.is_a?(Sketchup::ComponentInstance)
  b = e.bounds
  d = e.definition
  out << "  #{e.class} name=#{e.name.inspect} def=#{d.name.inspect} 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)}) 자식그룹=#{d.entities.grep(Sketchup::Group).length}"
end
out << "== 정의 전수: SWD_04 포함 이름 =="
m.definitions.each do |d|
  next unless d.name.to_s.include?("SWD_04") || d.instances.any? { |i| i.name.to_s.include?("SWD_04") }
  out << "  def=#{d.name.inspect} 인스턴스=#{d.instances.length} count_used=#{d.count_used_instances rescue '?'}"
  d.instances.each do |i|
    par = i.parent
    b = i.bounds
    lb = d.bounds
    lp = 0
    d.entities.grep(Sketchup::Face).each { |f| f.loops.each { |l| lp += 1 unless l.outer? } }
    out << "    inst name=#{i.name.inspect} parentdef=#{(par.respond_to?(:name) ? par.name : par.class).inspect} valid=#{i.valid?} deleted=#{i.deleted?}"
    out << "      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 << "      로컬 (#{r1(lb.min.x)},#{r1(lb.min.y)},#{r1(lb.min.z)})..(#{r1(lb.max.x)},#{r1(lb.max.y)},#{r1(lb.max.z)}) 내부루프=#{lp} solid=#{i.manifold?} faces=#{d.entities.grep(Sketchup::Face).length}"
  end
end
out << "== 그룹#2 내부 전수 =="
g2 = m.entities.grep(Sketchup::Group).find { |g| g.definition.name == "그룹#2" }
if g2
  g2.entities.each do |e|
    next unless e.is_a?(Sketchup::Group) || e.is_a?(Sketchup::ComponentInstance)
    b = e.bounds
    out << "  #{e.class} #{e.name.inspect} def=#{e.definition.name.inspect} (#{r1(b.min.x)},#{r1(b.min.y)},#{r1(b.min.z)})..(#{r1(b.max.x)},#{r1(b.max.y)},#{r1(b.max.z)})"
  end
  out << "  그룹#2 loose faces=#{g2.entities.grep(Sketchup::Face).length} ents=#{g2.entities.length}"
end
puts out.join("\n")
"RD10"
