netlist_reader_firstformat.cpp 10.8 KB
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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 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 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 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 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 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 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
/**
 * @file pcbnew/netlist_reader_firstformat.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
 */


/*
 *  Netlist reader using the first format of pcbnew netlist.
 * This netlist reader build the list of modules found in netlist
 * (list in m_componentsInNetlist)
 * and update pads netnames
 */

#include <fctsys.h>
#include <kicad_string.h>
#include <wxPcbStruct.h>
#include <richio.h>

#include <class_board.h>
#include <class_module.h>
#include <pcbnew.h>

#include <netlist_reader.h>
#include <boost/foreach.hpp>

// constants used by ReadOldFmtNetlistModuleDescr():
#define BUILDLIST  true
#define READMODULE false


/*
 * Function ReadOldFmtdNetList
 * Update footprints (load missing footprints and delete on request extra
 * footprints)
 * Update References, values, "TIME STAMP" and connectivity data
 * return true if Ok
 *
 *  the format of the netlist is something like:
 * # EESchema Netlist Version 1.0 generee le  18/5/2005-12:30:22
 *  (
 *  ( 40C08647 $noname R20 4,7K {Lib=R}
 *  (    1 VCC )
 *  (    2 MODB_1 )
 *  )
 *  ( 40C0863F $noname R18 4,7_k {Lib=R}
 *  (    1 VCC )
 *  (    2 MODA_1 )
 *  )
 *  }
 * #End
 */
bool NETLIST_READER::ReadOldFmtdNetList( FILE* aFile )
{
    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 );

    while( netlineReader.ReadLine() )
    {
        char* line = StrPurge( netlineReader.Line() );

        if( is_comment ) // Comments in progress
        {
            // Test for end of the current comment
            if( ( line = strchr( line, '}' ) ) == NULL )
                continue;

            is_comment = false;
        }
        if( *line == '{' ) // Start Comment or Pcbnew info section
        {
            is_comment = true;
            if( ReadLibpartSectionOpt() && state == 0 &&
                (strnicmp( line, "{ Allowed footprints", 20 ) == 0) )
            {
                ReadOldFmtFootprintFilterList( netlineReader );
                continue;
            }
            if( ( line = strchr( line, '}' ) ) == NULL )
                continue;
        }

        if( *line == '(' )
            state++;

        if( *line == ')' )
            state--;

        if( state == 2 )
        {
            ReadOldFmtNetlistModuleDescr( line, BUILDLIST );
            continue;
        }

        if( state >= 3 ) // First pass: pad descriptions are not read here.
        {
            state--;
        }
    }

    if( IsCvPcbMode() )
    {
        for( ; ; )
        {
            /* Search the beginning of Allowed footprints section */

            if( netlineReader.ReadLine( ) == 0 )
                break;
            char* line = StrPurge( netlineReader.Line() );
            if( strnicmp( line, "{ Allowed footprints", 20 ) == 0 )
            {
                ReadOldFmtFootprintFilterList( netlineReader );
                return true;
            }
        }
        return true;
    }

    if( BuildModuleListOnlyOpt() )
        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" ) );

    TestFootprintsMatchingAndExchange();

    /* Second read , All footprints are on board.
     * Update the schematic info (pad netnames)
     */
    netlineReader.Rewind();
    m_currModule = NULL;
    state = 0;
    is_comment = false;

    while( netlineReader.ReadLine() )
    {
        char* line = StrPurge( netlineReader.Line() );

        if( is_comment )   // we are reading a comment
        {
            // Test for end of the current comment
            if( ( line = strchr( line, '}' ) ) == NULL )
                continue;
            is_comment = false;
        }

        if( *line == '{' ) // this is the beginning of a comment
        {
            is_comment = true;

            if( ( line = strchr( line, '}' ) ) == NULL )
                continue;
        }

        if( *line == '(' )
            state++;

        if( *line == ')' )
            state--;

        if( state == 2 )
        {
            m_currModule = ReadOldFmtNetlistModuleDescr( line, READMODULE );
            continue;
        }

        if( state >= 3 )
        {
            if( m_currModule )
                SetPadNetName( line );
            state--;
        }
    }

    return true;
}


