KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 The 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 <lib_id.h>
31#include <progress_reporter.h>
32#include <string_utils.h>
33#include <thread_pool.h>
35
36#include <kiplatform/io.h>
37
38#include <wx/textfile.h>
39#include <wx/txtstrm.h>
40#include <wx/wfstream.h>
41
42
44{
45 FP_LIB_TABLE* fptable = m_owner->GetTable();
46
47 wxASSERT( fptable );
48
49 const FOOTPRINT* footprint = fptable->GetEnumeratedFootprint( m_nickname, m_fpname );
50
51 if( footprint == nullptr ) // Should happen only with malformed/broken libraries
52 {
53 m_pad_count = 0;
55 }
56 else
57 {
60 m_keywords = footprint->GetKeywords();
61 m_doc = footprint->GetLibDescription();
62 }
63
64 m_loaded = true;
65}
66
67
69{
70 m_list.clear();
72}
73
74
75bool FOOTPRINT_LIST_IMPL::CatchErrors( const std::function<void()>& aFunc )
76{
77 try
78 {
79 aFunc();
80 }
81 catch( const IO_ERROR& ioe )
82 {
83 m_errors.move_push( std::make_unique<IO_ERROR>( ioe ) );
84 return false;
85 }
86 catch( const std::exception& se )
87 {
88 // This is a round about way to do this, but who knows what THROW_IO_ERROR()
89 // may be tricked out to do someday, keep it in the game.
90 try
91 {
92 THROW_IO_ERROR( se.what() );
93 }
94 catch( const IO_ERROR& ioe )
95 {
96 m_errors.move_push( std::make_unique<IO_ERROR>( ioe ) );
97 }
98
99 return false;
100 }
101
102 return true;
103}
104
105
106bool FOOTPRINT_LIST_IMPL::ReadFootprintFiles( FP_LIB_TABLE* aTable, const wxString* aNickname,
107 PROGRESS_REPORTER* aProgressReporter )
108{
109 long long int generatedTimestamp = 0;
110
111 if( !CatchErrors( [&]()
112 {
113 generatedTimestamp = aTable->GenerateTimestamp( aNickname );
114 } ) )
115 {
116 return false;
117 }
118
119 if( generatedTimestamp == m_list_timestamp )
120 return true;
121
122 // Disable KIID generation: not needed for library parts; sometimes very slow
123 KIID_NIL_SET_RESET reset_kiid;
124
125 m_progress_reporter = aProgressReporter;
126
127 m_cancelled = false;
128 m_lib_table = aTable;
129
130 // Clear data before reading files
131 m_errors.clear();
132 m_list.clear();
133 m_queue.clear();
134
135 if( aNickname )
136 {
137 m_queue.push( *aNickname );
138 }
139 else
140 {
141 for( const wxString& nickname : aTable->GetLogicalLibs() )
142 m_queue.push( nickname );
143 }
144
146 {
148 m_progress_reporter->Report( _( "Loading footprints..." ) );
149 }
150
152
155
156 if( m_cancelled )
157 m_list_timestamp = 0; // God knows what we got before we were canceled
158 else
159 m_list_timestamp = generatedTimestamp;
160
161 return m_errors.empty();
162}
163
164
166{
167 // Parse the footprints in parallel.
168
171 size_t num_elements = m_queue.size();
172 std::vector<std::future<size_t>> returns( num_elements );
173
174 auto fp_thread =
175 [ this, &queue_parsed ]() -> size_t
176 {
177 wxString nickname;
178
179 if( m_cancelled || !m_queue.pop( nickname ) )
180 return 0;
181
182 wxArrayString fpnames;
183
185 [&]()
186 {
187 m_lib_table->FootprintEnumerate( fpnames, nickname, false );
188 } );
189
190 for( wxString fpname : fpnames )
191 {
193 [&]()
194 {
195 auto* fpinfo = new FOOTPRINT_INFO_IMPL( this, nickname, fpname );
196 queue_parsed.move_push( std::unique_ptr<FOOTPRINT_INFO>( fpinfo ) );
197 } );
198
199 if( m_cancelled )
200 return 0;
201 }
202
205
206 return 1;
207 };
208
209 for( size_t ii = 0; ii < num_elements; ++ii )
210 returns[ii] = tp.submit( fp_thread );
211
212 for( const std::future<size_t>& ret : returns )
213 {
214 std::future_status status = ret.wait_for( std::chrono::milliseconds( 250 ) );
215
216 while( status != std::future_status::ready )
217 {
220
221 status = ret.wait_for( std::chrono::milliseconds( 250 ) );
222 }
223 }
224
225 std::unique_ptr<FOOTPRINT_INFO> fpi;
226
227 while( queue_parsed.pop( fpi ) )
228 m_list.push_back( std::move( fpi ) );
229
230 std::sort( m_list.begin(), m_list.end(),
231 []( std::unique_ptr<FOOTPRINT_INFO> const& lhs,
232 std::unique_ptr<FOOTPRINT_INFO> const& rhs ) -> bool
233 {
234 return *lhs < *rhs;
235 } );
236}
237
238
240 m_list_timestamp( 0 ),
241 m_progress_reporter( nullptr ),
242 m_cancelled( false )
243{
244}
245
246
247void FOOTPRINT_LIST_IMPL::WriteCacheToFile( const wxString& aFilePath )
248{
249 wxFileName tmpFileName = wxFileName::CreateTempFileName( aFilePath );
250 wxFFileOutputStream outStream( tmpFileName.GetFullPath() );
251 wxTextOutputStream txtStream( outStream );
252
253 if( !outStream.IsOk() )
254 {
255 return;
256 }
257
258 txtStream << wxString::Format( wxT( "%lld" ), m_list_timestamp ) << endl;
259
260 for( std::unique_ptr<FOOTPRINT_INFO>& fpinfo : m_list )
261 {
262 txtStream << fpinfo->GetLibNickname() << endl;
263 txtStream << fpinfo->GetName() << endl;
264 txtStream << EscapeString( fpinfo->GetDesc(), CTX_LINE ) << endl;
265 txtStream << EscapeString( fpinfo->GetKeywords(), CTX_LINE ) << endl;
266 txtStream << wxString::Format( wxT( "%d" ), fpinfo->GetOrderNum() ) << endl;
267 txtStream << wxString::Format( wxT( "%u" ), fpinfo->GetPadCount() ) << endl;
268 txtStream << wxString::Format( wxT( "%u" ), fpinfo->GetUniquePadCount() ) << endl;
269 }
270
271 txtStream.Flush();
272 outStream.Close();
273
274 // Preserve the permissions of the current file
275 KIPLATFORM::IO::DuplicatePermissions( aFilePath, tmpFileName.GetFullPath() );
276
277 if( !wxRenameFile( tmpFileName.GetFullPath(), aFilePath, true ) )
278 {
279 // cleanup in case rename failed
280 // its also not the end of the world since this is just a cache file
281 wxRemoveFile( tmpFileName.GetFullPath() );
282 }
283}
284
285
286void FOOTPRINT_LIST_IMPL::ReadCacheFromFile( const wxString& aFilePath )
287{
288 wxTextFile cacheFile( aFilePath );
289
291 m_list.clear();
292
293 try
294 {
295 if( cacheFile.Exists() && cacheFile.Open() )
296 {
297 cacheFile.GetFirstLine().ToLongLong( &m_list_timestamp );
298
299 while( cacheFile.GetCurrentLine() + 6 < cacheFile.GetLineCount() )
300 {
301 wxString libNickname = cacheFile.GetNextLine();
302 wxString name = cacheFile.GetNextLine();
303 wxString desc = UnescapeString( cacheFile.GetNextLine() );
304 wxString keywords = UnescapeString( cacheFile.GetNextLine() );
305 int orderNum = wxAtoi( cacheFile.GetNextLine() );
306 unsigned int padCount = (unsigned) wxAtoi( cacheFile.GetNextLine() );
307 unsigned int uniquePadCount = (unsigned) wxAtoi( cacheFile.GetNextLine() );
308
309 FOOTPRINT_INFO_IMPL* fpinfo = new FOOTPRINT_INFO_IMPL( libNickname, name, desc,
310 keywords, orderNum,
311 padCount, uniquePadCount );
312
313 m_list.emplace_back( std::unique_ptr<FOOTPRINT_INFO>( fpinfo ) );
314 }
315 }
316 }
317 catch( ... )
318 {
319 // whatever went wrong, invalidate the cache
321 }
322
323 // Sanity check: an empty list is very unlikely to be correct.
324 if( m_list.size() == 0 )
326
327 if( cacheFile.IsOpened() )
328 cacheFile.Close();
329}
const char * name
Definition: DXF_plotter.cpp:62
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
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
FP_LIB_TABLE * m_lib_table
no ownership
FP_LIB_TABLE * GetTable() const
SYNC_QUEUE< std::unique_ptr< IO_ERROR > > m_errors
some can be PARSE_ERRORs also
std::vector< std::unique_ptr< FOOTPRINT_INFO > > m_list
wxString GetLibDescription() const
Definition: footprint.h:260
unsigned GetPadCount(INCLUDE_NPTH_T aIncludeNPTH=INCLUDE_NPTH_T(INCLUDE_NPTH)) const
Return the number of pads.
Definition: footprint.cpp:2013
unsigned GetUniquePadCount(INCLUDE_NPTH_T aIncludeNPTH=INCLUDE_NPTH_T(INCLUDE_NPTH)) const
Return the number of unique non-blank pads.
Definition: footprint.cpp:2063
wxString GetKeywords() const
Definition: footprint.h:263
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,...
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:77
RAII class to safely set/reset nil KIIDs for use in footprint/symbol loading.
Definition: kiid.h:230
std::vector< wxString > GetLogicalLibs()
Return the logical library names, all of them that are pertinent to a look up done on this LIB_TABLE.
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:69
#define THROW_IO_ERROR(msg)
Definition: ki_exception.h:39
bool DuplicatePermissions(const wxString &aSrc, const wxString &aDest)
Duplicates the file security data from one file to another ensuring that they are the same between bo...
Definition: unix/io.cpp:47
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:30
static thread_pool * tp
Definition: thread_pool.cpp:28
BS::thread_pool thread_pool
Definition: thread_pool.h:31
Definition of file extensions used in Kicad.