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