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.

161 lines
4.2 KiB

15 years ago
  1. /* build_BOM_from_board.cpp */
  2. #include <fctsys.h>
  3. #include <confirm.h>
  4. #include <kicad_string.h>
  5. #include <gestfich.h>
  6. #include <pcbnew.h>
  7. #include <wxPcbStruct.h>
  8. #include <macros.h>
  9. #include <project.h>
  10. #include <class_board.h>
  11. #include <class_module.h>
  12. #include <wx/listimpl.cpp>
  13. /* creates a BOM list rom board
  14. * The format is:
  15. * "Id";"Designator";"Package";"Number";"Designation";"Supplier and ref";
  16. * 1;"P1";"DB25FC";1;"DB25FEMELLE";;;
  17. * 2;"U9";"PGA120";1;"4003APG120";;;
  18. * 3;"JP1";"pin_array_8x2";1;"CONN_8X2";;;
  19. * 4;"RR1";"r_pack9";1;"9x1K";;;
  20. * 5;"X1";"HC-18UH";1;"8MHz";;;
  21. * 6;"U8";"24dip300";1;"EP600";;;
  22. * 7;"U5";"32dip600";1;"628128";;;
  23. * 8;"C2,C3";"C1";2;"47pF";;;
  24. * 9;"U1";"20dip300";1;"74LS245";;;
  25. * 10;"U3";"20dip300";1;"74LS541";;;
  26. * 11;"U2";"20dip300";1;"74LS688";;;
  27. * 12;"C1,C4,C5,C6";"CP6";4;"47uF";;;
  28. */
  29. const wxString CsvFileExtension( wxT( "csv" ) ); // BOM file extension
  30. const wxString CsvFileWildcard( _( "Comma separated value files (*.csv)|*.csv" ) );
  31. class cmp
  32. {
  33. public:
  34. wxString m_Ref;
  35. wxString m_Val;
  36. FPID m_fpid;
  37. int m_Id;
  38. int m_CmpCount;
  39. };
  40. WX_DECLARE_LIST( cmp, CmpList );
  41. WX_DEFINE_LIST( CmpList )
  42. void PCB_EDIT_FRAME::RecreateBOMFileFromBoard( wxCommandEvent& aEvent )
  43. {
  44. wxFileName fn;
  45. FILE* fp_bom;
  46. MODULE* module = GetBoard()->m_Modules;
  47. wxString msg;
  48. if( module == NULL )
  49. {
  50. DisplayError( this, _( "No Modules!" ) );
  51. return;
  52. }
  53. /* Set the file extension: */
  54. fn = GetBoard()->GetFileName();
  55. fn.SetExt( CsvFileExtension );
  56. wxString pro_dir = wxPathOnly( Prj().GetProjectFullName() );
  57. wxFileDialog dlg( this, _( "Save Bill of Materials" ), pro_dir,
  58. fn.GetFullName(), CsvFileWildcard,
  59. wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
  60. if( dlg.ShowModal() == wxID_CANCEL )
  61. return;
  62. fn = dlg.GetPath();
  63. fp_bom = wxFopen( fn.GetFullPath(), wxT( "wt" ) );
  64. if( fp_bom == NULL )
  65. {
  66. msg.Printf( _( "Unable to create file <%s>" ), GetChars( fn.GetFullPath() ) );
  67. DisplayError( this, msg );
  68. return;
  69. }
  70. // Write header:
  71. msg = wxT( "\"" );
  72. msg << _( "Id" ) << wxT( "\";\"" );
  73. msg << _( "Designator" ) << wxT( "\";\"" );
  74. msg << _( "Package" ) << wxT( "\";\"" );
  75. msg << _( "Quantity" ) << wxT( "\";\"" );
  76. msg << _( "Designation" ) << wxT( "\";\"" );
  77. msg << _( "Supplier and ref" ) << wxT( "\";\n" );
  78. fprintf( fp_bom, "%s", TO_UTF8( msg ) );
  79. // Build list
  80. CmpList list;
  81. cmp* comp = NULL;
  82. CmpList::iterator iter;
  83. int i = 1;
  84. while( module != NULL )
  85. {
  86. bool valExist = false;
  87. // try to find component in existing list
  88. for( iter = list.begin(); iter != list.end(); iter++ )
  89. {
  90. cmp* current = *iter;
  91. if( (current->m_Val == module->GetValue()) && (current->m_fpid == module->GetFPID()) )
  92. {
  93. current->m_Ref.Append( wxT( ", " ), 1 );
  94. current->m_Ref.Append( module->GetReference() );
  95. current->m_CmpCount++;
  96. valExist = true;
  97. break;
  98. }
  99. }
  100. // If component does not exist yet, create new one and append it to the list.
  101. if( valExist == false )
  102. {
  103. comp = new cmp();
  104. comp->m_Id = i++;
  105. comp->m_Val = module->GetValue();
  106. comp->m_Ref = module->GetReference();
  107. comp->m_fpid = module->GetFPID();
  108. comp->m_CmpCount = 1;
  109. list.Append( comp );
  110. }
  111. // increment module
  112. module = module->Next();
  113. }
  114. // Print list
  115. for( iter = list.begin(); iter != list.end(); iter++ )
  116. {
  117. cmp* current = *iter;
  118. msg.Empty();
  119. msg << current->m_Id << wxT( ";\"" );
  120. msg << current->m_Ref << wxT( "\";\"" );
  121. msg << FROM_UTF8( current->m_fpid.Format().c_str() ) << wxT( "\";" );
  122. msg << current->m_CmpCount << wxT( ";\"" );
  123. msg << current->m_Val << wxT( "\";;;\n" );
  124. fprintf( fp_bom, "%s", TO_UTF8( msg ) );
  125. list.DeleteObject( current );
  126. delete (current);
  127. }
  128. fclose( fp_bom );
  129. }