readschematicnetlist.cpp 9.21 KB
Newer Older
1 2 3
/****************************/
/* readschematicnetlist.cpp */
/****************************/
plyatov's avatar
plyatov committed
4

5
/* Read a nelist type Eeschema or OrcadPCB2 and build the component list
6 7 8
 * Manages the lines like :
 * ( XXXXXX VALEUR|(pin1,pin2,...=newalim) ID VALEUR
 */
plyatov's avatar
plyatov committed
9 10 11 12

#include "fctsys.h"
#include "wxstruct.h"
#include "common.h"
13 14
#include "confirm.h"
#include "kicad_string.h"
15
#include "macros.h"
plyatov's avatar
plyatov committed
16

17
#include "cvpcb.h"
plyatov's avatar
plyatov committed
18
#include "protos.h"
19 20
#include "cvstruct.h"

jean-pierre charras's avatar
jean-pierre charras committed
21 22
#include "richio.h"

plyatov's avatar
plyatov committed
23

24
#define SEPARATEUR '|'  /* Separator character in NetList */
plyatov's avatar
plyatov committed
25 26


jean-pierre charras's avatar
jean-pierre charras committed
27 28
static int ReadPinConnection( FILE_LINE_READER& aNetlistReader, COMPONENT* CurrentCmp );
static int ReadFootprintFilterList( FILE_LINE_READER& aNetlistReader, COMPONENT_LIST& aComponentsList );
29

plyatov's avatar
plyatov committed
30

31 32 33 34
/* Sort the list alphabetically by component and and returns
 * a pointer to the 1st element of list */

#define BUFFER_CHAR_SIZE 1024   // Size of buffers used to store netlist data
plyatov's avatar
plyatov committed
35

36

37 38
/**
 * Function ReadSchematicNetlist
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
 * Read a Eeschema (or OrcadPCB) netlist
 * like:
 * # EESchema Netlist Version 1.1 created  15/5/2008-12:09:21
 * (
 *  ( /32568D1E $noname  JP1 CONN_8X2 {Lib=CONN_8X2}
 *   (    1 GND )
 *   (    2 /REF10 )
 *   (    3 GND )
 *   (    4 /REF11 )
 *   (    5 GND )
 *   (    6 /REF7 )
 *   (    7 GND )
 *   (    8 /REF9 )
 *   (    9 GND )
 *   (   10 /REF6 )
 *   (   11 GND )
 *   (   12 /REF8 )
 *   (   13 GND )
 *   (   14 /REF4 )
 *   (   15 GND )
 *   (   16 /REF5 )
 *  )
 *  ( /325679C1 $noname  RR1 9x1K {Lib=RR9}
 *   (    1 VCC )
 *   (    2 /REF5 )
 *   (    3 /REF4 )
 *   (    4 /REF8 )
 *   (    5 /REF6 )
 *   (    6 /REF9 )
 *   (    7 /REF7 )
 *   (    8 /REF11 )
 *   (    9 /REF10 )
 *   (   10  ? )
 *  )
 * )
 * *
 * { Allowed footprints by component:
 * $component R5
 * R?
 * SM0603
 * SM0805
 * $endlist
 * $component C2
 * SM*
 * C?
 * C1-1
 * $endlist
 * $endfootprintlist
 * }
 */
