netlist_reader_kicad.cpp 13.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
/**
 * @file pcbnew/netlist_reader_kicad.cpp
 */
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 1992-2011 Jean-Pierre Charras.
 * Copyright (C) 1992-2011 KiCad Developers, see change_log.txt for contributors.
 *
 * 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 <wx/wx.h>
#include <netlist_lexer.h>  // netlist_lexer is common to Eeschema and Pcbnew
#include <netlist_reader.h>

using namespace NL_T;

/**
 * Class PCB_PLOT_PARAMS_PARSER
 * is the parser class for PCB_PLOT_PARAMS.
 */
class NETLIST_READER_KICAD_PARSER : public NETLIST_LEXER
{
private:
    T token;
    NETLIST_READER * netlist_reader;

public:
    NETLIST_READER_KICAD_PARSER( FILE_LINE_READER* aReader, NETLIST_READER *aNetlistReader );

    /**
     * Function Parse
     * parse the full netlist
     */
    void Parse( BOARD * aBrd ) throw( IO_ERROR, PARSE_ERROR );

    /**
     * Function ParseComp
     * parse the comp description like
     * (comp (ref P1)
     * (value DB25FEMELLE)
     * (footprint DB25FC)
     * (libsource (lib conn) (part DB25))
     * (sheetpath (names /) (tstamps /))
     * (tstamp 3256759C))
     */
63
    COMPONENT_INFO* ParseComp() throw( IO_ERROR, PARSE_ERROR );
64

65 66

    /**
67
     * Function ParseKicadLibpartList
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
     * Read the section "libparts" like:
     * (libparts
     *   (libpart (lib device) (part C)
     *     (description "Condensateur non polarise")
     *     (footprints
     *       (fp SM*)
     *       (fp C?)
     *       (fp C1-1))
     *     (fields
     *       (field (name Reference) C)
     *       (field (name Value) C))
     *     (pins
     *       (pin (num 1) (name ~) (type passive))
     *       (pin (num 2) (name ~) (type passive))))
     *
     *  And add the strings giving the footprint filter (subsection footprints)
     *  of the corresponding module info
     *  <p>This section is used by CvPcb, and is not useful in Pcbnew,
     *  therefore it it not always read </p>
     */
88
    void ParseKicadLibpartList() throw( IO_ERROR, PARSE_ERROR );
89 90 91 92 93 94 95 96 97 98 99

    /**
     * Function ParseNet
     * Parses a section like
     * (net (code 20) (name /PC-A0)
     *  (node (ref BUS1) (pin 62))
     *  (node (ref U3) (pin 3))
     *  (node (ref U9) (pin M6)))
     *
     * and set the corresponfings pads netnames
     */
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
    void ParseNet( BOARD * aBrd ) throw( IO_ERROR, PARSE_ERROR );

    /**
     * Function SkipCurrent
     * Skip the current token level, i.e
     * search for the RIGHT parenthesis which closes the current description
     */
    void SkipCurrent() throw( IO_ERROR, PARSE_ERROR );

    // Useful for debug only:
    const char* getTokenName( T aTok )
    {
        return NETLIST_LEXER::TokenName( aTok );
    }
};


bool NETLIST_READER::ReadKicadNetList( FILE* aFile )
{
119
    BOARD * brd = m_pcbframe ? m_pcbframe->GetBoard() : NULL;
120 121 122 123 124

        // netlineReader dtor will close aFile
    FILE_LINE_READER netlineReader( aFile, m_netlistFullName );
    NETLIST_READER_KICAD_PARSER netlist_parser( &netlineReader, this );

125 126 127 128 129 130 131 132 133 134 135 136
    try
    {
        netlist_parser.Parse( brd );
    }
    catch( IO_ERROR& ioe )
    {
        ioe.errorText += '\n';
        ioe.errorText += _("Netlist error.");

        wxMessageBox( ioe.errorText );
        return false;
    }
137

138
    return true;
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
}



// NETLIST_READER_KICAD_PARSER
NETLIST_READER_KICAD_PARSER::NETLIST_READER_KICAD_PARSER( FILE_LINE_READER* aReader,
                                                          NETLIST_READER *aNetlistReader ) :
    NETLIST_LEXER( aReader )
{
    netlist_reader = aNetlistReader;
}

/**
 * Function SkipCurrent
 * Skip the current token level, i.e
 * search for the RIGHT parenthesis which closes the current description
 */
