Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/changelog.d/2382.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Rename named selection
Original file line number Diff line number Diff line change
Expand Up @@ -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
16 changes: 16 additions & 0 deletions src/ansys/geometry/core/_grpc/_services/v0/named_selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 10 additions & 0 deletions src/ansys/geometry/core/designer/selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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."""
Expand Down
33 changes: 33 additions & 0 deletions tests/integration/test_design.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
Loading