KiCad PCB EDA Suite
Loading...
Searching...
No Matches
footprint_filter.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) 2016 Jean-Pierre Charras, jp.charras at wanadoo.fr
5
* Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6
*
7
* This program is free software: you can redistribute it and/or modify it
8
* under the terms of the GNU General Public License as published by the
9
* Free Software Foundation, either version 3 of the License, or (at your
10
* option) any later version.
11
*
12
* This program is distributed in the hope that it will be useful, but
13
* WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
* General Public License for more details.
16
*
17
* You should have received a copy of the GNU General Public License
18
* along with this program. If not, see <https://www.gnu.org/licenses/>.
19
*/
20
21
#include <
footprint_filter.h
>
22
#include <stdexcept>
23
#include <wx/tokenzr.h>
24
25
using
FOOTPRINT_FILTER_IT
=
FOOTPRINT_FILTER::ITERATOR
;
26
27
28
FOOTPRINT_FILTER::ITERATOR::ITERATOR
() :
29
m_pos
( 0 ),
30
m_filter
( nullptr )
31
{
32
}
33
34
35
FOOTPRINT_FILTER::ITERATOR::ITERATOR
(
FOOTPRINT_FILTER_IT
const
& aOther ) :
36
m_pos
( aOther.
m_pos
),
37
m_filter
( aOther.
m_filter
)
38
{
39
}
40
41
42
FOOTPRINT_FILTER::ITERATOR::ITERATOR
(
FOOTPRINT_FILTER
& aFilter ) :
43
m_pos
( (size_t) -1 ),
44
m_filter
( &aFilter )
45
{
46
increment
();
47
}
48
49
50
void
FOOTPRINT_FILTER_IT::increment
()
51
{
52
if
( !
m_filter
|| !
m_filter
->m_list ||
m_filter
->m_list->GetCount() == 0 )
53
{
54
m_pos
= 0;
55
return
;
56
}
57
58
int
filter_type =
m_filter
->m_filter_type;
59
FOOTPRINT_LIST
* list =
m_filter
->m_list;
60
wxString& lib_name =
m_filter
->m_lib_name;
61
62
for
( ++
m_pos
;
m_pos
< list->GetCount(); ++
m_pos
)
63
{
64
FOOTPRINT_INFO
& candidate = list->GetItem(
m_pos
);
65
66
if
( filter_type ==
FOOTPRINT_FILTER::UNFILTERED_FP_LIST
)
67
break
;
68
69
if
( filter_type &
FOOTPRINT_FILTER::FILTERING_BY_PIN_COUNT
)
70
{
71
if
( !
PinCountMatch
( candidate ) )
72
continue
;
73
}
74
75
if
( filter_type &
FOOTPRINT_FILTER::FILTERING_BY_LIBRARY
)
76
{
77
if
( !lib_name.IsEmpty() && !candidate.
InLibrary
( lib_name ) )
78
continue
;
79
}
80
81
if
( filter_type &
FOOTPRINT_FILTER::FILTERING_BY_COMPONENT_FP_FILTER
)
82
{
83
if
( !
FootprintFilterMatch
( candidate ) )
84
continue
;
85
}
86
87
if
( ( filter_type &
FOOTPRINT_FILTER::FILTERING_BY_TEXT_PATTERN
) )
88
{
89
bool
exclude =
false
;
90
91
for
( std::unique_ptr<EDA_COMBINED_MATCHER>& matcher :
m_filter
->m_pattern_filters )
92
{
93
if
( !matcher->ScoreTerms( candidate.
GetSearchTerms
() ) )
94
{
95
exclude =
true
;
96
break
;
97
}
98
}
99
100
if
( exclude )
101
continue
;
102
}
103
104
// Candidate passed all filters; exit loop
105
break
;
106
}
107
}
108
109
110
bool
FOOTPRINT_FILTER_IT::equal
(
FOOTPRINT_FILTER_IT
const
& aOther )
const
111
{
112
// Invalid iterators are always equal
113
return
(
m_pos
== aOther.
m_pos
) && (
m_filter
== aOther.
m_filter
||
m_pos
== (size_t) -1 );
114
}
115
116
117
FOOTPRINT_INFO
&
FOOTPRINT_FILTER_IT::dereference
()
const
118
{
119
if
(
m_filter
&&
m_filter
->m_list && m_pos < m_filter->
m_list
->GetCount() )
120
return
m_filter
->m_list->GetItem(
m_pos
);
121
else
122
throw
std::out_of_range(
"Attempt to dereference past FOOTPRINT_FILTER::end()"
);
123
}
124
125
126
bool
FootprintFilterMatchesName
(
const
EDA_PATTERN_MATCH
& aFilter,
const
wxString& aLibNickname,
127
const
wxString& aFootprintName )
128
{
129
wxString candidate;
130
131
// If the filter contains a ':' character, include the library nickname in the match
132
// string.
133
if
( aFilter.
GetPattern
().Contains( wxS(
":"
) ) )
134
candidate = aLibNickname.Lower() + wxS(
":"
);
135
136
candidate += aFootprintName.Lower();
137
138
return
aFilter.
Find
( candidate ).
valid
();
139
}
140
141
142
bool
FOOTPRINT_FILTER_IT::FootprintFilterMatch
(
FOOTPRINT_INFO
& aItem )
143
{
144
if
(
m_filter
->m_footprint_filters.empty() )
145
return
true
;
146
147
for
(
const
std::unique_ptr<EDA_PATTERN_MATCH>& each_filter :
m_filter
->m_footprint_filters )
148
{
149
if
(
FootprintFilterMatchesName
( *each_filter, aItem.
GetLibNickname
(), aItem.
GetFootprintName
() ) )
150
return
true
;
151
}
152
153
return
false
;
154
}
155
156
157
bool
FOOTPRINT_FILTER_IT::PinCountMatch
(
FOOTPRINT_INFO
& aItem )
158
{
159
return
m_filter
->m_pin_count >= 0 && (unsigned)
m_filter
->m_pin_count == aItem.
GetNumberedPadCount
();
160
}
161
162
163
FOOTPRINT_FILTER::FOOTPRINT_FILTER
(
FOOTPRINT_LIST
& aList ) :
164
FOOTPRINT_FILTER
()
165
{
166
SetList
( aList );
167
}
168
169
170
FOOTPRINT_FILTER::FOOTPRINT_FILTER
() :
171
m_list
( nullptr ),
172
m_pin_count
( -1 ),
173
m_filter_type
(
UNFILTERED_FP_LIST
)
174
{
175
}
176
177
178
void
FOOTPRINT_FILTER::SetList
(
FOOTPRINT_LIST
& aList )
179
{
180
m_list
= &aList;
181
}
182
183
184
void
FOOTPRINT_FILTER::ClearFilters
()
185
{
186
m_filter_type
=
UNFILTERED_FP_LIST
;
187
}
188
189
190
void
FOOTPRINT_FILTER::FilterByLibrary
(
const
wxString& aLibName )
191
{
192
m_lib_name
= aLibName;
193
m_filter_type
|=
FILTERING_BY_LIBRARY
;
194
}
195
196
197
void
FOOTPRINT_FILTER::FilterByPinCount
(
int
aPinCount )
198
{
199
m_pin_count
= aPinCount;
200
m_filter_type
|=
FILTERING_BY_PIN_COUNT
;
201
}
202
203
204
void
FOOTPRINT_FILTER::FilterByFootprintFilters
(
const
wxArrayString& aFilters )
205
{
206
m_footprint_filters
.clear();
207
208
for
(
const
wxString& each_pattern : aFilters )
209
{
210
m_footprint_filters
.push_back( std::make_unique<EDA_PATTERN_MATCH_WILDCARD_ANCHORED>() );
211
m_footprint_filters
.back()->SetPattern( each_pattern.Lower() );
212
}
213
214
m_filter_type
|=
FILTERING_BY_COMPONENT_FP_FILTER
;
215
}
216
217
218
void
FOOTPRINT_FILTER::FilterByTextPattern
( wxString
const
& aPattern )
219
{
220
m_filter_pattern
= aPattern;
221
222
wxStringTokenizer tokenizer( aPattern.Lower(),
" \t\r\n"
, wxTOKEN_STRTOK );
223
224
while
( tokenizer.HasMoreTokens() )
225
{
226
const
wxString term = tokenizer.GetNextToken().Lower();
227
m_pattern_filters
.push_back( std::make_unique<EDA_COMBINED_MATCHER>( term,
CTX_LIBITEM
) );
228
}
229
230
m_filter_type
|=
FILTERING_BY_TEXT_PATTERN
;
231
}
232
233
234
FOOTPRINT_FILTER_IT
FOOTPRINT_FILTER::begin
()
235
{
236
return
FOOTPRINT_FILTER_IT
( *
this
);
237
}
238
239
240
FOOTPRINT_FILTER_IT
FOOTPRINT_FILTER::end
()
241
{
242
FOOTPRINT_FILTER_IT
end_it( *
this
);
243
end_it.
m_pos
=
m_list
?
m_list
->GetCount() : 0;
244
return
end_it;
245
}
EDA_PATTERN_MATCH
Interface for a pattern matcher for which there are several implementations.
Definition
eda_pattern_match.h:67
EDA_PATTERN_MATCH::GetPattern
virtual wxString const & GetPattern() const =0
Return the pattern passed to SetPattern().
EDA_PATTERN_MATCH::Find
virtual FIND_RESULT Find(const wxString &aCandidate) const =0
Return the location and possibly length of a match if a given candidate string matches the set patter...
FOOTPRINT_FILTER::ITERATOR
Inner iterator class returned by begin() and end().
Definition
footprint_filter.h:101
FOOTPRINT_FILTER::ITERATOR::PinCountMatch
bool PinCountMatch(FOOTPRINT_INFO &aItem)
Check if the stored component matches an item by pin count.
Definition
footprint_filter.cpp:157
FOOTPRINT_FILTER::ITERATOR::dereference
FOOTPRINT_INFO & dereference() const
Definition
footprint_filter.cpp:117
FOOTPRINT_FILTER::ITERATOR::equal
bool equal(const ITERATOR &aOther) const
Definition
footprint_filter.cpp:110
FOOTPRINT_FILTER::ITERATOR::m_filter
FOOTPRINT_FILTER * m_filter
Definition
footprint_filter.h:116
FOOTPRINT_FILTER::ITERATOR::m_pos
size_t m_pos
Definition
footprint_filter.h:115
FOOTPRINT_FILTER::ITERATOR::FootprintFilterMatch
bool FootprintFilterMatch(FOOTPRINT_INFO &aItem)
Check if the stored component matches an item by footprint filter.
Definition
footprint_filter.cpp:142
FOOTPRINT_FILTER::ITERATOR::FOOTPRINT_FILTER
friend class FOOTPRINT_FILTER
Definition
footprint_filter.h:109
FOOTPRINT_FILTER::ITERATOR::ITERATOR
ITERATOR()
Definition
footprint_filter.cpp:28
FOOTPRINT_FILTER::ITERATOR::increment
void increment()
Definition
footprint_filter.cpp:50
FOOTPRINT_FILTER::FOOTPRINT_FILTER
FOOTPRINT_FILTER(FOOTPRINT_LIST &aList)
Construct a filter.
Definition
footprint_filter.cpp:163
FOOTPRINT_FILTER::FOOTPRINT_FILTER
FOOTPRINT_FILTER()
Construct a filter without assigning a footprint list.
Definition
footprint_filter.cpp:170
FOOTPRINT_FILTER::m_pin_count
int m_pin_count
Definition
footprint_filter.h:158
FOOTPRINT_FILTER::m_list
FOOTPRINT_LIST * m_list
Definition
footprint_filter.h:154
FOOTPRINT_FILTER::SetList
void SetList(FOOTPRINT_LIST &aList)
Set the list to filter.
Definition
footprint_filter.cpp:178
FOOTPRINT_FILTER::FilterByPinCount
void FilterByPinCount(int aPinCount)
Set a pin count to filter by.
Definition
footprint_filter.cpp:197
FOOTPRINT_FILTER::begin
ITERATOR begin()
Get an iterator to the beginning of the filtered view.
Definition
footprint_filter.cpp:234
FOOTPRINT_FILTER::m_filter_pattern
wxString m_filter_pattern
Definition
footprint_filter.h:157
FOOTPRINT_FILTER::m_filter_type
int m_filter_type
Definition
footprint_filter.h:159
FOOTPRINT_FILTER::m_lib_name
wxString m_lib_name
Definition
footprint_filter.h:156
FOOTPRINT_FILTER::FilterByTextPattern
void FilterByTextPattern(const wxString &aPattern)
Add a pattern to filter by name, including wildcards and optionally a colon-delimited library name.
Definition
footprint_filter.cpp:218
FOOTPRINT_FILTER::ClearFilters
void ClearFilters()
Clear all filter criteria.
Definition
footprint_filter.cpp:184
FOOTPRINT_FILTER::FilterByLibrary
void FilterByLibrary(const wxString &aLibName)
Add library name to filter criteria.
Definition
footprint_filter.cpp:190
FOOTPRINT_FILTER::end
ITERATOR end()
Get an iterator to the end of the filtered view.
Definition
footprint_filter.cpp:240
FOOTPRINT_FILTER::m_pattern_filters
std::vector< std::unique_ptr< EDA_COMBINED_MATCHER > > m_pattern_filters
Definition
footprint_filter.h:161
FOOTPRINT_FILTER::m_footprint_filters
std::vector< std::unique_ptr< EDA_PATTERN_MATCH > > m_footprint_filters
Definition
footprint_filter.h:162
FOOTPRINT_FILTER::UNFILTERED_FP_LIST
@ UNFILTERED_FP_LIST
Definition
footprint_filter.h:147
FOOTPRINT_FILTER::FILTERING_BY_LIBRARY
@ FILTERING_BY_LIBRARY
Definition
footprint_filter.h:150
FOOTPRINT_FILTER::FILTERING_BY_PIN_COUNT
@ FILTERING_BY_PIN_COUNT
Definition
footprint_filter.h:149
FOOTPRINT_FILTER::FILTERING_BY_TEXT_PATTERN
@ FILTERING_BY_TEXT_PATTERN
Definition
footprint_filter.h:151
FOOTPRINT_FILTER::FILTERING_BY_COMPONENT_FP_FILTER
@ FILTERING_BY_COMPONENT_FP_FILTER
Definition
footprint_filter.h:148
FOOTPRINT_FILTER::FilterByFootprintFilters
void FilterByFootprintFilters(const wxArrayString &aFilters)
Set a list of footprint filters to filter by.
Definition
footprint_filter.cpp:204
FOOTPRINT_INFO
Helper class to handle the list of footprints available in libraries.
Definition
footprint_info.h:50
FOOTPRINT_INFO::InLibrary
bool InLibrary(const wxString &aLibrary) const
Test if the FOOTPRINT_INFO object was loaded from aLibrary.
Definition
footprint_info.cpp:95
FOOTPRINT_INFO::GetLibNickname
wxString GetLibNickname() const override
Definition
footprint_info.h:59
FOOTPRINT_INFO::GetFootprintName
const wxString & GetFootprintName() const
Definition
footprint_info.h:57
FOOTPRINT_INFO::GetNumberedPadCount
unsigned GetNumberedPadCount() const
Definition
footprint_info.h:88
FOOTPRINT_INFO::GetSearchTerms
std::vector< SEARCH_TERM > & GetSearchTerms() override
Returns the cache itself, not a copy.
Definition
footprint_info.cpp:85
FOOTPRINT_LIST
A list of FOOTPRINT_INFO objects, along with a list of IO_ERROR objects or PARSE_ERRORs that were thr...
Definition
footprint_info.h:162
CTX_LIBITEM
@ CTX_LIBITEM
Definition
eda_pattern_match.h:205
FOOTPRINT_FILTER_IT
FOOTPRINT_FILTER::ITERATOR FOOTPRINT_FILTER_IT
Definition
footprint_filter.cpp:25
footprint_filter.h
FootprintFilterMatchesName
bool FootprintFilterMatchesName(const EDA_PATTERN_MATCH &aFilter, const wxString &aLibNickname, const wxString &aFootprintName)
Whether a given footprint matches a specified filter.
Definition
footprint_filter.cpp:126
EDA_PATTERN_MATCH::FIND_RESULT::valid
bool valid() const
Definition
eda_pattern_match.h:74
src
common
footprint_filter.cpp
Generated on Tue Oct 27 2026 00:06:18 for KiCad PCB EDA Suite by
1.13.2