美文网首页SketchupSketchUp自动化
SketchUp的二次开发探索 (四)自动化建筑遐想

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

作者: nicaicaiwo | 来源:发表于2019-11-13 21:05 被阅读0次

    我有一个梦想

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

    image

    虽然,上面只是我做的一个概念图,但是,整天埋头画图的建筑师是否已经忽略了身边事物的发展?AI设计其实已经在我们身边,它如同BIM,VR设计一样席卷而来。可能踉踉跄跄,但一定会来。

    计算机替代设计师已经不可避免,特别是对大多数做重复劳动的绘图员而言,更是如此。学会编程将和使用计算机一样变为大势所趋。

    尽管不是编程专业,但已经有很多人在自学Ruby、Python等脚本语言,工作和生活上,用来爬虫、找方案爬资料,甚至在做设计,协同软件,非常方便。

    SketchUp提供了丰富的Ruby API,让我们不限于现有的工具和插件来做设计。更能直接通过API操控我们的模型。

    如果把建筑的生长封装成一个方法,接受各种场地/时间/天气/阳光等参数,那这样生长出来的建筑会是什么样的呢?


    自动化建筑道阻且长,

    需要各位建筑师的共同努力!!!

    今天我们通过实现一个动态时钟,

    来学习一些自动化操作模型的基础知识。

    ​动态时钟▼

    细节展示 动态时钟准确匹配当前时间

    思路分析

    1、绘制表盘和表针

    2、操控秒/分/时针旋转关系

    3、根据当前时间初始化指针角度

    ​1、绘制表盘和表针▼ ​

    绘制表盘和时钟

    对应代码:(由于顶点参数太占位置简化了表达式)

    
    # 初始化模型实例
    ent = Sketchup.active_model.entities
    ​
    # 表盘/时针/分针/秒针组件
    group_circle = ent.add_group
    group_hour = ent.add_group
    group_minute = ent.add_group
    group_second = ent.add_group
    ​
    # 表盘
    circle = group_circle.entities.add_circle [0,0.1,2.2],[0,1,0],2.2
    circle_face = group_circle.entities.add_face circle
    ​
    # 时针/分针/秒针 P1 P2 P3 P4 代表指针面的4个顶点坐标
    face_hour = group_hour.entities.add_face Ph1,Ph2,Ph3,Ph4
    face_minute = group_minute.entities.add_face Pm1,Pm2,Pm3,Pm4
    face_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.1
    face_hour.pushpull 0.015
    face_minute.pushpull 0.015
    face_second.pushpull 0.01
    
    

    ​2、操控秒/分/时针旋转关系

    首先我们需要知道怎么操控一个组件旋转,

    查看官方文档可以看到:

    
    .rotation(point, vector, angle) ⇒ Geom::Transformation
    point = 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::Transformation.rotation(point, vector, angle)即可创建一个旋转动效,

    point旋转中心点

    vector旋转平面向量

    angle旋转角度

    我们先来试试操控一条线旋转30度▼

    image

    对应代码:

    
    mod = Sketchup.active_model # Open model
    ent = mod.entities # All entities in model
    sel = mod.selection # Current selection
    ​
    # 旋转30度
    tr = Geom::Transformation.new [0,0,0],[0,1,0],30.degrees
    ent.transform_entities tr, sel
    
    

    那么怎样才能让组件1秒之后旋转30度呢?

    这时候我们需要用到延时动画,

    我们找一下文档:

    
    # Beep once after 10 seconds.
    id = UI.start_timer(10, false) { UI.beep }
    
    

    把我们的旋转动画放到3秒的延时动画里面:

    image

    对应代码:

    
    mod = Sketchup.active_model # Open model
    ent = mod.entities # All entities in model
    sel = 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
    
    

    再来分析一下时针/分针/秒针的旋转关系:

    • 秒针1秒旋转6度

    • 分针1 60秒旋转6度

    • 时针1 60 60 秒旋转30度

    
    # 设置秒针/分针和时针的一次旋转角度
    tr_ms = Geom::Transformation.new [0,0,2.2],[0,1,0],6.degrees
    tr_h = Geom::Transformation.new [0,0,2.2],[0,1,0],30.degrees
    ​
    # 设置1小时的持续时间
    (0..60*60*1).each do |s|
    # 9:10
    UI.start_timer(s, false) {
    group_second.entities.transform_entities tr_ms, group_second
    puts "sec:"+s.to_s
    }
    UI.start_timer(s*60, false) {
    group_minute.entities.transform_entities tr_ms, group_minute
    puts "min"+s.to_s
    }
    UI.start_timer(s*60*60, false) {
    group_hour.entities.transform_entities tr_h, group_hour
    puts "hour"+s.to_s
    }
    end
    
    

    如果想要指针更圆滑的旋转,

    可以拆分一下角度,

    比如每0.1秒旋转0.6度。

    ​3、根据当前时间初始化指针角度

    最后我们来处理一下指针初始角度,对应上当前的时间。

    在Ruby中可用Time.new来获取当前时间,

    然后计算出指针到12点的夹角度数。

    
    # 获取当前时间转换成指针的初始角度
    time = Time.new
    tr_hour = Geom::Transformation.new [0,0,0],[0,1,0],time.hour*30.degrees
    tr_min = Geom::Transformation.new [0,0,0],[0,1,0],time.min*6.degrees
    tr_sec = Geom::Transformation.new [0,0,0],[0,1,0],time.sec*6.degrees
    ​
    # 旋转相应角度
    group_hour.entities.transform_entities tr_hour, group_hour
    group_minute.entities.transform_entities tr_min, group_minute
    group_second.entities.transform_entities tr_sec, group_second
    
    

    至此我们就实现了我们的目标效果。

    最后我把这个时钟做成了一个插件,

    动态时钟插件 交互窗口

    扫描下方二维码,

    关注公众号Nicaicaiwo,

    回复 时钟,

    即可免费获取

    关注公众号获取更多有趣脚本

    相关文章

      网友评论

        本文标题:SketchUp的二次开发探索 (四)自动化建筑遐想

        本文链接:https://www.haomeiwen.com/subject/ssbkictx.html