KiCad PCB EDA Suite
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-2022 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
34
35
36/* Helper function for sorting hole list.
37 * Compare function used for sorting holes type type:
38 * plated then not plated
39 * then by increasing diameter value
40 * then by attribute type (vias, pad, mechanical)
41 * then by X then Y position
42 */
43static bool cmpHoleSorting( const HOLE_INFO& a, const HOLE_INFO& b )
44{
46 return b.m_Hole_NotPlated;
47
50
51 // At this point (same diameter, same plated type), group by attribute
52 // type (via, pad, mechanical, although currently only not plated pads are mechanical)
55
56 // At this point (same diameter, same type), sort by X then Y position.
57 // This is optimal for drilling and make the file reproducible as long as holes
58 // have not changed, even if the data order has changed.
59 if( a.m_Hole_Pos.x != b.m_Hole_Pos.x )
60 return a.m_Hole_Pos.x < b.m_Hole_Pos.x;
61
62 return a.m_Hole_Pos.y < b.m_Hole_Pos.y;
63}
64
65
67 bool aGenerateNPTH_list )
68{
69 HOLE_INFO new_hole;
70
71 m_holeListBuffer.clear();
72 m_toolListBuffer.clear();
73
74 wxASSERT( aLayerPair.first < aLayerPair.second ); // fix the caller
75
76 // build hole list for vias
77 if( ! aGenerateNPTH_list ) // vias are always plated !
78 {
79 for( auto track : m_pcb->Tracks() )
80 {
81 if( track->Type() != PCB_VIA_T )
82 continue;
83
84 PCB_VIA* via = static_cast<PCB_VIA*>( track );
85 int hole_sz = via->GetDrillValue();
86
87 if( hole_sz == 0 ) // Should not occur.
88 continue;
89
90 new_hole.m_ItemParent = via;
91
92 if( aLayerPair == DRILL_LAYER_PAIR( F_Cu, B_Cu ) )
94 else
96
97 new_hole.m_Tool_Reference = -1; // Flag value for Not initialized
98 new_hole.m_Hole_Orient = ANGLE_0;
99 new_hole.m_Hole_Diameter = hole_sz;
100 new_hole.m_Hole_NotPlated = false;
101 new_hole.m_Hole_Size.x = new_hole.m_Hole_Size.y = new_hole.m_Hole_Diameter;
102
103 new_hole.m_Hole_Shape = 0; // hole shape: round
104 new_hole.m_Hole_Pos = via->GetStart();
105
106 via->LayerPair( &new_hole.m_Hole_Top_Layer, &new_hole.m_Hole_Bottom_Layer );
107
108 // LayerPair() returns params with m_Hole_Bottom_Layer > m_Hole_Top_Layer
109 // Remember: top layer = 0 and bottom layer = 31 for through hole vias
110 // Any captured via should be from aLayerPair.first to aLayerPair.second exactly.
111 if( new_hole.m_Hole_Top_Layer != aLayerPair.first ||
112 new_hole.m_Hole_Bottom_Layer != aLayerPair.second )
113 continue;
114
115 m_holeListBuffer.push_back( new_hole );
116 }
117 }
118
119 if( aLayerPair == DRILL_LAYER_PAIR( F_Cu, B_Cu ) )
120 {
121 // add holes for thru hole pads
122 for( FOOTPRINT* footprint : m_pcb->Footprints() )
123 {
124 for( PAD* pad : footprint->Pads() )
125 {
126 if( !m_merge_PTH_NPTH )
127 {
128 if( !aGenerateNPTH_list && pad->GetAttribute() == PAD_ATTRIB::NPTH )
129 continue;
130
131 if( aGenerateNPTH_list && pad->GetAttribute() != PAD_ATTRIB::NPTH )
132 continue;
133 }
134
135 if( pad->GetDrillSize().x == 0 )
136 continue;
137
138 new_hole.m_ItemParent = pad;
139 new_hole.m_Hole_NotPlated = (pad->GetAttribute() == PAD_ATTRIB::NPTH);
140 new_hole.m_HoleAttribute = new_hole.m_Hole_NotPlated
143 new_hole.m_Tool_Reference = -1; // Flag is: Not initialized
144 new_hole.m_Hole_Orient = pad->GetOrientation();
145 new_hole.m_Hole_Shape = 0; // hole shape: round
146 new_hole.m_Hole_Diameter = std::min( pad->GetDrillSize().x, pad->GetDrillSize().y );
147 new_hole.m_Hole_Size.x = new_hole.m_Hole_Size.y = new_hole.m_Hole_Diameter;
148
149 if( pad->GetDrillShape() != PAD_DRILL_SHAPE_CIRCLE )
150 new_hole.m_Hole_Shape = 1; // oval flag set
151
152 new_hole.m_Hole_Size = pad->GetDrillSize();
153 new_hole.m_Hole_Pos = pad->GetPosition(); // hole position
154 new_hole.m_Hole_Bottom_Layer = B_Cu;
155 new_hole.m_Hole_Top_Layer = F_Cu; // pad holes are through holes
156 m_holeListBuffer.push_back( new_hole );
157 }
158 }
159 }
160
161 // Sort holes per increasing diameter value (and for each dimater, by position)
162 sort( m_holeListBuffer.begin(), m_holeListBuffer.end(), cmpHoleSorting );
163
164 // build the tool list
165 int last_hole = -1; // Set to not initialized (this is a value not used
166 // for m_holeListBuffer[ii].m_Hole_Diameter)
167 bool last_notplated_opt = false;
169
170 DRILL_TOOL new_tool( 0, false );
171 unsigned jj;
172
173 for( unsigned ii = 0; ii < m_holeListBuffer.size(); ii++ )
174 {
175 if( m_holeListBuffer[ii].m_Hole_Diameter != last_hole
176 || m_holeListBuffer[ii].m_Hole_NotPlated != last_notplated_opt
178 || m_holeListBuffer[ii].m_HoleAttribute != last_attribute
179#endif
180 )
181 {
182 new_tool.m_Diameter = m_holeListBuffer[ii].m_Hole_Diameter;
183 new_tool.m_Hole_NotPlated = m_holeListBuffer[ii].m_Hole_NotPlated;
184 new_tool.m_HoleAttribute = m_holeListBuffer[ii].m_HoleAttribute;
185 m_toolListBuffer.push_back( new_tool );
186 last_hole = new_tool.m_Diameter;
187 last_notplated_opt = new_tool.m_Hole_NotPlated;
188 last_attribute = new_tool.m_HoleAttribute;
189 }
190
191 jj = m_toolListBuffer.size();
192
193 if( jj == 0 )
194 continue; // Should not occurs
195
196 m_holeListBuffer[ii].m_Tool_Reference = jj; // Tool value Initialized (value >= 1)
197
198 m_toolListBuffer.back().m_TotalCount++;
199
200 if( m_holeListBuffer[ii].m_Hole_Shape )
201 m_toolListBuffer.back().m_OvalCount++;
202 }
203}
204
205
206std::vector<DRILL_LAYER_PAIR> GENDRILL_WRITER_BASE::getUniqueLayerPairs() const
207{
208 wxASSERT( m_pcb );
209
211
212 vias.Collect( m_pcb, { PCB_VIA_T } );
213
214 std::set<DRILL_LAYER_PAIR> unique;
215 DRILL_LAYER_PAIR layer_pair;
216
217 for( int i = 0; i < vias.GetCount(); ++i )
218 {
219 PCB_VIA* v = static_cast<PCB_VIA*>( vias[i] );
220
221 v->LayerPair( &layer_pair.first, &layer_pair.second );
222
223 // only make note of blind buried.
224 // thru hole is placed unconditionally as first in fetched list.
225 if( layer_pair != DRILL_LAYER_PAIR( F_Cu, B_Cu ) )
226 unique.insert( layer_pair );
227 }
228
229 std::vector<DRILL_LAYER_PAIR> ret;
230
231 ret.emplace_back( F_Cu, B_Cu ); // always first in returned list
232
233 for( const DRILL_LAYER_PAIR& pair : unique )
234 ret.push_back( pair );
235
236 return ret;
237}
238
239
240const std::string GENDRILL_WRITER_BASE::layerName( PCB_LAYER_ID aLayer ) const
241{
242 // Generic names here.
243 switch( aLayer )
244 {
245 case F_Cu:
246 return "front";
247 case B_Cu:
248 return "back";
249 default:
250 return StrPrintf( "in%d", aLayer );
251 }
252}
253
254
256{
257 std::string ret = layerName( aPair.first );
258 ret += '-';
259 ret += layerName( aPair.second );
260
261 return ret;
262}
263
264
266 bool aMerge_PTH_NPTH ) const
267{
268 wxASSERT( m_pcb );
269
270 wxString extend;
271
272 if( aNPTH )
273 extend = wxT( "-NPTH" );
274 else if( aPair == DRILL_LAYER_PAIR( F_Cu, B_Cu ) )
275 {
276 if( !aMerge_PTH_NPTH )
277 extend = wxT( "-PTH" );
278 // if merged, extend with nothing
279 }
280 else
281 {
282 extend += '-';
283 extend += layerPairName( aPair );
284 }
285
286 wxFileName fn = m_pcb->GetFileName();
287
288 fn.SetName( fn.GetName() + extend );
289 fn.SetExt( m_drillFileExtension );
290
291 wxString ret = fn.GetFullName();
292
293 return ret;
294}
295
296void GENDRILL_WRITER_BASE::CreateMapFilesSet( const wxString& aPlotDirectory,
297 REPORTER * aReporter )
298{
299 wxFileName fn;
300 wxString msg;
301
302 std::vector<DRILL_LAYER_PAIR> hole_sets = getUniqueLayerPairs();
303
304 // append a pair representing the NPTH set of holes, for separate drill files.
305 if( !m_merge_PTH_NPTH )
306 hole_sets.emplace_back( F_Cu, B_Cu );
307
308 for( std::vector<DRILL_LAYER_PAIR>::const_iterator it = hole_sets.begin();
309 it != hole_sets.end(); ++it )
310 {
311 DRILL_LAYER_PAIR pair = *it;
312 // For separate drill files, the last layer pair is the NPTH drill file.
313 bool doing_npth = m_merge_PTH_NPTH ? false : ( it == hole_sets.end() - 1 );
314
315 buildHolesList( pair, doing_npth );
316
317 // The file is created if it has holes, or if it is the non plated drill file
318 // to be sure the NPTH file is up to date in separate files mode.
319 // Also a PTH drill file is always created, to be sure at least one plated hole drill file
320 // is created (do not create any PTH drill file can be seen as not working drill generator).
321 if( getHolesCount() > 0 || doing_npth || pair == DRILL_LAYER_PAIR( F_Cu, B_Cu ) )
322 {
323 fn = getDrillFileName( pair, doing_npth, m_merge_PTH_NPTH );
324 fn.SetPath( aPlotDirectory );
325
326 fn.SetExt( wxEmptyString ); // Will be added by GenDrillMap
327 wxString fullfilename = fn.GetFullPath() + wxT( "-drl_map" );
328 fullfilename << wxT(".") << GetDefaultPlotExtension( m_mapFileFmt );
329
330 bool success = genDrillMapFile( fullfilename, m_mapFileFmt );
331
332 if( ! success )
333 {
334 if( aReporter )
335 {
336 msg.Printf( _( "Failed to create file '%s'." ), fullfilename );
337 aReporter->Report( msg, RPT_SEVERITY_ERROR );
338 }
339
340 return;
341 }
342 else
343 {
344 if( aReporter )
345 {
346 msg.Printf( _( "Created file '%s'." ), fullfilename );
347 aReporter->Report( msg, RPT_SEVERITY_ACTION );
348 }
349 }
350 }
351 }
352}
353
354
356 DRILL_LAYER_PAIR aLayerPair, TYPE_FILE aHoleType,
357 bool aCompatNCdrill ) const
358{
359// Build a wxString containing the .FileFunction attribute for drill files.
360// %TF.FileFunction,Plated[NonPlated],layer1num,layer2num,PTH[NPTH][Blind][Buried],Drill[Route][Mixed]*%
361 wxString text;
362
363 if( aCompatNCdrill )
364 text = wxT( "; #@! " );
365 else
366 text = wxT( "%" );
367
368 text << wxT( "TF.FileFunction," );
369
370 if( aHoleType == NPTH_FILE )
371 text << wxT( "NonPlated," );
372 else if( aHoleType == MIXED_FILE ) // only for Excellon format
373 text << wxT( "MixedPlating," );
374 else
375 text << wxT( "Plated," );
376
377 int layer1 = aLayerPair.first;
378 int layer2 = aLayerPair.second;
379 // In Gerber files, layers num are 1 to copper layer count instead of F_Cu to B_Cu
380 // (0 to copper layer count-1)
381 // Note also for a n copper layers board, gerber layers num are 1 ... n
382 layer1 += 1;
383
384 if( layer2 == B_Cu )
385 layer2 = m_pcb->GetCopperLayerCount();
386 else
387 layer2 += 1;
388
389 text << layer1 << wxT( "," ) << layer2;
390
391 // Now add PTH or NPTH or Blind or Buried attribute
392 int toplayer = 1;
393 int bottomlayer = m_pcb->GetCopperLayerCount();
394
395 if( aHoleType == NPTH_FILE )
396 text << wxT( ",NPTH" );
397 else if( aHoleType == MIXED_FILE ) // only for Excellon format
398 {
399 // write nothing
400 }
401 else if( layer1 == toplayer && layer2 == bottomlayer )
402 text << wxT( ",PTH" );
403 else if( layer1 == toplayer || layer2 == bottomlayer )
404 text << wxT( ",Blind" );
405 else
406 text << wxT( ",Buried" );
407
408 // In NC drill file, these previous parameters should be enough:
409 if( aCompatNCdrill )
410 return text;
411
412
413 // Now add Drill or Route or Mixed:
414 // file containing only round holes have Drill attribute
415 // file containing only oblong holes have Routed attribute
416 // file containing both holes have Mixed attribute
417 bool hasOblong = false;
418 bool hasDrill = false;
419
420 for( unsigned ii = 0; ii < m_holeListBuffer.size(); ii++ )
421 {
422 const HOLE_INFO& hole_descr = m_holeListBuffer[ii];
423
424 if( hole_descr.m_Hole_Shape ) // m_Hole_Shape not 0 is an oblong hole)
425 hasOblong = true;
426 else
427 hasDrill = true;
428 }
429
430 if( hasOblong && hasDrill )
431 text << wxT( ",Mixed" );
432 else if( hasDrill )
433 text << wxT( ",Drill" );
434 else if( hasOblong )
435 text << wxT( ",Route" );
436
437 // else: empty file.
438
439 // End of .FileFunction attribute:
440 text << wxT( "*%" );
441
442 return text;
443}
FOOTPRINTS & Footprints()
Definition: board.h:307
int GetCopperLayerCount() const
Definition: board.cpp:502
TRACKS & Tracks()
Definition: board.h:304
const wxString & GetFileName() const
Definition: board.h:302
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.
void 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 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
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:59
Collect all BOARD_ITEM objects of a given set of KICAD_T type(s).
Definition: collectors.h:522
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:631
void LayerPair(PCB_LAYER_ID *top_layer, PCB_LAYER_ID *bottom_layer) const
Function LayerPair Return the 2 layers used by the via (the via actually uses all layers between thes...
Definition: pcb_track.cpp:542
A pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:71
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:412
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:59
@ B_Cu
Definition: layer_ids.h:95
@ F_Cu
Definition: layer_ids.h:64
@ NPTH
like PAD_PTH, but not plated
@ PAD_DRILL_SHAPE_CIRCLE
Definition: pad_shapes.h:70
@ 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:84
@ PCB_VIA_T
class PCB_VIA, a via (like a track segment on a copper layer)
Definition: typeinfo.h:102