轉(zhuǎn)自http://blog.csdn.net/article/details/38357541
里面還有更多內(nèi)容。
我們可以在Sketchup中用“弧”和“圓”工具去畫出相應圖形,但是我們畫出來并不是真正意義上的圓或弧形,而是由一段段細小的直線片段組成的。用編碼實現(xiàn)時,實體類有三個方法來生成類似于弧形的圖案,每一個方法返回的是一組邊對象集。這三個方法是add_curve, add_circle, 和 add_arc。這三個方法于畫多邊形的方法非常類似,即add_ngon。 1、畫曲線——add_curve [ruby] view plaincopy
- 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方法產(chǎn)生一個五條邊組成的邊集,這就是說,add_curve方法產(chǎn)生的還是直線,并不是圓滑的曲線。但是,當我們把點數(shù)增加時,這些由點生成的緊密的多段線將像一個圓曲線。
2、圓形--add_circle add_circle方法需要三個參數(shù),分別是原點坐標、圓正向向量、和直徑,如下代碼所示。 [ruby] view plaincopy
- circle = Sketchup.active_model.entities.add_circle [1, 2, 3],[4, 5, 6], 7
(1,2,3)是原點坐標,(4.5.6)是圓的正向向量,7是圓的直徑,下圖紅色方框中顯示的就是生成的圓。在代碼運行結果中返回的就是一系列邊的對象。
3、多邊形 畫多邊形的方法add_ngon與畫圓的方法很像,唯一的區(qū)別在于,在畫圓時,系統(tǒng)默認是的是由24條直線片段圍成圓,而多邊形是由你指定邊數(shù),下面看看代碼如何實現(xiàn)。
[ruby] view plaincopy
- 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、弧形 畫弧形相比畫圓,需要我們指定起點和止點的角度,如下一條命令:
[ruby] view plaincopy
- arc = Sketchup.active_model.entities.add_arc [0,0,0], [0,1,0],[0,0,1], 50, 0, 90.degrees
第一個參數(shù)(0,0,0)表示圓弧的原點;
第二個參數(shù)(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.
|