void NETLIST_READER_KICAD_PARSER::SkipCurrent() throw( IO_ERROR, PARSE_ERROR )
{
    int curr_level = 0;
    while( ( token = NextTok() ) != T_EOF )
    {
        if( token == T_LEFT )
            curr_level--;
        if( token == T_RIGHT )
        {
            curr_level++;
            if( curr_level > 0 )
                return;
        }
    }
}


void NETLIST_READER_KICAD_PARSER::Parse( BOARD * aBrd )
    throw( IO_ERROR, PARSE_ERROR )
{
    wxString text;
    while( ( token = NextTok() ) != T_EOF )
    {
        if( token == T_LEFT )
            token = NextTok();
        if( token == T_components )
        {
            // The section comp starts here.
            while( ( token = NextTok() ) != T_RIGHT )
            {
                if( token == T_LEFT )
                    token = NextTok();
                if( token == T_comp )
                {
                    // A comp section if found. Read it
191 192
                    COMPONENT_INFO* cmp_info = ParseComp();
                    netlist_reader->AddModuleInfo( cmp_info );
193 194
                }
            }
195
            if( netlist_reader->BuildModuleListOnlyOpt() )
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
                return; // at this point, the module list is read and built.
            // Load new footprints
            netlist_reader->InitializeModules();
            netlist_reader->TestFootprintsMatchingAndExchange();
        }

        if( token == T_nets )
        {
            // The section nets starts here.
            while( ( token = NextTok() ) != T_RIGHT )
            {
                if( token == T_LEFT )
                    token = NextTok();
                if( token == T_net )
                {
                    // A net section if found. Read it
                    ParseNet( aBrd );
                }
            }
        }
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230

        if( token == T_libparts && netlist_reader->ReadLibpartSectionOpt() )
        {
            // The section libparts starts here.
            while( ( token = NextTok() ) != T_RIGHT )
            {
                if( token == T_LEFT )
                    token = NextTok();
                if( token == T_libpart )
                {
                    // A libpart section if found. Read it
                    ParseKicadLibpartList();
                }
            }
        }
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
    }
}

void NETLIST_READER_KICAD_PARSER::ParseNet( BOARD * aBrd )
    throw( IO_ERROR, PARSE_ERROR )
{
    /* Parses a section like
     * (net (code 20) (name /PC-A0)
     *  (node (ref BUS1) (pin 62))
     *  (node (ref U3) (pin 3))
     *  (node (ref U9) (pin M6)))
     */

    wxString code;
    wxString name;
    wxString cmpref;
    wxString pin;
    D_PAD * pad = NULL;
    int nodecount = 0;
    // The token net was read, so the next data is (code <number>)
    while( (token = NextTok()) != T_RIGHT )
    {
        if( token == T_LEFT )
            token = NextTok();
        switch( token )
        {
        case T_code:
            NeedSYMBOLorNUMBER();
            code = FROM_UTF8( CurText() );
            NeedRIGHT();
        break;

        case T_name:
            NeedSYMBOLorNUMBER();
            name = FROM_UTF8( CurText() );
            NeedRIGHT();
            if( name.IsEmpty() )      // Give a dummy net name like N-000109
                name = wxT("N-00000") + code;
            break;

        case T_node:
            while( (token = NextTok()) != T_RIGHT )
            {
                if( token == T_LEFT )
                    token = NextTok();
                switch( token )
                {
                case T_ref:
                    NeedSYMBOLorNUMBER();
                    cmpref = FROM_UTF8( CurText() );
                    NeedRIGHT();
                    break;

                case T_pin:
                    NeedSYMBOLorNUMBER();
                    pin = FROM_UTF8( CurText() );
                    NeedRIGHT();
                    break;

                default:
                    SkipCurrent();
                    break;
                }
            }
            pad = netlist_reader->SetPadNetName( cmpref, pin, name );
            nodecount++;
            break;

        default:
            SkipCurrent();
            break;
        }
    }

305
    // When there is only one item in net, clear pad netname
306 307 308 309 310
    if( nodecount < 2 && pad )
        pad->SetNetname( wxEmptyString );
}


