summaryrefslogtreecommitdiff
path: root/common/lset.cpp
blob: 4e4248dd7f9629809085eb36d12eb600ed5768b4 (plain)
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
408
409
410
411
412
413
414
415
416
417
418
419
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 2014 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
 * Copyright (C) 2014 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 <stdarg.h>
#include <assert.h>

#include <layers_id_colors_and_visibility.h>
#include <class_board.h>


LSET::LSET( const LAYER_ID* aArray, unsigned aCount ) :
    BASE_SET()
{
    for( unsigned i=0; i<aCount; ++i )
        set( aArray[i] );
}


LSET::LSET( unsigned aIdCount, LAYER_ID aFirst, ... ) :
    BASE_SET()
{
    // The constructor, without the mandatory aFirst argument, could have been confused
    // by the compiler with the LSET( LAYER_ID ).  With aFirst, that ambiguity is not
    // present.  Therefore aIdCount must always be >=1.
    wxASSERT_MSG( aIdCount > 0, wxT( "aIdCount must be >= 1" ) );

    set( aFirst );

    if( --aIdCount )
    {
        va_list ap;

        va_start( ap, aFirst );

        for( unsigned i=0;  i<aIdCount;  ++i )
        {
            LAYER_ID id = (LAYER_ID) va_arg( ap, int );

            // printf( "%s: id:%d LAYER_ID_COUNT:%d\n", __func__, id, LAYER_ID_COUNT );

            assert( unsigned( id ) < LAYER_ID_COUNT );

            set( id );
        }

        va_end( ap );
    }
}


const wxChar* LSET::Name( LAYER_ID aLayerId )
{
    const wxChar* txt;

    // using a switch to explicitly show the mapping more clearly
    switch( aLayerId )
    {
    case F_Cu:              txt = wxT( "F.Cu" );            break;
    case In1_Cu:            txt = wxT( "In1.Cu" );          break;
    case In2_Cu:            txt = wxT( "In2.Cu" );          break;
    case In3_Cu:            txt = wxT( "In3.Cu" );          break;
    case In4_Cu:            txt = wxT( "In4.Cu" );          break;
    case In5_Cu:            txt = wxT( "In5.Cu" );          break;
    case In6_Cu:            txt = wxT( "In6.Cu" );          break;
    case In7_Cu:            txt = wxT( "In7.Cu" );          break;
    case In8_Cu:            txt = wxT( "In8.Cu" );          break;
    case In9_Cu:            txt = wxT( "In9.Cu" );          break;
    case In10_Cu:           txt = wxT( "In10.Cu" );         break;
    case In11_Cu:           txt = wxT( "In11.Cu" );         break;
    case In12_Cu:           txt = wxT( "In12.Cu" );         break;
    case In13_Cu:           txt = wxT( "In13.Cu" );         break;
    case In14_Cu:           txt = wxT( "In14.Cu" );         break;
    case In15_Cu:           txt = wxT( "In15.Cu" );         break;
    case In16_Cu:           txt = wxT( "In16.Cu" );         break;
    case In17_Cu:           txt = wxT( "In17.Cu" );         break;
    case In18_Cu:           txt = wxT( "In18.Cu" );         break;
    case In19_Cu:           txt = wxT( "In19.Cu" );         break;
    case In20_Cu:           txt = wxT( "In20.Cu" );         break;
    case In21_Cu:           txt = wxT( "In21.Cu" );         break;
    case In22_Cu:           txt = wxT( "In22.Cu" );         break;
    case In23_Cu:           txt = wxT( "In23.Cu" );         break;
    case In24_Cu:           txt = wxT( "In24.Cu" );         break;
    case In25_Cu:           txt = wxT( "In25.Cu" );         break;
    case In26_Cu:           txt = wxT( "In26.Cu" );         break;
    case In27_Cu:           txt = wxT( "In27.Cu" );         break;
    case In28_Cu:           txt = wxT( "In28.Cu" );         break;
    case In29_Cu:           txt = wxT( "In29.Cu" );         break;
    case In30_Cu:           txt = wxT( "In30.Cu" );         break;
    case B_Cu:              txt = wxT( "B.Cu" );            break;

    // Technicals
    case B_Adhes:           txt = wxT( "B.Adhes" );         break;
    case F_Adhes:           txt = wxT( "F.Adhes" );         break;
    case B_Paste:           txt = wxT( "B.Paste" );         break;
    case F_Paste:           txt = wxT( "F.Paste" );         break;
    case B_SilkS:           txt = wxT( "B.SilkS" );         break;
    case F_SilkS:           txt = wxT( "F.SilkS" );         break;
    case B_Mask:            txt = wxT( "B.Mask" );          break;
    case F_Mask:            txt = wxT( "F.Mask" );          break;

    // Users
    case Dwgs_User:         txt = wxT( "Dwgs.User" );       break;
    case Cmts_User:         txt = wxT( "Cmts.User" );       break;
    case Eco1_User:         txt = wxT( "Eco1.User" );       break;
    case Eco2_User:         txt = wxT( "Eco2.User" );       break;
    case Edge_Cuts:         txt = wxT( "Edge.Cuts" );       break;
    case Margin:            txt = wxT( "Margin" );          break;

    // Footprint
    case F_CrtYd:           txt = wxT( "F.CrtYd" );         break;
    case B_CrtYd:           txt = wxT( "B.CrtYd" );         break;
    case F_Fab:             txt = wxT( "F.Fab" );           break;
    case B_Fab:             txt = wxT( "B.Fab" );           break;

    default:
        wxASSERT_MSG( 0, wxT( "aLayerId out of range" ) );
                            txt = wxT( "BAD INDEX!" );      break;
    }

    return txt;
}


