KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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, [email protected]
5 * Copyright The 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 <string_utils.h>
31#include <pcb_edit_frame.h>
32#include <board.h>
33#include <project.h>
35#include <footprint.h>
37#include <wx/filedlg.h>
38#include <vector>
39#include <kiplatform/ui.h>
40
41
42/* creates a BOM list from board
43 * The format is:
44 * "Id";"Designator";"Footprint";"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
60{
61public:
62 std::vector<wxString> m_Refs;
63 wxString m_Val;
66};
67
68
70{
71 BOARD* board = m_frame->GetBoard();
72 wxFileName fn;
73 FILE* fp_bom;
74
75 if( board->Footprints().empty() )
76 {
77 m_frame->ShowInfoBarError( _( "Cannot export BOM: there are no footprints on the PCB." ) );
78 return 0;
79 }
80
81 /* Set the file extension: */
82 fn = board->GetFileName();
83 fn.SetExt( FILEEXT::CsvFileExtension );
84
85 wxString pro_dir = wxPathOnly( m_frame->Prj().GetProjectFullName() );
86
87 wxFileDialog dlg( m_frame, _( "Save Bill of Materials" ), pro_dir, fn.GetFullName(),
88 FILEEXT::CsvFileWildcard(), wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
89
91
92 if( dlg.ShowModal() == wxID_CANCEL )
93 return 0;
94
95 fn = dlg.GetPath();
96
97 fp_bom = wxFopen( fn.GetFullPath(), wxT( "wt" ) );
98
99 if( fp_bom == nullptr )
100 {
101 DisplayError( m_frame, wxString::Format( _( "Failed to create file '%s'." ), fn.GetFullPath() ) );
102 return 0;
103 }
104
105 // Write header:
106 wxString msg = wxT( "\"" );
107 msg << _( "Id" ) << wxT( "\";\"" );
108 msg << _( "Designator" ) << wxT( "\";\"" );
109 msg << _( "Footprint" ) << 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 std::vector<BOM_ENTRY> list;
117
118 for( FOOTPRINT* footprint : board->Footprints() )
119 {
120 if( footprint->GetAttributes() & FP_EXCLUDE_FROM_BOM )
121 continue;
122
123 bool valExist = false;
124
125 // try to find component in existing list
126 for( BOM_ENTRY& curEntry : list )
127 {
128 if( curEntry.m_Val == footprint->GetValue() && curEntry.m_FPID == footprint->GetFPID() )
129 {
130 curEntry.m_Refs.emplace_back( footprint->Reference().GetShownText( false ) );
131 curEntry.m_Count++;
132
133 valExist = true;
134 break;
135 }
136 }
137
138 // If component does not exist yet, create new one and append it to the list.
139 if( !valExist )
140 {
141 list.emplace_back();
142 BOM_ENTRY& newEntry = list.back();
143 newEntry.m_Val = footprint->Value().GetShownText( false );
144 newEntry.m_Refs.emplace_back( footprint->Reference().GetShownText( false ) );
145 newEntry.m_FPID = footprint->GetFPID();
146 newEntry.m_Count = 1;
147 }
148 }
149
150 for( BOM_ENTRY& curEntry : list )
151 {
152 std::sort( curEntry.m_Refs.begin(), curEntry.m_Refs.end(),
153 []( const wxString& lhs, const wxString& rhs )
154 {
155 return StrNumCmp( lhs, rhs, true /* ignore case */ ) < 0;
156 } );
157 }
158
159 std::sort( list.begin(), list.end(),
160 []( const BOM_ENTRY& lhs, const BOM_ENTRY& rhs )
161 {
162 return StrNumCmp( lhs.m_Refs[0], rhs.m_Refs[0], true /* ignore case */ ) < 0;
163 } );
164
165 // Print list.
166 int id = 1;
167
168 for( const BOM_ENTRY& curEntry : list )
169 {
170 msg.Empty();
171
172 msg << id++ << wxT( ";\"" );
173
174 msg << curEntry.m_Refs[0];
175
176 for( int ii = 1; ii < (int) curEntry.m_Refs.size(); ++ii )
177 msg << wxT( ", " ) << curEntry.m_Refs[ii];
178
179 msg << wxT( "\";\"" );
180
181 msg << From_UTF8( curEntry.m_FPID.GetLibItemName().c_str() ) << wxT( "\";" );
182 msg << curEntry.m_Count << wxT( ";\"" );
183 msg << curEntry.m_Val << wxT( "\";;;\n" );
184 fprintf( fp_bom, "%s", TO_UTF8( msg ) );
185 }
186
187 fclose( fp_bom );
188
189 return 0;
190}
int GenBOMFileFromBoard(const TOOL_EVENT &aEvent)
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:322
wxString m_Val
LIB_ID m_FPID
int m_Count
std::vector< wxString > m_Refs
A logical library item identifier and consists of various portions much like a URI.
Definition lib_id.h:49
BOARD * board() const
FOOTPRINT * footprint() const
Generic, UI-independent tool event.
Definition tool_event.h:171
void DisplayError(wxWindow *aParent, const wxString &aText)
Display an error or warning message box with aMessage.
Definition confirm.cpp:177
This file is part of the common library.
#define _(s)
@ FP_EXCLUDE_FROM_BOM
Definition footprint.h:87
static const std::string CsvFileExtension
static wxString CsvFileWildcard()
This file contains miscellaneous commonly used macros and functions.
void AllowNetworkFileSystems(wxDialog *aDialog)
Configure a file dialog to show network and virtual file systems.
Definition wxgtk/ui.cpp:717
wxString From_UTF8(const char *cstring)
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition of file extensions used in Kicad.