Opened 12 years ago
Closed 6 years ago
#7744 closed enhancement (fixed)
STL, AMF, X3D and PLY export for 3D printing
Reported by:  colah  Owned by:  colah 

Priority:  major  Milestone:  sage6.8 
Component:  graphics  Keywords:  3DPrinting, STL, X3D 
Cc:  niles, kcrisman, jdemeyer, tmonteil  Merged in:  
Authors:  Christopher Olah, Frédéric Chapoton  Reviewers:  Miguel Marco 
Report Upstream:  N/A  Work issues:  
Branch:  09cba3c (Commits, GitHub, GitLab)  Commit:  09cba3c60aa2a949b33daffa25fde025fa2440f0 
Dependencies:  Stopgaps: 
Description
def surface_to_stl(surface): """ Return an STL representation of the surface. INPUT:  `surface`  any surface, eg. output of a 3d plot function. OUTPUT: A string that represents the surface in the STL format. COMMENTS: (1) You must view the surface before plotting it. Otherwise, this will not work. (2) In order to do 3d printing with this, you will need to convert it into gcode. Skeinforge is an opensource program that can do this. (3) The size of the surface is not normalized in export. Sage's units will become the units of the STL description. These seem to be ~0.05 cm (at least when printed using skeinforge > replicatorg > hacklab.to's cupcake). (4) Be aware of the overhang limits of your 3d printer; most printers can only handle an overhang of Pi/2 (45*) before your model will start drooping. EXAMPLES: sage: x,y,z = var('x,y,z') sage: a = implicit_plot3d(x^2+y^2+z^29, [x,5,5], [y,5,5],[z,5,5]) sage: a sage: f=file.open("foo.stl",'w') sage: f.write(surface_to_stl(a)) sage: f.close() """ out = "solid mathsurface\n" for i in surface.face_list(): n = ( i[1][1]*i[2][2]  i[2][1]*i[1][2], i[1][2]*i[2][0]  i[1][0]*i[2] 2], i[1][0]*i[2][1]  i[2][0]*i[1][1] ) abs = (n[0]^2+n[1]^2+n[2]^2)^0.5 n= (n[0]/abs,n[1]/abs,n[2]/abs) out += " facet normal " + repr(n[0]) + " " + repr(n[1]) + " " + repr(n[2]) out += " outer loop\n" out += " vertex " + repr(i[0][0]) + " " + repr(i[0][1]) + " " + repr(i[0][2]) + "\n" out += " vertex " + repr(i[1][0]) + " " + repr(i[1][1]) + " " + repr(i[1][2]) + "\n" out += " vertex " + repr(i[2][0]) + " " + repr(i[2][1]) + " " + repr(i[2][2]) + "\n" out += " endloop\n" out += " endfacet\n" out += "endsolid surface\n" return out
Change History (43)
comment:1 Changed 12 years ago by
 Summary changed from STL Export  Allows for 3d printing of surfaces to STL Export
comment:2 Changed 8 years ago by
 Milestone changed from sage5.11 to sage5.12
comment:3 Changed 7 years ago by
 Milestone changed from sage6.1 to sage6.2
comment:4 Changed 7 years ago by
 Milestone changed from sage6.2 to sage6.3
comment:5 Changed 7 years ago by
 Branch set to u/chapoton/7744
 Commit set to 37a9d058e6ded668003654a7cf13e07f75fb73e7
 Status changed from new to needs_review
comment:6 Changed 7 years ago by
comment:7 Changed 7 years ago by
 Commit changed from 37a9d058e6ded668003654a7cf13e07f75fb73e7 to 20ba42b7811e9afcddec309290f0c767006c91df
Branch pushed to git repo; I updated commit sha1. New commits:
20ba42b  trac #7744 add AMF export

comment:8 Changed 7 years ago by
 Summary changed from STL Export to STL and AMF export for 3D printing
I have tested the STL export by loading the result in FreeCAD, and it works. I have now added the "more modern" xmlbased AMF format, but not tested that it works sor far, as FreeCAD is not able to load that file format.
It is necessary to compress the AMF file with ZIP..
comment:9 Changed 7 years ago by
 Commit changed from 20ba42b7811e9afcddec309290f0c767006c91df to 63b6ec9d7cde9dbc22a007692749ebb1d7fc74b4
Branch pushed to git repo; I updated commit sha1. New commits:
63b6ec9  trac #7744 more formats one can save (and x3d is the best)

comment:10 Changed 7 years ago by
 Commit changed from 63b6ec9d7cde9dbc22a007692749ebb1d7fc74b4 to 2a113378663f0a3be23536cf6476ddc40a630dd3
Branch pushed to git repo; I updated commit sha1. New commits:
2a11337  trac #7744 added the PLY file format

comment:11 Changed 7 years ago by
 Summary changed from STL and AMF export for 3D printing to STL, AMF, X3D and PLY export for 3D printing
comment:12 Changed 7 years ago by
 Cc niles added
comment:13 Changed 7 years ago by
Maybe one could add the json format ?
comment:14 Changed 7 years ago by
 Commit changed from 2a113378663f0a3be23536cf6476ddc40a630dd3 to 56a75e71e77ca5cf6ff8f107473edfbb6fedf532
Branch pushed to git repo; I updated commit sha1. New commits:
56a75e7  trac #7744 minor details, more doc

