本系列指南使用的是 SketchUp2018 最新版本,基于 ruby语言进行插件开发。(文章来源:微信公众号:Design-AI-Lab)
环境准备
- 先下载一个扩展,用于方便调试代码:
- https://github.com/Aerilius/sketchup-console-plus
- 打开 Extension Manager
- 选择 Install Extension
- 安装成功后,打开,界面如下:
- 这样,我们可以输入多行代码,并且代码也有了提示功能了~
SketchUp模型
通过 Sketchup.active_model 获取当前的 SketchUp 模型,它充当大多数 Ruby API 交互的“入口”。通过 model 下的各种方法,进行相关的操作,详情可以参看API 文档:
http://ruby.sketchup.com/Sketchup/Model.html
其中, UI.messagebox 是 SketchUp 里的提示框,类似于web 开发的 alert 。
model = Sketchup.active_model entities = model.entities layers = model.layers materials = model.materials component_definitions = model.definitions selection = model.selection first_entity = entities[0] UI.messagebox(“First thing in your model is a #{first_entity.typename}”) number_materials = materials.length UI.messagebox(“Your model has #{number_materials} materials.”)
添加点
通过 Geom::Point3d.new 构造一个空间点,再通过 add_cpoint 添加进 model 的entities 里。
model = Sketchup.active_model entities = model.active_entities
point1 = Geom::Point3d.new(100,200,300)
constpoint = entities.add_cpoint point1
if (constpoint) UI.messagebox constpoint
else UI.messagebox “Failure”
end
- ps:
构造点也可以通过 point1 = [100,200,350]
试一下生成大批量的随机点吧!
model = Sketchup.active_model entities = model.active_entities
for i in 0..1000 r1=rand(0)>0.5?1:(-1) r2=rand(0)>0.5?1:(-1) x=i*rand(100)*r1*r2 y=i*rand(100)*r1*r2 z=i*rand(200)*r1*r2 puts “x:#{x},y:#{y},z:#{z}” point = [x,y,z] constpoint = entities.add_cpoint point
end
效果如下图
添加直线
通过 model.entities 来添加直线,SketchUp 叫 edges 。先使用 entities.clear!清空下模型。然后,试下一下代码:
point1 = [0,0,0] point2 = [100,100,100] entities.add_edges(point1, point2)
改造下上文随机生成空间点的代码:
model = Sketchup.active_model entities = model.active_entities points=[]
for i in 0..1000 r1=rand(0)>0.5?1:(-1) r2=rand(0)>0.5?1:(-1) x=i*rand(100)*r1*r2 y=i*rand(100)*r1*r2 z=i*rand(200)*r1*r2 #puts “x:#{x},y:#{y},z:#{z}” point = [x,y,z] points.push(point) entities.add_edges(points[points.length-2], point) #constpoint = entities.add_cpoint point
end
- ps:
ruby 的代码注释,单行使用的是 # 号
一团乱麻~哈哈,我们再改造下代码。
model = Sketchup.active_model entities = model.active_entities points=[[0,0,0]]
for i in 0..500 p=points[points.length-1] r1=rand(0)>0.5?1:(-1) r2=rand(0)>0.5?1:(-1) x=p[0]+i*rand(1000)*r1*r2 y=p[1]+i*rand(1000)*r1*r2 z=p[2]+i*rand(2000)*r1*r2 #puts “x:#{x},y:#{y},z:#{z}” point = [x,y,z] points.push(point) entities.add_edges(points[points.length-2], point) #constpoint = entities.add_cpoint point
end
空间随机生长的一个折线:
空间折线构筑物
SketchUp 通过 entities.add_face 添加面。先使用 entities.clear! 清空下模型。然后,试下一下代码:
model = Sketchup.active_model entities = model.active_entities points=[[0,0,0]]
for i in 0..500 p=points[points.length-1] r1=rand(0)>0.5?1:(-1) r2=rand(0)>0.5?1:(-1) x=p[0]+i*rand(200) y=p[1]+i*rand(20)*r1*r2 z=i*rand(100) #puts “x:#{x},y:#{y},z:#{z}” point = [x,y,z] entities.add_edges(p, point) points.push(point)
#constpoint = entities.add_cpoint point
if points.length>3 p1 = points[points.length-3] p2 = points[points.length-2] p3 = points[points.length-1] entities.add_face(p1, p2, p3, p1) endend
多生成几个就可以达到下图效果:
评论0