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-2020 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 <kicad_string.h>
35 #include <kiface_ids.h>
36 #include <kiway.h>
37 #include <lib_id.h>
38 #include <thread>
39 #include <utility>
40 
41 
42 FOOTPRINT_INFO* FOOTPRINT_LIST::GetFootprintInfo( const wxString& aLibNickname,
43  const wxString& aFootprintName )
44 {
45  if( aFootprintName.IsEmpty() )
46  return NULL;
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 NULL;
55 }
56 
57 
58 FOOTPRINT_INFO* FOOTPRINT_LIST::GetFootprintInfo( const wxString& aFootprintName )
59 {
60  if( aFootprintName.IsEmpty() )
61  return NULL;
62 
63  LIB_ID fpid;
64 
65  wxCHECK_MSG( fpid.Parse( aFootprintName ) < 0, NULL,
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  KIFACE* kiface = aKiway.KiFACE( KIWAY::FACE_PCB );
127 
128  ptr = kiface->IfaceOrAddress( aId );
129 
130  return static_cast<FOOTPRINT_LIST*>( ptr );
131  }
132  catch( ... )
133  {
134  return nullptr;
135  }
136 }
137 
138 
140 {
141  FOOTPRINT_LIST* footprintInfo = get_instance_from_id( aKiway, KIFACE_FOOTPRINT_LIST );
142 
143  if( ! footprintInfo )
144  return nullptr;
145 
146  if( !footprintInfo->GetCount() )
147  {
148  footprintInfo->ReadCacheFromFile( aKiway.Prj().GetProjectPath() + "fp-info-cache" );
149  }
150 
151  return footprintInfo;
152 }
153 
154 
156 {
157  m_total_libs = 0;
158 }
159 
160 
162 {
163  // This is NOP if the load has finished
164  Abort();
165 }
166 
167 
169 {
170  m_list = aList;
171 }
172 
173 
174 void FOOTPRINT_ASYNC_LOADER::Start( FP_LIB_TABLE* aTable, wxString const* aNickname,
175  unsigned aNThreads )
176 {
177  // Capture the FP_LIB_TABLE into m_last_table. Formatting it as a string instead of storing the
178  // raw data avoids having to pull in the FP-specific parts.
179  STRING_FORMATTER sof;
180  aTable->Format( &sof, 0 );
181  m_last_table = sof.GetString();
182 
183  m_list->startWorkers( aTable, aNickname, this, aNThreads );
184 }
185 
186 
188 {
189  if( m_list )
190  {
191  bool rv = m_list->joinWorkers();
192  m_list = nullptr;
193  return rv;
194  }
195  else
196  return true;
197 }
198 
199 
201 {
202  if( m_list )
203  {
204  m_list->stopWorkers();
205  m_list = nullptr;
206  }
207 }
BITMAP2CMP_SETTINGS kiface
const UTF8 & GetLibItemName() const
Definition: lib_id.h:106
static FOOTPRINT_LIST * get_instance_from_id(KIWAY &aKiway, int aId)
int StrNumCmp(const wxString &aString1, const wxString &aString2, bool aIgnoreCase)
Compare two strings with alphanumerical content.
Definition: string.cpp:420
bool InLibrary(const wxString &aLibrary) const
Test if the FOOTPRINT_INFO object was loaded from aLibrary.
virtual PROJECT & Prj() const
Return the PROJECT associated with this KIWAY.
Definition: kiway.cpp:174
void DisplayErrors(wxTopLevelWindow *aCaller=NULL)
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.
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
#define NULL
void Start(FP_LIB_TABLE *aTable, wxString const *aNickname=nullptr, unsigned aNThreads=DEFAULT_THREADS)
Launch the worker threads.
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
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
Definition: lib_id.h:92
pcbnew DSO
Definition: kiway.h:269
virtual void startWorkers(FP_LIB_TABLE *aTable, wxString const *aNickname, FOOTPRINT_ASYNC_LOADER *aLoader, unsigned aNThreads)=0
Launch worker threads to load footprints.
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:435
HTML_MESSAGE_BOX.
virtual KIFACE * KiFACE(FACE_T aFaceId, bool doLoad=true)
Return the KIFACE* given a FACE_T.
Definition: kiway.cpp:180
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 indention 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:122
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)
#define _(s)
Definition: 3d_actions.cpp:33
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:147
FOOTPRINT_LIST * m_list
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:411
Return a pointer to the global instance of FOOTPRINT_LIST from pcbnew.
Definition: kiface_ids.h:39
void Abort()
Safely stop the current process.