SketchUp 开发之 Geom模块(上)

Geom模块

Geom模块是我们在做SketchUp二次开发中最常用的模块之一,该模块对点和线处理,相关判断封装,重点在于对数据的判断,方便开发者完成自己的业务逻辑。

Sketchup 的类方法,用于激活当前模型,model是返回当前多个或单个模型

model = Sketchup.active_model

active_entities model调用 active_entities 旨在返回一组实体对象,个人理解为返回了当前Sketchup打开的这个场景中包含的点、线、向量等信息,是一个数组,当我们要用ruby画线时,会往这个数组中添加元素

entities = model.active_entities

经纬度对象 Geom::LatLong 创建和操作纬度和经度坐标的各种方法

latlongData = [40.01700, 105.28300]
latlong = Geom::LatLong.new(latlongData)
puts " 经纬度坐标 #{latlong}"

3D 点对象 Geom::Point3d

# 定义个点
# 默认单位 英寸
mypoint = Geom::Point3d.new(2,2,2)
# 获取 X、Y、Z
puts mypoint.x
puts mypoint.y
puts mypoint.z
# 点到射线的距离
point1 = Geom::Point3d.new 1,1,1
line = [Geom::Point3d.new(0,0,0), Geom::Vector3d.new(0,0,1)]
distance = point1.distance_to_line line
puts " 距离 #{distance}"
# 点偏移 
point1 = Geom::Point3d.new(10.mm,10.mm,10.mm)
vector = Geom::Vector3d.new(0,0,10.mm)
# 点 1 向 Z轴方向偏移 10mm
point2 = point1.offset! vector
puts "偏移后的点 #{point2}"
# 一个点是否在线上
line = [Geom::Point3d.new(0,0,0), Geom::Vector3d.new(0,0,1)]
point = Geom::Point3d.new 10,10,10
status = point.on_line? line
puts " 点是否在一条直线上 #{status}"
# 同理 判断一个点是否在一个平面上
plane = [Geom::Point3d.new(0,0,0), Geom::Vector3d.new(0,0,1)]
point = Geom::Point3d.new 10,10,10
status = point.on_plane? plane
puts " 点是否在一个平面上 #{status}"
# 检测两个点之间的向量
point2 = Geom::Point3d.new 100,200,300
point1 = Geom::Point3d.new 10,10,10
vector = point1.vector_to point2
puts vector
# 创建一个点,作为两个点的线性组合,point = weight1 * point1 + weight2 * point2
point1 = Geom::Point3d.new(0,0,0)
point2 = Geom::Point3d.new(100.mm,0,0)
point = Geom::Point3d.linear_combination(0.25, point1, 0.75, point2)
puts point
# + 、- 运算符
tempPoint = Geom::Point3d.new(10,10,10)
tempPoint2 = tempPoint - [5,5,5]
tempPoint3 = tempPoint + [1,1,-1]
tempPoint4 = tempPoint3 - [1,1,-1]
puts "运算符"
puts tempPoint
puts tempPoint2
puts tempPoint3
puts tempPoint4

边框盒子对象 Geom :: BoundingBox

puts "BoundingBox"
point1 = Geom::Point3d.new(10,0,0)
point2 = Geom::Point3d.new(5,5,0)
point3 = Geom::Point3d.new(0,0,0)

# 新建一个边框,也可以定义一个空边框,但一个具备意义的边框应该不少于两个点
boundingbox = Geom::BoundingBox.new
boundingbox.add(point1,point2,point3)
# 判断这个边框是否为空
isEmpty = boundingbox.empty?
puts isEmpty

# 获取这个边框的中心点
boxCenter = boundingbox.center
puts boxCenter

# 边框是否包含点,包括了边缘上的点
isContains = boundingbox.contains?([10, 5, 0])
puts isContains
# 检查指定点对象,就是说输出相对于这个边框某个位置的点
# - 0 = [0, 0, 0] (left front bottom)
# - 1 = [1, 0, 0] (right front bottom)
# - 2 = [0, 1, 0] (left back bottom)
# - 3 = [1, 1, 0] (right back bottom)
# - 4 = [0, 0, 1] (left front top)
# - 5 = [1, 0, 1] (right front top)
# - 6 = [0, 1, 1] (left back top)
# - 7 = [1, 1, 1] (right back top)
cpoint = boundingbox.corner(6)
puts cpoint
# 边框深度
length = boundingbox.depth
puts length

# 边框高度
length = boundingbox.height
puts length

# 边框宽度
length = boundingbox.width
puts length

# 获取当前边框对角线长度
length = boundingbox.diagonal
puts length
# 判断两个边框是否相交  相交则返回 boundingbox2 不相交返回 空
boundingbox2 = Geom::BoundingBox.new
boundingbox2.add([5, 5, 0], [10, 10, 0])
boundingbox3 = boundingbox.intersect(boundingbox2)
# 边框 数值最大的点 和 数值最小的点
puts boundingbox3.min
puts boundingbox3.max

运行结果

SketchUp 开发之 Geom模块(上)_第1张图片
运行结果.png

你可能感兴趣的:(SketchUp 开发之 Geom模块(上))