天天看点

sketchup Ruby API学习笔记(四)——Geom(几何下)

作者:钢之炼筋术士

5.16 Class: Geom::PolygonMesh多边形

entities = Sketchup.active_model.active_entities
face = entities.grep(Sketchup::Face).first

mesh = face.mesh

group = entities.add_group
group.entities.add_faces_from_mesh(mesh)           

要测试以上代码,先要在视图中画一个face。然后代码会复制这个face到mech,再用一个group包裹柱它。

5.16.1 #initialize⇒ Geom::PolygonMesh

5.16.1 #initialize(numpts) ⇒ Geom::PolygonMesh

5.16.1 #initialize(numpts, numpolys) ⇒ Geom::PolygonMesh

Parameters:

  • numpts (Integer) — How many points will be in the mesh.
  • numpolys(Integer) — How many polygons will be in the mesh.

这啥意思大家自己猜吧。实在不行就百度翻译把:)

很少情况下,构造还带参的。

5.16.2 #add_point(point) ⇒ Integer

mesh = Geom::PolygonMesh.new
point = Geom::Point3d.new(0, 1, 2)
index = mesh.add_point(point)           

返回从1开始

5.16.3 #add_polygon(index, index, index, ...) ⇒ Integer

5.16.3 #add_polygon(index_array) ⇒ Integer

5.16.3 #add_polygon(point3d, point3d, point3d, ...) ⇒ Integer

5.16.3 #add_polygon(point3d_array) ⇒ Integer

mesh = Geom::PolygonMesh.new
# add points to mesh...
mesh.add_point(Geom::Point3d.new(0, 0, 0))
mesh.add_point(Geom::Point3d.new(1, 0, 0))
mesh.add_point(Geom::Point3d.new(1, 1, 0))
polygon_index = mesh.add_polygon(1, 2, 3)           
mesh = Geom::PolygonMesh.new
# add points to mesh...
mesh.add_point(Geom::Point3d.new(0, 0, 0))
mesh.add_point(Geom::Point3d.new(1, 0, 0))
mesh.add_point(Geom::Point3d.new(1, 1, 0))
polygon_index = mesh.add_polygon([1, 2, 3])           
mesh = Geom::PolygonMesh.new
point1 = Geom::Point3d.new(0, 1, 2)
point2 = Geom::Point3d.new(1, 0, 2)
point3 = Geom::Point3d.new(2, 0, 1)
polygon_index = mesh.add_polygon(point1, point2, point3)           
mesh = Geom::PolygonMesh.new
point1 = Geom::Point3d.new(0, 1, 2)
point2 = Geom::Point3d.new(1, 0, 2)
point3 = Geom::Point3d.new(2, 0, 1)
polygon_index = mesh.add_polygon([point1, point2, point3])           

5.16.4 #count_points⇒ Integer

mesh = Geom::PolygonMesh.new
point = Geom::Point3d.new(0, 1, 2)
mesh.add_point(point)
num = mesh.count_points           

5.16.5 #count_polygons⇒ Integer

mesh = Geom::PolygonMesh.new
point1 = Geom::Point3d.new(0, 1, 2)
point2 = Geom::Point3d.new(1, 0, 2)
point3 = Geom::Point3d.new(2, 0, 1)
mesh.add_polygon(point1, point2, point3)
nump = mesh.count_polygons           

5.16.6 #normal_at(index) ⇒ Geom::Vector3d?返回索引的法线

5.16.7 #point_at(index) ⇒ Geom::Point3d?返回索引的点

mesh = Geom::PolygonMesh.new
point1 = Geom::Point3d.new(0, 1, 2)
point2 = Geom::Point3d.new(10, 20, 30)
mesh.add_point(point1)
mesh.add_point(point2)
point_from_index = mesh.point_at(1)           

5.16.8 #point_index(point) ⇒ Integer返回点的索引

mesh = Geom::PolygonMesh.new
point1 = Geom::Point3d.new(0, 1, 2)
point2 = Geom::Point3d.new(10, 20, 30)
mesh.add_point(point1)
mesh.add_point(point2)
index = mesh.point_index(point2)           

5.16.9 #points⇒ Array<Geom::Point3d>返回点的数组

mesh = Geom::PolygonMesh.new
point1 = Geom::Point3d.new(0, 1, 2)
point2 = Geom::Point3d.new(10, 20, 30)
mesh.add_point(point1)
mesh.add_point(point2)
# Returns array of points in the mesh.
points = mesh.points           

