|
|
OPENSCAD的difference操作符,能轻松实现三轴CNC效果:工件不动,刀具动。
但是要实现四轴CNC的效果(工件旋转,刀具动),在代码逻辑上就有点困难了。
于是我封装了一个函数来实现这个目的,只需要指定切削路径和工件所需切削的N个角度即可,这在加工思维上就通顺多了。
DEMO效果是把一个球切割出8条裂痕。只要你稍加修改代码,实现五轴CNC的切削效果(工件旋转+倾斜,刀具动)也轻而易举。
- $fn=20;
- // 通过递归实现工件在 Z 轴上的自转
- module rotate_workpiece_z(angles, idx = 0)
- {
- if (idx >= len(angles))
- {
- children(0);
- }
- else
- {
- rotate([0, 0, -angles[idx]])
- {
- difference()
- {
- rotate([0, 0, angles[idx]])
- {
- rotate_workpiece_z(angles, idx + 1)
- {
- children(0);
- children(1);
- }
- }
- children(1);
- }
- }
- }
- }
- // 自定义工件
- module workpiece()
- {
- sphere(10);
- }
- // 自定义切削路径
- module cutting_path()
- {
- module ring(out_r, in_r, h)
- {
- difference()
- {
- cylinder(h, out_r, out_r, true);
- cylinder(h, in_r, in_r, true);
- }
- }
- ring_out_r = 10+1;
- ring_in_r = 8;
- ring_h = 0.2;
- rotate([0, 90, 0])
- {
- ring(ring_out_r, ring_in_r, ring_h);
- }
- }
- // 执行切削
- render()
- {
- rotate_workpiece_z([0, 30, 60, 90, 120, 150]) //每当工件旋转到这些角度就切削一次
- {
- workpiece(); //工件
- cutting_path(); //切削路径
- }
- }
复制代码 |
|