summaryrefslogtreecommitdiffstats
path: root/Project/ImportForm.cpp
blob: e8ad42ecf3bf87f4ce3305df7aed41099b6e7c6e (plain)
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#include "ImportForm.h"
#include "Workspace.h"

ImportForm::ImportForm(wxWindow* parent, Workspace* workspace) : ImportFormBase(parent)
{
    SetInitialSize();

    m_parent = parent;
    m_workspace = workspace;
}

ImportForm::~ImportForm() {}

void ImportForm::OnButtonCancelClick(wxCommandEvent& event)
{
    EndModal(wxID_CANCEL);
    if(m_workspace) delete m_workspace;
}

void ImportForm::OnButtonOKClick(wxCommandEvent& event)
{
    if(ImportSelectedFiles())
        EndModal(wxID_OK);
    else {
        // Error message
        wxMessageDialog msgDialog(this, _("It was not possible to import the selected files."), _("Error"),
                                  wxOK | wxCENTRE | wxICON_ERROR);
        msgDialog.ShowModal();
    }
}

bool ImportForm::ImportSelectedFiles()
{
    ParseAnarede parseAnarede(m_filePickerANAREDELST->GetFileName(), m_filePickerANAREDEPWF->GetFileName());
    if(!parseAnarede.Parse()) return false;
    double scale = 2.0;

    std::vector<Element*> elementList;
    auto components = parseAnarede.GetComponents();
    for(auto it = components.begin(), itEnd = components.end(); it != itEnd; ++it) {
        switch((*it).type) {
            case ANA_BUS: {
                Bus* bus = new Bus(wxPoint2DDouble((*it).position.m_x * scale, (*it).position.m_y * scale));
                bus->SetWidth((*it).length * scale);
                bus->SetPosition(bus->GetPosition());  // Update bus rect
                bus->StartMove(bus->GetPosition());
                // bus->Move(bus->GetPosition() + wxPoint2DDouble(bus->GetWidth() / 2, bus->GetHeight() / 2));
                if((*it).rotationID == 0) {
                    bus->Move(bus->GetPosition() + wxPoint2DDouble(bus->GetWidth() / 2, bus->GetHeight() / 2));
                } else if((*it).rotationID == 1) {
                    for(int i = 0; i < (*it).rotationID * 2; ++i) bus->Rotate();
                    bus->Move(bus->GetPosition() + wxPoint2DDouble(-bus->GetHeight() / 2, bus->GetWidth() / 2));
                } else if((*it).rotationID == 2) {
                    for(int i = 0; i < (*it).rotationID * 2; ++i) bus->Rotate();
                    bus->Move(bus->GetPosition() + wxPoint2DDouble(-bus->GetWidth() / 2, -bus->GetHeight() / 2));
                } else if((*it).rotationID == 3) {
                    for(int i = 0; i < (*it).rotationID * 2; ++i) bus->Rotate();
                    bus->Move(bus->GetPosition() + wxPoint2DDouble(-bus->GetHeight() / 2, -bus->GetWidth() / 2));
                }

                elementList.push_back(bus);
            } break;
            case ANA_GENERATOR: {
            } break;
            case ANA_LOAD: {
            } break;
            case ANA_SHUNT: {
            } break;
            case ANA_MIT: {
            } break;
            case ANA_TRANSFORMER: {
            } break;
            case ANA_LINE: {
            } break;
            case ANA_IND_LOAD: {
            } break;
            case ANA_IND_SHUNT: {
            } break;
            case ANA_IND_GENERATOR: {
            } break;
        }
    }
    m_workspace->SetElementList(elementList);
    return true;
}

ParseAnarede::ParseAnarede(wxFileName lstFile, wxFileName pwfFile)
{
    m_lstFile = lstFile;
    m_pwfFile = pwfFile;
}

