KiCad PCB EDA Suite
footprint_info.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, <jp.charras@wanadoo.fr>
5  * Copyright (C) 2013-2016 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
6  * Copyright (C) 1992-2021 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, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25 
26 /*
27  * Functions to read footprint libraries and fill m_footprints by available footprints names
28  * and their documentation (comments and keywords)
29  */
30 
31 #include <footprint_info.h>
32 #include <fp_lib_table.h>
34 #include <string_utils.h>
35 #include <kiface_ids.h>
36 #include <kiway.h>
37 #include <lib_id.h>
38 #include <thread>
39 #include <utility>
40 #include <kiface_base.h>
41 
42 FOOTPRINT_INFO* FOOTPRINT_LIST::GetFootprintInfo( const wxString& aLibNickname,
43  const wxString& aFootprintName )
44 {
45  if( aFootprintName.IsEmpty() )
46  return nullptr;
47 
48  for( std::unique_ptr<FOOTPRINT_INFO>& fp : m_list )
49  {
50  if( aLibNickname == fp->GetLibNickname() && aFootprintName == fp->GetFootprintName() )
51  return fp.get();
52  }
53 
54  return nullptr;
55 }
56 
57 
58 FOOTPRINT_INFO* FOOTPRINT_LIST::GetFootprintInfo( const wxString& aFootprintName )
59 {
60  if( aFootprintName.IsEmpty() )
61  return nullptr;
62 
63  LIB_ID fpid;
64 
65  wxCHECK_MSG( fpid.Parse( aFootprintName ) < 0, nullptr,
66  wxString::Format( wxT( "'%s' is not a valid LIB_ID." ), aFootprintName ) );
67 
68  return GetFootprintInfo( fpid.GetLibNickname(), fpid.GetLibItemName() );
69 }
70 
71 
72 bool FOOTPRINT_INFO::InLibrary( const wxString& aLibrary ) const
73 {
74  return aLibrary == m_nickname;
75 }
76 
77 
78 bool operator<( const FOOTPRINT_INFO& lhs, const FOOTPRINT_INFO& rhs )
79 {
80  int retv = StrNumCmp( lhs.m_nickname, rhs.m_nickname, false );
81 
82  if( retv != 0 )
83  return retv < 0;
84 
85  // Technically footprint names are not case sensitive because the file name is used
86  // as the footprint name. On windows this would be problematic because windows does
87  // not support case sensitive file names by default. This should not cause any issues
88  // and allow for a future change to use the name defined in the footprint file.
89  return StrNumCmp( lhs.m_fpname, rhs.m_fpname, false ) < 0;
90 }
91 
92 
93 void FOOTPRINT_LIST::DisplayErrors( wxTopLevelWindow* aWindow )
94 {
95  // @todo: go to a more HTML !<table>! ? centric output, possibly with recommendations
96  // for remedy of errors. Add numeric error codes to PARSE_ERROR, and switch on them for
97  // remedies, etc. Full access is provided to everything in every exception!
98 
99  HTML_MESSAGE_BOX dlg( aWindow, _( "Load Error" ) );
100 
101  dlg.MessageSet( _( "Errors were encountered loading footprints:" ) );
102 
103  wxString msg;
104 
105  while( std::unique_ptr<IO_ERROR> error = PopError() )
106  {
107  wxString tmp = error->Problem();
108 
109  // Preserve new lines in error messages so queued errors don't run together.
110  tmp.Replace( "\n", "<BR>" );
111  msg += wxT( "<p>" ) + tmp + wxT( "</p>" );
112  }
113 
114  dlg.AddHTML_Text( msg );
115 
116  dlg.ShowModal();
117 }
118 
119 
120 static FOOTPRINT_LIST* get_instance_from_id( KIWAY& aKiway, int aId )
121 {
122  void* ptr = nullptr;
123 
124  try
125  {
126  ptr = Kiface().IfaceOrAddress( aId );
127 
128  if( !ptr )
129  {
130  KIFACE* kiface = aKiway.KiFACE( KIWAY::FACE_PCB );
131  ptr = kiface->IfaceOrAddress( aId );
132  }
133 
134  return static_cast<FOOTPRINT_LIST*>( ptr );
135  }
136  catch( ... )
137  {
138  return nullptr;
139  }
140 }
141 
142 
144 {
145  FOOTPRINT_LIST* footprintInfo = get_instance_from_id( aKiway, KIFACE_FOOTPRINT_LIST );
146 
147  if( !footprintInfo )
148  return nullptr;
149 
150  if( !footprintInfo->GetCount() )
151  footprintInfo->ReadCacheFromFile( aKiway.Prj().GetProjectPath() + "fp-info-cache" );
152 
153  return footprintInfo;
154 }
155 
156 
158 {
159  m_total_libs = 0;
160 }
161 
162 
164 {
165  // This is NOP if the load has finished
166  Abort();
167 }
168 
169 
171 {
172  m_list = aList;
173 }
174 
175 
176 void FOOTPRINT_ASYNC_LOADER::Start( FP_LIB_TABLE* aTable, wxString const* aNickname,
177  unsigned aNThreads )
178 {
179  // Disable KIID generation: not needed for library parts; sometimes very slow
180  KIID::CreateNilUuids( true );
181 
182  // Capture the FP_LIB_TABLE into m_last_table. Formatting it as a string instead of storing the
183  // raw data avoids having to pull in the FP-specific parts.
184  STRING_FORMATTER sof;
185  aTable->Format( &sof, 0 );
186  m_last_table = sof.GetString();
187 
188  m_list->startWorkers( aTable, aNickname, this, aNThreads );
189 }
190 
191 
193 {
194  if( m_list )
195  {
196  bool rv = m_list->joinWorkers();
197  m_list = nullptr;
198  return rv;
199  }
200  else
201  return true;
202 }
203 
204 
206 {
207  if( m_list )
208  {
209  m_list->stopWorkers();
210  m_list = nullptr;
211  }
212 
213  KIID::CreateNilUuids( false );
214 }
BITMAP2CMP_SETTINGS kiface
const UTF8 & GetLibItemName() const
Definition: lib_id.h:104
static FOOTPRINT_LIST * get_instance_from_id(KIWAY &aKiway, int aId)
virtual void * IfaceOrAddress(int aDataId) override=0
Return pointer to the requested object.
bool InLibrary(const wxString &aLibrary) const
Test if the FOOTPRINT_INFO object was loaded from aLibrary.
KIFACE_BASE & Kiface()
Global KIFACE_BASE "get" accessor.
virtual PROJECT & Prj() const
Return the PROJECT associated with this KIWAY.
Definition: kiway.cpp:190
virtual void stopWorkers()=0
Stop worker threads.
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:51
FOOTPRINT_LIST * m_list
virtual const wxString GetProjectPath() const
Return the full path of the project.
Definition: project.cpp:122
FOOTPRINT_ASYNC_LOADER()
Construct an asynchronous loader.
virtual bool joinWorkers()=0
Join worker threads.
static FOOTPRINT_LIST * GetInstance(KIWAY &aKiway)
Factory function to return a FOOTPRINT_LIST via Kiway.
unsigned GetCount() const
wxString m_fpname
Module name.
bool operator<(const FOOTPRINT_INFO &lhs, const FOOTPRINT_INFO &rhs)
A minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the same KiCad...
Definition: kiway.h:260
#define _(s)
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
Definition: lib_id.h:90
pcbnew DSO
Definition: kiway.h:269
FOOTPRINT_INFO * GetFootprintInfo(const wxString &aFootprintName)
Get info for a footprint by id.
bool Join()
Wait until the worker threads are finished, and then perform any required single-threaded finishing o...
const std::string & GetString()
Definition: richio.h:438
virtual KIFACE * KiFACE(FACE_T aFaceId, bool doLoad=true)
Return the KIFACE* given a FACE_T.
Definition: kiway.cpp:196
void DisplayErrors(wxTopLevelWindow *aCaller=nullptr)
void MessageSet(const wxString &message)
Add a message (in bold) to message list.
std::unique_ptr< IO_ERROR > PopError()
virtual void Format(OUTPUTFORMATTER *aOutput, int aIndentLevel) const override
Generate the table in s-expression format to aOutput with an indentation level of aIndentLevel.
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
int Parse(const UTF8 &aId, bool aFix=false)
Parse LIB_ID with the information from aId.
Definition: lib_id.cpp:49
Holds a list of FOOTPRINT_INFO objects, along with a list of IO_ERRORs or PARSE_ERRORs that were thro...
virtual void ReadCacheFromFile(const wxString &aFilePath)
static void CreateNilUuids(bool aNil=true)
Definition: kiid.cpp:240
virtual void startWorkers(FP_LIB_TABLE *aTable, const wxString *aNickname, FOOTPRINT_ASYNC_LOADER *aLoader, unsigned aNThreads)=0
Launch worker threads to load footprints.
void AddHTML_Text(const wxString &message)
Add HTML text (without any change) to message list.
Implement a participant in the KIWAY alchemy.
Definition: kiway.h:148
FOOTPRINT_LIST * m_list
void Start(FP_LIB_TABLE *aTable, const wxString *aNickname=nullptr, unsigned aNThreads=DEFAULT_THREADS)
Launch the worker threads.
int StrNumCmp(const wxString &aString1, const wxString &aString2, bool aIgnoreCase)
Compare two strings with alphanumerical content.
wxString m_nickname
library as known in FP_LIB_TABLE
void SetList(FOOTPRINT_LIST *aList)
Assign a FOOTPRINT_LIST to the loader.
Implement an OUTPUTFORMATTER to a memory buffer.
Definition: richio.h:414
Return a pointer to the global instance of FOOTPRINT_LIST from pcbnew.
Definition: kiface_ids.h:39
void Abort()
Safely stop the current process.