diff --git a/coxeter/io.py b/coxeter/io.py index 48a54daf..6a902c19 100644 --- a/coxeter/io.py +++ b/coxeter/io.py @@ -45,8 +45,13 @@ def to_obj(shape, filename): content += "\n" - for f in shape.faces: - content += f"f {' '.join([str(v_index + 1) for v_index in f])}\n" + if shape.__getattribute__("faces"): + for f in shape.faces: + content += f"f {' '.join([str(v_index + 1) for v_index in f])}\n" + elif shape.__getattribute__("num_vertices"): # Pack all vertices into one facet + content += f"f {' '.join([str(i + 1) for i in range(shape.num_vertices)])}\n" + else: + raise TypeError(f"Unsupported shape type `{type(shape)}` was provided.") content = content[:-1] diff --git a/coxeter/shapes/polygon.py b/coxeter/shapes/polygon.py index f59dc328..ffe80614 100644 --- a/coxeter/shapes/polygon.py +++ b/coxeter/shapes/polygon.py @@ -838,3 +838,22 @@ def to_hoomd(self): self.centroid = old_centroid return hoomd_dict + + def save(self, filename): + """Save the polyhedron object to a file using methods from ``coxeter.io``. + + Args: + filename (str, pathlib.Path, or os.PathLike): + The name or path of the output file, including the extension which must + be one of the following: .obj, .off, .stl, .ply, .vtk, .x3d, or .html + + Raises + ------ + ValueError: If the file type does not match one of the valid outputs. + OSError: If open() encounters a problem. + """ + filetype = Path(filename).suffix.lstrip(".").capitalize() + if filetype == "OBJ": + io.to_obj(self, filename) + else: + raise ValueError("filetype must be one of the following: .obj") diff --git a/coxeter/shapes/polyhedron.py b/coxeter/shapes/polyhedron.py index 1fdb7b3c..2c9fb8f2 100644 --- a/coxeter/shapes/polyhedron.py +++ b/coxeter/shapes/polyhedron.py @@ -5,6 +5,7 @@ import warnings from functools import cached_property +from pathlib import Path import numpy as np import rowan @@ -1022,22 +1023,20 @@ def to_hoomd(self): self.centroid = old_centroid return hoomd_dict - def save(self, filetype, filename): + def save(self, filename): """Save the polyhedron object to a file using methods from ``coxeter.io``. Args: - filetype (str): - The file format to export polyhedron to. Must be one of the following: - OBJ, OFF, STL, PLY, VTK, X3D, HTML. - filename (str, pathlib.Path, or os.PathLike): - The name or path of the output file, including the extension. + The name or path of the output file, including the extension which must + be one of the following: .obj, .off, .stl, .ply, .vtk, .x3d, or .html Raises ------ - ValueError: If filetype is not one of the required strings. + ValueError: If the file type does not match one of the valid outputs. OSError: If open() encounters a problem. """ + filetype = Path(filename).suffix.lstrip(".").capitalize() if filetype == "OBJ": io.to_obj(self, filename) elif filetype == "OFF": @@ -1054,6 +1053,6 @@ def save(self, filetype, filename): io.to_html(self, filename) else: raise ValueError( - "filetype must be one of the following: OBJ, OFF, " - "STL, PLY, VTK, X3D, HTML" + "filetype must be one of the following: .obj, .off, .stl, .ply, .vtk, " + ".x3d, or .html" )