diff --git a/doc/changelog.d/2382.added.md b/doc/changelog.d/2382.added.md new file mode 100644 index 0000000000..e908c647e6 --- /dev/null +++ b/doc/changelog.d/2382.added.md @@ -0,0 +1 @@ +Rename named selection diff --git a/src/ansys/geometry/core/_grpc/_services/base/named_selection.py b/src/ansys/geometry/core/_grpc/_services/base/named_selection.py index c794bd723d..0da31ecab0 100644 --- a/src/ansys/geometry/core/_grpc/_services/base/named_selection.py +++ b/src/ansys/geometry/core/_grpc/_services/base/named_selection.py @@ -53,3 +53,8 @@ def create_named_selection(self, **kwargs) -> dict: def delete_named_selection(self, **kwargs) -> dict: """Delete a named selection by id.""" pass + + @abstractmethod + def rename_named_selection(self, **kwargs) -> dict: + """Rename a named selection by id.""" + pass diff --git a/src/ansys/geometry/core/_grpc/_services/v0/named_selection.py b/src/ansys/geometry/core/_grpc/_services/v0/named_selection.py index 761eaf7430..68dbeb79ae 100644 --- a/src/ansys/geometry/core/_grpc/_services/v0/named_selection.py +++ b/src/ansys/geometry/core/_grpc/_services/v0/named_selection.py @@ -107,3 +107,19 @@ def delete_named_selection(self, **kwargs): # noqa: D102 # Return the response - empty dictionary return {} + + @protect_grpc + def rename_named_selection(self, **kwargs): # noqa: D102 + from ansys.api.geometry.v0.namedselections_pb2 import SetNameRequest + + # Create the request - assumes all inputs are valid and of the proper type + request = SetNameRequest( + id=build_grpc_id(kwargs["id"]), + new_name=kwargs["new_name"], + ) + + # Call the gRPC service + self.stub.SetName(request) + + # Return the response - empty dictionary + return {} diff --git a/src/ansys/geometry/core/_grpc/_services/v1/named_selection.py b/src/ansys/geometry/core/_grpc/_services/v1/named_selection.py index 57fd83f9d8..200c24c356 100644 --- a/src/ansys/geometry/core/_grpc/_services/v1/named_selection.py +++ b/src/ansys/geometry/core/_grpc/_services/v1/named_selection.py @@ -58,3 +58,7 @@ def create_named_selection(self, **kwargs): # noqa: D102 @protect_grpc def delete_named_selection(self, **kwargs): # noqa: D102 raise NotImplementedError + + @protect_grpc + def rename_named_selection(self, **kwargs): # noqa: D102 + raise NotImplementedError diff --git a/src/ansys/geometry/core/designer/selection.py b/src/ansys/geometry/core/designer/selection.py index f326c961d8..ec0a365417 100644 --- a/src/ansys/geometry/core/designer/selection.py +++ b/src/ansys/geometry/core/designer/selection.py @@ -39,6 +39,7 @@ get_faces_from_ids, get_vertices_from_ids, ) +from ansys.geometry.core.misc.checks import min_backend_version if TYPE_CHECKING: # pragma: no cover from ansys.geometry.core.designer.design import Design @@ -156,6 +157,15 @@ def name(self) -> str: """Name of the named selection.""" return self._name + @name.setter + @min_backend_version(26, 1, 0) + def name(self, value: str) -> None: + """Set the name of the named selection.""" + self._grpc_client.services.named_selection.rename_named_selection( + id=self._id, new_name=value + ) + self._name = value + @property def bodies(self) -> list[Body]: """All bodies in the named selection.""" diff --git a/tests/integration/test_design.py b/tests/integration/test_design.py index 11acbbca5b..fafdd557ba 100644 --- a/tests/integration/test_design.py +++ b/tests/integration/test_design.py @@ -596,6 +596,39 @@ def test_named_selections(modeler: Modeler): assert len(design.named_selections) == 3 +def test_rename_named_selection(modeler: Modeler): + """Test for renaming and verifying a ``NamedSelection``.""" + # Create your design on the server side + design = modeler.create_design("NamedSelection_Test") + + # Create 2 Sketch objects and draw a circle and a polygon (all client side) + sketch_1 = Sketch() + sketch_1.circle(Point2D([10, 10], UNITS.mm), Quantity(10, UNITS.mm)) + sketch_2 = Sketch() + sketch_2.polygon(Point2D([-30, -30], UNITS.mm), Quantity(10, UNITS.mm), sides=5) + + # Build 2 independent components and bodies + circle_comp = design.add_component("CircleComponent") + body_circle_comp = circle_comp.extrude_sketch("Circle", sketch_1, Quantity(50, UNITS.mm)) + polygon_comp = design.add_component("PolygonComponent") + body_polygon_comp = polygon_comp.extrude_sketch("Polygon", sketch_2, Quantity(30, UNITS.mm)) + + # Create the NamedSelection + only_circle = design.create_named_selection("OnlyCircle", bodies=[body_circle_comp]) + design.create_named_selection("OnlyPolygon", bodies=[body_polygon_comp]) + design.create_named_selection("CircleAndPolygon", bodies=[body_circle_comp, body_polygon_comp]) + + # Check that the named selections are available + assert len(design.named_selections) == 3 + + # Rename the only circle NS + only_circle.name = "JustCircle" + assert only_circle.name == "JustCircle" + assert design.named_selections[0].name == "JustCircle" + assert design.named_selections[1].name == "OnlyPolygon" + assert design.named_selections[2].name == "CircleAndPolygon" + + def test_old_backend_version(modeler: Modeler, use_grpc_client_old_backend: Modeler): # Try to vefify name selection using earlier backend version design = modeler.open_file(Path(FILES_DIR, "25R1BasicBoxNameSelection.scdocx"))