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.
|
2021-07-19 23:56:05 +00:00
|
|
|
* Copyright (C) 2013 Wayne Stambaugh <stambaughw@gmail.com>.
|
|
|
|
* Copyright (C) 1992-2021 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 <richio.h>
|
2021-07-29 09:56:22 +00:00
|
|
|
#include <string_utils.h>
|
2012-01-26 09:37:36 +00:00
|
|
|
|
2019-11-17 12:58:38 +00:00
|
|
|
#include "pcb_netlist.h"
|
|
|
|
#include "netlist_reader.h"
|
2012-01-26 09:37:36 +00:00
|
|
|
|
2017-06-08 21:47:21 +00:00
|
|
|
void LEGACY_NETLIST_READER::LoadNetlist()
|
2012-01-26 09:37:36 +00:00
|
|
|
{
|
2013-04-25 16:29:35 +00:00
|
|
|
int state = 0;
|
|
|
|
bool is_comment = false;
|
2021-07-19 23:56:05 +00:00
|
|
|
COMPONENT* component = nullptr;
|
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
|
2021-07-19 23:56:05 +00:00
|
|
|
if( ( line = strchr( line, '}' ) ) == nullptr )
|
2012-01-26 09:37:36 +00:00
|
|
|
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
|
|
|
|
2021-07-19 23:56:05 +00:00
|
|
|
if( ( line = strchr( line, '}' ) ) == nullptr )
|
2012-01-26 09:37:36 +00:00
|
|
|
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
|
|
|
{
|
2021-07-19 23:56:05 +00:00
|
|
|
wxASSERT( component != nullptr );
|
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 )
|
2012-01-26 09:37:36 +00:00
|
|
|
{
|
|
|
|
char* text;
|
2013-04-25 16:29:35 +00:00
|
|
|
wxString msg;
|
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
|
|
|
|
2020-02-20 12:11:04 +00:00
|
|
|
// Sample component line: /68183921-93a5-49ac-91b0-49d05a0e1647 $noname R20 4.7K {Lib=R}
|
2012-01-26 09:37:36 +00:00
|
|
|
|
|
|
|
// Read time stamp (first word)
|
2021-07-19 23:56:05 +00:00
|
|
|
if( ( text = strtok( line, " ()\t\n" ) ) == nullptr )
|
2013-04-25 16:29:35 +00:00
|
|
|
{
|
2017-12-24 15:04:02 +00:00
|
|
|
msg = _( "Cannot parse time stamp in symbol section of netlist." );
|
2013-04-25 16:29:35 +00:00
|
|
|
THROW_PARSE_ERROR( msg, m_lineReader->GetSource(), line, m_lineReader->LineNumber(),
|
|
|
|
m_lineReader->Length() );
|
|
|
|
}
|
|
|
|
|
2020-02-21 22:20:42 +00:00
|
|
|
KIID_PATH path( FROM_UTF8( text ) );
|
2012-01-26 09:37:36 +00:00
|
|
|
|
|
|
|
// Read footprint name (second word)
|
2021-07-19 23:56:05 +00:00
|
|
|
if( ( text = strtok( nullptr, " ()\t\n" ) ) == nullptr )
|
2013-04-25 16:29:35 +00:00
|
|
|
{
|
2017-12-24 15:04:02 +00:00
|
|
|
msg = _( "Cannot parse footprint name in symbol 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)
|
2021-07-19 23:56:05 +00:00
|
|
|
if( ( text = strtok( nullptr, " ()\t\n" ) ) == nullptr )
|
2012-01-26 09:37:36 +00:00
|
|
|
{
|
2017-12-24 15:04:02 +00:00
|
|
|
msg = _( "Cannot parse reference designator in symbol 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-25 16:29:35 +00:00
|
|
|
reference = FROM_UTF8( text );
|
|
|
|
|
|
|
|
// Read schematic value (forth word)
|
2021-07-19 23:56:05 +00:00
|
|
|
if( ( text = strtok( nullptr, " ()\t\n" ) ) == nullptr )
|
2012-01-26 09:37:36 +00:00
|
|
|
{
|
2017-12-24 15:04:02 +00:00
|
|
|
msg = _( "Cannot parse value in symbol 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-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
|
2021-07-19 23:56:05 +00:00
|
|
|
if( ( text = strtok( nullptr, " ()\t\n" ) ) != nullptr )
|
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-07-24 19:02:59 +00:00
|
|
|
fpid.SetLibItemName( footprintName );
|
2013-09-17 00:52:08 +00:00
|
|
|
|
2021-02-21 04:03:56 +00:00
|
|
|
COMPONENT* component = new COMPONENT( fpid, reference, value, path, {} );
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-06-08 21:47:21 +00:00
|
|
|
void LEGACY_NETLIST_READER::loadNet( char* aText, COMPONENT* aComponent )
|
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
|
|
|
|
2021-07-19 23:56:05 +00:00
|
|
|
if( ( p = strtok( line, " ()\t\n" ) ) == nullptr )
|
2013-04-25 16:29:35 +00:00
|
|
|
{
|
2017-12-24 15:04:02 +00:00
|
|
|
msg = _( "Cannot parse pin name in symbol net section of netlist." );
|
2013-04-25 16:29:35 +00:00
|
|
|
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 );
|
|
|
|
|
2021-07-19 23:56:05 +00:00
|
|
|
if( ( p = strtok( nullptr, " ()\t\n" ) ) == nullptr )
|
2012-01-26 09:37:36 +00:00
|
|
|
{
|
2017-12-24 15:04:02 +00:00
|
|
|
msg = _( "Cannot parse net name in symbol net section of netlist." );
|
2013-04-25 16:29:35 +00:00
|
|
|
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
|
|
|
|
2021-01-21 21:46:03 +00:00
|
|
|
aComponent->AddNet( pinName, netName, wxEmptyString, wxEmptyString );
|
2012-01-26 09:37:36 +00:00
|
|
|
}
|
2012-01-28 19:25:59 +00:00
|
|
|
|
|
|
|
|
2017-06-08 21:47:21 +00:00
|
|
|
void LEGACY_NETLIST_READER::loadFootprintFilters()
|
2012-01-28 19:25:59 +00:00
|
|
|
{
|
2013-04-25 16:29:35 +00:00
|
|
|
wxArrayString filters;
|
|
|
|
wxString cmpRef;
|
|
|
|
char* line;
|
2021-07-19 23:56:05 +00:00
|
|
|
COMPONENT* component = nullptr; // Suppress compile warning
|
2012-01-28 19:25:59 +00:00
|
|
|
|
2021-07-19 23:56:05 +00:00
|
|
|
while( ( line = m_lineReader->ReadLine() ) != nullptr )
|
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
|
|
|
{
|
2021-07-19 23:56:05 +00:00
|
|
|
wxASSERT( component != nullptr );
|
2013-04-25 16:29:35 +00:00
|
|
|
component->SetFootprintFilters( filters );
|
2021-07-19 23:56:05 +00:00
|
|
|
component = nullptr;
|
2013-04-25 16:29:35 +00:00
|
|
|
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.
|
2021-07-19 23:56:05 +00:00
|
|
|
if( component == nullptr )
|
2012-01-28 19:25:59 +00:00
|
|
|
{
|
2013-04-25 16:29:35 +00:00
|
|
|
wxString msg;
|
2021-06-16 22:35:00 +00:00
|
|
|
msg.Printf( _( "Cannot find symbol %s in footprint filter section of netlist." ),
|
|
|
|
cmpRef );
|
2013-04-25 16:29:35 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-07-19 23:56:05 +00:00
|
|
|
|
|
|
|
// LocalWords: EDA Charras pcb netlist noname cmp endlist
|
|
|
|
// LocalWords: endfootprintlist
|