프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / DTI_PID / Shapes / EngineeringEquipmentItem.py @ 673d8cba

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

1
# coding: utf-8
2

    
3
import sys
4
import os
5
import math
6
from PyQt5.QtGui import *
7
from PyQt5.QtCore import *
8
from PyQt5.QtSvg import *
9
from PyQt5.QtWidgets import (QApplication, QGraphicsItem)
10

    
11
from SymbolSvgItem import SymbolSvgItem
12
from EngineeringConnectorItem import QEngineeringConnectorItem
13
from EngineeringTextItem import QEngineeringTextItem
14
from UserInputAttribute import UserInputAttribute
15

    
16
class QEngineeringEquipmentItem(SymbolSvgItem):
17
    clicked = pyqtSignal(QGraphicsSvgItem)
18
    ZVALUE = 10
19
    EQUIP_COLUMN_LIST = None
20

    
21
    '''
22
    '''
23
    def __init__(self, path, uid=None):
24
        SymbolSvgItem.__init__(self, path, uid)
25
        self.setZValue(QEngineeringEquipmentItem.ZVALUE)
26

    
27
        if QEngineeringEquipmentItem.EQUIP_COLUMN_LIST is None:
28
            from AppDocData import AppDocData
29

    
30
            appDocData = AppDocData.instance()
31
            QEngineeringEquipmentItem.EQUIP_COLUMN_LIST = appDocData.getColNames('EQUIPMENT_DATA_LIST')
32

    
33
    def texts(self):
34
        """
35
        return text type of labels
36
        """
37
        from EngineeringTextItem import QEngineeringTextItem
38

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

    
41
    '''
42
        @brief  connect attribute
43
        @author humkyung
44
        @date   2018.05.03
45
    '''
46
    def connectAttribute(self, attributes):
47
        from QEngineeringTagNoTextItem import QEngineeringTagNoTextItem
48

    
49
        self._associations.clear()
50

    
51
        try:
52
            rect = self.sceneBoundingRect()
53
            attrs = [attr for attr in attributes if type(attr) is QEngineeringTagNoTextItem]
54
            for attr in attrs:
55
                if rect.contains(attr.center()):
56
                    self.add_assoc_item(attr)
57

    
58
            if not 'Text Item' in self._associations or not self._associations['Text Item']:
59
                minDist = None
60
                selected = None
61
                # get nearest text from equipment
62
                for attr in attrs:
63
                    dx = attr.center().x() - rect.center().x()
64
                    dy = attr.center().y() - rect.center().y()
65
                    dist = math.sqrt(dx*dx + dy*dy)
66
                    if (minDist is None) or (dist < minDist):
67
                        minDist = dist
68
                        selected = attr
69
            
70
                if selected is not None: self.add_assoc_item(selected)
71
        except Exception as ex:
72
            from App import App 
73
            from AppDocData import MessageType
74

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

    
78
    '''
79
        @brief      get attributes
80
        @author     humkyung
81
        @date       2018.06.14
82
    '''
83
    def getAttributes(self):
84
        _attrs = {}
85
        try:
86
            from AppDocData import AppDocData
87
            from EngineeringTextItem import QEngineeringTextItem
88

    
89
            # 해당 Type의 attribute setting
90
            docData = AppDocData.instance()
91
            symbolAttrs = docData.getSymbolAttribute(self.type)
92
            
93
            _texts = self.texts()
94
            _symbols = self.symbols()
95
            for attr in symbolAttrs:
96
                if attr.AttributeType == 'Text Item':
97
                    at = int(attr.AttrAt)
98
                    if len(_texts) > at:
99
                        item = _texts[at]
100
                        _attrs[attr] = eval(attr.Expression) if attr.Expression else ''
101
                    else:
102
                        _attrs[attr] = ''
103
                elif attr.AttributeType == 'Symbol Item':
104
                    at = int(attr.AttrAt)
105
                    if len(_symbols) > at:
106
                        item = _symbols[at]
107
                        _attrs[attr] = eval(attr.Expression) if attr.Expression else ''
108
                    else:
109
                        _attrs[attr] = ''
110
                else:
111
                    _attrs[attr] = ''#self.attrs[attr[1]] if attr[1] in self.attrs.keys() else ''
112
        except Exception as ex:
