openscad 的编程语言有点效仿c/c++, 前面讲到说可以使用变量来灵活控制模型的参数, 另外openscad也是支持 函数式的编程模式 和编程语言的 流程控制编程模式.
下面来领会一下这个openscad 的妙处!
module spoked_wheel(radius=12, width=5, thickness=5, number_of_spokes=7, spoke_radius=1.5) {
rotate([90,0,0]) {
// 圆环通过两个短圆柱求差得到
inner_radius = radius - thickness/2;
difference() {
cylinder(h=width,r=radius,center=true);
cylinder(h=width + 1,r=inner_radius,center=true);
}
// 轮轴通过圆柱的选择得到
spoke_length = radius - thickness/4; //获得轴的长度
step = 360/number_of_spokes; //获得旋转的度数
for (i=[0:step:359]) {
angle = i;
rotate([0,90,angle])
cylinder(h=spoke_length,r=spoke_radius);
}
}
}
module body(base_height=10, top_height=14, base_length=60, top_length=30, width=20, top_offset=5) {
// Car body base
cube([base_length,width,base_height],center=true);
// Car body top
translate([top_offset,0,base_height/2+top_height/2 - 0.001])
cube([top_length,width,top_height],center=true);
}
module axle(track=35, radius=2) {
rotate([90,0,0])
cylinder(h=track,r=2,center=true);
}
$fa = 1;
$fs = 0.4;
track = 35;
body_roll = 0;
wheels_turn = 0;
base_length = 100;
top_length = 75;
top_offset = 5;
front_axle_offset = 30;
rear_axle_1_offset = 10;
rear_axle_2_offset = 35;
wheel_radius = 12;
// Body
rotate([body_roll,0,0]) {
body(base_length=base_length, top_length=top_length, top_offset=top_offset);
}
// Front left wheel
translate([-front_axle_offset,-track/2,0])
rotate([0,0,wheels_turn])
spoked_wheel(wheel_radius=wheel_radius);
// Front right wheel
translate([-front_axle_offset,track/2,0])
rotate([0,0,wheels_turn])
spoked_wheel(wheel_radius=wheel_radius);
// Rear left wheel 1
translate([rear_axle_1_offset,-track/2,0])
rotate([0,0,0])
spoked_wheel(wheel_radius=wheel_radius);
// Rear right wheel 1
translate([rear_axle_1_offset,track/2,0])
rotate([0,0,0])
spoked_wheel(wheel_radius=wheel_radius);
// Rear left wheel 2
translate([rear_axle_2_offset,-track/2,0])
rotate([0,0,0])
spoked_wheel(wheel_radius=wheel_radius);
// Rear right wheel 2
translate([rear_axle_2_offset,track/2,0])
rotate([0,0,0])
spoked_wheel(wheel_radius=wheel_radius);
// Front axle
translate([-front_axle_offset,0,0])
axle();
// Rear axle 1
translate([rear_axle_1_offset,0,0])
axle();
// Rear axle 2
translate([rear_axle_2_offset,0,0])
axle();
我们也可以通过for循环来实现花样放车,比如下图为官方的例子实现效果,大家可以自己试试:
提示, 部分代码如下:
for (j=[0:1:2]) {
r = 140 + j*70;
n = 12 + 2*j;
step = 360/n;
for (angle=[0:step:359]) {
dx = r*cos(angle);
dy = r*sin(angle);
translate([dx,dy,0])
rotate(angle)
car();
}
}
另外 openscad 还提供 if 或者 bool?a:b 来做 流程控制.
module body(base_height=10, top_height=14, base_length=60, top_length=30, width=20, top_offset=5, top, front_bumper, rear_bumper) {
// Car body base
cube([base_length,width,base_height],center=true);
// Car body top
if (top) {
translate([top_offset,0,base_height/2+top_height/2])
cube([top_length,width,top_height],center=true);
}
// Front bumper
if (front_bumper) {
color("blue") {
translate([-base_length/2,0,0])rotate([90,0,0]) {
cylinder(h=width - base_height,r=base_height/2,center=true);
translate([0,0,(width - base_height)/2])
sphere(r=base_height/2);
translate([0,0,-(width - base_height)/2])
sphere(r=base_height/2);
}
}
}
// Rear bumper
if (rear_bumper) {
color("blue") {
translate([base_length/2,0,0])rotate([90,0,0]) {
cylinder(h=width - base_height,r=base_height/2,center=true);
translate([0,0,(width - base_height)/2])
sphere(r=base_height/2);
translate([0,0,-(width - base_height)/2])
sphere(r=base_height/2);
}
}
}
}
$fa = 1;
$fs = 0.4;
body(top=false,front_bumper=true,rear_bumper=true);
是不是感觉晕晕的,官网提供的例子到这里,但是就算是晕晕的,你也跟着官网已经做出了一个非常漂亮的小车模型.蛮有成就感的吧,
如果代码不懂,可以留言,建议仔细看看代码,代码熟悉了,以后建模就更加方便.
参考
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
暂无评论内容