89
int WinEDA_CvpcbFrame::ReadSchematicNetlist()
plyatov's avatar
plyatov committed
90
{
91
    char       alim[1024];
jean-pierre charras's avatar
jean-pierre charras committed
92 93 94
    int        idx, jj, k, l;
    char       cbuffer[BUFFER_CHAR_SIZE];      /* temporary storage */
    char* ptchar;
95 96 97 98 99
    COMPONENT* Cmp;
    FILE*      source;

    m_modified = false;
    m_isEESchemaNetlist = false;
100

101
    /* Clear components buffer */
102 103
    if( !m_components.empty() )
    {
104
        m_components.clear();
105
    }
106

107 108
    source = wxFopen( m_NetlistFileName.GetFullPath(), wxT( "rt" ) );

109 110
    if( source == 0 )
    {
jean-pierre charras's avatar
jean-pierre charras committed
111 112
        DisplayError( this, _( "File <" ) + m_NetlistFileName.GetFullPath() +
                     _( "> not found" ) );
113
        return -1;
114 115
    }

jean-pierre charras's avatar
jean-pierre charras committed
116 117 118
    FILE_LINE_READER netlistReader( source,  BUFFER_CHAR_SIZE );
    char* Line = netlistReader;

119
    /* Read the file header (must be  "( { OrCAD PCB" or "({ OrCAD PCB" )
120
     * or "# EESchema Netlist"
121
     */
jean-pierre charras's avatar
jean-pierre charras committed
122 123
    netlistReader.ReadLine( );

124
    /* test for netlist type PCB2 */
jean-pierre charras's avatar
jean-pierre charras committed
125 126 127 128
    idx = strnicmp( Line, "( {", 3 );
    if( idx != 0 )
        idx = strnicmp( Line, "({", 2 );
    if( idx != 0 )
129
    {
jean-pierre charras's avatar
jean-pierre charras committed
130 131 132
        idx = strnicmp( Line, "# EESchema", 7 ); /* net type EESchema */
        if( idx == 0 )
            m_isEESchemaNetlist = true;
133 134
    }

jean-pierre charras's avatar
jean-pierre charras committed
135
    if( idx != 0 )
136 137 138 139
    {
        wxString msg, Lineconv = CONV_FROM_UTF8( Line );
        msg.Printf( _( "Unknown file format <%s>" ), Lineconv.GetData() );
        DisplayError( this, msg );
140 141
        fclose( source );
        return -3;
142 143 144 145 146
    }

    SetStatusText( _( "Netlist Format: EESchema" ), 0 );


147
    /* Read the netlist */
148 149 150 151
    for( ; ; )
    {
        /* Search the beginning of a component description */

jean-pierre charras's avatar
jean-pierre charras committed
152
        if( netlistReader.ReadLine( ) == 0 )
153 154 155
            break;

        /* Remove blanks */
jean-pierre charras's avatar
jean-pierre charras committed
156 157 158
        idx = 0;
        while( Line[idx] == ' ' )
            idx++;
159 160

        /* remove empty lines : */
jean-pierre charras's avatar
jean-pierre charras committed
161
        if( Line[idx] < ' ' )
162 163
            continue;

jean-pierre charras's avatar
jean-pierre charras committed
164
        if( strnicmp( &Line[idx], "{ Allowed footprints", 20 ) == 0 )
165
        {
jean-pierre charras's avatar
jean-pierre charras committed
166
            ReadFootprintFilterList( netlistReader, m_components );
167 168 169
            continue;
        }

jean-pierre charras's avatar
jean-pierre charras committed
170
        if( strnicmp( &Line[idx], "( ", 2 ) != 0 )
171 172 173 174 175
            continue;

        /*******************************/
        /* Component description found */
        /*******************************/
jean-pierre charras's avatar
jean-pierre charras committed
176
        Cmp = new COMPONENT();  // Creates the new component storage
177

jean-pierre charras's avatar
jean-pierre charras committed
178 179
        while( Line[idx] != ' ' )
            idx++;
180

jean-pierre charras's avatar
jean-pierre charras committed
181 182
        while( Line[idx] == ' ' )
            idx++;
183

jean-pierre charras's avatar
jean-pierre charras committed
184 185 186 187 188 189
        /* idx points the beginning of the schematic time stamp */
        jj = 0;
        while( Line[idx] != ' ' && Line[idx] )
            cbuffer[jj++] = Line[idx++];
        cbuffer[jj] = 0;
        Cmp->m_TimeStamp = CONV_FROM_UTF8(cbuffer);
190 191

        /* search val/ref.lib */
jean-pierre charras's avatar
jean-pierre charras committed
192 193
        while( Line[idx] == ' ' )
            idx++;
194

195 196
        /* idx points the component value.
         * Read value */
jean-pierre charras's avatar
jean-pierre charras committed
197
        ptchar = strstr( &Line[idx], " " );  // Search end of value field (space)
198 199 200 201
        if( ptchar == 0 )
        {
            wxString msg;
            msg.Printf( _( "Netlist error: %s" ), Line );
202
            DisplayError( this, msg );
203 204 205 206 207
            k = 0;
        }
        else
            k = ptchar - Line;

jean-pierre charras's avatar
jean-pierre charras committed
208
        for( jj = 0; idx < k; idx++ )
209
        {
jean-pierre charras's avatar
jean-pierre charras committed
210
            if( Line[idx] == SEPARATEUR )
211
                break;
jean-pierre charras's avatar
jean-pierre charras committed
212
            cbuffer[jj++] = Line[idx];
213
        }
jean-pierre charras's avatar
jean-pierre charras committed
214
        cbuffer[jj] = 0;
215 216 217
        // Copy footprint name:
        if( m_isEESchemaNetlist && (strnicmp( cbuffer, "$noname", 7 ) != 0) )
            Cmp->m_Module = CONV_FROM_UTF8(cbuffer);
218

jean-pierre charras's avatar
jean-pierre charras committed
219
        if( (Line[++idx] == '(') && (Line[k - 1] == ')' ) )
220
        {
jean-pierre charras's avatar
jean-pierre charras committed
221 222 223
            idx++; l = 0;
            while( k - 1 > idx )
                alim[l++] = Line[idx++];
224 225
        }
        else
jean-pierre charras's avatar
jean-pierre charras committed
226
            idx = k;
227 228

        /* Search component reference */
jean-pierre charras's avatar
jean-pierre charras committed
229 230
        while( Line[idx] != ' ' && Line[idx] )
            idx++;
231 232

        /* goto end of value field */
jean-pierre charras's avatar
jean-pierre charras committed
233 234
        while( Line[idx] == ' ' && Line[idx] )
            idx++;
235

236
        /* goto beginning of reference */
jean-pierre charras's avatar
jean-pierre charras committed
237
        for( jj = 0; ; idx++ )
238
        {
jean-pierre charras's avatar
jean-pierre charras committed
239
            if( Line[idx] == ' ' || Line[idx] == 0)
240
                break;
jean-pierre charras's avatar
jean-pierre charras committed
241
            cbuffer[jj++] = Line[idx];
242
        }
jean-pierre charras's avatar
jean-pierre charras committed
243 244
        cbuffer[jj] = 0;
        Cmp->m_Reference = CONV_FROM_UTF8(cbuffer);
245 246

        /* Search component value */
jean-pierre charras's avatar
jean-pierre charras committed
247 248
        while( Line[idx] == ' ' && Line[idx] )
            idx++;
249

250
        /** goto beginning of value */
251

jean-pierre charras's avatar
jean-pierre charras committed
252
        for( jj = 0 ; ; idx++ )
253
        {
jean-pierre charras's avatar
jean-pierre charras committed
254
            if( (Line[idx] == ' ') || (Line[idx] == '\n') || (Line[idx] == '\r') || Line[idx] == 0)
255
                break;
jean-pierre charras's avatar
jean-pierre charras committed
256
            cbuffer[jj++] = Line[idx];
257
        }
jean-pierre charras's avatar
jean-pierre charras committed
258 259
        cbuffer[jj] = 0;
        Cmp->m_Value = CONV_FROM_UTF8(cbuffer);
260

261
        m_components.push_back( Cmp );
262

jean-pierre charras's avatar
jean-pierre charras committed
263
        ReadPinConnection( netlistReader, Cmp );
264 265 266 267
    }

    fclose( source );

268
    m_components.sort();
269 270

    return 0;
plyatov's avatar
plyatov committed
271
}
272

