KiCad PCB EDA Suite
Loading...
Searching...
No Matches
gendrill_file_writer_base.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) 2017 Jean_Pierre Charras <jp.charras at wanadoo.fr>
5 * Copyright (C) 2015 SoftPLC Corporation, Dick Hollenbeck <[email protected]>
6 * Copyright (C) 1992-2024 KiCad Developers, see AUTHORS.txt for contributors.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, you may find one here:
20 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21 * or you may search the http://www.gnu.org website for the version 2 license,
22 * or you may write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 */
25
26#include <board.h>
27#include <footprint.h>
28#include <pad.h>
29#include <pcb_track.h>
30#include <collectors.h>
31#include <reporter.h>
32#include <richio.h>
33
35
36
37/* Helper function for sorting hole list.
38 * Compare function used for sorting holes type type:
39 * plated then not plated
40 * then by increasing diameter value
41 * then by attribute type (vias, pad, mechanical)
42 * then by X then Y position
43 */
44static bool cmpHoleSorting( const HOLE_INFO& a, const HOLE_INFO& b )
45{
47 return b.m_Hole_NotPlated;
48
51
52 // At this point (same diameter, same plated type), group by attribute
53 // type (via, pad, mechanical, although currently only not plated pads are mechanical)
56
57 // At this point (same diameter, same type), sort by X then Y position.
58 // This is optimal for drilling and make the file reproducible as long as holes
59 // have not changed, even if the data order has changed.
60 if( a.m_Hole_Pos.x != b.m_Hole_Pos.x )
61 return a.m_Hole_Pos.x < b.m_Hole_Pos.x;
62
63 return a.m_Hole_Pos.y < b.m_Hole_Pos.y;
64}
65
66
68 bool aGenerateNPTH_list )
69{
70 HOLE_INFO new_hole;
71
72 m_holeListBuffer.clear();
73 m_toolListBuffer.clear();
74
75 wxASSERT( aLayerPair.first < aLayerPair.second ); // fix the caller
76
77 // build hole list for vias
78 if( ! aGenerateNPTH_list ) // vias are always plated !
79 {
80 for( auto track : m_pcb->Tracks() )
81 {
82 if( track->Type() != PCB_VIA_T )
83 continue;
84
85 PCB_VIA* via = static_cast<PCB_VIA*>( track );
86 int hole_sz = via->GetDrillValue();
87
88 if( hole_sz == 0 ) // Should not occur.
89 continue;
90
91 new_hole.m_ItemParent = via;
92
93 if( aLayerPair == DRILL_LAYER_PAIR( F_Cu, B_Cu ) )
94 new_hole.m_HoleAttribute = HOLE_ATTRIBUTE::HOLE_VIA_THROUGH;
95 else
96 new_hole.m_HoleAttribute = HOLE_ATTRIBUTE::HOLE_VIA_BURIED;
97
98 new_hole.m_Tool_Reference = -1; // Flag value for Not initialized
99 new_hole.m_Hole_Orient = ANGLE_0;
100 new_hole.m_Hole_Diameter = hole_sz;
101 new_hole.m_Hole_NotPlated = false;
102 new_hole.m_Hole_Size.x = new_hole.m_Hole_Size.y = new_hole.m_Hole_Diameter;
103
104 new_hole.m_Hole_Shape = 0; // hole shape: round
105 new_hole.m_Hole_Pos = via->GetStart();
106
107 via->LayerPair( &new_hole.m_Hole_Top_Layer, &new_hole.m_Hole_Bottom_Layer );
108
109 // LayerPair() returns params with m_Hole_Bottom_Layer > m_Hole_Top_Layer
110 // Remember: top layer = 0 and bottom layer = 31 for through hole vias
111 // Any captured via should be from aLayerPair.first to aLayerPair.second exactly.
112 if( new_hole.m_Hole_Top_Layer != aLayerPair.first ||
113 new_hole.m_Hole_Bottom_Layer != aLayerPair.second )
114 continue;
115
116 m_holeListBuffer.push_back( new_hole );
117 }
118 }
119
120 if( aLayerPair == DRILL_LAYER_PAIR( F_Cu, B_Cu ) )
121 {
122 // add holes for thru hole pads
123 for( FOOTPRINT* footprint : m_pcb->Footprints() )
124 {
125 for( PAD* pad : footprint->Pads() )
126 {
127 if( !m_merge_PTH_NPTH )
128 {
129 if( !aGenerateNPTH_list && pad->GetAttribute() == PAD_ATTRIB::NPTH )
130 continue;
131
132 if( aGenerateNPTH_list && pad->GetAttribute() != PAD_ATTRIB::NPTH )
133 continue;
134 }
135
136 if( pad->GetDrillSize().x == 0 )
137 continue;
138
139 new_hole.m_ItemParent = pad;
140 new_hole.m_Hole_NotPlated = (pad->GetAttribute() == PAD_ATTRIB::NPTH);
141 new_hole.m_HoleAttribute = new_hole.m_Hole_NotPlated
142 ? HOLE_ATTRIBUTE::HOLE_MECHANICAL
143 : HOLE_ATTRIBUTE::HOLE_PAD;
144 new_hole.m_Tool_Reference = -1; // Flag is: Not initialized
145 new_hole.m_Hole_Orient = pad->GetOrientation();
146 new_hole.m_Hole_Shape = 0; // hole shape: round
147 new_hole.m_Hole_Diameter = std::min( pad->GetDrillSize().x, pad->GetDrillSize().y );
148 new_hole.m_Hole_Size.x = new_hole.m_Hole_Size.y = new_hole.m_Hole_Diameter;
149
150 // Convert oblong holes that are actually circular into drill hits
151 if( pad->GetDrillShape() != PAD_DRILL_SHAPE::CIRCLE &&
152 pad->GetDrillSizeX() != pad->GetDrillSizeY() )
153 {
154 new_hole.m_Hole_Shape = 1; // oval flag set
155 }
156
157 new_hole.m_Hole_Size = pad->GetDrillSize();
158 new_hole.m_Hole_Pos = pad->GetPosition(); // hole position
159 new_hole.m_Hole_Bottom_Layer = B_Cu;
160 new_hole.m_Hole_Top_Layer = F_Cu; // pad holes are through holes
161 m_holeListBuffer.push_back( new_hole );
162 }
163 }
164 }
165
166 // Sort holes per increasing diameter value (and for each dimater, by position)
167 sort( m_holeListBuffer.begin(), m_holeListBuffer.end(), cmpHoleSorting );
168
169 // build the tool list
170 int last_hole = -1; // Set to not initialized (this is a value not used
171 // for m_holeListBuffer[ii].m_Hole_Diameter)
172 bool last_notplated_opt = false;
173 HOLE_ATTRIBUTE last_attribute = HOLE_ATTRIBUTE::HOLE_UNKNOWN;
174
175 DRILL_TOOL new_tool( 0, false );
176 unsigned jj;
177
178 for( unsigned ii = 0; ii < m_holeListBuffer.size(); ii++ )
179 {
180 if( m_holeListBuffer[ii].m_Hole_Diameter != last_hole
181 || m_holeListBuffer[ii].m_Hole_NotPlated != last_notplated_opt
183 || m_holeListBuffer[ii].m_HoleAttribute != last_attribute
184#endif
185 )
186 {
187 new_tool.m_Diameter = m_holeListBuffer[ii].m_Hole_Diameter;
188 new_tool.m_Hole_NotPlated = m_holeListBuffer[ii].m_Hole_NotPlated;
189 new_tool.m_HoleAttribute = m_holeListBuffer[ii].m_HoleAttribute;
190 m_toolListBuffer.push_back( new_tool );
191 last_hole = new_tool.m_Diameter;
192 last_notplated_opt = new_tool.m_Hole_NotPlated;
193 last_attribute = new_tool.m_HoleAttribute;
194 }
195
196 jj = m_toolListBuffer.size();
197
198 if( jj == 0 )
199 continue; // Should not occurs
200
201 m_holeListBuffer[ii].m_Tool_Reference = jj; // Tool value Initialized (value >= 1)
202
203 m_toolListBuffer.back().m_TotalCount++;
204
205 if( m_holeListBuffer[ii].m_Hole_Shape )
206 m_toolListBuffer.back().m_OvalCount++;
207 }
208}
209
210
211std::vector<DRILL_LAYER_PAIR> GENDRILL_WRITER_BASE::getUniqueLayerPairs() const
212{
213 wxASSERT( m_pcb );
214
216
217 vias.Collect( m_pcb, { PCB_VIA_T } );
218
219 std::set<DRILL_LAYER_PAIR> unique;
220 DRILL_LAYER_PAIR layer_pair;
221
222 for( int i = 0; i < vias.GetCount(); ++i )
223 {
224 PCB_VIA* v = static_cast<PCB_VIA*>( vias[i] );
225
226 v->LayerPair( &layer_pair.first, &layer_pair.second );
227
228 // only make note of blind buried.
229 // thru hole is placed unconditionally as first in fetched list.
230 if( layer_pair != DRILL_LAYER_PAIR( F_Cu, B_Cu ) )
231 unique.insert( layer_pair );
232 }
233
234 std::vector<DRILL_LAYER_PAIR> ret;
235
236 ret.emplace_back( F_Cu, B_Cu ); // always first in returned list
237
238 for( const DRILL_LAYER_PAIR& pair : unique )
239 ret.push_back( pair );
240
241 return ret;
242}
243
244
245const std::string GENDRILL_WRITER_BASE::layerName( PCB_LAYER_ID aLayer ) const
246{
247 // Generic names here.
248 switch( aLayer )
249 {
250 case F_Cu:
251 return "front";
252 case B_Cu:
253 return "back";
254 default:
255 {
256 // aLayer use even values, and the first internal layer (In1) is B_Cu + 2.
257 int ly_id = ( aLayer - B_Cu ) / 2;
258 return StrPrintf( "in%d", ly_id );
259 }
260 }
261}
262
263
265{
266 std::string ret = layerName( aPair.first );
267 ret += '-';
268 ret += layerName( aPair.second );
269
270 return ret;
271}
272
273
275 bool aMerge_PTH_NPTH ) const
276{
277 wxASSERT( m_pcb );
278
279 wxString extend;
280
281 if( aNPTH )
282 extend = wxT( "-NPTH" );
283 else if( aPair == DRILL_LAYER_PAIR( F_Cu, B_Cu ) )
284 {
285 if( !aMerge_PTH_NPTH )
286 extend = wxT( "-PTH" );
287 // if merged, extend with nothing
288 }
289 else
290 {
291 extend += '-';
292 extend += layerPairName( aPair );
293 }
294
295 wxFileName fn = m_pcb->GetFileName();
296
297 fn.SetName( fn.GetName() + extend );
298 fn.SetExt( m_drillFileExtension );
299
300 wxString ret = fn.GetFullName();
301
302 return ret;
303}
304
305bool GENDRILL_WRITER_BASE::CreateMapFilesSet( const wxString& aPlotDirectory,
306 REPORTER * aReporter )
307{
308 wxFileName fn;
309 wxString msg;
310
311 std::vector<DRILL_LAYER_PAIR> hole_sets = getUniqueLayerPairs();
312
313 // append a pair representing the NPTH set of holes, for separate drill files.
314 if( !m_merge_PTH_NPTH )
315 hole_sets.emplace_back( F_Cu, B_Cu );
316
317 for( std::vector<DRILL_LAYER_PAIR>::const_iterator it = hole_sets.begin();
318 it != hole_sets.end(); ++it )
319 {
320 DRILL_LAYER_PAIR pair = *it;
321 // For separate drill files, the last layer pair is the NPTH drill file.
322 bool doing_npth = m_merge_PTH_NPTH ? false : ( it == hole_sets.end() - 1 );
323
324 buildHolesList( pair, doing_npth );
325
326 // The file is created if it has holes, or if it is the non plated drill file
327 // to be sure the NPTH file is up to date in separate files mode.
328 // Also a PTH drill file is always created, to be sure at least one plated hole drill file
329 // is created (do not create any PTH drill file can be seen as not working drill generator).
330 if( getHolesCount() > 0 || doing_npth || pair == DRILL_LAYER_PAIR( F_Cu, B_Cu ) )
331 {
333 fn.SetPath( aPlotDirectory );
334
335 fn.SetExt( wxEmptyString ); // Will be added by GenDrillMap
336 wxString fullfilename = fn.GetFullPath() + wxT( "-drl_map" );
337 fullfilename << wxT(".") << GetDefaultPlotExtension( m_mapFileFmt );
338
339 bool success = genDrillMapFile( fullfilename, m_mapFileFmt );
340
341 if( ! success )
342 {
343 if( aReporter )
344 {
345 msg.Printf( _( "Failed to create file '%s'." ), fullfilename );
346 aReporter->Report( msg, RPT_SEVERITY_ERROR );
347 }
348
349 return false;
350 }
351 else
352 {
353 if( aReporter )
354 {
355 msg.Printf( _( "Created file '%s'." ), fullfilename );
356 aReporter->Report( msg, RPT_SEVERITY_ACTION );
357 }
358 }
359 }
360 }
361
362 return true;
363}
364
365
367 DRILL_LAYER_PAIR aLayerPair, TYPE_FILE aHoleType,
368 bool aCompatNCdrill ) const
369{
370// Build a wxString containing the .FileFunction attribute for drill files.
371// %TF.FileFunction,Plated[NonPlated],layer1num,layer2num,PTH[NPTH][Blind][Buried],Drill[Route][Mixed]*%
372 wxString text;
373
374 if( aCompatNCdrill )
375 text = wxT( "; #@! " );
376 else
377 text = wxT( "%" );
378
379 text << wxT( "TF.FileFunction," );
380
381 if( aHoleType == NPTH_FILE )
382 text << wxT( "NonPlated," );
383 else if( aHoleType == MIXED_FILE ) // only for Excellon format
384 text << wxT( "MixedPlating," );
385 else
386 text << wxT( "Plated," );
387
388 int layer1 = aLayerPair.first;
389 int layer2 = aLayerPair.second;
390 // In Gerber files, layers num are 1 to copper layer count instead of F_Cu to B_Cu
391 // (0 to copper layer count-1)
392 // Note also for a n copper layers board, gerber layers num are 1 ... n
393 //
394 // Copper layers use even values, so the layer id in file is
395 // (Copper layer id) /2 + 1 if layer is not B_Cu
396 if( layer1 == F_Cu )
397 layer1 = 1;
398 else
399 layer1 = ( ( layer1 - B_Cu ) / 2 ) + 1;
400
401 if( layer2 == B_Cu )
402 layer2 = m_pcb->GetCopperLayerCount();
403 else
404 layer2 = ( ( layer2 - B_Cu ) / 2) + 1;
405
406 text << layer1 << wxT( "," ) << layer2;
407
408 // Now add PTH or NPTH or Blind or Buried attribute
409 int toplayer = 1;
410 int bottomlayer = m_pcb->GetCopperLayerCount();
411
412 if( aHoleType == NPTH_FILE )
413 text << wxT( ",NPTH" );
414 else if( aHoleType == MIXED_FILE ) // only for Excellon format
415 {
416 // write nothing
417 }
418 else if( layer1 == toplayer && layer2 == bottomlayer )
419 text << wxT( ",PTH" );
420 else if( layer1 == toplayer || layer2 == bottomlayer )
421 text << wxT( ",Blind" );
422 else
423 text << wxT( ",Buried" );
424
425 // In NC drill file, these previous parameters should be enough:
426 if( aCompatNCdrill )
427 return text;
428
429
430 // Now add Drill or Route or Mixed:
431 // file containing only round holes have Drill attribute
432 // file containing only oblong holes have Routed attribute
433 // file containing both holes have Mixed attribute
434 bool hasOblong = false;
435 bool hasDrill = false;
436
437 for( unsigned ii = 0; ii < m_holeListBuffer.size(); ii++ )
438 {
439 const HOLE_INFO& hole_descr = m_holeListBuffer[ii];
440
441 if( hole_descr.m_Hole_Shape ) // m_Hole_Shape not 0 is an oblong hole)
442 hasOblong = true;
443 else
444 hasDrill = true;
445 }
446
447 if( hasOblong && hasDrill )
448 text << wxT( ",Mixed" );
449 else if( hasDrill )
450 text << wxT( ",Drill" );
451 else if( hasOblong )
452 text << wxT( ",Rout" );
453
454 // else: empty file.
455
456 // End of .FileFunction attribute:
457 text << wxT( "*%" );
458
459 return text;
460}
int GetCopperLayerCount() const
Definition: board.cpp:741
const FOOTPRINTS & Footprints() const
Definition: board.h:331
const TRACKS & Tracks() const
Definition: board.h:329
const wxString & GetFileName() const
Definition: board.h:327
int GetCount() const
Return the number of objects in the list.
Definition: collector.h:81
HOLE_ATTRIBUTE m_HoleAttribute
std::vector< DRILL_LAYER_PAIR > getUniqueLayerPairs() const
Get unique layer pairs by examining the micro and blind_buried vias.
virtual const wxString getDrillFileName(DRILL_LAYER_PAIR aPair, bool aNPTH, bool aMerge_PTH_NPTH) const
void buildHolesList(DRILL_LAYER_PAIR aLayerPair, bool aGenerateNPTH_list)
Create the list of holes and tools for a given board.
const wxString BuildFileFunctionAttributeString(DRILL_LAYER_PAIR aLayerPair, TYPE_FILE aHoleType, bool aCompatNCdrill=false) const
std::vector< HOLE_INFO > m_holeListBuffer
bool genDrillMapFile(const wxString &aFullFileName, PLOT_FORMAT aFormat)
Plot a map of drill marks for holes.
std::vector< DRILL_TOOL > m_toolListBuffer
const std::string layerPairName(DRILL_LAYER_PAIR aPair) const
bool CreateMapFilesSet(const wxString &aPlotDirectory, REPORTER *aReporter=nullptr)
Create the full set of map files for the board, in PS, PDF ... format (use SetMapFileFormat() to sele...
const std::string layerName(PCB_LAYER_ID aLayer) const
Handle hole which must be drilled (diameter, position and layers).
PCB_LAYER_ID m_Hole_Bottom_Layer
PCB_LAYER_ID m_Hole_Top_Layer
HOLE_ATTRIBUTE m_HoleAttribute
BOARD_ITEM * m_ItemParent
Definition: pad.h:54
Collect all BOARD_ITEM objects of a given set of KICAD_T type(s).
Definition: collectors.h:511
void Collect(BOARD_ITEM *aBoard, const std::vector< KICAD_T > &aTypes)
Collect BOARD_ITEM objects using this class's Inspector method, which does the collection.
Definition: collectors.cpp:518
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)
Definition: pcb_track.cpp:1161
A pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:72
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)=0
Report a string with a given severity.
wxString GetDefaultPlotExtension(PLOT_FORMAT aFormat)
Returns the default plot extension for a format.
#define _(s)
static constexpr EDA_ANGLE ANGLE_0
Definition: eda_angle.h:401
static bool cmpHoleSorting(const HOLE_INFO &a, const HOLE_INFO &b)
helper classes to handle hole info for drill files generators.
#define USE_ATTRIB_FOR_HOLES
std::pair< PCB_LAYER_ID, PCB_LAYER_ID > DRILL_LAYER_PAIR
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:60
@ B_Cu
Definition: layer_ids.h:65
@ F_Cu
Definition: layer_ids.h:64
@ RPT_SEVERITY_ERROR
@ RPT_SEVERITY_ACTION
int StrPrintf(std::string *result, const char *format,...)
This is like sprintf() but the output is appended to a std::string instead of to a character array.
Definition: richio.cpp:68
@ PCB_VIA_T
class PCB_VIA, a via (like a track segment on a copper layer)
Definition: typeinfo.h:97