2017-03-23 00:59:25 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2016 Jean-Pierre Charras, jp.charras at wanadoo.fr
|
2022-10-16 16:45:10 +00:00
|
|
|
* Copyright (C) 1992-2022 KiCad Developers, see AUTHORS.txt for contributors.
|
2017-03-23 00:59:25 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation, either version 3 of the License, or (at your
|
|
|
|
* option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <footprint_filter.h>
|
|
|
|
#include <stdexcept>
|
2019-08-21 13:06:41 +00:00
|
|
|
#include <wx/tokenzr.h>
|
2017-03-23 00:59:25 +00:00
|
|
|
|
|
|
|
using FOOTPRINT_FILTER_IT = FOOTPRINT_FILTER::ITERATOR;
|
|
|
|
|
|
|
|
|
|
|
|
FOOTPRINT_FILTER::ITERATOR::ITERATOR() : m_pos( 0 ), m_filter( nullptr )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
FOOTPRINT_FILTER::ITERATOR::ITERATOR( FOOTPRINT_FILTER_IT const& aOther )
|
|
|
|
: m_pos( aOther.m_pos ), m_filter( aOther.m_filter )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
FOOTPRINT_FILTER::ITERATOR::ITERATOR( FOOTPRINT_FILTER& aFilter )
|
|
|
|
: m_pos( (size_t) -1 ), m_filter( &aFilter )
|
|
|
|
{
|
|
|
|
increment();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FOOTPRINT_FILTER_IT::increment()
|
|
|
|
{
|
|
|
|
if( !m_filter || !m_filter->m_list || m_filter->m_list->GetCount() == 0 )
|
|
|
|
{
|
|
|
|
m_pos = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-21 13:06:41 +00:00
|
|
|
int filter_type = m_filter->m_filter_type;
|
|
|
|
FOOTPRINT_LIST* list = m_filter->m_list;
|
|
|
|
wxString& lib_name = m_filter->m_lib_name;
|
2017-03-23 00:59:25 +00:00
|
|
|
|
2019-08-21 13:06:41 +00:00
|
|
|
for( ++m_pos; m_pos < list->GetCount(); ++m_pos )
|
2017-03-23 00:59:25 +00:00
|
|
|
{
|
2019-08-21 13:06:41 +00:00
|
|
|
FOOTPRINT_INFO& candidate = list->GetItem( m_pos );
|
2017-03-23 00:59:25 +00:00
|
|
|
|
2019-08-21 13:06:41 +00:00
|
|
|
if( filter_type == FOOTPRINT_FILTER::UNFILTERED_FP_LIST )
|
|
|
|
break;
|
2017-03-23 00:59:25 +00:00
|
|
|
|
2021-05-05 22:23:34 +00:00
|
|
|
if( filter_type & FOOTPRINT_FILTER::FILTERING_BY_PIN_COUNT )
|
2017-03-23 00:59:25 +00:00
|
|
|
{
|
2021-05-05 22:23:34 +00:00
|
|
|
if( !PinCountMatch( candidate ) )
|
2019-08-21 13:06:41 +00:00
|
|
|
continue;
|
|
|
|
}
|
2017-03-23 00:59:25 +00:00
|
|
|
|
2021-05-05 22:23:34 +00:00
|
|
|
if( filter_type & FOOTPRINT_FILTER::FILTERING_BY_LIBRARY )
|
2019-08-21 13:06:41 +00:00
|
|
|
{
|
2021-05-05 22:23:34 +00:00
|
|
|
if( !lib_name.IsEmpty() && !candidate.InLibrary( lib_name ) )
|
2019-08-21 13:06:41 +00:00
|
|
|
continue;
|
|
|
|
}
|
2017-03-23 00:59:25 +00:00
|
|
|
|
2021-05-05 22:23:34 +00:00
|
|
|
if( filter_type & FOOTPRINT_FILTER::FILTERING_BY_COMPONENT_FP_FILTER )
|
2019-08-21 13:06:41 +00:00
|
|
|
{
|
2021-05-05 22:23:34 +00:00
|
|
|
if( !FootprintFilterMatch( candidate ) )
|
2019-08-21 13:06:41 +00:00
|
|
|
continue;
|
2017-03-23 00:59:25 +00:00
|
|
|
}
|
|
|
|
|
2019-09-03 18:28:54 +00:00
|
|
|
if( ( filter_type & FOOTPRINT_FILTER::FILTERING_BY_TEXT_PATTERN ) )
|
2017-03-23 00:59:25 +00:00
|
|
|
{
|
2019-08-21 13:06:41 +00:00
|
|
|
wxString searchStr = wxString::Format( wxT( "%s:%s %s" ),
|
|
|
|
candidate.GetLibNickname(),
|
|
|
|
candidate.GetFootprintName(),
|
|
|
|
candidate.GetSearchText() );
|
2023-04-14 22:34:14 +00:00
|
|
|
bool exclude = false;
|
2019-08-21 13:06:41 +00:00
|
|
|
|
2022-10-16 16:45:10 +00:00
|
|
|
for( std::unique_ptr<EDA_COMBINED_MATCHER>& matcher : m_filter->m_pattern_filters )
|
2019-08-21 13:06:41 +00:00
|
|
|
{
|
2023-04-14 22:34:14 +00:00
|
|
|
if( !matcher->Find( searchStr.Lower() ) )
|
2019-08-21 13:06:41 +00:00
|
|
|
{
|
|
|
|
exclude = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( exclude )
|
|
|
|
continue;
|
2017-03-23 00:59:25 +00:00
|
|
|
}
|
|
|
|
|
2019-08-21 13:06:41 +00:00
|
|
|
// Candidate passed all filters; exit loop
|
|
|
|
break;
|
|
|
|
}
|
2017-03-23 00:59:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool FOOTPRINT_FILTER_IT::equal( FOOTPRINT_FILTER_IT const& aOther ) const
|
|
|
|
{
|
|
|
|
// Invalid iterators are always equal
|
|
|
|
return ( m_pos == aOther.m_pos ) && ( m_filter == aOther.m_filter || m_pos == (size_t) -1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
FOOTPRINT_INFO& FOOTPRINT_FILTER_IT::dereference() const
|
|
|
|
{
|
|
|
|
if( m_filter && m_filter->m_list && m_pos < m_filter->m_list->GetCount() )
|
|
|
|
return m_filter->m_list->GetItem( m_pos );
|
|
|
|
else
|
|
|
|
throw std::out_of_range( "Attempt to dereference past FOOTPRINT_FILTER::end()" );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool FOOTPRINT_FILTER_IT::FootprintFilterMatch( FOOTPRINT_INFO& aItem )
|
|
|
|
{
|
|
|
|
if( m_filter->m_footprint_filters.empty() )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// The matching is case insensitive
|
2017-04-11 06:26:56 +00:00
|
|
|
wxString name;
|
2017-03-23 00:59:25 +00:00
|
|
|
|
2022-10-16 16:45:10 +00:00
|
|
|
for( const std::unique_ptr<EDA_PATTERN_MATCH>& each_filter : m_filter->m_footprint_filters )
|
2017-03-23 00:59:25 +00:00
|
|
|
{
|
2017-04-11 06:26:56 +00:00
|
|
|
name.Empty();
|
|
|
|
|
2017-03-23 00:59:25 +00:00
|
|
|
// If the filter contains a ':' character, include the library name in the pattern
|
2023-01-17 00:07:41 +00:00
|
|
|
if( each_filter->GetPattern().Contains( wxS( ":" ) ) )
|
2017-03-23 00:59:25 +00:00
|
|
|
{
|
2023-01-17 00:07:41 +00:00
|
|
|
name = aItem.GetLibNickname().Lower() + wxS( ":" );
|
2017-03-23 00:59:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
name += aItem.GetFootprintName().Lower();
|
|
|
|
|
2020-11-04 02:49:19 +00:00
|
|
|
if( each_filter->Find( name ) )
|
2017-03-23 00:59:25 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool FOOTPRINT_FILTER_IT::PinCountMatch( FOOTPRINT_INFO& aItem )
|
|
|
|
{
|
2017-07-16 04:30:42 +00:00
|
|
|
return m_filter->m_pin_count >= 0 &&
|
|
|
|
(unsigned) m_filter->m_pin_count == aItem.GetUniquePadCount();
|
2017-03-23 00:59:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
FOOTPRINT_FILTER::FOOTPRINT_FILTER( FOOTPRINT_LIST& aList ) : FOOTPRINT_FILTER()
|
|
|
|
{
|
|
|
|
SetList( aList );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
FOOTPRINT_FILTER::FOOTPRINT_FILTER()
|
|
|
|
: m_list( nullptr ), m_pin_count( -1 ), m_filter_type( UNFILTERED_FP_LIST )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FOOTPRINT_FILTER::SetList( FOOTPRINT_LIST& aList )
|
|
|
|
{
|
|
|
|
m_list = &aList;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FOOTPRINT_FILTER::ClearFilters()
|
|
|
|
{
|
|
|
|
m_filter_type = UNFILTERED_FP_LIST;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-21 13:06:41 +00:00
|
|
|
void FOOTPRINT_FILTER::FilterByLibrary( const wxString& aLibName )
|
2017-03-23 00:59:25 +00:00
|
|
|
{
|
|
|
|
m_lib_name = aLibName;
|
|
|
|
m_filter_type |= FILTERING_BY_LIBRARY;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FOOTPRINT_FILTER::FilterByPinCount( int aPinCount )
|
|
|
|
{
|
|
|
|
m_pin_count = aPinCount;
|
|
|
|
m_filter_type |= FILTERING_BY_PIN_COUNT;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-21 13:06:41 +00:00
|
|
|
void FOOTPRINT_FILTER::FilterByFootprintFilters( const wxArrayString& aFilters )
|
2017-03-23 00:59:25 +00:00
|
|
|
{
|
|
|
|
m_footprint_filters.clear();
|
|
|
|
|
2019-08-21 13:06:41 +00:00
|
|
|
for( const wxString& each_pattern : aFilters )
|
2017-03-23 00:59:25 +00:00
|
|
|
{
|
2018-04-19 20:42:25 +00:00
|
|
|
m_footprint_filters.push_back( std::make_unique<EDA_PATTERN_MATCH_WILDCARD_EXPLICIT>() );
|
2017-04-11 06:26:56 +00:00
|
|
|
m_footprint_filters.back()->SetPattern( each_pattern.Lower() );
|
2017-03-23 00:59:25 +00:00
|
|
|
}
|
|
|
|
|
2019-09-03 18:28:54 +00:00
|
|
|
m_filter_type |= FILTERING_BY_COMPONENT_FP_FILTER;
|
2017-03-23 00:59:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-09-03 18:28:54 +00:00
|
|
|
void FOOTPRINT_FILTER::FilterByTextPattern( wxString const& aPattern )
|
2017-03-23 00:59:25 +00:00
|
|
|
{
|
|
|
|
m_filter_pattern = aPattern;
|
2019-08-21 13:06:41 +00:00
|
|
|
|
|
|
|
wxStringTokenizer tokenizer( aPattern.Lower() );
|
|
|
|
|
|
|
|
while( tokenizer.HasMoreTokens() )
|
|
|
|
{
|
|
|
|
const wxString term = tokenizer.GetNextToken().Lower();
|
2022-08-12 23:51:19 +00:00
|
|
|
m_pattern_filters.push_back( std::make_unique<EDA_COMBINED_MATCHER>( term, CTX_LIBITEM ) );
|
2019-08-21 13:06:41 +00:00
|
|
|
}
|
|
|
|
|
2019-09-03 18:28:54 +00:00
|
|
|
m_filter_type |= FILTERING_BY_TEXT_PATTERN;
|
2017-03-23 00:59:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
FOOTPRINT_FILTER_IT FOOTPRINT_FILTER::begin()
|
|
|
|
{
|
|
|
|
return FOOTPRINT_FILTER_IT( *this );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
FOOTPRINT_FILTER_IT FOOTPRINT_FILTER::end()
|
|
|
|
{
|
|
|
|
FOOTPRINT_FILTER_IT end_it( *this );
|
2018-04-12 09:07:43 +00:00
|
|
|
end_it.m_pos = m_list ? m_list->GetCount() : 0;
|
2017-03-23 00:59:25 +00:00
|
|
|
return end_it;
|
|
|
|
}
|