Geom模塊
Geom模塊是我們?cè)谧鯯ketchUp二次開(kāi)發(fā)中最常用的模塊之一,該模塊對(duì)點(diǎn)和線處理,相關(guān)判斷封裝,重點(diǎn)在于對(duì)數(shù)據(jù)的判斷,方便開(kāi)發(fā)者完成自己的業(yè)務(wù)邏輯。
Sketchup 的類方法,用于激活當(dāng)前模型,model是返回當(dāng)前多個(gè)或單個(gè)模型
model = Sketchup.active_model
active_entities model調(diào)用 active_entities 旨在返回一組實(shí)體對(duì)象,個(gè)人理解為返回了當(dāng)前Sketchup打開(kāi)的這個(gè)場(chǎng)景中包含的點(diǎn)、線、向量等信息,是一個(gè)數(shù)組,當(dāng)我們要用ruby畫(huà)線時(shí),會(huì)往這個(gè)數(shù)組中添加元素
entities = model.active_entities
經(jīng)緯度對(duì)象 Geom::LatLong 創(chuàng)建和操作緯度和經(jīng)度坐標(biāo)的各種方法
latlongData = [40.01700, 105.28300]
latlong = Geom::LatLong.new(latlongData)
puts " 經(jīng)緯度坐標(biāo) #{latlong}"
3D 點(diǎn)對(duì)象 Geom::Point3d
# 定義個(gè)點(diǎn)
# 默認(rèn)單位 英寸
mypoint = Geom::Point3d.new(2,2,2)
# 獲取 X、Y、Z
puts mypoint.x
puts mypoint.y
puts mypoint.z
# 點(diǎn)到射線的距離
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}"
# 點(diǎn)偏移
point1 = Geom::Point3d.new(10.mm,10.mm,10.mm)
vector = Geom::Vector3d.new(0,0,10.mm)
# 點(diǎn) 1 向 Z軸方向偏移 10mm
point2 = point1.offset! vector
puts "偏移后的點(diǎn) #{point2}"
# 一個(gè)點(diǎn)是否在線上
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 " 點(diǎn)是否在一條直線上 #{status}"
# 同理 判斷一個(gè)點(diǎn)是否在一個(gè)平面上
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 " 點(diǎn)是否在一個(gè)平面上 #{status}"
# 檢測(cè)兩個(gè)點(diǎn)之間的向量
point2 = Geom::Point3d.new 100,200,300
point1 = Geom::Point3d.new 10,10,10
vector = point1.vector_to point2
puts vector
# 創(chuàng)建一個(gè)點(diǎn),作為兩個(gè)點(diǎn)的線性組合,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
# + 、- 運(yùn)算符
tempPoint = Geom::Point3d.new(10,10,10)
tempPoint2 = tempPoint - [5,5,5]
tempPoint3 = tempPoint + [1,1,-1]
tempPoint4 = tempPoint3 - [1,1,-1]
puts "運(yùn)算符"
puts tempPoint
puts tempPoint2
puts tempPoint3
puts tempPoint4
邊框盒子對(duì)象 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)
# 新建一個(gè)邊框,也可以定義一個(gè)空邊框,但一個(gè)具備意義的邊框應(yīng)該不少于兩個(gè)點(diǎn)
boundingbox = Geom::BoundingBox.new
boundingbox.add(point1,point2,point3)
# 判斷這個(gè)邊框是否為空
isEmpty = boundingbox.empty?
puts isEmpty
# 獲取這個(gè)邊框的中心點(diǎn)
boxCenter = boundingbox.center
puts boxCenter
# 邊框是否包含點(diǎn),包括了邊緣上的點(diǎn)
isContains = boundingbox.contains?([10, 5, 0])
puts isContains
# 檢查指定點(diǎn)對(duì)象,就是說(shuō)輸出相對(duì)于這個(gè)邊框某個(gè)位置的點(diǎn)
# - 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
# 邊框?qū)挾?length = boundingbox.width
puts length
# 獲取當(dāng)前邊框?qū)蔷€長(zhǎng)度
length = boundingbox.diagonal
puts length
# 判斷兩個(gè)邊框是否相交 相交則返回 boundingbox2 不相交返回 空
boundingbox2 = Geom::BoundingBox.new
boundingbox2.add([5, 5, 0], [10, 10, 0])
boundingbox3 = boundingbox.intersect(boundingbox2)
# 邊框 數(shù)值最大的點(diǎn) 和 數(shù)值最小的點(diǎn)
puts boundingbox3.min
puts boundingbox3.max
運(yùn)行結(jié)果
運(yùn)行結(jié)果.png