KiCad PCB EDA Suite
Loading...
Searching...
No Matches
board_stackup.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2019 Jean-Pierre Charras, jp.charras at wanadoo.fr
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 3
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21
22#include "board_stackup.h"
23#include <algorithm>
24#include <base_units.h>
25#include <string_utils.h>
26#include <layer_ids.h>
28#include <board.h>
29#include <i18n_utility.h> // For _HKI definition
32#include <richio.h>
33#include <google/protobuf/any.pb.h>
34#include <api/board/board.pb.h>
35#include <api/api_enums.h>
36#include <api/api_utils.h>
37
38
40{
41 if( m_Material != aOther.m_Material ) return false;
42 if( m_Thickness != aOther.m_Thickness ) return false;
43 if( m_ThicknessLocked != aOther.m_ThicknessLocked ) return false;
44 if( m_EpsilonR != aOther.m_EpsilonR ) return false;
45 if( m_LossTangent != aOther.m_LossTangent ) return false;
46 if( m_SpecFreq != aOther.m_SpecFreq ) return false;
47 if( m_DielectricModel != aOther.m_DielectricModel ) return false;
48 if( m_Color != aOther.m_Color ) return false;
49
50 return true;
51}
52
53
55{
56 DIELECTRIC_PRMS item_prms;
57 m_DielectricPrmsList.emplace_back( item_prms );
59 m_Type = aType;
61 SetEnabled( true );
62
63 // Initialize parameters to a usual value for allowed types:
64 switch( m_Type )
65 {
69 break;
70
72 m_TypeName = KEY_CORE; // or prepreg
74 SetMaterial( wxT( "FR4" ) ); // or other dielectric name
75 SetLossTangent( 0.02 ); // for FR4
76 SetEpsilonR( 4.5 ); // for FR4
77 SetSpecFreq( 1e9 ); // for FR4
79 break;
80
82 m_TypeName = wxT( "solderpaste" );
83 break;
84
86 m_TypeName = wxT( "soldermask" );
88 SetMaterial( NotSpecifiedPrm() ); // or other solder mask material name
91 break;
92
94 m_TypeName = wxT( "silkscreen" );
96 SetMaterial( NotSpecifiedPrm() ); // or other silkscreen material name
98 break;
99
101 break;
102 }
103}
104
105
116
117
119{
120 if( m_Type != aOther.m_Type ) return false;
121 if( m_LayerName != aOther.m_LayerName ) return false;
122 if( m_TypeName != aOther.m_TypeName ) return false;
123 if( m_LayerId != aOther.m_LayerId ) return false;
124 if( m_DielectricLayerId != aOther.m_DielectricLayerId ) return false;
125 if( m_enabled != aOther.m_enabled ) return false;
126
127 if( !std::equal( std::begin( m_DielectricPrmsList ), std::end( m_DielectricPrmsList ),
128 std::begin( aOther.m_DielectricPrmsList ),
129 []( const DIELECTRIC_PRMS& aA, const DIELECTRIC_PRMS& aB )
130 {
131 return aA == aB;
132 } ) )
133 {
134 return false;
135 }
136
137 return true;
138}
139
140
141void BOARD_STACKUP_ITEM::AddDielectricPrms( int aDielectricPrmsIdx )
142{
143 // add a DIELECTRIC_PRMS item to m_DielectricPrmsList
144 DIELECTRIC_PRMS new_prms;
145
146 m_DielectricPrmsList.emplace( m_DielectricPrmsList.begin() + aDielectricPrmsIdx, new_prms );
147}
148
149
150void BOARD_STACKUP_ITEM::RemoveDielectricPrms( int aDielectricPrmsIdx )
151{
152 // Remove a DIELECTRIC_PRMS item from m_DielectricPrmsList if possible
153
154 if( GetSublayersCount() < 2
155 || aDielectricPrmsIdx < 0
156 || aDielectricPrmsIdx >= GetSublayersCount() )
157 {
158 return;
159 }
160
161 m_DielectricPrmsList.erase( m_DielectricPrmsList.begin() + aDielectricPrmsIdx );
162}
163
164
165
167{
168 // A reasonable thickness for copper layers:
169 return pcbIUScale.mmToIU( 0.035 );
170}
171
172
174{
175 // A reasonable thickness for solder mask:
176 return pcbIUScale.mmToIU( 0.01 );
177}
178
179
180// Getters:
181wxString BOARD_STACKUP_ITEM::GetColor( int aDielectricSubLayer ) const
182{
183 wxASSERT( aDielectricSubLayer >= 0 && aDielectricSubLayer < GetSublayersCount() );
184
185 return m_DielectricPrmsList[aDielectricSubLayer].m_Color;
186}
187
188int BOARD_STACKUP_ITEM::GetThickness( int aDielectricSubLayer ) const
189{
190 wxASSERT( aDielectricSubLayer >= 0 && aDielectricSubLayer < GetSublayersCount() );
191
192 return m_DielectricPrmsList[aDielectricSubLayer].m_Thickness;
193}
194
195
196double BOARD_STACKUP_ITEM::GetLossTangent( int aDielectricSubLayer ) const
197{
198 wxASSERT( aDielectricSubLayer >= 0 && aDielectricSubLayer < GetSublayersCount() );
199
200 return m_DielectricPrmsList[aDielectricSubLayer].m_LossTangent;
201}
202
203
204double BOARD_STACKUP_ITEM::GetSpecFreq( int aDielectricSubLayer ) const
205{
206 wxASSERT( aDielectricSubLayer >= 0 && aDielectricSubLayer < GetSublayersCount() );
207
208 return m_DielectricPrmsList[aDielectricSubLayer].m_SpecFreq;
209}
210
211
213{
214 wxASSERT( aDielectricSubLayer >= 0 && aDielectricSubLayer < GetSublayersCount() );
215
216 return m_DielectricPrmsList[aDielectricSubLayer].m_DielectricModel;
217}
218
219
220double BOARD_STACKUP_ITEM::GetEpsilonR( int aDielectricSubLayer ) const
221{
222 wxASSERT( aDielectricSubLayer >= 0 && aDielectricSubLayer < GetSublayersCount() );
223
224 return m_DielectricPrmsList[aDielectricSubLayer].m_EpsilonR;
225}
226
227
228bool BOARD_STACKUP_ITEM::IsThicknessLocked( int aDielectricSubLayer ) const
229{
230 wxASSERT( aDielectricSubLayer >= 0 && aDielectricSubLayer < GetSublayersCount() );
231
232 return m_DielectricPrmsList[aDielectricSubLayer].m_ThicknessLocked;
233}
234
235
236wxString BOARD_STACKUP_ITEM::GetMaterial( int aDielectricSubLayer ) const
237{
238 wxASSERT( aDielectricSubLayer >= 0 && aDielectricSubLayer < GetSublayersCount() );
239
240 return m_DielectricPrmsList[aDielectricSubLayer].m_Material;
241}
242
243
244// Setters:
245void BOARD_STACKUP_ITEM::SetColor( const wxString& aColorName , int aDielectricSubLayer )
246{
247 wxASSERT( aDielectricSubLayer >= 0 && aDielectricSubLayer < GetSublayersCount() );
248
249 if( aDielectricSubLayer >= 0 && aDielectricSubLayer < GetSublayersCount() )
250 m_DielectricPrmsList[aDielectricSubLayer].m_Color = aColorName;
251}
252
253
254void BOARD_STACKUP_ITEM::SetThickness( int aThickness, int aDielectricSubLayer )
255{
256 wxASSERT( aDielectricSubLayer >= 0 && aDielectricSubLayer < GetSublayersCount() );
257
258 if( aDielectricSubLayer >= 0 && aDielectricSubLayer < GetSublayersCount() )
259 m_DielectricPrmsList[aDielectricSubLayer].m_Thickness = aThickness;
260}
261
262
263void BOARD_STACKUP_ITEM::SetLossTangent( double aTg, int aDielectricSubLayer )
264{
265 wxASSERT( aDielectricSubLayer >= 0 && aDielectricSubLayer < GetSublayersCount() );
266
267 if( aDielectricSubLayer >= 0 && aDielectricSubLayer < GetSublayersCount() )
268 m_DielectricPrmsList[aDielectricSubLayer].m_LossTangent = aTg;
269}
270
271
272void BOARD_STACKUP_ITEM::SetSpecFreq( const double aSpecFreq, const int aDielectricSubLayer )
273{
274 wxASSERT( aDielectricSubLayer >= 0 && aDielectricSubLayer < GetSublayersCount() );
275
276 if( aDielectricSubLayer >= 0 && aDielectricSubLayer < GetSublayersCount() )
277 m_DielectricPrmsList[aDielectricSubLayer].m_SpecFreq = aSpecFreq;
278}
279
280
281void BOARD_STACKUP_ITEM::SetDielectricModel( DIELECTRIC_MODEL aModel, int aDielectricSubLayer )
282{
283 wxASSERT( aDielectricSubLayer >= 0 && aDielectricSubLayer < GetSublayersCount() );
284
285 if( aDielectricSubLayer >= 0 && aDielectricSubLayer < GetSublayersCount() )
286 m_DielectricPrmsList[aDielectricSubLayer].m_DielectricModel = aModel;
287}
288
289
290void BOARD_STACKUP_ITEM::SetEpsilonR( double aEpsilon, int aDielectricSubLayer )
291{
292 wxASSERT( aDielectricSubLayer >= 0 && aDielectricSubLayer < GetSublayersCount() );
293
294 if( aDielectricSubLayer >= 0 && aDielectricSubLayer < GetSublayersCount() )
295 m_DielectricPrmsList[aDielectricSubLayer].m_EpsilonR = aEpsilon;
296}
297
298
299void BOARD_STACKUP_ITEM::SetThicknessLocked( bool aLocked, int aDielectricSubLayer )
300{
301 wxASSERT( aDielectricSubLayer >= 0 && aDielectricSubLayer < GetSublayersCount() );
302
303 if( aDielectricSubLayer >= 0 && aDielectricSubLayer < GetSublayersCount() )
304 m_DielectricPrmsList[aDielectricSubLayer].m_ThicknessLocked = aLocked;
305}
306
307
308void BOARD_STACKUP_ITEM::SetMaterial( const wxString& aName, int aDielectricSubLayer )
309{
310 wxASSERT( aDielectricSubLayer >= 0 && aDielectricSubLayer < GetSublayersCount() );
311
312 if( aDielectricSubLayer >= 0 && aDielectricSubLayer < GetSublayersCount() )
313 m_DielectricPrmsList[aDielectricSubLayer].m_Material = aName;
314}
315
316
322
323
329
330
335
336
337bool BOARD_STACKUP_ITEM::HasMaterialValue( int aDielectricSubLayer ) const
338{
339 // return true if the material is specified
340 return IsMaterialEditable() && IsPrmSpecified( GetMaterial( aDielectricSubLayer ) );
341}
342
343
350
351
358
359
366
367
368wxString BOARD_STACKUP_ITEM::FormatEpsilonR( int aDielectricSubLayer ) const
369{
370 // return a wxString to print/display Epsilon R
371 // note: we do not want scientific notation
372 wxString txt = UIDouble2Str( GetEpsilonR( aDielectricSubLayer ) );
373 return txt;
374}
375
376
377wxString BOARD_STACKUP_ITEM::FormatLossTangent( int aDielectricSubLayer ) const
378{
379 // return a wxString to print/display Loss Tangent
380 // note: we do not want scientific notation
381 wxString txt = UIDouble2Str( GetLossTangent( aDielectricSubLayer ) );
382 return txt;
383}
384
385
387{
388 // return a wxString to print/display a dielectric name
389 wxString lname;
390 lname.Printf( _( "Dielectric %d" ), GetDielectricLayerId() );
391
392 return lname;
393}
394
395
397{
398 m_HasDielectricConstrains = false; // True if some dielectric layers have constrains
399 // (Loss tg and Epison R)
400 m_HasThicknessConstrains = false; // True if some dielectric or copper layers have constrains
402 m_EdgePlating = false; // True if edge board is plated
403 m_FinishType = wxT( "None" ); // undefined finish type
404}
405
406
408{
413 m_FinishType = aOther.m_FinishType;
414
415 // All items in aOther.m_list have to be duplicated, because aOther.m_list
416 // manage pointers to these items
417 for( BOARD_STACKUP_ITEM* item : aOther.m_list )
418 {
419 BOARD_STACKUP_ITEM* dup_item = new BOARD_STACKUP_ITEM( *item );
420 Add( dup_item );
421 }
422}
423
424
426{
431 m_FinishType = aOther.m_FinishType;
432
433 RemoveAll();
434
435 // All items in aOther.m_list have to be duplicated, because aOther.m_list
436 // manage pointers to these items
437 for( BOARD_STACKUP_ITEM* item : aOther.m_list )
438 {
439 BOARD_STACKUP_ITEM* dup_item = new BOARD_STACKUP_ITEM( *item );
440 Add( dup_item );
441 }
442
443 return *this;
444}
445
446
447bool BOARD_STACKUP::operator==( const BOARD_STACKUP& aOther ) const
448{
449 if( m_HasDielectricConstrains != aOther.m_HasDielectricConstrains ) return false;
450 if( m_HasThicknessConstrains != aOther.m_HasThicknessConstrains ) return false;
451 if( m_EdgeConnectorConstraints != aOther.m_EdgeConnectorConstraints ) return false;
452 if( m_EdgePlating != aOther.m_EdgePlating ) return false;
453 if( m_FinishType != aOther.m_FinishType ) return false;
454
455 if( m_list.size() != aOther.m_list.size() )
456 return false;
457
458 if( !std::equal( std::begin( m_list ), std::end( m_list ), std::begin( aOther.m_list ),
459 []( const BOARD_STACKUP_ITEM* aA, const BOARD_STACKUP_ITEM* aB )
460 {
461 return *aA == *aB;
462 } ) )
463 {
464 return false;
465 }
466
467 return true;
468}
469
470
471void BOARD_STACKUP::Serialize( kiapi::board::BoardStackup& stackup ) const
472{
473 using namespace kiapi::board;
474
475 for( const BOARD_STACKUP_ITEM* item : m_list )
476 {
477 BoardStackupLayer* layer = stackup.mutable_layers()->Add();
478
479 layer->mutable_thickness()->set_value_nm( item->GetThickness() );
480 layer->set_layer( ToProtoEnum<PCB_LAYER_ID, types::BoardLayer>( item->GetBrdLayerId() ) );
481 layer->set_enabled( item->IsEnabled() );
482 layer->set_type( ToProtoEnum<BOARD_STACKUP_ITEM_TYPE, BoardStackupLayerType>( item->GetType() ) );
483
484 if( item->IsColorEditable() )
485 {
486 wxString colorStr = item->GetColor( 0 );
487
488 if( IsPrmSpecified( colorStr ) )
489 {
490 KIGFX::COLOR4D color( colorStr );
491 kiapi::common::PackColor( *layer->mutable_color(), color );
492 }
493 }
494
495 switch( item->GetType() )
496 {
498 {
499 layer->set_material_name( "copper" );
500 // (no copper params yet...)
501 break;
502 }
503
505 {
506 BoardStackupDielectricLayer* dielectric = layer->mutable_dielectric();
507
508 const wxString& typeName = item->GetTypeName();
509
510 if( typeName == KEY_CORE )
511 dielectric->set_type( BoardStackupDielectricType::BSDT_CORE );
512 else if( typeName == KEY_PREPREG )
513 dielectric->set_type( BoardStackupDielectricType::BSDT_PREPREG );
514 else
515 dielectric->set_type( BoardStackupDielectricType::BSDT_NONE );
516
517 for( int i = 0; i < item->GetSublayersCount(); ++i )
518 {
519 BoardStackupDielectricProperties* props = dielectric->mutable_layer()->Add();
520 props->set_epsilon_r( item->GetEpsilonR( i ) );
521 props->set_loss_tangent( item->GetLossTangent( i ) );
522 props->set_material_name( item->GetMaterial( i ).ToUTF8() );
523 props->mutable_thickness()->set_value_nm( item->GetThickness( i ) );
524
525 props->set_thickness_locked( item->IsThicknessLocked( i ) );
526
527 if( item->GetSpecFreq( i ) != 0.0 )
528 props->set_spec_frequency( item->GetSpecFreq( i ) );
529
530 props->set_dielectric_model(
531 ToProtoEnum<DIELECTRIC_MODEL, DielectricModel>( item->GetDielectricModel( i ) ) );
532 }
533
534 break;
535 }
536
538 {
539 BoardStackupSoldermaskLayer* soldermask = layer->mutable_soldermask();
540 soldermask->set_epsilon_r( item->GetEpsilonR( 0 ) );
541 soldermask->set_loss_tangent( item->GetLossTangent( 0 ) );
542 soldermask->set_material_name( item->GetMaterial( 0 ).ToUTF8() );
543 soldermask->mutable_thickness()->set_value_nm( item->GetThickness( 0 ) );
544 break;
545 }
546
548 {
549 BoardStackupSilkscreenLayer* silkscreen = layer->mutable_silkscreen();
550 silkscreen->set_material_name( item->GetMaterial( 0 ).ToUTF8() );
551 break;
552 }
553
554 default:
555 break;
556 }
557 }
558
559 stackup.mutable_finish()->set_type_name( m_FinishType.ToUTF8() );
560 stackup.mutable_impedance()->set_is_controlled( m_HasDielectricConstrains );
561
562 BoardEdgeSettings* edge = stackup.mutable_edge();
563 edge->mutable_connector()->set_type(
565 edge->mutable_plating()->set_has_edge_plating( m_EdgePlating );
566
567}
568
569
570void BOARD_STACKUP::Serialize( google::protobuf::Any& aContainer ) const
571{
572 kiapi::board::BoardStackup stackup;
573 Serialize( stackup );
574
575 if( !aContainer.PackFrom( stackup ) )
576 wxFAIL_MSG( wxT( "Failed to pack BOARD_STACKUP" ) );
577}
578
579
580bool BOARD_STACKUP::Deserialize( const google::protobuf::Any& aContainer )
581{
582 // Read-only for now
583 kiapi::board::BoardStackup stackup;
584
585 if( !aContainer.UnpackTo( &stackup ) )
586 return false;
587
588 return Deserialize( stackup );
589}
590
591
592bool BOARD_STACKUP::Deserialize( const kiapi::board::BoardStackup& aInput )
593{
594 // Read-only for now
595 return false;
596}
597
598
600{
601 for( BOARD_STACKUP_ITEM* item : m_list )
602 delete item;
603
604 m_list.clear();
605}
606
607
609{
610 if( aIndex < 0 || aIndex >= GetCount() )
611 return nullptr;
612
613 return GetList()[aIndex];
614}
615
616
618{
619 // return the board thickness from the thickness of BOARD_STACKUP_ITEM list
620 int thickness = 0;
621
622 for( BOARD_STACKUP_ITEM* item : m_list )
623 {
624 if( item->IsThicknessEditable() && item->IsEnabled() )
625 {
626 thickness += item->GetThickness();
627
628 // dielectric layers can have more than one main layer
629 // add thickness of all sublayers
630 for( int idx = 1; idx < item->GetSublayersCount(); idx++ )
631 {
632 thickness += item->GetThickness( idx );
633 }
634 }
635 }
636
637 return thickness;
638}
639
640
642{
643 bool change = false;
644 // Build the suitable stackup:
645 BOARD_STACKUP stackup;
646 stackup.BuildDefaultStackupList( aSettings );
647
648 auto sameLayer =
649 []( const BOARD_STACKUP_ITEM* aFirst, const BOARD_STACKUP_ITEM* aSecond )
650 {
651 return aFirst->GetBrdLayerId() == aSecond->GetBrdLayerId()
652 && ( aFirst->GetBrdLayerId() != UNDEFINED_LAYER
653 || aFirst->GetDielectricLayerId() == aSecond->GetDielectricLayerId() );
654 };
655
656 // First, find removed layers:
657 for( BOARD_STACKUP_ITEM* curr_item: m_list )
658 {
659 if( std::ranges::none_of( stackup.GetList(),
660 [&]( const BOARD_STACKUP_ITEM* aItem )
661 {
662 return sameLayer( curr_item, aItem );
663 } ) )
664 {
665 change = true;
666 break;
667 }
668 }
669
670 // Now initialize all stackup items to the initial values, when exist
671 for( BOARD_STACKUP_ITEM* item : stackup.GetList() )
672 {
673 auto initial = std::ranges::find_if( m_list,
674 [&]( const BOARD_STACKUP_ITEM* aItem )
675 {
676 return sameLayer( item, aItem );
677 } );
678
679 if( initial != m_list.end() )
680 *item = **initial;
681 else
682 change = true;
683 }
684
685 // Transfer layer settings:
686 *this = stackup;
687
688 // Transfer other stackup settings from aSettings
689 const BOARD_STACKUP& source_stackup = aSettings->GetStackupDescriptor();
692 m_EdgePlating = source_stackup.m_EdgePlating;
693 m_FinishType = source_stackup.m_FinishType;
694
695 return change;
696}
697
698
699void BOARD_STACKUP::BuildDefaultStackupList( const BOARD_DESIGN_SETTINGS* aSettings, int aActiveCopperLayersCount )
700{
701 // Creates a default stackup, according to the current BOARD_DESIGN_SETTINGS settings.
702 // Note: the m_TypeName string is made translatable using _HKI marker, but is not
703 // translated when building the stackup.
704 // It will be used as this in files, and can be translated only in dialog
705 // if aSettings == NULL, build a full stackup (with 32 copper layers)
706 LSET enabledLayer = aSettings ? aSettings->GetEnabledLayers() : StackupAllowedBrdLayers();
707 int copperLayerCount = aSettings ? aSettings->GetCopperLayerCount() : 32;
708
709 // We need to calculate a suitable dielectric layer thickness.
710 // If no settings, and if aActiveCopperLayersCount is given, use it
711 // (If no settings, and no aActiveCopperLayersCount, the full 32 layers are used)
712 int activeCuLayerCount = copperLayerCount;
713
714 if( aSettings == nullptr && aActiveCopperLayersCount > 0 )
715 activeCuLayerCount = aActiveCopperLayersCount;
716
717 int brd__thickness = aSettings ? aSettings->GetBoardThickness() : pcbIUScale.mmToIU( 1.6 );
718 int diel_thickness = brd__thickness - ( BOARD_STACKUP_ITEM::GetCopperDefaultThickness() * activeCuLayerCount );
719
720 // Take in account the solder mask thickness:
721 int sm_count = ( enabledLayer & LSET( { F_Mask, B_Mask } ) ).count();
722 diel_thickness -= BOARD_STACKUP_ITEM::GetMaskDefaultThickness() * sm_count;
723 diel_thickness /= std::max( 1, activeCuLayerCount - 1 );
724
725 int dielectric_idx = 0;
726
727 // Add silk screen, solder mask and solder paste layers on top
728 if( enabledLayer[F_SilkS] )
729 {
731 item->SetBrdLayerId( F_SilkS );
732 item->SetTypeName( _HKI( "Top Silk Screen" ) );
733 Add( item );
734 }
735
736 if( enabledLayer[F_Paste] )
737 {
739 item->SetBrdLayerId( F_Paste );
740 item->SetTypeName( _HKI( "Top Solder Paste" ) );
741 Add( item );
742 }
743
744 if( enabledLayer[F_Mask] )
745 {
747 item->SetBrdLayerId( F_Mask );
748 item->SetTypeName( _HKI( "Top Solder Mask" ) );
749 Add( item );
750 }
751
752 // Add copper and dielectric layers
753 for( PCB_LAYER_ID layer : enabledLayer.CuStack() )
754 {
756 item->SetBrdLayerId( layer );
757 item->SetTypeName( KEY_COPPER );
758 Add( item );
759
760 if( layer == B_Cu )
761 break;
762
763 // Add the dielectric layer:
765 item->SetThickness( diel_thickness );
766 item->SetDielectricLayerId( dielectric_idx + 1 );
767
768 // Display a dielectric default layer name:
769 if( (dielectric_idx & 1) == 0 )
770 {
771 item->SetTypeName( KEY_CORE );
772 item->SetMaterial( wxT( "FR4" ) );
773 }
774 else
775 {
776 item->SetTypeName( KEY_PREPREG );
777 item->SetMaterial( wxT( "FR4" ) );
778 }
779
780 Add( item );
781 dielectric_idx++;
782 }
783
784 // Add silk screen, solder mask and solder paste layers on bottom
785 if( enabledLayer[B_Mask] )
786 {
788 item->SetBrdLayerId( B_Mask );
789 item->SetTypeName( _HKI( "Bottom Solder Mask" ) );
790 Add( item );
791 }
792
793 if( enabledLayer[B_Paste] )
794 {
796 item->SetBrdLayerId( B_Paste );
797 item->SetTypeName( _HKI( "Bottom Solder Paste" ) );
798 Add( item );
799 }
800
801 if( enabledLayer[B_SilkS] )
802 {
804 item->SetBrdLayerId( B_SilkS );
805 item->SetTypeName( _HKI( "Bottom Silk Screen" ) );
806 Add( item );
807 }
808
809 // Transfer other stackup settings from aSettings
810 if( aSettings )
811 {
812 const BOARD_STACKUP& source_stackup = aSettings->GetStackupDescriptor();
815 m_EdgePlating = source_stackup.m_EdgePlating;
816 m_FinishType = source_stackup.m_FinishType;
817 }
818}
819
820
821void BOARD_STACKUP::FormatBoardStackup( OUTPUTFORMATTER* aFormatter, const BOARD* aBoard ) const
822{
823 // Board stackup is the ordered list from top to bottom of
824 // physical layers and substrate used to build the board.
825 if( m_list.empty() )
826 return;
827
828 aFormatter->Print( "(stackup" );
829
830 // Note:
831 // Unspecified parameters are not stored in file.
832 for( BOARD_STACKUP_ITEM* item: m_list )
833 {
834 wxString layer_name;
835
836 if( item->GetBrdLayerId() == UNDEFINED_LAYER )
837 layer_name.Printf( wxT( "dielectric %d" ), item->GetDielectricLayerId() );
838 else
839 layer_name = LSET::Name( item->GetBrdLayerId() );
840
841 aFormatter->Print( "(layer %s (type %s)",
842 aFormatter->Quotew( layer_name ).c_str(),
843 aFormatter->Quotew( item->GetTypeName() ).c_str() );
844
845 // Output other parameters (in sub layer list there is at least one item)
846 for( int idx = 0; idx < item->GetSublayersCount(); idx++ )
847 {
848 if( idx ) // not for the main (first) layer.
849 aFormatter->Print( " addsublayer" );
850
851 if( item->IsColorEditable() && IsPrmSpecified( item->GetColor( idx ) ) )
852 {
853 aFormatter->Print( "(color %s)",
854 aFormatter->Quotew( item->GetColor( idx ) ).c_str() );
855 }
856
857 if( item->IsThicknessEditable() )
858 {
859 aFormatter->Print( "(thickness %s",
860 EDA_UNIT_UTILS::FormatInternalUnits( pcbIUScale, item->GetThickness( idx ) ).c_str() );
861
862 if( item->GetType() == BS_ITEM_TYPE_DIELECTRIC && item->IsThicknessLocked( idx ) )
863 aFormatter->Print( " locked" );
864
865 aFormatter->Print( ")" );
866 }
867
868 if( item->HasMaterialValue( idx ) )
869 {
870 aFormatter->Print( "(material %s)",
871 aFormatter->Quotew( item->GetMaterial( idx ) ).c_str() );
872 }
873
874 if( item->HasEpsilonRValue() && item->HasMaterialValue( idx ) )
875 aFormatter->Print( "(epsilon_r %s)", FormatDouble2Str( item->GetEpsilonR( idx ) ).c_str() );
876
877 if( item->HasLossTangentValue() && item->HasMaterialValue( idx ) )
878 {
879 aFormatter->Print( "(loss_tangent %s)",
880 FormatDouble2Str( item->GetLossTangent( idx ) ).c_str() );
881 }
882
883 if( item->HasSpecFreqValue() && item->HasMaterialValue( idx ) )
884 {
885 aFormatter->Print( "(spec_frequency %s)", FormatDouble2Str( item->GetSpecFreq( idx ) ).c_str() );
886
887 wxString modelToken = wxT( "constant" );
888
889 if( item->GetDielectricModel( idx ) == DIELECTRIC_MODEL::DJORDJEVIC_SARKAR )
890 modelToken = wxT( "djordjevic_sarkar" );
891
892 aFormatter->Print( "(dielectric_model %s)", modelToken.ToStdString().c_str() );
893 }
894 }
895
896 aFormatter->Print( ")" );
897 }
898
899 // Other infos about board, related to layers and other fabrication specifications
901 aFormatter->Print( "(copper_finish %s)", aFormatter->Quotew( m_FinishType ).c_str() );
902
903 KICAD_FORMAT::FormatBool( aFormatter, "dielectric_constraints", m_HasDielectricConstrains );
904
906 {
907 aFormatter->Print( "(edge_connector %s)",
908 m_EdgeConnectorConstraints > 1 ? "bevelled": "yes" );
909 }
910
911 if( m_EdgePlating )
912 KICAD_FORMAT::FormatBool( aFormatter, "edge_plating", true );
913
914 aFormatter->Print( ")" );
915}
916
917
918int BOARD_STACKUP::GetLayerDistance( PCB_LAYER_ID aFirstLayer, PCB_LAYER_ID aSecondLayer ) const
919{
920 wxASSERT( IsCopperLayer( aFirstLayer ) && IsCopperLayer( aSecondLayer ) );
921
922 if( aFirstLayer == aSecondLayer )
923 return 0;
924
925 // B_Cu is always the last copper layer but doesn't have the last numerical value
926 if( aSecondLayer != B_Cu && ( aSecondLayer < aFirstLayer || aFirstLayer == B_Cu ) )
927 std::swap( aFirstLayer, aSecondLayer );
928
929 int total = 0;
930 bool start = false;
931 bool half = false;
932
933 for( const BOARD_STACKUP_ITEM* item : m_list )
934 {
935 // Will be UNDEFINED_LAYER for dielectrics
936 const PCB_LAYER_ID layer = item->GetBrdLayerId();
937
938 if( layer != UNDEFINED_LAYER && !IsCopperLayer( layer ) )
939 continue; // Silk/mask layer
940
941 // Reached the start copper layer? Start counting the next dielectric after it
942 if( !start && ( layer != UNDEFINED_LAYER && layer == aFirstLayer ) )
943 {
944 start = true;
945
946 // Only count half of each internal copper layer
947 if( aFirstLayer != F_Cu && aFirstLayer != B_Cu )
948 half = true;
949 }
950 else if( !start )
951 {
952 continue;
953 }
954
955 // Reached the stop copper layer? we're done
956 if( start && ( layer != UNDEFINED_LAYER && layer == aSecondLayer ) )
957 {
958 // Only count half of each internal copper layer
959 if( aSecondLayer != F_Cu && aSecondLayer != B_Cu )
960 half = true;
961 }
962
963 for( int sublayer = 0; sublayer < item->GetSublayersCount(); sublayer++ )
964 {
965 const int subThickness = item->GetThickness( sublayer );
966 total += half ? ( subThickness / 2 ) : subThickness;
967 }
968
969 half = false;
970
971 if( layer != UNDEFINED_LAYER && layer == aSecondLayer )
972 break;
973 }
974
975 return total;
976}
977
978
979int BOARD_STACKUP::GetDrillDepth( PCB_LAYER_ID aFirstLayer, PCB_LAYER_ID aSecondLayer ) const
980{
981 int total = 0;
982 bool half = false;
983 bool done = false;
984
985 auto processStackupItem =
986 [&]( const BOARD_STACKUP_ITEM* item )
987 {
988 // Will be UNDEFINED_LAYER for dielectrics
989 const PCB_LAYER_ID layer = item->GetBrdLayerId();
990
991 if( layer != UNDEFINED_LAYER && !IsCopperLayer( layer ) )
992 return; // Silk/mask layer
993
994 if( item->GetSublayersCount() > 0 )
995 {
996 for( int sublayer = 0; sublayer < item->GetSublayersCount(); sublayer++ )
997 {
998 const int subThickness = item->GetThickness( sublayer );
999 total += half ? ( subThickness / 2 ) : subThickness;
1000 }
1001 }
1002 else if( IsCopperLayer( layer ) )
1003 {
1005 }
1006
1007 if( half )
1008 done = true;
1009 else if( layer == aSecondLayer )
1010 half = true;
1011 };
1012
1013 if( IsCopperLayerLowerThan( aSecondLayer, aFirstLayer ) )
1014 {
1015 for( const BOARD_STACKUP_ITEM* item : m_list )
1016 {
1017 processStackupItem( item );
1018
1019 if( done )
1020 return total;
1021 }
1022 }
1023 else
1024 {
1025 for( const BOARD_STACKUP_ITEM* item : std::ranges::reverse_view( m_list ) )
1026 {
1027 processStackupItem( item );
1028
1029 if( done )
1030 return total;
1031 }
1032 }
1033
1034 return total;
1035}
1036
1037
1038bool IsPrmSpecified( const wxString& aPrmValue )
1039{
1040 // return true if the param value is specified:
1041
1042 if( !aPrmValue.IsEmpty()
1043 && ( aPrmValue.CmpNoCase( NotSpecifiedPrm() ) != 0 )
1044 && aPrmValue != wxGetTranslation( NotSpecifiedPrm() ) )
1045 {
1046 return true;
1047 }
1048
1049 return false;
1050}
KICOMMON_API types::KiCadObjectType ToProtoEnum(KICAD_T aValue)
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:128
bool IsPrmSpecified(const wxString &aPrmValue)
@ BS_EDGE_CONNECTOR_NONE
BOARD_STACKUP_ITEM_TYPE
@ BS_ITEM_TYPE_UNDEFINED
@ BS_ITEM_TYPE_COPPER
@ BS_ITEM_TYPE_SILKSCREEN
@ BS_ITEM_TYPE_DIELECTRIC
@ BS_ITEM_TYPE_SOLDERPASTE
@ BS_ITEM_TYPE_SOLDERMASK
Container for design settings for a BOARD object.
const LSET & GetEnabledLayers() const
Return a bit-mask of all the layers that are enabled.
int GetBoardThickness() const
The full thickness of the board including copper and masks.
BOARD_STACKUP & GetStackupDescriptor()
Manage one layer needed to make a physical board.
void AddDielectricPrms(int aDielectricPrmsIdx)
Add (insert) a DIELECTRIC_PRMS item to m_DielectricPrmsList all values are set to default.
DIELECTRIC_MODEL GetDielectricModel(int aDielectricSubLayer=0) const
PCB_LAYER_ID m_LayerId
the layer id (F.Cu to B.Cu, F.Silk, B.silk, F.Mask, B.Mask) and UNDEFINED_LAYER (-1) for dielectric l...
int GetSublayersCount() const
bool m_enabled
true if this stackup item must be taken in account, false to ignore it.
void SetDielectricLayerId(int aLayerId)
bool HasSpecFreqValue() const
double GetEpsilonR(int aDielectricSubLayer=0) const
wxString GetColor(int aDielectricSubLayer=0) const
bool HasEpsilonRValue() const
void SetThickness(int aThickness, int aDielectricSubLayer=0)
bool IsMaterialEditable() const
BOARD_STACKUP_ITEM_TYPE m_Type
void SetDielectricModel(DIELECTRIC_MODEL aModel, int aDielectricSubLayer=0)
bool HasMaterialValue(int aDielectricSubLayer=0) const
void SetThicknessLocked(bool aLocked, int aDielectricSubLayer=0)
wxString FormatDielectricLayerName() const
void SetSpecFreq(double aSpecFreq, int aDielectricSubLayer=0)
void SetMaterial(const wxString &aName, int aDielectricSubLayer=0)
BOARD_STACKUP_ITEM(BOARD_STACKUP_ITEM_TYPE aType)
bool HasLossTangentValue() const
PCB_LAYER_ID GetBrdLayerId() const
bool IsThicknessEditable() const
void SetLossTangent(double aTg, int aDielectricSubLayer=0)
wxString m_LayerName
name of layer as shown in layer manager. Useful to create reports
int GetThickness(int aDielectricSubLayer=0) const
void SetEnabled(bool aEnable)
std::vector< DIELECTRIC_PRMS > m_DielectricPrmsList
List of dielectric parameters usually only one item, but in complex (microwave) boards,...
static int GetMaskDefaultThickness()
wxString GetMaterial(int aDielectricSubLayer=0) const
wxString m_TypeName
type name of layer (copper, silk screen, core, prepreg ...)
void SetBrdLayerId(PCB_LAYER_ID aBrdLayerId)
void SetTypeName(const wxString &aName)
bool IsThicknessLocked(int aDielectricSubLayer=0) const
wxString FormatEpsilonR(int aDielectricSubLayer=0) const
int m_DielectricLayerId
the "layer" id for dielectric layers, from 1 (top) to 31 (bottom) (only 31 dielectric layers for 32 c...
void SetColor(const wxString &aColorName, int aDielectricSubLayer=0)
void SetEpsilonR(double aEpsilon, int aDielectricSubLayer=0)
double GetSpecFreq(int aDielectricSubLayer=0) const
void RemoveDielectricPrms(int aDielectricPrmsIdx)
Remove a DIELECTRIC_PRMS item from m_DielectricPrmsList.
int GetDielectricLayerId() const
bool operator==(const BOARD_STACKUP_ITEM &aOther) const
bool IsColorEditable() const
wxString FormatLossTangent(int aDielectricSubLayer=0) const
double GetLossTangent(int aDielectricSubLayer=0) const
static int GetCopperDefaultThickness()
int GetDrillDepth(PCB_LAYER_ID aFirstLayer, PCB_LAYER_ID aSecondLayer) const
Calculate the drill depth needed to cut from aFirstLayer /through/ aSecondLayer.
void RemoveAll()
Delete all items in list and clear the list.
const std::vector< BOARD_STACKUP_ITEM * > & GetList() const
static LSET StackupAllowedBrdLayers()
int GetCount() const
bool SynchronizeWithBoard(const BOARD_DESIGN_SETTINGS *aSettings)
Synchronize the BOARD_STACKUP_ITEM* list with the board.
bool Deserialize(const google::protobuf::Any &aContainer) override
Deserializes the given protobuf message into this object.
bool operator==(const BOARD_STACKUP &aOther) const
int BuildBoardThicknessFromStackup() const
bool m_HasDielectricConstrains
True if some layers have impedance controlled tracks or have specific constrains for micro-wave appli...
void Add(BOARD_STACKUP_ITEM *aItem)
Add a new item in stackup layer.
void BuildDefaultStackupList(const BOARD_DESIGN_SETTINGS *aSettings, int aActiveCopperLayersCount=0)
Create a default stackup, according to the current BOARD_DESIGN_SETTINGS settings.
void Serialize(google::protobuf::Any &aContainer) const override
Serializes this object to the given Any message.
BOARD_STACKUP & operator=(const BOARD_STACKUP &aOther)
BOARD_STACKUP_ITEM * GetStackupLayer(int aIndex)
int GetLayerDistance(PCB_LAYER_ID aFirstLayer, PCB_LAYER_ID aSecondLayer) const
Calculate the distance (height) between the two given copper layers.
std::vector< BOARD_STACKUP_ITEM * > m_list
bool m_EdgePlating
True if the edge board is plated.
BS_EDGE_CONNECTOR_CONSTRAINTS m_EdgeConnectorConstraints
If the board has edge connector cards, some constrains can be specified in job file: BS_EDGE_CONNECTO...
void FormatBoardStackup(OUTPUTFORMATTER *aFormatter, const BOARD *aBoard) const
Write the stackup info on board file.
bool m_HasThicknessConstrains
True if some layers (copper and/or dielectric) have specific thickness.
wxString m_FinishType
The name of external copper finish.
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:411
A helper class to manage a dielectric layer set of parameters.
double m_EpsilonR
For dielectric (and solder mask) the dielectric constant.
double m_SpecFreq
For dielectric the frequency (Hz) at which E_R is specified.
int m_Thickness
the physical layer thickness in internal units
wxString m_Material
type of material (for dielectric and solder mask)
bool operator==(const DIELECTRIC_PRMS &aOther) const
wxString m_Color
mainly for silkscreen and solder mask
DIELECTRIC_MODEL m_DielectricModel
For dielectric the dielectric frequency correction model.
bool m_ThicknessLocked
true for dielectric layers with a fixed thickness (for impedance controlled purposes),...
double m_LossTangent
For dielectric (and solder mask) the dielectric loss.
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:101
LSET is a set of PCB_LAYER_IDs.
Definition lset.h:37
LSEQ CuStack() const
Return a sequence of copper layers in starting from the front/top and extending to the back/bottom.
Definition lset.cpp:259
static wxString Name(PCB_LAYER_ID aLayerId)
Return the fixed name association with aLayerId.
Definition lset.cpp:184
An interface used to output 8 bit text in a convenient way.
Definition richio.h:294
std::string Quotew(const wxString &aWrapee) const
Definition richio.cpp:505
int PRINTF_FUNC_N Print(int nestLevel, const char *fmt,...)
Format and write text to the output stream.
Definition richio.cpp:432
#define _(s)
Some functions to handle hotkeys in KiCad.
bool IsCopperLayerLowerThan(PCB_LAYER_ID aLayerA, PCB_LAYER_ID aLayerB)
Return true if copper aLayerA is placed lower than aLayerB, false otherwise.
Definition layer_ids.h:891
bool IsCopperLayer(int aLayerId)
Test whether a layer is a copper layer.
Definition layer_ids.h:744
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:56
@ F_Paste
Definition layer_ids.h:100
@ B_Mask
Definition layer_ids.h:94
@ B_Cu
Definition layer_ids.h:61
@ F_Mask
Definition layer_ids.h:93
@ B_Paste
Definition layer_ids.h:101
@ F_SilkS
Definition layer_ids.h:96
@ UNDEFINED_LAYER
Definition layer_ids.h:57
@ B_SilkS
Definition layer_ids.h:97
@ F_Cu
Definition layer_ids.h:60
KICOMMON_API std::string FormatInternalUnits(const EDA_IU_SCALE &aIuScale, int aValue, EDA_DATA_TYPE aDataType=EDA_DATA_TYPE::DISTANCE)
Converts aValue from internal units to a string appropriate for writing to file.
void FormatBool(OUTPUTFORMATTER *aOut, const wxString &aKey, bool aValue)
Writes a boolean to the formatter, in the style (aKey [yes|no])
KICOMMON_API void PackColor(types::Color &aOutput, const KIGFX::COLOR4D &aInput)
#define _HKI(x)
Definition page_info.cpp:40
wxString NotSpecifiedPrm()
#define KEY_PREPREG
#define KEY_COPPER
#define KEY_CORE
#define DEFAULT_EPSILON_R_SILKSCREEN
#define DEFAULT_EPSILON_R_SOLDERMASK
std::string UIDouble2Str(double aValue)
Print a float number without using scientific notation and no trailing 0 We want to avoid scientific ...
std::string FormatDouble2Str(double aValue)
Print a float number without using scientific notation and no trailing 0 This function is intended in...
DIELECTRIC_MODEL
Frequency-domain model used for the substrate dielectric properties.