create account

Modify STL CAD files for CNC or 3D Print using Python Programming by makerhacks

View this thread on: hive.blogpeakd.comecency.com
· @makerhacks · (edited)
$10.19
Modify STL CAD files for CNC or 3D Print using Python Programming
<br class="wp-block-image"><img src="https://makerhacks.com/wp-content/uploads/2018/07/python-stl.png" alt="modify stl files with python code" class="wp-image-114821" /><br/></figure>

<p>We are pretty familiar at this point with the 3D design creation workflow. Drawing, dragging and dropping CAD files using a mouse and a package such as Fusion 360, or a browser-based application, such as Onshape.</p>

<p>What, though, if you have to create a whole bunch of personalised objects? Opening, editing, exporting doesn't take long per item, but if you have 100, that soon adds up to quite a chore!</p>

<p>A member of a Facebook group had just that issue. They needed to add consecutive serial numbers to a vape design he had created. Initial order was 100 items.</p>

<p>People suggested printing the vapes then lasering the serial numbers. Or maybe hand engraving.</p>

<p>What if you could generate the files with <em>code</em>?</p>

<h3>Generating and modifying STL CAD files with Python</h3>

<p><a href="https://gist.github.com/omiq/84e05971dc45af0add771432b280c1a3">View the full code for this project in this Gist.</a></p>

<p>Openscad allows us to generate CAD files in code, but if I am going to create a workflow then I like to use my <em>swiss army knife</em> which is Python. Well it turns out generating CAD in Python is a solved problem too!</p>

<p>Enter <a href="https://github.com/SolidCode/SolidPython"><strong>Solid Python</strong></a>.</p>

<p>Solid Python is a Python wrapper around Openscad. All the power of CAD, with all the flexibility and libraries of Python.</p>

<pre class="wp-block-preformatted">from solid import * <br/>d = difference()( cube(10), sphere(15) ) <br/>print(scad_render(d))</pre>

<p>If you are familiar with SCAD and Python syntax then that will make more sense than if not, obviously. Essentially here we are simply using the Solid library to create an object which is a sphere-shaped hole in a cube.</p>

<p></p>

<br class="wp-block-image"><img src="https://makerhacks.com/wp-content/uploads/2018/07/Screenshot-from-2018-07-10-19-29-20.png" alt="" class="wp-image-114872" /><br/></figure>

<p><em>Rendering</em> outputs the CAD code, and it can be printed to the screen or saved to a file.</p>

<h3>Modifying STL files</h3>

<p>In OpenSCAD we can modify an STL using the following import syntax:</p>

<pre class="wp-block-preformatted">import("base.stl");<br/><br/></pre>

<p>It wasn't documented, but I found in the code, that Solid has an equivalent feature:</p>

<pre class="wp-block-preformatted">a = import_stl("base.stl")</pre>

<p>So having our base STL loaded, all we need to do is add our appropriate text.</p>

<p>(In my case I am adding text to an edge-lit acrylic sign base that I had designed quickly in Tinkercad, you will need your own STL file and coordinates for the text)</p>

```
a = import_stl("base.stl") + translate([5, -37, 10])(
linear_extrude(height=2, convexity=4)(
text(annotation,
size=8,
font="helvetica",
halign="center",
valign="center")))
```

<p>Using the text object we can add arbitrary text (here using a string called <em>annotation</em>), choosing
	<br class="gr_ gr_27 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep" id="27" data-gr-id="27">font</g> family, size, and so-on, but we need that extrude to add depth.</p>

<p>We position this text using <em>translate</em> and the appropriate coordinates.</p>

<h3>Generating the new STL CAD file</h3>



<p>We could copy and paste the rendered code, but that would still be a chore for 100, so we need to automate OpenSCAD to generate STL files.</p>


<p>I got a little fancy and also generated a preview image and displayed it so I could see what it was doing, but you could obviously skip that aspect!</p>

```

scad_render_to_file(a, outfile)<br/><br/>os.system("openscad {} -o {}".format(outfile, stlfile))
os.system("openscad --preview --imgsize=512,512 {} -o {}".format(outfile, outimage))
picture = Image.open(outimage).show()</pre>
```


<br class="wp-block-image"><img src="https://makerhacks.com/wp-content/uploads/2018/07/export.png" alt="" class="wp-image-114911" /><br/></figure>

<h3>Bottom Line</h3>



<p>I am sure now you can extrapolate how using this approach, and supplying an appropriate string for the annotation and an output filename, you could generate hundreds of custom 3D objects ready to CNC or 3D print.</p>


<p><strong>Go try it and let me know how it works out for you!</strong></p>
<hr>