5.16.10 #polygon_at(index) ⇒ Array<Geom::Point3d>?

mesh = Geom::PolygonMesh.new
point1 = Geom::Point3d.new(0, 1, 2)
point2 = Geom::Point3d.new(1, 0, 2)
point3 = Geom::Point3d.new(2, 0, 1)
index = mesh.add_polygon(point1, point2, point3)
polygon = mesh.polygon_at(index)           

返回[1, 2, 3]

5.16.11 #polygon_points_at(index) ⇒ Array<Geom::Point3d>?

mesh = Geom::PolygonMesh.new
point1 = Geom::Point3d.new(0, 1, 2)
point2 = Geom::Point3d.new(1, 0, 2)
point3 = Geom::Point3d.new(2, 0, 1)
index = mesh.add_polygon(point1, point2, point3)
points = mesh.polygon_points_at(index)           

返回[Point3d(0, 1, 2), Point3d(1, 0, 2), Point3d(2, 0, 1)]

5.16.12 #polygons⇒ Array<Array<Integer>>

polygons = mesh.polygons [[1, 2, 3]]

5.16.13 #set_point(index, point) ⇒ Geom::PolygonMesh

mesh = Geom::PolygonMesh.new
point1 = Geom::Point3d.new(0, 1, 2)
point2 = Geom::Point3d.new(10, 20, 30)
index = mesh.add_point(point1)
mesh.set_point(index, point2)

#增加一行验证
mesh.point_at(1)
#返回(254 mm, 508 mm, 762 mm)           

5.16.14 #set_uv(index, point, front) ⇒ nil

略。。。

5.16.15 #transform!(transformation) ⇒ Geom::PolygonMesh

point1 = Geom::Point3d.new(100, 200, 300)
tr = Geom::Transformation.new(point1)
mesh = Geom::PolygonMesh.new
point2 = Geom::Point3d.new(0, 1, 2)
mesh.add_point(point2)
mesh.transform!(tr)

#增加验证
points = mesh.points
#返回[Point3d(100, 201, 302)]           

5.16.16 #uv_at(index, front) ⇒ Geom::Point3d?

5.16.17 #uvs(front) ⇒ Array<Geom::Point3d>

略。。。

5.17 Class: Geom::Transformation移动

5.17.1 #initialize⇒ Geom::Transformation

5.17.1 #initialize(point) ⇒ Geom::Transformation

5.17.1 #initialize(vector) ⇒ Geom::Transformation

5.17.1 #initialize(transform) ⇒ Geom::Transformation

5.17.1 #initialize(array) ⇒ Geom::Transformation

5.17.1 #initialize(scale) ⇒ Geom::Transformation

5.17.1 #initialize(origin, zaxis) ⇒ Geom::Transformation

5.17.1 #initialize(origin, xaxis, yaxis) ⇒ Geom::Transformation

5.17.1 #initialize(pt, axis, angle) ⇒ Geom::Transformation

5.17.1 #initialize(xaxis, yaxis, zaxis, origin) ⇒ Geom::Transformation

point = Geom::Point3d.new(10, 20, 30)
tr = Geom::Transformation.new(point)

#增加验证
tr.origin
#返回(254 mm, 508 mm, 762 mm)
           

5.17.2 .axes(origin, xaxis, yaxis, zaxis) ⇒ Geom::Transformation

5.17.2 .axes(origin, xaxis, yaxis) ⇒ Geom::Transformation

坐标系变换。(远点,向量x,向量y,向量z)

5.17.3 .interpolate(transform1, transform2, weight) ⇒ Geom::Transformation

origin = Geom::Point3d.new(0, 0, 0)
x = Geom::Vector3d.new(0, 1, 0)
y = Geom::Vector3d.new(1, 0, 0)
z = Geom::Vector3d.new(0, 0, 1)
point = Geom::Point3d.new(10, 20, 30)
t1 = Geom::Transformation.new(point)
t2 = Geom::Transformation.axes(origin, x, y, z)
# This produce a transformation that is a mix of 75% t1 and 25% t2.
t3 = Geom::Transformation.interpolate(t1, t2, 0.25)

t3.origin
#(190.5 mm, 381 mm, 571.5 mm)
t1.origin
#(254 mm, 508 mm, 762 mm)
t2.origin
#(0 mm, 0 mm, 0 mm)
           

