KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 The 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_base.h>
35#include <string_utils.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>
43#include <wx/msgdlg.h>
44
45#define QUOTE '\''
46
47
53wxString GetQuotedText( wxString& text )
54{
55 int i = text.Find( QUOTE );
56
57 if( wxNOT_FOUND == i )
58 return wxT( "" );
59
60 wxString shrt = text.Mid( i + 1 );
61 i = shrt.Find( QUOTE );
62
63 if( wxNOT_FOUND == i )
64 return wxT( "" );
65
66 text = shrt.Mid( i + 1 );
67 return shrt.Mid( 0, i );
68}
69
70
71// A sort compare function, used to sort a FOOTPRINT_EQUIVALENCE_LIST by cmp values
72// (m_ComponentValue member)
74{
75 return ref.m_ComponentValue.Cmp( test.m_ComponentValue ) >= 0;
76}
77
78
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();
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 if( fn.IsAbsolute() || fn.FileExists() )
98 tmp = fn.GetFullPath();
99 else
100 tmp = search.FindValidPath( fn.GetFullPath() );
101
102 if( !tmp )
103 {
104 error_count++;
105
106 if( aErrorMessages )
107 {
108 error_msg.Printf( _( "Equivalence file '%s' could not be found." ),
109 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 == nullptr )
123 {
124 error_count++;
125
126 if( aErrorMessages )
127 {
128 error_msg.Printf( _( "Error opening equivalence file '%s'." ), 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, nullptr, sizeof( line ) ) != nullptr )
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
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
171{
173 wxString msg;
174 wxString error_msg;
175
176 if( m_netlist.IsEmpty() )
177 return;
178
179 if( buildEquivalenceList( equivList, &error_msg ) )
180 wxMessageBox( error_msg, _( "Equivalence File Load Error" ), wxOK | wxICON_WARNING, this );
181
182 // Sort the association list by symbol value. When sorted, finding duplicate definitions
183 // (i.e. 2 or more items having the same symbol value) is easier.
184 std::sort( equivList.begin(), equivList.end(), sortListbyCmpValue );
185
186 // Display the number of footprint/symbol equivalences.
187 msg.Printf( _( "%lu footprint/symbol equivalences found." ), (unsigned long)equivList.size() );
188 SetStatusText( msg, 0 );
189
190 // Now, associate each free component with a footprint
192 error_msg.Empty();
193
194 bool firstAssoc = true;
195
196 for( int kk = 0; kk < (int) m_netlist.GetCount(); kk++ )
197 {
198 COMPONENT* component = m_netlist.GetComponent( kk );
199
200 bool found = false;
201
202 if( !component->GetFPID().empty() ) // the component has already a footprint
203 continue;
204
205 // Here a first attempt is made. We can have multiple equivItem of the same value.
206 // When happens, using the footprint filter of components can remove the ambiguity by
207 // filtering equivItem so one can use multiple equivList (for polar and non-polar caps
208 // for example)
209 wxString fpid_candidate;
210
211 for( int idx = 0; idx < (int) equivList.size(); idx++ )
212 {
213 FOOTPRINT_EQUIVALENCE& equivItem = equivList[idx];
214
215 if( equivItem.m_ComponentValue.CmpNoCase( component->GetValue() ) != 0 )
216 continue;
217
219
220 bool equ_is_unique = true;
221 int next = idx+1;
222 int previous = idx-1;
223
224 if( next < (int) equivList.size() && equivItem.m_ComponentValue == equivList[next].m_ComponentValue )
225 equ_is_unique = false;
226
227 if( previous >= 0 && equivItem.m_ComponentValue == equivList[previous].m_ComponentValue )
228 equ_is_unique = false;
229
230 // If the equivalence is unique, no ambiguity: use the association
231 if( fp && equ_is_unique )
232 {
233 AssociateFootprint( CVPCB_ASSOCIATION( kk, equivItem.m_FootprintFPID ), 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( fp && 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( fp )
248 {
249 size_t filtercount = component->GetFootprintFilters().GetCount();
250 found = ( filtercount == 0 ); // if no entries, do not filter
251
252 for( size_t jj = 0; jj < filtercount && !found; jj++ )
253 found = fp->GetFootprintName().Matches( component->GetFootprintFilters()[jj] );
254 }
255 else
256 {
257 msg.Printf( _( "Component %s: footprint %s not found in any of the project footprint libraries." ),
258 component->GetReference(),
259 equivItem.m_FootprintFPID );
260
261 if( !error_msg.IsEmpty() )
262 error_msg << wxT("\n\n");
263
264 error_msg += msg;
265 }
266
267 if( found )
268 {
269 AssociateFootprint( CVPCB_ASSOCIATION( kk, equivItem.m_FootprintFPID ), firstAssoc );
270 firstAssoc = false;
271 break;
272 }
273 }
274
275 if( found )
276 {
277 continue;
278 }
279 else if( !fpid_candidate.IsEmpty() )
280 {
281 AssociateFootprint( CVPCB_ASSOCIATION( kk, fpid_candidate ), firstAssoc );
282 firstAssoc = false;
283 continue;
284 }
285
286 // obviously the last chance: there's only one filter matching one footprint
287 if( component->GetFootprintFilters().GetCount() == 1 )
288 {
289 // we do not need to analyze wildcards: single footprint do not
290 // contain them and if there are wildcards it just will not match any
292 {
293 AssociateFootprint( CVPCB_ASSOCIATION( kk, component->GetFootprintFilters()[0] ), firstAssoc );
294 firstAssoc = false;
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_symbolsListBox->Refresh();
304}
#define QUOTE
wxString GetQuotedText(wxString &text)
Read the string between quotes.
bool sortListbyCmpValue(const FOOTPRINT_EQUIVALENCE &ref, const FOOTPRINT_EQUIVALENCE &test)
boost::ptr_vector< FOOTPRINT_EQUIVALENCE > FOOTPRINT_EQUIVALENCE_LIST
KIFACE_BASE & Kiface()
Global KIFACE_BASE "get" accessor.
Store all of the related footprint information found in a netlist.
Definition: pcb_netlist.h:100
const wxString & GetReference() const
Definition: pcb_netlist.h:143
const wxString & GetValue() const
Definition: pcb_netlist.h:146
const wxArrayString & GetFootprintFilters() const
Definition: pcb_netlist.h:171
const LIB_ID & GetFPID() const
Definition: pcb_netlist.h:161
A class to define a footprint association to be made in cvpcb.
void SetStatusText(const wxString &aText, int aNumber=0) override
int buildEquivalenceList(FOOTPRINT_EQUIVALENCE_LIST &aList, wxString *aErrorMessages=nullptr)
Read the .equ files and populate the list of equivalents.
FOOTPRINT_LIST * m_FootprintsList
void AssociateFootprint(const CVPCB_ASSOCIATION &aAssociation, bool aNewEntry=true, bool aAddUndoItem=true)
Associate a footprint with a specific component in the list.
SYMBOLS_LISTBOX * m_symbolsListBox
void AutomaticFootprintMatching()
Called by the automatic association button Read *.equ files to try to find corresponding footprint fo...
const wxString & GetFootprintName() const
FOOTPRINT_INFO * GetFootprintInfo(const wxString &aFootprintName)
Get info for a footprint by id.
SEARCH_STACK & KifaceSearch()
Only for DSO specific 'non-library' files.
Definition: kiface_base.h:116
PROJECT & Prj() const
Return a reference to the PROJECT associated with this KIWAY.
bool empty() const
Definition: lib_id.h:193
unsigned GetCount() const
Definition: pcb_netlist.h:295
bool IsEmpty() const
Definition: pcb_netlist.h:285
COMPONENT * GetComponent(unsigned aIndex)
Return the COMPONENT at aIndex.
Definition: pcb_netlist.h:303
The backing store for a PROJECT, in JSON format.
Definition: project_file.h:74
virtual PROJECT_FILE & GetProjectFile() const
Definition: project.h:204
Look for files in a number of paths.
Definition: search_stack.h:43
#define _(s)
This file contains miscellaneous commonly used macros and functions.
CITER next(CITER it)
Definition: ptree.cpp:124
wxString From_UTF8(const char *cstring)
char * GetLine(FILE *File, char *Line, int *LineNum, int SizeLine)
Read one line line from aFile.