转自http://blog.csdn.net/article/details/38357541
里面还有更多内容。
我们可以在Sketchup中用“弧”和“圆”工具去画出相应图形,但是我们画出来并不是真正意义上的圆或弧形,而是由一段段细小的直线片段组成的。用编码实现时,实体类有三个方法来生成类似于弧形的图案,每一个方法返回的是一组边对象集。这三个方法是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方法产生一个五条边组成的边集,这就是说,add_curve方法产生的还是直线,并不是圆滑的曲线。但是,当我们把点数增加时,这些由点生成的紧密的多段线将像一个圆曲线。
2、圆形--add_circle add_circle方法需要三个参数,分别是原点坐标、圆正向向量、和直径,如下代码所示。 [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与画圆的方法很像,唯一的区别在于,在画圆时,系统默认是的是由24条直线片段围成圆,而多边形是由你指定边数,下面看看代码如何实现。
[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
第一个参数(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.
|