代码改变世界

sketchup ruby编程之绘制梯段

2013-03-12 20:49  精诚所至 金石为开  阅读(1446)  评论(0编辑  收藏  举报

绘制梯段,首先定义一个绘制梯段的函数,然后,在插件下增加一个”绘制“命令,已激活插件。

UI.menu("PlugIns").add_item("Draw stairs") {
  UI.messagebox("I'm about to draw stairs!")
  draw_stairs
}
def draw_stairs
  stairs = 10
  rise = 8
  run = 12
  width = 100
  thickness = 3
  model = Sketchup.active_model
  entities = model.entities
  for step in 1..stairs
    x1 = 0
    x2 = width
    y1 = run * step
    y2 = run * (step + 1)
    z = rise * step
    pt1 = [x1, y1, z]
    pt2 = [x2, y1, z]
    pt3 = [x2, y2, z]
    pt4 = [x1, y2, z]
    new_face = entities.add_face pt1, pt2, pt3, pt4
    new_face.pushpull thickness
  end
end

首先定义四个变量,阶梯数,阶梯间隔,踏步宽度,踏步长度,添加面,然后推拉,完成一个踏步的绘制,,利用一个for循环,来完成全部踏步的绘制。

sketchup中利用ruby编程绘制台阶

原来ruby中的for循环是这个样子的,用到add_face和pushpull方法,以及如何添加新的菜单命令。