KiCad PCB EDA Suite
footprint_info_impl.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) 2011 Jean-Pierre Charras, <[email protected]>
5 * Copyright (C) 2013-2016 SoftPLC Corporation, Dick Hollenbeck <[email protected]>
6 * Copyright (C) 1992-2022 KiCad Developers, see AUTHORS.txt for contributors.
7 *
8 * This program is free software: you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation, either version 3 of the License, or (at your
11 * option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22
23#include <footprint_info_impl.h>
24
26#include <footprint.h>
27#include <footprint_info.h>
28#include <fp_lib_table.h>
29#include <kiway.h>
30#include <locale_io.h>
31#include <lib_id.h>
32#include <progress_reporter.h>
33#include <string_utils.h>
34#include <thread_pool.h>
36
37#include <wx/textfile.h>
38#include <wx/txtstrm.h>
39#include <wx/wfstream.h>
40
41
43{
44 FP_LIB_TABLE* fptable = m_owner->GetTable();
45
46 wxASSERT( fptable );
47
48 const FOOTPRINT* footprint = fptable->GetEnumeratedFootprint( m_nickname, m_fpname );
49
50 if( footprint == nullptr ) // Should happen only with malformed/broken libraries
51 {
52 m_pad_count = 0;
54 }
55 else
56 {
59 m_keywords = footprint->GetKeywords();
60 m_doc = footprint->GetDescription();
61 }
62
63 m_loaded = true;
64}
65
66
67bool FOOTPRINT_LIST_IMPL::CatchErrors( const std::function<void()>& aFunc )
68{
69 try
70 {
71 aFunc();
72 }
73 catch( const IO_ERROR& ioe )
74 {
75 m_errors.move_push( std::make_unique<IO_ERROR>( ioe ) );
76 return false;
77 }
78 catch( const std::exception& se )
79 {
80 // This is a round about way to do this, but who knows what THROW_IO_ERROR()
81 // may be tricked out to do someday, keep it in the game.
82 try
83 {
84 THROW_IO_ERROR( se.what() );
85 }
86 catch( const IO_ERROR& ioe )
87 {
88 m_errors.move_push( std::make_unique<IO_ERROR>( ioe ) );
89 }
90
91 return false;
92 }
93
94 return true;
95}
96
97
98bool FOOTPRINT_LIST_IMPL::ReadFootprintFiles( FP_LIB_TABLE* aTable, const wxString* aNickname,
99 PROGRESS_REPORTER* aProgressReporter )
100{
101 long long int generatedTimestamp = 0;
102
103 if( !CatchErrors( [&]()
104 {
105 generatedTimestamp = aTable->GenerateTimestamp( aNickname );
106 } ) )
107 {
108 return false;
109 }
110
111 if( generatedTimestamp == m_list_timestamp )
112 return true;
113
114 // Disable KIID generation: not needed for library parts; sometimes very slow
115 KIID_NIL_SET_RESET reset_kiid;
116
117 m_progress_reporter = aProgressReporter;
118
120 {
122 m_progress_reporter->Report( _( "Fetching footprint libraries..." ) );
123 }
124
125 m_cancelled = false;
126 m_lib_table = aTable;
127
128 // Clear data before reading files
129 m_errors.clear();
130 m_list.clear();
133
134 if( aNickname )
135 {
136 m_queue_in.push( *aNickname );
137 }
138 else
139 {
140 for( const wxString& nickname : aTable->GetLogicalLibs() )
141 m_queue_in.push( nickname );
142 }
143
144
145 loadLibs();
146
147 if( !m_cancelled )
148 {
150 {
153 m_progress_reporter->Report( _( "Loading footprints..." ) );
154 }
155
157
160 }
161
162 if( m_cancelled )
163 m_list_timestamp = 0; // God knows what we got before we were canceled
164 else
165 m_list_timestamp = generatedTimestamp;
166
167 return m_errors.empty();
168}
169
170
172{
174 size_t num_returns = m_queue_in.size();
175 std::vector<std::future<size_t>> returns( num_returns );
176
177 auto loader_job =
178 [this]() -> size_t
179 {
180 wxString nickname;
181 size_t retval = 0;
182
183 if( !m_cancelled && m_queue_in.pop( nickname ) )
184 {
185 if( CatchErrors( [this, &nickname]()
186 {
187 m_lib_table->PrefetchLib( nickname );
188 m_queue_out.push( nickname );
189 } ) && m_progress_reporter )
190 {
192 }
193
194 ++retval;
195 }
196
197 return retval;
198 };
199
200 for( size_t ii = 0; ii < num_returns; ++ii )
201 returns[ii] = tp.submit( loader_job );
202
203 for( const std::future<size_t>& ret : returns )
204 {
205 std::future_status status = ret.wait_for( std::chrono::milliseconds( 250 ) );
206
207 while( status != std::future_status::ready )
208 {
210 m_cancelled = true;
211
212 status = ret.wait_for( std::chrono::milliseconds( 250 ) );
213 }
214 }
215}
216
217
219{
220 LOCALE_IO toggle_locale;
221
222 // Parse the footprints in parallel. WARNING! This requires changing the locale, which is
223 // GLOBAL. It is only thread safe to construct the LOCALE_IO before the threads are created,
224 // destroy it after they finish, and block the main (GUI) thread while they work. Any deviation
225 // from this will cause nasal demons.
226 //
227 // TODO: blast LOCALE_IO into the sun
228
231 size_t num_elements = m_queue_out.size();
232 std::vector<std::future<size_t>> returns( num_elements );
233
234 auto fp_thread =
235 [ this, &queue_parsed ]() -> size_t
236 {
237 wxString nickname;
238
239 if( m_cancelled || !m_queue_out.pop( nickname ) )
240 return 0;
241
242 wxArrayString fpnames;
243
245 [&]()
246 {
247 m_lib_table->FootprintEnumerate( fpnames, nickname, false );
248 } );
249
250 for( wxString fpname : fpnames )
251 {
253 [&]()
254 {
255 auto* fpinfo = new FOOTPRINT_INFO_IMPL( this, nickname, fpname );
256 queue_parsed.move_push( std::unique_ptr<FOOTPRINT_INFO>( fpinfo ) );
257 } );
258
259 if( m_cancelled )
260 return 0;
261 }
262
265
266 return 1;
267 };
268
269 for( size_t ii = 0; ii < num_elements; ++ii )
270 returns[ii] = tp.submit( fp_thread );
271
272 for( const std::future<size_t>& ret : returns )
273 {
274 std::future_status status = ret.wait_for( std::chrono::milliseconds( 250 ) );
275
276 while( status != std::future_status::ready )
277 {
280
281 status = ret.wait_for( std::chrono::milliseconds( 250 ) );
282 }
283 }
284
285 std::unique_ptr<FOOTPRINT_INFO> fpi;
286
287 while( queue_parsed.pop( fpi ) )
288 m_list.push_back( std::move( fpi ) );
289
290 std::sort( m_list.begin(), m_list.end(),
291 []( std::unique_ptr<FOOTPRINT_INFO> const& lhs,
292 std::unique_ptr<FOOTPRINT_INFO> const& rhs ) -> bool
293 {
294 return *lhs < *rhs;
295 } );
296}
297
298
300 m_list_timestamp( 0 ),
301 m_progress_reporter( nullptr ),
302 m_cancelled( false )
303{
304}
305
306
307void FOOTPRINT_LIST_IMPL::WriteCacheToFile( const wxString& aFilePath )
308{
309 wxFileName tmpFileName = wxFileName::CreateTempFileName( aFilePath );
310 wxFFileOutputStream outStream( tmpFileName.GetFullPath() );
311 wxTextOutputStream txtStream( outStream );
312
313 if( !outStream.IsOk() )
314 {
315 return;
316 }
317
318 txtStream << wxString::Format( wxT( "%lld" ), m_list_timestamp ) << endl;
319
320 for( std::unique_ptr<FOOTPRINT_INFO>& fpinfo : m_list )
321 {
322 txtStream << fpinfo->GetLibNickname() << endl;
323 txtStream << fpinfo->GetName() << endl;
324 txtStream << EscapeString( fpinfo->GetDescription(), CTX_LINE ) << endl;
325 txtStream << EscapeString( fpinfo->GetKeywords(), CTX_LINE ) << endl;
326 txtStream << wxString::Format( wxT( "%d" ), fpinfo->GetOrderNum() ) << endl;
327 txtStream << wxString::Format( wxT( "%u" ), fpinfo->GetPadCount() ) << endl;
328 txtStream << wxString::Format( wxT( "%u" ), fpinfo->GetUniquePadCount() ) << endl;
329 }
330
331 txtStream.Flush();
332 outStream.Close();
333
334 if( !wxRenameFile( tmpFileName.GetFullPath(), aFilePath, true ) )
335 {
336 // cleanup in case rename failed
337 // its also not the end of the world since this is just a cache file
338 wxRemoveFile( tmpFileName.GetFullPath() );
339 }
340}
341
342
343void FOOTPRINT_LIST_IMPL::ReadCacheFromFile( const wxString& aFilePath )
344{
345 wxTextFile cacheFile( aFilePath );
346
348 m_list.clear();
349
350 try
351 {
352 if( cacheFile.Exists() && cacheFile.Open() )
353 {
354 cacheFile.GetFirstLine().ToLongLong( &m_list_timestamp );
355
356 while( cacheFile.GetCurrentLine() + 6 < cacheFile.GetLineCount() )
357 {
358 wxString libNickname = cacheFile.GetNextLine();
359 wxString name = cacheFile.GetNextLine();
360 wxString desc = UnescapeString( cacheFile.GetNextLine() );
361 wxString keywords = UnescapeString( cacheFile.GetNextLine() );
362 int orderNum = wxAtoi( cacheFile.GetNextLine() );
363 unsigned int padCount = (unsigned) wxAtoi( cacheFile.GetNextLine() );
364 unsigned int uniquePadCount = (unsigned) wxAtoi( cacheFile.GetNextLine() );
365
366 FOOTPRINT_INFO_IMPL* fpinfo = new FOOTPRINT_INFO_IMPL( libNickname, name, desc,
367 keywords, orderNum,
368 padCount, uniquePadCount );
369
370 m_list.emplace_back( std::unique_ptr<FOOTPRINT_INFO>( fpinfo ) );
371 }
372 }
373 }
374 catch( ... )
375 {
376 // whatever went wrong, invalidate the cache
378 }
379
380 // Sanity check: an empty list is very unlikely to be correct.
381 if( m_list.size() == 0 )
383
384 if( cacheFile.IsOpened() )
385 cacheFile.Close();
386}
const char * name
Definition: DXF_plotter.cpp:56
virtual void load() override
lazily load stuff not filled in by constructor. This may throw IO_ERRORS.
wxString m_doc
Footprint description.
wxString m_fpname
Module name.
wxString m_keywords
Footprint keywords.
unsigned m_unique_pad_count
Number of unique pads.
unsigned m_pad_count
Number of pads.
FOOTPRINT_LIST * m_owner
provides access to FP_LIB_TABLE
wxString m_nickname
library as known in FP_LIB_TABLE
bool ReadFootprintFiles(FP_LIB_TABLE *aTable, const wxString *aNickname=nullptr, PROGRESS_REPORTER *aProgressReporter=nullptr) override
Read all the footprints provided by the combination of aTable and aNickname.
std::atomic_bool m_cancelled
SYNC_QUEUE< wxString > m_queue_in
bool CatchErrors(const std::function< void()> &aFunc)
Call aFunc, pushing any IO_ERRORs and std::exceptions it throws onto m_errors.
void WriteCacheToFile(const wxString &aFilePath) override
void ReadCacheFromFile(const wxString &aFilePath) override
PROGRESS_REPORTER * m_progress_reporter
SYNC_QUEUE< wxString > m_queue_out
FP_LIB_TABLE * m_lib_table
no ownership
ERRLIST m_errors
some can be PARSE_ERRORs also
FP_LIB_TABLE * GetTable() const
unsigned GetPadCount(INCLUDE_NPTH_T aIncludeNPTH=INCLUDE_NPTH_T(INCLUDE_NPTH)) const
Return the number of pads.
Definition: footprint.cpp:1210
wxString GetDescription() const
Definition: footprint.h:218
unsigned GetUniquePadCount(INCLUDE_NPTH_T aIncludeNPTH=INCLUDE_NPTH_T(INCLUDE_NPTH)) const
Return the number of unique non-blank pads.
Definition: footprint.cpp:1229
wxString GetKeywords() const
Definition: footprint.h:221
void FootprintEnumerate(wxArrayString &aFootprintNames, const wxString &aNickname, bool aBestEfforts)
Return a list of footprint names contained within the library given by aNickname.
const FOOTPRINT * GetEnumeratedFootprint(const wxString &aNickname, const wxString &aFootprintName)
A version of FootprintLoad() for use after FootprintEnumerate() for more efficient cache management.
long long GenerateTimestamp(const wxString *aNickname)
Generate a hashed timestamp representing the last-mod-times of the library indicated by aNickname,...
void PrefetchLib(const wxString &aNickname)
If possible, prefetches the specified library (e.g.
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:76
RAII class to safely set/reset nil KIIDs for use in footprint/symbol loading.
Definition: kiid.h:199
std::vector< wxString > GetLogicalLibs()
Return the logical library names, all of them that are pertinent to a look up done on this LIB_TABLE.
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition: locale_io.h:41
A progress reporter interface for use in multi-threaded environments.
virtual bool KeepRefreshing(bool aWait=false)=0
Update the UI (if any).
virtual void Report(const wxString &aMessage)=0
Display aMessage in the progress bar dialog.
virtual void AdvancePhase()=0
Use the next available virtual zone of the dialog progress bar.
virtual void AdvanceProgress()=0
Increment the progress bar length (inside the current virtual zone).
virtual void SetMaxProgress(int aMaxProgress)=0
Fix the value that gives the 100 percent progress bar length (inside the current virtual zone).
Synchronized, locking queue.
Definition: sync_queue.h:32
bool pop(T &aReceiver)
Pop a value if the queue into the provided variable.
Definition: sync_queue.h:63
bool empty() const
Return true if the queue is empty.
Definition: sync_queue.h:82
void clear()
Clear the queue.
Definition: sync_queue.h:100
size_t size() const
Return the size of the queue.
Definition: sync_queue.h:91
void push(T const &aValue)
Push a value onto the queue.
Definition: sync_queue.h:41
void move_push(T &&aValue)
Move a value onto the queue.
Definition: sync_queue.h:50
#define _(s)
@ DO_NOT_INCLUDE_NPTH
Definition: footprint.h:57
#define THROW_IO_ERROR(msg)
Definition: ki_exception.h:38
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
wxString UnescapeString(const wxString &aSource)
wxString EscapeString(const wxString &aSource, ESCAPE_CONTEXT aContext)
The Escape/Unescape routines use HTML-entity-reference-style encoding to handle characters which are:...
@ CTX_LINE
Definition: string_utils.h:59
thread_pool & GetKiCadThreadPool()
Get a reference to the current thread pool.
Definition: thread_pool.cpp:32
static thread_pool * tp
Definition: thread_pool.cpp:30
BS::thread_pool thread_pool
Definition: thread_pool.h:30
Definition of file extensions used in Kicad.