# encoding: UTF-8
begin
  puts "###DZW-FIND-WIRE-20260920"
  m = Sketchup.active_model
  ents = lambda { |s| s.is_a?(Sketchup::Group) ? s.entities : s.definition.entities }
  found = []
  walk = nil
  walk = lambda do |ent, tr, path, depth|
    (ent.grep(Sketchup::Group) + ent.grep(Sketchup::ComponentInstance)).each do |s|
      nm = (s.respond_to?(:name) ? s.name.to_s : '')
      dn = s.is_a?(Sketchup::ComponentInstance) ? s.definition.name.to_s : ''
      lbl = nm.empty? ? "(무명)#{dn}" : nm
      st = tr * s.transformation
      if lbl.include?('윈치와이어')
        ps = []
        col = nil
        col = lambda do |en, t|
          en.grep(Sketchup::Edge).each { |ed| ed.vertices.each { |v| ps << v.position.transform(t) } }
          (en.grep(Sketchup::Group) + en.grep(Sketchup::ComponentInstance)).each { |q| col.call(ents.call(q), t * q.transformation) }
        end
        col.call(ents.call(s), st)
        if ps.empty?
          found << "#{path}/#{lbl} (정점 없음)"
        else
          found << "#{path}/#{lbl} 면수=#{ents.call(s).grep(Sketchup::Face).length} hidden=#{s.hidden?} 월드 X=#{ps.map { |p| p.x.to_mm }.minmax.map { |v| v.round(1) }.inspect} Y=#{ps.map { |p| p.y.to_mm }.minmax.map { |v| v.round(1) }.inspect} Z=#{ps.map { |p| p.z.to_mm }.minmax.map { |v| v.round(1) }.inspect}"
        end
      end
      walk.call(ents.call(s), st, "#{path}/#{lbl}", depth + 1) if depth < 3
    end
  end
  walk.call(m.entities, Geom::Transformation.new, '', 0)
  if found.empty?
    puts "결과: '윈치와이어_일체_20260920' 모델 어디에도 없음(깊이 4까지 탐색)"
  else
    found.each { |s| puts "발견 #{s}" }
  end
  puts "active_entities == model.entities ? #{(m.active_entities == m.entities)}"
  puts "모델 수정 없음"
rescue Exception => e
  puts "EXC #{e.class}: #{e.message}"
end
nil
