KiCad PCB EDA Suite
Loading...
Searching...
No Matches
drill_enumerator.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 The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
21
22#include <board.h>
24#include <footprint.h>
25#include <pad.h>
26#include <pcb_track.h>
27
28#include <set>
29#include <utility>
30
31
32namespace
33{
34
35DRILL_POST_MACHINING fromPadstack( const PADSTACK::POST_MACHINING_PROPS& aProps )
36{
38
40 pm.m_Size = aProps.size;
41 pm.m_Depth = aProps.depth;
42 pm.m_Angle = aProps.angle;
43
44 return pm;
45}
46
47
48bool isBackdrillSet( const PADSTACK::DRILL_PROPS& aDrill )
49{
50 return aDrill.start != UNDEFINED_LAYER && aDrill.end != UNDEFINED_LAYER
51 && ( aDrill.size.x > 0 || aDrill.size.y > 0 );
52}
53
54
55std::optional<DRILL_OPERATION> backdrillOperation( const PADSTACK::DRILL_PROPS& aDrill, DRILL_OP_KIND aKind,
56 const DRILL_SPAN& aSpan, BOARD_ITEM* aItem )
57{
58 if( !isBackdrillSet( aDrill ) || aDrill.start != aSpan.DrillStartLayer()
59 || aDrill.end != aSpan.DrillEndLayer() )
60 {
61 return std::nullopt;
62 }
63
65 op.m_Kind = aKind;
66 op.m_SourceItem = aItem;
67 op.m_SourceId = aItem->m_Uuid;
68 op.m_NotPlated = true;
69 op.m_TopLayer = aSpan.TopLayer();
70 op.m_BottomLayer = aSpan.BottomLayer();
71 op.m_DrillStart = aDrill.start;
72 op.m_DrillEnd = aDrill.end;
73 op.m_SizeXY = aDrill.size;
74 op.m_Diameter = aDrill.size.x;
75
76 if( aDrill.size.y > 0 )
77 op.m_Diameter = op.m_Diameter > 0 ? std::min( op.m_Diameter, aDrill.size.y ) : aDrill.size.y;
78
79 op.m_IsSlot = aDrill.shape != PAD_DRILL_SHAPE::CIRCLE && aDrill.size.x != aDrill.size.y;
80 return op;
81}
82
83
84void enumerateVia( PCB_VIA* aVia, const BOARD& aBoard, const DRILL_QUERY& aQuery, std::vector<DRILL_OPERATION>& aOut )
85{
86 const DRILL_SPAN& span = aQuery.m_Span;
87
88 auto stubLength =
89 [&]( PCB_LAYER_ID aStart, PCB_LAYER_ID aEnd ) -> std::optional<int>
90 {
91 if( aStart == UNDEFINED_LAYER || aEnd == UNDEFINED_LAYER )
92 return std::optional<int>();
93
94 const BOARD_STACKUP& stackup = aBoard.GetDesignSettings().GetStackupDescriptor();
95
96 return std::optional<int>( stackup.GetDrillDepth( aStart, aEnd ) );
97 };
98
99 if( span.m_IsBackdrill )
100 {
101 auto tryBackdrill =
102 [&]( const PADSTACK::DRILL_PROPS& aDrill, DRILL_OP_KIND aKind )
103 {
104 std::optional<DRILL_OPERATION> op = backdrillOperation( aDrill, aKind, span, aVia );
105
106 if( !op )
107 return;
108
109 op->m_Attribute = HOLE_ATTRIBUTE::HOLE_VIA_BACKDRILL;
110 op->m_Orientation = ANGLE_0;
111 op->m_Position = aVia->GetStart();
112 op->m_StubLength = stubLength( aDrill.start, aDrill.end );
113 // Backdrills remove the via barrel, so via surface protection is not inherited
114 op->m_Filled = aDrill.is_filled.value_or( false );
115 op->m_Capped = aDrill.is_capped.value_or( false );
116 aOut.push_back( std::move( *op ) );
117 };
118
119 tryBackdrill( aVia->Padstack().SecondaryDrill(), DRILL_OP_KIND::SECONDARY_DRILL );
120 tryBackdrill( aVia->Padstack().TertiaryDrill(), DRILL_OP_KIND::TERTIARY_DRILL );
121 return;
122 }
123
124 int holeSize = aVia->GetDrillValue();
125
126 if( holeSize == 0 )
127 return;
128
129 PCB_LAYER_ID topLayer;
130 PCB_LAYER_ID bottomLayer;
131 aVia->LayerPair( &topLayer, &bottomLayer );
132
133 if( DRILL_LAYER_PAIR( topLayer, bottomLayer ) != span.Pair()
134 && DRILL_LAYER_PAIR( bottomLayer, topLayer ) != span.Pair() )
135 {
136 return;
137 }
138
140 op.m_SourceItem = aVia;
141 op.m_SourceId = aVia->m_Uuid;
142
143 if( span.Pair() == DRILL_LAYER_PAIR( F_Cu, B_Cu ) )
145 else
147
149 op.m_Diameter = holeSize;
150 op.m_NotPlated = false;
151 op.m_SizeXY = VECTOR2I( holeSize, holeSize );
152 op.m_Position = aVia->GetStart();
153 op.m_TopLayer = topLayer;
154 op.m_BottomLayer = bottomLayer;
157
158 if( op.m_TopLayer == F_Cu )
159 {
163 }
164
165 if( op.m_BottomLayer == B_Cu )
166 {
170 }
171
172 op.m_FrontPostMachining = fromPadstack( aVia->Padstack().FrontPostMachining() );
173 op.m_BackPostMachining = fromPadstack( aVia->Padstack().BackPostMachining() );
174 op.m_DrillStart = aVia->Padstack().Drill().start;
175 op.m_DrillEnd = bottomLayer;
176
177 aOut.push_back( op );
178}
179
180
181void enumeratePad( PAD* aPad, const DRILL_QUERY& aQuery, std::vector<DRILL_OPERATION>& aOut )
182{
183 if( aQuery.m_Span.m_IsBackdrill )
184 {
185 if( !aQuery.m_PadMachining )
186 return;
187
188 auto addBackdrill = [&]( const PADSTACK::DRILL_PROPS& aDrill, DRILL_OP_KIND aKind )
189 {
190 std::optional<DRILL_OPERATION> op = backdrillOperation( aDrill, aKind, aQuery.m_Span, aPad );
191
192 if( !op )
193 return;
194
195 op->m_Attribute = HOLE_ATTRIBUTE::HOLE_PAD_BACKDRILL;
196 op->m_Position = aPad->GetPosition();
197 op->m_Orientation = aPad->GetOrientation();
198 aOut.push_back( std::move( *op ) );
199 };
200
201 addBackdrill( aPad->Padstack().SecondaryDrill(), DRILL_OP_KIND::SECONDARY_DRILL );
202 addBackdrill( aPad->Padstack().TertiaryDrill(), DRILL_OP_KIND::TERTIARY_DRILL );
203 return;
204 }
205
206 if( !aQuery.m_MergePTHNPTH )
207 {
208 if( !aQuery.m_NonPlatedOnly && aPad->GetAttribute() == PAD_ATTRIB::NPTH )
209 return;
210
211 if( aQuery.m_NonPlatedOnly && aPad->GetAttribute() != PAD_ATTRIB::NPTH )
212 return;
213 }
214
215 if( aPad->GetDrillSize().x == 0 )
216 return;
217
219 op.m_SourceItem = aPad;
220 op.m_SourceId = aPad->m_Uuid;
221 op.m_NotPlated = ( aPad->GetAttribute() == PAD_ATTRIB::NPTH );
222
223 if( op.m_NotPlated )
224 {
226 }
227 else if( aPad->GetProperty() == PAD_PROP::CASTELLATED )
228 {
230 }
231 else if( aPad->GetProperty() == PAD_PROP::PRESSFIT )
232 {
234 }
235 else
236 {
238 }
239
240 op.m_Orientation = aPad->GetOrientation();
241 op.m_Diameter = std::min( aPad->GetDrillSize().x, aPad->GetDrillSize().y );
242
243 op.m_IsSlot = IsDrillSlot( *aPad );
244
245 op.m_SizeXY = aPad->GetDrillSize();
246 op.m_Position = aPad->GetPosition();
247 op.m_BottomLayer = B_Cu;
248 op.m_TopLayer = F_Cu;
249
250 if( aQuery.m_PadMachining )
251 {
252 op.m_FrontPostMachining = fromPadstack( aPad->Padstack().FrontPostMachining() );
253 op.m_BackPostMachining = fromPadstack( aPad->Padstack().BackPostMachining() );
254 }
255
256 aOut.push_back( op );
257}
258
259} // namespace
260
261
262bool IsDrillSlot( const PAD& aPad )
263{
264 return aPad.HasHole()
266 && aPad.GetDrillSize().x != aPad.GetDrillSize().y;
267}
268
269
270std::vector<DRILL_OPERATION> EnumerateDrillOperations( const BOARD& aBoard, const DRILL_QUERY& aQuery )
271{
272 std::vector<DRILL_OPERATION> operations;
273
274 wxASSERT( IsCopperLayerLowerThan( aQuery.m_Span.BottomLayer(), aQuery.m_Span.TopLayer() )
275 || aQuery.m_Span.BottomLayer() == aQuery.m_Span.TopLayer() );
276
277 if( !aQuery.m_NonPlatedOnly )
278 {
279 for( PCB_TRACK* track : aBoard.Tracks() )
280 {
281 if( track->Type() != PCB_VIA_T )
282 continue;
283
284 enumerateVia( static_cast<PCB_VIA*>( track ), aBoard, aQuery, operations );
285 }
286 }
287
288 if( ( aQuery.m_Span.m_IsBackdrill && aQuery.m_PadMachining )
289 || ( !aQuery.m_Span.m_IsBackdrill && aQuery.m_Span.Pair() == DRILL_LAYER_PAIR( F_Cu, B_Cu ) ) )
290 {
291 for( FOOTPRINT* footprint : aBoard.Footprints() )
292 {
293 for( PAD* pad : footprint->Pads() )
294 enumeratePad( pad, aQuery, operations );
295 }
296 }
297
298 return operations;
299}
300
301
302std::vector<DRILL_SPAN> EnumerateDrillSpans( const BOARD& aBoard, bool aPadBackdrills )
303{
304 std::set<DRILL_SPAN> unique;
305
306 auto addBackdrill =
307 [&]( const PADSTACK::DRILL_PROPS& aDrill )
308 {
309 if( isBackdrillSet( aDrill ) )
310 unique.emplace( aDrill.start, aDrill.end, true, false );
311 };
312
313 for( PCB_TRACK* track : aBoard.Tracks() )
314 {
315 if( track->Type() != PCB_VIA_T )
316 continue;
317
318 PCB_VIA* via = static_cast<PCB_VIA*>( track );
320 PCB_LAYER_ID bottom;
321 via->LayerPair( &top, &bottom );
322
323 if( DRILL_LAYER_PAIR( top, bottom ) != DRILL_LAYER_PAIR( F_Cu, B_Cu ) )
324 unique.emplace( top, bottom, false, false );
325
326 addBackdrill( via->Padstack().SecondaryDrill() );
327 addBackdrill( via->Padstack().TertiaryDrill() );
328 }
329
330 if( aPadBackdrills )
331 {
332 for( FOOTPRINT* footprint : aBoard.Footprints() )
333 {
334 for( PAD* pad : footprint->Pads() )
335 {
336 addBackdrill( pad->Padstack().SecondaryDrill() );
337 addBackdrill( pad->Padstack().TertiaryDrill() );
338 }
339 }
340 }
341
342 std::vector<DRILL_SPAN> spans;
343 spans.emplace_back( F_Cu, B_Cu, false, false );
344
345 for( const DRILL_SPAN& span : unique )
346 {
347 if( span.m_IsBackdrill || span.Pair() != DRILL_LAYER_PAIR( F_Cu, B_Cu ) )
348 spans.push_back( span );
349 }
350
351 return spans;
352}
BOARD_STACKUP & GetStackupDescriptor()
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:99
Manage layers needed to make a physical board.
int GetDrillDepth(PCB_LAYER_ID aFirstLayer, PCB_LAYER_ID aSecondLayer) const
Calculate the drill depth needed to cut from aFirstLayer /through/ aSecondLayer.
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:411
const FOOTPRINTS & Footprints() const
Definition board.h:465
const TRACKS & Tracks() const
Definition board.h:463
BOARD_DESIGN_SETTINGS & GetDesignSettings()
Definition board.cpp:1318
const KIID m_Uuid
Definition eda_item.h:599
POST_MACHINING_PROPS & FrontPostMachining()
Definition padstack.h:372
DRILL_PROPS & TertiaryDrill()
Definition padstack.h:369
DRILL_PROPS & Drill()
Definition padstack.h:363
DRILL_PROPS & SecondaryDrill()
Definition padstack.h:366
POST_MACHINING_PROPS & BackPostMachining()
Definition padstack.h:375
Definition pad.h:61
PAD_PROP GetProperty() const
Definition pad.h:585
PAD_ATTRIB GetAttribute() const
Definition pad.h:582
VECTOR2I GetPosition() const override
Definition pad.cpp:255
VECTOR2I GetDrillSize() const
Definition pad.h:320
const PADSTACK & Padstack() const
Definition pad.h:331
EDA_ANGLE GetOrientation() const
Return the rotation angle of the pad.
Definition pad.cpp:1672
PAD_DRILL_SHAPE GetDrillShape() const
Definition pad.h:438
bool HasHole() const override
Definition pad.h:113
const VECTOR2I & GetStart() const
Definition pcb_track.h:100
FILLING_MODE ResolveFillingMode() const
CAPPING_MODE ResolveCappingMode() const
PLUGGING_MODE ResolveFrontPluggingMode() const
const PADSTACK & Padstack() const
Definition pcb_track.h:435
PLUGGING_MODE ResolveBackPluggingMode() const
COVERING_MODE ResolveBackCoveringMode() const
TENTING_MODE ResolveBackTentingMode() const
int GetDrillValue() const
Calculate the drill value for vias (m_drill if > 0, or default drill value for the board).
COVERING_MODE ResolveFrontCoveringMode() const
void LayerPair(PCB_LAYER_ID *top_layer, PCB_LAYER_ID *bottom_layer) const
Return the 2 layers used by the via (the via actually uses all layers between these 2 layers)
TENTING_MODE ResolveFrontTentingMode() const
bool IsDrillSlot(const PAD &aPad)
Whether a pad's hole is a slot rather than a round drill.
std::vector< DRILL_SPAN > EnumerateDrillSpans(const BOARD &aBoard, bool aPadBackdrills)
Every drill span present on the board, through-holes first.
std::vector< DRILL_OPERATION > EnumerateDrillOperations(const BOARD &aBoard, const DRILL_QUERY &aQuery)
The one place that decides what the board's holes are.
DRILL_OP_KIND
Which of the padstack's drill props produced the operation.
std::pair< PCB_LAYER_ID, PCB_LAYER_ID > DRILL_LAYER_PAIR
Definition drill_span.h:49
static constexpr EDA_ANGLE ANGLE_0
Definition eda_angle.h:472
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
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:56
@ B_Cu
Definition layer_ids.h:61
@ UNDEFINED_LAYER
Definition layer_ids.h:57
@ F_Cu
Definition layer_ids.h:60
@ NPTH
like PAD_PTH, but not plated mechanical use only, no connection allowed
Definition padstack.h:104
@ PRESSFIT
a PTH with a hole diameter with tight tolerances for press fit pin
Definition padstack.h:127
@ CASTELLATED
a pad with a castellated through hole
Definition padstack.h:125
One machining action, which is also one NC hit and one tool assignment.
DRILL_OP_KIND m_Kind
DRILL_POST_MACHINING m_BackPostMachining
PCB_LAYER_ID m_DrillEnd
PCB_LAYER_ID m_TopLayer
PCB_LAYER_ID m_BottomLayer
VECTOR2I m_SizeXY
Source axes, never normalized to (width, length).
BOARD_ITEM * m_SourceItem
non-owning, valid only for this enumeration
EDA_ANGLE m_Orientation
DRILL_POST_MACHINING m_FrontPostMachining
HOLE_ATTRIBUTE m_Attribute
PCB_LAYER_ID m_DrillStart
int m_Size
int m_Depth
int m_Angle
PAD_DRILL_POST_MACHINING_MODE m_Mode
Selects which operations EnumerateDrillOperations() returns.
bool m_NonPlatedOnly
Emit only non-plated holes.
bool m_MergePTHNPTH
Emit plated and non-plated together, as the merged drill file does.
DRILL_SPAN m_Span
bool m_PadMachining
Include pad backdrills and post-machining, kept off for NC drill output.
DRILL_LAYER_PAIR Pair() const
Definition drill_span.h:93
PCB_LAYER_ID DrillEndLayer() const
Definition drill_span.h:88
PCB_LAYER_ID TopLayer() const
Definition drill_span.h:71
PCB_LAYER_ID DrillStartLayer() const
Definition drill_span.h:83
PCB_LAYER_ID BottomLayer() const
Definition drill_span.h:78
bool m_IsBackdrill
Definition drill_span.h:130
The properties of a padstack drill.
Definition padstack.h:277
PCB_LAYER_ID start
Definition padstack.h:280
PCB_LAYER_ID end
Definition padstack.h:281
VECTOR2I size
Drill diameter (x == y) or slot dimensions (x != y)
Definition padstack.h:278
std::optional< bool > is_capped
True if the drill hole should be capped.
Definition padstack.h:284
std::optional< bool > is_filled
True if the drill hole should be filled completely.
Definition padstack.h:283
PAD_DRILL_SHAPE shape
Definition padstack.h:279
std::optional< PAD_DRILL_POST_MACHINING_MODE > mode
Definition padstack.h:292
KIBIS top(path, &reporter)
@ PCB_VIA_T
class PCB_VIA, a via (like a track segment on a copper layer)
Definition typeinfo.h:89
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708