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