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, see <https://www.gnu.org/licenses/>.
19 */
20
21/* build_BOM_from_board.cpp */
22
23
24#include <confirm.h>
25#include <macros.h>
26#include <string_utils.h>
27#include <pcb_edit_frame.h>
28#include <board.h>
29#include <project.h>
31#include <footprint.h>
33#include <wx/filedlg.h>
34#include <vector>
35#include <kiplatform/ui.h>
36
37
38/* creates a BOM list from board
39 * The format is:
40 * "Id";"Designator";"Footprint";"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
56{
57public:
58 std::vector<wxString> m_Refs;
59 wxString m_Val;
62};
63
64
66{
67 BOARD* board = m_frame->GetBoard();
68 wxFileName fn;
69 FILE* fp_bom;
70
71 if( board->Footprints().empty() )
72 {
73 m_frame->ShowInfoBarError( _( "Cannot export BOM: there are no footprints on the PCB." ) );
74 return 0;
75 }
76
77 /* Set the file extension: */
78 fn = board->GetFileName();
79 fn.SetExt( FILEEXT::CsvFileExtension );
80
81 wxString pro_dir = wxPathOnly( m_frame->Prj().GetProjectFullName() );
82
83 wxFileDialog dlg( m_frame, _( "Save Bill of Materials" ), pro_dir, fn.GetFullName(),
84 FILEEXT::CsvFileWildcard(), wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
85
87
88 if( dlg.ShowModal() == wxID_CANCEL )
89 return 0;
90
91 fn = dlg.GetPath();
92
93 fp_bom = wxFopen( fn.GetFullPath(), wxT( "wt" ) );
94
95 if( fp_bom == nullptr )
96 {
97 DisplayError( m_frame, wxString::Format( _( "Failed to create file '%s'." ), fn.GetFullPath() ) );
98 return 0;
99 }
100
101 // Write header:
102 wxString msg = wxT( "\"" );
103 msg << _( "Id" ) << wxT( "\";\"" );
104 msg << _( "Designator" ) << wxT( "\";\"" );
105 msg << _( "Footprint" ) << wxT( "\";\"" );
106 msg << _( "Quantity" ) << wxT( "\";\"" );
107 msg << _( "Designation" ) << wxT( "\";\"" );
108 msg << _( "Supplier and ref" ) << wxT( "\";\n" );
109 fprintf( fp_bom, "%s", TO_UTF8( msg ) );
110
111 // Build list
112 std::vector<BOM_ENTRY> list;
113
114 for( FOOTPRINT* footprint : board->Footprints() )
115 {
116 if( footprint->GetAttributes() & FP_EXCLUDE_FROM_BOM )
117 continue;
118
119 bool valExist = false;
120
121 // try to find component in existing list
122 for( BOM_ENTRY& curEntry : list )
123 {
124 if( curEntry.m_Val == footprint->GetValue() && curEntry.m_FPID == footprint->GetFPID() )
125 {
126 curEntry.m_Refs.emplace_back( footprint->Reference().GetShownText( false ) );
127 curEntry.m_Count++;
128
129 valExist = true;
130 break;
131 }
132 }
133
134 // If component does not exist yet, create new one and append it to the list.
135 if( !valExist )
136 {
137 list.emplace_back();
138 BOM_ENTRY& newEntry = list.back();
139 newEntry.m_Val = footprint->Value().GetShownText( false );
140 newEntry.m_Refs.emplace_back( footprint->Reference().GetShownText( false ) );
141 newEntry.m_FPID = footprint->GetFPID();
142 newEntry.m_Count = 1;
143 }
144 }
145
146 for( BOM_ENTRY& curEntry : list )
147 {
148 std::sort( curEntry.m_Refs.begin(), curEntry.m_Refs.end(),
149 []( const wxString& lhs, const wxString& rhs )
150 {
151 return StrNumCmp( lhs, rhs, true /* ignore case */ ) < 0;
152 } );
153 }
154
155 std::sort( list.begin(), list.end(),
156 []( const BOM_ENTRY& lhs, const BOM_ENTRY& rhs )
157 {
158 return StrNumCmp( lhs.m_Refs[0], rhs.m_Refs[0], true /* ignore case */ ) < 0;
159 } );
160
161 // Print list.
162 int id = 1;
163
164 for( const BOM_ENTRY& curEntry : list )
165 {
166 msg.Empty();
167
168 msg << id++ << wxT( ";\"" );
169
170 msg << curEntry.m_Refs[0];
171
172 for( int ii = 1; ii < (int) curEntry.m_Refs.size(); ++ii )
173 msg << wxT( ", " ) << curEntry.m_Refs[ii];
174
175 msg << wxT( "\";\"" );
176
177 msg << From_UTF8( curEntry.m_FPID.GetLibItemName().c_str() ) << wxT( "\";" );
178 msg << curEntry.m_Count << wxT( ";\"" );
179 msg << curEntry.m_Val << wxT( "\";;;\n" );
180 fprintf( fp_bom, "%s", TO_UTF8( msg ) );
181 }
182
183 fclose( fp_bom );
184
185 return 0;
186}
int GenBOMFileFromBoard(const TOOL_EVENT &aEvent)
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:372
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:45
BOARD * board() const
FOOTPRINT * footprint() const
Generic, UI-independent tool event.
Definition tool_event.h:167
void DisplayError(wxWindow *aParent, const wxString &aText)
Display an error or warning message box with aMessage.
Definition confirm.cpp:192
This file is part of the common library.
#define _(s)
@ FP_EXCLUDE_FROM_BOM
Definition footprint.h:86
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:448
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.