Quantcast
Channel: CodeSection,代码区,Python开发技术文章_教程 - CodeSec
Viewing all articles
Browse latest Browse all 9596

Codementor: Laser Cutting with Python

$
0
0

Wouldn't it be cool that apart from making apps, you can also produce physical things with your coding skills?

This basic tutorial will show you how you can use the python Turtle library to create shapes ready for laser cutting! The concept is the same for any language, I'm usingPython because lots of students use it in the UK.

Create a shape with the Turtle library

The Turtle library comes installed with Python, so you don't need to worry about installing it.

# import everything in the library from turtle import * # move forwards 100 pixels forward(100) # finish done()

Run your program, and you should see a line being drawn. The picture above is a necklace made by analyzing a sound file. After processing the .wav file, I needed to create a whole lot of circles. There are many primitive shapes you can use in the library ― here're the docs .

Export the design

After you've done your design, the key part is exporting in a format suitable for the laser cutter. Fortunately, the canvas that the Turtle library uses can easily export to PostScript!

At the end of your program (but before the done() ), add these lines:

# hide the little turtle icon so it doesn't get exported hideturtle() # export the drawing as a postscript file getscreen().getcanvas().postscript(file="circles.eps")

This will create a PostScript file in the current directory called circles.eps .

Cut it!

I use Corel Draw with my laser cutter, and it imports these files perfectly. I've also found SVGs to work well and there are lots of libraries for all languages you can use. An issue to watch out for is scaling, create a 100mm line somewhere and use it as a reference.


Viewing all articles
Browse latest Browse all 9596

Trending Articles