kicad/pcbnew/netlist.cpp

1095 lines
34 KiB
C++
Raw Normal View History

/***********************/
/* PCBNEW: netlist.cpp */
/***********************/
/*
* Functions to read a netlist. They are:
* - Load new footprints and nitialize net info
* - Test for missing or extra footprints
* - Recalculate full connectivity info
2008-02-19 00:28:42 +00:00
*
* Important remark:
* When reading a netlist Pcbnew must identify existing footprints (link
* between existing footprints an components in netlist)
* This identification can be from 2 fields:
* - The reference (U2, R5 ..): this is the normal mode
* - The Time Stamp (Signature Temporelle), useful after a full schematic
* reannotation because references can be changed for the same schematic.
* So when reading a netlist ReadPcbNetlist() can use references or time stamps
* to identify footprints on board and the corresponding component in schematic.
* If we want to fully reannotate a schematic this sequence must be used
* 1 - SAVE your board !!!
* 2 - Create and read the netlist (to ensure all info is correct, mainly
* references and time stamp)
* 3 - Reannotate the schematic (references will be changed, but not time stamps )
* 4 - Recreate and read the new netlist using the Time Stamp identification
* (that reinit the new references)
*/
#include "vector"
#include "algorithm"
#include "fctsys.h"
#include "common.h"
#include "class_drawpanel.h"
#include "confirm.h"
#include "kicad_string.h"
#include "gestfich.h"
#include "pcbnew.h"
2009-07-30 11:04:07 +00:00
#include "wxPcbStruct.h"
#include "richio.h"
2010-11-18 21:16:28 +00:00
#include "dialog_helpers.h"
#include "dialog_netlist.h"
// constants used by ReadPcbNetlist():
#define TESTONLY true
#define READMODULE false
/*
* Helper class, to store new footprints info found in netlist.
* New footprints are footprints relative to new components found in netlist
*/
class MODULE_INFO
{
public:
wxString m_LibName;
wxString m_CmpName;
wxString m_TimeStampPath;
public: MODULE_INFO( const wxString& libname,
const wxString& cmpname,
const wxString& timestamp_path )
{
m_LibName = libname;
m_CmpName = cmpname;
m_TimeStampPath = timestamp_path;
}
~MODULE_INFO() { };
};
/*
* Helper class, to read a netlist.
*/
class NETLIST_READER
{
private:
PCB_EDIT_FRAME* m_pcbframe; // the main pcbnew frame
wxTextCtrl* m_messageWindow; // a textctrl to show messages (can be NULL)
wxString m_netlistFullName; // The full netlist filename
wxString m_cmplistFullName; // The full component/footprint association filename
MODULE* m_currModule; // The footprint currently being read in netlist
std::vector <MODULE_INFO*> m_newModulesList; // The list of new footprints,
// found in netlist, but not on board
// (must be loaded from libraries)
bool m_useCmpFile; // true to use .cmp files as component/footprint file link
// false to use netlist only to know component/footprint link
public:
bool m_UseTimeStamp; // Set to true to identify footprits by time stamp
// false to use schematic reference
bool m_ChangeFootprints; // Set to true to change existing footprints to new ones
// when netlist gives a different footprint name
public: NETLIST_READER( PCB_EDIT_FRAME* aFrame, wxTextCtrl* aMessageWindow = NULL )
{
m_pcbframe = aFrame;
m_messageWindow = aMessageWindow;
m_UseTimeStamp = false;
m_ChangeFootprints = false;
m_useCmpFile = true;
}
~NETLIST_READER()
{
// Free new modules list:
for( unsigned ii = 0; ii < m_newModulesList.size(); ii++ )
delete m_newModulesList[ii];
m_newModulesList.clear();
}
/**
* Function ReadNetList
* The main function to read a netlist, and update the board
* @param aFile = the already opened file (will be closed by ReadNetList)
* @param aNetlistFileName = the netlist full file name (*.net file)
* @param aCmplistFileName = the auxiliary component full file name (*.cmp file)
* If the aCmplistFileName file is not given or not found,
* the netlist is used to know the component/footprint link.
*/
bool ReadNetList( FILE* aFile, const wxString& aNetlistFileName,
const wxString& aCmplistFileName );
/**
* Function BuildFootprintListFromNetlist
* Fill aBufName with footprints references read from the netlist.
* @param aNetlistFilename = netlist full file name
* @param aBufName = wxArrayString to fill with footprint names
* @return the footprint count, or -1 if netlist file cannot opened
*/
int BuildFootprintListFromNetlist( const wxString& aNetlistFilename,
wxArrayString& aBufName );
/**
* function RemoveExtraFootprints
* Remove (delete) not locked footprints found on board, but not in netlist
* @param aNetlistFileName = the netlist full file name (*.net file)
*/
void RemoveExtraFootprints( const wxString& aNetlistFileName );
private:
/**
* Function SetPadNetName
* Update a pad netname using the current footprint
* from the netlist (line format: ( <pad number> <net name> ) )
* @param aText = current line read from netlist
*/
bool SetPadNetName( char* aText );
/**
* Function ReadNetlistModuleDescr
* Read the full description of a footprint, from the netlist
* and update the corresponding module.
* @param aTstOnly bool to switch between 2 modes:
* aTstOnly = false:
* if the module does not exist, it is added to m_newModulesList
* aTstOnly = true:
* if the module does not exist, it is loaded and added to the board module list
* @param aText contains the first line of description
* This function uses m_useFichCmp as a flag to know the footprint name:
* If true: component file *.cmp is used
* If false: the netlist only is used
* This flag is reset to false if the .cmp file is not found
*/
MODULE* ReadNetlistModuleDescr( char* aText, bool aTstOnly );
/**
* Function loadNewModules
* Load from libraries new modules found in netlist and add them to the current Board.
*/
void loadNewModules();
/**
* function readModulesComponentsTable
* read the *.cmp file ( filename in m_cmplistFullName )
* giving the equivalence beteween modules and components
* @return true and the module name in aModuleName, false if not found
*
* @param aCmpIdent = component identification: schematic reference or time stamp
* @param aModuleName the footprint name corresponding to the component identification
*/
bool readModulesComponentsTable( const wxString* aCmpIdent, wxString& aModuleName );
};
/**
* Function OpenNetlistFile
* used to open a netlist file
*/
static FILE* OpenNetlistFile( const wxString& aFullFileName )
{
if( aFullFileName.IsEmpty() )
return false; /* No filename: exit */
FILE* file = wxFopen( aFullFileName, wxT( "rt" ) );
if( file == NULL )
{
wxString msg;
msg.Printf( _( "Netlist file %s not found" ), GetChars( aFullFileName ) );
wxMessageBox( msg );
}
return file;
}
/* Function to sort the footprint list.
* the given list is sorted by name
*/
static bool SortByLibName( MODULE_INFO* ref, MODULE_INFO* cmp )
{
int ii = ref->m_LibName.CmpNoCase( cmp->m_LibName );
return ii > 0;
}
/**
* Function ReadPcbNetlist
* Update footprints (load missing footprints and delete on request extra
* footprints)
* Update connectivity info ( Net Name list )
* Update Reference, value and "TIME STAMP"
* @param aNetlistFullFilename = netlist file name (*.net)
* @param aCmpFullFileName = cmp/footprint list file name (*.cmp) if not found,
* @param aMessageWindow = a wxTextCtrl to print messages (can be NULL).
* @param aChangeFootprint = true to change existing footprints
* when the netlist gives a different footprint.
* false to keep existing footprints
* @param aDeleteBadTracks - true to erase erroneous tracks after updating connectivity info.
* @param aDeleteExtraFootprints - true to remove unlocked footprints found on board but not in netlist.
* @param aSelect_By_Timestamp - true to use schematic timestamps instead of schematic references
* to identify footprints on board
* (Must be used after a full reannotation in schematic).
* @return true if Ok
*/
bool PCB_EDIT_FRAME::ReadPcbNetlist( const wxString& aNetlistFullFilename,
const wxString& aCmpFullFileName,
wxTextCtrl* aMessageWindow,
bool aChangeFootprint,
bool aDeleteBadTracks,
bool aDeleteExtraFootprints,
bool aSelect_By_Timestamp )
{
FILE* netfile = OpenNetlistFile( aNetlistFullFilename );
if( !netfile )
return false;
SetLastNetListRead( aNetlistFullFilename );
if( aMessageWindow )
{
wxString msg;
msg.Printf( _( "Reading Netlist \"%s\"" ), GetChars( aNetlistFullFilename ) );
aMessageWindow->AppendText( msg + wxT( "\n" ) );
if( ! aCmpFullFileName.IsEmpty() )
{
msg.Printf( _( "Using component/footprint link file \"%s\"" ), GetChars( aCmpFullFileName ) );
aMessageWindow->AppendText( msg + wxT( "\n" ) );
}
}
2009-08-03 07:55:08 +00:00
// Clear undo and redo lists to avoid inconsistencies between lists
GetScreen()->ClearUndoRedoList();
2009-08-03 07:55:08 +00:00
OnModify();
// Clear flags and pointeurs to avoid inconsistencies
GetBoard()->m_Status_Pcb = 0;
SetCurItem( NULL );
wxBusyCursor dummy; // Shows an hourglass while calculating
NETLIST_READER netList_Reader( this, aMessageWindow );
netList_Reader.m_UseTimeStamp = aSelect_By_Timestamp;
netList_Reader.m_ChangeFootprints = aChangeFootprint;
netList_Reader.ReadNetList( netfile, aNetlistFullFilename, aCmpFullFileName );
// Delete footprints not found in netlist:
if( aDeleteExtraFootprints )
{
netList_Reader.RemoveExtraFootprints( aNetlistFullFilename );
}
// Rebuild the board connectivity:
Compile_Ratsnest( NULL, true );
if( GetBoard()->m_Track )
{
if( aDeleteBadTracks ) // Remove erroneous tracks
{
RemoveMisConnectedTracks( NULL );
Compile_Ratsnest( NULL, true );
}
}
GetBoard()->DisplayInfo( this );
DrawPanel->Refresh();
return true;
}
/* function RemoveExtraFootprints
* Remove (delete) not locked footprints found on board, but not in netlist
*/
void NETLIST_READER::RemoveExtraFootprints( const wxString& aNetlistFileName )
{
wxArrayString modulesInNetlist;
// Build list of modules in the netlist
int modulesCount =
BuildFootprintListFromNetlist( aNetlistFileName, modulesInNetlist );
if( modulesCount == 0 )
return;
MODULE* NextModule;
MODULE* Module = m_pcbframe->GetBoard()->m_Modules;
bool ask_user = true;
for( ; Module != NULL; Module = NextModule )
{
int ii;
NextModule = Module->Next();
if( Module->m_ModuleStatus & MODULE_is_LOCKED )
continue;
for( ii = 0; ii < modulesCount; ii++ )
{
if( Module->m_Reference->m_Text.CmpNoCase( modulesInNetlist[ii] ) == 0 )
break; /* Module is found in net list. */
}
if( ii == modulesCount ) // Module not found in netlist.
{
if( ask_user )
{
ask_user = false;
if( !IsOK( NULL,
_( "Ok to delete not locked footprints not found in netlist?" ) ) )
break;
}
Module->DeleteStructure();
}
}
}
/*
* Function ReadNetlist
* Update footprints (load missing footprints and delete on request extra
* footprints)
* Update References, values, "TIME STAMP" and connectivity data
* return true if Ok
*
* the format of the netlist is something like:
* # EESchema Netlist Version 1.0 generee le 18/5/2005-12:30:22
* (
* ( 40C08647 $noname R20 4,7K {Lib=R}
* ( 1 VCC )
* ( 2 MODB_1 )
* )
* ( 40C0863F $noname R18 4,7_k {Lib=R}
* ( 1 VCC )
* ( 2 MODA_1 )
* )
* }
* #End
*/
bool NETLIST_READER::ReadNetList( FILE* aFile,
const wxString& aNetlistFileName,
const wxString& aCmplistFileName )
{
int State = 0;
int Comment = 0;
m_netlistFullName = aNetlistFileName;
m_cmplistFullName = aCmplistFileName;
m_useCmpFile = true;
/* First, read the netlist: Build the list of footprints to load (new
* footprints)
*/
FILE_LINE_READER netlineReader( aFile, m_netlistFullName ); // netlineReader dtor will close aFile
while( netlineReader.ReadLine() )
{
char* line = StrPurge( netlineReader.Line() );
if( Comment ) /* Comments in progress */
{
// Test for end of the current comment
if( ( line = strchr( line, '}' ) ) == NULL )
continue;
Comment = 0;
}
if( *line == '{' ) /* Start Comment */
{
Comment = 1;
if( ( line = strchr( line, '}' ) ) == NULL )
continue;
}
if( *line == '(' )
State++;
2008-02-19 00:28:42 +00:00
if( *line == ')' )
State--;
if( State == 2 )
{
ReadNetlistModuleDescr( line, TESTONLY );
continue;
}
if( State >= 3 ) // First pass: pad descriptions are not read here.
{
State--;
}
}
/* Load new footprints */
loadNewModules();
/* Second read , All footprints are on board.
* One must update the schematic info (pad netnames)
*/
netlineReader.Rewind();
m_currModule = NULL;
while( netlineReader.ReadLine() )
{
char* line = StrPurge( netlineReader.Line() );
if( Comment ) // we are reading a comment
{
// Test for end of the current comment
if( ( line = strchr( line, '}' ) ) == NULL )
continue;
Comment = 0;
}
if( *line == '{' ) // this is the beginning of a comment
{
Comment = 1;
if( ( line = strchr( line, '}' ) ) == NULL )
continue;
}
if( *line == '(' )
State++;
if( *line == ')' )
State--;
if( State == 2 )
{
m_currModule = ReadNetlistModuleDescr( line, READMODULE );
if( m_currModule == NULL ) // the module could not be created (perhaps
// footprint not found in library)
continue;
else /* clear pads netnames */
{
D_PAD* PtPad = m_currModule->m_Pads;
for( ; PtPad != NULL; PtPad = PtPad->Next() )
{
PtPad->SetNetname( wxEmptyString );
}
}
continue;
}
if( State >= 3 )
{
if( m_currModule )
SetPadNetName( line );
State--;
}
}
return true;
}
/* Function ReadNetlistModuleDescr
* Read the full description of a footprint, from the netlist
* and update the corresponding module.
* param aTstOnly bool to switch between 2 modes:
* If aTstOnly == false:
* if the module does not exist, it is added to m_newModulesList
* If aTstOnly = true:
* if the module does not exist, it is loaded and added to the board module list
* param aText contains the first line of description
* This function uses m_useFichCmp as a flag to know the footprint name:
* If true: component file *.cmp is used
* If false: the netlist only is used
* This flag is reset to false if the .cmp file is not found
* Analyze lines like:
* ($ 40C08647 noname R20 4.7 K Lib = R
* (1 VCC)
* (2 MODB_1)
*/
MODULE* NETLIST_READER::ReadNetlistModuleDescr( char* aText, bool aTstOnly )
{
char* text;
wxString TimeStampPath;
wxString TextNameLibMod;
wxString TextValeur;
wxString TextCmpName;
wxString NameLibCmp;
int Error = 0;
char Line[1024];
strcpy( Line, aText );
TextValeur = wxT( "~" );
2008-02-19 00:28:42 +00:00
if( ( text = strtok( Line, " ()\t\n" ) ) == NULL )
Error = 1;
else
TimeStampPath = FROM_UTF8( text );
2008-02-19 00:28:42 +00:00
if( ( text = strtok( NULL, " ()\t\n" ) ) == NULL )
Error = 1;
else
TextNameLibMod = FROM_UTF8( text );
2008-02-19 00:28:42 +00:00
if( ( text = strtok( NULL, " ()\t\n" ) ) == NULL )
Error = 1;
else
TextCmpName = FROM_UTF8( text );
2008-02-19 00:28:42 +00:00
if( ( text = strtok( NULL, " ()\t\n" ) ) == NULL )
Error = -1;
else
TextValeur = FROM_UTF8( text );
if( Error > 0 )
return NULL;
/* Test if module is already loaded. */
wxString * identMod = &TextCmpName;
if( m_UseTimeStamp )
identMod = &TimeStampPath;
MODULE* Module = m_pcbframe->GetBoard()->m_Modules;
MODULE* NextModule;
bool found = false;
for( ; Module != NULL; Module = NextModule )
{
NextModule = Module->Next();
if( m_UseTimeStamp ) /* identification by time stamp */
{
if( TimeStampPath.CmpNoCase( Module->m_Path ) == 0 )
found = true;
}
else /* identification by Reference */
{
if( TextCmpName.CmpNoCase( Module->m_Reference->m_Text ) == 0 )
found = true;
}
if( found ) // test footprint matching for existing modules: current
{
// m_LibRef and module name in netlist must match
if( aTstOnly != TESTONLY )
{
NameLibCmp = TextNameLibMod; // Use footprint name from netlist
if( m_useCmpFile ) // Try to get footprint name from .cmp file
{
m_useCmpFile = readModulesComponentsTable( identMod, NameLibCmp );
}
/* Module mismatch: current module and module specified in
* net list are different.
*/
if( Module->m_LibRef.CmpNoCase( NameLibCmp ) != 0 )
{
if( m_ChangeFootprints ) // footprint exchange allowed.
{
MODULE* NewModule =
m_pcbframe->Get_Librairie_Module( wxEmptyString,
NameLibCmp,
true );
if( NewModule ) /* Change old module to the new module
* (and delete the old one) */
2009-08-06 07:11:04 +00:00
{
m_pcbframe->Exchange_Module( Module, NewModule, NULL );
2009-08-06 07:11:04 +00:00
Module = NewModule;
}
}
else
{
wxString msg;
msg.Printf(
_(
"Component \"%s\": Mismatch! module is [%s] and netlist said [%s]\n" ),
GetChars( TextCmpName ),
GetChars( Module->m_LibRef ),
GetChars( NameLibCmp ) );
if( m_messageWindow )
m_messageWindow->AppendText( msg );
}
}
}
break;
}
}
if( Module == NULL ) /* a new module must be loaded from libs */
{
NameLibCmp = TextNameLibMod; // Use footprint name from netlist
if( m_useCmpFile ) // Try to get footprint name from .cmp file
{
m_useCmpFile = readModulesComponentsTable( identMod, NameLibCmp );
}
if( aTstOnly == TESTONLY )
{
MODULE_INFO* newMod;
newMod = new MODULE_INFO( NameLibCmp, TextCmpName, TimeStampPath );
m_newModulesList.push_back( newMod );
}
else
{
if( m_messageWindow )
{
wxString msg;
msg.Printf( _( "Component [%s] not found" ),
GetChars( TextCmpName ) );
m_messageWindow->AppendText( msg + wxT( "\n" ) );
}
}
return NULL; /* The module could not be loaded. */
}
/* Fields update ( reference, value and "Time Stamp") */
Module->m_Reference->m_Text = TextCmpName;
Module->m_Value->m_Text = TextValeur;
Module->m_Path = TimeStampPath;
return Module;
}
/*
* Function SetPadNetName
* Update a pad netname using the current footprint
* Line format: ( <pad number> <net name> )
* Param aText = current line read from netlist
*/
bool NETLIST_READER::SetPadNetName( char* aText )
{
D_PAD* pad;
char* TextPinName, * TextNetName;
bool found;
bool error = false;
char Line[256];
if( m_currModule == NULL )
return false;
strcpy( Line, aText );
if( ( TextPinName = strtok( Line, " ()\t\n" ) ) == NULL )
error = true;
if( ( TextNetName = strtok( NULL, " ()\t\n" ) ) == NULL )
error = true;
if( error )
return 0;
pad = m_currModule->m_Pads;
found = false;
for( ; pad != NULL; pad = pad->Next() )
{
if( strnicmp( TextPinName, pad->m_Padname, 4 ) == 0 )
{
found = true;
if( *TextNetName != '?' )
pad->SetNetname( FROM_UTF8( TextNetName ) );
else
pad->SetNetname( wxEmptyString );
}
}
if( !found )
{
if( m_messageWindow )
{
wxString msg;
wxString pin_name = FROM_UTF8( TextPinName );
msg.Printf( _( "Module [%s]: Pad [%s] not found" ),
GetChars( m_currModule->m_Reference->m_Text ),
GetChars( pin_name ) );
m_messageWindow->AppendText( msg + wxT( "\n" ) );
}
}
return found;
}
/**
* build and shows a list of existing modules on board
* The user can select a module from this list
* @return a pointer to the selected module or NULL
*/
MODULE* PCB_EDIT_FRAME::ListAndSelectModuleName( void )
{
MODULE* Module;
if( GetBoard()->m_Modules == NULL )
{
2010-11-19 18:50:23 +00:00
DisplayError( this, _( "No Modules" ) );
return 0;
}
2010-11-19 18:50:23 +00:00
wxArrayString listnames;
Module = (MODULE*) GetBoard()->m_Modules;
for( ; Module != NULL; Module = (MODULE*) Module->Next() )
2010-11-19 18:50:23 +00:00
listnames.Add( Module->m_Reference->m_Text );
2010-11-19 18:50:23 +00:00
WinEDAListBox dlg( this, _( "Components" ), listnames, wxEmptyString );
2010-11-19 18:50:23 +00:00
if( dlg.ShowModal() != wxID_OK )
return NULL;
wxString ref = dlg.GetTextSelection();
Module = (MODULE*) GetBoard()->m_Modules;
for( ; Module != NULL; Module = Module->Next() )
{
2010-11-19 18:50:23 +00:00
if( Module->m_Reference->m_Text == ref )
break;
}
return Module;
}
/*
* Function Test_Duplicate_Missing_And_Extra_Footprints
* Build a list of duplicate, missing and extra footprints
* from the current board and a netlist netlist :
* Shows 3 lists:
* 1 - duplicate footprints on board
* 2 - missing footprints (found in netlist but not on board)
* 3 - footprints not in netlist but on board
* @param aNetlistFullFilename = the full filename netlist
*/
void PCB_EDIT_FRAME::Test_Duplicate_Missing_And_Extra_Footprints(
const wxString& aNetlistFullFilename )
{
#define MAX_LEN_TXT 32
int ii;
int NbModulesNetListe, nberr = 0;
wxArrayString tmp;
wxArrayString list;
if( GetBoard()->m_Modules == NULL )
{
DisplayInfoMessage( this, _( "No modules" ) );
return;
}
/* Build the list of references of the net list modules. */
NETLIST_READER netList_Reader( this );
NbModulesNetListe = netList_Reader.BuildFootprintListFromNetlist( aNetlistFullFilename, tmp );
if( NbModulesNetListe < 0 )
return; /* File not found */
if( NbModulesNetListe == 0 )
{
wxMessageBox( _( "No modules in NetList" ) );
return;
}
/* Search for duplicate footprints. */
list.Add( _( "Duplicates" ) );
MODULE* Module = GetBoard()->m_Modules;
for( ; Module != NULL; Module = Module->Next() )
{
MODULE* pt_aux = Module->Next();
for( ; pt_aux != NULL; pt_aux = pt_aux->Next() )
{
if( Module->m_Reference->m_Text.CmpNoCase( pt_aux->m_Reference->m_Text ) == 0 )
{
list.Add( Module->m_Reference->m_Text );
nberr++;
break;
}
}
}
/* Search for the missing module by the net list. */
list.Add( _( "Missing:" ) );
for( ii = 0; ii < NbModulesNetListe; ii++ )
{
Module = (MODULE*) GetBoard()->m_Modules;
for( ; Module != NULL; Module = Module->Next() )
{
if( Module->m_Reference->m_Text.CmpNoCase( tmp[ii] ) == 0 )
{
break;
}
}
if( Module == NULL ) // Module missing, not found in board
{
list.Add( tmp[ii] );
nberr++;
}
}
/* Search for modules not in net list. */
list.Add( _( "Not in Netlist:" ) );
Module = GetBoard()->m_Modules;
for( ; Module != NULL; Module = Module->Next() )
{
for( ii = 0; ii < NbModulesNetListe; ii++ )
{
if( Module->m_Reference->m_Text.CmpNoCase( tmp[ii] ) == 0 )
{
break; /* Module is in net list. */
}
}
if( ii == NbModulesNetListe ) /* Module not found in netlist */
{
list.Add( Module->m_Reference->m_Text );
nberr++;
}
}
wxSingleChoiceDialog dlg( this, wxEmptyString, _( "Check Modules" ), list, NULL,
wxCHOICEDLG_STYLE & ~wxCANCEL );
dlg.ShowModal();
}
/**
* Function BuildFootprintListFromNetlist
* Fill BufName with footprints names read from the netlist.
* @param aNetlistFilename = netlist full file name
2010-12-29 17:47:32 +00:00
* @param aBufName = wxArrayString to fill with footprint names
* @return Footprint count, or -1 if netlist file cannot opened
*/
int NETLIST_READER::BuildFootprintListFromNetlist( const wxString& aNetlistFilename,
wxArrayString& aBufName )
{
int nb_modules_lus;
int State, Comment;
char* Text, * LibModName;
FILE* netfile = OpenNetlistFile( aNetlistFilename );
if( netfile == NULL )
return -1;
FILE_LINE_READER netlineReader( netfile, aNetlistFilename );
char* Line = netlineReader;
State = 0; Comment = 0;
nb_modules_lus = 0;
while( netlineReader.ReadLine() )
{
Text = StrPurge( Line );
if( Comment )
{
if( ( Text = strchr( Text, '}' ) ) == NULL )
continue;
Comment = 0;
}
if( *Text == '{' ) /* Comments. */
{
Comment = 1;
if( ( Text = strchr( Text, '}' ) ) == NULL )
continue;
}
if( *Text == '(' )
State++;
if( *Text == ')' )
State--;
if( State == 2 )
{
int Error = 0;
if( strtok( Line, " ()\t\n" ) == NULL )
Error = 1; /* TimeStamp */
if( ( LibModName = strtok( NULL, " ()\t\n" ) ) == NULL )
Error = 1;
/* Load the name of the component. */
if( ( Text = strtok( NULL, " ()\t\n" ) ) == NULL )
Error = 1;
nb_modules_lus++;
aBufName.Add( FROM_UTF8( Text ) );
continue;
}
if( State >= 3 )
{
State--;
}
}
return nb_modules_lus;
}
/*
* function readModulesComponentsTable
* read the *.cmp file ( filename in m_cmplistFullName )
* giving the equivalence modules / components
* return true and the module name in aModuleName, false if not found
2008-02-19 00:28:42 +00:00
*
* param aCmpIdent = component identification: schematic reference or time stamp
* param aModuleName the footprint name corresponding to the component identification
* Example file:
2008-02-19 00:28:42 +00:00
*
* Cmp-Mod V01 Genere by Pcbnew 29/10/2003-13: 11:6 *
* BeginCmp
* TimeStamp = /322D3011;
* Reference = BUS1;
* ValeurCmp = BUSPC;
* IdModule = BUS_PC;
* EndCmp
2008-02-19 00:28:42 +00:00
*
* BeginCmp
* TimeStamp = /32307DE2/AA450F67;
* Reference = C1;
* ValeurCmp = 47uF;
* IdModule = CP6;
* EndCmp
2008-02-19 00:28:42 +00:00
*
*/
bool NETLIST_READER::readModulesComponentsTable( const wxString* aCmpIdent, wxString& aModuleName )
{
wxString refcurrcmp; // Stores value read from line like Reference = BUS1;
wxString timestamp; // Stores value read from line like TimeStamp = /32307DE2/AA450F67;
wxString idmod; // Stores value read from line like IdModule = CP6;
FILE* cmpFile = wxFopen( m_cmplistFullName, wxT( "rt" ) );
if( cmpFile == NULL )
{
wxString msg;
msg.Printf( _( "File <%s> not found, use Netlist for lib module selection" ),
GetChars( m_cmplistFullName ) );
DisplayError( NULL, msg, 20 );
return false;
}
FILE_LINE_READER netlineReader( cmpFile, m_cmplistFullName ); // netlineReader dtor will close cmpFile
wxString buffer;
wxString value;
while( netlineReader.ReadLine() )
{
buffer = FROM_UTF8( netlineReader.Line() );
if( ! buffer.StartsWith( wxT("BeginCmp") ) )
continue;
/* Begin component description. */
refcurrcmp.Empty();
idmod.Empty();
timestamp.Empty();
while( netlineReader.ReadLine() )
{
buffer = FROM_UTF8( netlineReader.Line() );
if( buffer.StartsWith( wxT("EndCmp") ) )
break;
// store string value, stored between '=' and ';' delimiters.
value = buffer.AfterFirst( '=' );
value = value.BeforeLast( ';');
value.Trim(true);
value.Trim(false);
if( buffer.StartsWith( wxT("Reference") ) )
{
refcurrcmp = value;
continue;
}
if( buffer.StartsWith( wxT("IdModule =" ) ) )
{
idmod = value;
continue;
}
if( buffer.StartsWith( wxT("TimeStamp =" ) ) )
{
timestamp = value;
continue;
}
}
// Check if this component is the right component:
if( m_UseTimeStamp ) // Use schematic timestamp to locate the footprint
{
if( aCmpIdent->CmpNoCase( timestamp ) == 0 && !timestamp.IsEmpty() )
{ // Found
aModuleName = idmod;
return true;
}
}
else // Use schematic reference to locate the footprint
{
if( aCmpIdent->CmpNoCase( refcurrcmp ) == 0 ) // Found!
{
aModuleName = idmod;
return true;
}
}
}
return true;
}
/* Load new modules from library.
* If a new module is already loaded it is duplicated, which avoids multiple
* unnecessary disk or net access to read libraries.
*/
void NETLIST_READER::loadNewModules()
{
MODULE_INFO* ref, * cmp;
MODULE* Module = NULL;
wxPoint ModuleBestPosition;
BOARD* pcb = m_pcbframe->GetBoard();
if( m_newModulesList.size() == 0 )
return;
sort( m_newModulesList.begin(), m_newModulesList.end(), SortByLibName );
// Calculate the footprint "best" position:
if( pcb->ComputeBoundingBox( true ) )
{
ModuleBestPosition = pcb->m_BoundaryBox.GetEnd();
ModuleBestPosition.y += 5000;
}
ref = cmp = m_newModulesList[0];
for( unsigned ii = 0; ii < m_newModulesList.size(); ii++ )
{
cmp = m_newModulesList[ii];
if( (ii == 0) || ( ref->m_LibName != cmp->m_LibName) )
2008-12-06 08:21:54 +00:00
{
/* New footprint : must be loaded from a library */
Module = m_pcbframe->Get_Librairie_Module( wxEmptyString,
cmp->m_LibName,
false );
ref = cmp;
if( Module == NULL )
{
wxString msg;
msg.Printf( _( "Component [%s]: footprint <%s> not found" ),
GetChars( cmp->m_CmpName ),
GetChars( cmp->m_LibName ) );
DisplayError( NULL, msg );
continue;
}
Module->SetPosition( ModuleBestPosition );
/* Update schematic links : reference "Time Stamp" and schematic
*hierarchical path */
Module->m_Reference->m_Text = cmp->m_CmpName;
Module->m_TimeStamp = GetTimeStamp();
Module->m_Path = cmp->m_TimeStampPath;
}
else
{
/* Footprint already loaded from a library, duplicate it (faster)
*/
if( Module == NULL )
continue; /* Module does not exist in library. */
2008-12-06 08:21:54 +00:00
MODULE* newmodule = new MODULE( pcb );
newmodule->Copy( Module );
2008-12-06 08:21:54 +00:00
pcb->Add( newmodule, ADD_APPEND );
2008-12-06 08:21:54 +00:00
Module = newmodule;
Module->m_Reference->m_Text = cmp->m_CmpName;
Module->m_TimeStamp = GetTimeStamp();
Module->m_Path = cmp->m_TimeStampPath;
}
}
}