Skip to content

Commit 44ccdee

Browse files
finalize testing and accepting multiple bodies to detect sweepability
1 parent 9afb244 commit 44ccdee

File tree

4 files changed

+67
-37
lines changed

4 files changed

+67
-37
lines changed

src/ansys/geometry/core/_grpc/_services/v0/prepare_tools.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -394,13 +394,13 @@ def create_sphere_enclosure(self, **kwargs) -> dict: # noqa: D102
394394
"tracker_response": serialized_tracker_response,
395395
}
396396

397-
@protect_grpc
397+
@protect_grpc
398398
def is_body_sweepable(self, **kwargs): # noqa: D102
399399
from ansys.api.geometry.v0.preparetools_pb2 import IsBodySweepableRequest
400400

401401
# Create the request - assumes all inputs are valid and of the proper type
402402
request = IsBodySweepableRequest(
403-
body=build_grpc_id(kwargs["body_id"]),
403+
bodies=[build_grpc_id(id=id) for id in kwargs["body_ids"]],
404404
get_source_target_faces=kwargs["get_source_target_faces"],
405405
)
406406

@@ -409,13 +409,11 @@ def is_body_sweepable(self, **kwargs): # noqa: D102
409409

410410
# Return the response - formatted as a dictionary
411411
return {
412-
"result": response.result,
413-
"faces": [
412+
"results": [
414413
{
415-
"id": face.id,
416-
"surface_type": face.surface_type,
417-
"is_reversed": face.is_reversed,
414+
"sweepable": result.result,
415+
"face_ids": [face.id for face in result.face_ids]
418416
}
419-
for face in response.faces
420-
],
417+
for result in response.response_data
418+
]
421419
}

src/ansys/geometry/core/_grpc/_services/v1/prepare_tools.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,5 @@ def create_sphere_enclosure(self, **kwargs) -> dict: # noqa: D102
9696
raise NotImplementedError
9797

9898
@protect_grpc
99-
def is_body_sweepable(self, **kwargs): # noqa: D102
99+
def is_body_sweepable(self, **kwargs): # noqa: D102
100+
raise NotImplementedError

src/ansys/geometry/core/tools/prepare_tools.py

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
get_design_from_body,
3939
get_design_from_edge,
4040
get_design_from_face,
41+
get_faces_from_ids,
4142
)
4243
from ansys.geometry.core.misc.checks import check_type_all_elements_in_iterable, min_backend_version
4344
from ansys.geometry.core.misc.measurements import Distance
@@ -780,48 +781,44 @@ def create_sphere_enclosure(
780781
self._grpc_client.log.info("Failed to create enclosure...")
781782
return []
782783

783-
@min_backend_version(26, 1, 0)
784+
@min_backend_version(26, 1, 0)
784785
def is_body_sweepable(
785786
self,
786-
body: "Body",
787+
bodies: list["Body"],
787788
get_source_target_faces: bool = False,
788-
) -> tuple[bool, list["Face"]]:
789-
"""Check if a body is sweepable.
789+
) -> list[tuple[bool, list["Face"]]]:
790+
"""Check if bodies are sweepable.
790791
791792
Parameters
792793
----------
793-
body : Body
794-
Body to check.
794+
bodies : list[Body]
795+
List of bodies to check.
795796
get_source_target_faces : bool
796797
Whether to get source and target faces. By default, ``False``.
797798
798799
Returns
799800
-------
800-
tuple[bool, list[Face]]
801-
Tuple containing a boolean indicating if the body is sweepable and
801+
list[tuple[bool, list[Face]]]
802+
List of tuples, each containing a boolean indicating if the body is sweepable and
802803
a list of source and target faces if requested.
803804
"""
804805
from ansys.geometry.core.designer.body import Body
805-
from ansys.geometry.core.designer.face import Face, SurfaceType
806+
check_type_all_elements_in_iterable(bodies, Body)
806807

807-
# Verify inputs
808-
check_type_all_elements_in_iterable([body], Body)
808+
if not bodies:
809+
return []
809810

810811
response = self._grpc_client._services.prepare_tools.is_body_sweepable(
811-
body_id=body.id,
812+
body_ids=[body.id for body in bodies],
812813
get_source_target_faces=get_source_target_faces,
813814
)
814815

815-
faces = []
816-
if get_source_target_faces:
817-
faces = [
818-
Face(
819-
face.get("id"),
820-
SurfaceType(face.get("surface_type")),
821-
self,
822-
self._grpc_client,
823-
)
824-
for face in response.get("faces")
825-
]
816+
results = []
817+
for result_data in response.get("results"):
818+
faces = []
819+
parent_design = get_design_from_body(bodies[0])
820+
if get_source_target_faces:
821+
faces.extend(get_faces_from_ids(parent_design, result_data.get("face_ids")))
822+
results.append((result_data.get("sweepable"), faces))
826823

827-
return (response.get("result"), faces)
824+
return results

tests/integration/test_prepare_tools.py

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -298,12 +298,46 @@ def test_sphere_enclosure(modeler):
298298

299299
def test_is_body_sweepable(modeler: Modeler):
300300
"""Test body sweepability detection."""
301-
design = modeler.open_file(FILES_DIR / "1mm_Cube.dsco")
301+
design = modeler.open_file(FILES_DIR / "DifferentShapes.scdocx")
302302

303303
bodies = design.bodies
304-
assert len(bodies) == 1
304+
assert len(bodies) == 6
305305

306306
# Test sweepability of the body
307-
is_sweepable, faces = modeler.prepare_tools.is_body_sweepable(bodies[0])
307+
is_sweepable, faces = modeler.prepare_tools.is_body_sweepable([bodies[0]])[0]
308308
assert is_sweepable
309309
assert len(faces) == 0
310+
311+
# Test non-sweepable body
312+
is_sweepable, faces = modeler.prepare_tools.is_body_sweepable([bodies[2]])[0]
313+
assert not is_sweepable
314+
assert len(faces) == 0
315+
316+
# Test sweepability of a body and getting faces
317+
is_sweepable, faces = modeler.prepare_tools.is_body_sweepable(
318+
[bodies[0]], get_source_target_faces=True
319+
)[0]
320+
assert is_sweepable
321+
assert len(faces) == 2
322+
323+
# Test multiple bodies at once
324+
result = modeler.prepare_tools.is_body_sweepable(bodies)
325+
assert len(result) == 6
326+
assert result[0][0] # first body is sweepable
327+
assert result[1][0] # second body is sweepable
328+
assert not result[2][0] # third body is not sweepable
329+
assert not result[3][0] # fourth body is not sweepable
330+
331+
# Test with multiple bodys and getting faces
332+
result = modeler.prepare_tools.is_body_sweepable(bodies, get_source_target_faces=True)
333+
assert len(result) == 6
334+
assert result[0][0] # first body is sweepable
335+
assert len(result[0][1]) == 2 # two faces for first body
336+
assert result[1][0] # second body is sweepable
337+
assert len(result[1][1]) == 2 # two faces for second body
338+
assert not result[2][0] # third body is not sweepable
339+
assert len(result[2][1]) == 0 # no faces for third body
340+
341+
# Test with empty input
342+
result = modeler.prepare_tools.is_body_sweepable([])
343+
assert result == []

0 commit comments

Comments
 (0)