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.

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