From 6aa50304d9da85954fce693b9ae1615f6e00a741 Mon Sep 17 00:00:00 2001 From: Eduardo Behr Date: Wed, 25 May 2022 15:00:03 +0000 Subject: [PATCH] Eeschema: Fixing simulation plot CSV exporter --- eeschema/sim/sim_plot_frame.cpp | 45 +++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/eeschema/sim/sim_plot_frame.cpp b/eeschema/sim/sim_plot_frame.cpp index a157e5d72b..d85e203f7e 100644 --- a/eeschema/sim/sim_plot_frame.cpp +++ b/eeschema/sim/sim_plot_frame.cpp @@ -1217,28 +1217,41 @@ void SIM_PLOT_FRAME::menuSaveCsv( wxCommandEvent& event ) return; wxFFile out( saveDlg.GetPath(), "wb" ); - bool timeWritten = false; - for( const auto& t : GetCurrentPlot()->GetTraces() ) + std::map traces = GetCurrentPlot()->GetTraces(); + assert(traces); + + if( traces.size() == 0 ) + return; + + SIM_TYPE simType = m_circuitModel->GetSimType(); + + std::size_t rowCount = traces.begin()->second->GetDataX().size(); + + // write column header names on the first row + wxString xAxisName( m_simulator->GetXAxis( simType ) ); + out.Write( wxString::Format( "%s%c", xAxisName, SEPARATOR ) ); + + for( const auto& trace : traces ) { - const TRACE* trace = t.second; + wxString yAxisName = trace.first; + out.Write( wxString::Format( "%s%c", yAxisName, SEPARATOR ) ); + } - if( !timeWritten ) + out.Write( "\r\n" ); + + // write each row's numerical value + for ( std::size_t curRow=0; curRow < rowCount; curRow++ ) + { + double xAxisValue = traces.begin()->second->GetDataX().at( curRow ); + out.Write( wxString::Format( "%g%c", xAxisValue, SEPARATOR ) ); + + for( const auto& trace : traces ) { - out.Write( wxString::Format( "Time%c", SEPARATOR ) ); - - for( double v : trace->GetDataX() ) - out.Write( wxString::Format( "%g%c", v, SEPARATOR ) ); - - out.Write( "\r\n" ); - timeWritten = true; + double yAxisValue = trace.second->GetDataY().at( curRow ); + out.Write( wxString::Format( "%g%c", yAxisValue, SEPARATOR ) ); } - out.Write( wxString::Format( "%s%c", t.first, SEPARATOR ) ); - - for( double v : trace->GetDataY() ) - out.Write( wxString::Format( "%g%c", v, SEPARATOR ) ); - out.Write( "\r\n" ); }