有人可以解释一下 vertices.flatten!在 SketchUp Ruby 中对我来说?

发布于 2025-01-14 10:16:56 字数 1734 浏览 3 评论 0原文

下面是一段代码(由 Rafael Rivera 提供),它在 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

有人可以向我解释一下这行代码,它实际上做了什么以及为什么代码需要它工作。

vertices.flatten!

我知道什么是“.flatten!”正常情况下确实如此。我从 ruby​​api.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

但在 SketchUp 的世界里,“.flatten!” 是什么意思?实际上呢?

我将顶点数组“放入”控制台,并将其视为输出。

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

那么我到底在“扁平化”什么呢?

谢谢!

Below is a piece of code (credit to Rafael Rivera) which plots points at the vertices of a model in 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

Could someone please explain to me this line of code, what it actually does and why it's necessary for the code to work.

vertices.flatten!

I am aware what ".flatten!" does under normal circumstances. I understand this example perfectly from the 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

But in the world of SketchUp, what does ".flatten!" actually do?

I 'put' the vertices array to my console and I see this as output.

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

So what am I 'flattening' exactly?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青巷忧颜 2025-01-21 10:16:56

它的行为与您在 flatten 中观察到的行为完全相同,唯一的区别是它更改了调用它的对象,而不是返回更改后的对象。

让我们看一下这三行:

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

首先,创建了一个空数组。然后,通过迭代所有边,将边的顶点(很可能存储在数组中)添加到数组中。这意味着在这一行之后,您将拥有一个嵌套的顶点数组,如下所示(伪代码):

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

vertices.flatten! 然后会将 vertices 展平为:

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

It does exactly the same as the behavior you already observed with flatten with the only difference that it changes the object on which it is called instead of returning a changed object.

Let's have a look at these three lines:

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

First, there is an empty array created. Then by iterating over all edges the edges' vertices (which are very likely are stored in an array) are added to the array. That means after this line you have a nested array of vertices that looks like this (pseudo-code):

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

vertices.flatten! will then flatten the vertices to:

[vertice_1, vertice_2, vertice_3, vertice_4, vertice_1, vertice_4]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文