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  char* text = Line;
142  wxString value, footprint, wtext = FROM_UTF8( Line );
143 
144  value = GetQuotedText( wtext );
145 
146  if( text == NULL || ( *text == 0 ) || value.IsEmpty() )
147  continue;
148 
149  footprint = GetQuotedText( wtext );
150 
151  if( footprint.IsEmpty() )
152  continue;
153 
154  value.Replace( wxT( " " ), wxT( "_" ) );
155 
156  FOOTPRINT_EQUIVALENCE* equivItem = new FOOTPRINT_EQUIVALENCE();
157  equivItem->m_ComponentValue = value;
158  equivItem->m_FootprintFPID = footprint;
159  aList.push_back( equivItem );
160  }
161 
162  fclose( file );
163  }
164 
165  return error_count;
166 }
167 
168 
169 void CVPCB_MAINFRAME::AutomaticFootprintMatching( wxCommandEvent& event )
170 {
171  FOOTPRINT_EQUIVALENCE_LIST equiv_List;
172  COMPONENT* component;
173  wxString msg, error_msg;
174  size_t ii;
175 
176  if( m_netlist.IsEmpty() )
177  return;
178 
179  if( buildEquivalenceList( equiv_List, &error_msg ) )
180  wxMessageBox( error_msg, _( "Equivalence File Load Error" ), wxOK | wxICON_WARNING, this );
181 
182  // Sort the association list by component value.
183  // When sorted, find duplicate definitions (i.e. 2 or more items
184  // having the same component value) is more easy.
185  std::sort( equiv_List.begin(), equiv_List.end(), sortListbyCmpValue );
186 
187  // Display the number of footprint/component equivalences.
188  msg.Printf( _( "%lu footprint/cmp equivalences found." ), (unsigned long)equiv_List.size() );
189  SetStatusText( msg, 0 );
190 
191  // Now, associate each free component with a footprint, when the association
192  // is found in list
193  m_skipComponentSelect = true;
194  ii = 0;
195  error_msg.Empty();
196 
197  for( unsigned kk = 0; kk < m_netlist.GetCount(); kk++ )
198  {
199  component = m_netlist.GetComponent( kk );
200 
201  bool found = false;
202  m_compListBox->SetSelection( ii++, true );
203 
204  if( !component->GetFPID().empty() ) // the component has already a footprint
205  continue;
206 
207  // Here a first attempt is made. We can have multiple equivItem of the same value.
208  // When happens, using the footprint filter of components can remove the ambiguity by
209  // filtering equivItem so one can use multiple equiv_List (for polar and
210  // non-polar caps for example)
211  for( unsigned idx = 0; idx < equiv_List.size(); idx++ )
212  {
213  FOOTPRINT_EQUIVALENCE& equivItem = equiv_List[idx];
214 
215  if( equivItem.m_ComponentValue.CmpNoCase( component->GetValue() ) != 0 )
216  continue;
217 
218  const FOOTPRINT_INFO *module = m_FootprintsList->GetModuleInfo( equivItem.m_FootprintFPID );
219 
220  bool equ_is_unique = true;
221  unsigned next = idx+1;
222  int previous = idx-1;
223 
224  if( next < equiv_List.size() &&
225  equivItem.m_ComponentValue == equiv_List[next].m_ComponentValue )
226  equ_is_unique = false;
227 
228  if( previous >= 0 &&
229  equivItem.m_ComponentValue == equiv_List[previous].m_ComponentValue )
230  equ_is_unique = false;
231 
232  // If the equivalence is unique, no ambiguity: use the association
233  if( module && equ_is_unique )
234  {
235  SetNewPkg( equivItem.m_FootprintFPID );
236  found = true;
237  break;
238  }
239 
240  // The equivalence is not unique: use the footprint filter to try to remove
241  // ambiguity
242  if( module )
243  {
244  size_t filtercount = component->GetFootprintFilters().GetCount();
245  found = ( 0 == filtercount ); // if no entries, do not filter
246 
247  for( size_t jj = 0; jj < filtercount && !found; jj++ )
248  {
249  found = module->GetFootprintName().Matches( component->GetFootprintFilters()[jj] );
250  }
251  }
252  else
253  {
254  msg.Printf( _( "Component %s: footprint %s not found in any of the project "
255  "footprint libraries." ),
256  GetChars( component->GetReference() ),
257  GetChars( equivItem.m_FootprintFPID ) );
258 
259  if( ! error_msg.IsEmpty() )
260  error_msg << wxT("\n\n");
261 
262  error_msg += msg;
263  }
264 
265  if( found )
266  {
267  SetNewPkg( equivItem.m_FootprintFPID );
268  break;
269  }
270  }
271 
272  if( found )
273  continue;
274 
275  // obviously the last chance: there's only one filter matching one footprint
276  if( 1 == component->GetFootprintFilters().GetCount() )
277  {
278  // we do not need to analyze wildcards: single footprint do not
279  // contain them and if there are wildcards it just will not match any
280  const FOOTPRINT_INFO* module = m_FootprintsList->GetModuleInfo( component->GetFootprintFilters()[0] );
281 
282  if( module )
283  SetNewPkg( component->GetFootprintFilters()[0] );
284  }
285  }
286 
287  if( !error_msg.IsEmpty() )
288  wxMessageBox( error_msg, _( "CvPcb Warning" ), wxOK | wxICON_WARNING, this );
289 
290  m_skipComponentSelect = false;
291 }
CITER next(CITER it)
Definition: ptree.cpp:130
#define QUOTE
Definition: autosel.cpp:48
bool empty() const
Function empty.
Definition: lib_id.h:199
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 SetSelection(int index, bool State=true)
void SetNewPkg(const wxString &aFootprintName)
Function SetNewPkg links the footprint to the current selected component and selects the next compone...
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:169
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:125
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