2020from PySide6 .QtGui import (QIcon , QPixmap , QImage , QStandardItemModel , QStandardItem , QColor , QDropEvent , QWheelEvent )
2121
2222from VeraGrid .Gui .Diagrams .MapWidget .Branches .map_line_container import MapLineContainer
23+ from VeraGrid .Gui .Diagrams .MapWidget .Branches .map_line_polyline import MapLinePolyline
2324from VeraGrid .Gui .Diagrams .MapWidget .Injections .map_injections_template_graphics import MapInjectionTemplateGraphicItem
2425from VeraGrid .Gui .Diagrams .SchematicWidget .Substation .bus_graphics import BusGraphicItem
2526from VeraGrid .Gui .Diagrams .generic_graphics import GenericDiagramWidget
@@ -371,20 +372,22 @@ def _get_selected_line_containers(self) -> List[MAP_BRANCH_GRAPHIC_TYPES]:
371372 """
372373 selected_lines : List [MAP_BRANCH_GRAPHIC_TYPES ] = list ()
373374 selected_line_ids : set [int ] = set ()
375+ item : QGraphicsItem
374376 for item in self .diagram_scene .selectedItems ():
375- line_container = None
376- if isinstance (item , (MapAcLine , MapDcLine , MapHvdcLine , MapFluidPathLine )):
377- line_container = item
378- elif hasattr (item , "container" ) and isinstance (item .container , (MapAcLine , MapDcLine , MapHvdcLine , MapFluidPathLine )):
379- line_container = item .container
380- elif hasattr (item , "line_container" ) and isinstance (item .line_container , (MapAcLine , MapDcLine , MapHvdcLine , MapFluidPathLine )):
381- line_container = item .line_container
382-
383- if line_container is not None :
384- line_id = id (line_container )
377+ if isinstance (item , (MapAcLine ,
378+ MapDcLine ,
379+ MapHvdcLine ,
380+ MapFluidPathLine )):
381+ line_id = id (item )
385382 if line_id not in selected_line_ids :
386383 selected_line_ids .add (line_id )
387- selected_lines .append (line_container )
384+ selected_lines .append (item )
385+ elif isinstance (item , MapLinePolyline ):
386+ line_id = id (item .container )
387+ if line_id not in selected_line_ids :
388+ selected_line_ids .add (line_id )
389+ selected_lines .append (item .container )
390+
388391 return selected_lines
389392
390393 def _sync_line_handle_visibility (self ) -> None :
@@ -2278,8 +2281,7 @@ def consolidate_object_coordinates(self):
22782281
22792282 return
22802283
2281- def split_line_to_substation (
2282- self , preferred_line_container : MapAcLine | MapDcLine | MapHvdcLine | MapFluidPathLine | None = None ):
2284+ def split_line_to_substation (self ):
22832285 """
22842286 Split a selected line and connect it to a selected substation.
22852287 This creates two new lines: one from the original "from" bus to the selected substation,
@@ -2291,21 +2293,7 @@ def split_line_to_substation(
22912293 selected_lines = self .get_selected_line_segments_tuple ()
22922294 selected_substations = self .get_selected_substations_tuple ()
22932295
2294- if len (selected_substations ) != 1 :
2295- msg = QMessageBox ()
2296- msg .setIcon (QMessageBox .Icon .Information )
2297- msg .setText ("Please select exactly one substation." )
2298- msg .setWindowTitle ("Selection Error" )
2299- msg .exec ()
2300- return
2301-
2302- # Pick the line that was explicitly clicked from the context menu when available.
2303- if preferred_line_container is not None :
2304- line_api = preferred_line_container .api_object
2305- line_graphic = preferred_line_container
2306- elif len (selected_lines ) == 1 :
2307- line_api , line_graphic = selected_lines [0 ]
2308- else :
2296+ if len (selected_lines ) != 1 or len (selected_substations ) != 1 :
23092297 msg = QMessageBox ()
23102298 msg .setIcon (QMessageBox .Icon .Information )
23112299 msg .setText ("Please select exactly one line and one substation." )
@@ -2314,6 +2302,7 @@ def split_line_to_substation(
23142302 return
23152303
23162304 # Get the API objects
2305+ line_api , line_graphic = selected_lines [0 ]
23172306 substation_api , substation_graphic = selected_substations [0 ]
23182307 original_line_container = line_graphic
23192308
@@ -2457,7 +2446,7 @@ def split_line_to_substation(
24572446 protection_rating_factor = line_api .protection_rating_factor ,
24582447 circuit_idx = line_api .circuit_idx )
24592448
2460- # Line 2: from new bus to bus_to
2449+ # Line 2: from new bus to bus_to
24612450 line2 = Line (name = f"{ line_api .name } _2" ,
24622451 active = line_api .active ,
24632452 bus_from = suitable_bus ,
@@ -2495,7 +2484,7 @@ def split_line_to_substation(
24952484 for i in range (1 , closest_segment_idx + 1 ):
24962485 line1 .locations .add_location (lat = waypoints [i ][0 ], long = waypoints [i ][1 ], alt = 0.0 )
24972486
2498- # --- Assign offset waypoints ---
2487+ # --- Assign offset waypoints ---
24992488 # Add the 'backwards' point as the last waypoint for line1
25002489 line1 .locations .add_location (lat = new_lat1 , long = new_lon1 , alt = 0.0 )
25012490 # line1.locations.add_location(lat=substation_lat, long=substation_lon, alt=0.0)
@@ -2533,7 +2522,7 @@ def split_line_to_substation(
25332522 def _closest_point_on_segment (self , lat1 , lon1 , lat2 , lon2 , lat3 , lon3 ):
25342523 """
25352524 Find the closest point on a line segment to a given point using geographic coordinates.
2536-
2525+
25372526 :param lat1, lon1: Coordinates of the first endpoint of the segment
25382527 :param lat2, lon2: Coordinates of the second endpoint of the segment
25392528 :param lat3, lon3: Coordinates of the point to find the closest point to
@@ -2631,7 +2620,7 @@ def create_t_joint_to_substation(self):
26312620 Create a T-joint connection between a line and a selected substation using a selected waypoint.
26322621 This replaces the waypoint with a new substation, splits the original line into two segments,
26332622 and creates a new line connecting the selected substation to the new substation at the waypoint.
2634-
2623+
26352624 The user only needs to select a waypoint and a substation. The line is automatically determined
26362625 from the waypoint.
26372626 """
@@ -3014,25 +3003,17 @@ def create_t_joint_to_substation(self):
30143003 msg .setWindowTitle ("Operation Successful" )
30153004 msg .exec ()
30163005
3017- def change_line_connection (self , preferred_line_container : MapAcLine | MapDcLine | MapHvdcLine | MapFluidPathLine | None = None ):
3006+ def change_line_connection (self ):
30183007
30193008 selected_lines = self .get_selected_line_segments_tuple ()
30203009 selected_substations = self .get_selected_substations_tuple ()
30213010
3022- if len (selected_substations ) != 2 :
3023- self .gui .show_error_toast (message = "Please select exactly two substations." )
3024- return
3025-
3026- # Get the API objects
3027- if preferred_line_container is not None :
3028- line_api = preferred_line_container .api_object
3029- line_graphic = preferred_line_container
3030- elif len (selected_lines ) == 1 :
3031- line_api , line_graphic = selected_lines [0 ]
3032- else :
3011+ if len (selected_lines ) != 1 or len (selected_substations ) != 2 :
30333012 self .gui .show_error_toast (message = "Please select exactly one line and two substations." )
30343013 return
30353014
3015+ # Get the API objects
3016+ line_api , line_graphic = selected_lines [0 ]
30363017 substation_api_1 , substation_graphic_1 = selected_substations [0 ]
30373018 substation_api_2 , substation_graphic_2 = selected_substations [1 ]
30383019
0 commit comments