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

feat: add export_to functions #1147

Merged
merged 11 commits into from
Apr 19, 2024
1 change: 1 addition & 0 deletions doc/changelog.d/1147.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
feat: add ``export_to`` functions
204 changes: 199 additions & 5 deletions src/ansys/geometry/core/designer/design.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import numpy as np
from pint import Quantity, UndefinedUnitError

from ansys.geometry.core.connection.backend import BackendType
from ansys.geometry.core.connection.conversions import (
grpc_frame_to_frame,
grpc_matrix_to_matrix,
Expand Down Expand Up @@ -253,13 +254,13 @@ def download(
Format for the file to save to.
"""
# Sanity checks on inputs
if isinstance(file_location, Path):
file_location = str(file_location)
if isinstance(file_location, str):
file_location = Path(file_location)

# Check if the folder for the file location exists
if not Path(file_location).parent.exists():
if not file_location.parent.exists():
# Create the parent directory
Path(file_location).parent.mkdir(parents=True, exist_ok=True)
file_location.parent.mkdir(parents=True, exist_ok=True)

# Process response
self._grpc_client.log.debug(f"Requesting design download in {format.value[0]} format.")
Expand Down Expand Up @@ -292,6 +293,199 @@ def download(
f"Design is successfully downloaded at location {file_location}."
)

def __build_export_file_location(self, location: Union[Path, str, None], ext: str) -> Path:
"""
Build the file location for export functions.

Parameters
----------
location : Union[~pathlib.Path, str, None]
Location on disk to save the file to. If None, the file will be saved
in the current working directory.
ext : str
Extension to use for the file.

Returns
-------
~pathlib.Path
The file location for the export function.
"""
return (Path(location) if location else Path.cwd()) / f"{self.name}.{ext}"

def export_to_scdocx(self, location: Union[Path, str] = None) -> str:
"""
Export the design to an scdocx file.

Parameters
----------
location : Union[~pathlib.Path, str], optional
Location on disk to save the file to. If None, the file will be saved
in the current working directory.

Returns
-------
str
The path to the saved file.
"""
# Define the file location
file_location = self.__build_export_file_location(location, "scdocx")

# Export the design to an scdocx file
self.download(file_location, DesignFileFormat.SCDOCX)

# Return the file location
return file_location

def export_to_parasolid_text(self, location: Union[Path, str] = None) -> str:
"""
Export the design to a Parasolid text file.

Parameters
----------
location : Union[~pathlib.Path, str], optional
Location on disk to save the file to. If None, the file will be saved
in the current working directory.

Returns
-------
str
The path to the saved file.
"""
# Determine the extension based on the backend type
ext = "x_t" if self._grpc_client.backend_type == BackendType.LINUX_SERVICE else "xmt_txt"

# Define the file location
file_location = self.__build_export_file_location(location, ext)

# Export the design to a Parasolid text file
self.download(file_location, DesignFileFormat.PARASOLID_TEXT)

# Return the file location
return file_location

def export_to_parasolid_bin(self, location: Union[Path, str] = None) -> str:
"""
Export the design to a Parasolid binary file.

Parameters
----------
location : Union[~pathlib.Path, str], optional
Location on disk to save the file to. If None, the file will be saved
in the current working directory.

Returns
-------
str
The path to the saved file.
"""
# Determine the extension based on the backend type
ext = "x_b" if self._grpc_client.backend_type == BackendType.LINUX_SERVICE else "xmt_bin"

# Define the file location
file_location = self.__build_export_file_location(location, ext)

# Export the design to a Parasolid binary file
self.download(file_location, DesignFileFormat.PARASOLID_BIN)

# Return the file location
return file_location

def export_to_fmd(self, location: Union[Path, str] = None) -> str:
"""
Export the design to an FMD file.

Parameters
----------
location : Union[~pathlib.Path, str], optional
Location on disk to save the file to. If None, the file will be saved
in the current working directory.

Returns
-------
str
The path to the saved file.
"""
# Define the file location
file_location = self.__build_export_file_location(location, "fmd")

# Export the design to an FMD file
self.download(file_location, DesignFileFormat.FMD)

# Return the file location
return file_location

def export_to_step(self, location: Union[Path, str] = None) -> str:
"""
Export the design to a STEP file.

Parameters
----------
location : Union[~pathlib.Path, str], optional
Location on disk to save the file to. If None, the file will be saved
in the current working directory.

Returns
-------
str
The path to the saved file.
"""
# Define the file location
file_location = self.__build_export_file_location(location, "stp")

# Export the design to a STEP file
self.download(file_location, DesignFileFormat.STEP)

# Return the file location
return file_location

def export_to_iges(self, location: Union[Path, str] = None) -> str:
"""
Export the design to an IGES file.

Parameters
----------
location : Union[~pathlib.Path, str], optional
Location on disk to save the file to. If None, the file will be saved
in the current working directory.

Returns
-------
str
The path to the saved file.
"""
# Define the file location
file_location = self.__build_export_file_location(location, "igs")

# Export the design to an IGES file
self.download(file_location, DesignFileFormat.IGES)

# Return the file location
return file_location

def export_to_pmdb(self, location: Union[Path, str] = None) -> str:
"""
Export the design to a PMDB file.

Parameters
----------
location : Union[~pathlib.Path, str], optional
Location on disk to save the file to. If None, the file will be saved
in the current working directory.

Returns
-------
str
The path to the saved file.
"""
# Define the file location
file_location = self.__build_export_file_location(location, "pmdb")

# Export the design to a PMDB file
self.download(file_location, DesignFileFormat.PMDB)

# Return the file location
return file_location

@check_input_types
@ensure_design_is_active
def create_named_selection(
Expand Down Expand Up @@ -694,7 +888,7 @@ def __read_existing_design(self) -> None:
# Since they're the same in the backend.
self._name = design.name

response = self._commands_stub.GetAssembly(EntityIdentifier(id=""))
response = self._commands_stub.GetAssembly(EntityIdentifier(id=self._design_id))

# Store created objects
created_parts = {p.id: Part(p.id, p.name, [], []) for p in response.parts}
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def skip_if_linux(modeler: Modeler, test_name: str, element_not_available: str):
"""Skip test if running on Linux."""
if modeler.client.backend_type == BackendType.LINUX_SERVICE:
pytest.skip(
f"Skipping '{test_name}'. '{element_not_available}' not available on Linux service."
reason=f"Skipping '{test_name}'. '{element_not_available}' not on Linux service."
) # skip!


Expand Down
Loading
Loading