四 SketchUp的二次开发探索 自动化建筑遐想

我有一个梦想
如果能在地基内种建筑,那是多么美妙的一件事儿,晒晒太阳吹吹风 , 方案就生成了~ ▼

四  SketchUp的二次开发探索 自动化建筑遐想

文章插图
虽然,上面只是我做的一个概念图 , 但是,整天埋头画图的建筑师是否已经忽略了身边事物的发展?AI设计其实已经在我们身边,它如同BIM,VR设计一样席卷而来 。可能踉踉跄跄,但一定会来 。
计算机替代设计师已经不可避免,特别是对大多数做重复劳动的绘图员而言,更是如此 。学会编程将和使用计算机一样变为大势所趋 。
尽管不是编程专业,但已经有很多人在自学Ruby、等脚本语言 , 工作和生活上 , 用来爬虫、找方案爬资料,甚至在做设计 , 协同软件 , 非常方便 。
提供了丰富的Ruby API,让我们不限于现有的工具和插件来做设计 。更能直接通过API操控我们的模型 。
如果把建筑的生长封装成一个方法,接受各种场地/时间/天气/阳光等参数,那这样生长出来的建筑会是什么样的呢?
自动化建筑道阻且长,
需要各位建筑师的共同努力?。。?
今天我们通过实现一个动态时钟,
来学习一些自动化操作模型的基础知识 。
?动态时钟▼
四  SketchUp的二次开发探索 自动化建筑遐想

文章插图

四  SketchUp的二次开发探索 自动化建筑遐想

文章插图
思路分析
1、绘制表盘和表针
2、操控秒/分/时针旋转关系
3、根据当前时间初始化指针角度
?1、绘制表盘和表针▼ ?
四  SketchUp的二次开发探索 自动化建筑遐想

文章插图
对应代码:(由于顶点参数太占位置简化了表达式)
# 初始化模型实例ent = Sketchup.active_model.entities?# 表盘/时针/分针/秒针组件group_circle = ent.add_groupgroup_hour = ent.add_groupgroup_minute = ent.add_groupgroup_second = ent.add_group?# 表盘circle = group_circle.entities.add_circle [0,0.1,2.2],[0,1,0],2.2circle_face = group_circle.entities.add_face circle?# 时针/分针/秒针 P1 P2 P3 P4 代表指针面的4个顶点坐标face_hour = group_hour.entities.add_face Ph1,Ph2,Ph3,Ph4face_minute = group_minute.entities.add_face Pm1,Pm2,Pm3,Pm4face_second = group_second.entities.add_face Ps1,Ps2,Ps3,Ps4?# 时针/分针/秒针填色face_hour.material = "black"face_minute.material = "black"face_second.material = "red"?# 表盘/时针/分针/秒针推拉出高度circle_face.pushpull -0.1face_hour.pushpull 0.015face_minute.pushpull 0.015face_second.pushpull 0.01
?2、操控秒/分/时针旋转关系
首先我们需要知道怎么操控一个组件旋转,
查看官方文档可以看到:
.rotation(point, vector, angle) ? Geom::Transformationpoint = Geom::Point3d.new(10, 20, 0)vector = Geom::Vector3d.new(0, 0, 1)angle = 45.degrees # Return 45 degrees in radians.transformation = Geom::Transformation.rotation(point, vector, angle)
通过Geom::.(point, , angle)即可创建一个旋转动效,
point:旋转中心点
:旋转平面向量
angle :旋转角度
我们先来试试操控一条线旋转30度▼
四  SketchUp的二次开发探索 自动化建筑遐想

文章插图
对应代码:
mod = Sketchup.active_model # Open modelent = mod.entities # All entities in modelsel = mod.selection # Current selection?# 旋转30度tr = Geom::Transformation.new [0,0,0],[0,1,0],30.degreesent.transform_entities tr, sel
那么怎样才能让组件1秒之后旋转30度呢?
这时候我们需要用到延时动画,
我们找一下文档:
四  SketchUp的二次开发探索 自动化建筑遐想

文章插图
# Beep once after 10 seconds.id = UI.start_timer(10, false) { UI.beep }
把我们的旋转动画放到3秒的延时动画里面:
四  SketchUp的二次开发探索 自动化建筑遐想

文章插图
对应代码:
mod = Sketchup.active_model # Open modelent = mod.entities # All entities in modelsel = mod.selection # Current selection# 旋转30度tr = Geom::Transformation.new [0,0,0],[0,1,0],30.degrees# 10s后开始(0..20).each do |i|UI.start_timer(i+10,false){ent.transform_entities tr, sel}end
再来分析一下时针/分针/秒针的旋转关系:
# 设置秒针/分针和时针的一次旋转角度tr_ms = Geom::Transformation.new [0,0,2.2],[0,1,0],6.degreestr_h = Geom::Transformation.new [0,0,2.2],[0,1,0],30.degrees?# 设置1小时的持续时间(0..60*60*1).each do |s|# 9:10UI.start_timer(s, false) {group_second.entities.transform_entities tr_ms, group_secondputs "sec:"+s.to_s}UI.start_timer(s*60, false) {group_minute.entities.transform_entities tr_ms, group_minuteputs "min"+s.to_s}UI.start_timer(s*60*60, false) {group_hour.entities.transform_entities tr_h, group_hourputs "hour"+s.to_s}end
如果想要指针更圆滑的旋转,
可以拆分一下角度 , 
比如每0.1秒旋转0.6度 。
?3、根据当前时间初始化指针角度
最后我们来处理一下指针初始角度,对应上当前的时间 。
在Ruby中可用Time.new来获取当前时间,
然后计算出指针到12点的夹角度数 。
# 获取当前时间转换成指针的初始角度time = Time.newtr_hour = Geom::Transformation.new [0,0,0],[0,1,0],time.hour*30.degreestr_min = Geom::Transformation.new [0,0,0],[0,1,0],time.min*6.degreestr_sec = Geom::Transformation.new [0,0,0],[0,1,0],time.sec*6.degrees?# 旋转相应角度group_hour.entities.transform_entities tr_hour, group_hourgroup_minute.entities.transform_entities tr_min, group_minutegroup_second.entities.transform_entities tr_sec, group_second
至此我们就实现了我们的目标效果 。
最后我把这个时钟做成了一个插件,
四  SketchUp的二次开发探索 自动化建筑遐想

文章插图
【四SketchUp的二次开发探索 自动化建筑遐想】扫描下方二维码,