“add_cpoint”无法在 SketchUp 的 Ruby 中的方法中工作

发布于 2025-01-10 01:18:31 字数 1575 浏览 0 评论 0原文

我下面有一段代码,当代码在 SketchUp 的 Ruby 控制台中运行时,它在 [2,3,4] 和 [5,6,7] 处绘制了两个点。最终目标是能够允许用户选择一条线并在线的两端创建点,但我一次一步一步地进行。

sel = Sketchup.active_model.selection

fp = [2,3,4]
sp = [5,6,7]

fp = fp.map {|a| 39.3701 *a}
sp = sp.map {|a| 39.3701 *a}

p1 = Geom::Point3d.new(fp)
p2 = Geom::Point3d.new(sp)

addpoint1 = entities.add_cpoint p1
addpoint2 = entities.add_cpoint p2

我正在慢慢尝试使这段代码更加用户友好。我尝试通过制作它来升级它,因此您需要选择一条线来在 [2,3,4] 和 [5,6,7] 处创建两个点。

另外,数字 39.3701 是将单位转换为我想要的单位(米),SketchUp 有它自己的奇怪单位,我用“创可贴解决方案”解决了这个问题。

([2,3,4] [5,6,7] 是硬编码数组,我使用它们只是为了看看我是否正确编写了脚本)。

这是我对下一段代码的尝试。

def pointplot
    sel = Sketchup.active_model.selection

    fp = [2,3,4]
    sp = [5,6,7]
    
    fp = fp.map {|a| 39.3701 *a}
    sp = sp.map {|a| 39.3701 *a}

    p1 = Geom::Point3d.new(fp)
    p2 = Geom::Point3d.new(sp)

    addpoint1 = entities.add_cpoint p1
    addpoint2 = entities.add_cpoint p2
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

这段更大的代码将做的是当用户右键单击一行时向菜单添加一个选项。附加功能称为“点图”。

但是,当您单击“点图”功能时,它不会在 [2,3,4] 和 [5,6,7] 处创建两个点。

这些线路似乎存在问题。如果我在这两行之前放置 UI.messagebox() ,消息框将会出现。但是如果我将 UI.messagebox() 放在这两行之后,它就不会出现。

addpoint1 = entities.add_cpoint p1
addpoint2 = entities.add_cpoint p1

有人可以澄清我遇到的问题吗?

谢谢!

I have a piece of code below which plots two points at [2,3,4] and [5,6,7] when the code is run in SketchUp's Ruby console. The end goal is to be able to allow the user to select a line and create points at both ends of the line, but I am taking it step by step at a time.

sel = Sketchup.active_model.selection

fp = [2,3,4]
sp = [5,6,7]

fp = fp.map {|a| 39.3701 *a}
sp = sp.map {|a| 39.3701 *a}

p1 = Geom::Point3d.new(fp)
p2 = Geom::Point3d.new(sp)

addpoint1 = entities.add_cpoint p1
addpoint2 = entities.add_cpoint p2

I am slowly trying to make this code more user friendly. I tried to upgrade it by making it so you need to select a line to create two points at [2,3,4] and [5,6,7].

Also, the number 39.3701 is to convert the units into the units I want (meters), SketchUp has it's own weird units which I've sorta solved with a 'band aid solution'.

([2,3,4] [5,6,7] are hard-coded arrays which I am using just to see if I've scripted things correctly).

Here is my attempt at the next bit of code.

def pointplot
    sel = Sketchup.active_model.selection

    fp = [2,3,4]
    sp = [5,6,7]
    
    fp = fp.map {|a| 39.3701 *a}
    sp = sp.map {|a| 39.3701 *a}

    p1 = Geom::Point3d.new(fp)
    p2 = Geom::Point3d.new(sp)

    addpoint1 = entities.add_cpoint p1
    addpoint2 = entities.add_cpoint p2
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

What this bigger piece of code will do is add an option to the menu when the user right clicks on a line. The additional function is called "Point Plot."

However when you click on the 'Point Plot' function, it does not create the two points at [2,3,4] and [5,6,7].

There seems to be an issue around these lines. If I were to put a UI.messagebox() before those two lines the messagebox will appear. But if I put the UI.messagebox() after those two lines, it will not appear.

addpoint1 = entities.add_cpoint p1
addpoint2 = entities.add_cpoint p1

Could anyone please clarify the problem I am running into?

Thanks!

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

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

发布评论

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

评论(1

二手情话 2025-01-17 01:18:31

这是在选定边的每个顶点创建“构造点”的示例......

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 }

Here is an example for creating a 'construction point' at each vertex of selected edges...

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 }

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