← Back to team overview

yade-users team mailing list archive

Re: [Question #497350]: Export a polyhedron into a *.stl or * .gts file

 

Question #497350 on Yade changed:
https://answers.launchpad.net/yade/+question/497350

    Status: Answered => Open

Tina Asia is still having a problem:
Hi,

Jan, I have tried your code, but the *.stl only contains some triangles,
My code is as follows (My YADE is the latest vision):

from yade import polyhedra_utils
from yade import export
from yade import plot
from yade import qt

gravel = PolyhedraMat()
gravel.IsSplitable = True
gravel.strength = 7.9e6
gravel.density = 2678#kg/m^3
gravel.young = 5.98e7 #Pa
gravel.poisson =0.3
gravel.frictionAngle = 0.5 #rad

#create a polyhedron
poly=polyhedra_utils.polyhedra(material=gravel,size=(0.05,0.05,0.05),seed=1)
O.bodies.append(poly)
poly.state.pos=(0,0,0.025)

def polyhedra2stl(stl): # stl is string file name, e.g. 'test.stl'
    # get triangles
    trisCoords = []
    for b in O.bodies:
        if not isinstance(b.shape,Polyhedra): # skip non-polyhedra bodies
            continue
        vs = [b.state.pos + b.state.ori*v for v in b.shape.v] # vertices in global coords
        trisIds = b.shape.GetSurfaceTriangulation() # "triangles" in one array
        l = len(trisIds) / 3
        trisIds = [trisIds[3*i:3*i+3] for i in range(l)] # triangles as array of three indices
        for t in trisIds:
            trisCoords.append([vs[i] for i in t]) # triangles as coords of its vertices
    # generate content of stl file, see [1]
    lines = ['solid polyhedra']
    for v1,v2,v3 in trisCoords:
        n = (v2-v1).cross(v3-v1)
        lines.append('facet normal {} {} {}'.format(n[0],n[1],n[2]))
        lines.append(' outer loop')
        for v in (v1,v2,v3):
            lines.append(' vertex {} {} {}'.format(v[0],v[1],v[2]))
            lines.append(' endloop')
            lines.append('endfacet')
            lines.append('endsolid polyhedra')
    # write to file
    with open(stl,'w') as f:
        f.writelines(l+'\n' for l in lines)

polyhedra2stl('tina.stl')

-- 
You received this question notification because your team yade-users is
an answer contact for Yade.