Coverage for backend/django/flowsheetInternals/unitops/logic/insert_translator_block.py: 100%

25 statements  

« prev     ^ index     » next       coverage.py v7.10.7, created at 2026-06-23 21:51 +0000

1from flowsheetInternals.unitops.models.Port import Port 

2from flowsheetInternals.unitops.models.simulation_object_factory import SimulationObjectFactory 

3from typing import TYPE_CHECKING 

4if TYPE_CHECKING: 

5 from flowsheetInternals.unitops.models import SimulationObject 

6 

7def insert_translator_block(stream: "SimulationObject") -> "SimulationObject": 

8 """ 

9 Inserts a translator block into the flowsheet, connecting it to the given simulation object. 

10 

11 Args: 

12 stream: The intermediate stream that will be split then connected to the translator block. 

13 Returns: 

14 The newly created translator block. 

15 """ 

16 # New position with offsets 

17 stream_graphic = stream.graphicObject.last() 

18 x_pos = stream_graphic.x + (stream_graphic.width / 2) 

19 y_pos = stream_graphic.y + (stream_graphic.height / 2) 

20 

21 # Create a new translator block 

22 translator_block = SimulationObjectFactory.create_simulation_object( 

23 coordinates={'x': x_pos, 'y': y_pos}, 

24 objectType="translator", 

25 create_attached_streams=False, 

26 flowsheet=stream.flowsheet, 

27 ) 

28 translator_ports = {port.key: port for port in translator_block.ports.filter(key__in=["inlet", "outlet"])} 

29 

30 # Split the selected stream 

31 outlet_stream = stream 

32 inlet_stream = stream.split_stream() 

33 

34 # Connect streams to the translator block's ports 

35 translator_ports["inlet"].stream = outlet_stream 

36 translator_ports["outlet"].stream = inlet_stream 

37 

38 Port.objects.bulk_update([translator_ports["inlet"], translator_ports["outlet"]], ["stream"]) 

39 

40 # Set stream positions based on the translator block's position 

41 translator_block_graphic = translator_block.graphicObject.last() 

42 translator_block_coordinates = {'x': translator_block_graphic.x, 'y': translator_block_graphic.y} 

43 upstream_graphic_object = translator_ports["inlet"].stream.graphicObject.last() 

44 downstream_graphic_object = translator_ports["outlet"].stream.graphicObject.last() 

45 

46 upstream_graphic_object.x = translator_block_coordinates['x'] - (upstream_graphic_object.width + 20) 

47 upstream_graphic_object.y = translator_block_coordinates['y'] + (translator_block_graphic.height / 2) - (upstream_graphic_object.height / 2) 

48 downstream_graphic_object.x = translator_block_coordinates['x'] + translator_block_graphic.width + 20 

49 downstream_graphic_object.y = translator_block_coordinates['y'] + (translator_block_graphic.height / 2) - (downstream_graphic_object.height / 2) 

50 

51 upstream_graphic_object.save() 

52 downstream_graphic_object.save() 

53 

54 return translator_block