#概要
sketchupでrubyやってみた。
練習問題やってみた。
#練習問題
木を作れ。
#写真
#サンプルコード
class TreeStem
def entity
@entity
end
def initialize
model = Sketchup.active_model
entities = Sketchup.active_model.active_entities
self.prompt
conifer = @data[0].to_i
treeheight = @data[1].to_i.mm
rootradius = (@data[2].to_f / 2).mm
growsections = @data[3].to_i
branchings = @data[4].to_i
anbranch = @data[5].to_f
reducgrow = @data[6].to_f
fluctvalue = @data[7].to_f
minradius = 5.mm
reducrate = (minradius / rootradius) ** (1.0 / growsections)
branchings += 1 if conifer == 1
if (conifer == 1 and anbranch < 0.7)
anbranch = 0.7
end
np = 0
(0..growsections).each { |i|
np += reducgrow ** i
}
rheight = treeheight / np
Sketchup.active_model.start_operation self.class.name
defstem = model.definitions.add "dstem"
entstem = defstem.entities
defbr = model.definitions.add "dbranch"
entbr = defbr.entities
porigin = Geom::Point3d.new(0, 0, 0)
pnbranch = Geom::Point3d.new(0, 0, rheight)
vroot = Geom::Vector3d.new(0, 0, rheight)
ec = entbr.add_circle porigin, vroot.normalize, rootradius, 5
ef = entbr.add_face ec
ef.pushpull -rheight
entstem.add_instance defbr, (Geom::Transformation.new porigin)
brps = []
brps << [porigin, vroot, pnbranch]
(1..growsections).each do |g|
escale = reducrate ** g
brpn = []
brps.each do |brset|
polder, volder, prefer = brset
anspread = 2.0 * Math::PI / branchings
anspread = 2.0 * Math::PI / (branchings - 1) if conifer == 1
(1..branchings).each do |b|
vb = volder.clone
vb.length = vb.length * (1 - 0.95 ** (b - 1))
porigin = prefer + vb.reverse
afluct = 1.0 + (rand(fluctvalue * 2) - fluctvalue) / 100.0
lfluct = 1.0 - (rand(fluctvalue) / 100.0)
trspread = Geom::Transformation.rotation porigin, volder, (anspread * b) * afluct
vn_x = Geom::Vector3d.new(1, 0, 0).transform trspread
vn_ypre = Geom::Vector3d.new(0, 1, 0).transform trspread
if (conifer == 1 and b == 1)
trtilt = Geom::Transformation.rotation porigin, vn_x, 0.02
else
trtilt = Geom::Transformation.rotation porigin, vn_x, anbranch * afluct
end
vnbranch = volder.transform trtilt
vn_y = vn_ypre.transform trtilt
if (conifer == 1 and b > 1)
lnb = (reducgrow - 0.1) * lfluct
else
lnb = (reducgrow + 0.1) * lfluct
end
vnbranch.length = volder.length * lnb
pnbranch = porigin + vnbranch
tra = Geom::Transformation.axes porigin, vn_x.normalize, vn_y.normalize, vnbranch.normalize
trs = Geom::Transformation.scaling escale, escale, vnbranch.length / vroot.length
trn = tra * trs
entstem.add_instance defbr, trn
brpn << [porigin, vnbranch, pnbranch]
end
end
brps = brpn.clone
brpn = []
end
@entity = defstem
Sketchup.active_model.commit_operation
end
def prompt
prompts = ["樹種(0/1)", "樹高(mm)", "目通径(mm)", "樹節数", "枝分数", "枝張角(rad)", "生長率", "揺らぎ(%)"]
values = [0, 5000, 150, 5, 4, 0.7, 0.6, 15]
enums = ["0|1", nil, nil, "4|5|6", "3|4|5", "0.5|0.6|0.7|0.8|0.9|1.0", "0.5|0.6|0.7|0.8", nil]
@data = inputbox prompts, values, enums, "tree"
end
end
def test3
treestem = TreeStem.new
definition = treestem.entity
Sketchup.active_model.place_component definition, true
end
以上。