2009-03-30 15:36:41 +00:00
|
|
|
/* build_BOM_from_board.cpp */
|
|
|
|
|
|
|
|
|
2012-01-23 04:33:36 +00:00
|
|
|
#include <fctsys.h>
|
|
|
|
#include <confirm.h>
|
|
|
|
#include <kicad_string.h>
|
|
|
|
#include <gestfich.h>
|
|
|
|
#include <pcbnew.h>
|
|
|
|
#include <wxPcbStruct.h>
|
|
|
|
#include <macros.h>
|
|
|
|
|
|
|
|
#include <class_board.h>
|
|
|
|
#include <class_module.h>
|
2009-03-30 15:36:41 +00:00
|
|
|
|
|
|
|
#include <wx/listimpl.cpp>
|
|
|
|
|
|
|
|
/* creates a BOM list rom board
|
2011-03-03 19:08:13 +00:00
|
|
|
* 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";;;
|
|
|
|
*/
|
2009-03-30 15:36:41 +00:00
|
|
|
|
2009-04-05 20:49:15 +00:00
|
|
|
const wxString CsvFileExtension( wxT( "csv" ) ); // BOM file extension
|
|
|
|
|
|
|
|
const wxString CsvFileWildcard( _( "Comma separated value files (*.csv)|*.csv" ) );
|
|
|
|
|
2009-03-30 15:36:41 +00:00
|
|
|
|
|
|
|
class cmp
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
wxString m_Ref;
|
|
|
|
wxString m_Val;
|
|
|
|
wxString m_Pkg;
|
|
|
|
int m_Id;
|
|
|
|
int m_CmpCount;
|
|
|
|
};
|
|
|
|
WX_DECLARE_LIST( cmp, CmpList );
|
|
|
|
|
2010-10-04 12:58:07 +00:00
|
|
|
WX_DEFINE_LIST( CmpList )
|
2009-03-30 15:36:41 +00:00
|
|
|
|
2011-03-01 19:26:17 +00:00
|
|
|
void PCB_EDIT_FRAME::RecreateBOMFileFromBoard( wxCommandEvent& aEvent )
|
2009-03-30 15:36:41 +00:00
|
|
|
{
|
2009-04-05 20:49:15 +00:00
|
|
|
wxFileName fn;
|
|
|
|
FILE* FichBom;
|
|
|
|
MODULE* Module = GetBoard()->m_Modules;
|
|
|
|
wxString msg;
|
2009-03-30 15:36:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
if( Module == NULL )
|
|
|
|
{
|
|
|
|
DisplayError( this, _( "No Modules!" ) );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the file extension: */
|
2012-08-29 16:59:50 +00:00
|
|
|
fn = GetBoard()->GetFileName();
|
2009-04-05 20:49:15 +00:00
|
|
|
fn.SetExt( CsvFileExtension );
|
|
|
|
|
2009-04-25 10:12:30 +00:00
|
|
|
wxFileDialog dlg( this, _( "Save Bill of Materials" ), wxGetCwd(),
|
2009-04-05 20:49:15 +00:00
|
|
|
fn.GetFullName(), CsvFileWildcard,
|
|
|
|
wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
|
|
|
|
|
|
|
|
if( dlg.ShowModal() == wxID_CANCEL )
|
2009-03-30 15:36:41 +00:00
|
|
|
return;
|
|
|
|
|
2009-04-05 20:49:15 +00:00
|
|
|
fn = dlg.GetPath();
|
|
|
|
|
|
|
|
FichBom = wxFopen( fn.GetFullPath(), wxT( "wt" ) );
|
2009-03-30 15:36:41 +00:00
|
|
|
|
|
|
|
if( FichBom == NULL )
|
|
|
|
{
|
2009-04-05 20:49:15 +00:00
|
|
|
msg = _( "Unable to create file " ) + fn.GetFullPath();
|
2009-03-30 15:36:41 +00:00
|
|
|
DisplayError( this, msg );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write header:
|
2011-03-03 19:08:13 +00:00
|
|
|
msg = wxT( "\"" );
|
|
|
|
msg << _( "Id" ) << wxT( "\";\"" );
|
|
|
|
msg << _( "Designator" ) << wxT( "\";\"" );
|
|
|
|
msg << _( "Package" ) << wxT( "\";\"" );
|
|
|
|
msg << _( "Quantity" ) << wxT( "\";\"" );
|
|
|
|
msg << _( "Designation" ) << wxT( "\";\"" );
|
|
|
|
msg << _( "Supplier and ref" ) << wxT( "\";\n" );
|
2011-02-28 18:36:19 +00:00
|
|
|
fprintf( FichBom, "%s", TO_UTF8( msg ) );
|
2009-03-30 15:36:41 +00:00
|
|
|
|
|
|
|
// Build list
|
|
|
|
CmpList list;
|
|
|
|
cmp* comp = NULL;
|
|
|
|
CmpList::iterator iter;
|
|
|
|
int i = 1;
|
2011-03-03 19:08:13 +00:00
|
|
|
while( Module != NULL )
|
2009-03-30 15:36:41 +00:00
|
|
|
{
|
|
|
|
bool valExist = false;
|
|
|
|
|
2010-01-28 13:10:46 +00:00
|
|
|
// try to find component in existing list
|
|
|
|
for( iter = list.begin(); iter != list.end(); iter++ )
|
2009-03-30 15:36:41 +00:00
|
|
|
{
|
2010-01-28 13:10:46 +00:00
|
|
|
cmp* current = *iter;
|
2013-03-13 18:53:58 +00:00
|
|
|
|
|
|
|
if( (current->m_Val == Module->GetValue()) && (current->m_Pkg == Module->GetLibRef()) )
|
2009-03-30 15:36:41 +00:00
|
|
|
{
|
2010-01-28 13:10:46 +00:00
|
|
|
current->m_Ref.Append( wxT( ", " ), 1 );
|
2013-03-13 18:53:58 +00:00
|
|
|
current->m_Ref.Append( Module->GetReference() );
|
2010-01-28 13:10:46 +00:00
|
|
|
current->m_CmpCount++;
|
|
|
|
|
|
|
|
valExist = true;
|
|
|
|
break;
|
2009-03-30 15:36:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-28 13:10:46 +00:00
|
|
|
// If component does not exist yet, create new one and append it to the list.
|
|
|
|
if( valExist == false )
|
2009-03-30 15:36:41 +00:00
|
|
|
{
|
2011-03-03 19:08:13 +00:00
|
|
|
comp = new cmp();
|
|
|
|
comp->m_Id = i++;
|
2013-03-13 18:53:58 +00:00
|
|
|
comp->m_Val = Module->GetValue();
|
|
|
|
comp->m_Ref = Module->GetReference();
|
|
|
|
comp->m_Pkg = Module->GetLibRef();
|
2009-03-30 15:36:41 +00:00
|
|
|
comp->m_CmpCount = 1;
|
|
|
|
list.Append( comp );
|
|
|
|
}
|
2011-02-28 18:36:19 +00:00
|
|
|
|
2010-01-28 13:10:46 +00:00
|
|
|
// increment module
|
|
|
|
Module = Module->Next();
|
2009-03-30 15:36:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Print list
|
|
|
|
for( iter = list.begin(); iter != list.end(); iter++ )
|
|
|
|
{
|
|
|
|
cmp* current = *iter;
|
|
|
|
|
|
|
|
msg.Empty();
|
|
|
|
|
|
|
|
msg << current->m_Id << wxT( ";\"" );
|
|
|
|
msg << current->m_Ref << wxT( "\";\"" );
|
|
|
|
msg << current->m_Pkg << wxT( "\";" );
|
|
|
|
msg << current->m_CmpCount << wxT( ";\"" );
|
|
|
|
msg << current->m_Val << wxT( "\";;;\n" );
|
2011-02-28 18:36:19 +00:00
|
|
|
fprintf( FichBom, "%s", TO_UTF8( msg ) );
|
2009-03-30 15:36:41 +00:00
|
|
|
|
|
|
|
list.DeleteObject( current );
|
|
|
|
delete (current);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fclose( FichBom );
|
|
|
|
}
|