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.

174 lines
4.4 KiB

  1. /**********************/
  2. /* CVPCB: autosel.cpp */
  3. /**********************/
  4. /* Routines for automatic selection of modules. */
  5. #include "fctsys.h"
  6. #include "common.h"
  7. #include "confirm.h"
  8. #include "gestfich.h"
  9. #include "appl_wxstruct.h"
  10. #include "kicad_string.h"
  11. #include "cvpcb.h"
  12. #include "protos.h"
  13. #include "cvpcb_mainframe.h"
  14. #include "cvstruct.h"
  15. #define QUOTE '\''
  16. class FOOTPRINT_ALIAS
  17. {
  18. public:
  19. int m_Type;
  20. wxString m_Name;
  21. wxString m_FootprintName;
  22. FOOTPRINT_ALIAS() { m_Type = 0; }
  23. };
  24. typedef boost::ptr_vector< FOOTPRINT_ALIAS > FOOTPRINT_ALIAS_LIST;
  25. /*
  26. * read the string between quotes and put it in aTarget
  27. * put text in aTarget
  28. * return a pointer to the last read char (the second quote if Ok)
  29. */
  30. char * ReadQuotedText(wxString & aTarget, char * aText)
  31. {
  32. // search the first quote:
  33. for( ; *aText != 0; aText++ )
  34. {
  35. if( *aText == QUOTE )
  36. break;
  37. }
  38. if ( *aText == 0 )
  39. return NULL;
  40. aText++;
  41. for(; *aText != 0; aText++ )
  42. {
  43. if( *aText == QUOTE )
  44. break;
  45. aTarget.Append(*aText);
  46. }
  47. return aText;
  48. }
  49. /*
  50. * Called by the automatic association button
  51. * Read *.equ files to try to find corresponding footprint
  52. * for each component that is not already linked to a footprint ( a "free"
  53. * component )
  54. * format of a line:
  55. * 'cmp_ref' 'footprint_name'
  56. */
  57. void CVPCB_MAINFRAME::AssocieModule( wxCommandEvent& event )
  58. {
  59. FOOTPRINT_ALIAS_LIST aliases;
  60. FOOTPRINT_ALIAS* alias;
  61. wxFileName fn;
  62. wxString msg, tmp;
  63. char Line[1024];
  64. FILE* file;
  65. size_t ii;
  66. if( m_components.empty() )
  67. return;
  68. /* Find equivalents in all available files. */
  69. for( ii = 0; ii < m_AliasLibNames.GetCount(); ii++ )
  70. {
  71. fn = m_AliasLibNames[ii];
  72. fn.SetExt( FootprintAliasFileExtension );
  73. tmp = wxGetApp().FindLibraryPath( fn );
  74. if( !tmp )
  75. {
  76. msg.Printf( _( "Footprint alias library file <%s> could not be \
  77. found in the default search paths." ),
  78. GetChars( fn.GetFullName() ) );
  79. wxMessageBox( msg, titleLibLoadError, wxOK | wxICON_ERROR );
  80. continue;
  81. }
  82. file = wxFopen( tmp, wxT( "rt" ) );
  83. if( file == NULL )
  84. {
  85. msg.Printf( _( "Error opening alias library <%s>." ), GetChars( tmp ) );
  86. wxMessageBox( msg, titleLibLoadError, wxOK | wxICON_ERROR );
  87. continue;
  88. }
  89. while( GetLine( file, Line, NULL, sizeof(Line) ) != NULL )
  90. {
  91. char* text = Line;
  92. wxString value, footprint;
  93. text = ReadQuotedText( value, text );
  94. if( text == NULL || ( *text == 0 ) || value.IsEmpty() )
  95. continue;
  96. text++;
  97. text = ReadQuotedText( footprint, text );
  98. if( footprint.IsEmpty() )
  99. continue;
  100. alias = new FOOTPRINT_ALIAS();
  101. alias->m_Name = value;
  102. alias->m_FootprintName = footprint;
  103. aliases.push_back( alias );
  104. text++;
  105. }
  106. fclose( file );
  107. }
  108. /* Display the number of footprint aliases. */
  109. msg.Printf( _( "%d footprint aliases found." ), aliases.size() );
  110. SetStatusText( msg, 0 );
  111. ii = 0;
  112. BOOST_FOREACH( COMPONENT& component, m_components )
  113. {
  114. m_ListCmp->SetSelection( ii++, true );
  115. if( !component.m_Module.IsEmpty() )
  116. continue;
  117. BOOST_FOREACH( FOOTPRINT_ALIAS& alias, aliases )
  118. {
  119. if( alias.m_Name.CmpNoCase( component.m_Value ) != 0 )
  120. continue;
  121. BOOST_FOREACH( FOOTPRINT& footprint, m_footprints )
  122. {
  123. if( alias.m_FootprintName.CmpNoCase( footprint.m_Module ) == 0 )
  124. {
  125. SetNewPkg( footprint.m_Module );
  126. break;
  127. }
  128. }
  129. if( component.m_Module.IsEmpty() )
  130. {
  131. msg.Printf( _( "Component %s: footprint %s not found in \
  132. any of the project footprint libraries." ),
  133. GetChars( component.m_Reference ),
  134. GetChars( alias.m_FootprintName ) );
  135. wxMessageBox( msg, _( "CVPcb Error" ), wxOK | wxICON_ERROR,
  136. this );
  137. }
  138. }
  139. }
  140. }