LSEQ LSET::CuStack() const
{
    // desired sequence
    static const LAYER_ID sequence[] = {
        F_Cu,
        In1_Cu,
        In2_Cu,
        In3_Cu,
        In4_Cu,
        In5_Cu,
        In6_Cu,
        In7_Cu,
        In8_Cu,
        In9_Cu,
        In10_Cu,
        In11_Cu,
        In12_Cu,
        In13_Cu,
        In14_Cu,
        In15_Cu,
        In16_Cu,
        In17_Cu,
        In18_Cu,
        In19_Cu,
        In20_Cu,
        In21_Cu,
        In22_Cu,
        In23_Cu,
        In24_Cu,
        In25_Cu,
        In26_Cu,
        In27_Cu,
        In28_Cu,
        In29_Cu,
        In30_Cu,
        B_Cu,           // 31
    };

    return Seq( sequence, DIM( sequence ) );
}


LSEQ LSET::Technicals( LSET aSetToOmit ) const
{
    // desired sequence
    static const LAYER_ID sequence[] = {
        B_Adhes,
        F_Adhes,
        B_Paste,
        F_Paste,
        B_SilkS,
        F_SilkS,
        B_Mask,
        F_Mask,
        B_CrtYd,
        F_CrtYd,
        B_Fab,
        F_Fab,
    };

    LSET subset = ~aSetToOmit & *this;

    return subset.Seq( sequence, DIM( sequence ) );
}


LSEQ LSET::Users() const
{
    // desired
    static const LAYER_ID sequence[] = {
        Dwgs_User,
        Cmts_User,
        Eco1_User,
        Eco2_User,
        Edge_Cuts,
        Margin,
   };

   return Seq( sequence, DIM( sequence ) );
}


std::string LSET::FmtBin() const
{
    std::string ret;

    int     bit_count = size();

    for( int bit=0;  bit<bit_count;  ++bit )
    {
        if( bit )
        {
            if( !( bit % 8 ) )
                ret += '|';
            else if( !( bit % 4 ) )
                ret += '_';
        }

        ret += (*this)[bit] ? '1' :  '0';
    }

    // reverse of string
    return std::string( ret.rbegin(), ret.rend() );
}


