Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mostieri/export geometry utils interface #328

Merged
merged 5 commits into from
Oct 17, 2023
Merged
Changes from 3 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
99 changes: 97 additions & 2 deletions src/ansys/pyensight/core/utils/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,6 @@ def image(
) -> None:
"""Render an image of the current EnSight scene.

This method returns a PIL image object.

Parameters
----------
filename : str
Expand Down Expand Up @@ -464,3 +462,100 @@ def _animation_remote(
mp4_data = fp.read()

return mp4_data

GEOM_EXPORT_GLTF = "gltf2"
GEOM_EXPORT_AVZ = "avz"
GEOM_EXPORT_PLY = "ply"
GEOM_EXPORT_STL = "stl"

extension_map = {
GEOM_EXPORT_GLTF: ".glb",
GEOM_EXPORT_AVZ: ".avz",
GEOM_EXPORT_PLY: ".ply",
GEOM_EXPORT_STL: ".stl",
}

def _geometry_remote(
self, format: str, begin_timestep: int = 0, end_timestep: int = 0, delta_timestep: int = 1
) -> bytes:
"""EnSight-side implementation.

Parameters
----------
format : str
The format to export
begin_timestep: int
mariostieriansys marked this conversation as resolved.
Show resolved Hide resolved
The first timestep to export
end_timestep: int
The final timestep to export
delta_timestep: int
The delta timestep to use when exporting

Returns
-------
bytes
Geometry export in bytes
"""
rawdata = None
extension = self.extension_map.get(format)
if not extension:
raise RuntimeError("The geometry export format provided is not supported.")
with tempfile.TemporaryDirectory() as tmpdirname:
self._ensight.objs.core.PARTS.set_attr("SELECTED", True)
mariostieriansys marked this conversation as resolved.
Show resolved Hide resolved
self._ensight.savegeom.format(format)
self._ensight.savegeom.begin_step(begin_timestep)
self._ensight.savegeom.end_step(end_timestep)
self._ensight.savegeom.step_by(delta_timestep)
tmpfilename = os.path.join(tmpdirname, str(uuid.uuid1()))
self._ensight.savegeom.save_geometric_entities(tmpfilename)
with open(tmpfilename + extension, "rb") as tmpfile:
rawdata = tmpfile.read()
return rawdata

def geometry(
self,
filename: str,
format: str = GEOM_EXPORT_GLTF,
begin_timestep: int = 0,
end_timestep: int = 0,
delta_timestep: int = 1,
) -> None:
"""Export a geometry file.

Parameters
----------
filename: str
mariostieriansys marked this conversation as resolved.
Show resolved Hide resolved
The location where to export the geometry. This is on the PyEnSight system
format : str
The format to export
begin_timestep: int
The first timestep to export
end_timestep: int
The final timestep to export
delta_timestep: int
The delta timestep to use when exporting

Examples
--------
>>> s = LocalLauncher().start()
>>> data = f"{s.cei_home}/ensight{s.cei_suffix}gui/demos/Crash Queries.ens"
>>> s.ensight.objs.ensxml_restore_file(data)
>>> s.ensight.utils.export.geometry("local_file.glb", format=s.ensight.utils.export.GEOM_EXPORT_GLTF)
"""
self._remote_support_check()
raw_data = None
if isinstance(self._ensight, ModuleType):
raw_data = self._geometry_remote(
format,
begin_timestep=begin_timestep,
end_timestep=end_timestep,
delta_timestep=delta_timestep,
)
else:
cmd = f"ensight.utils.export._geometry_remote('{format}', {begin_timestep}, {end_timestep}, {delta_timestep})"
raw_data = self._ensight._session.cmd(cmd)
if raw_data:
with open(filename, "wb") as fp:
fp.write(raw_data)
else:
raise IOError("Export was not successful")
Loading