2012-02-01 20:17:43 +00:00
|
|
|
/**
|
2013-05-28 16:54:59 +00:00
|
|
|
* @file netlist_reader.cpp
|
2012-02-01 20:17:43 +00:00
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 1992-2011 Jean-Pierre Charras.
|
2016-11-20 23:35:08 +00:00
|
|
|
* Copyright (C) 2013-2016 Wayne Stambaugh <stambaughw@verizon.net>.
|
2017-07-24 19:02:59 +00:00
|
|
|
* Copyright (C) 1992-2016 KiCad Developers, see change_log.txt for contributors.
|
2012-02-01 20:17:43 +00:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-04-25 16:29:35 +00:00
|
|
|
#include <kicad_string.h>
|
|
|
|
#include <reporter.h>
|
2012-02-01 20:17:43 +00:00
|
|
|
|
2013-09-14 20:33:22 +00:00
|
|
|
#include <pcb_netlist.h>
|
2013-04-25 16:29:35 +00:00
|
|
|
#include <netlist_reader.h>
|
2012-02-01 20:17:43 +00:00
|
|
|
#include <class_module.h>
|
|
|
|
|
2013-04-25 16:29:35 +00:00
|
|
|
#include <wx/regex.h>
|
2012-02-01 20:17:43 +00:00
|
|
|
|
|
|
|
|
2013-04-25 16:29:35 +00:00
|
|
|
NETLIST_READER::~NETLIST_READER()
|
2012-02-01 20:17:43 +00:00
|
|
|
{
|
2015-06-07 18:18:45 +00:00
|
|
|
delete m_lineReader;
|
|
|
|
delete m_footprintReader;
|
2013-04-25 16:29:35 +00:00
|
|
|
}
|
2012-02-01 20:17:43 +00:00
|
|
|
|
|
|
|
|
2013-04-25 16:29:35 +00:00
|
|
|
NETLIST_READER::NETLIST_FILE_T NETLIST_READER::GuessNetlistFileType( LINE_READER* aLineReader )
|
|
|
|
{
|
2013-05-25 10:41:24 +00:00
|
|
|
// Orcad Pcb2 netlist format starts by "( {", followed by an unknown comment,
|
|
|
|
// depending on the tool which created the file
|
|
|
|
wxRegEx reOrcad( wxT( "(?i)[ ]*\\([ \t]+{+" ), wxRE_ADVANCED );
|
2013-04-25 16:29:35 +00:00
|
|
|
wxASSERT( reOrcad.IsValid() );
|
2013-05-25 10:41:24 +00:00
|
|
|
// Our legacy netlist format starts by "# EESchema Netlist "
|
2013-04-25 16:29:35 +00:00
|
|
|
wxRegEx reLegacy( wxT( "(?i)#[ \t]+EESchema[ \t]+Netlist[ \t]+" ), wxRE_ADVANCED );
|
|
|
|
wxASSERT( reLegacy.IsValid() );
|
2013-05-25 10:41:24 +00:00
|
|
|
// Our new netlist format starts by "(export (version "
|
2013-04-25 16:29:35 +00:00
|
|
|
wxRegEx reKicad( wxT( "[ ]*\\(export[ ]+" ), wxRE_ADVANCED );
|
|
|
|
wxASSERT( reKicad.IsValid() );
|
|
|
|
|
|
|
|
wxString line;
|
2012-02-01 20:17:43 +00:00
|
|
|
|
2013-04-25 16:29:35 +00:00
|
|
|
while( aLineReader->ReadLine() )
|
|
|
|
{
|
|
|
|
line = FROM_UTF8( aLineReader->Line() );
|
|
|
|
|
2013-05-25 10:41:24 +00:00
|
|
|
if( reLegacy.Matches( line ) )
|
2013-04-25 16:29:35 +00:00
|
|
|
return LEGACY;
|
|
|
|
else if( reKicad.Matches( line ) )
|
|
|
|
return KICAD;
|
2013-05-25 10:41:24 +00:00
|
|
|
else if( reOrcad.Matches( line ) )
|
|
|
|
return ORCAD;
|
2012-02-01 20:17:43 +00:00
|
|
|
}
|
2013-04-25 16:29:35 +00:00
|
|
|
|
|
|
|
return UNKNOWN;
|
2012-02-01 20:17:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-25 16:29:35 +00:00
|
|
|
NETLIST_READER* NETLIST_READER::GetNetlistReader( NETLIST* aNetlist,
|
|
|
|
const wxString& aNetlistFileName,
|
|
|
|
const wxString& aCompFootprintFileName )
|
2012-02-01 20:17:43 +00:00
|
|
|
{
|
2013-04-25 16:29:35 +00:00
|
|
|
wxASSERT( aNetlist != NULL );
|
|
|
|
|
2016-05-29 15:02:34 +00:00
|
|
|
std::unique_ptr< FILE_LINE_READER > file_rdr(new FILE_LINE_READER( aNetlistFileName ) );
|
2012-02-01 20:17:43 +00:00
|
|
|
|
2015-06-07 18:18:45 +00:00
|
|
|
NETLIST_FILE_T type = GuessNetlistFileType( file_rdr.get() );
|
|
|
|
file_rdr->Rewind();
|
2012-02-01 20:17:43 +00:00
|
|
|
|
2013-04-25 16:29:35 +00:00
|
|
|
// The component footprint link reader is NULL if no file name was specified.
|
2016-05-29 15:02:34 +00:00
|
|
|
std::unique_ptr<CMP_READER> cmp_rdr( aCompFootprintFileName.IsEmpty() ?
|
2015-06-07 18:18:45 +00:00
|
|
|
NULL :
|
|
|
|
new CMP_READER( new FILE_LINE_READER( aCompFootprintFileName ) ) );
|
2012-02-01 20:17:43 +00:00
|
|
|
|
2013-04-25 16:29:35 +00:00
|
|
|
switch( type )
|
2012-02-01 20:17:43 +00:00
|
|
|
{
|
2013-04-25 16:29:35 +00:00
|
|
|
case LEGACY:
|
|
|
|
case ORCAD:
|
2015-06-07 18:18:45 +00:00
|
|
|
return new LEGACY_NETLIST_READER( file_rdr.release(), aNetlist, cmp_rdr.release() );
|
2013-04-25 16:29:35 +00:00
|
|
|
|
|
|
|
case KICAD:
|
2015-06-07 18:18:45 +00:00
|
|
|
return new KICAD_NETLIST_READER( file_rdr.release(), aNetlist, cmp_rdr.release() );
|
2013-04-25 16:29:35 +00:00
|
|
|
|
|
|
|
default: // Unrecognized format:
|
|
|
|
break;
|
2012-02-01 20:17:43 +00:00
|
|
|
}
|
|
|
|
|
2013-04-25 16:29:35 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-06-08 21:47:21 +00:00
|
|
|
bool CMP_READER::Load( NETLIST* aNetlist )
|
2013-04-25 16:29:35 +00:00
|
|
|
{
|
2013-05-25 16:10:19 +00:00
|
|
|
wxCHECK_MSG( aNetlist != NULL,true, wxT( "No netlist passed to CMP_READER::Load()" ) );
|
2013-04-25 16:29:35 +00:00
|
|
|
|
|
|
|
wxString reference; // Stores value read from line like Reference = BUS1;
|
|
|
|
wxString timestamp; // Stores value read from line like TimeStamp = /32307DE2/AA450F67;
|
|
|
|
wxString footprint; // Stores value read from line like IdModule = CP6;
|
2012-02-01 20:17:43 +00:00
|
|
|
wxString buffer;
|
|
|
|
wxString value;
|
|
|
|
|
2013-05-25 16:10:19 +00:00
|
|
|
bool ok = true;
|
2013-04-25 16:29:35 +00:00
|
|
|
|
|
|
|
while( m_lineReader->ReadLine() )
|
2012-02-01 20:17:43 +00:00
|
|
|
{
|
2013-04-25 16:29:35 +00:00
|
|
|
buffer = FROM_UTF8( m_lineReader->Line() );
|
2012-02-01 20:17:43 +00:00
|
|
|
|
2013-04-25 16:29:35 +00:00
|
|
|
if( !buffer.StartsWith( wxT( "BeginCmp" ) ) )
|
2012-02-01 20:17:43 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// Begin component description.
|
2013-04-25 16:29:35 +00:00
|
|
|
reference.Empty();
|
2012-02-01 20:17:43 +00:00
|
|
|
footprint.Empty();
|
|
|
|
timestamp.Empty();
|
|
|
|
|
2013-04-25 16:29:35 +00:00
|
|
|
while( m_lineReader->ReadLine() )
|
2012-02-01 20:17:43 +00:00
|
|
|
{
|
2013-04-25 16:29:35 +00:00
|
|
|
buffer = FROM_UTF8( m_lineReader->Line() );
|
2012-02-01 20:17:43 +00:00
|
|
|
|
2013-04-25 16:29:35 +00:00
|
|
|
if( buffer.StartsWith( wxT( "EndCmp" ) ) )
|
2012-02-01 20:17:43 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
// store string value, stored between '=' and ';' delimiters.
|
|
|
|
value = buffer.AfterFirst( '=' );
|
2013-04-25 16:29:35 +00:00
|
|
|
value = value.BeforeLast( ';' );
|
|
|
|
value.Trim( true );
|
|
|
|
value.Trim( false );
|
2012-02-01 20:17:43 +00:00
|
|
|
|
2013-04-25 16:29:35 +00:00
|
|
|
if( buffer.StartsWith( wxT( "Reference" ) ) )
|
2012-02-01 20:17:43 +00:00
|
|
|
{
|
2013-04-25 16:29:35 +00:00
|
|
|
reference = value;
|
2012-02-01 20:17:43 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-04-25 16:29:35 +00:00
|
|
|
if( buffer.StartsWith( wxT( "IdModule =" ) ) )
|
2012-02-01 20:17:43 +00:00
|
|
|
{
|
|
|
|
footprint = value;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-04-25 16:29:35 +00:00
|
|
|
if( buffer.StartsWith( wxT( "TimeStamp =" ) ) )
|
2012-02-01 20:17:43 +00:00
|
|
|
{
|
|
|
|
timestamp = value;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-25 16:29:35 +00:00
|
|
|
// Find the corresponding item in component list:
|
|
|
|
COMPONENT* component = aNetlist->GetComponentByReference( reference );
|
2012-02-01 20:17:43 +00:00
|
|
|
|
2013-09-14 20:33:22 +00:00
|
|
|
// The corresponding component could no longer existing in the netlist. This
|
2015-09-22 17:05:07 +00:00
|
|
|
// can happen when it is removed from schematic and still exists in footprint
|
2013-09-14 20:33:22 +00:00
|
|
|
// assignment list. This is an usual case during the life of a design.
|
2013-05-25 15:02:16 +00:00
|
|
|
if( component )
|
2013-09-14 20:33:22 +00:00
|
|
|
{
|
2016-11-20 23:35:08 +00:00
|
|
|
LIB_ID fpid;
|
2013-09-17 00:52:08 +00:00
|
|
|
|
2018-07-26 14:38:30 +00:00
|
|
|
if( !footprint.IsEmpty() && fpid.Parse( footprint, LIB_ID::ID_PCB, true ) >= 0 )
|
2013-09-17 00:52:08 +00:00
|
|
|
{
|
|
|
|
wxString error;
|
2019-08-04 08:33:54 +00:00
|
|
|
error.Printf( _( "Invalid footprint ID in\nfile: \"%s\"\nline: %d" ),
|
|
|
|
m_lineReader->GetSource(), m_lineReader->LineNumber() );
|
2013-09-17 00:52:08 +00:00
|
|
|
|
|
|
|
THROW_IO_ERROR( error );
|
|
|
|
}
|
|
|
|
|
2016-11-20 23:35:08 +00:00
|
|
|
// For checking purpose, store the existing LIB_ID (if any) in the alternate fpid copy
|
|
|
|
// if this existing LIB_ID differs from the LIB_ID read from the .cmp file.
|
|
|
|
// CvPcb can ask for user to chose the right LIB_ID.
|
|
|
|
// It happens if the LIB_ID was modified outside CvPcb.
|
2014-10-28 15:15:39 +00:00
|
|
|
if( fpid != component->GetFPID() && !component->GetFPID().empty() )
|
|
|
|
component->SetAltFPID( component->GetFPID() );
|
|
|
|
|
2013-09-17 00:52:08 +00:00
|
|
|
component->SetFPID( fpid );
|
2013-09-14 20:33:22 +00:00
|
|
|
}
|
2013-05-25 16:10:19 +00:00
|
|
|
else
|
2013-09-14 20:33:22 +00:00
|
|
|
{
|
2013-05-25 16:10:19 +00:00
|
|
|
ok = false; // can be used to display a warning in Pcbnew.
|
2013-09-14 20:33:22 +00:00
|
|
|
}
|
2012-02-01 20:17:43 +00:00
|
|
|
}
|
2013-05-25 16:10:19 +00:00
|
|
|
|
|
|
|
return ok;
|
2012-02-01 20:17:43 +00:00
|
|
|
}
|