readschematicnetlist.cpp 9.17 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
 */
plyatov's avatar
plyatov committed
7 8 9 10

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

15
#include "cvpcb.h"
16
#include "cvpcb_mainframe.h"
jean-pierre charras's avatar
jean-pierre charras committed
17 18
#include "richio.h"

plyatov's avatar
plyatov committed
19

20
#define SEPARATEUR '|'  /* Separator character in NetList */
plyatov's avatar
plyatov committed
21 22


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

plyatov's avatar
plyatov committed
26

27 28 29 30
/* 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
31

32

33 34
/**
 * Function ReadSchematicNetlist
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
 * 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
 * }
 */
85
int CVPCB_MAINFRAME::ReadSchematicNetlist()
plyatov's avatar
plyatov committed
86
{
87
    char       alim[1024];
jean-pierre charras's avatar
jean-pierre charras committed
88 89
    int        idx, jj, k, l;
    char       cbuffer[BUFFER_CHAR_SIZE];      /* temporary storage */
Dick Hollenbeck's avatar
Dick Hollenbeck committed
90
    char*      ptchar;
91 92 93 94 95
    COMPONENT* Cmp;
    FILE*      source;

    m_modified = false;
    m_isEESchemaNetlist = false;
96

97
    /* Clear components buffer */
98 99
    if( !m_components.empty() )
    {
100
        m_components.clear();
101
    }
102

103 104
    source = wxFopen( m_NetlistFileName.GetFullPath(), wxT( "rt" ) );

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

112
    // FILE_LINE_READER will close the file.
Dick Hollenbeck's avatar
Dick Hollenbeck committed
113 114
    FILE_LINE_READER netlistReader( source, m_NetlistFileName.GetFullPath() );

115
    /* Read the file header (must be  "( { OrCAD PCB" or "({ OrCAD PCB" )
116
     * or "# EESchema Netlist"
117
     */
118
    netlistReader.ReadLine();
119
    const char* Line = netlistReader.Line();
jean-pierre charras's avatar
jean-pierre charras committed
120

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

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

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


143
    /* Read the netlist */
144 145 146 147
    for( ; ; )
    {
        /* Search the beginning of a component description */

jean-pierre charras's avatar
jean-pierre charras committed
148
        if( netlistReader.ReadLine( ) == 0 )
149
            break;
150
        Line = netlistReader.Line();
151 152

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

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

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

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

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

jean-pierre charras's avatar
jean-pierre charras committed
175 176
        while( Line[idx] != ' ' )
            idx++;
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 183 184 185
        /* idx points the beginning of the schematic time stamp */
        jj = 0;
        while( Line[idx] != ' ' && Line[idx] )
            cbuffer[jj++] = Line[idx++];
        cbuffer[jj] = 0;
186
        Cmp->m_TimeStamp = FROM_UTF8(cbuffer);
187 188

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

192 193
        /* idx points the component value.
         * Read value */
Dick Hollenbeck's avatar
Dick Hollenbeck committed
194
        ptchar = strstr( (char*) &Line[idx], " " );  // Search end of value field (space)
195 196
        if( ptchar == 0 )
        {
197
            wxString msg = _( "Netlist error: " );
198
            msg << FROM_UTF8( Line );
199
            DisplayError( this, msg );
200 201 202 203 204
            k = 0;
        }
        else
            k = ptchar - Line;

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

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

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

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

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

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

247
        /** goto beginning of value */
248

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

258
        m_components.push_back( Cmp );
259

jean-pierre charras's avatar
jean-pierre charras committed
260
        ReadPinConnection( netlistReader, Cmp );
261 262
    }

263
    m_components.sort();
264 265

    return 0;
plyatov's avatar
plyatov committed
266
}
267

268

jean-pierre charras's avatar
jean-pierre charras committed
269
int ReadFootprintFilterList(  FILE_LINE_READER& aNetlistReader, COMPONENT_LIST& aComponentsList )
270
{
Dick Hollenbeck's avatar
Dick Hollenbeck committed
271
    const char* Line = aNetlistReader;
272 273
    wxString    CmpRef;
    COMPONENT*  Cmp = NULL;
274 275 276

    for( ; ; )
    {
jean-pierre charras's avatar
jean-pierre charras committed
277
        if( aNetlistReader.ReadLine( )  == 0 )
278 279 280 281 282 283 284 285 286 287 288
            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
        {
289
            CmpRef = FROM_UTF8( Line + 11 );
jean-pierre charras's avatar
jean-pierre charras committed
290 291
            CmpRef.Trim( true );
            CmpRef.Trim( false );
292

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

    return 1;
311
}
plyatov's avatar
plyatov committed
312 313


jean-pierre charras's avatar
jean-pierre charras committed
314
int ReadPinConnection( FILE_LINE_READER& aNetlistReader, COMPONENT* Cmp )
plyatov's avatar
plyatov committed
315
{
316 317
    int         i, jj;
    char        cbuffer[BUFFER_CHAR_SIZE];
318 319 320

    for( ; ; )
    {
321
        /* Find beginning of description. */
322 323
        for( ; ; )
        {
324
            if( aNetlistReader.ReadLine() == 0 )
325 326
                return -1;

327 328
            char*  Line = aNetlistReader.Line();

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

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

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

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

343
            /* End of description? */
344 345 346
            if( Line[i] == ')' )
                return 0;

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

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

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

jean-pierre charras's avatar
jean-pierre charras committed
363
            for( jj = 0; ; i++ )
364
            {
jean-pierre charras's avatar
jean-pierre charras committed
365
                if( Line[i] == ' ' || Line[i] == '\n' || Line[i] == '\r' || Line[i] == 0 )
366
                    break;
jean-pierre charras's avatar
jean-pierre charras committed
367
                cbuffer[jj++] = Line[i];
368
            }
jean-pierre charras's avatar
jean-pierre charras committed
369
            cbuffer[jj] =  0;
370
            Pin->m_Net = FROM_UTF8(cbuffer);
371

372
            Cmp->m_Pins.push_back( Pin );
373 374
        }
    }
plyatov's avatar
plyatov committed
375
}