KiCad PCB EDA Suite
Loading...
Searching...
No Matches
padstack.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) 2024 Jon Evans <[email protected]>
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include <convert_basic_shapes_to_polygon.h> // RECT_CHAMFER_POSITIONS
22#include "padstack.h"
23#include <api/api_enums.h>
24#include <api/api_utils.h>
25#include <api/api_pcb_utils.h>
26#include <api/board/board_types.pb.h>
27#include <layer_range.h>
28#include <macros.h>
29#include <magic_enum.hpp>
30#include <pad.h>
31#include <board.h>
32#include <pcb_shape.h>
33#include <properties/property.h>
35
36
40
41
67
68
70{
71 m_parent = aOther.m_parent;
72 *this = aOther;
73
75 [&]( PCB_LAYER_ID aLayer )
76 {
77 for( std::shared_ptr<PCB_SHAPE>& shape : CopperLayer( aLayer ).custom_shapes )
78 shape->SetParent( m_parent );
79 } );
80}
81
82
84{
85 // NOTE: m_parent is not copied from operator=, because this operator is commonly used to
86 // update the padstack properties, and such an update must not change the parent PAD to point to
87 // the parent of some different padstack.
88
89 m_mode = aOther.m_mode;
90 m_layerSet = aOther.m_layerSet;
98 m_drill = aOther.m_drill;
103
104 // Data consistency enforcement logic that used to live in the pad properties dialog.
105 // While it might be tempting to put these in the individual property setters, there's no
106 // well-defined order in which they're called, and many of the consistency checks are
107 // between multiple properties.
108
110 [&]( PCB_LAYER_ID aLayer )
111 {
112 PAD_SHAPE shape = Shape( aLayer );
113
114 // Make sure leftover primitives don't stick around
115 ClearPrimitives( aLayer );
116
117 // For custom pad shape, duplicate primitives of the pad to copy
118 if( shape == PAD_SHAPE::CUSTOM )
119 ReplacePrimitives( aOther.Primitives( aLayer ), aLayer );
120
121 // rounded rect pads with radius ratio = 0 are in fact rect pads.
122 // So set the right shape (and perhaps issues with a radius = 0)
123 if( shape == PAD_SHAPE::ROUNDRECT && RoundRectRadiusRatio( aLayer ) == 0.0 )
125 } );
126
127 return *this;
128}
129
130
131bool PADSTACK::operator==( const PADSTACK& aOther ) const
132{
133 if( m_mode != aOther.m_mode )
134 return false;
135
136 if( m_layerSet != aOther.m_layerSet )
137 return false;
138
139 if( m_customName != aOther.m_customName )
140 return false;
141
142 if( m_orientation != aOther.m_orientation )
143 return false;
144
145 if( m_frontMaskProps != aOther.m_frontMaskProps )
146 return false;
147
148 if( m_backMaskProps != aOther.m_backMaskProps )
149 return false;
150
152 return false;
153
155 return false;
156
157 if( m_drill != aOther.m_drill )
158 return false;
159
160 if( m_secondaryDrill != aOther.m_secondaryDrill )
161 return false;
162
163 if( m_tertiaryDrill != aOther.m_tertiaryDrill )
164 return false;
165
167 return false;
168
170 return false;
171
172 bool copperMatches = true;
173
175 [&]( PCB_LAYER_ID aLayer )
176 {
177 if( CopperLayer( aLayer ) != aOther.CopperLayer( aLayer ) )
178 copperMatches = false;
179 } );
180
181 return copperMatches;
182}
183
184
185bool PADSTACK::unpackCopperLayer( const kiapi::board::types::PadStackLayer& aProto )
186{
187 using namespace kiapi::board::types;
189
190 if( m_mode == MODE::NORMAL && layer != ALL_LAYERS )
191 return false;
192
193 if( m_mode == MODE::FRONT_INNER_BACK && layer != F_Cu && layer != INNER_LAYERS && layer != B_Cu )
194 return false;
195
196 SetSize( kiapi::common::UnpackVector2( aProto.size() ), layer );
197 SetShape( FromProtoEnum<PAD_SHAPE>( aProto.shape() ), layer );
198 Offset( layer ) = kiapi::common::UnpackVector2( aProto.offset() );
199 SetAnchorShape( FromProtoEnum<PAD_SHAPE>( aProto.custom_anchor_shape() ), layer );
200
201 SHAPE_PROPS& props = CopperLayer( layer ).shape;
202 props.chamfered_rect_ratio = aProto.chamfer_ratio();
203 props.round_rect_radius_ratio = aProto.corner_rounding_ratio();
204
205 if( Shape( layer ) == PAD_SHAPE::TRAPEZOID && aProto.has_trapezoid_delta() )
206 TrapezoidDeltaSize( layer ) = kiapi::common::UnpackVector2( aProto.trapezoid_delta() );
207
208 if( aProto.chamfered_corners().top_left() )
210
211 if( aProto.chamfered_corners().top_right() )
213
214 if( aProto.chamfered_corners().bottom_left() )
216
217 if( aProto.chamfered_corners().bottom_right() )
219
220 ClearPrimitives( layer );
221 google::protobuf::Any a;
222
223 for( const BoardGraphicShape& shapeProto : aProto.custom_shapes() )
224 {
225 a.PackFrom( shapeProto );
226 std::unique_ptr<PCB_SHAPE> shape = std::make_unique<PCB_SHAPE>( m_parent );
227
228 if( shape->Deserialize( a ) )
229 AddPrimitive( shape.release(), layer );
230 }
231
232 return true;
233}
234
235
236bool PADSTACK::Deserialize( const google::protobuf::Any& aContainer )
237{
238 using namespace kiapi::board::types;
239 PadStack padstack;
240
241 auto unpackOptional = []<typename ProtoEnum>( const ProtoEnum& aProto,
242 std::optional<bool>& aDest, ProtoEnum aTrueValue,
243 ProtoEnum aFalseValue )
244 {
245 if( aProto == aTrueValue )
246 aDest = true;
247 else if( aProto == aFalseValue )
248 aDest = false;
249 else
250 aDest = std::nullopt;
251 };
252
253 auto unpackPostMachining = []( const PostMachiningProperties& aProto,
255 {
256 switch( aProto.mode() )
257 {
258 case VDPM_NOT_POST_MACHINED: aDest.mode = PAD_DRILL_POST_MACHINING_MODE::NOT_POST_MACHINED; break;
259 case VDPM_COUNTERBORE: aDest.mode = PAD_DRILL_POST_MACHINING_MODE::COUNTERBORE; break;
260 case VDPM_COUNTERSINK: aDest.mode = PAD_DRILL_POST_MACHINING_MODE::COUNTERSINK; break;
261 default: aDest.mode = std::nullopt; break;
262 }
263
264 aDest.size = aProto.size();
265 aDest.depth = aProto.depth();
266 aDest.angle = aProto.angle();
267 };
268
269 if( !aContainer.UnpackTo( &padstack ) )
270 return false;
271
272 m_mode = FromProtoEnum<MODE>( padstack.type() );
273 SetLayerSet( kiapi::board::UnpackLayerSet( padstack.layers() ) );
274 m_orientation = EDA_ANGLE( padstack.angle().value_degrees(), DEGREES_T );
275
276 Drill().size = kiapi::common::UnpackVector2( padstack.drill().diameter() );
277 Drill().start = FromProtoEnum<PCB_LAYER_ID>( padstack.drill().start_layer() );
278 Drill().end = FromProtoEnum<PCB_LAYER_ID>( padstack.drill().end_layer() );
279 unpackOptional( padstack.drill().capped(), Drill().is_capped, VDCM_CAPPED, VDCM_UNCAPPED );
280 unpackOptional( padstack.drill().filled(), Drill().is_filled, VDFM_FILLED, VDFM_UNFILLED );
281
282 if( padstack.has_front_post_machining() )
283 unpackPostMachining( padstack.front_post_machining(), FrontPostMachining() );
284
285 if( padstack.has_back_post_machining() )
286 unpackPostMachining( padstack.back_post_machining(), BackPostMachining() );
287
288 Drill().shape = FromProtoEnum<PAD_DRILL_SHAPE>( padstack.drill().shape() );
289
290 if( padstack.has_secondary_drill() )
291 {
292 const DrillProperties& secondary = padstack.secondary_drill();
293
294 SecondaryDrill().size = kiapi::common::UnpackVector2( secondary.diameter() );
295 SecondaryDrill().start = FromProtoEnum<PCB_LAYER_ID>( secondary.start_layer() );
296 SecondaryDrill().end = FromProtoEnum<PCB_LAYER_ID>( secondary.end_layer() );
297 SecondaryDrill().shape = FromProtoEnum<PAD_DRILL_SHAPE>( secondary.shape() );
298
299 unpackOptional( secondary.capped(), SecondaryDrill().is_capped, VDCM_CAPPED, VDCM_UNCAPPED );
300 unpackOptional( secondary.filled(), SecondaryDrill().is_filled, VDFM_FILLED, VDFM_UNFILLED );
301 }
302 else
303 {
304 SecondaryDrill().size = { 0, 0 };
308 SecondaryDrill().is_capped = std::nullopt;
309 SecondaryDrill().is_filled = std::nullopt;
310 }
311
312 if( padstack.has_tertiary_drill() )
313 {
314 const DrillProperties& tertiary = padstack.tertiary_drill();
315
316 TertiaryDrill().size = kiapi::common::UnpackVector2( tertiary.diameter() );
317 TertiaryDrill().start = FromProtoEnum<PCB_LAYER_ID>( tertiary.start_layer() );
318 TertiaryDrill().end = FromProtoEnum<PCB_LAYER_ID>( tertiary.end_layer() );
319 TertiaryDrill().shape = FromProtoEnum<PAD_DRILL_SHAPE>( tertiary.shape() );
320
321 unpackOptional( tertiary.capped(), TertiaryDrill().is_capped, VDCM_CAPPED, VDCM_UNCAPPED );
322 unpackOptional( tertiary.filled(), TertiaryDrill().is_filled, VDFM_FILLED, VDFM_UNFILLED );
323 }
324 else
325 {
326 TertiaryDrill().size = { 0, 0 };
330 TertiaryDrill().is_capped = std::nullopt;
331 TertiaryDrill().is_filled = std::nullopt;
332 }
333
334 for( const PadStackLayer& layer : padstack.copper_layers() )
335 {
336 if( !unpackCopperLayer( layer ) )
337 return false;
338 }
339
340 CopperLayer( ALL_LAYERS ).thermal_gap = std::nullopt;
342
343 if( padstack.has_zone_settings() )
344 {
346 FromProtoEnum<ZONE_CONNECTION>( padstack.zone_settings().zone_connection() );
347
348 if( padstack.zone_settings().has_thermal_spokes() )
349 {
350 const ThermalSpokeSettings& thermals = padstack.zone_settings().thermal_spokes();
351
352 if( thermals.has_gap() )
353 CopperLayer( ALL_LAYERS ).thermal_gap = thermals.gap().value_nm();
354
355 if( thermals.has_width() )
356 CopperLayer( ALL_LAYERS ).thermal_spoke_width = thermals.width().value_nm();
357
358 SetThermalSpokeAngle( EDA_ANGLE( thermals.angle().value_degrees(), DEGREES_T ), F_Cu );
359 }
360 }
361 else
362 {
365 }
366
368 FromProtoEnum<UNCONNECTED_LAYER_MODE>( padstack.unconnected_layer_removal() ) );
369
370 unpackOptional( padstack.front_outer_layers().solder_mask_mode(),
371 FrontOuterLayers().has_solder_mask, SMM_MASKED, SMM_UNMASKED );
372
373 unpackOptional( padstack.back_outer_layers().solder_mask_mode(),
374 BackOuterLayers().has_solder_mask, SMM_MASKED, SMM_UNMASKED );
375
376 unpackOptional( padstack.front_outer_layers().covering_mode(), FrontOuterLayers().has_covering,
377 VCM_COVERED, VCM_UNCOVERED );
378
379 unpackOptional( padstack.back_outer_layers().covering_mode(), BackOuterLayers().has_covering,
380 VCM_COVERED, VCM_UNCOVERED );
381
382 unpackOptional( padstack.front_outer_layers().plugging_mode(), FrontOuterLayers().has_plugging,
383 VPM_PLUGGED, VPM_UNPLUGGED );
384
385 unpackOptional( padstack.back_outer_layers().plugging_mode(), BackOuterLayers().has_plugging,
386 VPM_PLUGGED, VPM_UNPLUGGED );
387
388 unpackOptional( padstack.front_outer_layers().solder_paste_mode(),
389 FrontOuterLayers().has_solder_paste, SPM_PASTE, SPM_NO_PASTE );
390
391 unpackOptional( padstack.back_outer_layers().solder_paste_mode(),
392 BackOuterLayers().has_solder_paste, SPM_PASTE, SPM_NO_PASTE );
393
394 if( padstack.front_outer_layers().has_solder_mask_settings()
395 && padstack.front_outer_layers().solder_mask_settings().has_solder_mask_margin() )
396 {
398 padstack.front_outer_layers().solder_mask_settings().solder_mask_margin().value_nm();
399 }
400 else
401 {
402 FrontOuterLayers().solder_mask_margin = std::nullopt;
403 }
404
405 if( padstack.back_outer_layers().has_solder_mask_settings()
406 && padstack.back_outer_layers().solder_mask_settings().has_solder_mask_margin() )
407 {
409 padstack.back_outer_layers().solder_mask_settings().solder_mask_margin().value_nm();
410 }
411 else
412 {
413 BackOuterLayers().solder_mask_margin = std::nullopt;
414 }
415
416 if( padstack.front_outer_layers().has_solder_paste_settings()
417 && padstack.front_outer_layers().solder_paste_settings().has_solder_paste_margin() )
418 {
420 padstack.front_outer_layers().solder_paste_settings().solder_paste_margin().value_nm();
421 }
422 else
423 {
424 FrontOuterLayers().solder_paste_margin = std::nullopt;
425 }
426
427 if( padstack.back_outer_layers().has_solder_paste_settings()
428 && padstack.back_outer_layers().solder_paste_settings().has_solder_paste_margin() )
429 {
431 padstack.back_outer_layers().solder_paste_settings().solder_paste_margin().value_nm();
432 }
433 else
434 {
435 BackOuterLayers().solder_paste_margin = std::nullopt;
436 }
437
438 if( padstack.front_outer_layers().has_solder_paste_settings()
439 && padstack.front_outer_layers().solder_paste_settings().has_solder_paste_margin_ratio() )
440 {
442 padstack.front_outer_layers().solder_paste_settings().solder_paste_margin_ratio().value();
443 }
444 else
445 {
447 }
448
449 if( padstack.back_outer_layers().has_solder_paste_settings()
450 && padstack.back_outer_layers().solder_paste_settings().has_solder_paste_margin_ratio() )
451 {
453 padstack.back_outer_layers().solder_paste_settings().solder_paste_margin_ratio().value();
454 }
455 else
456 {
458 }
459
460
461 return true;
462}
463
464
466{
467 bool hasSecondary = m_secondaryDrill.size.x > 0;
468 bool hasTertiary = m_tertiaryDrill.size.x > 0;
469
470 if( !hasSecondary && !hasTertiary )
472
473 if( hasSecondary && hasTertiary )
475
476 if( hasSecondary )
477 {
478 if( m_secondaryDrill.start == F_Cu )
480
482 }
483
484 if( hasTertiary )
485 {
486 if( m_tertiaryDrill.start == B_Cu )
488
490 }
491
493}
494
495
497{
498 auto initDrill = [this]( DRILL_PROPS& aDrill, PCB_LAYER_ID aStart )
499 {
500 if( aDrill.size.x <= 0 )
501 {
502 aDrill.size = m_drill.size * 1.1; // Backdrill slightly larger than main drill
504 aDrill.start = aStart;
505 aDrill.end = aStart;
506 }
507 else
508 {
509 aDrill.start = aStart;
510 }
511 };
513 {
514 m_tertiaryDrill.size = { 0, 0 };
515 }
516
518 {
519 m_secondaryDrill.size = { 0, 0 };
520 }
521
523 {
524 m_secondaryDrill.start = B_Cu;
525
526 if( m_secondaryDrill.size.x > 0 ) { /* ok */ }
527 else initDrill( m_secondaryDrill, B_Cu );
528 }
529
531 {
532 m_tertiaryDrill.start = F_Cu;
533
534 if( m_tertiaryDrill.size.x > 0 ) { /* ok */ }
535 else initDrill( m_tertiaryDrill, F_Cu );
536 }
537}
538
539
540std::optional<int> PADSTACK::GetBackdrillSize( bool aTop ) const
541{
542 if( m_secondaryDrill.size.x > 0 )
543 {
544 if( aTop && m_secondaryDrill.start == F_Cu ) return m_secondaryDrill.size.x;
545 if( !aTop && m_secondaryDrill.start == B_Cu ) return m_secondaryDrill.size.x;
546 }
547 if( m_tertiaryDrill.size.x > 0 )
548 {
549 if( aTop && m_tertiaryDrill.start == F_Cu ) return m_tertiaryDrill.size.x;
550 if( !aTop && m_tertiaryDrill.start == B_Cu ) return m_tertiaryDrill.size.x;
551 }
552 return std::nullopt;
553}
554
555
556void PADSTACK::SetBackdrillSize( bool aTop, std::optional<int> aSize )
557{
558 DRILL_PROPS* target = nullptr;
559
560 if( aTop )
561 {
562 if( m_secondaryDrill.start == F_Cu ) target = &m_secondaryDrill;
563 else if( m_tertiaryDrill.start == F_Cu ) target = &m_tertiaryDrill;
564 }
565 else
566 {
567 if( m_secondaryDrill.start == B_Cu ) target = &m_secondaryDrill;
568 else if( m_tertiaryDrill.start == B_Cu ) target = &m_tertiaryDrill;
569 }
570
571 if( !target )
572 {
573 if( m_secondaryDrill.size.x <= 0 ) target = &m_secondaryDrill;
574 else if( m_tertiaryDrill.size.x <= 0 ) target = &m_tertiaryDrill;
575 }
576
577 if( !target )
578 {
579 if( aTop ) target = &m_tertiaryDrill;
580 else target = &m_secondaryDrill;
581 }
582
583 if( aSize.has_value() )
584 {
585 target->size = { *aSize, *aSize };
587 target->start = aTop ? F_Cu : B_Cu;
588 if( target->end == UNDEFINED_LAYER ) target->end = UNDEFINED_LAYER;
589 }
590 else
591 {
592 target->size = { 0, 0 };
593 }
594}
595
596
598{
599 if( m_secondaryDrill.size.x > 0 )
600 {
601 if( aTop && m_secondaryDrill.start == F_Cu ) return m_secondaryDrill.end;
602 if( !aTop && m_secondaryDrill.start == B_Cu ) return m_secondaryDrill.end;
603 }
604 if( m_tertiaryDrill.size.x > 0 )
605 {
606 if( aTop && m_tertiaryDrill.start == F_Cu ) return m_tertiaryDrill.end;
607 if( !aTop && m_tertiaryDrill.start == B_Cu ) return m_tertiaryDrill.end;
608 }
609 return UNDEFINED_LAYER;
610}
611
612
614{
615 DRILL_PROPS* target = nullptr;
616
617 if( aTop )
618 {
619 if( m_secondaryDrill.start == F_Cu ) target = &m_secondaryDrill;
620 else if( m_tertiaryDrill.start == F_Cu ) target = &m_tertiaryDrill;
621 }
622 else
623 {
624 if( m_secondaryDrill.start == B_Cu ) target = &m_secondaryDrill;
625 else if( m_tertiaryDrill.start == B_Cu ) target = &m_tertiaryDrill;
626 }
627
628 if( target )
629 {
630 target->end = aLayer;
631 }
632}
633
634void PADSTACK::Serialize( google::protobuf::Any& aContainer ) const
635{
636 using namespace kiapi::board::types;
637 PadStack padstack;
638
639 padstack.set_type( ToProtoEnum<MODE, PadStackType>( m_mode ) );
640 kiapi::board::PackLayerSet( *padstack.mutable_layers(), m_layerSet );
641 padstack.mutable_angle()->set_value_degrees( m_orientation.AsDegrees() );
642
643 kiapi::common::PackVector2( *padstack.mutable_drill()->mutable_diameter(), m_drill.size );
644 padstack.mutable_drill()->set_start_layer( ToProtoEnum<PCB_LAYER_ID, BoardLayer>( m_drill.start ) );
645 padstack.mutable_drill()->set_end_layer( ToProtoEnum<PCB_LAYER_ID, BoardLayer>( m_drill.end ) );
646 padstack.mutable_drill()->set_shape( ToProtoEnum<PAD_DRILL_SHAPE, kiapi::board::types::DrillShape>( m_drill.shape ) );
647
648 if( m_drill.is_capped.has_value() )
649 padstack.mutable_drill()->set_capped( m_drill.is_capped.value() ? VDCM_CAPPED : VDCM_UNCAPPED );
650
651 if( m_drill.is_filled.has_value() )
652 padstack.mutable_drill()->set_filled( m_drill.is_filled.value() ? VDFM_FILLED : VDFM_UNFILLED );
653
654 if( m_secondaryDrill.size.x > 0 )
655 {
656 DrillProperties* secondary = padstack.mutable_secondary_drill();
657 kiapi::common::PackVector2( *secondary->mutable_diameter(), m_secondaryDrill.size );
658 secondary->set_start_layer( ToProtoEnum<PCB_LAYER_ID, BoardLayer>( m_secondaryDrill.start ) );
659 secondary->set_end_layer( ToProtoEnum<PCB_LAYER_ID, BoardLayer>( m_secondaryDrill.end ) );
661
662 if( m_secondaryDrill.is_capped.has_value() )
663 secondary->set_capped( m_secondaryDrill.is_capped.value() ? VDCM_CAPPED : VDCM_UNCAPPED );
664
665 if( m_secondaryDrill.is_filled.has_value() )
666 secondary->set_filled( m_secondaryDrill.is_filled.value() ? VDFM_FILLED : VDFM_UNFILLED );
667 }
668
669 if( m_tertiaryDrill.size.x > 0 )
670 {
671 DrillProperties* tertiary = padstack.mutable_tertiary_drill();
672 kiapi::common::PackVector2( *tertiary->mutable_diameter(), m_tertiaryDrill.size );
673 tertiary->set_start_layer( ToProtoEnum<PCB_LAYER_ID, BoardLayer>( m_tertiaryDrill.start ) );
674 tertiary->set_end_layer( ToProtoEnum<PCB_LAYER_ID, BoardLayer>( m_tertiaryDrill.end ) );
676
677 if( m_tertiaryDrill.is_capped.has_value() )
678 tertiary->set_capped( m_tertiaryDrill.is_capped.value() ? VDCM_CAPPED : VDCM_UNCAPPED );
679
680 if( m_tertiaryDrill.is_filled.has_value() )
681 tertiary->set_filled( m_tertiaryDrill.is_filled.value() ? VDFM_FILLED : VDFM_UNFILLED );
682 }
683
684 auto packPostMachining = []( const PADSTACK::POST_MACHINING_PROPS& aProps,
685 PostMachiningProperties* aProto )
686 {
687 if( aProps.mode.has_value() )
688 {
689 switch( aProps.mode.value() )
690 {
691 case PAD_DRILL_POST_MACHINING_MODE::NOT_POST_MACHINED: aProto->set_mode( VDPM_NOT_POST_MACHINED ); break;
692 case PAD_DRILL_POST_MACHINING_MODE::COUNTERBORE: aProto->set_mode( VDPM_COUNTERBORE ); break;
693 case PAD_DRILL_POST_MACHINING_MODE::COUNTERSINK: aProto->set_mode( VDPM_COUNTERSINK ); break;
694 default: break;
695 }
696 }
697
698 aProto->set_size( aProps.size );
699 aProto->set_depth( aProps.depth );
700 aProto->set_angle( aProps.angle );
701 };
702
703 if( m_frontPostMachining.mode.has_value() || m_frontPostMachining.size > 0 )
704 packPostMachining( m_frontPostMachining, padstack.mutable_front_post_machining() );
705
706 if( m_backPostMachining.mode.has_value() || m_backPostMachining.size > 0 )
707 packPostMachining( m_backPostMachining, padstack.mutable_back_post_machining() );
708
710 [&]( PCB_LAYER_ID aLayer )
711 {
712 PadStackLayer* layer = padstack.add_copper_layers();
713 const COPPER_LAYER_PROPS& props = CopperLayer( aLayer );
714
715 layer->set_layer( ToProtoEnum<PCB_LAYER_ID, BoardLayer>( aLayer ) );
716 kiapi::common::PackVector2( *layer->mutable_size(), props.shape.size );
718 kiapi::common::PackVector2( *layer->mutable_offset(), props.shape.offset );
719 layer->set_custom_anchor_shape( ToProtoEnum<PAD_SHAPE, kiapi::board::types::PadStackShape>( props.shape.anchor_shape ) );
720 layer->set_chamfer_ratio( props.shape.chamfered_rect_ratio );
721 layer->set_corner_rounding_ratio( props.shape.round_rect_radius_ratio );
722
723 if( props.shape.shape == PAD_SHAPE::TRAPEZOID )
724 kiapi::common::PackVector2( *layer->mutable_trapezoid_delta(), props.shape.trapezoid_delta_size );
725
727 layer->mutable_chamfered_corners()->set_top_left( true );
728
730 layer->mutable_chamfered_corners()->set_top_right( true );
731
733 layer->mutable_chamfered_corners()->set_bottom_left( true );
734
736 layer->mutable_chamfered_corners()->set_bottom_right( true );
737
738 for( const std::shared_ptr<PCB_SHAPE>& shape : props.custom_shapes )
739 {
740 google::protobuf::Any a;
741 shape->Serialize( a );
742 a.UnpackTo( layer->add_custom_shapes() );
743 }
744 } );
745
746 if( CopperLayer( ALL_LAYERS ).zone_connection.has_value() )
747 {
748 padstack.mutable_zone_settings()->set_zone_connection(
750 }
751
752 if( CopperLayer( ALL_LAYERS ).thermal_gap.has_value() )
753 {
754 padstack.mutable_zone_settings()->mutable_thermal_spokes()->mutable_gap()->set_value_nm(
755 CopperLayer( ALL_LAYERS ).thermal_gap.value() );
756 }
757
758 if( CopperLayer( ALL_LAYERS ).thermal_spoke_width.has_value() )
759 {
760 padstack.mutable_zone_settings()->mutable_thermal_spokes()->mutable_width()->set_value_nm(
761 CopperLayer( ALL_LAYERS ).thermal_spoke_width.value() );
762 }
763
764 if( CopperLayer( ALL_LAYERS ).thermal_spoke_angle.has_value() )
765 {
766 padstack.mutable_zone_settings()->mutable_thermal_spokes()->mutable_angle()->set_value_degrees(
767 CopperLayer( ALL_LAYERS ).thermal_spoke_angle.value().AsDegrees() );
768 }
769
770 padstack.set_unconnected_layer_removal( ToProtoEnum<UNCONNECTED_LAYER_MODE,
771 kiapi::board::types::UnconnectedLayerRemoval>( m_unconnectedLayerMode ) );
772
773 if( FrontOuterLayers().has_solder_mask.has_value() )
774 {
775 padstack.mutable_front_outer_layers()->set_solder_mask_mode(
776 FrontOuterLayers().has_solder_mask.value() ? SMM_MASKED : SMM_UNMASKED );
777 }
778
779 if( BackOuterLayers().has_solder_mask.has_value() )
780 {
781 padstack.mutable_back_outer_layers()->set_solder_mask_mode(
782 BackOuterLayers().has_solder_mask.value() ? SMM_MASKED : SMM_UNMASKED );
783 }
784
785 if( FrontOuterLayers().has_covering.has_value() )
786 {
787 padstack.mutable_front_outer_layers()->set_covering_mode(
788 FrontOuterLayers().has_covering.value() ? VCM_COVERED : VCM_UNCOVERED );
789 }
790
791 if( BackOuterLayers().has_covering.has_value() )
792 {
793 padstack.mutable_back_outer_layers()->set_covering_mode(
794 BackOuterLayers().has_covering.value() ? VCM_COVERED : VCM_UNCOVERED );
795 }
796
797 if( FrontOuterLayers().has_plugging.has_value() )
798 {
799 padstack.mutable_front_outer_layers()->set_plugging_mode(
800 FrontOuterLayers().has_plugging.value() ? VPM_PLUGGED : VPM_UNPLUGGED );
801 }
802
803 if( BackOuterLayers().has_plugging.has_value() )
804 {
805 padstack.mutable_back_outer_layers()->set_plugging_mode(
806 BackOuterLayers().has_plugging.value() ? VPM_PLUGGED : VPM_UNPLUGGED );
807 }
808
809 if( FrontOuterLayers().has_solder_paste.has_value() )
810 {
811 padstack.mutable_front_outer_layers()->set_solder_paste_mode(
812 FrontOuterLayers().has_solder_paste.value() ? SPM_PASTE : SPM_NO_PASTE );
813 }
814
815 if( BackOuterLayers().has_solder_paste.has_value() )
816 {
817 padstack.mutable_back_outer_layers()->set_solder_paste_mode(
818 BackOuterLayers().has_solder_paste.value() ? SPM_PASTE : SPM_NO_PASTE );
819 }
820
821 if( FrontOuterLayers().solder_mask_margin.has_value() )
822 {
823 padstack.mutable_front_outer_layers()->mutable_solder_mask_settings()->mutable_solder_mask_margin()->set_value_nm(
824 FrontOuterLayers().solder_mask_margin.value() );
825 }
826
827 if( BackOuterLayers().solder_mask_margin.has_value() )
828 {
829 padstack.mutable_back_outer_layers()->mutable_solder_mask_settings()->mutable_solder_mask_margin()->set_value_nm(
830 BackOuterLayers().solder_mask_margin.value() );
831 }
832
833 if( FrontOuterLayers().solder_paste_margin.has_value() )
834 {
835 padstack.mutable_front_outer_layers()->mutable_solder_paste_settings()->mutable_solder_paste_margin()->set_value_nm(
836 FrontOuterLayers().solder_paste_margin.value() );
837 }
838
839 if( BackOuterLayers().solder_paste_margin.has_value() )
840 {
841 padstack.mutable_back_outer_layers()->mutable_solder_paste_settings()->mutable_solder_paste_margin()->set_value_nm(
842 BackOuterLayers().solder_paste_margin.value() );
843 }
844
845 if( FrontOuterLayers().solder_paste_margin_ratio.has_value() )
846 {
847 padstack.mutable_front_outer_layers()->mutable_solder_paste_settings()->mutable_solder_paste_margin_ratio()->set_value(
848 FrontOuterLayers().solder_paste_margin_ratio.value() );
849 }
850
851 if( BackOuterLayers().solder_paste_margin_ratio.has_value() )
852 {
853 padstack.mutable_back_outer_layers()->mutable_solder_paste_settings()->mutable_solder_paste_margin_ratio()->set_value(
854 BackOuterLayers().solder_paste_margin_ratio.value() );
855 }
856
857 aContainer.PackFrom( padstack );
858}
859
860
861void PADSTACK::SetSize( const VECTOR2I& aSize, PCB_LAYER_ID aLayer )
862{
863 VECTOR2I size = aSize;
864
865 if( size.x < 0 )
866 size.x = 0;
867
868 if( size.y < 0 )
869 size.y = 0;
870
871 CopperLayer( aLayer ).shape.size = size;
872}
873
874
876{
877 return CopperLayer( aLayer ).shape.size;
878}
879
880
882{
883 return CopperLayer( aLayer ).shape.shape;
884}
885
886
888{
889 CopperLayer( aLayer ).shape.shape = aShape;
890}
891
892
894{
895 return m_drill.shape;
896}
897
898
900{
901 m_drill.shape = aShape;
902}
903
904
906{
907 return CopperLayer( aLayer ).shape.offset;
908}
909
910
912{
913 return CopperLayer( aLayer ).shape.offset;
914}
915
916
918{
919 return CopperLayer( aLayer ).shape.anchor_shape;
920}
921
922
924{
925 CopperLayer( aLayer ).shape.anchor_shape = aShape;
926}
927
928
933
934
936{
937 return CopperLayer( aLayer ).shape.trapezoid_delta_size;
938}
939
940
942{
944}
945
946
948{
949 CopperLayer( aLayer ).shape.round_rect_radius_ratio = aRatio;
950}
951
952
954{
955 const VECTOR2I& size = Size( aLayer );
956 return KiROUND( std::min( size.x, size.y ) * RoundRectRadiusRatio( aLayer ) );
957}
958
959
960void PADSTACK::SetRoundRectRadius( double aRadius, PCB_LAYER_ID aLayer )
961{
962 const VECTOR2I& size = Size( aLayer );
963 int min_r = std::min( size.x, size.y );
964
965 if( min_r > 0 )
966 SetRoundRectRadiusRatio( aRadius / min_r, aLayer );
967}
968
969
971{
972 return CopperLayer( aLayer ).shape.chamfered_rect_ratio;
973}
974
975
976void PADSTACK::SetChamferRatio( double aRatio, PCB_LAYER_ID aLayer )
977{
978 CopperLayer( aLayer ).shape.chamfered_rect_ratio = aRatio;
979}
980
981
986
987
988const int& PADSTACK::ChamferPositions( PCB_LAYER_ID aLayer ) const
989{
991}
992
993
994void PADSTACK::SetChamferPositions( int aPositions, PCB_LAYER_ID aLayer )
995{
996 CopperLayer( aLayer ).shape.chamfered_rect_positions = aPositions;
997}
998
999
1000std::optional<int>& PADSTACK::Clearance( PCB_LAYER_ID aLayer )
1001{
1002 return CopperLayer( aLayer ).clearance;
1003}
1004
1005
1006const std::optional<int>& PADSTACK::Clearance( PCB_LAYER_ID aLayer ) const
1007{
1008 return CopperLayer( aLayer ).clearance;
1009}
1010
1011
1012std::optional<int>& PADSTACK::SolderMaskMargin( PCB_LAYER_ID aLayer )
1013{
1014 if( IsFrontLayer( aLayer ) )
1016 else if( IsBackLayer( aLayer ) )
1018 else
1019 return FrontOuterLayers().solder_mask_margin; // Should not happen
1020}
1021
1022
1023const std::optional<int>& PADSTACK::SolderMaskMargin( PCB_LAYER_ID aLayer ) const
1024{
1025 if( IsFrontLayer( aLayer ) )
1027 else if( IsBackLayer( aLayer ) )
1029 else
1030 return FrontOuterLayers().solder_mask_margin; // Should not happen
1031}
1032
1033
1034std::optional<int>& PADSTACK::SolderPasteMargin( PCB_LAYER_ID aLayer )
1035{
1036 if( IsFrontLayer( aLayer ) )
1038 else if( IsBackLayer( aLayer ) )
1040 else
1041 return FrontOuterLayers().solder_paste_margin; // Should not happen
1042}
1043
1044
1045const std::optional<int>& PADSTACK::SolderPasteMargin( PCB_LAYER_ID aLayer ) const
1046{
1047 if( IsFrontLayer( aLayer ) )
1049 else if( IsBackLayer( aLayer ) )
1051 else
1052 return FrontOuterLayers().solder_paste_margin; // Should not happen
1053}
1054
1055
1056std::optional<double>& PADSTACK::SolderPasteMarginRatio( PCB_LAYER_ID aLayer )
1057{
1058 if( IsFrontLayer( aLayer ) )
1060 else if( IsBackLayer( aLayer ) )
1062 else
1063 return FrontOuterLayers().solder_paste_margin_ratio; // Should not happen
1064}
1065
1066
1067const std::optional<double>& PADSTACK::SolderPasteMarginRatio( PCB_LAYER_ID aLayer ) const
1068{
1069 if( IsFrontLayer( aLayer ) )
1071 else if( IsBackLayer( aLayer ) )
1073 else
1074 return FrontOuterLayers().solder_paste_margin_ratio; // Should not happen
1075}
1076
1077
1078std::optional<ZONE_CONNECTION>& PADSTACK::ZoneConnection( PCB_LAYER_ID aLayer )
1079{
1080 return CopperLayer( aLayer ).zone_connection;
1081}
1082
1083
1084const std::optional<ZONE_CONNECTION>& PADSTACK::ZoneConnection( PCB_LAYER_ID aLayer ) const
1085{
1086 return CopperLayer( aLayer ).zone_connection;
1087}
1088
1089
1090std::optional<int>& PADSTACK::ThermalSpokeWidth( PCB_LAYER_ID aLayer )
1091{
1092 return CopperLayer( aLayer ).thermal_spoke_width;
1093}
1094
1095
1096const std::optional<int>& PADSTACK::ThermalSpokeWidth( PCB_LAYER_ID aLayer ) const
1097{
1098 return CopperLayer( aLayer ).thermal_spoke_width;
1099}
1100
1101
1102std::optional<int>& PADSTACK::ThermalGap( PCB_LAYER_ID aLayer )
1103{
1104 return CopperLayer( aLayer ).thermal_gap;
1105}
1106
1107
1108const std::optional<int>& PADSTACK::ThermalGap( PCB_LAYER_ID aLayer ) const
1109{
1110 return CopperLayer( aLayer ).thermal_gap;
1111}
1112
1113
1115{
1116 if( Shape( aLayer ) == PAD_SHAPE::OVAL || Shape( aLayer ) == PAD_SHAPE::RECTANGLE
1117 || Shape( aLayer ) == PAD_SHAPE::ROUNDRECT || Shape( aLayer ) == PAD_SHAPE::CHAMFERED_RECT )
1118 {
1119 return ANGLE_90;
1120 }
1121
1122 return ANGLE_45;
1123}
1124
1125
1127{
1128 if( CopperLayer( aLayer ).thermal_spoke_angle.has_value() )
1129 return CopperLayer( aLayer ).thermal_spoke_angle.value();
1130
1131 return DefaultThermalSpokeAngleForShape( aLayer );
1132}
1133
1134
1136{
1137 CopperLayer( aLayer ).thermal_spoke_angle = aAngle;
1138}
1139
1140
1141std::vector<std::shared_ptr<PCB_SHAPE>>& PADSTACK::Primitives( PCB_LAYER_ID aLayer )
1142{
1143 return CopperLayer( aLayer ).custom_shapes;
1144}
1145
1146
1147const std::vector<std::shared_ptr<PCB_SHAPE>>& PADSTACK::Primitives( PCB_LAYER_ID aLayer ) const
1148{
1149 return CopperLayer( aLayer ).custom_shapes;
1150}
1151
1152
1154{
1155 CopperLayer( aLayer ).custom_shapes.emplace_back( aShape );
1156}
1157
1158
1159void PADSTACK::AppendPrimitives( const std::vector<std::shared_ptr<PCB_SHAPE>>& aList,
1160 PCB_LAYER_ID aLayer )
1161{
1162 std::vector<std::shared_ptr<PCB_SHAPE>>& list = CopperLayer( aLayer ).custom_shapes;
1163
1164 for( const std::shared_ptr<PCB_SHAPE>& item : aList )
1165 {
1166 PCB_SHAPE* new_shape = static_cast<PCB_SHAPE*>( item->Clone() );
1167 new_shape->SetParent( m_parent );
1168 list.emplace_back( new_shape );
1169 }
1170}
1171
1172
1173void PADSTACK::ReplacePrimitives( const std::vector<std::shared_ptr<PCB_SHAPE>>& aList,
1174 PCB_LAYER_ID aLayer )
1175{
1176 ClearPrimitives( aLayer );
1177 AppendPrimitives( aList, aLayer );
1178}
1179
1180
1182{
1183 CopperLayer( aLayer ).custom_shapes.clear();
1184}
1185
1186
1188{
1189 if( m_mode == MODE::NORMAL )
1190 return m_copperProps[ALL_LAYERS];
1191
1193 {
1194 if( IsFrontLayer( aLayer ) )
1195 return m_copperProps[F_Cu];
1196 else if( IsBackLayer( aLayer ) )
1197 return m_copperProps[B_Cu];
1198 else
1200 }
1201
1202 return m_copperProps[aLayer];
1203}
1204
1205
1207{
1209 {
1210 if( IsFrontLayer( aLayer ) && m_copperProps.contains( F_Cu ) )
1211 return m_copperProps.at( F_Cu );
1212 else if( IsBackLayer( aLayer ) && m_copperProps.contains( B_Cu ) )
1213 return m_copperProps.at( B_Cu );
1214 else if( m_copperProps.contains( INNER_LAYERS ) )
1215 return m_copperProps.at( INNER_LAYERS );
1216 }
1217 else if( m_mode == MODE::CUSTOM )
1218 {
1219 if( m_copperProps.count( aLayer ) )
1220 return m_copperProps.at( aLayer );
1221 }
1222
1223 return m_copperProps.at( ALL_LAYERS );
1224}
1225
1226
1227void PADSTACK::ForEachUniqueLayer( const std::function<void( PCB_LAYER_ID )>& aMethod ) const
1228{
1229 if( m_mode == MODE::NORMAL )
1230 {
1231 aMethod( ALL_LAYERS );
1232 }
1233 else if( m_mode == MODE::FRONT_INNER_BACK )
1234 {
1235 aMethod( F_Cu );
1236 aMethod( INNER_LAYERS );
1237 aMethod( B_Cu );
1238 }
1239 else
1240 {
1241 for( const auto& [layer, props] : m_copperProps )
1242 aMethod( layer );
1243 }
1244}
1245
1246
1247std::vector<PCB_LAYER_ID> PADSTACK::UniqueLayers() const
1248{
1249 std::vector<PCB_LAYER_ID> layers;
1250
1252 [&]( PCB_LAYER_ID layer )
1253 {
1254 layers.push_back( layer );
1255 } );
1256
1257 return layers;
1258}
1259
1260
1262{
1263 if( m_mode == MODE::NORMAL )
1264 return ALL_LAYERS;
1265
1267 {
1268 if( IsFrontLayer( aLayer ) )
1269 return F_Cu;
1270 else if( IsBackLayer( aLayer ) )
1271 return B_Cu;
1272 else
1273 return INNER_LAYERS;
1274 }
1275
1276 if( m_copperProps.count( aLayer ) )
1277 return aLayer;
1278
1279 return ALL_LAYERS;
1280}
1281
1282
1284{
1285 LSET layers;
1286
1287 if( m_mode == MODE::NORMAL && aOther.m_mode == MODE::NORMAL )
1288 {
1289 layers.set( ALL_LAYERS );
1290 }
1291 else
1292 {
1294 [&]( PCB_LAYER_ID layer )
1295 {
1296 layers.set( layer );
1297 } );
1298 aOther.ForEachUniqueLayer(
1299 [&]( PCB_LAYER_ID layer )
1300 {
1301 layers.set( layer );
1302 } );
1303 }
1304
1305 return layers;
1306}
1307
1308
1309std::optional<bool> PADSTACK::IsTented( PCB_LAYER_ID aSide ) const
1310{
1311 if( IsFrontLayer( aSide ) )
1313 else if( IsBackLayer( aSide ) )
1315 else
1316 return std::nullopt;
1317}
1318
1319
1320std::optional<bool> PADSTACK::IsCovered( PCB_LAYER_ID aSide ) const
1321{
1322 if( IsFrontLayer( aSide ) )
1324 else if( IsBackLayer( aSide ) )
1326 else
1327 return std::nullopt;
1328}
1329
1330
1331std::optional<bool> PADSTACK::IsPlugged( PCB_LAYER_ID aSide ) const
1332{
1333 if( IsFrontLayer( aSide ) )
1335 else if( IsBackLayer( aSide ) )
1337 else
1338 return std::nullopt;
1339}
1340
1341
1342std::optional<bool> PADSTACK::IsCapped() const
1343{
1344 return m_drill.is_capped;
1345}
1346
1347
1348std::optional<bool> PADSTACK::IsFilled() const
1349{
1350 return m_drill.is_filled;
1351}
1352
1353
1354#define TEST( a, b ) { if( a != b ) return a - b; }
1355
1356
1357int PADSTACK::Compare( const PADSTACK* aLeft, const PADSTACK* aRight )
1358{
1359 int diff;
1360
1361 TEST( (int) aLeft->m_mode, (int) aRight->m_mode );
1362
1363 if( aLeft->m_layerSet != aRight->m_layerSet )
1364 return aLeft->m_layerSet.Seq() < aRight->m_layerSet.Seq();
1365
1366 if( ( diff = aLeft->m_customName.Cmp( aRight->m_customName ) ) != 0 )
1367 return diff;
1368
1370
1371 if( ( diff = aLeft->m_frontMaskProps.Compare( aRight->m_frontMaskProps ) ) != 0 )
1372 return diff;
1373
1374 if( ( diff = aLeft->m_backMaskProps.Compare( aRight->m_backMaskProps ) ) != 0 )
1375 return diff;
1376
1377 TEST( (int) aLeft->m_unconnectedLayerMode, (int) aRight->m_unconnectedLayerMode );
1378 TEST( (int) aLeft->m_customShapeInZoneMode, (int) aRight->m_customShapeInZoneMode );
1379
1380 if( ( diff = aLeft->m_drill.Compare( aRight->m_drill ) ) != 0 )
1381 return diff;
1382
1383 if( ( diff = aLeft->m_secondaryDrill.Compare( aRight->m_secondaryDrill ) ) != 0 )
1384 return diff;
1385
1386 if( ( diff = aLeft->m_tertiaryDrill.Compare( aRight->m_tertiaryDrill ) ) != 0 )
1387 return diff;
1388
1389 if( ( diff = aLeft->m_frontPostMachining.Compare( aRight->m_frontPostMachining ) ) != 0 )
1390 return diff;
1391
1392 if( ( diff = aLeft->m_backPostMachining.Compare( aRight->m_backPostMachining ) ) != 0 )
1393 return diff;
1394
1395 aLeft->ForEachUniqueLayer(
1396 [&]( PCB_LAYER_ID aLayer )
1397 {
1398 if( diff != 0 ) // we want to return the first non-matching layer
1399 return;
1400
1401 diff = aLeft->CopperLayer( aLayer ).Compare( aRight->CopperLayer( aLayer ) );
1402 } );
1403
1404 if( diff != 0 )
1405 return diff;
1406
1407 return 0;
1408}
1409
1410
1412{
1413 return m_copperProps.count( aLayer ) > 0;
1414}
1415
1416
1417double PADSTACK::Similarity( const PADSTACK& aOther ) const
1418{
1419 double similarity = 1.0;
1420
1421 if( m_mode != aOther.m_mode )
1422 similarity *= 0.9;
1423
1424 if( m_layerSet != aOther.m_layerSet )
1425 similarity *= 0.9;
1426
1427 if( m_customName != aOther.m_customName )
1428 similarity *= 0.9;
1429
1430 if( m_orientation != aOther.m_orientation )
1431 similarity *= 0.9;
1432
1433 if( m_frontMaskProps != aOther.m_frontMaskProps )
1434 similarity *= 0.9;
1435
1436 if( m_backMaskProps != aOther.m_backMaskProps )
1437 similarity *= 0.9;
1438
1440 similarity *= 0.9;
1441
1443 similarity *= 0.9;
1444
1445 if( m_drill != aOther.m_drill )
1446 similarity *= 0.9;
1447
1448 if( m_secondaryDrill != aOther.m_secondaryDrill )
1449 similarity *= 0.9;
1450
1451 if( m_tertiaryDrill != aOther.m_tertiaryDrill )
1452 similarity *= 0.9;
1453
1455 similarity *= 0.9;
1456
1458 similarity *= 0.9;
1459
1461 [&]( PCB_LAYER_ID aLayer )
1462 {
1463 similarity *= CopperLayer( aLayer ).Similarity( aOther.CopperLayer( aLayer ) );
1464 } );
1465
1466 return similarity;
1467}
1468
1469
1471{
1473 {
1474 std::unordered_map<PCB_LAYER_ID, COPPER_LAYER_PROPS> oldCopperProps = m_copperProps;
1475 m_copperProps.clear();
1476
1477 m_copperProps[aBoard->FlipLayer( F_Cu )] = oldCopperProps[F_Cu];
1478 m_copperProps[INNER_LAYERS] = oldCopperProps[INNER_LAYERS];
1479 m_copperProps[aBoard->FlipLayer( B_Cu )] = oldCopperProps[B_Cu];
1480 }
1481 else if( m_mode == MODE::CUSTOM )
1482 {
1483 std::unordered_map<PCB_LAYER_ID, COPPER_LAYER_PROPS> oldCopperProps = m_copperProps;
1484 m_copperProps.clear();
1485
1486 for( const auto& [layer, props] : oldCopperProps )
1487 m_copperProps[aBoard->FlipLayer( layer )] = props;
1488 }
1489
1490 std::swap( m_frontMaskProps, m_backMaskProps );
1491
1492 m_drill.start = aBoard->FlipLayer( m_drill.start );
1493 m_drill.end = aBoard->FlipLayer( m_drill.end );
1494
1495 m_secondaryDrill.start = aBoard->FlipLayer( m_secondaryDrill.start );
1496 m_secondaryDrill.end = aBoard->FlipLayer( m_secondaryDrill.end );
1497
1498 m_tertiaryDrill.start = aBoard->FlipLayer( m_tertiaryDrill.start );
1499 m_tertiaryDrill.end = aBoard->FlipLayer( m_tertiaryDrill.end );
1500
1502}
1503
1504
1506{
1507 return m_drill.start;
1508}
1509
1510
1512{
1513 return m_drill.end;
1514}
1515
1516
1517wxString PADSTACK::Name() const
1518{
1519 return m_customName;
1520}
1521
1522
1526 size( 0, 0 ),
1527 offset( 0, 0 ),
1529 chamfered_rect_ratio( 0.0 ),
1531 trapezoid_delta_size( 0, 0 )
1532{
1533}
1534
1535
1537{
1538 return shape == aOther.shape &&
1539 anchor_shape == aOther.anchor_shape &&
1540 size == aOther.size &&
1541 offset == aOther.offset &&
1546}
1547
1548
1550{
1551 TEST( (int) shape, (int) aOther.shape );
1552 TEST( (int) anchor_shape, (int) aOther.anchor_shape );
1553 TEST( size.x, aOther.size.x );
1554
1556 TEST( size.y, aOther.size.y );
1557
1558 TEST( offset.x, aOther.offset.x );
1559 TEST( offset.y, aOther.offset.y );
1560
1561 if( abs( round_rect_radius_ratio - aOther.round_rect_radius_ratio ) > 0.0001 )
1562 return round_rect_radius_ratio > aOther.round_rect_radius_ratio ? 1 : -1;
1563
1564 if( abs( chamfered_rect_ratio - aOther.chamfered_rect_ratio ) > 0.0001 )
1565 return chamfered_rect_ratio > aOther.chamfered_rect_ratio ? 1 : -1;
1566
1568
1569 return 0;
1570}
1571
1572
1574{
1575 if( !( shape == aOther.shape ) ) return false;
1576 if( zone_connection != aOther.zone_connection ) return false;
1577 if( thermal_spoke_width != aOther.thermal_spoke_width ) return false;
1578 if( thermal_spoke_angle != aOther.thermal_spoke_angle ) return false;
1579 if( thermal_gap != aOther.thermal_gap ) return false;
1580 if( clearance != aOther.clearance ) return false;
1581
1582 if( custom_shapes.size() != aOther.custom_shapes.size() ) return false;
1583
1584 // Deep compare of shapes?
1585 // For now, just check pointers or size
1586 return true;
1587}
1588
1589
1591{
1592 double similarity = 1.0;
1593
1594 if( shape != aOther.shape )
1595 similarity *= 0.5;
1596
1597 if( zone_connection != aOther.zone_connection )
1598 similarity *= 0.9;
1599
1601 similarity *= 0.9;
1602
1604 similarity *= 0.9;
1605
1606 if( thermal_gap != aOther.thermal_gap )
1607 similarity *= 0.9;
1608
1609 if( clearance != aOther.clearance )
1610 similarity *= 0.9;
1611
1612 if( custom_shapes != aOther.custom_shapes )
1613 similarity *= 0.5;
1614
1615 return similarity;
1616}
1617
1618
1619#define TEST_OPT( a, b, v ) \
1620 { \
1621 if( a.has_value() != b.has_value() ) \
1622 return a.has_value() - b.has_value(); \
1623 if( (int) a.value_or( v ) - (int) b.value_or( v ) != 0 ) \
1624 return (int) a.value_or( v ) - (int) b.value_or( v ); \
1625 }
1626
1627#define TEST_OPT_ANGLE( a, b, v ) \
1628 { \
1629 if( a.has_value() != b.has_value() ) \
1630 return a.has_value() - b.has_value(); \
1631 if( abs( a.value_or( v ).AsDegrees() - b.value_or( v ).AsDegrees() ) > 0.001 ) \
1632 return a.value_or( v ).AsDegrees() > b.value_or( v ).AsDegrees() ? 1 : -1; \
1633 }
1634
1635
1637{
1638 int diff;
1639
1640 if( ( diff = shape.Compare( aOther.shape ) ) != 0 )
1641 return diff;
1642
1646 TEST_OPT( thermal_gap, aOther.thermal_gap, 0 );
1647 TEST_OPT( clearance, aOther.clearance, 0 );
1648
1649 if( ( diff = (int) custom_shapes.size() - (int) aOther.custom_shapes.size() ) != 0 )
1650 return diff;
1651
1652 for( int ii = 0; ii < (int) custom_shapes.size(); ++ii )
1653 {
1654 if( ( diff = custom_shapes[ii]->Compare( aOther.custom_shapes[ii].get() ) ) != 0 )
1655 return diff;
1656 }
1657
1658 return 0;
1659}
1660
1661
1672
1673
1675{
1678
1679 if( solder_paste_margin_ratio.has_value() != aOther.solder_paste_margin_ratio.has_value() )
1680 return solder_paste_margin_ratio.has_value() - aOther.solder_paste_margin_ratio.has_value();
1681 if( abs( solder_paste_margin_ratio.value_or( 0.0 ) - aOther.solder_paste_margin_ratio.value_or( 0.0 ) ) > 0.0001 )
1682 return solder_paste_margin_ratio.value_or( 0.0 ) > aOther.solder_paste_margin_ratio.value_or( 0.0 ) ? 1 : -1;
1683
1684 TEST_OPT( has_solder_mask, aOther.has_solder_mask, false );
1685 TEST_OPT( has_solder_paste, aOther.has_solder_paste, false );
1686 TEST_OPT( has_covering, aOther.has_covering, false );
1687 TEST_OPT( has_plugging, aOther.has_plugging, false );
1688
1689 return 0;
1690}
1691
1692
1694{
1695 return size == aOther.size &&
1696 shape == aOther.shape &&
1697 start == aOther.start &&
1698 end == aOther.end &&
1699 is_filled == aOther.is_filled &&
1700 is_capped == aOther.is_capped;
1701}
1702
1703
1705{
1706 TEST( (int) shape, (int) aOther.shape );
1707
1708 TEST( size.x, aOther.size.x );
1709
1711 TEST( size.y, aOther.size.y );
1712
1713 TEST( (int) start, (int) aOther.start );
1714 TEST( (int) end, (int) aOther.end );
1715
1716 TEST_OPT( is_filled, aOther.is_filled, false );
1717 TEST_OPT( is_capped, aOther.is_capped, false );
1718
1719 return 0;
1720}
1721
1722
1724{
1725 return mode == aOther.mode &&
1726 size == aOther.size &&
1727 depth == aOther.depth &&
1728 angle == aOther.angle;
1729}
1730
1731
1733{
1735 TEST( size, aOther.size );
1736 TEST( depth, aOther.depth );
1737 TEST( angle, aOther.angle );
1738
1739 return 0;
1740}
1741
1742
1744{
1745 m_mode = aMode;
1746}
types::KiCadObjectType ToProtoEnum(KICAD_T aValue)
Definition api_enums.cpp:97
KICAD_T FromProtoEnum(types::KiCadObjectType aValue)
Definition api_enums.cpp:36
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:990
BASE_SET & set(size_t pos)
Definition base_set.h:116
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:84
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:322
PCB_LAYER_ID FlipLayer(PCB_LAYER_ID aLayer) const
Definition board.cpp:912
int AsTenthsOfADegree() const
Definition eda_angle.h:118
virtual void SetParent(EDA_ITEM *aParent)
Definition eda_item.cpp:93
LSET is a set of PCB_LAYER_IDs.
Definition lset.h:37
LSEQ Seq(const LSEQ &aSequence) const
Return an LSEQ from the union of this LSET and a desired sequence.
Definition lset.cpp:313
std::optional< bool > IsFilled() const
bool operator==(const PADSTACK &aOther) const
Definition padstack.cpp:131
std::optional< int > & Clearance(PCB_LAYER_ID aLayer=F_Cu)
void AddPrimitive(PCB_SHAPE *aShape, PCB_LAYER_ID aLayer)
Adds a custom shape primitive to the padstack.
void ReplacePrimitives(const std::vector< std::shared_ptr< PCB_SHAPE > > &aPrimitivesList, PCB_LAYER_ID aLayer)
Clears the existing primitive list (freeing the owned shapes) and adds copies of the given shapes to ...
void SetBackdrillMode(BACKDRILL_MODE aMode)
Definition padstack.cpp:496
bool Deserialize(const google::protobuf::Any &aContainer) override
Deserializes the given protobuf message into this object.
Definition padstack.cpp:236
MASK_LAYER_PROPS & FrontOuterLayers()
Definition padstack.h:369
double Similarity(const PADSTACK &aOther) const
Return a measure of how likely the other object is to represent the same object.
int RoundRectRadius(PCB_LAYER_ID aLayer) const
Definition padstack.cpp:953
void SetDrillShape(PAD_DRILL_SHAPE aShape)
Definition padstack.cpp:899
std::optional< double > & SolderPasteMarginRatio(PCB_LAYER_ID aLayer=F_Cu)
wxString m_customName
! An override for the IPC-7351 padstack name
Definition padstack.h:534
void SetBackdrillSize(bool aTop, std::optional< int > aSize)
Definition padstack.cpp:556
DRILL_PROPS m_drill
!
Definition padstack.h:560
void ForEachUniqueLayer(const std::function< void(PCB_LAYER_ID)> &aMethod) const
Runs the given callable for each active unique copper layer in this padstack, meaning F_Cu for MODE::...
void SetThermalSpokeAngle(EDA_ANGLE aAngle, PCB_LAYER_ID aLayer=F_Cu)
void SetRoundRectRadiusRatio(double aRatio, PCB_LAYER_ID aLayer)
Definition padstack.cpp:947
UNCONNECTED_LAYER_MODE m_unconnectedLayerMode
Definition padstack.h:549
void ClearPrimitives(PCB_LAYER_ID aLayer)
void SetUnconnectedLayerMode(UNCONNECTED_LAYER_MODE aMode)
Definition padstack.h:364
bool HasExplicitDefinitionForLayer(PCB_LAYER_ID aLayer) const
Check if the padstack has an explicit definition for the given layer.
std::optional< bool > IsTented(PCB_LAYER_ID aSide) const
Checks if this padstack is tented (covered in soldermask) on the given side.
void SetMode(MODE aMode)
std::optional< int > & ThermalSpokeWidth(PCB_LAYER_ID aLayer=F_Cu)
std::optional< int > & SolderPasteMargin(PCB_LAYER_ID aLayer=F_Cu)
void FlipLayers(BOARD *aBoard)
Flips the padstack layers in the case that the pad's parent footprint is flipped to the other side of...
POST_MACHINING_PROPS m_backPostMachining
Definition padstack.h:569
std::optional< int > & SolderMaskMargin(PCB_LAYER_ID aLayer=F_Cu)
void SetChamferRatio(double aRatio, PCB_LAYER_ID aLayer)
Definition padstack.cpp:976
PCB_LAYER_ID EffectiveLayerFor(PCB_LAYER_ID aLayer) const
Determines which geometry layer should be used for the given input layer.
PADSTACK & operator=(const PADSTACK &aOther)
Definition padstack.cpp:83
void SetShape(PAD_SHAPE aShape, PCB_LAYER_ID aLayer)
Definition padstack.cpp:887
EDA_ANGLE DefaultThermalSpokeAngleForShape(PCB_LAYER_ID aLayer=F_Cu) const
VECTOR2I & TrapezoidDeltaSize(PCB_LAYER_ID aLayer)
Definition padstack.cpp:929
DRILL_PROPS m_secondaryDrill
! Secondary drill, used to define back-drilling starting from the bottom side
Definition padstack.h:563
VECTOR2I & Offset(PCB_LAYER_ID aLayer)
Definition padstack.cpp:905
MASK_LAYER_PROPS m_backMaskProps
! The overrides applied to back outer technical layers
Definition padstack.h:547
COPPER_LAYER_PROPS & CopperLayer(PCB_LAYER_ID aLayer)
EDA_ANGLE ThermalSpokeAngle(PCB_LAYER_ID aLayer=F_Cu) const
CUSTOM_SHAPE_ZONE_MODE m_customShapeInZoneMode
How to build the custom shape in zone, to create the clearance area: CUSTOM_SHAPE_ZONE_MODE::OUTLINE ...
Definition padstack.h:556
bool unpackCopperLayer(const kiapi::board::types::PadStackLayer &aProto)
Definition padstack.cpp:185
PAD_DRILL_SHAPE DrillShape() const
Definition padstack.cpp:893
void SetChamferPositions(int aPositions, PCB_LAYER_ID aLayer)
Definition padstack.cpp:994
POST_MACHINING_PROPS & FrontPostMachining()
Definition padstack.h:357
DRILL_PROPS m_tertiaryDrill
! Tertiary drill, used to define back-drilling starting from the top side
Definition padstack.h:566
void SetRoundRectRadius(double aRadius, PCB_LAYER_ID aLayer)
Definition padstack.cpp:960
std::optional< bool > IsPlugged(PCB_LAYER_ID aSide) const
LSET RelevantShapeLayers(const PADSTACK &aOther) const
Returns the set of layers that must be considered if checking one padstack against another.
std::optional< int > & ThermalGap(PCB_LAYER_ID aLayer=F_Cu)
DRILL_PROPS & TertiaryDrill()
Definition padstack.h:354
PAD_SHAPE Shape(PCB_LAYER_ID aLayer) const
Definition padstack.cpp:881
DRILL_PROPS & Drill()
Definition padstack.h:348
void SetAnchorShape(PAD_SHAPE aShape, PCB_LAYER_ID aLayer)
Definition padstack.cpp:923
BOARD_ITEM * m_parent
! The BOARD_ITEM this PADSTACK belongs to; will be used as the parent for owned shapes
Definition padstack.h:525
PADSTACK(BOARD_ITEM *aParent)
Definition padstack.cpp:42
std::optional< bool > IsCapped() const
std::vector< PCB_LAYER_ID > UniqueLayers() const
void SetBackdrillEndLayer(bool aTop, PCB_LAYER_ID aLayer)
Definition padstack.cpp:613
LSET m_layerSet
! The board layers that this padstack is active on
Definition padstack.h:531
std::optional< int > GetBackdrillSize(bool aTop) const
Definition padstack.cpp:540
std::unordered_map< PCB_LAYER_ID, COPPER_LAYER_PROPS > m_copperProps
! The properties applied to copper layers if they aren't overridden
Definition padstack.h:541
BACKDRILL_MODE GetBackdrillMode() const
Definition padstack.cpp:465
static int Compare(const PADSTACK *aPadstackRef, const PADSTACK *aPadstackCmp)
Compare two padstacks and return 0 if they are equal.
PCB_LAYER_ID EndLayer() const
std::optional< bool > IsCovered(PCB_LAYER_ID aSide) const
int & ChamferPositions(PCB_LAYER_ID aLayer)
Definition padstack.cpp:982
MASK_LAYER_PROPS m_frontMaskProps
! The overrides applied to front outer technical layers
Definition padstack.h:544
const VECTOR2I & Size(PCB_LAYER_ID aLayer) const
Definition padstack.cpp:875
MODE
! Copper geometry mode: controls how many unique copper layer shapes this padstack has
Definition padstack.h:170
@ NORMAL
Shape is the same on all layers.
Definition padstack.h:171
@ CUSTOM
Shapes can be defined on arbitrary layers.
Definition padstack.h:173
@ FRONT_INNER_BACK
Up to three shapes can be defined (F_Cu, inner copper layers, B_Cu)
Definition padstack.h:172
void AppendPrimitives(const std::vector< std::shared_ptr< PCB_SHAPE > > &aPrimitivesList, PCB_LAYER_ID aLayer)
Appends a copy of each shape in the given list to this padstack's custom shape list.
DRILL_PROPS & SecondaryDrill()
Definition padstack.h:351
double RoundRectRadiusRatio(PCB_LAYER_ID aLayer) const
Definition padstack.cpp:941
PCB_LAYER_ID GetBackdrillEndLayer(bool aTop) const
Definition padstack.cpp:597
PCB_LAYER_ID StartLayer() const
POST_MACHINING_PROPS & BackPostMachining()
Definition padstack.h:360
POST_MACHINING_PROPS m_frontPostMachining
Definition padstack.h:568
PAD_SHAPE AnchorShape(PCB_LAYER_ID aLayer) const
Definition padstack.cpp:917
MASK_LAYER_PROPS & BackOuterLayers()
Definition padstack.h:372
void Serialize(google::protobuf::Any &aContainer) const override
Serializes this object to the given Any message.
Definition padstack.cpp:634
wxString Name() const
! Returns the name of this padstack in IPC-7351 format
void SetSize(const VECTOR2I &aSize, PCB_LAYER_ID aLayer)
Definition padstack.cpp:861
double ChamferRatio(PCB_LAYER_ID aLayer) const
Definition padstack.cpp:970
static constexpr PCB_LAYER_ID ALL_LAYERS
! Temporary layer identifier to identify code that is not padstack-aware
Definition padstack.h:177
static constexpr PCB_LAYER_ID INNER_LAYERS
! The layer identifier to use for "inner layers" on top/inner/bottom padstacks
Definition padstack.h:180
void SetLayerSet(const LSET &aSet)
Definition padstack.h:324
std::optional< ZONE_CONNECTION > & ZoneConnection(PCB_LAYER_ID aLayer=F_Cu)
@ NORMAL
Padstack for a footprint pad.
Definition padstack.h:163
MODE m_mode
! The copper layer variation mode this padstack is in
Definition padstack.h:528
EDA_ANGLE m_orientation
! The rotation of the pad relative to an outer reference frame
Definition padstack.h:537
std::vector< std::shared_ptr< PCB_SHAPE > > & Primitives(PCB_LAYER_ID aLayer)
static constexpr EDA_ANGLE ANGLE_0
Definition eda_angle.h:411
static constexpr EDA_ANGLE ANGLE_90
Definition eda_angle.h:413
@ DEGREES_T
Definition eda_angle.h:31
static constexpr EDA_ANGLE ANGLE_45
Definition eda_angle.h:412
#define TEST(a, b)
bool IsFrontLayer(PCB_LAYER_ID aLayerId)
Layer classification: check if it's a front layer.
Definition layer_ids.h:780
bool IsBackLayer(PCB_LAYER_ID aLayerId)
Layer classification: check if it's a back layer.
Definition layer_ids.h:803
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:60
@ B_Cu
Definition layer_ids.h:65
@ UNDEFINED_LAYER
Definition layer_ids.h:61
@ F_Cu
Definition layer_ids.h:64
This file contains miscellaneous commonly used macros and functions.
void PackLayerSet(google::protobuf::RepeatedField< int > &aOutput, const LSET &aLayerSet)
LSET UnpackLayerSet(const google::protobuf::RepeatedField< int > &aProtoLayerSet)
KICOMMON_API VECTOR2I UnpackVector2(const types::Vector2 &aInput)
Definition api_utils.cpp:86
KICOMMON_API void PackVector2(types::Vector2 &aOutput, const VECTOR2I &aInput)
Definition api_utils.cpp:79
#define TEST_OPT_ANGLE(a, b, v)
#define TEST_OPT(a, b, v)
PAD_DRILL_POST_MACHINING_MODE
Definition padstack.h:76
PAD_DRILL_SHAPE
The set of pad drill shapes, used with PAD::{Set,Get}DrillShape()
Definition padstack.h:69
CUSTOM_SHAPE_ZONE_MODE
Definition padstack.h:137
BACKDRILL_MODE
Definition padstack.h:84
PAD_SHAPE
The set of pad shapes, used with PAD::{Set,Get}Shape()
Definition padstack.h:52
@ CHAMFERED_RECT
Definition padstack.h:60
@ ROUNDRECT
Definition padstack.h:57
@ TRAPEZOID
Definition padstack.h:56
@ RECTANGLE
Definition padstack.h:54
UNCONNECTED_LAYER_MODE
Definition padstack.h:128
#define IMPLEMENT_ENUM_TO_WXANY(type)
Definition property.h:821
The features of a padstack that can vary between copper layers All parameters are optional; leaving t...
Definition padstack.h:227
double Similarity(const COPPER_LAYER_PROPS &aOther) const
std::optional< ZONE_CONNECTION > zone_connection
Definition padstack.h:229
int Compare(const COPPER_LAYER_PROPS &aOther) const
std::optional< int > thermal_spoke_width
Definition padstack.h:230
std::vector< std::shared_ptr< PCB_SHAPE > > custom_shapes
Definition padstack.h:239
bool operator==(const COPPER_LAYER_PROPS &aOther) const
std::optional< EDA_ANGLE > thermal_spoke_angle
Definition padstack.h:231
std::optional< int > clearance
Definition padstack.h:233
std::optional< int > thermal_gap
Definition padstack.h:232
! The properties of a padstack drill. Drill position is always the pad position (origin).
Definition padstack.h:266
PCB_LAYER_ID start
Definition padstack.h:269
PCB_LAYER_ID end
Definition padstack.h:270
bool operator==(const DRILL_PROPS &aOther) const
VECTOR2I size
Drill diameter (x == y) or slot dimensions (x != y)
Definition padstack.h:267
std::optional< bool > is_capped
True if the drill hole should be capped.
Definition padstack.h:273
std::optional< bool > is_filled
True if the drill hole should be filled completely.
Definition padstack.h:272
PAD_DRILL_SHAPE shape
Definition padstack.h:268
int Compare(const DRILL_PROPS &aOther) const
bool operator==(const MASK_LAYER_PROPS &aOther) const
std::optional< int > solder_mask_margin
Definition padstack.h:251
int Compare(const MASK_LAYER_PROPS &aOther) const
std::optional< bool > has_covering
True if the pad on this side should have covering.
Definition padstack.h:257
std::optional< int > solder_paste_margin
Definition padstack.h:252
std::optional< double > solder_paste_margin_ratio
Definition padstack.h:253
std::optional< bool > has_solder_mask
True if this outer layer has mask (is not tented)
Definition padstack.h:255
std::optional< bool > has_solder_paste
True if this outer layer has solder paste.
Definition padstack.h:256
std::optional< bool > has_plugging
True if the drill hole should be plugged on this side.
Definition padstack.h:258
std::optional< PAD_DRILL_POST_MACHINING_MODE > mode
Definition padstack.h:281
bool operator==(const POST_MACHINING_PROPS &aOther) const
int Compare(const POST_MACHINING_PROPS &aOther) const
VECTOR2I trapezoid_delta_size
Delta for PAD_SHAPE::TRAPEZOID; half the delta squeezes one end and half expands the other.
Definition padstack.h:207
int Compare(const SHAPE_PROPS &aOther) const
VECTOR2I offset
Offset of the shape center from the pad center.
Definition padstack.h:197
bool operator==(const SHAPE_PROPS &aOther) const
VECTOR2I size
Size of the shape, or of the anchor pad for custom shape pads.
Definition padstack.h:188
double chamfered_rect_ratio
Size of chamfer: ratio of smallest of X,Y size.
Definition padstack.h:200
double round_rect_radius_ratio
Definition padstack.h:199
PAD_SHAPE shape
Shape of the pad.
Definition padstack.h:186
PAD_SHAPE anchor_shape
Shape of the anchor when shape == PAD_SHAPE::CUSTOM.
Definition padstack.h:187
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695
@ NONE
Pads are not covered.
Definition zones.h:49