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.

169 lines
5.1 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2009-2014 Jean-Pierre Charras, jean-pierre.charras@ujf-grenoble.fr
  5. * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. /* build_BOM_from_board.cpp */
  25. #include <confirm.h>
  26. #include <macros.h>
  27. #include <string_utils.h>
  28. #include <pcb_edit_frame.h>
  29. #include <board.h>
  30. #include <project.h>
  31. #include <wildcards_and_files_ext.h>
  32. #include <footprint.h>
  33. #include <tools/board_editor_control.h>
  34. #include <wx/filedlg.h>
  35. #include <vector>
  36. /* creates a BOM list from board
  37. * The format is:
  38. * "Id";"Designator";"Footprint";"Number";"Designation";"Supplier and ref";
  39. * 1;"P1";"DB25FC";1;"DB25FEMELLE";;;
  40. * 2;"U9";"PGA120";1;"4003APG120";;;
  41. * 3;"JP1";"pin_array_8x2";1;"CONN_8X2";;;
  42. * 4;"RR1";"r_pack9";1;"9x1K";;;
  43. * 5;"X1";"HC-18UH";1;"8MHz";;;
  44. * 6;"U8";"24dip300";1;"EP600";;;
  45. * 7;"U5";"32dip600";1;"628128";;;
  46. * 8;"C2,C3";"C1";2;"47pF";;;
  47. * 9;"U1";"20dip300";1;"74LS245";;;
  48. * 10;"U3";"20dip300";1;"74LS541";;;
  49. * 11;"U2";"20dip300";1;"74LS688";;;
  50. * 12;"C1,C4,C5,C6";"CP6";4;"47uF";;;
  51. */
  52. class BOM_ENTRY
  53. {
  54. public:
  55. wxString m_Ref;
  56. wxString m_Val;
  57. LIB_ID m_FPID;
  58. int m_Id;
  59. int m_Count;
  60. };
  61. using BOM_ENTRY_LIST = std::vector<BOM_ENTRY>;
  62. int BOARD_EDITOR_CONTROL::GenBOMFileFromBoard( const TOOL_EVENT& aEvent )
  63. {
  64. BOARD* board = m_frame->GetBoard();
  65. wxFileName fn;
  66. FILE* fp_bom;
  67. if( board->Footprints().empty() )
  68. {
  69. m_frame->ShowInfoBarError( _( "Cannot export BOM: there are no footprints on the PCB." ) );
  70. return 0;
  71. }
  72. /* Set the file extension: */
  73. fn = board->GetFileName();
  74. fn.SetExt( FILEEXT::CsvFileExtension );
  75. wxString pro_dir = wxPathOnly( m_frame->Prj().GetProjectFullName() );
  76. wxFileDialog dlg( m_frame, _( "Save Bill of Materials" ), pro_dir, fn.GetFullName(),
  77. FILEEXT::CsvFileWildcard(), wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
  78. if( dlg.ShowModal() == wxID_CANCEL )
  79. return 0;
  80. fn = dlg.GetPath();
  81. fp_bom = wxFopen( fn.GetFullPath(), wxT( "wt" ) );
  82. if( fp_bom == nullptr )
  83. {
  84. DisplayError( m_frame, wxString::Format( _( "Failed to create file '%s'." ), fn.GetFullPath() ) );
  85. return 0;
  86. }
  87. // Write header:
  88. wxString msg = wxT( "\"" );
  89. msg << _( "Id" ) << wxT( "\";\"" );
  90. msg << _( "Designator" ) << wxT( "\";\"" );
  91. msg << _( "Footprint" ) << wxT( "\";\"" );
  92. msg << _( "Quantity" ) << wxT( "\";\"" );
  93. msg << _( "Designation" ) << wxT( "\";\"" );
  94. msg << _( "Supplier and ref" ) << wxT( "\";\n" );
  95. fprintf( fp_bom, "%s", TO_UTF8( msg ) );
  96. // Build list
  97. BOM_ENTRY_LIST list;
  98. int i = 1;
  99. for( FOOTPRINT* footprint : board->Footprints() )
  100. {
  101. if( footprint->GetAttributes() & FP_EXCLUDE_FROM_BOM )
  102. continue;
  103. bool valExist = false;
  104. // try to find component in existing list
  105. for( BOM_ENTRY& curEntry : list )
  106. {
  107. if( curEntry.m_Val == footprint->GetValue() && curEntry.m_FPID == footprint->GetFPID() )
  108. {
  109. curEntry.m_Ref.Append( wxT( ", " ), 1 );
  110. curEntry.m_Ref.Append( footprint->Reference().GetShownText( false ) );
  111. curEntry.m_Count++;
  112. valExist = true;
  113. break;
  114. }
  115. }
  116. // If component does not exist yet, create new one and append it to the list.
  117. if( !valExist )
  118. {
  119. list.emplace_back();
  120. BOM_ENTRY& newEntry = list.back();
  121. newEntry.m_Id = i++;
  122. newEntry.m_Val = footprint->Value().GetShownText( false );
  123. newEntry.m_Ref = footprint->Reference().GetShownText( false );
  124. newEntry.m_FPID = footprint->GetFPID();
  125. newEntry.m_Count = 1;
  126. }
  127. }
  128. // Print list.
  129. for( const BOM_ENTRY& curEntry : list )
  130. {
  131. msg.Empty();
  132. msg << curEntry.m_Id << wxT( ";\"" );
  133. msg << curEntry.m_Ref << wxT( "\";\"" );
  134. msg << From_UTF8( curEntry.m_FPID.GetLibItemName().c_str() ) << wxT( "\";" );
  135. msg << curEntry.m_Count << wxT( ";\"" );
  136. msg << curEntry.m_Val << wxT( "\";;;\n" );
  137. fprintf( fp_bom, "%s", TO_UTF8( msg ) );
  138. }
  139. fclose( fp_bom );
  140. return 0;
  141. }