2013-04-25 16:29:35 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2021-07-16 20:13:26 +00:00
|
|
|
* Copyright (C) 1992-2021 KiCad Developers, see AUTHORS.txt for contributors.
|
2013-04-25 16:29:35 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
2011-09-30 18:15:37 +00:00
|
|
|
/**
|
2018-01-28 08:51:28 +00:00
|
|
|
* @file auto_associate.cpp
|
2011-09-30 18:15:37 +00:00
|
|
|
*/
|
2007-06-05 12:10:51 +00:00
|
|
|
|
2015-01-13 08:05:43 +00:00
|
|
|
// This file handle automatic selection of footprints, from .equ files which give
|
2016-11-20 23:35:08 +00:00
|
|
|
// a footprint LIB_ID associated to a component value.
|
2015-09-21 20:39:59 +00:00
|
|
|
// These associations have this form:
|
2015-01-13 08:05:43 +00:00
|
|
|
// 'FT232BL' 'QFP:LQFP-32_7x7mm_Pitch0.8mm'
|
|
|
|
|
2007-06-05 12:10:51 +00:00
|
|
|
|
2021-09-14 22:45:14 +00:00
|
|
|
#include <kiface_base.h>
|
2021-07-29 09:56:22 +00:00
|
|
|
#include <string_utils.h>
|
2012-01-23 04:33:36 +00:00
|
|
|
#include <macros.h>
|
|
|
|
|
2019-07-24 20:56:17 +00:00
|
|
|
#include <auto_associate.h>
|
|
|
|
#include <cvpcb_association.h>
|
2012-01-23 04:33:36 +00:00
|
|
|
#include <cvpcb_mainframe.h>
|
2018-01-28 08:51:28 +00:00
|
|
|
#include <listboxes.h>
|
2020-05-31 21:42:04 +00:00
|
|
|
#include <project/project_file.h>
|
2021-06-06 12:41:16 +00:00
|
|
|
#include <wx/msgdlg.h>
|
2007-06-05 12:10:51 +00:00
|
|
|
|
2014-07-05 20:42:59 +00:00
|
|
|
#define QUOTE '\''
|
|
|
|
|
2007-06-05 12:10:51 +00:00
|
|
|
|
2021-07-16 20:13:26 +00:00
|
|
|
/**
|
|
|
|
* Read the string between quotes.
|
|
|
|
*
|
|
|
|
* @return a the quoted string.
|
2009-04-23 15:02:18 +00:00
|
|
|
*/
|
2017-06-10 17:02:00 +00:00
|
|
|
wxString GetQuotedText( wxString& text )
|
2009-04-23 15:02:18 +00:00
|
|
|
{
|
2011-06-10 14:33:52 +00:00
|
|
|
int i = text.Find( QUOTE );
|
2013-04-25 16:29:35 +00:00
|
|
|
|
|
|
|
if( wxNOT_FOUND == i )
|
|
|
|
return wxT( "" );
|
|
|
|
|
2011-06-10 14:33:52 +00:00
|
|
|
wxString shrt = text.Mid( i + 1 );
|
|
|
|
i = shrt.Find( QUOTE );
|
2013-04-25 16:29:35 +00:00
|
|
|
|
|
|
|
if( wxNOT_FOUND == i )
|
|
|
|
return wxT( "" );
|
|
|
|
|
2011-06-10 14:33:52 +00:00
|
|
|
text = shrt.Mid( i + 1 );
|
|
|
|
return shrt.Mid( 0, i );
|
2009-04-23 15:02:18 +00:00
|
|
|
}
|
2007-06-05 12:10:51 +00:00
|
|
|
|
2009-04-05 20:49:15 +00:00
|
|
|
|
2015-01-13 08:05:43 +00:00
|
|
|
// A sort compare function, used to sort a FOOTPRINT_EQUIVALENCE_LIST by cmp values
|
|
|
|
// (m_ComponentValue member)
|
|
|
|
bool sortListbyCmpValue( const FOOTPRINT_EQUIVALENCE& ref, const FOOTPRINT_EQUIVALENCE& test )
|
2007-06-05 12:10:51 +00:00
|
|
|
{
|
2015-01-13 08:05:43 +00:00
|
|
|
return ref.m_ComponentValue.Cmp( test.m_ComponentValue ) >= 0;
|
|
|
|
}
|
Modular KiCad Blueprint Milestone B), major portions:
*) When kicad.exe closes a project, close any open KIFACEs so that they cannot
get disassociated from their true PROJECT.
*) Allow loading eeschema library editor from kicad.exe
*) Allow loading pcbnew library editor from kicad.exe
*) Rename LIB_COMPONENT to LIB_PART.
*) Add class PART_LIBS, and PART_LIB.
*) Make PART_LIBS non-global, i.e. PROJECT specific.
*) Implement "data on demand" for PART_LIBS
*) Implement "data on demand" for schematic SEARCH_STACK.
*) Use RSTRINGs to retain eeschema editor's notion of last library and part being edited.
*) Get rid of library search on every SCH_COMPONENT::Draw() call, instead use
a weak pointer.
*) Remove all chdir() calls so projects don't need to be CWD.
*) Romove APPEND support from OpenProjectFiles().
*) Make OpenProjectFiles() robust, even for creating new projects.
*) Load EESCHEMA colors in the KIWAY::OnKiwayStart() rather in window open,
and save them in the .eeschema config file, not in the project file.
*) Fix bug with wxDir() while accessing protected dirs in kicad.exe
*) Consolidate template copying into PROJECT class, not in kicad.exe source.
*) Generally untangle eeschema, making its libraries not global but rather
held in the PROJECT.
2014-08-13 20:28:54 +00:00
|
|
|
|
2015-09-21 20:39:59 +00:00
|
|
|
|
2020-11-07 14:31:50 +00:00
|
|
|
int CVPCB_MAINFRAME::buildEquivalenceList( FOOTPRINT_EQUIVALENCE_LIST& aList,
|
|
|
|
wxString* aErrorMessages )
|
2015-01-13 08:05:43 +00:00
|
|
|
{
|
2017-06-10 17:02:00 +00:00
|
|
|
char line[1024];
|
2015-01-13 08:05:43 +00:00
|
|
|
int error_count = 0;
|
|
|
|
FILE* file;
|
|
|
|
wxFileName fn;
|
|
|
|
wxString tmp, error_msg;
|
2009-04-05 20:49:15 +00:00
|
|
|
|
2020-05-26 02:27:27 +00:00
|
|
|
SEARCH_STACK& search = Kiface().KifaceSearch();
|
|
|
|
PROJECT_FILE& project = Prj().GetProjectFile();
|
2009-04-05 20:49:15 +00:00
|
|
|
|
2015-01-13 08:05:43 +00:00
|
|
|
// Find equivalences in all available files, and populates the
|
|
|
|
// equiv_List with all equivalences found in .equ files
|
2020-11-07 14:31:50 +00:00
|
|
|
for( const wxString& equfile : project.m_EquivalenceFiles )
|
2009-04-05 20:49:15 +00:00
|
|
|
{
|
2020-05-26 02:27:27 +00:00
|
|
|
fn = wxExpandEnvVars( equfile );
|
2014-07-05 20:42:59 +00:00
|
|
|
|
2014-04-08 04:55:53 +00:00
|
|
|
tmp = search.FindValidPath( fn.GetFullPath() );
|
2009-04-05 20:49:15 +00:00
|
|
|
|
|
|
|
if( !tmp )
|
|
|
|
{
|
2015-01-13 08:05:43 +00:00
|
|
|
error_count++;
|
|
|
|
|
|
|
|
if( aErrorMessages )
|
|
|
|
{
|
2021-06-16 22:35:00 +00:00
|
|
|
error_msg.Printf( _( "Equivalence file '%s' could not be found in the "
|
2015-01-13 08:05:43 +00:00
|
|
|
"default search paths." ),
|
2020-11-07 14:31:50 +00:00
|
|
|
fn.GetFullName() );
|
2015-01-13 08:05:43 +00:00
|
|
|
|
|
|
|
if( ! aErrorMessages->IsEmpty() )
|
|
|
|
*aErrorMessages << wxT("\n\n");
|
|
|
|
|
|
|
|
*aErrorMessages += error_msg;
|
|
|
|
}
|
|
|
|
|
2009-04-05 20:49:15 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
file = wxFopen( tmp, wxT( "rt" ) );
|
|
|
|
|
2021-07-16 20:13:26 +00:00
|
|
|
if( file == nullptr )
|
2009-04-05 20:49:15 +00:00
|
|
|
{
|
2015-01-13 08:05:43 +00:00
|
|
|
error_count++;
|
|
|
|
|
|
|
|
if( aErrorMessages )
|
|
|
|
{
|
2021-06-16 22:35:00 +00:00
|
|
|
error_msg.Printf( _( "Error opening equivalence file '%s'." ), tmp );
|
2015-01-13 08:05:43 +00:00
|
|
|
|
|
|
|
if( ! aErrorMessages->IsEmpty() )
|
|
|
|
*aErrorMessages << wxT("\n\n");
|
|
|
|
|
|
|
|
*aErrorMessages += error_msg;
|
|
|
|
}
|
|
|
|
|
2009-04-05 20:49:15 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-07-16 20:13:26 +00:00
|
|
|
while( GetLine( file, line, nullptr, sizeof( line ) ) != nullptr )
|
2009-04-05 20:49:15 +00:00
|
|
|
{
|
2017-06-10 17:02:00 +00:00
|
|
|
if( *line == 0 )
|
|
|
|
continue;
|
2009-05-06 11:55:36 +00:00
|
|
|
|
2017-06-10 17:02:00 +00:00
|
|
|
wxString wtext = FROM_UTF8( line );
|
|
|
|
wxString value = GetQuotedText( wtext );
|
2009-04-21 17:56:27 +00:00
|
|
|
|
2017-06-10 17:02:00 +00:00
|
|
|
if( value.IsEmpty() )
|
2009-04-21 17:56:27 +00:00
|
|
|
continue;
|
2009-04-05 20:49:15 +00:00
|
|
|
|
2017-06-10 17:02:00 +00:00
|
|
|
wxString footprint = GetQuotedText( wtext );
|
2009-04-05 20:49:15 +00:00
|
|
|
|
2009-05-06 11:55:36 +00:00
|
|
|
if( footprint.IsEmpty() )
|
|
|
|
continue;
|
2009-04-21 17:56:27 +00:00
|
|
|
|
2011-06-10 14:33:52 +00:00
|
|
|
value.Replace( wxT( " " ), wxT( "_" ) );
|
|
|
|
|
2015-01-13 08:05:43 +00:00
|
|
|
FOOTPRINT_EQUIVALENCE* equivItem = new FOOTPRINT_EQUIVALENCE();
|
|
|
|
equivItem->m_ComponentValue = value;
|
|
|
|
equivItem->m_FootprintFPID = footprint;
|
|
|
|
aList.push_back( equivItem );
|
2009-04-05 20:49:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fclose( file );
|
|
|
|
}
|
|
|
|
|
2015-01-13 08:05:43 +00:00
|
|
|
return error_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-07-18 21:47:01 +00:00
|
|
|
void CVPCB_MAINFRAME::AutomaticFootprintMatching()
|
2015-01-13 08:05:43 +00:00
|
|
|
{
|
2020-11-07 14:31:50 +00:00
|
|
|
FOOTPRINT_EQUIVALENCE_LIST equivList;
|
|
|
|
wxString msg;
|
|
|
|
wxString error_msg;
|
2015-01-13 08:05:43 +00:00
|
|
|
|
|
|
|
if( m_netlist.IsEmpty() )
|
|
|
|
return;
|
|
|
|
|
2020-11-07 14:31:50 +00:00
|
|
|
if( buildEquivalenceList( equivList, &error_msg ) )
|
|
|
|
wxMessageBox( error_msg, _( "Equivalence File Load Error" ), wxOK | wxICON_WARNING, this );
|
2015-01-13 08:05:43 +00:00
|
|
|
|
2020-11-07 14:31:50 +00:00
|
|
|
// Sort the association list by symbol value. When sorted, finding duplicate definitions
|
|
|
|
// (i.e. 2 or more items having the same symbol value) is easier.
|
|
|
|
std::sort( equivList.begin(), equivList.end(), sortListbyCmpValue );
|
2015-01-13 08:05:43 +00:00
|
|
|
|
2020-11-07 14:31:50 +00:00
|
|
|
// Display the number of footprint/symbol equivalences.
|
|
|
|
msg.Printf( _( "%lu footprint/symbol equivalences found." ), (unsigned long)equivList.size() );
|
2009-04-21 17:56:27 +00:00
|
|
|
SetStatusText( msg, 0 );
|
|
|
|
|
2020-11-07 14:31:50 +00:00
|
|
|
// Now, associate each free component with a footprint
|
2012-05-25 06:58:52 +00:00
|
|
|
m_skipComponentSelect = true;
|
2015-01-13 08:05:43 +00:00
|
|
|
error_msg.Empty();
|
2013-04-25 16:29:35 +00:00
|
|
|
|
2019-07-26 18:58:20 +00:00
|
|
|
bool firstAssoc = true;
|
2013-04-25 16:29:35 +00:00
|
|
|
for( unsigned kk = 0; kk < m_netlist.GetCount(); kk++ )
|
2009-04-05 20:49:15 +00:00
|
|
|
{
|
2017-06-10 12:05:30 +00:00
|
|
|
COMPONENT* component = m_netlist.GetComponent( kk );
|
2013-04-25 16:29:35 +00:00
|
|
|
|
2011-10-20 11:28:37 +00:00
|
|
|
bool found = false;
|
2009-05-06 11:55:36 +00:00
|
|
|
|
2015-01-13 08:05:43 +00:00
|
|
|
if( !component->GetFPID().empty() ) // the component has already a footprint
|
2009-05-06 11:55:36 +00:00
|
|
|
continue;
|
2009-04-23 15:02:18 +00:00
|
|
|
|
2015-01-13 08:05:43 +00:00
|
|
|
// Here a first attempt is made. We can have multiple equivItem of the same value.
|
|
|
|
// When happens, using the footprint filter of components can remove the ambiguity by
|
2020-11-07 14:31:50 +00:00
|
|
|
// filtering equivItem so one can use multiple equivList (for polar and non-polar caps
|
|
|
|
// for example)
|
2017-06-10 17:02:00 +00:00
|
|
|
wxString fpid_candidate;
|
|
|
|
|
2020-11-07 14:31:50 +00:00
|
|
|
for( unsigned idx = 0; idx < equivList.size(); idx++ )
|
2009-04-23 15:02:18 +00:00
|
|
|
{
|
2020-11-07 14:31:50 +00:00
|
|
|
FOOTPRINT_EQUIVALENCE& equivItem = equivList[idx];
|
2011-08-29 19:50:05 +00:00
|
|
|
|
2015-01-13 08:05:43 +00:00
|
|
|
if( equivItem.m_ComponentValue.CmpNoCase( component->GetValue() ) != 0 )
|
2009-05-06 11:55:36 +00:00
|
|
|
continue;
|
2009-04-23 15:02:18 +00:00
|
|
|
|
2020-11-07 14:31:50 +00:00
|
|
|
const FOOTPRINT_INFO *fp = m_FootprintsList->GetFootprintInfo( equivItem.m_FootprintFPID );
|
2015-01-13 08:05:43 +00:00
|
|
|
|
|
|
|
bool equ_is_unique = true;
|
|
|
|
unsigned next = idx+1;
|
2015-01-19 19:03:50 +00:00
|
|
|
int previous = idx-1;
|
2015-01-13 08:05:43 +00:00
|
|
|
|
2020-11-07 14:31:50 +00:00
|
|
|
if( next < equivList.size()
|
|
|
|
&& equivItem.m_ComponentValue == equivList[next].m_ComponentValue )
|
|
|
|
{
|
2015-01-13 08:33:17 +00:00
|
|
|
equ_is_unique = false;
|
2020-11-07 14:31:50 +00:00
|
|
|
}
|
2015-01-13 08:33:17 +00:00
|
|
|
|
2020-11-07 14:31:50 +00:00
|
|
|
if( previous >= 0
|
|
|
|
&& equivItem.m_ComponentValue == equivList[previous].m_ComponentValue )
|
|
|
|
{
|
2015-01-13 08:05:43 +00:00
|
|
|
equ_is_unique = false;
|
2020-11-07 14:31:50 +00:00
|
|
|
}
|
2011-06-10 14:33:52 +00:00
|
|
|
|
2015-01-13 08:05:43 +00:00
|
|
|
// If the equivalence is unique, no ambiguity: use the association
|
2020-11-07 14:31:50 +00:00
|
|
|
if( fp && equ_is_unique )
|
2015-01-13 08:05:43 +00:00
|
|
|
{
|
2019-07-26 18:58:20 +00:00
|
|
|
AssociateFootprint( CVPCB_ASSOCIATION( kk, equivItem.m_FootprintFPID ),
|
2020-11-07 14:31:50 +00:00
|
|
|
firstAssoc );
|
2019-07-26 18:58:20 +00:00
|
|
|
firstAssoc = false;
|
2015-01-13 08:05:43 +00:00
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-06-10 17:02:00 +00:00
|
|
|
// Store the first candidate found in list, when equivalence is not unique
|
|
|
|
// We use it later.
|
2020-11-07 14:31:50 +00:00
|
|
|
if( fp && fpid_candidate.IsEmpty() )
|
2017-06-10 17:02:00 +00:00
|
|
|
fpid_candidate = equivItem.m_FootprintFPID;
|
|
|
|
|
2015-01-13 08:05:43 +00:00
|
|
|
// The equivalence is not unique: use the footprint filter to try to remove
|
|
|
|
// ambiguity
|
2017-06-10 17:02:00 +00:00
|
|
|
// if the footprint filter does not remove ambiguity, we will use fpid_candidate
|
2020-11-07 14:31:50 +00:00
|
|
|
if( fp )
|
2011-06-10 14:33:52 +00:00
|
|
|
{
|
2013-04-25 16:29:35 +00:00
|
|
|
size_t filtercount = component->GetFootprintFilters().GetCount();
|
2011-06-10 14:33:52 +00:00
|
|
|
found = ( 0 == filtercount ); // if no entries, do not filter
|
2009-04-23 15:02:18 +00:00
|
|
|
|
2011-06-12 16:38:43 +00:00
|
|
|
for( size_t jj = 0; jj < filtercount && !found; jj++ )
|
2020-11-07 14:31:50 +00:00
|
|
|
found = fp->GetFootprintName().Matches( component->GetFootprintFilters()[jj] );
|
2011-06-10 14:33:52 +00:00
|
|
|
}
|
|
|
|
else
|
2009-05-06 11:55:36 +00:00
|
|
|
{
|
2013-04-25 16:29:35 +00:00
|
|
|
msg.Printf( _( "Component %s: footprint %s not found in any of the project "
|
|
|
|
"footprint libraries." ),
|
2020-11-07 14:31:50 +00:00
|
|
|
component->GetReference(), equivItem.m_FootprintFPID );
|
2015-01-13 08:05:43 +00:00
|
|
|
|
|
|
|
if( ! error_msg.IsEmpty() )
|
|
|
|
error_msg << wxT("\n\n");
|
|
|
|
|
|
|
|
error_msg += msg;
|
2009-04-23 15:02:18 +00:00
|
|
|
}
|
2013-04-25 16:29:35 +00:00
|
|
|
|
2011-06-10 14:33:52 +00:00
|
|
|
if( found )
|
|
|
|
{
|
2020-11-07 14:31:50 +00:00
|
|
|
AssociateFootprint( CVPCB_ASSOCIATION( kk, equivItem.m_FootprintFPID ),
|
|
|
|
firstAssoc );
|
2019-07-26 18:58:20 +00:00
|
|
|
firstAssoc = false;
|
2011-06-10 14:33:52 +00:00
|
|
|
break;
|
|
|
|
}
|
2009-04-23 15:02:18 +00:00
|
|
|
}
|
2013-04-25 16:29:35 +00:00
|
|
|
|
2015-01-13 08:05:43 +00:00
|
|
|
if( found )
|
2020-11-07 14:31:50 +00:00
|
|
|
{
|
2015-01-13 08:05:43 +00:00
|
|
|
continue;
|
2020-11-07 14:31:50 +00:00
|
|
|
}
|
2017-06-10 17:02:00 +00:00
|
|
|
else if( !fpid_candidate.IsEmpty() )
|
|
|
|
{
|
2019-07-26 18:58:20 +00:00
|
|
|
AssociateFootprint( CVPCB_ASSOCIATION( kk, fpid_candidate ), firstAssoc );
|
|
|
|
firstAssoc = false;
|
2017-06-10 17:02:00 +00:00
|
|
|
continue;
|
|
|
|
}
|
2015-01-13 08:05:43 +00:00
|
|
|
|
2014-07-05 20:42:59 +00:00
|
|
|
// obviously the last chance: there's only one filter matching one footprint
|
2015-01-13 08:05:43 +00:00
|
|
|
if( 1 == component->GetFootprintFilters().GetCount() )
|
2013-04-25 16:29:35 +00:00
|
|
|
{
|
2015-09-21 20:39:59 +00:00
|
|
|
// we do not need to analyze wildcards: single footprint do not
|
2014-07-05 20:42:59 +00:00
|
|
|
// contain them and if there are wildcards it just will not match any
|
2020-11-07 14:31:50 +00:00
|
|
|
if( m_FootprintsList->GetFootprintInfo( component->GetFootprintFilters()[0] ) )
|
2019-07-26 18:58:20 +00:00
|
|
|
{
|
|
|
|
AssociateFootprint( CVPCB_ASSOCIATION( kk, component->GetFootprintFilters()[0] ),
|
2020-11-07 14:31:50 +00:00
|
|
|
firstAssoc );
|
2019-07-26 18:58:20 +00:00
|
|
|
firstAssoc = false;
|
|
|
|
}
|
2011-10-20 11:28:37 +00:00
|
|
|
}
|
2009-04-05 20:49:15 +00:00
|
|
|
}
|
2013-04-25 16:29:35 +00:00
|
|
|
|
2015-01-13 08:05:43 +00:00
|
|
|
if( !error_msg.IsEmpty() )
|
|
|
|
wxMessageBox( error_msg, _( "CvPcb Warning" ), wxOK | wxICON_WARNING, this );
|
|
|
|
|
2012-05-25 06:58:52 +00:00
|
|
|
m_skipComponentSelect = false;
|
2021-03-29 10:46:05 +00:00
|
|
|
m_symbolsListBox->Refresh();
|
2007-06-05 12:10:51 +00:00
|
|
|
}
|