KiCad PCB EDA Suite
Loading...
Searching...
No Matches
eeschema.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) 2004 Jean-Pierre Charras, [email protected]
5 * Copyright (C) 2008 Wayne Stambaugh <[email protected]>
6 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22#include <algorithm>
23
24#include <api/api_handler_sch.h>
25#include <api/api_server.h>
26#include <api/api_utils.h>
29#include <pgm_base.h>
30#include <kiface_base.h>
33#include <confirm.h>
34#include <gestfich.h>
35#include <eda_dde.h>
37#include "eeschema_helpers.h"
39#include <reporter.h>
40#include "git/kigit_sch_merge.h"
43#include <eeschema_settings.h>
44#include <sch_edit_frame.h>
46#include <symbol_edit_frame.h>
47#include <symbol_viewer_frame.h>
53#include <kiway.h>
54#include <project_sch.h>
55#include <richio.h>
58#include <sexpr/sexpr.h>
59#include <sexpr/sexpr_parser.h>
60#include <string_utils.h>
61#include <trace_helpers.h>
62#include <thread_pool.h>
63#include <kiface_ids.h>
64#include <widgets/kistatusbar.h>
66#include <wx/ffile.h>
67#include <wx/tokenzr.h>
69
70#include <schematic.h>
71#include <connection_graph.h>
82#include <sim/simulator_frame.h>
83
85#include <toolbars_sch_editor.h>
87
88#include <sch_io/sch_io.h>
89#include <sch_io/sch_io_mgr.h>
90
91#include <wx/crt.h>
92
93// The main sheet of the project
95
96
97namespace SCH {
98
99// Non-job kiface exports for diff/merge (returned by IfaceOrAddress). Defined
100// after the kiface instance so they can route into its jobs handler.
101static int eeschemaMergeExport( int aKind, const wxString& aAncestor, const wxString& aOurs,
102 const wxString& aTheirs, const wxString& aOutput, bool aInteractive,
103 bool aSingleFile, REPORTER* aReporter );
104static int eeschemaOpenDiffDialogExport( int aKind, const wxString& aFileA, const wxString& aFileB,
105 const wxString& aLabelA, const wxString& aLabelB,
106 wxWindow* aParent, REPORTER* aReporter );
107
108
109
110// TODO: This should move out of this file
111static std::unique_ptr<SCHEMATIC> readSchematicFromFile( const std::string& aFilename )
112{
113 SCH_IO* pi = SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_KICAD );
114 std::unique_ptr<SCHEMATIC> schematic = std::make_unique<SCHEMATIC>( nullptr );
115
117
118 wxFileName pro( aFilename );
119 pro.SetExt( FILEEXT::ProjectFileExtension );
120 pro.MakeAbsolute();
121 wxString projectPath = pro.GetFullPath();
122
123 PROJECT* project = manager.GetProject( projectPath );
124
125 if( !project )
126 {
127 manager.LoadProject( projectPath, true );
128 project = manager.GetProject( projectPath );
129 }
130
131 schematic->Reset();
132 schematic->SetProject( project );
133 SCH_SHEET* rootSheet = pi->LoadSchematicFile( aFilename, schematic.get() );
134
135 if( !rootSheet )
136 return nullptr;
137
138 std::vector<SCH_SHEET*> topLevelSheets = schematic->GetTopLevelSheets();
139 bool rootIsTopLevel = std::find( topLevelSheets.begin(), topLevelSheets.end(), rootSheet )
140 != topLevelSheets.end();
141 bool rootIsVirtualRoot = rootSheet == &schematic->Root() || rootSheet->IsVirtualRootSheet();
142
143 if( !rootIsTopLevel && !rootIsVirtualRoot )
144 schematic->SetTopLevelSheets( { rootSheet } );
145
146 SCH_SCREENS screens( schematic->Root() );
147
148 for( SCH_SCREEN* screen = screens.GetFirst(); screen; screen = screens.GetNext() )
149 screen->UpdateLocalLibSymbolLinks();
150
151 SCH_SHEET_LIST sheets = schematic->Hierarchy();
152
153 // Restore all of the loaded symbol instances from the root sheet screen.
154 sheets.UpdateSymbolInstanceData( schematic->RootScreen()->GetSymbolInstances() );
155
156 if( schematic->RootScreen()->GetFileFormatVersionAtLoad() < 20230221 )
157 {
158 for( SCH_SCREEN* screen = screens.GetFirst(); screen; screen = screens.GetNext() )
159 screen->FixLegacyPowerSymbolMismatches();
160 }
161
162 for( SCH_SCREEN* screen = screens.GetFirst(); screen; screen = screens.GetNext() )
163 screen->MigrateSimModels();
164
165 sheets.AnnotatePowerSymbols();
166
167 // NOTE: This is required for multi-unit symbols to be correct
168 for( SCH_SHEET_PATH& sheet : sheets )
169 sheet.UpdateAllScreenReferences();
170
171 // TODO: this must handle SchematicCleanup somehow. The original version didn't because
172 // it knew that QA test cases were saved in a clean state.
173
174 // TODO: does this need to handle PruneOrphanedSymbolInstances() and
175 // PruneOrphanedSheetInstances()?
176
177 schematic->ConnectionGraph()->Recalculate( sheets, true );
178
179 return schematic;
180}
181
182
183// TODO: This should move out of this file
184bool generateSchematicNetlist( const wxString& aFilename, std::string& aNetlist )
185{
186 std::unique_ptr<SCHEMATIC> schematic = readSchematicFromFile( aFilename.ToStdString() );
187 NETLIST_EXPORTER_KICAD exporter( schematic.get() );
188 STRING_FORMATTER formatter;
189
190 exporter.Format( &formatter, GNL_ALL | GNL_OPT_KICAD );
191 aNetlist = formatter.GetString();
192
193 return true;
194}
195
196
197static struct IFACE : public KIFACE_BASE, public UNITS_PROVIDER
198{
199 // Of course all are virtual overloads, implementations of the KIFACE.
200
201 IFACE( const char* aName, KIWAY::FACE_T aType ) :
202 KIFACE_BASE( aName, aType ),
205 {}
206
207 bool OnKifaceStart( PGM_BASE* aProgram, int aCtlBits, KIWAY* aKiway ) override;
208
209 void Reset() override;
210
211 void OnKifaceEnd() override;
212
213 wxWindow* CreateKiWindow( wxWindow* aParent, int aClassId, KIWAY* aKiway, int aCtlBits = 0 ) override
214 {
215 switch( aClassId )
216 {
217 case FRAME_SCH:
218 {
219 SCH_EDIT_FRAME* frame = new SCH_EDIT_FRAME( aKiway, aParent );
220
222
223 if( Kiface().IsSingle() )
225 // only run this under single_top, not under a project manager.
227 }
228
229 return frame;
230 }
231
233 return new SYMBOL_EDIT_FRAME( aKiway, aParent );
234
235 case FRAME_SIMULATOR:
236 {
237 try
238 {
239 SIMULATOR_FRAME* frame = new SIMULATOR_FRAME( aKiway, aParent );
240 return frame;
241 }
242 catch( const SIMULATOR_INIT_ERR& )
243 {
244 // catch the init err exception as we don't want it to bubble up
245 // its going to be some ngspice install issue but we don't want to log that
246 return nullptr;
247 }
248 }
250 case FRAME_SCH_VIEWER:
251 return new SYMBOL_VIEWER_FRAME( aKiway, aParent );
252
255 bool cancelled = false;
256 SYMBOL_CHOOSER_FRAME* chooser = new SYMBOL_CHOOSER_FRAME( aKiway, aParent, cancelled );
257
258 if( cancelled )
259 {
260 chooser->Destroy();
261 return nullptr;
263
264 return chooser;
265 }
268 InvokeSchEditSymbolLibTable( aKiway, aParent );
269 // Dialog has completed; nothing to return.
270 return nullptr;
274 // Dialog has completed; nothing to return.
275 return nullptr;
276
278 return new PANEL_SYM_DISPLAY_OPTIONS( aParent, GetAppSettings<SYMBOL_EDITOR_SETTINGS>( "symbol_editor" ) );
283 EDA_BASE_FRAME* frame = aKiway->Player( FRAME_SCH_SYMBOL_EDITOR, false );
284
285 if( !frame )
286 frame = aKiway->Player( FRAME_SCH_VIEWER, false );
287
288 if( !frame )
289 frame = aKiway->Player( FRAME_SCH, false );
290
291 if( frame )
292 SetUserUnits( frame->GetUserUnits() );
293
294 return new PANEL_GRID_SETTINGS( aParent, this, frame, cfg, FRAME_SCH_SYMBOL_EDITOR );
295 }
296
298 {
299 EDA_BASE_FRAME* frame = aKiway->Player( FRAME_SCH_SYMBOL_EDITOR, false );
300
301 if( !frame )
302 frame = aKiway->Player( FRAME_SCH_VIEWER, false );
303
304 if( !frame )
305 frame = aKiway->Player( FRAME_SCH, false );
306
307 if( frame )
308 SetUserUnits( frame->GetUserUnits() );
309
310 return new PANEL_SYM_EDITING_OPTIONS( aParent, this, frame );
311 }
312
314 {
315 APP_SETTINGS_BASE* cfg = GetAppSettings<SYMBOL_EDITOR_SETTINGS>( "symbol_editor" );
316 TOOLBAR_SETTINGS* tb = GetToolbarSettings<SYMBOL_EDIT_TOOLBAR_SETTINGS>( "symbol_editor-toolbars" );
317
318 std::vector<TOOL_ACTION*> actions;
319 std::vector<ACTION_TOOLBAR_CONTROL*> controls;
320
321 for( TOOL_ACTION* action : ACTION_MANAGER::GetActionList() )
322 actions.push_back( action );
323
324 for( ACTION_TOOLBAR_CONTROL* control : ACTION_TOOLBAR::GetCustomControlList( FRAME_SCH_SYMBOL_EDITOR ) )
325 controls.push_back( control );
326
327 return new PANEL_TOOLBAR_CUSTOMIZATION( aParent, cfg, tb, FRAME_SCH_SYMBOL_EDITOR, actions, controls );
328 }
329
330 case PANEL_SYM_COLORS:
331 return new PANEL_SYM_COLOR_SETTINGS( aParent );
332
334 return new PANEL_EESCHEMA_DISPLAY_OPTIONS( aParent, GetAppSettings<EESCHEMA_SETTINGS>( "eeschema" ) );
335
336 case PANEL_SCH_GRIDS:
337 {
338 EESCHEMA_SETTINGS* cfg = GetAppSettings<EESCHEMA_SETTINGS>( "eeschema" );
339 EDA_BASE_FRAME* frame = aKiway->Player( FRAME_SCH, false );
340
341 if( !frame )
342 frame = aKiway->Player( FRAME_SCH_SYMBOL_EDITOR, false );
343
344 if( !frame )
345 frame = aKiway->Player( FRAME_SCH_VIEWER, false );
346
347 if( frame )
348 SetUserUnits( frame->GetUserUnits() );
349
350 return new PANEL_GRID_SETTINGS( aParent, this, frame, cfg, FRAME_SCH );
351 }
352
354 {
355 EDA_BASE_FRAME* frame = aKiway->Player( FRAME_SCH, false );
356
357 if( !frame )
358 frame = aKiway->Player( FRAME_SCH_SYMBOL_EDITOR, false );
359
360 if( !frame )
361 frame = aKiway->Player( FRAME_SCH_VIEWER, false );
362
363 if( frame )
364 SetUserUnits( frame->GetUserUnits() );
365
366 return new PANEL_EESCHEMA_EDITING_OPTIONS( aParent, this, frame );
367 }
368
370 {
371 APP_SETTINGS_BASE* cfg = GetAppSettings<EESCHEMA_SETTINGS>( "eeschema" );
372 TOOLBAR_SETTINGS* tb = GetToolbarSettings<SCH_EDIT_TOOLBAR_SETTINGS>( "eeschema-toolbars" );
373
374 std::vector<TOOL_ACTION*> actions;
375 std::vector<ACTION_TOOLBAR_CONTROL*> controls;
376
377 for( TOOL_ACTION* action : ACTION_MANAGER::GetActionList() )
378 actions.push_back( action );
379
380 for( ACTION_TOOLBAR_CONTROL* control : ACTION_TOOLBAR::GetCustomControlList( FRAME_SCH ) )
381 controls.push_back( control );
382
383 return new PANEL_TOOLBAR_CUSTOMIZATION( aParent, cfg, tb, FRAME_SCH, actions, controls );
384 }
385
386 case PANEL_SCH_COLORS:
387 return new PANEL_EESCHEMA_COLOR_SETTINGS( aParent );
388
390 return new PANEL_TEMPLATE_FIELDNAMES( aParent, nullptr );
391
393 {
394 EDA_BASE_FRAME* frame = aKiway->Player( FRAME_SCH, false );
395
396 if( !frame )
397 frame = aKiway->Player( FRAME_SCH_SYMBOL_EDITOR, false );
398
399 if( !frame )
400 frame = aKiway->Player( FRAME_SCH_VIEWER, false );
401
402 return new class PANEL_SCH_DATA_SOURCES( aParent, frame );
403 }
404
406 return new PANEL_SIMULATOR_PREFERENCES( aParent );
407
408 default:
409 return nullptr;
410 }
411 }
412
423 void* IfaceOrAddress( int aDataId ) override
424 {
425 switch( aDataId )
426 {
428 return (void*) generateSchematicNetlist;
429
431 return reinterpret_cast<void*>( &eeschemaMergeExport );
432
434 return reinterpret_cast<void*>( &eeschemaOpenDiffDialogExport );
435 }
436
437 return nullptr;
438 }
439
442
448 void SaveFileAs( const wxString& aProjectBasePath, const wxString& aProjectName,
449 const wxString& aNewProjectBasePath, const wxString& aNewProjectName,
450 const wxString& aSrcFilePath, wxString& aErrors ) override;
451
452
453 int HandleJob( JOB* aJob, REPORTER* aReporter, PROGRESS_REPORTER* aProgressReporter ) override;
454
455 bool HandleJobConfig( JOB* aJob, wxWindow* aParent ) override;
456
457 bool HandleApiOpenDocument( const wxString& aPath,
458 KICAD_API_SERVER* aServer,
459 wxString* aError ) override;
460
461 bool HandleApiCloseDocument( const wxString& aSchFileName,
462 KICAD_API_SERVER* aServer,
463 wxString* aError ) override;
464
465 void PreloadLibraries( KIWAY* aKiway ) override;
466 void CancelPreload( bool aBlock = true ) override;
467 void ProjectChanged() override;
468
469private:
470 std::unique_ptr<EESCHEMA_JOBS_HANDLER> m_jobHandler;
471 std::shared_ptr<BACKGROUND_JOB> m_libraryPreloadBackgroundJob;
472 std::future<void> m_libraryPreloadReturn;
474 std::atomic_bool m_libraryPreloadAbort;
475
476 void closeCurrentDocument( KICAD_API_SERVER* aServer );
477
478 KIWAY* m_kiway = nullptr;
480 std::shared_ptr<HEADLESS_SCH_CONTEXT> m_openContext;
481 std::unique_ptr<API_HANDLER_SCH> m_openHandler;
482
483} kiface( "eeschema", KIWAY::FACE_SCH );
484
485
486int eeschemaMergeExport( int aKind, const wxString& aAncestor, const wxString& aOurs,
487 const wxString& aTheirs, const wxString& aOutput, bool aInteractive,
488 bool aSingleFile, REPORTER* aReporter )
489{
490 return kiface.JobHandler()->RunMerge( static_cast<KICAD_DIFF::DOC_KIND>( aKind ), aAncestor,
491 aOurs, aTheirs, aOutput, aInteractive, aSingleFile,
492 aReporter );
493}
494
495
496int eeschemaOpenDiffDialogExport( int aKind, const wxString& aFileA, const wxString& aFileB,
497 const wxString& aLabelA, const wxString& aLabelB,
498 wxWindow* aParent, REPORTER* aReporter )
499{
500 return kiface.JobHandler()->OpenDiffDialog( static_cast<KICAD_DIFF::DOC_KIND>( aKind ), aFileA,
501 aFileB, aLabelA, aLabelB, aParent, aReporter );
502}
503
504} // namespace
505
506using namespace SCH;
507
508
510
511
512// KIFACE_GETTER's actual spelling is a substitution macro found in kiway.h.
513// KIFACE_GETTER will not have name mangling due to declaration in kiway.h.
514KIFACE_API KIFACE* KIFACE_GETTER( int* aKIFACEversion, int aKiwayVersion, PGM_BASE* aProgram )
515{
516 return &kiface;
517}
518
519
520bool IFACE::OnKifaceStart( PGM_BASE* aProgram, int aCtlBits, KIWAY* aKiway )
521{
522 // This is process-level-initialization, not project-level-initialization of the DSO.
523 // Do nothing in here pertinent to a project!
525
526 // Register the symbol editor settings as well because they share a KiFACE and need to be
527 // loaded prior to use to avoid threading deadlocks
529 aProgram->GetSettingsManager().RegisterSettings( symSettings ); // manager takes ownership
530
531 // We intentionally register KifaceSettings after SYMBOL_EDITOR_SETTINGS
532 // In legacy configs, many settings were in a single editor config nd the migration routine
533 // for the main editor file will try and call into the now separate settings stores
534 // to move the settings into them
536
537 start_common( aCtlBits );
538
539 m_kiway = aKiway;
540
541 m_jobHandler = std::make_unique<EESCHEMA_JOBS_HANDLER>( aKiway );
542
544 {
545 m_jobHandler->SetReporter( &CLI_REPORTER::GetInstance() );
546 m_jobHandler->SetProgressReporter( &CLI_PROGRESS_REPORTER::GetInstance() );
547 }
548
549 // Register the schematic and symbol-library merge drivers with libgit2 so
550 // `.gitattributes` entries `merge=kicad-sch` and `merge=kicad-sym-lib`
551 // route through KiCad-aware merge logic.
554
555 return true;
556}
557
558
560{
561}
562
563
565{
566 constexpr static int interval = 150;
567 constexpr static int timeLimit = 120000;
568
569 wxCHECK( aKiway, /* void */ );
570
571 // Use compare_exchange to atomically check and set the flag to prevent race conditions
572 // when PreloadLibraries is called multiple times concurrently (e.g., from project manager
573 // and schematic editor both scheduling via CallAfter)
574 bool expected = false;
575
576 if( !m_libraryPreloadInProgress.compare_exchange_strong( expected, true ) )
577 return;
578
580
582 Pgm().GetBackgroundJobMonitor().Create( _( "Loading Symbol Libraries" ) );
583
584 auto preload =
585 [this, aKiway]() -> void
586 {
587 std::shared_ptr<BACKGROUND_JOB_REPORTER> reporter =
589
591
592 int elapsed = 0;
593 bool aborted = false;
594
595 reporter->Report( _( "Loading Symbol Libraries" ) );
596 adapter->AsyncLoad();
597
598 while( true )
599 {
600 if( m_libraryPreloadAbort.load() )
601 {
602 m_libraryPreloadAbort.store( false );
603 aborted = true;
604 break;
605 }
606
607 std::this_thread::sleep_for( std::chrono::milliseconds( interval ) );
608
609 if( std::optional<float> loadStatus = adapter->AsyncLoadProgress() )
610 {
611 float progress = *loadStatus;
612 reporter->SetCurrentProgress( progress );
613
614 if( progress >= 1 )
615 break;
616 }
617 else
618 {
619 reporter->SetCurrentProgress( 1 );
620 break;
621 }
622
623 elapsed += interval;
624
625 if( elapsed > timeLimit )
626 break;
627 }
628
629 // AbortAsyncLoad() sets the adapter's worker abort flag and then blocks,
630 // so workers exit at their next checkpoint. BlockUntilLoaded() alone just
631 // waits for each future to complete naturally, which can hang indefinitely
632 // if a worker is stuck on a stalled network or filesystem operation.
633 if( aborted )
634 adapter->AbortAsyncLoad();
635 else
636 adapter->BlockUntilLoaded();
637
638 // If aborted, skip operations that use the adapter since the project may have changed
639 // and the adapter's project reference could be stale. This prevents use-after-free
640 // crashes when switching projects during library preload.
641 if( !aborted )
642 {
643 // Collect library load errors for async reporting
644 wxString errors = adapter->GetLibraryLoadErrors();
645
646 wxLogTrace( traceLibraries, "eeschema PreloadLibraries: errors.IsEmpty()=%d, length=%zu",
647 errors.IsEmpty(), errors.length() );
648
649 std::vector<LOAD_MESSAGE> messages = ExtractLibraryLoadErrors( errors, RPT_SEVERITY_ERROR );
650
651 if( !messages.empty() )
652 {
653 wxLogTrace( traceLibraries, " -> collected %zu messages, calling AddLibraryLoadMessages",
654 messages.size() );
655 Pgm().AddLibraryLoadMessages( messages );
656 }
657 else
658 {
659 wxLogTrace( traceLibraries, " -> no errors from symbol libraries" );
660 }
661 }
662 else
663 {
664 wxLogTrace( traceLibraries, "eeschema PreloadLibraries: aborted, skipping symbol processing" );
665 }
666
669 m_libraryPreloadInProgress.store( false );
670
671 // Only send reload notifications if we weren't aborted
672 if( !aborted )
673 {
674 std::string payload = "";
675 aKiway->ExpressMail( FRAME_SCH, MAIL_RELOAD_LIB, payload, nullptr, true );
676 aKiway->ExpressMail( FRAME_SCH_SYMBOL_EDITOR, MAIL_RELOAD_LIB, payload, nullptr, true );
677 aKiway->ExpressMail( FRAME_SCH_VIEWER, MAIL_RELOAD_LIB, payload, nullptr, true );
678 }
679 };
680
681 std::future<void> preloadFuture = std::async( std::launch::async, preload );
682 m_libraryPreloadReturn = std::move( preloadFuture );
683}
684
685
686void IFACE::CancelPreload( bool aBlock )
687{
688 if( m_libraryPreloadInProgress.load() )
689 {
690 m_libraryPreloadAbort.store( true );
691
692 if( aBlock )
694 }
695}
696
697
699{
700 if( m_libraryPreloadInProgress.load() )
701 m_libraryPreloadAbort.store( true );
702}
703
704
706{
707 end_common();
708}
709
710
711void IFACE::SaveFileAs( const wxString& aProjectBasePath, const wxString& aProjectName,
712 const wxString& aNewProjectBasePath, const wxString& aNewProjectName,
713 const wxString& aSrcFilePath, wxString& aErrors )
714{
715 wxFileName destFile( aSrcFilePath );
716 wxString destPath = destFile.GetPathWithSep();
717 wxUniChar pathSep = wxFileName::GetPathSeparator();
718 wxString ext = destFile.GetExt();
719
720 if( destPath.StartsWith( aProjectBasePath + pathSep ) )
721 destPath.Replace( aProjectBasePath, aNewProjectBasePath, false );
722
723 destFile.SetPath( destPath );
724
729 {
730 if( destFile.GetName() == aProjectName )
731 {
732 destFile.SetName( aNewProjectName );
733 }
734 else if( destFile.GetName() == aNewProjectName )
735 {
736 wxString msg;
737
738 if( !aErrors.empty() )
739 aErrors += wxS( "\n" );
740
741 msg.Printf( _( "Cannot copy file '%s' as it will be overwritten by the new root "
742 "sheet file." ), destFile.GetFullPath() );
743 aErrors += msg;
744 return;
745 }
746
747 CopySexprFile( aSrcFilePath, destFile.GetFullPath(),
748 [&]( const std::string& token, wxString& value ) -> bool
749 {
750 if( token == "project" && value == aProjectName )
751 {
752 value = aNewProjectName;
753 return true;
754 }
755
756 return false;
757 },
758 aErrors );
759 }
761 {
762 // Symbols are not project-specific. Keep their source names.
763 KiCopyFile( aSrcFilePath, destFile.GetFullPath(), aErrors );
764 }
768 {
769 if( destFile.GetName() == aProjectName + wxS( "-cache" ) )
770 destFile.SetName( aNewProjectName + wxS( "-cache" ) );
771
772 if( destFile.GetName() == aProjectName + wxS( "-rescue" ) )
773 destFile.SetName( aNewProjectName + wxS( "-rescue" ) );
774
775 KiCopyFile( aSrcFilePath, destFile.GetFullPath(), aErrors );
776 }
777 else if( ext == FILEEXT::NetlistFileExtension )
778 {
779 if( destFile.GetName() == aProjectName )
780 destFile.SetName( aNewProjectName );
781
782 CopySexprFile( aSrcFilePath, destFile.GetFullPath(),
783 [&]( const std::string& token, wxString& value ) -> bool
784 {
785 if( token == "source" )
786 {
787 for( const wxString& extension : { wxString( wxT( ".sch" ) ), wxString( wxT( ".kicad_sch" ) ) } )
788 {
789 if( value == aProjectName + extension )
790 {
791 value = aNewProjectName + extension;
792 return true;
793 }
794 else if( value == aProjectBasePath + "/" + aProjectName + extension )
795 {
796 value = aNewProjectBasePath + "/" + aNewProjectName + extension;
797 return true;
798 }
799 else if( value.StartsWith( aProjectBasePath ) )
800 {
801 value.Replace( aProjectBasePath, aNewProjectBasePath, false );
802 return true;
803 }
804 }
805 }
806
807 return false;
808 },
809 aErrors );
810 }
811 else if( destFile.GetName() == FILEEXT::SymbolLibraryTableFileName )
812 {
813 wxFileName libTableFn( aSrcFilePath );
814 LIBRARY_TABLE libTable( libTableFn, LIBRARY_TABLE_SCOPE::PROJECT );
815 libTable.SetPath( destFile.GetFullPath() );
816 libTable.SetType( LIBRARY_TABLE_TYPE::SYMBOL );
817
818 for( LIBRARY_TABLE_ROW& row : libTable.Rows() )
819 {
820 wxString uri = row.URI();
821
822 uri.Replace( wxS( "/" ) + aProjectName + wxS( "-cache.lib" ),
823 wxS( "/" ) + aNewProjectName + wxS( "-cache.lib" ) );
824 uri.Replace( wxS( "/" ) + aProjectName + wxS( "-rescue.lib" ),
825 wxS( "/" ) + aNewProjectName + wxS( "-rescue.lib" ) );
826 uri.Replace( wxS( "/" ) + aProjectName + wxS( ".lib" ),
827 wxS( "/" ) + aNewProjectName + wxS( ".lib" ) );
828
829 row.SetURI( uri );
830 }
831
832 libTable.Save().map_error(
833 [&]( const LIBRARY_ERROR& aError )
834 {
835 wxString msg;
836
837 if( !aErrors.empty() )
838 aErrors += wxT( "\n" );
839
840 msg.Printf( _( "Cannot copy file '%s'." ), destFile.GetFullPath() );
841 aErrors += msg;
842 } );
843 }
844 else
845 {
846 wxFAIL_MSG( wxS( "Unexpected filetype for Eeschema::SaveFileAs()" ) );
847 }
848}
849
850
851int IFACE::HandleJob( JOB* aJob, REPORTER* aReporter, PROGRESS_REPORTER* aProgressReporter )
852{
853 return m_jobHandler->RunJob( aJob, aReporter, aProgressReporter );
854}
855
856
857bool IFACE::HandleJobConfig( JOB* aJob, wxWindow* aParent )
858{
859 return m_jobHandler->HandleJobConfig( aJob, aParent );
860}
861
862
863// TODO(JE) some of the below methods can probably be factored out and shared between sch/pcb
865{
866 if( m_openHandler )
867 {
868 if( aServer )
869 aServer->DeregisterHandler( m_openHandler.get() );
870
871 m_openHandler.reset();
872 }
873
874 m_openContext.reset();
875
876 delete m_openSchematic;
877 m_openSchematic = nullptr;
878
879 // The jobs handler caches the last-loaded schematic. Clear it so the next job
880 // uses the schematic from the newly opened document rather than a stale copy.
881 m_jobHandler->ClearCachedSchematic();
882}
883
884
885bool IFACE::HandleApiOpenDocument( const wxString& aPath, KICAD_API_SERVER* aServer,
886 wxString* aError )
887{
888 wxCHECK( aServer, false );
889
890 if( aPath.IsEmpty() )
891 {
892 if( aError )
893 *aError = wxS( "No path specified to open" );
894
895 return false;
896 }
897
898 wxFileName projectPath( aPath );
899
900 if( projectPath.GetExt() == FILEEXT::KiCadSchematicFileExtension )
901 projectPath.SetExt( FILEEXT::ProjectFileExtension );
902 else if( projectPath.GetExt() != FILEEXT::ProjectFileExtension )
903 projectPath.SetExt( FILEEXT::ProjectFileExtension );
904
905 projectPath.MakeAbsolute();
906
907 // Close any existing document before loading a new project. LoadProject with
908 // aSetActive=true destroys the old PROJECT, which would leave the old schematic
909 // and context holding dangling project pointers.
910 closeCurrentDocument( aServer );
911
912 SETTINGS_MANAGER& settingsManager = Pgm().GetSettingsManager();
913
914 if( !settingsManager.LoadProject( projectPath.GetFullPath(), true ) )
915 {
916 wxLogTrace( traceApi, "Warning: no project file found for %s", aPath );
917 }
918
919 PROJECT* project = settingsManager.GetProject( projectPath.GetFullPath() );
920
921 if( !project )
922 {
923 if( aError )
924 *aError = wxString::Format( wxS( "Error loading project for %s" ), aPath );
925
926 return false;
927 }
928
929 wxFileName schPath( projectPath );
930 schPath.SetExt( FILEEXT::KiCadSchematicFileExtension );
931
932 if( !schPath.FileExists() )
933 {
934 if( aError )
935 *aError = wxString::Format( wxS( "File not found: %s" ), schPath.GetFullPath() );
936
937 return false;
938 }
939
940 SCHEMATIC* schematic = nullptr;
941
942 try
943 {
944 schematic = EESCHEMA_HELPERS::LoadSchematic( schPath.GetFullPath(), false, false, project );
945
946 if( !schematic )
947 {
948 if( aError )
949 *aError = wxS( "Failed to load schematic" );
950
951 return false;
952 }
953 }
954 catch( ... )
955 {
956 if( aError )
957 *aError = wxS( "Failed to load schematic" );
958
959 return false;
960 }
961
962 m_openSchematic = schematic;
963
964 m_openContext = std::make_shared<HEADLESS_SCH_CONTEXT>( m_openSchematic, project, m_kiway );
965 m_openHandler = std::make_unique<API_HANDLER_SCH>( m_openContext );
966 aServer->RegisterHandler( m_openHandler.get() );
967
968 return true;
969}
970
971
972bool IFACE::HandleApiCloseDocument( const wxString& aSchFileName, KICAD_API_SERVER* aServer,
973 wxString* aError )
974{
975 wxCHECK( aServer, false );
976
977 if( !m_openContext )
978 {
979 if( aError )
980 *aError = wxS( "No document is currently open" );
981
982 return false;
983 }
984
985 if( !aSchFileName.IsEmpty() )
986 {
987 wxFileName currentSch( m_openContext->GetCurrentFileName() );
988
989 if( currentSch.GetFullName() != aSchFileName )
990 {
991 if( aError )
992 *aError = wxS( "Requested document does not match the open document" );
993
994 return false;
995 }
996 }
997
998 closeCurrentDocument( aServer );
999 return true;
1000}
constexpr EDA_IU_SCALE schIUScale
Definition base_units.h:123
KIFACE_BASE & Kiface()
Global KIFACE_BASE "get" accessor.
static std::list< TOOL_ACTION * > & GetActionList()
Return list of TOOL_ACTIONs.
static std::list< ACTION_TOOLBAR_CONTROL * > GetCustomControlList(FRAME_T aContext)
Get the list of custom controls that could be used on a particular frame type.
APP_SETTINGS_BASE is a settings class that should be derived for each standalone KiCad application.
std::shared_ptr< BACKGROUND_JOB > Create(const wxString &aName)
Creates a background job with the given name.
void Remove(std::shared_ptr< BACKGROUND_JOB > job)
Removes the given background job from any lists and frees it.
static CLI_PROGRESS_REPORTER & GetInstance()
static CLI_REPORTER & GetInstance()
Definition reporter.cpp:157
The base frame for deriving all KiCad main window classes.
static void SetSchEditFrame(SCH_EDIT_FRAME *aSchEditFrame)
static SCHEMATIC * LoadSchematic(const wxString &aFileName, bool aSetActive, bool aForceDefaultProject, PROJECT *aProject=nullptr, bool aCalculateConnectivity=true)
Handle Eeschema job dispatches.
An simple container class that lets us dispatch output jobs to kifaces.
Definition job.h:184
void RegisterHandler(API_HANDLER *aHandler)
Adds a new request handler to the server.
void DeregisterHandler(API_HANDLER *aHandler)
A KIFACE implementation.
Definition kiface_base.h:35
KIFACE_BASE(const char *aKifaceName, KIWAY::FACE_T aId)
Definition kiface_base.h:63
void InitSettings(APP_SETTINGS_BASE *aSettings)
Definition kiface_base.h:93
void end_common()
Common things to do for a top program module, during OnKifaceEnd();.
APP_SETTINGS_BASE * KifaceSettings() const
Definition kiface_base.h:91
bool start_common(int aCtlBits)
Common things to do for a top program module, during OnKifaceStart().
int m_start_flags
flags provided in OnKifaceStart()
bool IsSingle() const
Is this KIFACE running under single_top?
static int Apply(const git_merge_driver_source *aSrc, const char **aPathOut, unsigned int *aModeOut, git_buf *aMergedOut)
void CreateServer(int service, bool local=true)
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:311
virtual KIWAY_PLAYER * Player(FRAME_T aFrameType, bool doCreate=true, wxTopLevelWindow *aParent=nullptr)
Return the KIWAY_PLAYER* given a FRAME_T.
Definition kiway.cpp:398
virtual void ExpressMail(FRAME_T aDestination, MAIL_T aCommand, std::string &aPayload, wxWindow *aSource=nullptr, bool aFromOtherThread=false)
Send aPayload to aDestination from aSource.
Definition kiway.cpp:496
FACE_T
Known KIFACE implementations.
Definition kiway.h:317
@ FACE_SCH
eeschema DSO
Definition kiway.h:318
virtual PROJECT & Prj() const
Return the PROJECT associated with this KIWAY.
Definition kiway.cpp:201
std::optional< float > AsyncLoadProgress() const
Returns async load progress between 0.0 and 1.0, or nullopt if load is not in progress.
void AbortAsyncLoad()
Aborts any async load in progress; blocks until fully done aborting.
wxString GetLibraryLoadErrors() const
Returns all library load errors as newline-separated strings for display.
void AsyncLoad()
Loads all available libraries for this adapter type in the background.
Generate the KiCad netlist format supported by Pcbnew.
void Format(OUTPUTFORMATTER *aOutputFormatter, int aCtl)
Output this s-expression netlist into aOutputFormatter.
Container for data for KiCad programs.
Definition pgm_base.h:102
virtual BACKGROUND_JOBS_MONITOR & GetBackgroundJobMonitor() const
Definition pgm_base.h:130
void ClearLibraryLoadMessages()
Clear library load messages from all registered status bars.
void AddLibraryLoadMessages(const std::vector< LOAD_MESSAGE > &aMessages)
Add library load messages to all registered status bars.
Definition pgm_base.cpp:990
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition pgm_base.h:124
A progress reporter interface for use in multi-threaded environments.
static SYMBOL_LIBRARY_ADAPTER * SymbolLibAdapter(PROJECT *aProject)
Accessor for project symbol library manager adapter.
Container for project specific data.
Definition project.h:62
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:90
Schematic editor (Eeschema) main window.
Base class that schematic file and library loading and saving plugins should derive from.
Definition sch_io.h:59
virtual SCH_SHEET * LoadSchematicFile(const wxString &aFileName, SCHEMATIC *aSchematic, SCH_SHEET *aAppendToMe=nullptr, const std::map< std::string, UTF8 > *aProperties=nullptr)
Load information from some input file format that this SCH_IO implementation knows about,...
Definition sch_io.cpp:67
Container class that holds multiple SCH_SCREEN objects in a hierarchy.
Definition sch_screen.h:746
SCH_SCREEN * GetNext()
SCH_SCREEN * GetFirst()
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
void AnnotatePowerSymbols()
Silently annotate the not yet annotated power symbols of the entire hierarchy of the sheet path list.
void UpdateSymbolInstanceData(const std::vector< SCH_SYMBOL_INSTANCE > &aSymbolInstances)
Update all of the symbol instance information using aSymbolInstances.
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition sch_sheet.h:44
bool IsVirtualRootSheet() const
T * RegisterSettings(T *aSettings, bool aLoadNow=true)
Take ownership of the pointer passed in.
bool LoadProject(const wxString &aFullPath, bool aSetActive=true)
Load a project or sets up a new project with a specified path.
PROJECT * GetProject(const wxString &aFullPath) const
Retrieve a loaded project by name.
Simple error container for failure to init the simulation engine and ultimately abort the frame const...
Implement an OUTPUTFORMATTER to a memory buffer.
Definition richio.h:418
const std::string & GetString()
Definition richio.h:441
Symbol library viewer main window.
An interface to the global shared library manager that is schematic-specific and linked to one projec...
Symbol library viewer main window.
UNITS_PROVIDER(const EDA_IU_SCALE &aIuScale, EDA_UNITS aUnits)
EDA_UNITS GetUserUnits() const
void SetUserUnits(EDA_UNITS aUnits)
This file is part of the common library.
#define _(s)
DDE server & client.
#define KICAD_SCH_PORT_SERVICE_NUMBER
Eeschema listens on this port for commands from Pcbnew.
Definition eda_dde.h:39
EDA_UNITS
Definition eda_units.h:44
SCH_SHEET * g_RootSheet
Definition eeschema.cpp:94
KIFACE_BASE & Kiface()
Global KIFACE_BASE "get" accessor.
Definition eeschema.cpp:509
@ PANEL_SYM_EDIT_GRIDS
Definition frame_type.h:74
@ FRAME_SCH_SYMBOL_EDITOR
Definition frame_type.h:31
@ PANEL_SCH_FIELD_NAME_TEMPLATES
Definition frame_type.h:84
@ PANEL_SCH_TOOLBARS
Definition frame_type.h:83
@ FRAME_SCH_VIEWER
Definition frame_type.h:32
@ PANEL_SCH_DISP_OPTIONS
Definition frame_type.h:79
@ PANEL_SCH_SIMULATOR
Definition frame_type.h:85
@ FRAME_SCH
Definition frame_type.h:30
@ PANEL_SYM_TOOLBARS
Definition frame_type.h:77
@ FRAME_SIMULATOR
Definition frame_type.h:34
@ PANEL_SYM_EDIT_OPTIONS
Definition frame_type.h:75
@ PANEL_SCH_EDIT_OPTIONS
Definition frame_type.h:81
@ PANEL_SYM_DISP_OPTIONS
Definition frame_type.h:73
@ DIALOG_SCH_LIBRARY_TABLE
Definition frame_type.h:126
@ PANEL_SCH_DATA_SOURCES
Definition frame_type.h:86
@ PANEL_SYM_COLORS
Definition frame_type.h:76
@ PANEL_SCH_GRIDS
Definition frame_type.h:80
@ PANEL_SCH_COLORS
Definition frame_type.h:82
@ DIALOG_DESIGN_BLOCK_LIBRARY_TABLE
Definition frame_type.h:125
@ FRAME_SYMBOL_CHOOSER
Definition frame_type.h:33
void CopySexprFile(const wxString &aSrcPath, const wxString &aDestPath, std::function< bool(const std::string &token, wxString &value)> aCallback, wxString &aErrors)
Definition gestfich.cpp:334
void KiCopyFile(const wxString &aSrcPath, const wxString &aDestPath, wxString &aErrors)
Definition gestfich.cpp:307
static const std::string LegacySchematicFileExtension
static const std::string NetlistFileExtension
static const std::string SymbolLibraryTableFileName
static const std::string ProjectFileExtension
static const std::string SchematicSymbolFileExtension
static const std::string KiCadSchematicFileExtension
static const std::string LegacySymbolLibFileExtension
static const std::string KiCadSymbolLibFileExtension
static const std::string BackupFileSuffix
static const std::string LegacySymbolDocumentFileExtension
const wxChar *const traceLibraries
Flag to enable library table and library manager tracing.
const wxChar *const traceApi
Flag to enable debug output related to the IPC API and its plugin system.
Definition api_utils.cpp:29
#define KIFACE_API
@ KIFACE_MERGE_DOCUMENT
int (*)( int aKind, const wxString& aAncestor, const wxString& aOurs, const wxString& aTheirs,...
Definition kiface_ids.h:45
@ KIFACE_NETLIST_SCHEMATIC
Definition kiface_ids.h:38
@ KIFACE_OPEN_DIFF_DIALOG
int (*)( int aKind, const wxString& aFileA, const wxString& aFileB, const wxString& aLabelA,...
Definition kiface_ids.h:52
#define KFCTL_CLI
Running as CLI app.
Definition kiway.h:161
#define KIFACE_GETTER
Definition kiway.h:109
@ MAIL_RELOAD_LIB
Definition mail_type.h:54
DOC_KIND
Document type a diff/merge entry point should route to, derived from a file path's extension.
bool RegisterMergeDriver(const char *aName, MERGE_APPLY_FN aApply)
Register a KiCad merge driver with libgit2.
static int eeschemaMergeExport(int aKind, const wxString &aAncestor, const wxString &aOurs, const wxString &aTheirs, const wxString &aOutput, bool aInteractive, bool aSingleFile, REPORTER *aReporter)
Definition eeschema.cpp:486
static std::unique_ptr< SCHEMATIC > readSchematicFromFile(const std::string &aFilename)
Definition eeschema.cpp:111
SCH::IFACE KIFACE_BASE, UNITS_PROVIDER kiface("eeschema", KIWAY::FACE_SCH)
bool generateSchematicNetlist(const wxString &aFilename, std::string &aNetlist)
Definition eeschema.cpp:184
static int eeschemaOpenDiffDialogExport(int aKind, const wxString &aFileA, const wxString &aFileB, const wxString &aLabelA, const wxString &aLabelB, wxWindow *aParent, REPORTER *aReporter)
Definition eeschema.cpp:496
#define GNL_ALL
@ GNL_OPT_KICAD
void InvokeEditDesignBlockLibTable(KIWAY *aKiway, wxWindow *aParent)
void InvokeSchEditSymbolLibTable(KIWAY *aKiway, wxWindow *aParent)
PGM_BASE & Pgm()
The global program "get" accessor.
see class PGM_BASE
@ RPT_SEVERITY_ERROR
T * GetToolbarSettings(const wxString &aFilename)
T * GetAppSettings(const char *aFilename)
std::vector< LOAD_MESSAGE > ExtractLibraryLoadErrors(const wxString &aErrorString, int aSeverity)
Parse library load error messages, extracting user-facing information while stripping internal code l...
bool OnKifaceStart(PGM_BASE *aProgram, int aCtlBits, KIWAY *aKiway) override
Typically start_common() is called from here.
Implement a participant in the KIWAY alchemy.
Definition kiway.h:152
std::future< void > m_libraryPreloadReturn
Definition eeschema.cpp:472
bool OnKifaceStart(PGM_BASE *aProgram, int aCtlBits, KIWAY *aKiway) override
Typically start_common() is called from here.
Definition eeschema.cpp:520
std::shared_ptr< BACKGROUND_JOB > m_libraryPreloadBackgroundJob
Definition eeschema.cpp:471
std::unique_ptr< API_HANDLER_SCH > m_openHandler
Definition eeschema.cpp:481
void PreloadLibraries(KIWAY *aKiway) override
Definition eeschema.cpp:564
void SaveFileAs(const wxString &aProjectBasePath, const wxString &aProjectName, const wxString &aNewProjectBasePath, const wxString &aNewProjectName, const wxString &aSrcFilePath, wxString &aErrors) override
Saving a file under a different name is delegated to the various KIFACEs because the project doesn't ...
Definition eeschema.cpp:711
void ProjectChanged() override
Definition eeschema.cpp:698
void CancelPreload(bool aBlock=true) override
Definition eeschema.cpp:686
void closeCurrentDocument(KICAD_API_SERVER *aServer)
Definition eeschema.cpp:864
wxWindow * CreateKiWindow(wxWindow *aParent, int aClassId, KIWAY *aKiway, int aCtlBits=0) override
Create a wxWindow for the current project.
Definition eeschema.cpp:213
EESCHEMA_JOBS_HANDLER * JobHandler() const
Accessor for the non-job diff/merge exports (eeschemaMergeExport etc.).
Definition eeschema.cpp:441
std::atomic_bool m_libraryPreloadAbort
Definition eeschema.cpp:474
KIWAY * m_kiway
Definition eeschema.cpp:478
IFACE(const char *aName, KIWAY::FACE_T aType)
Definition eeschema.cpp:201
void Reset() override
Reloads global state.
Definition eeschema.cpp:559
bool HandleApiOpenDocument(const wxString &aPath, KICAD_API_SERVER *aServer, wxString *aError) override
Definition eeschema.cpp:885
bool HandleApiCloseDocument(const wxString &aSchFileName, KICAD_API_SERVER *aServer, wxString *aError) override
Definition eeschema.cpp:972
void * IfaceOrAddress(int aDataId) override
Return a pointer to the requested object.
Definition eeschema.cpp:423
int HandleJob(JOB *aJob, REPORTER *aReporter, PROGRESS_REPORTER *aProgressReporter) override
Definition eeschema.cpp:851
std::unique_ptr< EESCHEMA_JOBS_HANDLER > m_jobHandler
Definition eeschema.cpp:470
std::atomic_bool m_libraryPreloadInProgress
Definition eeschema.cpp:473
bool HandleJobConfig(JOB *aJob, wxWindow *aParent) override
Definition eeschema.cpp:857
std::shared_ptr< HEADLESS_SCH_CONTEXT > m_openContext
Definition eeschema.cpp:480
SCHEMATIC * m_openSchematic
Definition eeschema.cpp:479
void OnKifaceEnd() override
Called just once just before the DSO is to be unloaded.
Definition eeschema.cpp:705
IbisParser parser & reporter
VECTOR3I expected(15, 30, 45)
static const long long MM
wxLogTrace helper definitions.
Definition of file extensions used in Kicad.