std::string LSET::FmtHex() const
{
    std::string ret;

    static const char hex[] = "0123456789abcdef";

    int     nibble_count = ( size() + 3 ) / 4;

    for( int nibble=0;  nibble<nibble_count;  ++nibble )
    {
        unsigned ndx = 0;

        // test 4 consecutive bits and set ndx to 0-15:
        for( int nibble_bit=0;  nibble_bit<4;  ++nibble_bit )
        {
            if( (*this)[nibble_bit + nibble*4] )
                ndx |= (1 << nibble_bit);
        }

        if( nibble && !( nibble % 8 ) )
            ret += '_';

        assert( ndx < DIM( hex ) );

        ret += hex[ndx];
    }

    // reverse of string
    return std::string( ret.rbegin(), ret.rend() );
}


int LSET::ParseHex( const char* aStart, int aCount )
{
    LSET tmp;

    const char* rstart = aStart + aCount - 1;
    const char* rend   = aStart - 1;

    const int bitcount = size();

    int nibble_ndx = 0;

    while( rstart > rend )
    {
        int cc = *rstart--;

        if( cc == '_' )
            continue;

        int nibble;

        if( cc >= '0' && cc <= '9' )
            nibble = cc - '0';
        else if( cc >= 'a' && cc <= 'f' )
            nibble = cc - 'a' + 10;
        else if( cc >= 'A' && cc <= 'F' )
            nibble = cc - 'A' + 10;
        else
            break;

        int bit = nibble_ndx * 4;

        for( int ndx=0; bit<bitcount && ndx<4; ++bit, ++ndx )
            if( nibble & (1<<ndx) )
                tmp.set( bit );

        if( bit >= bitcount )
            break;

        ++nibble_ndx;
    }

    int byte_count = aStart + aCount - 1 - rstart;

    assert( byte_count >= 0 );

    if( byte_count > 0 )
        *this = tmp;

    return byte_count;
}


LSEQ LSET::Seq( const LAYER_ID* aWishListSequence, unsigned aCount ) const
{
    LSEQ ret;

#if defined(DEBUG) && 0
    LSET    dup_detector;

    for( unsigned i=0; i<aCount;  ++i )
    {
        LAYER_ID id = aWishListSequence[i];

        if( test( id ) )
        {
            wxASSERT_MSG( !dup_detector[id], wxT( "Duplicate in aWishListSequence" ) );
            dup_detector[id] = true;

            ret.push_back( id );
        }
    }
#else

    for( unsigned i=0; i<aCount;  ++i )
    {
        LAYER_ID id = aWishListSequence[i];

        if( test( id ) )
            ret.push_back( id );
    }
#endif

    return ret;
}


LSEQ LSET::Seq() const
{
    LSEQ    ret;

    for( unsigned i=0;  i<size();  ++i )
    {
        if( test(i) )
            ret.push_back( LAYER_ID( i ) );
    }

    return ret;
}


LSEQ LSET::SeqStackupBottom2Top() const
{
    // bottom-to-top stack-up layers
    static const LAYER_ID sequence[] = {
        B_Fab,
        B_CrtYd,
        B_Adhes,
        B_SilkS,
        B_Paste,
        B_Mask,
        B_Cu,
        In30_Cu,
        In29_Cu,
        In28_Cu,
        In27_Cu,
        In26_Cu,
        In25_Cu,
        In24_Cu,
        In23_Cu,
        In22_Cu,
        In21_Cu,
        In20_Cu,
        In19_Cu,
        In18_Cu,
        In17_Cu,
        In16_Cu,
        In15_Cu,
        In14_Cu,
        In13_Cu,
        In12_Cu,
        In11_Cu,
        In10_Cu,
        In9_Cu,
        In8_Cu,
        In7_Cu,
        In6_Cu,
        In5_Cu,
        In4_Cu,
        In3_Cu,
        In2_Cu,
        In1_Cu,
        F_Cu,
        F_Mask,
        F_Paste,
        F_SilkS,
        F_Adhes,
        F_CrtYd,
        F_Fab,
        Dwgs_User,
        Cmts_User,
        Eco1_User,
        Eco2_User,
        Margin,
        Edge_Cuts,
    };

    return Seq( sequence, DIM( sequence ) );
}


