1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
|
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2015 Jean-Pierre Charras, jp.charras at wanadoo.fr
* Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
* Copyright (C) 1992-2015 KiCad Developers, see AUTHORS.txt for contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
/**
* @file pcbnew/pcbplot.cpp
*/
#include <fctsys.h>
#include <plot_common.h>
#include <confirm.h>
#include <wxPcbStruct.h>
#include <pcbplot.h>
#include <pcbstruct.h>
#include <base_units.h>
#include <reporter.h>
#include <class_board.h>
#include <pcbnew.h>
#include <plotcontroller.h>
#include <pcb_plot_params.h>
#include <wx/ffile.h>
#include <dialog_plot.h>
#include <macros.h>
#include <build_version.h>
const wxString GetGerberProtelExtension( LAYER_NUM aLayer )
{
if( IsCopperLayer( aLayer ) )
{
if( aLayer == F_Cu )
return wxT( "gtl" );
else if( aLayer == B_Cu )
return wxT( "gbl" );
else
{
return wxString::Format( wxT( "g%d" ), aLayer+1 );
}
}
else
{
switch( aLayer )
{
case B_Adhes: return wxT( "gba" );
case F_Adhes: return wxT( "gta" );
case B_Paste: return wxT( "gbp" );
case F_Paste: return wxT( "gtp" );
case B_SilkS: return wxT( "gbo" );
case F_SilkS: return wxT( "gto" );
case B_Mask: return wxT( "gbs" );
case F_Mask: return wxT( "gts" );
case Edge_Cuts: return wxT( "gm1" );
case Dwgs_User:
case Cmts_User:
case Eco1_User:
case Eco2_User:
default: return wxT( "gbr" );
}
}
}
wxString GetGerberFileFunctionAttribute( const BOARD *aBoard,
LAYER_NUM aLayer, bool aUseX1CompatibilityMode )
{
wxString attrib;
switch( aLayer )
{
case F_Adhes:
attrib = wxString( wxT( "Glue,Top" ) );
break;
case B_Adhes:
attrib = wxString( wxT( "Glue,Bot" ) );
break;
case F_SilkS:
attrib = wxString( wxT( "Legend,Top" ) );
break;
case B_SilkS:
attrib = wxString( wxT( "Legend,Bot" ) );
break;
case F_Mask:
attrib = wxString( wxT( "Soldermask,Top" ) );
break;
case B_Mask:
attrib = wxString( wxT( "Soldermask,Bot" ) );
break;
case F_Paste:
attrib = wxString( wxT( "Paste,Top" ) );
break;
case B_Paste:
attrib = wxString( wxT( "Paste,Bot" ) );
break;
case Edge_Cuts:
// Board outline.
// Can be "Profile,NP" (Not Plated: usual) or "Profile,P"
// This last is the exception (Plated)
attrib = wxString( wxT( "Profile,NP" ) );
break;
case Dwgs_User:
attrib = wxString( wxT( "Drawing" ) );
break;
case Cmts_User:
attrib = wxString( wxT( "Other,Comment" ) );
break;
case Eco1_User:
attrib = wxString( wxT( "Other,ECO1" ) );
break;
case Eco2_User:
attrib = wxString( wxT( "Other,ECO2" ) );
break;
case B_Fab:
attrib = wxString( wxT( "Other,Fab,Bot" ) );
break;
case F_Fab:
attrib = wxString( wxT( "Other,Fab,Top" ) );
break;
case B_Cu:
attrib = wxString::Format( wxT( "Copper,L%d,Bot" ), aBoard->GetCopperLayerCount() );
break;
case F_Cu:
attrib = wxString::Format( wxT( "Copper,L1,Top" ) );
break;
default:
if( IsCopperLayer( aLayer ) )
attrib = wxString::Format( wxT( "Copper,L%d,Inr" ), aLayer+1 );
else
attrib = wxString::Format( wxT( "Other,User" ), aLayer+1 );
break;
}
// Add the signal type of the layer, if relevant
if( IsCopperLayer( aLayer ) )
{
LAYER_T type = aBoard->GetLayerType( ToLAYER_ID( aLayer ) );
switch( type )
{
case LT_SIGNAL:
attrib += wxString( wxT( ",Signal" ) );
break;
case LT_POWER:
attrib += wxString( wxT( ",Plane" ) );
break;
case LT_MIXED:
attrib += wxString( wxT( ",Mixed" ) );
break;
default:
break; // do nothing (but avoid a warning for unhandled LAYER_T values from GCC)
}
}
wxString fileFct;
if( aUseX1CompatibilityMode )
fileFct.Printf( "G04 #@! TF.FileFunction,%s*", GetChars( attrib ) );
else
fileFct.Printf( "%%TF.FileFunction,%s*%%", GetChars( attrib ) );
return fileFct;
}
/* Add some X2 attributes to the file header, as defined in the
* Gerber file format specification J4 and "Revision 2015.06"
*/
#define USE_REVISION_2015_06_ATTR
void AddGerberX2Attribute( PLOTTER * aPlotter,
const BOARD *aBoard, LAYER_NUM aLayer )
{
wxString text;
#ifdef USE_REVISION_2015_06_ATTR
// Creates the TF,.GenerationSoftware. Format is:
// %TF,.GenerationSoftware,<vendor>,<application name>[,<application version>]*%
text.Printf( wxT( "%%TF.GenerationSoftware,KiCad,Pcbnew,%s*%%" ), GetBuildVersion() );
aPlotter->AddLineToHeader( text );
// creates the TF.CreationDate ext:
// The attribute value must conform to the full version of the ISO 8601
// date and time format, including time and time zone. Note that this is
// the date the Gerber file was effectively created,
// not the time the project of PCB was started
wxDateTime date( wxDateTime::GetTimeNow() );
// Date format: see http://www.cplusplus.com/reference/ctime/strftime
wxString msg = date.Format( wxT( "%z" ) ); // Extract the time zone offset
// The time zone offset format is + (or -) mm or hhmm (mm = number of minutes, hh = number of hours)
// we want +(or -) hh:mm
if( msg.Len() > 3 )
msg.insert( 3, ":", 1 ),
text.Printf( wxT( "%%TF.CreationDate,%s%s*%%" ), GetChars( date.FormatISOCombined() ), GetChars( msg ) );
aPlotter->AddLineToHeader( text );
// Creates the TF,.ProjectId. Format is (from Gerber file format doc):
// %TF.ProjectId,<project id>,<project GUID>,<revision id>*%
// <project id> is the name of the project, restricted to basic ASCII symbols only,
// and comma not accepted
// All illegal chars will be replaced by underscore
// <project GUID> is a 32 hexadecimal digits string which is an unique id of a project.
// This is a random 128-bit number expressed in 32 hexadecimal digits.
// See en.wikipedia.org/wiki/GUID for more information
// However Kicad does not handle such a project GUID, so it is built from the board name
// Rem: <project id> accepts only ASCII 7 code (only basic ASCII codes are allowed in gerber files).
wxFileName fn = aBoard->GetFileName();
msg = fn.GetFullName();
wxString guid;
// Build a 32 digits GUID from the board name:
for( unsigned ii = 0; ii < msg.Len(); ii++ )
{
int cc1 = int( msg[ii] ) & 0x0F;
int cc2 = ( int( msg[ii] ) >> 4) & 0x0F;
guid << wxString::Format( wxT( "%X%X" ), cc2, cc1 );
if( guid.Len() >= 32 )
break;
}
// guid has 32 digits, so add missing digits
int cnt = 32 - guid.Len();
if( cnt > 0 )
guid.Append( '0', cnt );
// build the <project id> string: this is the board short filename (without ext)
// and all non ASCII chars and comma are replaced by '_'
msg = fn.GetName();
msg.Replace( wxT( "," ), wxT( "_" ) );
// build the <rec> string. All non ASCII chars and comma are replaced by '_'
wxString rev = ((BOARD*)aBoard)->GetTitleBlock().GetRevision();
rev.Replace( wxT( "," ), wxT( "_" ) );
if( rev.IsEmpty() )
rev = wxT( "rev?" );
text.Printf( wxT( "%%TF.ProjectId,%s,%s,%s*%%" ), msg.ToAscii(), GetChars( guid ), rev.ToAscii() );
aPlotter->AddLineToHeader( text );
#endif
// Add the TF.FileFunction
text = GetGerberFileFunctionAttribute( aBoard, aLayer, false );
aPlotter->AddLineToHeader( text );
}
void BuildPlotFileName( wxFileName* aFilename, const wxString& aOutputDir,
const wxString& aSuffix, const wxString& aExtension )
{
// aFilename contains the base filename only (without path and extension)
// when calling this function.
// It is expected to be a valid filename (this is usually the board filename)
aFilename->SetPath( aOutputDir );
// Set the file extension
aFilename->SetExt( aExtension );
// remove leading and trailing spaces if any from the suffix, if
// something survives add it to the name;
// also the suffix can contain some not allowed chars in filename (/ \ . :),
// so change them to underscore
// Remember it can be called from a python script, so the illegal chars
// have to be filtered here.
wxString suffix = aSuffix;
suffix.Trim( true );
suffix.Trim( false );
wxString badchars = wxFileName::GetForbiddenChars(wxPATH_DOS);
badchars.Append( '%' );
for( unsigned ii = 0; ii < badchars.Len(); ii++ )
suffix.Replace( badchars[ii], wxT("_") );
if( !suffix.IsEmpty() )
aFilename->SetName( aFilename->GetName() + wxT( "-" ) + suffix );
}
PLOT_CONTROLLER::PLOT_CONTROLLER( BOARD *aBoard )
{
m_plotter = NULL;
m_board = aBoard;
m_plotLayer = UNDEFINED_LAYER;
}
PLOT_CONTROLLER::~PLOT_CONTROLLER()
{
ClosePlot();
}
/* IMPORTANT THING TO KNOW: the locale during plots *MUST* be kept as
* C/POSIX using a LOCALE_IO object on the stack. This even when
* opening/closing the plotfile, since some drivers do I/O even then */
void PLOT_CONTROLLER::ClosePlot()
{
LOCALE_IO toggle;
if( m_plotter )
{
m_plotter->EndPlot();
delete m_plotter;
m_plotter = NULL;
}
}
bool PLOT_CONTROLLER::OpenPlotfile( const wxString &aSuffix,
PlotFormat aFormat,
const wxString &aSheetDesc )
{
LOCALE_IO toggle;
/* Save the current format: sadly some plot routines depends on this
but the main reason is that the StartPlot method uses it to
dispatch the plotter creation */
GetPlotOptions().SetFormat( aFormat );
// Ensure that the previous plot is closed
ClosePlot();
// Now compute the full filename for the output and start the plot
// (after ensuring the output directory is OK)
wxString outputDirName = GetPlotOptions().GetOutputDirectory() ;
wxFileName outputDir = wxFileName::DirName( outputDirName );
wxString boardFilename = m_board->GetFileName();
if( EnsureFileDirectoryExists( &outputDir, boardFilename ) )
{
// outputDir contains now the full path of plot files
m_plotFile = boardFilename;
m_plotFile.SetPath( outputDir.GetPath() );
wxString fileExt = GetDefaultPlotExtension( aFormat );
// Gerber format can use specific file ext, depending on layers
// (now not a good practice, because the official file ext is .gbr)
if( GetPlotOptions().GetFormat() == PLOT_FORMAT_GERBER &&
GetPlotOptions().GetUseGerberProtelExtensions() )
fileExt = GetGerberProtelExtension( GetLayer() );
// Build plot filenames from the board name and layer names:
BuildPlotFileName( &m_plotFile, outputDir.GetPath(), aSuffix, fileExt );
m_plotter = StartPlotBoard( m_board, &GetPlotOptions(), ToLAYER_ID( GetLayer() ),
m_plotFile.GetFullPath(), aSheetDesc );
}
return( m_plotter != NULL );
}
bool PLOT_CONTROLLER::PlotLayer()
{
LOCALE_IO toggle;
// No plot open, nothing to do...
if( !m_plotter )
return false;
// Fully delegated to the parent
PlotOneBoardLayer( m_board, m_plotter, ToLAYER_ID( GetLayer() ), GetPlotOptions() );
return true;
}
void PLOT_CONTROLLER::SetColorMode( bool aColorMode )
{
if( !m_plotter )
return;
m_plotter->SetColorMode( aColorMode );
}
bool PLOT_CONTROLLER::GetColorMode()
{
if( !m_plotter )
return false;
return m_plotter->GetColorMode();
}
|