Skip to content

Commit 7039aea

Browse files
umutsoysalansyspyansys-ci-botRobPasMuepre-commit-ci[bot]
authored
feat: setting instance name during component creation (#1382)
Co-authored-by: pyansys-ci-bot <92810346+pyansys-ci-bot@users.noreply.github.com> Co-authored-by: Roberto Pastor Muela <37798125+RobPasMue@users.noreply.github.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent e46854e commit 7039aea

File tree

3 files changed

+48
-5
lines changed

3 files changed

+48
-5
lines changed

doc/changelog.d/1382.added.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
setting instance name during component creation

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

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ class Component:
133133
template : Component, default: None
134134
Template to create this component from. This creates an
135135
instance component that shares a master with the template component.
136+
instance_name: str, default: None
137+
User defined optional name for the component instance.
136138
preexisting_id : str, default: None
137139
ID of a component pre-existing on the server side to use to create the component
138140
on the client-side data model. If an ID is specified, a new component is not
@@ -160,6 +162,7 @@ def __init__(
160162
parent_component: Union["Component", None],
161163
grpc_client: GrpcClient,
162164
template: Optional["Component"] = None,
165+
instance_name: Optional[str] = None,
163166
preexisting_id: str | None = None,
164167
master_component: MasterComponent | None = None,
165168
read_existing_comp: bool = False,
@@ -171,21 +174,33 @@ def __init__(
171174
self._bodies_stub = BodiesStub(self._grpc_client.channel)
172175
self._commands_stub = CommandsStub(self._grpc_client.channel)
173176

177+
# Align instance name behavior with the server - empty string if None
178+
instance_name = instance_name if instance_name else ""
179+
174180
if preexisting_id:
175181
self._name = name
176182
self._id = preexisting_id
183+
self._instance_name = instance_name
177184
else:
178185
if parent_component:
179186
template_id = template.id if template else ""
180187
new_component = self._component_stub.Create(
181-
CreateRequest(name=name, parent=parent_component.id, template=template_id)
188+
CreateRequest(
189+
name=name,
190+
parent=parent_component.id,
191+
template=template_id,
192+
instance_name=instance_name,
193+
)
182194
)
195+
183196
# Remove this method call once we know Service sends correct ObjectPath id
184197
self._id = new_component.component.id
185198
self._name = new_component.component.name
199+
self._instance_name = new_component.component.instance_name
186200
else:
187201
self._name = name
188202
self._id = None
203+
self._instance_name = instance_name
189204

190205
# Initialize needed instance variables
191206
self._components = []
@@ -231,6 +246,11 @@ def name(self) -> str:
231246
"""Name of the component."""
232247
return self._name
233248

249+
@property
250+
def instance_name(self) -> str:
251+
"""Name of the component instance."""
252+
return self._instance_name
253+
234254
@property
235255
def components(self) -> list["Component"]:
236256
"""List of ``Component`` objects inside of the component."""
@@ -367,7 +387,9 @@ def reset_placement(self):
367387

368388
@check_input_types
369389
@ensure_design_is_active
370-
def add_component(self, name: str, template: Optional["Component"] = None) -> "Component":
390+
def add_component(
391+
self, name: str, template: Optional["Component"] = None, instance_name: str = None
392+
) -> "Component":
371393
"""Add a new component under this component within the design assembly.
372394
373395
Parameters
@@ -383,18 +405,20 @@ def add_component(self, name: str, template: Optional["Component"] = None) -> "C
383405
Component
384406
New component with no children in the design assembly.
385407
"""
386-
new_comp = Component(name, self, self._grpc_client, template=template)
408+
new_comp = Component(
409+
name, self, self._grpc_client, template=template, instance_name=instance_name
410+
)
387411
master = new_comp._master_component
388412
master_id = new_comp.id.split("/")[-1]
389-
390413
for comp in self._master_component.occurrences:
391414
if comp.id != self.id:
392415
comp.components.append(
393416
Component(
394417
name,
395418
comp,
396419
self._grpc_client,
397-
template,
420+
template=template,
421+
instance_name=instance_name,
398422
preexisting_id=f"{comp.id}/{master_id}",
399423
master_component=master,
400424
read_existing_comp=True,

tests/integration/test_design.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,24 @@ def test_named_selections(modeler: Modeler):
371371
assert len(design.named_selections) == 3
372372

373373

374+
def test_add_component_with_instance_name(modeler: Modeler):
375+
design = modeler.create_design("DesignHierarchyExample")
376+
circle_sketch = Sketch()
377+
circle_sketch.circle(Point2D([10, 10], UNITS.mm), Distance(10, UNITS.mm))
378+
379+
slot_sketch = Sketch()
380+
slot_sketch.slot(Point2D([40, 10], UNITS.mm), Distance(20, UNITS.mm), Distance(10, UNITS.mm))
381+
382+
nested_component = design.add_component("NestedComponent")
383+
nested_component2 = design.add_component("NestedComponent2", instance_name="first instance")
384+
385+
assert nested_component.name == "NestedComponent"
386+
assert nested_component.instance_name == ""
387+
388+
assert nested_component2.name == "NestedComponent2"
389+
assert nested_component2.instance_name == "first instance"
390+
391+
374392
def test_faces_edges(modeler: Modeler):
375393
"""Test for verifying the correct creation and usage of ``Face`` and
376394
``Edge`` objects.

0 commit comments

Comments
 (0)