hytos / DTI_PID / DTI_PID / TextItemFactory.py @ fd329e57
이력 | 보기 | 이력해설 | 다운로드 (12.3 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.isSizeText(text, sizeDelimiter): |
118 |
item = QEngineeringSizeTextItem() |
119 |
item.setToolTip('SIZE = {}'.format(text))
|
120 |
item.setPlainText(text) |
121 |
elif self.isTagNoText(text): |
122 |
item = QEngineeringTagNoTextItem() |
123 |
item.setToolTip('TAG NO = {}'.format(text))
|
124 |
item.setPlainText(text) |
125 |
elif self.is_valve_operation_code(text): |
126 |
item = QEngineeringValveOperCodeTextItem() |
127 |
item.setToolTip('Valve Operation Code = {}'.format(text))
|
128 |
item.setPlainText(text) |
129 |
elif self.is_reserved_word(text): |
130 |
item = QEngineeringReservedWordTextItem() |
131 |
item.setToolTip('Reserved Word = {}'.format(text))
|
132 |
item.setPlainText(text) |
133 |
else:
|
134 |
item = QEngineeringTextItem() |
135 |
item.setToolTip(text) |
136 |
item.setPlainText(text) |
137 |
docData.texts.append(item) |
138 |
except Exception as ex: |
139 |
from App import App |
140 |
|
141 |
message = 'error occured({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, sys.exc_info()[-1].tb_lineno) |
142 |
App.mainWnd().addMessage.emit(MessageType.Error, message) |
143 |
|
144 |
return item
|
145 |
|
146 |
'''
|
147 |
@brief Check Text startwith word
|
148 |
@author kyouho
|
149 |
@date 2018.07.04
|
150 |
'''
|
151 |
def isStartWithWord(self, text, word): |
152 |
sliceText = text[0:len(word[0])] |
153 |
if sliceText == word[0]: |
154 |
return (True, text[len(sliceText):len(text)], word[1]) |
155 |
else:
|
156 |
return (False,) |
157 |
|
158 |
|
159 |
'''
|
160 |
@brief Check Text startwith number
|
161 |
@author kyouho
|
162 |
@date 2018.07.05
|
163 |
'''
|
164 |
def isStartWithNumber(self, text): |
165 |
sliceText = text[0:1] |
166 |
num = ''
|
167 |
if self.isNumber(sliceText): |
168 |
text = text[1:len(text)] |
169 |
num += text |
170 |
while len(text) > 0: |
171 |
sliceText = text[0:1] |
172 |
|
173 |
if self.isNumber(sliceText): |
174 |
text = text[1:len(text)] |
175 |
num += text |
176 |
else:
|
177 |
break
|
178 |
|
179 |
return (True, text, num) |
180 |
else:
|
181 |
return (False,) |
182 |
|
183 |
'''
|
184 |
@brief Check Number
|
185 |
@author kyouho
|
186 |
@date 2018.07.05
|
187 |
@history kyouho 2018.07.09 add Refular Expression
|
188 |
'''
|
189 |
def isNumber(self, num): |
190 |
p = re.compile('(^[0-9]+$)')
|
191 |
result = p.match(num) |
192 |
|
193 |
if result:
|
194 |
return True |
195 |
else:
|
196 |
return False |
197 |
|
198 |
'''
|
199 |
@brief Check Number
|
200 |
@author kyouho
|
201 |
@date 2018.07.05
|
202 |
'''
|
203 |
def isTagSeqNo(self, text, pattern): |
204 |
try:
|
205 |
if pattern is not None: |
206 |
attr = text |
207 |
result = eval(pattern)
|
208 |
if self.isNumber(result): |
209 |
sliceText = text[0:len(result)] |
210 |
return (True, text[len(sliceText):len(text)], sliceText) |
211 |
else:
|
212 |
return (False,) |
213 |
else:
|
214 |
return (False,) |
215 |
except Exception as ex: |
216 |
print('error occured({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, sys.exc_info()[-1].tb_lineno)) |
217 |
|
218 |
'''
|
219 |
@brief Check Number
|
220 |
@author kyouho
|
221 |
@date 2018.07.05
|
222 |
'''
|
223 |
def isLimitWord(self, text, pattern): |
224 |
try:
|
225 |
if pattern is not None: |
226 |
attr = text |
227 |
length = pattern[0]
|
228 |
isNumber = pattern[1]
|
229 |
|
230 |
if length: # Length가 있으면 Length만큼 문자열을 가져와 길이를 검사한다. |
231 |
result = eval('attr[:' + str(length) + ']') |
232 |
if int(length) != len(result): |
233 |
return(False,) |
234 |
elif isNumber:
|
235 |
match = re.search(re.compile('^[0-9]+'), attr)
|
236 |
if match:
|
237 |
result = match.group(match.start()) |
238 |
else:
|
239 |
return (False,) |
240 |
else:
|
241 |
result = attr |
242 |
|
243 |
if isNumber:
|
244 |
if self.isNumber(result): |
245 |
sliceText = text[0:len(result)] |
246 |
return (True, text[len(sliceText):len(text)], sliceText) |
247 |
else:
|
248 |
return (False,) |
249 |
else:
|
250 |
sliceText = text[0:len(result)] |
251 |
return (True, text[len(sliceText):len(text)], sliceText) |
252 |
else:
|
253 |
return (False,) |
254 |
except Exception as ex: |
255 |
print('error occured({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, sys.exc_info()[-1].tb_lineno)) |
256 |
|
257 |
'''
|
258 |
@brief Check if given text is Note No Text (ex : NOTE 1, NOTE 2, ...)
|
259 |
@author Jeongwoo
|
260 |
@date 2018.04.26
|
261 |
@history 2018.05.16 Jeongwoo Modify Validator flag
|
262 |
'''
|
263 |
def create_note_no_text(self, textInfo): |
264 |
item = None
|
265 |
|
266 |
appDocData = AppDocData.instance() |
267 |
configs = appDocData.getConfigs('Note No Tag Rule', 'Note No Expression') |
268 |
if 1 == len(configs) and configs[0].value: |
269 |
expression = configs[0].value
|
270 |
match = re.search(re.compile(expression), textInfo.getText()) |
271 |
|
272 |
if match:
|
273 |
configs = appDocData.getConfigs('Note No Tag Rule', 'Note No Symbol Name') |
274 |
if 1 == len(configs) and configs[0].value: |
275 |
symbol_names = configs[0].value.split(',') |
276 |
for symbol in appDocData.symbols: |
277 |
if symbol.name in symbol_names and symbol.includes(textInfo.center): |
278 |
item = QEngineeringNoteItem(symbol=symbol) |
279 |
break
|
280 |
else:
|
281 |
item = QEngineeringNoteItem() |
282 |
|
283 |
if item:
|
284 |
item.setToolTip(textInfo.getText()) |
285 |
item.setPlainText(textInfo.getText()) |
286 |
|
287 |
return item
|
288 |
|
289 |
'''
|
290 |
@brief check if given text is size text
|
291 |
@author humkyung
|
292 |
@date 2018.05.02
|
293 |
'''
|
294 |
def isSizeText(self, text, delimiter=None): |
295 |
from NominalPipeSize import NominalPipeSizeTable |
296 |
|
297 |
pipe_sizes = NominalPipeSizeTable.instance().pipe_sizes |
298 |
|
299 |
tokens = text.upper().split(delimiter) if delimiter is not None else [text] |
300 |
for token in tokens: |
301 |
matches = [sizeData for sizeData in pipe_sizes if sizeData.find(token.strip())] |
302 |
if not matches: return False |
303 |
|
304 |
return True |
305 |
|
306 |
'''
|
307 |
@brief check if given text is tag no
|
308 |
@author humkyung
|
309 |
@date 2018.05.03
|
310 |
'''
|
311 |
def isTagNoText(self, text): |
312 |
from CodeTables import CodeTable |
313 |
|
314 |
found = CodeTable.instance('EqpTagNames').find_starts_with(text)
|
315 |
|
316 |
return True if found else False |
317 |
|
318 |
def is_valve_operation_code(self, text): |
319 |
"""
|
320 |
check if given text is valve operation code
|
321 |
"""
|
322 |
from CodeTables import CodeTable |
323 |
|
324 |
found = CodeTable.instance('ValveOperCodes').find_starts_with(text)
|
325 |
|
326 |
return True if found else False |
327 |
|
328 |
def is_reserved_word(self, text): |
329 |
""" check if given text is reserved words """
|
330 |
from CodeTables import CodeTable |
331 |
|
332 |
found = CodeTable.instance('ReservedWords').find_starts_with(text)
|
333 |
|
334 |
return True if found else False |