KiCad PCB EDA Suite
sim_plot_frame.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 CERN
5 * Copyright (C) 2016-2022 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 <wx/debug.h>
28#include <wx/stc/stc.h>
29
30// For some obscure reason, needed on msys2 with some wxWidgets versions (3.0) to avoid
31// undefined symbol at link stage (due to use of #include <pegtl.hpp>)
32// Should not create issues on other platforms
33#include <wx/menu.h>
34
36#include <sch_edit_frame.h>
37#include <eeschema_id.h>
38#include <kiway.h>
39#include <confirm.h>
40#include <bitmaps.h>
44#include <scintilla_tricks.h>
45#include "string_utils.h"
46#include <pgm_base.h>
47#include "ngspice.h"
48#include "sim_plot_frame.h"
49#include "sim_plot_panel.h"
50#include "spice_simulator.h"
51#include "spice_reporter.h"
52#include <menus_helpers.h>
53#include <tool/tool_manager.h>
54#include <tools/ee_actions.h>
55#include <eeschema_settings.h>
56#include <wx/ffile.h>
57#include <wx/filedlg.h>
58#include <wx_filename.h>
59
60
62{
63 int res = (int) aFirst | (int) aSecond;
64
65 return (SIM_PLOT_TYPE) res;
66}
67
68
70{
71public:
73 m_parent( aParent )
74 {
75 }
76
77 REPORTER& Report( const wxString& aText, SEVERITY aSeverity = RPT_SEVERITY_UNDEFINED ) override
78 {
79 wxCommandEvent* event = new wxCommandEvent( EVT_SIM_REPORT );
80 event->SetString( aText );
81 wxQueueEvent( m_parent, event );
82 return *this;
83 }
84
85 bool HasMessage() const override
86 {
87 return false; // Technically "indeterminate" rather than false.
88 }
89
90 void OnSimStateChange( SPICE_SIMULATOR* aObject, SIM_STATE aNewState ) override
91 {
92 wxCommandEvent* event = nullptr;
93
94 switch( aNewState )
95 {
96 case SIM_IDLE:
97 event = new wxCommandEvent( EVT_SIM_FINISHED );
98 break;
99
100 case SIM_RUNNING:
101 event = new wxCommandEvent( EVT_SIM_STARTED );
102 break;
103
104 default:
105 wxFAIL;
106 return;
107 }
108
109 wxQueueEvent( m_parent, event );
110 }
111
112private:
114};
115
116
117SIM_PLOT_FRAME::SIM_PLOT_FRAME( KIWAY* aKiway, wxWindow* aParent ) :
118 SIM_PLOT_FRAME_BASE( aParent ),
119 m_lastSimPlot( nullptr ),
120 m_plotNumber( 0 ),
121 m_simFinished( false )
122{
123 SetKiway( this, aKiway );
124 m_signalsIconColorList = nullptr;
125
127
128 if( m_schematicFrame == nullptr )
129 throw std::runtime_error( "There is no schematic window" );
130
131 // Give an icon
132 wxIcon icon;
133 icon.CopyFromBitmap( KiBitmap( BITMAPS::simulator ) );
134 SetIcon( icon );
135
137
138 if( !m_simulator )
139 {
140 throw std::runtime_error( "Could not create simulator instance" );
141 return;
142 }
143
144 // Get the previous size and position of windows:
145 LoadSettings( config() );
146
147 // Prepare the color list to plot traces
149
150 // Give icons to menuitems
152
153 m_simulator->Init();
154
155 m_reporter = new SIM_THREAD_REPORTER( this );
156 m_simulator->SetReporter( m_reporter );
157
158 // the settings dialog will be created later, on demand.
159 // if created in the ctor, for some obscure reason, there is an issue
160 // on Windows: when open it, the simulator frame is sent to the background.
161 // instead of being behind the dialog frame (as it does)
162 m_settingsDlg = nullptr;
163
165
166 Bind( EVT_SIM_UPDATE, &SIM_PLOT_FRAME::onSimUpdate, this );
167 Bind( EVT_SIM_REPORT, &SIM_PLOT_FRAME::onSimReport, this );
168 Bind( EVT_SIM_STARTED, &SIM_PLOT_FRAME::onSimStarted, this );
169 Bind( EVT_SIM_FINISHED, &SIM_PLOT_FRAME::onSimFinished, this );
170 Bind( EVT_SIM_CURSOR_UPDATE, &SIM_PLOT_FRAME::onCursorUpdate, this );
171
172 // Toolbar buttons
173 m_toolSimulate = m_toolBar->AddTool( ID_SIM_RUN, _( "Run/Stop Simulation" ),
174 KiBitmap( BITMAPS::sim_run ), _( "Run Simulation" ), wxITEM_NORMAL );
175 m_toolAddSignals = m_toolBar->AddTool( ID_SIM_ADD_SIGNALS, _( "Add Signals" ),
176 KiBitmap( BITMAPS::sim_add_signal ), _( "Add signals to plot" ), wxITEM_NORMAL );
177 m_toolProbe = m_toolBar->AddTool( ID_SIM_PROBE, _( "Probe" ),
178 KiBitmap( BITMAPS::sim_probe ), _( "Probe signals on the schematic" ), wxITEM_NORMAL );
179 m_toolTune = m_toolBar->AddTool( ID_SIM_TUNE, _( "Tune" ),
180 KiBitmap( BITMAPS::sim_tune ), _( "Tune component values" ), wxITEM_NORMAL );
181 m_toolSettings = m_toolBar->AddTool( wxID_ANY, _( "Sim Parameters" ),
182 KiBitmap( BITMAPS::config ), _( "Simulation parameters and settings" ), wxITEM_NORMAL );
183
184 // Start all toolbar buttons except settings as disabled
185 m_toolSimulate->Enable( false );
186 m_toolAddSignals->Enable( false );
187 m_toolProbe->Enable( false );
188 m_toolTune->Enable( false );
189 m_toolSettings->Enable( true );
190
191 Bind( wxEVT_UPDATE_UI, &SIM_PLOT_FRAME::menuSimulateUpdate, this, m_toolSimulate->GetId() );
192 Bind( wxEVT_UPDATE_UI, &SIM_PLOT_FRAME::menuAddSignalsUpdate, this,
193 m_toolAddSignals->GetId() );
194 Bind( wxEVT_UPDATE_UI, &SIM_PLOT_FRAME::menuProbeUpdate, this, m_toolProbe->GetId() );
195 Bind( wxEVT_UPDATE_UI, &SIM_PLOT_FRAME::menuTuneUpdate, this, m_toolTune->GetId() );
196
197 Bind( wxEVT_COMMAND_TOOL_CLICKED, &SIM_PLOT_FRAME::onSimulate, this, m_toolSimulate->GetId() );
198 Bind( wxEVT_COMMAND_TOOL_CLICKED, &SIM_PLOT_FRAME::onAddSignal, this,
199 m_toolAddSignals->GetId() );
200 Bind( wxEVT_COMMAND_TOOL_CLICKED, &SIM_PLOT_FRAME::onProbe, this, m_toolProbe->GetId() );
201 Bind( wxEVT_COMMAND_TOOL_CLICKED, &SIM_PLOT_FRAME::onTune, this, m_toolTune->GetId() );
202 Bind( wxEVT_COMMAND_TOOL_CLICKED, &SIM_PLOT_FRAME::onSettings, this, m_toolSettings->GetId() );
203
204 Bind( EVT_WORKBOOK_MODIFIED, &SIM_PLOT_FRAME::onWorkbookModified, this );
205 Bind( EVT_WORKBOOK_CLR_MODIFIED, &SIM_PLOT_FRAME::onWorkbookClrModified, this );
206
207 // Bind toolbar buttons event to existing menu event handlers, so they behave the same
208 Bind( wxEVT_COMMAND_MENU_SELECTED, &SIM_PLOT_FRAME::onSimulate, this,
209 m_runSimulation->GetId() );
210 Bind( wxEVT_COMMAND_MENU_SELECTED, &SIM_PLOT_FRAME::onAddSignal, this, m_addSignals->GetId() );
211 Bind( wxEVT_COMMAND_MENU_SELECTED, &SIM_PLOT_FRAME::onProbe, this, m_probeSignals->GetId() );
212 Bind( wxEVT_COMMAND_MENU_SELECTED, &SIM_PLOT_FRAME::onTune, this, m_tuneValue->GetId() );
213 Bind( wxEVT_COMMAND_MENU_SELECTED, &SIM_PLOT_FRAME::onShowNetlist, this,
214 m_showNetlist->GetId() );
215 Bind( wxEVT_COMMAND_MENU_SELECTED, &SIM_PLOT_FRAME::onSettings, this,
216 m_boardAdapter->GetId() );
217
218 m_toolBar->Realize();
219
220#ifndef wxHAS_NATIVE_TABART
221 // Non-native default tab art has ugly gradients we don't want
222 m_workbook->SetArtProvider( new wxAuiSimpleTabArt() );
223#endif
224
225 // Ensure new items are taken in account by sizers:
226 Layout();
227
228 // resize the subwindows size. At least on Windows, calling wxSafeYield before
229 // resizing the subwindows forces the wxSplitWindows size events automatically generated
230 // by wxWidgets to be executed before our resize code.
231 // Otherwise, the changes made by setSubWindowsSashSize are overwritten by one these
232 // events
233 wxSafeYield();
235
236 // Ensure the window is on top
237 Raise();
238
239 initWorkbook();
240 updateTitle();
241}
242
243
245{
246 NULL_REPORTER devnull;
247
248 m_simulator->Attach( nullptr, devnull );
249 m_simulator->SetReporter( nullptr );
250 delete m_reporter;
252
253 if( m_settingsDlg )
254 m_settingsDlg->Destroy();
255}
256
257
259{
260 EESCHEMA_SETTINGS* cfg = dynamic_cast<EESCHEMA_SETTINGS*>( aCfg );
261 wxASSERT( cfg );
262
263 if( cfg )
264 {
266
267 // Read subwindows sizes (should be > 0 )
273 }
274
276
277 NGSPICE* currentSim = dynamic_cast<NGSPICE*>( m_simulator.get() );
278
279 if( currentSim )
280 m_simulator->Settings() = project.m_SchematicSettings->m_NgspiceSimulatorSettings;
281}
282
283
285{
286 EESCHEMA_SETTINGS* cfg = dynamic_cast<EESCHEMA_SETTINGS*>( aCfg );
287 wxASSERT( cfg );
288
289 if( cfg )
290 {
292
293 cfg->m_Simulator.plot_panel_width = m_splitterLeftRight->GetSashPosition();
295 cfg->m_Simulator.signal_panel_height = m_splitterSignals->GetSashPosition();
296 cfg->m_Simulator.cursors_panel_height = m_splitterTuneValues->GetSashPosition();
298 }
299
300 if( !m_isNonUserClose ) // If we're exiting the project has already been released.
301 {
303
304 if( project.m_SchematicSettings )
305 project.m_SchematicSettings->m_NgspiceSimulatorSettings->SaveToFile();
306
307 if( m_schematicFrame )
309 }
310}
311
312
314{
315 EESCHEMA_SETTINGS* cfg = dynamic_cast<EESCHEMA_SETTINGS*>( aCfg );
316 wxASSERT( cfg );
317
318 return cfg ? &cfg->m_Simulator.window : nullptr;
319}
320
321
323{
324 // Removed for the time being. We cannot run the simulation on simulator launch, as it may
325 // take a lot of time, confusing the user.
326 // TODO: Change workbook loading routines so that they don't run the simulation until the user
327 // initiates it.
328
329 /*if( !m_simulator->Settings()->GetWorkbookFilename().IsEmpty() )
330 {
331 wxFileName filename = m_simulator->Settings()->GetWorkbookFilename();
332 filename.SetPath( Prj().GetProjectPath() );
333
334 if( !loadWorkbook( filename.GetFullPath() ) )
335 m_simulator->Settings()->SetWorkbookFilename( "" );
336 }*/
337}
338
339
341{
342 wxFileName filename = Prj().AbsolutePath( m_simulator->Settings()->GetWorkbookFilename() );
343
344 bool readOnly = false;
345 bool unsaved = false;
346
347 if( filename.IsOk() && filename.FileExists() )
348 readOnly = !filename.IsFileWritable();
349 else
350 unsaved = true;
351
352 wxString title;
353
354 if( m_workbook->IsModified() )
355 title = wxT( "*" ) + filename.GetName();
356 else
357 title = filename.GetName();
358
359 if( readOnly )
360 title += wxS( " " ) + _( "[Read Only]" );
361
362 if( unsaved )
363 title += wxS( " " ) + _( "[Unsaved]" );
364
365 title += wxT( " \u2014 " ) + _( "Spice Simulator" );
366
367 SetTitle( title );
368}
369
370
371// A small helper struct to handle bitmaps initialization in menus
373{
376};
377
378
380{
381 // Give icons to menuitems of the main menubar
382 BM_MENU_INIT_ITEM bm_list[]
383 {
384 // File menu:
385 { wxID_NEW, BITMAPS::simulator },
386 { wxID_OPEN, BITMAPS::directory_open },
387 { wxID_SAVE, BITMAPS::save },
390 { wxID_CLOSE, BITMAPS::exit },
391
392 // simulator menu:
399
400 // View menu
401 { wxID_ZOOM_IN, BITMAPS::zoom_in },
402 { wxID_ZOOM_OUT, BITMAPS::zoom_out },
403 { wxID_ZOOM_FIT, BITMAPS::zoom_fit_in_page },
408
409 { 0, BITMAPS::INVALID_BITMAP } // Sentinel
410 };
411
412 // wxMenuItems are already created and attached to the m_mainMenu wxMenuBar.
413 // A problem is the fact setting bitmaps in wxMenuItems after they are attached
414 // to a wxMenu do not work in all cases.
415 // So the trick is:
416 // Remove the wxMenuItem from its wxMenu
417 // Set the bitmap
418 // Insert the modified wxMenuItem to its previous place
419 for( int ii = 0; bm_list[ii].m_MenuId; ++ii )
420 {
421 wxMenuItem* item = m_mainMenu->FindItem( bm_list[ii].m_MenuId );
422
423 if( !item || ( bm_list[ii].m_Bitmap == BITMAPS::INVALID_BITMAP ) )
424 continue;
425
426 wxMenu* menu = item->GetMenu();
427
428 // Calculate the initial index of item inside the wxMenu parent.
429 wxMenuItemList& mlist = menu->GetMenuItems();
430 int mpos = mlist.IndexOf( item );
431
432 if( mpos >= 0 ) // Should be always the case
433 {
434 // Modify the bitmap
435 menu->Remove( item );
436 AddBitmapToMenuItem( item, KiBitmap( bm_list[ii].m_Bitmap ) );
437
438 // Insert item to its the initial index
439 menu->Insert( mpos, item );
440 }
441 }
442}
443
444
446{
449
452
455
458}
459
460
461void SIM_PLOT_FRAME::StartSimulation( const wxString& aSimCommand )
462{
463 wxCHECK_RET( m_circuitModel->CommandToSimType( getCurrentSimCommand() ) != ST_UNKNOWN,
464 wxT( "Unknown simulation type" ) );
465
466 if( !m_settingsDlg )
468
469 m_simConsole->Clear();
470
471 if( aSimCommand != wxEmptyString )
472 m_circuitModel->SetSimCommand( aSimCommand );
473 else if( m_circuitModel->GetSheetSimCommand() != getCurrentSimCommand() )
474 m_circuitModel->SetSimCommand( getCurrentSimCommand() );
475 else
476 m_circuitModel->SetSimCommand( wxEmptyString );
477
478 // Make .save all and .probe alli permanent for now.
482
483 wxString errors;
484 WX_STRING_REPORTER reporter( &errors );
485
486 if( !m_schematicFrame->ReadyToNetlist( _( "Simulator requires a fully annotated schematic." ) )
487 || !m_simulator->Attach( m_circuitModel, reporter ) )
488 {
489 DisplayErrorMessage( this, _( "Errors during netlist generation; simulation aborted.\n\n" )
490 + errors );
491 return;
492 }
493
494 std::unique_lock<std::mutex> simulatorLock( m_simulator->GetMutex(), std::try_to_lock );
495
496 if( simulatorLock.owns_lock() )
497 {
498 wxBusyCursor toggle;
499
500 updateTuners();
501 applyTuners();
502 // Prevents memory leak on succeding simulations by deleting old vectors
503 m_simulator->Clean();
504 m_simulator->Run();
505 }
506 else
507 {
508 DisplayErrorMessage( this, _( "Another simulation is already running." ) );
509 }
510}
511
512
514{
515 SIM_PANEL_BASE* plotPanel = nullptr;
516 SIM_TYPE simType = NGSPICE_CIRCUIT_MODEL::CommandToSimType( aSimCommand );
517
518 if( SIM_PANEL_BASE::IsPlottable( simType ) )
519 {
520 SIM_PLOT_PANEL* panel;
521 panel = new SIM_PLOT_PANEL( aSimCommand, m_workbook, this, wxID_ANY );
522
524 Pgm().GetCommonSettings()->m_Input.scroll_modifier_zoom != 0 );
525
526 plotPanel = dynamic_cast<SIM_PANEL_BASE*>( panel );
527 }
528 else
529 {
530 SIM_NOPLOT_PANEL* panel;
531 panel = new SIM_NOPLOT_PANEL( aSimCommand, m_workbook, wxID_ANY );
532 plotPanel = dynamic_cast<SIM_PANEL_BASE*>( panel );
533 }
534
535 wxString pageTitle( m_simulator->TypeToName( simType, true ) );
536 pageTitle.Prepend( wxString::Format( _( "Plot%u - " ), (unsigned int) ++m_plotNumber ) );
537
538 m_workbook->AddPage( dynamic_cast<wxWindow*>( plotPanel ), pageTitle, true );
539
540 return plotPanel;
541}
542
543
544void SIM_PLOT_FRAME::AddVoltagePlot( const wxString& aNetName )
545{
546 addPlot( aNetName, SPT_VOLTAGE );
547}
548
549
550void SIM_PLOT_FRAME::AddCurrentPlot( const wxString& aDeviceName )
551{
552 addPlot( aDeviceName, SPT_CURRENT );
553}
554
555
557{
559
560 if( !plotPanel )
561 return;
562
563 wxString ref = aSymbol->GetField( REFERENCE_FIELD )->GetShownText();
564
565 // Do not add multiple instances for the same component.
566 auto tunerIt = std::find_if( m_tuners.begin(), m_tuners.end(),
567 [ref]( const TUNER_SLIDER* tuner )
568 {
569 return tuner->GetComponentName() == ref;
570 } );
571
572 if( tunerIt != m_tuners.end() )
573 return; // We already have it.
574
575 const SPICE_ITEM* item = GetExporter()->FindItem( std::string( ref.ToUTF8() ) );
576 const SIM_MODEL::PARAM* tunerParam = item->model->GetTunerParam();
577
578 // Do nothing if the symbol is not tunable.
579 if( !tunerParam )
580 return;
581
582 try
583 {
584 TUNER_SLIDER* tuner = new TUNER_SLIDER( this, m_tunePanel, aSymbol );
585 m_tuneSizer->Add( tuner );
586 m_tuners.push_back( tuner );
587 m_tunePanel->Layout();
588 }
589 catch( const KI_PARAM_ERROR& e )
590 {
591 DisplayErrorMessage( nullptr, e.What() );
592 }
593}
594
595void SIM_PLOT_FRAME::UpdateTunerValue( SCH_SYMBOL* aSymbol, const wxString& aValue )
596{
598 {
599 if( item == aSymbol )
600 {
601 SIM_LIB_MGR mgr( Prj() );
602 SIM_MODEL& model = mgr.CreateModel( *aSymbol ).model;
603
604 const SIM_MODEL::PARAM* tunerParam = model.GetTunerParam();
605
606 if( !tunerParam )
607 return;
608
609 model.SetParamValue( tunerParam->info.name, std::string( aValue.ToUTF8() ) );
610 model.WriteFields( aSymbol->GetFields() );
611
612 m_schematicFrame->UpdateItem( aSymbol, false, true );
614 }
615 }
616}
617
618
619void SIM_PLOT_FRAME::RemoveTuner( TUNER_SLIDER* aTuner, bool aErase )
620{
621 if( aErase )
622 m_tuners.remove( aTuner );
623
624 aTuner->Destroy();
625 m_tunePanel->Layout();
626}
627
628
630{
632
633 return ( ( !curPage || curPage->GetType() == ST_UNKNOWN ) ?
634 nullptr :
635 dynamic_cast<SIM_PLOT_PANEL*>( curPage ) );
636}
637
638
640{
641 return m_circuitModel.get();
642}
643
644
645std::shared_ptr<SPICE_SIMULATOR_SETTINGS>& SIM_PLOT_FRAME::GetSimulatorSettings()
646{
647 wxASSERT( m_simulator->Settings() );
648
649 return m_simulator->Settings();
650}
651
652
653void SIM_PLOT_FRAME::addPlot( const wxString& aName, SIM_PLOT_TYPE aType )
654{
655 SIM_TYPE simType = m_circuitModel->GetSimType();
656
657 if( simType == ST_UNKNOWN )
658 {
659 m_simConsole->AppendText( _( "Error: simulation type not defined!\n" ) );
660 m_simConsole->SetInsertionPointEnd();
661 return;
662 }
663 else if( !SIM_PANEL_BASE::IsPlottable( simType ) )
664 {
665 m_simConsole->AppendText( _( "Error: simulation type doesn't support plotting!\n" ) );
666 m_simConsole->SetInsertionPointEnd();
667 return;
668 }
669
670 // Create a new plot if the current one displays a different type
671 SIM_PLOT_PANEL* plotPanel = GetCurrentPlot();
672
673 if( !plotPanel || plotPanel->GetType() != simType )
674 {
675 plotPanel =
676 dynamic_cast<SIM_PLOT_PANEL*>( NewPlotPanel( m_circuitModel->GetSimCommand() ) );
677 }
678
679 wxASSERT( plotPanel );
680
681 if( !plotPanel ) // Something is wrong
682 return;
683
684 bool updated = false;
685 SIM_PLOT_TYPE xAxisType = getXAxisType( simType );
686
687 if( xAxisType == SPT_LIN_FREQUENCY || xAxisType == SPT_LOG_FREQUENCY )
688 {
689 int baseType = aType & ~( SPT_AC_MAG | SPT_AC_PHASE );
690
691 // Add two plots: magnitude & phase
692 updated |= updatePlot( aName, ( SIM_PLOT_TYPE )( baseType | SPT_AC_MAG ), plotPanel );
693 updated |= updatePlot( aName, ( SIM_PLOT_TYPE )( baseType | SPT_AC_PHASE ), plotPanel );
694 }
695 else
696 {
697 updated = updatePlot( aName, aType, plotPanel );
698 }
699
700 if( updated )
701 {
703 }
704}
705
706
707void SIM_PLOT_FRAME::removePlot( const wxString& aPlotName )
708{
709 SIM_PLOT_PANEL* plotPanel = GetCurrentPlot();
710
711 if( !plotPanel )
712 return;
713
714 wxASSERT( plotPanel->TraceShown( aPlotName ) );
715 m_workbook->DeleteTrace( plotPanel, aPlotName );
716 plotPanel->GetPlotWin()->Fit();
717
719 wxCommandEvent dummy;
721}
722
723
724bool SIM_PLOT_FRAME::updatePlot( const wxString& aName, SIM_PLOT_TYPE aType,
725 SIM_PLOT_PANEL* aPlotPanel )
726{
727 SIM_TYPE simType = m_circuitModel->GetSimType();
728
729 wxString plotTitle = aName;
730
731 if( aType & SPT_AC_MAG )
732 plotTitle += _( " (mag)" );
733 else if( aType & SPT_AC_PHASE )
734 plotTitle += _( " (phase)" );
735
736 if( !SIM_PANEL_BASE::IsPlottable( simType ) )
737 {
738 // There is no plot to be shown
739 m_simulator->Command( wxString::Format( wxT( "print %s" ), aName ).ToStdString() );
740
741 return false;
742 }
743
744 // First, handle the x axis
745 wxString xAxisName( m_simulator->GetXAxis( simType ) );
746
747 if( xAxisName.IsEmpty() )
748 return false;
749
750 auto data_x = m_simulator->GetMagPlot( (const char*) xAxisName.c_str() );
751 unsigned int size = data_x.size();
752
753 if( data_x.empty() )
754 return false;
755
756 std::vector<double> data_y;
757
758 // Now, Y axis data
759 switch( m_circuitModel->GetSimType() )
760 {
761 case ST_AC:
762 wxASSERT_MSG( !( ( aType & SPT_AC_MAG ) && ( aType & SPT_AC_PHASE ) ),
763 wxT( "Cannot set both AC_PHASE and AC_MAG bits" ) );
764
765 if( aType & SPT_AC_MAG )
766 data_y = m_simulator->GetMagPlot( (const char*) aName.c_str() );
767 else if( aType & SPT_AC_PHASE )
768 data_y = m_simulator->GetPhasePlot( (const char*) aName.c_str() );
769 else
770 wxASSERT_MSG( false, wxT( "Plot type missing AC_PHASE or AC_MAG bit" ) );
771
772 break;
773
774 case ST_NOISE:
775 case ST_DC:
776 case ST_TRANSIENT:
777 data_y = m_simulator->GetMagPlot( (const char*) aName.c_str() );
778 break;
779
780 default:
781 wxASSERT_MSG( false, wxT( "Unhandled plot type" ) );
782 return false;
783 }
784
785 if( data_y.size() != size )
786 return false;
787
788 // If we did a two-source DC analysis, we need to split the resulting vector and add traces
789 // for each input step
790 SPICE_DC_PARAMS source1, source2;
791
792 if( m_circuitModel->GetSimType() == ST_DC
793 && m_circuitModel->ParseDCCommand( m_circuitModel->GetSimCommand(), &source1, &source2 ) )
794 {
795 if( !source2.m_source.IsEmpty() )
796 {
797 // Source 1 is the inner loop, so lets add traces for each Source 2 (outer loop) step
798 SPICE_VALUE v = source2.m_vstart;
799 wxString name;
800
801 size_t offset = 0;
802 size_t outer = ( size_t )( ( source2.m_vend - v ) / source2.m_vincrement ).ToDouble();
803 size_t inner = data_x.size() / ( outer + 1 );
804
805 wxASSERT( data_x.size() % ( outer + 1 ) == 0 );
806
807 for( size_t idx = 0; idx <= outer; idx++ )
808 {
809 name = wxString::Format( wxT( "%s (%s = %s V)" ),
810 plotTitle,
811 source2.m_source,
812 v.ToString() );
813
814 std::vector<double> sub_x( data_x.begin() + offset,
815 data_x.begin() + offset + inner );
816 std::vector<double> sub_y( data_y.begin() + offset,
817 data_y.begin() + offset + inner );
818
819 m_workbook->AddTrace( aPlotPanel, name, aName, inner, sub_x.data(), sub_y.data(),
820 aType );
821
822 v = v + source2.m_vincrement;
823 offset += inner;
824 }
825
826 return true;
827 }
828 }
829
830 m_workbook->AddTrace( aPlotPanel, plotTitle, aName, size, data_x.data(), data_y.data(), aType );
831
832 return true;
833}
834
835
837{
838 m_signals->ClearAll();
839
840 SIM_PLOT_PANEL* plotPanel = GetCurrentPlot();
841
842 if( !plotPanel )
843 return;
844
845 wxSize size = m_signals->GetClientSize();
846 m_signals->AppendColumn( _( "Signal" ), wxLIST_FORMAT_LEFT, size.x );
847
848 // Build an image list, to show the color of the corresponding trace
849 // in the plot panel
850 // This image list is used for trace and cursor lists
851 wxMemoryDC bmDC;
852 const int isize = bmDC.GetCharHeight();
853
854 if( m_signalsIconColorList == nullptr )
855 m_signalsIconColorList = new wxImageList( isize, isize, false );
856 else
857 m_signalsIconColorList->RemoveAll();
858
859 for( const auto& trace : GetCurrentPlot()->GetTraces() )
860 {
861 wxBitmap bitmap( isize, isize );
862 bmDC.SelectObject( bitmap );
863 wxColour tcolor = trace.second->GetPen().GetColour();
864
865 wxColour bgColor = m_signals->wxWindow::GetBackgroundColour();
866 bmDC.SetPen( wxPen( bgColor ) );
867 bmDC.SetBrush( wxBrush( bgColor ) );
868 bmDC.DrawRectangle( 0, 0, isize, isize ); // because bmDC.Clear() does not work in wxGTK
869
870 bmDC.SetPen( wxPen( tcolor ) );
871 bmDC.SetBrush( wxBrush( tcolor ) );
872 bmDC.DrawRectangle( 0, isize / 4 + 1, isize, isize / 2 );
873
874 bmDC.SelectObject( wxNullBitmap ); // Needed to initialize bitmap
875
876 bitmap.SetMask( new wxMask( bitmap, *wxBLACK ) );
877 m_signalsIconColorList->Add( bitmap );
878 }
879
880 if( bmDC.IsOk() )
881 {
882 bmDC.SetBrush( wxNullBrush );
883 bmDC.SetPen( wxNullPen );
884 }
885
886 m_signals->SetImageList( m_signalsIconColorList, wxIMAGE_LIST_SMALL );
887
888 // Fill the signals listctrl. Keep the order of names and
889 // the order of icon color identical, because the icons
890 // are also used in cursor list, and the color index is
891 // calculated from the trace name index
892 int imgidx = 0;
893
894 for( const auto& trace : plotPanel->GetTraces() )
895 {
896 m_signals->InsertItem( imgidx, trace.first, imgidx );
897 imgidx++;
898 }
899}
900
901
903{
904 const auto& spiceItems = m_circuitModel->GetItems();
905
906 for( auto it = m_tuners.begin(); it != m_tuners.end(); /* iteration inside the loop */ )
907 {
908 const wxString& ref = (*it)->GetComponentName();
909
910 if( std::find_if( spiceItems.begin(), spiceItems.end(),
911 [&]( const SPICE_ITEM& item )
912 {
913 return item.refName == ref;
914 }) == spiceItems.end() )
915 {
916 // The component does not exist anymore, remove the associated tuner
917 TUNER_SLIDER* tuner = *it;
918 it = m_tuners.erase( it );
919 RemoveTuner( tuner, false );
920 }
921 else
922 {
923 ++it;
924 }
925 }
926}
927
928
930{
931 for( auto& tuner : m_tuners )
932 m_simulator->Command( tuner->GetTunerCommand() );
933}
934
935
936bool SIM_PLOT_FRAME::loadWorkbook( const wxString& aPath )
937{
939
940 wxTextFile file( aPath );
941
942#define DISPLAY_LOAD_ERROR( fmt ) DisplayErrorMessage( this, wxString::Format( _( fmt ), \
943 file.GetCurrentLine()+1 ) )
944
945 if( !file.Open() )
946 return false;
947
948 long plotsCount;
949
950 if( !file.GetFirstLine().ToLong( &plotsCount ) ) // GetFirstLine instead of GetNextLine
951 {
952 DISPLAY_LOAD_ERROR( "Error loading workbook: Line %d is not an integer." );
953 file.Close();
954
955 return false;
956 }
957
958 for( long i = 0; i < plotsCount; ++i )
959 {
960 long plotType, tracesCount;
961
962 if( !file.GetNextLine().ToLong( &plotType ) )
963 {
964 DISPLAY_LOAD_ERROR( "Error loading workbook: Line %d is not an integer." );
965 file.Close();
966
967 return false;
968 }
969
970 wxString simCommand = UnescapeString( file.GetNextLine() );
971 NewPlotPanel( simCommand );
972 StartSimulation( simCommand );
973
974 // Perform simulation, so plots can be added with values
975 do
976 {
977 wxThread::This()->Sleep( 50 );
978 }
979 while( m_simulator->IsRunning() );
980
981 if( !file.GetNextLine().ToLong( &tracesCount ) )
982 {
983 DISPLAY_LOAD_ERROR( "Error loading workbook: Line %d is not an integer." );
984 file.Close();
985
986 return false;
987 }
988
989 for( long j = 0; j < tracesCount; ++j )
990 {
991 long traceType;
992 wxString name, param;
993
994 if( !file.GetNextLine().ToLong( &traceType ) )
995 {
996 DISPLAY_LOAD_ERROR( "Error loading workbook: Line %d is not an integer." );
997 file.Close();
998
999 return false;
1000 }
1001
1002 name = file.GetNextLine();
1003
1004 if( name.IsEmpty() )
1005 {
1006 DISPLAY_LOAD_ERROR( "Error loading workbook: Line %d is empty." );
1007 file.Close();
1008
1009 return false;
1010 }
1011
1012 param = file.GetNextLine();
1013
1014 #if 0 // no longer in use
1015 if( param.IsEmpty() )
1016 {
1017 DISPLAY_LOAD_ERROR( "Error loading workbook: Line %d is empty." );
1018 file.Close();
1019
1020 return false;
1021 }
1022 #endif
1023
1024 addPlot( name, (SIM_PLOT_TYPE) traceType );
1025 }
1026 }
1027
1028 file.Close();
1029
1030 wxFileName filename( aPath );
1031 filename.MakeRelativeTo( Prj().GetProjectPath() );
1032
1033 // Remember the loaded workbook filename.
1034 m_simulator->Settings()->SetWorkbookFilename( filename.GetFullPath() );
1035
1036 // Successfully loading a workbook does not count as modifying it.
1038 return true;
1039}
1040
1041
1042bool SIM_PLOT_FRAME::saveWorkbook( const wxString& aPath )
1043{
1044 wxFileName filename = aPath;
1045 filename.SetExt( WorkbookFileExtension );
1046
1047 wxTextFile file( filename.GetFullPath() );
1048
1049 if( file.Exists() )
1050 {
1051 if( !file.Open() )
1052 return false;
1053
1054 file.Clear();
1055 }
1056 else
1057 {
1058 file.Create();
1059 }
1060
1061 file.AddLine( wxString::Format( wxT( "%llu" ), m_workbook->GetPageCount() ) );
1062
1063 for( size_t i = 0; i < m_workbook->GetPageCount(); i++ )
1064 {
1065 const SIM_PANEL_BASE* basePanel = dynamic_cast<const SIM_PANEL_BASE*>( m_workbook->GetPage( i ) );
1066
1067 if( !basePanel )
1068 {
1069 file.AddLine( wxString::Format( wxT( "%llu" ), 0ull ) );
1070 continue;
1071 }
1072
1073 file.AddLine( wxString::Format( wxT( "%d" ), basePanel->GetType() ) );
1074 file.AddLine( EscapeString( m_workbook->GetSimCommand( basePanel ), CTX_LINE ) );
1075
1076 const SIM_PLOT_PANEL* plotPanel = dynamic_cast<const SIM_PLOT_PANEL*>( basePanel );
1077
1078 if( !plotPanel )
1079 {
1080 file.AddLine( wxString::Format( wxT( "%llu" ), 0ull ) );
1081 continue;
1082 }
1083
1084 file.AddLine( wxString::Format( wxT( "%llu" ), plotPanel->GetTraces().size() ) );
1085
1086 for( const auto& trace : plotPanel->GetTraces() )
1087 {
1088 file.AddLine( wxString::Format( wxT( "%d" ), trace.second->GetType() ) );
1089 file.AddLine( trace.second->GetName() );
1090 file.AddLine( trace.second->GetParam() );
1091 }
1092 }
1093
1094 bool res = file.Write();
1095 file.Close();
1096
1097 // Store the filename of the last saved workbook.
1098 if( res )
1099 {
1100 filename.MakeRelativeTo( Prj().GetProjectPath() );
1101 m_simulator->Settings()->SetWorkbookFilename( filename.GetFullPath() );
1102 }
1103
1105 return res;
1106}
1107
1108
1110{
1111 wxFileName filename = m_simulator->Settings()->GetWorkbookFilename();
1112
1113 if( filename.GetName().IsEmpty() )
1114 {
1115 if( Prj().GetProjectName().IsEmpty() )
1116 {
1117 filename.SetName( _( "noname" ) );
1118 filename.SetExt( WorkbookFileExtension );
1119 }
1120 else
1121 {
1122 filename.SetName( Prj().GetProjectName() );
1123 filename.SetExt( WorkbookFileExtension );
1124 }
1125 }
1126
1127 return filename.GetFullName();
1128}
1129
1130
1132{
1133 wxFileName path = m_simulator->Settings()->GetWorkbookFilename();
1134
1135 path.Normalize( FN_NORMALIZE_FLAGS | wxPATH_NORM_ENV_VARS, Prj().GetProjectPath() );
1136 return path.GetPath();
1137}
1138
1139
1141{
1142 switch( aType )
1143 {
1145 case ST_AC: return SPT_LIN_FREQUENCY;
1146 case ST_DC: return SPT_SWEEP;
1147 case ST_TRANSIENT: return SPT_TIME;
1148 default:
1149 wxASSERT_MSG( false, wxT( "Unhandled simulation type" ) );
1150 return (SIM_PLOT_TYPE) 0;
1151 }
1152}
1153
1154
1155void SIM_PLOT_FRAME::menuNewPlot( wxCommandEvent& aEvent )
1156{
1157 SIM_TYPE type = m_circuitModel->GetSimType();
1158
1159 if( SIM_PANEL_BASE::IsPlottable( type ) )
1160 NewPlotPanel( m_circuitModel->GetSimCommand() );
1161}
1162
1163
1164void SIM_PLOT_FRAME::menuOpenWorkbook( wxCommandEvent& event )
1165{
1166 wxFileDialog openDlg( this, _( "Open simulation workbook" ), getDefaultPath(), "",
1167 WorkbookFileWildcard(), wxFD_OPEN | wxFD_FILE_MUST_EXIST );
1168
1169 if( openDlg.ShowModal() == wxID_CANCEL )
1170 return;
1171
1172 loadWorkbook( openDlg.GetPath() );
1173}
1174
1175
1176void SIM_PLOT_FRAME::menuSaveWorkbook( wxCommandEvent& event )
1177{
1178 if( !m_workbook->IsModified() )
1179 return;
1180
1181 wxString filename = m_simulator->Settings()->GetWorkbookFilename();
1182
1183 if( filename.IsEmpty() )
1184 {
1185 menuSaveWorkbookAs( event );
1186 return;
1187 }
1188
1189 saveWorkbook( Prj().AbsolutePath( m_simulator->Settings()->GetWorkbookFilename() ) );
1190}
1191
1192
1193void SIM_PLOT_FRAME::menuSaveWorkbookAs( wxCommandEvent& event )
1194{
1195 wxFileDialog saveAsDlg( this, _( "Save Simulation Workbook As" ), getDefaultPath(),
1197 wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
1198
1199 if( saveAsDlg.ShowModal() == wxID_CANCEL )
1200 return;
1201
1202 saveWorkbook( Prj().AbsolutePath( saveAsDlg.GetPath() ) );
1203}
1204
1205
1206void SIM_PLOT_FRAME::menuSaveImage( wxCommandEvent& event )
1207{
1208 if( !GetCurrentPlot() )
1209 return;
1210
1211 wxFileDialog saveDlg( this, _( "Save Plot as Image" ), "", "", PngFileWildcard(),
1212 wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
1213
1214 if( saveDlg.ShowModal() == wxID_CANCEL )
1215 return;
1216
1217 GetCurrentPlot()->GetPlotWin()->SaveScreenshot( saveDlg.GetPath(), wxBITMAP_TYPE_PNG );
1218}
1219
1220
1221void SIM_PLOT_FRAME::menuSaveCsv( wxCommandEvent& event )
1222{
1223 if( !GetCurrentPlot() )
1224 return;
1225
1226 const wxChar SEPARATOR = ';';
1227
1228 wxFileDialog saveDlg( this, _( "Save Plot Data" ), "", "", CsvFileWildcard(),
1229 wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
1230
1231 if( saveDlg.ShowModal() == wxID_CANCEL )
1232 return;
1233
1234 wxFFile out( saveDlg.GetPath(), "wb" );
1235
1236 std::map<wxString, TRACE *> traces = GetCurrentPlot()->GetTraces();
1237
1238 if( traces.size() == 0 )
1239 return;
1240
1241 SIM_TYPE simType = m_circuitModel->GetSimType();
1242
1243 std::size_t rowCount = traces.begin()->second->GetDataX().size();
1244
1245 // write column header names on the first row
1246 wxString xAxisName( m_simulator->GetXAxis( simType ) );
1247 out.Write( wxString::Format( wxT( "%s%c" ), xAxisName, SEPARATOR ) );
1248
1249 for( const auto& trace : traces )
1250 {
1251 wxString yAxisName = trace.first;
1252 out.Write( wxString::Format( wxT( "%s%c" ), yAxisName, SEPARATOR ) );
1253 }
1254
1255 out.Write( wxS( "\r\n" ) );
1256
1257 // write each row's numerical value
1258 for ( std::size_t curRow=0; curRow < rowCount; curRow++ )
1259 {
1260 double xAxisValue = traces.begin()->second->GetDataX().at( curRow );
1261 out.Write( wxString::Format( wxT( "%g%c" ), xAxisValue, SEPARATOR ) );
1262
1263 for( const auto& trace : traces )
1264 {
1265 double yAxisValue = trace.second->GetDataY().at( curRow );
1266 out.Write( wxString::Format( wxT( "%g%c" ), yAxisValue, SEPARATOR ) );
1267 }
1268
1269 out.Write( wxS( "\r\n" ) );
1270 }
1271
1272 out.Close();
1273}
1274
1275
1276void SIM_PLOT_FRAME::menuZoomIn( wxCommandEvent& event )
1277{
1278 if( GetCurrentPlot() )
1280}
1281
1282
1283void SIM_PLOT_FRAME::menuZoomOut( wxCommandEvent& event )
1284{
1285 if( GetCurrentPlot() )
1287}
1288
1289
1290void SIM_PLOT_FRAME::menuZoomFit( wxCommandEvent& event )
1291{
1292 if( GetCurrentPlot() )
1294}
1295
1296
1297void SIM_PLOT_FRAME::menuShowGrid( wxCommandEvent& event )
1298{
1300
1301 if( plot )
1302 plot->ShowGrid( !plot->IsGridShown() );
1303}
1304
1305
1306void SIM_PLOT_FRAME::menuShowGridUpdate( wxUpdateUIEvent& event )
1307{
1309
1310 event.Check( plot ? plot->IsGridShown() : false );
1311}
1312
1313
1314void SIM_PLOT_FRAME::menuShowLegend( wxCommandEvent& event )
1315{
1317
1318 if( plot )
1319 plot->ShowLegend( !plot->IsLegendShown() );
1320}
1321
1322
1323void SIM_PLOT_FRAME::menuShowLegendUpdate( wxUpdateUIEvent& event )
1324{
1326 event.Check( plot ? plot->IsLegendShown() : false );
1327}
1328
1329
1330void SIM_PLOT_FRAME::menuShowDotted( wxCommandEvent& event )
1331{
1333
1334 if( plot )
1335 plot->SetDottedCurrentPhase( !plot->GetDottedCurrentPhase() );
1336}
1337
1338
1339void SIM_PLOT_FRAME::menuShowDottedUpdate( wxUpdateUIEvent& event )
1340{
1342
1343 event.Check( plot ? plot->GetDottedCurrentPhase() : false );
1344}
1345
1346
1347void SIM_PLOT_FRAME::menuWhiteBackground( wxCommandEvent& event )
1348{
1350
1351 // Rebuild the color list to plot traces
1353
1354 // Now send changes to all SIM_PLOT_PANEL
1355 for( size_t page = 0; page < m_workbook->GetPageCount(); page++ )
1356 {
1357 wxWindow* curPage = m_workbook->GetPage( page );
1358
1359 // ensure it is truly a plot panel and not the (zero plots) placeholder
1360 // which is only SIM_PLOT_PANEL_BASE
1361 SIM_PLOT_PANEL* panel = dynamic_cast<SIM_PLOT_PANEL*>( curPage );
1362
1363 if( panel )
1364 panel->UpdatePlotColors();
1365 }
1366}
1367
1368
1369void SIM_PLOT_FRAME::menuSimulateUpdate( wxUpdateUIEvent& event )
1370{
1371 event.Enable( m_circuitModel->CommandToSimType( getCurrentSimCommand() ) != ST_UNKNOWN );
1372}
1373
1374
1375void SIM_PLOT_FRAME::menuAddSignalsUpdate( wxUpdateUIEvent& event )
1376{
1377 event.Enable( m_simFinished );
1378}
1379
1380
1381void SIM_PLOT_FRAME::menuProbeUpdate( wxUpdateUIEvent& event )
1382{
1383 event.Enable( m_simFinished );
1384}
1385
1386
1387void SIM_PLOT_FRAME::menuTuneUpdate( wxUpdateUIEvent& event )
1388{
1389 event.Enable( m_simFinished );
1390}
1391
1392
1393void SIM_PLOT_FRAME::onPlotClose( wxAuiNotebookEvent& event )
1394{
1395}
1396
1397
1398void SIM_PLOT_FRAME::onPlotClosed( wxAuiNotebookEvent& event )
1399{
1400 if( m_workbook->GetPageCount() == 0 )
1401 {
1402 m_signals->ClearAll();
1403 m_cursors->ClearAll();
1404 }
1405 else
1406 {
1408 wxCommandEvent dummy;
1410 }
1411}
1412
1413
1414void SIM_PLOT_FRAME::onPlotChanged( wxAuiNotebookEvent& event )
1415{
1417 wxCommandEvent dummy;
1419}
1420
1421
1422void SIM_PLOT_FRAME::onPlotDragged( wxAuiNotebookEvent& event )
1423{
1424}
1425
1426
1427void SIM_PLOT_FRAME::onSignalDblClick( wxMouseEvent& event )
1428{
1429 // Remove signal from the plot panel when double clicked
1430 long idx = m_signals->GetFocusedItem();
1431
1432 if( idx != wxNOT_FOUND )
1433 removePlot( m_signals->GetItemText( idx, 0 ) );
1434}
1435
1436
1437void SIM_PLOT_FRAME::onSignalRClick( wxListEvent& event )
1438{
1439 int idx = event.GetIndex();
1440
1441 if( idx != wxNOT_FOUND )
1442 m_signals->Select( idx );
1443
1444 idx = m_signals->GetFirstSelected();
1445
1446 if( idx != wxNOT_FOUND )
1447 {
1448 const wxString& netName = m_signals->GetItemText( idx, 0 );
1449 SIGNAL_CONTEXT_MENU ctxMenu( netName, this );
1450 m_signals->PopupMenu( &ctxMenu );
1451 }
1452}
1453
1454
1455void SIM_PLOT_FRAME::onWorkbookModified( wxCommandEvent& event )
1456{
1457 updateTitle();
1458}
1459
1460
1461void SIM_PLOT_FRAME::onWorkbookClrModified( wxCommandEvent& event )
1462{
1463 updateTitle();
1464}
1465
1466
1467void SIM_PLOT_FRAME::onSimulate( wxCommandEvent& event )
1468{
1469 if( m_simulator->IsRunning() )
1470 m_simulator->Stop();
1471 else
1473}
1474
1475
1476void SIM_PLOT_FRAME::onSettings( wxCommandEvent& event )
1477{
1478 SIM_PANEL_BASE* plotPanelWindow = getCurrentPlotWindow();
1479
1480 if( !m_settingsDlg )
1481 m_settingsDlg = new DIALOG_SIM_SETTINGS( this, m_circuitModel, m_simulator->Settings() );
1482
1483 wxString errors;
1484 WX_STRING_REPORTER reporter( &errors );
1485
1486 if( !m_circuitModel->ReadSchematicAndLibraries( NETLIST_EXPORTER_SPICE::OPTION_DEFAULT_FLAGS,
1487 reporter ) )
1488 {
1489 DisplayErrorMessage( this, _( "Errors during netlist generation; simulation aborted.\n\n" )
1490 + errors );
1491 return;
1492 }
1493
1494 if( m_workbook->GetPageIndex( plotPanelWindow ) != wxNOT_FOUND )
1495 m_settingsDlg->SetSimCommand( m_workbook->GetSimCommand( plotPanelWindow ) );
1496
1497 if( m_settingsDlg->ShowModal() == wxID_OK )
1498 {
1499 wxString oldCommand;
1500
1501 if( m_workbook->GetPageIndex( plotPanelWindow ) != wxNOT_FOUND )
1502 oldCommand = m_workbook->GetSimCommand( plotPanelWindow );
1503 else
1504 oldCommand = wxString();
1505
1506 wxString newCommand = m_settingsDlg->GetSimCommand();
1507 SIM_TYPE newSimType = NGSPICE_CIRCUIT_MODEL::CommandToSimType( newCommand );
1508
1509 // If it is a new simulation type, open a new plot
1510 // For the DC sim, check if sweep source type has changed (char 4 will contain 'v',
1511 // 'i', 'r' or 't'.
1512 if( !plotPanelWindow
1513 || ( plotPanelWindow && plotPanelWindow->GetType() != newSimType )
1514 || ( newSimType == ST_DC
1515 && oldCommand.Lower().GetChar( 4 ) != newCommand.Lower().GetChar( 4 ) ) )
1516 {
1517 plotPanelWindow = NewPlotPanel( newCommand );
1518 }
1519 else
1520 {
1521 // Update simulation command in the current plot
1522 m_workbook->SetSimCommand( plotPanelWindow, newCommand );
1523 }
1524
1525 m_simulator->Init();
1526 }
1527}
1528
1529
1530void SIM_PLOT_FRAME::onAddSignal( wxCommandEvent& event )
1531{
1532 wxCHECK_RET( m_simFinished, wxT( "No simulation results available" ) );
1533
1534 SIM_PLOT_PANEL* plotPanel = GetCurrentPlot();
1535
1536 if( !plotPanel || !m_circuitModel || plotPanel->GetType() != m_circuitModel->GetSimType() )
1537 {
1538 DisplayInfoMessage( this, _( "You need to run plot-providing simulation first." ) );
1539 return;
1540 }
1541
1542 DIALOG_SIGNAL_LIST dialog( this, m_circuitModel.get() );
1543 dialog.ShowModal();
1544}
1545
1546
1547void SIM_PLOT_FRAME::onProbe( wxCommandEvent& event )
1548{
1549 wxCHECK_RET( m_simFinished, wxT( "No simulation results available" ) );
1550
1551 if( m_schematicFrame == nullptr )
1552 return;
1553
1554 wxWindow* blocking_dialog = m_schematicFrame->Kiway().GetBlockingDialog();
1555
1556 if( blocking_dialog )
1557 blocking_dialog->Close( true );
1558
1560 m_schematicFrame->Raise();
1561}
1562
1563
1564void SIM_PLOT_FRAME::onTune( wxCommandEvent& event )
1565{
1566 wxCHECK_RET( m_simFinished, wxT( "No simulation results available" ) );
1567
1568 if( m_schematicFrame == nullptr )
1569 return;
1570
1571 wxWindow* blocking_dialog = m_schematicFrame->Kiway().GetBlockingDialog();
1572
1573 if( blocking_dialog )
1574 blocking_dialog->Close( true );
1575
1577 m_schematicFrame->Raise();
1578}
1579
1580
1581void SIM_PLOT_FRAME::onShowNetlist( wxCommandEvent& event )
1582{
1583 class NETLIST_VIEW_DIALOG : public DIALOG_SHIM
1584 {
1585 public:
1586 enum
1587 {
1588 MARGIN_LINE_NUMBERS
1589 };
1590
1591 void onClose( wxCloseEvent& evt )
1592 {
1593 wxPostEvent( this, wxCommandEvent( wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL ) );
1594 }
1595
1596 NETLIST_VIEW_DIALOG( wxWindow* parent, wxString source) :
1597 DIALOG_SHIM( parent, wxID_ANY, wxT( "SPICE Netlist" ), wxDefaultPosition,
1598 wxDefaultSize, wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER )
1599 {
1600 wxStyledTextCtrl* text = new wxStyledTextCtrl( this, wxID_ANY );
1601 text->SetMinSize( wxSize( 600, 400 ) );
1602
1603 text->SetMarginWidth( MARGIN_LINE_NUMBERS, 50 );
1604 text->StyleSetForeground( wxSTC_STYLE_LINENUMBER, wxColour( 75, 75, 75 ) );
1605 text->StyleSetBackground( wxSTC_STYLE_LINENUMBER, wxColour( 220, 220, 220 ) );
1606 text->SetMarginType( MARGIN_LINE_NUMBERS, wxSTC_MARGIN_NUMBER );
1607
1608 wxFont fixedFont = KIUI::GetMonospacedUIFont();
1609
1610 for( size_t i = 0; i < wxSTC_STYLE_MAX; ++i )
1611 text->StyleSetFont( i, fixedFont );
1612
1613 text->StyleClearAll(); // Addresses a bug in wx3.0 where styles are not correctly set
1614
1615 text->SetWrapMode( wxSTC_WRAP_WORD );
1616
1617 text->SetText( source );
1618
1619 text->SetLexer( wxSTC_LEX_SPICE );
1620
1621 wxBoxSizer* sizer = new wxBoxSizer( wxVERTICAL );
1622 sizer->Add( text, 1, wxEXPAND | wxALL, 5 );
1623 SetSizer( sizer );
1624
1625 Connect( wxEVT_CLOSE_WINDOW, wxCloseEventHandler( NETLIST_VIEW_DIALOG::onClose ),
1626 nullptr, this );
1627
1628 m_scintillaTricks = std::make_unique<SCINTILLA_TRICKS>( text, wxT( "{}" ), false );
1629
1630 finishDialogSettings();
1631 }
1632
1633 std::unique_ptr<SCINTILLA_TRICKS> m_scintillaTricks;
1634 };
1635
1636 if( m_schematicFrame == nullptr || m_simulator == nullptr )
1637 return;
1638
1639 wxString errors;
1640 WX_STRING_REPORTER reporter( &errors );
1641 STRING_FORMATTER formatter;
1642 m_circuitModel->GetNetlist( &formatter, reporter );
1643
1644 NETLIST_VIEW_DIALOG dlg( this, errors.IsEmpty() ? formatter.GetString() : errors );
1645 dlg.ShowModal();
1646}
1647
1648
1649bool SIM_PLOT_FRAME::canCloseWindow( wxCloseEvent& aEvent )
1650{
1651 if( m_workbook->IsModified() )
1652 {
1653 wxFileName filename = m_simulator->Settings()->GetWorkbookFilename();
1654
1655 if( filename.GetName().IsEmpty() )
1656 {
1657 if( Prj().GetProjectName().IsEmpty() )
1658 filename.SetFullName( wxT( "noname.wbk" ) );
1659 else
1660 filename.SetFullName( Prj().GetProjectName() + wxT( ".wbk" ) );
1661 }
1662
1663 wxString fullFilename = filename.GetFullName();
1664 wxString msg = _( "Save changes to '%s' before closing?" );
1665
1666 return HandleUnsavedChanges( this, wxString::Format( msg, fullFilename ),
1667 [&]() -> bool
1668 {
1669 return saveWorkbook( Prj().AbsolutePath( fullFilename ) );
1670 } );
1671 }
1672
1673 return true;
1674}
1675
1676
1678{
1679 if( m_simulator->IsRunning() )
1680 m_simulator->Stop();
1681
1682 // Prevent memory leak on exit by deleting all simulation vectors
1683 m_simulator->Clean();
1684
1685 // Cancel a running simProbe or simTune tool
1687
1688 SaveSettings( config() );
1689 Destroy();
1690}
1691
1692
1693void SIM_PLOT_FRAME::onCursorUpdate( wxCommandEvent& event )
1694{
1695 wxSize size = m_cursors->GetClientSize();
1696 SIM_PLOT_PANEL* plotPanel = GetCurrentPlot();
1697 m_cursors->ClearAll();
1698
1699 if( !plotPanel )
1700 return;
1701
1703 m_cursors->SetImageList(m_signalsIconColorList, wxIMAGE_LIST_SMALL);
1704
1705 // Fill the signals listctrl
1706 m_cursors->AppendColumn( _( "Signal" ), wxLIST_FORMAT_LEFT, size.x / 2 );
1707 const long X_COL = m_cursors->AppendColumn( plotPanel->GetLabelX(), wxLIST_FORMAT_LEFT,
1708 size.x / 4 );
1709
1710 wxString labelY1 = plotPanel->GetLabelY1();
1711 wxString labelY2 = plotPanel->GetLabelY2();
1712 wxString labelY;
1713
1714 if( !labelY2.IsEmpty() )
1715 labelY = labelY1 + wxT( " / " ) + labelY2;
1716 else
1717 labelY = labelY1;
1718
1719 const long Y_COL = m_cursors->AppendColumn( labelY, wxLIST_FORMAT_LEFT, size.x / 4 );
1720
1721 // Update cursor values
1722 int itemidx = 0;
1723
1724 for( const auto& trace : plotPanel->GetTraces() )
1725 {
1726 if( CURSOR* cursor = trace.second->GetCursor() )
1727 {
1728 // Find the right icon color in list.
1729 // It is the icon used in m_signals list for the same trace
1730 long iconColor = m_signals->FindItem( -1, trace.first );
1731
1732 const wxRealPoint coords = cursor->GetCoords();
1733 long idx = m_cursors->InsertItem( itemidx++, trace.first, iconColor );
1734 m_cursors->SetItem( idx, X_COL, SPICE_VALUE( coords.x ).ToSpiceString() );
1735 m_cursors->SetItem( idx, Y_COL, SPICE_VALUE( coords.y ).ToSpiceString() );
1736 }
1737 }
1738}
1739
1740
1741void SIM_PLOT_FRAME::onSimStarted( wxCommandEvent& aEvent )
1742{
1743 m_toolBar->SetToolNormalBitmap( ID_SIM_RUN, KiBitmap( BITMAPS::sim_stop ) );
1744 SetCursor( wxCURSOR_ARROWWAIT );
1745}
1746
1747
1748void SIM_PLOT_FRAME::onSimFinished( wxCommandEvent& aEvent )
1749{
1750 m_toolBar->SetToolNormalBitmap( ID_SIM_RUN, KiBitmap( BITMAPS::sim_run ) );
1751 SetCursor( wxCURSOR_ARROW );
1752
1753 SIM_TYPE simType = m_circuitModel->GetSimType();
1754
1755 if( simType == ST_UNKNOWN )
1756 return;
1757
1758 SIM_PANEL_BASE* plotPanelWindow = getCurrentPlotWindow();
1759
1760 if( !plotPanelWindow || plotPanelWindow->GetType() != simType )
1761 plotPanelWindow = NewPlotPanel( m_circuitModel->GetSimCommand() );
1762
1763 // Sometimes (for instance with a directive like wrdata my_file.csv "my_signal")
1764 // the simulator is in idle state (simulation is finished), but still running, during
1765 // the time the file is written. So gives a slice of time to fully finish the work:
1766 if( m_simulator->IsRunning() )
1767 {
1768 int max_time = 40; // For a max timeout = 2s
1769
1770 do
1771 {
1772 wxMilliSleep( 50 );
1773 wxYield();
1774
1775 if( max_time )
1776 max_time--;
1777
1778 } while( max_time && m_simulator->IsRunning() );
1779 }
1780 // Is a warning message useful if the simulatior is still running?
1781
1782 // If there are any signals plotted, update them
1783 if( SIM_PANEL_BASE::IsPlottable( simType ) )
1784 {
1785 SIM_PLOT_PANEL* plotPanel = dynamic_cast<SIM_PLOT_PANEL*>( plotPanelWindow );
1786 wxCHECK_RET( plotPanel, wxT( "not a SIM_PLOT_PANEL" ) );
1787
1788 struct TRACE_DESC
1789 {
1790 wxString m_name;
1792 };
1793
1794 std::vector<struct TRACE_DESC> traceInfo;
1795
1796 // Get information about all the traces on the plot, remove and add again
1797 for( auto& trace : plotPanel->GetTraces() )
1798 {
1799 struct TRACE_DESC placeholder;
1800 placeholder.m_name = trace.second->GetName();
1801 placeholder.m_type = trace.second->GetType();
1802
1803 traceInfo.push_back( placeholder );
1804 }
1805
1806 for( auto& trace : traceInfo )
1807 {
1808 if( !updatePlot( trace.m_name, trace.m_type, plotPanel ) )
1809 removePlot( trace.m_name );
1810 }
1811
1813 plotPanel->GetPlotWin()->UpdateAll();
1814 plotPanel->ResetScales();
1815 }
1816 else if( simType == ST_OP )
1817 {
1818 m_simConsole->AppendText( _( "\n\nSimulation results:\n\n" ) );
1819 m_simConsole->SetInsertionPointEnd();
1820
1821 for( const auto& vec : m_simulator->AllPlots() )
1822 {
1823 std::vector<double> val_list = m_simulator->GetRealPlot( vec, 1 );
1824
1825 if( val_list.size() == 0 ) // The list of values can be empty!
1826 continue;
1827
1828 double val = val_list.at( 0 );
1829 wxString outLine, signal;
1830 SIM_PLOT_TYPE type = m_circuitModel->VectorToSignal( vec, signal );
1831
1832 const size_t tab = 25; //characters
1833 size_t padding = ( signal.length() < tab ) ? ( tab - signal.length() ) : 1;
1834
1835 outLine.Printf( wxT( "%s%s" ),
1836 ( signal + wxT( ":" ) ).Pad( padding, wxUniChar( ' ' ) ),
1837 SPICE_VALUE( val ).ToSpiceString() );
1838
1839 outLine.Append( type == SPT_CURRENT ? wxT( "A\n" ) : wxT( "V\n" ) );
1840
1841 m_simConsole->AppendText( outLine );
1842 m_simConsole->SetInsertionPointEnd();
1843
1844 // @todo display calculated values on the schematic
1845 }
1846 }
1847
1848 m_lastSimPlot = plotPanelWindow;
1849 m_simFinished = true;
1850}
1851
1852
1853void SIM_PLOT_FRAME::onSimUpdate( wxCommandEvent& aEvent )
1854{
1855 static bool updateInProgress = false;
1856
1857 // skip update when events are triggered too often and previous call didn't end yet
1858 if( updateInProgress )
1859 return;
1860
1861 updateInProgress = true;
1862
1863 if( m_simulator->IsRunning() )
1864 m_simulator->Stop();
1865
1867 {
1868 // We need to rerun simulation, as the simulator currently stores
1869 // results for another plot
1871 }
1872 else
1873 {
1874 std::unique_lock<std::mutex> simulatorLock( m_simulator->GetMutex(), std::try_to_lock );
1875
1876 if( simulatorLock.owns_lock() )
1877 {
1878 // Incremental update
1879 m_simConsole->Clear();
1880
1881 // Do not export netlist, it is already stored in the simulator
1882 applyTuners();
1883
1884 m_simulator->Run();
1885 }
1886 else
1887 DisplayErrorMessage( this, _( "Another simulation is already running." ) );
1888 }
1889 updateInProgress = false;
1890}
1891
1892
1893void SIM_PLOT_FRAME::onSimReport( wxCommandEvent& aEvent )
1894{
1895 m_simConsole->AppendText( aEvent.GetString() + "\n" );
1896 m_simConsole->SetInsertionPointEnd();
1897}
1898
1899
1901 SIM_PLOT_FRAME* aPlotFrame ) :
1902 m_signal( aSignal ),
1903 m_plotFrame( aPlotFrame )
1904{
1906
1907 AddMenuItem( this, HIDE_SIGNAL, _( "Hide Signal" ), _( "Erase the signal from plot screen" ),
1909
1910 TRACE* trace = plot->GetTrace( m_signal );
1911
1912 if( trace->HasCursor() )
1913 AddMenuItem( this, HIDE_CURSOR, _( "Hide Cursor" ), KiBitmap( BITMAPS::pcb_target ) );
1914 else
1915 AddMenuItem( this, SHOW_CURSOR, _( "Show Cursor" ), KiBitmap( BITMAPS::pcb_target ) );
1916
1917 Connect( wxEVT_COMMAND_MENU_SELECTED, wxMenuEventHandler( SIGNAL_CONTEXT_MENU::onMenuEvent ),
1918 nullptr, this );
1919}
1920
1921
1923{
1924 SIM_PLOT_PANEL* plot = m_plotFrame->GetCurrentPlot();
1925
1926 switch( aEvent.GetId() )
1927 {
1928 case HIDE_SIGNAL:
1929 m_plotFrame->removePlot( m_signal );
1930 break;
1931
1932 case SHOW_CURSOR:
1933 plot->EnableCursor( m_signal, true );
1934 break;
1935
1936 case HIDE_CURSOR:
1937 plot->EnableCursor( m_signal, false );
1938 break;
1939 }
1940}
1941
1942
1943wxDEFINE_EVENT( EVT_SIM_UPDATE, wxCommandEvent );
1944wxDEFINE_EVENT( EVT_SIM_REPORT, wxCommandEvent );
1945
1946wxDEFINE_EVENT( EVT_SIM_STARTED, wxCommandEvent );
1947wxDEFINE_EVENT( EVT_SIM_FINISHED, wxCommandEvent );
const char * name
Definition: DXF_plotter.cpp:56
wxBitmap KiBitmap(BITMAPS aBitmap, int aHeightTag)
Construct a wxBitmap from an image identifier Returns the image from the active theme if the image ha...
Definition: bitmap.cpp:105
wxMenuItem * AddMenuItem(wxMenu *aMenu, int aId, const wxString &aText, const wxBitmap &aImage, wxItemKind aType=wxITEM_NORMAL)
Create and insert a menu item with an icon into aMenu.
Definition: bitmap.cpp:265
void AddBitmapToMenuItem(wxMenuItem *aMenu, const wxBitmap &aImage)
Add a bitmap to a menuitem.
Definition: bitmap.cpp:251
BITMAPS
A list of all bitmap identifiers.
Definition: bitmaps_list.h:33
@ add_dashed_line
@ INVALID_BITMAP
@ zoom_fit_in_page
@ directory_open
@ sim_add_signal
static TOOL_ACTION cancelInteractive
Definition: actions.h:63
APP_SETTINGS_BASE is a settings class that should be derived for each standalone KiCad application.
Definition: app_settings.h:101
Cursor attached to a trace to follow its values:
Dialog helper object to sit in the inheritance tree between wxDialog and any class written by wxFormB...
Definition: dialog_shim.h:83
const wxString & GetSimCommand() const
bool SetSimCommand(const wxString &aCommand)
virtual APP_SETTINGS_BASE * config() const
Returns the settings object used in SaveSettings(), and is overloaded in KICAD_MANAGER_FRAME.
virtual void LoadSettings(APP_SETTINGS_BASE *aCfg)
Load common frame parameters from a configuration file.
virtual void SaveSettings(APP_SETTINGS_BASE *aCfg)
Save common frame parameters to a configuration data file.
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:85
static TOOL_ACTION simTune
Definition: ee_actions.h:249
static TOOL_ACTION simProbe
Definition: ee_actions.h:248
EE_TYPE OfType(KICAD_T aType) const
Definition: sch_rtree.h:238
PROJECT & Prj() const
Return a reference to the PROJECT associated with this KIWAY.
KIWAY & Kiway() const
Return a reference to the KIWAY that this object has an opportunity to participate in.
Definition: kiway_holder.h:53
void SetKiway(wxWindow *aDest, KIWAY *aKiway)
It is only used for debugging, since "this" is not a wxWindow*.
HOLDER_TYPE m_type
Definition: kiway_holder.h:85
bool Destroy() override
Our version of Destroy() which is virtual from wxWidgets.
A minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the same KiCad...
Definition: kiway.h:273
virtual KIWAY_PLAYER * Player(FRAME_T aFrameType, bool doCreate=true, wxTopLevelWindow *aParent=nullptr)
Return the KIWAY_PLAYER* given a FRAME_T.
Definition: kiway.cpp:394
wxWindow * GetBlockingDialog()
Gets the window pointer to the blocking dialog (to send it signals)
Definition: kiway.cpp:617
Hold a translatable error message and may be used when throwing exceptions containing a translated er...
Definition: ki_exception.h:45
const wxString What() const
Definition: ki_exception.h:57
const SPICE_ITEM * FindItem(const std::string &aRefName) const
Find and return the item corresponding to aRefName.
Special netlist exporter flavor that allows one to override simulation commands.
static SIM_TYPE CommandToSimType(const wxString &aCmd)
Return simulation type basing on a simulation command directive.
A singleton reporter that reports to nowhere.
Definition: reporter.h:223
The backing store for a PROJECT, in JSON format.
Definition: project_file.h:65
virtual const wxString GetProjectPath() const
Return the full path of the project.
Definition: project.cpp:126
virtual PROJECT_FILE & GetProjectFile() const
Definition: project.h:148
virtual const wxString AbsolutePath(const wxString &aFileName) const
Fix up aFileName if it is relative to the project's directory to be an absolute path and filename.
Definition: project.cpp:305
A pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:71
Schematic editor (Eeschema) main window.
void SaveProjectSettings() override
Save changes to the project settings to the project (.pro) file.
void OnModify() override
Must be called after a schematic change in order to set the "modify" flag and update other data struc...
SCH_SCREEN * GetScreen() const override
Return a pointer to a BASE_SCREEN or one of its derivatives.
bool ReadyToNetlist(const wxString &aAnnotateMessage)
Check if we are ready to write a netlist file for the current schematic.
SCHEMATIC & Schematic() const
void UpdateItem(EDA_ITEM *aItem, bool isAddOrDelete=false, bool aUpdateRtree=false) override
Mark an item for refresh.
wxString GetShownText(int aDepth=0, bool aAllowExtraText=true) const override
Return the string actually shown after processing of the base text.
Definition: sch_field.cpp:169
EE_RTREE & Items()
Gets the full RTree, usually for iterating.
Definition: sch_screen.h:109
Schematic symbol object.
Definition: sch_symbol.h:80
SCH_FIELD * GetField(MANDATORY_FIELD_T aFieldType)
Return a mandatory field in this symbol.
Definition: sch_symbol.cpp:788
void GetFields(std::vector< SCH_FIELD * > &aVector, bool aVisibleOnly)
Populate a std::vector with SCH_FIELDs.
Definition: sch_symbol.cpp:824
static std::shared_ptr< SPICE_SIMULATOR > CreateInstance(const std::string &aName)
SIM_MODEL & CreateModel(SIM_MODEL::TYPE aType, int aSymbolPinCount)
Definition: sim_lib_mgr.cpp:70
virtual const PARAM * GetTunerParam() const
Definition: sim_model.h:486
void WriteFields(std::vector< T > &aFields) const
Definition: sim_model.cpp:460
virtual void SetParamValue(int aParamIndex, const SIM_VALUE &aValue)
Definition: sim_model.cpp:750
static bool IsPlottable(SIM_TYPE aSimType)
SIM_TYPE GetType() const
static void FillDefaultColorList(bool aWhiteBg)
Fills m_colorList by a default set of colors.
Panel that was used as the most recent one for simulations.
SIGNAL_CONTEXT_MENU(const wxString &aSignal, SIM_PLOT_FRAME *aPlotFrame)
void onMenuEvent(wxMenuEvent &aEvent)
Class SIM_PLOT_FRAME_BASE.
wxSplitterWindow * m_splitterTuneValues
wxMenuItem * m_runSimulation
SIM_WORKBOOK * m_workbook
wxSplitterWindow * m_splitterPlotAndConsole
wxSplitterWindow * m_splitterSignals
wxSplitterWindow * m_splitterLeftRight
Implementing SIM_PLOT_FRAME_BASE.
void setSubWindowsSashSize()
int m_splitterSignalsSashPosition
void onShowNetlist(wxCommandEvent &event)
void setIconsForMenuItems()
Give icons to menuitems of the main menubar.
void onProbe(wxCommandEvent &event)
void AddVoltagePlot(const wxString &aNetName)
Add a voltage plot for a given net name.
void menuShowLegendUpdate(wxUpdateUIEvent &event) override
const NGSPICE_CIRCUIT_MODEL * GetExporter() const
Return the netlist exporter object used for simulations.
void onCursorUpdate(wxCommandEvent &aEvent)
void onAddSignal(wxCommandEvent &event)
void onPlotDragged(wxAuiNotebookEvent &event) override
wxString getDefaultPath()
Return the default path to be used in file browser dialog.
void onSignalRClick(wxListEvent &event) override
void RemoveTuner(TUNER_SLIDER *aTuner, bool aErase=true)
Remove an existing tuner.
void onSimReport(wxCommandEvent &aEvent)
void menuZoomIn(wxCommandEvent &event) override
void updateSignalList()
Update the list of currently plotted signals.
void menuWhiteBackground(wxCommandEvent &event) override
void menuSaveWorkbook(wxCommandEvent &event) override
void onSimStarted(wxCommandEvent &aEvent)
wxToolBarToolBase * m_toolTune
void menuShowDotted(wxCommandEvent &event) override
wxToolBarToolBase * m_toolAddSignals
SIM_THREAD_REPORTER * m_reporter
List of currently displayed tuners.
DIALOG_SIM_SETTINGS * m_settingsDlg
void SaveSettings(APP_SETTINGS_BASE *aCfg) override
Save common frame parameters to a configuration data file.
wxToolBarToolBase * m_toolSimulate
void AddTuner(SCH_SYMBOL *aSymbol)
Add a tuner for a symbol.
void menuSimulateUpdate(wxUpdateUIEvent &event) override
void menuAddSignalsUpdate(wxUpdateUIEvent &event) override
bool loadWorkbook(const wxString &aPath)
Load plot settings from a file.
void UpdateTunerValue(SCH_SYMBOL *aSymbol, const wxString &aValue)
Safely update a field of the associated symbol without dereferencing the symbol.
void onSimUpdate(wxCommandEvent &aEvent)
bool saveWorkbook(const wxString &aPath)
Save plot settings to a file.
void onWorkbookClrModified(wxCommandEvent &event)
wxToolBarToolBase * m_toolProbe
void onPlotChanged(wxAuiNotebookEvent &event) override
void AddCurrentPlot(const wxString &aDeviceName)
Add a current plot for a particular device.
void updateTitle()
Set the main window title bar text.
void menuSaveCsv(wxCommandEvent &event) override
void updateTuners()
Filter out tuners for components that do not exist anymore.
SIM_PLOT_FRAME(KIWAY *aKiway, wxWindow *aParent)
void onWorkbookModified(wxCommandEvent &event)
void onSignalDblClick(wxMouseEvent &event) override
void StartSimulation(const wxString &aSimCommand=wxEmptyString)
void menuShowLegend(wxCommandEvent &event) override
int m_splitterTuneValuesSashPosition
bool GetPlotBgOpt() const
SIM_PANEL_BASE * m_lastSimPlot
imagelists used to add a small colored icon to signal names and cursors name, the same color as the c...
void onPlotClosed(wxAuiNotebookEvent &event) override
void onSimulate(wxCommandEvent &event)
void menuShowGridUpdate(wxUpdateUIEvent &event) override
wxString getDefaultFilename()
Return the default filename (with extension) to be used in file browser dialog.
void menuShowDottedUpdate(wxUpdateUIEvent &event) override
wxToolBarToolBase * m_toolSettings
void menuSaveWorkbookAs(wxCommandEvent &event) override
void onPlotClose(wxAuiNotebookEvent &event) override
bool updatePlot(const wxString &aName, SIM_PLOT_TYPE aType, SIM_PLOT_PANEL *aPlotPanel)
Update plot in a particular SIM_PLOT_PANEL.
bool canCloseWindow(wxCloseEvent &aEvent) override
SIM_PLOT_PANEL * GetCurrentPlot() const
Return the currently opened plot panel (or NULL if there is none).
void doCloseWindow() override
wxString getCurrentSimCommand() const
void menuTuneUpdate(wxUpdateUIEvent &event) override
void initWorkbook()
Load the currently active workbook stored in the project settings.
std::list< TUNER_SLIDER * > m_tuners
SIM_PANEL_BASE * getCurrentPlotWindow() const
Return the currently opened plot panel (or NULL if there is none).
void addPlot(const wxString &aName, SIM_PLOT_TYPE aType)
Add a new plot to the current panel.
void applyTuners()
Apply component values specified using tuner sliders to the current netlist.
void removePlot(const wxString &aPlotName)
Remove a plot with a specific title.
void LoadSettings(APP_SETTINGS_BASE *aCfg) override
Load common frame parameters from a configuration file.
std::shared_ptr< SPICE_SIMULATOR_SETTINGS > & GetSimulatorSettings()
void menuShowGrid(wxCommandEvent &event) override
WINDOW_SETTINGS * GetWindowSettings(APP_SETTINGS_BASE *aCfg) override
Return a pointer to the window settings for this frame.
void onSimFinished(wxCommandEvent &aEvent)
void onSettings(wxCommandEvent &event)
void menuSaveImage(wxCommandEvent &event) override
SIM_PANEL_BASE * NewPlotPanel(wxString aSimCommand)
Create a new plot panel for a given simulation type and adds it to the main notebook.
void onTune(wxCommandEvent &event)
int m_splitterLeftRightSashPosition
SCH_EDIT_FRAME * m_schematicFrame
void menuNewPlot(wxCommandEvent &aEvent) override
void menuOpenWorkbook(wxCommandEvent &event) override
void menuZoomOut(wxCommandEvent &event) override
wxImageList * m_signalsIconColorList
std::shared_ptr< NGSPICE_CIRCUIT_MODEL > m_circuitModel
std::shared_ptr< SPICE_SIMULATOR > m_simulator
SIM_PLOT_TYPE getXAxisType(SIM_TYPE aType) const
Return X axis for a given simulation type.
int m_splitterPlotAndConsoleSashPosition
void menuProbeUpdate(wxUpdateUIEvent &event) override
unsigned int m_plotNumber
void menuZoomFit(wxCommandEvent &event) override
void UpdatePlotColors()
‍Update plot colors
void ResetScales()
Update trace line style.
wxString GetLabelY2() const
bool TraceShown(const wxString &aName) const
const std::map< wxString, TRACE * > & GetTraces() const
mpWindow * GetPlotWin() const
wxString GetLabelY1() const
wxString GetLabelX() const
REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) override
Report a string with a given severity.
void OnSimStateChange(SPICE_SIMULATOR *aObject, SIM_STATE aNewState) override
SIM_THREAD_REPORTER(SIM_PLOT_FRAME *aParent)
SIM_PLOT_FRAME * m_parent
bool HasMessage() const override
Returns true if the reporter client is non-empty.
const wxString & GetSimCommand(const SIM_PANEL_BASE *aPlotPanel)
Definition: sim_workbook.h:61
bool AddTrace(SIM_PLOT_PANEL *aPlotPanel, const wxString &aTitle, const wxString &aName, int aPoints, const double *aX, const double *aY, SIM_PLOT_TYPE aType)
bool DeleteAllPages() override
void SetSimCommand(SIM_PANEL_BASE *aPlotPanel, const wxString &aSimCommand)
Definition: sim_workbook.h:55
void ClrModified()
bool DeleteTrace(SIM_PLOT_PANEL *aPlotPanel, const wxString &aName)
bool IsModified() const
Definition: sim_workbook.h:67
bool AddPage(wxWindow *aPage, const wxString &aCaption, bool aSelect=false, const wxBitmap &aBitmap=wxNullBitmap)
Interface to receive simulation updates from SPICE_SIMULATOR class.
< Helper class to handle Spice way of expressing values (e.g. 10.5 Meg) Helper class to recognize Spi...
Definition: spice_value.h:35
wxString ToString() const
Return string value as when converting double to string (e.g.
wxString ToSpiceString() const
Return string value in Spice format (e.g.
Implement an OUTPUTFORMATTER to a memory buffer.
Definition: richio.h:415
const std::string & GetString()
Definition: richio.h:438
TOOL_MANAGER * GetToolManager() const
Return the MVC controller.
Definition: tools_holder.h:54
bool RunAction(const std::string &aActionName, bool aNow=false, T aParam=NULL)
Run the specified action.
Definition: tool_manager.h:142
bool HasCursor() const
Custom widget to handle quick component values modification and simulation on the fly.
Definition: tuner_slider.h:44
A wrapper for reporting to a wxString object.
Definition: reporter.h:164
bool SaveScreenshot(const wxString &filename, wxBitmapType type=wxBITMAP_TYPE_BMP, wxSize imageSize=wxDefaultSize, bool fit=false)
Draw the window on a wxBitmap, then save it to a file.
Definition: mathplot.cpp:2630
void ZoomIn(const wxPoint &centerPoint=wxDefaultPosition)
Zoom into current view and refresh display.
Definition: mathplot.cpp:2070
void ZoomOut(const wxPoint &centerPoint=wxDefaultPosition)
Zoom out current view and refresh display.
Definition: mathplot.cpp:2127
void UpdateAll()
Refresh display.
Definition: mathplot.cpp:2403
void EnableMouseWheelPan(bool enabled)
Enable/disable trackpad friendly panning (2-axis scroll wheel)
Definition: mathplot.h:1220
void Fit() override
Set view to fit global bounding box of all plot layers and refresh display.
Definition: mathplot.cpp:1910
void DisplayInfoMessage(wxWindow *aParent, const wxString &aMessage, const wxString &aExtraInfo)
Display an informational message box with aMessage.
Definition: confirm.cpp:320
bool HandleUnsavedChanges(wxWindow *aParent, const wxString &aMessage, const std::function< bool()> &aSaveFunction)
Display a dialog with Save, Cancel and Discard Changes buttons.
Definition: confirm.cpp:240
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition: confirm.cpp:299
This file is part of the common library.
#define _(s)
@ ID_SIM_PROBE
Definition: eeschema_id.h:80
@ ID_SIM_RUN
Definition: eeschema_id.h:78
@ ID_SIM_ADD_SIGNALS
Definition: eeschema_id.h:81
@ ID_SIM_TUNE
Definition: eeschema_id.h:79
@ FRAME_SCH
Definition: frame_type.h:34
const std::string WorkbookFileExtension
wxString WorkbookFileWildcard()
wxString PngFileWildcard()
wxString CsvFileWildcard()
Macros and inline functions to create menus items in menubars or popup menus.
wxFont GetMonospacedUIFont()
Definition: ui_common.cpp:85
see class PGM_BASE
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
SEVERITY
@ RPT_SEVERITY_UNDEFINED
static LIB_SYMBOL * dummy()
Used to draw a dummy shape when a LIB_SYMBOL is not found in library.
Definition: sch_symbol.cpp:74
#define DISPLAY_LOAD_ERROR(fmt)
wxDEFINE_EVENT(EVT_SIM_UPDATE, wxCommandEvent)
SIM_PLOT_TYPE operator|(SIM_PLOT_TYPE aFirst, SIM_PLOT_TYPE aSecond)
Subclass of SIM_PLOT_FRAME_BASE, which is generated by wxFormBuilder.
#define ID_SAVE_AS_CSV
#define ID_MENU_SET_SIMUL
#define ID_MENU_TUNE_SIGNALS
#define ID_MENU_ADD_SIGNAL
#define ID_MENU_WHITE_BG
#define ID_MENU_PROBE_SIGNALS
#define ID_MENU_DOTTED
#define ID_SAVE_AS_IMAGE
#define ID_MENU_SHOW_NETLIST
#define ID_MENU_RUN_SIM
#define ID_MENU_SHOW_GRID
#define ID_MENU_SHOW_LEGEND
SIM_PLOT_TYPE
Definition: sim_types.h:47
@ SPT_TIME
Definition: sim_types.h:55
@ SPT_LOG_FREQUENCY
Definition: sim_types.h:57
@ SPT_AC_PHASE
Definition: sim_types.h:51
@ SPT_SWEEP
Definition: sim_types.h:58
@ SPT_VOLTAGE
Definition: sim_types.h:49
@ SPT_CURRENT
Definition: sim_types.h:50
@ SPT_AC_MAG
Definition: sim_types.h:52
@ SPT_LIN_FREQUENCY
Definition: sim_types.h:56
SIM_TYPE
< Possible simulation types
Definition: sim_types.h:32
@ ST_TRANSIENT
Definition: sim_types.h:42
@ ST_UNKNOWN
Definition: sim_types.h:33
@ ST_NOISE
Definition: sim_types.h:37
@ ST_AC
Definition: sim_types.h:34
@ ST_DC
Definition: sim_types.h:35
@ ST_OP
Definition: sim_types.h:38
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:111
SIM_STATE
@ SIM_IDLE
@ SIM_RUNNING
wxString UnescapeString(const wxString &aSource)
wxString EscapeString(const wxString &aSource, ESCAPE_CONTEXT aContext)
The Escape/Unescape routines use HTML-entity-reference-style encoding to handle characters which are:...
@ CTX_LINE
Definition: string_utils.h:59
SPICE_VALUE m_vstart
SPICE_VALUE m_vincrement
SPICE_VALUE m_vend
const SIM_MODEL * model
Stores the common settings that are saved and loaded for each window / frame.
Definition: app_settings.h:83
@ REFERENCE_FIELD
Field Reference of part, i.e. "IC21".
@ SCH_SYMBOL_T
Definition: typeinfo.h:156
Definition of file extensions used in Kicad.
#define SEPARATOR
Definition: wx_combobox.cpp:27
#define FN_NORMALIZE_FLAGS
Default flags to pass to wxFileName::Normalize().
Definition: wx_filename.h:38