五、Sketchup用ruby進行二次開發--建立圖形(面及pushpull、followMe方法的應用)
在上一節講到了如何建立直線、曲線、圓及弧線,本節講如何建立面和使用push/pull和Follow Me方法對面進行推拉建立三維體。
1、建立面
建立面使用的方法是add_face,也是繼承於Entities父類。add_face的引數是由一系列用逗號分割開的點或邊,下面看看程式碼如何實現。
生成的圖形就是一個五邊形# 先建立五個點 pt1 = [0, 1, 0] pt2 = [-0.951, 0.309, 0] pt3 = [-0.588, -0.809, 0] pt4 = [ 0.588, -0.809, 0] pt5 = [ 0.951, 0.309, 0] # 用這五個點畫一個面 pent = Sketchup.active_model.entities.add_face pt1, pt2, pt3, pt4, pt5 # 打印出這五個點的座標資訊 puts "Point 0: " + pent.vertices[0].position.to_s puts "Point 1: " + pent.vertices[1].position.to_s puts "Point 2: " + pent.vertices[2].position.to_s puts "Point 3: " + pent.vertices[3].position.to_s
特別注意的是點的順序非常重要,如果把pt2和pt3的順改變一下,這個圖形看起來就不像一個五邊形了。但改變點的順時針方向或逆時針方向是沒有關係,依然是五邊形。
有趣的是生成的面的正面符合右手定理,即如果點的順序是順時針方向,它的正面向量就是(0,0,-1),我可以用程式碼驗證一下。面的方向在push和pull中會用到,稍後講到。
pent.normal
#輸出 (0, 0, -1)
pent.reverse!
pent.normal
#輸出 (0, 0, 1)
2、classify_point方法
classify_point方法用於判斷給定的點是否在這個面上,判斷方法是通過返回的值,如表所示。
• 0 - 不確定的點
• 1 - 點在面上
• 2 - 點在面的某一條邊上
• 4 - 點面的一個頂點
• 8 - 點在面所在的平面上,但不在這個面上
• 16 - 點不在這個面所在的平面上
例如:
3、pushpull方法face = Sketchup.active_model.entities.add_face [-1, -1, 0], [-1, 1, 0], [1, 1, 0], [1, -1, 0] face.classify_point [0, 0, 0] #輸出 1 face.classify_point [1, 1, 0] #輸出 4 face.classify_point [1, 2, 0] #輸出 8 face.classify_point [1, 1, 1] #輸出 16
這一小節是本節的重點,相信使用過Sketchup的筒子們,肯定特別熟悉推拉工具。face類提供了兩個方法把二維面擴充套件為三維體:pushpull和followme,這跟Sketchup的Push/Pull和Follow Me工具是一樣的。
pushpull非常容易使用和理解,pushpull的引數是一個整數,如果整數是正數,那麼就會沿著面的正向量進行拉伸,類似,如果是負數就會沿著反方向拉伸。說到這裡不能不說在已有三維體上進行切割功能了,只要在一個三維體指定一個面進行反向拉伸就能將其切除了,要是說的不明白可以看下面這個例子。
# Create the box
ent = Sketchup.active_model.entities
main_face = ent.add_face [0,0,0], [6,0,0], [6,8,0], [0,8,0]
main_face.reverse!
main_face.pushpull 5
# Draw a line across the upper-right corner
cut = ent.add_line [6,6,5], [4,8,5]
# Remove the new face
cut.faces[1].pushpull -5
圖1,生成一個四方體
圖2,在頂部生成一條
圖3,切去一個角。很神奇吧。
如果最後一行程式碼
cut.faces[1].pushpull -5
把faces[1]變為faces[0],那麼圖形就變為留下小的那一小部分。
4、followme方法
通過pushpull方法,我們可以沿著正向或反向進行拉伸,這還遠遠滿足不了我們的需求。現在followme方法可以沿著你給定的向量進行擴充套件。
followme的擴充套件向量我們成為路徑,可以使一條或多條直線,如果是多條直線則需要滿足一下兩個要求。
* 所有的路徑必須是相連的。
*要進行拉伸的面不能與路徑所在的面重合,這一條非常重要。
下面看例子
# Access the Entities container
model = Sketchup.active_model
ent = model.entities
# 建立一個圓面,直徑為1英寸
circle = ent.add_circle [0,0,0], [0,0,1], 1
circle_face = ent.add_face circle
# 建立一個正方形的路徑
path = ent.add_curve [10,0,0], [10,0,5], [10,5,5],
[10,5,0], [10,0,0]
# Extrude the circle along the path
circle_face.followme path
生成的圖形類似於兩個直角的白鐵皮煙囪合一塊。再來看一個例子
# Access the Entities object
model = Sketchup.active_model
ents = model.entities
# Create the 2-D shape
curve = ents.add_curve [0, 0, 1.244209], [0.116554, 0, 1.238382],
[0.160261, 0, 1.217985], [0.186486, 0, 1.188846],
[0.1894, 0, 1.165536], [0.17483, 0, 1.145139],
[0.142778, 0, 1.127656], [0.096157, 0, 1.118914],
[0.093243, 0, 1.063551], [0.175152, 0, 0.996269],
[0.175152, 0, 0.915269], [0.28237, 0, 0.871026],
[0.375392, 0, 0.801741], [0.448486, 0, 0.711683],
[0.497151, 0, 0.606398], [0.51839, 0, 0.492371],
[0.510894, 0, 0.376625], [0.475126, 0, 0.26629],
[0.413287, 0, 0.168161], [0.329188, 0, 0.088283],
[0.228007, 0, 0.031575], [0.115978, 0, 0.001531],
[0, 0, 0], [0, 0, 1.244209]
curve_face = ents.add_face curve
# Create the circular path
path = ents.add_circle [0, 0, 0], [0, 0, 1], 2
# Create the figure
curve_face.followme path
生成的圖形為下圖所示,這圖形比較複雜,大家看看程式碼也很好理解。
由於這個以後用的比較多,我再來第三個例子吧。
# Create the box
ents = Sketchup.active_model.entities
main_face = ents.add_face [0,0,0], [5,0,0], [5,8,0], [0,8,0]
main_face.reverse!
main_face.pushpull 6, true
# Draw a line across a corner
cut = ents.add_line [5, 7, 6], [5, 8, 5]
# Create the chamfer
cut.faces[0].followme main_face.edges
生成的圖形為
好了,今天到這吧,馬上凌晨1點了。