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-2023 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 ) )
93 new_hole.m_HoleAttribute = HOLE_ATTRIBUTE::HOLE_VIA_THROUGH;
94 else
95 new_hole.m_HoleAttribute = HOLE_ATTRIBUTE::HOLE_VIA_BURIED;
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
141 ? HOLE_ATTRIBUTE::HOLE_MECHANICAL
142 : HOLE_ATTRIBUTE::HOLE_PAD;
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 // Convert oblong holes that are actually circular into drill hits
150 if( pad->GetDrillShape() != PAD_DRILL_SHAPE_CIRCLE &&
151 pad->GetDrillSizeX() != pad->GetDrillSizeY() )
152 {
153 new_hole.m_Hole_Shape = 1; // oval flag set
154 }
155
156 new_hole.m_Hole_Size = pad->GetDrillSize();
157 new_hole.m_Hole_Pos = pad->GetPosition(); // hole position
158 new_hole.m_Hole_Bottom_Layer = B_Cu;
159 new_hole.m_Hole_Top_Layer = F_Cu; // pad holes are through holes
160 m_holeListBuffer.push_back( new_hole );
161 }
162 }
163 }
164
165 // Sort holes per increasing diameter value (and for each dimater, by position)
166 sort( m_holeListBuffer.begin(), m_holeListBuffer.end(), cmpHoleSorting );
167
168 // build the tool list
169 int last_hole = -1; // Set to not initialized (this is a value not used
170 // for m_holeListBuffer[ii].m_Hole_Diameter)
171 bool last_notplated_opt = false;
172 HOLE_ATTRIBUTE last_attribute = HOLE_ATTRIBUTE::HOLE_UNKNOWN;
173
174 DRILL_TOOL new_tool( 0, false );
175 unsigned jj;
176
177 for( unsigned ii = 0; ii < m_holeListBuffer.size(); ii++ )
178 {
179 if( m_holeListBuffer[ii].m_Hole_Diameter != last_hole
180 || m_holeListBuffer[ii].m_Hole_NotPlated != last_notplated_opt
182 || m_holeListBuffer[ii].m_HoleAttribute != last_attribute
183#endif
184 )
185 {
186 new_tool.m_Diameter = m_holeListBuffer[ii].m_Hole_Diameter;
187 new_tool.m_Hole_NotPlated = m_holeListBuffer[ii].m_Hole_NotPlated;
188 new_tool.m_HoleAttribute = m_holeListBuffer[ii].m_HoleAttribute;
189 m_toolListBuffer.push_back( new_tool );
190 last_hole = new_tool.m_Diameter;
191 last_notplated_opt = new_tool.m_Hole_NotPlated;
192 last_attribute = new_tool.m_HoleAttribute;
193 }
194
195 jj = m_toolListBuffer.size();
196
197 if( jj == 0 )
198 continue; // Should not occurs
199
200 m_holeListBuffer[ii].m_Tool_Reference = jj; // Tool value Initialized (value >= 1)
201
202 m_toolListBuffer.back().m_TotalCount++;
203
204 if( m_holeListBuffer[ii].m_Hole_Shape )
205 m_toolListBuffer.back().m_OvalCount++;
206 }
207}
208
209
210std::vector<DRILL_LAYER_PAIR> GENDRILL_WRITER_BASE::getUniqueLayerPairs() const
211{
212 wxASSERT( m_pcb );
213
215
216 vias.Collect( m_pcb, { PCB_VIA_T } );
217
218 std::set<DRILL_LAYER_PAIR> unique;
219 DRILL_LAYER_PAIR layer_pair;
220
221 for( int i = 0; i < vias.GetCount(); ++i )
222 {
223 PCB_VIA* v = static_cast<PCB_VIA*>( vias[i] );
224
225 v->LayerPair( &layer_pair.first, &layer_pair.second );
226
227 // only make note of blind buried.
228 // thru hole is placed unconditionally as first in fetched list.
229 if( layer_pair != DRILL_LAYER_PAIR( F_Cu, B_Cu ) )
230 unique.insert( layer_pair );
231 }
232
233 std::vector<DRILL_LAYER_PAIR> ret;
234
235 ret.emplace_back( F_Cu, B_Cu ); // always first in returned list
236
237 for( const DRILL_LAYER_PAIR& pair : unique )
238 ret.push_back( pair );
239
240 return ret;
241}
242
243
244const std::string GENDRILL_WRITER_BASE::layerName( PCB_LAYER_ID aLayer ) const
245{
246 // Generic names here.
247 switch( aLayer )
248 {
249 case F_Cu:
250 return "front";
251 case B_Cu:
252 return "back";
253 default:
254 return StrPrintf( "in%d", aLayer );
255 }
256}
257
258
260{
261 std::string ret = layerName( aPair.first );
262 ret += '-';
263 ret += layerName( aPair.second );
264
265 return ret;
266}
267
268
270 bool aMerge_PTH_NPTH ) const
271{
272 wxASSERT( m_pcb );
273
274 wxString extend;
275
276 if( aNPTH )
277 extend = wxT( "-NPTH" );
278 else if( aPair == DRILL_LAYER_PAIR( F_Cu, B_Cu ) )
279 {
280 if( !aMerge_PTH_NPTH )
281 extend = wxT( "-PTH" );
282 // if merged, extend with nothing
283 }
284 else
285 {
286 extend += '-';
287 extend += layerPairName( aPair );
288 }
289
290 wxFileName fn = m_pcb->GetFileName();
291
292 fn.SetName( fn.GetName() + extend );
293 fn.SetExt( m_drillFileExtension );
294
295 wxString ret = fn.GetFullName();
296
297 return ret;
298}
299
300bool GENDRILL_WRITER_BASE::CreateMapFilesSet( const wxString& aPlotDirectory,
301 REPORTER * aReporter )
302{
303 wxFileName fn;
304 wxString msg;
305
306 std::vector<DRILL_LAYER_PAIR> hole_sets = getUniqueLayerPairs();
307
308 // append a pair representing the NPTH set of holes, for separate drill files.
309 if( !m_merge_PTH_NPTH )
310 hole_sets.emplace_back( F_Cu, B_Cu );
311
312 for( std::vector<DRILL_LAYER_PAIR>::const_iterator it = hole_sets.begin();
313 it != hole_sets.end(); ++it )
314 {
315 DRILL_LAYER_PAIR pair = *it;
316 // For separate drill files, the last layer pair is the NPTH drill file.
317 bool doing_npth = m_merge_PTH_NPTH ? false : ( it == hole_sets.end() - 1 );
318
319 buildHolesList( pair, doing_npth );
320
321 // The file is created if it has holes, or if it is the non plated drill file
322 // to be sure the NPTH file is up to date in separate files mode.
323 // Also a PTH drill file is always created, to be sure at least one plated hole drill file
324 // is created (do not create any PTH drill file can be seen as not working drill generator).
325 if( getHolesCount() > 0 || doing_npth || pair == DRILL_LAYER_PAIR( F_Cu, B_Cu ) )
326 {
328 fn.SetPath( aPlotDirectory );
329
330 fn.SetExt( wxEmptyString ); // Will be added by GenDrillMap
331 wxString fullfilename = fn.GetFullPath() + wxT( "-drl_map" );
332 fullfilename << wxT(".") << GetDefaultPlotExtension( m_mapFileFmt );
333
334 bool success = genDrillMapFile( fullfilename, m_mapFileFmt );
335
336 if( ! success )
337 {
338 if( aReporter )
339 {
340 msg.Printf( _( "Failed to create file '%s'." ), fullfilename );
341 aReporter->Report( msg, RPT_SEVERITY_ERROR );
342 }
343
344 return false;
345 }
346 else
347 {
348 if( aReporter )
349 {
350 msg.Printf( _( "Created file '%s'." ), fullfilename );
351 aReporter->Report( msg, RPT_SEVERITY_ACTION );
352 }
353 }
354 }
355 }
356
357 return true;
358}
359
360
362 DRILL_LAYER_PAIR aLayerPair, TYPE_FILE aHoleType,
363 bool aCompatNCdrill ) const
364{
365// Build a wxString containing the .FileFunction attribute for drill files.
366// %TF.FileFunction,Plated[NonPlated],layer1num,layer2num,PTH[NPTH][Blind][Buried],Drill[Route][Mixed]*%
367 wxString text;
368
369 if( aCompatNCdrill )
370 text = wxT( "; #@! " );
371 else
372 text = wxT( "%" );
373
374 text << wxT( "TF.FileFunction," );
375
376 if( aHoleType == NPTH_FILE )
377 text << wxT( "NonPlated," );
378 else if( aHoleType == MIXED_FILE ) // only for Excellon format
379 text << wxT( "MixedPlating," );
380 else
381 text << wxT( "Plated," );
382
383 int layer1 = aLayerPair.first;
384 int layer2 = aLayerPair.second;
385 // In Gerber files, layers num are 1 to copper layer count instead of F_Cu to B_Cu
386 // (0 to copper layer count-1)
387 // Note also for a n copper layers board, gerber layers num are 1 ... n
388 layer1 += 1;
389
390 if( layer2 == B_Cu )
391 layer2 = m_pcb->GetCopperLayerCount();
392 else
393 layer2 += 1;
394
395 text << layer1 << wxT( "," ) << layer2;
396
397 // Now add PTH or NPTH or Blind or Buried attribute
398 int toplayer = 1;
399 int bottomlayer = m_pcb->GetCopperLayerCount();
400
401 if( aHoleType == NPTH_FILE )
402 text << wxT( ",NPTH" );
403 else if( aHoleType == MIXED_FILE ) // only for Excellon format
404 {
405 // write nothing
406 }
407 else if( layer1 == toplayer && layer2 == bottomlayer )
408 text << wxT( ",PTH" );
409 else if( layer1 == toplayer || layer2 == bottomlayer )
410 text << wxT( ",Blind" );
411 else
412 text << wxT( ",Buried" );
413
414 // In NC drill file, these previous parameters should be enough:
415 if( aCompatNCdrill )
416 return text;
417
418
419 // Now add Drill or Route or Mixed:
420 // file containing only round holes have Drill attribute
421 // file containing only oblong holes have Routed attribute
422 // file containing both holes have Mixed attribute
423 bool hasOblong = false;
424 bool hasDrill = false;
425
426 for( unsigned ii = 0; ii < m_holeListBuffer.size(); ii++ )
427 {
428 const HOLE_INFO& hole_descr = m_holeListBuffer[ii];
429
430 if( hole_descr.m_Hole_Shape ) // m_Hole_Shape not 0 is an oblong hole)
431 hasOblong = true;
432 else
433 hasDrill = true;
434 }
435
436 if( hasOblong && hasDrill )
437 text << wxT( ",Mixed" );
438 else if( hasDrill )
439 text << wxT( ",Drill" );
440 else if( hasOblong )
441 text << wxT( ",Rout" );
442
443 // else: empty file.
444
445 // End of .FileFunction attribute:
446 text << wxT( "*%" );
447
448 return text;
449}
int GetCopperLayerCount() const
Definition: board.cpp:656
const FOOTPRINTS & Footprints() const
Definition: board.h:322
const TRACKS & Tracks() const
Definition: board.h:320
const wxString & GetFileName() const
Definition: board.h:318
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:59
Collect all BOARD_ITEM objects of a given set of KICAD_T type(s).
Definition: collectors.h:523
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:516
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:952
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:435
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:95
@ F_Cu
Definition: layer_ids.h:64
@ PAD_DRILL_SHAPE_CIRCLE
Definition: pad_shapes.h:54
@ 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