­

如何在曲面上布置各种体块?建筑师编程指南之SketchUp插件开发 3

  • 2019 年 11 月 12 日
  • 笔记

今天整理文章,发现《建筑师编程指南》这个系列,还没更完,有好几篇都躺在草稿箱里没发,发现网上关于sketchup的编程资料太少,不继续更好像不太好?

关于这个系列,不熟悉的可以翻阅前2期。

01期 建筑师编程指南之SketchUp插件开发

02期 ruby 语言的特点,以及阅读 SketchUp API 文档的技巧

第三篇,来点复杂,用代码实现一个案例:

犹太人大屠杀纪念碑群

第二次世界大战中,600万犹太人在种族清洗中被屠杀。

这个作品我非常喜欢,今天我们用代码来实现此类雕塑群的生成设计,首先我们需要提炼影响设计的变量有哪些,假设 M 为设计结果,

M = ( Grid , Heights )

Grid 为每一个纪念碑的截面,假设每个截面都是相等的大小,间距也相等, Heights 为高度数据集。

实现逻辑

01

根据区域范围,及方块大小,生成网格

在这里区域范围,我们粗暴点认为是矩形区域,通过 _size 来设置, _grid 则上文的 Grid ,_originPoint 为原点。

只要把上图中的黑点坐标计算出来,就可以完成我们这一步的任务。

这里我们需要考虑下,希望通过 createGrid 函数获得怎么样的输出结果?

写成代码:

_originPoint=[0,0,0]    _size={    "width"=>36000,    "height"=>36000    }    _grid={    "width"=>300,    "height"=>800    }    pointsResult=createGrid(_originPoint,_size,_grid)

createGrid 函数的编写:

def createGrid(_originPoint,_size,_grid)        #间距固定,设定为常量。      _px=600      _py=500        _w=_size["width"]      _h=_size["height"]      _z=_originPoint[2]        _x=_originPoint[0]      _y=_originPoint[1]        _gw=_grid["width"]      _gh=_grid["height"]        _xNum=(_w/(_gw+_px)).floor.to_f      _yNum=(_h/(_gh+_py)).floor.to_f        _points={         "row"=> 0       }        for i in 1..(_xNum-1)           _pointX1=_x+(_gw+_px)*(i-1)         _pointX2=_x+(_gw+_px)*i-_px           #p [_pointX1,_pointX2]         _gpoints=[]           for j in 1..(_yNum-1)           _pointY1=_y+(_gh+_py)*(j-1)           _pointY2=_y+(_gh+_py)*j-_py             _point1=[_pointX1,_pointY1,_z]           _point2=[_pointX1,_pointY2,_z]           _point3=[_pointX2,_pointY2,_z]           _point4=[_pointX2,_pointY1,_z]             _gpoints.push([_point1,_point2,_point3,_point4])             if j==1 && i==1              #调试用的              #p [_point1,_point2,_point3,_point4]           end           end           _points[_points["row"]]=_gpoints         _points["row"]=_points["row"]+1          end        return _points    end

02

计算高度

高度我们先选 sin 函数来生成,通过峰值数量 _peakValue ,及生成数值的间距 _parts 来控制。

createHeights 方法如下:

def createHeights(_peakValue,_parts)      _res=[]    _np=(_peakValue+2)*Math::PI/_parts      for x in 1.._parts        y=Math.sin(x*_np)      _res.push(y)      end      #p _res      return _res    end

通过以下代码调用:

_peakValue=3  _parts=pointsResult["row"]  _zsOfRow=createHeights(_peakValue,_parts)

这部分光 p 出来看数据可能比较抽象,我们加一段在 SketchUp 里绘制点的命令,把数据画出来。

model = Sketchup.active_model  entities = model.active_entities    for x in 0..(_zsbyRow.length-1)     entities.add_cpoint [x,_zsOfRow[x],0]  end

是不是调试起来方便多了?也可以把段封装成方法,专用来调试使用。

上面只计算了一个方向的,另一个方向我们也计算下:

_peakValueOfColumn=1  _partsOfColumn=pointsResult[0].length    _zsOfColumn=createHeights(_peakValueOfColumn,_partsOfColumn)

这个时候,我们调试的代码可以把 x , y 坐标对调下:

 for x in 0..(_zsOfColumn.length-1)       entities.add_cpoint [_zsOfColumn[x],x,0]   end

这样的话,调试起来更方便了,可以快速的把 x , y 两个轴的高度变化显示出来。

接下来,根据高度拉伸网格中的方块,此部分交给大家自行实现啦~

以上是去年的思考,实现之后会发现,Heights很难做到丝滑过度、连续变化的感觉。Heights的获取,有没有一种更好、更为优雅,简洁的实现方式呢?

回到开头所提到的公式:

M = ( Grid , Heights )

Heights究竟如何生成比较好呢?今天我们引入 曲面 来生成Heights。通过su,我们可以快速地通过手绘曲线,然后使用sandbox工具快速生成地形,如下图:

通过在地形表面上捕获若干的网格点,我们就可以获得高低起伏的Heights数据啦~

03

试验下效果

利用地形生成连续变化的Heights,更容易控制,也更容易实现。

实现的主要代码及涉及到的知识点,会在后面的更新中发布出来,有兴趣也可以加入mixlab无界社区的社群,一起探讨,研究,学习~~

本期的技巧:

通过地形来产生连续变化的Heights

你get到了吗?