-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmodels.py
More file actions
212 lines (168 loc) · 7.7 KB
/
Copy pathmodels.py
File metadata and controls
212 lines (168 loc) · 7.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# -*- encoding: utf-8 -*-
from xml.etree import ElementTree
from xml.dom import minidom
from PySide import QtCore, QtGui
from graphics import Node, Edge
from bs4 import BeautifulSoup
class DiagramToXML(ElementTree.Element):
'''
Esta classe possui as funções que armazenam as informações
necessárias à reconstrução do diagrama grafico em um
arquivo XML
'''
def __init__(self, scene):
'''
Função que inicializa o objeto criado pela classe DiagramToXML
'''
super(DiagramToXML, self).__init__('items')
self.scene = scene
lista = self.scene.items()
lista.reverse()
for item in self.scene.items():
if isinstance(item, Node):
node = ElementTree.Element(
'node', attrib={'type': str(item.myItemType)})
id = ElementTree.Element('id')
id.text = str(item.id)
node.append(id)
x = ElementTree.Element('x')
x.text = str(item.scenePos().x())
node.append(x)
y = ElementTree.Element('y')
y.text = str(item.scenePos().y())
node.append(y)
width = ElementTree.Element('width')
width.text = str(item.rect().width())
node.append(width)
height = ElementTree.Element('height')
height.text = str(item.rect().height())
node.append(height)
self.append(node)
for item in lista:
if isinstance(item, Edge):
edge = ElementTree.Element('edge')
w1 = ElementTree.Element('w1')
w1.text = str(item.w1.id)
w2 = ElementTree.Element('w2')
w2.text = str(item.w2.id)
edge.append(w1)
edge.append(w2)
self.append(edge)
def write_xml(self, path):
'''
Função que cria o arquivo XML na localização indicada pelo
argumento path
'''
xml_string = ElementTree.tostring(self)
dom_element = (minidom.parseString(xml_string))
f = open(path, 'w')
f.write(dom_element.toprettyxml())
f.close()
class XMLToDiagram():
'''
Classe que realiza a conversão do arquivo XML com as informações do
diagrama em um diagrama gráfico interativo.
'''
def __init__(self, scene, file_path):
self.scene = scene
self.file_path = file_path
xml_tree = ElementTree.parse(self.file_path)
xml_element = xml_tree.getroot()
self.scene.clear()
for child in xml_element:
if child.tag == 'node':
if child.attrib['type'] == '0':
item = Node(
int(child.attrib['type']), self.scene.mySubstationMenu)
self.scene.addItem(item)
item.setPos(
float(child.find('x').text), float(
child.find('y').text))
item.id = int(child.find('id').text)
elif child.attrib['type'] == '1':
item = Node(
int(child.attrib['type']), self.scene.myRecloserMenu)
item.id = int(child.find('id').text)
item.setPos(float(child.find('x').text), float(
child.find('y').text))
self.scene.addItem(item)
elif child.attrib['type'] == '2':
item = Node(int(
child.attrib['type']), self.scene.myBusMenu)
item.setPos(float(child.find('x').text), float(
child.find('y').text))
item.id = int(child.find('id').text)
item.setRect(
0, 0, float(child.find('width').text), float(
child.find('height').text))
self.scene.addItem(item)
elif child.attrib['type'] == '3':
item = Node(int(child.attrib['type']), None)
item.setPos(
float(child.find('x').text), float(
child.find('y').text))
item.id = int(child.find('id').text)
self.scene.addItem(item)
elif child.attrib['type'] == '4':
item = Node(int(child.attrib['type']), None)
item.setPos(
float(child.find('x').text), float(
child.find('y').text))
item.id = int(child.find('id').text)
self.scene.addItem(item)
elif child.attrib['type'] == '5':
item = Node(int(child.attrib['type']), None)
item.setPos(
float(child.find('x').text), float(
child.find('y').text))
item.id = int(child.find('id').text)
self.scene.addItem(item)
elif child.tag == 'edge':
for item in self.scene.items():
if isinstance(item, Node) and item.id == int(child.find('w1').text):
w1 = item
elif isinstance(item, Node) and item.id == int(child.find('w2').text):
w2 = item
edge = Edge(w1, w2, self.scene.myLineMenu)
self.scene.addItem(edge)
self.scene.addItem(edge.GhostRetItem)
class CimXML():
'''Classe que representa os dados dos componentes em padrão CIM'''
def __init__(self, scene):
self.scene = scene
self.cim_xml = BeautifulSoup()
self.cim_xml.append(self.cim_xml.new_tag("Node"))
self.cim_xml.find('Node').append(self.cim_xml.new_tag("Breaker"))
for item in scene.items():
if isinstance(item, Node):
if item.myItemType == item.Religador:
tag_id = self.cim_xml.new_tag(str(item.id))
self.cim_xml.find("Breaker").append(tag_id)
tag_rc = self.cim_xml.new_tag("ratedCurrent")
tag_rc.append(str(item.chave.ratedCurrent))
tag_id.append(tag_rc)
tag_itt = self.cim_xml.new_tag("inTransitTime")
tag_itt.append(str(item.chave.inTransitTime))
tag_id.append(tag_itt)
tag_bc = self.cim_xml.new_tag("breakingCapacity")
tag_bc.append(str(item.chave.breakingCapacity))
tag_id.append(tag_bc)
tag_rs = self.cim_xml.new_tag("recloseSequences")
tag_rs.append(str(item.chave.recloseSequences))
tag_id.append(tag_rs)
tag_state = self.cim_xml.new_tag("state")
tag_state.append(str(item.chave.estado))
tag_id.append(tag_state)
# self.cim_xml.find(str(item.id)).append(self.cim_xml.new_tag("ratedCurrent"))
# self.cim_xml.find(str(item.id)).append(self.cim_xml.new_tag("inTransitTime"))
# self.cim_xml.find(str(item.id)).append(self.cim_xml.new_tag("breakingCapacity"))
# self.cim_xml.find(str(item.id)).append(self.cim_xml.new_tag("recloseSequences"))
# self.cim_xml.find(str(item.id)).append(self.cim_xml.new_tag("state"))
def write_xml(self, path):
'''
Função que cria o arquivo XML na localização indicada pelo
argumento path
'''
f = open(path, 'w')
f.write(self.cim_xml.prettify())
f.close()