38#include <wx/snglinst.h>
53#include <nlohmann/json.hpp>
69#include <python_scripting.h>
75#include <wx/tokenzr.h>
107 using json = nlohmann::json;
111 json input = json::parse( aFilterJson.ToStdString() );
113 int pinCount = input.value(
"pin_count", 0 );
114 bool zeroFilters = input.value(
"zero_filters",
true );
115 int maxResults = input.value(
"max_results", 400 );
117 std::vector<std::unique_ptr<EDA_PATTERN_MATCH>> filterMatchers;
119 if( input.contains(
"filters" ) && input[
"filters"].is_array() )
121 for(
const auto& f : input[
"filters"] )
125 wxString pattern = wxString::FromUTF8( f.get<std::string>() );
126 auto matcher = std::make_unique<EDA_PATTERN_MATCH_WILDCARD_ANCHORED>();
127 matcher->SetPattern( pattern.Lower() );
128 filterMatchers.push_back( std::move( matcher ) );
133 bool hasFilters = ( pinCount > 0 || !filterMatchers.empty() );
135 if( zeroFilters && !hasFilters )
142 wxWindow* focus = wxWindow::FindFocus();
143 wxWindow*
top = focus ? wxGetTopLevelParent( focus ) : wxTheApp->GetTopWindow();
164 json output = json::array();
169 std::vector<FOOTPRINT*> footprints = adapter->
GetFootprints( nickname,
true );
181 if( fpPadCount != pinCount )
186 if( !filterMatchers.empty() )
188 bool matches =
false;
190 for(
const auto& matcher : filterMatchers )
195 if( matcher->GetPattern().Contains( wxS(
":" ) ) )
196 name = fp->GetFPID().GetLibNickname().wx_str().Lower() + wxS(
":" );
198 name += fp->GetFPID().GetLibItemName().wx_str().Lower();
200 if( matcher->Find(
name ) )
211 wxString libId = fp->GetFPID().Format();
212 output.push_back( libId.ToStdString() );
214 if( ++count >= maxResults )
218 if( count >= maxResults )
222 return wxString::FromUTF8( output.dump() );
224 catch(
const std::exception& e )
244 void Reset()
override;
290 dlg.SetKiway( &dlg, aKiway );
293 if( dlg.ShowQuasiModal() == wxID_OK )
358 return new PANEL_FP_EDITOR_FIELD_DEFAULTS( aParent );
374 return new PANEL_FP_EDITOR_GRAPHICS_DEFAULTS( aParent,
this );
385 std::vector<TOOL_ACTION*> actions;
386 std::vector<ACTION_TOOLBAR_CONTROL*> controls;
389 actions.push_back( action );
392 controls.push_back( control );
394 return new PANEL_TOOLBAR_CUSTOMIZATION( aParent, cfg, tb, actions, controls );
398 return new PANEL_FP_EDITOR_COLOR_SETTINGS( aParent );
417 return new PANEL_GRID_SETTINGS( aParent,
this, frame, cfg,
FRAME_PCB_EDITOR );
437 return new PANEL_EDIT_OPTIONS( aParent,
this, frame,
false );
442 BOARD* board =
nullptr;
446 board =
static_cast<PCB_EDIT_FRAME*
>( boardProvider )->
GetBoard();
448 return new PANEL_PCBNEW_COLOR_SETTINGS( aParent, board );
456 std::vector<TOOL_ACTION*> actions;
457 std::vector<ACTION_TOOLBAR_CONTROL*> controls;
460 actions.push_back( action );
463 controls.push_back( control );
465 return new PANEL_TOOLBAR_CUSTOMIZATION( aParent, cfg, tb, actions, controls );
469 return new PANEL_PCBNEW_ACTION_PLUGINS( aParent );
472 return new PANEL_3D_DISPLAY_OPTIONS( aParent );
475 return new PANEL_3D_OPENGL_OPTIONS( aParent );
478 return new PANEL_3D_RAYTRACING_OPTIONS( aParent );
485 std::vector<TOOL_ACTION*> actions;
486 std::vector<ACTION_TOOLBAR_CONTROL*> controls;
489 actions.push_back( action );
492 controls.push_back( control );
494 return new PANEL_TOOLBAR_CUSTOMIZATION( aParent, cfg, tb, actions, controls );
524 wxWindow* focus = wxWindow::FindFocus();
525 wxWindow*
top = focus ? wxGetTopLevelParent( focus ) : wxTheApp->GetTopWindow();
560 void SaveFileAs(
const wxString& aProjectBasePath,
const wxString& aSrcProjectName,
561 const wxString& aNewProjectBasePath,
const wxString& aNewProjectName,
562 const wxString& aSrcFilePath, wxString& aErrors )
override;
621 m_jobHandler = std::make_unique<PCBNEW_JOBS_HANDLER>( aKiway );
645 const wxString& aNewProjectBasePath,
const wxString& aNewProjectName,
646 const wxString& aSrcFilePath, wxString& aErrors )
648 wxFileName destFile( aSrcFilePath );
649 wxString destPath = destFile.GetPathWithSep();
650 wxUniChar pathSep = wxFileName::GetPathSeparator();
651 wxString ext = destFile.GetExt();
653 if( destPath.StartsWith( aProjectBasePath + pathSep ) )
654 destPath.Replace( aProjectBasePath, aNewProjectBasePath,
false );
656 wxString srcProjectFootprintLib = pathSep + aSrcProjectName + wxT(
".pretty" ) + pathSep;
657 wxString newProjectFootprintLib = pathSep + aNewProjectName + wxT(
".pretty" ) + pathSep;
659 destPath.Replace( srcProjectFootprintLib, newProjectFootprintLib,
true );
661 destFile.SetPath( destPath );
666 if( destFile.GetName() == aSrcProjectName )
667 destFile.SetName( aNewProjectName );
670 [&](
const std::string& token, wxString& value )
672 if( token ==
"sheetfile" )
674 for( const wxString& extension : { wxT(
".sch" ), wxT(
".kicad_sch" ) } )
676 if( value == aSrcProjectName + extension )
678 value = aNewProjectName + extension;
681 else if( value == aProjectBasePath +
"/" + aSrcProjectName + extension )
683 value = aNewProjectBasePath +
"/" + aNewProjectName + extension;
686 else if( value.StartsWith( aProjectBasePath ) )
688 value.Replace( aProjectBasePath, aNewProjectBasePath, false );
700 if( destFile.GetName() == aSrcProjectName )
701 destFile.SetName( aNewProjectName );
703 KiCopyFile( aSrcFilePath, destFile.GetFullPath(), aErrors );
709 KiCopyFile( aSrcFilePath, destFile.GetFullPath(), aErrors );
715 else if( ext == wxT(
"rpt" ) )
721 KiCopyFile( aSrcFilePath, destFile.GetFullPath(), aErrors );
725 wxFileName libTableFn( aSrcFilePath );
727 libTable.SetPath( destFile.GetFullPath() );
730 for( LIBRARY_TABLE_ROW& row : libTable.Rows() )
732 wxString uri = row.URI();
734 uri.Replace( wxT(
"/" ) + aSrcProjectName + wxT(
".pretty" ),
735 wxT(
"/" ) + aNewProjectName + wxT(
".pretty" ) );
740 libTable.Save().map_error(
741 [&](
const LIBRARY_ERROR& aError )
745 if( !aErrors.empty() )
746 aErrors += wxT(
"\n" );
748 msg.Printf(
_(
"Cannot copy file '%s'." ), destFile.GetFullPath() );
754 wxFAIL_MSG( wxT(
"Unexpected filetype for Pcbnew::SaveFileAs()" ) );
761 return m_jobHandler->RunJob( aJob, aReporter, aProgressReporter );
773 constexpr static int interval = 150;
774 constexpr static int timeLimit = 120000;
792 [
this, aKiway]() ->
void
794 std::shared_ptr<BACKGROUND_JOB_REPORTER> reporter =
800 bool aborted =
false;
802 reporter->Report(
_(
"Loading Footprint Libraries" ) );
813 std::this_thread::sleep_for( std::chrono::milliseconds( interval ) );
817 float progress = *loadStatus;
818 reporter->SetCurrentProgress( progress );
825 reporter->SetCurrentProgress( 1 );
831 if( elapsed > timeLimit )
850 "pcbnew PreloadLibraries: adapter errors.IsEmpty()=%d, length=%zu",
851 errors.IsEmpty(), errors.length() );
853 if( !errors.IsEmpty() )
855 std::vector<LOAD_MESSAGE> messages =
858 wxLogTrace(
traceLibraries,
" -> adapter: collected %zu messages",
861 if( !messages.empty() )
866 wxLogTrace(
traceLibraries,
" -> no errors from footprint adapter" );
871 wxLogTrace(
traceLibraries,
"pcbnew PreloadLibraries: aborted, skipping footprint processing" );
882 std::string payload =
"";
constexpr EDA_IU_SCALE pcbIUScale
KIFACE_BASE & Kiface()
Global KIFACE_BASE "get" accessor.
static std::list< TOOL_ACTION * > & GetActionList()
Return list of TOOL_ACTIONs.
std::shared_ptr< BACKGROUND_JOB > Create(const wxString &aName)
Creates a background job with the given name.
void Remove(std::shared_ptr< BACKGROUND_JOB > job)
Removes the given background job from any lists and frees it.
static CLI_PROGRESS_REPORTER & GetInstance()
static CLI_REPORTER & GetInstance()
The base frame for deriving all KiCad main window classes.
An simple container class that lets us dispatch output jobs to kifaces.
KIFACE_BASE(const char *aKifaceName, KIWAY::FACE_T aId)
void InitSettings(APP_SETTINGS_BASE *aSettings)
void end_common()
Common things to do for a top program module, during OnKifaceEnd();.
APP_SETTINGS_BASE * KifaceSettings() const
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?
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...
virtual KIWAY_PLAYER * Player(FRAME_T aFrameType, bool doCreate=true, wxTopLevelWindow *aParent=nullptr)
Return the KIWAY_PLAYER* given a FRAME_T.
virtual void ExpressMail(FRAME_T aDestination, MAIL_T aCommand, std::string &aPayload, wxWindow *aSource=nullptr, bool aFromOtherThread=false)
Send aPayload to aDestination from aSource.
FACE_T
Known KIFACE implementations.
virtual void CommonSettingsChanged(int aFlags=0)
Call CommonSettingsChanged() on all KIWAY_PLAYERs.
virtual PROJECT & Prj() const
Return the PROJECT associated with this KIWAY.
std::optional< float > AsyncLoadProgress() const
Returns async load progress between 0.0 and 1.0, or nullopt if load is not in progress.
wxString GetLibraryLoadErrors() const
Returns all library load errors as newline-separated strings for display.
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.
The main frame for Pcbnew.
Container for data for KiCad programs.
virtual BACKGROUND_JOBS_MONITOR & GetBackgroundJobMonitor() const
void ClearLibraryLoadMessages()
Clear library load messages from all registered status bars.
void AddLibraryLoadMessages(const std::vector< LOAD_MESSAGE > &aMessages)
Add library load messages to all registered status bars.
virtual SETTINGS_MANAGER & GetSettingsManager() const
A progress reporter interface for use in multi-threaded environments.
static FOOTPRINT_LIBRARY_ADAPTER * FootprintLibAdapter(PROJECT *aProject)
Container for project specific data.
A pure virtual class used to derive REPORTER objects from.
T * RegisterSettings(T *aSettings, bool aLoadNow=true)
Take ownership of the pointer passed in.
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 KICAD_PCB_PORT_SERVICE_NUMBER
Pcbnew listens on this port for commands from Eeschema.
Abstract pattern-matching tool and implementations.
FRAME_T
The set of EDA_BASE_FRAME derivatives, typically stored in EDA_BASE_FRAME::m_Ident.
@ PANEL_FP_DEFAULT_GRAPHICS_VALUES
@ PANEL_FP_DISPLAY_OPTIONS
@ PANEL_FP_USER_LAYER_NAMES
@ DIALOG_PCB_LIBRARY_TABLE
@ FRAME_FOOTPRINT_PREVIEW
@ FRAME_FOOTPRINT_CHOOSER
@ PANEL_FP_DEFAULT_FIELDS
@ PANEL_PCB_ACTION_PLUGINS
@ PANEL_3DV_DISPLAY_OPTIONS
void CopySexprFile(const wxString &aSrcPath, const wxString &aDestPath, std::function< bool(const std::string &token, wxString &value)> aCallback, wxString &aErrors)
void KiCopyFile(const wxString &aSrcPath, const wxString &aDestPath, wxString &aErrors)
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.
@ KIFACE_SCRIPTING_LEGACY
@ KIFACE_FOOTPRINT_LIBRARY_ADAPTER
@ KIFACE_FILTER_FOOTPRINTS
Function pointer type: wxString (*)(const wxString& aFilterJson) Input JSON: {"pin_count": N,...
#define KFCTL_CLI
Running as CLI app.
This file contains miscellaneous commonly used macros and functions.
PCB::IFACE KIFACE_BASE, UNITS_PROVIDER kiface("pcbnew", KIWAY::FACE_PCB)
void InvokePcbLibTableEditor(KIWAY *aKiway, wxWindow *aCaller)
Function InvokePcbLibTableEditor shows the modal DIALOG_FP_LIB_TABLE for purposes of editing the glob...
PyObject * PyInit__pcbnew(void)
static wxString filterFootprints(const wxString &aFilterJson)
Filter footprints based on criteria passed as JSON.
KIFACE_BASE & Kiface()
Global KIFACE_BASE "get" accessor.
void ScriptingSetPcbEditFrame(PCB_EDIT_FRAME *aPcbEditFrame)
PGM_BASE & Pgm()
The global program "get" accessor.
T * GetToolbarSettings(const wxString &aFilename)
T * GetAppSettings(const char *aFilename)
std::vector< LOAD_MESSAGE > ExtractLibraryLoadErrors(const wxString &aErrorString, int aSeverity)
Parse library load error messages, extracting user-facing information while stripping internal code l...
bool OnKifaceStart(PGM_BASE *aProgram, int aCtlBits, KIWAY *aKiway) override
Typically start_common() is called from here.
Implement a participant in the KIWAY alchemy.
std::atomic_bool m_libraryPreloadInProgress
bool OnKifaceStart(PGM_BASE *aProgram, int aCtlBits, KIWAY *aKiway) override
Typically start_common() is called from here.
void PreloadLibraries(KIWAY *aKiway) override
void ProjectChanged() override
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 ...
void CancelPreload(bool aBlock=true) override
std::shared_ptr< BACKGROUND_JOB > m_libraryPreloadBackgroundJob
void * IfaceOrAddress(int aDataId) override
Return a pointer to the requested object.
void Reset() override
Reloads global state.
wxWindow * CreateKiWindow(wxWindow *aParent, int aClassId, KIWAY *aKiway, int aCtlBits=0) override
Create a wxWindow for the current project.
IFACE(const char *aName, KIWAY::FACE_T aType)
std::atomic_bool m_libraryPreloadAbort
int HandleJob(JOB *aJob, REPORTER *aReporter, PROGRESS_REPORTER *aProgressReporter) override
std::unique_ptr< PCBNEW_JOBS_HANDLER > m_jobHandler
bool HandleJobConfig(JOB *aJob, wxWindow *aParent) override
void OnKifaceEnd() override
Called just once just before the DSO is to be unloaded.
std::future< void > m_libraryPreloadReturn
KIBIS top(path, &reporter)
VECTOR3I expected(15, 30, 45)
thread_pool & GetKiCadThreadPool()
Get a reference to the current thread pool.
BS::priority_thread_pool thread_pool
wxLogTrace helper definitions.
Definition of file extensions used in Kicad.