[![makerhacks.png](https://cdn.steemitimages.com/DQmQt85v2egWPFwnp55DneenrwKCR6sQLinEfq3JAo68FwR/makerhacks.png)](/@makerhacks)

<br /><center><hr/><em>Posted from my blog with <a href='https://wordpress.org/plugins/steempress/'>SteemPress</a> : https://makerhacks.com/modify-stl-cad-files-for-cnc-or-3d-print-using-python-programming/</em><hr/></center>
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
👎  
properties (23)
authormakerhacks
permlinkmodifystlcadfilesforcncor3dprintusingpythonprogramming-mtbpntdw0k
categorymaking
json_metadata{"community":"steempress","app":"steemit/0.1","image":["https://makerhacks.com/wp-content/uploads/2018/07/python-stl.png","https://makerhacks.com/wp-content/uploads/2018/07/Screenshot-from-2018-07-10-19-29-20.png","https://makerhacks.com/wp-content/uploads/2018/07/export.png","https://cdn.steemitimages.com/DQmQt85v2egWPFwnp55DneenrwKCR6sQLinEfq3JAo68FwR/makerhacks.png"],"tags":["making","arduino","steemmakers","printing3d","design3d"],"original_link":"https://makerhacks.com/modify-stl-cad-files-for-cnc-or-3d-print-using-python-programming/","links":["https://gist.github.com/omiq/84e05971dc45af0add771432b280c1a3","https://github.com/SolidCode/SolidPython","/@makerhacks","https://wordpress.org/plugins/steempress/","https://makerhacks.com/modify-stl-cad-files-for-cnc-or-3d-print-using-python-programming/"],"format":"markdown"}
created2018-07-11 01:43:09
last_update2018-07-11 01:51:42
depth0
children3
last_payout2018-07-18 01:43:09
cashout_time1969-12-31 23:59:59
total_payout_value7.832 HBD
curator_payout_value2.355 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length4,862
author_reputation156,977,359,570,955
root_title"Modify STL CAD files for CNC or 3D Print using Python Programming"
beneficiaries
0.
accountsteempress-io
weight1,500
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id64,227,015
net_rshares5,786,765,712,870
author_curate_reward""
vote details (33)
@hawk399 ·
$0.10
Hey that's really cool. It took me a moment until I understood that you use Python to generate more complex/individual OpenSCAD models.
The last function with the preview is a really cool idea as well. As I have never worked with Python before. Would this part also work under Windows or only under Linux?
👍  
properties (23)
authorhawk399
permlinkre-makerhacks-modifystlcadfilesforcncor3dprintusingpythonprogramming-mtbpntdw0k-20180711t075007237z
categorymaking
json_metadata{"tags":["making"],"app":"steemit/0.1"}
created2018-07-11 07:50:06
last_update2018-07-11 07:50:06
depth1
children1
last_payout2018-07-18 07:50:06
cashout_time1969-12-31 23:59:59
total_payout_value0.074 HBD
curator_payout_value0.025 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length305
author_reputation334,852,361,464
root_title"Modify STL CAD files for CNC or 3D Print using Python Programming"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id64,256,089
net_rshares48,345,696,109
author_curate_reward""
vote details (1)
@makerhacks ·
It would work under Windows, you would just need to check the command line part works the same or needs the full path I think? :)
properties (22)
authormakerhacks
permlinkre-hawk399-re-makerhacks-modifystlcadfilesforcncor3dprintusingpythonprogramming-mtbpntdw0k-20180711t155708645z
categorymaking
json_metadata{"tags":["making"],"app":"steemit/0.1"}
created2018-07-11 15:57:09
last_update2018-07-11 15:57:09
depth2
children0
last_payout2018-07-18 15:57:09
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length129
author_reputation156,977,359,570,955
root_title"Modify STL CAD files for CNC or 3D Print using Python Programming"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id64,304,568
net_rshares0
@steemmakers ·
<div class='pull-right'><center><a href='http://www.steemmakers.com'><img src='https://www.steemmakers.com/img/comment_logo_makers.png' /></a></center></div><b>Congratulations</b> This post has been upvoted by SteemMakers. We are a community-based project that aims to support makers and DIYers on the blockchain in every way possible. <br/><br/>Join our <a href='https://discord.gg/EFGbRuW'>Discord Channel</a> to connect with us and nominate your own or somebody else's posts in our review channel.<br/><br/><b>Help us to reward you for making it !</b> Join <a href='https://www.steemmakers.com/#/Trail'>our voting trail</a> or <a href='https://www.steemmakers.com/#/Delegation'>delegate steem power</a> to the community account. <br/><br/>Your post is also presented on the community website <a href='http://www.steemmakers.com'>www.steemmakers.com</a> where you can find other selected content. <br/><br/>If you like our work, please consider upvoting this comment to support the growth of our community. Thank you.
properties (22)
authorsteemmakers
permlinkre-makerhacks-modifystlcadfilesforcncor3dprintusingpythonprogramming-mtbpntdw0k-20180711t073638906z
categorymaking
json_metadata""
created2018-07-11 07:36:36
last_update2018-07-11 07:36:36
depth1
children0
last_payout2018-07-18 07:36:36
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1,019
author_reputation1,907,312,584,548
root_title"Modify STL CAD files for CNC or 3D Print using Python Programming"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id64,255,006
net_rshares0