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-2017 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 <class_module.h>
32 #include <common.h>
33 #include <fctsys.h>
34 #include <footprint_info.h>
35 #include <fp_lib_table.h>
36 #include <html_messagebox.h>
37 #include <io_mgr.h>
38 #include <kiface_ids.h>
39 #include <kiway.h>
40 #include <lib_id.h>
41 #include <macros.h>
42 #include <pgm_base.h>
43 #include <thread>
44 #include <utility>
46 
47 
48 FOOTPRINT_INFO* FOOTPRINT_LIST::GetModuleInfo( const wxString& aLibNickname,
49  const wxString& aFootprintName )
50 {
51  if( aFootprintName.IsEmpty() )
52  return NULL;
53 
54  for( auto& fp : m_list )
55  {
56  if( aLibNickname == fp->GetLibNickname() && aFootprintName == fp->GetFootprintName() )
57  return &*fp;
58  }
59 
60  return NULL;
61 }
62 
63 
64 FOOTPRINT_INFO* FOOTPRINT_LIST::GetModuleInfo( const wxString& aFootprintName )
65 {
66  if( aFootprintName.IsEmpty() )
67  return NULL;
68 
69  LIB_ID fpid;
70 
71  wxCHECK_MSG( fpid.Parse( aFootprintName, LIB_ID::ID_PCB ) < 0, NULL,
72  wxString::Format( wxT( "\"%s\" is not a valid LIB_ID." ), aFootprintName ) );
73 
74  return GetModuleInfo( fpid.GetLibNickname(), fpid.GetLibItemName() );
75 }
76 
77 
78 bool FOOTPRINT_INFO::InLibrary( const wxString& aLibrary ) const
79 {
80  return aLibrary == m_nickname;
81 }
82 
83 
84 void FOOTPRINT_LIST::DisplayErrors( wxTopLevelWindow* aWindow )
85 {
86  // @todo: go to a more HTML !<table>! ? centric output, possibly with
87  // recommendations for remedy of errors. Add numeric error codes
88  // to PARSE_ERROR, and switch on them for remedies, etc. Full
89  // access is provided to everything in every exception!
90 
91  HTML_MESSAGE_BOX dlg( aWindow, _( "Load Error" ) );
92 
93  dlg.MessageSet( _( "Errors were encountered loading footprints:" ) );
94 
95  wxString msg;
96 
97  while( auto error = PopError() )
98  {
99  wxString tmp = error->Problem();
100 
101  // Preserve new lines in error messages so queued errors don't run together.
102  tmp.Replace( "\n", "<BR>" );
103  msg += wxT( "<p>" ) + tmp + wxT( "</p>" );
104  }
105 
106  dlg.AddHTML_Text( msg );
107 
108  dlg.ShowModal();
109 }
110 
111 
112 static FOOTPRINT_LIST* get_instance_from_id( KIWAY& aKiway, int aId )
113 {
114  void* ptr = nullptr;
115 
116  try
117  {
118  KIFACE* kiface = aKiway.KiFACE( KIWAY::FACE_PCB );
119 
120  if( !kiface )
121  return nullptr;
122 
123  ptr = kiface->IfaceOrAddress( aId );
124 
125  if( !ptr )
126  return nullptr;
127  }
128  catch( ... )
129  {
130  return nullptr;
131  }
132 
133  return static_cast<FOOTPRINT_LIST*>( ptr );
134 }
135 
136 
138 {
139  FOOTPRINT_LIST* footprintInfo = get_instance_from_id( aKiway, KIFACE_FOOTPRINT_LIST );
140 
141  if( ! footprintInfo )
142  return nullptr;
143 
144  if( !footprintInfo->GetCount() )
145  {
146  wxTextFile footprintInfoCache( aKiway.Prj().GetProjectPath() + "fp-info-cache" );
147  footprintInfo->ReadCacheFromFile( &footprintInfoCache );
148  }
149 
150  return footprintInfo;
151 }
152 
153 
155 {
156  m_total_libs = 0;
157 }
158 
159 
161 {
162  // This is NOP if the load has finished
163  Abort();
164 }
165 
166 
168 {
169  m_list = aList;
170 }
171 
172 
174  FP_LIB_TABLE* aTable, wxString const* aNickname, unsigned aNThreads )
175 {
176  // Capture the FP_LIB_TABLE into m_last_table. Formatting it as a string instead of storing the
177  // raw data avoids having to pull in the FP-specific parts.
178  STRING_FORMATTER sof;
179  aTable->Format( &sof, 0 );
180  m_last_table = sof.GetString();
181 
182  m_list->StartWorkers( aTable, aNickname, this, aNThreads );
183 }
184 
185 
187 {
188  if( m_list )
189  {
190  bool rv = m_list->JoinWorkers();
191  m_list = nullptr;
192  return rv;
193  }
194  else
195  return true;
196 }
197 
198 
200 {
201  if( m_list )
202  {
203  m_list->StopWorkers();
204  m_list = nullptr;
205  }
206 }
static FOOTPRINT_LIST * get_instance_from_id(KIWAY &aKiway, int aId)
FOOTPRINT_INFO * GetModuleInfo(const wxString &aFootprintId)
Get info for a module by id.
void DisplayErrors(wxTopLevelWindow *aCaller=NULL)
virtual bool JoinWorkers()=0
Join worker threads.
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
VTBL_ENTRY void * IfaceOrAddress(int aDataId)=0
Function IfaceOrAddress returns a pointer to the requested object.
FOOTPRINT_ASYNC_LOADER()
Construct an asynchronous loader.
This file contains miscellaneous commonly used macros and functions.
virtual void ReadCacheFromFile(wxTextFile *aFile)
unsigned GetCount() const
static FOOTPRINT_LIST * GetInstance(KIWAY &aKiway)
Factory function to return a FOOTPRINT_LIST via Kiway.
void Start(FP_LIB_TABLE *aTable, wxString const *aNickname=nullptr, unsigned aNThreads=DEFAULT_THREADS)
Launch the worker threads.
bool InLibrary(const wxString &aLibrary) const
Test if the FOOTPRINT_INFO object was loaded from aLibrary.
const UTF8 & GetLibItemName() const
Definition: lib_id.h:114
VTBL_ENTRY PROJECT & Prj() const
Function Prj returns the PROJECT associated with this KIWAY.
Definition: kiway.cpp:142
Subclass of DIALOG_DISPLAY_HTML_TEXT_BASE, which is generated by wxFormBuilder.
The common library.
Class KIWAY is a minimalistic software bus for communications between various DLLs/DSOs (DSOs) within...
Definition: kiway.h:258
pcbnew DSO
Definition: kiway.h:267
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:475
Class HTML_MESSAGE_BOX.
VTBL_ENTRY KIFACE * KiFACE(FACE_T aFaceId, bool doLoad=true)
Function KiFACE returns the KIFACE* given a FACE_T.
Definition: kiway.cpp:148
void MessageSet(const wxString &message)
Function MessageSet adds 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.
see class PGM_BASE
Holds a list of FOOTPRINT_INFO objects, along with a list of IO_ERRORs or PARSE_ERRORs that were thro...
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
void AddHTML_Text(const wxString &message)
Function AddHTML_Text adds html text (without any change) to message list.
Class KIFACE is used by a participant in the KIWAY alchemy.
Definition: kiway.h:151
VTBL_ENTRY const wxString GetProjectPath() const
Function GetProjectPath returns the full path of the project.
Definition: project.cpp:102
The common library.
return & kiface
Definition: pcbnew.cpp:219
Module description (excepted pads)
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
Definition: lib_id.h:97
int Parse(const UTF8 &aId, LIB_ID_TYPE aType, bool aFix=false)
Parse LIB_ID with the information from aId.
Definition: lib_id.cpp:122
void SetList(FOOTPRINT_LIST *aList)
Assign a FOOTPRINT_LIST to the loader.
Class STRING_FORMATTER implements OUTPUTFORMATTER to a memory buffer.
Definition: richio.h:445
Return a pointer to the global instance of FOOTPRINT_LIST from pcbnew.
Definition: kiface_ids.h:39
void Abort()
Safely stop the current process.
virtual void StartWorkers(FP_LIB_TABLE *aTable, wxString const *aNickname, FOOTPRINT_ASYNC_LOADER *aLoader, unsigned aNThreads)=0
Launch worker threads to load footprints.