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, <[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 <kiface_base.h>
41
42FOOTPRINT_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
58FOOTPRINT_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
72bool FOOTPRINT_INFO::InLibrary( const wxString& aLibrary ) const
73{
74 return aLibrary == m_nickname;
75}
76
77
78bool 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
93void 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( wxS( "\n" ), wxS( "<BR>" ) );
111 msg += wxT( "<p>" ) + tmp + wxT( "</p>" );
112 }
113
114 dlg.AddHTML_Text( msg );
115
116 dlg.ShowModal();
117}
118
119
120static 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{
146
147 if( !footprintInfo )
148 return nullptr;
149
150 if( !footprintInfo->GetCount() )
151 footprintInfo->ReadCacheFromFile( aKiway.Prj().GetProjectPath() + wxS( "fp-info-cache" ) );
152
153 return footprintInfo;
154}
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 m_fpname
Module name.
wxString m_nickname
library as known in FP_LIB_TABLE
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:274
virtual KIFACE * KiFACE(FACE_T aFaceId, bool doLoad=true)
Return the KIFACE* given a FACE_T.
Definition: kiway.cpp:198
@ FACE_PCB
pcbnew DSO
Definition: kiway.h:282
virtual PROJECT & Prj() const
Return the PROJECT associated with this KIWAY.
Definition: kiway.cpp:192
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:50
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:126
return & kiface
Definition: cvpcb.cpp:112
#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
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 StrNumCmp(const wxString &aString1, const wxString &aString2, bool aIgnoreCase)
Compare two strings with alphanumerical content.
Implement a participant in the KIWAY alchemy.
Definition: kiway.h:151