|
1 | 1 | """``Design`` class module."""
|
2 | 2 |
|
| 3 | +from enum import Enum |
3 | 4 | from pathlib import Path
|
4 | 5 | from typing import List, Optional, Union
|
5 | 6 |
|
|
30 | 31 | from ansys.geometry.core.misc import check_type
|
31 | 32 |
|
32 | 33 |
|
| 34 | +class DesignFileFormat(Enum): |
| 35 | + """Available file formats supported by the Design class for download.""" |
| 36 | + |
| 37 | + SCDOCX = "SCDOCX", None |
| 38 | + PARASOLID_TEXT = "PARASOLID_TEXT", PartExportFormat.PARTEXPORTFORMAT_PARASOLID_TEXT |
| 39 | + PARASOLID_BIN = "PARASOLID_BIN", PartExportFormat.PARTEXPORTFORMAT_PARASOLID_BINARY |
| 40 | + INVALID = "INVALID", None |
| 41 | + |
| 42 | + |
33 | 43 | class Design(Component):
|
34 | 44 | """
|
35 | 45 | Provides a ``Design`` for organizing geometry assemblies.
|
@@ -125,72 +135,62 @@ def save(self, file_location: Union[Path, str]) -> None:
|
125 | 135 | self._grpc_client.log.debug(f"Design successfully saved at location {file_location}.")
|
126 | 136 |
|
127 | 137 | @protect_grpc
|
128 |
| - def download(self, file_location: Union[Path, str], as_stream: Optional[bool] = False) -> None: |
| 138 | + def download( |
| 139 | + self, |
| 140 | + file_location: Union[Path, str], |
| 141 | + format: Optional[DesignFileFormat] = DesignFileFormat.SCDOCX, |
| 142 | + as_stream: Optional[bool] = False, |
| 143 | + ) -> None: |
129 | 144 | """Downloads a design from the active geometry server instance.
|
130 | 145 |
|
131 | 146 | Parameters
|
132 | 147 | ----------
|
133 | 148 | file_location : Union[Path, str]
|
134 | 149 | Location on disk where the file should be saved.
|
135 |
| - as_stream : bool, Default: False |
136 |
| - Boolean indicating whether to use the gRPC stream functionality. |
| 150 | + format : Optional[DesignFileFormat] |
| 151 | + Format in which to export the design. By default, as an ``SCDOCX`` file. |
| 152 | + as_stream : bool, default: False |
| 153 | + Boolean indicating whether to use the gRPC stream functionality (if possible). |
137 | 154 | or the single message approach. By default, ``False``
|
138 | 155 | """
|
139 | 156 | # Sanity checks on inputs
|
140 | 157 | if isinstance(file_location, Path):
|
141 | 158 | file_location = str(file_location)
|
142 | 159 | check_type(file_location, str)
|
| 160 | + check_type(format, DesignFileFormat) |
| 161 | + check_type(as_stream, bool) |
143 | 162 |
|
144 | 163 | # Process response (as stream or single file)
|
| 164 | + stream_msg = f"Downloading design in {format.value[0]} format using stream mechanism." |
| 165 | + single_msg = ( |
| 166 | + f"Downloading design in {format.value[0]} format using single-message mechanism." |
| 167 | + ) |
145 | 168 | received_bytes = bytes()
|
146 |
| - if as_stream: |
147 |
| - self._grpc_client.log.debug("Downloading design using stream mechanism.") |
148 |
| - response_iterator = self._commands_stub.DownloadFileStream(Empty()) |
149 |
| - for response in response_iterator: |
150 |
| - received_bytes += response.chunk |
151 |
| - else: |
152 |
| - self._grpc_client.log.debug("Downloading design using single-message mechanism.") |
153 |
| - response = self._commands_stub.DownloadFile(Empty()) |
| 169 | + if format is DesignFileFormat.SCDOCX: |
| 170 | + if as_stream: |
| 171 | + self._grpc_client.log.debug(stream_msg) |
| 172 | + response_iterator = self._commands_stub.DownloadFileStream(Empty()) |
| 173 | + for response in response_iterator: |
| 174 | + received_bytes += response.chunk |
| 175 | + else: |
| 176 | + self._grpc_client.log.debug(single_msg) |
| 177 | + response = self._commands_stub.DownloadFile(Empty()) |
| 178 | + received_bytes += response.data |
| 179 | + elif (format is DesignFileFormat.PARASOLID_TEXT) or ( |
| 180 | + format is DesignFileFormat.PARASOLID_BIN |
| 181 | + ): |
| 182 | + if as_stream: |
| 183 | + self._grpc_client.log.warning( |
| 184 | + "Streaming mechanism not supported for Parasolid format." |
| 185 | + ) |
| 186 | + self._grpc_client.log.debug(single_msg) |
| 187 | + response = self._design_stub.ExportDesign(ExportDesignRequest(format=format.value[1])) |
154 | 188 | received_bytes += response.data
|
155 |
| - |
156 |
| - # Write to file |
157 |
| - downloaded_file = open(file_location, "wb") |
158 |
| - downloaded_file.write(received_bytes) |
159 |
| - downloaded_file.close() |
160 |
| - |
161 |
| - self._grpc_client.log.debug(f"Design successfully downloaded at location {file_location}.") |
162 |
| - |
163 |
| - @protect_grpc |
164 |
| - def export_parasolid( |
165 |
| - self, file_location: Union[Path, str], export_text_format: Optional[bool] = False |
166 |
| - ) -> None: |
167 |
| - """Downloads a design in parasolid format from the active geometry server instance. |
168 |
| -
|
169 |
| - Parameters |
170 |
| - ---------- |
171 |
| - file_location : Union[Path, str] |
172 |
| - Location on disk where the file should be saved. |
173 |
| - export_text_format : bool, default: False |
174 |
| - Boolean indicating whether to request in binary format or in text format. |
175 |
| - ``True`` will request text format, ``False`` will request binary format. |
176 |
| - By default, ``False``. |
177 |
| - """ |
178 |
| - if isinstance(file_location, Path): |
179 |
| - file_location = str(file_location) |
180 |
| - check_type(file_location, str) |
181 |
| - |
182 |
| - self._grpc_client.log.debug("Downloading design parasolid using single-message mechanism.") |
183 |
| - |
184 |
| - # Process response as single file |
185 |
| - received_bytes = bytes() |
186 |
| - response = self._design_stub.ExportDesign( |
187 |
| - ExportDesignRequest( |
188 |
| - format=PartExportFormat.PARTEXPORTFORMAT_PARASOLID_TEXT |
189 |
| - if export_text_format |
190 |
| - else PartExportFormat.PARTEXPORTFORMAT_PARASOLID_BINARY |
| 189 | + else: |
| 190 | + self._grpc_client.log.warning( |
| 191 | + f"{format.value[0]} format requested not supported. Ignoring download request." |
191 | 192 | )
|
192 |
| - ) |
193 |
| - received_bytes += response.data |
| 193 | + return |
194 | 194 |
|
195 | 195 | # Write to file
|
196 | 196 | downloaded_file = open(file_location, "wb")
|
|
0 commit comments