小白教程
所有教程
关于
Search
108.162.216.253
108.162.216.253
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“OpenSCAD 用模块来组织代码”的源代码
本页内容
上一节:
OpenSCAD_调整模型大小和更多组合对象的方法
下一节:
OpenSCAD_使用单独的脚本和库
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
== 定义和使用模块 == 上一章最后一个例子的脚本很长。这是用更复杂的轮子设计(需要创建许多语句)替换简单的圆柱形轮子(需要创建一个语句)的结果。要将轮子从简单设计更改为复杂设计,您必须识别定义简单轮子的所有气缸命令,并将其替换为定义复杂轮子的命令。这个过程听起来类似于你必须经历的改变车轮直径的步骤。当没有使用变量时,您必须在脚本中识别相应的值并用新值一一替换它们。使用 wheel_radius 变量改进了这种重复且耗时的过程,使您能够快速轻松地更改轮子的直径。当您想完全改变车轮的设计时,您能做些什么来改善相应的容易出错的过程吗?答案是肯定的!您可以使用与应用于整个零件/模型的变量类似的模块。您可以将设计的一部分甚至整个模型定义为一个模块。 首先记住复杂轮子的设计。 <sample title="wheel_with_spherical_sides_and_holes.scad " desc=""> $fa = 1; $fs = 0.4; wheel_radius=10; side_spheres_radius=50; hub_thickness=4; cylinder_radius=2; cylinder_height=2*wheel_radius; difference() { // Wheel sphere sphere(r=wheel_radius); // Side sphere 1 translate([0,side_spheres_radius + hub_thickness/2,0]) sphere(r=side_spheres_radius); // Side sphere 2 translate([0,- (side_spheres_radius + hub_thickness/2),0]) sphere(r=side_spheres_radius); // Cylinder 1 translate([wheel_radius/2,0,0]) rotate([90,0,0]) cylinder(h=cylinder_height,r=cylinder_radius,center=true); // Cylinder 2 translate([0,0,wheel_radius/2]) rotate([90,0,0]) cylinder(h=cylinder_height,r=cylinder_radius,center=true); // Cylinder 3 translate([-wheel_radius/2,0,0]) rotate([90,0,0]) cylinder(h=cylinder_height,r=cylinder_radius,center=true); // Cylinder 4 translate([0,0,-wheel_radius/2]) rotate([90,0,0]) cylinder(h=cylinder_height,r=cylinder_radius,center=true); } </sample> [[文件:Wheel with spherical sides and holes.jpg]] 您可以通过以下方式将上述轮子定义为模块。 <sample title="blank_model.scad " desc=""> $fa = 1; $fs = 0.4; module wheel() { wheel_radius=10; side_spheres_radius=50; hub_thickness=4; cylinder_radius=2; cylinder_height=2*wheel_radius; difference() { // Wheel sphere sphere(r=wheel_radius); // Side sphere 1 translate([0,side_spheres_radius + hub_thickness/2,0]) sphere(r=side_spheres_radius); // Side sphere 2 translate([0,- (side_spheres_radius + hub_thickness/2),0]) sphere(r=side_spheres_radius); // Cylinder 1 translate([wheel_radius/2,0,0]) rotate([90,0,0]) cylinder(h=cylinder_height,r=cylinder_radius,center=true); // Cylinder 2 translate([0,0,wheel_radius/2]) rotate([90,0,0]) cylinder(h=cylinder_height,r=cylinder_radius,center=true); // Cylinder 3 translate([-wheel_radius/2,0,0]) rotate([90,0,0]) cylinder(h=cylinder_height,r=cylinder_radius,center=true); // Cylinder 4 translate([0,0,-wheel_radius/2]) rotate([90,0,0]) cylinder(h=cylinder_height,r=cylinder_radius,center=true); } } </sample> [[文件:Blank model.jpg]] 您需要做一些正确的事情。您应该注意的第一件事是,为了定义一个模块,您必须键入单词 module 后跟您要为该模块提供的名称。在这种情况下,模块被命名为wheel。模块名称后跟一对括号。目前括号内没有任何内容,因为尚未为此模块定义参数。最后,在这对括号之后是一对花括号。定义相应对象的所有命令都放在大括号内。最后不需要分号。 您应该注意的第二件事是 OpenSCAD 没有创建任何轮子。这是因为您刚刚定义了 wheel 模块但还没有使用它。为了创建轮子,您需要添加一个创建轮子的语句,类似于添加语句来创建任何原始对象(立方体、球体等)的方式。 <sample title="wheel_created_by_module.scad " desc=""> $fa = 1; $fs = 0.4; module wheel() { wheel_radius=10; side_spheres_radius=50; hub_thickness=4; cylinder_radius=2; cylinder_height=2*wheel_radius; difference() { // Wheel sphere sphere(r=wheel_radius); // Side sphere 1 translate([0,side_spheres_radius + hub_thickness/2,0]) sphere(r=side_spheres_radius); // Side sphere 2 translate([0,- (side_spheres_radius + hub_thickness/2),0]) sphere(r=side_spheres_radius); // Cylinder 1 translate([wheel_radius/2,0,0]) rotate([90,0,0]) cylinder(h=cylinder_height,r=cylinder_radius,center=true); // Cylinder 2 translate([0,0,wheel_radius/2]) rotate([90,0,0]) cylinder(h=cylinder_height,r=cylinder_radius,center=true); // Cylinder 3 translate([-wheel_radius/2,0,0]) rotate([90,0,0]) cylinder(h=cylinder_height,r=cylinder_radius,center=true); // Cylinder 4 translate([0,0,-wheel_radius/2]) rotate([90,0,0]) cylinder(h=cylinder_height,r=cylinder_radius,center=true); } } wheel(); </sample> [[文件:Wheel with spherical sides and holes.jpg]] 您可以将定义模块视为扩展 OpenSCAD 脚本语言。当您定义了一个轮子模块时,它就像拥有一个额外的可用原始对象。在这种情况下,新对象是您定义的轮子。然后,您可以像使用任何其他可用原语一样使用此模块。
返回至“
OpenSCAD 用模块来组织代码
”。
上一节:
OpenSCAD_调整模型大小和更多组合对象的方法
下一节:
OpenSCAD_使用单独的脚本和库