프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / DTI_PID / Shapes / EngineeringInstrumentItem.py @ a4806b86

이력 | 보기 | 이력해설 | 다운로드 (10.5 KB)

1
# coding: utf-8
2
"""
3
This is Engineering Instrument Item module
4
"""
5

    
6
import sys
7
import os
8
import math
9
from PyQt5.QtGui import *
10
from PyQt5.QtCore import *
11
from PyQt5.QtSvg import *
12
from PyQt5.QtWidgets import (QApplication, QGraphicsItem)
13

    
14
from SymbolSvgItem import SymbolSvgItem
15
from EngineeringConnectorItem import QEngineeringConnectorItem
16
from EngineeringTextItem import QEngineeringTextItem
17
from UserInputAttribute import UserInputAttribute
18

    
19
class QEngineeringInstrumentItem(SymbolSvgItem):
20
    """
21
    This is Engineering Instrument Item class
22
    """
23
    clicked = pyqtSignal(QGraphicsSvgItem)
24
    INST_COLUMN_LIST = None 
25

    
26
    '''
27
    '''
28
    def __init__(self, path, uid=None, flip=None):
29
        SymbolSvgItem.__init__(self, path, uid, flip=flip)
30

    
31
        self._measuredVairableCode = None
32
        self._typeModifier = None
33
        self._tagSeqNo = None
34
        self._tagSuffix = None
35

    
36
        if QEngineeringInstrumentItem.INST_COLUMN_LIST is None:
37
            from AppDocData import AppDocData
38

    
39
            appDocData = AppDocData.instance()
40
            QEngineeringInstrumentItem.INST_COLUMN_LIST = appDocData.getColNames('INSTRUMENT_DATA_LIST')
41

    
42
    def texts(self):
43
        """
44
        return text type of labels
45
        """
46
        from EngineeringTextItem import QEngineeringTextItem
47

    
48
        return sorted([x for x in self.associations() if issubclass(type(x), QEngineeringTextItem)], key=lambda attr: attr.loc[1])
49

    
50
    '''
51
        @brief  getter of measured variable code
52
        @author humkyung
53
        @date   2018.05.06
54
    '''
55
    @property
56
    def measuredVariableCode(self):
57
        return self._measuredVairableCode
58

    
59
    '''
60
        @brief  setter of measured variable code
61
        @author humkyung
62
        @date   2018.05.06
63
    '''
64
    @measuredVariableCode.setter
65
    def measuredVariableCode(self, value):
66
        self._measuredVairableCode = value
67

    
68
    '''
69
        @brief  getter of type modifier
70
        @author humkyung
71
        @date   2018.05.06
72
    '''
73
    @property
74
    def typeModifier(self):
75
        return self._typeModifier
76
    
77
    '''
78
        @brief  setter of type modifier
79
        @author humkyung
80
        @date   2018.05.06
81
    '''
82
    @typeModifier.setter
83
    def typeModifier(self, value):
84
        self._typeModifier = value
85
    
86
    '''
87
        @brief  getter of tag seq no
88
        @author humkyung
89
        @date   2018.05.06
90
    '''
91
    @property
92
    def tagSeqNo(self):
93
        return self._tagSeqNo
94
    
95
    '''
96
        @brief  setter of tag seq no
97
        @author humkyung
98
        @date   2018.05.06
99
    '''
100
    @tagSeqNo.setter
101
    def tagSeqNo(self, value):
102
        self._tagSeqNo = value
103

    
104
    '''
105
        @brief  getter of tag suffix
106
        @author humkyung
107
        @date   2018.05.06
108
    '''
109
    @property
110
    def tagSuffix(self):
111
        return self._tagSuffix
112

    
113
    '''
114
        @brief  setter of tag suffix
115
        @author humkyung
116
        @date   2018.05.06
117
    '''
118
    @tagSuffix.setter
119
    def tagSuffix(self, value):