273

jean-pierre charras's avatar
jean-pierre charras committed
274
int ReadFootprintFilterList(  FILE_LINE_READER& aNetlistReader, COMPONENT_LIST& aComponentsList )
275
{
jean-pierre charras's avatar
jean-pierre charras committed
276
    char*       Line = aNetlistReader;
277 278
    wxString   CmpRef;
    COMPONENT* Cmp = NULL;
279 280 281

    for( ; ; )
    {
jean-pierre charras's avatar
jean-pierre charras committed
282
        if( aNetlistReader.ReadLine( )  == 0 )
283 284 285 286 287 288 289 290 291 292 293 294
            break;
        if( strnicmp( Line, "$endlist", 8 ) == 0 )
        {
            Cmp = NULL;
            continue;
        }
        if( strnicmp( Line, "$endfootprintlist", 4 ) == 0 )
            return 0;

        if( strnicmp( Line, "$component", 10 ) == 0 ) // New component ref found
        {
            CmpRef = CONV_FROM_UTF8( Line + 11 );
jean-pierre charras's avatar
jean-pierre charras committed
295 296
            CmpRef.Trim( true );
            CmpRef.Trim( false );
297

298
            /* Search the new component in list */
jean-pierre charras's avatar
jean-pierre charras committed
299
            BOOST_FOREACH( COMPONENT & component, aComponentsList )
300
            {
301
                Cmp = &component;
302 303 304 305 306 307 308
                if( Cmp->m_Reference == CmpRef )
                    break;
            }
        }
        else if( Cmp )
        {
            wxString fp = CONV_FROM_UTF8( Line + 1 );
jean-pierre charras's avatar
jean-pierre charras committed
309 310
            fp.Trim( false );
            fp.Trim( true );
311 312 313 314 315
            Cmp->m_FootprintFilter.Add( fp );
        }
    }

    return 1;
316
}
plyatov's avatar
plyatov committed
317 318


