From 6fddeb89bcc4854fced17aea2c6aa47227336a63 Mon Sep 17 00:00:00 2001 From: jean-pierre charras Date: Sat, 28 Jan 2012 20:25:59 +0100 Subject: [PATCH] New netlist format: Cvpcb: code cleaning. Pcbew: minor enhancements --- cvpcb/class_cvpcb.cpp | 41 ---------- cvpcb/cvpcb.h | 36 +++------ cvpcb/readschematicnetlist.cpp | 20 +---- cvpcb/writenetlistpcbnew.cpp | 43 ----------- pcbnew/netlist.cpp | 7 ++ pcbnew/netlist_reader.h | 37 +++++++++ pcbnew/netlist_reader_firstformat.cpp | 106 ++++++++++++++++++++++---- pcbnew/netlist_reader_kicad.cpp | 16 +++- 8 files changed, 160 insertions(+), 146 deletions(-) diff --git a/cvpcb/class_cvpcb.cpp b/cvpcb/class_cvpcb.cpp index fd865aaf54..2583e3b046 100644 --- a/cvpcb/class_cvpcb.cpp +++ b/cvpcb/class_cvpcb.cpp @@ -9,53 +9,12 @@ #include -PIN::PIN() -{ - m_Index = 0; /* Variable used by types of netlists. */ - m_Type = 0; /* Electrical type. */ -} - - bool operator<( const PIN& item1, const PIN& item2 ) { return StrNumCmp( item1.m_Number, item2.m_Number, 4, true ) < 0; } -bool operator==( const PIN& item1, const PIN& item2 ) -{ - return ( item1.m_Number == item2.m_Number ); -} - - -bool same_pin_number( const PIN* item1, const PIN* item2 ) -{ - wxASSERT( item1 != NULL && item2 != NULL ); - - return ( item1->m_Number == item2->m_Number ); -} - - -bool same_pin_net( const PIN* item1, const PIN* item2 ) -{ - wxASSERT( item1 != NULL && item2 != NULL ); - - return ( item1->m_Net == item2->m_Net ); -} - - -COMPONENT::COMPONENT() -{ - m_Num = 0; - m_Multi = 0; -} - - -COMPONENT::~COMPONENT() -{ -} - - bool operator<( const COMPONENT& item1, const COMPONENT& item2 ) { return StrNumCmp( item1.m_Reference, item2.m_Reference, INT_MAX, true ) < 0; diff --git a/cvpcb/cvpcb.h b/cvpcb/cvpcb.h index 6f5414dda7..48bdb41934 100644 --- a/cvpcb/cvpcb.h +++ b/cvpcb/cvpcb.h @@ -22,14 +22,12 @@ class PIN { public: - int m_Index; /* Type of net list. */ - int m_Type; /* Electrical connection type. */ wxString m_Net; /* Name of net. */ wxString m_Number; wxString m_Name; - wxString m_Repere; /* Formats used by net lister. */ - PIN(); + PIN() {}; + ~PIN() {}; }; typedef boost::ptr_vector< PIN > PIN_LIST; @@ -37,29 +35,19 @@ typedef boost::ptr_vector< PIN > PIN_LIST; /* PIN object list sort function. */ extern bool operator<( const PIN& item1, const PIN& item2 ); -/* PIN uniqueness test function. */ -extern bool operator==( const PIN& item1, const PIN& item2 ); - -extern bool same_pin_number( const PIN* item1, const PIN* item2 ); -extern bool same_pin_net( const PIN* item1, const PIN* item2 ); - - class COMPONENT { public: - int m_Num; /* Component number. */ - int m_Multi; /* Part if component has multiple parts. */ - PIN_LIST m_Pins; /* List of component pins. */ - wxString m_Reference; /* Reference designator: U3, R5 ... */ - wxString m_Value; /* Value: 7400, 47K ... */ - wxString m_TimeStamp; /* Time stamp ("00000000" if absent) */ - wxString m_Module; /* Footprint (module) name. */ - wxString m_Repere; /* Net list format */ - wxArrayString m_FootprintFilter; /* List of allowed footprints (wildcards - * allowed ). If empty: no filtering */ + wxString m_Reference; // Reference designator: U3, R5 + wxString m_Value; // Value: 7400, 47K + wxString m_TimeStamp; // Time stamp ( default value = "00000000") + wxString m_Module; // Footprint (module) name. + wxArrayString m_FootprintFilter;// List of allowed footprints (wildcards + // allowed ). If empty: no filtering + PIN_LIST m_Pins; // List of component pins. - COMPONENT(); - ~COMPONENT(); + COMPONENT() {}; + ~COMPONENT() {}; }; typedef boost::ptr_vector< COMPONENT > COMPONENT_LIST; @@ -77,6 +65,4 @@ extern const wxString FootprintAliasFileWildcard; extern const wxString titleLibLoadError; -void Plume( int state ); - #endif /* __CVPCB_H__ */ diff --git a/cvpcb/readschematicnetlist.cpp b/cvpcb/readschematicnetlist.cpp index a3ed60d53d..977bf358a6 100644 --- a/cvpcb/readschematicnetlist.cpp +++ b/cvpcb/readschematicnetlist.cpp @@ -16,9 +16,6 @@ #include -#define SEPARATEUR '|' /* Separator character in NetList */ - - static int ReadPinConnection( FILE_LINE_READER& aNetlistReader, COMPONENT* CurrentCmp ); static int ReadFootprintFilterList( FILE_LINE_READER& aNetlistReader, COMPONENT_LIST& aComponentsList ); @@ -83,8 +80,7 @@ static int ReadFootprintFilterList( FILE_LINE_READER& aNetlistReader, COMPONENT_ */ int CVPCB_MAINFRAME::ReadSchematicNetlist() { - char alim[1024]; - int idx, jj, k, l; + int idx, jj, k; char cbuffer[BUFFER_CHAR_SIZE]; /* temporary storage */ char* ptchar; COMPONENT* Cmp; @@ -203,8 +199,6 @@ int CVPCB_MAINFRAME::ReadSchematicNetlist() for( jj = 0; idx < k; idx++ ) { - if( Line[idx] == SEPARATEUR ) - break; cbuffer[jj++] = Line[idx]; } cbuffer[jj] = 0; @@ -212,15 +206,6 @@ int CVPCB_MAINFRAME::ReadSchematicNetlist() if( m_isEESchemaNetlist && strnicmp( cbuffer, "$noname", 7 ) != 0 ) Cmp->m_Module = FROM_UTF8(cbuffer); - if( (Line[++idx] == '(') && (Line[k - 1] == ')' ) ) - { - idx++; l = 0; - while( k - 1 > idx ) - alim[l++] = Line[idx++]; - } - else - idx = k; - /* Search component reference */ while( Line[idx] != ' ' && Line[idx] ) idx++; @@ -243,8 +228,7 @@ int CVPCB_MAINFRAME::ReadSchematicNetlist() while( Line[idx] == ' ' && Line[idx] ) idx++; - /** goto beginning of value */ - + // goto beginning of value for( jj = 0 ; ; idx++ ) { if( (Line[idx] == ' ') || (Line[idx] == '\n') || (Line[idx] == '\r') || Line[idx] == 0) diff --git a/cvpcb/writenetlistpcbnew.cpp b/cvpcb/writenetlistpcbnew.cpp index 16577e9b33..74bbd43f20 100644 --- a/cvpcb/writenetlistpcbnew.cpp +++ b/cvpcb/writenetlistpcbnew.cpp @@ -15,48 +15,6 @@ static void WriteFootprintFilterInfos( FILE* dest, COMPONENT_LIST& list ); - -static void RemoveDuplicatePins( COMPONENT& component ) -{ - PIN_LIST::iterator i; - PIN *pin1, *pin2; - wxString msg; - - if( component.m_Pins.size() <= 1 ) - return; - - i = component.m_Pins.begin(); - pin1 = &(*i); - ++i; - - while( i != component.m_Pins.end() ) - { - pin2 = &(*i); - - wxASSERT( pin2 != NULL ); - - if( !same_pin_number( pin1, pin2 ) ) - { - pin1 = pin2; - ++i; - continue; - } - - if( !same_pin_net( pin1, pin2 ) ) - { - msg.Printf( _( "Component %s %s pin %s : Different Nets" ), - GetChars( component.m_Reference ), - GetChars( component.m_Value ), - pin1->m_Number.GetData() ); - DisplayError( NULL, msg, 60 ); - } - pin1 = pin2; - i = component.m_Pins.erase( i ); - delete pin2; - } -} - - /** * Create KiCad net list file. * @@ -88,7 +46,6 @@ int CVPCB_MAINFRAME::GenNetlistPcbnew( FILE* file,bool isEESchemaNetlist ) fprintf( file, "%s\n", TO_UTF8( component.m_Value ) ); component.m_Pins.sort(); - RemoveDuplicatePins( component ); BOOST_FOREACH( PIN& pin, component.m_Pins ) { diff --git a/pcbnew/netlist.cpp b/pcbnew/netlist.cpp index bad6148344..05fda51748 100644 --- a/pcbnew/netlist.cpp +++ b/pcbnew/netlist.cpp @@ -166,6 +166,7 @@ bool PCB_EDIT_FRAME::ReadPcbNetlist( const wxString& aNetlistFullFilename, netList_Reader.m_UseTimeStamp = aSelect_By_Timestamp; netList_Reader.m_ChangeFootprints = aChangeFootprint; netList_Reader.m_UseCmpFile = useCmpfile; + netList_Reader.ReadFootprintFilterSetOpt( true ); netList_Reader.SetFilesnames( aNetlistFullFilename, aCmpFullFileName ); bool success = netList_Reader.ReadNetList( netfile ); @@ -277,6 +278,9 @@ bool NETLIST_READER::InitializeModules() readModuleComponentLinkfile(); } + if( m_pcbframe == NULL ) + return true; + for( unsigned ii = 0; ii < m_modulesInNetlist.size(); ii++ ) { MODULE_INFO* currmod_info = m_modulesInNetlist[ii]; @@ -326,6 +330,9 @@ bool NETLIST_READER::InitializeModules() void NETLIST_READER::TestFootprintsMatchingAndExchange() { + if( m_pcbframe == NULL ) + return; + for( MODULE* module = m_pcbframe->GetBoard()->m_Modules; module; module = module->Next() ) { // Search for the corresponding module info diff --git a/pcbnew/netlist_reader.h b/pcbnew/netlist_reader.h index e394fa733f..8c351dcb07 100644 --- a/pcbnew/netlist_reader.h +++ b/pcbnew/netlist_reader.h @@ -53,6 +53,7 @@ public: wxString m_Reference; wxString m_Value; wxString m_TimeStamp; + wxArrayString m_FootprintFilter; public: MODULE_INFO( const wxString& libname, const wxString& cmpname, @@ -95,6 +96,7 @@ private: // (must be loaded from libraries) bool m_buildModuleListOnly; // if true read netlist, populates m_modulesInNetlist // but do not read and change nets and modules on board + bool m_readFootprintFilter; // if true read footprint filter section enum typenetlist m_typeNetlist; // type opt the netlist currently read public: @@ -113,6 +115,7 @@ public: NETLIST_READER( PCB_EDIT_FRAME* aFrame, wxTextCtrl* aMessageWindow = NUL m_ChangeFootprints = false; m_UseCmpFile = true; m_buildModuleListOnly = false; + m_readFootprintFilter = false; m_typeNetlist = NETLIST_TYPE_UNSPECIFIED; } @@ -141,6 +144,19 @@ public: NETLIST_READER( PCB_EDIT_FRAME* aFrame, wxTextCtrl* aMessageWindow = NUL m_modulesInNetlist.push_back( aModInfo ); } + /** + * Function ReadFootprintFilterSetOpt + * Set to true or false the read footprint filter option + * When this option is false, the footprint filter section is ignored + * When this option is true, the footprint filter section is read, + * an filter strings are stored in module info + * @param aOpt = the value of option + */ + void ReadFootprintFilterSetOpt( bool aOpt ) + { + m_readFootprintFilter = aOpt; + } + /** * Function BuildModuleListOnlySetOpt * Set to true or false the Build Module List Only option @@ -218,6 +234,27 @@ public: NETLIST_READER( PCB_EDIT_FRAME* aFrame, wxTextCtrl* aMessageWindow = NUL */ bool ReadOldFmtdNetList( FILE* aFile ); + /** + * Function ReadOldFmtFootprintFilterList + * Read the section "Allowed footprints" like: + * { Allowed footprints by component: + * $component R11 + * R? + * SM0603 + * SM0805 + * R?-* + * SM1206 + * $endlist + * $endfootprintlist + * } + * + * And add the strings giving the footprint filter to m_FootprintFilter + * of the corresponding module info + *

