sagemath / sage

Main repository of SageMath
https://www.sagemath.org
Other
1.41k stars 475 forks source link

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

Closed 41051cdb-393d-4861-8e3e-611cc2de81d7 closed 9 years ago

41051cdb-393d-4861-8e3e-611cc2de81d7 commented 14 years ago
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 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

CC: @nilesjohnson @kcrisman @jdemeyer @sagetrac-tmonteil

Component: graphics

Keywords: 3D-Printing, STL, X3D

Author: Christopher Olah, Frédéric Chapoton

Branch/Commit: 09cba3c

Reviewer: Miguel Marco

Issue created by migration from https://trac.sagemath.org/ticket/7744

fchapoton commented 10 years ago

Changed author from colah to colah, Frédéric Chapoton

fchapoton commented 10 years ago

Branch: u/chapoton/7744

fchapoton commented 10 years ago
comment:5

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:

37a9d05trac #7744 code for producing STL files for surfaces in sagemath
fchapoton commented 10 years ago

Commit: 37a9d05

fchapoton commented 10 years ago

Changed author from colah, Frédéric Chapoton to Christopher Olah, Frédéric Chapoton

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 10 years ago

Changed commit from 37a9d05 to 20ba42b

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 10 years ago

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

20ba42btrac #7744 add AMF export
fchapoton commented 10 years ago
comment:8

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

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 10 years ago

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

63b6ec9trac #7744 more formats one can save (and x3d is the best)
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 10 years ago

Changed commit from 20ba42b to 63b6ec9

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 10 years ago

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

2a11337trac #7744 added the PLY file format
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 10 years ago

Changed commit from 63b6ec9 to 2a11337

fchapoton commented 10 years ago
comment:13

Maybe one could add the json format ?

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 10 years ago

Changed commit from 2a11337 to 56a75e7

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 10 years ago

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

56a75e7trac #7744 minor details, more doc
fchapoton commented 10 years ago
comment:15

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.

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 10 years ago

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

58a76d8Merge branch 'u/chapoton/7744' of ssh://trac.sagemath.org:22/sage into 7744
b8f41fetrac #7744 fixing doctest
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 10 years ago

Changed commit from 56a75e7 to b8f41fe

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 10 years ago

Changed commit from b8f41fe to a2c486e

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 10 years ago

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

a2c486eMerge with 6.4.beta0
fchapoton commented 10 years ago
comment:19

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

miguelmarco commented 10 years ago
comment:20

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

fchapoton commented 10 years ago

Changed keywords from 3D-Printing STL to 3D-Printing, STL, X3D

fchapoton commented 10 years ago
comment:21

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

miguelmarco commented 10 years ago
comment:22

Yes, it did work fine.

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 10 years ago

Changed commit from a2c486e to 77e53ed

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 10 years ago

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

93199cbMerge with 6.4.beta4
77e53edtrac #7744 added a TODO
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 10 years ago

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

d43d75ctrac #7744 minor details
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 10 years ago

Changed commit from 77e53ed to d43d75c

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 10 years ago

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

1c7510bMerge branch 'u/chapoton/7744' of trac.sagemath.org:sage into 6.4.b6
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 10 years ago

Changed commit from d43d75c to 1c7510b

fchapoton commented 9 years ago
comment:26

Hello ? This one would be useful for #12212.

kcrisman commented 9 years ago
comment:27

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.

fchapoton commented 9 years ago
comment:28

ping ? anybody for a review ?

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 9 years ago

Changed commit from 1c7510b to 94a4385

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 9 years ago

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

94a4385Merge branch 'u/chapoton/7744' into 6.6.b3
fchapoton commented 9 years ago
comment:30

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

fchapoton commented 9 years ago
comment:32

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

fchapoton commented 9 years ago
comment:33

Still nobody for a review ?

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 9 years ago

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

6b716f6Merge branch 'u/chapoton/7744' of trac.sagemath.org:sage into 7744
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 9 years ago

Changed commit from 94a4385 to 6b716f6

fchapoton commented 9 years ago
comment:35

ping ?

fchapoton commented 9 years ago
comment:36

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

miguelmarco commented 9 years ago
comment:37

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.

fchapoton commented 9 years ago
comment:38

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.

miguelmarco commented 9 years ago
comment:39

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.

fchapoton commented 9 years ago
comment:40

ok, I am doing that right now.

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 9 years ago

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

f2881e3Merge branch 'u/chapoton/7744' of trac.sagemath.org:sage into 6.8.rc1
09cba3ctrac #7744 reviewer's comments
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 9 years ago

Changed commit from 6b716f6 to 09cba3c

miguelmarco commented 9 years ago

Reviewer: Miguel Marco