|
28 | 28 |
|
29 | 29 | from fourcipp.fourc_input import FourCInput |
30 | 30 |
|
31 | | -from cubitpy.conf import cupy |
| 31 | +from cubitpy.conf import GeometryType, cupy |
32 | 32 | from cubitpy.cubit_group import CubitGroup |
33 | 33 | from cubitpy.cubit_to_fourc_input import get_input_file_with_mesh |
34 | 34 | from cubitpy.cubit_wrapper.cubit_wrapper_host import CubitConnect |
@@ -361,6 +361,88 @@ def reset(self): |
361 | 361 | self.cubit.reset() |
362 | 362 | self._default_cubit_variables() |
363 | 363 |
|
| 364 | + def cmd_return(self, cmd: str, geometry_type: GeometryType, **kwargs): |
| 365 | + """Run a cubit command and return the created geometry object. |
| 366 | +
|
| 367 | + Args: |
| 368 | + cmd: The cubit command to run. |
| 369 | + geometry_type: The geometry type that should be checked for a new geometry. |
| 370 | + filter_sheet_bodies: If volumes that are sheet bodies should be ignored. |
| 371 | + Defaults to true. |
| 372 | +
|
| 373 | + Returns: |
| 374 | + If a single geometry object of the given type is created, this object |
| 375 | + is returned. This function expects that a single geometry item of the given |
| 376 | + type is created, otherwise an error will be raised. For use cases, where one |
| 377 | + expects a variable amount of created items or wants to check multiple |
| 378 | + different geometry types, please refer to `cmd_return_dict`. |
| 379 | + """ |
| 380 | + geometry_dict = self.cmd_return_dict(cmd, [geometry_type], **kwargs) |
| 381 | + if len(geometry_dict[geometry_type]) == 1: |
| 382 | + return geometry_dict[geometry_type][0] |
| 383 | + else: |
| 384 | + raise ValueError( |
| 385 | + f"Expected a single created item of type {geometry_type}, but got {geometry_dict[geometry_type]}" |
| 386 | + ) |
| 387 | + |
| 388 | + def cmd_return_dict( |
| 389 | + self, |
| 390 | + cmd: str, |
| 391 | + geometry_types: list[GeometryType], |
| 392 | + *, |
| 393 | + filter_sheet_bodies: bool = True, |
| 394 | + ): |
| 395 | + """Run a cubit command and return created geometry objects. |
| 396 | +
|
| 397 | + Args: |
| 398 | + cmd: The cubit command to run. |
| 399 | + geometry_types: The geometry types that should be checked for new geometries. |
| 400 | + filter_sheet_bodies: If volumes that are sheet bodies should be ignored. |
| 401 | + Defaults to true. |
| 402 | +
|
| 403 | + Returns: |
| 404 | + A dictionary of the created geometry objects. The dictionary keys are the |
| 405 | + geometry types, the values are lists containing the respective objects. |
| 406 | + """ |
| 407 | + |
| 408 | + # Store the already existing ids for all requested geometry types. |
| 409 | + geometry_ids_before = { |
| 410 | + geometry: set(self.get_entities(geometry.get_cubit_string())) |
| 411 | + for geometry in geometry_types |
| 412 | + } |
| 413 | + |
| 414 | + # For CoreForm, we need to check that the volumes are not sheet bodies |
| 415 | + if cupy.is_coreform() and filter_sheet_bodies: |
| 416 | + if cupy.geometry.volume in geometry_ids_before: |
| 417 | + volume_ids_before = geometry_ids_before[cupy.geometry.volume] |
| 418 | + volume_ids_before_no_sheet_bodies = { |
| 419 | + id for id in volume_ids_before if not self.is_sheet_body(id) |
| 420 | + } |
| 421 | + geometry_ids_before[cupy.geometry.volume] = ( |
| 422 | + volume_ids_before_no_sheet_bodies |
| 423 | + ) |
| 424 | + |
| 425 | + # Run the command. |
| 426 | + self.cmd(cmd) |
| 427 | + |
| 428 | + # Get the objects that were created by the command. |
| 429 | + create_objects = {} |
| 430 | + for geometry, ids_before in geometry_ids_before.items(): |
| 431 | + ids_after = set(self.get_entities(geometry.get_cubit_string())) |
| 432 | + ids_new = ids_after - ids_before |
| 433 | + |
| 434 | + if ( |
| 435 | + cupy.is_coreform() |
| 436 | + and filter_sheet_bodies |
| 437 | + and geometry == cupy.geometry.volume |
| 438 | + ): |
| 439 | + ids_new = {id for id in ids_new if not self.is_sheet_body(id)} |
| 440 | + |
| 441 | + geometry_objects = self.get_items(geometry, item_ids=ids_new) |
| 442 | + create_objects[geometry] = geometry_objects |
| 443 | + |
| 444 | + return create_objects |
| 445 | + |
364 | 446 | def display_in_cubit(self, labels=[], delay=0.5, testing=False): |
365 | 447 | """Save the state to a cubit file and open cubit with that file. |
366 | 448 | Additionally labels can be displayed in cubit to simplify the mesh |
|
0 commit comments