KiCad PCB EDA Suite
Loading...
Searching...
No Matches
import_local_footprint_libs.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 The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
21
22#include <wx/filefn.h>
23#include <wx/filename.h>
24#include <wx/log.h>
25
29#include <pgm_base.h>
30#include <project.h>
31#include <project_pcb.h>
32#include <reporter.h>
33#include <pcb_io/pcb_io_mgr.h>
34
35
36std::vector<wxString> RegisterLocalFootprintLibraries( PROJECT& aProject, const wxString& aSourceProjectDir,
37 const std::vector<wxString>& aLibraries,
38 REPORTER& aReporter )
39{
40 std::vector<wxString> added;
41
42 if( aLibraries.empty() )
43 return added;
44
47 std::optional<LIBRARY_TABLE*> optTable =
49
50 wxCHECK_MSG( adapter && optTable.has_value(), added, "Could not load footprint lib table." );
51
52 LIBRARY_TABLE* table = optTable.value();
53 wxString projectPath = aProject.GetProjectPath();
54
55 for( const wxString& file : aLibraries )
56 {
57 if( file.empty() )
58 continue;
59
60 wxFileName fn( file );
61
62 // a project can list a library that was never shipped
63 if( !fn.FileExists() && !wxDirExists( fn.GetFullPath() ) )
64 continue;
65
67
68 if( type == PCB_IO_MGR::FILE_TYPE_NONE )
69 {
70 wxLogTrace( "KIWAY", "Unknown file type: %s", fn.GetFullPath() );
71 continue;
72 }
73
74 // Only libraries that live under the source project are relocated; a plain path prefix would
75 // also match a sibling directory sharing the project name's stem.
76 wxFileName relFn( fn );
77 bool isProjectLocal = !aSourceProjectDir.IsEmpty() && relFn.MakeRelativeTo( aSourceProjectDir )
78 && !relFn.IsAbsolute() && !relFn.GetFullPath().StartsWith( wxS( ".." ) );
79 wxString libTableUri;
80
81 if( isProjectLocal )
82 {
83 // Copy a project-local library into the KiCad project directory and reference it
84 // with a project-relative path so the fp-lib-table stays portable.
85 if( !fn.FileExists() )
86 continue;
87
88 wxFileName projectFn( projectPath, fn.GetFullName() );
89
90 if( fn.GetFullPath() != projectFn.GetFullPath() && !projectFn.FileExists()
91 && !wxCopyFile( fn.GetFullPath(), projectFn.GetFullPath(), false ) )
92 {
93 aReporter.Report( wxString::Format( _( "Error copying footprint library '%s'." ), fn.GetFullPath() ),
95 continue;
96 }
97
98 libTableUri = wxS( "${KIPRJMOD}/" ) + fn.GetFullName();
99 }
100 else
101 {
102 // External library referenced by absolute path. Preserve the original path.
103 libTableUri = fn.GetFullPath();
104 }
105
106 if( !table->HasRow( fn.GetName() ) )
107 {
108 LIBRARY_TABLE_ROW& row = table->InsertRow();
109 row.SetNickname( fn.GetName() );
110 row.SetURI( libTableUri );
111 row.SetType( PCB_IO_MGR::ShowType( type ) );
112 added.emplace_back( fn.GetName() );
113 }
114 }
115
116 if( added.empty() )
117 return added;
118
119 bool success = true;
120
121 table->Save().map_error(
122 [&]( const LIBRARY_ERROR& aError )
123 {
124 aReporter.Report( _( "Error saving project library table:\n\n" ) + aError.message,
126 success = false;
127 } );
128
129 if( !success )
130 return {};
131
132 manager.AbortAsyncLoads();
134
135 for( const wxString& nick : added )
136 adapter->LoadOne( nick );
137
138 return added;
139}
An interface to the global shared library manager that is schematic-specific and linked to one projec...
std::optional< LIB_STATUS > LoadOne(LIB_DATA *aLib) override
Loads or reloads the given library, if it exists.
std::optional< LIBRARY_TABLE * > Table(LIBRARY_TABLE_TYPE aType, LIBRARY_TABLE_SCOPE aScope)
Retrieves a given table; creating a new empty project table if a valid project is loaded and the give...
void AbortAsyncLoads()
Abort any async library loading operations in progress.
void LoadProjectTables(std::initializer_list< LIBRARY_TABLE_TYPE > aTablesToLoad={})
(Re)loads the project library tables in the given list, or all tables if no list is given
void SetNickname(const wxString &aNickname)
void SetType(const wxString &aType)
void SetURI(const wxString &aUri)
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 GuessPluginTypeFromLibPath(const wxString &aLibPath, int aCtl=0)
Return a plugin type given a footprint library's libPath.
static const wxString ShowType(PCB_FILE_T aFileType)
Return a brief name for a plugin given aFileType enum.
virtual LIBRARY_MANAGER & GetLibraryManager() const
Definition pgm_base.h:125
static FOOTPRINT_LIBRARY_ADAPTER * FootprintLibAdapter(PROJECT *aProject)
Container for project specific data.
Definition project.h:63
virtual const wxString GetProjectPath() const
Return the full path of the project.
Definition project.cpp:276
A pure virtual class used to derive REPORTER objects from.
Definition reporter.h:73
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)
Report a string with a given severity.
Definition reporter.h:102
#define _(s)
std::vector< wxString > RegisterLocalFootprintLibraries(PROJECT &aProject, const wxString &aSourceProjectDir, const std::vector< wxString > &aLibraries, REPORTER &aReporter)
Register the footprint libraries a source project names in the project footprint library table.
PGM_BASE & Pgm()
The global program "get" accessor.
see class PGM_BASE
@ RPT_SEVERITY_ERROR
wxString message