Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: setting instance name during component creation #1382

Merged
merged 22 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
e7fb641
component name
umutsoysalansys Aug 28, 2024
5e38956
chore: adding changelog file 1382.added.md
pyansys-ci-bot Aug 28, 2024
6f945f2
Merge branch 'main' into feat/component_instance_name
umutsoysalansys Aug 28, 2024
a9feed8
update
umutsoysalansys Aug 30, 2024
7249b9b
unit tests fix
umutsoysalansys Sep 2, 2024
33c564d
Update src/ansys/geometry/core/designer/component.py
umutsoysalansys Sep 3, 2024
1f969b8
Update src/ansys/geometry/core/designer/component.py
umutsoysalansys Sep 3, 2024
41f69e7
Update src/ansys/geometry/core/designer/component.py
umutsoysalansys Sep 3, 2024
ed7dda8
Update tests/integration/test_design.py
umutsoysalansys Sep 3, 2024
89c25b7
Update tests/integration/test_design.py
umutsoysalansys Sep 3, 2024
0f72375
typos
umutsoysalansys Sep 3, 2024
2f8b908
Merge branch 'main' into feat/component_instance_name
umutsoysalansys Sep 3, 2024
de1cc6c
Update src/ansys/geometry/core/designer/component.py
RobPasMue Sep 3, 2024
e0101e6
Merge branch 'main' into feat/component_instance_name
RobPasMue Sep 3, 2024
65834eb
Update src/ansys/geometry/core/designer/component.py
umutsoysalansys Sep 3, 2024
ef0e66a
Update src/ansys/geometry/core/designer/component.py
RobPasMue Sep 4, 2024
212eac6
fix: align behavior
RobPasMue Sep 4, 2024
cef6c57
chore: auto fixes from pre-commit hooks
pre-commit-ci[bot] Sep 4, 2024
de31f46
ci: improve testing (double check name as well)
RobPasMue Sep 4, 2024
1d16826
Merge branch 'feat/component_instance_name' of https://github.com/ans…
RobPasMue Sep 4, 2024
6f92a49
Merge branch 'main' into feat/component_instance_name
RobPasMue Sep 4, 2024
ad711b2
Merge branch 'main' into feat/component_instance_name
RobPasMue Sep 5, 2024
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/1382.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
setting instance name during component creation
34 changes: 29 additions & 5 deletions src/ansys/geometry/core/designer/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ class Component:
template : Component, default: None
Template to create this component from. This creates an
instance component that shares a master with the template component.
instance_name: str, default: None
User defined optional name for the component instance.
preexisting_id : str, default: None
ID of a component pre-existing on the server side to use to create the component
on the client-side data model. If an ID is specified, a new component is not
Expand Down Expand Up @@ -160,6 +162,7 @@ def __init__(
parent_component: Union["Component", None],
grpc_client: GrpcClient,
template: Optional["Component"] = None,
instance_name: Optional[str] = None,
umutsoysalansys marked this conversation as resolved.
Show resolved Hide resolved
preexisting_id: str | None = None,
master_component: MasterComponent | None = None,
read_existing_comp: bool = False,
Expand All @@ -170,22 +173,34 @@ def __init__(
self._component_stub = ComponentsStub(self._grpc_client.channel)
self._bodies_stub = BodiesStub(self._grpc_client.channel)
self._commands_stub = CommandsStub(self._grpc_client.channel)

# Align instance name behavior with the server - empty string if None
instance_name = instance_name if instance_name else ""

if preexisting_id:
self._name = name
self._id = preexisting_id
self._instance_name = instance_name
else:
if parent_component:
template_id = template.id if template else ""
new_component = self._component_stub.Create(
CreateRequest(name=name, parent=parent_component.id, template=template_id)
CreateRequest(
name=name,
parent=parent_component.id,
template=template_id,
instance_name=instance_name,
)
)

# Remove this method call once we know Service sends correct ObjectPath id
self._id = new_component.component.id
self._name = new_component.component.name
self._instance_name = new_component.component.instance_name
umutsoysalansys marked this conversation as resolved.
Show resolved Hide resolved
else:
self._name = name
self._id = None
RobPasMue marked this conversation as resolved.
Show resolved Hide resolved
self._instance_name = instance_name

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

@property
def instance_name(self) -> str:
"""Name of the component instance."""
return self._instance_name

@property
def components(self) -> list["Component"]:
"""List of ``Component`` objects inside of the component."""
Expand Down Expand Up @@ -367,7 +387,9 @@ def reset_placement(self):

@check_input_types
@ensure_design_is_active
def add_component(self, name: str, template: Optional["Component"] = None) -> "Component":
def add_component(
self, name: str, template: Optional["Component"] = None, instance_name: str = None
) -> "Component":
"""Add a new component under this component within the design assembly.

Parameters
Expand All @@ -383,18 +405,20 @@ def add_component(self, name: str, template: Optional["Component"] = None) -> "C
Component
New component with no children in the design assembly.
"""
new_comp = Component(name, self, self._grpc_client, template=template)
new_comp = Component(
name, self, self._grpc_client, template=template, instance_name=instance_name
)
master = new_comp._master_component
master_id = new_comp.id.split("/")[-1]

for comp in self._master_component.occurrences:
if comp.id != self.id:
comp.components.append(
Component(
name,
comp,
self._grpc_client,
template,
template=template,
instance_name=instance_name,
preexisting_id=f"{comp.id}/{master_id}",
master_component=master,
read_existing_comp=True,
Expand Down
15 changes: 15 additions & 0 deletions tests/integration/test_design.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,21 @@ def test_named_selections(modeler: Modeler):
assert len(design.named_selections) == 3


def test_add_component_with_instance_name(modeler: Modeler):
design = modeler.create_design("DesignHierarchyExample")
circle_sketch = Sketch()
circle_sketch.circle(Point2D([10, 10], UNITS.mm), Distance(10, UNITS.mm))

slot_sketch = Sketch()
slot_sketch.slot(Point2D([40, 10], UNITS.mm), Distance(20, UNITS.mm), Distance(10, UNITS.mm))

nested_component = design.add_component("NestedComponent")
nested_component2 = design.add_component("NestedComponent2", instance_name="first instance")

assert nested_component.instance_name == ""
assert nested_component2.instance_name == "first instance"


def test_faces_edges(modeler: Modeler):
"""Test for verifying the correct creation and usage of ``Face`` and
``Edge`` objects.
Expand Down
Loading