Skip to content

Commit af1b65d

Browse files
committed
updating to return Emit node instead of ID
1 parent 6b1ba5a commit af1b65d

File tree

3 files changed

+35
-85
lines changed

3 files changed

+35
-85
lines changed

src/ansys/aedt/core/emit_core/emit_schematic.py

Lines changed: 22 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
# SOFTWARE.
2424

2525

26+
from ansys.aedt.core.emit_core.nodes.emit_node import EmitNode
2627
from ansys.aedt.core.generic.general_methods import pyaedt_function_handler
2728

2829

@@ -61,7 +62,7 @@ def _emit_com_module(self):
6162
raise RuntimeError(f"Failed to retrieve EmitCom module: {e}")
6263

6364
@pyaedt_function_handler
64-
def create_component(self, component_type: str, name: str = None, library: str = None) -> int:
65+
def create_component(self, component_type: str, name: str = None, library: str = None) -> EmitNode:
6566
"""Create a component.
6667
6768
Parameters
@@ -75,8 +76,8 @@ def create_component(self, component_type: str, name: str = None, library: str =
7576
7677
Returns
7778
-------
78-
int
79-
The ID of the created component.
79+
EmitNode
80+
The EmitNode of the created component.
8081
8182
Raises
8283
------
@@ -119,20 +120,22 @@ def create_component(self, component_type: str, name: str = None, library: str =
119120
f"Using exact match component '{component.name}' from library '{component.component_library}"
120121
"' for type '{component_type}'."
121122
)
123+
revision = self.emit_instance.results.get_revision()
122124

123125
# Create the component using the EmitCom module
124126
new_component_id = self._emit_com_module.CreateEmitComponent(
125127
name, component.name, component.component_library
126128
)
127-
return new_component_id
129+
component_node = revision._get_node(node_id=new_component_id)
130+
return component_node
128131
except Exception as e:
129132
self.emit_instance.logger.error(f"Failed to create component '{name}' of type '{component_type}': {e}")
130133
raise RuntimeError(f"Failed to create component of type '{component_type}': {e}")
131134

132135
@pyaedt_function_handler
133136
def create_radio_antenna(
134137
self, radio_type: str, radio_name: str = None, antenna_name: str = None, library: str = None
135-
) -> tuple[int, int]:
138+
) -> tuple[EmitNode, EmitNode]:
136139
"""Create a new radio and antenna and connect them.
137140
138141
Parameters
@@ -152,8 +155,8 @@ def create_radio_antenna(
152155
153156
Returns
154157
-------
155-
tuple
156-
A tuple containing the IDs of the created radio and antenna.
158+
tuple[EmitNode, EmitNode]
159+
A tuple containing the EmitNode of the created radio and antenna.
157160
158161
Raises
159162
------
@@ -165,74 +168,38 @@ def create_radio_antenna(
165168
library = library or ""
166169

167170
try:
168-
new_radio_id = self.create_component(radio_type, radio_name, library)
169-
new_antenna_id = self.create_component("Antenna", antenna_name, "Antennas")
170-
if new_radio_id and new_antenna_id:
171-
self.connect_components(new_antenna_id, new_radio_id) # Connect antenna to radio
172-
return new_radio_id, new_antenna_id
171+
new_radio = self.create_component(radio_type, radio_name, library)
172+
new_antenna = self.create_component("Antenna", antenna_name, "Antennas")
173+
if new_radio and new_antenna:
174+
self.connect_components(new_antenna.name, new_radio.name) # Connect antenna to radio
175+
return new_radio, new_antenna
173176
except Exception as e:
174177
self.emit_instance.logger.error(f"Failed to create radio of type '{radio_type}' or antenna: {e}")
175178
raise RuntimeError(f"Failed to create radio of type '{radio_type}' or antenna: {e}")
176179

177180
@pyaedt_function_handler
178-
def connect_components(self, component_id_1: int, component_id_2: int):
181+
def connect_components(self, component_name_1: str, component_name_2: str) -> None:
179182
"""Connect two components in the schematic.
180183
181184
Parameters
182185
----------
183-
component_id_1 : str
184-
ID of the first component.
185-
component_id_2 : str
186-
ID of the second component.
186+
component_1 : str
187+
Name of the first component.
188+
component_2 : str
189+
Name of the second component.
187190
188191
Raises
189192
------
190193
RuntimeError
191194
If the connection fails.
192195
"""
193196
try:
194-
component_name_1 = self.get_component_properties(component_id_1, "Name")
195-
component_name_2 = self.get_component_properties(component_id_2, "Name")
196197
self.emit_instance._oeditor.PlaceComponent(component_name_1, component_name_2)
197198
self.emit_instance.logger.info(
198199
f"Successfully connected components '{component_name_1}' and '{component_name_2}'."
199200
)
200201
except Exception as e:
201202
self.emit_instance.logger.error(
202-
f"Failed to connect components '{component_id_1}' and '{component_id_2}': {e}"
203+
f"Failed to connect components '{component_name_1}' and '{component_name_2}': {e}"
203204
)
204-
raise RuntimeError(f"Failed to connect components '{component_id_1}' and '{component_id_2}': {e}")
205-
206-
@pyaedt_function_handler
207-
def get_component_properties(self, component_id: int, property_key: str = None) -> dict:
208-
"""Get properties of a component.
209-
210-
Parameters
211-
----------
212-
component_id : int
213-
ID of the component.
214-
property_key : str, optional
215-
Specific property key to retrieve. If ``None``, all properties are returned.
216-
217-
Returns
218-
-------
219-
dict or str
220-
Dictionary containing all properties of the component if `property_key` is ``None``.
221-
Otherwise, the value of the specified property key.
222-
223-
Raises
224-
------
225-
KeyError
226-
If the specified property key is not found.
227-
"""
228-
try:
229-
props = self._emit_com_module.GetEmitNodeProperties(0, component_id, True)
230-
props_dict = {prop.split("=", 1)[0]: prop.split("=", 1)[1] for prop in props}
231-
if property_key is None:
232-
return props_dict
233-
if property_key in props_dict:
234-
return props_dict[property_key]
235-
raise KeyError(f"Property key '{property_key}' not found.")
236-
except Exception as e:
237-
self.emit_instance.logger.error(f"Failed to retrieve properties for component '{component_id}': {e}")
238-
raise RuntimeError(f"Failed to retrieve properties for component '{component_id}': {e}")
205+
raise RuntimeError(f"Failed to connect components '{component_name_1}' and '{component_name_2}': {e}")

src/ansys/aedt/core/modeler/circuits/primitives_circuit.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ def create_interface_port(self, name, location=None, angle=0):
358358
arg2 = ["NAME:Attributes", "Page:=", 1, "X:=", xpos, "Y:=", ypos, "Angle:=", angle, "Flip:=", False]
359359
comp_name = self.oeditor.CreateIPort(arg1, arg2)
360360

361-
id = int(comp_name.split(";")[1])
361+
comp_id = int(comp_name.split(";")[1])
362362
self.add_id_to_component(id, comp_name)
363363
# return id, self.components[id].composed_name
364364
for el in self.components:
@@ -475,7 +475,7 @@ def create_gnd(self, location=None, angle=0, page=1):
475475
["NAME:GroundProps"],
476476
["NAME:Attributes", "Page:=", page, "X:=", xpos, "Y:=", ypos, "Angle:=", angle, "Flip:=", False],
477477
)
478-
id = int(name.split(";")[1])
478+
comp_id = int(name.split(";")[1])
479479
self.add_id_to_component(id, name)
480480
# return id, self.components[id].composed_name
481481
for el in self.components:
@@ -1389,12 +1389,12 @@ def refresh_all_ids(self):
13891389
return len(self.components)
13901390

13911391
@pyaedt_function_handler()
1392-
def add_id_to_component(self, id, name=None):
1392+
def add_id_to_component(self, comp_id, name=None):
13931393
"""Add an ID to a component.
13941394
13951395
Parameters
13961396
----------
1397-
id : int
1397+
comp_id : int
13981398
ID to assign to the component.
13991399
14001400
Returns
@@ -1420,7 +1420,7 @@ def add_id_to_component(self, id, name=None):
14201420
obj = self.oeditor.GetAllElements()
14211421
for el in obj:
14221422
name = el.split(";")
1423-
if len(name) > 1 and str(id) == name[1]:
1423+
if len(name) > 1 and str(comp_id) == name[1]:
14241424
o = CircuitComponent(self, tabname=self.tab_name)
14251425
o.name = name[0]
14261426
if len(name) > 2:

tests/system/solvers/test_26_emit.py

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@
5050
from ansys.aedt.core.emit_core.emit_constants import ResultType
5151
from ansys.aedt.core.emit_core.emit_constants import TxRxMode
5252
from ansys.aedt.core.emit_core.nodes import generated
53+
from ansys.aedt.core.emit_core.nodes.emit_node import EmitNode
5354

54-
# from ansys.aedt.core.emit_core.nodes.emit_node import EmitNode
5555
# from ansys.aedt.core.emit_core.nodes.generated import *
5656
# from ansys.aedt.core.emit_core.results.revision import Revision
5757
from ansys.aedt.core.modeler.circuits.primitives_emit import EmitAntennaComponent
@@ -1636,7 +1636,7 @@ def test_28_create_component(self, add_app):
16361636
self.aedtapp = add_app(project_name="create_component", application=Emit)
16371637
self.aedtapp.logger.info = MagicMock()
16381638
new_radio = self.aedtapp.schematic.create_component("MICS")
1639-
assert new_radio == 3
1639+
assert isinstance(new_radio, EmitNode)
16401640
self.aedtapp.logger.info.assert_called_with(
16411641
r"Using component 'MICS' from library 'Radios\Commercial Unlicensed Systems\Medical' for type 'MICS'."
16421642
)
@@ -1660,8 +1660,8 @@ def test_28_create_component(self, add_app):
16601660
def test_29_create_radio_antenna(self, add_app):
16611661
self.aedtapp = add_app(project_name="radio_antenna", application=Emit)
16621662
new_radio, new_antenna = self.aedtapp.schematic.create_radio_antenna("MICS", "Radio", "Antenna")
1663-
assert new_radio == 3
1664-
assert new_antenna == 4
1663+
assert isinstance(new_radio, EmitNode)
1664+
assert isinstance(new_antenna, EmitNode)
16651665
with pytest.raises(RuntimeError) as e:
16661666
self.aedtapp.schematic.create_radio_antenna("WrongComponent", "Radio", "Antenna")
16671667
assert "Failed to create radio of type 'WrongComponent'" in str(e.value)
@@ -1672,28 +1672,11 @@ def test_30_connect_components(self, add_app):
16721672
self.aedtapp.logger.info = MagicMock()
16731673
new_radio = self.aedtapp.schematic.create_component("MICS")
16741674
new_antenna = self.aedtapp.schematic.create_component("Antenna")
1675-
self.aedtapp.schematic.connect_components(new_radio, new_antenna)
1675+
self.aedtapp.schematic.connect_components(new_radio.name, new_antenna.name)
16761676
self.aedtapp.logger.info.assert_called_with("Successfully connected components 'MICS' and 'Antenna'.")
16771677
with pytest.raises(RuntimeError) as e:
1678-
self.aedtapp.schematic.connect_components(new_radio, 6)
1678+
self.aedtapp.schematic.connect_components(new_radio.name, "WrongComponent")
16791679
assert (
1680-
"Failed to connect components '3' and '6': Failed to retrieve properties for component '6': "
1681-
"Failed to execute gRPC AEDT command: GetEmitNodeProperties"
1680+
"Failed to connect components 'MICS' and 'WrongComponent': "
1681+
"Failed to execute gRPC AEDT command: PlaceComponent"
16821682
) in str(e.value)
1683-
1684-
@pytest.mark.skipif(config["desktopVersion"] < "2025.2", reason="Skipped on versions earlier than 2025 R2.")
1685-
def test_31_get_component_properties(self, add_app):
1686-
self.aedtapp = add_app(project_name="component_properties", application=Emit)
1687-
new_radio = self.aedtapp.schematic.create_component("MICS")
1688-
new_radio_props = self.aedtapp.schematic.get_component_properties(new_radio)
1689-
assert isinstance(new_radio_props, dict)
1690-
assert new_radio_props["Name"] == "MICS"
1691-
assert new_radio_props["Type"] == "RadioNode"
1692-
assert new_radio_props["IconAlias"] == ":Radio"
1693-
new_radio_name = self.aedtapp.schematic.get_component_properties(new_radio, "Name")
1694-
assert new_radio_name == "MICS"
1695-
with pytest.raises(RuntimeError) as e:
1696-
self.aedtapp.schematic.get_component_properties(new_radio, "WrongProp")
1697-
assert ("Failed to retrieve properties for component '3': \"Property key 'WrongProp' not found.\"") in str(
1698-
e.value
1699-
)

0 commit comments

Comments
 (0)