From 0487d0b38d9781db90ad86022e273bc7b1d7630c Mon Sep 17 00:00:00 2001
From: Oliver <oliver.henry.walters@gmail.com>
Date: Thu, 2 Feb 2017 01:20:40 +1100
Subject: [PATCH] Allow footprint filtering based on FootprintLibName:

- If the filter has : (colon) character, then LibName:FootprintName pattern is matched
- If no : character, only FootprintName is used (current behaviour)
---
 cvpcb/class_footprints_listbox.cpp | 33 ++++++++++++++++++++++-----------
 pcbnew/pcb_netlist.cpp             | 21 ++++++++++++++++++---
 pcbnew/pcb_netlist.h               |  2 +-
 3 files changed, 41 insertions(+), 15 deletions(-)

diff --git a/cvpcb/class_footprints_listbox.cpp b/cvpcb/class_footprints_listbox.cpp
index 74965c3..88489e8 100644
--- a/cvpcb/class_footprints_listbox.cpp
+++ b/cvpcb/class_footprints_listbox.cpp
@@ -150,29 +150,40 @@ void FOOTPRINTS_LISTBOX::SetFootprints( FOOTPRINT_LIST& aList, const wxString& a
             continue;
         }
 
+        // Filter footprints by selected library
         if( (aFilterType & FILTERING_BY_LIBRARY) && !aLibName.IsEmpty()
             && !aList.GetItem( ii ).InLibrary( aLibName ) )
             continue;
 
+        // Filter footprints by symbol fp-filters
         if( (aFilterType & FILTERING_BY_COMPONENT_KEYWORD) && aComponent
-            && !aComponent->MatchesFootprintFilters( aList.GetItem( ii ).GetFootprintName() ) )
+            && !aComponent->MatchesFootprintFilters( aList.GetItem( ii ).GetNickname(), aList.GetItem( ii ).GetFootprintName() ) )
             continue;
 
+        // Filter footprints by symbol pin-count
         if( (aFilterType & FILTERING_BY_PIN_COUNT) && aComponent
             && aComponent->GetNetCount() != aList.GetItem( ii ).GetUniquePadCount() )
             continue;
 
-        // We can search (Using case insensitive search) in full LIB_ID or only
-        // in the fp name itself.
-        // After tests, only in the fp name itself looks better.
-        // However, the code to take in account the nickname is just commented, no removed.
-        wxString currname = //aList.GetItem( ii ).GetNickname().Lower() + ":" +
-                            aList.GetItem( ii ).GetFootprintName().Lower();
-
-        if( (aFilterType & FILTERING_BY_NAME) && !aFootPrintFilterPattern.IsEmpty()
-            && patternFilter.Find( currname ) == EDA_PATTERN_NOT_FOUND )
+        // Filter footprints by text-input
+        if( (aFilterType & FILTERING_BY_NAME ) && !aFootPrintFilterPattern.IsEmpty() )
         {
-            continue;
+            wxString currname = "";
+
+            // If the search string contains a ':' character,
+            // include the library name in the search string
+            // e.g. LibName:FootprintName
+            if( aFootPrintFilterPattern.Contains( ":" ) )
+            {
+                currname = aList.GetItem( ii ).GetNickname().Lower() + ":";
+            }
+
+            currname += aList.GetItem( ii ).GetFootprintName().Lower();
+
+            if( patternFilter.Find( currname ) == EDA_PATTERN_NOT_FOUND )
+            {
+                continue;
+            }
         }
 
         msg.Printf( wxT( "%3d %s:%s" ), int( newList.GetCount() + 1 ),
diff --git a/pcbnew/pcb_netlist.cpp b/pcbnew/pcb_netlist.cpp
index 90a45a2..2184658 100644
--- a/pcbnew/pcb_netlist.cpp
+++ b/pcbnew/pcb_netlist.cpp
@@ -33,6 +33,7 @@
 
 #include <pcb_netlist.h>
 #include <class_module.h>
+#include <eda_pattern_match.h>
 
 
 int COMPONENT_NET::Format( OUTPUTFORMATTER* aOut, int aNestLevel, int aCtl )
@@ -72,18 +73,32 @@ const COMPONENT_NET& COMPONENT::GetNet( const wxString& aPinName )
 }
 
 
-bool COMPONENT::MatchesFootprintFilters( const wxString& aFootprintName ) const
+bool COMPONENT::MatchesFootprintFilters( const wxString& aLibraryName, const wxString& aFootprintName ) const
 {
     if( m_footprintFilters.GetCount() == 0 )
         return true;
 
     // The matching is case insensitive
-    wxString name = aFootprintName.Upper();
+    wxString name = "";
+
+    EDA_PATTERN_MATCH_WILDCARD patternFilter;
 
     for( unsigned ii = 0; ii < m_footprintFilters.GetCount(); ii++ )
     {
-        if( name.Matches( m_footprintFilters[ii].Upper() ) )
+        // If the filter contains a ':' character, include the library name in the pattern
+        if( m_footprintFilters[ii].Contains( ":" ) )
+        {
+            name = aLibraryName.Lower() + ":";
+        }
+
+        name += aFootprintName.Lower();
+
+        patternFilter.SetPattern( m_footprintFilters[ii].Lower() );
+
+        if( patternFilter.Find( name ) != EDA_PATTERN_NOT_FOUND )
+        {
             return true;
+        }
     }
 
     return false;
diff --git a/pcbnew/pcb_netlist.h b/pcbnew/pcb_netlist.h
index 9b694ca..a4d3e5c 100644
--- a/pcbnew/pcb_netlist.h
+++ b/pcbnew/pcb_netlist.h
@@ -179,7 +179,7 @@ public:
      * @return true if \a aFootprintName matches any of the footprint filters or no footprint
      *         filters are defined.
      */
-    bool MatchesFootprintFilters( const wxString& aFootprintName ) const;
+    bool MatchesFootprintFilters( const wxString& aLibraryName, const wxString& aFootprintName ) const;
 
     MODULE* GetModule( bool aRelease = false )
     {
-- 
1.9.5.msysgit.0

