Skip to content

Commit 5e99f81

Browse files
added components to NS
reverted changes to move_translate
1 parent 44c6d85 commit 5e99f81

File tree

5 files changed

+56
-6
lines changed

5 files changed

+56
-6
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,7 @@ def create_named_selection(
607607
edges: list[Edge] | None = None,
608608
beams: list[Beam] | None = None,
609609
design_points: list[DesignPoint] | None = None,
610+
components: list[Component] | None = None,
610611
) -> NamedSelection:
611612
"""Create a named selection on the active Geometry server instance.
612613
@@ -624,6 +625,8 @@ def create_named_selection(
624625
All beams to include in the named selection.
625626
design_points : list[DesignPoint], default: None
626627
All design points to include in the named selection.
628+
components : list[Component], default: None
629+
All components to include in the named selection.
627630
628631
Returns
629632
-------
@@ -637,10 +640,10 @@ def create_named_selection(
637640
one of the optional parameters must be provided.
638641
"""
639642
# Verify that at least one entity is provided
640-
if not any([bodies, faces, edges, beams, design_points]):
643+
if not any([bodies, faces, edges, beams, design_points, components]):
641644
raise ValueError(
642645
"At least one of the following must be provided: "
643-
"bodies, faces, edges, beams, or design_points."
646+
"bodies, faces, edges, beams, design_points, or components."
644647
)
645648

646649
named_selection = NamedSelection(
@@ -652,6 +655,7 @@ def create_named_selection(
652655
edges=edges,
653656
beams=beams,
654657
design_points=design_points,
658+
components=components,
655659
)
656660

657661
self._named_selections[named_selection.name] = named_selection

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1308,16 +1308,16 @@ def get_round_info(self, face: "Face") -> tuple[bool, Real]:
13081308
@min_backend_version(25, 2, 0)
13091309
def move_translate(
13101310
self,
1311-
selection: NamedSelection | Component,
1311+
selection: NamedSelection,
13121312
direction: UnitVector3D,
13131313
distance: Distance | Quantity | Real,
13141314
) -> bool:
13151315
"""Move a selection by a distance in a direction.
13161316
13171317
Parameters
13181318
----------
1319-
selection : NamedSelection | Component
1320-
Named selection or component to move.
1319+
selection : NamedSelection
1320+
Named selection to move.
13211321
direction : UnitVector3D
13221322
Direction to move in.
13231323
distance : Distance | Quantity | Real

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@
2727
from ansys.geometry.core.connection.conversions import grpc_point_to_point3d
2828
from ansys.geometry.core.designer.beam import Beam
2929
from ansys.geometry.core.designer.body import Body
30+
from ansys.geometry.core.designer.component import Component
3031
from ansys.geometry.core.designer.designpoint import DesignPoint
3132
from ansys.geometry.core.designer.edge import Edge
3233
from ansys.geometry.core.designer.face import Face
3334
from ansys.geometry.core.misc.auxiliary import (
3435
get_beams_from_ids,
3536
get_bodies_from_ids,
37+
get_components_from_ids,
3638
get_edges_from_ids,
3739
get_faces_from_ids,
3840
)
@@ -67,6 +69,8 @@ class NamedSelection:
6769
All beams to include in the named selection.
6870
design_points : list[DesignPoints], default: None
6971
All design points to include in the named selection.
72+
components: list[Component], default: None
73+
All components to include in the named selection.
7074
"""
7175

7276
def __init__(
@@ -79,6 +83,7 @@ def __init__(
7983
edges: list[Edge] | None = None,
8084
beams: list[Beam] | None = None,
8185
design_points: list[DesignPoint] | None = None,
86+
components: list[Component] | None = None,
8287
preexisting_id: str | None = None,
8388
):
8489
"""Initialize the ``NamedSelection`` class."""
@@ -97,13 +102,16 @@ def __init__(
97102
beams = []
98103
if design_points is None:
99104
design_points = []
105+
if components is None:
106+
components = []
100107

101108
# Instantiate
102109
self._bodies = bodies
103110
self._faces = faces
104111
self._edges = edges
105112
self._beams = beams
106113
self._design_points = design_points
114+
self._components = components
107115

108116
# Store ids for later use... when verifying if the NS changed.
109117
self._ids_cached = {
@@ -112,6 +120,7 @@ def __init__(
112120
"edges": [edge.id for edge in edges],
113121
"beams": [beam.id for beam in beams],
114122
"design_points": [dp.id for dp in design_points],
123+
"components": [component.id for component in components],
115124
}
116125

117126
if preexisting_id:
@@ -193,6 +202,16 @@ def design_points(self) -> list[DesignPoint]:
193202
]
194203

195204
return self._design_points
205+
206+
@property
207+
def components(self) -> list[Component]:
208+
"""All components in the named selection."""
209+
self.__verify_ns()
210+
if self._components is None:
211+
# Get all components from the named selection
212+
self._components = get_components_from_ids(self._design, self._ids_cached["components"])
213+
214+
return self._components
196215

197216
def __verify_ns(self) -> None:
198217
"""Verify that the contents of the named selection are up to date."""

src/ansys/geometry/core/misc/auxiliary.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,30 @@ def get_bodies_from_ids(design: "Design", body_ids: list[str]) -> list["Body"]:
183183
return [body for body in __traverse_all_bodies(design) if body.id in body_ids]
184184

185185

186+
def get_components_from_ids(design: "Design", component_ids: list[str]) -> list["Component"]:
187+
"""Find the ``Component`` objects inside a ``Design`` from its ids.
188+
189+
Parameters
190+
----------
191+
design : Design
192+
Parent design for the components.
193+
component_ids : list[str]
194+
List of component ids.
195+
196+
Returns
197+
-------
198+
list[Component]
199+
List of Component objects.
200+
201+
Notes
202+
-----
203+
This method takes a design and component ids, and gets their corresponding ``Component`` object.
204+
"""
205+
return [
206+
comp for comp in __traverse_component_elem("components", design) if comp.id in component_ids
207+
] # noqa: E501
208+
209+
186210
def get_faces_from_ids(design: "Design", face_ids: list[str]) -> list["Face"]:
187211
"""Find the ``Face`` objects inside a ``Design`` from its ids.
188212

tests/integration/test_geometry_commands.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1186,9 +1186,12 @@ def test_move_translate_component(modeler: Modeler):
11861186
sketch = Sketch().box(Point2D([0, 0]), 1, 1)
11871187
box_body = component.extrude_sketch("BoxBody", sketch, 1)
11881188

1189+
# Add the component to a named selection
1190+
ns = design.create_named_selection("ComponentNS", components=[component])
1191+
11891192
# Move the component
11901193
success = modeler.geometry_commands.move_translate(
1191-
component,
1194+
ns,
11921195
UNITVECTOR3D_Z,
11931196
Distance(2, UNITS.m),
11941197
)

0 commit comments

Comments
 (0)