프로젝트

일반

사용자정보

통계
| 개정판:

hytos / HYTOS / HYTOS / Shapes / EngineeringRunItem.py @ ce5ca212

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

1
# coding: utf-8
2
""" This is engineering run item moduel """
3

    
4
import sys
5
import os.path
6
import copy
7
try:
8
    from PyQt5.QtCore import Qt, QPointF, QRectF, pyqtSignal, QObject, QT_VERSION_STR
9
    from PyQt5.QtGui import QImage, QPixmap, QPainterPath, QBrush, QPen, QTransform
10
    from PyQt5.QtWidgets import QGraphicsView, QGraphicsScene, QFileDialog, QGraphicsItem, QAbstractGraphicsShapeItem, QGraphicsPathItem
11
except ImportError:
12
    try:
13
        from PyQt4.QtCore import Qt, QRectF, pyqtSignal, QObject, QT_VERSION_STR
14
        from PyQt4.QtGui import QGraphicsView, QGraphicsScene, QImage, QPixmap, QPainterPath, QFileDialog
15
    except ImportError:
16
        raise ImportError("ImageViewerQt: Requires PyQt5 or PyQt4.")
17

    
18
from EngineeringAbstractItem import QEngineeringAbstractItem
19

    
20
class QEngineeringRunItem(QEngineeringAbstractItem):
21
    """ This is engineering run item class """
22

    
23
    def __init__(self):
24
        import uuid
25
        QEngineeringAbstractItem.__init__(self)
26

    
27
        self.uid = uuid.uuid4() # generate UUID
28
        self._items = []
29
        self._visible = False
30
        self.owner = None
31

    
32
    @property
33
    def Type(self):
34
        """ return type of run(Drain/Process) """
35
        from SymbolSvgItem import SymbolSvgItem
36
        from EngineeringLineItem import QEngineeringLineItem
37

    
38
        if len(self._items) == 2:
39
            if 1 == len([item for item in self._items if type(item) is QEngineeringLineItem]) and\
40
            1 == len([item for item in self._items if type(item) is SymbolSvgItem]): return 'Drain'
41

    
42
        return 'Process'
43

    
44
    @property
45
    def visible(self):
46
        """ return visible """
47
        return self._visible
48

    
49
    @visible.setter
50
    def visible(self, value):
51
        """ set visible value """
52
        self._visible = value
53
        for item in self.items:
54
            if issubclass(type(item), QGraphicsItem):
55
                item.setVisible(value)
56

    
57
    '''
58
        @brief  getter of items
59
        @author humkyung
60
        @date   2018.05.15
61
    '''
62
    @property
63
    def items(self):
64
        return self._items
65

    
66
    '''
67
        @brief  setter of items
68
        @author humkyung
69
        @date   2018.05.15
70
    '''
71
    @items.setter
72
    def items(self, value):
73
        self._items = value
74

    
75
    def explode(self):
76
        """ explode run and subtract it from line no """
77

    
78
        try:
79
            for item in self.items:
80
                item.owner = None
81
        finally:
82
            self.items.clear()
83

    
84
            if self.owner: self.owner.runs.remove(self)
85

    
86
    '''
87
        @brief  get two points which's length is max
88
        @author humkyung
89
        @date   2018.05.23
90
    '''
91
    def getLongestTwoPoints(self, pts):
92
        import math
93
        res = [None, None]
94

    
95
        maxDistance = None
96
        for i in range(len(pts)):
97
            for j in range(i+1, len(pts)):
98
                dx = pts[i][0] - pts[j][0]
99
                dy = pts[i][1] - pts[j][1]
100
                dist = math.sqrt(dx*dx + dy*dy)
101
                if (maxDistance is None) or (maxDistance < dist):
102
                    maxDistance = dist
103
                    res[0] = pts[i]
104
                    res[1] = pts[j]
105
        
106
        return res
107

    
108
    def arrange_flow_direction(self):
109
        """ arrange flow direction of line """
110
        from EngineeringLineItem import QEngineeringLineItem
111

    
112
        for at in range(len(self.items)):
113
            if type(self.items[at]) is QEngineeringLineItem and at > 0:
114
                self.items[at].arrange_flow_direction(self.items[at-1])
115
            elif type(self.items[at]) is QEngineeringLineItem and len(self.items) > 1:
116
                self.items[at].arrange_flow_direction(self.items[at+1])
117
                self.items[at].reverse()
118

    
119
    def reverse(self):
120
        """ reverse line's flow direction """
121
        from EngineeringLineItem import QEngineeringLineItem
122

    
123
        self.items.reverse()
124
        for at in range(len(self.items)):
125
            if type(self.items[at]) is QEngineeringLineItem:
126
                self.items[at].reverse()
127

    
128
    '''
129
        @brief  add flow mark
130
        @author humkyung
131
        @date   2018.06.17
132
    '''
133
    def addFlowMark(self):
134
        from EngineeringLineItem import QEngineeringLineItem
135

    
136
        try:
137
            connectedLines = [item for item in self.items if type(item) is QEngineeringLineItem]
138
                    
139
            maxLength = None
140
            maxLengthItem = None
141
            for line in connectedLines:
142
                length = line.length()
143
                if maxLength is None or maxLength < length:
144
                    maxLength = length
145
                    maxLengthItem = line
146

    
147
            if maxLengthItem is not None: maxLengthItem.addFlowArrow()
148
        except Exception as ex:
149
            from App import App
150
            from AppDocData import MessageType
151
            
152
            message = 'error occurred({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, sys.exc_info()[-1].tb_lineno)
153
            App.mainWnd().addMessage.emit(MessageType.Error, message)
154

    
155
    '''
156
        @brief  write to xml
157
        @author humkyung
158
        @date   2018.05.16
159
        @history    Jeongwoo 2018.05.24 Add math.fabs() on sublist's if-statement
160
                                        Change toler(1 → 5) and Modify Longest Line's coords // Make comments Test Code
161
                    humkyung 2018.06.22 sort lines before writing to xml
162
                    kyouho  2018.08.09 Line 묶기
163
    '''
164
    def toXml(self):
165
        from xml.etree.ElementTree import Element, SubElement, dump, ElementTree
166
        from EngineeringLineItem import QEngineeringLineItem
167
        from SymbolSvgItem import SymbolSvgItem
168

    
169
        try:
170
            node = Element('RUN')
171
            node.attrib['TYPE'] = self.Type
172
            for item in self.items:
173
                # skip line item which's connected items are same
174
                if (type(item) is QEngineeringLineItem) and item.connectors[0].connectedItem is not None and (item.connectors[0].connectedItem is item.connectors[1].connectedItem):
175
                    continue
176
                # up to here
177

    
178
                node.append(item.toXml())
179

    
180
        except Exception as ex: 
181
            from App import App 
182
            from AppDocData import MessageType
183

    
184
            message = 'error occurred({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, sys.exc_info()[-1].tb_lineno)
185
            App.mainWnd().addMessage.emit(MessageType.Error, message)
186
            return None
187

    
188
        return node
클립보드 이미지 추가 (최대 크기: 500 MB)