Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

383
Views
¿Podría alguien explicar vertices.flatten! en SketchUp Ruby para mí?

A continuación se muestra un fragmento de código (crédito a Rafael Rivera) que traza puntos en los vértices de un modelo en SketchUp.

 def pointplot model = Sketchup.active_model entities = model.active_entities selection = model.selection edges = selection.grep(Sketchup::Edge) if edges.empty? msg = 'Select one or more edges before using this tool.' UI.messagebox(msg) return end vertices = [] edges.each { |edge| vertices << edge.vertices } vertices.flatten! vertices.uniq! vertices.each { |vertex| entities.add_cpoint vertex.position } end def check_line sel = Sketchup.active_model.selection ok = sel.find { |e| e.typename == "Edge" } ok ? MF_ENABLED : MF_GRAYED end UI.add_context_menu_handler do |menu| menu.add_separator item = menu.add_item("Point Plot") { pointplot } menu.set_validation_proc(item) {check_line} end

¿Podría alguien explicarme esta línea de código, qué hace realmente y por qué es necesario que el código funcione?

vertices.flatten!

Soy consciente de lo que ".flatten!" hace en circunstancias normales. Entiendo perfectamente este ejemplo de rubyapi.org

 a = [ 0, [ 1, [2, 3], 4 ], 5 ] a.flatten!(1) # => [0, 1, [2, 3], 4, 5] a = [ 0, [ 1, [2, 3], 4 ], 5 ] a.flatten!(2) # => [0, 1, 2, 3, 4, 5] a = [ 0, [ 1, [2, 3], 4 ], 5 ] a.flatten!(3) # => [0, 1, 2, 3, 4, 5] [0, 1, 2].flatten!(1) # => nil

Pero en el mundo de SketchUp, ¿qué significa ".flatten!" en realidad hacer?

'Pongo' la matriz de vértices en mi consola y veo esto como salida.

 #<Sketchup::Vertex:0x00000180a0788440> #<Sketchup::Vertex:0x00000180a0788418> #<Sketchup::Vertex:0x00000180a07883c8> #<Sketchup::Vertex:0x00000180a07883a0> #<Sketchup::Vertex:0x00000180a0788440> #<Sketchup::Vertex:0x00000180a0788418> #<Sketchup::Vertex:0x00000180a07883c8>

Entonces, ¿qué estoy 'aplanando' exactamente?

¡Gracias!

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Hace exactamente lo mismo que el comportamiento que ya observó con flatten con la única diferencia de que cambia el objeto en el que se llama en lugar de devolver un objeto modificado.

Echemos un vistazo a estas tres líneas:

 vertices = [] edges.each { |edge| vertices << edge.vertices } vertices.flatten!

Primero, se crea una matriz vacía. Luego, al iterar sobre todos los bordes, los vértices de los bordes (que muy probablemente estén almacenados en una matriz) se agregan a la matriz. Eso significa que después de esta línea tienes una matriz anidada de vértices que se ve así (pseudocódigo):

 [[vertice_1, vertice_2], [vertice_3, vertice_4], [vertice_1, vertice_4]]

vertices.flatten! luego aplanará los vertices a:

 [vertice_1, vertice_2, vertice_3, vertice_4, vertice_1, vertice_4]
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!