from vtk import *
from gengeo import *
from TriMeshTools import *
from sys import argv

def stl2vtk (inputfile="test.stl"):
   reader = vtkSTLReader()
   reader.SetFileName (inputfile)
   reader.Update()

   thePoly = vtkPolyData()
   thePoly.ShallowCopy(reader.GetOutput())

   wi = vtkPolyDataWriter()
   wi.SetInput(thePoly)
   wi.SetFileName(inputfile.split('.')[0]+'.vtk')
   wi.Write ()

   """
   wo = vtkSTLWriter()
   wo.SetInputData(thePoly)
   wo.SetFileName(inputfile.split('.')[0]+'_extruded.stl')
   wo.Write()
   """

def readstl(inputfile="test.stl"):
   reader = vtkSTLReader()
   reader.SetFileName (inputfile)
   reader.Update()

   thePoly = vtkPolyData()
   thePoly.ShallowCopy(reader.GetOutput())

   pts = []
   for i in range (thePoly.GetNumberOfPoints()):
      p = thePoly.GetPoint(i)
      pts.append(Vector3(p[0],p[1],p[2]))

   tris = []
   for i in range (thePoly.GetNumberOfPolys()):
      t = thePoly.GetCell(i)
      id0 = t.GetPointIds().GetId(0)
      id1 = t.GetPointIds().GetId(1)
      id2 = t.GetPointIds().GetId(2)
      tris.append([id0,id1,id2,2])

   bounds = thePoly.GetBounds()
   minPt = Vector3(bounds[0],bounds[2],bounds[4])
   maxPt = Vector3(bounds[1],bounds[3],bounds[5])
   bbox = [minPt,maxPt]

   return pts,tris,bbox

def stl2TriPatchSet(infile="test.stl"):
   pts,tris,bbox = readstl(infile)
   tripatchset = TriPatchSet()
   for tri in tris:
       p1 = pts[tri[0]]
       p2 = pts[tri[1]]
       p3 = pts[tri[2]]
       tag = tri[3]
       tripatchset.addTriangle(p1,p2,p3,tag)

   return tripatchset,bbox

if __name__ == "__main__":
   if (len(argv)>1): 
      stl2vtk(argv[1])
      pts,tris,bbox = readstl(argv[1])
      writeTriMeshFile(pts,tris,outputfile=argv[1].split('.')[0]+'.msh')
   else:
      stl2vtk()
      pts,tris,bbox = readstl()
      writeTriMeshFile(pts,tris,outputfile='test.msh')

GlossyBlue theme adapted by David Gilbert
Powered by PmWiki
www.000webhost.com