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
64// Reporter is stored by pointer in KIBIS, so keep this here to avoid crashes
65static wxString s_errors;
66static WX_STRING_REPORTER s_reporter( &s_errors );
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( SIMULATOR* aObject, SIM_STATE aNewState ) override
91 {
92 wxCommandEvent* event = nullptr;
93
94 switch( aNewState )
95 {
96 case SIM_IDLE: event = new wxCommandEvent( EVT_SIM_FINISHED ); break;
97 case SIM_RUNNING: event = new wxCommandEvent( EVT_SIM_STARTED ); break;
98 default: wxFAIL; return;
99 }
100
101 wxQueueEvent( m_parent, event );
102 }
103
104private:
106};
107
108
109BEGIN_EVENT_TABLE( SIMULATOR_FRAME, KIWAY_PLAYER )
110 EVT_MENU( wxID_EXIT, SIMULATOR_FRAME::onExit )
111 EVT_MENU( wxID_CLOSE, SIMULATOR_FRAME::onExit )
112END_EVENT_TABLE()
113
114
115SIMULATOR_FRAME::SIMULATOR_FRAME( KIWAY* aKiway, wxWindow* aParent ) :
116 KIWAY_PLAYER( aKiway, aParent, FRAME_SIMULATOR, _( "Simulator" ), wxDefaultPosition,
117 wxDefaultSize, wxDEFAULT_FRAME_STYLE, wxT( "simulator" ), unityScale ),
118 m_schematicFrame( nullptr ),
119 m_toolBar( nullptr ),
120 m_ui( nullptr ),
121 m_simFinished( false ),
122 m_workbookModified( false )
123{
124 m_schematicFrame = (SCH_EDIT_FRAME*) Kiway().Player( FRAME_SCH, false );
125 wxASSERT( m_schematicFrame );
126
127 // Give an icon
128 wxIcon icon;
129 icon.CopyFromBitmap( KiBitmap( BITMAPS::simulator ) );
130 SetIcon( icon );
131
132 wxBoxSizer* mainSizer = new wxBoxSizer( wxVERTICAL );
133 SetSizer( mainSizer );
134
135 m_infoBar = new WX_INFOBAR( this );
136 mainSizer->Add( m_infoBar, 0, wxEXPAND, 0 );
137
138 m_toolBar = new ACTION_TOOLBAR( this, wxID_ANY, wxDefaultPosition, wxDefaultSize,
139 wxAUI_TB_DEFAULT_STYLE|wxAUI_TB_HORZ_LAYOUT|wxAUI_TB_PLAIN_BACKGROUND );
140 m_toolBar->Realize();
141 mainSizer->Add( m_toolBar, 0, wxEXPAND, 5 );
142
143 m_ui = new SIMULATOR_FRAME_UI( this, m_schematicFrame );
144 mainSizer->Add( m_ui, 1, wxEXPAND, 5 );
145
146 m_simulator = SIMULATOR::CreateInstance( "ngspice" );
147 wxASSERT( m_simulator );
148
149 LoadSettings( config() );
150
151 NGSPICE_SETTINGS* settings = dynamic_cast<NGSPICE_SETTINGS*>( m_simulator->Settings().get() );
152
153 wxCHECK2( settings, /* do nothing in release builds*/ );
154
155 if( settings && settings->GetWorkbookFilename().IsEmpty() )
157
158 m_simulator->Init();
159
160 m_reporter = new SIM_THREAD_REPORTER( this );
161 m_simulator->SetReporter( m_reporter );
162
163 m_circuitModel = std::make_shared<SPICE_CIRCUIT_MODEL>( &m_schematicFrame->Schematic() );
164
165 setupTools();
166 setupUIConditions();
167
168 ReCreateHToolbar();
169 ReCreateMenuBar();
170
171 Bind( wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler( SIMULATOR_FRAME::onExit ), this,
172 wxID_EXIT );
173
174 Bind( EVT_SIM_UPDATE, &SIMULATOR_FRAME::onUpdateSim, this );
175 Bind( EVT_SIM_REPORT, &SIMULATOR_FRAME::onSimReport, this );
176 Bind( EVT_SIM_STARTED, &SIMULATOR_FRAME::onSimStarted, this );
177 Bind( EVT_SIM_FINISHED, &SIMULATOR_FRAME::onSimFinished, this );
178
179 // Ensure new items are taken in account by sizers:
180 Layout();
181
182 // resize the subwindows size. At least on Windows, calling wxSafeYield before
183 // resizing the subwindows forces the wxSplitWindows size events automatically generated
184 // by wxWidgets to be executed before our resize code.
185 // Otherwise, the changes made by setSubWindowsSashSize are overwritten by one these
186 // events
187 wxSafeYield();
188 m_ui->SetSubWindowsSashSize();
189
190 // Ensure the window is on top
191 Raise();
192
193 m_ui->InitWorkbook();
194 UpdateTitle();
195}
196
197
199{
200 NULL_REPORTER devnull;
201
202 m_simulator->Attach( nullptr, wxEmptyString, 0, wxEmptyString, devnull );
203 m_simulator->SetReporter( nullptr );
204 delete m_reporter;
205}
206
207
209{
210 // Create the manager
212 m_toolManager->SetEnvironment( nullptr, nullptr, nullptr, config(), this );
213
215
216 // Attach the events to the tool dispatcher
218 Bind( wxEVT_CHAR_HOOK, &TOOL_DISPATCHER::DispatchWxEvent, m_toolDispatcher );
219
220 // Register tools
224}
225
226
228{
230
231 UpdateTitle();
232
234}
235
236
238{
239 EESCHEMA_SETTINGS* cfg = dynamic_cast<EESCHEMA_SETTINGS*>( aCfg );
240 wxASSERT( cfg );
241
242 if( cfg )
243 {
245 m_ui->LoadSettings( cfg );
246 }
247
249
250 NGSPICE* currentSim = dynamic_cast<NGSPICE*>( m_simulator.get() );
251
252 if( currentSim )
253 m_simulator->Settings() = project.m_SchematicSettings->m_NgspiceSettings;
254}
255
256
258{
259 EESCHEMA_SETTINGS* cfg = dynamic_cast<EESCHEMA_SETTINGS*>( aCfg );
260 wxASSERT( cfg );
261
262 if( cfg )
263 {
265 m_ui->SaveSettings( cfg );
266 }
267
269
270 if( project.m_SchematicSettings )
271 {
272 bool modified = project.m_SchematicSettings->m_NgspiceSettings->SaveToFile();
273
274 if( m_schematicFrame && modified )
276 }
277}
278
279
280void SIMULATOR_FRAME::CommonSettingsChanged( bool aEnvVarsChanged, bool aTextVarsChanged )
281{
282 KIWAY_PLAYER::CommonSettingsChanged( aEnvVarsChanged, aTextVarsChanged );
283
284 auto* cfg = dynamic_cast<EESCHEMA_SETTINGS*>( m_toolManager->GetSettings() );
285 wxASSERT( cfg != nullptr );
286 m_ui->ApplyPreferences( cfg->m_Simulator.preferences );
287}
288
289
291{
292 EESCHEMA_SETTINGS* cfg = dynamic_cast<EESCHEMA_SETTINGS*>( aCfg );
293 wxASSERT( cfg );
294
295 return cfg ? &cfg->m_Simulator.window : nullptr;
296}
297
298
300{
301 if( m_ui->GetCurrentSimTab() )
303 else
304 return m_circuitModel->GetSchTextSimCommand();
305}
306
307
309{
311}
312
313
315{
316 if( SIM_TAB* simTab = m_ui->GetCurrentSimTab() )
317 return simTab->GetSimOptions();
318 else
320}
321
322
324{
325 bool unsaved = true;
326 bool readOnly = false;
327 wxString title;
328 wxFileName filename = Prj().AbsolutePath( m_simulator->Settings()->GetWorkbookFilename() );
329
330 if( filename.IsOk() && filename.FileExists() )
331 {
332 unsaved = false;
333 readOnly = !filename.IsFileWritable();
334 }
335
337 title = wxT( "*" ) + filename.GetName();
338 else
339 title = filename.GetName();
340
341 if( readOnly )
342 title += wxS( " " ) + _( "[Read Only]" );
343
344 if( unsaved )
345 title += wxS( " " ) + _( "[Unsaved]" );
346
347 title += wxT( " \u2014 " ) + _( "Spice Simulator" );
348
349 SetTitle( title );
350}
351
352
353
354bool SIMULATOR_FRAME::LoadSimulator( const wxString& aSimCommand, unsigned aSimOptions )
355{
356 s_errors.clear();
357
358 if( !m_schematicFrame->ReadyToNetlist( _( "Simulator requires a fully annotated schematic." ) ) )
359 return false;
360
361 // If we are using the new connectivity, make sure that we do a full-rebuild
362 if( ADVANCED_CFG::GetCfg().m_IncrementalConnectivity )
364
365 if( !m_simulator->Attach( m_circuitModel, aSimCommand, aSimOptions, Prj().GetProjectPath(),
366 s_reporter ) )
367 {
368 DisplayErrorMessage( this, _( "Errors during netlist generation.\n\n" ) + s_errors );
369 return false;
370 }
371
372 return true;
373}
374
375
376void SIMULATOR_FRAME::ReloadSimulator( const wxString& aSimCommand, unsigned aSimOptions )
377{
378 if( !m_simulator->Attach( m_circuitModel, aSimCommand, aSimOptions, Prj().GetProjectPath(),
379 s_reporter ) )
380 {
381 DisplayErrorMessage( this, _( "Errors during netlist generation.\n\n" ) + s_errors );
382 }
383}
384
385
387{
388 SIM_TAB* simTab = m_ui->GetCurrentSimTab();
389
390 if( !simTab )
391 return;
392
393 if( simTab->GetSimCommand().Upper().StartsWith( wxT( "FFT" ) )
394 || simTab->GetSimCommand().Upper().Contains( wxT( "\nFFT" ) ) )
395 {
396 wxString tranSpicePlot;
397
398 if( SIM_TAB* tranPlotTab = m_ui->GetSimTab( ST_TRAN ) )
399 tranSpicePlot = tranPlotTab->GetSpicePlotName();
400
401 if( tranSpicePlot.IsEmpty() )
402 {
403 DisplayErrorMessage( this, _( "You must run a TRAN simulation first; its results"
404 "will be used for the fast Fourier transform." ) );
405 }
406 else
407 {
408 m_simulator->Command( "setplot " + tranSpicePlot.ToStdString() );
409
410 wxArrayString commands = wxSplit( simTab->GetSimCommand(), '\n' );
411
412 for( const wxString& command : commands )
413 {
414 wxBusyCursor wait;
415 m_simulator->Command( command.ToStdString() );
416 }
417
418 simTab->SetSpicePlotName( m_simulator->CurrentPlotName() );
419 m_ui->OnSimRefresh( true );
420
421#if 0
422 m_simulator->Command( "setplot" ); // Print available plots to console
423 m_simulator->Command( "display" ); // Print vectors in current plot to console
424#endif
425 }
426
427 return;
428 }
429 else
430 {
431 if( m_ui->GetSimTabIndex( simTab ) == 0
432 && m_circuitModel->GetSchTextSimCommand() != simTab->GetLastSchTextSimCommand() )
433 {
434 if( simTab->GetLastSchTextSimCommand().IsEmpty()
435 || IsOK( this, _( "Schematic sheet simulation command directive has changed. "
436 "Do you wish to update the Simulation Command?" ) ) )
437 {
438 simTab->SetSimCommand( m_circuitModel->GetSchTextSimCommand() );
439 simTab->SetLastSchTextSimCommand( simTab->GetSimCommand() );
440 OnModify();
441 }
442 }
443 }
444
445 if( !LoadSimulator( simTab->GetSimCommand(), simTab->GetSimOptions() ) )
446 return;
447
448 std::unique_lock<std::mutex> simulatorLock( m_simulator->GetMutex(), std::try_to_lock );
449
450 if( simulatorLock.owns_lock() )
451 {
452 m_ui->OnSimUpdate();
453 m_simulator->Run();
454
455 // Netlist from schematic may have changed; update signals list, measurements list,
456 // etc.
458 }
459 else
460 {
461 DisplayErrorMessage( this, _( "Another simulation is already running." ) );
462 }
463}
464
465
466SIM_TAB* SIMULATOR_FRAME::NewSimTab( const wxString& aSimCommand )
467{
468 return m_ui->NewSimTab( aSimCommand );
469}
470
471
472const std::vector<wxString> SIMULATOR_FRAME::SimPlotVectors()
473{
474 return m_ui->SimPlotVectors();
475}
476
477
478const std::vector<wxString> SIMULATOR_FRAME::Signals()
479{
480 return m_ui->Signals();
481}
482
483
484const std::map<int, wxString>& SIMULATOR_FRAME::UserDefinedSignals()
485{
486 return m_ui->UserDefinedSignals();
487}
488
489
490void SIMULATOR_FRAME::SetUserDefinedSignals( const std::map<int, wxString>& aSignals )
491{
492 m_ui->SetUserDefinedSignals( aSignals );
493}
494
495
496void SIMULATOR_FRAME::AddVoltageTrace( const wxString& aNetName )
497{
498 m_ui->AddTrace( aNetName, SPT_VOLTAGE );
499}
500
501
502void SIMULATOR_FRAME::AddCurrentTrace( const wxString& aDeviceName )
503{
504 m_ui->AddTrace( aDeviceName, SPT_CURRENT );
505}
506
507
508void SIMULATOR_FRAME::AddTuner( const SCH_SHEET_PATH& aSheetPath, SCH_SYMBOL* aSymbol )
509{
510 m_ui->AddTuner( aSheetPath, aSymbol );
511}
512
513
515{
516 return m_ui->GetCurrentSimTab();
517}
518
519
520bool SIMULATOR_FRAME::LoadWorkbook( const wxString& aPath )
521{
522 if( m_ui->LoadWorkbook( aPath ) )
523 {
524 UpdateTitle();
525
526 // Successfully loading a workbook does not count as modifying it. Clear the modified
527 // flag after all the EVT_WORKBOOK_MODIFIED events have been processed.
528 CallAfter( [this]()
529 {
530 m_workbookModified = false;
531 } );
532
533 return true;
534 }
535
536 return false;
537}
538
539
540bool SIMULATOR_FRAME::SaveWorkbook( const wxString& aPath )
541{
542 if( m_ui->SaveWorkbook( aPath ) )
543 {
544 m_workbookModified = false;
545 UpdateTitle();
546
547 return true;
548 }
549
550 return false;
551}
552
553
555{
557}
558
559
561{
562 SIM_TAB* simTab = m_ui->GetCurrentSimTab();
563 DIALOG_SIM_COMMAND dlg( this, m_circuitModel, m_simulator->Settings() );
564
565 s_errors.clear();
566
567 if( !simTab )
568 return false;
569
570 if( !m_circuitModel->ReadSchematicAndLibraries( NETLIST_EXPORTER_SPICE::OPTION_DEFAULT_FLAGS,
571 s_reporter ) )
572 {
573 DisplayErrorMessage( this, _( "Errors during netlist generation.\n\n" )
574 + s_errors );
575 }
576
577 dlg.SetSimCommand( simTab->GetSimCommand() );
578 dlg.SetSimOptions( simTab->GetSimOptions() );
579 dlg.SetPlotSettings( simTab );
580
581 if( dlg.ShowModal() == wxID_OK )
582 {
583 simTab->SetSimCommand( dlg.GetSimCommand() );
584 dlg.ApplySettings( simTab );
586 OnModify();
587 return true;
588 }
589
590 return false;
591}
592
593
594bool SIMULATOR_FRAME::canCloseWindow( wxCloseEvent& aEvent )
595{
597 {
598 wxFileName filename = m_simulator->Settings()->GetWorkbookFilename();
599
600 if( filename.GetName().IsEmpty() )
601 {
602 if( Prj().GetProjectName().IsEmpty() )
603 filename.SetFullName( wxT( "noname.wbk" ) );
604 else
605 filename.SetFullName( Prj().GetProjectName() + wxT( ".wbk" ) );
606 }
607
608 return HandleUnsavedChanges( this, _( "Save changes to workbook?" ),
609 [&]() -> bool
610 {
611 return SaveWorkbook( Prj().AbsolutePath( filename.GetFullName() ) );
612 } );
613 }
614
615 return true;
616}
617
618
620{
621 if( m_simulator->IsRunning() )
622 m_simulator->Stop();
623
624 // Prevent memory leak on exit by deleting all simulation vectors
625 m_simulator->Clean();
626
627 // Cancel a running simProbe or simTune tool
629
630 SaveSettings( config() );
631
632 m_simulator->Settings() = nullptr;
633
634 Destroy();
635}
636
637
639{
641
643 wxASSERT( mgr );
644
645 auto showGridCondition =
646 [this]( const SELECTION& aSel )
647 {
648 SIM_PLOT_TAB* plotTab = dynamic_cast<SIM_PLOT_TAB*>( GetCurrentSimTab() );
649 return plotTab && plotTab->IsGridShown();
650 };
651
652 auto showLegendCondition =
653 [this]( const SELECTION& aSel )
654 {
655 SIM_PLOT_TAB* plotTab = dynamic_cast<SIM_PLOT_TAB*>( GetCurrentSimTab() );
656 return plotTab && plotTab->IsLegendShown();
657 };
658
659 auto showDottedCondition =
660 [this]( const SELECTION& aSel )
661 {
662 SIM_PLOT_TAB* plotTab = dynamic_cast<SIM_PLOT_TAB*>( GetCurrentSimTab() );
663 return plotTab && plotTab->GetDottedSecondary();
664 };
665
666 auto darkModePlotCondition =
667 [this]( const SELECTION& aSel )
668 {
669 return m_ui->DarkModePlots();
670 };
671
672 auto simRunning =
673 [this]( const SELECTION& aSel )
674 {
675 return m_simulator && m_simulator->IsRunning();
676 };
677
678 auto simFinished =
679 [this]( const SELECTION& aSel )
680 {
681 return m_simFinished;
682 };
683
684 auto haveSim =
685 [this]( const SELECTION& aSel )
686 {
687 return GetCurrentSimTab() != nullptr;
688 };
689
690 auto havePlot =
691 [this]( const SELECTION& aSel )
692 {
693 return dynamic_cast<SIM_PLOT_TAB*>( GetCurrentSimTab() ) != nullptr;
694 };
695
696 auto haveZoomUndo =
697 [this]( const SELECTION& aSel )
698 {
699 SIM_PLOT_TAB* plotTab = dynamic_cast<SIM_PLOT_TAB*>( GetCurrentSimTab() );
700 return plotTab && plotTab->GetPlotWin()->UndoZoomStackSize() > 0;
701 };
702
703 auto haveZoomRedo =
704 [this]( const SELECTION& aSel )
705 {
706 SIM_PLOT_TAB* plotTab = dynamic_cast<SIM_PLOT_TAB*>( GetCurrentSimTab() );
707 return plotTab && plotTab->GetPlotWin()->RedoZoomStackSize() > 0;
708 };
709
710#define ENABLE( x ) ACTION_CONDITIONS().Enable( x )
711#define CHECK( x ) ACTION_CONDITIONS().Check( x )
712
716
721
722 mgr->SetConditions( ACTIONS::zoomUndo, ENABLE( haveZoomUndo ) );
723 mgr->SetConditions( ACTIONS::zoomRedo, ENABLE( haveZoomRedo ) );
724 mgr->SetConditions( EE_ACTIONS::toggleGrid, CHECK( showGridCondition ) );
725 mgr->SetConditions( EE_ACTIONS::toggleLegend, CHECK( showLegendCondition ) );
726 mgr->SetConditions( EE_ACTIONS::toggleDottedSecondary, CHECK( showDottedCondition ) );
727 mgr->SetConditions( EE_ACTIONS::toggleDarkModePlots, CHECK( darkModePlotCondition ) );
728
731 mgr->SetConditions( EE_ACTIONS::runSimulation, ENABLE( !simRunning ) );
732 mgr->SetConditions( EE_ACTIONS::stopSimulation, ENABLE( simRunning ) );
733 mgr->SetConditions( EE_ACTIONS::simProbe, ENABLE( simFinished ) );
734 mgr->SetConditions( EE_ACTIONS::simTune, ENABLE( simFinished ) );
736
737#undef CHECK
738#undef ENABLE
739}
740
741
742void SIMULATOR_FRAME::onSimStarted( wxCommandEvent& aEvent )
743{
744 SetCursor( wxCURSOR_ARROWWAIT );
745}
746
747
748void SIMULATOR_FRAME::onSimFinished( wxCommandEvent& aEvent )
749{
750 // Sometimes (for instance with a directive like wrdata my_file.csv "my_signal")
751 // the simulator is in idle state (simulation is finished), but still running, during
752 // the time the file is written. So gives a slice of time to fully finish the work:
753 if( m_simulator->IsRunning() )
754 {
755 int max_time = 40; // For a max timeout = 2s
756
757 do
758 {
759 wxMilliSleep( 50 );
760 wxYield();
761
762 if( max_time )
763 max_time--;
764
765 } while( max_time && m_simulator->IsRunning() );
766 }
767
768 // ensure the shown cursor is the default cursor, not the wxCURSOR_ARROWWAIT set when
769 // staring the simulator in onSimStarted:
770 SetCursor( wxNullCursor );
771
772 // Is a warning message useful if the simulatior is still running?
773 SCHEMATIC& schematic = m_schematicFrame->Schematic();
774 schematic.ClearOperatingPoints();
775
776 m_simFinished = true;
777
778 m_ui->OnSimRefresh( true );
779
782}
783
784
785void SIMULATOR_FRAME::onUpdateSim( wxCommandEvent& aEvent )
786{
787 static bool updateInProgress = false;
788
789 // skip update when events are triggered too often and previous call didn't end yet
790 if( updateInProgress )
791 return;
792
793 updateInProgress = true;
794
795 if( m_simulator->IsRunning() )
796 m_simulator->Stop();
797
798 std::unique_lock<std::mutex> simulatorLock( m_simulator->GetMutex(), std::try_to_lock );
799
800 if( simulatorLock.owns_lock() )
801 {
802 m_ui->OnSimUpdate();
803 m_simulator->Run();
804 }
805 else
806 {
807 DisplayErrorMessage( this, _( "Another simulation is already running." ) );
808 }
809
810 updateInProgress = false;
811}
812
813
814void SIMULATOR_FRAME::onSimReport( wxCommandEvent& aEvent )
815{
816 m_ui->OnSimReport( aEvent.GetString() );
817}
818
819
820void SIMULATOR_FRAME::onExit( wxCommandEvent& aEvent )
821{
822 if( aEvent.GetId() == wxID_EXIT )
823 Kiway().OnKiCadExit();
824
825 if( aEvent.GetId() == wxID_CLOSE )
826 Close( false );
827}
828
829
831{
833 m_workbookModified = true;
834 UpdateTitle();
835}
836
837
838wxDEFINE_EVENT( EVT_SIM_UPDATE, wxCommandEvent );
839wxDEFINE_EVENT( EVT_SIM_REPORT, wxCommandEvent );
840
841wxDEFINE_EVENT( EVT_SIM_STARTED, wxCommandEvent );
842wxDEFINE_EVENT( EVT_SIM_FINISHED, wxCommandEvent );
constexpr EDA_IU_SCALE unityScale
Definition: base_units.h:111
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:176
static TOOL_ACTION cancelInteractive
Definition: actions.h:64
static TOOL_ACTION zoomRedo
Definition: actions.h:130
static TOOL_ACTION zoomUndo
Definition: actions.h:129
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:284
static TOOL_ACTION openWorkbook
Definition: ee_actions.h:271
static TOOL_ACTION stopSimulation
Definition: ee_actions.h:286
static TOOL_ACTION toggleLegend
Definition: ee_actions.h:281
static TOOL_ACTION saveWorkbook
Definition: ee_actions.h:272
static TOOL_ACTION saveWorkbookAs
Definition: ee_actions.h:273
static TOOL_ACTION exportPlotAsCSV
Definition: ee_actions.h:275
static TOOL_ACTION simTune
Definition: ee_actions.h:280
static TOOL_ACTION toggleDarkModePlots
Definition: ee_actions.h:283
static TOOL_ACTION exportPlotAsPNG
Definition: ee_actions.h:274
static TOOL_ACTION showNetlist
Definition: ee_actions.h:288
static TOOL_ACTION simProbe
Definition: ee_actions.h:279
static TOOL_ACTION toggleDottedSecondary
Definition: ee_actions.h:282
static TOOL_ACTION exportPlotToSchematic
Definition: ee_actions.h:277
static TOOL_ACTION runSimulation
Definition: ee_actions.h:285
static TOOL_ACTION newAnalysisTab
Definition: ee_actions.h:270
static TOOL_ACTION exportPlotToClipboard
Definition: ee_actions.h:276
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:55
A wxFrame capable of the OpenProjectFiles function, meaning it can load a portion of a KiCad project.
Definition: kiway_player.h:65
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:717
virtual KIWAY_PLAYER * Player(FRAME_T aFrameType, bool doCreate=true, wxTopLevelWindow *aParent=nullptr)
Return the KIWAY_PLAYER* given a FRAME_T.
Definition: kiway.cpp:406
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:231
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:105
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:62
ACTION_MANAGER * GetActionManager() const
Definition: tool_manager.h:302
APP_SETTINGS_BASE * GetSettings() const
Definition: tool_manager.h:400
bool PostAction(const std::string &aActionName, T aParam)
Run the specified action after the current action (coroutine) ends.
Definition: tool_manager.h:235
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:1269
int UndoZoomStackSize() const
Definition: mathplot.h:1268
bool IsOK(wxWindow *aParent, const wxString &aMessage)
Display a yes/no dialog with aMessage and returns the user response.
Definition: confirm.cpp:250
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:130
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition: confirm.cpp:195
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
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)
static wxString s_errors
@ SIM_IDLE
@ SIM_RUNNING
KIWAY Kiway(KFCTL_STANDALONE)
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.