Skip to content
Open
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
9 changes: 7 additions & 2 deletions coxeter/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
19 changes: 19 additions & 0 deletions coxeter/shapes/polygon.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
17 changes: 8 additions & 9 deletions coxeter/shapes/polyhedron.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import warnings
from functools import cached_property
from pathlib import Path

import numpy as np
import rowan
Expand Down Expand Up @@ -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":
Expand All @@ -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"
)
Loading