hytos / DTI_PID / DTI_PID / TextItemFactory.py @ b4555d97
이력 | 보기 | 이력해설 | 다운로드 (13.5 KB)
1 |
# coding: utf-8
|
---|---|
2 |
""" This is TextItemFactor module """
|
3 |
|
4 |
from SingletonInstance import SingletonInstane |
5 |
import re |
6 |
try:
|
7 |
from PyQt5.QtCore import * |
8 |
from PyQt5.QtGui import * |
9 |
from PyQt5.QtWidgets import * |
10 |
except ImportError: |
11 |
try:
|
12 |
from PyQt4.QtCore import * |
13 |
from PyQt4.QtGui import * |
14 |
except ImportError: |
15 |
raise ImportError("ImageViewerQt: Requires PyQt5 or PyQt4.") |
16 |
from AppDocData import * |
17 |
import sys, os |
18 |
sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)) + '\\Shapes') |
19 |
from EngineeringTextItem import QEngineeringTextItem |
20 |
from EngineeringLineNoTextItem import QEngineeringLineNoTextItem |
21 |
from EngineeringNoteItem import QEngineeringNoteItem |
22 |
from QEngineeringSizeTextItem import QEngineeringSizeTextItem |
23 |
from QEngineeringTagNoTextItem import QEngineeringTagNoTextItem |
24 |
from EngineeringValveOperCodeTextItem import QEngineeringValveOperCodeTextItem |
25 |
from EngineeringReservedWordTextItem import QEngineeringReservedWordTextItem |
26 |
from SpecialItemTypesDialog import SpecialItemTypes |
27 |
|
28 |
class TextItemFactory(SingletonInstane): |
29 |
""" This is TextItemFactor class """
|
30 |
def __init__(self): |
31 |
self.delimiter = '"' |
32 |
self.lineNoDelimiter = '!-!' |
33 |
|
34 |
'''
|
35 |
@history 2018.04.27 Jeongwoo Add condition on elif-statement (delimiter != '' and lineNoConfig != '')
|
36 |
humkyung 2018.05.02 create engineering size text item if condition matches
|
37 |
humkyung 2018.06.19 set line no's color
|
38 |
kyouho 2018.07.04 edit for isLineNo method (add parameter)
|
39 |
humkyung 2018.08.08 fill linePropertyData and tagSeqNoPattern if given data is None
|
40 |
euisung 2018.11.19 now textitemfactory set plain text in createTextItem() for allowables
|
41 |
'''
|
42 |
def createTextItem(self, textInfo): |
43 |
from Configs import LineNoConfig |
44 |
|
45 |
item = None
|
46 |
|
47 |
try:
|
48 |
text = textInfo.getText() |
49 |
docData = AppDocData.instance() |
50 |
|
51 |
""" create normal text item if it is not in drawing area """
|
52 |
area = docData.getArea('Drawing')
|
53 |
if area is None or not area.contains([textInfo.getX(), textInfo.getY()]): |
54 |
item = QEngineeringTextItem() |
55 |
item.setToolTip(text) |
56 |
item.setPlainText(text) |
57 |
|
58 |
configs = docData.getConfigs('Size', 'Delimiter') |
59 |
sizeDelimiter = configs[0].value.upper() if 1 == len(configs) else None |
60 |
|
61 |
line_no_configs = LineNoConfig.instance() |
62 |
if line_no_configs:
|
63 |
for line_no_config in line_no_configs: |
64 |
result = line_no_config.parse(text) |
65 |
if result[0]: |
66 |
sizeUnit = line_no_config.unit |
67 |
break
|
68 |
else:
|
69 |
result = (False,)
|
70 |
|
71 |
if result[0]: |
72 |
item = QEngineeringLineNoTextItem() |
73 |
text = ''.join(result[1]) |
74 |
for i in result[2]: |
75 |
text = text[:i] + '\n' + text[i:]
|
76 |
item.setPlainText(text) |
77 |
|
78 |
# get color option
|
79 |
configs = docData.getConfigs('Line Color', 'Visible Option') |
80 |
if configs:
|
81 |
visibleOption = configs[0].value
|
82 |
else:
|
83 |
visibleOption = 'Random'
|
84 |
|
85 |
configs = docData.getConfigs('Color Property', 'State') |
86 |
if configs:
|
87 |
color_property = configs[0].value
|
88 |
|
89 |
# set line no color
|
90 |
if visibleOption == 'Random': |
91 |
rgb = docData.colors |
92 |
item.change_color(QColor(rgb.red, rgb.green, rgb.blue).name()) |
93 |
#item.setColor(QColor(rgb.red, rgb.green, rgb.blue).name())
|
94 |
else:
|
95 |
configs = docData.getConfigs('Line No', 'Configuration') |
96 |
configs = configs[0].value.split(self.delimiter) |
97 |
|
98 |
values = result[1]
|
99 |
index = configs.index(color_property) |
100 |
if index >= 0: |
101 |
value = values[index] |
102 |
line_properties = docData.getLinePropertiesByUID(color_property) |
103 |
if line_properties[0].Attribute.replace(' ','') == 'NominalDiameter': |
104 |
if sizeUnit == "Inch": |
105 |
value = docData.convertInchToMetric(value) |
106 |
|
107 |
configs = docData.getConfigs(color_property, value) |
108 |
if configs:
|
109 |
data = configs[0].value
|
110 |
rgb = data.split(',')
|
111 |
item.change_color(QColor(int(rgb[0]), int(rgb[1]), int(rgb[2])).name()) |
112 |
# up to here
|
113 |
docData.tracerLineNos.append(item) |
114 |
else:
|
115 |
item = self.create_note_no_text(textInfo)
|
116 |
if item:
|
117 |
pass
|
118 |
elif self.is_reserved_word(text): |
119 |
item = QEngineeringReservedWordTextItem() |
120 |
item.setToolTip('Reserved Word = {}'.format(text))
|
121 |
item.setPlainText(text) |
122 |
elif self.isSizeText(text, sizeDelimiter): |
123 |
item = QEngineeringSizeTextItem() |
124 |
text = text.replace(' ', '') |
125 |
item.setToolTip('SIZE = {}'.format(text))
|
126 |
item.setPlainText(text) |
127 |
elif self.isTagNoText(text): |
128 |
item = QEngineeringTagNoTextItem() |
129 |
item.setToolTip('TAG NO = {}'.format(text))
|
130 |
item.setPlainText(text) |
131 |
elif self.is_valve_operation_code(text): |
132 |
item = QEngineeringValveOperCodeTextItem() |
133 |
item.setToolTip('Valve Operation Code = {}'.format(text))
|
134 |
item.setPlainText(text) |
135 |
else:
|
136 |
item = QEngineeringTextItem() |
137 |
item.setToolTip(text) |
138 |
item.setPlainText(text) |
139 |
item.special_item_type = SpecialItemTypes.instance().find_match_exactly(item) |
140 |
docData.texts.append(item) |
141 |
except Exception as ex: |
142 |
from App import App |
143 |
from AppDocData import MessageType |
144 |
|
145 |
message = 'error occurred({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, sys.exc_info()[-1].tb_lineno) |
146 |
App.mainWnd().addMessage.emit(MessageType.Error, message) |
147 |
|
148 |
return item
|
149 |
|
150 |
'''
|
151 |
@brief Check Text startwith word
|
152 |
@author kyouho
|
153 |
@date 2018.07.04
|
154 |
'''
|
155 |
def isStartWithWord(self, text, word): |
156 |
sliceText = text[0:len(word[0])] |
157 |
if sliceText == word[0]: |
158 |
return (True, text[len(sliceText):len(text)], word[1]) |
159 |
else:
|
160 |
return (False,) |
161 |
|
162 |
|
163 |
'''
|
164 |
@brief Check Text startwith number
|
165 |
@author kyouho
|
166 |
@date 2018.07.05
|
167 |
'''
|
168 |
def isStartWithNumber(self, text): |
169 |
sliceText = text[0:1] |
170 |
num = ''
|
171 |
if self.isNumber(sliceText): |
172 |
text = text[1:len(text)] |
173 |
num += text |
174 |
while len(text) > 0: |
175 |
sliceText = text[0:1] |
176 |
|
177 |
if self.isNumber(sliceText): |
178 |
text = text[1:len(text)] |
179 |
num += text |
180 |
else:
|
181 |
break
|
182 |
|
183 |
return (True, text, num) |
184 |
else:
|
185 |
return (False,) |
186 |
|
187 |
'''
|
188 |
@brief Check Number
|
189 |
@author kyouho
|
190 |
@date 2018.07.05
|
191 |
@history kyouho 2018.07.09 add Refular Expression
|
192 |
'''
|
193 |
def isNumber(self, num): |
194 |
p = re.compile('(^[0-9]+$)')
|
195 |
result = p.match(num) |
196 |
|
197 |
if result:
|
198 |
return True |
199 |
else:
|
200 |
return False |
201 |
|
202 |
'''
|
203 |
@brief Check Number
|
204 |
@author kyouho
|
205 |
@date 2018.07.05
|
206 |
'''
|
207 |
def isTagSeqNo(self, text, pattern): |
208 |
try:
|
209 |
if pattern is not None: |
210 |
attr = text |
211 |
result = eval(pattern)
|
212 |
if self.isNumber(result): |
213 |
sliceText = text[0:len(result)] |
214 |
return (True, text[len(sliceText):len(text)], sliceText) |
215 |
else:
|
216 |
return (False,) |
217 |
else:
|
218 |
return (False,) |
219 |
except Exception as ex: |
220 |
print('error occurred({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, sys.exc_info()[-1].tb_lineno)) |
221 |
|
222 |
'''
|
223 |
@brief Check Number
|
224 |
@author kyouho
|
225 |
@date 2018.07.05
|
226 |
'''
|
227 |
def isLimitWord(self, text, pattern): |
228 |
try:
|
229 |
if pattern is not None: |
230 |
attr = text |
231 |
length = pattern[0]
|
232 |
isNumber = pattern[1]
|
233 |
|
234 |
if length: # Length가 있으면 Length만큼 문자열을 가져와 길이를 검사한다. |
235 |
result = eval('attr[:' + str(length) + ']') |
236 |
if int(length) != len(result): |
237 |
return(False,) |
238 |
elif isNumber:
|
239 |
match = re.search(re.compile('^[0-9]+'), attr)
|
240 |
if match:
|
241 |
result = match.group(match.start()) |
242 |
else:
|
243 |
return (False,) |
244 |
else:
|
245 |
result = attr |
246 |
|
247 |
if isNumber:
|
248 |
if self.isNumber(result): |
249 |
sliceText = text[0:len(result)] |
250 |
return (True, text[len(sliceText):len(text)], sliceText) |
251 |
else:
|
252 |
return (False,) |
253 |
else:
|
254 |
sliceText = text[0:len(result)] |
255 |
return (True, text[len(sliceText):len(text)], sliceText) |
256 |
else:
|
257 |
return (False,) |
258 |
except Exception as ex: |
259 |
print('error occurred({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, sys.exc_info()[-1].tb_lineno)) |
260 |
|
261 |
'''
|
262 |
@brief Check if given text is Note No Text (ex : NOTE 1, NOTE 2, ...)
|
263 |
@author Jeongwoo
|
264 |
@date 2018.04.26
|
265 |
@history 2018.05.16 Jeongwoo Modify Validator flag
|
266 |
'''
|
267 |
def create_note_no_text(self, textInfo): |
268 |
item = None
|
269 |
|
270 |
appDocData = AppDocData.instance() |
271 |
configs = appDocData.getConfigs('Note No Tag Rule', 'Note No Expression') |
272 |
if 1 == len(configs) and configs[0].value: |
273 |
expression = configs[0].value
|
274 |
match = re.search(re.compile(expression), textInfo.getText()) |
275 |
|
276 |
if match:
|
277 |
configs = appDocData.getConfigs('Note No Tag Rule', 'Note No Symbol Name') |
278 |
if 1 == len(configs) and configs[0].value: |
279 |
symbol_names = configs[0].value.split(',') |
280 |
for symbol in appDocData.symbols: |
281 |
if symbol.name in symbol_names and symbol.includes(textInfo.center) and '"' not in textInfo.getText(): |
282 |
item = QEngineeringNoteItem(symbol=symbol) |
283 |
item.owner = symbol |
284 |
symbol.add_assoc_item(item, 0)
|
285 |
break
|
286 |
else:
|
287 |
item = QEngineeringNoteItem() |
288 |
|
289 |
if item:
|
290 |
item.setToolTip(textInfo.getText()) |
291 |
item.setPlainText(textInfo.getText()) |
292 |
|
293 |
return item
|
294 |
|
295 |
'''
|
296 |
@brief check if given text is size text
|
297 |
@author humkyung
|
298 |
@date 2018.05.02
|
299 |
'''
|
300 |
def isSizeText(self, text, delimiter=None): |
301 |
from NominalPipeSize import NominalPipeSizeTable |
302 |
|
303 |
sizeUnit = None
|
304 |
configs = AppDocData.instance().getConfigs('Project', 'Unit') |
305 |
sizeUnit = configs[0].value if 1 == len(configs) else None |
306 |
|
307 |
pipe_sizes = NominalPipeSizeTable.instance().pipe_sizes |
308 |
|
309 |
text = text.replace("'", '"').upper() |
310 |
|
311 |
first, second = None, None |
312 |
# for imperial
|
313 |
if text.find('"') is not -1: |
314 |
first = text[:text.find('"') + 1] |
315 |
second = text[text.find('"') + 1:].replace(delimiter.upper(), '', 1).strip() if delimiter is not None else text[text.find('"') + 1:].strip() |
316 |
# for metric
|
317 |
else:
|
318 |
split_text = text.strip().split(delimiter.upper()) |
319 |
if len(split_text) > 2: |
320 |
return False |
321 |
first = split_text[0]
|
322 |
second = split_text[1] if len(split_text) is 2 else None |
323 |
|
324 |
tokens = [first, second] if second is not None and len(second) is not 0 else [first] |
325 |
for token in tokens: |
326 |
matches = [sizeData for sizeData in pipe_sizes if (sizeData.find(token, sizeUnit) if sizeUnit else sizeData.find(token))] |
327 |
if not matches: return False |
328 |
|
329 |
return True |
330 |
|
331 |
'''
|
332 |
@brief check if given text is tag no
|
333 |
@author humkyung
|
334 |
@date 2018.05.03
|
335 |
'''
|
336 |
def isTagNoText(self, text): |
337 |
from CodeTables import CodeTable |
338 |
|
339 |
found = CodeTable.instance('EqpTagNames').find_match_exactly(text)
|
340 |
|
341 |
return True if found else False |
342 |
|
343 |
def is_valve_operation_code(self, text): |
344 |
"""
|
345 |
check if given text is valve operation code
|
346 |
"""
|
347 |
from CodeTables import CodeTable |
348 |
|
349 |
found = CodeTable.instance('ValveOperCodes').find_match_exactly(text)
|
350 |
|
351 |
return True if found else False |
352 |
|
353 |
def is_reserved_word(self, text): |
354 |
""" check if given text is reserved words """
|
355 |
from CodeTables import CodeTable |
356 |
|
357 |
found = CodeTable.instance('ReservedWords').find_match_exactly(text)
|
358 |
|
359 |
return True if found else False |