This section is used by CvPcb, and is not useful in Pcbnew, + * therefore it it not always read

+ */ + bool ReadOldFmtFootprintFilterList( FILE_LINE_READER& aNetlistReader ); + /** * Function ReadKicadNetList * The main function to read a netlist (new netlist format, using S expressions), diff --git a/pcbnew/netlist_reader_firstformat.cpp b/pcbnew/netlist_reader_firstformat.cpp index c5e1324df3..ab71d0a619 100644 --- a/pcbnew/netlist_reader_firstformat.cpp +++ b/pcbnew/netlist_reader_firstformat.cpp @@ -1,6 +1,7 @@ /** * @file pcbnew/netlist_reader_firstformat.cpp */ + /* * This program source code file is part of KiCad, a free EDA CAD application. * @@ -43,9 +44,10 @@ #include #include +#include // constants used by ReadOldFmtNetlistModuleDescr(): -#define BUILDLIST true +#define BUILDLIST true #define READMODULE false @@ -72,11 +74,12 @@ */ bool NETLIST_READER::ReadOldFmtdNetList( FILE* aFile ) { - int state = 0; + int state = 0; bool is_comment = false; /* First, read the netlist: Build the list of footprints found in netlist */ + // netlineReader dtor will close aFile FILE_LINE_READER netlineReader( aFile, m_netlistFullName ); @@ -92,10 +95,15 @@ bool NETLIST_READER::ReadOldFmtdNetList( FILE* aFile ) is_comment = false; } - if( *line == '{' ) // Start Comment + if( *line == '{' ) // Start Comment or Pcbnew info section { is_comment = true; - + if( m_readFootprintFilter && state == 0 && + (strnicmp( line, "{ Allowed footprints", 20 ) == 0) ) + { + ReadOldFmtFootprintFilterList( netlineReader ); + continue; + } if( ( line = strchr( line, '}' ) ) == NULL ) continue; } @@ -119,13 +127,13 @@ bool NETLIST_READER::ReadOldFmtdNetList( FILE* aFile ) } if( BuildModuleListOnlyOpt() ) - return true; // at this point, the module list is read and built. + return true; // at this point, the module list is read and built. // Load new footprints bool success = InitializeModules(); - if( ! success ) - wxMessageBox( _("Some footprints are not found in libraries") ); + if( !success ) + wxMessageBox( _( "Some footprints are not found in libraries" ) ); TestFootprintsMatchingAndExchange(); @@ -184,7 +192,7 @@ bool NETLIST_READER::ReadOldFmtdNetList( FILE* aFile ) /* Function ReadOldFmtNetlistModuleDescr * Read the beginning of a footprint description, from the netlist * and add a module info to m_modulesInNetlist - * Analyze lines like: + * Analyze the first line of a component description in netlist like: * ( /40C08647 $noname R20 4.7K {Lib=R} * (1 VCC) * (2 MODB_1) @@ -195,8 +203,8 @@ MODULE* NETLIST_READER::ReadOldFmtNetlistModuleDescr( char* aText, bool aBuildLi char* text; wxString timeStampPath; // the full time stamp read from netlist wxString footprintName; // the footprint name read from netlist - wxString cmpValue; // the component value read from netlist - wxString cmpReference; // the component schematic reference read from netlist + wxString cmpValue; // the component value read from netlist + wxString cmpReference; // the component schematic reference read from netlist bool error = false; char line[1024]; @@ -236,13 +244,13 @@ MODULE* NETLIST_READER::ReadOldFmtNetlistModuleDescr( char* aText, bool aBuildLi if( aBuildList ) { MODULE_INFO* mod_info = new MODULE_INFO( footprintName, cmpReference, - cmpValue, timeStampPath ); + cmpValue, timeStampPath ); AddModuleInfo( mod_info ); return NULL; } // search the module loaded on board - // reference and time stamps are already updated so we can used search by reference + // reference and time stamps are already updated so we can use search by reference only MODULE* module = m_pcbframe->GetBoard()->FindModuleByReference( cmpReference ); if( module == NULL ) { @@ -266,8 +274,8 @@ MODULE* NETLIST_READER::ReadOldFmtNetlistModuleDescr( char* aText, bool aBuildLi */ bool NETLIST_READER::SetPadNetName( char* aText ) { - char* p; - char line[256]; + char* p; + char line[256]; if( m_currModule == NULL ) return false; @@ -284,8 +292,8 @@ bool NETLIST_READER::SetPadNetName( char* aText ) wxString netName = FROM_UTF8( p ); - bool found = false; - for( D_PAD* pad = m_currModule->m_Pads; pad; pad = pad->Next() ) + bool found = false; + for( D_PAD* pad = m_currModule->m_Pads; pad; pad = pad->Next() ) { wxString padName = pad->GetPadName(); @@ -313,3 +321,69 @@ bool NETLIST_READER::SetPadNetName( char* aText ) return found; } + + +/* + * Read the section "Allowed footprints" like: + * { Allowed footprints by component: + * $component R11 + * R? + * SM0603 + * SM0805 + * R?-* + * SM1206 + * $endlist + * $endfootprintlist + * } + * + * And add the strings giving the footprint filter to m_FootprintFilter + * of the corresponding module info + * This section is used by CvPcb, and is not useful in Pcbnew, + * therefore it it not always read + */ +bool NETLIST_READER::ReadOldFmtFootprintFilterList( FILE_LINE_READER& aNetlistReader ) +{ + wxString cmpRef; + MODULE_INFO* mod_info = NULL; + + while( aNetlistReader.ReadLine() ) + { + const char* Line = aNetlistReader.Line(); + + if( strnicmp( Line, "$endlist", 8 ) == 0 ) // end of list for the current component + { + mod_info = NULL; + continue; + } + if( strnicmp( Line, "$endfootprintlist", 4 ) == 0 ) + // End of this section + return 0; + + if( strnicmp( Line, "$component", 10 ) == 0 ) // New component reference found + { + cmpRef = FROM_UTF8( Line + 11 ); + cmpRef.Trim( true ); + cmpRef.Trim( false ); + + // Search the current component in module info list: + BOOST_FOREACH( MODULE_INFO * &component, m_modulesInNetlist ) + { + if( component->m_Reference == cmpRef ) + { + mod_info = component; + break; + } + } + } + else if( mod_info ) + { + // Add new filter to list + wxString fp = FROM_UTF8( Line + 1 ); + fp.Trim( false ); + fp.Trim( true ); + mod_info->m_FootprintFilter.Add( fp ); + } + } + + return true; +} diff --git a/pcbnew/netlist_reader_kicad.cpp b/pcbnew/netlist_reader_kicad.cpp index cfd605e0d7..8c32f961c2 100644 --- a/pcbnew/netlist_reader_kicad.cpp +++ b/pcbnew/netlist_reader_kicad.cpp @@ -81,16 +81,26 @@ public: bool NETLIST_READER::ReadKicadNetList( FILE* aFile ) { - bool success = true; BOARD * brd = m_pcbframe->GetBoard(); // netlineReader dtor will close aFile FILE_LINE_READER netlineReader( aFile, m_netlistFullName ); NETLIST_READER_KICAD_PARSER netlist_parser( &netlineReader, this ); - netlist_parser.Parse( brd ); + try + { + netlist_parser.Parse( brd ); + } + catch( IO_ERROR& ioe ) + { + ioe.errorText += '\n'; + ioe.errorText += _("Netlist error."); - return success; + wxMessageBox( ioe.errorText ); + return false; + } + + return true; }