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_services.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 );
314static KICAD_DIFF::DIFF_CANVAS_CONTEXT*
315pcbnewAttachDiffCanvasesExport( int aKind, const wxString& aFileA, const wxString& aFileB, WIDGET_DIFF_CANVAS* aBefore,
316 WIDGET_DIFF_CANVAS* aAfter, KICAD_DIFF::DOCUMENT_DIFF* aDiff, REPORTER* aReporter );
317
318
319static struct IFACE : public KIFACE_BASE, public UNITS_PROVIDER
320{
321 // Of course all are virtual overloads, implementations of the KIFACE.
322
323 IFACE( const char* aName, KIWAY::FACE_T aType ) :
324 KIFACE_BASE( aName, aType ),
326 {}
327
328 bool OnKifaceStart( PGM_BASE* aProgram, int aCtlBits, KIWAY* aKiway ) override;
329
330 void Reset() override;
331
332 void OnKifaceEnd() override;
333
334 wxWindow* CreateKiWindow( wxWindow* aParent, int aClassId, KIWAY* aKiway, int aCtlBits = 0 ) override
335 {
336 switch( aClassId )
337 {
339 {
340 auto frame = new PCB_EDIT_FRAME( aKiway, aParent );
341
342 if( Kiface().IsSingle() )
343 {
344 // only run this under single_top, not under a project manager.
347 }
348
349 return frame;
350 }
353 return new FOOTPRINT_EDIT_FRAME( aKiway, aParent );
354
356 return new FOOTPRINT_VIEWER_FRAME( aKiway, aParent );
359 return new FOOTPRINT_CHOOSER_FRAME( aKiway, aParent );
360
362 return new FOOTPRINT_WIZARD_FRAME( aKiway, aParent, FRAME_T( aClassId ) );
365 return FOOTPRINT_PREVIEW_PANEL::New( aKiway, aParent, this );
366
371 // The dialog's constructor probably failed to set its Kiway because the
372 // dynamic_cast fails when aParent was allocated by a separate compilation
373 // module. So set it directly.
374 dlg.SetKiway( &dlg, aKiway );
375
376 // Use QuasiModal so that HTML help window will work
377 if( dlg.ShowQuasiModal() == wxID_OK )
380 // Dialog has completed; nothing to return.
381 return nullptr;
385 InvokePcbLibTableEditor( aKiway, aParent );
386 // Dialog has completed; nothing to return.
387 return nullptr;
391
392 case PANEL_FP_GRIDS:
393 {
395 EDA_BASE_FRAME* frame = aKiway->Player( FRAME_FOOTPRINT_EDITOR, false );
396
397 if( !frame )
398 frame = aKiway->Player( FRAME_FOOTPRINT_VIEWER, false );
399
400 if( !frame )
401 frame = aKiway->Player( FRAME_PCB_EDITOR, false );
402
403 if( frame )
404 SetUserUnits( frame->GetUserUnits() );
405
406 return new PANEL_GRID_SETTINGS( aParent, this, frame, cfg, FRAME_FOOTPRINT_EDITOR );
407 }
408
413
415 return new PANEL_PCBNEW_DISPLAY_ORIGIN( aParent, GetAppSettings<FOOTPRINT_EDITOR_SETTINGS>( "fpedit" ),
417
419 {
420 EDA_BASE_FRAME* frame = aKiway->Player( FRAME_FOOTPRINT_EDITOR, false );
421
422 if( !frame )
423 frame = aKiway->Player( FRAME_FOOTPRINT_VIEWER, false );
424
425 if( !frame )
426 frame = aKiway->Player( FRAME_PCB_EDITOR, false );
427
428 if( frame )
429 SetUserUnits( frame->GetUserUnits() );
430
431 return new PANEL_EDIT_OPTIONS( aParent, this, frame, true );
432 }
433
435 {
436 EDA_BASE_FRAME* frame = aKiway->Player( FRAME_FOOTPRINT_EDITOR, false );
437
438 if( !frame )
439 frame = aKiway->Player( FRAME_FOOTPRINT_VIEWER, false );
440
441 if( !frame )
442 frame = aKiway->Player( FRAME_PCB_EDITOR, false );
443
444 if( frame )
445 SetUserUnits( frame->GetUserUnits() );
446
447 return new PANEL_FP_EDITOR_FIELD_DEFAULTS( aParent );
448 }
449
451 {
452 EDA_BASE_FRAME* frame = aKiway->Player( FRAME_FOOTPRINT_EDITOR, false );
453
454 if( !frame )
455 frame = aKiway->Player( FRAME_FOOTPRINT_VIEWER, false );
456
457 if( !frame )
458 frame = aKiway->Player( FRAME_PCB_EDITOR, false );
459
460 if( frame )
461 SetUserUnits( frame->GetUserUnits() );
462
463 return new PANEL_FP_EDITOR_GRAPHICS_DEFAULTS( aParent, this );
464 }
465
467 return new class PANEL_FP_USER_LAYER_NAMES( aParent );
468
470 {
471 APP_SETTINGS_BASE* cfg = GetAppSettings<FOOTPRINT_EDITOR_SETTINGS>( "fpedit" );
472 TOOLBAR_SETTINGS* tb = GetToolbarSettings<FOOTPRINT_EDIT_TOOLBAR_SETTINGS>( "fpedit-toolbars" );
473
474 std::vector<TOOL_ACTION*> actions;
475 std::vector<ACTION_TOOLBAR_CONTROL*> controls;
476
477 for( TOOL_ACTION* action : ACTION_MANAGER::GetActionList() )
478 actions.push_back( action );
479
480 for( ACTION_TOOLBAR_CONTROL* control : ACTION_TOOLBAR::GetCustomControlList( FRAME_FOOTPRINT_EDITOR ) )
481 controls.push_back( control );
482
483 return new PANEL_TOOLBAR_CUSTOMIZATION( aParent, cfg, tb, FRAME_FOOTPRINT_EDITOR, actions, controls );
484 }
485
486 case PANEL_FP_COLORS:
487 return new PANEL_FP_EDITOR_COLOR_SETTINGS( aParent );
488
490 return new PANEL_DISPLAY_OPTIONS( aParent, GetAppSettings<PCBNEW_SETTINGS>( "pcbnew" ) );
491
492 case PANEL_PCB_GRIDS:
493 {
494 PCBNEW_SETTINGS* cfg = GetAppSettings<PCBNEW_SETTINGS>( "pcbnew" );
495 EDA_BASE_FRAME* frame = aKiway->Player( FRAME_PCB_EDITOR, false );
496
497 if( !frame )
498 frame = aKiway->Player( FRAME_FOOTPRINT_EDITOR, false );
499
500 if( !frame )
501 frame = aKiway->Player( FRAME_FOOTPRINT_VIEWER, false );
502
503 if( frame )
504 SetUserUnits( frame->GetUserUnits() );
505
506 return new PANEL_GRID_SETTINGS( aParent, this, frame, cfg, FRAME_PCB_EDITOR );
507 }
508
512
514 return new PANEL_PCBNEW_DISPLAY_ORIGIN( aParent, GetAppSettings<PCBNEW_SETTINGS>( "pcbnew" ),
516
518 {
519 EDA_BASE_FRAME* frame = aKiway->Player( FRAME_PCB_EDITOR, false );
520
521 if( !frame )
522 frame = aKiway->Player( FRAME_FOOTPRINT_EDITOR, false );
523
524 if( !frame )
525 frame = aKiway->Player( FRAME_FOOTPRINT_VIEWER, false );
526
527 if( frame )
528 SetUserUnits( frame->GetUserUnits() );
529
530 return new PANEL_EDIT_OPTIONS( aParent, this, frame, false );
531 }
532
533 case PANEL_PCB_COLORS:
534 {
535 BOARD* board = nullptr;
536 EDA_BASE_FRAME* boardProvider = aKiway->Player( FRAME_PCB_EDITOR, false );
537
538 if( boardProvider )
539 board = static_cast<PCB_EDIT_FRAME*>( boardProvider )->GetBoard();
540
541 return new PANEL_PCBNEW_COLOR_SETTINGS( aParent, board );
542 }
543
545 {
546 APP_SETTINGS_BASE* cfg = GetAppSettings<PCBNEW_SETTINGS>( "pcbnew" );
547 TOOLBAR_SETTINGS* tb = GetToolbarSettings<PCB_EDIT_TOOLBAR_SETTINGS>( "pcbnew-toolbars" );
548
549 std::vector<TOOL_ACTION*> actions;
550 std::vector<ACTION_TOOLBAR_CONTROL*> controls;
551
552 for( TOOL_ACTION* action : ACTION_MANAGER::GetActionList() )
553 actions.push_back( action );
554
555 for( ACTION_TOOLBAR_CONTROL* control : ACTION_TOOLBAR::GetCustomControlList( FRAME_PCB_EDITOR ) )
556 controls.push_back( control );
557
558 return new PANEL_TOOLBAR_CUSTOMIZATION( aParent, cfg, tb, FRAME_PCB_EDITOR, actions, controls );
559 }
560
562 return new PANEL_PCBNEW_ACTION_PLUGINS( aParent );
563
565 return new PANEL_3D_DISPLAY_OPTIONS( aParent );
566
567 case PANEL_3DV_OPENGL:
568 return new PANEL_3D_OPENGL_OPTIONS( aParent );
569
571 return new PANEL_3D_RAYTRACING_OPTIONS( aParent );
572
574 {
575 APP_SETTINGS_BASE* cfg = GetAppSettings<EDA_3D_VIEWER_SETTINGS>( "3d_viewer" );
576 TOOLBAR_SETTINGS* tb = GetToolbarSettings<EDA_3D_VIEWER_TOOLBAR_SETTINGS>( "3d_viewer-toolbars" );
577
578 std::vector<TOOL_ACTION*> actions;
579 std::vector<ACTION_TOOLBAR_CONTROL*> controls;
580
581 for( TOOL_ACTION* action : ACTION_MANAGER::GetActionList() )
582 actions.push_back( action );
583
584 for( ACTION_TOOLBAR_CONTROL* control : ACTION_TOOLBAR::GetCustomControlList( FRAME_PCB_DISPLAY3D ) )
585 controls.push_back( control );
586
587 return new PANEL_TOOLBAR_CUSTOMIZATION( aParent, cfg, tb, FRAME_PCB_DISPLAY3D, actions, controls );
588 }
589
590 default:
591 return nullptr;
592 }
593 }
594
605 void* IfaceOrAddress( int aDataId ) override
606 {
607 switch( aDataId )
608 {
610 {
611 // This is the mechanism by which FOOTPRINT_SELECT_WIDGET can get access to the adapter
612 // without directly linking to pcbnew or pcbcommon, going through PROJECT::FootprintLibAdapter
613 PROJECT* project = nullptr;
614
615 if( wxTheApp )
616 {
617 wxWindow* focus = wxWindow::FindFocus();
618 wxWindow* top = focus ? wxGetTopLevelParent( focus ) : wxTheApp->GetTopWindow();
619
620 if( top )
621 {
622 if( KIWAY_HOLDER* holder = dynamic_cast<KIWAY_HOLDER*>( top ) )
623 project = &holder->Prj();
624 }
625 }
626
627 if( !project )
629
631 }
632
635
637 // Start the background load of the libraries filtered by filterFootprints
639
641 // Report how far the load started by startFootprintMatchLoad() has got
643
646
649
652
653 default:
654 return nullptr;
655 }
656 }
657
659 PCBNEW_JOBS_HANDLER* JobHandler() const { return m_jobHandler.get(); }
660
666 void SaveFileAs( const wxString& aProjectBasePath, const wxString& aSrcProjectName,
667 const wxString& aNewProjectBasePath, const wxString& aNewProjectName,
668 const wxString& aSrcFilePath, wxString& aErrors ) override;
669
670 int HandleJob( JOB* aJob, REPORTER* aReporter, PROGRESS_REPORTER* aProgressReporter ) override;
671
672 bool HandleJobConfig( JOB* aJob, wxWindow* aParent ) override;
673
674 bool HandleApiOpenDocument( const DOCUMENT_SPEC& aSpec,
675 KICAD_API_SERVER* aServer,
676 wxString* aError ) override;
677
678 bool HandleApiCloseDocument( const DOCUMENT_SPEC& aSpec,
679 KICAD_API_SERVER* aServer,
680 wxString* aError ) override;
681
682 bool handleOpenPcb( const wxString& aPath, KICAD_API_SERVER* aServer, wxString* aError );
683
684 bool handleCreatePcb( const wxString& aPath, KICAD_API_SERVER* aServer, wxString* aError );
685
686 bool handleOpenFootprint( const wxString& aProjectPath, const wxString& aLibIdStr, KICAD_API_SERVER* aServer,
687 wxString* aError );
688
689 void PreloadLibraries( KIWAY* aKiway ) override;
690 void ProjectChanged() override;
691 void CancelPreload( bool aBlock = true ) override;
692 void RegisterLibraryHandlers( KICAD_API_SERVER* aServer ) override;
693 bool LoadAllLibraries() override;
694
695
696private:
697 std::unique_ptr<PCBNEW_JOBS_HANDLER> m_jobHandler;
698 std::shared_ptr<BACKGROUND_JOB> m_libraryPreloadBackgroundJob;
699 std::future<void> m_libraryPreloadReturn;
701 std::atomic_bool m_libraryPreloadAbort;
702
703 void closeBoard( KICAD_API_SERVER* aServer );
704 void closeFootprint( KICAD_API_SERVER* aServer );
705
706 KIWAY* m_kiway = nullptr;
707 std::shared_ptr<HEADLESS_PCB_CONTEXT> m_openContext;
708 std::unique_ptr<API_HANDLER_PCB> m_openHandler;
709 std::shared_ptr<HEADLESS_FOOTPRINT_CONTEXT> m_openFpContext;
710 std::unique_ptr<API_HANDLER_FOOTPRINT> m_openFpHandler;
711 std::unique_ptr<API_HANDLER_FP_LIBRARIES> m_apiHandlerFpLibs;
712
713} kiface( "pcbnew", KIWAY::FACE_PCB );
714
715
716int pcbnewMergeExport( int aKind, const wxString& aAncestor, const wxString& aOurs,
717 const wxString& aTheirs, const wxString& aOutput, bool aInteractive,
718 bool aSingleFile, REPORTER* aReporter )
719{
720 return kiface.JobHandler()->RunMerge( static_cast<KICAD_DIFF::DOC_KIND>( aKind ), aAncestor,
721 aOurs, aTheirs, aOutput, aInteractive, aSingleFile,
722 aReporter );
723}
724
725
726int pcbnewOpenDiffDialogExport( int aKind, const wxString& aFileA, const wxString& aFileB,
727 const wxString& aLabelA, const wxString& aLabelB,
728 wxWindow* aParent, REPORTER* aReporter )
729{
730 return kiface.JobHandler()->OpenDiffDialog( static_cast<KICAD_DIFF::DOC_KIND>( aKind ), aFileA,
731 aFileB, aLabelA, aLabelB, aParent, aReporter );
732}
733
734
736 const wxString& aFileB, WIDGET_DIFF_CANVAS* aBefore,
737 WIDGET_DIFF_CANVAS* aAfter,
738 KICAD_DIFF::DOCUMENT_DIFF* aDiff, REPORTER* aReporter )
739{
740 return kiface.JobHandler()->AttachDiffCanvases( static_cast<KICAD_DIFF::DOC_KIND>( aKind ), aFileA, aFileB, aBefore,
741 aAfter, *aDiff, aReporter );
742}
743
744} // namespace
745
746
747using namespace PCB;
748
749
751
752
753// KIFACE_GETTER's actual spelling is a substitution macro found in kiway.h.
754// KIFACE_GETTER will not have name mangling due to declaration in kiway.h.
755KIFACE_API KIFACE* KIFACE_GETTER( int* aKIFACEversion, int aKiwayVersion, PGM_BASE* aProgram )
756{
757 return &kiface;
758}
759
760
761bool IFACE::OnKifaceStart( PGM_BASE* aProgram, int aCtlBits, KIWAY* aKiway )
762{
763 // This is process-level-initialization, not project-level-initialization of the DSO.
764 // Do nothing in here pertinent to a project!
766
767 SETTINGS_MANAGER& mgr = aProgram->GetSettingsManager();
768
771
772 // We intentionally register KifaceSettings after FOOTPRINT_EDITOR_SETTINGS and EDA_3D_VIEWER_SETTINGS
773 // In legacy configs, many settings were in a single editor config and the migration routine
774 // for the main editor file will try and call into the now separate settings stores
775 // to move the settings into them
777
778 // Register the footprint editor settings as well because they share a KiFACE and need to be
779 // loaded prior to use to avoid threading deadlocks
781
782 start_common( aCtlBits );
783
784 m_kiway = aKiway;
785
786 m_jobHandler = std::make_unique<PCBNEW_JOBS_HANDLER>( aKiway );
787
789 {
790 m_jobHandler->SetReporter( &CLI_REPORTER::GetInstance() );
791 m_jobHandler->SetProgressReporter( &CLI_PROGRESS_REPORTER::GetInstance() );
792 }
793
794 // Register the PCB and footprint merge drivers with libgit2 so
795 // .gitattributes entries `merge=kicad-pcb` and `merge=kicad-fp` route
796 // through our 3-way merge pipeline. git_merge_driver_register is not
797 // thread-safe; this runs once at kiface init before any background work
798 // spawns. The registry is idempotent so re-loads of the kiface are safe.
801
802 if( Pgm().ApiServerOrNull() )
803 {
804 m_apiHandlerFpLibs = std::make_unique<API_HANDLER_FP_LIBRARIES>();
806 }
807
808 return true;
809}
810
811
813{
814 closeBoard( nullptr );
815 closeFootprint( nullptr );
816}
817
818
820{
821 CancelPreload( true );
822
823#if defined( KICAD_NATIVE_MODEL_PREVIEW ) && defined( __WXGTK3__ )
824 ShutdownNativeModelFilePickerGtk();
825#endif
826
828 {
829 if( Pgm().ApiServerOrNull() )
831
832 m_apiHandlerFpLibs.reset();
833 }
834
835 // Release the CLI-cached board while the static DRC_ITEM tables it serializes against are
836 // still alive; deferring to static teardown crashes reading dangling severity keys
837 if( m_jobHandler )
838 m_jobHandler->ClearCachedBoard();
839
840 end_common();
841}
842
843
844void IFACE::SaveFileAs( const wxString& aProjectBasePath, const wxString& aSrcProjectName,
845 const wxString& aNewProjectBasePath, const wxString& aNewProjectName,
846 const wxString& aSrcFilePath, wxString& aErrors )
847{
848 wxFileName destFile( aSrcFilePath );
849 wxString destPath = destFile.GetPathWithSep();
850 wxUniChar pathSep = wxFileName::GetPathSeparator();
851 wxString ext = destFile.GetExt();
852
853 if( destPath.StartsWith( aProjectBasePath + pathSep ) )
854 destPath.Replace( aProjectBasePath, aNewProjectBasePath, false );
855
856 wxString srcProjectFootprintLib = pathSep + aSrcProjectName + wxT( ".pretty" ) + pathSep;
857 wxString newProjectFootprintLib = pathSep + aNewProjectName + wxT( ".pretty" ) + pathSep;
858
859 destPath.Replace( srcProjectFootprintLib, newProjectFootprintLib, true );
860
861 destFile.SetPath( destPath );
862
865 {
866 if( destFile.GetName() == aSrcProjectName )
867 destFile.SetName( aNewProjectName );
868
869 CopySexprFile( aSrcFilePath, destFile.GetFullPath(),
870 [&]( const std::string& token, wxString& value )
871 {
872 if( token == "sheetfile" )
873 {
874 for( const wxString extension : { wxT( ".sch" ), wxT( ".kicad_sch" ) } )
875 {
876 if( value == aSrcProjectName + extension )
877 {
878 value = aNewProjectName + extension;
879 return true;
880 }
881 else if( value == aProjectBasePath + "/" + aSrcProjectName + extension )
882 {
883 value = aNewProjectBasePath + "/" + aNewProjectName + extension;
884 return true;
885 }
886 else if( value.StartsWith( aProjectBasePath ) )
887 {
888 value.Replace( aProjectBasePath, aNewProjectBasePath, false );
889 return true;
890 }
891 }
892 }
893
894 return false;
895 },
896 aErrors );
897 }
898 else if( ext == FILEEXT::LegacyPcbFileExtension )
899 {
900 if( destFile.GetName() == aSrcProjectName )
901 destFile.SetName( aNewProjectName );
902
903 KiCopyFile( aSrcFilePath, destFile.GetFullPath(), aErrors );
904 }
907 {
908 // Footprints are not project-specific. Keep their source names.
909 KiCopyFile( aSrcFilePath, destFile.GetFullPath(), aErrors );
910 }
912 {
913 // TODO
914 }
915 else if( ext == wxT( "rpt" ) )
916 {
917 // DRC must be the "gold standard". Since we can't guarantee that there aren't
918 // any non-deterministic cases in the save-as algorithm, we don't want to certify
919 // the result with the source's DRC report. Therefore copy it under the old
920 // name.
921 KiCopyFile( aSrcFilePath, destFile.GetFullPath(), aErrors );
922 }
923 else if( destFile.GetName() == FILEEXT::FootprintLibraryTableFileName )
924 {
925 wxFileName libTableFn( aSrcFilePath );
926 LIBRARY_TABLE libTable( libTableFn, LIBRARY_TABLE_SCOPE::PROJECT );
927 libTable.SetPath( destFile.GetFullPath() );
928 libTable.SetType( LIBRARY_TABLE_TYPE::FOOTPRINT );
929
930 for( LIBRARY_TABLE_ROW& row : libTable.Rows() )
931 {
932 wxString uri = row.URI();
933
934 uri.Replace( wxT( "/" ) + aSrcProjectName + wxT( ".pretty" ),
935 wxT( "/" ) + aNewProjectName + wxT( ".pretty" ) );
936
937 row.SetURI( uri );
938 }
939
940 libTable.Save().map_error(
941 [&]( const LIBRARY_ERROR& aError )
942 {
943 wxString msg;
944
945 if( !aErrors.empty() )
946 aErrors += wxT( "\n" );
947
948 msg.Printf( _( "Cannot copy file '%s'." ), destFile.GetFullPath() );
949 aErrors += msg;
950 } );
951 }
952 else
953 {
954 wxFAIL_MSG( wxT( "Unexpected filetype for Pcbnew::SaveFileAs()" ) );
955 }
956}
957
958
959int IFACE::HandleJob( JOB* aJob, REPORTER* aReporter, PROGRESS_REPORTER* aProgressReporter )
960{
961 return m_jobHandler->RunJob( aJob, aReporter, aProgressReporter );
962}
963
964
965bool IFACE::HandleJobConfig( JOB* aJob, wxWindow* aParent )
966{
967 return m_jobHandler->HandleJobConfig( aJob, aParent );
968}
969
970
972{
973 if( m_openHandler )
974 {
975 if( aServer )
976 aServer->DeregisterHandler( m_openHandler.get() );
977
978 m_openHandler.reset();
979 }
980
981 m_openContext.reset();
982
983 // The jobs handler caches the last-loaded board. Clear it so the next job
984 // uses the board from the newly opened document rather than a stale copy.
985 if( m_jobHandler )
986 m_jobHandler->ClearCachedBoard();
987}
988
989
991{
992 if( m_openFpHandler )
993 {
994 if( aServer )
995 aServer->DeregisterHandler( m_openFpHandler.get() );
996
997 m_openFpHandler.reset();
998 }
999
1000 m_openFpContext.reset();
1001}
1002
1003
1004bool IFACE::HandleApiOpenDocument( const DOCUMENT_SPEC& aSpec, KICAD_API_SERVER* aServer, wxString* aError )
1005{
1006 wxCHECK( aServer, false );
1007
1008 if( aSpec.kind == DOCUMENT_SPEC::KIND::LIBID_KIND )
1009 return handleOpenFootprint( aSpec.path, aSpec.libId.GetUniStringLibId(), aServer, aError );
1010
1011 if( aSpec.kind == DOCUMENT_SPEC::KIND::CREATE_KIND )
1012 return handleCreatePcb( aSpec.path, aServer, aError );
1013
1014 if( aSpec.path.IsEmpty() )
1015 {
1016 if( aError )
1017 *aError = wxS( "No path specified to open" );
1018
1019 return false;
1020 }
1021
1022 return handleOpenPcb( aSpec.path, aServer, aError );
1023}
1024
1025
1026bool IFACE::handleOpenFootprint( const wxString& aProjectPath, const wxString& aLibIdStr, KICAD_API_SERVER* aServer,
1027 wxString* aError )
1028{
1029 LIB_ID fpid;
1030
1031 if( fpid.Parse( aLibIdStr ) >= 0 )
1032 {
1033 if( aError )
1034 *aError = wxString::Format( wxS( "Invalid footprint LIB_ID: %s" ), aLibIdStr );
1035
1036 return false;
1037 }
1038
1039 wxFileName projectPath( aProjectPath );
1040 projectPath.MakeAbsolute();
1041
1042 SETTINGS_MANAGER& settingsManager = Pgm().GetSettingsManager();
1043
1044 if( !settingsManager.LoadProject( projectPath.GetFullPath(), true ) )
1045 {
1046 wxLogTrace( traceApi, "Warning: no project file found for %s", aProjectPath );
1047 }
1048
1049 PROJECT* project = settingsManager.GetProject( projectPath.GetFullPath() );
1050
1051 if( !project )
1052 {
1053 if( aError )
1054 *aError = wxString::Format( wxS( "Error loading project for %s" ), aProjectPath );
1055
1056 return false;
1057 }
1058
1059 std::shared_ptr<HEADLESS_FOOTPRINT_CONTEXT> newContext;
1060
1061 try
1062 {
1064 adapter->AsyncLoad();
1065 adapter->BlockUntilLoaded();
1066 std::unique_ptr<FOOTPRINT> footprint( adapter->LoadFootprintWithOptionalNickname( fpid, true ) );
1067
1068 if( !footprint )
1069 {
1070 if( aError )
1071 *aError = wxString::Format( wxS( "Footprint not found: %s" ), aLibIdStr );
1072
1073 return false;
1074 }
1075
1076 newContext = std::make_shared<HEADLESS_FOOTPRINT_CONTEXT>(
1077 std::move( footprint ), fpid, project, GetAppSettings<FOOTPRINT_EDITOR_SETTINGS>( "fpedit" ), m_kiway );
1078 }
1079 catch( ... )
1080 {
1081 if( aError )
1082 *aError = wxString::Format( wxS( "Failed to load footprint: %s" ), aLibIdStr );
1083
1084 return false;
1085 }
1086
1087 closeFootprint( aServer );
1088
1089 if( !m_apiHandlerFpLibs )
1090 {
1091 m_apiHandlerFpLibs = std::make_unique<API_HANDLER_FP_LIBRARIES>();
1092 aServer->RegisterHandler( m_apiHandlerFpLibs.get() );
1093 }
1094
1095 m_openFpContext = std::move( newContext );
1096
1097 m_openFpHandler = std::make_unique<API_HANDLER_FOOTPRINT>( m_openFpContext, nullptr );
1098 aServer->RegisterHandler( m_openFpHandler.get() );
1099
1100 return true;
1101}
1102
1103
1104bool IFACE::handleOpenPcb( const wxString& aPath, KICAD_API_SERVER* aServer, wxString* aError )
1105{
1106 wxFileName projectPath( aPath );
1107
1108 if( projectPath.GetExt() == FILEEXT::KiCadPcbFileExtension )
1109 projectPath.SetExt( FILEEXT::ProjectFileExtension );
1110 else if( projectPath.GetExt() != FILEEXT::ProjectFileExtension )
1111 projectPath.SetExt( FILEEXT::ProjectFileExtension );
1112
1113 projectPath.MakeAbsolute();
1114
1115 // We currently only support one document per type (and each needs to come from
1116 // the same project). This will need evolution once we support MDI and multi-project.
1117 closeBoard( aServer );
1118
1119 SETTINGS_MANAGER& settingsManager = Pgm().GetSettingsManager();
1120
1121 // Reuse an already-loaded project if one exists for this path.
1122 PROJECT* project = settingsManager.GetProject( projectPath.GetFullPath() );
1123
1124 if( !project )
1125 {
1126 if( !settingsManager.LoadProject( projectPath.GetFullPath(), true ) )
1127 wxLogTrace( traceApi, "Warning: no project file found for %s", aPath );
1128
1129 project = settingsManager.GetProject( projectPath.GetFullPath() );
1130 }
1131
1132 if( !project )
1133 {
1134 if( aError )
1135 *aError = wxString::Format( wxS( "Error loading project for %s" ), aPath );
1136
1137 return false;
1138 }
1139
1140 wxFileName boardPath( projectPath );
1141 boardPath.SetExt( FILEEXT::KiCadPcbFileExtension );
1142
1143 if( !boardPath.FileExists() )
1144 {
1145 if( aError )
1146 *aError = wxString::Format( wxS( "File not found: %s" ), aPath );
1147
1148 return false;
1149 }
1150
1151 PCB_IO_MGR::PCB_FILE_T pluginType =
1153
1154 if( pluginType == PCB_IO_MGR::FILE_TYPE_NONE )
1155 {
1156 if( aError )
1157 *aError = wxString::Format( wxS( "%s is not a recognized file type" ), aPath );
1158
1159 return false;
1160 }
1161
1162 std::shared_ptr<HEADLESS_PCB_CONTEXT> newContext;
1163
1164 try
1165 {
1166 std::unique_ptr<BOARD> loadedBoard = BOARD_LOADER::Load( boardPath.GetFullPath(), pluginType, project );
1167
1168 if( !loadedBoard )
1169 {
1170 if( aError )
1171 *aError = wxS( "Failed to load board" );
1172
1173 return false;
1174 }
1175
1176 newContext = std::make_shared<HEADLESS_PCB_CONTEXT>( std::move( loadedBoard ), project,
1178 }
1179 catch( const IO_ERROR& ioe )
1180 {
1181 if( aError )
1182 *aError = wxString::Format( wxS( "Failed to load board: %s" ), ioe.What() );
1183
1184 return false;
1185 }
1186 catch( ... )
1187 {
1188 if( aError )
1189 *aError = wxS( "Failed to load board" );
1190
1191 return false;
1192 }
1193
1194 if( !m_apiHandlerFpLibs )
1195 {
1196 m_apiHandlerFpLibs = std::make_unique<API_HANDLER_FP_LIBRARIES>();
1197 aServer->RegisterHandler( m_apiHandlerFpLibs.get() );
1198 }
1199
1200 m_openContext = std::move( newContext );
1201
1202 m_openHandler = std::make_unique<API_HANDLER_PCB>( m_openContext, nullptr );
1203 aServer->RegisterHandler( m_openHandler.get() );
1204
1205 return true;
1206}
1207
1208
1209bool IFACE::handleCreatePcb( const wxString& aPath, KICAD_API_SERVER* aServer, wxString* aError )
1210{
1211 wxFileName boardPath( aPath );
1212 boardPath.MakeAbsolute();
1213
1214 wxFileName projectPath( boardPath );
1215 projectPath.SetExt( FILEEXT::ProjectFileExtension );
1216
1217 if( m_openContext && m_openContext->IsContentModified() )
1218 {
1219 if( aError )
1220 *aError = wxS( "The current board has unsaved changes; save or revert it first" );
1221
1222 return false;
1223 }
1224
1225 closeBoard( aServer );
1226
1227 SETTINGS_MANAGER& settingsManager = Pgm().GetSettingsManager();
1228
1229 PROJECT* project = settingsManager.GetProject( projectPath.GetFullPath() );
1230
1231 if( !project )
1232 {
1233 settingsManager.LoadProject( projectPath.GetFullPath(), true );;
1234 project = settingsManager.GetProject( projectPath.GetFullPath() );
1235 }
1236
1237 if( !project )
1238 {
1239 if( aError )
1240 *aError = wxString::Format( wxS( "Error creating project for %s" ), aPath );
1241
1242 return false;
1243 }
1244
1245 std::shared_ptr<HEADLESS_PCB_CONTEXT> newContext;
1246
1247 try
1248 {
1249 std::unique_ptr<BOARD> newBoard = BOARD_LOADER::CreateEmptyBoard( project );
1250
1251 if( !newBoard )
1252 {
1253 if( aError )
1254 *aError = wxS( "Failed to create board" );
1255
1256 return false;
1257 }
1258
1259 newBoard->SetFileName( boardPath.GetFullPath() );
1260
1261 newContext = std::make_shared<HEADLESS_PCB_CONTEXT>( std::move( newBoard ), project,
1263 }
1264 catch( ... )
1265 {
1266 if( aError )
1267 *aError = wxS( "Failed to create board" );
1268
1269 return false;
1270 }
1271
1272 m_openContext = std::move( newContext );
1273
1274 if( !m_apiHandlerFpLibs )
1275 {
1276 m_apiHandlerFpLibs = std::make_unique<API_HANDLER_FP_LIBRARIES>();
1277 aServer->RegisterHandler( m_apiHandlerFpLibs.get() );
1278 }
1279
1280 m_openHandler = std::make_unique<API_HANDLER_PCB>( m_openContext, nullptr );
1281 aServer->RegisterHandler( m_openHandler.get() );
1282
1283 return true;
1284}
1285
1286
1287bool IFACE::HandleApiCloseDocument( const DOCUMENT_SPEC& aSpec, KICAD_API_SERVER* aServer, wxString* aError )
1288{
1289 wxCHECK( aServer, false );
1290
1291 if( aSpec.kind == DOCUMENT_SPEC::KIND::LIBID_KIND )
1292 {
1293 if( !m_openFpContext )
1294 {
1295 if( aError )
1296 *aError = wxS( "No footprint is currently open" );
1297
1298 return false;
1299 }
1300
1301 if( aSpec.libId.IsValid() && m_openFpContext->GetLoadedFPID() != aSpec.libId )
1302 {
1303 if( aError )
1304 *aError = wxS( "Requested document does not match the open document" );
1305
1306 return false;
1307 }
1308
1309 closeFootprint( aServer );
1310 return true;
1311 }
1312
1313 if( !m_openContext )
1314 {
1315 if( aError )
1316 *aError = wxS( "No board is currently open" );
1317
1318 return false;
1319 }
1320
1321 if( !aSpec.path.IsEmpty() )
1322 {
1323 wxFileName currentBoard( m_openContext->GetCurrentFileName() );
1324
1325 if( currentBoard.GetFullName() != wxFileName( aSpec.path ).GetFullName() )
1326 {
1327 if( aError )
1328 *aError = wxS( "Requested document does not match the open document" );
1329
1330 return false;
1331 }
1332 }
1333
1334 closeBoard( aServer );
1335 return true;
1336}
1337
1338
1340{
1341 wxCHECK( aKiway, /* void */ );
1342
1343 // Use compare_exchange to atomically check and set the flag to prevent race conditions
1344 // when PreloadLibraries is called multiple times concurrently (e.g., from project manager
1345 // and pcb editor both scheduling via CallAfter)
1346 bool expected = false;
1347
1348 if( !m_libraryPreloadInProgress.compare_exchange_strong( expected, true ) )
1349 return;
1350
1352
1354 Pgm().GetBackgroundJobMonitor().Create( _( "Loading Footprint Libraries" ) );
1355
1356 auto preload =
1357 [this, aKiway]() -> void
1358 {
1359 std::shared_ptr<BACKGROUND_JOB_REPORTER> reporter =
1361
1363
1364 reporter->Report( _( "Loading Footprint Libraries" ) );
1365 bool aborted = !adapter->RunPreload( *reporter, m_libraryPreloadAbort );
1366
1367 // If aborted, skip operations that use the adapter since the project may have changed
1368 // and the adapter's project reference could be stale. This prevents use-after-free
1369 // crashes when switching projects during library preload.
1370 if( !aborted )
1371 {
1372 // Report library load errors from adapter
1374 }
1375 else
1376 {
1377 wxLogTrace( traceLibraries, "pcbnew PreloadLibraries: aborted, skipping footprint processing" );
1378 }
1379
1382
1383 // Only send reload notifications if we weren't aborted
1384 if( !aborted )
1385 {
1386 std::string payload = "";
1387 aKiway->ExpressMail( FRAME_PCB_EDITOR, MAIL_RELOAD_LIB, payload, nullptr, true );
1388 aKiway->ExpressMail( FRAME_FOOTPRINT_EDITOR, MAIL_RELOAD_LIB, payload, nullptr, true );
1389 aKiway->ExpressMail( FRAME_CVPCB, MAIL_RELOAD_LIB, payload, nullptr, true );
1390 }
1391
1392 // A cancel that came after the loop left the flag set, and it would end the next preload at once
1393 m_libraryPreloadAbort.store( false );
1394 m_libraryPreloadInProgress.store( false );
1395 };
1396
1397 std::future<void> preloadFuture = std::async( std::launch::async, preload );
1398 m_libraryPreloadReturn = std::move( preloadFuture );
1399}
1400
1401
1403{
1404 if( m_libraryPreloadInProgress.load() )
1405 m_libraryPreloadAbort.store( true );
1406}
1407
1408
1409void IFACE::CancelPreload( bool aBlock )
1410{
1411 if( m_libraryPreloadInProgress.load() )
1412 {
1413 m_libraryPreloadAbort.store( true );
1414
1415 if( aBlock )
1416 {
1418
1419 // The preload has ended, so a flag it never read can only stop the next one
1420 m_libraryPreloadAbort.store( false );
1421 }
1422 }
1423}
1424
1425
1427{
1428 wxCHECK_RET( aServer, "no API server provided" );
1429
1430 if( !m_apiHandlerFpLibs )
1431 {
1432 m_apiHandlerFpLibs = std::make_unique<API_HANDLER_FP_LIBRARIES>();
1433 aServer->RegisterHandler( m_apiHandlerFpLibs.get() );
1434 }
1435}
1436
1437
1439{
1441
1442 if( !adapter )
1443 return false;
1444
1445 adapter->AsyncLoad();
1446 return true;
1447}
const char * name
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:128
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)
Keeps the documents drawn on a pair of diff canvases alive.
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.
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:401
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:499
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:593
virtual PROJECT & Prj() const
Return the PROJECT associated with this KIWAY.
Definition kiway.cpp:208
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.
bool RunPreload(PROGRESS_REPORTER &aReporter, std::atomic_bool &aAbort)
Starts AsyncLoad() and waits for it on the calling thread, reporting progress to aReporter.
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:102
virtual BACKGROUND_JOBS_MONITOR & GetBackgroundJobMonitor() const
Definition pgm_base.h:130
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:142
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition pgm_base.h:124
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.
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.
bool LoadProject(const wxString &aFullPath, bool aSetActive=true, bool aOverrideLock=false)
Load a project or sets up a new project with a specified path.
UNITS_PROVIDER(const EDA_IU_SCALE &aIuScale, EDA_UNITS aUnits)
EDA_UNITS GetUserUnits() const
void SetUserUnits(EDA_UNITS aUnits)
GAL-backed canvas for visualizing a KICAD_DIFF::DIFF_SCENE.
This file is part of the common library.
KIFACE_BASE & Kiface()
Global KIFACE_BASE "get" accessor.
Definition cvpcb.cpp:178
#define _(s)
Abstract pattern-matching tool and implementations.
EDA_UNITS
Definition eda_units.h:45
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:100
@ FRAME_PCB_EDITOR
Definition frame_type.h:38
@ PANEL_3DV_TOOLBARS
Definition frame_type.h:111
@ FRAME_FOOTPRINT_VIEWER
Definition frame_type.h:41
@ PANEL_3DV_OPENGL
Definition frame_type.h:109
@ PANEL_FP_DEFAULT_GRAPHICS_VALUES
Definition frame_type.h:95
@ PANEL_PCB_TOOLBARS
Definition frame_type.h:104
@ PANEL_PCB_ORIGINS_AXES
Definition frame_type.h:106
@ FRAME_FOOTPRINT_WIZARD
Definition frame_type.h:42
@ PANEL_PCB_EDIT_OPTIONS
Definition frame_type.h:102
@ PANEL_FP_DISPLAY_OPTIONS
Definition frame_type.h:88
@ PANEL_PCB_COLORS
Definition frame_type.h:103
@ PANEL_FP_SNAPPING
Definition frame_type.h:90
@ PANEL_FP_USER_LAYER_NAMES
Definition frame_type.h:96
@ PANEL_3DV_RAYTRACING
Definition frame_type.h:110
@ DIALOG_PCB_LIBRARY_TABLE
Definition frame_type.h:131
@ FRAME_FOOTPRINT_PREVIEW
Definition frame_type.h:44
@ FRAME_FOOTPRINT_CHOOSER
Definition frame_type.h:40
@ PANEL_FP_GRIDS
Definition frame_type.h:89
@ PANEL_FP_ORIGINS_AXES
Definition frame_type.h:97
@ PANEL_PCB_DISPLAY_OPTS
Definition frame_type.h:99
@ DIALOG_CONFIGUREPATHS
Definition frame_type.h:128
@ PANEL_FP_COLORS
Definition frame_type.h:92
@ PANEL_FP_DEFAULT_FIELDS
Definition frame_type.h:94
@ FRAME_FOOTPRINT_EDITOR
Definition frame_type.h:39
@ FRAME_PCB_DISPLAY3D
Definition frame_type.h:43
@ PANEL_FP_EDIT_OPTIONS
Definition frame_type.h:91
@ PANEL_FP_TOOLBARS
Definition frame_type.h:93
@ PANEL_PCB_ACTION_PLUGINS
Definition frame_type.h:105
@ PANEL_3DV_DISPLAY_OPTIONS
Definition frame_type.h:108
@ FRAME_CVPCB
Definition frame_type.h:48
@ PANEL_PCB_SNAPPING
Definition frame_type.h:101
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:443
void KiCopyFile(const wxString &aSrcPath, const wxString &aDestPath, wxString &aErrors)
Definition gestfich.cpp:416
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:70
@ 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_ATTACH_DIFF_CANVASES
KICAD_DIFF::DIFF_CANVAS_CONTEXT* (*)( int aKind, const wxString& aFileA, const wxString& aFileB,...
Definition kiface_ids.h:85
@ KIFACE_OPEN_DIFF_DIALOG
int (*)( int aKind, const wxString& aFileA, const wxString& aFileB, const wxString& aLabelA,...
Definition kiface_ids.h:77
void * KifaceServiceAddress(T aAddress)
Return a service address for IfaceOrAddress().
#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:55
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:726
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:716
static KICAD_DIFF::DIFF_CANVAS_CONTEXT * pcbnewAttachDiffCanvasesExport(int aKind, const wxString &aFileA, const wxString &aFileB, WIDGET_DIFF_CANVAS *aBefore, WIDGET_DIFF_CANVAS *aAfter, KICAD_DIFF::DOCUMENT_DIFF *aDiff, REPORTER *aReporter)
Definition pcbnew.cpp:735
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:750
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.
The full set of changes between two parsed documents of one type.
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:700
bool OnKifaceStart(PGM_BASE *aProgram, int aCtlBits, KIWAY *aKiway) override
Typically start_common() is called from here.
Definition pcbnew.cpp:761
void PreloadLibraries(KIWAY *aKiway) override
Definition pcbnew.cpp:1339
KIWAY * m_kiway
Definition pcbnew.cpp:706
void ProjectChanged() override
Definition pcbnew.cpp:1402
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:844
void CancelPreload(bool aBlock=true) override
Definition pcbnew.cpp:1409
std::shared_ptr< BACKGROUND_JOB > m_libraryPreloadBackgroundJob
Definition pcbnew.cpp:698
void * IfaceOrAddress(int aDataId) override
Return a pointer to the requested object.
Definition pcbnew.cpp:605
void RegisterLibraryHandlers(KICAD_API_SERVER *aServer) override
Register this face's library API handlers on the given server.
Definition pcbnew.cpp:1426
void Reset() override
Reloads global state.
Definition pcbnew.cpp:812
std::unique_ptr< API_HANDLER_FOOTPRINT > m_openFpHandler
Definition pcbnew.cpp:710
wxWindow * CreateKiWindow(wxWindow *aParent, int aClassId, KIWAY *aKiway, int aCtlBits=0) override
Create a wxWindow for the current project.
Definition pcbnew.cpp:334
bool handleOpenFootprint(const wxString &aProjectPath, const wxString &aLibIdStr, KICAD_API_SERVER *aServer, wxString *aError)
Definition pcbnew.cpp:1026
IFACE(const char *aName, KIWAY::FACE_T aType)
Definition pcbnew.cpp:323
void closeFootprint(KICAD_API_SERVER *aServer)
Definition pcbnew.cpp:990
std::unique_ptr< API_HANDLER_PCB > m_openHandler
Definition pcbnew.cpp:708
std::atomic_bool m_libraryPreloadAbort
Definition pcbnew.cpp:701
int HandleJob(JOB *aJob, REPORTER *aReporter, PROGRESS_REPORTER *aProgressReporter) override
Definition pcbnew.cpp:959
void closeBoard(KICAD_API_SERVER *aServer)
Definition pcbnew.cpp:971
bool LoadAllLibraries() override
Starts a background load of all libraries of the type owned by this face.
Definition pcbnew.cpp:1438
std::shared_ptr< HEADLESS_FOOTPRINT_CONTEXT > m_openFpContext
Definition pcbnew.cpp:709
bool HandleApiCloseDocument(const DOCUMENT_SPEC &aSpec, KICAD_API_SERVER *aServer, wxString *aError) override
Close the document identified by aSpec.
Definition pcbnew.cpp:1287
std::shared_ptr< HEADLESS_PCB_CONTEXT > m_openContext
Definition pcbnew.cpp:707
bool HandleApiOpenDocument(const DOCUMENT_SPEC &aSpec, KICAD_API_SERVER *aServer, wxString *aError) override
Definition pcbnew.cpp:1004
PCBNEW_JOBS_HANDLER * JobHandler() const
Accessor for the non-job diff/merge exports (pcbnewMergeExport etc.).
Definition pcbnew.cpp:659
std::unique_ptr< API_HANDLER_FP_LIBRARIES > m_apiHandlerFpLibs
Definition pcbnew.cpp:711
std::unique_ptr< PCBNEW_JOBS_HANDLER > m_jobHandler
Definition pcbnew.cpp:697
bool HandleJobConfig(JOB *aJob, wxWindow *aParent) override
Definition pcbnew.cpp:965
bool handleCreatePcb(const wxString &aPath, KICAD_API_SERVER *aServer, wxString *aError)
Definition pcbnew.cpp:1209
bool handleOpenPcb(const wxString &aPath, KICAD_API_SERVER *aServer, wxString *aError)
Definition pcbnew.cpp:1104
void OnKifaceEnd() override
Called just once just before the DSO is to be unloaded.
Definition pcbnew.cpp:819
std::future< void > m_libraryPreloadReturn
Definition pcbnew.cpp:699
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.