bool ParseAnarede::Parse()
{
    wxTextFile lst(m_lstFile.GetFullPath());
    wxTextFile pwf(m_pwfFile.GetFullPath());
    if(!lst.Open()) return false;
    if(!pwf.Open()) return false;

    // Parse LST file
    for(wxString line = lst.GetFirstLine(); !lst.Eof(); line = lst.GetNextLine()) {
        // Current line
        switch(static_cast<char>(line[0])) {
            case 'C': {  // Component
                int parsePosition = 1;

                Component component;

                component.id = wxAtoi(GetLSTLineNextValue(line, parsePosition));
                component.type = static_cast<ElementTypeAnarede>(wxAtoi(GetLSTLineNextValue(line, parsePosition)));

                if(component.type == ANA_BUS) {
                    if(!GetLenghtAndRotationFromBusCode(GetLSTLineNextValue(line, parsePosition), component.length,
                                                        component.rotationID))
                        return false;
                } else {
                    component.rotationID = wxAtoi(GetLSTLineNextValue(line, parsePosition));
                }
                parsePosition += 2;  // Jump to position
                if(!GetLSTLineNextValue(line, parsePosition).ToCDouble(&component.position.m_x)) return false;
                if(!GetLSTLineNextValue(line, parsePosition).ToCDouble(&component.position.m_y)) return false;
                parsePosition += 8;  // Jump to electrical ID
                component.electricalID = wxAtoi(GetLSTLineNextValue(line, parsePosition));

                // Bus connection IDs
                if(component.type != ANA_BUS) {
                    int fromBus = wxAtoi(GetLSTLineNextValue(line, parsePosition));  // Origin bus
                    int toBus = 0;
                    // If the component is a transformer, parse the next value (toBus)
                    if(component.type == ANA_TRANSFORMER) {
                        toBus = wxAtoi(GetLSTLineNextValue(line, parsePosition));  // Destiny bus
                    }
                    // Iterate through the already parsed components (the buses is saved first)
                    for(auto it = m_components.begin(), itEnd = m_components.end(); it != itEnd; ++it) {
                        if((*it).type == ANA_BUS) {
                            if(fromBus == (*it).electricalID) {
                                component.busConnectionID[0] = std::make_pair((*it).id, fromBus);
                            } else if(toBus == (*it).electricalID) {
                                component.busConnectionID[1] = std::make_pair((*it).id, toBus);
                            }
                        }
                    }
                }

                m_components.push_back(component);

            } break;
            case 'L': {  // Line
                int parsePosition = 1;

                PowerLine pLine;

                pLine.id = wxAtoi(GetLSTLineNextValue(line, parsePosition));
                pLine.type = static_cast<ElementTypeAnarede>(wxAtoi(GetLSTLineNextValue(line, parsePosition)));
                parsePosition += 4;  // Jump to from bus

                int fromBus = wxAtoi(GetLSTLineNextValue(line, parsePosition));  // Origin bus
                int toBus = wxAtoi(GetLSTLineNextValue(line, parsePosition));    // Destiny bus

                // Iterate through the already parsed components
                for(auto it = m_components.begin(), itEnd = m_components.end(); it != itEnd; ++it) {
                    if((*it).type == ANA_BUS) {
                        if(fromBus == (*it).id) {
                            pLine.busConnectionID[0] = std::make_pair((*it).id, (*it).electricalID);
                        } else if(toBus == (*it).id) {
                            pLine.busConnectionID[1] = std::make_pair((*it).id, (*it).electricalID);
                        }
                    }
                }
                pLine.electricalID = wxAtoi(GetLSTLineNextValue(line, parsePosition));

                m_lines.push_back(pLine);
            } break;
            case 'U': {  // Connection
                // The connection data depends on the component type, so this data definition must be more generic.
                int parsePosition = 1;

                int id = wxAtoi(GetLSTLineNextValue(line, parsePosition));
                (void)id;            // Unused id.
                parsePosition += 5;  // Jump to next relevant data
                int data1 = wxAtoi(GetLSTLineNextValue(line, parsePosition));
                int data2 = wxAtoi(GetLSTLineNextValue(line, parsePosition));
                int data3 = wxAtoi(GetLSTLineNextValue(line, parsePosition));
                int data4 = wxAtoi(GetLSTLineNextValue(line, parsePosition));
                int data5 = wxAtoi(GetLSTLineNextValue(line, parsePosition));
                int data6 = wxAtoi(GetLSTLineNextValue(line, parsePosition));

                // If data5 differs from -1, the connection is power line data with data5's value as its ID, otherwise
                // data1 is the ID of a component.
                if(data5 != -1) {
                    // Iterate through parsed lines
                    for(auto it = m_lines.begin(), itEnd = m_lines.end(); it != itEnd; ++it) {
                        if(data5 == (*it).id) {
                            (*it).busConnectionNode[0] = std::make_pair(data1, data2);
                            (*it).busConnectionNode[1] = std::make_pair(data3, data4);
                            for(int i = 0; i < data6; ++i) {
                                wxPoint2DDouble nodePt;
                                if(!GetLSTLineNextValue(line, parsePosition).ToCDouble(&nodePt.m_x)) return false;
                                if(!GetLSTLineNextValue(line, parsePosition).ToCDouble(&nodePt.m_y)) return false;
                                (*it).nodesPosition.push_back(nodePt);
                            }
                        }
                    }
                } else {
                    // Iterate through parsed components
                    for(auto it = m_components.begin(), itEnd = m_components.end(); it != itEnd; ++it) {
                        if(data1 == (*it).id) {
                            if((*it).type == ANA_TRANSFORMER) {
                                if(data2 == 1) {  // Primary
                                    (*it).busConnectionNode[0] = std::make_pair(data3, data4);
                                } else if(data2 == 2) {  // Secondary
                                    (*it).busConnectionNode[1] = std::make_pair(data3, data4);
                                }
                            } else {
                                (*it).busConnectionNode[0] = std::make_pair(data3, data4);
                            }
                        }
                    }
                }

            } break;
            default:
                break;
        }
    }
    // Last line

    /*for(auto it = m_components.begin(), itEnd = m_components.end(); it != itEnd; ++it) {
        wxString comp = wxString::Format("ID = %d\n", (*it).id);
        comp += wxString::Format("TIPO = %d\n", (*it).type);
        comp += wxString::Format("TAMANHO = %f\n", (*it).length);
        comp += wxString::Format("ROTACAO = %d\n", (*it).rotationID);
        comp += wxString::Format("POS = (%f , %f)\n", (*it).position.m_x, (*it).position.m_y);
        comp += wxString::Format("ID ELETRICO = %d\n", (*it).electricalID);
        for(int i = 0; i < 2; ++i) {
            comp += wxString::Format("BARRA[%d]<IDg, IDe> = <%d , %d>\n", i, (*it).busConnectionID[i].first,
                                     (*it).busConnectionID[i].second);
        }
        for(int i = 0; i < 2; ++i) {
            comp += wxString::Format("NODE[%d]<IDg, IDp> = <%d , %d>\n", i, (*it).busConnectionNode[i].first,
                                     (*it).busConnectionNode[i].second);
        }
        wxMessageBox(comp);
    }
    for(auto it = m_lines.begin(), itEnd = m_lines.end(); it != itEnd; ++it) {
        wxString comp = wxString::Format("ID = %d\n", (*it).id);
        comp += wxString::Format("TIPO = %d\n", (*it).type);
        comp += wxString::Format("ID ELETRICO = %d\n", (*it).electricalID);
        for(int i = 0; i < 2; ++i) {
            comp += wxString::Format("BARRA[%d]<IDg, IDe> = <%d , %d>\n", i, (*it).busConnectionID[i].first,
                                     (*it).busConnectionID[i].second);
        }
        for(int i = 0; i < 2; ++i) {
            comp += wxString::Format("NODE[%d]<IDg, IDp> = <%d , %d>\n", i, (*it).busConnectionNode[i].first,
                                     (*it).busConnectionNode[i].second);
        }
        for(unsigned int i = 0; i < (*it).nodesPosition.size(); ++i) {
            comp += wxString::Format("NODEPos[%d] = (%f , %f)\n", i, (*it).nodesPosition[i].m_x,
                                     (*it).nodesPosition[i].m_y);
        }
        wxMessageBox(comp);
    }*/
    return true;
}