311
COMPONENT_INFO* NETLIST_READER_KICAD_PARSER::ParseComp()
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
    throw( IO_ERROR, PARSE_ERROR )
{
   /* Parses a section like
     * (comp (ref P1)
     * (value DB25FEMELLE)
     * (footprint DB25FC)
     * (libsource (lib conn) (part DB25))
     * (sheetpath (names /) (tstamps /))
     * (tstamp 3256759C))
     *
     * other fields (unused) are skipped
     * A component need a reference, value, foorprint name and a full time stamp
     * The full time stamp is the sheetpath time stamp + the component time stamp
     */
    wxString ref;
    wxString value;
    wxString footprint;
329
    wxString libpart;
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
    wxString pathtimestamp, timestamp;
    // The token comp was read, so the next data is (ref P1)

    while( (token = NextTok()) != T_RIGHT )
    {
        if( token == T_LEFT )
            token = NextTok();
        switch( token )
        {
        case T_ref:
            NeedSYMBOLorNUMBER();
            ref = FROM_UTF8( CurText() );
            NeedRIGHT();
            break;

        case T_value:
            NeedSYMBOLorNUMBER();
            value = FROM_UTF8( CurText() );
            NeedRIGHT();
            break;

        case T_footprint:
            NeedSYMBOLorNUMBER();
            footprint = FROM_UTF8( CurText() );
            NeedRIGHT();
            break;

        case T_libsource:
358 359 360 361 362 363 364 365 366 367 368 369 370 371
            // Read libsource
            while( (token = NextTok()) != T_RIGHT )
            {
                if( token == T_LEFT )
                    token = NextTok();
                if( token == T_part )
                {
                    NeedSYMBOLorNUMBER();
                    libpart = FROM_UTF8( CurText() );
                    NeedRIGHT();
                }
                else
                    SkipCurrent();
            }
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
            break;

        case T_sheetpath:
            while( ( token = NextTok() ) != T_tstamps );
            NeedSYMBOLorNUMBER();
            pathtimestamp = FROM_UTF8( CurText() );
            NeedRIGHT();
            NeedRIGHT();
            break;

        case T_tstamp:
            NeedSYMBOLorNUMBER();
            timestamp = FROM_UTF8( CurText() );
            NeedRIGHT();
            break;

        default:
            // Skip not used data (i.e all other tokens)
            SkipCurrent();
            break;
        }
    }
    pathtimestamp += timestamp;
395 396
    COMPONENT_INFO* cmp_info = new COMPONENT_INFO( footprint, ref, value, pathtimestamp );
    cmp_info->m_Libpart = libpart;
397

398
    return cmp_info;
399
}
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418

/* Read the section "libparts" like:
 * (libparts
 *   (libpart (lib device) (part C)
 *     (description "Condensateur non polarise")
 *     (footprints
 *       (fp SM*)
 *       (fp C?)
 *       (fp C1-1))
 *     (fields
 *       (field (name Reference) C)
 *       (field (name Value) C))
 *     (pins
 *       (pin (num 1) (name ~) (type passive))
 *       (pin (num 2) (name ~) (type passive))))
 *
 *  And add the strings giving the footprint filter (subsection footprints)
 *  of the corresponding module info
 */
419
void NETLIST_READER_KICAD_PARSER::ParseKicadLibpartList() throw( IO_ERROR, PARSE_ERROR )
420
{
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
   /* Parses a section like
     *   (libpart (lib device) (part C)
     *     (description "Condensateur non polarise")
     *     (footprints
     *       (fp SM*)
     *       (fp C?)
     *       (fp C1-1))
     *     (fields
     *       (field (name Reference) C)
     *       (field (name Value) C))
     *     (pins
     *       (pin (num 1) (name ~) (type passive))
     *       (pin (num 2) (name ~) (type passive))))
     *
     * Currently footprints section/fp are read and data stored
     * other fields (unused) are skipped
     */
    wxString device;
    wxString filter;
    LIPBART_INFO* libpart_info = NULL;

    // The last token read was libpart, so read the next token
    while( (token = NextTok()) != T_RIGHT )
    {
        if( token == T_LEFT )
            token = NextTok();
        switch( token )
        {
        case T_part:
            NeedSYMBOLorNUMBER();
            device = FROM_UTF8( CurText() );
            NeedRIGHT();
            libpart_info = new LIPBART_INFO( device );
            netlist_reader->AddLibpartInfo( libpart_info );
            break;

        case T_footprints:
            // Ensure "(part C)" was already read
            if( libpart_info == NULL )
                Expecting( T_part );
            // Read all fp elements (footprint filter item)
            while( (token = NextTok()) != T_RIGHT )
            {
                if( token == T_LEFT )
                    token = NextTok();
                if( token != T_fp )
                    Expecting( T_fp );
                NeedSYMBOLorNUMBER();
                filter = FROM_UTF8( CurText() );
                NeedRIGHT();
                libpart_info->m_FootprintFilter.Add( filter );
            }
            break;

        default:
            // Skip not used data (i.e all other tokens)
            SkipCurrent();
            break;
        }
    }
481
}