Skip to content

Commit 46477e2

Browse files
jacobrkerstetterpre-commit-ci[bot]pyansys-ci-bot
authored
feat: remove materials (#2180)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: pyansys-ci-bot <92810346+pyansys-ci-bot@users.noreply.github.com>
1 parent 32797ba commit 46477e2

File tree

11 files changed

+149
-0
lines changed

11 files changed

+149
-0
lines changed

doc/changelog.d/2180.added.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Remove materials

src/ansys/geometry/core/_grpc/_services/base/bodies.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,11 @@ def get_assigned_material(self, **kwargs) -> dict:
149149
"""Get the assigned material of a body."""
150150
pass
151151

152+
@abstractmethod
153+
def remove_assigned_material(self, **kwargs) -> dict:
154+
"""Remove the assigned material of a body."""
155+
pass
156+
152157
@abstractmethod
153158
def set_name(self, **kwargs) -> dict:
154159
"""Set the name of a body."""

src/ansys/geometry/core/_grpc/_services/base/materials.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,8 @@ def __init__(self, channel: grpc.Channel):
4343
def add_material(self, **kwargs) -> dict:
4444
"""Add material to the service design."""
4545
pass
46+
47+
@abstractmethod
48+
def remove_material(self, **kwargs) -> dict:
49+
"""Remove material from the service design."""
50+
pass

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,19 @@ def get_assigned_material(self, **kwargs) -> dict: # noqa: D102
495495
# Return the response - formatted as a dictionary
496496
return {"material": from_grpc_material_to_material(resp)}
497497

498+
@protect_grpc
499+
def remove_assigned_material(self, **kwargs) -> dict: # noqa: D102
500+
from ansys.api.geometry.v0.bodies_pb2 import RemoveAssignedMaterialRequest
501+
502+
# Create the request - assumes all inputs are valid and of the proper type
503+
request = RemoveAssignedMaterialRequest(ids=[build_grpc_id(id) for id in kwargs["ids"]])
504+
505+
# Call the gRPC service
506+
resp = self.stub.RemoveAssignedMaterial(request=request)
507+
508+
# Return the response - formatted as a dictionary
509+
return {"successfully_removed": [id for id in resp.successfully_removed]}
510+
498511
@protect_grpc
499512
def set_name(self, **kwargs) -> dict: # noqa: D102
500513
from ansys.api.geometry.v0.bodies_pb2 import SetNameRequest

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,18 @@ def add_material(self, **kwargs) -> dict: # noqa: D102
6262

6363
# Convert the response to a dictionary
6464
return {}
65+
66+
@protect_grpc
67+
def remove_material(self, **kwargs) -> dict: # noqa: D102
68+
from ansys.api.geometry.v0.materials_pb2 import RemoveFromDocumentRequest
69+
70+
# Create the request - assumes all inputs are valid and of the proper type
71+
request = RemoveFromDocumentRequest(
72+
request_data=[from_material_to_grpc_material(mat) for mat in kwargs["materials"]]
73+
)
74+
75+
# Call the gRPC service
76+
_ = self.stub.RemoveFromDocument(request=request)
77+
78+
# Convert the response to a dictionary
79+
return {}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@ def set_assigned_material(self, **kwargs) -> dict: # noqa: D102
136136
def get_assigned_material(self, **kwargs) -> dict: # noqa: D102
137137
raise NotImplementedError
138138

139+
@protect_grpc
140+
def remove_assigned_material(self, **kwargs): # noqa: D102
141+
raise NotImplementedError
142+
139143
@protect_grpc
140144
def set_name(self, **kwargs) -> dict: # noqa: D102
141145
raise NotImplementedError

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,7 @@ def __init__(self, channel: grpc.Channel): # noqa: D102
5050
@protect_grpc
5151
def add_material(self, **kwargs) -> dict: # noqa: D102
5252
raise NotImplementedError
53+
54+
@protect_grpc
55+
def remove_material(self, **kwargs) -> dict: # noqa: D102
56+
raise NotImplementedError

src/ansys/geometry/core/designer/body.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,11 @@ def get_assigned_material(self) -> Material:
338338
"""
339339
return
340340

341+
@abstractmethod
342+
def remove_assigned_material(self) -> None:
343+
"""Remove the material assigned to the body."""
344+
return
345+
341346
@abstractmethod
342347
def add_midsurface_thickness(self, thickness: Quantity) -> None:
343348
"""Add a mid-surface thickness to a surface body.
@@ -1061,6 +1066,11 @@ def get_assigned_material(self) -> Material: # noqa: D102
10611066
response = self._grpc_client.services.bodies.get_assigned_material(id=self.id)
10621067
return response.get("material")
10631068

1069+
@min_backend_version(26, 1, 0)
1070+
def remove_assigned_material(self) -> None: # noqa: D102
1071+
self._grpc_client.log.debug(f"Removing assigned material for body {self.id}.")
1072+
self._grpc_client.services.bodies.remove_assigned_material(ids=[self.id])
1073+
10641074
@protect_grpc
10651075
@check_input_types
10661076
def add_midsurface_thickness(self, thickness: Quantity) -> None: # noqa: D102
@@ -1593,6 +1603,10 @@ def assign_material(self, material: Material) -> None: # noqa: D102
15931603
def get_assigned_material(self) -> Material: # noqa: D102
15941604
return self._template.get_assigned_material()
15951605

1606+
@ensure_design_is_active
1607+
def remove_assigned_material(self): # noqa: D102
1608+
self._template.remove_assigned_material()
1609+
15961610
@ensure_design_is_active
15971611
def add_midsurface_thickness(self, thickness: Quantity) -> None: # noqa: D102
15981612
self._template.add_midsurface_thickness(thickness)

src/ansys/geometry/core/designer/design.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,24 @@ def add_material(self, material: Material) -> None:
227227
self._materials.append(material)
228228
self._grpc_client.log.debug(f"Material {material.name} is successfully added to design.")
229229

230+
@min_backend_version(26, 1, 0)
231+
@check_input_types
232+
@ensure_design_is_active
233+
def remove_material(self, material: Material | list[Material]) -> None:
234+
"""Remove a material from the design.
235+
236+
Parameters
237+
----------
238+
material : Material | list[Material]
239+
Material or list of materials to remove.
240+
"""
241+
material = material if isinstance(material, list) else [material]
242+
243+
self._grpc_client.services.materials.remove_material(materials=material)
244+
for mat in material:
245+
self._materials.remove(mat)
246+
self._grpc_client.log.debug(f"Material {mat.name} is successfully removed from design.")
247+
230248
@check_input_types
231249
@ensure_design_is_active
232250
def save(self, file_location: Path | str, write_body_facets: bool = False) -> None:

tests/integration/test_design.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,38 @@ def test_get_empty_material(modeler: Modeler):
322322
assert len(mat_service.properties) == 1
323323

324324

325+
def test_remove_material_from_body(modeler: Modeler):
326+
"""Test removing a material from a body."""
327+
# Create a design and a sketch
328+
design = modeler.create_design("RemoveMaterialTest")
329+
sketch = Sketch()
330+
sketch.circle(Point2D([0, 0], UNITS.mm), Quantity(10, UNITS.mm))
331+
332+
# Extrude the sketch to create a body
333+
body = design.extrude_sketch("CircleBody", sketch, Quantity(10, UNITS.mm))
334+
335+
# Create and assign a material
336+
density = Quantity(7850, UNITS.kg / (UNITS.m**3))
337+
material = Material(
338+
"Steel",
339+
density,
340+
[MaterialProperty(MaterialPropertyType.POISSON_RATIO, "Poisson", Quantity(0.3))],
341+
)
342+
design.add_material(material)
343+
body.assign_material(material)
344+
assert body.material.name == "Steel"
345+
346+
# Remove the material from the body
347+
body.remove_assigned_material()
348+
349+
# Check that the body no longer has a material assigned
350+
assert body.material.name == ""
351+
assert len(body.material.properties) == 1
352+
assert body.material.properties[MaterialPropertyType.DENSITY].quantity == Quantity(
353+
0, UNITS.kg / (UNITS.m**3)
354+
)
355+
356+
325357
def test_face_to_body_creation(modeler: Modeler):
326358
"""Test in charge of validating the extrusion of an existing face."""
327359
# Create a Sketch and draw a circle (all client side)

0 commit comments

Comments
 (0)