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/listimpl.cpp>
38#include <wx/filedlg.h>
39
40
41/* creates a BOM list from board
42 * The format is:
43 * "Id";"Designator";"Footprint";"Number";"Designation";"Supplier and ref";
44 * 1;"P1";"DB25FC";1;"DB25FEMELLE";;;
45 * 2;"U9";"PGA120";1;"4003APG120";;;
46 * 3;"JP1";"pin_array_8x2";1;"CONN_8X2";;;
47 * 4;"RR1";"r_pack9";1;"9x1K";;;
48 * 5;"X1";"HC-18UH";1;"8MHz";;;
49 * 6;"U8";"24dip300";1;"EP600";;;
50 * 7;"U5";"32dip600";1;"628128";;;
51 * 8;"C2,C3";"C1";2;"47pF";;;
52 * 9;"U1";"20dip300";1;"74LS245";;;
53 * 10;"U3";"20dip300";1;"74LS541";;;
54 * 11;"U2";"20dip300";1;"74LS688";;;
55 * 12;"C1,C4,C5,C6";"CP6";4;"47uF";;;
56 */
57
59{
60public:
61 wxString m_Ref;
62 wxString m_Val;
64 int m_Id;
66};
67
68WX_DECLARE_LIST( BOM_ENTRY, BOM_ENTRY_LIST );
69
70WX_DEFINE_LIST( BOM_ENTRY_LIST )
71
72
73int BOARD_EDITOR_CONTROL::GenBOMFileFromBoard( const TOOL_EVENT& aEvent )
74{
75 BOARD* board = m_frame->GetBoard();
76 wxFileName fn;
77 FILE* fp_bom;
78
79 if( board->Footprints().empty() )
80 {
81 m_frame->ShowInfoBarError( _( "Cannot export BOM: there are no footprints on the PCB." ) );
82 return 0;
83 }
84
85 /* Set the file extension: */
86 fn = board->GetFileName();
87 fn.SetExt( FILEEXT::CsvFileExtension );
88
89 wxString pro_dir = wxPathOnly( m_frame->Prj().GetProjectFullName() );
90
91 wxFileDialog dlg( m_frame, _( "Save Bill of Materials" ), pro_dir, fn.GetFullName(),
92 FILEEXT::CsvFileWildcard(), wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
93
94 if( dlg.ShowModal() == wxID_CANCEL )
95 return 0;
96
97 fn = dlg.GetPath();
98
99 fp_bom = wxFopen( fn.GetFullPath(), wxT( "wt" ) );
100
101 if( fp_bom == nullptr )
102 {
103 DisplayError( m_frame, wxString::Format( _( "Failed to create file '%s'." ), fn.GetFullPath() ) );
104 return 0;
105 }
106
107 // Write header:
108 wxString msg = wxT( "\"" );
109 msg << _( "Id" ) << wxT( "\";\"" );
110 msg << _( "Designator" ) << wxT( "\";\"" );
111 msg << _( "Footprint" ) << 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 BOM_ENTRY_LIST list;
119 int i = 1;
120
121 for( FOOTPRINT* footprint : board->Footprints() )
122 {
123 if( footprint->GetAttributes() & FP_EXCLUDE_FROM_BOM )
124 continue;
125
126 bool valExist = false;
127
128 // try to find component in existing list
129 for( auto iter = list.begin(); iter != list.end(); ++iter )
130 {
131 BOM_ENTRY* curEntry = *iter;
132
133 if( curEntry->m_Val == footprint->GetValue() && curEntry->m_FPID == footprint->GetFPID() )
134 {
135 curEntry->m_Ref.Append( wxT( ", " ), 1 );
136 curEntry->m_Ref.Append( footprint->Reference().GetShownText( false ) );
137 curEntry->m_Count++;
138
139 valExist = true;
140 break;
141 }
142 }
143
144 // If component does not exist yet, create new one and append it to the list.
145 if( valExist == false )
146 {
147 BOM_ENTRY* newEntry = new BOM_ENTRY();
148 newEntry->m_Id = i++;
149 newEntry->m_Val = footprint->Value().GetShownText( false );
150 newEntry->m_Ref = footprint->Reference().GetShownText( false );
151 newEntry->m_FPID = footprint->GetFPID();
152 newEntry->m_Count = 1;
153 list.Append( newEntry );
154 }
155 }
156
157 // Print list. Also delete temporary created objects.
158 for( size_t ii = list.GetCount(); ii > 0; ii-- )
159 {
160 BOM_ENTRY* curEntry = *list.begin(); // Because the first object will be removed
161 // from list, all objects will be get here
162 msg.Empty();
163
164 msg << curEntry->m_Id << wxT( ";\"" );
165 msg << curEntry->m_Ref << wxT( "\";\"" );
166 msg << From_UTF8( curEntry->m_FPID.GetLibItemName().c_str() ) << wxT( "\";" );
167 msg << curEntry->m_Count << wxT( ";\"" );
168 msg << curEntry->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( curEntry );
173 delete curEntry;
174 }
175
176 fclose( fp_bom );
177
178 return 0;
179}
WX_DECLARE_LIST(BOM_ENTRY, BOM_ENTRY_LIST)
Handle actions specific to the board editor in PcbNew.
virtual const BOARD * GetBoard() const
Return the BOARD in which this BOARD_ITEM resides, or NULL if none.
Definition: board_item.cpp:79
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:317
const FOOTPRINTS & Footprints() const
Definition: board.h:358
const wxString & GetFileName() const
Definition: board.h:354
wxString m_Val
int m_Id
wxString m_Ref
LIB_ID m_FPID
int m_Count
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:49
const UTF8 & GetLibItemName() const
Definition: lib_id.h:102
Generic, UI-independent tool event.
Definition: tool_event.h:168
const char * c_str() const
Definition: utf8.h:103
void DisplayError(wxWindow *aParent, const wxString &aText)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:169
This file is part of the common library.
#define _(s)
@ FP_EXCLUDE_FROM_BOM
Definition: footprint.h:83
static const std::string CsvFileExtension
static wxString CsvFileWildcard()
This file contains miscellaneous commonly used macros and functions.
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: string_utils.h:429
Definition of file extensions used in Kicad.