LAYER_ID FlipLayer( LAYER_ID aLayerId, int aCopperLayersCount )
{
    switch( aLayerId )
    {
    case B_Cu:              return F_Cu;
    case F_Cu:              return B_Cu;

    case B_SilkS:           return F_SilkS;
    case F_SilkS:           return B_SilkS;

    case B_Adhes:           return F_Adhes;
    case F_Adhes:           return B_Adhes;

    case B_Mask:            return F_Mask;
    case F_Mask:            return B_Mask;

    case B_Paste:           return F_Paste;
    case F_Paste:           return B_Paste;

    case B_CrtYd:           return F_CrtYd;
    case F_CrtYd:           return B_CrtYd;

    case B_Fab:             return F_Fab;
    case F_Fab:             return B_Fab;

    default:    // change internal layer if aCopperLayersCount is >= 4
        if( IsCopperLayer( aLayerId ) && aCopperLayersCount >= 4 )
        {
            // internal copper layers count is aCopperLayersCount-2
            LAYER_ID fliplayer = LAYER_ID(aCopperLayersCount - 2 - ( aLayerId - In1_Cu ) );
            // Ensure fliplayer has a value which does not crash pcbnew:
            if( fliplayer < F_Cu )
                fliplayer = F_Cu;

            if( fliplayer > B_Cu )
                fliplayer = B_Cu;

            return fliplayer;
        }

        // No change for the other layers
        return aLayerId;
    }
}


LSET FlipLayerMask( LSET aMask, int aCopperLayersCount )
{
    // layers on physical outside of a board:
    const static LSET and_mask( 16,     // !! update count
                B_Cu,       F_Cu,
                B_SilkS,    F_SilkS,
                B_Adhes,    F_Adhes,
                B_Mask,     F_Mask,
                B_Paste,    F_Paste,
                B_Adhes,    F_Adhes,
                B_CrtYd,    F_CrtYd,
                B_Fab,      F_Fab
                );

    LSET newMask = aMask & ~and_mask;

    if( aMask[B_Cu] )
        newMask.set( F_Cu );

    if( aMask[F_Cu] )
        newMask.set( B_Cu );

    if( aMask[B_SilkS] )
        newMask.set( F_SilkS );

    if( aMask[F_SilkS] )
        newMask.set( B_SilkS );

    if( aMask[B_Adhes] )
        newMask.set( F_Adhes );

    if( aMask[F_Adhes] )
        newMask.set( B_Adhes );

    if( aMask[B_Mask] )
        newMask.set( F_Mask );

    if( aMask[F_Mask] )
        newMask.set( B_Mask );

    if( aMask[B_Paste] )
        newMask.set( F_Paste );

    if( aMask[F_Paste] )
        newMask.set( B_Paste );

    if( aMask[B_Adhes] )
        newMask.set( F_Adhes );

    if( aMask[F_Adhes] )
        newMask.set( B_Adhes );

    if( aMask[B_CrtYd] )
        newMask.set( F_CrtYd );

    if( aMask[F_CrtYd] )
        newMask.set( B_CrtYd );

    if( aMask[B_Fab] )
        newMask.set( F_Fab );

    if( aMask[F_Fab] )
        newMask.set( B_Fab );

    if( aCopperLayersCount >= 4 )   // Internal layers exist
    {
        LSET internalMask = aMask & ~LSET::InternalCuMask();

        if( internalMask != LSET::InternalCuMask() )
        {   // the mask does not include all internal layers. Therefore
            // the flipped mask for internal copper layers must be built
            int innerLayerCnt = aCopperLayersCount -2;

            for( int ii = 0; ii < innerLayerCnt; ii++ )
            {
                if( internalMask[innerLayerCnt - ii + In1_Cu] )
                    newMask.set( ii + In1_Cu );
                else
                    newMask.reset( ii + In1_Cu );
            }
        }
    }

    return newMask;
}


