Opened 13 years ago

Closed 7 years ago

# STL, AMF, X3D and PLY export for 3D printing

Reported by: Owned by: colah colah major sage-6.8 graphics 3D-Printing, STL, X3D niles, kcrisman, jdemeyer, tmonteil Christopher Olah, Frédéric Chapoton Miguel Marco N/A 09cba3c 09cba3c60aa2a949b33daffa25fde025fa2440f0

### 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.

(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 open-source
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^2-9, [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
```

### comment:1 Changed 13 years ago by colah

• Summary changed from STL Export -- Allows for 3d printing of surfaces to STL Export

### comment:2 Changed 9 years ago by jdemeyer

• Milestone changed from sage-5.11 to sage-5.12

### comment:3 Changed 9 years ago by vbraun_spam

• Milestone changed from sage-6.1 to sage-6.2

### comment:4 Changed 8 years ago by vbraun_spam

• Milestone changed from sage-6.2 to sage-6.3

### comment:5 Changed 8 years ago by chapoton

• Authors changed from colah to colah, Frédéric Chapoton
• Branch set to u/chapoton/7744
• Commit set to 37a9d058e6ded668003654a7cf13e07f75fb73e7
• Status changed from new to needs_review

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:

 ​37a9d05 `trac #7744 code for producing STL files for surfaces in sagemath`
Last edited 8 years ago by chapoton (previous) (diff)

### comment:6 Changed 8 years ago by chapoton

• Authors changed from colah, Frédéric Chapoton to Christopher Olah, Frédéric Chapoton

### comment:7 Changed 8 years ago by git

• 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 8 years ago by chapoton

• 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" xml-based 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..

Last edited 8 years ago by chapoton (previous) (diff)

### comment:9 Changed 8 years ago by git

• 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 8 years ago by git

• 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 8 years ago by chapoton

• Summary changed from STL and AMF export for 3D printing to STL, AMF, X3D and PLY export for 3D printing

### comment:13 Changed 8 years ago by chapoton

Maybe one could add the json format ?

### comment:14 Changed 8 years ago by git

• 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 8 years ago by chapoton

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 8 years ago by git

• Commit changed from 56a75e71e77ca5cf6ff8f107473edfbb6fedf532 to b8f41feb79a6bd569a60a2f6a54a59aca995a9de

Branch pushed to git repo; I updated commit sha1. New commits:

 ​58a76d8 `Merge branch 'u/chapoton/7744' of ssh://trac.sagemath.org:22/sage into 7744` ​b8f41fe `trac #7744 fixing doctest`

### comment:17 Changed 8 years ago by vbraun_spam

• Milestone changed from sage-6.3 to sage-6.4

### comment:18 Changed 8 years ago by git

• 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 8 years ago by chapoton

patchbot has turned green ! time for a review, anybody ?

### comment:20 Changed 8 years ago by mmarco

I will take a look at it next week, when i will have my 3d printer available to check the output.

### comment:21 Changed 8 years ago by chapoton

Hello ! have you checked that the output is accepted by your 3d printer ?

### comment:22 Changed 8 years ago by mmarco

Yes, it did work fine.

### comment:23 Changed 8 years ago by git

• Commit changed from a2c486e291ac76a9fe0346a1b50e05b383014c39 to 77e53edfe2ea22f237918867734a4d67dcb9f475

Branch pushed to git repo; I updated commit sha1. New commits:

 ​93199cb `Merge with 6.4.beta4` ​77e53ed `trac #7744 added a TODO`

### comment:24 Changed 8 years ago by git

• 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 8 years ago by git

• 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:26 Changed 8 years ago by chapoton

Hello ? This one would be useful for #12212.

### comment:27 Changed 8 years ago by kcrisman

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 8 years ago by chapoton

ping ? anybody for a review ?

### comment:29 Changed 7 years ago by git

• 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 7 years ago by chapoton

Could please someone have a look at that ? it has already started rotting..

### comment:31 Changed 7 years ago by chapoton

• Milestone changed from sage-6.4 to sage-6.6

### comment:32 Changed 7 years ago by chapoton

There are some people interested by this kind of stuff, see here : http://ask.sagemath.org/question/26075/transferring-3d-plots/

### comment:33 Changed 7 years ago by chapoton

Still nobody for a review ?

### comment:34 Changed 7 years ago by git

• 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:35 Changed 7 years ago by chapoton

• Milestone changed from sage-6.6 to sage-6.8

ping ?

### comment:36 Changed 7 years ago by chapoton

please, is there anybody to have a look ? this is not that complicated, just a few text-writing routines.

### comment:37 Changed 7 years ago by mmarco

I am looking at it.

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 7 years ago by chapoton

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 7 years ago by mmarco

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 7 years ago by chapoton

ok, I am doing that right now.

### comment:41 Changed 7 years ago by git

• Commit changed from 6b716f6f3132da41894d41e8b9e1947af7aad6b2 to 09cba3c60aa2a949b33daffa25fde025fa2440f0

Branch pushed to git repo; I updated commit sha1. New commits:

 ​f2881e3 `Merge branch 'u/chapoton/7744' of trac.sagemath.org:sage into 6.8.rc1` ​09cba3c `trac #7744 reviewer's comments`

### comment:42 Changed 7 years ago by mmarco

• 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 7 years ago by vbraun

• Branch changed from u/chapoton/7744 to 09cba3c60aa2a949b33daffa25fde025fa2440f0
• Resolution set to fixed
• Status changed from positive_review to closed
Note: See TracTickets for help on using tickets.