KiCad PCB EDA Suite
Loading...
Searching...
No Matches
simulator_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-2023 CERN
5 * Copyright (C) 2016-2024 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
29// For some obscure reason, needed on msys2 with some wxWidgets versions (3.0) to avoid
30// undefined symbol at link stage (due to use of #include <pegtl.hpp>)
31// Should not create issues on other platforms
32#include <wx/menu.h>
33
35#include <sch_edit_frame.h>
36#include <kiway.h>
37#include <confirm.h>
38#include <bitmaps.h>
42#include <widgets/wx_grid.h>
43#include <tool/tool_manager.h>
45#include <tool/action_manager.h>
46#include <tool/action_toolbar.h>
47#include <tool/common_control.h>
49#include <tools/ee_actions.h>
50#include <string_utils.h>
51#include <pgm_base.h>
52#include "ngspice.h"
53#include <sim/simulator_frame.h>
55#include <sim/sim_plot_tab.h>
56#include <sim/spice_simulator.h>
58#include <eeschema_settings.h>
59#include <advanced_config.h>
60
61#include <memory>
62
63
65{
66public:
68 m_parent( aParent )
69 {
70 }
71
72 REPORTER& Report( const wxString& aText, SEVERITY aSeverity = RPT_SEVERITY_UNDEFINED ) override
73 {
74 wxCommandEvent* event = new wxCommandEvent( EVT_SIM_REPORT );
75 event->SetString( aText );
76 wxQueueEvent( m_parent, event );
77 return *this;
78 }
79
80 bool HasMessage() const override
81 {
82 return false; // Technically "indeterminate" rather than false.
83 }
84
85 void OnSimStateChange( SIMULATOR* aObject, SIM_STATE aNewState ) override
86 {
87 wxCommandEvent* event = nullptr;
88
89 switch( aNewState )
90 {
91 case SIM_IDLE: event = new wxCommandEvent( EVT_SIM_FINISHED ); break;
92 case SIM_RUNNING: event = new wxCommandEvent( EVT_SIM_STARTED ); break;
93 default: wxFAIL; return;
94 }
95
96 wxQueueEvent( m_parent, event );
97 }
98
99private:
101};
102
103
104BEGIN_EVENT_TABLE( SIMULATOR_FRAME, KIWAY_PLAYER )
105 EVT_MENU( wxID_EXIT, SIMULATOR_FRAME::onExit )
106 EVT_MENU( wxID_CLOSE, SIMULATOR_FRAME::onExit )
107END_EVENT_TABLE()
108
109
110SIMULATOR_FRAME::SIMULATOR_FRAME( KIWAY* aKiway, wxWindow* aParent ) :
111 KIWAY_PLAYER( aKiway, aParent, FRAME_SIMULATOR, _( "Simulator" ), wxDefaultPosition,
112 wxDefaultSize, wxDEFAULT_FRAME_STYLE, wxT( "simulator" ), unityScale ),
113 m_schematicFrame( nullptr ),
114 m_toolBar( nullptr ),
115 m_ui( nullptr ),
116 m_simFinished( false ),
117 m_workbookModified( false )
118{
119 m_schematicFrame = (SCH_EDIT_FRAME*) Kiway().Player( FRAME_SCH, false );
120 wxASSERT( m_schematicFrame );
121
122 // Give an icon
123 wxIcon icon;
124 icon.CopyFromBitmap( KiBitmap( BITMAPS::simulator ) );
125 SetIcon( icon );
126
127 wxBoxSizer* mainSizer = new wxBoxSizer( wxVERTICAL );
128 SetSizer( mainSizer );
129
130 m_infoBar = new WX_INFOBAR( this );
131 mainSizer->Add( m_infoBar, 0, wxEXPAND, 0 );
132
133 m_toolBar = new ACTION_TOOLBAR( this, wxID_ANY, wxDefaultPosition, wxDefaultSize,
134 wxAUI_TB_DEFAULT_STYLE|wxAUI_TB_HORZ_LAYOUT|wxAUI_TB_PLAIN_BACKGROUND );
135 m_toolBar->Realize();
136 mainSizer->Add( m_toolBar, 0, wxEXPAND, 5 );
137
138 m_ui = new SIMULATOR_FRAME_UI( this, m_schematicFrame );
139 mainSizer->Add( m_ui, 1, wxEXPAND, 5 );
140
141 m_simulator = SIMULATOR::CreateInstance( "ngspice" );
142 wxASSERT( m_simulator );
143
144 LoadSettings( config() );
145
146 NGSPICE_SETTINGS* settings = dynamic_cast<NGSPICE_SETTINGS*>( m_simulator->Settings().get() );
147
148 wxCHECK2( settings, /* do nothing in release builds*/ );
149
150 if( settings && settings->GetWorkbookFilename().IsEmpty() )
152
153 m_simulator->Init();
154
155 m_reporter = new SIM_THREAD_REPORTER( this );
156 m_simulator->SetReporter( m_reporter );
157
158 m_circuitModel = std::make_shared<SPICE_CIRCUIT_MODEL>( &m_schematicFrame->Schematic(), this );
159
160 setupTools();
161 setupUIConditions();
162
163 ReCreateHToolbar();
164 ReCreateMenuBar();
165
166 Bind( wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler( SIMULATOR_FRAME::onExit ), this,
167 wxID_EXIT );
168
169 Bind( EVT_SIM_UPDATE, &SIMULATOR_FRAME::onUpdateSim, this );
170 Bind( EVT_SIM_REPORT, &SIMULATOR_FRAME::onSimReport, this );
171 Bind( EVT_SIM_STARTED, &SIMULATOR_FRAME::onSimStarted, this );
172 Bind( EVT_SIM_FINISHED, &SIMULATOR_FRAME::onSimFinished, this );
173
174 // Ensure new items are taken in account by sizers:
175 Layout();
176
177 // resize the subwindows size. At least on Windows, calling wxSafeYield before
178 // resizing the subwindows forces the wxSplitWindows size events automatically generated
179 // by wxWidgets to be executed before our resize code.
180 // Otherwise, the changes made by setSubWindowsSashSize are overwritten by one these
181 // events
182 wxSafeYield();
183 m_ui->SetSubWindowsSashSize();
184
185 // Ensure the window is on top
186 Raise();
187
188 m_ui->InitWorkbook();
189 UpdateTitle();
190}
191
192
194{
195 NULL_REPORTER devnull;
196
197 m_simulator->Attach( nullptr, wxEmptyString, 0, devnull );
198 m_simulator->SetReporter( nullptr );
199 delete m_reporter;
200}
201
202
204{
205 // Create the manager
207 m_toolManager->SetEnvironment( nullptr, nullptr, nullptr, config(), this );
208
210
211 // Attach the events to the tool dispatcher
213 Bind( wxEVT_CHAR_HOOK, &TOOL_DISPATCHER::DispatchWxEvent, m_toolDispatcher );
214
215 // Register tools
219}
220
221
223{
225
226 UpdateTitle();
227
229}
230
231
233{
234 EESCHEMA_SETTINGS* cfg = dynamic_cast<EESCHEMA_SETTINGS*>( aCfg );
235 wxASSERT( cfg );
236
237 if( cfg )
238 {
240 m_ui->LoadSettings( cfg );
241 }
242
244
245 NGSPICE* currentSim = dynamic_cast<NGSPICE*>( m_simulator.get() );
246
247 if( currentSim )
248 m_simulator->Settings() = project.m_SchematicSettings->m_NgspiceSettings;
249}
250
251
253{
254 EESCHEMA_SETTINGS* cfg = dynamic_cast<EESCHEMA_SETTINGS*>( aCfg );
255 wxASSERT( cfg );
256
257 if( cfg )
258 {
260 m_ui->SaveSettings( cfg );
261 }
262
264
265 if( project.m_SchematicSettings )
266 {
267 bool modified = project.m_SchematicSettings->m_NgspiceSettings->SaveToFile();
268
269 if( m_schematicFrame && modified )
271 }
272}
273
274
275void SIMULATOR_FRAME::CommonSettingsChanged( bool aEnvVarsChanged, bool aTextVarsChanged )
276{
277 KIWAY_PLAYER::CommonSettingsChanged( aEnvVarsChanged, aTextVarsChanged );
278
279 auto* cfg = dynamic_cast<EESCHEMA_SETTINGS*>( m_toolManager->GetSettings() );
280 wxASSERT( cfg != nullptr );
281 m_ui->ApplyPreferences( cfg->m_Simulator.preferences );
282}
283
284
286{
287 EESCHEMA_SETTINGS* cfg = dynamic_cast<EESCHEMA_SETTINGS*>( aCfg );
288 wxASSERT( cfg );
289
290 return cfg ? &cfg->m_Simulator.window : nullptr;
291}
292
293
295{
296 if( m_ui->GetCurrentSimTab() )
298 else
299 return m_circuitModel->GetSchTextSimCommand();
300}
301
302
304{
306}
307
308
310{
311 if( SIM_TAB* simTab = m_ui->GetCurrentSimTab() )
312 return simTab->GetSimOptions();
313 else
315}
316
317
319{
320 bool unsaved = true;
321 bool readOnly = false;
322 wxString title;
323 wxFileName filename = Prj().AbsolutePath( m_simulator->Settings()->GetWorkbookFilename() );
324
325 if( filename.IsOk() && filename.FileExists() )
326 {
327 unsaved = false;
328 readOnly = !filename.IsFileWritable();
329 }
330
332 title = wxT( "*" ) + filename.GetName();
333 else
334 title = filename.GetName();
335
336 if( readOnly )
337 title += wxS( " " ) + _( "[Read Only]" );
338
339 if( unsaved )
340 title += wxS( " " ) + _( "[Unsaved]" );
341
342 title += wxT( " \u2014 " ) + _( "Spice Simulator" );
343
344 SetTitle( title );
345}
346
347
348
349bool SIMULATOR_FRAME::LoadSimulator( const wxString& aSimCommand, unsigned aSimOptions )
350{
351 wxString errors;
352 WX_STRING_REPORTER reporter( &errors );
353
354 if( !m_schematicFrame->ReadyToNetlist( _( "Simulator requires a fully annotated schematic." ) ) )
355 return false;
356
357 // If we are using the new connectivity, make sure that we do a full-rebuild
358 if( ADVANCED_CFG::GetCfg().m_IncrementalConnectivity )
360
361 if( !m_simulator->Attach( m_circuitModel, aSimCommand, aSimOptions, reporter ) )
362 {
363 DisplayErrorMessage( this, _( "Errors during netlist generation.\n\n" ) + errors );
364 return false;
365 }
366
367 return true;
368}
369
370
371void SIMULATOR_FRAME::ReloadSimulator( const wxString& aSimCommand, unsigned aSimOptions )
372{
373 wxString errors;
374 WX_STRING_REPORTER reporter( &errors );
375
376 if( !m_simulator->Attach( m_circuitModel, aSimCommand, aSimOptions, reporter ) )
377 {
378 DisplayErrorMessage( this, _( "Errors during netlist generation.\n\n" ) + errors );
379 }
380}
381
382
384{
385 SIM_TAB* simTab = m_ui->GetCurrentSimTab();
386
387 if( !simTab )
388 return;
389
390 if( simTab->GetSimCommand().Upper().StartsWith( wxT( "FFT" ) )
391 || simTab->GetSimCommand().Upper().Contains( wxT( "\nFFT" ) ) )
392 {
393 wxString tranSpicePlot;
394
395 if( SIM_TAB* tranPlotTab = m_ui->GetSimTab( ST_TRAN ) )
396 tranSpicePlot = tranPlotTab->GetSpicePlotName();
397
398 if( tranSpicePlot.IsEmpty() )
399 {
400 DisplayErrorMessage( this, _( "You must run a TRAN simulation first; its results"
401 "will be used for the fast Fourier transform." ) );
402 }
403 else
404 {
405 m_simulator->Command( "setplot " + tranSpicePlot.ToStdString() );
406
407 wxArrayString commands = wxSplit( simTab->GetSimCommand(), '\n' );
408
409 for( const wxString& command : commands )
410 {
411 wxBusyCursor wait;
412 m_simulator->Command( command.ToStdString() );
413 }
414
415 simTab->SetSpicePlotName( m_simulator->CurrentPlotName() );
416 m_ui->OnSimRefresh( true );
417
418#if 0
419 m_simulator->Command( "setplot" ); // Print available plots to console
420 m_simulator->Command( "display" ); // Print vectors in current plot to console
421#endif
422 }
423
424 return;
425 }
426 else
427 {
428 if( m_ui->GetSimTabIndex( simTab ) == 0
429 && m_circuitModel->GetSchTextSimCommand() != simTab->GetLastSchTextSimCommand() )
430 {
431 if( simTab->GetLastSchTextSimCommand().IsEmpty()
432 || IsOK( this, _( "Schematic sheet simulation command directive has changed. "
433 "Do you wish to update the Simulation Command?" ) ) )
434 {
435 simTab->SetSimCommand( m_circuitModel->GetSchTextSimCommand() );
436 simTab->SetLastSchTextSimCommand( simTab->GetSimCommand() );
437 OnModify();
438 }
439 }
440 }
441
442 if( !LoadSimulator( simTab->GetSimCommand(), simTab->GetSimOptions() ) )
443 return;
444
445 std::unique_lock<std::mutex> simulatorLock( m_simulator->GetMutex(), std::try_to_lock );
446
447 if( simulatorLock.owns_lock() )
448 {
449 m_ui->OnSimUpdate();
450 m_simulator->Run();
451 }
452 else
453 {
454 DisplayErrorMessage( this, _( "Another simulation is already running." ) );
455 }
456}
457
458
459SIM_TAB* SIMULATOR_FRAME::NewSimTab( const wxString& aSimCommand )
460{
461 return m_ui->NewSimTab( aSimCommand );
462}
463
464
465const std::vector<wxString> SIMULATOR_FRAME::SimPlotVectors()
466{
467 return m_ui->SimPlotVectors();
468}
469
470
471const std::vector<wxString> SIMULATOR_FRAME::Signals()
472{
473 return m_ui->Signals();
474}
475
476
477const std::map<int, wxString>& SIMULATOR_FRAME::UserDefinedSignals()
478{
479 return m_ui->UserDefinedSignals();
480}
481
482
483void SIMULATOR_FRAME::SetUserDefinedSignals( const std::map<int, wxString>& aSignals )
484{
485 m_ui->SetUserDefinedSignals( aSignals );
486}
487
488
489void SIMULATOR_FRAME::AddVoltageTrace( const wxString& aNetName )
490{
491 m_ui->AddTrace( aNetName, SPT_VOLTAGE );
492}
493
494
495void SIMULATOR_FRAME::AddCurrentTrace( const wxString& aDeviceName )
496{
497 m_ui->AddTrace( aDeviceName, SPT_CURRENT );
498}
499
500
501void SIMULATOR_FRAME::AddTuner( const SCH_SHEET_PATH& aSheetPath, SCH_SYMBOL* aSymbol )
502{
503 m_ui->AddTuner( aSheetPath, aSymbol );
504}
505
506
508{
509 return m_ui->GetCurrentSimTab();
510}
511
512
513bool SIMULATOR_FRAME::LoadWorkbook( const wxString& aPath )
514{
515 if( m_ui->LoadWorkbook( aPath ) )
516 {
517 UpdateTitle();
518
519 // Successfully loading a workbook does not count as modifying it. Clear the modified
520 // flag after all the EVT_WORKBOOK_MODIFIED events have been processed.
521 CallAfter( [=]()
522 {
523 m_workbookModified = false;
524 } );
525
526 return true;
527 }
528
529 return false;
530}
531
532
533bool SIMULATOR_FRAME::SaveWorkbook( const wxString& aPath )
534{
535 if( m_ui->SaveWorkbook( aPath ) )
536 {
537 m_workbookModified = false;
538 UpdateTitle();
539
540 return true;
541 }
542
543 return false;
544}
545
546
548{
550}
551
552
554{
555 SIM_TAB* simTab = m_ui->GetCurrentSimTab();
556 DIALOG_SIM_COMMAND dlg( this, m_circuitModel, m_simulator->Settings() );
557 wxString errors;
558 WX_STRING_REPORTER reporter( &errors );
559
560 if( !simTab )
561 return false;
562
563 if( !m_circuitModel->ReadSchematicAndLibraries( NETLIST_EXPORTER_SPICE::OPTION_DEFAULT_FLAGS,
564 reporter ) )
565 {
566 DisplayErrorMessage( this, _( "Errors during netlist generation.\n\n" )
567 + errors );
568 }
569
570 dlg.SetSimCommand( simTab->GetSimCommand() );
571 dlg.SetSimOptions( simTab->GetSimOptions() );
572 dlg.SetPlotSettings( simTab );
573
574 if( dlg.ShowModal() == wxID_OK )
575 {
576 simTab->SetSimCommand( dlg.GetSimCommand() );
577 dlg.ApplySettings( simTab );
579 OnModify();
580 return true;
581 }
582
583 return false;
584}
585
586
587bool SIMULATOR_FRAME::canCloseWindow( wxCloseEvent& aEvent )
588{
590 {
591 wxFileName filename = m_simulator->Settings()->GetWorkbookFilename();
592
593 if( filename.GetName().IsEmpty() )
594 {
595 if( Prj().GetProjectName().IsEmpty() )
596 filename.SetFullName( wxT( "noname.wbk" ) );
597 else
598 filename.SetFullName( Prj().GetProjectName() + wxT( ".wbk" ) );
599 }
600
601 return HandleUnsavedChanges( this, _( "Save changes to workbook?" ),
602 [&]() -> bool
603 {
604 return SaveWorkbook( Prj().AbsolutePath( filename.GetFullName() ) );
605 } );
606 }
607
608 return true;
609}
610
611
613{
614 if( m_simulator->IsRunning() )
615 m_simulator->Stop();
616
617 // Prevent memory leak on exit by deleting all simulation vectors
618 m_simulator->Clean();
619
620 // Cancel a running simProbe or simTune tool
622
623 SaveSettings( config() );
624
625 m_simulator->Settings() = nullptr;
626
627 Destroy();
628}
629
630
632{
634
636 wxASSERT( mgr );
637
638 auto showGridCondition =
639 [this]( const SELECTION& aSel )
640 {
641 SIM_PLOT_TAB* plotTab = dynamic_cast<SIM_PLOT_TAB*>( GetCurrentSimTab() );
642 return plotTab && plotTab->IsGridShown();
643 };
644
645 auto showLegendCondition =
646 [this]( const SELECTION& aSel )
647 {
648 SIM_PLOT_TAB* plotTab = dynamic_cast<SIM_PLOT_TAB*>( GetCurrentSimTab() );
649 return plotTab && plotTab->IsLegendShown();
650 };
651
652 auto showDottedCondition =
653 [this]( const SELECTION& aSel )
654 {
655 SIM_PLOT_TAB* plotTab = dynamic_cast<SIM_PLOT_TAB*>( GetCurrentSimTab() );
656 return plotTab && plotTab->GetDottedSecondary();
657 };
658
659 auto darkModePlotCondition =
660 [this]( const SELECTION& aSel )
661 {
662 return m_ui->DarkModePlots();
663 };
664
665 auto simRunning =
666 [this]( const SELECTION& aSel )
667 {
668 return m_simulator && m_simulator->IsRunning();
669 };
670
671 auto simFinished =
672 [this]( const SELECTION& aSel )
673 {
674 return m_simFinished;
675 };
676
677 auto haveSim =
678 [this]( const SELECTION& aSel )
679 {
680 return GetCurrentSimTab() != nullptr;
681 };
682
683 auto havePlot =
684 [this]( const SELECTION& aSel )
685 {
686 return dynamic_cast<SIM_PLOT_TAB*>( GetCurrentSimTab() ) != nullptr;
687 };
688
689 auto haveZoomUndo =
690 [this]( const SELECTION& aSel )
691 {
692 SIM_PLOT_TAB* plotTab = dynamic_cast<SIM_PLOT_TAB*>( GetCurrentSimTab() );
693 return plotTab && plotTab->GetPlotWin()->UndoZoomStackSize() > 0;
694 };
695
696 auto haveZoomRedo =
697 [this]( const SELECTION& aSel )
698 {
699 SIM_PLOT_TAB* plotTab = dynamic_cast<SIM_PLOT_TAB*>( GetCurrentSimTab() );
700 return plotTab && plotTab->GetPlotWin()->RedoZoomStackSize() > 0;
701 };
702
703#define ENABLE( x ) ACTION_CONDITIONS().Enable( x )
704#define CHECK( x ) ACTION_CONDITIONS().Check( x )
705
709
712
713 mgr->SetConditions( ACTIONS::zoomUndo, ENABLE( haveZoomUndo ) );
714 mgr->SetConditions( ACTIONS::zoomRedo, ENABLE( haveZoomRedo ) );
715 mgr->SetConditions( EE_ACTIONS::toggleGrid, CHECK( showGridCondition ) );
716 mgr->SetConditions( EE_ACTIONS::toggleLegend, CHECK( showLegendCondition ) );
717 mgr->SetConditions( EE_ACTIONS::toggleDottedSecondary, CHECK( showDottedCondition ) );
718 mgr->SetConditions( EE_ACTIONS::toggleDarkModePlots, CHECK( darkModePlotCondition ) );
719
722 mgr->SetConditions( EE_ACTIONS::runSimulation, ENABLE( !simRunning ) );
723 mgr->SetConditions( EE_ACTIONS::stopSimulation, ENABLE( simRunning ) );
724 mgr->SetConditions( EE_ACTIONS::simProbe, ENABLE( simFinished ) );
725 mgr->SetConditions( EE_ACTIONS::simTune, ENABLE( simFinished ) );
727
728#undef CHECK
729#undef ENABLE
730}
731
732
733void SIMULATOR_FRAME::onSimStarted( wxCommandEvent& aEvent )
734{
735 SetCursor( wxCURSOR_ARROWWAIT );
736}
737
738
739void SIMULATOR_FRAME::onSimFinished( wxCommandEvent& aEvent )
740{
741 // Sometimes (for instance with a directive like wrdata my_file.csv "my_signal")
742 // the simulator is in idle state (simulation is finished), but still running, during
743 // the time the file is written. So gives a slice of time to fully finish the work:
744 if( m_simulator->IsRunning() )
745 {
746 int max_time = 40; // For a max timeout = 2s
747
748 do
749 {
750 wxMilliSleep( 50 );
751 wxYield();
752
753 if( max_time )
754 max_time--;
755
756 } while( max_time && m_simulator->IsRunning() );
757 }
758
759 // ensure the shown cursor is the default cursor, not the wxCURSOR_ARROWWAIT set when
760 // staring the simulator in onSimStarted:
761 SetCursor( wxNullCursor );
762
763 // Is a warning message useful if the simulatior is still running?
764 SCHEMATIC& schematic = m_schematicFrame->Schematic();
765 schematic.ClearOperatingPoints();
766
767 m_simFinished = true;
768
769 m_ui->OnSimRefresh( true );
770
773}
774
775
776void SIMULATOR_FRAME::onUpdateSim( wxCommandEvent& aEvent )
777{
778 static bool updateInProgress = false;
779
780 // skip update when events are triggered too often and previous call didn't end yet
781 if( updateInProgress )
782 return;
783
784 updateInProgress = true;
785
786 if( m_simulator->IsRunning() )
787 m_simulator->Stop();
788
789 std::unique_lock<std::mutex> simulatorLock( m_simulator->GetMutex(), std::try_to_lock );
790
791 if( simulatorLock.owns_lock() )
792 {
793 m_ui->OnSimUpdate();
794 m_simulator->Run();
795 }
796 else
797 {
798 DisplayErrorMessage( this, _( "Another simulation is already running." ) );
799 }
800
801 updateInProgress = false;
802}
803
804
805void SIMULATOR_FRAME::onSimReport( wxCommandEvent& aEvent )
806{
807 m_ui->OnSimReport( aEvent.GetString() );
808}
809
810
811void SIMULATOR_FRAME::onExit( wxCommandEvent& aEvent )
812{
813 if( aEvent.GetId() == wxID_EXIT )
814 Kiway().OnKiCadExit();
815
816 if( aEvent.GetId() == wxID_CLOSE )
817 Close( false );
818}
819
820
822{
824 m_workbookModified = true;
825 UpdateTitle();
826}
827
828
829wxDEFINE_EVENT( EVT_SIM_UPDATE, wxCommandEvent );
830wxDEFINE_EVENT( EVT_SIM_REPORT, wxCommandEvent );
831
832wxDEFINE_EVENT( EVT_SIM_STARTED, wxCommandEvent );
833wxDEFINE_EVENT( EVT_SIM_FINISHED, wxCommandEvent );
constexpr EDA_IU_SCALE unityScale
Definition: base_units.h:112
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:104
static TOOL_ACTION toggleGrid
Definition: actions.h:172
static TOOL_ACTION cancelInteractive
Definition: actions.h:63
static TOOL_ACTION zoomRedo
Definition: actions.h:129
static TOOL_ACTION zoomUndo
Definition: actions.h:128
Manage TOOL_ACTION objects.
void SetConditions(const TOOL_ACTION &aAction, const ACTION_CONDITIONS &aConditions)
Set the conditions the UI elements for activating a specific tool action should use for determining t...
Define the structure of a toolbar with buttons that invoke ACTIONs.
static const ADVANCED_CFG & GetCfg()
Get the singleton instance's config, which is shared by all consumers.
APP_SETTINGS_BASE is a settings class that should be derived for each standalone KiCad application.
Definition: app_settings.h:92
Handle actions that are shared between different applications.
void SetPlotSettings(const SIM_TAB *aSimTab)
void SetSimCommand(const wxString &aCommand)
void ApplySettings(SIM_TAB *aTab)
void SetSimOptions(int aOptions)
const wxString & GetSimCommand() const
virtual APP_SETTINGS_BASE * config() const
Returns the settings object used in SaveSettings(), and is overloaded in KICAD_MANAGER_FRAME.
void ShowChangedLanguage() override
Redraw the menus and what not in current language.
virtual void setupUIConditions()
Setup the UI conditions for the various actions and their controls in this frame.
virtual void OnModify()
Must be called after a model change in order to set the "modify" flag and do other frame-specific pro...
virtual void LoadSettings(APP_SETTINGS_BASE *aCfg)
Load common frame parameters from a configuration file.
void CommonSettingsChanged(bool aEnvVarsChanged, bool aTextVarsChanged) override
Notification event that some of the common (suite-wide) settings have changed.
virtual void SaveSettings(APP_SETTINGS_BASE *aCfg)
Save common frame parameters to a configuration data file.
virtual void Refresh(bool aEraseBackground=true, const wxRect *aRect=nullptr) override
static TOOL_ACTION simAnalysisProperties
Definition: ee_actions.h:280
static TOOL_ACTION openWorkbook
Definition: ee_actions.h:269
static TOOL_ACTION stopSimulation
Definition: ee_actions.h:282
static TOOL_ACTION toggleLegend
Definition: ee_actions.h:277
static TOOL_ACTION saveWorkbook
Definition: ee_actions.h:270
static TOOL_ACTION saveWorkbookAs
Definition: ee_actions.h:271
static TOOL_ACTION exportPlotAsCSV
Definition: ee_actions.h:273
static TOOL_ACTION simTune
Definition: ee_actions.h:276
static TOOL_ACTION toggleDarkModePlots
Definition: ee_actions.h:279
static TOOL_ACTION exportPlotAsPNG
Definition: ee_actions.h:272
static TOOL_ACTION showNetlist
Definition: ee_actions.h:284
static TOOL_ACTION simProbe
Definition: ee_actions.h:275
static TOOL_ACTION toggleDottedSecondary
Definition: ee_actions.h:278
static TOOL_ACTION runSimulation
Definition: ee_actions.h:281
static TOOL_ACTION newAnalysisTab
Definition: ee_actions.h:268
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
A wxFrame capable of the OpenProjectFiles function, meaning it can load a portion of a KiCad project.
Definition: kiway_player.h:67
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:279
void OnKiCadExit()
Definition: kiway.cpp:743
virtual KIWAY_PLAYER * Player(FRAME_T aFrameType, bool doCreate=true, wxTopLevelWindow *aParent=nullptr)
Return the KIWAY_PLAYER* given a FRAME_T.
Definition: kiway.cpp:432
Container for Ngspice simulator settings.
void SetCompatibilityMode(NGSPICE_COMPATIBILITY_MODE aMode)
A singleton reporter that reports to nowhere.
Definition: reporter.h:223
The backing store for a PROJECT, in JSON format.
Definition: project_file.h:70
virtual PROJECT_FILE & GetProjectFile() const
Definition: project.h:166
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:320
A pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:71
Holds all the data relating to one schematic.
Definition: schematic.h:75
void ClearOperatingPoints()
Clear operating points from a .op simulation.
Definition: schematic.h:224
SCH_DRAW_PANEL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
Schematic editor (Eeschema) main window.
void RefreshOperatingPointDisplay()
Refresh the display of any operaintg points.
void OnModify() override
Must be called after a schematic change in order to set the "modify" flag and update other data struc...
bool ReadyToNetlist(const wxString &aAnnotateMessage)
Check if we are ready to write a netlist file for the current schematic.
SCHEMATIC & Schematic() const
void RecalculateConnections(SCH_COMMIT *aCommit, SCH_CLEANUP_FLAGS aCleanupFlags)
Generate the connection data for the entire schematic hierarchy.
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
Schematic symbol object.
Definition: sch_symbol.h:109
static bool ShowAlways(const SELECTION &aSelection)
The default condition function (always returns true).
Handle actions for the various symbol editor and viewers.
The SIMULATOR_FRAME_UI holds the main user-interface for running simulations.
SIM_TAB * NewSimTab(const wxString &aSimCommand)
Create a new simulation tab for a given simulation type.
void SetUserDefinedSignals(const std::map< int, wxString > &aSignals)
void OnSimRefresh(bool aFinal)
SIM_TAB * GetSimTab(SIM_TYPE aType) const
std::vector< wxString > SimPlotVectors() const
std::vector< wxString > Signals() const
bool SaveWorkbook(const wxString &aPath)
Save plot, signal, cursor, measurement, etc.
SIM_TAB * GetCurrentSimTab() const
Return the currently opened plot panel (or NULL if there is none).
bool LoadWorkbook(const wxString &aPath)
Load plot, signal, cursor, measurement, etc.
bool DarkModePlots() const
const std::map< int, wxString > & UserDefinedSignals()
void AddTrace(const wxString &aName, SIM_TRACE_TYPE aType)
Add a new trace to the current plot.
void SaveSettings(EESCHEMA_SETTINGS *aCfg)
int GetSimTabIndex(SIM_TAB *aPlot) const
void OnSimReport(const wxString &aMsg)
void ApplyPreferences(const SIM_PREFERENCES &aPrefs)
Called when settings are changed via the common Preferences dialog.
void AddTuner(const SCH_SHEET_PATH &aSheetPath, SCH_SYMBOL *aSymbol)
Add a tuner for a symbol.
void LoadSettings(EESCHEMA_SETTINGS *aCfg)
The SIMULATOR_FRAME holds the main user-interface for running simulations.
SIM_TAB * GetCurrentSimTab() const
Return the current tab (or NULL if there is none).
void ShowChangedLanguage() override
bool canCloseWindow(wxCloseEvent &aEvent) override
void onSimFinished(wxCommandEvent &aEvent)
void CommonSettingsChanged(bool aEnvVarsChanged, bool aTextVarsChanged) override
Notification event that some of the common (suite-wide) settings have changed.
bool LoadSimulator(const wxString &aSimCommand, unsigned aSimOptions)
Check and load the current netlist into the simulator.
void onSimReport(wxCommandEvent &aEvent)
wxString GetCurrentSimCommand() const
void onExit(wxCommandEvent &event)
std::shared_ptr< SPICE_SIMULATOR > m_simulator
SIM_TYPE GetCurrentSimType() const
void setupUIConditions() override
Setup the UI conditions for the various actions and their controls in this frame.
void SaveSettings(APP_SETTINGS_BASE *aCfg) override
Save common frame parameters to a configuration data file.
void AddCurrentTrace(const wxString &aDeviceName)
Add a current trace for a given device to the current plot.
void OnModify() override
Must be called after a model change in order to set the "modify" flag and do other frame-specific pro...
bool SaveWorkbook(const wxString &aPath)
Save plot, signal, cursor, measurement, etc.
const std::vector< wxString > Signals()
void doCloseWindow() override
const std::vector< wxString > SimPlotVectors()
void AddVoltageTrace(const wxString &aNetName)
Add a voltage trace for a given net to the current plot.
void ToggleDarkModePlots()
Toggle dark-mode of the plot tabs.
SIM_THREAD_REPORTER * m_reporter
void AddTuner(const SCH_SHEET_PATH &aSheetPath, SCH_SYMBOL *aSymbol)
Add a tuner for a symbol.
void onSimStarted(wxCommandEvent &aEvent)
void LoadSettings(APP_SETTINGS_BASE *aCfg) override
Load common frame parameters from a configuration file.
std::shared_ptr< SPICE_CIRCUIT_MODEL > m_circuitModel
SIM_TAB * NewSimTab(const wxString &aSimCommand)
Create a new plot tab for a given simulation type.
bool EditAnalysis()
Shows a dialog for editing the current tab's simulation command, or creating a new tab with a differe...
int GetCurrentOptions() const
bool LoadWorkbook(const wxString &aPath)
Load plot, signal, cursor, measurement, etc.
WINDOW_SETTINGS * GetWindowSettings(APP_SETTINGS_BASE *aCfg) override
Return a pointer to the window settings for this frame.
void onUpdateSim(wxCommandEvent &aEvent)
void UpdateTitle()
Set the main window title bar text.
const std::map< int, wxString > & UserDefinedSignals()
void SetUserDefinedSignals(const std::map< int, wxString > &aSignals)
void ReloadSimulator(const wxString &aSimCommand, unsigned aSimOptions)
Re-send the current command and settings to the simulator.
SCH_EDIT_FRAME * m_schematicFrame
SIMULATOR_FRAME_UI * m_ui
Interface to receive simulation updates from SPICE_SIMULATOR class.
static std::shared_ptr< SPICE_SIMULATOR > CreateInstance(const std::string &aName)
mpWindow * GetPlotWin() const
Definition: sim_plot_tab.h:350
bool IsGridShown() const
Definition: sim_plot_tab.h:285
bool GetDottedSecondary() const
Toggle cursor for a particular trace.
Definition: sim_plot_tab.h:329
bool IsLegendShown() const
Definition: sim_plot_tab.h:299
int GetSimOptions() const
Definition: sim_tab.h:55
const wxString & GetSpicePlotName() const
Definition: sim_tab.h:61
void SetLastSchTextSimCommand(const wxString &aCmd)
Definition: sim_tab.h:59
void SetSimCommand(const wxString &aSimCommand)
Definition: sim_tab.h:53
const wxString & GetSimCommand() const
Definition: sim_tab.h:52
wxString GetLastSchTextSimCommand() const
Definition: sim_tab.h:58
void SetSpicePlotName(const wxString &aPlotName)
Definition: sim_tab.h:62
SIMULATOR_FRAME * m_parent
SIM_THREAD_REPORTER(SIMULATOR_FRAME *aParent)
REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) override
Report a string with a given severity.
bool HasMessage() const override
Returns true if the reporter client is non-empty.
void OnSimStateChange(SIMULATOR *aObject, SIM_STATE aNewState) override
static SIM_TYPE CommandToSimType(const wxString &aCmd)
Return simulation type basing on a simulation command directive.
wxString GetWorkbookFilename() const
TOOL_MANAGER * m_toolManager
Definition: tools_holder.h:167
TOOL_DISPATCHER * m_toolDispatcher
Definition: tools_holder.h:169
TOOL_MANAGER * GetToolManager() const
Return the MVC controller.
Definition: tools_holder.h:55
virtual void DispatchWxEvent(wxEvent &aEvent)
Process wxEvents (mostly UI events), translate them to TOOL_EVENTs, and make tools handle those.
Master controller class:
Definition: tool_manager.h:57
ACTION_MANAGER * GetActionManager() const
Definition: tool_manager.h:289
APP_SETTINGS_BASE * GetSettings() const
Definition: tool_manager.h:387
bool PostAction(const std::string &aActionName, T aParam)
Run the specified action after the current action (coroutine) ends.
Definition: tool_manager.h:230
void RegisterTool(TOOL_BASE *aTool)
Add a tool to the manager set and sets it up.
void SetEnvironment(EDA_ITEM *aModel, KIGFX::VIEW *aView, KIGFX::VIEW_CONTROLS *aViewControls, APP_SETTINGS_BASE *aSettings, TOOLS_HOLDER *aFrame)
Set the work environment (model, view, view controls and the parent window).
void InitTools()
Initializes all registered tools.
A modified version of the wxInfoBar class that allows us to:
Definition: wx_infobar.h:75
A wrapper for reporting to a wxString object.
Definition: reporter.h:164
int RedoZoomStackSize() const
Definition: mathplot.h:1267
int UndoZoomStackSize() const
Definition: mathplot.h:1266
bool IsOK(wxWindow *aParent, const wxString &aMessage)
Display a yes/no dialog with aMessage and returns the user response.
Definition: confirm.cpp:360
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:305
This file is part of the common library.
#define CHECK(x)
#define ENABLE(x)
#define _(s)
@ FRAME_SCH
Definition: frame_type.h:34
@ FRAME_SIMULATOR
Definition: frame_type.h:38
KIWAY Kiway
see class PGM_BASE
SEVERITY
@ RPT_SEVERITY_UNDEFINED
@ GLOBAL_CLEANUP
@ SPT_VOLTAGE
Definition: sim_types.h:52
@ SPT_CURRENT
Definition: sim_types.h:53
SIM_TYPE
< Possible simulation types
Definition: sim_types.h:32
@ ST_TRAN
Definition: sim_types.h:42
wxDEFINE_EVENT(EVT_SIM_UPDATE, wxCommandEvent)
@ SIM_IDLE
@ SIM_RUNNING
Stores the common settings that are saved and loaded for each window / frame.
Definition: app_settings.h:74
Definition of file extensions used in Kicad.