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.

135 lines
3.8 KiB

  1. /***************/
  2. /* loadcmp.cpp */
  3. /***************/
  4. #include "fctsys.h"
  5. #include "wxstruct.h"
  6. #include "common.h"
  7. #include "confirm.h"
  8. #include "kicad_string.h"
  9. #include "gestfich.h"
  10. #include "macros.h"
  11. #include "appl_wxstruct.h"
  12. #include "cvpcb.h"
  13. #include "protos.h"
  14. #include "cvstruct.h"
  15. #include "class_DisplayFootprintsFrame.h"
  16. /**
  17. * Analyze the libraries to find the module.
  18. * If this module is found, copy it into memory, and
  19. * string end of the list of modules.
  20. *
  21. * @param CmpName - Module name
  22. * @return - Module if found otherwise NULL.
  23. */
  24. MODULE* DISPLAY_FOOTPRINTS_FRAME::Get_Module( const wxString& CmpName )
  25. {
  26. int LineNum, Found = 0;
  27. unsigned ii;
  28. char Line[1024], Name[255];
  29. wxString tmp, msg;
  30. wxFileName fn;
  31. MODULE* Module = NULL;
  32. WinEDA_CvpcbFrame* parent = ( WinEDA_CvpcbFrame* ) GetParent();
  33. for( ii = 0; ii < parent->m_ModuleLibNames.GetCount(); ii++ )
  34. {
  35. fn = parent->m_ModuleLibNames[ii];
  36. fn.SetExt( ModuleFileExtension );
  37. tmp = wxGetApp().FindLibraryPath( fn );
  38. if( !tmp )
  39. {
  40. msg.Printf( _( "PCB foot print library file <%s> could not be \
  41. found in the default search paths." ),
  42. GetChars( fn.GetFullName() ) );
  43. wxMessageBox( msg, titleLibLoadError, wxOK | wxICON_ERROR, this );
  44. continue;
  45. }
  46. FILE* file = wxFopen( tmp, wxT( "rt" ) );
  47. if( file == NULL )
  48. {
  49. msg.Printf( _( "Could not open PCB foot print library file <%s>." ),
  50. GetChars( tmp ) );
  51. wxMessageBox( msg, titleLibLoadError, wxOK | wxICON_ERROR, this );
  52. continue;
  53. }
  54. /* Read header. */
  55. LineNum = 0;
  56. GetLine( file, Line, &LineNum );
  57. StrPurge( Line );
  58. if( strnicmp( Line, ENTETE_LIBRAIRIE, L_ENTETE_LIB ) != 0 )
  59. {
  60. msg.Printf( _( "<%s> is not a valid Kicad PCB foot print library." ),
  61. GetChars( tmp ) );
  62. wxMessageBox( msg, titleLibLoadError, wxOK | wxICON_ERROR, this );
  63. fclose( file );
  64. return NULL;
  65. }
  66. Found = 0;
  67. while( !Found && GetLine( file, Line, &LineNum ) )
  68. {
  69. if( strncmp( Line, "$MODULE", 6 ) == 0 )
  70. break;
  71. if( strnicmp( Line, "$INDEX", 6 ) == 0 )
  72. {
  73. while( GetLine( file, Line, &LineNum ) )
  74. {
  75. if( strnicmp( Line, "$EndINDEX", 9 ) == 0 )
  76. break;
  77. StrPurge( Line );
  78. if( stricmp( Line, CONV_TO_UTF8( CmpName ) ) == 0 )
  79. {
  80. Found = 1;
  81. break;
  82. }
  83. }
  84. }
  85. }
  86. while( Found && GetLine( file, Line, &LineNum ) )
  87. {
  88. if( Line[0] != '$' )
  89. continue;
  90. if( Line[1] != 'M' )
  91. continue;
  92. if( strnicmp( Line, "$MODULE", 7 ) != 0 )
  93. continue;
  94. /* Read component name. */
  95. sscanf( Line + 7, " %s", Name );
  96. if( stricmp( Name, CONV_TO_UTF8( CmpName ) ) == 0 )
  97. {
  98. Module = new MODULE( GetBoard() );
  99. // Switch the locale to standard C (needed to print floating
  100. // point numbers like 1.3)
  101. SetLocaleTo_C_standard();
  102. Module->ReadDescr( file, &LineNum );
  103. SetLocaleTo_Default(); // revert to the current locale
  104. Module->SetPosition( wxPoint( 0, 0 ) );
  105. fclose( file );
  106. return Module;
  107. }
  108. }
  109. fclose( file );
  110. file = NULL;
  111. }
  112. msg.Printf( _( "Module %s not found" ), CmpName.GetData() );
  113. DisplayError( this, msg );
  114. return NULL;
  115. }