1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2009-2014 Jean-Pierre Charras, jean-pierre.charras@ujf-grenoble.fr
* Copyright (C) 1992-2012 KiCad Developers, see AUTHORS.txt for contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
/* build_BOM_from_board.cpp */
#include <fctsys.h>
#include <confirm.h>
#include <kicad_string.h>
#include <gestfich.h>
#include <pcbnew.h>
#include <wxPcbStruct.h>
#include <macros.h>
#include <project.h>
#include <class_board.h>
#include <class_module.h>
#include <wx/listimpl.cpp>
/* creates a BOM list rom board
* The format is:
* "Id";"Designator";"Package";"Number";"Designation";"Supplier and ref";
* 1;"P1";"DB25FC";1;"DB25FEMELLE";;;
* 2;"U9";"PGA120";1;"4003APG120";;;
* 3;"JP1";"pin_array_8x2";1;"CONN_8X2";;;
* 4;"RR1";"r_pack9";1;"9x1K";;;
* 5;"X1";"HC-18UH";1;"8MHz";;;
* 6;"U8";"24dip300";1;"EP600";;;
* 7;"U5";"32dip600";1;"628128";;;
* 8;"C2,C3";"C1";2;"47pF";;;
* 9;"U1";"20dip300";1;"74LS245";;;
* 10;"U3";"20dip300";1;"74LS541";;;
* 11;"U2";"20dip300";1;"74LS688";;;
* 12;"C1,C4,C5,C6";"CP6";4;"47uF";;;
*/
const wxString CsvFileExtension( wxT( "csv" ) ); // BOM file extension
const wxString CsvFileWildcard( _( "Comma separated value files (*.csv)|*.csv" ) );
class cmp
{
public:
wxString m_Ref;
wxString m_Val;
FPID m_fpid;
int m_Id;
int m_CmpCount;
};
WX_DECLARE_LIST( cmp, CmpList );
WX_DEFINE_LIST( CmpList )
void PCB_EDIT_FRAME::RecreateBOMFileFromBoard( wxCommandEvent& aEvent )
{
wxFileName fn;
FILE* fp_bom;
MODULE* module = GetBoard()->m_Modules;
wxString msg;
if( module == NULL )
{
DisplayError( this, _( "Cannot export BOM: there are no footprints in the PCB" ) );
return;
}
/* Set the file extension: */
fn = GetBoard()->GetFileName();
fn.SetExt( CsvFileExtension );
wxString pro_dir = wxPathOnly( Prj().GetProjectFullName() );
wxFileDialog dlg( this, _( "Save Bill of Materials" ), pro_dir,
fn.GetFullName(), CsvFileWildcard,
wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
if( dlg.ShowModal() == wxID_CANCEL )
return;
fn = dlg.GetPath();
fp_bom = wxFopen( fn.GetFullPath(), wxT( "wt" ) );
if( fp_bom == NULL )
{
msg.Printf( _( "Unable to create file <%s>" ), GetChars( fn.GetFullPath() ) );
DisplayError( this, msg );
return;
}
// Write header:
msg = wxT( "\"" );
msg << _( "Id" ) << wxT( "\";\"" );
msg << _( "Designator" ) << wxT( "\";\"" );
msg << _( "Package" ) << wxT( "\";\"" );
msg << _( "Quantity" ) << wxT( "\";\"" );
msg << _( "Designation" ) << wxT( "\";\"" );
msg << _( "Supplier and ref" ) << wxT( "\";\n" );
fprintf( fp_bom, "%s", TO_UTF8( msg ) );
// Build list
CmpList list;
cmp* comp = NULL;
CmpList::iterator iter;
int i = 1;
while( module != NULL )
{
bool valExist = false;
// try to find component in existing list
for( iter = list.begin(); iter != list.end(); ++iter )
{
cmp* current = *iter;
if( (current->m_Val == module->GetValue()) && (current->m_fpid == module->GetFPID()) )
{
current->m_Ref.Append( wxT( ", " ), 1 );
current->m_Ref.Append( module->GetReference() );
current->m_CmpCount++;
valExist = true;
break;
}
}
// If component does not exist yet, create new one and append it to the list.
if( valExist == false )
{
comp = new cmp();
comp->m_Id = i++;
comp->m_Val = module->GetValue();
comp->m_Ref = module->GetReference();
comp->m_fpid = module->GetFPID();
comp->m_CmpCount = 1;
list.Append( comp );
}
// increment module
module = module->Next();
}
// Print list. Also delete temporary created objects.
for( size_t ii = list.GetCount(); ii > 0; ii-- )
{
cmp* current = *list.begin(); // Because the first object will be removed
// from list, all objects will be get here
msg.Empty();
msg << current->m_Id << wxT( ";\"" );
msg << current->m_Ref << wxT( "\";\"" );
msg << FROM_UTF8( current->m_fpid.GetFootprintName().c_str() ) << wxT( "\";" );
msg << current->m_CmpCount << wxT( ";\"" );
msg << current->m_Val << wxT( "\";;;\n" );
fprintf( fp_bom, "%s", TO_UTF8( msg ) );
// We do not need this object, now: remove it from list and delete it
list.DeleteObject( current );
delete (current);
}
fclose( fp_bom );
}
|