@@ -295,65 +295,46 @@ void PlotWindow::plot()
295295 // PLOT CSV
296296 else if (mFile .fileName ().endsWith (" csv" ))
297297 {
298- // open the file
299- mFile .open (QIODevice::ReadOnly);
300- mpTextStream = new QTextStream (&mFile );
301- currentLine = mpTextStream->readLine ();
302- currentLine.remove (QChar (' "' ));
303- QStringList allVariablesInFile = currentLine.split (" ," );
304- allVariablesInFile.removeLast ();
305-
298+ /* open the file */
306299 QStringList variablesPlotted;
307- QVector<int > variablesToPlotIndex;
308- for (int j = 0 ; j < allVariablesInFile.length (); j++)
309- {
310- if ((mVariablesList .contains (allVariablesInFile[j])) or (getPlotType () == PlotWindow::PLOTALL))
311- {
312- variablesToPlotIndex.push_back (j);
313- variablesPlotted.append (allVariablesInFile[j]);
314- }
315- }
316-
317- // create plot curves
318- PlotCurve *pPlotCurve[variablesToPlotIndex.size ()];
319- for (int i = 0 ; i < variablesToPlotIndex.size (); i++)
320- {
321- pPlotCurve[i] = new PlotCurve (mpPlot);
322- pPlotCurve[i]->setFileName (QFileInfo (mFile ).fileName ());
323- mpPlot->addPlotCurve (pPlotCurve[i]);
324- pPlotCurve[i]->setTitle (variablesPlotted.at (i));
325- }
300+ struct csv_data *csvReader;
301+ csvReader = read_csv (mFile .fileName ().toStdString ().c_str ());
302+ if (csvReader == NULL )
303+ throw PlotException (tr (" Failed to open simulation result file %1" ).arg (mFile .fileName ()));
326304
327- // Assign Values
328- while (!mpTextStream->atEnd ())
305+ // Read in timevector
306+ double *timeVals = read_csv_dataset (csvReader, " time" );
307+ if (timeVals == NULL )
308+ throw NoVariableException (" Variable doesnt exist: time" );
309+ // read in all values
310+ for (int i = 0 ; i < csvReader->numvars ; i++)
329311 {
330- currentLine = mpTextStream->readLine ();
331- QStringList values = currentLine.split (" ," );
332- values.removeLast ();
333-
334- for (int i = 0 ; i < variablesToPlotIndex.size (); i++)
312+ if (mVariablesList .contains (csvReader->variables [i]) or getPlotType () == PlotWindow::PLOTALL)
335313 {
336- QString valuesString = values[0 ];
337- pPlotCurve[i]->addXAxisValue (valuesString.toDouble ());
338- QString valuesString2 = values[variablesToPlotIndex[i]];
339- pPlotCurve[i]->addYAxisValue (valuesString2.toDouble ());
314+ variablesPlotted.append (csvReader->variables [i]);
315+ double *vals = read_csv_dataset (csvReader, csvReader->variables [i]);
316+ if (vals == NULL )
317+ throw NoVariableException (tr (" Variable doesnt exist: %1" ).arg (csvReader->variables [i]).toStdString ().c_str ());
318+ PlotCurve *pPlotCurve = new PlotCurve (mpPlot);
319+ pPlotCurve = new PlotCurve (mpPlot);
320+ pPlotCurve->setFileName (QFileInfo (mFile ).fileName ());
321+ mpPlot->addPlotCurve (pPlotCurve);
322+ pPlotCurve->setTitle (csvReader->variables [i]);
323+ for (int i = 0 ; i < csvReader->numsteps ; i++)
324+ {
325+ pPlotCurve->addXAxisValue (timeVals[i]);
326+ pPlotCurve->addYAxisValue (vals[i]);
327+ }
328+ pPlotCurve->setData (timeVals, vals, csvReader->numsteps );
329+ pPlotCurve->attach (mpPlot);
330+ mpPlot->replot ();
340331 }
341332 }
342-
343- // plot the curves
344- for (int i = 0 ; i < variablesToPlotIndex.size (); i++)
345- {
346- pPlotCurve[i]->setData (pPlotCurve[i]->getXAxisVector (), pPlotCurve[i]->getYAxisVector (),
347- pPlotCurve[i]->getSize ());
348- pPlotCurve[i]->attach (mpPlot);
349- mpPlot->replot ();
350- }
351-
352333 // if plottype is PLOT then check which requested variables are not found in the file
353334 if (getPlotType () == PlotWindow::PLOT)
354335 checkForErrors (mVariablesList , variablesPlotted);
355336 // close the file
356- mFile . close ( );
337+ omc_free_csv_reader (csvReader );
357338 }
358339 // PLOT MAT
359340 else if (mFile .fileName ().endsWith (" mat" ))
0 commit comments