120
        self._tagSuffix = value
121

    
122
    '''
123
        @brief  connect attribute
124
        @author humkyung
125
        @date   2018.05.06
126
    '''
127
    def connectAttribute(self, attributes):
128
        self._associations.clear()
129

    
130
        try:
131
            rect = self.sceneBoundingRect()
132
            for attr in attributes:
133
                if rect.contains(attr.center()):
134
                    if issubclass(type(attr), QEngineeringTextItem):
135
                        attr.owner = self  # set owner of text
136
                        self.add_assoc_item(attr)
137
                    elif issubclass(type(attr), SymbolSvgItem):
138
                        attr._owner = self.uid  # set owner of symbol
139
                        self.add_assoc_item(attr)
140
        except Exception as ex:
141
            from App import App 
142
            from AppDocData import MessageType
143

    
144
            message = 'error occured({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, sys.exc_info()[-1].tb_lineno)
145
            App.mainWnd().addMessage.emit(MessageType.Error, message)
146

    
147
    '''
148
        @brief      get attributes
149
        @author     humkyung
150
        @date       2018.06.14
151
    '''
152
    def getAttributes(self):
153
        _attrs = {}
154
        try:
155
            from AppDocData import AppDocData
156
            from EngineeringTextItem import QEngineeringTextItem
157

    
158
            # 해당 Type의 attribute setting
159
            docData = AppDocData.instance()
160
            symbolAttrs = docData.getSymbolAttribute(self.type)
161
            _texts = self.texts()
162
            _symbols = self.symbols()
163
            for attr in symbolAttrs:
164
                if attr.AttributeType == 'Size Text Item' or attr.AttributeType == 'Text Item' or attr.AttributeType == 'Valve Oper Code':
165
                    at = int(attr.AttrAt)
166
                    items = [text for text in _texts if self.assoc_type(text) == attr.AttributeType]
167
                    if len(items) > at:
168
                        item = items[at]
169
                        _attrs[attr] = eval(attr.Expression) if attr.Expression else ''
170
                    else:
171
                        _attrs[attr] = ''
172
                elif attr.AttributeType == 'Symbol Item':
173
                    at = int(attr.AttrAt)
174
                    if len(_symbols) > at:
175
                        item = _symbols[at]
176
                        _attrs[attr] = eval(attr.Expression) if attr.Expression else ''
177
                    else:
178
                        _attrs[attr] = ''
179
                else:
180
                    matches = [prop for prop in self.attrs if prop.UID == attr.UID]
181
                    if len(matches) == 1:
182
                        _attrs[matches[0]] = self.attrs[matches[0]]
183
                    else:
184
                        _attrs[attr] = ''
185
        except Exception as ex:
186
            from App import App 
187
            from AppDocData import MessageType
188

    
189
            message = 'error occured({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, sys.exc_info()[-1].tb_lineno)
190
            App.mainWnd().addMessage.emit(MessageType.Error, message)
191
        
192
        return _attrs
193

    
194
    '''
195
        @brief  generate xml code for attribute
196
        @author humkyung
197
        @date   2018.05.06
198
    '''
199
    def toXmlAsAttribute(self, parentNode):
200
        from xml.etree.ElementTree import Element, SubElement, dump, ElementTree
201

    
202
        try:
203
            attrNode = Element('ATTRIBUTE')
204

    
205
            uidNode = Element('UID')
206
            uidNode.text = str(self.uid)
207
            attrNode.append(uidNode)
208

    
209
            nameNode = Element('NAME')
210
            nameNode.text = 'Measured Variable Code'
211
            attrNode.append(nameNode)
212

    
213
            valueNode = Element('VALUE')
214
            valueNode.text = self.measuredVariableCode
215
            attrNode.append(valueNode)
216

    
217
            parentNode.append(attrNode) 
218
        except Exception as ex:
