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