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
79 int CVPCB_MAINFRAME::buildEquivalenceList( FOOTPRINT_EQUIVALENCE_LIST& aList, wxString * aErrorMessages )
80 {
81  char line[1024];
82  int error_count = 0;
83  FILE* file;
84  wxFileName fn;
85  wxString tmp, error_msg;
86 
87  SEARCH_STACK& search = Kiface().KifaceSearch();
88  PROJECT_FILE& project = Prj().GetProjectFile();
89 
90  // Find equivalences in all available files, and populates the
91  // equiv_List with all equivalences found in .equ files
92  for( const auto& equfile : project.m_EquivalenceFiles )
93  {
94  fn = wxExpandEnvVars( equfile );
95 
96  tmp = search.FindValidPath( fn.GetFullPath() );
97 
98  if( !tmp )
99  {
100  error_count++;
101 
102  if( aErrorMessages )
103  {
104  error_msg.Printf( _( "Equivalence file \"%s\" could not be found in the "
105  "default search paths." ),
106  GetChars( fn.GetFullName() ) );
107 
108  if( ! aErrorMessages->IsEmpty() )
109  *aErrorMessages << wxT("\n\n");
110 
111  *aErrorMessages += error_msg;
112  }
113 
114  continue;
115  }
116 
117  file = wxFopen( tmp, wxT( "rt" ) );
118 
119  if( file == NULL )
120  {
121  error_count++;
122 
123  if( aErrorMessages )
124  {
125  error_msg.Printf( _( "Error opening equivalence file \"%s\"." ), GetChars( tmp ) );
126 
127  if( ! aErrorMessages->IsEmpty() )
128  *aErrorMessages << wxT("\n\n");
129 
130  *aErrorMessages += error_msg;
131  }
132 
133  continue;
134  }
135 
136  while( GetLine( file, line, NULL, sizeof( line ) ) != NULL )
137  {
138  if( *line == 0 )
139  continue;
140 
141  wxString wtext = FROM_UTF8( line );
142  wxString value = GetQuotedText( wtext );
143 
144  if( value.IsEmpty() )
145  continue;
146 
147  wxString footprint = GetQuotedText( wtext );
148 
149  if( footprint.IsEmpty() )
150  continue;
151 
152  value.Replace( wxT( " " ), wxT( "_" ) );
153 
154  FOOTPRINT_EQUIVALENCE* equivItem = new FOOTPRINT_EQUIVALENCE();
155  equivItem->m_ComponentValue = value;
156  equivItem->m_FootprintFPID = footprint;
157  aList.push_back( equivItem );
158  }
159 
160  fclose( file );
161  }
162 
163  return error_count;
164 }
165 
166 
168 {
169  FOOTPRINT_EQUIVALENCE_LIST equiv_List;
170  wxString msg, error_msg;
171 
172  if( m_netlist.IsEmpty() )
173  return;
174 
175  if( buildEquivalenceList( equiv_List, &error_msg ) )
176  wxMessageBox( error_msg, _( "Equivalence File Load Error" ), wxOK | wxICON_WARNING, this );
177 
178  // Sort the association list by component value.
179  // When sorted, find duplicate definitions (i.e. 2 or more items
180  // having the same component value) is more easy.
181  std::sort( equiv_List.begin(), equiv_List.end(), sortListbyCmpValue );
182 
183  // Display the number of footprint/component equivalences.
184  msg.Printf( _( "%lu footprint/cmp equivalences found." ), (unsigned long)equiv_List.size() );
185  SetStatusText( msg, 0 );
186 
187  // Now, associate each free component with a footprint, when the association
188  // is found in list
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 equiv_List (for polar and
205  // non-polar caps for example)
206  wxString fpid_candidate;
207 
208  for( unsigned idx = 0; idx < equiv_List.size(); idx++ )
209  {
210  FOOTPRINT_EQUIVALENCE& equivItem = equiv_List[idx];
211 
212  if( equivItem.m_ComponentValue.CmpNoCase( component->GetValue() ) != 0 )
213  continue;
214 
215  const FOOTPRINT_INFO *module = m_FootprintsList->GetModuleInfo( equivItem.m_FootprintFPID );
216 
217  bool equ_is_unique = true;
218  unsigned next = idx+1;
219  int previous = idx-1;
220 
221  if( next < equiv_List.size() &&
222  equivItem.m_ComponentValue == equiv_List[next].m_ComponentValue )
223  equ_is_unique = false;
224 
225  if( previous >= 0 &&
226  equivItem.m_ComponentValue == equiv_List[previous].m_ComponentValue )
227  equ_is_unique = false;
228 
229  // If the equivalence is unique, no ambiguity: use the association
230  if( module && equ_is_unique )
231  {
233  firstAssoc );
234  firstAssoc = false;
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  AssociateFootprint( CVPCB_ASSOCIATION( kk, equivItem.m_FootprintFPID ), firstAssoc );
273  firstAssoc = false;
274  break;
275  }
276  }
277 
278  if( found )
279  continue;
280  else if( !fpid_candidate.IsEmpty() )
281  {
282  AssociateFootprint( CVPCB_ASSOCIATION( kk, fpid_candidate ), firstAssoc );
283  firstAssoc = false;
284  continue;
285  }
286 
287  // obviously the last chance: there's only one filter matching one footprint
288  if( 1 == component->GetFootprintFilters().GetCount() )
289  {
290  // we do not need to analyze wildcards: single footprint do not
291  // contain them and if there are wildcards it just will not match any
292  const FOOTPRINT_INFO* module = m_FootprintsList->GetModuleInfo( component->GetFootprintFilters()[0] );
293 
294  if( module )
295  {
297  firstAssoc );
298  firstAssoc = false;
299  }
300  }
301  }
302 
303  if( !error_msg.IsEmpty() )
304  wxMessageBox( error_msg, _( "CvPcb Warning" ), wxOK | wxICON_WARNING, this );
305 
306  m_skipComponentSelect = false;
307  m_compListBox->Refresh();
308 }
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:114
wxString GetQuotedText(wxString &text)
FOOTPRINT_INFO * GetModuleInfo(const wxString &aFootprintId)
Get info for a module by id.
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:254
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:61
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:164
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:129
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:149
std::vector< wxString > m_EquivalenceFiles
CvPcb params.
Definition: project_file.h:160
void SetStatusText(const wxString &aText, int aNumber=0) override
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:263
char * GetLine(FILE *File, char *Line, int *LineNum, int SizeLine)
Read one line line from aFile.
Definition: string.cpp:363
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:153
#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:242
const wxString & GetValue() const
Definition: pcb_netlist.h:151
const wxArrayString & GetFootprintFilters() const
Definition: pcb_netlist.h:175
const wxString & GetFootprintName() const