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->SetCoordX( x );
48
49 aTrace->SetCursor( aCursorId, cursor );
50 aPlotTab->GetPlotWin()->AddLayer( cursor );
51 };
52
53 wxArrayString items = wxSplit( aParams, '|' );
54
55 for( const wxString& item : items )
56 {
57 if( item.StartsWith( wxS( "rgb" ) ) )
58 {
59 wxColour color;
60 color.Set( item );
61 aTrace->SetTraceColour( color );
62 aPlotTab->UpdateTraceStyle( aTrace );
63 }
64 else if( item.StartsWith( wxS( "cursor1" ) ) )
65 {
66 wxArrayString parts = wxSplit( item, ':' );
67 double val;
68
69 if( parts.size() == 3 )
70 {
71 parts[0].AfterFirst( '=' ).ToDouble( &val );
72 m_cursorFormats[0][0].FromString( parts[1] );
73 m_cursorFormats[0][1].FromString( parts[2] );
74 addCursor( 1, val );
75 }
76 }
77 else if( item.StartsWith( wxS( "cursor2" ) ) )
78 {
79 wxArrayString parts = wxSplit( item, ':' );
80 double val;
81
82 if( parts.size() == 3 )
83 {
84 parts[0].AfterFirst( '=' ).ToDouble( &val );
85 m_cursorFormats[1][0].FromString( parts[1] );
86 m_cursorFormats[1][1].FromString( parts[2] );
87 addCursor( 2, val );
88 }
89 }
90 else if( item.StartsWith( wxS( "cursorD" ) ) )
91 {
92 wxArrayString parts = wxSplit( item, ':' );
93
94 if( parts.size() == 3 )
95 {
96 m_cursorFormats[2][0].FromString( parts[1] );
97 m_cursorFormats[2][1].FromString( parts[2] );
98 }
99 }
100 else if( item == wxS( "dottedSecondary" ) )
101 {
102 aPlotTab->SetDottedSecondary( true );
103 }
104 else if( item == wxS( "hideGrid" ) )
105 {
106 aPlotTab->ShowGrid( false );
107 }
108 }
109}
110
111
112bool SIMULATOR_FRAME_UI::loadLegacyWorkbook( const wxString& aPath )
113{
114 wxTextFile file( aPath );
115
116#define EXPECTING( msg ) \
117 DisplayErrorMessage( this, wxString::Format( _( "Error loading workbook: line %d: %s." ), \
118 file.GetCurrentLine()+1, \
119 msg ) )
120
121 if( !file.Open() )
122 return false;
123
124 long version = 1;
125 wxString firstLine = file.GetFirstLine();
126 wxString pageCountLine;
127
128 if( firstLine.StartsWith( wxT( "version " ) ) )
129 {
130 if( !firstLine.substr( 8 ).ToLong( &version ) )
131 {
132 EXPECTING( _( "expecting version" ) );
133 file.Close();
134
135 return false;
136 }
137
138 pageCountLine = file.GetNextLine();
139 }
140 else
141 {
142 pageCountLine = firstLine;
143 }
144
145 long tabCount;
146
147 if( !pageCountLine.ToLong( &tabCount ) )
148 {
149 EXPECTING( _( "expecting simulation tab count" ) );
150 file.Close();
151
152 return false;
153 }
154
155 std::map<SIM_PLOT_TAB*, std::vector<std::tuple<long, wxString, wxString>>> traceInfo;
156
157 for( long i = 0; i < tabCount; ++i )
158 {
159 long simType, tracesCount;
160
161 if( !file.GetNextLine().ToLong( &simType ) )
162 {
163 EXPECTING( _( "expecting simulation tab type" ) );
164 file.Close();
165
166 return false;
167 }
168
169 wxString command = UnescapeString( file.GetNextLine() );
170 wxString simCommand;
172 wxStringTokenizer tokenizer( command, wxT( "\r\n" ), wxTOKEN_STRTOK );
173
174 if( version >= 2 )
175 {
176 simOptions &= ~NETLIST_EXPORTER_SPICE::OPTION_ADJUST_INCLUDE_PATHS;
177 simOptions &= ~NETLIST_EXPORTER_SPICE::OPTION_SAVE_ALL_VOLTAGES;
178 simOptions &= ~NETLIST_EXPORTER_SPICE::OPTION_SAVE_ALL_CURRENTS;
179 }
180
181 if( version >= 3 )
182 {
183 simOptions &= ~NETLIST_EXPORTER_SPICE::OPTION_SAVE_ALL_DISSIPATIONS;
184 }
185
186 while( tokenizer.HasMoreTokens() )
187 {
188 wxString line = tokenizer.GetNextToken();
189
190 if( line.StartsWith( wxT( ".kicad adjustpaths" ) ) )
192 else if( line.StartsWith( wxT( ".save all" ) ) )
194 else if( line.StartsWith( wxT( ".probe alli" ) ) )
196 else if( line.StartsWith( wxT( ".probe allp" ) ) )
198 else
199 simCommand += line + wxT( "\n" );
200 }
201
202 SIM_TAB* simTab = NewSimTab( simCommand );
203 SIM_PLOT_TAB* plotTab = dynamic_cast<SIM_PLOT_TAB*>( simTab );
204
205 simTab->SetSimOptions( simOptions );
206
207 if( !file.GetNextLine().ToLong( &tracesCount ) )
208 {
209 EXPECTING( _( "expecting trace count" ) );
210 file.Close();
211
212 return false;
213 }
214
215 if( plotTab )
216 traceInfo[plotTab] = {};
217
218 for( long j = 0; j < tracesCount; ++j )
219 {
220 long traceType;
221 wxString name, param;
222
223 if( !file.GetNextLine().ToLong( &traceType ) )
224 {
225 EXPECTING( _( "expecting trace type" ) );
226 file.Close();
227
228 return false;
229 }
230
231 name = file.GetNextLine();
232
233 if( name.IsEmpty() )
234 {
235 EXPECTING( _( "expecting trace name" ) );
236 file.Close();
237
238 return false;
239 }
240
241 param = file.GetNextLine();
242
243 if( param.IsEmpty() )
244 {
245 EXPECTING( _( "expecting trace color" ) );
246 file.Close();
247
248 return false;
249 }
250
251 if( plotTab )
252 traceInfo[plotTab].emplace_back( std::make_tuple( traceType, name, param ) );
253 }
254
255 if( version > 4 )
256 {
257 long measurementCount;
258
259 if( !file.GetNextLine().ToLong( &measurementCount ) )
260 {
261 EXPECTING( _( "expecting measurement count" ) );
262 file.Close();
263
264 return false;
265 }
266
267 for( int ii = 0; ii < (int) measurementCount; ++ ii )
268 {
269 wxString measurement = file.GetNextLine();
270
271 if( measurement.IsEmpty() )
272 {
273 EXPECTING( _( "expecting measurement definition" ) );
274 file.Close();
275
276 return false;
277 }
278
279 wxString format = file.GetNextLine();
280
281 if( format.IsEmpty() )
282 {
283 EXPECTING( _( "expecting measurement format definition" ) );
284 file.Close();
285
286 return false;
287 }
288
289 if( plotTab )
290 plotTab->Measurements().emplace_back( measurement, format );
291 }
292 }
293 }
294
295 long userDefinedSignalCount;
296
297 if( file.GetNextLine().ToLong( &userDefinedSignalCount ) )
298 {
299 for( int ii = 0; ii < (int) userDefinedSignalCount; ++ii )
300 m_userDefinedSignals[ ii ] = file.GetNextLine();
301 }
302
303 for( const auto& [ plotTab, traceInfoVector ] : traceInfo )
304 {
305 for( const auto& [ traceType, signalName, param ] : traceInfoVector )
306 {
307 if( traceType == SPT_UNKNOWN && signalName == wxS( "$LEGEND" ) )
308 {
309 wxArrayString coords = wxSplit( param, ' ' );
310
311 if( coords.size() >= 2 )
312 {
313 long x = 0;
314 long y = 0;
315
316 coords[0].ToLong( &x );
317 coords[1].ToLong( &y );
318 plotTab->SetLegendPosition( wxPoint( (int) x, (int) y ) );
319 }
320
321 plotTab->ShowLegend( true );
322 }
323 else
324 {
325 wxString vectorName = vectorNameFromSignalName( plotTab, signalName, nullptr );
326 TRACE* trace = plotTab->GetOrAddTrace( vectorName, (int) traceType );
327
328 if( version >= 4 && trace )
329 parseTraceParams( plotTab, trace, signalName, param );
330 }
331 }
332
333 plotTab->UpdatePlotColors();
334 }
335
336 if( SIM_TAB* simTab = GetCurrentSimTab() )
337 {
338 m_simulatorFrame->LoadSimulator( simTab->GetSimCommand(), simTab->GetSimOptions() );
339
340 if( version >= 5 )
341 {
342 simTab = dynamic_cast<SIM_TAB*>( m_plotNotebook->GetPage( 0 ) );
343
344 if( simTab )
345 simTab->SetLastSchTextSimCommand( file.GetNextLine() );
346 }
347 }
348
349 file.Close();
350 return true;
351}
352
353
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
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:41