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