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 (C) 1992-2022 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 for( unsigned kk = 0; kk < m_netlist.GetCount(); kk++ )
196 {
197 COMPONENT* component = m_netlist.GetComponent( kk );
198
199 bool found = false;
200
201 if( !component->GetFPID().empty() ) // the component has already a footprint
202 continue;
203
204 // Here a first attempt is made. We can have multiple equivItem of the same value.
205 // When happens, using the footprint filter of components can remove the ambiguity by
206 // filtering equivItem so one can use multiple equivList (for polar and non-polar caps
207 // for example)
208 wxString fpid_candidate;
209
210 for( unsigned idx = 0; idx < equivList.size(); idx++ )
211 {
212 FOOTPRINT_EQUIVALENCE& equivItem = equivList[idx];
213
214 if( equivItem.m_ComponentValue.CmpNoCase( component->GetValue() ) != 0 )
215 continue;
216
218
219 bool equ_is_unique = true;
220 unsigned next = idx+1;
221 int previous = idx-1;
222
223 if( next < equivList.size()
224 && equivItem.m_ComponentValue == equivList[next].m_ComponentValue )
225 {
226 equ_is_unique = false;
227 }
228
229 if( previous >= 0
230 && equivItem.m_ComponentValue == equivList[previous].m_ComponentValue )
231 {
232 equ_is_unique = false;
233 }
234
235 // If the equivalence is unique, no ambiguity: use the association
236 if( fp && equ_is_unique )
237 {
239 firstAssoc );
240 firstAssoc = false;
241 found = true;
242 break;
243 }
244
245 // Store the first candidate found in list, when equivalence is not unique
246 // We use it later.
247 if( fp && fpid_candidate.IsEmpty() )
248 fpid_candidate = equivItem.m_FootprintFPID;
249
250 // The equivalence is not unique: use the footprint filter to try to remove
251 // ambiguity
252 // if the footprint filter does not remove ambiguity, we will use fpid_candidate
253 if( fp )
254 {
255 size_t filtercount = component->GetFootprintFilters().GetCount();
256 found = ( 0 == filtercount ); // if no entries, do not filter
257
258 for( size_t jj = 0; jj < filtercount && !found; jj++ )
259 found = fp->GetFootprintName().Matches( component->GetFootprintFilters()[jj] );
260 }
261 else
262 {
263 msg.Printf( _( "Component %s: footprint %s not found in any of the project "
264 "footprint libraries." ),
265 component->GetReference(), equivItem.m_FootprintFPID );
266
267 if( ! error_msg.IsEmpty() )
268 error_msg << wxT("\n\n");
269
270 error_msg += msg;
271 }
272
273 if( found )
274 {
276 firstAssoc );
277 firstAssoc = false;
278 break;
279 }
280 }
281
282 if( found )
283 {
284 continue;
285 }
286 else if( !fpid_candidate.IsEmpty() )
287 {
288 AssociateFootprint( CVPCB_ASSOCIATION( kk, fpid_candidate ), firstAssoc );
289 firstAssoc = false;
290 continue;
291 }
292
293 // obviously the last chance: there's only one filter matching one footprint
294 if( 1 == component->GetFootprintFilters().GetCount() )
295 {
296 // we do not need to analyze wildcards: single footprint do not
297 // contain them and if there are wildcards it just will not match any
299 {
301 firstAssoc );
302 firstAssoc = false;
303 }
304 }
305 }
306
307 if( !error_msg.IsEmpty() )
308 wxMessageBox( error_msg, _( "CvPcb Warning" ), wxOK | wxICON_WARNING, this );
309
310 m_skipComponentSelect = false;
311 m_symbolsListBox->Refresh();
312}
#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:86
const wxString & GetReference() const
Definition: pcb_netlist.h:127
const wxString & GetValue() const
Definition: pcb_netlist.h:130
const wxArrayString & GetFootprintFilters() const
Definition: pcb_netlist.h:155
const LIB_ID & GetFPID() const
Definition: pcb_netlist.h:145
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:244
bool IsEmpty() const
Definition: pcb_netlist.h:234
COMPONENT * GetComponent(unsigned aIndex)
Return the COMPONENT at aIndex.
Definition: pcb_netlist.h:252
The backing store for a PROJECT, in JSON format.
Definition: project_file.h:70
virtual PROJECT_FILE & GetProjectFile() const
Definition: project.h:166
Look for files in a number of paths.
Definition: search_stack.h:42
#define _(s)
This file contains miscellaneous commonly used macros and functions.
CITER next(CITER it)
Definition: ptree.cpp:126
wxString From_UTF8(const char *cstring)
char * GetLine(FILE *File, char *Line, int *LineNum, int SizeLine)
Read one line line from aFile.