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

miguelmarco commented 9 years ago
comment:42

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.

vbraun commented 9 years ago

Changed branch from u/chapoton/7744 to 09cba3c