hytos / DTI_PID / DTI_PID / Drawing.py @ eea44d8f
이력 | 보기 | 이력해설 | 다운로드 (14.4 KB)
1 |
# coding: utf-8
|
---|---|
2 |
""" This is Drawing module """
|
3 |
|
4 |
import os |
5 |
import sys |
6 |
import uuid |
7 |
from PIL import PngImagePlugin, JpegImagePlugin |
8 |
from PIL import Image |
9 |
from PIL.ImageQt import ImageQt |
10 |
import numpy as np |
11 |
from AppDocData import AppDocData |
12 |
|
13 |
sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)) + '\\Shapes') |
14 |
from SymbolSvgItem import SymbolSvgItem |
15 |
from EngineeringTextItem import QEngineeringTextItem |
16 |
from EngineeringLineItem import QEngineeringLineItem |
17 |
from EngineeringLineNoTextItem import QEngineeringLineNoTextItem |
18 |
from EngineeringRunItem import QEngineeringRunItem |
19 |
from EngineeringVendorItem import QEngineeringVendorItem |
20 |
from EngineeringUnknownItem import QEngineeringUnknownItem |
21 |
from QEngineeringTrimLineNoTextItem import QEngineeringTrimLineNoTextItem |
22 |
|
23 |
class Source: |
24 |
""" This is Source class """
|
25 |
def __init__(self, source): |
26 |
if type(source) is np.ndarray: |
27 |
self.source = Image.fromarray(source)
|
28 |
elif type(source) is PngImagePlugin.PngImageFile or type(source) is JpegImagePlugin.JpegImageFile: |
29 |
self.source = source
|
30 |
elif type(source) is QImage: |
31 |
self.source = Image.fromqimage(source)
|
32 |
elif type(source) is QPixmap: |
33 |
self.source = Image.fromqpixmap(source)
|
34 |
|
35 |
'''
|
36 |
@history 2018.05.18 Jeongwoo When Parameter [rect] is None, return whole image
|
37 |
'''
|
38 |
def getPyImageOnRect(self, rect = None): |
39 |
if rect is not None: |
40 |
return self.source.copy().crop((rect.left(), rect.top(), rect.right(), rect.bottom())) |
41 |
else:
|
42 |
return self.source.copy() |
43 |
|
44 |
def getQImageOnRect(self, rect = None): |
45 |
image = ImageQt(self.getPyImageOnRect(rect))
|
46 |
return image.convertToFormat(QImage.Format_RGBA8888)
|
47 |
|
48 |
class Drawing: |
49 |
'''
|
50 |
@brief construction
|
51 |
@author humkyung
|
52 |
@date 2018.07.07
|
53 |
'''
|
54 |
def __init__(self, uid, name, datetime): |
55 |
import uuid |
56 |
app_doc_data = AppDocData.instance() |
57 |
|
58 |
self.name = name
|
59 |
self._datetime = datetime
|
60 |
''' set drawing uid '''
|
61 |
if not uid: |
62 |
drawings = app_doc_data.getDrawings() |
63 |
drawing = [drawing for drawing in drawings if self.name == os.path.splitext(drawing.name)[0]] |
64 |
self.UID = drawing[0].UID if drawing else uuid.uuid4() |
65 |
self._datetime = drawing[0].datetime if drawing else None |
66 |
else:
|
67 |
self.UID = uid
|
68 |
|
69 |
self.width = 0 |
70 |
self.height = 0 |
71 |
self._attrs = [['Drawing No', ''], ['Rev No', ''], ['Unit', '']] # attributes |
72 |
|
73 |
def __del__(self): |
74 |
""" destructor """
|
75 |
pass
|
76 |
|
77 |
@property
|
78 |
def datetime(self): |
79 |
""" return datetime """
|
80 |
return self._datetime |
81 |
|
82 |
@datetime.setter
|
83 |
def datetime(self, value): |
84 |
""" set date time of drawing """
|
85 |
self._datetime = value
|
86 |
|
87 |
'''
|
88 |
@brief getter of attrs
|
89 |
@author humkyung
|
90 |
@date 2018.07.07
|
91 |
'''
|
92 |
@property
|
93 |
def attrs(self): |
94 |
return self._attrs |
95 |
|
96 |
'''
|
97 |
@brief setter of attrs
|
98 |
@author humkyung
|
99 |
@date 2018.07.07
|
100 |
'''
|
101 |
@attrs.setter
|
102 |
def attrs(self, value): |
103 |
self._attrs = value
|
104 |
|
105 |
def set_pid_source(self, image): |
106 |
""" set image source for drawing """
|
107 |
self.width, self.height = image.size |
108 |
self.currentPidSource = Source(image)
|
109 |
|
110 |
'''
|
111 |
@brief set attribute
|
112 |
@author humkyung
|
113 |
@date 2018.07.07
|
114 |
'''
|
115 |
def setAttr(self, name, value): |
116 |
attrs = [attr for attr in self._attrs if attr[0] == name] |
117 |
if attrs:
|
118 |
attrs[0][1] = value |
119 |
else:
|
120 |
self._attrs.append([name, value])
|
121 |
|
122 |
print('attribute({},{})'.format(name, value))
|
123 |
|
124 |
def from_database(self): |
125 |
""" load all components of drawing """
|
126 |
|
127 |
items = [] |
128 |
tracer_line_nos = [] |
129 |
app_doc_data = AppDocData.instance() |
130 |
try:
|
131 |
components = app_doc_data.get_components(self.UID)
|
132 |
|
133 |
""" parsing all symbols """
|
134 |
for symbol in [component for component in components if component['SymbolType_UID'] != -1]: |
135 |
item = SymbolSvgItem.from_database(symbol) |
136 |
if item: items.append(item)
|
137 |
|
138 |
# parse texts
|
139 |
for text in [component for component in components if component['Name'] == 'Text' and component['SymbolType_UID'] == -1]: |
140 |
item = QEngineeringTextItem.from_database(text) |
141 |
if item:
|
142 |
item.uid = text['UID']
|
143 |
item.attribute = text['Value']
|
144 |
items.append(item) |
145 |
|
146 |
# note
|
147 |
for note in [component for component in components if component['Name'] == 'Note' and component['SymbolType_UID'] == -1]: |
148 |
item = QEngineeringTextItem.from_database(note) |
149 |
if item is not None: |
150 |
item.uid = note['UID']
|
151 |
items.append(item) |
152 |
|
153 |
for line in [component for component in components if component['Name'] == 'Line' and component['SymbolType_UID'] == -1]: |
154 |
item = QEngineeringLineItem.from_database(line) |
155 |
if item: items.append(item)
|
156 |
|
157 |
for unknown in [component for component in components if component['Name'] == 'Unknown' and component['SymbolType_UID'] == -1]: |
158 |
item = QEngineeringUnknownItem.from_database(unknown) |
159 |
if item: items.append(item)
|
160 |
|
161 |
for component in [component for component in components if component['Name'] == 'Line NO' and component['SymbolType_UID'] == -1]: |
162 |
line_no = QEngineeringLineNoTextItem.from_database(component) |
163 |
if type(line_no) is QEngineeringLineNoTextItem: |
164 |
runs = app_doc_data.get_pipe_runs(str(line_no.uid))
|
165 |
if not runs: continue |
166 |
for run in runs: |
167 |
line_run = QEngineeringRunItem() |
168 |
run_items = app_doc_data.get_pipe_run_items(run['UID'])
|
169 |
for record in run_items: |
170 |
uid = record['Components_UID']
|
171 |
matches = [item for item in items if str(item) == str(uid)] |
172 |
if matches:
|
173 |
matches[0]._owner = line_no
|
174 |
line_run.items.append(matches[0])
|
175 |
line_run.owner = line_no |
176 |
line_no.runs.append(line_run) |
177 |
|
178 |
for component in [component for component in components if component['Name'] == 'Trim Line NO' and component['SymbolType_UID'] == -1]: |
179 |
tracer_line_no = QEngineeringTrimLineNoTextItem() |
180 |
tracer_line_no.uid = uuid.UUID(component['UID'])
|
181 |
|
182 |
runs = app_doc_data.get_pipe_runs(str(tracer_line_no.uid))
|
183 |
if not runs: continue |
184 |
for run in runs: |
185 |
line_run = QEngineeringRunItem() |
186 |
run_items = app_doc_data.get_pipe_run_items(run['UID'])
|
187 |
for record in run_items: |
188 |
uid = record['Components_UID']
|
189 |
matches = [item for item in items if str(item) == str(uid)] |
190 |
if matches:
|
191 |
matches[0].owner = tracer_line_no
|
192 |
line_run.items.append(matches[0])
|
193 |
tracer_line_no.runs.append(line_run) |
194 |
|
195 |
tracer_line_nos.append(tracer_line_no) |
196 |
|
197 |
for component in [component for component in components if component['Name'] == 'VendorPackage' and component['SymbolType_UID'] == -1]: |
198 |
item = QEngineeringVendorItem.from_database(component) |
199 |
if item: items.append(item)
|
200 |
except Exception as ex: |
201 |
from App import App |
202 |
from AppDocData import MessageType |
203 |
|
204 |
message = 'error occured({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, sys.exc_info()[-1].tb_lineno) |
205 |
App.mainWnd().addMessage.emit(MessageType.Error, message) |
206 |
finally:
|
207 |
return items,tracer_line_nos
|
208 |
|
209 |
def export(self, path): |
210 |
""" save items of drawing to given xml file """
|
211 |
import XmlGenerator as xg |
212 |
|
213 |
items,tracer_line_nos = self.from_database()
|
214 |
xg.writeXmlOnScene(path, self.width, self.height, items, tracer_line_nos) |
215 |
|
216 |
def transfer(self, database): |
217 |
""" data transfer to given database """
|
218 |
from AppDocData import AppDocData, MessageType |
219 |
|
220 |
app_doc_data = AppDocData.instance() |
221 |
conn = database.connect() |
222 |
with conn:
|
223 |
try:
|
224 |
cursor = conn.cursor() |
225 |
sql = database.to_sql('delete from LINE_DATA_LIST where PNID_NO=?')
|
226 |
cursor.execute(sql, (self.name,))
|
227 |
sql = database.to_sql('delete from EQUIPMENT_DATA_LIST where PNID_NO=?')
|
228 |
cursor.execute(sql,(self.name,))
|
229 |
sql = database.to_sql('delete from VALVE_DATA_LIST where PNID_NO=?')
|
230 |
cursor.execute(sql, (self.name,))
|
231 |
sql = database.to_sql('delete from INSTRUMENT_DATA_LIST where PNID_NO=?')
|
232 |
cursor.execute(sql, (self.name,))
|
233 |
sql = database.to_sql('delete from NOTE_DATA_LIST where PNID_NO=?')
|
234 |
cursor.execute(sql, (self.name,))
|
235 |
sql = database.to_sql('delete from TitleBlockValues where Drawings_UID=(select UID from Drawings where [Name]=?)')
|
236 |
cursor.execute(sql, (self.name,))
|
237 |
sql = database.to_sql('delete from LineNoAttributes where Components_UID in (select UID from Components where Drawings_UID=(select UID from Drawings where Name=?))')
|
238 |
cursor.execute(sql, (self.name,))
|
239 |
sql = database.to_sql('delete from Attributes where Components_UID in (select UID from Components where Drawings_UID=(select UID from Drawings where Name=?))')
|
240 |
cursor.execute(sql, (self.name,))
|
241 |
sql = database.to_sql('delete from Associations where Components_UID in (select UID from Components where Drawings_UID=(select UID from Drawings where Name=?))')
|
242 |
cursor.execute(sql, (self.name,))
|
243 |
sql = database.to_sql('delete from Points where Components_UID in (select UID from Components where Drawings_UID=(select UID from Drawings where Name=?))')
|
244 |
cursor.execute(sql, (self.name,))
|
245 |
sql = database.to_sql('delete from PipeRunItems where PipeRuns_UID in (select UID from PipeRuns where Drawings_UID=(select UID from Drawings where Name=?))')
|
246 |
cursor.execute(sql, (self.name,))
|
247 |
sql = database.to_sql('delete from PipeRuns where Drawings_UID=(select UID from Drawings where Name=?)')
|
248 |
cursor.execute(sql, (self.name,))
|
249 |
sql = database.to_sql('delete from Components where Drawings_UID=(select UID from Drawings where Name=?)')
|
250 |
cursor.execute(sql, (self.name,))
|
251 |
sql = database.to_sql('delete from Drawings where Name=?')
|
252 |
cursor.execute(sql, (self.name,))
|
253 |
|
254 |
sql = database.to_sql('insert into Drawings(UID,Name,DATETIME) values(?,?,?)')
|
255 |
cursor.execute(sql, (self.UID, self.name, self.datetime)) |
256 |
|
257 |
components = app_doc_data.get_components(str(self.UID)) |
258 |
for component in components: |
259 |
sql = database.to_sql('insert into Components(UID,Drawings_UID,Symbol_UID,X,Y,Width,Height,Rotation,Area,Value,Owner,[From],[To],Connected,[Supplied By],SpecialItemTypes_UID, OriginIndex) \
|
260 |
values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)')
|
261 |
cursor.execute(sql, (component['UID'], component['Drawings_UID'], component['Symbol_UID'], |
262 |
component['X'], component['Y'], component['Width'], component['Height'], component['Rotation'], component['Area'], |
263 |
component['Value'], component['Owner'], component['From'], component['To'], component['Connected'], |
264 |
component['Supplied By'], component['SpecialItemTypes_UID'], component['OriginIndex'])) |
265 |
|
266 |
for component in components: |
267 |
pipe_runs = app_doc_data.get_pipe_runs(component['UID'])
|
268 |
for pipe_run in pipe_runs: |
269 |
sql = database.to_sql('insert into PipeRuns(UID,Drawings_UID,Type,Owner,[Index]) values(?,?,?,?,?)')
|
270 |
cursor.execute(sql, (pipe_run[0], pipe_run[1], pipe_run[2], pipe_run[3], pipe_run[4])) |
271 |
pipe_run_items = app_doc_data.get_pipe_run_items(pipe_run[0])
|
272 |
for pipe_run_item in pipe_run_items: |
273 |
sql = database.to_sql('insert into PipeRunItems(UID,PipeRuns_UID,[Index],Components_UID) values(?,?,?,?)')
|
274 |
cursor.execute(sql, (pipe_run_item[0], pipe_run_item[1], pipe_run_item[2], pipe_run_item[3])) |
275 |
|
276 |
connectors = app_doc_data.get_component_connectors(component['UID'])
|
277 |
for connector in connectors: |
278 |
sql = database.to_sql('insert into Points(Components_UID,[Index],X,Y,Connected,Connected_At) values(?,?,?,?,?,?)')
|
279 |
cursor.execute(sql, (connector['Components_UID'], connector['Index'], connector['X'], connector['Y'], connector['Connected'], connector['Connected_At'])) |
280 |
|
281 |
associations = app_doc_data.get_component_associations(component['UID'])
|
282 |
for assoc in associations: |
283 |
sql = database.to_sql('insert into Associations(UID,[Type],Components_UID,Association) values(?,?,?,?)')
|
284 |
cursor.execute(sql, (assoc['UID'], assoc['Type'], assoc['Components_UID'], assoc['Association'])) |
285 |
|
286 |
attributes = app_doc_data.get_component_attributes(component['UID'])
|
287 |
for attr in attributes: |
288 |
sql = database.to_sql('insert into Attributes(UID,Components_UID,SymbolAttribute_UID,Value) values(?,?,?,?)')
|
289 |
cursor.execute(sql, (attr['UID'], attr['Components_UID'], attr['SymbolAttribute_UID'], attr['Value'])) |
290 |
|
291 |
conn.commit() |
292 |
# Catch the exception
|
293 |
except Exception as ex: |
294 |
# Roll back any change if something goes wrong
|
295 |
conn.rollback() |
296 |
|
297 |
print(sql) |
298 |
from App import App |
299 |
message = 'error occurred({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, sys.exc_info()[-1].tb_lineno) |
300 |
App.mainWnd().addMessage.emit(MessageType.Error, message) |