KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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, <[email protected]>
5 * Copyright (C) 2013-2016 SoftPLC Corporation, Dick Hollenbeck <[email protected]>
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 <wx/tokenzr.h>
41#include <kiface_base.h>
42
43FOOTPRINT_INFO* FOOTPRINT_LIST::GetFootprintInfo( const wxString& aLibNickname,
44 const wxString& aFootprintName )
45{
46 if( aFootprintName.IsEmpty() )
47 return nullptr;
48
49 for( std::unique_ptr<FOOTPRINT_INFO>& fp : m_list )
50 {
51 if( aLibNickname == fp->GetLibNickname() && aFootprintName == fp->GetFootprintName() )
52 return fp.get();
53 }
54
55 return nullptr;
56}
57
58
59FOOTPRINT_INFO* FOOTPRINT_LIST::GetFootprintInfo( const wxString& aFootprintName )
60{
61 if( aFootprintName.IsEmpty() )
62 return nullptr;
63
64 LIB_ID fpid;
65
66 wxCHECK_MSG( fpid.Parse( aFootprintName ) < 0, nullptr,
67 wxString::Format( wxT( "'%s' is not a valid LIB_ID." ), aFootprintName ) );
68
69 return GetFootprintInfo( fpid.GetLibNickname(), fpid.GetLibItemName() );
70}
71
72
73std::vector<SEARCH_TERM> FOOTPRINT_INFO::GetSearchTerms()
74{
75 std::vector<SEARCH_TERM> terms;
76
77 terms.emplace_back( SEARCH_TERM( GetName(), 8 ) );
78
79 wxStringTokenizer keywordTokenizer( GetKeywords(), wxS( " " ), wxTOKEN_STRTOK );
80
81 while( keywordTokenizer.HasMoreTokens() )
82 terms.emplace_back( SEARCH_TERM( keywordTokenizer.GetNextToken(), 4 ) );
83
84 // Also include keywords as one long string, just in case
85 terms.emplace_back( SEARCH_TERM( GetKeywords(), 1 ) );
86 terms.emplace_back( SEARCH_TERM( GetDesc(), 1 ) );
87
88 return terms;
89}
90
91
92bool FOOTPRINT_INFO::InLibrary( const wxString& aLibrary ) const
93{
94 return aLibrary == m_nickname;
95}
96
97
98bool operator<( const FOOTPRINT_INFO& lhs, const FOOTPRINT_INFO& rhs )
99{
100 int retv = StrNumCmp( lhs.m_nickname, rhs.m_nickname, false );
101
102 if( retv != 0 )
103 return retv < 0;
104
105 // Technically footprint names are not case sensitive because the file name is used
106 // as the footprint name. On windows this would be problematic because windows does
107 // not support case sensitive file names by default. This should not cause any issues
108 // and allow for a future change to use the name defined in the footprint file.
109 return StrNumCmp( lhs.m_fpname, rhs.m_fpname, false ) < 0;
110}
111
112
113void FOOTPRINT_LIST::DisplayErrors( wxTopLevelWindow* aWindow )
114{
115 // @todo: go to a more HTML !<table>! ? centric output, possibly with recommendations
116 // for remedy of errors. Add numeric error codes to PARSE_ERROR, and switch on them for
117 // remedies, etc. Full access is provided to everything in every exception!
118
119 HTML_MESSAGE_BOX dlg( aWindow, _( "Load Error" ) );
120
121 dlg.MessageSet( _( "Errors were encountered loading footprints:" ) );
122
123 wxString msg;
124
125 while( std::unique_ptr<IO_ERROR> error = PopError() )
126 {
127 wxString tmp = EscapeHTML( error->Problem() );
128
129 // Preserve new lines in error messages so queued errors don't run together.
130 tmp.Replace( wxS( "\n" ), wxS( "<BR>" ) );
131 msg += wxT( "<p>" ) + tmp + wxT( "</p>" );
132 }
133
134 dlg.AddHTML_Text( msg );
135
136 dlg.ShowModal();
137}
138
139
140static FOOTPRINT_LIST* get_instance_from_id( KIWAY& aKiway, int aId )
141{
142 void* ptr = nullptr;
143
144 try
145 {
146 ptr = Kiface().IfaceOrAddress( aId );
147
148 if( !ptr )
149 {
150 KIFACE* kiface = aKiway.KiFACE( KIWAY::FACE_PCB );
151 ptr = kiface->IfaceOrAddress( aId );
152 }
153
154 return static_cast<FOOTPRINT_LIST*>( ptr );
155 }
156 catch( ... )
157 {
158 return nullptr;
159 }
160}
161
162
164{
166
167 if( !footprintInfo )
168 return nullptr;
169
170 if( !footprintInfo->GetCount() )
171 footprintInfo->ReadCacheFromFile( aKiway.Prj().GetProjectPath() + wxS( "fp-info-cache" ) );
172
173 return footprintInfo;
174}
KIFACE_BASE & Kiface()
Global KIFACE_BASE "get" accessor.
bool InLibrary(const wxString &aLibrary) const
Test if the FOOTPRINT_INFO object was loaded from aLibrary.
wxString GetName() const override
wxString m_fpname
Module name.
wxString GetKeywords()
std::vector< SEARCH_TERM > GetSearchTerms() override
wxString m_nickname
library as known in FP_LIB_TABLE
wxString GetDesc() override
Holds a list of FOOTPRINT_INFO objects, along with a list of IO_ERRORs or PARSE_ERRORs that were thro...
void DisplayErrors(wxTopLevelWindow *aCaller=nullptr)
virtual void ReadCacheFromFile(const wxString &aFilePath)
FOOTPRINT_INFO * GetFootprintInfo(const wxString &aFootprintName)
Get info for a footprint by id.
unsigned GetCount() const
std::unique_ptr< IO_ERROR > PopError()
static FOOTPRINT_LIST * GetInstance(KIWAY &aKiway)
Factory function to return a FOOTPRINT_LIST via Kiway.
void MessageSet(const wxString &message)
Add a message (in bold) to message list.
void AddHTML_Text(const wxString &message)
Add HTML text (without any change) to message list.
virtual void * IfaceOrAddress(int aDataId) override=0
Return pointer to the requested object.
A minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the same KiCad...
Definition: kiway.h:279
virtual KIFACE * KiFACE(FACE_T aFaceId, bool doLoad=true)
Return the KIFACE* given a FACE_T.
Definition: kiway.cpp:202
@ FACE_PCB
pcbnew DSO
Definition: kiway.h:287
virtual PROJECT & Prj() const
Return the PROJECT associated with this KIWAY.
Definition: kiway.cpp:196
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:49
int Parse(const UTF8 &aId, bool aFix=false)
Parse LIB_ID with the information from aId.
Definition: lib_id.cpp:51
const UTF8 & GetLibItemName() const
Definition: lib_id.h:102
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
Definition: lib_id.h:87
virtual const wxString GetProjectPath() const
Return the full path of the project.
Definition: project.cpp:135
#define _(s)
bool operator<(const FOOTPRINT_INFO &lhs, const FOOTPRINT_INFO &rhs)
static FOOTPRINT_LIST * get_instance_from_id(KIWAY &aKiway, int aId)
@ KIFACE_FOOTPRINT_LIST
Return a pointer to the global instance of FOOTPRINT_LIST from pcbnew.
Definition: kiface_ids.h:39
int StrNumCmp(const wxString &aString1, const wxString &aString2, bool aIgnoreCase)
Compare two strings with alphanumerical content.
wxString EscapeHTML(const wxString &aString)
Return a new wxString escaped for embedding in HTML.
void * IfaceOrAddress(int aDataId) override
Return a pointer to the requested object.
Implement a participant in the KIWAY alchemy.
Definition: kiway.h:151
IFACE KIFACE_BASE kiface("pcb_test_frame", KIWAY::FACE_PCB)