KiCad PCB EDA Suite
auto_associate.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-2018 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 <kiface_i.h>
35 #include <kicad_string.h>
36 #include <macros.h>
37 
38 #include <auto_associate.h>
39 #include <cvpcb_association.h>
40 #include <cvpcb_mainframe.h>
41 #include <listboxes.h>
42 
43 #define QUOTE '\''
44 
45 
46 /*
47  * read the string between quotes and put it in aTarget
48  * put text in aTarget
49  * return a pointer to the last read char (the second quote if OK)
50  */
51 wxString GetQuotedText( wxString& text )
52 {
53  int i = text.Find( QUOTE );
54 
55  if( wxNOT_FOUND == i )
56  return wxT( "" );
57 
58  wxString shrt = text.Mid( i + 1 );
59  i = shrt.Find( QUOTE );
60 
61  if( wxNOT_FOUND == i )
62  return wxT( "" );
63 
64  text = shrt.Mid( i + 1 );
65  return shrt.Mid( 0, i );
66 }
67 
68 
69 // A sort compare function, used to sort a FOOTPRINT_EQUIVALENCE_LIST by cmp values
70 // (m_ComponentValue member)
72 {
73  return ref.m_ComponentValue.Cmp( test.m_ComponentValue ) >= 0;
74 }
75 
76 
77 // read the .equ files and populate the list of equivalents
78 int CVPCB_MAINFRAME::buildEquivalenceList( FOOTPRINT_EQUIVALENCE_LIST& aList, wxString * aErrorMessages )
79 {
80  char line[1024];
81  int error_count = 0;
82  FILE* file;
83  wxFileName fn;
84  wxString tmp, error_msg;
85 
86  SEARCH_STACK& search = Kiface().KifaceSearch();
87 
88  // Find equivalences in all available files, and populates the
89  // equiv_List with all equivalences found in .equ files
90  for( unsigned ii = 0; ii < m_EquFilesNames.GetCount(); ii++ )
91  {
92  fn = wxExpandEnvVars( m_EquFilesNames[ii] );
93 
94  tmp = search.FindValidPath( fn.GetFullPath() );
95 
96  if( !tmp )
97  {
98  error_count++;
99 
100  if( aErrorMessages )
101  {
102  error_msg.Printf( _( "Equivalence file \"%s\" could not be found in the "
103  "default search paths." ),
104  GetChars( fn.GetFullName() ) );
105 
106  if( ! aErrorMessages->IsEmpty() )
107  *aErrorMessages << wxT("\n\n");
108 
109  *aErrorMessages += error_msg;
110  }
111 
112  continue;
113  }
114 
115  file = wxFopen( tmp, wxT( "rt" ) );
116 
117  if( file == NULL )
118  {
119  error_count++;
120 
121  if( aErrorMessages )
122  {
123  error_msg.Printf( _( "Error opening equivalence file \"%s\"." ), GetChars( tmp ) );
124 
125  if( ! aErrorMessages->IsEmpty() )
126  *aErrorMessages << wxT("\n\n");
127 
128  *aErrorMessages += error_msg;
129  }
130 
131  continue;
132  }
133 
134  while( GetLine( file, line, NULL, sizeof( line ) ) != NULL )
135  {
136  if( *line == 0 )
137  continue;
138 
139  wxString wtext = FROM_UTF8( line );
140  wxString value = GetQuotedText( wtext );
141 
142  if( value.IsEmpty() )
143  continue;
144 
145  wxString footprint = GetQuotedText( wtext );
146 
147  if( footprint.IsEmpty() )
148  continue;
149 
150  value.Replace( wxT( " " ), wxT( "_" ) );
151 
152  FOOTPRINT_EQUIVALENCE* equivItem = new FOOTPRINT_EQUIVALENCE();
153  equivItem->m_ComponentValue = value;
154  equivItem->m_FootprintFPID = footprint;
155  aList.push_back( equivItem );
156  }
157 
158  fclose( file );
159  }
160 
161  return error_count;
162 }
163 
164 
166 {
167  FOOTPRINT_EQUIVALENCE_LIST equiv_List;
168  wxString msg, error_msg;
169 
170  if( m_netlist.IsEmpty() )
171  return;
172 
173  if( buildEquivalenceList( equiv_List, &error_msg ) )
174  wxMessageBox( error_msg, _( "Equivalence File Load Error" ), wxOK | wxICON_WARNING, this );
175 
176  // Sort the association list by component value.
177  // When sorted, find duplicate definitions (i.e. 2 or more items
178  // having the same component value) is more easy.
179  std::sort( equiv_List.begin(), equiv_List.end(), sortListbyCmpValue );
180 
181  // Display the number of footprint/component equivalences.
182  msg.Printf( _( "%lu footprint/cmp equivalences found." ), (unsigned long)equiv_List.size() );
183  SetStatusText( msg, 0 );
184 
185  // Now, associate each free component with a footprint, when the association
186  // is found in list
187  m_skipComponentSelect = true;
188  error_msg.Empty();
189 
190  bool firstAssoc = true;
191  for( unsigned kk = 0; kk < m_netlist.GetCount(); kk++ )
192  {
193  COMPONENT* component = m_netlist.GetComponent( kk );
194 
195  bool found = false;
196 
197  if( !component->GetFPID().empty() ) // the component has already a footprint
198  continue;
199 
200  // Here a first attempt is made. We can have multiple equivItem of the same value.
201  // When happens, using the footprint filter of components can remove the ambiguity by
202  // filtering equivItem so one can use multiple equiv_List (for polar and
203  // non-polar caps for example)
204  wxString fpid_candidate;
205 
206  for( unsigned idx = 0; idx < equiv_List.size(); idx++ )
207  {
208  FOOTPRINT_EQUIVALENCE& equivItem = equiv_List[idx];
209 
210  if( equivItem.m_ComponentValue.CmpNoCase( component->GetValue() ) != 0 )
211  continue;
212 
213  const FOOTPRINT_INFO *module = m_FootprintsList->GetModuleInfo( equivItem.m_FootprintFPID );
214 
215  bool equ_is_unique = true;
216  unsigned next = idx+1;
217  int previous = idx-1;
218 
219  if( next < equiv_List.size() &&
220  equivItem.m_ComponentValue == equiv_List[next].m_ComponentValue )
221  equ_is_unique = false;
222 
223  if( previous >= 0 &&
224  equivItem.m_ComponentValue == equiv_List[previous].m_ComponentValue )
225  equ_is_unique = false;
226 
227  // If the equivalence is unique, no ambiguity: use the association
228  if( module && equ_is_unique )
229  {
231  firstAssoc );
232  firstAssoc = false;
233  found = true;
234  break;
235  }
236 
237  // Store the first candidate found in list, when equivalence is not unique
238  // We use it later.
239  if( module && fpid_candidate.IsEmpty() )
240  fpid_candidate = equivItem.m_FootprintFPID;
241 
242  // The equivalence is not unique: use the footprint filter to try to remove
243  // ambiguity
244  // if the footprint filter does not remove ambiguity, we will use fpid_candidate
245  if( module )
246  {
247  size_t filtercount = component->GetFootprintFilters().GetCount();
248  found = ( 0 == filtercount ); // if no entries, do not filter
249 
250  for( size_t jj = 0; jj < filtercount && !found; jj++ )
251  {
252  found = module->GetFootprintName().Matches( component->GetFootprintFilters()[jj] );
253  }
254  }
255  else
256  {
257  msg.Printf( _( "Component %s: footprint %s not found in any of the project "
258  "footprint libraries." ),
259  GetChars( component->GetReference() ),
260  GetChars( equivItem.m_FootprintFPID ) );
261 
262  if( ! error_msg.IsEmpty() )
263  error_msg << wxT("\n\n");
264 
265  error_msg += msg;
266  }
267 
268  if( found )
269  {
270  AssociateFootprint( CVPCB_ASSOCIATION( kk, equivItem.m_FootprintFPID ), firstAssoc );
271  firstAssoc = false;
272  break;
273  }
274  }
275 
276  if( found )
277  continue;
278  else if( !fpid_candidate.IsEmpty() )
279  {
280  AssociateFootprint( CVPCB_ASSOCIATION( kk, fpid_candidate ), firstAssoc );
281  firstAssoc = false;
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  {
295  firstAssoc );
296  firstAssoc = false;
297  }
298  }
299  }
300 
301  if( !error_msg.IsEmpty() )
302  wxMessageBox( error_msg, _( "CvPcb Warning" ), wxOK | wxICON_WARNING, this );
303 
304  m_skipComponentSelect = false;
305  m_compListBox->Refresh();
306 }
CITER next(CITER it)
Definition: ptree.cpp:130
void AutomaticFootprintMatching()
Called by the automatic association button Read *.equ files to try to find corresponding footprint fo...
wxString FindValidPath(const wxString &aFileName) const
Definition: search_stack.h:73
FOOTPRINT_LIST * m_FootprintsList
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:62
wxString GetQuotedText(wxString &text)
FOOTPRINT_INFO * GetModuleInfo(const wxString &aFootprintId)
Get info for a module by id.
wxArrayString m_EquFilesNames
COMPONENTS_LISTBOX * m_compListBox
void AssociateFootprint(const CVPCB_ASSOCIATION &aAssociation, bool aNewEntry=true, bool aAddUndoItem=true)
Associate a footprint with a specific component in the list.
bool sortListbyCmpValue(const FOOTPRINT_EQUIVALENCE &ref, const FOOTPRINT_EQUIVALENCE &test)
unsigned GetCount() const
Function GetCount.
Definition: pcb_netlist.h:256
Class SEARCH_STACK looks for files in a number of places.
Definition: search_stack.h:41
bool empty() const
Definition: lib_id.h:186
KIFACE_I & Kiface()
Global KIFACE_I "get" accessor.
Definition: kicad.cpp:51
This file contains miscellaneous commonly used macros and functions.
const LIB_ID & GetFPID() const
Definition: pcb_netlist.h:166
SEARCH_STACK & KifaceSearch()
Only for DSO specific 'non-library' files.
Definition: kiface_i.h:127
boost::ptr_vector< FOOTPRINT_EQUIVALENCE > FOOTPRINT_EQUIVALENCE_LIST
const wxString & GetReference() const
Definition: pcb_netlist.h:151
void SetStatusText(const wxString &aText, int aNumber=0) override
#define _(s)
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
COMPONENT * GetComponent(unsigned aIndex)
Function GetComponent returns the COMPONENT at aIndex.
Definition: pcb_netlist.h:265
char * GetLine(FILE *File, char *Line, int *LineNum, int SizeLine)
Read one line line from aFile.
Definition: string.cpp:329
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:101
#define QUOTE
size_t i
Definition: json11.cpp:649
A class to define a footprint association to be made in cvpcb.
bool IsEmpty() const
Function IsEmpty()
Definition: pcb_netlist.h:244
const wxString & GetValue() const
Definition: pcb_netlist.h:153
const wxArrayString & GetFootprintFilters() const
Definition: pcb_netlist.h:177
const wxString & GetFootprintName() const