/* Function ReadOldFmtNetlistModuleDescr
 * Read the beginning of a footprint  description, from the netlist
 * and add a module info to m_componentsInNetlist
 * Analyze the first line of a component description in netlist like:
 * ( /40C08647 $noname R20 4.7K {Lib=R}
 * (1 VCC)
 * (2 MODB_1)
 * )
 */
MODULE* NETLIST_READER::ReadOldFmtNetlistModuleDescr( char* aText, bool aBuildList )
{
    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
    bool     error = false;
    char     line[1024];

    strcpy( line, aText );

    cmpValue = wxT( "~" );

    // Read descr line like  /40C08647 $noname R20 4.7K {Lib=R}

    // Read time stamp (first word)
    if( ( text = strtok( line, " ()\t\n" ) ) == NULL )
        error = true;
    else
        timeStampPath = FROM_UTF8( text );

    // Read footprint name (second word)
    if( ( text = strtok( NULL, " ()\t\n" ) ) == NULL )
        error = true;
    else
        footprintName = FROM_UTF8( text );

    // Read schematic reference (third word)
    if( ( text = strtok( NULL, " ()\t\n" ) ) == NULL )
        error = true;
    else
        cmpReference = FROM_UTF8( text );

    // Read schematic value (forth word)
    if( ( text = strtok( NULL, " ()\t\n" ) ) == NULL )
        error = true;
    else
        cmpValue = FROM_UTF8( text );

    if( error )
        return NULL;

    if( aBuildList )
    {
        COMPONENT_INFO* cmp_info = new COMPONENT_INFO( footprintName, cmpReference,
                                                 cmpValue, timeStampPath );
        AddModuleInfo( cmp_info );
        return NULL;
    }

    // search the module loaded on board
    // 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 )
    {
        if( m_messageWindow )
        {
            wxString msg;
            msg.Printf( _( "Component [%s] not found" ), GetChars( cmpReference ) );
            m_messageWindow->AppendText( msg + wxT( "\n" ) );
        }
    }

    return module;
}


/*
 * Function SetPadNetName
 *  Update a pad netname using the current footprint
 *  Line format: ( <pad number> = <net name> )
 *  Param aText = current line read from netlist
 */
bool NETLIST_READER::SetPadNetName( char* aText )
{
    char* p;
    char  line[256];

    if( m_currModule == NULL )
        return false;

    strncpy( line, aText, sizeof(line) );

    if( ( p = strtok( line, " ()\t\n" ) ) == NULL )
        return false;

    wxString pinName = FROM_UTF8( p );

    if( ( p = strtok( NULL, " ()\t\n" ) ) == NULL )
        return false;

    wxString netName = FROM_UTF8( p );

    bool     found = false;
    for( D_PAD* pad = m_currModule->m_Pads; pad; pad = pad->Next() )
    {
        wxString padName = pad->GetPadName();

        if( padName == pinName )
        {
            found = true;
            if( (char) netName[0] != '?' )
                pad->SetNetname( netName );
            else
                pad->SetNetname( wxEmptyString );
        }
    }

    if( !found )
    {
        if( m_messageWindow )
        {
            wxString msg;
            msg.Printf( _( "Module [%s]: Pad [%s] not found" ),
                        GetChars( m_currModule->m_Reference->m_Text ),
                        GetChars( pinName ) );
            m_messageWindow->AppendText( msg + wxT( "\n" ) );
        }
    }

    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;
    COMPONENT_INFO* cmp_info = NULL;

    while( aNetlistReader.ReadLine() )
    {
        const char* Line = aNetlistReader.Line();

        if( strnicmp( Line, "$endlist", 8 ) == 0 ) // end of list for the current component
        {
            cmp_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( COMPONENT_INFO * &component, m_componentsInNetlist )
            {
                if( component->m_Reference == cmpRef )
                {
                    cmp_info = component;
                    break;
                }
            }
        }
        else if( cmp_info )
        {
            // Add new filter to list
            wxString fp = FROM_UTF8( Line + 1 );
            fp.Trim( false );
            fp.Trim( true );
            cmp_info->m_FootprintFilter.Add( fp );
        }
    }

    return true;
}