t3 = t1*0.75+t2*0.25

5.17.4 .rotation(point, vector, angle) ⇒ Geom::Transformation

point = Geom::Point3d.new(10, 20, 0)
vector = Geom::Vector3d.new(0, 0, 1)
angle = 45.degrees # Return 45 degrees in radians.
transformation = Geom::Transformation.rotation(point, vector, angle)           

点、向量、转角。旋转,略。。。(还没搞清楚)

5.17.5 .scaling(scale) ⇒ Geom::Transformation

5.17.5 .scaling(xscale, yscale, zscale) ⇒ Geom::Transformation

5.17.5 .scaling(point, scale) ⇒ Geom::Transformation

5.17.5 .scaling(point, xscale, yscale, zscale) ⇒ Geom::Transformation

point = Geom::Point3d.new(20, 30, 0)
scale = 10
tr = Geom::Transformation.scaling(point, scale)

tr.origin
#(-4572 mm, -6858 mm, 0 mm)           

缩放

5.17.6 .translation(vector) ⇒ Geom::Transformation

5.17.6 .translation(point) ⇒ Geom::Transformation

vector = Geom::Vector3d.new(0, 1, 0)
tr = Geom::Transformation.translation(vector)

tr.origin
#(0 mm, 25.4 mm, 0 mm)           

移动

5.17.7 #*(point) ⇒ Geom::Point3d

5.17.7 #*(vector) ⇒ Geom::Vector3d

5.17.7 #*(transformation) ⇒ Geom::Transformation

5.17.7 #*(point) ⇒ Array(Float, Float, Float)

5.17.7 #*(plane) ⇒ Array(Float, Float, Float, Float)

5.17.7 #*(plane) ⇒ Array(Float, Float, Float, Float)

point1 = Geom::Point3d.new(10, 20, 30)
point2 = Geom::Point3d.new(2, 2, 2)
tr = Geom::Transformation.new(point1)
# Returns Point3d(12, 22, 32)
point3 = tr * point2
#(304.8 mm, 558.8 mm, 812.8 mm)           

5.17.8 #clone⇒ Geom::Transformation

point = Geom::Point3d.new(10, 20, 30)
tr1 = Geom::Transformation.new(point)
tr2 = tr1.clone           

5.17.9 #identity?⇒ Boolean

point = Geom::Point3d.new(10, 20, 30)
tr = Geom::Transformation.new(point)
# Returns false.
status = tr.identity?
tr = Geom::Transformation.new(ORIGIN)
# Returns false.
status = tr.identity?
tr = Geom::Transformation.new
# Returns true.
status = tr.identity?
# Returns true.
status = IDENTITY.identity?           

还没搞懂。。。

5.17.10 #inverse⇒ Geom::Transformation

point = Geom::Point3d.new(10, 20, 30)
tr1 = Geom::Transformation.new(point)
tr2 = tr1.inverse

tr1.origin
#(254 mm, 508 mm, 762 mm)
tr2.origin
#(-254 mm, -508 mm, -762 mm)           

5.17.11 #invert!⇒ Geom::Transformation

point = Geom::Point3d.new(10, 20, 30)
tr = Geom::Transformation.new(point)
tr.invert!

tr.origin
#(-254 mm, -508 mm, -762 mm)           

本身被赋值

5.17.12 #origin⇒ Geom::Point3d

point1 = Geom::Point3d.new(10, 20, 30)
tr = Geom::Transformation.new(point1)
point2 = tr.origin
#(254 mm, 508 mm, 762 mm)           

5.17.13 #set!(transformation) ⇒ Geom::Transformation

5.17.13 #set!(point) ⇒ Geom::Transformation

5.17.13 #set!(vector) ⇒ Geom::Transformation

5.17.13 #set!(matrix) ⇒ Geom::Transformation

5.17.13 #set!(scale) ⇒ Geom::Transformation

赋值

point1 = Geom::Point3d.new(10, 20, 30)
tr1 = Geom::Transformation.new(point1)
point2 = Geom::Point3d.new(60, 40, 70)
tr1.set!(point2)

tr1.origin
#(1524 mm, 1016 mm, 1778 mm)           

5.17.14 #to_a⇒ Array<Float>

5.17.15 #xaxis⇒ Geom::Vector3d

5.17.15 #yaxis⇒ Geom::Vector3d

5.17.15 #zaxis⇒ Geom::Vector3d

