hytos / DTI_PID / DTI_PID / Shapes / EngineeringEquipmentItem.py @ 48cabe25
이력 | 보기 | 이력해설 | 다운로드 (13.3 KB)
1 |
# coding: utf-8
|
---|---|
2 |
""" This is engineering equipment item module """
|
3 |
|
4 |
import sys |
5 |
import os |
6 |
import math |
7 |
from PyQt5.QtGui import * |
8 |
from PyQt5.QtCore import * |
9 |
from PyQt5.QtSvg import * |
10 |
from PyQt5.QtWidgets import (QApplication, QGraphicsItem) |
11 |
|
12 |
from SymbolSvgItem import SymbolSvgItem |
13 |
from EngineeringConnectorItem import QEngineeringConnectorItem |
14 |
from EngineeringAbstractItem import QEngineeringAbstractItem |
15 |
from EngineeringTextItem import QEngineeringTextItem |
16 |
from UserInputAttribute import UserInputAttribute |
17 |
|
18 |
class QEngineeringEquipmentItem(SymbolSvgItem): |
19 |
""" This is engineering equipment item class """
|
20 |
|
21 |
clicked = pyqtSignal(QGraphicsSvgItem) |
22 |
ZVALUE = 10
|
23 |
EQUIP_COLUMN_LIST = None
|
24 |
EQUIP_COLOR = None
|
25 |
|
26 |
def __init__(self, path, uid=None, flip=0): |
27 |
from SymbolAttr import SymbolProp |
28 |
|
29 |
SymbolSvgItem.__init__(self, path, uid, flip=flip)
|
30 |
self.setZValue(QEngineeringEquipmentItem.ZVALUE)
|
31 |
|
32 |
self._properties = \
|
33 |
{ \ |
34 |
SymbolProp(None, 'ITEM_NO', 'Tag No', Expression="self.tag_no"):None, |
35 |
SymbolProp(None, 'Desc', 'String', Expression="self.desc"):None |
36 |
} |
37 |
|
38 |
if QEngineeringEquipmentItem.EQUIP_COLUMN_LIST is None: |
39 |
from AppDocData import AppDocData |
40 |
|
41 |
appDocData = AppDocData.instance() |
42 |
QEngineeringEquipmentItem.EQUIP_COLUMN_LIST = appDocData.getColNames('EQUIPMENT_DATA_LIST')
|
43 |
|
44 |
def texts(self): |
45 |
""" return text type of labels """
|
46 |
from EngineeringTextItem import QEngineeringTextItem |
47 |
|
48 |
return sorted([x for x in self.associations() if issubclass(type(x), QEngineeringTextItem)], key=lambda attr: attr.loc[1]) |
49 |
|
50 |
@property
|
51 |
def tag_no(self): |
52 |
""" getter of tag_no """
|
53 |
from QEngineeringTagNoTextItem import QEngineeringTagNoTextItem |
54 |
|
55 |
matches = [assoc for assoc in self.associations() if type(assoc) is QEngineeringTagNoTextItem] |
56 |
return matches[0].text() if matches else None |
57 |
|
58 |
@property
|
59 |
def desc(self): |
60 |
""" getter of desc """
|
61 |
from CodeTables import CodeTable |
62 |
|
63 |
try:
|
64 |
if self.tag_no: |
65 |
matches = [value for value in CodeTable.instance('EqpTagNames').values if value[1] == self.tag_no] |
66 |
if matches: return matches[0][2] |
67 |
except Exception as ex: |
68 |
from App import App |
69 |
from AppDocData import MessageType |
70 |
|
71 |
message = 'error occured({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, sys.exc_info()[-1].tb_lineno) |
72 |
App.mainWnd().addMessage.emit(MessageType.Error, message) |
73 |
|
74 |
return '' |
75 |
|
76 |
'''
|
77 |
@brief connect attribute
|
78 |
@author humkyung
|
79 |
@date 2018.05.03
|
80 |
'''
|
81 |
def connectAttribute(self, attributes, clear=True): |
82 |
from QEngineeringTagNoTextItem import QEngineeringTagNoTextItem |
83 |
|
84 |
if clear:
|
85 |
self.clear_attr_and_assoc_item()
|
86 |
|
87 |
# tag no text item will find owner itself
|
88 |
'''
|
89 |
try:
|
90 |
rect = self.sceneBoundingRect()
|
91 |
attrs = [attr for attr in attributes if type(attr) is QEngineeringTagNoTextItem]
|
92 |
for attr in attrs:
|
93 |
if rect.contains(attr.center()):
|
94 |
self.add_assoc_item(attr)
|
95 |
attr.onwer = self
|
96 |
|
97 |
# not have equipment name which is located inside equipment
|
98 |
if not 'Tag No' in self._associations or not self._associations['Tag No']:
|
99 |
minDist = None
|
100 |
selected = None
|
101 |
# get nearest text from equipment
|
102 |
for attr in attrs:
|
103 |
dx = attr.center().x() - rect.center().x()
|
104 |
dy = attr.center().y() - rect.center().y()
|
105 |
dist = math.sqrt(dx*dx + dy*dy)
|
106 |
if (minDist is None) or (dist < minDist):
|
107 |
minDist = dist
|
108 |
selected = attr
|
109 |
|
110 |
if selected is not None:
|
111 |
self.add_assoc_item(selected)
|
112 |
selected.onwer = self
|
113 |
except Exception as ex:
|
114 |
from App import App
|
115 |
from AppDocData import MessageType
|
116 |
|
117 |
message = 'error occured({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, sys.exc_info()[-1].tb_lineno)
|
118 |
App.mainWnd().addMessage.emit(MessageType.Error, message)
|
119 |
'''
|
120 |
|
121 |
def getColor(self): |
122 |
""" return equipment's color """
|
123 |
from AppDocData import AppDocData |
124 |
from DisplayColors import DisplayColors |
125 |
from DisplayColors import DisplayOptions |
126 |
from EngineeringAbstractItem import QEngineeringAbstractItem |
127 |
from EngineeringLineItem import QEngineeringLineItem |
128 |
|
129 |
#if DisplayOptions.DisplayByLineType == DisplayColors.instance().option:
|
130 |
if self.hover: |
131 |
return SymbolSvgItem.HOVER_COLOR
|
132 |
elif not QEngineeringEquipmentItem.EQUIP_COLOR: |
133 |
app_doc_data = AppDocData.instance() |
134 |
configs = app_doc_data.getConfigs('Equipment', 'Color') |
135 |
QEngineeringEquipmentItem.EQUIP_COLOR = configs[0].value if configs else QEngineeringAbstractItem.DEFAULT_COLOR |
136 |
|
137 |
return QEngineeringEquipmentItem.EQUIP_COLOR
|
138 |
#else:
|
139 |
# return SymbolSvgItem.getColor(self)
|
140 |
|
141 |
'''
|
142 |
@brief get attributes
|
143 |
@author humkyung
|
144 |
@date 2018.06.14
|
145 |
'''
|
146 |
'''
|
147 |
def getAttributes(self):
|
148 |
_attrs = {}
|
149 |
try:
|
150 |
from AppDocData import AppDocData
|
151 |
from EngineeringAbstractItem import QEngineeringAbstractItem
|
152 |
from EngineeringTextItem import QEngineeringTextItem
|
153 |
|
154 |
# 해당 Type의 attribute setting
|
155 |
docData = AppDocData.instance()
|
156 |
symbolAttrs = docData.getSymbolAttribute(self.type)
|
157 |
|
158 |
_texts = self.texts()
|
159 |
_symbols = self.symbols()
|
160 |
for attr in symbolAttrs:
|
161 |
if attr.AttributeType == 'Tag No' or attr.AttributeType == 'Size Text Item' or attr.AttributeType == 'Text Item' or attr.AttributeType == 'Valve Oper Code':
|
162 |
at = int(attr.AttrAt)
|
163 |
items = [text for text in _texts if QEngineeringAbstractItem.assoc_type(text) == attr.AttributeType]
|
164 |
if len(items) > at:
|
165 |
item = items[at]
|
166 |
_attrs[attr] = eval(attr.Expression) if attr.Expression else ''
|
167 |
else:
|
168 |
_attrs[attr] = ''
|
169 |
elif attr.AttributeType == 'Symbol Item':
|
170 |
at = int(attr.AttrAt)
|
171 |
if len(_symbols) > at:
|
172 |
item = _symbols[at]
|
173 |
_attrs[attr] = eval(attr.Expression) if attr.Expression else ''
|
174 |
else:
|
175 |
_attrs[attr] = ''
|
176 |
else:
|
177 |
matches = [prop for prop in self.attrs if prop.UID == attr.UID]
|
178 |
if len(matches) == 1:
|
179 |
_attrs[matches[0]] = self.attrs[matches[0]]
|
180 |
else:
|
181 |
_attrs[attr] = ''
|
182 |
except Exception as ex:
|
183 |
from App import App
|
184 |
from AppDocData import MessageType
|
185 |
|
186 |
message = 'error occured({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, sys.exc_info()[-1].tb_lineno)
|
187 |
App.mainWnd().addMessage.emit(MessageType.Error, message)
|
188 |
|
189 |
return _attrs
|
190 |
'''
|
191 |
|
192 |
def highlight(self, flag): |
193 |
""" highlight/unhighlight the equpment """
|
194 |
|
195 |
try:
|
196 |
self.hover = flag
|
197 |
self.update()
|
198 |
|
199 |
for assoc in self.associations(): |
200 |
assoc.highlight(flag) |
201 |
|
202 |
for connector in self.connectors: |
203 |
connector.highlight(flag) |
204 |
except Exception as ex: |
205 |
from App import App |
206 |
from AppDocData import MessageType |
207 |
|
208 |
message = 'error occured({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, sys.exc_info()[-1].tb_lineno) |
209 |
App.mainWnd().addMessage.emit(MessageType.Error, message) |
210 |
|
211 |
'''
|
212 |
@brief generate xml code for equipment
|
213 |
@author humkyung
|
214 |
@date 2018.05.09
|
215 |
'''
|
216 |
def toXml(self): |
217 |
from xml.etree.ElementTree import Element, SubElement, dump, ElementTree |
218 |
|
219 |
try:
|
220 |
node = SymbolSvgItem.toXml(self)
|
221 |
except Exception as ex: |
222 |
from App import App |
223 |
from AppDocData import MessageType |
224 |
|
225 |
message = 'error occured({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, sys.exc_info()[-1].tb_lineno) |
226 |
App.mainWnd().addMessage.emit(MessageType.Error, message) |
227 |
|
228 |
return None |
229 |
|
230 |
return node
|
231 |
|
232 |
def toSql(self): |
233 |
""" convert equipment data to sql query """
|
234 |
from AppDocData import AppDocData |
235 |
res = [] |
236 |
|
237 |
appDocData = AppDocData.instance() |
238 |
|
239 |
cols = ['UID', 'ITEM_NO', 'PNID_NO'] |
240 |
values = ['?','?','?'] |
241 |
param = [str(self.uid), self.tag_no, appDocData.activeDrawing.name] |
242 |
|
243 |
""" save attributes to database """
|
244 |
_attrs = self.getAttributes()
|
245 |
for key,value in _attrs.items(): |
246 |
cols.append(key.Attribute) |
247 |
values.append('?')
|
248 |
param.append(value) |
249 |
|
250 |
sql = 'insert into EQUIPMENT_DATA_LIST({}) values({})'.format(','.join(cols), ','.join(values)) |
251 |
res.append((sql, (tuple(param),)))
|
252 |
|
253 |
cols = ['UID', 'Drawings_UID', 'Symbol_UID', 'X', 'Y', 'Width', 'Height', 'Rotation', 'Area', 'Owner', 'Connected', '[Supplied By]', \ |
254 |
'SpecialItemTypes_UID', 'OriginIndex'] |
255 |
values = ['?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?'] |
256 |
param = [(str(self.uid), str(appDocData.activeDrawing.UID), str(self.dbUid), self.loc[0], self.loc[1], self.size[0], self.size[1], str(self.angle), |
257 |
self.area, str(self.owner) if self.owner else None, \ |
258 |
str(self.conns[0]) if self.conns else None, \ |
259 |
self.prop('Supplied By'), \ |
260 |
str(self.special_item_type) if self.special_item_type else None,\ |
261 |
self.currentPointModeIndex)]
|
262 |
sql = 'insert into Components({}) values({})'.format(','.join(cols), ','.join(values)) |
263 |
res.append((sql, tuple(param)))
|
264 |
|
265 |
_attrs = self.getAttributes()
|
266 |
if _attrs:
|
267 |
cols = ['UID', 'Components_UID', 'SymbolAttribute_UID', 'Value'] |
268 |
values = ['?', '?', '?', '?'] |
269 |
params = [] |
270 |
for key in _attrs.keys(): |
271 |
params.append((str(uuid.uuid4()), str(self.uid), str(key.UID), str(_attrs[key]))) |
272 |
sql = 'insert into Attributes({}) values({})'.format(','.join(cols), ','.join(values)) |
273 |
res.append((sql, tuple(params)))
|
274 |
|
275 |
if self.associations(): |
276 |
cols = ['UID', '[Type]', 'Components_UID', 'Association'] |
277 |
values = ['?', '?', '?', '?'] |
278 |
params = [] |
279 |
for assoc in self.associations(): |
280 |
param = [str(uuid.uuid4()), QEngineeringAbstractItem.assoc_type(assoc), str(self.uid), str(assoc.uid)] |
281 |
sql = 'insert into Associations({}) values({})'.format(','.join(cols), ','.join(values)) |
282 |
res.append((sql, tuple(param)))
|
283 |
|
284 |
# save connectors to database
|
285 |
if self.connectors: |
286 |
cols = ['Components_UID', '[Index]', 'X', 'Y', 'Connected', 'Connected_At'] |
287 |
values = ['?', '?', '?', '?', '?', '?'] |
288 |
params = [] |
289 |
index = 1
|
290 |
for connector in self.connectors: |
291 |
params.append(\ |
292 |
(#str(connector.uid),
|
293 |
str(self.uid), index, connector.connectPoint[0], connector.connectPoint[1],\ |
294 |
str(connector.connectedItem.uid) if connector.connectedItem else None,\ |
295 |
str(connector._connected_at))\
|
296 |
) |
297 |
sql = 'insert into Points({}) values({})'.format(','.join(cols), ','.join(values)) |
298 |
res.append((sql, tuple(params)))
|
299 |
# up to here
|
300 |
|
301 |
return res
|
302 |
|
303 |
'''
|
304 |
@brief return equip Data List
|
305 |
@author kyouho
|
306 |
@date 2018.08.14
|
307 |
'''
|
308 |
def getEquipmentDataList(self): |
309 |
dataList = [] |
310 |
try:
|
311 |
from AppDocData import AppDocData |
312 |
|
313 |
docData = AppDocData.instance() |
314 |
attrs = self.attrs
|
315 |
|
316 |
for index in range(len(QEngineeringEquipmentItem.EQUIP_COLUMN_LIST)): |
317 |
dataList.append('')
|
318 |
|
319 |
dataList[0] = str(self.uid) |
320 |
dataList[14] = docData.imgName
|
321 |
|
322 |
for attr in attrs: |
323 |
attrInfo = docData.getSymbolAttributeByUID(attr.UID) |
324 |
attrName = attrInfo[0]
|
325 |
if QEngineeringEquipmentItem.EQUIP_COLUMN_LIST.count(attrName):
|
326 |
colIndex = QEngineeringEquipmentItem.EQUIP_COLUMN_LIST.index(attrName) |
327 |
|
328 |
if type(attr) is UserInputAttribute: |
329 |
dataList[colIndex] = attr.text if attr.text is not None else '' |
330 |
elif type(attr) is QEngineeringTextItem: |
331 |
dataList[colIndex] = attr.text() |
332 |
else:
|
333 |
dataList[colIndex] = attr.uid |
334 |
|
335 |
except Exception as ex: |
336 |
print('error occured({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, sys.exc_info()[-1].tb_lineno)) |
337 |
|
338 |
return dataList
|