Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions pygem/stlhandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def parse(self, filename):
return mesh_points


def write(self, mesh_points, filename):
def write(self, mesh_points, filename, write_bin=False):
"""
Writes a unv file, called filename, copying all the lines from self.filename but
the coordinates. mesh_points is a matrix that contains the new coordinates to
Expand All @@ -54,6 +54,7 @@ def write(self, mesh_points, filename):
:param numpy.ndarray mesh_points: it is a `n_points`-by-3 matrix containing
the coordinates of the points of the mesh.
:param string filename: name of the output file.
:param boolean write_bin: flag to write in the binary format. Default is False.

.. todo:: DOCS
"""
Expand All @@ -72,8 +73,11 @@ def write(self, mesh_points, filename):
for i in range(0, n_triplets):
for j in range(0, 3):
data['vectors'][i][j] = mesh_points[3*i + j]

stl_mesh.save(self.outfile, mode=1, update_normals=True)

if not write_bin:
stl_mesh.save(self.outfile, mode=1, update_normals=True)
else:
stl_mesh.save(self.outfile, update_normals=True)


def plot(self, plot_file=None, save_fig=False):
Expand Down
Binary file added tests/test_datasets/test_sphere_bin.stl
Binary file not shown.
77 changes: 76 additions & 1 deletion tests/test_stlhandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,13 @@ def test_stl_parse_coords_4(self):
def test_stl_parse_coords_5(self):
stl_handler = sh.StlHandler()
mesh_points = stl_handler.parse('tests/test_datasets/test_sphere.stl')
np.testing.assert_almost_equal(mesh_points[-1][2], -39.05963898)
np.testing.assert_almost_equal(mesh_points[-1][2], -39.05963898)


def test_stl_parse_coords_5_bin(self):
stl_handler = sh.StlHandler()
mesh_points = stl_handler.parse('tests/test_datasets/test_sphere_bin.stl')
np.testing.assert_almost_equal(mesh_points[-1][2], -39.05963898)


def test_stl_write_failing_filename_type(self):
Expand Down Expand Up @@ -132,7 +138,66 @@ def test_stl_write_comparison(self):
stl_handler.write(mesh_points, outfilename)
self.assertTrue(filecmp.cmp(outfilename, outfilename_expected))
os.remove(outfilename)


def test_stl_write_binary_from_binary(self):
stl_handler = sh.StlHandler()
mesh_points = stl_handler.parse('tests/test_datasets/test_sphere_bin.stl')
mesh_points[0] = [-40.2, -20.5, 60.9]
mesh_points[1] = [-40.2, -10.5, 60.9]
mesh_points[2] = [-40.2, -10.5, 60.9]
mesh_points[2000] = [-40.2, -20.5, 60.9]
mesh_points[2001] = [-40.2, -10.5, 60.9]
mesh_points[2002] = [-40.2, -10.5, 60.9]
mesh_points[6100] = [-40.2, -20.5, 60.9]
mesh_points[6101] = [-40.2, -10.5, 60.9]
mesh_points[6102] = [-40.2, -10.5, 60.9]

outfilename = 'tests/test_datasets/test_sphere_out.stl'

stl_handler.write(mesh_points, outfilename, write_bin=True)
os.remove(outfilename)


def test_stl_write_binary_from_ascii(self):
stl_handler = sh.StlHandler()
mesh_points = stl_handler.parse('tests/test_datasets/test_sphere.stl')
mesh_points[0] = [-40.2, -20.5, 60.9]
mesh_points[1] = [-40.2, -10.5, 60.9]
mesh_points[2] = [-40.2, -10.5, 60.9]
mesh_points[2000] = [-40.2, -20.5, 60.9]
mesh_points[2001] = [-40.2, -10.5, 60.9]
mesh_points[2002] = [-40.2, -10.5, 60.9]
mesh_points[6100] = [-40.2, -20.5, 60.9]
mesh_points[6101] = [-40.2, -10.5, 60.9]
mesh_points[6102] = [-40.2, -10.5, 60.9]

outfilename = 'tests/test_datasets/test_sphere_out.stl'

stl_handler.write(mesh_points, outfilename, write_bin=True)
os.remove(outfilename)


def test_stl_write_ascii_from_binary(self):
stl_handler = sh.StlHandler()
mesh_points = stl_handler.parse('tests/test_datasets/test_sphere_bin.stl')
mesh_points[0] = [-40.2, -20.5, 60.9]
mesh_points[1] = [-40.2, -10.5, 60.9]
mesh_points[2] = [-40.2, -10.5, 60.9]
mesh_points[2000] = [-40.2, -20.5, 60.9]
mesh_points[2001] = [-40.2, -10.5, 60.9]
mesh_points[2002] = [-40.2, -10.5, 60.9]
mesh_points[6100] = [-40.2, -20.5, 60.9]
mesh_points[6101] = [-40.2, -10.5, 60.9]
mesh_points[6102] = [-40.2, -10.5, 60.9]

outfilename = 'tests/test_datasets/test_sphere_out.stl'
outfilename_expected = 'tests/test_datasets/test_sphere_out_true.stl'

stl_handler.write(mesh_points, outfilename, write_bin=False)
self.assertTrue(filecmp.cmp(outfilename, outfilename_expected))
os.remove(outfilename)


def test_stl_plot_save_fig(self):
stl_handler = sh.StlHandler()
Expand All @@ -142,6 +207,16 @@ def test_stl_plot_save_fig(self):
assert False
else:
os.remove('tests/test_datasets/test_sphere.png')


def test_stl_plot_save_fig_bin(self):
stl_handler = sh.StlHandler()
mesh_points = stl_handler.parse('tests/test_datasets/test_sphere_bin.stl')
stl_handler.plot(save_fig=True)
if not os.path.isfile('tests/test_datasets/test_sphere_bin.png'):
assert False
else:
os.remove('tests/test_datasets/test_sphere_bin.png')


def test_stl_plot_failing_outfile_type(self):
Expand Down