5.18 Class: Geom::Transformation2d

5.19 Class: Geom::UTM

utm,是通用横墨卡托格网系统的英文缩写

5.19.1 #initialize(zone_number, zone_letter, x, y) ⇒ Geom::UTM

5.19.1 #initialize(utm) ⇒ Geom::UTM

5.19.1 #initialize(array) ⇒ Geom::UTM

# Create a copy of an existing UTM object.
utm = Geom::UTM.new(utm2)
#以上代码会报错,因为utm2还不存在:)
# Create a new UTM object from scratch.
utm = Geom::UTM.new(13, "T", 475849.37521, 4429682.73749)
#返回UTM(13 T 475849.37521 4429682.73749)           

5.19.2 #to_a⇒ Array(Integer, String, Float, Float)

# Create a new UTM object from scratch.
utm = Geom::UTM.new(13, "T", 475849.37521, 4429682.73749)
a = utm.to_a
#返回[13, "T", 475849.37521, 4429682.73749]           

5.19.3 #to_latlong⇒ Geom::LatLong

# Create a new UTM object from scratch.
utm = Geom::UTM.new(13, "T", 475849.37521, 4429682.73749)
ll = utm.to_latlong
#返回LatLong(40.01700¥フラ, 105.28300│ᆬ﾿)           

5.19.4 #to_s⇒ String

# Create a new UTM object from scratch.
utm = Geom::UTM.new(13, "T", 475849.37521, 4429682.73749)
string = utm.to_s
#返回UTM(13 T 475849.37521 4429682.73749)           

5.19.5 #x⇒ Float

# Create a new UTM object from scratch.
utm = Geom::UTM.new(13, "T", 475849.37521, 4429682.73749)
x = utm.x           

5.19.6 #y⇒ Float

# Create a new UTM object from scratch.
utm = Geom::UTM.new(13, "T", 475849.37521, 4429682.73749)
y = utm.y           

5.19.7 #zone_letter⇒ String

# Create a new UTM object from scratch.
utm = Geom::UTM.new(13, "T", 475849.37521, 4429682.73749)
zl = utm.zone_letter           

5.19.8 #zone_number⇒ Integer

# Create a new UTM object from scratch.
utm = Geom::UTM.new(13, "T", 475849.37521, 4429682.73749)
zn = utm.zone_number           

5.20 Class: Geom::Vector2d

5.21 Class: Geom::Vector3d 三维向量

5.21.1 #initialize⇒ Geom::Vector3d

5.21.1 #initialize(x, y, z) ⇒ Geom::Vector3d

5.21.1 #initialize(array3d) ⇒ Geom::Vector3d

5.21.1 #initialize(array2d) ⇒ Geom::Vector3d

5.21.1 #initialize(vector) ⇒ Geom::Vector3d

# A vector that runs up the Z axis.
vector = Geom::Vector3d.new(0,0,1)
if (vector)
  UI.messagebox vector
else
  UI.messagebox "Failure"
end           

5.21.2 .linear_combination(weight1, vector1, weight2, vector2) ⇒ Geom::Vector3d

5.21.2 .linear_combination(x, xaxis, y, yaxis, z, zaxis) ⇒ Geom::Vector3d

# Create a vector that is a 50%/50% linear combination of two others.
vec1 = Geom::Vector3d.new 3,0,0
vec2 = Geom::Vector3d.new 0,3,0
new_vector = Geom::Vector3d.linear_combination(0.5, vec1, 0.5, vec2)
# new_vector will now contain a Vector3d(1.5, 1.5, 0)           

5.21.3 #%(vector) ⇒ Float

vector1 = Geom::Vector3d.new(0, 0, 1)
vector2 = Geom::Vector3d.new(0, 1, 0)
dot = vector1 % vector2
#0.0           

点积

5.21.4 #*(vector) ⇒ Geom::Vector3d

vector1 = Geom::Vector3d.new(1, 0, 0)
vector2 = Geom::Vector3d.new(0, 1, 0)
vector3 = vector1 * vector2
#(0, 0, 1)           
vector = Geom::Vector3d.new(1, 0, 0)
vector2 = Geom::Vector3d.new(0, 1, 0)
vector3 = vector.cross(vector2)
#(0, 0, 1)           

差积

5.21.5 #+(vector2) ⇒ Geom::Vector3d

