四、Sketchup用ruby進行二次開發--Edge Arrays: Curves, Circles, Arcs和 Polygons
我們可以在Sketchup中用“弧”和“圓”工具去畫出相應圖形,但是我們畫出來並不是真正意義上的圓或弧形,而是由一段段細小的直線片段組成的。用編碼實現時,實體類有三個方法來生成類似於弧形的圖案,每一個方法返回的是一組邊物件集。這三個方法是add_curve, add_circle, 和 add_arc。這三個方法於畫多邊形的方法非常類似,即add_ngon。
1、畫曲線——add_curve
pt1 = [0, 1, 0] pt2 = [0.588, -0.809, 0] pt3 = [-0.951, 0.309, 0] pt4 = [0.951, 0.309, 0] pt5 = [-0.588, -0.809, 0] curve = Sketchup.active_model.entities.add_curve pt1, pt2, pt3,pt4, pt5, pt1
在這裡,add_curve方法產生一個五條邊組成的邊集,這就是說,add_curve方法產生的還是直線,並不是圓滑的曲線。但是,當我們把點數增加時,這些由點生成的緊密的多段線將像一個圓曲線。
2、圓形--add_circle
add_circle方法需要三個引數,分別是原點座標、圓正向向量、和直徑,如下程式碼所示。
circle = Sketchup.active_model.entities.add_circle [1, 2, 3],[4, 5, 6], 7
(1,2,3)是原點座標,(4.5.6)是圓的正向向量,7是圓的直徑,下圖紅色方框中顯示的就是生成的圓。在程式碼執行結果中返回的就是一系列邊的物件。
3、多邊形
畫多邊形的方法add_ngon與畫圓的方法很像,唯一的區別在於,在畫圓時,系統預設是的是由24條直線片段圍成圓,而多邊形是由你指定邊數,下面看看程式碼如何實現。
程式碼很容易理解,不做多解釋。ents = Sketchup.active_model.entities normal = [0, 0, 1] radius = 1 # Polygon with 8 sides ents.add_ngon [0, 0, 0], normal, radius, 8 # Circle with 8 sides ents.add_circle [3, 0, 0], normal, radius, 8 # Polygon with 24 sides ents.add_ngon [6, 0, 0], normal, radius, 24 # Circle with 24 sides ents.add_circle [9, 0, 0], normal, radius
4、弧形
畫弧形相比畫圓,需要我們指定起點和止點的角度,如下一條命令:
arc = Sketchup.active_model.entities.add_arc [0,0,0], [0,1,0],[0,0,1], 50, 0, 90.degrees
第一個引數(0,0,0)表示圓弧的原點;
第二個引數(0,1,0)表示圓弧的起點。
The following command creates an arc centered at [0, 0, 0] that intercepts an angle from 0° to 90°. The angle is measured from the y-axis, so the vector at 0° is [0, 1, 0]. The arc has a radius of 5 and lies in the x-y plane, so its normal vector is [0, 0, 1]. The number of segments is left to its default value.