wxString ParseAnarede::GetLSTLineNextValue(wxString line, int& currentPos)
{
    // Parse each line column
    wxString strValue = "=";
    for(; currentPos < static_cast<int>(line.length()); ++currentPos) {
        if(line[currentPos] != ' ') {  // Skip the firsts whitespaces
            wxString parsedLine = line.Mid(currentPos);
            strValue = parsedLine.BeforeFirst(' ');  // Get the character before the first whitespace
            currentPos += strValue.length();         // Set the current parse position
            break;
        }
    }
    return strValue;
}

bool ParseAnarede::GetLenghtAndRotationFromBusCode(wxString code, double& lenght, int& rotationID)
{
    long longCode;
    if(!code.ToLong(&longCode)) return false;
    std::stringstream hexCode;
    hexCode << std::hex << longCode;
    wxString hexCodeStr = hexCode.str();
    wxString lenghtStr = hexCodeStr.Left(hexCodeStr.length() - 1);
    wxString rotationIDStr = hexCodeStr.Right(1);
    if(!lenghtStr.ToDouble(&lenght)) return false;
    lenght = 4.8 * lenght - 16.0;
    long rotationIDLong;
    if(!rotationIDStr.ToLong(&rotationIDLong)) return false;
    rotationID = static_cast<int>(rotationIDLong);
    return true;
}