KiCad PCB EDA Suite
Loading...
Searching...
No Matches
legacy_workbook.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2016-2023 CERN
5 * Copyright (C) 2016-2023 KiCad Developers, see AUTHORS.txt for contributors.
6 * @author Tomasz Wlostowski <[email protected]>
7 * @author Maciej Suminski <[email protected]>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 3
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, you may find one here:
21 * https://www.gnu.org/licenses/gpl-3.0.html
22 * or you may search the http://www.gnu.org website for the version 3 license,
23 * or you may write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25 */
26
27#include <memory>
28#include <wx/debug.h>
29
31#include <confirm.h>
32#include <string_utils.h>
34#include <sim/simulator_frame.h>
35#include <sim/sim_plot_tab.h>
36
37
39 const wxString& aSignalName, const wxString& aParams )
40{
41 auto addCursor =
42 [&]( int aCursorId, double x )
43 {
44 CURSOR* cursor = new CURSOR( aTrace, aPlotTab );
45
46 cursor->SetName( aSignalName );
47 cursor->SetPen( wxPen( aTrace->GetTraceColour() ) );
48 cursor->SetCoordX( x );
49
50 aTrace->SetCursor( aCursorId, cursor );
51 aPlotTab->GetPlotWin()->AddLayer( cursor );
52 };
53
54 wxArrayString items = wxSplit( aParams, '|' );
55
56 for( const wxString& item : items )
57 {
58 if( item.StartsWith( wxS( "rgb" ) ) )
59 {
60 wxColour color;
61 color.Set( item );
62 aTrace->SetTraceColour( color );
63 aPlotTab->UpdateTraceStyle( aTrace );
64 }
65 else if( item.StartsWith( wxS( "cursor1" ) ) )
66 {
67 wxArrayString parts = wxSplit( item, ':' );
68 double val;
69
70 if( parts.size() == 3 )
71 {
72 parts[0].AfterFirst( '=' ).ToDouble( &val );
73 m_cursorFormats[0][0].FromString( parts[1] );
74 m_cursorFormats[0][1].FromString( parts[2] );
75 addCursor( 1, val );
76 }
77 }
78 else if( item.StartsWith( wxS( "cursor2" ) ) )
79 {
80 wxArrayString parts = wxSplit( item, ':' );
81 double val;
82
83 if( parts.size() == 3 )
84 {
85 parts[0].AfterFirst( '=' ).ToDouble( &val );
86 m_cursorFormats[1][0].FromString( parts[1] );
87 m_cursorFormats[1][1].FromString( parts[2] );
88 addCursor( 2, val );
89 }
90 }
91 else if( item.StartsWith( wxS( "cursorD" ) ) )
92 {
93 wxArrayString parts = wxSplit( item, ':' );
94
95 if( parts.size() == 3 )
96 {
97 m_cursorFormats[2][0].FromString( parts[1] );
98 m_cursorFormats[2][1].FromString( parts[2] );
99 }
100 }
101 else if( item == wxS( "dottedSecondary" ) )
102 {
103 aPlotTab->SetDottedSecondary( true );
104 }
105 else if( item == wxS( "hideGrid" ) )
106 {
107 aPlotTab->ShowGrid( false );
108 }
109 }
110}
111
112
113bool SIMULATOR_FRAME_UI::loadLegacyWorkbook( const wxString& aPath )
114{
115 wxTextFile file( aPath );
116
117#define EXPECTING( msg ) \
118 DisplayErrorMessage( this, wxString::Format( _( "Error loading workbook: line %d: %s." ), \
119 file.GetCurrentLine()+1, \
120 msg ) )
121
122 if( !file.Open() )
123 return false;
124
125 long version = 1;
126 wxString firstLine = file.GetFirstLine();
127 wxString pageCountLine;
128
129 if( firstLine.StartsWith( wxT( "version " ) ) )
130 {
131 if( !firstLine.substr( 8 ).ToLong( &version ) )
132 {
133 EXPECTING( _( "expecting version" ) );
134 file.Close();
135
136 return false;
137 }
138
139 pageCountLine = file.GetNextLine();
140 }
141 else
142 {
143 pageCountLine = firstLine;
144 }
145
146 long tabCount;
147
148 if( !pageCountLine.ToLong( &tabCount ) )
149 {
150 EXPECTING( _( "expecting simulation tab count" ) );
151 file.Close();
152
153 return false;
154 }
155
156 std::map<SIM_PLOT_TAB*, std::vector<std::tuple<long, wxString, wxString>>> traceInfo;
157
158 for( long i = 0; i < tabCount; ++i )
159 {
160 long simType, tracesCount;
161
162 if( !file.GetNextLine().ToLong( &simType ) )
163 {
164 EXPECTING( _( "expecting simulation tab type" ) );
165 file.Close();
166
167 return false;
168 }
169
170 wxString command = UnescapeString( file.GetNextLine() );
171 wxString simCommand;
173 wxStringTokenizer tokenizer( command, wxT( "\r\n" ), wxTOKEN_STRTOK );
174
175 if( version >= 2 )
176 {
177 simOptions &= ~NETLIST_EXPORTER_SPICE::OPTION_ADJUST_INCLUDE_PATHS;
178 simOptions &= ~NETLIST_EXPORTER_SPICE::OPTION_SAVE_ALL_VOLTAGES;
179 simOptions &= ~NETLIST_EXPORTER_SPICE::OPTION_SAVE_ALL_CURRENTS;
180 }
181
182 if( version >= 3 )
183 {
184 simOptions &= ~NETLIST_EXPORTER_SPICE::OPTION_SAVE_ALL_DISSIPATIONS;
185 }
186
187 while( tokenizer.HasMoreTokens() )
188 {
189 wxString line = tokenizer.GetNextToken();
190
191 if( line.StartsWith( wxT( ".kicad adjustpaths" ) ) )
193 else if( line.StartsWith( wxT( ".save all" ) ) )
195 else if( line.StartsWith( wxT( ".probe alli" ) ) )
197 else if( line.StartsWith( wxT( ".probe allp" ) ) )
199 else
200 simCommand += line + wxT( "\n" );
201 }
202
203 SIM_TAB* simTab = NewSimTab( simCommand );
204 SIM_PLOT_TAB* plotTab = dynamic_cast<SIM_PLOT_TAB*>( simTab );
205
206 simTab->SetSimOptions( simOptions );
207
208 if( !file.GetNextLine().ToLong( &tracesCount ) )
209 {
210 EXPECTING( _( "expecting trace count" ) );
211 file.Close();
212
213 return false;
214 }
215
216 if( plotTab )
217 traceInfo[plotTab] = {};
218
219 for( long j = 0; j < tracesCount; ++j )
220 {
221 long traceType;
222 wxString name, param;
223
224 if( !file.GetNextLine().ToLong( &traceType ) )
225 {
226 EXPECTING( _( "expecting trace type" ) );
227 file.Close();
228
229 return false;
230 }
231
232 name = file.GetNextLine();
233
234 if( name.IsEmpty() )
235 {
236 EXPECTING( _( "expecting trace name" ) );
237 file.Close();
238
239 return false;
240 }
241
242 param = file.GetNextLine();
243
244 if( param.IsEmpty() )
245 {
246 EXPECTING( _( "expecting trace color" ) );
247 file.Close();
248
249 return false;
250 }
251
252 if( plotTab )
253 traceInfo[plotTab].emplace_back( std::make_tuple( traceType, name, param ) );
254 }
255
256 if( version > 4 )
257 {
258 long measurementCount;
259
260 if( !file.GetNextLine().ToLong( &measurementCount ) )
261 {
262 EXPECTING( _( "expecting measurement count" ) );
263 file.Close();
264
265 return false;
266 }
267
268 for( int ii = 0; ii < (int) measurementCount; ++ ii )
269 {
270 wxString measurement = file.GetNextLine();
271
272 if( measurement.IsEmpty() )
273 {
274 EXPECTING( _( "expecting measurement definition" ) );
275 file.Close();
276
277 return false;
278 }
279
280 wxString format = file.GetNextLine();
281
282 if( format.IsEmpty() )
283 {
284 EXPECTING( _( "expecting measurement format definition" ) );
285 file.Close();
286
287 return false;
288 }
289
290 if( plotTab )
291 plotTab->Measurements().emplace_back( measurement, format );
292 }
293 }
294 }
295
296 long userDefinedSignalCount;
297
298 if( file.GetNextLine().ToLong( &userDefinedSignalCount ) )
299 {
300 for( int ii = 0; ii < (int) userDefinedSignalCount; ++ii )
301 m_userDefinedSignals[ ii ] = file.GetNextLine();
302 }
303
304 for( const auto& [ plotTab, traceInfoVector ] : traceInfo )
305 {
306 for( const auto& [ traceType, signalName, param ] : traceInfoVector )
307 {
308 if( traceType == SPT_UNKNOWN && signalName == wxS( "$LEGEND" ) )
309 {
310 wxArrayString coords = wxSplit( param, ' ' );
311
312 if( coords.size() >= 2 )
313 {
314 long x = 0;
315 long y = 0;
316
317 coords[0].ToLong( &x );
318 coords[1].ToLong( &y );
319 plotTab->SetLegendPosition( wxPoint( (int) x, (int) y ) );
320 }
321
322 plotTab->ShowLegend( true );
323 }
324 else
325 {
326 wxString vectorName = vectorNameFromSignalName( plotTab, signalName, nullptr );
327 TRACE* trace = plotTab->GetOrAddTrace( vectorName, (int) traceType );
328
329 if( version >= 4 && trace )
330 parseTraceParams( plotTab, trace, signalName, param );
331 }
332 }
333
334 plotTab->UpdatePlotColors();
335 }
336
337 if( SIM_TAB* simTab = GetCurrentSimTab() )
338 {
339 m_simulatorFrame->LoadSimulator( simTab->GetSimCommand(), simTab->GetSimOptions() );
340
341 if( version >= 5 )
342 {
343 simTab = dynamic_cast<SIM_TAB*>( m_plotNotebook->GetPage( 0 ) );
344
345 if( simTab )
346 simTab->SetLastSchTextSimCommand( file.GetNextLine() );
347 }
348 }
349
350 file.Close();
351 return true;
352}
353
354
int color
Definition: DXF_plotter.cpp:58
const char * name
Definition: DXF_plotter.cpp:57
The SIMULATOR_FRAME holds the main user-interface for running simulations.
Definition: sim_plot_tab.h:64
SIM_TAB * NewSimTab(const wxString &aSimCommand)
Create a new simulation tab for a given simulation type.
bool loadLegacyWorkbook(const wxString &aPath)
wxString vectorNameFromSignalName(SIM_PLOT_TAB *aPlotTab, const wxString &aSignalName, int *aTraceType)
Get the simulator output vector name for a given signal name and type.
SIM_TAB * GetCurrentSimTab() const
Return the currently opened plot panel (or NULL if there is none).
std::map< int, wxString > m_userDefinedSignals
SIMULATOR_FRAME * m_simulatorFrame
void parseTraceParams(SIM_PLOT_TAB *aPlotTab, TRACE *aTrace, const wxString &aSignalName, const wxString &aParams)
SPICE_VALUE_FORMAT m_cursorFormats[3][2]
bool LoadSimulator(const wxString &aSimCommand, unsigned aSimOptions)
Check and load the current netlist into the simulator.
mpWindow * GetPlotWin() const
Definition: sim_plot_tab.h:350
void ShowGrid(bool aEnable)
Definition: sim_plot_tab.h:268
std::vector< std::pair< wxString, wxString > > & Measurements()
Definition: sim_plot_tab.h:360
void UpdateTraceStyle(TRACE *trace)
Update plot colors.
void SetDottedSecondary(bool aEnable)
Draw secondary signal traces (current or phase) with dotted lines.
Definition: sim_plot_tab.h:319
void SetLastSchTextSimCommand(const wxString &aCmd)
Definition: sim_tab.h:59
void SetSimOptions(int aOptions)
Definition: sim_tab.h:56
void SetTraceColour(const wxColour &aColour)
Definition: sim_plot_tab.h:181
void SetCursor(int aCursorId, CURSOR *aCursor)
Definition: sim_plot_tab.h:175
wxColour GetTraceColour() const
Definition: sim_plot_tab.h:182
bool AddLayer(mpLayer *layer, bool refreshDisplay=true)
Add a plot layer to the canvas.
Definition: mathplot.cpp:1970
This file is part of the common library.
#define _(s)
#define EXPECTING(msg)
@ SPT_UNKNOWN
Definition: sim_types.h:67
wxString UnescapeString(const wxString &aSource)
void FromString(const wxString &aString)
Definition: spice_value.cpp:40