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