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