KiCad PCB EDA Suite
build_BOM_from_board.cpp
Go to the documentation of this file.
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-2020 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 
25 /* build_BOM_from_board.cpp */
26 
27 
28 #include <confirm.h>
29 #include <macros.h>
30 #include <kicad_string.h>
31 #include <pcb_edit_frame.h>
32 #include <project.h>
34 #include <footprint.h>
35 #include <wx/listimpl.cpp>
36 
37 /* creates a BOM list from board
38  * The format is:
39  * "Id";"Designator";"Package";"Number";"Designation";"Supplier and ref";
40  * 1;"P1";"DB25FC";1;"DB25FEMELLE";;;
41  * 2;"U9";"PGA120";1;"4003APG120";;;
42  * 3;"JP1";"pin_array_8x2";1;"CONN_8X2";;;
43  * 4;"RR1";"r_pack9";1;"9x1K";;;
44  * 5;"X1";"HC-18UH";1;"8MHz";;;
45  * 6;"U8";"24dip300";1;"EP600";;;
46  * 7;"U5";"32dip600";1;"628128";;;
47  * 8;"C2,C3";"C1";2;"47pF";;;
48  * 9;"U1";"20dip300";1;"74LS245";;;
49  * 10;"U3";"20dip300";1;"74LS541";;;
50  * 11;"U2";"20dip300";1;"74LS688";;;
51  * 12;"C1,C4,C5,C6";"CP6";4;"47uF";;;
52  */
53 
54 const wxString CsvFileExtension( wxT( "csv" ) ); // BOM file extension
55 
56 class BOM_ENTRY
57 {
58 public:
59  wxString m_Ref;
60  wxString m_Val;
62  int m_Id;
63  int m_Count;
64 };
65 WX_DECLARE_LIST( BOM_ENTRY, BOM_ENTRY_LIST );
66 
67 WX_DEFINE_LIST( BOM_ENTRY_LIST )
68 
69 
70 void PCB_EDIT_FRAME::RecreateBOMFileFromBoard( wxCommandEvent& aEvent )
71 {
72  wxFileName fn;
73  FILE* fp_bom;
74  wxString msg;
75 
76  if( GetBoard()->Footprints().empty() )
77  {
78  ShowInfoBarError( _( "Cannot export BOM: there are no footprints on the PCB." ) );
79  return;
80  }
81 
82  /* Set the file extension: */
83  fn = GetBoard()->GetFileName();
84  fn.SetExt( CsvFileExtension );
85 
86  wxString pro_dir = wxPathOnly( Prj().GetProjectFullName() );
87 
88  wxFileDialog dlg( this, _( "Save Bill of Materials" ), pro_dir, fn.GetFullName(),
89  CsvFileWildcard(), wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
90 
91  if( dlg.ShowModal() == wxID_CANCEL )
92  return;
93 
94  fn = dlg.GetPath();
95 
96  fp_bom = wxFopen( fn.GetFullPath(), wxT( "wt" ) );
97 
98  if( fp_bom == NULL )
99  {
100  msg.Printf( _( "Unable to create file \"%s\"" ), fn.GetFullPath() );
101  DisplayError( this, msg );
102  return;
103  }
104 
105  // Write header:
106  msg = wxT( "\"" );
107  msg << _( "Id" ) << wxT( "\";\"" );
108  msg << _( "Designator" ) << wxT( "\";\"" );
109  msg << _( "Package" ) << wxT( "\";\"" );
110  msg << _( "Quantity" ) << wxT( "\";\"" );
111  msg << _( "Designation" ) << wxT( "\";\"" );
112  msg << _( "Supplier and ref" ) << wxT( "\";\n" );
113  fprintf( fp_bom, "%s", TO_UTF8( msg ) );
114 
115  // Build list
116  BOM_ENTRY_LIST list;
117  int i = 1;
118 
119  for( FOOTPRINT* footprint : GetBoard()->Footprints() )
120  {
121  bool valExist = false;
122 
123  // try to find component in existing list
124  for( auto iter = list.begin(); iter != list.end(); ++iter )
125  {
126  BOM_ENTRY* curEntry = *iter;
127 
128  if( curEntry->m_Val == footprint->GetValue()
129  && curEntry->m_FPID == footprint->GetFPID() )
130  {
131  curEntry->m_Ref.Append( wxT( ", " ), 1 );
132  curEntry->m_Ref.Append( footprint->Reference().GetShownText() );
133  curEntry->m_Count++;
134 
135  valExist = true;
136  break;
137  }
138  }
139 
140  // If component does not exist yet, create new one and append it to the list.
141  if( valExist == false )
142  {
143  BOM_ENTRY* newEntry = new BOM_ENTRY();
144  newEntry->m_Id = i++;
145  newEntry->m_Val = footprint->Value().GetShownText();
146  newEntry->m_Ref = footprint->Reference().GetShownText();
147  newEntry->m_FPID = footprint->GetFPID();
148  newEntry->m_Count = 1;
149  list.Append( newEntry );
150  }
151  }
152 
153  // Print list. Also delete temporary created objects.
154  for( size_t ii = list.GetCount(); ii > 0; ii-- )
155  {
156  BOM_ENTRY* curEntry = *list.begin(); // Because the first object will be removed
157  // from list, all objects will be get here
158 
159  msg.Empty();
160 
161  msg << curEntry->m_Id << wxT( ";\"" );
162  msg << curEntry->m_Ref << wxT( "\";\"" );
163  msg << FROM_UTF8( curEntry->m_FPID.GetLibItemName().c_str() ) << wxT( "\";" );
164  msg << curEntry->m_Count << wxT( ";\"" );
165  msg << curEntry->m_Val << wxT( "\";;;\n" );
166  fprintf( fp_bom, "%s", TO_UTF8( msg ) );
167 
168  // We do not need this object, now: remove it from list and delete it
169  list.DeleteObject( curEntry );
170  delete (curEntry);
171  }
172 
173  fclose( fp_bom );
174 }
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:252
const UTF8 & GetLibItemName() const
Definition: lib_id.h:106
static wxString FROM_UTF8(const char *cstring)
Convert a UTF8 encoded C string to a wxString for all wxWidgets build modes.
Definition: macros.h:110
This file is part of the common library.
PROJECT & Prj()
Definition: kicad.cpp:349
int m_Count
wxString CsvFileWildcard()
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:51
const wxString CsvFileExtension(wxT("csv"))
const wxString & GetFileName() const
Definition: board.h:300
This file contains miscellaneous commonly used macros and functions.
const char * c_str() const
Definition: utf8.h:102
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: macros.h:96
LIB_ID m_FPID
#define NULL
Definition of file extensions used in Kicad.
BOARD * GetBoard()
int m_Id
#define _(s)
Definition: 3d_actions.cpp:33
The main frame for Pcbnew.
static bool empty(const wxTextEntryBase *aCtrl)
wxString m_Val
WX_DECLARE_LIST(BOM_ENTRY, BOM_ENTRY_LIST)
wxString m_Ref