← 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: Open => Answered

Jan Stránský proposed the following answer:
Hi Tina,

> "Can't open file: tina.st"

it means that the file cannot be opened :-) could you check if it
exists? I put here a complete script, that works for me:

#################################################
from yade import polyhedra_utils
from yade import export, ymport
from yade import plot
from yade import pack
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)
poly.shape.wire = True
O.bodies.append(poly)
poly.state.pos=(0,0,0.025)

def polyhedra2stl(stl):
  trisCoords = []
  for b in O.bodies:
    if not isinstance(b.shape,Polyhedra):
      continue
    vs = [b.state.pos + b.state.ori*v for v in b.shape.v]
    trisIds = b.shape.GetSurfaceTriangulation()
    l = len(trisIds) / 3
    trisIds = [trisIds[3*i:3*i+3] for i in range(l)]
    for t in trisIds:
      trisCoords.append([vs[i] for i in t])
  #
  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')
  #
  with open(stl,'w') as f:
    f.writelines(l+'\n' for l in lines)

polyhedra2stl('tina.stl')

facets = ymport.stl('tina.stl')
surf = gts.Surface()
for f in facets:
  vs = [f.state.pos + f.state.ori*v for v in f.shape.vertices]
  gtsvs = [gts.Vertex(v[0],v[1],v[2]) for v in vs]
  es = [gts.Edge(gtsvs[i],gtsvs[j]) for i,j in ((0,1),(1,2),(2,0))]
  face = gts.Face(es[0],es[1],es[2])
  surf.add(face)
surf.cleanup(1e-6)
assert surf.is_closed()
pred = pack.inGtsSurface(surf)
sphs = pack.randomDensePack(pred,radius=0.001,spheresInCell=500)
O.bodies.append(sphs)
#################################################

cheers
Jan

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