KiCad PCB EDA Suite
autosel.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) 1992-2017 KiCad Developers, see AUTHORS.txt for contributors.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, you may find one here:
18  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19  * or you may search the http://www.gnu.org website for the version 2 license,
20  * or you may write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23 
28 // This file handle automatic selection of footprints, from .equ files which give
29 // a footprint LIB_ID associated to a component value.
30 // These associations have this form:
31 // 'FT232BL' 'QFP:LQFP-32_7x7mm_Pitch0.8mm'
32 
33 
34 #include <fctsys.h>
35 #include <common.h>
36 #include <kiface_i.h>
37 #include <project.h>
38 #include <gestfich.h>
39 #include <pgm_base.h>
40 #include <kicad_string.h>
41 #include <macros.h>
42 
43 #include <cvpcb.h>
44 #include <cvpcb_mainframe.h>
45 #include <listview_classes.h>
46 #include <autosel.h>
47 
48 #define QUOTE '\''
49 
50 
51 /*
52  * read the string between quotes and put it in aTarget
53  * put text in aTarget
54  * return a pointer to the last read char (the second quote if OK)
55  */
56 wxString GetQuotedText( wxString& text )
57 {
58  int i = text.Find( QUOTE );
59 
60  if( wxNOT_FOUND == i )
61  return wxT( "" );
62 
63  wxString shrt = text.Mid( i + 1 );
64  i = shrt.Find( QUOTE );
65 
66  if( wxNOT_FOUND == i )
67  return wxT( "" );
68 
69  text = shrt.Mid( i + 1 );
70  return shrt.Mid( 0, i );
71 }
72 
73 
74 // A sort compare function, used to sort a FOOTPRINT_EQUIVALENCE_LIST by cmp values
75 // (m_ComponentValue member)
77 {
78  return ref.m_ComponentValue.Cmp( test.m_ComponentValue ) >= 0;
79 }
80 
81 
82 // read the .equ files and populate the list of equivalents
83 int CVPCB_MAINFRAME::buildEquivalenceList( FOOTPRINT_EQUIVALENCE_LIST& aList, wxString * aErrorMessages )
84 {
85  char line[1024];
86  int error_count = 0;
87  FILE* file;
88  wxFileName fn;
89  wxString tmp, error_msg;
90 
91  SEARCH_STACK& search = Kiface().KifaceSearch();
92 
93  // Find equivalences in all available files, and populates the
94  // equiv_List with all equivalences found in .equ files
95  for( unsigned ii = 0; ii < m_EquFilesNames.GetCount(); ii++ )
96  {
97  fn = wxExpandEnvVars( m_EquFilesNames[ii] );
98 
99  tmp = search.FindValidPath( fn.GetFullPath() );
100 
101  if( !tmp )
102  {
103  error_count++;
104 
105  if( aErrorMessages )
106  {
107  error_msg.Printf( _( "Equivalence file '%s' could not be found in the "
108  "default search paths." ),
109  GetChars( fn.GetFullName() ) );
110 
111  if( ! aErrorMessages->IsEmpty() )
112  *aErrorMessages << wxT("\n\n");
113 
114  *aErrorMessages += error_msg;
115  }
116 
117  continue;
118  }
119 
120  file = wxFopen( tmp, wxT( "rt" ) );
121 
122  if( file == NULL )
123  {
124  error_count++;
125 
126  if( aErrorMessages )
127  {
128  error_msg.Printf( _( "Error opening equivalence file '%s'." ), GetChars( tmp ) );
129 
130  if( ! aErrorMessages->IsEmpty() )
131  *aErrorMessages << wxT("\n\n");
132 
133  *aErrorMessages += error_msg;
134  }
135 
136  continue;
137  }
138 
139  while( GetLine( file, line, NULL, sizeof( line ) ) != NULL )
140  {
141  if( *line == 0 )
142  continue;
143 
144  wxString wtext = FROM_UTF8( line );
145  wxString value = GetQuotedText( wtext );
146 
147  if( value.IsEmpty() )
148  continue;
149 
150  wxString footprint = GetQuotedText( wtext );
151 
152  if( footprint.IsEmpty() )
153  continue;
154 
155  value.Replace( wxT( " " ), wxT( "_" ) );
156 
157  FOOTPRINT_EQUIVALENCE* equivItem = new FOOTPRINT_EQUIVALENCE();
158  equivItem->m_ComponentValue = value;
159  equivItem->m_FootprintFPID = footprint;
160  aList.push_back( equivItem );
161  }
162 
163  fclose( file );
164  }
165 
166  return error_count;
167 }
168 
169 
170 void CVPCB_MAINFRAME::AutomaticFootprintMatching( wxCommandEvent& event )
171 {
172  FOOTPRINT_EQUIVALENCE_LIST equiv_List;
173  wxString msg, error_msg;
174 
175  if( m_netlist.IsEmpty() )
176  return;
177 
178  if( buildEquivalenceList( equiv_List, &error_msg ) )
179  wxMessageBox( error_msg, _( "Equivalence File Load Error" ), wxOK | wxICON_WARNING, this );
180 
181  // Sort the association list by component value.
182  // When sorted, find duplicate definitions (i.e. 2 or more items
183  // having the same component value) is more easy.
184  std::sort( equiv_List.begin(), equiv_List.end(), sortListbyCmpValue );
185 
186  // Display the number of footprint/component equivalences.
187  msg.Printf( _( "%lu footprint/cmp equivalences found." ), (unsigned long)equiv_List.size() );
188  SetStatusText( msg, 0 );
189 
190  // Now, associate each free component with a footprint, when the association
191  // is found in list
192  m_skipComponentSelect = true;
193  error_msg.Empty();
194 
195  for( unsigned kk = 0; kk < m_netlist.GetCount(); kk++ )
196  {
197  COMPONENT* component = m_netlist.GetComponent( kk );
198 
199  bool found = false;
200 
201  if( !component->GetFPID().empty() ) // the component has already a footprint
202  continue;
203 
204  // Here a first attempt is made. We can have multiple equivItem of the same value.
205  // When happens, using the footprint filter of components can remove the ambiguity by
206  // filtering equivItem so one can use multiple equiv_List (for polar and
207  // non-polar caps for example)
208  wxString fpid_candidate;
209 
210  for( unsigned idx = 0; idx < equiv_List.size(); idx++ )
211  {
212  FOOTPRINT_EQUIVALENCE& equivItem = equiv_List[idx];
213 
214  if( equivItem.m_ComponentValue.CmpNoCase( component->GetValue() ) != 0 )
215  continue;
216 
217  const FOOTPRINT_INFO *module = m_FootprintsList->GetModuleInfo( equivItem.m_FootprintFPID );
218 
219  bool equ_is_unique = true;
220  unsigned next = idx+1;
221  int previous = idx-1;
222 
223  if( next < equiv_List.size() &&
224  equivItem.m_ComponentValue == equiv_List[next].m_ComponentValue )
225  equ_is_unique = false;
226 
227  if( previous >= 0 &&
228  equivItem.m_ComponentValue == equiv_List[previous].m_ComponentValue )
229  equ_is_unique = false;
230 
231  // If the equivalence is unique, no ambiguity: use the association
232  if( module && equ_is_unique )
233  {
234  SetNewPkg( equivItem.m_FootprintFPID, kk );
235  found = true;
236  break;
237  }
238 
239  // Store the first candidate found in list, when equivalence is not unique
240  // We use it later.
241  if( module && fpid_candidate.IsEmpty() )
242  fpid_candidate = equivItem.m_FootprintFPID;
243 
244  // The equivalence is not unique: use the footprint filter to try to remove
245  // ambiguity
246  // if the footprint filter does not remove ambiguity, we will use fpid_candidate
247  if( module )
248  {
249  size_t filtercount = component->GetFootprintFilters().GetCount();
250  found = ( 0 == filtercount ); // if no entries, do not filter
251 
252  for( size_t jj = 0; jj < filtercount && !found; jj++ )
253  {
254  found = module->GetFootprintName().Matches( component->GetFootprintFilters()[jj] );
255  }
256  }
257  else
258  {
259  msg.Printf( _( "Component %s: footprint %s not found in any of the project "
260  "footprint libraries." ),
261  GetChars( component->GetReference() ),
262  GetChars( equivItem.m_FootprintFPID ) );
263 
264  if( ! error_msg.IsEmpty() )
265  error_msg << wxT("\n\n");
266 
267  error_msg += msg;
268  }
269 
270  if( found )
271  {
272  SetNewPkg( equivItem.m_FootprintFPID, kk );
273  break;
274  }
275  }
276 
277  if( found )
278  continue;
279  else if( !fpid_candidate.IsEmpty() )
280  {
281  SetNewPkg( fpid_candidate, kk );
282  continue;
283  }
284 
285  // obviously the last chance: there's only one filter matching one footprint
286  if( 1 == component->GetFootprintFilters().GetCount() )
287  {
288  // we do not need to analyze wildcards: single footprint do not
289  // contain them and if there are wildcards it just will not match any
290  const FOOTPRINT_INFO* module = m_FootprintsList->GetModuleInfo( component->GetFootprintFilters()[0] );
291 
292  if( module )
293  {
294  SetNewPkg( component->GetFootprintFilters()[0], kk );
295  }
296  }
297  }
298 
299  if( !error_msg.IsEmpty() )
300  wxMessageBox( error_msg, _( "CvPcb Warning" ), wxOK | wxICON_WARNING, this );
301 
302  m_skipComponentSelect = false;
303  m_compListBox->Refresh();
304 }
CITER next(CITER it)
Definition: ptree.cpp:130
#define QUOTE
Definition: autosel.cpp:48
bool empty() const
Function empty.
Definition: lib_id.h:198
const wxString & GetFootprintName() const
const LIB_ID & GetFPID() const
Definition: pcb_netlist.h:164
static wxString FROM_UTF8(const char *cstring)
function FROM_UTF8 converts a UTF8 encoded C string to a wxString for all wxWidgets build modes...
Definition: macros.h:53
This file is part of the common library TODO brief description.
wxArrayString m_EquFilesNames
COMPONENTS_LISTBOX * m_compListBox
const wxArrayString & GetFootprintFilters() const
Definition: pcb_netlist.h:175
void SetNewPkg(const wxString &aFootprintName)
Function SetNewPkg set the footprint name for all selected components in component list and selects t...
wxString m_ComponentValue
Definition: autosel.h:37
Class SEARCH_STACK looks for files in a number of places.
Definition: search_stack.h:41
bool IsEmpty() const
Function IsEmpty()
Definition: pcb_netlist.h:235
void AutomaticFootprintMatching(wxCommandEvent &event)
Called by the automatic association button Read *.equ files to try to find corresponding footprint fo...
Definition: autosel.cpp:170
KIFACE_I & Kiface()
Global KIFACE_I "get" accessor.
Definition: kicad.cpp:52
This file contains miscellaneous commonly used macros and functions.
const wxString & GetReference() const
Definition: pcb_netlist.h:149
const wxString & GetValue() const
Definition: pcb_netlist.h:151
SEARCH_STACK & KifaceSearch()
Only for DSO specific 'non-library' files.
Definition: kiface_i.h:127
wxString GetQuotedText(wxString &text)
Definition: autosel.cpp:56
boost::ptr_vector< FOOTPRINT_EQUIVALENCE > FOOTPRINT_EQUIVALENCE_LIST
Definition: autosel.h:43
bool sortListbyCmpValue(const FOOTPRINT_EQUIVALENCE &ref, const FOOTPRINT_EQUIVALENCE &test)
Definition: autosel.cpp:76
Class COMPONENT is used to store components and all of their related information found in a netlist...
Definition: pcb_netlist.h:83
int buildEquivalenceList(FOOTPRINT_EQUIVALENCE_LIST &aList, wxString *aErrorMessages=NULL)
read the .equ files and populate the list of equvalents
Definition: autosel.cpp:83
COMPONENT * GetComponent(unsigned aIndex)
Function GetComponent returns the COMPONENT at aIndex.
Definition: pcb_netlist.h:256
char * GetLine(FILE *File, char *Line, int *LineNum, int SizeLine)
Function GetLine reads one line line from aFile.
Definition: string.cpp:213
static const wxChar * GetChars(const wxString &s)
Function GetChars returns a wxChar* to the actual wxChar* data within a wxString, and is helpful for ...
Definition: macros.h:92
see class PGM_BASE
The common library.
wxString m_FootprintFPID
Definition: autosel.h:38
wxString FindValidPath(const wxString &aFileName) const
Definition: search_stack.h:71
unsigned GetCount() const
Function GetCount.
Definition: pcb_netlist.h:247
std::unique_ptr< FOOTPRINT_LIST > m_FootprintsList