comment:15 Changed 7 years ago by
Let us forget about the json format, which is private between sage and sagenb.
I think this ticket is ready for review now. It would be very useful to have it merged for use in #12212.
comment:16 Changed 7 years ago by
 Commit changed from 56a75e71e77ca5cf6ff8f107473edfbb6fedf532 to b8f41feb79a6bd569a60a2f6a54a59aca995a9de
comment:17 Changed 7 years ago by
 Milestone changed from sage6.3 to sage6.4
comment:18 Changed 7 years ago by
 Commit changed from b8f41feb79a6bd569a60a2f6a54a59aca995a9de to a2c486e291ac76a9fe0346a1b50e05b383014c39
Branch pushed to git repo; I updated commit sha1. New commits:
a2c486e  Merge with 6.4.beta0

comment:19 Changed 7 years ago by
patchbot has turned green ! time for a review, anybody ?
comment:20 Changed 7 years ago by
I will take a look at it next week, when i will have my 3d printer available to check the output.
comment:21 Changed 7 years ago by
 Keywords X3D added
Hello ! have you checked that the output is accepted by your 3d printer ?
comment:22 Changed 7 years ago by
Yes, it did work fine.
comment:23 Changed 7 years ago by
 Commit changed from a2c486e291ac76a9fe0346a1b50e05b383014c39 to 77e53edfe2ea22f237918867734a4d67dcb9f475
comment:24 Changed 7 years ago by
 Commit changed from 77e53edfe2ea22f237918867734a4d67dcb9f475 to d43d75c8b736571bc8e7cc3dc85a29a70cbf673e
Branch pushed to git repo; I updated commit sha1. New commits:
d43d75c  trac #7744 minor details

comment:25 Changed 7 years ago by
 Commit changed from d43d75c8b736571bc8e7cc3dc85a29a70cbf673e to 1c7510bfc74cf1f5da36b036d5103ce03aa1d48b
Branch pushed to git repo; I updated commit sha1. New commits:
1c7510b  Merge branch 'u/chapoton/7744' of trac.sagemath.org:sage into 6.4.b6

comment:27 Changed 7 years ago by
I guess  this is pretty far outside my domain, anyway. The changes here are not compatible with the ones at #16640, for what it's worth, though probably are easily taken into account.
comment:28 Changed 6 years ago by
ping ? anybody for a review ?
comment:29 Changed 6 years ago by
 Commit changed from 1c7510bfc74cf1f5da36b036d5103ce03aa1d48b to 94a4385ce29df1cb5c8cbae13683595f827e30e9
Branch pushed to git repo; I updated commit sha1. New commits:
94a4385  Merge branch 'u/chapoton/7744' into 6.6.b3

comment:30 Changed 6 years ago by
 Cc jdemeyer added
Could please someone have a look at that ? it has already started rotting..
comment:31 Changed 6 years ago by
 Milestone changed from sage6.4 to sage6.6
comment:32 Changed 6 years ago by
There are some people interested by this kind of stuff, see here : http://ask.sagemath.org/question/26075/transferring3dplots/
comment:34 Changed 6 years ago by
 Commit changed from 94a4385ce29df1cb5c8cbae13683595f827e30e9 to 6b716f6f3132da41894d41e8b9e1947af7aad6b2
Branch pushed to git repo; I updated commit sha1. New commits:
6b716f6  Merge branch 'u/chapoton/7744' of trac.sagemath.org:sage into 7744

comment:36 Changed 6 years ago by
please, is there anybody to have a look ? this is not that complicated, just a few textwriting routines.
comment:37 Changed 6 years ago by
I am looking at it.
Some comments:
Would there be some way to doctest the saving feature? Maybe saving to a temporary file, then reading it, showing some part of the content and erasing.
The warning about only working for triangulated surfaces should go in a Warning section
Otherwise the code looks good, although i am having some trouble to rebase it to latest version in order to test it.
comment:38 Changed 6 years ago by
Thanks. I am available, so one can hope to make progress together.
I will wait for your review, unless you want me to make the required changes myself right now.
comment:39 Changed 6 years ago by
If you make the changes i suggested, and also rebase it to the latest development branch, i can test it and give the positive review.
comment:40 Changed 6 years ago by
ok, I am doing that right now.
comment:41 Changed 6 years ago by
 Commit changed from 6b716f6f3132da41894d41e8b9e1947af7aad6b2 to 09cba3c60aa2a949b33daffa25fde025fa2440f0
comment:42 Changed 6 years ago by
 Reviewers set to Miguel Marco
 Status changed from needs_review to positive_review
Ok, it looks good. Although i didn't have any software that could import the .amf file (.x3d, .stl and .ply import ok in Bllender).
I failed to save a sphere() object, since it is a TransformGroup? object. Maybe it would be a good idea to have support for triangulating that kind of objects, but it would be a matter for a different ticket.
comment:43 Changed 6 years ago by
 Branch changed from u/chapoton/7744 to 09cba3c60aa2a949b33daffa25fde025fa2440f0
 Resolution set to fixed
 Status changed from positive_review to closed
Useful for 3D printers, see http://www.thingiverse.com/thing:12784 for an interesting use case.
I have mostly rewritten the code for better efficiency.
Maybe this could be added in some way to the options of
.save
?And it would be much better to use the binary STL format instead.
New commits:
trac #7744 code for producing STL files for surfaces in sagemath