2012-01-26 09:37:36 +00:00
|
|
|
/**
|
2013-05-28 16:54:59 +00:00
|
|
|
* @file pcbnew/legacy_netlist_reader.cpp
|
2012-01-26 09:37:36 +00:00
|
|
|
*/
|
2012-01-28 19:25:59 +00:00
|
|
|
|
2012-01-26 09:37:36 +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-03-25 01:18:16 +00:00
|
|
|
* Copyright (C) 1992-2017 KiCad Developers, see AUTHORS.txt for contributors.
|
2012-01-26 09:37:36 +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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <fctsys.h>
|
|
|
|
#include <richio.h>
|
2013-04-25 16:29:35 +00:00
|
|
|
#include <kicad_string.h>
|
2012-01-26 09:37:36 +00:00
|
|
|
|
2013-09-14 20:33:22 +00:00
|
|
|
#include <pcb_netlist.h>
|
2012-01-26 09:37:36 +00:00
|
|
|
#include <netlist_reader.h>
|
|
|
|
|
2015-02-21 00:41:10 +00:00
|
|
|
void LEGACY_NETLIST_READER::LoadNetlist() throw ( IO_ERROR, PARSE_ERROR, boost::bad_pointer )
|
2012-01-26 09:37:36 +00:00
|
|
|
{
|
2013-04-25 16:29:35 +00:00
|
|
|
int state = 0;
|
|
|
|
bool is_comment = false;
|
|
|
|
COMPONENT* component = NULL;
|
2012-01-26 09:37:36 +00:00
|
|
|
|
2013-04-25 16:29:35 +00:00
|
|
|
while( m_lineReader->ReadLine() )
|
2012-01-26 09:37:36 +00:00
|
|
|
{
|
2013-04-25 16:29:35 +00:00
|
|
|
char* line = StrPurge( m_lineReader->Line() );
|
2012-01-26 09:37:36 +00:00
|
|
|
|
|
|
|
if( is_comment ) // Comments in progress
|
|
|
|
{
|
|
|
|
// Test for end of the current comment
|
|
|
|
if( ( line = strchr( line, '}' ) ) == NULL )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
is_comment = false;
|
|
|
|
}
|
2013-04-25 16:29:35 +00:00
|
|
|
|
2012-01-28 19:25:59 +00:00
|
|
|
if( *line == '{' ) // Start Comment or Pcbnew info section
|
2012-01-26 09:37:36 +00:00
|
|
|
{
|
|
|
|
is_comment = true;
|
2013-04-25 16:29:35 +00:00
|
|
|
|
|
|
|
if( m_loadFootprintFilters && state == 0
|
2016-08-16 08:27:09 +00:00
|
|
|
&& (strncasecmp( line, "{ Allowed footprints", 20 ) == 0) )
|
2012-01-28 19:25:59 +00:00
|
|
|
{
|
2013-04-25 16:29:35 +00:00
|
|
|
loadFootprintFilters();
|
2012-01-28 19:25:59 +00:00
|
|
|
continue;
|
|
|
|
}
|
2013-04-25 16:29:35 +00:00
|
|
|
|
2012-01-26 09:37:36 +00:00
|
|
|
if( ( line = strchr( line, '}' ) ) == NULL )
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( *line == '(' )
|
|
|
|
state++;
|
|
|
|
|
|
|
|
if( *line == ')' )
|
|
|
|
state--;
|
|
|
|
|
|
|
|
if( state == 2 )
|
|
|
|
{
|
2013-04-25 16:29:35 +00:00
|
|
|
component = loadComponent( line );
|
2012-01-26 09:37:36 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-04-25 16:29:35 +00:00
|
|
|
if( state >= 3 ) // Pad descriptions are read here.
|
2012-01-26 09:37:36 +00:00
|
|
|
{
|
2013-04-25 16:29:35 +00:00
|
|
|
wxASSERT( component != NULL );
|
2012-03-17 03:01:53 +00:00
|
|
|
|
2013-04-25 16:29:35 +00:00
|
|
|
loadNet( line, component );
|
2012-01-26 09:37:36 +00:00
|
|
|
state--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-25 16:29:35 +00:00
|
|
|
if( m_footprintReader )
|
2012-02-01 19:49:37 +00:00
|
|
|
{
|
2013-04-25 16:29:35 +00:00
|
|
|
m_footprintReader->Load( m_netlist );
|
2012-02-01 19:49:37 +00:00
|
|
|
}
|
2012-01-26 09:37:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-17 18:47:21 +00:00
|
|
|
COMPONENT* LEGACY_NETLIST_READER::loadComponent( char* aText )
|
|
|
|
throw( PARSE_ERROR, boost::bad_pointer )
|
2012-01-26 09:37:36 +00:00
|
|
|
{
|
|
|
|
char* text;
|
2013-04-25 16:29:35 +00:00
|
|
|
wxString msg;
|
|
|
|
wxString timeStamp; // the full time stamp read from netlist
|
2013-04-28 20:20:40 +00:00
|
|
|
wxString footprintName; // the footprint name read from netlist
|
2013-04-25 16:29:35 +00:00
|
|
|
wxString value; // the component value read from netlist
|
|
|
|
wxString reference; // the component schematic reference designator read from netlist
|
2013-04-28 20:20:40 +00:00
|
|
|
wxString name; // the name of component that was placed in the schematic
|
2012-01-26 09:37:36 +00:00
|
|
|
char line[1024];
|
|
|
|
|
2015-08-10 08:17:22 +00:00
|
|
|
strncpy( line, aText, sizeof(line)-1 );
|
|
|
|
line[sizeof(line)-1] = '\0';
|
2012-01-26 09:37:36 +00:00
|
|
|
|
2013-04-25 16:29:35 +00:00
|
|
|
value = wxT( "~" );
|
2012-01-26 09:37:36 +00:00
|
|
|
|
2013-04-25 16:29:35 +00:00
|
|
|
// Sample component line: /40C08647 $noname R20 4.7K {Lib=R}
|
2012-01-26 09:37:36 +00:00
|
|
|
|
|
|
|
// Read time stamp (first word)
|
|
|
|
if( ( text = strtok( line, " ()\t\n" ) ) == NULL )
|
2013-04-25 16:29:35 +00:00
|
|
|
{
|
|
|
|
msg = _( "Cannot parse time stamp in component section of netlist." );
|
|
|
|
THROW_PARSE_ERROR( msg, m_lineReader->GetSource(), line, m_lineReader->LineNumber(),
|
|
|
|
m_lineReader->Length() );
|
|
|
|
}
|
|
|
|
|
|
|
|
timeStamp = FROM_UTF8( text );
|
2012-01-26 09:37:36 +00:00
|
|
|
|
|
|
|
// Read footprint name (second word)
|
|
|
|
if( ( text = strtok( NULL, " ()\t\n" ) ) == NULL )
|
2013-04-25 16:29:35 +00:00
|
|
|
{
|
2013-04-28 20:20:40 +00:00
|
|
|
msg = _( "Cannot parse footprint name in component section of netlist." );
|
2013-04-25 16:29:35 +00:00
|
|
|
THROW_PARSE_ERROR( msg, m_lineReader->GetSource(), aText, m_lineReader->LineNumber(),
|
|
|
|
m_lineReader->Length() );
|
|
|
|
}
|
2012-01-26 09:37:36 +00:00
|
|
|
|
2013-04-28 20:20:40 +00:00
|
|
|
footprintName = FROM_UTF8( text );
|
|
|
|
|
|
|
|
// The footprint name will have to be looked up in the *.cmp file.
|
|
|
|
if( footprintName == wxT( "$noname" ) )
|
|
|
|
footprintName = wxEmptyString;
|
2012-01-26 09:37:36 +00:00
|
|
|
|
2013-04-25 16:29:35 +00:00
|
|
|
// Read schematic reference designator (third word)
|
2012-01-26 09:37:36 +00:00
|
|
|
if( ( text = strtok( NULL, " ()\t\n" ) ) == NULL )
|
|
|
|
{
|
2013-04-25 16:29:35 +00:00
|
|
|
msg = _( "Cannot parse reference designator in component section of netlist." );
|
|
|
|
THROW_PARSE_ERROR( msg, m_lineReader->GetSource(), aText, m_lineReader->LineNumber(),
|
|
|
|
m_lineReader->Length() );
|
2012-01-26 09:37:36 +00:00
|
|
|
}
|
|
|
|
|
2013-04-25 16:29:35 +00:00
|
|
|
reference = FROM_UTF8( text );
|
|
|
|
|
|
|
|
// Read schematic value (forth word)
|
|
|
|
if( ( text = strtok( NULL, " ()\t\n" ) ) == NULL )
|
2012-01-26 09:37:36 +00:00
|
|
|
{
|
2013-04-25 16:29:35 +00:00
|
|
|
msg = _( "Cannot parse value in component section of netlist." );
|
|
|
|
THROW_PARSE_ERROR( msg, m_lineReader->GetSource(), aText, m_lineReader->LineNumber(),
|
|
|
|
m_lineReader->Length() );
|
2012-01-26 09:37:36 +00:00
|
|
|
}
|
|
|
|
|
2013-04-25 16:29:35 +00:00
|
|
|
value = FROM_UTF8( text );
|
|
|
|
|
2013-04-28 20:20:40 +00:00
|
|
|
// Read component name (fifth word) {Lib=C}
|
2013-05-25 10:41:24 +00:00
|
|
|
// This is an optional field (a comment), which does not always exists
|
|
|
|
if( ( text = strtok( NULL, " ()\t\n" ) ) != NULL )
|
2013-04-28 20:20:40 +00:00
|
|
|
{
|
2013-05-25 10:41:24 +00:00
|
|
|
name = FROM_UTF8( text ).AfterFirst( wxChar( '=' ) ).BeforeLast( wxChar( '}' ) );
|
2013-04-28 20:20:40 +00:00
|
|
|
}
|
|
|
|
|
2016-11-20 23:35:08 +00:00
|
|
|
LIB_ID fpid;
|
2013-09-08 18:31:21 +00:00
|
|
|
|
2013-09-17 00:52:08 +00:00
|
|
|
if( !footprintName.IsEmpty() )
|
2017-03-25 01:18:16 +00:00
|
|
|
fpid.SetLibItemName( TO_UTF8( footprintName ) );
|
2013-09-17 00:52:08 +00:00
|
|
|
|
2013-09-08 18:31:21 +00:00
|
|
|
COMPONENT* component = new COMPONENT( fpid, reference, value, timeStamp );
|
2013-04-28 20:20:40 +00:00
|
|
|
component->SetName( name );
|
2013-04-25 16:29:35 +00:00
|
|
|
m_netlist->AddComponent( component );
|
|
|
|
return component;
|
2012-01-26 09:37:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-25 16:29:35 +00:00
|
|
|
void LEGACY_NETLIST_READER::loadNet( char* aText, COMPONENT* aComponent ) throw( PARSE_ERROR )
|
2012-01-26 09:37:36 +00:00
|
|
|
{
|
2013-04-25 16:29:35 +00:00
|
|
|
wxString msg;
|
|
|
|
char* p;
|
|
|
|
char line[256];
|
2012-01-26 09:37:36 +00:00
|
|
|
|
2013-04-25 16:29:35 +00:00
|
|
|
strncpy( line, aText, sizeof( line ) );
|
2015-01-18 11:49:32 +00:00
|
|
|
line[ sizeof(line) - 1 ] = '\0';
|
2012-01-26 09:37:36 +00:00
|
|
|
|
|
|
|
if( ( p = strtok( line, " ()\t\n" ) ) == NULL )
|
2013-04-25 16:29:35 +00:00
|
|
|
{
|
|
|
|
msg = _( "Cannot parse pin name in component net section of netlist." );
|
|
|
|
THROW_PARSE_ERROR( msg, m_lineReader->GetSource(), line, m_lineReader->LineNumber(),
|
|
|
|
m_lineReader->Length() );
|
|
|
|
}
|
2012-01-26 09:37:36 +00:00
|
|
|
|
|
|
|
wxString pinName = FROM_UTF8( p );
|
|
|
|
|
|
|
|
if( ( p = strtok( NULL, " ()\t\n" ) ) == NULL )
|
|
|
|
{
|
2013-04-25 16:29:35 +00:00
|
|
|
msg = _( "Cannot parse net name in component net section of netlist." );
|
|
|
|
THROW_PARSE_ERROR( msg, m_lineReader->GetSource(), line, m_lineReader->LineNumber(),
|
|
|
|
m_lineReader->Length() );
|
2012-01-26 09:37:36 +00:00
|
|
|
}
|
|
|
|
|
2013-04-25 16:29:35 +00:00
|
|
|
wxString netName = FROM_UTF8( p );
|
|
|
|
|
|
|
|
if( (char) netName[0] == '?' ) // ? indicates no net connected to pin.
|
|
|
|
netName = wxEmptyString;
|
2012-01-26 09:37:36 +00:00
|
|
|
|
2013-04-25 16:29:35 +00:00
|
|
|
aComponent->AddNet( pinName, netName );
|
2012-01-26 09:37:36 +00:00
|
|
|
}
|
2012-01-28 19:25:59 +00:00
|
|
|
|
|
|
|
|
2013-04-25 16:29:35 +00:00
|
|
|
void LEGACY_NETLIST_READER::loadFootprintFilters() throw( IO_ERROR, PARSE_ERROR )
|
2012-01-28 19:25:59 +00:00
|
|
|
{
|
2013-04-25 16:29:35 +00:00
|
|
|
wxArrayString filters;
|
|
|
|
wxString cmpRef;
|
|
|
|
char* line;
|
2013-05-25 10:41:24 +00:00
|
|
|
COMPONENT* component = NULL; // Suppress compil warning
|
2012-01-28 19:25:59 +00:00
|
|
|
|
2013-04-25 16:29:35 +00:00
|
|
|
while( ( line = m_lineReader->ReadLine() ) != NULL )
|
2012-01-28 19:25:59 +00:00
|
|
|
{
|
2016-08-16 08:27:09 +00:00
|
|
|
if( strncasecmp( line, "$endlist", 8 ) == 0 ) // end of list for the current component
|
2012-01-28 19:25:59 +00:00
|
|
|
{
|
2013-04-25 16:29:35 +00:00
|
|
|
wxASSERT( component != NULL );
|
|
|
|
component->SetFootprintFilters( filters );
|
|
|
|
component = NULL;
|
|
|
|
filters.Clear();
|
2012-01-28 19:25:59 +00:00
|
|
|
continue;
|
|
|
|
}
|
2013-04-25 16:29:35 +00:00
|
|
|
|
2016-08-16 08:27:09 +00:00
|
|
|
if( strncasecmp( line, "$endfootprintlist", 4 ) == 0 )
|
2012-01-28 19:25:59 +00:00
|
|
|
// End of this section
|
2013-04-25 16:29:35 +00:00
|
|
|
return;
|
2012-01-28 19:25:59 +00:00
|
|
|
|
2016-08-16 08:27:09 +00:00
|
|
|
if( strncasecmp( line, "$component", 10 ) == 0 ) // New component reference found
|
2012-01-28 19:25:59 +00:00
|
|
|
{
|
2012-11-27 13:50:01 +00:00
|
|
|
cmpRef = FROM_UTF8( line + 11 );
|
2012-01-28 19:25:59 +00:00
|
|
|
cmpRef.Trim( true );
|
|
|
|
cmpRef.Trim( false );
|
|
|
|
|
2013-04-25 16:29:35 +00:00
|
|
|
component = m_netlist->GetComponentByReference( cmpRef );
|
|
|
|
|
|
|
|
// Cannot happen if the netlist is valid.
|
|
|
|
if( component == NULL )
|
2012-01-28 19:25:59 +00:00
|
|
|
{
|
2013-04-25 16:29:35 +00:00
|
|
|
wxString msg;
|
|
|
|
msg.Printf( _( "Cannot find component \'%s\' in footprint filter section "
|
|
|
|
"of netlist." ), GetChars( cmpRef ) );
|
|
|
|
THROW_PARSE_ERROR( msg, m_lineReader->GetSource(), line, m_lineReader->LineNumber(),
|
|
|
|
m_lineReader->Length() );
|
2012-01-28 19:25:59 +00:00
|
|
|
}
|
|
|
|
}
|
2013-04-25 16:29:35 +00:00
|
|
|
else
|
2012-01-28 19:25:59 +00:00
|
|
|
{
|
|
|
|
// Add new filter to list
|
2012-11-27 13:50:01 +00:00
|
|
|
wxString fp = FROM_UTF8( line + 1 );
|
2012-01-28 19:25:59 +00:00
|
|
|
fp.Trim( false );
|
|
|
|
fp.Trim( true );
|
2013-04-25 16:29:35 +00:00
|
|
|
filters.Add( fp );
|
2012-01-28 19:25:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|