jean-pierre charras's avatar
jean-pierre charras committed
319
int ReadPinConnection( FILE_LINE_READER& aNetlistReader, COMPONENT* Cmp )
plyatov's avatar
plyatov committed
320
{
321
    int      i, jj;
jean-pierre charras's avatar
jean-pierre charras committed
322 323
    char*     Line = aNetlistReader;
    char cbuffer[BUFFER_CHAR_SIZE];
324 325 326

    for( ; ; )
    {
327
        /* Find beginning of description. */
328 329
        for( ; ; )
        {
jean-pierre charras's avatar
jean-pierre charras committed
330
            if( aNetlistReader.ReadLine( ) == 0 )
331 332
                return -1;

333
            /* Remove blanks from the beginning of the line. */
334 335 336 337 338 339 340 341 342 343 344 345 346
            i = 0; while( Line[i] == ' ' )
                i++;

            while( Line[i] == '(' )
                i++;

            while( Line[i] == ' ' )
                i++;

            /* remove empty lines : */
            if( Line[i] < ' ' )
                continue;

347
            /* End of description? */
348 349 350
            if( Line[i] == ')' )
                return 0;

jean-pierre charras's avatar
jean-pierre charras committed
351
            PIN * Pin = new PIN();
352

jean-pierre charras's avatar
jean-pierre charras committed
353 354
            /* Read pin name, usually 4 letters */
            for( jj = 0; ; i++ )
355
            {
jean-pierre charras's avatar
jean-pierre charras committed
356
                if( Line[i] == ' ' || Line[i] == 0 )
357
                    break;
jean-pierre charras's avatar
jean-pierre charras committed
358
                cbuffer[jj++] = Line[i];
359
            }
jean-pierre charras's avatar
jean-pierre charras committed
360 361
            cbuffer[jj] =  0;
            Pin->m_Number = CONV_FROM_UTF8(cbuffer);
362 363 364 365 366

            /* Read netname */
            while( Line[i] == ' ' )
                i++;

jean-pierre charras's avatar
jean-pierre charras committed
367
            for( jj = 0; ; i++ )
368
            {
jean-pierre charras's avatar
jean-pierre charras committed
369
                if( Line[i] == ' ' || Line[i] == '\n' || Line[i] == '\r' || Line[i] == 0 )
370
                    break;
jean-pierre charras's avatar
jean-pierre charras committed
371
                cbuffer[jj++] = Line[i];
372
            }
jean-pierre charras's avatar
jean-pierre charras committed
373 374
            cbuffer[jj] =  0;
            Pin->m_Net = CONV_FROM_UTF8(cbuffer);
375

376
            Cmp->m_Pins.push_back( Pin );
377 378
        }
    }
plyatov's avatar
plyatov committed
379
}