2013-11-27 01:49:27 +05:30
|
|
|
#!/usr/bin/env python
|
2014-04-02 00:11:14 +05:30
|
|
|
# -*- coding: UTF-8 no BOM -*-
|
2013-11-27 01:49:27 +05:30
|
|
|
|
2014-12-19 00:56:52 +05:30
|
|
|
import os,sys,string,re,vtk
|
2013-11-27 01:49:27 +05:30
|
|
|
import damask
|
2014-12-19 00:56:52 +05:30
|
|
|
from optparse import OptionParser
|
|
|
|
|
|
|
|
scriptID = string.replace('$Id$','\n','\\n')
|
|
|
|
scriptName = os.path.splitext(scriptID.split()[1])[0]
|
|
|
|
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# MAIN
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
|
|
|
|
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [file[s]]', description = """
|
2015-05-21 05:43:50 +05:30
|
|
|
Produce a VTK point cloud dataset based on coordinates given in an ASCIItable.
|
2014-12-19 00:56:52 +05:30
|
|
|
|
|
|
|
""", version = scriptID)
|
2013-11-27 01:49:27 +05:30
|
|
|
|
2015-08-24 19:12:29 +05:30
|
|
|
parser.add_option('-p','--position',
|
|
|
|
dest = 'position',
|
|
|
|
type = 'string', metavar = 'string',
|
|
|
|
help = 'column label for coordinates [%default]')
|
2013-11-27 01:49:27 +05:30
|
|
|
|
2015-08-24 19:12:29 +05:30
|
|
|
parser.set_defaults(position = 'pos',
|
|
|
|
)
|
2013-11-27 01:49:27 +05:30
|
|
|
|
2015-08-24 19:12:29 +05:30
|
|
|
(options, filenames) = parser.parse_args()
|
2013-11-27 01:49:27 +05:30
|
|
|
|
2015-08-24 19:12:29 +05:30
|
|
|
# --- loop over input files -------------------------------------------------------------------------
|
2013-11-27 01:49:27 +05:30
|
|
|
|
2015-08-24 19:12:29 +05:30
|
|
|
if filenames == []: filenames = [None]
|
2013-11-27 01:49:27 +05:30
|
|
|
|
|
|
|
for name in filenames:
|
2015-08-24 19:12:29 +05:30
|
|
|
try:
|
|
|
|
table = damask.ASCIItable(name = name,
|
|
|
|
buffered = False, readonly = True)
|
|
|
|
except: continue
|
|
|
|
table.croak(damask.util.emph(scriptName)+(': '+name if name else ''))
|
|
|
|
|
|
|
|
# --- interpret header ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
table.head_read()
|
|
|
|
|
|
|
|
errors = []
|
|
|
|
remarks = []
|
|
|
|
if table.label_dimension(options.position) != 3: errors.append('columns "{}" have dimension {}'.format(options.position,
|
|
|
|
table.label_dimension(options.position)))
|
|
|
|
if remarks != []: table.croak(remarks)
|
|
|
|
if errors != []:
|
|
|
|
table.croak(errors)
|
|
|
|
table.close(dismiss=True)
|
|
|
|
continue
|
2013-11-27 01:49:27 +05:30
|
|
|
|
|
|
|
# ------------------------------------------ process data ---------------------------------------
|
|
|
|
|
2015-08-24 19:12:29 +05:30
|
|
|
table.data_readArray(options.position)
|
|
|
|
|
2013-11-27 01:49:27 +05:30
|
|
|
Polydata = vtk.vtkPolyData()
|
|
|
|
Points = vtk.vtkPoints()
|
|
|
|
Vertices = vtk.vtkCellArray()
|
|
|
|
|
2014-11-18 20:52:00 +05:30
|
|
|
for p in table.data:
|
2013-11-27 01:49:27 +05:30
|
|
|
id = Points.InsertNextPoint(p)
|
|
|
|
Vertices.InsertNextCell(1)
|
|
|
|
Vertices.InsertCellPoint(id)
|
|
|
|
|
|
|
|
Polydata.SetPoints(Points)
|
|
|
|
Polydata.SetVerts(Vertices)
|
|
|
|
Polydata.Modified()
|
2015-08-24 19:12:29 +05:30
|
|
|
if vtk.VTK_MAJOR_VERSION <= 5: Polydata.Update()
|
2013-11-27 01:49:27 +05:30
|
|
|
|
|
|
|
# ------------------------------------------ output result ---------------------------------------
|
|
|
|
|
2015-08-24 19:12:29 +05:30
|
|
|
if name:
|
|
|
|
(dir,filename) = os.path.split(name)
|
|
|
|
writer = vtk.vtkXMLPolyDataWriter()
|
|
|
|
writer.SetDataModeToBinary()
|
|
|
|
writer.SetCompressorTypeToZLib()
|
|
|
|
writer.SetFileName(os.path.join(dir,os.path.splitext(filename)[0]+'_{}'.format(options.position) \
|
|
|
|
+'.'+writer.GetDefaultFileExtension()))
|
|
|
|
if vtk.VTK_MAJOR_VERSION <= 5: writer.SetInput(Polydata)
|
|
|
|
else: writer.SetInputData(Polydata)
|
|
|
|
writer.Write()
|
2013-11-27 01:49:27 +05:30
|
|
|
|
2015-08-24 19:12:29 +05:30
|
|
|
else:
|
|
|
|
writer = vtk.vtkPolyDataWriter()
|
|
|
|
writer.WriteToOutputStringOn()
|
|
|
|
writer.SetFileTypeToASCII()
|
|
|
|
writer.SetHeader('# powered by '+scriptID)
|
|
|
|
if vtk.VTK_MAJOR_VERSION <= 5: writer.SetInput(Polydata)
|
|
|
|
else: writer.SetInputData(Polydata)
|
|
|
|
writer.Write()
|
|
|
|
sys.stdout.write(writer.GetOutputString()[0:writer.GetOutputStringLength()])
|
|
|
|
|
|
|
|
table.close() # close input ASCII table
|