# encoding: UTF-8
# READ-ONLY 조회
m = Sketchup.active_model
ents = m.entities
puts "MODEL_PATH=#{m.path}"
puts "TOP_ENTITY_COUNT=#{ents.count}"

def bb_s(e)
  begin
    b = e.bounds
    "min(%.1f,%.1f,%.1f) max(%.1f,%.1f,%.1f)" % [b.min.x.to_mm, b.min.y.to_mm, b.min.z.to_mm, b.max.x.to_mm, b.max.y.to_mm, b.max.z.to_mm]
  rescue
    "(bbox?)"
  end
end

def walk(ents, path, depth, out)
  ents.each do |e|
    next unless e.is_a?(Sketchup::Group) || e.is_a?(Sketchup::ComponentInstance)
    nm = e.name.to_s
    nm = "(무명)" if nm.empty?
    if e.is_a?(Sketchup::ComponentInstance)
      dn = e.definition.name.to_s
      nm = "#{nm} <def:#{dn}>"
    end
    out << "#{'  '*depth}- #{path}/#{nm} | hidden=#{e.hidden?} | layer=#{e.layer.name} | vis=#{e.layer.visible?} | #{bb_s(e)}"
    if depth < 3
      sub = e.is_a?(Sketchup::Group) ? e.entities : e.definition.entities
      walk(sub, "#{path}/#{nm}", depth+1, out)
    end
  end
end

out = []
walk(ents, "", 0, out)
puts "----- GROUP TREE -----"
puts out.join("\n")
puts "----- END (#{out.size} nodes) -----"
nil
