|
23 | 23 | from __future__ import annotations |
24 | 24 |
|
25 | 25 | from collections.abc import Sequence |
| 26 | +import pathlib |
| 27 | +import typing |
26 | 28 |
|
27 | 29 | from ansys.api.acp.v0 import section_cut_pb2, section_cut_pb2_grpc |
28 | 30 |
|
29 | 31 | from .._utils.array_conversions import to_1D_double_array, to_tuple_from_1D_array |
| 32 | +from .._utils.path_to_str import path_to_str_checked |
30 | 33 | from .._utils.property_protocols import ReadOnlyProperty, ReadWriteProperty |
| 34 | +from .._utils.typing_helper import PATH as _PATH |
| 35 | +from ._grpc_helpers.exceptions import wrap_grpc_errors |
31 | 36 | from ._grpc_helpers.linked_object_list import define_linked_object_list |
32 | 37 | from ._grpc_helpers.property_helper import ( |
33 | 38 | grpc_data_property, |
|
39 | 44 | from .enums import ( |
40 | 45 | ExtrusionType, |
41 | 46 | IntersectionType, |
| 47 | + SectionCutCDBExportType, |
42 | 48 | SectionCutType, |
43 | 49 | extrusion_type_from_pb, |
44 | 50 | extrusion_type_to_pb, |
45 | 51 | intersection_type_from_pb, |
46 | 52 | intersection_type_to_pb, |
| 53 | + section_cut_cdb_export_type_to_pb, |
47 | 54 | section_cut_type_from_pb, |
48 | 55 | section_cut_type_to_pb, |
49 | 56 | status_type_from_pb, |
@@ -225,3 +232,81 @@ def _create_stub(self) -> section_cut_pb2_grpc.ObjectServiceStub: |
225 | 232 | number_of_interpolation_points: ReadWriteProperty[int, int] = grpc_data_property( |
226 | 233 | "properties.number_of_interpolation_points" |
227 | 234 | ) |
| 235 | + |
| 236 | + def export( |
| 237 | + self, |
| 238 | + path: _PATH, |
| 239 | + *, |
| 240 | + export_type: SectionCutCDBExportType = "mesh_only", |
| 241 | + ) -> None: |
| 242 | + """Export the section cut to a CDB file. |
| 243 | +
|
| 244 | + Parameters |
| 245 | + ---------- |
| 246 | + path : |
| 247 | + Path to the file where the section cut is saved. |
| 248 | + export_type : |
| 249 | + Determines what is exported to the CDB file. Options are: |
| 250 | +
|
| 251 | + - ``"mesh_only"``: Only the mesh (elements and nodes) is exported. |
| 252 | + - ``"solid_model"``: The section cut is expanded into a slice of |
| 253 | + solid elements. In addition, the material properties are exported |
| 254 | + and the element coordinate systems are aligned with the fiber |
| 255 | + direction. This model can be used to compute the equivalent |
| 256 | + beam properties of the section cut. |
| 257 | +
|
| 258 | + """ |
| 259 | + with self._server_wrapper.auto_download(path) as export_path: |
| 260 | + with wrap_grpc_errors(): |
| 261 | + self._get_stub().ExportToCDB( # type: ignore |
| 262 | + section_cut_pb2.ExportToCDBRequest( |
| 263 | + resource_path=self._resource_path, |
| 264 | + path=export_path, |
| 265 | + export_type=typing.cast( |
| 266 | + typing.Any, section_cut_cdb_export_type_to_pb(export_type) |
| 267 | + ), |
| 268 | + ) |
| 269 | + ) |
| 270 | + |
| 271 | + def export_becas_input( |
| 272 | + self, |
| 273 | + path: _PATH, |
| 274 | + *, |
| 275 | + export_strength_limits: bool = True, |
| 276 | + ) -> None: |
| 277 | + """Export the section cut to BECAS input files. |
| 278 | +
|
| 279 | + Parameters |
| 280 | + ---------- |
| 281 | + path : |
| 282 | + Path to the directory where the input files are saved. |
| 283 | + export_strength_limits : |
| 284 | + Determines whether strength limits are exported to the |
| 285 | + BECAS input files. |
| 286 | +
|
| 287 | + """ |
| 288 | + # The 'auto_download' context manager cannot be directly used here |
| 289 | + # because the export path is not a file but a directory. The BECAS |
| 290 | + # export produces multiple files, which we all need to download. |
| 291 | + # Currently (May '25), this is the only export that produces multiple |
| 292 | + # files at once. If there are more, we should either extend 'auto_download', |
| 293 | + # or create a new context manager for this purpose. |
| 294 | + export_path = pathlib.Path( |
| 295 | + self._server_wrapper.filetransfer_handler.to_export_path(path, is_directory=True) |
| 296 | + ) |
| 297 | + expected_filenames = ["E2D.in", "EMAT.in", "MATPROPS.in", "N2D.in"] |
| 298 | + if export_strength_limits: |
| 299 | + expected_filenames.append("FAILMAT.in") |
| 300 | + |
| 301 | + with wrap_grpc_errors(): |
| 302 | + self._get_stub().ExportToBECAS( # type: ignore |
| 303 | + section_cut_pb2.ExportToBECASRequest( |
| 304 | + resource_path=self._resource_path, |
| 305 | + path=path_to_str_checked(export_path), |
| 306 | + export_strength_limits=export_strength_limits, |
| 307 | + ) |
| 308 | + ) |
| 309 | + for filename in expected_filenames: |
| 310 | + self._server_wrapper.filetransfer_handler.download_file_if_autotransfer( |
| 311 | + export_path / filename, pathlib.Path(path) / filename |
| 312 | + ) |
0 commit comments