개정판 260e8739
issue #000: add file
Change-Id: I95995339807b6824c92985e21d241d9295ed2288
DTI_PID/DTI_PID/Commands/ReplaceInsertCommand.py | ||
---|---|---|
1 |
# coding: utf-8 |
|
2 |
""" This is Load command module """ |
|
3 |
import os.path |
|
4 |
import sys, math |
|
5 |
from enum import Enum |
|
6 |
import uuid |
|
7 |
from PyQt5.QtCore import * |
|
8 |
from PyQt5.QtWidgets import * |
|
9 |
from PyQt5.QtGui import * |
|
10 |
from AppDocData import AppDocData |
|
11 |
from AbstractCommand import AbstractCommand |
|
12 |
from QtImageViewer import QtImageViewer |
|
13 |
|
|
14 |
from EngineeringAbstractItem import QEngineeringAbstractItem |
|
15 |
from EngineeringLineItem import QEngineeringLineItem |
|
16 |
from SymbolSvgItem import SymbolSvgItem |
|
17 |
from EngineeringEquipmentItem import QEngineeringEquipmentItem |
|
18 |
from EngineeringInstrumentItem import QEngineeringInstrumentItem |
|
19 |
from EngineeringVendorItem import QEngineeringVendorItem |
|
20 |
|
|
21 |
|
|
22 |
class ReplaceInsertCommand(AbstractCommand): |
|
23 |
CONDITION_REPLACE = {'HasCon':['item.has_connection'], 'HasNoCon':['not item.has_connection'], 'ConLine':['False'], 'All':['True']} |
|
24 |
CONDITION_INSERT = {'HasCon':['conn.connetionItem'], 'HasNoCon':['not conn.connectedItem'], 'ConLine':['conn.connectedItem and type(conn.connectedItem) is QEngineeringLineItem', 'conn.connectedItem.is_piping(True)'], 'All':['True']} |
|
25 |
|
|
26 |
display_message = pyqtSignal(Enum, str) |
|
27 |
|
|
28 |
def __init__(self): |
|
29 |
super(ReplaceInsertCommand, self).__init__(None) |
|
30 |
|
|
31 |
def execute(self, scene, find_symbol, replace_symbol, replace_action, condition, custom=None, strict=False, auto=True): |
|
32 |
"""replace or insert symbol""" |
|
33 |
|
|
34 |
from App import App |
|
35 |
from AppDocData import AppDocData |
|
36 |
from AppDocData import MessageType |
|
37 |
|
|
38 |
app_doc_data = AppDocData.instance() |
|
39 |
try: |
|
40 |
if condition == 'Custom': |
|
41 |
conditions = [text for text in custom.split('\n')] |
|
42 |
else: |
|
43 |
conditions = ReplaceInsertCommand.CONDITION_REPLACE[condition] if replace_action == 'Replace' else ReplaceInsertCommand.CONDITION_INSERT[condition] |
|
44 |
|
|
45 |
symbol_items = [item for item in scene.items() |
|
46 |
if issubclass(type(item), SymbolSvgItem) and find_symbol == item.name] if find_symbol != 'Equipment Package' \ |
|
47 |
else [item for item in scene.items() |
|
48 |
if (issubclass(type(item), QEngineeringVendorItem) or issubclass(type(item), QEngineeringEquipmentItem)) and item.pack_type == 'Equipment Package'] |
|
49 |
|
|
50 |
new_symbols = [] |
|
51 |
if replace_action == 'Replace' and find_symbol != 'Equipment Package': |
|
52 |
# replace |
|
53 |
for item in reversed(symbol_items): |
|
54 |
for condition in conditions: |
|
55 |
if condition and not eval(condition): |
|
56 |
symbol_items.remove(item) |
|
57 |
break |
|
58 |
|
|
59 |
if not symbol_items: |
|
60 |
return |
|
61 |
|
|
62 |
for item in symbol_items: |
|
63 |
scenePos = item.mapToScene(item.transformOriginPoint()) |
|
64 |
item.transfer.onRemoved.emit(item) |
|
65 |
|
|
66 |
svg = QtImageViewer.createSymbolObject(replace_symbol) |
|
67 |
new_symbols.append([svg, scenePos, item.angle, item.flip]) |
|
68 |
|
|
69 |
for item in new_symbols: |
|
70 |
QtImageViewer.matchSymbolToLine(scene, item[0], item[1], angle=item[2], flip=item[3], strict=strict, auto=auto) |
|
71 |
item[0].bind_close_items() |
|
72 |
|
|
73 |
elif replace_action == 'Insert' and find_symbol != 'Equipment Package': |
|
74 |
# insert |
|
75 |
if not symbol_items: |
|
76 |
return |
|
77 |
|
|
78 |
for item in symbol_items: |
|
79 |
for conn in item.connectors: |
|
80 |
target = True |
|
81 |
for condition in conditions: |
|
82 |
if condition and not eval(condition): |
|
83 |
target = False |
|
84 |
break |
|
85 |
|
|
86 |
if target: |
|
87 |
scenePos = conn.sceneBoundingRect().center() |
|
88 |
svg = QtImageViewer.createSymbolObject(replace_symbol) |
|
89 |
new_symbols.append([svg, scenePos]) |
|
90 |
|
|
91 |
for item in new_symbols: |
|
92 |
QtImageViewer.matchSymbolToLine(scene, item[0], item[1], strict=strict, auto=auto) |
|
93 |
elif replace_action == 'Insert' and find_symbol == 'Equipment Package': |
|
94 |
if not symbol_items: |
|
95 |
return |
|
96 |
|
|
97 |
items = [item for item in scene.items() \ |
|
98 |
if (issubclass(type(item), SymbolSvgItem) or type(item) is QEngineeringLineItem) and item.has_connection] |
|
99 |
|
|
100 |
for package in symbol_items: |
|
101 |
x, y = package.sceneBoundingRect().x(), package.sceneBoundingRect().y() |
|
102 |
targets = [] |
|
103 |
for item in items: |
|
104 |
for conn in item.connectors[:2]: |
|
105 |
if conn.parentItem().has_connection and not conn.connectedItem and package.includes(conn, margin = 100): |
|
106 |
matches = [target for target in targets if target.parentItem() is conn.parentItem()] |
|
107 |
if not matches: |
|
108 |
targets.append(conn) |
|
109 |
else: |
|
110 |
ox, oy = abs(x - matches[0].sceneBoundingRect().x()), abs(y - matches[0].sceneBoundingRect().y()) |
|
111 |
nx, ny = abs(x - conn.sceneBoundingRect().x()), abs(y - conn.sceneBoundingRect().y()) |
|
112 |
old_dist = math.sqrt(ox * ox + oy * oy) |
|
113 |
new_dist = math.sqrt(nx * nx + ny * ny) |
|
114 |
if old_dist > new_dist: |
|
115 |
targets.remove(matches[0]) |
|
116 |
targets.append(conn) |
|
117 |
|
|
118 |
for target in targets: |
|
119 |
svg = QtImageViewer.createSymbolObject(replace_symbol) |
|
120 |
QtImageViewer.matchSymbolToLine(scene, svg, target.sceneBoundingRect().center(), strict=strict, auto=auto) |
|
121 |
|
|
122 |
for conn in svg.connectors: |
|
123 |
if not conn.connectedItem: |
|
124 |
conn.connect(package) |
|
125 |
break |
|
126 |
except Exception as ex: |
|
127 |
message = f"error occurred({repr(ex)}) in {sys.exc_info()[-1].tb_frame.f_code.co_filename}:" \ |
|
128 |
f"{sys.exc_info()[-1].tb_lineno}" |
|
129 |
self.display_message.emit(MessageType.Error, message) |
내보내기 Unified diff