在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:SolidCode/SolidPython开源软件地址:https://github.com/SolidCode/SolidPython开源编程语言:Python 99.5%开源软件介绍:SolidPython
SolidPython: OpenSCAD for PythonSolidPython is a generalization of Phillip Tiefenbacher's openscad module, found on Thingiverse. It generates valid OpenSCAD code from Python code with minimal overhead. Here's a simple example: This Python code: from solid import *
d = difference()(
cube(10),
sphere(15)
)
print(scad_render(d)) Generates this OpenSCAD code: difference(){
cube(10);
sphere(15);
} That doesn't seem like such a savings, but the following SolidPython code is a lot shorter (and I think clearer) than the SCAD code it compiles to: from solid import *
from solid.utils import *
d = cube(5) + right(5)(sphere(5)) - cylinder(r=2, h=6) Generates this OpenSCAD code: difference(){ union(){ cube(5); translate( [5, 0,0]){ sphere(5); } } cylinder(r=2, h=6); } AdvantagesBecause you're using Python, a lot of things are easy that would be hard or impossible in pure OpenSCAD. Among these are:
Installing SolidPython
Using SolidPython
Importing OpenSCAD code
check the current location designated in OpenSCAD library directories. Ex:
module box(w,h,d){ cube([w,h,d]); }
from solid import *
scadfile = import_scad('/path/to/scadfile.scad')
b = scadfile.box(2,4,6)
scad_render_to_file(b, 'out_file.scad')
from solid import *
# MCAD is OpenSCAD's most common utility library: https://github.com/openscad/MCAD
# If it's installed for OpenSCAD (on MacOS, at: ``$HOME/Documents/OpenSCAD/libraries``)
mcad = import_scad('MCAD')
# MCAD contains about 15 separate packages, each included as its own namespace
print(dir(mcad)) # => ['bearing', 'bitmap', 'boxes', etc...]
mount = mcad.motors.stepper_motor_mount(nema_standard=17)
scad_render_to_file(mount, 'motor_mount_file.scad')
Ex:
module box(w,h,d){ cube([w,h,d]); }
from solid import *
# use() puts the module `box()` into the global namespace
use('/path/to/scadfile.scad')
b = box(2,4,6)
scad_render_to_file(b, 'out_file.scad') Example CodeThe best way to learn how SolidPython works is to look at the included example code. If you've installed SolidPython, the following line of Python will print(the location of ) the examples directory: import os, solid; print(os.path.dirname(solid.__file__) + '/examples') Or browse the example code on Github here Adding your own code to the example file solid/examples/solidpython_template.py will make some of the setup easier. Extra syntactic sugarBasic operatorsFollowing Elmo Mäntynen's suggestion, SCAD objects override the basic operators + (union), - (difference), and * (intersection). So c = cylinder(r=10, h=5) + cylinder(r=2, h=30) is the same as: c = union()(
cylinder(r=10, h=5),
cylinder(r=2, h=30)
) Likewise: c = cylinder(r=10, h=5)
c -= cylinder(r=2, h=30) is the same as: c = difference()(
cylinder(r=10, h=5),
cylinder(r=2, h=30)
) First-class Negative Space (Holes)OpenSCAD requires you to be very careful with the order in which you add
or subtract objects. SolidPython's Consider making a joint where two pipes come together. In OpenSCAD you need to make two cylinders, union them, then make two smaller cylinders, union them, then subtract the smaller from the larger. Using hole(), you can make a pipe, specify that its center should remain open, and then add two pipes together knowing that the central void area will stay empty no matter what other objects are added to that structure. Example: outer = cylinder(r=pipe_od, h=seg_length)
inner = cylinder(r=pipe_id, h=seg_length)
pipe_a = outer - hole()(inner) Once you've made something a hole, eventually you'll want to put
something, like a bolt, into it. To do this, we need to specify that
there's a given 'part' with a hole and that other parts may occupy the
space in that hole. This is done with the See solid/examples/hole_example.py for the complete picture. AnimationOpenSCAD has a special variable, See solid/examples/animation_example.py for more details. solid.utilsSolidPython includes a number of useful functions in solid/utils.py. Currently these include: Directions: (up, down, left, right, forward, back) for arranging things:up(10)(
cylinder()
) seems a lot clearer to me than: translate( [0,0,10])(
cylinder()
) I took this from someone's SCAD work and have lost track of the
original author.
My apologies.
ArcsI've found this useful for fillets and rounds. arc(rad=10, start_degrees=90, end_degrees=210) draws an arc of radius 10 counterclockwise from 90 to 210 degrees. arc_inverted(rad=10, start_degrees=0, end_degrees=90) draws the portion of a 10x10 square NOT in a 90 degree circle of radius 10. This is the shape you need to add to make fillets or remove to make rounds. Extrude Along Path
See solid/examples/path_extrude_example.py for use. Bill Of MaterialsPut The example file solid/examples/bom_scad.py illustrates this. Check it out. solid.screw_threadsolid.screw_thread includes a method, thread() that makes internal and external screw threads. See solid/examples/screw_thread_example.py for more details. solid.splinessolid.splines contains functions to generate smooth Catmull-Rom curves through control points. from solid import translate from solid.splines import catmull_rom_polygon, bezier_polygon from euclid3 import Point2 points = [ Point2(0,0), Point2(1,1), Point2(2,1), Point2(2,-1) ] shape = catmull_rom_polygon(points, show_controls=True) bezier_shape = translate([3,0,0])(bezier_polygon(points, subdivisions=20)) See solid/examples/splines_example.py for more details and options. Jupyter RendererRender SolidPython or OpenSCAD code in Jupyter notebooks using ViewSCAD, or install directly via: pip install viewscad (Take a look at the repo page, though, since there's a tiny bit more installation required) ContactEnjoy, and please send any questions or bug reports to me at
Cheers! Evan LicenseThis library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. Some class docstrings are derived from the OpenSCAD User Manual, so are available under the Creative Commons Attribution-ShareAlike License. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论