219
            print('error occured({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, sys.exc_info()[-1].tb_lineno))
220

    
221
        try:
222
            attrNode = Element('ATTRIBUTE')
223

    
224
            uidNode = Element('UID')
225
            uidNode.text = str(self.uid)
226
            attrNode.append(uidNode)
227

    
228
            nameNode = Element('NAME')
229
            nameNode.text = 'Type Modifier'
230
            attrNode.append(nameNode)
231

    
232
            valueNode = Element('VALUE')
233
            valueNode.text = self.typeModifier
234
            attrNode.append(valueNode)
235

    
236
            parentNode.append(attrNode) 
237
        except Exception as ex:
238
            print('error occured({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, sys.exc_info()[-1].tb_lineno))
239

    
240
        try:
241
            attrNode = Element('ATTRIBUTE')
242

    
243
            uidNode = Element('UID')
244
            uidNode.text = str(self.uid)
245
            attrNode.append(uidNode)
246

    
247
            nameNode = Element('NAME')
248
            nameNode.text = 'Tag Seq No'
249
            attrNode.append(nameNode)
250

    
251
            valueNode = Element('VALUE')
252
            valueNode.text = self.tagSeqNo
253
            attrNode.append(valueNode)
254

    
255
            parentNode.append(attrNode) 
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
        try:
260
            attrNode = Element('ATTRIBUTE')
261

    
262
            uidNode = Element('UID')
263
            uidNode.text = str(self.uid)
264
            attrNode.append(uidNode)
265

    
266
            nameNode = Element('NAME')
267
            nameNode.text = 'Tag Suffix'
268
            attrNode.append(nameNode)
269

    
270
            valueNode = Element('VALUE')
271
            valueNode.text = self.tagSuffix
272
            attrNode.append(valueNode)
273

    
274
            parentNode.append(attrNode) 
275
        except Exception as ex:
276
            print('error occured({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, sys.exc_info()[-1].tb_lineno))
277

    
278
    def toSql(self):
279
        """
280
        convert instrument data to sql query
281
        """
282
        from AppDocData import AppDocData
283

    
284
        appDocData = AppDocData.instance()
285

    
286
        cols = ['UID', 'PNID_NO']
287
        values = ['?','?']
288
        param = [str(self.uid), appDocData.activeDrawing.name]
289
        _attrs = self.getAttributes()
290
        for key in _attrs.keys():
291
            cols.append(key.Attribute)
292
            values.append('?')
293
            param.append(_attrs[key])
294

    
295
        sql = 'insert or replace into INSTRUMENT_DATA_LIST({}) values({})'.format(','.join(cols), ','.join(values))
296

    
297
        return (sql, tuple(param))
298

    
299
    '''
300
        @brief      return inst Data List
301
        @author     kyouho
302
        @date       2018.08.14
303
    '''
304
    def getInstrumentDataList(self):
305
        dataList = []
306
        try:
307
            from AppDocData import AppDocData
308

    
309
            docData = AppDocData.instance()
310
            attrs = self.getAttributes()
311

    
312
            for index in range(len(QEngineeringInstrumentItem.INST_COLUMN_LIST)):
313
                dataList.append('')
314

    
315
            dataList[0] = str(self.uid)
316
            dataList[13] = docData.imgName
317

    
318
            for key in attrs.keys():
319
                attrInfo = docData.getSymbolAttributeByUID(key.UID)
320
                attrName = attrInfo.Attribute
321
                if attrName in QEngineeringInstrumentItem.INST_COLUMN_LIST:
322
                    colIndex = QEngineeringInstrumentItem.INST_COLUMN_LIST.index(attrName)
323
                    dataList[colIndex] = attrs[key]
324
        except Exception as ex:
325
            from App import App 
326
            from AppDocData import MessageType
327

    
328
            message = 'error occured({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, sys.exc_info()[-1].tb_lineno)
329
            App.mainWnd().addMessage.emit(MessageType.Error, message)
330

    
331
        return dataList