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.

178 lines
5.5 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 (C) 1992-2021 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 <project.h>
  30. #include <wildcards_and_files_ext.h>
  31. #include <footprint.h>
  32. #include <wx/listimpl.cpp>
  33. #include <wx/filedlg.h>
  34. /* creates a BOM list from board
  35. * The format is:
  36. * "Id";"Designator";"Footprint";"Number";"Designation";"Supplier and ref";
  37. * 1;"P1";"DB25FC";1;"DB25FEMELLE";;;
  38. * 2;"U9";"PGA120";1;"4003APG120";;;
  39. * 3;"JP1";"pin_array_8x2";1;"CONN_8X2";;;
  40. * 4;"RR1";"r_pack9";1;"9x1K";;;
  41. * 5;"X1";"HC-18UH";1;"8MHz";;;
  42. * 6;"U8";"24dip300";1;"EP600";;;
  43. * 7;"U5";"32dip600";1;"628128";;;
  44. * 8;"C2,C3";"C1";2;"47pF";;;
  45. * 9;"U1";"20dip300";1;"74LS245";;;
  46. * 10;"U3";"20dip300";1;"74LS541";;;
  47. * 11;"U2";"20dip300";1;"74LS688";;;
  48. * 12;"C1,C4,C5,C6";"CP6";4;"47uF";;;
  49. */
  50. class BOM_ENTRY
  51. {
  52. public:
  53. wxString m_Ref;
  54. wxString m_Val;
  55. LIB_ID m_FPID;
  56. int m_Id;
  57. int m_Count;
  58. };
  59. WX_DECLARE_LIST( BOM_ENTRY, BOM_ENTRY_LIST );
  60. WX_DEFINE_LIST( BOM_ENTRY_LIST )
  61. void PCB_EDIT_FRAME::RecreateBOMFileFromBoard( wxCommandEvent& aEvent )
  62. {
  63. wxFileName fn;
  64. FILE* fp_bom;
  65. wxString msg;
  66. if( GetBoard()->Footprints().empty() )
  67. {
  68. ShowInfoBarError( _( "Cannot export BOM: there are no footprints on the PCB." ) );
  69. return;
  70. }
  71. /* Set the file extension: */
  72. fn = GetBoard()->GetFileName();
  73. fn.SetExt( CsvFileExtension );
  74. wxString pro_dir = wxPathOnly( Prj().GetProjectFullName() );
  75. wxFileDialog dlg( this, _( "Save Bill of Materials" ), pro_dir, fn.GetFullName(),
  76. CsvFileWildcard(), wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
  77. if( dlg.ShowModal() == wxID_CANCEL )
  78. return;
  79. fn = dlg.GetPath();
  80. fp_bom = wxFopen( fn.GetFullPath(), wxT( "wt" ) );
  81. if( fp_bom == nullptr )
  82. {
  83. msg.Printf( _( "Failed to create file '%s'." ), fn.GetFullPath() );
  84. DisplayError( this, msg );
  85. return;
  86. }
  87. // Write header:
  88. 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 : GetBoard()->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( auto iter = list.begin(); iter != list.end(); ++iter )
  106. {
  107. BOM_ENTRY* curEntry = *iter;
  108. if( curEntry->m_Val == footprint->GetValue()
  109. && curEntry->m_FPID == footprint->GetFPID() )
  110. {
  111. curEntry->m_Ref.Append( wxT( ", " ), 1 );
  112. curEntry->m_Ref.Append( footprint->Reference().GetShownText() );
  113. curEntry->m_Count++;
  114. valExist = true;
  115. break;
  116. }
  117. }
  118. // If component does not exist yet, create new one and append it to the list.
  119. if( valExist == false )
  120. {
  121. BOM_ENTRY* newEntry = new BOM_ENTRY();
  122. newEntry->m_Id = i++;
  123. newEntry->m_Val = footprint->Value().GetShownText();
  124. newEntry->m_Ref = footprint->Reference().GetShownText();
  125. newEntry->m_FPID = footprint->GetFPID();
  126. newEntry->m_Count = 1;
  127. list.Append( newEntry );
  128. }
  129. }
  130. // Print list. Also delete temporary created objects.
  131. for( size_t ii = list.GetCount(); ii > 0; ii-- )
  132. {
  133. BOM_ENTRY* curEntry = *list.begin(); // Because the first object will be removed
  134. // from list, all objects will be get here
  135. msg.Empty();
  136. msg << curEntry->m_Id << wxT( ";\"" );
  137. msg << curEntry->m_Ref << wxT( "\";\"" );
  138. msg << FROM_UTF8( curEntry->m_FPID.GetLibItemName().c_str() ) << wxT( "\";" );
  139. msg << curEntry->m_Count << wxT( ";\"" );
  140. msg << curEntry->m_Val << wxT( "\";;;\n" );
  141. fprintf( fp_bom, "%s", TO_UTF8( msg ) );
  142. // We do not need this object, now: remove it from list and delete it
  143. list.DeleteObject( curEntry );
  144. delete curEntry;
  145. }
  146. fclose( fp_bom );
  147. }