# encoding: UTF-8
# cnted — 최상위 loose edge / 안내선 집계 (읽기 전용)
MM = 25.4
m = Sketchup.active_model
out = []
edges = m.entities.grep(Sketchup::Edge)
loose = edges.select { |e| e.faces.empty? }
clines = m.entities.grep(Sketchup::ConstructionLine)
cpts = m.entities.grep(Sketchup::ConstructionPoint)
txt = m.entities.grep(Sketchup::Text)
dims = m.entities.to_a.select { |e| e.is_a?(Sketchup::DimensionLinear) || e.is_a?(Sketchup::DimensionRadial) }
out << "최상위 에지 #{edges.length}개 (그중 면 없는 고립 #{loose.length}개)"
out << "안내선 #{clines.length}개 / 안내점 #{cpts.length}개 / 텍스트 #{txt.length}개 / 치수 #{dims.length}개"
out << "최상위 면(loose face) #{m.entities.grep(Sketchup::Face).length}개"
out << "최상위 그룹 #{m.entities.grep(Sketchup::Group).length}개 / 컴포넌트 #{m.entities.grep(Sketchup::ComponentInstance).length}개"
sol = m.entities.grep(Sketchup::Group).select { |g| g.manifold? rescue false }
out << "최상위 솔리드 그룹 #{sol.length}개"
out << "== 고립 에지 상세(최대 40개) =="
loose.sort_by { |e| -e.entityID }[0, 40].each do |e|
  a = e.start.position; b = e.end.position
  out << "  eid=#{e.entityID} (#{(a.x*25.4).round(1)},#{(a.y*25.4).round(1)},#{(a.z*25.4).round(1)})-(#{(b.x*25.4).round(1)},#{(b.y*25.4).round(1)},#{(b.z*25.4).round(1)}) 길이 #{(e.length*25.4).round(1)}mm layer=#{e.layer.name}"
end
out << "== 안내선 상세(최대 20개) =="
clines.sort_by { |e| -e.entityID }[0, 20].each do |e|
  out << "  eid=#{e.entityID} start=#{e.start ? "(#{(e.start.x*25.4).round(1)},#{(e.start.y*25.4).round(1)},#{(e.start.z*25.4).round(1)})" : 'inf'} len=#{e.end && e.start ? (e.start.distance(e.end)*25.4).round(1) : '-'}"
end
puts out.join("\n")
"CNTED"
