You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

311 lines
9.9 KiB

6 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 1992-2018 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. /**
  24. * @file auto_associate.cpp
  25. */
  26. // This file handle automatic selection of footprints, from .equ files which give
  27. // a footprint LIB_ID associated to a component value.
  28. // These associations have this form:
  29. // 'FT232BL' 'QFP:LQFP-32_7x7mm_Pitch0.8mm'
  30. #include <kiface_i.h>
  31. #include <kicad_string.h>
  32. #include <macros.h>
  33. #include <auto_associate.h>
  34. #include <cvpcb_association.h>
  35. #include <cvpcb_mainframe.h>
  36. #include <listboxes.h>
  37. #include <project/project_file.h>
  38. #include <wx/msgdlg.h>
  39. #define QUOTE '\''
  40. /*
  41. * read the string between quotes and put it in aTarget
  42. * put text in aTarget
  43. * return a pointer to the last read char (the second quote if OK)
  44. */
  45. wxString GetQuotedText( wxString& text )
  46. {
  47. int i = text.Find( QUOTE );
  48. if( wxNOT_FOUND == i )
  49. return wxT( "" );
  50. wxString shrt = text.Mid( i + 1 );
  51. i = shrt.Find( QUOTE );
  52. if( wxNOT_FOUND == i )
  53. return wxT( "" );
  54. text = shrt.Mid( i + 1 );
  55. return shrt.Mid( 0, i );
  56. }
  57. // A sort compare function, used to sort a FOOTPRINT_EQUIVALENCE_LIST by cmp values
  58. // (m_ComponentValue member)
  59. bool sortListbyCmpValue( const FOOTPRINT_EQUIVALENCE& ref, const FOOTPRINT_EQUIVALENCE& test )
  60. {
  61. return ref.m_ComponentValue.Cmp( test.m_ComponentValue ) >= 0;
  62. }
  63. // read the .equ files and populate the list of equivalents
  64. int CVPCB_MAINFRAME::buildEquivalenceList( FOOTPRINT_EQUIVALENCE_LIST& aList,
  65. wxString* aErrorMessages )
  66. {
  67. char line[1024];
  68. int error_count = 0;
  69. FILE* file;
  70. wxFileName fn;
  71. wxString tmp, error_msg;
  72. SEARCH_STACK& search = Kiface().KifaceSearch();
  73. PROJECT_FILE& project = Prj().GetProjectFile();
  74. // Find equivalences in all available files, and populates the
  75. // equiv_List with all equivalences found in .equ files
  76. for( const wxString& equfile : project.m_EquivalenceFiles )
  77. {
  78. fn = wxExpandEnvVars( equfile );
  79. tmp = search.FindValidPath( fn.GetFullPath() );
  80. if( !tmp )
  81. {
  82. error_count++;
  83. if( aErrorMessages )
  84. {
  85. error_msg.Printf( _( "Equivalence file \"%s\" could not be found in the "
  86. "default search paths." ),
  87. fn.GetFullName() );
  88. if( ! aErrorMessages->IsEmpty() )
  89. *aErrorMessages << wxT("\n\n");
  90. *aErrorMessages += error_msg;
  91. }
  92. continue;
  93. }
  94. file = wxFopen( tmp, wxT( "rt" ) );
  95. if( file == NULL )
  96. {
  97. error_count++;
  98. if( aErrorMessages )
  99. {
  100. error_msg.Printf( _( "Error opening equivalence file \"%s\"." ), tmp );
  101. if( ! aErrorMessages->IsEmpty() )
  102. *aErrorMessages << wxT("\n\n");
  103. *aErrorMessages += error_msg;
  104. }
  105. continue;
  106. }
  107. while( GetLine( file, line, NULL, sizeof( line ) ) != NULL )
  108. {
  109. if( *line == 0 )
  110. continue;
  111. wxString wtext = FROM_UTF8( line );
  112. wxString value = GetQuotedText( wtext );
  113. if( value.IsEmpty() )
  114. continue;
  115. wxString footprint = GetQuotedText( wtext );
  116. if( footprint.IsEmpty() )
  117. continue;
  118. value.Replace( wxT( " " ), wxT( "_" ) );
  119. FOOTPRINT_EQUIVALENCE* equivItem = new FOOTPRINT_EQUIVALENCE();
  120. equivItem->m_ComponentValue = value;
  121. equivItem->m_FootprintFPID = footprint;
  122. aList.push_back( equivItem );
  123. }
  124. fclose( file );
  125. }
  126. return error_count;
  127. }
  128. void CVPCB_MAINFRAME::AutomaticFootprintMatching()
  129. {
  130. FOOTPRINT_EQUIVALENCE_LIST equivList;
  131. wxString msg;
  132. wxString error_msg;
  133. if( m_netlist.IsEmpty() )
  134. return;
  135. if( buildEquivalenceList( equivList, &error_msg ) )
  136. wxMessageBox( error_msg, _( "Equivalence File Load Error" ), wxOK | wxICON_WARNING, this );
  137. // Sort the association list by symbol value. When sorted, finding duplicate definitions
  138. // (i.e. 2 or more items having the same symbol value) is easier.
  139. std::sort( equivList.begin(), equivList.end(), sortListbyCmpValue );
  140. // Display the number of footprint/symbol equivalences.
  141. msg.Printf( _( "%lu footprint/symbol equivalences found." ), (unsigned long)equivList.size() );
  142. SetStatusText( msg, 0 );
  143. // Now, associate each free component with a footprint
  144. m_skipComponentSelect = true;
  145. error_msg.Empty();
  146. bool firstAssoc = true;
  147. for( unsigned kk = 0; kk < m_netlist.GetCount(); kk++ )
  148. {
  149. COMPONENT* component = m_netlist.GetComponent( kk );
  150. bool found = false;
  151. if( !component->GetFPID().empty() ) // the component has already a footprint
  152. continue;
  153. // Here a first attempt is made. We can have multiple equivItem of the same value.
  154. // When happens, using the footprint filter of components can remove the ambiguity by
  155. // filtering equivItem so one can use multiple equivList (for polar and non-polar caps
  156. // for example)
  157. wxString fpid_candidate;
  158. for( unsigned idx = 0; idx < equivList.size(); idx++ )
  159. {
  160. FOOTPRINT_EQUIVALENCE& equivItem = equivList[idx];
  161. if( equivItem.m_ComponentValue.CmpNoCase( component->GetValue() ) != 0 )
  162. continue;
  163. const FOOTPRINT_INFO *fp = m_FootprintsList->GetFootprintInfo( equivItem.m_FootprintFPID );
  164. bool equ_is_unique = true;
  165. unsigned next = idx+1;
  166. int previous = idx-1;
  167. if( next < equivList.size()
  168. && equivItem.m_ComponentValue == equivList[next].m_ComponentValue )
  169. {
  170. equ_is_unique = false;
  171. }
  172. if( previous >= 0
  173. && equivItem.m_ComponentValue == equivList[previous].m_ComponentValue )
  174. {
  175. equ_is_unique = false;
  176. }
  177. // If the equivalence is unique, no ambiguity: use the association
  178. if( fp && equ_is_unique )
  179. {
  180. AssociateFootprint( CVPCB_ASSOCIATION( kk, equivItem.m_FootprintFPID ),
  181. firstAssoc );
  182. firstAssoc = false;
  183. found = true;
  184. break;
  185. }
  186. // Store the first candidate found in list, when equivalence is not unique
  187. // We use it later.
  188. if( fp && fpid_candidate.IsEmpty() )
  189. fpid_candidate = equivItem.m_FootprintFPID;
  190. // The equivalence is not unique: use the footprint filter to try to remove
  191. // ambiguity
  192. // if the footprint filter does not remove ambiguity, we will use fpid_candidate
  193. if( fp )
  194. {
  195. size_t filtercount = component->GetFootprintFilters().GetCount();
  196. found = ( 0 == filtercount ); // if no entries, do not filter
  197. for( size_t jj = 0; jj < filtercount && !found; jj++ )
  198. found = fp->GetFootprintName().Matches( component->GetFootprintFilters()[jj] );
  199. }
  200. else
  201. {
  202. msg.Printf( _( "Component %s: footprint %s not found in any of the project "
  203. "footprint libraries." ),
  204. component->GetReference(), equivItem.m_FootprintFPID );
  205. if( ! error_msg.IsEmpty() )
  206. error_msg << wxT("\n\n");
  207. error_msg += msg;
  208. }
  209. if( found )
  210. {
  211. AssociateFootprint( CVPCB_ASSOCIATION( kk, equivItem.m_FootprintFPID ),
  212. firstAssoc );
  213. firstAssoc = false;
  214. break;
  215. }
  216. }
  217. if( found )
  218. {
  219. continue;
  220. }
  221. else if( !fpid_candidate.IsEmpty() )
  222. {
  223. AssociateFootprint( CVPCB_ASSOCIATION( kk, fpid_candidate ), firstAssoc );
  224. firstAssoc = false;
  225. continue;
  226. }
  227. // obviously the last chance: there's only one filter matching one footprint
  228. if( 1 == component->GetFootprintFilters().GetCount() )
  229. {
  230. // we do not need to analyze wildcards: single footprint do not
  231. // contain them and if there are wildcards it just will not match any
  232. if( m_FootprintsList->GetFootprintInfo( component->GetFootprintFilters()[0] ) )
  233. {
  234. AssociateFootprint( CVPCB_ASSOCIATION( kk, component->GetFootprintFilters()[0] ),
  235. firstAssoc );
  236. firstAssoc = false;
  237. }
  238. }
  239. }
  240. if( !error_msg.IsEmpty() )
  241. wxMessageBox( error_msg, _( "CvPcb Warning" ), wxOK | wxICON_WARNING, this );
  242. m_skipComponentSelect = false;
  243. m_symbolsListBox->Refresh();
  244. }