生成openscad源代码的python库
OpenPySCAD的Python项目详细描述
openpyscad
生成OpenSCAD源代码的python库。此库在处理三维数据时提供直观的界面。 openpyscad同时支持python2(2.7)和python3(3.4+)。
安装
pip install openpyscad
如何使用
- 编写python代码如下:
importopenpyscadasopsc1=ops.Cube([10,20,10])c2=ops.Cube([20,10,10])(c1+c2).write("sample.scad")
- 生成的代码将写在“sample.scad”中。openscad可以检测到代码的更改并自动重新加载。太酷了:d
union(){ cube([10, 20, 10]); cube([20, 10, 10]); };
生成的代码示例
三维形状
Python:
Sphere(r=10,_fn=100)Cube([10,10,10])Cylinder(h=10,r=10)p=Polyhedron(points=[[10,10,0],[10,-10,0],[-10,-10,0],[-10,10,0],[0,0,10]],faces=[[0,1,4],[1,2,4],[2,3,4],[3,0,4],[1,0,3],[2,1,3]])
生成的openscad代码:
sphere(r=10, $fn=100); cube(size=[10, 10, 10]); cylinder(h=10, r=10); polyhedron(points=[[10, 10, 0], [10, -10, 0], [-10, -10, 0], [-10, 10, 0], [0, 0, 10]], faces=[[0, 1, 4], [1, 2, 4], [2, 3, 4], [3, 0, 4], [1, 0, 3], [2, 1, 3]]);
布尔运算
Python:
# UnionCube([20,10,10])+Cube([10,20,10])# You can also write like thisu=Union()u.append(Cube[20,10,10])u.append(Cube[10,20,10])# DifferenceCube([20,10,10])-Cube([10,20,10])# You can also write like thisi=Difference()i.append(Cube[20,10,10])i.append(Cube[10,20,10])# IntersectionCube([20,10,10])&Cube([10,20,10])# You can also write like thisi=Intersection()i.append(Cube[20,10,10])i.append(Cube[10,20,10])
生成的openscad代码:
// Union union(){ cube([20, 10, 10]) cube([10, 20, 10]) }; // Difference difference(){ cube([20, 10, 10]); cube([10, 20, 10]); }; // Intersection intersection(){ cube([20, 10, 10]); cube([10, 20, 10]); };
转换
Python:
# TranslateCube([20,10,10]).translate([10,10,10])# RotateCube([20,10,10]).rotate([0,0,45])# ScaleCube([20,10,10]).scale([2,1,1])# ResizeCube([20,10,10]).resize([2,1,1])# MirrorCube([20,10,10]).mirror([1,1,1])# ColorCube([20,10,10]).color("Red")# OffsetCircle(10).offset(10)
生成的openscad代码:
// Translate translate(v=[10, 10, 10]){ cube([20, 10, 10]); }; // Rotate rotate(v=[0, 0, 45]){ cube([20, 10, 10]); }; // Scale scale(v=[2, 1, 1]){ cube([20, 10, 10]); }; // Resize resize(newsize=[2, 1, 1]){ cube(size=[20, 10, 10]); }; // Mirror mirror([1, 1, 1]){ cube(size=[20, 10, 10]); }; // Color color("Red"){ cube(size=[20, 10, 10]); }; // Offset offset(r=10){ circle(r=10); };
修饰语
openpyscad提供modifiers接口(“*”,“!”、“”和“%”)。
Python:
c1=Cube(10)c1.disable()# add "*" characterc1.show_only()# add "!" characterc1.is_debug()# add "#" characterc1.is_transparent()# add "&" character
许可证
麻省理工学院