@@ -118,14 +118,15 @@ def create_component(self, component_type: str, name: str = None, library: str =
118118 raise ValueError (f"Multiple components found for type '{ component_type } ', but no exact match." )
119119 self .emit_instance .logger .info (
120120 f"Using exact match component '{ component .name } ' from library '{ component .component_library } "
121- "' for type '{component_type}'."
121+ f "' for type '{ component_type } '."
122122 )
123+ stripped_component_name = component .name .strip ("'" )
123124 revision = self .emit_instance .results .get_revision ()
124-
125125 # Create the component using the EmitCom module
126126 new_component_id = self ._emit_com_module .CreateEmitComponent (
127- name , component . name , component .component_library
127+ name , stripped_component_name , component .component_library
128128 )
129+
129130 component_node = revision ._get_node (node_id = new_component_id )
130131 return component_node
131132 except Exception as e :
@@ -171,35 +172,116 @@ def create_radio_antenna(
171172 new_radio = self .create_component (radio_type , radio_name , library )
172173 new_antenna = self .create_component ("Antenna" , antenna_name , "Antennas" )
173174 if new_radio and new_antenna :
174- self .connect_components (new_antenna . name , new_radio . name ) # Connect antenna to radio
175+ self .connect_components (new_antenna , new_radio , "in" , "n1" ) # Connect antenna to radio
175176 return new_radio , new_antenna
176177 except Exception as e :
177178 self .emit_instance .logger .error (f"Failed to create radio of type '{ radio_type } ' or antenna: { e } " )
178179 raise RuntimeError (f"Failed to create radio of type '{ radio_type } ' or antenna: { e } " )
179180
180181 @pyaedt_function_handler
181- def connect_components (self , component_name_1 : str , component_name_2 : str ) -> None :
182+ def connect_components (
183+ self , component_1 : EmitNode , component_2 : EmitNode , component_port_1 : str = None , component_port_2 : str = None
184+ ) -> None :
182185 """Connect two components in the schematic.
183186
184187 Parameters
185188 ----------
186- component_1 : str
187- Name of the first component.
188- component_2 : str
189- Name of the second component.
189+ component_1 : EmitNode
190+ First component to connect.
191+ component_2 : EmitNode
192+ Second component to connect.
193+ component_port_1 : str, optional
194+ Port of the first component to connect. If ``None``, the default port is used.
195+ component_port_2 : str, optional
196+ Port of the second component to connect. If ``None``, the default port is used.
190197
191198 Raises
192199 ------
193200 RuntimeError
194201 If the connection fails.
195202 """
196203 try :
197- self .emit_instance ._oeditor .PlaceComponent (component_name_1 , component_name_2 )
204+ component_port_1 = component_port_1 or "n1"
205+ component_port_2 = component_port_2 or "n2"
206+ # Update the component ports to match the emit definition
207+ component_port_1 = self ._component_port_update (component_port_1 )
208+ component_port_2 = self ._component_port_update (component_port_2 )
209+ # Get the ports and their locations for both components
210+ ports_1 = self .emit_instance ._oeditor .GetComponentPorts (component_1 .name )
211+ port_locs_1 = {
212+ port : self .emit_instance ._oeditor .GetComponentPortLocation (component_1 .name , port ) for port in ports_1
213+ }
214+ if len (ports_1 ) == 1 :
215+ component_port_1 = ports_1 [0 ]
216+ if component_1 .properties ["Type" ] == "Multiplexer" :
217+ component_port_1 = component_port_1 .strip ("n" )
218+
219+ ports_2 = self .emit_instance ._oeditor .GetComponentPorts (component_2 .name )
220+ port_locs_2 = {
221+ port : self .emit_instance ._oeditor .GetComponentPortLocation (component_2 .name , port ) for port in ports_2
222+ }
223+ if len (ports_2 ) == 1 :
224+ component_port_2 = ports_2 [0 ]
225+ if component_2 .properties ["Type" ] == "Multiplexer" :
226+ component_port_2 = component_port_2 .strip ("n" )
227+
228+ # Validate that the specified ports exist in their respective components
229+ if component_port_1 not in port_locs_1 :
230+ raise ValueError (f"Port '{ component_port_1 } ' does not exist in component '{ component_1 .name } '." )
231+ if component_port_2 not in port_locs_2 :
232+ raise ValueError (f"Port '{ component_port_2 } ' does not exist in component '{ component_2 .name } '." )
233+ # Check if the ports are on the same side of the components
234+ component_1_antenna_pors_list = [x for x in component_1 .properties ["AntennaSidePorts" ].split ("|" )]
235+ component_1_radio_ports_list = [x for x in component_1 .properties ["RadioSidePorts" ].split ("|" )]
236+ if component_1 .properties ["Type" ] == "AntennaNode" :
237+ component_1_radio_ports_list = ["n" ]
238+ component_2_antenna_ports_list = [x for x in component_2 .properties ["AntennaSidePorts" ].split ("|" )]
239+ component_2_radio_ports_list = [x for x in component_2 .properties ["RadioSidePorts" ].split ("|" )]
240+ if component_2 .properties ["Type" ] == "AntennaNode" :
241+ component_2_radio_ports_list = ["n" ]
242+ if (
243+ component_port_1 [- 1 ] in component_1_antenna_pors_list
244+ and component_port_2 [- 1 ] in component_2_antenna_ports_list
245+ ) or (
246+ component_port_1 [- 1 ] in component_1_radio_ports_list
247+ and component_port_2 [- 1 ] in component_2_radio_ports_list
248+ ):
249+ raise RuntimeError ("Both ports are on the same side. Connection cannot be established." )
250+ # Move the first component to align with the second component's port
251+ delta_x = port_locs_2 [component_port_2 ][0 ] - port_locs_1 [component_port_1 ][0 ]
252+ delta_y = port_locs_2 [component_port_2 ][1 ] - port_locs_1 [component_port_1 ][1 ]
253+ self .emit_instance ._oeditor .Move (component_1 .name , delta_x , delta_y )
198254 self .emit_instance .logger .info (
199- f"Successfully connected components '{ component_name_1 } ' and '{ component_name_2 } '."
255+ f"Successfully connected components '{ component_1 . name } ' and '{ component_2 . name } '."
200256 )
201257 except Exception as e :
202258 self .emit_instance .logger .error (
203- f"Failed to connect components '{ component_name_1 } ' and '{ component_name_2 } ': { e } "
259+ f"Failed to connect components '{ component_1 .name } ' and '{ component_2 .name } ' with the given ports: { e } "
260+ )
261+ raise RuntimeError (
262+ f"Failed to connect components '{ component_1 .name } ' and '{ component_2 .name } ' with the given ports: { e } "
204263 )
205- raise RuntimeError (f"Failed to connect components '{ component_name_1 } ' and '{ component_name_2 } ': { e } " )
264+
265+ @pyaedt_function_handler
266+ def _component_port_update (self , input_port : str ) -> str :
267+ """Update the component port properties as emit definition.
268+
269+ Parameters
270+ ----------
271+ input_port : str
272+ Name of the input port to update.
273+
274+ Returns
275+ -------
276+ updated_port: int
277+ The updated port number after the update.
278+ """
279+
280+ port_number = input_port [- 1 ]
281+ if port_number in ["2" , "3" , "4" , "5" , "6" ]:
282+ updated_port = "n" + port_number
283+ elif port_number in ["1" , "n" ]:
284+ updated_port = "n1"
285+ else :
286+ raise ValueError (f"Invalid port format: '{ input_port } '" )
287+ return updated_port
0 commit comments