vector = Geom::Vector3d.new(0,0,2)
vector2 = Geom::Vector3d.new(0,1,0)
new_vector = vector + vector2
#(0, 1, 2)           

5.21.6 #-(vector2) ⇒ Geom::Vector3d

vector = Geom::Vector3d.new(0,0,2)
vector2 = Geom::Vector3d.new(0,1,0)
new_vector = vector - vector2
#(0, -1, 2)           

5.21.7 #<(vector2) ⇒ Boolean

vector = Geom::Vector3d.new(0,0,2)
vector2 = Geom::Vector3d.new(0,1,0)
lt = vector < vector2
#true           

5.21.8 #==(vector2) ⇒ Boolean

vector = Geom::Vector3d.new(1,0,0)
vector2 = Geom::Vector3d.new(0,1,0)
status = vector == vector2
# Returns false
UI.messagebox status           

5.21.9 #[](i) ⇒ Length

vector = Geom::Vector3d.new(1,0,0)
value = vector[0]
if (value)
  UI.messagebox value
else
  UI.messagebox "Failure"
end           

5.21.10 #[]=(index, value) ⇒ Numeric

vector[i] = coordinate           

5.21.11 #angle_between(vector2) ⇒ Float

vector1 = Geom::Vector3d.new(1,0,0)
vector2 = Geom::Vector3d.new(0,1,0)
angle = vector1.angle_between vector2
#1.5707963267948966  (in radians)           

5.21.12 #axes⇒ Array(Geom::Vector3d, Geom::Vector3d, Geom::Vector3d)

轴方法用于计算以给定矢量为z轴方向的任意一组轴。

返回三个矢量的数组[xaxis,yaxis,zaxis]

vector = Geom::Vector3d.new(1,0,0)
a = vector.axes
#[Vector3d(0, 1, 0), Vector3d(0, 0, 1), Vector3d(1, 0, 0)]           

5.21.13 #clone⇒ Geom::Vector3d

5.21.14 #cross(vector) ⇒ Geom::Vector3d

差积

5.21.15 #dot(vector) ⇒ Float

点积

5.21.16 #inspect⇒ Geom::Vector3d

vector = Geom::Vector3d.new(0,0,1)
out_string = vector.inspect
puts out_string
#Vector3d(0, 0, 1)           

5.21.17 #length⇒ Length

vector = Geom::Vector3d.new(0,0,1)
l = vector.length           

5.21.18 #length=(length) ⇒ Numeric

vector = Geom::Vector3d.new(0,0,1)
l = vector.length    #~25 mm
UI.messagebox(l)
newl = vector.length = 2  # 2           

5.21.19 #normalize⇒ Geom::Vector3d

vector = Geom::Vector3d.new(0,0,2)
vector2 = vector.normalize #001           

归一化

5.21.20 #normalize!⇒ Geom::Vector3d

归一化,并赋值

5.21.21 #parallel?(vector2) ⇒ Boolean

平行判断

status = vector.parallel?(vector2)           

5.21.22 #perpendicular?(vector2) ⇒ Boolean

垂直判断

vector = Geom::Vector3d.new(0,0,1)
vector2 = Geom::Vector3d.new(0,1,0)
status = vector.perpendicular?(vector2)#true           

5.21.23 #reverse⇒ Geom::Vector3d

返回逆向量

vector2 = vector.reverse           

5.21.24 #reverse!⇒ Geom::Vector3d

返回逆向量,并赋值

5.21.25 #samedirection?(vector2) ⇒ Boolean

判断向量是否同向。

vector = Geom::Vector3d.new(0,0,1)
vector2 = Geom::Vector3d.new(0,1,0)
status = vector.samedirection?(vector2)#false           

5.21.26 #set!(array3d) ⇒ Geom::Vector3d

5.21.26 #set!(vector) ⇒ Geom::Vector3d

5.21.26 #set!(x, y, z) ⇒ Geom::Vector3d

5.21.27 #to_a⇒ Array(Length, Length, Length)

5.21.27 #to_s⇒ String

5.21.28 #transform(transform) ⇒ Geom::Vector3d

5.21.29 #transform!(transform) ⇒ Geom::Vector3d

5.21.30 #unitvector?⇒ Boolean

是否单位向量

5.21.31 #valid?⇒ Boolean

是否有效向量(非零向量)

5.21.32 #x⇒ Length

xyz

5.21.33 #x=(x) ⇒ Numeric

xyz

继续阅读