scad 示例openscad
示例openscad
example.scad
// Ensure objects have a sufficient number of faces.
$fn = 30;
// Draw a flat panel 100mm * 100mm * 3mm and then remove 3mm holes for screws.
panel_size = 100;
screw_size = 3;
screw_distance_from_edge = 10;
difference() {
cube([panel_size,panel_size,3]);
translate([screw_distance_from_edge,screw_distance_from_edge,-1]) {
cylinder(d=screw_size, h=5);
}
translate([screw_distance_from_edge,panel_size-screw_distance_from_edge,-1]) {
cylinder(d=screw_size, h=5);
}
translate([panel_size-screw_distance_from_edge,screw_distance_from_edge,-1]) {
cylinder(d=screw_size, h=5);
}
translate([panel_size-screw_distance_from_edge,panel_size-screw_distance_from_edge,-1]) {
cylinder(d=screw_size, h=5);
}
}
scad openscad学习笔记4
txt
16//导入stl三维模型文件,可用犀牛打开
// example016.stl is derived from Mblock.stl
// (c) 2009 Will Langford licensed under
// the Creative Commons - GNU GPL license.
// http://www.thingiverse.com/thing:753
//
// Jonas Pfeil converted the file to binary
// STL and duplicated its content.
module blk1() {
cube([ 65, 28, 28 ], center = true);
}
module blk2() {
difference() {
translate([ 0, 0, 7.5 ])
cube([ 60, 28, 14 ], center = true);
cube([ 8, 32, 32 ], center = true);
}
}
module chop() {
translate([ -14, 0, 0 ])
import(file = "example016.stl", convexity = 12);
}
difference() {
blk1();
for (alpha = [0, 90, 180, 270]) {
rotate(alpha, [ 1, 0, 0]) render(convexity = 12)
difference() {
blk2();
chop();
}
}
}
17
// To render the DXF file from the command line:
// openscad -o example017.dxf -D'mode="parts"' example017.scad
//可展示一个图形的三种状态,parts是2D设计图,exploded是拆开分离,assembled是组合状态
// mode = "parts";
// mode = "exploded";
mode = "assembled";
thickness = 6;
locklen1 = 15;
locklen2 = 10;
boltlen = 15;
midhole = 10;
inner1_to_inner2 = 50;
total_height = 80;
module shape_tripod()
{
x1 = 0;
x2 = x1 + thickness;
x3 = x2 + locklen1;
x4 = x3 + thickness;
x5 = x4 + inner1_to_inner2;
x6 = x5 - thickness;
x7 = x6 - locklen2;
x8 = x7 - thickness;
x9 = x8 - thickness;
x10 = x9 - thickness;
y1 = 0;
y2 = y1 + thickness;
y3 = y2 + thickness;
y4 = y3 + thickness;
y5 = y3 + total_height - 3*thickness;
y6 = y5 + thickness;
union()
{
difference() {
polygon([
[ x1, y2 ], [ x2, y2 ],
[ x2, y1 ], [ x3, y1 ], [ x3, y2 ],
[ x4, y2 ], [ x4, y1 ], [ x5, y1 ],
[ x5 + thickness, y3 ], [ x5, y4 ],
[ x5, y5 ],
[ x6, y5 ], [ x6, y6 ], [ x7, y6 ], [ x7, y5 ], [ x8, y5 ],
[ x8, y6 ], [ x9, y5 ],
[ x9, y4 ], [ x10, y3 ],
[ x2, y3 ]
]);
translate([ x10, y4 ]) circle(thickness);
translate([ x5 + thickness, y4 ]) circle(thickness);
}
translate([ x5, y1 ])
square([ boltlen - thickness, thickness*2 ]);
translate([ x5 + boltlen - thickness, y2 ]) circle(thickness);
translate([ x2, y2 ]) intersection() {
circle(thickness);
translate([ -thickness*2, 0 ]) square(thickness*2);
}
translate([ x8, y5 ]) intersection() {
circle(thickness);
translate([ -thickness*2, 0 ]) square(thickness*2);
}
}
}
module shape_inner_disc()
{
difference() {
circle(midhole + boltlen + 2*thickness + locklen2);
for (alpha = [ 0, 120, 240 ])
rotate(alpha) translate([ 0, midhole + boltlen + thickness + locklen2/2 ]) square([ thickness, locklen2 ], true);
circle(midhole + boltlen);
}
}
module shape_outer_disc()
{
difference() {
circle(midhole + boltlen + inner1_to_inner2 + 2*thickness + locklen1);
for (alpha = [ 0, 120, 240 ])
rotate(alpha) translate([ 0, midhole + boltlen + inner1_to_inner2 + thickness + locklen1/2 ]) square([ thickness, locklen1 ], true);
circle(midhole + boltlen + inner1_to_inner2);
}
}
module parts()
{
tripod_x_off = locklen1 - locklen2 + inner1_to_inner2;
tripod_y_off = max(midhole + boltlen + inner1_to_inner2 + 4*thickness + locklen1, total_height);
shape_inner_disc();
shape_outer_disc();
for (s = [ [1,1], [-1,1], [1,-1] ])
scale(s) translate([ tripod_x_off, -tripod_y_off ]) shape_tripod();
}
module exploded()
{
translate([ 0, 0, total_height + 2*thickness ]) linear_extrude(height = thickness, convexity = 4) shape_inner_disc();
linear_extrude(height = thickness, convexity = 4) shape_outer_disc();
color([ 0.7, 0.7, 1 ]) for (alpha = [ 0, 120, 240 ])
rotate(alpha) translate([ 0, thickness*2 + locklen1 + inner1_to_inner2 + boltlen + midhole, 1.5*thickness ])
rotate([ 90, 0, -90 ]) linear_extrude(height = thickness, convexity = 10, center = true) shape_tripod();
}
module bottle()
{
r = boltlen + midhole;
h = total_height - thickness*2;
rotate_extrude(convexity = 2)
{
square([ r, h ]);
translate([ 0, h ])
intersection() {
square([ r, r ]);
scale([ 1, 0.7 ]) circle(r);
}
translate([ 0, h+r ])
intersection() {
translate([ 0, -r/2 ]) square([ r/2, r ]);
circle(r/2);
}
}
}
module assembled()
{
translate([ 0, 0, total_height - thickness ]) linear_extrude(height = thickness, convexity = 4) shape_inner_disc();
linear_extrude(height = thickness, convexity = 4) shape_outer_disc();
color([ 0.7, 0.7, 1 ]) for (alpha = [ 0, 120, 240 ])
rotate(alpha) translate([ 0, thickness*2 + locklen1 + inner1_to_inner2 + boltlen + midhole, 0 ])
rotate([ 90, 0, -90 ]) linear_extrude(height = thickness, convexity = 10, center = true) shape_tripod();
% translate([ 0, 0, thickness*2]) bottle();
}
if (mode == "parts")
parts();
if (mode == "exploded")
exploded();
if (mode == "assembled")
assembled();
18
//比较巧妙的复用和排列方式for...translate...
module step(len, mod)
{
for (i = [0:$children-1])
translate([ len*(i - ($children-1)/2), 0, 0 ]) children((i+mod) % $children);
}
for (i = [1:4])
{
translate([0, -250+i*100, 0]) step(100, i)
{
sphere(30);
cube(60, true);
cylinder(r = 30, h = 50, center = true);
union() {
cube(45, true);
rotate([45, 0, 0]) cube(50, true);
rotate([0, 45, 0]) cube(50, true);
rotate([0, 0, 45]) cube(50, true);
}
}
}
19
//此处lookup:
Look up value in table, and linearly interpolate if there's no exact match. The first argument is the value to look up. The second is the lookup table -- a vector of key-value pairs.
查找表中的值,如果没有精确匹配的情况下返回线性插值
Parameters
key
A lookup key
<key,value> array
keys and values
Notes
There is a bug where out-of-range keys will return the first value in the list. Newer versions of Openscad should use the top or bottom end of the table as appropriate instead.
//可理解为线性插值法拟合了一条曲线,value是2D坐标上曲线给定点
function get_cylinder_h(p) = lookup(p, [
[ -200, 5 ],
[ -50, 20 ],
[ -20, 18 ],
[ +80, 25 ],
[ +150, 2 ]
]);
for (i = [-100:5:+100]) {
// echo(i, get_cylinder_h(i));
translate([ i, 0, -30 ]) cylinder(r1 = 6, r2 = 2, h = get_cylinder_h(i)*3);
}
20
//螺丝螺母的绘制,真复杂,看看就好
module screw(type = 2, r1 = 15, r2 = 20, n = 7, h = 100, t = 8)
{
linear_extrude(height = h, twist = 360*t/n, convexity = t)
difference() {
circle(r2);
for (i = [0:n-1]) {
if (type == 1) rotate(i*360/n) polygon([
[ 2*r2, 0 ],
[ r2, 0 ],
[ r1*cos(180/n), r1*sin(180/n) ],
[ r2*cos(360/n), r2*sin(360/n) ],
[ 2*r2*cos(360/n), 2*r2*sin(360/n) ],
]);
if (type == 2) rotate(i*360/n) polygon([
[ 2*r2, 0 ],
[ r2, 0 ],
[ r1*cos(90/n), r1*sin(90/n) ],
[ r1*cos(180/n), r1*sin(180/n) ],
[ r2*cos(270/n), r2*sin(270/n) ],
[ 2*r2*cos(270/n), 2*r2*sin(270/n) ],
]);
}
}
}
module nut(type = 2, r1 = 16, r2 = 21, r3 = 30, s = 6, n = 7, h = 100/5, t = 8/5)
{
difference() {
cylinder($fn = s, r = r3, h = h);
translate([ 0, 0, -h/2 ]) screw(type, r1, r2, n, h*2, t*2);
}
}
module spring(r1 = 100, r2 = 10, h = 100, hr = 12)
{
stepsize = 1/16;
module segment(i1, i2) {
alpha1 = i1 * 360*r2/hr;
alpha2 = i2 * 360*r2/hr;
len1 = sin(acos(i1*2-1))*r2;
len2 = sin(acos(i2*2-1))*r2;
if (len1 < 0.01)
polygon([
[ cos(alpha1)*r1, sin(alpha1)*r1 ],
[ cos(alpha2)*(r1-len2), sin(alpha2)*(r1-len2) ],
[ cos(alpha2)*(r1+len2), sin(alpha2)*(r1+len2) ]
]);
if (len2 < 0.01)
polygon([
[ cos(alpha1)*(r1+len1), sin(alpha1)*(r1+len1) ],
[ cos(alpha1)*(r1-len1), sin(alpha1)*(r1-len1) ],
[ cos(alpha2)*r1, sin(alpha2)*r1 ],
]);
if (len1 >= 0.01 && len2 >= 0.01)
polygon([
[ cos(alpha1)*(r1+len1), sin(alpha1)*(r1+len1) ],
[ cos(alpha1)*(r1-len1), sin(alpha1)*(r1-len1) ],
[ cos(alpha2)*(r1-len2), sin(alpha2)*(r1-len2) ],
[ cos(alpha2)*(r1+len2), sin(alpha2)*(r1+len2) ]
]);
}
linear_extrude(height = 100, twist = 180*h/hr,
$fn = (hr/r2)/stepsize, convexity = 5) {
for (i = [ stepsize : stepsize : 1+stepsize/2 ])
segment(i-stepsize, min(i, 1));
}
}
translate([ -30, 0, 0 ])
screw();
translate([ 30, 0, 0 ])
nut();
spring();
21
//切片类似效果,有点炫酷
module thing()
{
$fa = 30;
difference() {
sphere(r = 25);
cylinder(h = 62.5, r1 = 12.5, r2 = 6.25, center = true);
rotate(90, [ 1, 0, 0 ]) cylinder(h = 62.5,
r1 = 12.5, r2 = 6.25, center = true);
rotate(90, [ 0, 1, 0 ]) cylinder(h = 62.5,
r1 = 12.5, r2 = 6.25, center = true);
}
}
module demo_proj()
{
linear_extrude(center = true, height = 0.5) projection(cut = false) thing();
% thing();
}
module demo_cut()
{
for (i=[-20:5:+20]) {
rotate(-30, [ 1, 1, 0 ]) translate([ 0, 0, -i ])
linear_extrude(center = true, height = 0.5) projection(cut = true)
translate([ 0, 0, i ]) rotate(+30, [ 1, 1, 0 ]) thing();
}
% thing();
}
translate([ -30, 0, 0 ]) demo_proj();
translate([ +30, 0, 0 ]) demo_cut();
22定义一个圆角的立方体
// size is a vector [w, h, d]
module roundedBox(size, radius, sidesonly)
{
rot = [ [0,0,0], [90,0,90], [90,90,0] ];
if (sidesonly) {
cube(size - [2*radius,0,0], true);
cube(size - [0,2*radius,0], true);
for (x = [radius-size[0]/2, -radius+size[0]/2],
y = [radius-size[1]/2, -radius+size[1]/2]) {
translate([x,y,0]) cylinder(r=radius, h=size[2], center=true);
}
}
else {
cube([size[0], size[1]-radius*2, size[2]-radius*2], center=true);
cube([size[0]-radius*2, size[1], size[2]-radius*2], center=true);
cube([size[0]-radius*2, size[1]-radius*2, size[2]], center=true);
for (axis = [0:2]) {
for (x = [radius-size[axis]/2, -radius+size[axis]/2],
y = [radius-size[(axis+1)%3]/2, -radius+size[(axis+1)%3]/2]) {
rotate(rot[axis])
translate([x,y,0])
cylinder(h=size[(axis+2)%3]-2*radius, r=radius, center=true);
}
}
for (x = [radius-size[0]/2, -radius+size[0]/2],
y = [radius-size[1]/2, -radius+size[1]/2],
z = [radius-size[2]/2, -radius+size[2]/2]) {
translate([x,y,z]) sphere(radius);
}
}
}
translate([-15,0,0])roundedBox([20,30,40], 5, true);
translate([15,0,0]) roundedBox([20,30,40], 5, false);
23展示字体文件结合3D的效果,可用于自制时钟
// Example combining MCAD/fonts.scad with search() function.
use <MCAD/fonts.scad>
thisFont=8bit_polyfont();
x_shift=thisFont[0][0];
y_shift=thisFont[0][1];
hours=["one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve"];
module clock_hour_words(word_offset=20.0,word_height=2.0) {
for(i=[0:(len(hours)-1)]) assign( hourHandAngle=(i+1)*360/len(hours), theseIndicies=search(hours[i],thisFont[2],1,1) ) {
rotate(90-hourHandAngle) translate([word_offset,0])
for( j=[0:(len(theseIndicies)-1)] ) translate([j*x_shift,-y_shift/2]) {
linear_extrude(height=word_height) polygon(points=thisFont[2][theseIndicies[j]][6][0],paths=thisFont[2][theseIndicies[j]][6][1]);
}
}
}
clock_hour_words(word_offset=16.0,word_height=5.0);
24
一个比较复杂的镂空模型,暂不能理解
// Menger Sponge
// By Nathan Hellweg, Emmett Lalish and Marius Kintel May 13, 2013
// CC-BY-SA license
// Size of edge of sponge
D=100;
// Fractal depth (number of iterations)
n=3;
module menger() {
difference() {
cube(D, center=true);
for (v=[[0,0,0], [0,0,90], [0,90,0]])
rotate(v) menger_negative(side=D, maxside=D, level=n);
}
}
module menger_negative(side=1, maxside=1, level=1) {
l=side/3;
cube([maxside*1.1, l, l], center=true);
if (level > 1) {
for (i=[-1:1], j=[-1:1])
if (i || j)
translate([0, i*l, j*l])
menger_negative(side=l, maxside=maxside, level=level-1);
}
}
difference() {
rotate([45, atan(1/sqrt(2)), 0]) menger();
translate([0,0,-D]) cube(2*D, center=true);
}
scad openScad研究3
txt
11
//多面体绘制,定义顶点和角度,下面是一个金字塔形状
polyhedron(
points = [
[10, 0, 0],
[0, 10, 0],
[-10, 0, 0],
[0, -10, 0],
[0, 0, 10]
],
//triangles表示哪几个顶点连起来构成的面
triangles = [
[0, 1, 2, 3],//前4个点连成底面可理解为数组前4个[0:3]
[4, 1, 0],//最后一个点为公共顶点,构成如下4个三角面
[4, 2, 1],
[4, 3, 2],
[4, 0, 3]
]
);
12
//导入.stl文件,要编辑需要另安装
//http://pan.baidu.com/share/link?shareid=3096945288&uk=1260508576可选用
// example012.stl is Mblock.stl, (c) 2009 Will Langford
// licensed under the Creative Commons - GNU GPL license.
// http://www.thingiverse.com/thing:753
difference()
{
sphere(20);
translate([ -2.92, 0.5, +20 ]) rotate([180, 0, 180])
import("example012.stl", convexity = 5);
}
13
intersection()
{
//linear_extrude挤压出,可理解为通过2d图形,冲压出一个厚度的3D模块
linear_extrude(height = 100, center = true, convexity= 3)
import(file = "example013.dxf");
rotate([0, 90, 0])
linear_extrude(height = 100, center = true, convexity= 3)
import(file = "example013.dxf");
rotate([90, 0, 0])
linear_extrude(height = 100, center = true, convexity= 3)
import(file = "example013.dxf");
}
14
//多面体另一种构建方式,多个长方体交叉切割而成
intersection_for(i = [
[0, 0, 0],
[10, 20, 300],
[200, 40, 57],
[20, 88, 57]
])
rotate(i) cube([100, 20, 20], center = true);
15
//二维图形
module shape()
{
difference()
{
translate([ -35, -35 ]) intersection()
{
union() {
difference() {
square(100, true);
square(50, true);
}
translate([ 50, 50 ])
square(15, true);
}
rotate(45) translate([ 0, -15 ]) square([ 100, 30 ]);
}
rotate(-45) scale([ 0.7, 1.3 ]) circle(5);
}
import(file = "example009.dxf", layer = "body",
convexity = 6, scale=2);
}
//图形生成挤压件可用下面函数
// linear_extrude(convexity = 2, center = true)
shape();
scad openScad study2
txt
example006最终效果是一个骰子
module example006()
{
//定义一个函数edgeprofile,边角效果
module edgeprofile()
{
//渲染(凸性为2,此处不明白convexity,更改此值-2~+1000未见显著影响)
render(convexity = 2) difference() {
//长方柱体基础
cube([20, 20, 150], center = true);
//减掉一段圆柱
translate([-10, -10, 0])
cylinder(h = 80, r = 10, center = true);
//减掉上下两个小球
translate([-10, -10, +40])
sphere(r = 10);
translate([-10, -10, -40])
sphere(r = 10);
}
}
//立方体减去边角,得到非常圆润的立方体
difference()
{
cube(100, center = true);
//对下面图形复制3份进行旋转
for (rot = [ [0, 0, 0], [1, 0, 0], [0, 1, 0] ]) {
rotate(90, rot)
//p是一个4个元素的坐标数组,每个元素,第一个是X偏移值,第二个是Y偏移值,第三个是旋转角度
for (p = [[+1, +1, 0], [-1, +1, 90], [-1, -1, 180], [+1, -1, 270]]) {
translate([ p[0]*50, p[1]*50, 0 ])
rotate(p[2], [0, 0, 1])
edgeprofile();
}
}
//下面构造6个面的点数,6个面,不同数目的圆球
for (i = [
//1点
[ 0, 0, [ [0, 0] ] ],
//2点
[ 90, 0, [ [-20, -20], [+20, +20] ] ],
//6点
[ 180, 0, [ [-20, -25], [-20, 0], [-20, +25], [+20, -25], [+20, 0], [+20, +25] ] ],
//5点
[ 270, 0, [ [0, 0], [-25, -25], [+25, -25], [-25, +25], [+25, +25] ] ],
//3点
[ 0, 90, [ [-25, -25], [0, 0], [+25, +25] ] ],
//4点
[ 0, -90, [ [-25, -25], [+25, -25], [-25, +25], [+25, +25] ] ]
]) {
rotate(i[0], [0, 0, 1]) rotate(i[1], [1, 0, 0]) translate([0, -50, 0])
for (j = i[2])
translate([j[0], 0, j[1]]) sphere(10);
}
}
}
example006();
7
//首次引入dxf文件,研究后发现就是方便工程制图的软件,对于精度要求非常高,造型比较复杂的很适用,不过要学工程制图的一些东西也相当大的一个工程量
module cutout()
{
intersection()
{
rotate(90, [1, 0, 0])
translate([0, 0, -50])
linear_extrude(height = 100, convexity = 1)
import(file = "example007.dxf", layer = "cutout1");
rotate(90, [0, 0, 1])
rotate(90, [1, 0, 0])
translate([0, 0, -50])
linear_extrude(height = 100, convexity = 2)
import(file = "example007.dxf", layer = "cutout2");
}
}
module clip()
{
difference() {
// NB! We have to use the deprecated module here since the "dorn"
// layer contains an open polyline, which is not yet supported
// by the import() module.
rotate_extrude(
file = "example007.dxf",
layer="dorn",
convexity = 3);
for (r = [0, 90])
rotate(r, [0, 0, 1])
cutout();
}
}
module cutview()
{
difference()
{
difference()
{
translate([0, 0, -10])
clip();
rotate(20, [0, 0, 1])
rotate(-20, [0, 1, 0])
translate([18, 0, 0])
cube(30, center = true);
}
# render(convexity = 5) intersection()
{
translate([0, 0, -10])
clip();
rotate(20, [0, 0, 1])
rotate(-20, [0, 1, 0])
translate([18, 0, 0])
cube(30, center = true);
}
}
}
translate([0, 0, -10])
clip();
// cutview();
此处首次出现import dxf文件,查看dxf文件需另装CAXA电子图版http://mydown.yesky.com/soft/multimedia/manipulate/24/459524_more.shtml
8
difference()
{
intersection()
{
translate([ -25, -25, -25])
linear_extrude(height = 50, convexity = 3)
import(file = "example008.dxf", layer = "G");
rotate(90, [1, 0, 0])
translate([ -25, -125, -25])
linear_extrude(height = 50, convexity = 3)
import(file = "example008.dxf", layer = "E");
rotate(90, [0, 1, 0])
translate([ -125, -125, -25])
linear_extrude(height = 50, convexity = 3)
import(file = "example008.dxf", layer = "B");
}
intersection()
{
translate([ -125, -25, -26])
linear_extrude(height = 52, convexity = 1)
import(file = "example008.dxf", layer = "X");
rotate(90, [0, 1, 0])
translate([ -125, -25, -26])
linear_extrude(height = 52, convexity = 1)
import(file = "example008.dxf", layer = "X");
}
}
9得到一个螺旋桨工程图,涉及调用尺寸和图层,比较复杂,占不深入研究
bodywidth = dxf_dim(file = "example009.dxf", name = "bodywidth");
fanwidth = dxf_dim(file = "example009.dxf", name = "fanwidth");
platewidth = dxf_dim(file = "example009.dxf", name = "platewidth");
fan_side_center = dxf_cross(file = "example009.dxf", layer = "fan_side_center");
fanrot = dxf_dim(file = "example009.dxf", name = "fanrot");
% linear_extrude(height = bodywidth, center = true, convexity = 10)
import(file = "example009.dxf", layer = "body");
% for (z = [+(bodywidth/2 + platewidth/2),
-(bodywidth/2 + platewidth/2)])
{
translate([0, 0, z])
linear_extrude(height = platewidth, center = true, convexity = 10)
import(file = "example009.dxf", layer = "plate");
}
intersection()
{
linear_extrude(height = fanwidth, center = true, convexity = 10, twist = -fanrot)
import(file = "example009.dxf", layer = "fan_top");
// NB! We have to use the deprecated module here since the "fan_side"
// layer contains an open polyline, which is not yet supported
// by the import() module.
rotate_extrude(file = "example009.dxf", layer = "fan_side",
origin = fan_side_center, convexity = 10);
}
10
// example010.dat generated using octave:?
//此处首次出现.DAT文件,其实就是数据格式文件,可用EXCEL查看,是一组横纵坐标数据,可根据下面公式生成,用于绘制各种曲面
// d = (sin(1:0.2:10)' * cos(1:0.2:10)) * 10;
// save("example010.dat", "d");
//此处用到intersection表示计算两个图形交叉后得到的图形
intersection()
{
surface(file = "example010.dat",
center = true, convexity = 5);
rotate(45, [0, 0, 1])
surface(file = "example010.dat",
center = true, convexity = 5);
scad openScad study1
txt
1
安装文件:windows版本
OpenSCAD快速使用手册:
2014-5-28jxb摘自http://bbs.dfpcn.com/viewtopic.php?id=242013-12-191,语法var=value;
//变量赋值
modulename(…){…}//模块定义,openscad中利用类C语言的模块的概念,通过重复利
用的方式,减轻工作量name();//调用指定名字的模块,进行绘制
functionname(…)=…//函数定义
name();
//函数调用
include<….scad>//包含头文件,通常用于包含一些通用的配置文件
use<….scad>//复用文件,可以将指定文件中的模块包含进来,方便在当前边的设计中,调用
那些模块
2,2D图形绘制
circle(radius)//以radius为半径画圆
square(size,center)//以size为边长绘制正方形
square([width,height],center)//以width,height长宽绘制长方形
polygon([points])//按点坐标作多边形
polygon([points],[paths])//依照点坐标和路径,作多边形
3,3D图形绘制
sphere(radius)//以radius为半径绘制球
cube(size)//以size为边长作正方体
cube([width,height,depth])//以width,height,depth作为长宽高作长方体
cylinder(h,r,center)//以h作高,r作半径,绘制圆柱
cylinder(h,r1,r2,center)//以h作高,r1,r2分别为上下圆半径作,圆锥体。
polyhedron(points,triangles,convexity)//多面体
4,变形
translate([x,y,z])//依照[x,y,z]响量进行平移
rotate([x,y,z])//围绕XYZ轴,分别作x,y,z度的旋转。scale([x,y,z])//沿XYZ轴,以x,y,z为比例缩放
resize([x,y,z],auto)//mirror([x,y,z])//镜像
multmatrix(m)color("colorname")color([r,g,b,a])hull()minkowski()//敏可夫斯基运算,对两个多面体进行运算。得到由一个多面体遍历另一个多面体
的结果。常用来作圆角。
逻辑操作
union()//多个多面体的总和,(或运算)
difference()//由第一个多面体,减去除第一个以外的多面体
intersection()//得出多个多面体相交的部分,(与运算)
操作字符,标记出指定多边形,以方便调整
*隐藏
!仅显示,不生成
#高亮显示
%透明显示
数学函数
abssign
acosasin
atan
atan2sincos
floor
round
ceil
lnlen
log
lookup
min
max
pow
sqrt
exprands其他
echo(…)
str(…)
for(i=[start:end]){…}//for循环
for(i=[start:step:end]){…}
for(i=[…,…,…]){…}
intersection_for(i=[start:end]){…}
intersection_for(i=[start:step:end]){…}
intersection_for(i=[…,…,…]){…}
if(…){…}assign(…){…}
search(…)
import("….stl")//导入stl模型
linear_extrude(height,center,convexity,twist,slices)//挤出
rotate_extrude(convexity)
surface(file="….dat",center,convexity)
projection(cut)//投影
render(convexity)//渲染
特殊变量
$fa最小角度
$fs最小长度
$fn最小分段
$t动画分步
example学习
//学习实例1
//文件命名为module XXX(){}
module example001()
{
function r_from_dia(d) = d / 2;
//对函数r_from_dia()赋值,其实就是计算半径的函数半径=直径/2
module rotcy(rot, r, h) {
//定义新模型,名为rotcy,包含3个参数rot,r,h
rotate(90, rot)
//旋转90度,方向为rot向量,按右手定则旋转,注意后面紧跟要旋转的对象,不加分号“;”
cylinder(r = r, h = h, center = true);
//绘制圆柱体,半径r,高h
}
//difference函数为从第一个形状中减去下面的形状
difference() {
sphere(r = r_from_dia(size));
//以size为半径绘制球
rotcy([0, 0, 0], cy_r, cy_h);
//绘制不旋转的圆柱圆面在Z轴
rotcy([1, 0, 0], cy_r, cy_h);
//绘制圆柱后圆面旋转到Y轴
rotcy([0, 1, 0], cy_r, cy_h);
//绘制圆柱后圆面旋转到X轴
}
//定义参数大小
size = 50;
hole = 25;
cy_r = r_from_dia(hole);
cy_h = r_from_dia(size * 2.5);
}
example001();
2
module example002()
{
//得出多面体相交部分
intersection() {
difference() {
union() {
cube([30, 30, 30], center = true);
//这样写立方体中心在原点,也等价写法:
//translate([-15,-15, -15])
//cube(30);
translate([0, 0, -25])
cube([15, 15, 50], center = true);
}
//三个长方体通道
union() {
cube([50, 10, 10], center = true);
cube([10, 50, 10], center = true);
cube([10, 10, 50], center = true);
}
}
//圆锥体部分
translate([0, 0, 5])
cylinder(h = 50, r1 = 20, r2 = 5, center = true);
}
}
example002();
3
module example003()
{
difference() {
union() {
cube([30, 30, 30], center = true);
cube([40, 15, 15], center = true);
cube([15, 40, 15], center = true);
cube([15, 15, 40], center = true);
}
union() {
cube([50, 10, 10], center = true);
cube([10, 50, 10], center = true);
cube([10, 10, 50], center = true);
}
}
}
example003();
4
//立方体与球的镂空效果
module example004()
{
difference() {
cube(30, center = true);
sphere(20);
}
}
example004();
5
//简单的亭子建筑效果
module example005()
{
translate([0, 0, -120]) {
//亭子底座部分
difference() {
//底部台面
cylinder(h = 50, r = 100);
//底部挖空圆柱部分
translate([0, 0, 10]) cylinder(h = 50, r = 80);
//进口缺角部分
translate([100, 0, 35]) cube(50, center = true);
}
//柱子部分循环画出6根柱子
for (i = [0:5]) {
echo(360*i/6, sin(360*i/6)*80, cos(360*i/6)*80);
translate([sin(360*i/6)*80, cos(360*i/6)*80, 0 ])
cylinder(h = 200, r=10);
}
//顶盖部分
translate([0, 0, 200])
cylinder(h = 80, r1 = 120, r2 = 0);
}
}
example005();
scad IKEA KOPPLA桌面安装支架。好!如果以快速模式打印(而不是默认为10%),请使用30%填充,并确保inf
IKEA KOPPLA桌面安装支架。好!如果在快速模式下打印(而不是默认值为10%),请使用30%填充,并确保填充速度不高于Cura快速模式下的打印和行进速度。 - 试试101%!!!!!!!!!!!
KOPPLA-landscape.scad
// This work is licensed under the
// Creative Commons Attribution-ShareAlike 4.0
// International License.
// To view a copy of this license, visit
// http://creativecommons.org/licenses/by-sa/4.0/
// Based on Euro power strip clip - customizable clip size
// by lizzard77, published Feb 16, 2016 who modified
// the original CAD model by ecloud to be customizable
// Parametrized for IKEA Koppla
// by probono, published March 11, 2017
// how long the legs should be
legLength = 42;
boardThickness = 50.5; // desk tickness + 0.5 mm allowance
// Use Rayher Crepla 2mm on both sides
// to get good grip
$fn = 64;
width = 53;
height = 42;
heightRadius = 60;
bracketThickness = 9;
powerStripOpeningWidth = 30;
clipOpeningWidth = legLength-7;
module powerStripCrossSection(thickness) {
intersection() {
translate([width / 2 - heightRadius, 0, 0])
cylinder(r=heightRadius, h=thickness, center=true);
translate([heightRadius - width / 2, 0, 0])
cylinder(r=heightRadius, h=thickness, center=true);
cube([width, height, thickness], center=true);
}
}
difference() {
totalWidth = width + clipOpeningWidth+3;
cube([62, totalWidth, bracketThickness]);
translate([31 - powerStripOpeningWidth/2, totalWidth-6, -1])
cube([powerStripOpeningWidth, 7, bracketThickness+2]);
translate([((62-boardThickness)/2), -1, -1])
cube([boardThickness, clipOpeningWidth+7, bracketThickness + 2]);
translate([31, totalWidth-5-height/2, bracketThickness/2])
powerStripCrossSection(bracketThickness+2);
}
KOPPLA.scad
// This work is licensed under the
// Creative Commons Attribution-ShareAlike 4.0
// International License.
// To view a copy of this license, visit
// http://creativecommons.org/licenses/by-sa/4.0/
// Based on Euro power strip clip - customizable clip size
// by lizzard77, published Feb 16, 2016 who modified
// the original CAD model by ecloud to be customizable
// Parametrized for IKEA Koppla
// by probono, published March 11, 2017
clipOpeningWidth = 22.5 ; // desk tickness + 0.5 mm allowance
// Use Rayher Crepla 2mm on both sides
// to get good grip
$fn = 64;
width = 53;
height = 42;
heightRadius = 60;
bracketThickness = 9;
powerStripOpeningWidth = 30;
module powerStripCrossSection(thickness) {
intersection() {
translate([width / 2 - heightRadius, 0, 0])
cylinder(r=heightRadius, h=thickness, center=true);
translate([heightRadius - width / 2, 0, 0])
cylinder(r=heightRadius, h=thickness, center=true);
cube([width, height, thickness], center=true);
}
}
difference() {
totalWidth = width + clipOpeningWidth+3;
cube([62, totalWidth, bracketThickness]);
translate([31 - powerStripOpeningWidth/2, totalWidth-6, -1])
cube([powerStripOpeningWidth, 7, bracketThickness+2]);
translate([-1, 5, -1])
cube([56, clipOpeningWidth, bracketThickness + 2]);
translate([31, totalWidth-5-height/2, bracketThickness/2])
powerStripCrossSection(bracketThickness+2);
}