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, see <https://www.gnu.org/licenses/>.
18 */
19
23
24// This file handle automatic selection of footprints, from .equ files which give
25// a footprint LIB_ID associated to a component value.
26// These associations have this form:
27// 'FT232BL' 'QFP:LQFP-32_7x7mm_Pitch0.8mm'
28
29
30#include <kiface_base.h>
31#include <string_utils.h>
32#include <macros.h>
33
34#include <auto_associate.h>
35#include <cvpcb_association.h>
36#include <cvpcb_mainframe.h>
37#include <listboxes.h>
39#include <wx/msgdlg.h>
40
41#define QUOTE '\''
42
43
49wxString GetQuotedText( wxString& text )
50{
51 int i = text.Find( QUOTE );
52
53 if( wxNOT_FOUND == i )
54 return wxT( "" );
55
56 wxString shrt = text.Mid( i + 1 );
57 i = shrt.Find( QUOTE );
58
59 if( wxNOT_FOUND == i )
60 return wxT( "" );
61
62 text = shrt.Mid( i + 1 );
63 return shrt.Mid( 0, i );
64}
65
66
67// A sort compare function, used to sort a FOOTPRINT_EQUIVALENCE_LIST by cmp values
68// (m_ComponentValue member)
70{
71 return ref.m_ComponentValue.Cmp( test.m_ComponentValue ) > 0;
72}
73
74
76 wxString* aErrorMessages )
77{
78 char line[1024];
79 int error_count = 0;
80 FILE* file;
81 wxFileName fn;
82 wxString tmp, error_msg;
83
84 SEARCH_STACK& search = Kiface().KifaceSearch();
86
87 // Find equivalences in all available files, and populates the
88 // equiv_List with all equivalences found in .equ files
89 for( const wxString& equfile : project.m_EquivalenceFiles )
90 {
91 fn = wxExpandEnvVars( equfile );
92
93 if( fn.IsAbsolute() || fn.FileExists() )
94 tmp = fn.GetFullPath();
95 else
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." ),
105 fn.GetFullName() );
106
107 if( ! aErrorMessages->IsEmpty() )
108 *aErrorMessages << wxT( "\n\n" );
109
110 *aErrorMessages += error_msg;
111 }
112
113 continue;
114 }
115
116 file = wxFopen( tmp, wxT( "rt" ) );
117
118 if( file == nullptr )
119 {
120 error_count++;
121
122 if( aErrorMessages )
123 {
124 error_msg.Printf( _( "Error opening equivalence file '%s'." ), tmp );
125
126 if( ! aErrorMessages->IsEmpty() )
127 *aErrorMessages << wxT( "\n\n" );
128
129 *aErrorMessages += error_msg;
130 }
131
132 continue;
133 }
134
135 while( GetLine( file, line, nullptr, sizeof( line ) ) != nullptr )
136 {
137 if( *line == 0 )
138 continue;
139
140 wxString wtext = From_UTF8( line );
141 wxString value = GetQuotedText( wtext );
142
143 if( value.IsEmpty() )
144 continue;
145
146 wxString footprint = GetQuotedText( wtext );
147
148 if( footprint.IsEmpty() )
149 continue;
150
151 value.Replace( wxT( " " ), wxT( "_" ) );
152
154 equivItem->m_ComponentValue = value;
155 equivItem->m_FootprintFPID = footprint;
156 aList.push_back( equivItem );
157 }
158
159 fclose( file );
160 }
161
162 return error_count;
163}
164
165
167{
169 wxString msg;
170 wxString error_msg;
171
172 if( m_netlist.IsEmpty() )
173 return;
174
175 if( buildEquivalenceList( equivList, &error_msg ) )
176 wxMessageBox( error_msg, _( "Equivalence File Load Error" ), wxOK | wxICON_WARNING, this );
177
178 // Sort the association list by symbol value. When sorted, finding duplicate definitions
179 // (i.e. 2 or more items having the same symbol value) is easier.
180 std::sort( equivList.begin(), equivList.end(), sortListbyCmpValue );
181
182 // Display the number of footprint/symbol equivalences.
183 msg.Printf( _( "%lu footprint/symbol equivalences found." ), (unsigned long)equivList.size() );
184 SetStatusText( msg, 0 );
185
186 // Now, associate each free component with a footprint
188 error_msg.Empty();
189
190 bool firstAssoc = true;
191
192 for( int kk = 0; kk < (int) m_netlist.GetCount(); kk++ )
193 {
194 COMPONENT* component = m_netlist.GetComponent( kk );
195
196 bool found = false;
197
198 if( !component->GetFPID().empty() ) // the component has already a footprint
199 continue;
200
201 // Here a first attempt is made. We can have multiple equivItem of the same value.
202 // When happens, using the footprint filter of components can remove the ambiguity by
203 // filtering equivItem so one can use multiple equivList (for polar and non-polar caps
204 // for example)
205 wxString fpid_candidate;
206
207 for( int idx = 0; idx < (int) equivList.size(); idx++ )
208 {
209 FOOTPRINT_EQUIVALENCE& equivItem = equivList[idx];
210
211 if( equivItem.m_ComponentValue.CmpNoCase( component->GetValue() ) != 0 )
212 continue;
213
214 const FOOTPRINT_INFO* fp = m_FootprintsList->GetFootprintInfo( equivItem.m_FootprintFPID );
215
216 bool equ_is_unique = true;
217 int next = idx+1;
218 int previous = idx-1;
219
220 if( next < (int) equivList.size() && equivItem.m_ComponentValue == equivList[next].m_ComponentValue )
221 equ_is_unique = false;
222
223 if( previous >= 0 && equivItem.m_ComponentValue == equivList[previous].m_ComponentValue )
224 equ_is_unique = false;
225
226 // If the equivalence is unique, no ambiguity: use the association
227 if( fp && equ_is_unique )
228 {
229 AssociateFootprint( CVPCB_ASSOCIATION( kk, equivItem.m_FootprintFPID ), firstAssoc );
230 firstAssoc = false;
231 found = true;
232 break;
233 }
234
235 // Store the first candidate found in list, when equivalence is not unique
236 // We use it later.
237 if( fp && fpid_candidate.IsEmpty() )
238 fpid_candidate = equivItem.m_FootprintFPID;
239
240 // The equivalence is not unique: use the footprint filter to try to remove
241 // ambiguity
242 // if the footprint filter does not remove ambiguity, we will use fpid_candidate
243 if( fp )
244 {
245 size_t filtercount = component->GetFootprintFilters().GetCount();
246 found = ( filtercount == 0 ); // if no entries, do not filter
247
248 for( size_t jj = 0; jj < filtercount && !found; jj++ )
249 found = fp->GetFootprintName().Matches( component->GetFootprintFilters()[jj] );
250 }
251 else
252 {
253 msg.Printf( _( "Component %s: footprint %s not found in any of the project footprint libraries." ),
254 component->GetReference(),
255 equivItem.m_FootprintFPID );
256
257 if( !error_msg.IsEmpty() )
258 error_msg << wxT("\n\n");
259
260 error_msg += msg;
261 }
262
263 if( found )
264 {
265 AssociateFootprint( CVPCB_ASSOCIATION( kk, equivItem.m_FootprintFPID ), firstAssoc );
266 firstAssoc = false;
267 break;
268 }
269 }
270
271 if( found )
272 {
273 continue;
274 }
275 else if( !fpid_candidate.IsEmpty() )
276 {
277 AssociateFootprint( CVPCB_ASSOCIATION( kk, fpid_candidate ), firstAssoc );
278 firstAssoc = false;
279 continue;
280 }
281
282 // obviously the last chance: there's only one filter matching one footprint
283 if( component->GetFootprintFilters().GetCount() == 1 )
284 {
285 // we do not need to analyze wildcards: single footprint do not
286 // contain them and if there are wildcards it just will not match any
287 if( m_FootprintsList->GetFootprintInfo( component->GetFootprintFilters()[0] ) )
288 {
289 AssociateFootprint( CVPCB_ASSOCIATION( kk, component->GetFootprintFilters()[0] ), firstAssoc );
290 firstAssoc = false;
291 }
292 }
293 }
294
295 if( !error_msg.IsEmpty() )
296 wxMessageBox( error_msg, _( "CvPcb Warning" ), wxOK | wxICON_WARNING, this );
297
298 m_skipComponentSelect = false;
299 m_symbolsListBox->Refresh();
300}
#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 component information found in a netlist.
const wxString & GetReference() const
const wxString & GetValue() const
const wxArrayString & GetFootprintFilters() const
const LIB_ID & GetFPID() const
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
SEARCH_STACK & KifaceSearch()
Only for DSO specific 'non-library' files.
PROJECT & Prj() const
Return a reference to the PROJECT associated with this KIWAY.
bool empty() const
Definition lib_id.h:189
The backing store for a PROJECT, in JSON format.
virtual PROJECT_FILE & GetProjectFile() const
Definition project.h:200
Look for files in a number of paths.
#define _(s)
This file contains miscellaneous commonly used macros and functions.
CITER next(CITER it)
Definition ptree.cpp:120
wxString From_UTF8(const char *cstring)
char * GetLine(FILE *File, char *Line, int *LineNum, int SizeLine)
Read one line line from aFile.