hytos / DTI_PID / DTI_PID / Shapes / EngineeringRunItem.py @ f7d568a9
이력 | 보기 | 이력해설 | 다운로드 (7.25 KB)
1 | c82b5644 | humkyung | # coding: utf-8
|
---|---|---|---|
2 | """ This is engineering run item moduel """
|
||
3 | |||
4 | 17704fb0 | humkyung | import sys |
5 | import os.path |
||
6 | import copy |
||
7 | try:
|
||
8 | from PyQt5.QtCore import Qt, QPointF, QRectF, pyqtSignal, QObject, QT_VERSION_STR |
||
9 | from PyQt5.QtGui import QImage, QPixmap, QPainterPath, QBrush, QPen, QTransform |
||
10 | from PyQt5.QtWidgets import QGraphicsView, QGraphicsScene, QFileDialog, QGraphicsItem, QAbstractGraphicsShapeItem, QGraphicsPathItem |
||
11 | except ImportError: |
||
12 | try:
|
||
13 | from PyQt4.QtCore import Qt, QRectF, pyqtSignal, QObject, QT_VERSION_STR |
||
14 | from PyQt4.QtGui import QGraphicsView, QGraphicsScene, QImage, QPixmap, QPainterPath, QFileDialog |
||
15 | except ImportError: |
||
16 | raise ImportError("ImageViewerQt: Requires PyQt5 or PyQt4.") |
||
17 | |||
18 | 3d6ff047 | humkyung | from EngineeringAbstractItem import QEngineeringAbstractItem |
19 | 17704fb0 | humkyung | |
20 | class QEngineeringRunItem(QEngineeringAbstractItem): |
||
21 | c82b5644 | humkyung | """ This is engineering run item class """
|
22 | |||
23 | 17704fb0 | humkyung | def __init__(self): |
24 | import uuid |
||
25 | QEngineeringAbstractItem.__init__(self)
|
||
26 | |||
27 | self.uid = uuid.uuid4() # generate UUID |
||
28 | self._items = []
|
||
29 | 7caf7034 | humkyung | self._visible = False |
30 | 990644d9 | humkyung | self.owner = None |
31 | 7caf7034 | humkyung | |
32 | @property
|
||
33 | 62762fec | humkyung | def Type(self): |
34 | """ return type of run(Drain/Process) """
|
||
35 | from SymbolSvgItem import SymbolSvgItem |
||
36 | from EngineeringLineItem import QEngineeringLineItem |
||
37 | |||
38 | if len(self._items) == 2: |
||
39 | if 1 == len([item for item in self._items if type(item) is QEngineeringLineItem]) and\ |
||
40 | 1 == len([item for item in self._items if type(item) is SymbolSvgItem]): return 'Drain' |
||
41 | |||
42 | return 'Process' |
||
43 | |||
44 | @property
|
||
45 | 7caf7034 | humkyung | def visible(self): |
46 | 9ff6820d | humkyung | """ return visible """
|
47 | 7caf7034 | humkyung | return self._visible |
48 | |||
49 | @visible.setter
|
||
50 | def visible(self, value): |
||
51 | 3ec1874a | humkyung | """ set visible value """
|
52 | 7caf7034 | humkyung | self._visible = value
|
53 | for item in self.items: |
||
54 | if issubclass(type(item), QGraphicsItem): |
||
55 | item.setVisible(value) |
||
56 | 17704fb0 | humkyung | |
57 | '''
|
||
58 | @brief getter of items
|
||
59 | @author humkyung
|
||
60 | @date 2018.05.15
|
||
61 | '''
|
||
62 | @property
|
||
63 | def items(self): |
||
64 | return self._items |
||
65 | |||
66 | '''
|
||
67 | @brief setter of items
|
||
68 | @author humkyung
|
||
69 | @date 2018.05.15
|
||
70 | '''
|
||
71 | @items.setter
|
||
72 | def items(self, value): |
||
73 | self._items = value
|
||
74 | |||
75 | 9ff6820d | humkyung | def explode(self): |
76 | c82b5644 | humkyung | """ explode run and subtract it from line no """
|
77 | 5a4d5665 | humkyung | |
78 | 9ff6820d | humkyung | try:
|
79 | for item in self.items: |
||
80 | item.owner = None
|
||
81 | finally:
|
||
82 | self.items.clear()
|
||
83 | fa7533ae | esham21 | |
84 | 990644d9 | humkyung | if self.owner: self.owner.runs.remove(self) |
85 | 9ff6820d | humkyung | |
86 | 17704fb0 | humkyung | '''
|
87 | @brief get two points which's length is max
|
||
88 | @author humkyung
|
||
89 | @date 2018.05.23
|
||
90 | '''
|
||
91 | def getLongestTwoPoints(self, pts): |
||
92 | import math |
||
93 | res = [None, None] |
||
94 | |||
95 | maxDistance = None
|
||
96 | for i in range(len(pts)): |
||
97 | for j in range(i+1, len(pts)): |
||
98 | dx = pts[i][0] - pts[j][0] |
||
99 | dy = pts[i][1] - pts[j][1] |
||
100 | dist = math.sqrt(dx*dx + dy*dy) |
||
101 | if (maxDistance is None) or (maxDistance < dist): |
||
102 | maxDistance = dist |
||
103 | res[0] = pts[i]
|
||
104 | res[1] = pts[j]
|
||
105 | |||
106 | return res
|
||
107 | |||
108 | 3ec1874a | humkyung | def arrange_flow_direction(self): |
109 | """ arrange flow direction of line """
|
||
110 | from EngineeringLineItem import QEngineeringLineItem |
||
111 | |||
112 | for at in range(len(self.items)): |
||
113 | if type(self.items[at]) is QEngineeringLineItem and at > 0: |
||
114 | self.items[at].arrange_flow_direction(self.items[at-1]) |
||
115 | c82b5644 | humkyung | elif type(self.items[at]) is QEngineeringLineItem and len(self.items) > 1: |
116 | self.items[at].arrange_flow_direction(self.items[at+1]) |
||
117 | self.items[at].reverse()
|
||
118 | 3ec1874a | humkyung | |
119 | def reverse(self): |
||
120 | """ reverse line's flow direction """
|
||
121 | from EngineeringLineItem import QEngineeringLineItem |
||
122 | |||
123 | c82b5644 | humkyung | self.items.reverse()
|
124 | 3ec1874a | humkyung | for at in range(len(self.items)): |
125 | if type(self.items[at]) is QEngineeringLineItem: |
||
126 | self.items[at].reverse()
|
||
127 | |||
128 | 17704fb0 | humkyung | '''
|
129 | @brief add flow mark
|
||
130 | @author humkyung
|
||
131 | @date 2018.06.17
|
||
132 | '''
|
||
133 | def addFlowMark(self): |
||
134 | from EngineeringLineItem import QEngineeringLineItem |
||
135 | |||
136 | try:
|
||
137 | connectedLines = [item for item in self.items if type(item) is QEngineeringLineItem] |
||
138 | |||
139 | maxLength = None
|
||
140 | maxLengthItem = None
|
||
141 | for line in connectedLines: |
||
142 | length = line.length() |
||
143 | if maxLength is None or maxLength < length: |
||
144 | maxLength = length |
||
145 | maxLengthItem = line |
||
146 | |||
147 | if maxLengthItem is not None: maxLengthItem.addFlowArrow() |
||
148 | except Exception as ex: |
||
149 | e57134bc | esham21 | print('error occurred({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, sys.exc_info()[-1].tb_lineno)) |
150 | 17704fb0 | humkyung | |
151 | '''
|
||
152 | @brief write to xml
|
||
153 | @author humkyung
|
||
154 | @date 2018.05.16
|
||
155 | @history Jeongwoo 2018.05.24 Add math.fabs() on sublist's if-statement
|
||
156 | Change toler(1 → 5) and Modify Longest Line's coords // Make comments Test Code
|
||
157 | humkyung 2018.06.22 sort lines before writing to xml
|
||
158 | 42d83048 | gaqhf | kyouho 2018.08.09 Line 묶기
|
159 | 17704fb0 | humkyung | '''
|
160 | def toXml(self): |
||
161 | from xml.etree.ElementTree import Element, SubElement, dump, ElementTree |
||
162 | from EngineeringLineItem import QEngineeringLineItem |
||
163 | from SymbolSvgItem import SymbolSvgItem |
||
164 | |||
165 | try:
|
||
166 | node = Element('RUN')
|
||
167 | 62762fec | humkyung | node.attrib['TYPE'] = self.Type |
168 | 17704fb0 | humkyung | for item in self.items: |
169 | # skip line item which's connected items are same
|
||
170 | 68ffb222 | esham21 | if (type(item) is QEngineeringLineItem) and item.connectors[0].connectedItem is not None and (item.connectors[0].connectedItem is item.connectors[1].connectedItem): |
171 | 17704fb0 | humkyung | continue
|
172 | # up to here
|
||
173 | |||
174 | 08da2494 | esham21 | xml = item.toXml() |
175 | if xml is not None: |
||
176 | node.append(item.toXml()) |
||
177 | 17704fb0 | humkyung | |
178 | 42d83048 | gaqhf | except Exception as ex: |
179 | ee195020 | humkyung | from App import App |
180 | from AppDocData import MessageType |
||
181 | |||
182 | e57134bc | esham21 | message = 'error occurred({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, sys.exc_info()[-1].tb_lineno) |
183 | ee195020 | humkyung | App.mainWnd().addMessage.emit(MessageType.Error, message) |
184 | c82b5644 | humkyung | return None |
185 | 17704fb0 | humkyung | |
186 | 5a4d5665 | humkyung | return node
|
187 | 1813be93 | humkyung | |
188 | def to_sql(self, index, owner): |
||
189 | """ generate sql phrase to save run to database """
|
||
190 | import uuid |
||
191 | from AppDocData import AppDocData |
||
192 | |||
193 | res = [] |
||
194 | |||
195 | app_doc_data = AppDocData.instance() |
||
196 | |||
197 | cols = ['UID', 'Drawings_UID', '[Type]', 'Owner', '[Index]'] |
||
198 | values = ['?', '?', '?', '?', '?'] |
||
199 | 60f50aee | humkyung | params = [(str(self.uid), str(app_doc_data.activeDrawing.UID), self.Type, str(owner), index)] |
200 | 48cabe25 | humkyung | sql = 'insert into PipeRuns({}) values({})'.format(','.join(cols), ','.join(values)) |
201 | 1813be93 | humkyung | res.append((sql, tuple(params)))
|
202 | |||
203 | cols = ['UID', 'PipeRuns_UID', '[Index]', 'Components_UID'] |
||
204 | values = ['?', '?', '?', '?'] |
||
205 | 60f50aee | humkyung | params = [] |
206 | 1813be93 | humkyung | |
207 | 60f50aee | humkyung | _index = 1
|
208 | 1813be93 | humkyung | for item in self.items: |
209 | 60f50aee | humkyung | params.append((str(uuid.uuid4()), str(self.uid), _index, str(item.uid))) |
210 | 1813be93 | humkyung | _index += 1
|
211 | 48cabe25 | humkyung | sql = 'insert into PipeRunItems({}) values({})'.format(','.join(cols), ','.join(values)) |
212 | 60f50aee | humkyung | if params: res.append((sql, tuple(params))) |
213 | 1813be93 | humkyung | |
214 | return res |