LAYER_ID LSET::ExtractLayer() const
{
    unsigned set_count = count();

    if( !set_count )
        return UNSELECTED_LAYER;
    else if( set_count > 1 )
        return UNDEFINED_LAYER;

    for( unsigned i=0; i < size(); ++i )
    {
        if( test( i ) )
            return LAYER_ID( i );
    }

    wxASSERT( 0 );  // set_count was verified as 1 above, what did you break?

    return UNDEFINED_LAYER;
}


LSET LSET::InternalCuMask()
{
    static const LAYER_ID cu_internals[] = {
        In1_Cu,
        In2_Cu,
        In3_Cu,
        In4_Cu,
        In5_Cu,
        In6_Cu,
        In7_Cu,
        In8_Cu,
        In9_Cu,
        In10_Cu,
        In11_Cu,
        In12_Cu,
        In13_Cu,
        In14_Cu,
        In15_Cu,
        In16_Cu,
        In17_Cu,
        In18_Cu,
        In19_Cu,
        In20_Cu,
        In21_Cu,
        In22_Cu,
        In23_Cu,
        In24_Cu,
        In25_Cu,
        In26_Cu,
        In27_Cu,
        In28_Cu,
        In29_Cu,
        In30_Cu,
    };

    static const LSET saved( cu_internals, DIM( cu_internals ) );
    return saved;
}


LSET LSET::AllCuMask( int aCuLayerCount )
{
    // retain all in static as the full set, which is a common case.
    static const LSET  all = InternalCuMask().set( F_Cu ).set( B_Cu );

    if( aCuLayerCount == MAX_CU_LAYERS )
        return all;

    // subtract out some Cu layers not wanted in the mask.
    LSET    ret = all;
    int     clear_count = MAX_CU_LAYERS - aCuLayerCount;

    clear_count = Clamp( 0, clear_count, MAX_CU_LAYERS - 2 );

    for( LAYER_NUM elem=In30_Cu;  clear_count; --elem, --clear_count )
    {
        ret.set( elem, false );
    }

    return ret;
}


LSET LSET::AllNonCuMask()
{
    static const LSET saved = LSET().set() & ~AllCuMask();
    return saved;
}


LSET LSET::AllLayersMask()
{
    static const LSET saved = LSET().set();
    return saved;
}


LSET LSET::BackTechMask()
{
    // (SILKSCREEN_LAYER_BACK | SOLDERMASK_LAYER_BACK | ADHESIVE_LAYER_BACK | SOLDERPASTE_LAYER_BACK)
    static const LSET saved( 6, B_SilkS, B_Mask, B_Adhes, B_Paste, B_CrtYd, B_Fab );
    return saved;
}


LSET LSET::FrontTechMask()
{
    // (SILKSCREEN_LAYER_FRONT | SOLDERMASK_LAYER_FRONT | ADHESIVE_LAYER_FRONT | SOLDERPASTE_LAYER_FRONT)
    static const LSET saved( 6, F_SilkS, F_Mask, F_Adhes, F_Paste, F_CrtYd, F_Fab );
    return saved;
}


LSET LSET::AllTechMask()
{
    static const LSET saved = BackTechMask() | FrontTechMask();
    return saved;
}


LSET LSET::UserMask()
{
    static const LSET saved( 6,
        Dwgs_User,
        Cmts_User,
        Eco1_User,
        Eco2_User,
        Edge_Cuts,
        Margin
        );

    return saved;
}


LSET LSET::FrontMask()
{
    static const LSET saved = FrontTechMask().set( F_Cu );
    return saved;
}


LSET LSET::BackMask()
{
    static const LSET saved = BackTechMask().set( B_Cu );
    return saved;
}


LSEQ LSET::UIOrder() const
{
    LAYER_ID order[LAYER_ID_COUNT];

    // Assmuming that the LAYER_ID order is according to preferred UI order, as of
    // today this is true.  When that becomes not true, its easy to change the order
    // in here to compensate.

    for( unsigned i=0;  i<DIM(order);  ++i )
        order[i] = LAYER_ID( i );

    return Seq( order, DIM( order ) );
}


LAYER_ID ToLAYER_ID( int aLayer )
{
    wxASSERT( unsigned( aLayer ) < LAYER_ID_COUNT );
    return LAYER_ID( aLayer );
}