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.

304 lines
9.6 KiB

* KIWAY Milestone A): Make major modules into DLL/DSOs. ! The initial testing of this commit should be done using a Debug build so that all the wxASSERT()s are enabled. Also, be sure and keep enabled the USE_KIWAY_DLLs option. The tree won't likely build without it. Turning it off is senseless anyways. If you want stable code, go back to a prior version, the one tagged with "stable". * Relocate all functionality out of the wxApp derivative into more finely targeted purposes: a) DLL/DSO specific b) PROJECT specific c) EXE or process specific d) configuration file specific data e) configuration file manipulations functions. All of this functionality was blended into an extremely large wxApp derivative and that was incompatible with the desire to support multiple concurrently loaded DLL/DSO's ("KIFACE")s and multiple concurrently open projects. An amazing amount of organization come from simply sorting each bit of functionality into the proper box. * Switch to wxConfigBase from wxConfig everywhere except instantiation. * Add classes KIWAY, KIFACE, KIFACE_I, SEARCH_STACK, PGM_BASE, PGM_KICAD, PGM_SINGLE_TOP, * Remove "Return" prefix on many function names. * Remove obvious comments from CMakeLists.txt files, and from else() and endif()s. * Fix building boost for use in a DSO on linux. * Remove some of the assumptions in the CMakeLists.txt files that windows had to be the host platform when building windows binaries. * Reduce the number of wxStrings being constructed at program load time via static construction. * Pass wxConfigBase* to all SaveSettings() and LoadSettings() functions so that these functions are useful even when the wxConfigBase comes from another source, as is the case in the KICAD_MANAGER_FRAME. * Move the setting of the KIPRJMOD environment variable into class PROJECT, so that it can be moved into a project variable soon, and out of FP_LIB_TABLE. * Add the KIWAY_PLAYER which is associated with a particular PROJECT, and all its child wxFrames and wxDialogs now have a Kiway() member function which returns a KIWAY& that that window tree branch is in support of. This is like wxWindows DNA in that child windows get this member with proper value at time of construction. * Anticipate some of the needs for milestones B) and C) and make code adjustments now in an effort to reduce work in those milestones. * No testing has been done for python scripting, since milestone C) has that being largely reworked and re-thought-out.
12 years ago
* KIWAY Milestone A): Make major modules into DLL/DSOs. ! The initial testing of this commit should be done using a Debug build so that all the wxASSERT()s are enabled. Also, be sure and keep enabled the USE_KIWAY_DLLs option. The tree won't likely build without it. Turning it off is senseless anyways. If you want stable code, go back to a prior version, the one tagged with "stable". * Relocate all functionality out of the wxApp derivative into more finely targeted purposes: a) DLL/DSO specific b) PROJECT specific c) EXE or process specific d) configuration file specific data e) configuration file manipulations functions. All of this functionality was blended into an extremely large wxApp derivative and that was incompatible with the desire to support multiple concurrently loaded DLL/DSO's ("KIFACE")s and multiple concurrently open projects. An amazing amount of organization come from simply sorting each bit of functionality into the proper box. * Switch to wxConfigBase from wxConfig everywhere except instantiation. * Add classes KIWAY, KIFACE, KIFACE_I, SEARCH_STACK, PGM_BASE, PGM_KICAD, PGM_SINGLE_TOP, * Remove "Return" prefix on many function names. * Remove obvious comments from CMakeLists.txt files, and from else() and endif()s. * Fix building boost for use in a DSO on linux. * Remove some of the assumptions in the CMakeLists.txt files that windows had to be the host platform when building windows binaries. * Reduce the number of wxStrings being constructed at program load time via static construction. * Pass wxConfigBase* to all SaveSettings() and LoadSettings() functions so that these functions are useful even when the wxConfigBase comes from another source, as is the case in the KICAD_MANAGER_FRAME. * Move the setting of the KIPRJMOD environment variable into class PROJECT, so that it can be moved into a project variable soon, and out of FP_LIB_TABLE. * Add the KIWAY_PLAYER which is associated with a particular PROJECT, and all its child wxFrames and wxDialogs now have a Kiway() member function which returns a KIWAY& that that window tree branch is in support of. This is like wxWindows DNA in that child windows get this member with proper value at time of construction. * Anticipate some of the needs for milestones B) and C) and make code adjustments now in an effort to reduce work in those milestones. * No testing has been done for python scripting, since milestone C) has that being largely reworked and re-thought-out.
12 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 <fctsys.h>
  31. #include <common.h>
  32. #include <kiface_i.h>
  33. #include <project.h>
  34. #include <gestfich.h>
  35. #include <pgm_base.h>
  36. #include <kicad_string.h>
  37. #include <macros.h>
  38. #include <cvpcb.h>
  39. #include <cvpcb_mainframe.h>
  40. #include <listboxes.h>
  41. #include <auto_associate.h>
  42. #define QUOTE '\''
  43. /*
  44. * read the string between quotes and put it in aTarget
  45. * put text in aTarget
  46. * return a pointer to the last read char (the second quote if OK)
  47. */
  48. wxString GetQuotedText( wxString& text )
  49. {
  50. int i = text.Find( QUOTE );
  51. if( wxNOT_FOUND == i )
  52. return wxT( "" );
  53. wxString shrt = text.Mid( i + 1 );
  54. i = shrt.Find( QUOTE );
  55. if( wxNOT_FOUND == i )
  56. return wxT( "" );
  57. text = shrt.Mid( i + 1 );
  58. return shrt.Mid( 0, i );
  59. }
  60. // A sort compare function, used to sort a FOOTPRINT_EQUIVALENCE_LIST by cmp values
  61. // (m_ComponentValue member)
  62. bool sortListbyCmpValue( const FOOTPRINT_EQUIVALENCE& ref, const FOOTPRINT_EQUIVALENCE& test )
  63. {
  64. return ref.m_ComponentValue.Cmp( test.m_ComponentValue ) >= 0;
  65. }
  66. // read the .equ files and populate the list of equivalents
  67. int CVPCB_MAINFRAME::buildEquivalenceList( FOOTPRINT_EQUIVALENCE_LIST& aList, wxString * aErrorMessages )
  68. {
  69. char line[1024];
  70. int error_count = 0;
  71. FILE* file;
  72. wxFileName fn;
  73. wxString tmp, error_msg;
  74. SEARCH_STACK& search = Kiface().KifaceSearch();
  75. // Find equivalences in all available files, and populates the
  76. // equiv_List with all equivalences found in .equ files
  77. for( unsigned ii = 0; ii < m_EquFilesNames.GetCount(); ii++ )
  78. {
  79. fn = wxExpandEnvVars( m_EquFilesNames[ii] );
  80. tmp = search.FindValidPath( fn.GetFullPath() );
  81. if( !tmp )
  82. {
  83. error_count++;
  84. if( aErrorMessages )
  85. {
  86. error_msg.Printf( _( "Equivalence file \"%s\" could not be found in the "
  87. "default search paths." ),
  88. GetChars( fn.GetFullName() ) );
  89. if( ! aErrorMessages->IsEmpty() )
  90. *aErrorMessages << wxT("\n\n");
  91. *aErrorMessages += error_msg;
  92. }
  93. continue;
  94. }
  95. file = wxFopen( tmp, wxT( "rt" ) );
  96. if( file == NULL )
  97. {
  98. error_count++;
  99. if( aErrorMessages )
  100. {
  101. error_msg.Printf( _( "Error opening equivalence file \"%s\"." ), GetChars( tmp ) );
  102. if( ! aErrorMessages->IsEmpty() )
  103. *aErrorMessages << wxT("\n\n");
  104. *aErrorMessages += error_msg;
  105. }
  106. continue;
  107. }
  108. while( GetLine( file, line, NULL, sizeof( line ) ) != NULL )
  109. {
  110. if( *line == 0 )
  111. continue;
  112. wxString wtext = FROM_UTF8( line );
  113. wxString value = GetQuotedText( wtext );
  114. if( value.IsEmpty() )
  115. continue;
  116. wxString footprint = GetQuotedText( wtext );
  117. if( footprint.IsEmpty() )
  118. continue;
  119. value.Replace( wxT( " " ), wxT( "_" ) );
  120. FOOTPRINT_EQUIVALENCE* equivItem = new FOOTPRINT_EQUIVALENCE();
  121. equivItem->m_ComponentValue = value;
  122. equivItem->m_FootprintFPID = footprint;
  123. aList.push_back( equivItem );
  124. }
  125. fclose( file );
  126. }
  127. return error_count;
  128. }
  129. void CVPCB_MAINFRAME::AutomaticFootprintMatching( wxCommandEvent& event )
  130. {
  131. FOOTPRINT_EQUIVALENCE_LIST equiv_List;
  132. wxString msg, error_msg;
  133. if( m_netlist.IsEmpty() )
  134. return;
  135. if( buildEquivalenceList( equiv_List, &error_msg ) )
  136. wxMessageBox( error_msg, _( "Equivalence File Load Error" ), wxOK | wxICON_WARNING, this );
  137. // Sort the association list by component value.
  138. // When sorted, find duplicate definitions (i.e. 2 or more items
  139. // having the same component value) is more easy.
  140. std::sort( equiv_List.begin(), equiv_List.end(), sortListbyCmpValue );
  141. // Display the number of footprint/component equivalences.
  142. msg.Printf( _( "%lu footprint/cmp equivalences found." ), (unsigned long)equiv_List.size() );
  143. SetStatusText( msg, 0 );
  144. // Now, associate each free component with a footprint, when the association
  145. // is found in list
  146. m_skipComponentSelect = true;
  147. error_msg.Empty();
  148. for( unsigned kk = 0; kk < m_netlist.GetCount(); kk++ )
  149. {
  150. COMPONENT* component = m_netlist.GetComponent( kk );
  151. bool found = false;
  152. if( !component->GetFPID().empty() ) // the component has already a footprint
  153. continue;
  154. // Here a first attempt is made. We can have multiple equivItem of the same value.
  155. // When happens, using the footprint filter of components can remove the ambiguity by
  156. // filtering equivItem so one can use multiple equiv_List (for polar and
  157. // non-polar caps for example)
  158. wxString fpid_candidate;
  159. for( unsigned idx = 0; idx < equiv_List.size(); idx++ )
  160. {
  161. FOOTPRINT_EQUIVALENCE& equivItem = equiv_List[idx];
  162. if( equivItem.m_ComponentValue.CmpNoCase( component->GetValue() ) != 0 )
  163. continue;
  164. const FOOTPRINT_INFO *module = m_FootprintsList->GetModuleInfo( equivItem.m_FootprintFPID );
  165. bool equ_is_unique = true;
  166. unsigned next = idx+1;
  167. int previous = idx-1;
  168. if( next < equiv_List.size() &&
  169. equivItem.m_ComponentValue == equiv_List[next].m_ComponentValue )
  170. equ_is_unique = false;
  171. if( previous >= 0 &&
  172. equivItem.m_ComponentValue == equiv_List[previous].m_ComponentValue )
  173. equ_is_unique = false;
  174. // If the equivalence is unique, no ambiguity: use the association
  175. if( module && equ_is_unique )
  176. {
  177. SetNewPkg( equivItem.m_FootprintFPID, kk );
  178. found = true;
  179. break;
  180. }
  181. // Store the first candidate found in list, when equivalence is not unique
  182. // We use it later.
  183. if( module && fpid_candidate.IsEmpty() )
  184. fpid_candidate = equivItem.m_FootprintFPID;
  185. // The equivalence is not unique: use the footprint filter to try to remove
  186. // ambiguity
  187. // if the footprint filter does not remove ambiguity, we will use fpid_candidate
  188. if( module )
  189. {
  190. size_t filtercount = component->GetFootprintFilters().GetCount();
  191. found = ( 0 == filtercount ); // if no entries, do not filter
  192. for( size_t jj = 0; jj < filtercount && !found; jj++ )
  193. {
  194. found = module->GetFootprintName().Matches( component->GetFootprintFilters()[jj] );
  195. }
  196. }
  197. else
  198. {
  199. msg.Printf( _( "Component %s: footprint %s not found in any of the project "
  200. "footprint libraries." ),
  201. GetChars( component->GetReference() ),
  202. GetChars( equivItem.m_FootprintFPID ) );
  203. if( ! error_msg.IsEmpty() )
  204. error_msg << wxT("\n\n");
  205. error_msg += msg;
  206. }
  207. if( found )
  208. {
  209. SetNewPkg( equivItem.m_FootprintFPID, kk );
  210. break;
  211. }
  212. }
  213. if( found )
  214. continue;
  215. else if( !fpid_candidate.IsEmpty() )
  216. {
  217. SetNewPkg( fpid_candidate, kk );
  218. continue;
  219. }
  220. // obviously the last chance: there's only one filter matching one footprint
  221. if( 1 == component->GetFootprintFilters().GetCount() )
  222. {
  223. // we do not need to analyze wildcards: single footprint do not
  224. // contain them and if there are wildcards it just will not match any
  225. const FOOTPRINT_INFO* module = m_FootprintsList->GetModuleInfo( component->GetFootprintFilters()[0] );
  226. if( module )
  227. {
  228. SetNewPkg( component->GetFootprintFilters()[0], kk );
  229. }
  230. }
  231. }
  232. if( !error_msg.IsEmpty() )
  233. wxMessageBox( error_msg, _( "CvPcb Warning" ), wxOK | wxICON_WARNING, this );
  234. m_skipComponentSelect = false;
  235. m_compListBox->Refresh();
  236. }