Các đối tượng trong SketchUp tập hợp thành các mảng, nên có thể duyệt qua chúng bằng các vòng lặp như for, each, while, do ...
Cũng có thể sử dụng các phương thức của mảng để tìm một đối tượng trong tập hợp như phương thức find, find_all ...
Ví dụ: tìm các mặt có phương pháp tuyển chỉ ra
# 16.findface.rb
# load "/Users/xuanthulab/Desktop/learn-ruby/sketchup/16.findface.rb"
require 'sketchup.rb'
require 'extensions.rb'
def findface(normal = [0,0,1])
# Tìm 1 Face đầu tiên có pháp tuyến normal
Sketchup.active_model.selection.clear # xóa selection
ents = Sketchup.active_model.entities
xface = ents.find {|ent| ent.typename == "Face" &&
ent.normal == normal}
puts "The face is: " + xface.to_s
Sketchup.active_model.selection.add xface # thêm kết quả tìm vào selection
end
def findface_all(normal = [0,0,1])
# Tìm tất cả các mặt có pháp tuyến normal
Sketchup.active_model.selection.clear
ents = Sketchup.active_model.entities
# Duyệt qua tất cả các Entity, tìm các Face trả về mảng
faces = ents.find_all {|ent| ent.typename == "Face" &&
ent.normal == normal}
puts "Founds: #{faces.length}"
# Duyệt qua tất cả các Face tìm được và in thông tin
faces.each {
|face|
puts face.to_s
}
Sketchup.active_model.selection.add faces
end
Kết quả tìm face có pháp tuyến [0, -1, 0]
Ví dụ thứ 2, duyệt qua tất cả các đường (Edge), qua đó tập hợp được tạo độ các đỉnh trong một mảng trả về
# 17.findvertex.rb
# load "/Users/xuanthulab/Desktop/learn-ruby/sketchup/17.findvertex.rb"
require 'sketchup.rb'
require 'extensions.rb'
def findvertex
ents = Sketchup.active_model.entities
# mảng chứa các đỉnh
vertex_array = []
ents.each {|ent|
if ent.typename == "Edge"
# Hợp hai mảng duy nhất
vertex_array = vertex_array | ent.vertices
end
}
return vertex_array
end
vertex_array = findvertex
puts vertex_array
vertex_array.each {|pt| puts "Các điểm: " + pt.position.to_s}
