Coverage for backend/django/flowsheetInternals/unitops/logic/insert_translator_block.py: 100%
26 statements
« prev ^ index » next coverage.py v7.10.7, created at 2026-07-22 05:22 +0000
« prev ^ index » next coverage.py v7.10.7, created at 2026-07-22 05:22 +0000
1from flowsheetInternals.unitops.models.Port import Port
2from flowsheetInternals.unitops.models.simulation_object_factory import SimulationObjectFactory
3from flowsheetInternals.unitops.services.edit_operations.recorder import (
4 tracked_bulk_update,
5)
6from typing import TYPE_CHECKING
7if TYPE_CHECKING:
8 from flowsheetInternals.unitops.models import SimulationObject
10def insert_translator_block(stream: "SimulationObject") -> "SimulationObject":
11 """
12 Inserts a translator block into the flowsheet, connecting it to the given simulation object.
14 Args:
15 stream: The intermediate stream that will be split then connected to the translator block.
16 Returns:
17 The newly created translator block.
18 """
19 # New position with offsets
20 stream_graphic = stream.graphicObject.last()
21 x_pos = stream_graphic.x + (stream_graphic.width / 2)
22 y_pos = stream_graphic.y + (stream_graphic.height / 2)
24 # Create a new translator block
25 translator_block = SimulationObjectFactory.create_simulation_object(
26 coordinates={'x': x_pos, 'y': y_pos},
27 objectType="translator",
28 create_attached_streams=False,
29 flowsheet=stream.flowsheet_state.flowsheet,
30 )
31 translator_ports = {port.key: port for port in translator_block.ports.filter(key__in=["inlet", "outlet"])}
33 # Split the selected stream
34 outlet_stream = stream
35 inlet_stream = stream.split_stream()
37 # Connect streams to the translator block's ports
38 translator_ports["inlet"].stream = outlet_stream
39 translator_ports["outlet"].stream = inlet_stream
41 tracked_bulk_update(
42 Port.objects,
43 [translator_ports["inlet"], translator_ports["outlet"]],
44 ["stream"],
45 )
47 # Set stream positions based on the translator block's position
48 translator_block_graphic = translator_block.graphicObject.last()
49 translator_block_coordinates = {'x': translator_block_graphic.x, 'y': translator_block_graphic.y}
50 upstream_graphic_object = translator_ports["inlet"].stream.graphicObject.last()
51 downstream_graphic_object = translator_ports["outlet"].stream.graphicObject.last()
53 upstream_graphic_object.x = translator_block_coordinates['x'] - (upstream_graphic_object.width + 20)
54 upstream_graphic_object.y = translator_block_coordinates['y'] + (translator_block_graphic.height / 2) - (upstream_graphic_object.height / 2)
55 downstream_graphic_object.x = translator_block_coordinates['x'] + translator_block_graphic.width + 20
56 downstream_graphic_object.y = translator_block_coordinates['y'] + (translator_block_graphic.height / 2) - (downstream_graphic_object.height / 2)
58 upstream_graphic_object.save()
59 downstream_graphic_object.save()
61 return translator_block