113
            from App import App 
114
            from AppDocData import MessageType
115

    
116
            message = 'error occured({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, sys.exc_info()[-1].tb_lineno)
117
            App.mainWnd().addMessage.emit(MessageType.Error, message)
118
        
119
        return _attrs
120

    
121
    '''
122
        @brief      generate xml code for equipment
123
        @author     humkyung
124
        @date       2018.05.09
125
    '''
126
    def toXml(self):
127
        from xml.etree.ElementTree import Element, SubElement, dump, ElementTree
128

    
129
        try:
130
            node = SymbolSvgItem.toXml(self)
131
            self.toXmlAsAttribute(node)
132
        except Exception as ex:
133
            print('error occured({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, sys.exc_info()[-1].tb_lineno))
134

    
135
        return node 
136

    
137
    '''
138
        @brief  generate xml code for attribute
139
        @author humkyung
140
        @date   2018.05.03
141
    '''
142
    def toXmlAsAttribute(self, parentNode):
143
        for attr in self.texts():
144
            parentNode.append(attr.toXml(self, None))
145

    
146
    def toSql(self):
147
        """
148
        convert equipment data to sql query
149
        """
150
        from AppDocData import AppDocData
151
        res = []
152

    
153
        appDocData = AppDocData.instance()
154

    
155
        cols = ['UID', 'PNID_NO']
156
        values = ['?','?']
157
        param = [str(self.uid), appDocData.activeDrawing.name]
158

    
159
        _attrs = self.getAttributes()
160

    
161
        for key in _attrs.keys():
162
            cols.append(key.Attribute)
163
            values.append('?')
164
            param.append(_attrs[key])
165

    
166
        sql = 'insert or replace into EQUIPMENT_DATA_LIST({}) values({})'.format(','.join(cols), ','.join(values))
167

    
168
        '''
169
        for key in _attrs.keys():
170
            if key.Attribute in QEngineeringEquipmentItem.EQUIP_COLUMN_LIST:
171
                cols.append(key.Attribute)
172
                values.append('?')
173
                param.append(_attrs[key])
174
            else:
175
                _cols = ['Symbol_UID', 'SymbolAttribute_UID', 'Value', 'Drawings_Name']
176
                _values = ['?', '?', '?', '?']
177
                _param = [str(self.uid), key.UID, _attrs[key], appDocData.activeDrawing.name]
178

179
                _sql = 'insert or replace into Attributes({}) values({})'.format(','.join(_cols), ','.join(_values))
180
                res.append((_sql, tuple(_param)))
181
        
182
        sql = 'insert or replace into EQUIPMENT_DATA_LIST({}) values({})'.format(','.join(cols), ','.join(values))
183
        res.insert(0, (sql, tuple(param)))
184
        
185
        return res
186
        '''
187
        return (sql, tuple(param))
188

    
189
    '''
190
        @brief      return equip Data List
191
        @author     kyouho
192
        @date       2018.08.14
193
    '''
194
    def getEquipmentDataList(self):
195
        dataList = []
196
        try:
197
            from AppDocData import AppDocData
198

    
199
            docData = AppDocData.instance()
200
            attrs = self.attrs
201

    
202
            for index in range(len(QEngineeringEquipmentItem.EQUIP_COLUMN_LIST)):
203
                dataList.append('')
204

    
205
            dataList[0] = str(self.uid)
206
            dataList[14] = docData.imgName
207

    
208
            for attr in attrs:
209
                attrInfo = docData.getSymbolAttributeByUID(attr.attribute)
210
                attrName = attrInfo[0]
211
                if QEngineeringEquipmentItem.EQUIP_COLUMN_LIST.count(attrName):
212
                    colIndex = QEngineeringEquipmentItem.EQUIP_COLUMN_LIST.index(attrName)
213
                    
214
                    if type(attr) is UserInputAttribute:
215
                        dataList[colIndex] = attr.text if attr.text is not None else ''
216
                    elif type(attr) is QEngineeringTextItem:
217
                        dataList[colIndex] = attr.text()
218
                    else:
219
                        dataList[colIndex] = attr.uid
220
            
221
        except Exception as ex:
222
            print('error occured({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, sys.exc_info()[-1].tb_lineno))
223

    
224
        return dataList