KiCad PCB EDA Suite
Loading...
Searching...
No Matches
pcbnew.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 Jean-Pierre Charras, jp.charras at wanadoo.fr
5 * Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <[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 <pgm_base.h>
23#include <eda_pattern_match.h>
26#include <confirm.h>
29#include <api/api_handler_pcb.h>
30#include <api/api_server.h>
31#include <api/api_utils.h>
35#include <kiface_base.h>
36#include <kiface_ids.h>
37#include <kiway_holder.h>
38#include <pcb_edit_frame.h>
39#include <macros.h>
40#include <gestfich.h>
41#include <paths.h>
42#include <pcbnew_settings.h>
43#include <footprint.h>
53#include <footprint_info_impl.h>
54#include <footprint.h>
55#include <board.h>
56#include <board_loader.h>
57#include <lib_id.h>
58#include <nlohmann/json.hpp>
62#include <panel_edit_options.h>
74#include <project_pcb.h>
75#include <string_utils.h>
76#include <thread_pool.h>
77#include <trace_helpers.h>
78#include <widgets/kistatusbar.h>
79
80#include <wx/tokenzr.h>
81
83#include <invoke_pcb_dialog.h>
85#include "pcbnew_jobs_handler.h"
87#include <reporter.h>
88#include "git/kigit_pcb_merge.h"
89#include "git/kigit_fp_merge.h"
91
94#if defined( KICAD_NATIVE_MODEL_PREVIEW ) && defined( __WXGTK3__ )
96#endif
98#include <toolbars_pcb_editor.h>
99
100#include <wx/crt.h>
101
102
108{
109 PROJECT* project = nullptr;
110
111 if( wxTheApp )
112 {
113 wxWindow* focus = wxWindow::FindFocus();
114 wxWindow* top = focus ? wxGetTopLevelParent( focus ) : wxTheApp->GetTopWindow();
115
116 if( top )
117 {
118 if( KIWAY_HOLDER* holder = dynamic_cast<KIWAY_HOLDER*>( top ) )
119 project = &holder->Prj();
120 }
121 }
122
123 if( !project )
125
126 return project;
127}
128
129
144static wxString filterFootprints( const wxString& aFilterJson )
145{
146 using json = nlohmann::json;
147
149 result.m_Success = false;
150
151 try
152 {
153 json input = json::parse( aFilterJson.utf8_string() );
154
155 unsigned pinCount = input.value( "pin_count", 0 );
156 bool zeroFilters = input.value( "zero_filters", true );
157 int maxResults = input.value( "max_results", 400 );
158
159 std::vector<std::unique_ptr<EDA_PATTERN_MATCH>> filterMatchers;
160
161 if( input.contains( "filters" ) && input["filters"].is_array() )
162 {
163 for( const auto& f : input["filters"] )
164 {
165 if( f.is_string() )
166 {
167 wxString pattern = wxString::FromUTF8( f.get<std::string>() );
168 auto matcher = std::make_unique<EDA_PATTERN_MATCH_WILDCARD_ANCHORED>();
169 matcher->SetPattern( pattern.Lower() );
170 filterMatchers.push_back( std::move( matcher ) );
171 }
172 }
173 }
174
175 bool hasFilters = ( pinCount > 0 || !filterMatchers.empty() );
176
177 // A query that can only match nothing is a successful empty result, not a failure.
178 if( zeroFilters && !hasFilters )
179 {
180 result.m_Success = true;
181 return result.ToJsonStr();
182 }
183
185
186 if( !adapter )
187 return result.ToJsonStr();
188
189 adapter->AsyncLoad();
190 adapter->BlockUntilLoaded();
191
192 for( const wxString& nickname : adapter->GetLibraryNames() )
193 {
194 std::vector<FOOTPRINT*> footprints = adapter->GetFootprints( nickname, true );
195
196 for( FOOTPRINT* fp : footprints )
197 {
198 if( !fp )
199 continue;
200
201 // Pin count filter
202 if( pinCount > 0 )
203 {
204 unsigned fpPadCount = fp->GetNumberedPadCount();
205
206 if( fpPadCount != pinCount )
207 continue;
208 }
209
210 // Footprint filter patterns with case-insensitive matching
211 if( !filterMatchers.empty() )
212 {
213 bool matches = false;
214
215 for( const auto& matcher : filterMatchers )
216 {
217 wxString name;
218
219 // If filter contains ':', include library nickname in match string
220 if( matcher->GetPattern().Contains( wxS( ":" ) ) )
221 name = fp->GetFPID().GetLibNickname().wx_str().Lower() + wxS( ":" );
222
223 name += fp->GetFPID().GetLibItemName().wx_str().Lower();
224
225 if( matcher->Find( name ) )
226 {
227 matches = true;
228 break;
229 }
230 }
231
232 if( !matches )
233 continue;
234 }
235
236 // The list is full already, so this match means the result set is
237 // truncated; the list itself keeps its maxResults entries.
238 if( maxResults > 0 && static_cast<int>( result.m_MatchingNames.size() ) >= maxResults )
239 {
240 result.m_IsLimited = true;
241 break;
242 }
243
244 wxString libId = fp->GetFPID().Format();
245 result.m_MatchingNames.emplace_back( libId );
246 }
247
248 if( result.m_IsLimited )
249 break;
250 }
251
252 result.m_Success = true;
253
254 return result.ToJsonStr();
255 }
256 catch( const std::exception& )
257 {
258 // A failure carries no matches, even if the scan had already collected some.
259 result.m_MatchingNames.clear();
260 result.m_Success = false;
261 return result.ToJsonStr();
262 }
263}
264
265
270{
272
273 if( adapter )
274 {
275 adapter->AsyncLoad();
276 return true;
277 }
278
279 return false;
280}
281
282
284 * Return how far a background load has got.
285 *
286 * Never blocks: callers can poll this while showing a loading indication, and only run
287 * #filterFootprints() (which does wait for the load) after it returns 1.0.
288 */
290{
292
293 if( !adapter )
294 return 1.0f;
295
296 // Nothing to report means there is nothing to wait for.
297 if( std::optional<float> progress = adapter->AsyncLoadProgress() )
298 return *progress;
299
300 return 1.0f;
301}
302
303
304namespace PCB {
305
306// Non-job kiface exports for diff/merge (returned by IfaceOrAddress). Defined
307// after the kiface instance so they can route into its jobs handler.
308static int pcbnewMergeExport( int aKind, const wxString& aAncestor, const wxString& aOurs,
309 const wxString& aTheirs, const wxString& aOutput, bool aInteractive,
310 bool aSingleFile, REPORTER* aReporter );
311static int pcbnewOpenDiffDialogExport( int aKind, const wxString& aFileA, const wxString& aFileB,
312 const wxString& aLabelA, const wxString& aLabelB,
313 wxWindow* aParent, REPORTER* aReporter );
314
315
316static struct IFACE : public KIFACE_BASE, public UNITS_PROVIDER
317{
318 // Of course all are virtual overloads, implementations of the KIFACE.
319
320 IFACE( const char* aName, KIWAY::FACE_T aType ) :
321 KIFACE_BASE( aName, aType ),
323 {}
324
325 bool OnKifaceStart( PGM_BASE* aProgram, int aCtlBits, KIWAY* aKiway ) override;
326
327 void Reset() override;
328
329 void OnKifaceEnd() override;
330
331 wxWindow* CreateKiWindow( wxWindow* aParent, int aClassId, KIWAY* aKiway, int aCtlBits = 0 ) override
332 {
333 switch( aClassId )
334 {
335 case FRAME_PCB_EDITOR:
336 {
337 auto frame = new PCB_EDIT_FRAME( aKiway, aParent );
338
339 if( Kiface().IsSingle() )
340 {
341 // only run this under single_top, not under a project manager.
344 }
346 return frame;
347 }
348
350 return new FOOTPRINT_EDIT_FRAME( aKiway, aParent );
351
353 return new FOOTPRINT_VIEWER_FRAME( aKiway, aParent );
354
356 return new FOOTPRINT_CHOOSER_FRAME( aKiway, aParent );
357
359 return new FOOTPRINT_WIZARD_FRAME( aKiway, aParent, FRAME_T( aClassId ) );
362 return FOOTPRINT_PREVIEW_PANEL::New( aKiway, aParent, this );
363
365 {
366 DIALOG_CONFIGURE_PATHS dlg( aParent );
367
368 // The dialog's constructor probably failed to set its Kiway because the
369 // dynamic_cast fails when aParent was allocated by a separate compilation
370 // module. So set it directly.
371 dlg.SetKiway( &dlg, aKiway );
373 // Use QuasiModal so that HTML help window will work
374 if( dlg.ShowQuasiModal() == wxID_OK )
377 // Dialog has completed; nothing to return.
378 return nullptr;
380
382 InvokePcbLibTableEditor( aKiway, aParent );
383 // Dialog has completed; nothing to return.
384 return nullptr;
388
392 EDA_BASE_FRAME* frame = aKiway->Player( FRAME_FOOTPRINT_EDITOR, false );
394 if( !frame )
395 frame = aKiway->Player( FRAME_FOOTPRINT_VIEWER, false );
397 if( !frame )
398 frame = aKiway->Player( FRAME_PCB_EDITOR, false );
399
400 if( frame )
401 SetUserUnits( frame->GetUserUnits() );
402
403 return new PANEL_GRID_SETTINGS( aParent, this, frame, cfg, FRAME_FOOTPRINT_EDITOR );
404 }
405
410
412 return new PANEL_PCBNEW_DISPLAY_ORIGIN( aParent, GetAppSettings<FOOTPRINT_EDITOR_SETTINGS>( "fpedit" ),
414
416 {
417 EDA_BASE_FRAME* frame = aKiway->Player( FRAME_FOOTPRINT_EDITOR, false );
418
419 if( !frame )
420 frame = aKiway->Player( FRAME_FOOTPRINT_VIEWER, false );
421
422 if( !frame )
423 frame = aKiway->Player( FRAME_PCB_EDITOR, false );
424
425 if( frame )
426 SetUserUnits( frame->GetUserUnits() );
427
428 return new PANEL_EDIT_OPTIONS( aParent, this, frame, true );
429 }
430
432 {
433 EDA_BASE_FRAME* frame = aKiway->Player( FRAME_FOOTPRINT_EDITOR, false );
434
435 if( !frame )
436 frame = aKiway->Player( FRAME_FOOTPRINT_VIEWER, false );
437
438 if( !frame )
439 frame = aKiway->Player( FRAME_PCB_EDITOR, false );
440
441 if( frame )
442 SetUserUnits( frame->GetUserUnits() );
443
444 return new PANEL_FP_EDITOR_FIELD_DEFAULTS( aParent );
445 }
446
448 {
449 EDA_BASE_FRAME* frame = aKiway->Player( FRAME_FOOTPRINT_EDITOR, false );
450
451 if( !frame )
452 frame = aKiway->Player( FRAME_FOOTPRINT_VIEWER, false );
453
454 if( !frame )
455 frame = aKiway->Player( FRAME_PCB_EDITOR, false );
456
457 if( frame )
458 SetUserUnits( frame->GetUserUnits() );
459
460 return new PANEL_FP_EDITOR_GRAPHICS_DEFAULTS( aParent, this );
461 }
462
464 return new class PANEL_FP_USER_LAYER_NAMES( aParent );
465
467 {
468 APP_SETTINGS_BASE* cfg = GetAppSettings<FOOTPRINT_EDITOR_SETTINGS>( "fpedit" );
469 TOOLBAR_SETTINGS* tb = GetToolbarSettings<FOOTPRINT_EDIT_TOOLBAR_SETTINGS>( "fpedit-toolbars" );
470
471 std::vector<TOOL_ACTION*> actions;
472 std::vector<ACTION_TOOLBAR_CONTROL*> controls;
473
474 for( TOOL_ACTION* action : ACTION_MANAGER::GetActionList() )
475 actions.push_back( action );
476
477 for( ACTION_TOOLBAR_CONTROL* control : ACTION_TOOLBAR::GetCustomControlList( FRAME_FOOTPRINT_EDITOR ) )
478 controls.push_back( control );
479
480 return new PANEL_TOOLBAR_CUSTOMIZATION( aParent, cfg, tb, FRAME_FOOTPRINT_EDITOR, actions, controls );
481 }
482
483 case PANEL_FP_COLORS:
484 return new PANEL_FP_EDITOR_COLOR_SETTINGS( aParent );
485
487 return new PANEL_DISPLAY_OPTIONS( aParent, GetAppSettings<PCBNEW_SETTINGS>( "pcbnew" ) );
488
489 case PANEL_PCB_GRIDS:
490 {
491 PCBNEW_SETTINGS* cfg = GetAppSettings<PCBNEW_SETTINGS>( "pcbnew" );
492 EDA_BASE_FRAME* frame = aKiway->Player( FRAME_PCB_EDITOR, false );
493
494 if( !frame )
495 frame = aKiway->Player( FRAME_FOOTPRINT_EDITOR, false );
496
497 if( !frame )
498 frame = aKiway->Player( FRAME_FOOTPRINT_VIEWER, false );
499
500 if( frame )
501 SetUserUnits( frame->GetUserUnits() );
502
503 return new PANEL_GRID_SETTINGS( aParent, this, frame, cfg, FRAME_PCB_EDITOR );
504 }
505
509
511 return new PANEL_PCBNEW_DISPLAY_ORIGIN( aParent, GetAppSettings<PCBNEW_SETTINGS>( "pcbnew" ),
513
515 {
516 EDA_BASE_FRAME* frame = aKiway->Player( FRAME_PCB_EDITOR, false );
517
518 if( !frame )
519 frame = aKiway->Player( FRAME_FOOTPRINT_EDITOR, false );
520
521 if( !frame )
522 frame = aKiway->Player( FRAME_FOOTPRINT_VIEWER, false );
523
524 if( frame )
525 SetUserUnits( frame->GetUserUnits() );
526
527 return new PANEL_EDIT_OPTIONS( aParent, this, frame, false );
528 }
529
530 case PANEL_PCB_COLORS:
531 {
532 BOARD* board = nullptr;
533 EDA_BASE_FRAME* boardProvider = aKiway->Player( FRAME_PCB_EDITOR, false );
534
535 if( boardProvider )
536 board = static_cast<PCB_EDIT_FRAME*>( boardProvider )->GetBoard();
537
538 return new PANEL_PCBNEW_COLOR_SETTINGS( aParent, board );
539 }
540
542 {
543 APP_SETTINGS_BASE* cfg = GetAppSettings<PCBNEW_SETTINGS>( "pcbnew" );
544 TOOLBAR_SETTINGS* tb = GetToolbarSettings<PCB_EDIT_TOOLBAR_SETTINGS>( "pcbnew-toolbars" );
545
546 std::vector<TOOL_ACTION*> actions;
547 std::vector<ACTION_TOOLBAR_CONTROL*> controls;
548
549 for( TOOL_ACTION* action : ACTION_MANAGER::GetActionList() )
550 actions.push_back( action );
551
552 for( ACTION_TOOLBAR_CONTROL* control : ACTION_TOOLBAR::GetCustomControlList( FRAME_PCB_EDITOR ) )
553 controls.push_back( control );
554
555 return new PANEL_TOOLBAR_CUSTOMIZATION( aParent, cfg, tb, FRAME_PCB_EDITOR, actions, controls );
556 }
557
559 return new PANEL_PCBNEW_ACTION_PLUGINS( aParent );
560
562 return new PANEL_3D_DISPLAY_OPTIONS( aParent );
563
564 case PANEL_3DV_OPENGL:
565 return new PANEL_3D_OPENGL_OPTIONS( aParent );
566
568 return new PANEL_3D_RAYTRACING_OPTIONS( aParent );
569
571 {
572 APP_SETTINGS_BASE* cfg = GetAppSettings<EDA_3D_VIEWER_SETTINGS>( "3d_viewer" );
573 TOOLBAR_SETTINGS* tb = GetToolbarSettings<EDA_3D_VIEWER_TOOLBAR_SETTINGS>( "3d_viewer-toolbars" );
574
575 std::vector<TOOL_ACTION*> actions;
576 std::vector<ACTION_TOOLBAR_CONTROL*> controls;
577
578 for( TOOL_ACTION* action : ACTION_MANAGER::GetActionList() )
579 actions.push_back( action );
580
581 for( ACTION_TOOLBAR_CONTROL* control : ACTION_TOOLBAR::GetCustomControlList( FRAME_PCB_DISPLAY3D ) )
582 controls.push_back( control );
583
584 return new PANEL_TOOLBAR_CUSTOMIZATION( aParent, cfg, tb, FRAME_PCB_DISPLAY3D, actions, controls );
585 }
586
587 default:
588 return nullptr;
589 }
590 }
591
602 void* IfaceOrAddress( int aDataId ) override
603 {
604 switch( aDataId )
605 {
607 {
608 // This is the mechanism by which FOOTPRINT_SELECT_WIDGET can get access to the adapter
609 // without directly linking to pcbnew or pcbcommon, going through PROJECT::FootprintLibAdapter
610 PROJECT* project = nullptr;
611
612 if( wxTheApp )
613 {
614 wxWindow* focus = wxWindow::FindFocus();
615 wxWindow* top = focus ? wxGetTopLevelParent( focus ) : wxTheApp->GetTopWindow();
616
617 if( top )
618 {
619 if( KIWAY_HOLDER* holder = dynamic_cast<KIWAY_HOLDER*>( top ) )
620 project = &holder->Prj();
621 }
622 }
623
624 if( !project )
626
628 }
629
631 {
632 // Return function pointer for filtering footprints
633 // Signature: wxString (*)(const wxString& aFilterJson)
634 return reinterpret_cast<void*>( &filterFootprints );
635 }
636
638 {
639 // Start the background load of the libraries filtered by filterFootprints
640 // Signature: void (*)()
641 return reinterpret_cast<void*>( &startFootprintLibraryLoad );
642 }
643
645 {
646 // Report how far the load started by startFootprintMatchLoad() has got
647 // Signature: float (*)()
648 return reinterpret_cast<void*>( &footprintLibraryLoadProgress );
649 }
650
652 return reinterpret_cast<void*>( &pcbnewMergeExport );
653
655 return reinterpret_cast<void*>( &pcbnewOpenDiffDialogExport );
656
657 default:
658 return nullptr;
659 }
660 }
661
663 PCBNEW_JOBS_HANDLER* JobHandler() const { return m_jobHandler.get(); }
664
670 void SaveFileAs( const wxString& aProjectBasePath, const wxString& aSrcProjectName,
671 const wxString& aNewProjectBasePath, const wxString& aNewProjectName,
672 const wxString& aSrcFilePath, wxString& aErrors ) override;
673
674 int HandleJob( JOB* aJob, REPORTER* aReporter, PROGRESS_REPORTER* aProgressReporter ) override;
675
676 bool HandleJobConfig( JOB* aJob, wxWindow* aParent ) override;
677
678 bool HandleApiOpenDocument( const DOCUMENT_SPEC& aSpec,
679 KICAD_API_SERVER* aServer,
680 wxString* aError ) override;
681
682 bool HandleApiCloseDocument( const DOCUMENT_SPEC& aSpec,
683 KICAD_API_SERVER* aServer,
684 wxString* aError ) override;
685
686 bool handleOpenPcb( const wxString& aPath, KICAD_API_SERVER* aServer, wxString* aError );
687
688 bool handleCreatePcb( const wxString& aPath, KICAD_API_SERVER* aServer, wxString* aError );
689
690 bool handleOpenFootprint( const wxString& aProjectPath, const wxString& aLibIdStr, KICAD_API_SERVER* aServer,
691 wxString* aError );
692
693 void PreloadLibraries( KIWAY* aKiway ) override;
694 void ProjectChanged() override;
695 void CancelPreload( bool aBlock = true ) override;
696 void RegisterLibraryHandlers( KICAD_API_SERVER* aServer ) override;
697 bool LoadAllLibraries() override;
698
699
700private:
701 std::unique_ptr<PCBNEW_JOBS_HANDLER> m_jobHandler;
702 std::shared_ptr<BACKGROUND_JOB> m_libraryPreloadBackgroundJob;
703 std::future<void> m_libraryPreloadReturn;
705 std::atomic_bool m_libraryPreloadAbort;
706
707 void closeBoard( KICAD_API_SERVER* aServer );
708 void closeFootprint( KICAD_API_SERVER* aServer );
709
710 KIWAY* m_kiway = nullptr;
711 std::shared_ptr<HEADLESS_PCB_CONTEXT> m_openContext;
712 std::unique_ptr<API_HANDLER_PCB> m_openHandler;
713 std::shared_ptr<HEADLESS_FOOTPRINT_CONTEXT> m_openFpContext;
714 std::unique_ptr<API_HANDLER_FOOTPRINT> m_openFpHandler;
715 std::unique_ptr<API_HANDLER_FP_LIBRARIES> m_apiHandlerFpLibs;
716
717} kiface( "pcbnew", KIWAY::FACE_PCB );
718
719
720int pcbnewMergeExport( int aKind, const wxString& aAncestor, const wxString& aOurs,
721 const wxString& aTheirs, const wxString& aOutput, bool aInteractive,
722 bool aSingleFile, REPORTER* aReporter )
723{
724 return kiface.JobHandler()->RunMerge( static_cast<KICAD_DIFF::DOC_KIND>( aKind ), aAncestor,
725 aOurs, aTheirs, aOutput, aInteractive, aSingleFile,
726 aReporter );
727}
728
729
730int pcbnewOpenDiffDialogExport( int aKind, const wxString& aFileA, const wxString& aFileB,
731 const wxString& aLabelA, const wxString& aLabelB,
732 wxWindow* aParent, REPORTER* aReporter )
733{
734 return kiface.JobHandler()->OpenDiffDialog( static_cast<KICAD_DIFF::DOC_KIND>( aKind ), aFileA,
735 aFileB, aLabelA, aLabelB, aParent, aReporter );
736}
737
738} // namespace
739
740
741using namespace PCB;
742
743
745
746
747// KIFACE_GETTER's actual spelling is a substitution macro found in kiway.h.
748// KIFACE_GETTER will not have name mangling due to declaration in kiway.h.
749KIFACE_API KIFACE* KIFACE_GETTER( int* aKIFACEversion, int aKiwayVersion, PGM_BASE* aProgram )
750{
751 return &kiface;
752}
753
754
755bool IFACE::OnKifaceStart( PGM_BASE* aProgram, int aCtlBits, KIWAY* aKiway )
756{
757 // This is process-level-initialization, not project-level-initialization of the DSO.
758 // Do nothing in here pertinent to a project!
760
761 SETTINGS_MANAGER& mgr = aProgram->GetSettingsManager();
762
765
766 // We intentionally register KifaceSettings after FOOTPRINT_EDITOR_SETTINGS and EDA_3D_VIEWER_SETTINGS
767 // In legacy configs, many settings were in a single editor config and the migration routine
768 // for the main editor file will try and call into the now separate settings stores
769 // to move the settings into them
771
772 // Register the footprint editor settings as well because they share a KiFACE and need to be
773 // loaded prior to use to avoid threading deadlocks
775
776 start_common( aCtlBits );
777
778 m_kiway = aKiway;
779
780 m_jobHandler = std::make_unique<PCBNEW_JOBS_HANDLER>( aKiway );
781
783 {
784 m_jobHandler->SetReporter( &CLI_REPORTER::GetInstance() );
785 m_jobHandler->SetProgressReporter( &CLI_PROGRESS_REPORTER::GetInstance() );
786 }
787
788 // Register the PCB and footprint merge drivers with libgit2 so
789 // .gitattributes entries `merge=kicad-pcb` and `merge=kicad-fp` route
790 // through our 3-way merge pipeline. git_merge_driver_register is not
791 // thread-safe; this runs once at kiface init before any background work
792 // spawns. The registry is idempotent so re-loads of the kiface are safe.
795
796 if( Pgm().ApiServerOrNull() )
797 {
798 m_apiHandlerFpLibs = std::make_unique<API_HANDLER_FP_LIBRARIES>();
800 }
801
802 return true;
803}
804
805
807{
808 closeBoard( nullptr );
809 closeFootprint( nullptr );
810}
811
812
814{
815#if defined( KICAD_NATIVE_MODEL_PREVIEW ) && defined( __WXGTK3__ )
816 ShutdownNativeModelFilePickerGtk();
817#endif
818
820 {
821 if( Pgm().ApiServerOrNull() )
823
824 m_apiHandlerFpLibs.reset();
825 }
826
827 // Release the CLI-cached board while the static DRC_ITEM tables it serializes against are
828 // still alive; deferring to static teardown crashes reading dangling severity keys
829 if( m_jobHandler )
830 m_jobHandler->ClearCachedBoard();
831
832 end_common();
833}
834
835
836void IFACE::SaveFileAs( const wxString& aProjectBasePath, const wxString& aSrcProjectName,
837 const wxString& aNewProjectBasePath, const wxString& aNewProjectName,
838 const wxString& aSrcFilePath, wxString& aErrors )
839{
840 wxFileName destFile( aSrcFilePath );
841 wxString destPath = destFile.GetPathWithSep();
842 wxUniChar pathSep = wxFileName::GetPathSeparator();
843 wxString ext = destFile.GetExt();
844
845 if( destPath.StartsWith( aProjectBasePath + pathSep ) )
846 destPath.Replace( aProjectBasePath, aNewProjectBasePath, false );
847
848 wxString srcProjectFootprintLib = pathSep + aSrcProjectName + wxT( ".pretty" ) + pathSep;
849 wxString newProjectFootprintLib = pathSep + aNewProjectName + wxT( ".pretty" ) + pathSep;
850
851 destPath.Replace( srcProjectFootprintLib, newProjectFootprintLib, true );
852
853 destFile.SetPath( destPath );
854
857 {
858 if( destFile.GetName() == aSrcProjectName )
859 destFile.SetName( aNewProjectName );
860
861 CopySexprFile( aSrcFilePath, destFile.GetFullPath(),
862 [&]( const std::string& token, wxString& value )
863 {
864 if( token == "sheetfile" )
865 {
866 for( const wxString extension : { wxT( ".sch" ), wxT( ".kicad_sch" ) } )
867 {
868 if( value == aSrcProjectName + extension )
869 {
870 value = aNewProjectName + extension;
871 return true;
872 }
873 else if( value == aProjectBasePath + "/" + aSrcProjectName + extension )
874 {
875 value = aNewProjectBasePath + "/" + aNewProjectName + extension;
876 return true;
877 }
878 else if( value.StartsWith( aProjectBasePath ) )
879 {
880 value.Replace( aProjectBasePath, aNewProjectBasePath, false );
881 return true;
882 }
883 }
884 }
885
886 return false;
887 },
888 aErrors );
889 }
890 else if( ext == FILEEXT::LegacyPcbFileExtension )
891 {
892 if( destFile.GetName() == aSrcProjectName )
893 destFile.SetName( aNewProjectName );
894
895 KiCopyFile( aSrcFilePath, destFile.GetFullPath(), aErrors );
896 }
899 {
900 // Footprints are not project-specific. Keep their source names.
901 KiCopyFile( aSrcFilePath, destFile.GetFullPath(), aErrors );
902 }
904 {
905 // TODO
906 }
907 else if( ext == wxT( "rpt" ) )
908 {
909 // DRC must be the "gold standard". Since we can't guarantee that there aren't
910 // any non-deterministic cases in the save-as algorithm, we don't want to certify
911 // the result with the source's DRC report. Therefore copy it under the old
912 // name.
913 KiCopyFile( aSrcFilePath, destFile.GetFullPath(), aErrors );
914 }
915 else if( destFile.GetName() == FILEEXT::FootprintLibraryTableFileName )
916 {
917 wxFileName libTableFn( aSrcFilePath );
918 LIBRARY_TABLE libTable( libTableFn, LIBRARY_TABLE_SCOPE::PROJECT );
919 libTable.SetPath( destFile.GetFullPath() );
920 libTable.SetType( LIBRARY_TABLE_TYPE::FOOTPRINT );
921
922 for( LIBRARY_TABLE_ROW& row : libTable.Rows() )
923 {
924 wxString uri = row.URI();
925
926 uri.Replace( wxT( "/" ) + aSrcProjectName + wxT( ".pretty" ),
927 wxT( "/" ) + aNewProjectName + wxT( ".pretty" ) );
928
929 row.SetURI( uri );
930 }
931
932 libTable.Save().map_error(
933 [&]( const LIBRARY_ERROR& aError )
934 {
935 wxString msg;
936
937 if( !aErrors.empty() )
938 aErrors += wxT( "\n" );
939
940 msg.Printf( _( "Cannot copy file '%s'." ), destFile.GetFullPath() );
941 aErrors += msg;
942 } );
943 }
944 else
945 {
946 wxFAIL_MSG( wxT( "Unexpected filetype for Pcbnew::SaveFileAs()" ) );
947 }
948}
949
950
951int IFACE::HandleJob( JOB* aJob, REPORTER* aReporter, PROGRESS_REPORTER* aProgressReporter )
952{
953 return m_jobHandler->RunJob( aJob, aReporter, aProgressReporter );
954}
955
956
957bool IFACE::HandleJobConfig( JOB* aJob, wxWindow* aParent )
958{
959 return m_jobHandler->HandleJobConfig( aJob, aParent );
960}
961
962
964{
965 if( m_openHandler )
966 {
967 if( aServer )
968 aServer->DeregisterHandler( m_openHandler.get() );
969
970 m_openHandler.reset();
971 }
972
973 m_openContext.reset();
974
975 // The jobs handler caches the last-loaded board. Clear it so the next job
976 // uses the board from the newly opened document rather than a stale copy.
977 if( m_jobHandler )
978 m_jobHandler->ClearCachedBoard();
979}
980
981
983{
984 if( m_openFpHandler )
985 {
986 if( aServer )
987 aServer->DeregisterHandler( m_openFpHandler.get() );
988
989 m_openFpHandler.reset();
990 }
991
992 m_openFpContext.reset();
993}
994
995
996bool IFACE::HandleApiOpenDocument( const DOCUMENT_SPEC& aSpec, KICAD_API_SERVER* aServer, wxString* aError )
997{
998 wxCHECK( aServer, false );
999
1000 if( aSpec.kind == DOCUMENT_SPEC::KIND::LIBID_KIND )
1001 return handleOpenFootprint( aSpec.path, aSpec.libId.GetUniStringLibId(), aServer, aError );
1002
1003 if( aSpec.kind == DOCUMENT_SPEC::KIND::CREATE_KIND )
1004 return handleCreatePcb( aSpec.path, aServer, aError );
1005
1006 if( aSpec.path.IsEmpty() )
1007 {
1008 if( aError )
1009 *aError = wxS( "No path specified to open" );
1010
1011 return false;
1012 }
1013
1014 return handleOpenPcb( aSpec.path, aServer, aError );
1015}
1016
1017
1018bool IFACE::handleOpenFootprint( const wxString& aProjectPath, const wxString& aLibIdStr, KICAD_API_SERVER* aServer,
1019 wxString* aError )
1020{
1021 LIB_ID fpid;
1022
1023 if( fpid.Parse( aLibIdStr ) >= 0 )
1024 {
1025 if( aError )
1026 *aError = wxString::Format( wxS( "Invalid footprint LIB_ID: %s" ), aLibIdStr );
1027
1028 return false;
1029 }
1030
1031 wxFileName projectPath( aProjectPath );
1032 projectPath.MakeAbsolute();
1033
1034 SETTINGS_MANAGER& settingsManager = Pgm().GetSettingsManager();
1035
1036 if( !settingsManager.LoadProject( projectPath.GetFullPath(), true ) )
1037 {
1038 wxLogTrace( traceApi, "Warning: no project file found for %s", aProjectPath );
1039 }
1040
1041 PROJECT* project = settingsManager.GetProject( projectPath.GetFullPath() );
1042
1043 if( !project )
1044 {
1045 if( aError )
1046 *aError = wxString::Format( wxS( "Error loading project for %s" ), aProjectPath );
1047
1048 return false;
1049 }
1050
1051 std::shared_ptr<HEADLESS_FOOTPRINT_CONTEXT> newContext;
1052
1053 try
1054 {
1056 adapter->AsyncLoad();
1057 adapter->BlockUntilLoaded();
1058 std::unique_ptr<FOOTPRINT> footprint( adapter->LoadFootprintWithOptionalNickname( fpid, true ) );
1059
1060 if( !footprint )
1061 {
1062 if( aError )
1063 *aError = wxString::Format( wxS( "Footprint not found: %s" ), aLibIdStr );
1064
1065 return false;
1066 }
1067
1068 newContext = std::make_shared<HEADLESS_FOOTPRINT_CONTEXT>(
1069 std::move( footprint ), fpid, project, GetAppSettings<FOOTPRINT_EDITOR_SETTINGS>( "fpedit" ), m_kiway );
1070 }
1071 catch( ... )
1072 {
1073 if( aError )
1074 *aError = wxString::Format( wxS( "Failed to load footprint: %s" ), aLibIdStr );
1075
1076 return false;
1077 }
1078
1079 closeFootprint( aServer );
1080
1081 if( !m_apiHandlerFpLibs )
1082 {
1083 m_apiHandlerFpLibs = std::make_unique<API_HANDLER_FP_LIBRARIES>();
1084 aServer->RegisterHandler( m_apiHandlerFpLibs.get() );
1085 }
1086
1087 m_openFpContext = std::move( newContext );
1088
1089 m_openFpHandler = std::make_unique<API_HANDLER_FOOTPRINT>( m_openFpContext, nullptr );
1090 aServer->RegisterHandler( m_openFpHandler.get() );
1091
1092 return true;
1093}
1094
1095
1096bool IFACE::handleOpenPcb( const wxString& aPath, KICAD_API_SERVER* aServer, wxString* aError )
1097{
1098 wxFileName projectPath( aPath );
1099
1100 if( projectPath.GetExt() == FILEEXT::KiCadPcbFileExtension )
1101 projectPath.SetExt( FILEEXT::ProjectFileExtension );
1102 else if( projectPath.GetExt() != FILEEXT::ProjectFileExtension )
1103 projectPath.SetExt( FILEEXT::ProjectFileExtension );
1104
1105 projectPath.MakeAbsolute();
1106
1107 // We currently only support one document per type (and each needs to come from
1108 // the same project). This will need evolution once we support MDI and multi-project.
1109 closeBoard( aServer );
1110
1111 SETTINGS_MANAGER& settingsManager = Pgm().GetSettingsManager();
1112
1113 // Reuse an already-loaded project if one exists for this path.
1114 PROJECT* project = settingsManager.GetProject( projectPath.GetFullPath() );
1115
1116 if( !project )
1117 {
1118 if( !settingsManager.LoadProject( projectPath.GetFullPath(), true ) )
1119 wxLogTrace( traceApi, "Warning: no project file found for %s", aPath );
1120
1121 project = settingsManager.GetProject( projectPath.GetFullPath() );
1122 }
1123
1124 if( !project )
1125 {
1126 if( aError )
1127 *aError = wxString::Format( wxS( "Error loading project for %s" ), aPath );
1128
1129 return false;
1130 }
1131
1132 wxFileName boardPath( projectPath );
1133 boardPath.SetExt( FILEEXT::KiCadPcbFileExtension );
1134
1135 if( !boardPath.FileExists() )
1136 {
1137 if( aError )
1138 *aError = wxString::Format( wxS( "File not found: %s" ), aPath );
1139
1140 return false;
1141 }
1142
1143 PCB_IO_MGR::PCB_FILE_T pluginType =
1145
1146 if( pluginType == PCB_IO_MGR::FILE_TYPE_NONE )
1147 {
1148 if( aError )
1149 *aError = wxString::Format( wxS( "%s is not a recognized file type" ), aPath );
1150
1151 return false;
1152 }
1153
1154 std::shared_ptr<HEADLESS_PCB_CONTEXT> newContext;
1155
1156 try
1157 {
1158 std::unique_ptr<BOARD> loadedBoard = BOARD_LOADER::Load( boardPath.GetFullPath(), pluginType, project );
1159
1160 if( !loadedBoard )
1161 {
1162 if( aError )
1163 *aError = wxS( "Failed to load board" );
1164
1165 return false;
1166 }
1167
1168 newContext = std::make_shared<HEADLESS_PCB_CONTEXT>( std::move( loadedBoard ), project,
1170 }
1171 catch( const IO_ERROR& ioe )
1172 {
1173 if( aError )
1174 *aError = wxString::Format( wxS( "Failed to load board: %s" ), ioe.What() );
1175
1176 return false;
1177 }
1178 catch( ... )
1179 {
1180 if( aError )
1181 *aError = wxS( "Failed to load board" );
1182
1183 return false;
1184 }
1185
1186 if( !m_apiHandlerFpLibs )
1187 {
1188 m_apiHandlerFpLibs = std::make_unique<API_HANDLER_FP_LIBRARIES>();
1189 aServer->RegisterHandler( m_apiHandlerFpLibs.get() );
1190 }
1191
1192 m_openContext = std::move( newContext );
1193
1194 m_openHandler = std::make_unique<API_HANDLER_PCB>( m_openContext, nullptr );
1195 aServer->RegisterHandler( m_openHandler.get() );
1196
1197 return true;
1198}
1199
1200
1201bool IFACE::handleCreatePcb( const wxString& aPath, KICAD_API_SERVER* aServer, wxString* aError )
1202{
1203 wxFileName boardPath( aPath );
1204 boardPath.MakeAbsolute();
1205
1206 wxFileName projectPath( boardPath );
1207 projectPath.SetExt( FILEEXT::ProjectFileExtension );
1208
1209 if( m_openContext && m_openContext->IsContentModified() )
1210 {
1211 if( aError )
1212 *aError = wxS( "The current board has unsaved changes; save or revert it first" );
1213
1214 return false;
1215 }
1216
1217 closeBoard( aServer );
1218
1219 SETTINGS_MANAGER& settingsManager = Pgm().GetSettingsManager();
1220
1221 PROJECT* project = settingsManager.GetProject( projectPath.GetFullPath() );
1222
1223 if( !project )
1224 {
1225 settingsManager.LoadProject( projectPath.GetFullPath(), true );;
1226 project = settingsManager.GetProject( projectPath.GetFullPath() );
1227 }
1228
1229 if( !project )
1230 {
1231 if( aError )
1232 *aError = wxString::Format( wxS( "Error creating project for %s" ), aPath );
1233
1234 return false;
1235 }
1236
1237 std::shared_ptr<HEADLESS_PCB_CONTEXT> newContext;
1238
1239 try
1240 {
1241 std::unique_ptr<BOARD> newBoard = BOARD_LOADER::CreateEmptyBoard( project );
1242
1243 if( !newBoard )
1244 {
1245 if( aError )
1246 *aError = wxS( "Failed to create board" );
1247
1248 return false;
1249 }
1250
1251 newBoard->SetFileName( boardPath.GetFullPath() );
1252
1253 newContext = std::make_shared<HEADLESS_PCB_CONTEXT>( std::move( newBoard ), project,
1255 }
1256 catch( ... )
1257 {
1258 if( aError )
1259 *aError = wxS( "Failed to create board" );
1260
1261 return false;
1262 }
1263
1264 m_openContext = std::move( newContext );
1265
1266 if( !m_apiHandlerFpLibs )
1267 {
1268 m_apiHandlerFpLibs = std::make_unique<API_HANDLER_FP_LIBRARIES>();
1269 aServer->RegisterHandler( m_apiHandlerFpLibs.get() );
1270 }
1271
1272 m_openHandler = std::make_unique<API_HANDLER_PCB>( m_openContext, nullptr );
1273 aServer->RegisterHandler( m_openHandler.get() );
1274
1275 return true;
1276}
1277
1278
1279bool IFACE::HandleApiCloseDocument( const DOCUMENT_SPEC& aSpec, KICAD_API_SERVER* aServer, wxString* aError )
1280{
1281 wxCHECK( aServer, false );
1282
1283 if( aSpec.kind == DOCUMENT_SPEC::KIND::LIBID_KIND )
1284 {
1285 if( !m_openFpContext )
1286 {
1287 if( aError )
1288 *aError = wxS( "No footprint is currently open" );
1289
1290 return false;
1291 }
1292
1293 if( aSpec.libId.IsValid() && m_openFpContext->GetLoadedFPID() != aSpec.libId )
1294 {
1295 if( aError )
1296 *aError = wxS( "Requested document does not match the open document" );
1297
1298 return false;
1299 }
1300
1301 closeFootprint( aServer );
1302 return true;
1303 }
1304
1305 if( !m_openContext )
1306 {
1307 if( aError )
1308 *aError = wxS( "No board is currently open" );
1309
1310 return false;
1311 }
1312
1313 if( !aSpec.path.IsEmpty() )
1314 {
1315 wxFileName currentBoard( m_openContext->GetCurrentFileName() );
1316
1317 if( currentBoard.GetFullName() != wxFileName( aSpec.path ).GetFullName() )
1318 {
1319 if( aError )
1320 *aError = wxS( "Requested document does not match the open document" );
1321
1322 return false;
1323 }
1324 }
1325
1326 closeBoard( aServer );
1327 return true;
1328}
1329
1330
1332{
1333 constexpr static int interval = 150;
1334 constexpr static int timeLimit = 120000;
1335
1336 wxCHECK( aKiway, /* void */ );
1337
1338 // Use compare_exchange to atomically check and set the flag to prevent race conditions
1339 // when PreloadLibraries is called multiple times concurrently (e.g., from project manager
1340 // and pcb editor both scheduling via CallAfter)
1341 bool expected = false;
1342
1343 if( !m_libraryPreloadInProgress.compare_exchange_strong( expected, true ) )
1344 return;
1345
1347
1349 Pgm().GetBackgroundJobMonitor().Create( _( "Loading Footprint Libraries" ) );
1350
1351 auto preload =
1352 [this, aKiway]() -> void
1353 {
1354 std::shared_ptr<BACKGROUND_JOB_REPORTER> reporter =
1356
1358
1359 int elapsed = 0;
1360 bool aborted = false;
1361
1362 reporter->Report( _( "Loading Footprint Libraries" ) );
1363 adapter->AsyncLoad();
1364
1365 while( true )
1366 {
1367 if( m_libraryPreloadAbort.load() )
1368 {
1369 m_libraryPreloadAbort.store( false );
1370 aborted = true;
1371 break;
1372 }
1373
1374 std::this_thread::sleep_for( std::chrono::milliseconds( interval ) );
1375
1376 if( std::optional<float> loadStatus = adapter->AsyncLoadProgress() )
1377 {
1378 float progress = *loadStatus;
1379 reporter->SetCurrentProgress( progress );
1380
1381 if( progress >= 1 )
1382 break;
1383 }
1384 else
1385 {
1386 reporter->SetCurrentProgress( 1 );
1387 break;
1388 }
1389
1390 elapsed += interval;
1391
1392 if( elapsed > timeLimit )
1393 break;
1394 }
1395
1396 // AbortAsyncLoad() sets the adapter's worker abort flag and then blocks,
1397 // so workers exit at their next checkpoint. BlockUntilLoaded() alone just
1398 // waits for each future to complete naturally, which can hang indefinitely
1399 // if a worker is stuck on a stalled network or filesystem operation.
1400 if( aborted )
1401 adapter->AbortAsyncLoad();
1402 else
1403 adapter->BlockUntilLoaded();
1404
1405 // If aborted, skip operations that use the adapter since the project may have changed
1406 // and the adapter's project reference could be stale. This prevents use-after-free
1407 // crashes when switching projects during library preload.
1408 if( !aborted )
1409 {
1410 // Report library load errors from adapter
1412 }
1413 else
1414 {
1415 wxLogTrace( traceLibraries, "pcbnew PreloadLibraries: aborted, skipping footprint processing" );
1416 }
1417
1418 m_libraryPreloadAbort.store( false );
1421 m_libraryPreloadInProgress.store( false );
1422
1423 // Only send reload notifications if we weren't aborted
1424 if( !aborted )
1425 {
1426 std::string payload = "";
1427 aKiway->ExpressMail( FRAME_PCB_EDITOR, MAIL_RELOAD_LIB, payload, nullptr, true );
1428 aKiway->ExpressMail( FRAME_FOOTPRINT_EDITOR, MAIL_RELOAD_LIB, payload, nullptr, true );
1429 aKiway->ExpressMail( FRAME_CVPCB, MAIL_RELOAD_LIB, payload, nullptr, true );
1430 }
1431 };
1432
1433 std::future<void> preloadFuture = std::async( std::launch::async, preload );
1434 m_libraryPreloadReturn = std::move( preloadFuture );
1435}
1436
1437
1439{
1440 if( m_libraryPreloadInProgress.load() )
1441 m_libraryPreloadAbort.store( true );
1442}
1443
1444
1445void IFACE::CancelPreload( bool aBlock )
1446{
1447 if( m_libraryPreloadInProgress.load() )
1448 {
1449 m_libraryPreloadAbort.store( true );
1450
1451 if( aBlock )
1453 }
1454}
1455
1456
1458{
1459 wxCHECK_RET( aServer, "no API server provided" );
1460
1461 if( !m_apiHandlerFpLibs )
1462 {
1463 m_apiHandlerFpLibs = std::make_unique<API_HANDLER_FP_LIBRARIES>();
1464 aServer->RegisterHandler( m_apiHandlerFpLibs.get() );
1465 }
1466}
1467
1468
1470{
1472
1473 if( !adapter )
1474 return false;
1475
1476 adapter->AsyncLoad();
1477 return true;
1478}
const char * name
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:128
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.
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 std::unique_ptr< BOARD > CreateEmptyBoard(PROJECT *aProject)
static std::unique_ptr< BOARD > Load(const wxString &aFileName, PCB_IO_MGR::PCB_FILE_T aFormat, PROJECT *aProject, const OPTIONS &aOptions)
static CLI_PROGRESS_REPORTER & GetInstance()
static CLI_REPORTER & GetInstance()
Definition reporter.cpp:261
static bool IsOnStandardSocketPath()
static void AnnounceToPrimary(FRAME_T aFrameType)
The base frame for deriving all KiCad main window classes.
SNAP_INFERENCE_SETTINGS m_SnapInference
An interface to the global shared library manager that is schematic-specific and linked to one projec...
std::vector< FOOTPRINT * > GetFootprints(const wxString &aNickname, bool aBestEfforts=false)
Retrieves a list of footprints contained in a given loaded library.
FOOTPRINT * LoadFootprintWithOptionalNickname(const LIB_ID &aFootprintId, bool aKeepUUID)
Load a footprint having aFootprintId with possibly an empty nickname.
static FOOTPRINT_PREVIEW_PANEL * New(KIWAY *aKiway, wxWindow *aParent, UNITS_PROVIDER *aUnitsProvider)
Component library viewer main window.
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
virtual const wxString What() const
A composite of Problem() and Where()
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:64
void InitSettings(APP_SETTINGS_BASE *aSettings)
Definition kiface_base.h:94
void end_common()
Common things to do for a top program module, during OnKifaceEnd();.
APP_SETTINGS_BASE * KifaceSettings() const
Definition kiface_base.h:92
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)
libgit2 merge-driver apply callback shim.
static int Apply(const git_merge_driver_source *aSrc, const char **aPathOut, unsigned int *aModeOut, git_buf *aMergedOut)
A mix in class which holds the location of a wxWindow's KIWAY.
void SetKiway(wxWindow *aDest, KIWAY *aKiway)
It is only used for debugging, since "this" is not a wxWindow*.
A minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the same KiCad...
Definition kiway.h:348
virtual KIWAY_PLAYER * Player(FRAME_T aFrameType, bool doCreate=true, wxTopLevelWindow *aParent=nullptr)
Return the KIWAY_PLAYER* given a FRAME_T.
Definition kiway.cpp:388
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:486
FACE_T
Known KIFACE implementations.
Definition kiway.h:354
@ FACE_PCB
pcbnew DSO
Definition kiway.h:356
virtual void CommonSettingsChanged(int aFlags=0)
Call CommonSettingsChanged() on all KIWAY_PLAYERs.
Definition kiway.cpp:580
virtual PROJECT & Prj() const
Return the PROJECT associated with this KIWAY.
Definition kiway.cpp:201
std::optional< float > AsyncLoadProgress() const
Returns the fraction of the libraries queued by AsyncLoad() that have been processed,...
std::vector< KI_ERROR > GetLibraryLoadErrors() const
Returns all library load errors as newline-separated strings for display.
void AbortAsyncLoad()
Aborts any async load in progress; blocks until fully done aborting.
std::vector< wxString > GetLibraryNames() const
Returns a list of library nicknames that are available (skips any that failed to load)
void AsyncLoad()
Loads all available libraries for this adapter type in the background.
A logical library item identifier and consists of various portions much like a URI.
Definition lib_id.h:45
int Parse(const UTF8 &aId, bool aFix=false)
Parse LIB_ID with the information from aId.
Definition lib_id.cpp:65
bool IsValid() const
Check if this LID_ID is valid.
Definition lib_id.h:168
wxString GetUniStringLibId() const
Definition lib_id.h:144
MAGNETIC_SETTINGS m_MagneticItems
SNAP_INFERENCE_SETTINGS m_SnapInference
The main frame for Pcbnew.
PCB_FILE_T
The set of file types that the PCB_IO_MGR knows about, and for which there has been a plugin written,...
Definition pcb_io_mgr.h:52
static PCB_FILE_T FindPluginTypeFromBoardPath(const wxString &aFileName, int aCtl=0)
Return a plugin type given a path for a board file.
Container for data for KiCad programs.
Definition pgm_base.h:101
virtual BACKGROUND_JOBS_MONITOR & GetBackgroundJobMonitor() const
Definition pgm_base.h:129
void AddLibraryLoadMessages(const std::vector< KI_ERROR > &aMessages)
Add library load messages to all registered status bars.
void ClearLibraryLoadMessages()
Clear library load messages from all registered status bars.
KICAD_API_SERVER & GetApiServer()
Definition pgm_base.h:141
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition pgm_base.h:123
A progress reporter interface for use in multi-threaded environments.
static FOOTPRINT_LIBRARY_ADAPTER * FootprintLibAdapter(PROJECT *aProject)
Container for project specific data.
Definition project.h:63
A pure virtual class used to derive REPORTER objects from.
Definition reporter.h:73
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.
PROJECT & Prj() const
A helper while we are not MDI-capable – return the one and only project.
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)
Abstract pattern-matching tool and implementations.
EDA_UNITS
Definition eda_units.h:44
FRAME_T
The set of EDA_BASE_FRAME derivatives, typically stored in EDA_BASE_FRAME::m_Ident.
Definition frame_type.h:29
@ PANEL_PCB_GRIDS
Definition frame_type.h:102
@ FRAME_PCB_EDITOR
Definition frame_type.h:38
@ PANEL_3DV_TOOLBARS
Definition frame_type.h:113
@ FRAME_FOOTPRINT_VIEWER
Definition frame_type.h:41
@ PANEL_3DV_OPENGL
Definition frame_type.h:111
@ PANEL_FP_DEFAULT_GRAPHICS_VALUES
Definition frame_type.h:97
@ PANEL_PCB_TOOLBARS
Definition frame_type.h:106
@ PANEL_PCB_ORIGINS_AXES
Definition frame_type.h:108
@ FRAME_FOOTPRINT_WIZARD
Definition frame_type.h:42
@ PANEL_PCB_EDIT_OPTIONS
Definition frame_type.h:104
@ PANEL_FP_DISPLAY_OPTIONS
Definition frame_type.h:90
@ PANEL_PCB_COLORS
Definition frame_type.h:105
@ PANEL_FP_SNAPPING
Definition frame_type.h:92
@ PANEL_FP_USER_LAYER_NAMES
Definition frame_type.h:98
@ PANEL_3DV_RAYTRACING
Definition frame_type.h:112
@ DIALOG_PCB_LIBRARY_TABLE
Definition frame_type.h:133
@ FRAME_FOOTPRINT_PREVIEW
Definition frame_type.h:44
@ FRAME_FOOTPRINT_CHOOSER
Definition frame_type.h:40
@ PANEL_FP_GRIDS
Definition frame_type.h:91
@ PANEL_FP_ORIGINS_AXES
Definition frame_type.h:99
@ PANEL_PCB_DISPLAY_OPTS
Definition frame_type.h:101
@ DIALOG_CONFIGUREPATHS
Definition frame_type.h:130
@ PANEL_FP_COLORS
Definition frame_type.h:94
@ PANEL_FP_DEFAULT_FIELDS
Definition frame_type.h:96
@ FRAME_FOOTPRINT_EDITOR
Definition frame_type.h:39
@ FRAME_PCB_DISPLAY3D
Definition frame_type.h:43
@ PANEL_FP_EDIT_OPTIONS
Definition frame_type.h:93
@ PANEL_FP_TOOLBARS
Definition frame_type.h:95
@ PANEL_PCB_ACTION_PLUGINS
Definition frame_type.h:107
@ PANEL_3DV_DISPLAY_OPTIONS
Definition frame_type.h:110
@ FRAME_CVPCB
Definition frame_type.h:48
@ PANEL_PCB_SNAPPING
Definition frame_type.h:103
nlohmann::json json
Definition gerbview.cpp:50
void CopySexprFile(const wxString &aSrcPath, const wxString &aDestPath, std::function< bool(const std::string &token, wxString &value)> aCallback, wxString &aErrors)
Definition gestfich.cpp:375
void KiCopyFile(const wxString &aSrcPath, const wxString &aDestPath, wxString &aErrors)
Definition gestfich.cpp:348
static const std::string ProjectFileExtension
static const std::string LegacyPcbFileExtension
static const std::string FootprintLibraryTableFileName
static const std::string BackupFileSuffix
static const std::string LegacyFootprintLibPathExtension
static const std::string FootprintAssignmentFileExtension
static const std::string KiCadFootprintFileExtension
static const std::string KiCadPcbFileExtension
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:33
#define KIFACE_API
@ KIFACE_MERGE_DOCUMENT
int (*)( int aKind, const wxString& aAncestor, const wxString& aOurs, const wxString& aTheirs,...
Definition kiface_ids.h:55
@ KIFACE_FOOTPRINT_LIBRARY_ADAPTER
Definition kiface_ids.h:30
@ KIFACE_FOOTPRINTS_LOAD_PROGRESS
Function pointer type: float (*)() Returns how far the library loader has gotten as a fraction of the...
Definition kiface_ids.h:45
@ KIFACE_FILTER_FOOTPRINTS
Function pointer type: wxString (*)(const wxString& aFilterJson) Input JSON: {"pin_count": N,...
Definition kiface_ids.h:35
@ KIFACE_TRIGGER_FOOTPRINTS_LOAD
Function pointer type: bool (*)() Starts loading the footprint libraries in the background.
Definition kiface_ids.h:40
@ KIFACE_OPEN_DIFF_DIALOG
int (*)( int aKind, const wxString& aFileA, const wxString& aFileB, const wxString& aLabelA,...
Definition kiface_ids.h:62
#define KFCTL_CLI
Running as CLI app.
Definition kiway.h:176
#define KIFACE_GETTER
Definition kiway.h:110
#define KICTL_KICAD_ONLY
chosen file is from KiCad according to user
This file contains miscellaneous commonly used macros and functions.
@ 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.
PCB::IFACE KIFACE_BASE, UNITS_PROVIDER kiface("pcbnew", KIWAY::FACE_PCB)
static int pcbnewOpenDiffDialogExport(int aKind, const wxString &aFileA, const wxString &aFileB, const wxString &aLabelA, const wxString &aLabelB, wxWindow *aParent, REPORTER *aReporter)
Definition pcbnew.cpp:730
static int pcbnewMergeExport(int aKind, const wxString &aAncestor, const wxString &aOurs, const wxString &aTheirs, const wxString &aOutput, bool aInteractive, bool aSingleFile, REPORTER *aReporter)
Definition pcbnew.cpp:720
void InvokePcbLibTableEditor(KIWAY *aKiway, wxWindow *aCaller)
Show the modal DIALOG_EDIT_LIBRARY_TABLES for purposes of editing the global and project library tabl...
PANEL_SNAPPING * CreateSnappingPanel(wxWindow *aParent, SETTINGS_T *aCfg, FRAME_T aFrameType, SNAP_INFERENCE_SETTINGS SETTINGS_T::*aInference=nullptr, MAGNETIC_SETTINGS SETTINGS_T::*aMagnetics=nullptr)
Build the snapping page for an editor.
static bool startFootprintLibraryLoad()
Start loading the footprint libraries in the background.
Definition pcbnew.cpp:269
static PROJECT * footprintLibraryProject()
Return the project whose footprint libraries the kiface-level footprint services work with.
Definition pcbnew.cpp:107
static wxString filterFootprints(const wxString &aFilterJson)
Filter footprints based on criteria passed as JSON.
Definition pcbnew.cpp:144
static float footprintLibraryLoadProgress()
Return how far a background load has got.
Definition pcbnew.cpp:289
KIFACE_BASE & Kiface()
Global KIFACE_BASE "get" accessor.
Definition pcbnew.cpp:744
PGM_BASE & Pgm()
The global program "get" accessor.
see class PGM_BASE
T * GetToolbarSettings(const wxString &aFilename)
T * GetAppSettings(const char *aFilename)
The outcome of a footprint match query, and the JSON encoding used to move it across the Pcbnew kifac...
bool OnKifaceStart(PGM_BASE *aProgram, int aCtlBits, KIWAY *aKiway) override
Typically start_common() is called from here.
wxString path
File path (KIND::FILE_KIND) or project path (KIND::LIBID_KIND).
Definition kiway.h:164
LIB_ID libId
Library identifier; valid when kind == KIND::LIBID_KIND.
Definition kiway.h:165
Implement a participant in the KIWAY alchemy.
Definition kiway.h:153
std::atomic_bool m_libraryPreloadInProgress
Definition pcbnew.cpp:704
bool OnKifaceStart(PGM_BASE *aProgram, int aCtlBits, KIWAY *aKiway) override
Typically start_common() is called from here.
Definition pcbnew.cpp:755
void PreloadLibraries(KIWAY *aKiway) override
Definition pcbnew.cpp:1331
KIWAY * m_kiway
Definition pcbnew.cpp:710
void ProjectChanged() override
Definition pcbnew.cpp:1438
void SaveFileAs(const wxString &aProjectBasePath, const wxString &aSrcProjectName, 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 pcbnew.cpp:836
void CancelPreload(bool aBlock=true) override
Definition pcbnew.cpp:1445
std::shared_ptr< BACKGROUND_JOB > m_libraryPreloadBackgroundJob
Definition pcbnew.cpp:702
void * IfaceOrAddress(int aDataId) override
Return a pointer to the requested object.
Definition pcbnew.cpp:602
void RegisterLibraryHandlers(KICAD_API_SERVER *aServer) override
Register this face's library API handlers on the given server.
Definition pcbnew.cpp:1457
void Reset() override
Reloads global state.
Definition pcbnew.cpp:806
std::unique_ptr< API_HANDLER_FOOTPRINT > m_openFpHandler
Definition pcbnew.cpp:714
wxWindow * CreateKiWindow(wxWindow *aParent, int aClassId, KIWAY *aKiway, int aCtlBits=0) override
Create a wxWindow for the current project.
Definition pcbnew.cpp:331
bool handleOpenFootprint(const wxString &aProjectPath, const wxString &aLibIdStr, KICAD_API_SERVER *aServer, wxString *aError)
Definition pcbnew.cpp:1018
IFACE(const char *aName, KIWAY::FACE_T aType)
Definition pcbnew.cpp:320
void closeFootprint(KICAD_API_SERVER *aServer)
Definition pcbnew.cpp:982
std::unique_ptr< API_HANDLER_PCB > m_openHandler
Definition pcbnew.cpp:712
std::atomic_bool m_libraryPreloadAbort
Definition pcbnew.cpp:705
int HandleJob(JOB *aJob, REPORTER *aReporter, PROGRESS_REPORTER *aProgressReporter) override
Definition pcbnew.cpp:951
void closeBoard(KICAD_API_SERVER *aServer)
Definition pcbnew.cpp:963
bool LoadAllLibraries() override
Starts a background load of all libraries of the type owned by this face.
Definition pcbnew.cpp:1469
std::shared_ptr< HEADLESS_FOOTPRINT_CONTEXT > m_openFpContext
Definition pcbnew.cpp:713
bool HandleApiCloseDocument(const DOCUMENT_SPEC &aSpec, KICAD_API_SERVER *aServer, wxString *aError) override
Close the document identified by aSpec.
Definition pcbnew.cpp:1279
std::shared_ptr< HEADLESS_PCB_CONTEXT > m_openContext
Definition pcbnew.cpp:711
bool HandleApiOpenDocument(const DOCUMENT_SPEC &aSpec, KICAD_API_SERVER *aServer, wxString *aError) override
Definition pcbnew.cpp:996
PCBNEW_JOBS_HANDLER * JobHandler() const
Accessor for the non-job diff/merge exports (pcbnewMergeExport etc.).
Definition pcbnew.cpp:663
std::unique_ptr< API_HANDLER_FP_LIBRARIES > m_apiHandlerFpLibs
Definition pcbnew.cpp:715
std::unique_ptr< PCBNEW_JOBS_HANDLER > m_jobHandler
Definition pcbnew.cpp:701
bool HandleJobConfig(JOB *aJob, wxWindow *aParent) override
Definition pcbnew.cpp:957
bool handleCreatePcb(const wxString &aPath, KICAD_API_SERVER *aServer, wxString *aError)
Definition pcbnew.cpp:1201
bool handleOpenPcb(const wxString &aPath, KICAD_API_SERVER *aServer, wxString *aError)
Definition pcbnew.cpp:1096
void OnKifaceEnd() override
Called just once just before the DSO is to be unloaded.
Definition pcbnew.cpp:813
std::future< void > m_libraryPreloadReturn
Definition pcbnew.cpp:703
IbisParser parser & reporter
KIBIS top(path, &reporter)
VECTOR3I expected(15, 30, 45)
static const long long MM
wxString result
Test unit parsing edge cases and error handling.
#define ENVVARS_CHANGED
wxLogTrace helper definitions.
Definition of file extensions used in Kicad.