KiCad PCB EDA Suite
pcbplot.cpp File Reference
#include <plotters/plotter.h>
#include <pcbplot.h>
#include <base_units.h>
#include <locale_io.h>
#include <reporter.h>
#include <board.h>
#include <board_design_settings.h>
#include <plotcontroller.h>
#include <pcb_plot_params.h>
#include <wx/ffile.h>
#include <dialog_plot.h>
#include <build_version.h>
#include <gbr_metadata.h>
#include <render_settings.h>

Go to the source code of this file.

Functions

const wxString GetGerberProtelExtension (int aLayer)
 
const wxString GetGerberFileFunctionAttribute (const BOARD *aBoard, int aLayer)
 Return the "file function" attribute for aLayer, as defined in the Gerber file format specification J1 (chapter 5). More...
 
static const wxString GetGerberFilePolarityAttribute (int aLayer)
 
static wxString & makeStringCompatX1 (wxString &aText, bool aUseX1CompatibilityMode)
 
void AddGerberX2Header (PLOTTER *aPlotter, const BOARD *aBoard, bool aUseX1CompatibilityMode)
 Calculate some X2 attributes as defined in the Gerber file format specification J4 (chapter 5) and add them the to the gerber file header. More...
 
void AddGerberX2Attribute (PLOTTER *aPlotter, const BOARD *aBoard, int aLayer, bool aUseX1CompatibilityMode)
 Calculate some X2 attributes as defined in the Gerber file format specification and add them to the gerber file header. More...
 
void BuildPlotFileName (wxFileName *aFilename, const wxString &aOutputDir, const wxString &aSuffix, const wxString &aExtension)
 Complete a plot filename. More...
 

Function Documentation

◆ AddGerberX2Attribute()

void AddGerberX2Attribute ( PLOTTER aPlotter,
const BOARD aBoard,
int  aLayer,
bool  aUseX1CompatibilityMode 
)

Calculate some X2 attributes as defined in the Gerber file format specification and add them to the gerber file header.

TF.GenerationSoftware TF.CreationDate TF.ProjectId TF.FileFunction TF.FilePolarity

Parameters
aPlotteris the current plotter.
aBoardis the board, needed to extract some info.
aLayeris the layer number to create the attribute for.
aUseX1CompatibilityModeset to false to generate X2 attributes, true to use X1 compatibility (X2 attributes added as structured comments, starting by "G04 #@! " followed by the X2 attribute.

Definition at line 342 of file pcbplot.cpp.

344{
345 AddGerberX2Header( aPlotter, aBoard, aUseX1CompatibilityMode );
346
347 wxString text;
348
349 // Add the TF.FileFunction
350 text = GetGerberFileFunctionAttribute( aBoard, aLayer );
351 aPlotter->AddLineToHeader( makeStringCompatX1( text, aUseX1CompatibilityMode ) );
352
353 // Add the TF.FilePolarity (for layers which support that)
355
356 if( !text.IsEmpty() )
357 aPlotter->AddLineToHeader( makeStringCompatX1( text, aUseX1CompatibilityMode ) );
358}
void AddLineToHeader(const wxString &aExtraString)
Add a line to the list of free lines to print at the beginning of the file.
Definition: plotter.h:168
static wxString & makeStringCompatX1(wxString &aText, bool aUseX1CompatibilityMode)
Definition: pcbplot.cpp:264
void AddGerberX2Header(PLOTTER *aPlotter, const BOARD *aBoard, bool aUseX1CompatibilityMode)
Calculate some X2 attributes as defined in the Gerber file format specification J4 (chapter 5) and ad...
Definition: pcbplot.cpp:276
const wxString GetGerberFileFunctionAttribute(const BOARD *aBoard, int aLayer)
Return the "file function" attribute for aLayer, as defined in the Gerber file format specification J...
Definition: pcbplot.cpp:81
static const wxString GetGerberFilePolarityAttribute(int aLayer)
Definition: pcbplot.cpp:203

References AddGerberX2Header(), PLOTTER::AddLineToHeader(), GetGerberFileFunctionAttribute(), GetGerberFilePolarityAttribute(), makeStringCompatX1(), and text.

Referenced by StartPlotBoard().

◆ AddGerberX2Header()

void AddGerberX2Header ( PLOTTER aPlotter,
const BOARD aBoard,
bool  aUseX1CompatibilityMode = false 
)

Calculate some X2 attributes as defined in the Gerber file format specification J4 (chapter 5) and add them the to the gerber file header.

TF.GenerationSoftware TF.CreationDate TF.ProjectId file format attribute is not added

Parameters
aPlotteris the current plotter.
aBoardis the board, needed to extract some info.
aUseX1CompatibilityModeset to false to generate X2 attributes, true to use X1 compatibility (X2 attributes added as structured comments, starting by "G04 #@! " followed by the X2 attribute

Definition at line 276 of file pcbplot.cpp.

277{
278 wxString text;
279
280 // Creates the TF,.GenerationSoftware. Format is:
281 // %TF,.GenerationSoftware,<vendor>,<application name>[,<application version>]*%
282 text.Printf( wxT( "%%TF.GenerationSoftware,KiCad,Pcbnew,%s*%%" ), GetBuildVersion() );
283 aPlotter->AddLineToHeader( makeStringCompatX1( text, aUseX1CompatibilityMode ) );
284
285 // creates the TF.CreationDate attribute:
288 aPlotter->AddLineToHeader( text );
289
290 // Creates the TF,.ProjectId. Format is (from Gerber file format doc):
291 // %TF.ProjectId,<project id>,<project GUID>,<revision id>*%
292 // <project id> is the name of the project, restricted to basic ASCII symbols only,
293 // Rem: <project id> accepts only ASCII 7 code (only basic ASCII codes are allowed in
294 // gerber files) and comma not accepted.
295 // All illegal chars will be replaced by underscore.
296 //
297 // <project GUID> is a string which is an unique id of a project.
298 // However Kicad does not handle such a project GUID, so it is built from the board name
299 wxFileName fn = aBoard->GetFileName();
300 wxString msg = fn.GetFullName();
301
302 // Build a <project GUID>, from the board name
303 wxString guid = GbrMakeProjectGUIDfromString( msg );
304
305 // build the <project id> string: this is the board short filename (without ext)
306 // and all non ASCII chars and comma are replaced by '_'
307 msg = fn.GetName();
308 msg.Replace( wxT( "," ), wxT( "_" ) );
309
310 // build the <revision id> string. All non ASCII chars and comma are replaced by '_'
311 wxString rev = ExpandTextVars( aBoard->GetTitleBlock().GetRevision(), aBoard->GetProject() );
312 rev.Replace( wxT( "," ), wxT( "_" ) );
313
314 if( rev.IsEmpty() )
315 rev = wxT( "rev?" );
316
317 text.Printf( wxT( "%%TF.ProjectId,%s,%s,%s*%%" ), msg.ToAscii(), guid, rev.ToAscii() );
318 aPlotter->AddLineToHeader( makeStringCompatX1( text, aUseX1CompatibilityMode ) );
319
320 // Add the TF.SameCoordinates to specify that all gerber files uses the same origin and
321 // orientation, and the registration between files is OK.
322 // The parameter of TF.SameCoordinates is a string that is common to all files using the
323 // same registration. The string value has no meaning; it is just a key.
324 // Because there is no mirroring/rotation in Kicad, only the plot offset origin can create
325 // incorrect registration, so we create a key from plot offset options.
326 //
327 // Currently the key is "Original" when using absolute Pcbnew coordinates, and the PY and PY
328 // position of the auxiliary axis when using it.
329 // If we ever add user-settable absolute Pcbnew coordinates, we'll need to change the way
330 // the key is built to ensure file only using the *same* axis have the same key.
331 wxString registration_id = wxT( "Original" );
332 VECTOR2I auxOrigin = aBoard->GetDesignSettings().GetAuxOrigin();
333
334 if( aBoard->GetPlotOptions().GetUseAuxOrigin() && auxOrigin.x && auxOrigin.y )
335 registration_id.Printf( wxT( "PX%xPY%x" ), auxOrigin.x, auxOrigin.y );
336
337 text.Printf( wxT( "%%TF.SameCoordinates,%s*%%" ), registration_id.GetData() );
338 aPlotter->AddLineToHeader( makeStringCompatX1( text, aUseX1CompatibilityMode ) );
339}
wxString GetBuildVersion()
Get the full KiCad version string.
const VECTOR2I & GetAuxOrigin()
TITLE_BLOCK & GetTitleBlock()
Definition: board.h:632
const wxString & GetFileName() const
Definition: board.h:302
const PCB_PLOT_PARAMS & GetPlotOptions() const
Definition: board.h:629
PROJECT * GetProject() const
Definition: board.h:440
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Definition: board.cpp:682
bool GetUseAuxOrigin() const
const wxString & GetRevision() const
Definition: title_block.h:86
wxString ExpandTextVars(const wxString &aSource, const PROJECT *aProject)
Definition: common.cpp:58
wxString GbrMakeProjectGUIDfromString(const wxString &aText)
Build a project GUID using format RFC4122 Version 1 or 4 from the project name, because a KiCad proje...
wxString GbrMakeCreationDateAttributeString(GBR_NC_STRING_FORMAT aFormat)
@ GBR_NC_STRING_FORMAT_X1
Definition: gbr_metadata.h:61
@ GBR_NC_STRING_FORMAT_X2
Definition: gbr_metadata.h:62

References PLOTTER::AddLineToHeader(), ExpandTextVars(), GBR_NC_STRING_FORMAT_X1, GBR_NC_STRING_FORMAT_X2, GbrMakeCreationDateAttributeString(), GbrMakeProjectGUIDfromString(), BOARD_DESIGN_SETTINGS::GetAuxOrigin(), GetBuildVersion(), BOARD::GetDesignSettings(), BOARD::GetFileName(), BOARD::GetPlotOptions(), BOARD::GetProject(), TITLE_BLOCK::GetRevision(), BOARD::GetTitleBlock(), PCB_PLOT_PARAMS::GetUseAuxOrigin(), makeStringCompatX1(), text, VECTOR2< T >::x, and VECTOR2< T >::y.

Referenced by AddGerberX2Attribute(), GERBER_WRITER::createDrillFile(), PLACEFILE_GERBER_WRITER::CreatePlaceFile(), and GENDRILL_WRITER_BASE::genDrillMapFile().

◆ BuildPlotFileName()

void BuildPlotFileName ( wxFileName *  aFilename,
const wxString &  aOutputDir,
const wxString &  aSuffix,
const wxString &  aExtension 
)

Complete a plot filename.

It forces the output directory, adds a suffix to the name, and sets the specified extension. The suffix is usually the layer name and replaces illegal file name character in the suffix with an underscore character.

Parameters
aFilenameis the file name to initialize that contains the base filename.
aOutputDiris the path.
aSuffixis the suffix to add to the base filename.
aExtensionis the file extension.

Definition at line 361 of file pcbplot.cpp.

363{
364 // aFilename contains the base filename only (without path and extension)
365 // when calling this function.
366 // It is expected to be a valid filename (this is usually the board filename)
367 aFilename->SetPath( aOutputDir );
368
369 // Set the file extension
370 aFilename->SetExt( aExtension );
371
372 // remove leading and trailing spaces if any from the suffix, if
373 // something survives add it to the name;
374 // also the suffix can contain some not allowed chars in filename (/ \ . : and some others),
375 // so change them to underscore
376 // Remember it can be called from a python script, so the illegal chars
377 // have to be filtered here.
378 wxString suffix = aSuffix;
379 suffix.Trim( true );
380 suffix.Trim( false );
381
382 wxString badchars = wxFileName::GetForbiddenChars(wxPATH_DOS);
383 badchars.Append( "%." );
384
385 for( unsigned ii = 0; ii < badchars.Len(); ii++ )
386 suffix.Replace( badchars[ii], wxT("_") );
387
388 if( !suffix.IsEmpty() )
389 aFilename->SetName( aFilename->GetName() + wxT( "-" ) + suffix );
390}

Referenced by DIALOG_EXPORT_SVG::ExportSVGFile(), PCBNEW_JOBS_HANDLER::JobExportGerbers(), PLOT_CONTROLLER::OpenPlotfile(), and DIALOG_PLOT::Plot().

◆ GetGerberFileFunctionAttribute()

const wxString GetGerberFileFunctionAttribute ( const BOARD aBoard,
int  aLayer 
)

Return the "file function" attribute for aLayer, as defined in the Gerber file format specification J1 (chapter 5).

The returned string includes the "%TF.FileFunction" attribute prefix and the "*%" suffix.

Parameters
aBoardis the board, needed to get the total count of copper layers.
aLayeris the layer number to create the attribute for.
Returns
The attribute, as a text string

Definition at line 81 of file pcbplot.cpp.

82{
83 wxString attrib;
84
85 switch( aLayer )
86 {
87 case F_Adhes:
88 attrib = wxT( "Glue,Top" );
89 break;
90
91 case B_Adhes:
92 attrib = wxT( "Glue,Bot" );
93 break;
94
95 case F_SilkS:
96 attrib = wxT( "Legend,Top" );
97 break;
98
99 case B_SilkS:
100 attrib = wxT( "Legend,Bot" );
101 break;
102
103 case F_Mask:
104 attrib = wxT( "Soldermask,Top" );
105 break;
106
107 case B_Mask:
108 attrib = wxT( "Soldermask,Bot" );
109 break;
110
111 case F_Paste:
112 attrib = wxT( "Paste,Top" );
113 break;
114
115 case B_Paste:
116 attrib = wxT( "Paste,Bot" );
117 break;
118
119 case Edge_Cuts:
120 // Board outline.
121 // Can be "Profile,NP" (Not Plated: usual) or "Profile,P"
122 // This last is the exception (Plated)
123 attrib = wxT( "Profile,NP" );
124 break;
125
126 case Dwgs_User:
127 attrib = wxT( "OtherDrawing,Comment" );
128 break;
129
130 case Cmts_User:
131 attrib = wxT( "Other,Comment" );
132 break;
133
134 case Eco1_User:
135 attrib = wxT( "Other,ECO1" );
136 break;
137
138 case Eco2_User:
139 attrib = wxT( "Other,ECO2" );
140 break;
141
142 case B_Fab:
143 // This is actually a assembly layer
144 attrib = wxT( "AssemblyDrawing,Bot" );
145 break;
146
147 case F_Fab:
148 // This is actually a assembly layer
149 attrib = wxT( "AssemblyDrawing,Top" );
150 break;
151
152 case B_Cu:
153 attrib.Printf( wxT( "Copper,L%d,Bot" ), aBoard->GetCopperLayerCount() );
154 break;
155
156 case F_Cu:
157 attrib = wxT( "Copper,L1,Top" );
158 break;
159
160 default:
161 if( IsCopperLayer( aLayer ) )
162 attrib.Printf( wxT( "Copper,L%d,Inr" ), aLayer+1 );
163 else
164 attrib.Printf( wxT( "Other,User" ), aLayer+1 );
165 break;
166 }
167
168 // This code adds a optional parameter: the type of copper layers.
169 // Because it is not used by Pcbnew (it can be used only by external autorouters)
170 // user do not really set this parameter.
171 // Therefore do not add it.
172 // However, this code is left here, for perhaps a future usage.
173#if 0
174 // Add the signal type of the layer, if relevant
175 if( IsCopperLayer( aLayer ) )
176 {
177 LAYER_T type = aBoard->GetLayerType( ToLAYER_ID( aLayer ) );
178
179 switch( type )
180 {
181 case LT_SIGNAL:
182 attrib += wxT( ",Signal" );
183 break;
184 case LT_POWER:
185 attrib += wxT( ",Plane" );
186 break;
187 case LT_MIXED:
188 attrib += wxT( ",Mixed" );
189 break;
190 default:
191 break; // do nothing (but avoid a warning for unhandled LAYER_T values from GCC)
192 }
193 }
194#endif
195
196 wxString fileFct;
197 fileFct.Printf( wxT( "%%TF.FileFunction,%s*%%" ), attrib );
198
199 return fileFct;
200}
LAYER_T
The allowed types of layers, same as Specctra DSN spec.
Definition: board.h:143
@ LT_POWER
Definition: board.h:146
@ LT_MIXED
Definition: board.h:147
@ LT_SIGNAL
Definition: board.h:145
LAYER_T GetLayerType(PCB_LAYER_ID aLayer) const
Return the type of the copper layer given by aLayer.
Definition: board.cpp:486
int GetCopperLayerCount() const
Definition: board.cpp:541
bool IsCopperLayer(int aLayerId)
Tests whether a layer is a copper layer.
Definition: layer_ids.h:825
@ B_Adhes
Definition: layer_ids.h:97
@ Edge_Cuts
Definition: layer_ids.h:113
@ Dwgs_User
Definition: layer_ids.h:109
@ F_Paste
Definition: layer_ids.h:101
@ Cmts_User
Definition: layer_ids.h:110
@ F_Adhes
Definition: layer_ids.h:98
@ B_Mask
Definition: layer_ids.h:106
@ B_Cu
Definition: layer_ids.h:95
@ Eco1_User
Definition: layer_ids.h:111
@ F_Mask
Definition: layer_ids.h:107
@ B_Paste
Definition: layer_ids.h:100
@ F_Fab
Definition: layer_ids.h:120
@ F_SilkS
Definition: layer_ids.h:104
@ Eco2_User
Definition: layer_ids.h:112
@ B_SilkS
Definition: layer_ids.h:103
@ F_Cu
Definition: layer_ids.h:64
@ B_Fab
Definition: layer_ids.h:119
PCB_LAYER_ID ToLAYER_ID(int aLayer)
Definition: lset.cpp:932

References B_Adhes, B_Cu, B_Fab, B_Mask, B_Paste, B_SilkS, Cmts_User, Dwgs_User, Eco1_User, Eco2_User, Edge_Cuts, F_Adhes, F_Cu, F_Fab, F_Mask, F_Paste, F_SilkS, BOARD::GetCopperLayerCount(), BOARD::GetLayerType(), IsCopperLayer(), LT_MIXED, LT_POWER, LT_SIGNAL, and ToLAYER_ID().

Referenced by AddGerberX2Attribute().

◆ GetGerberFilePolarityAttribute()

static const wxString GetGerberFilePolarityAttribute ( int  aLayer)
static

Definition at line 203 of file pcbplot.cpp.

204{
205 /* build the string %TF.FilePolarity,Positive*%
206 * or %TF.FilePolarity,Negative*%
207 * an emply string for layers which do not use a polarity
208 *
209 * The value of the .FilePolarity specifies whether the image represents the
210 * presence or absence of material.
211 * This attribute can only be used when the file represents a pattern in a material layer,
212 * e.g. copper, solder mask, legend.
213 * Together with.FileFunction it defines the role of that image in
214 * the layer structure of the PCB.
215 * Note that the .FilePolarity attribute does not change the image -
216 * no attribute does.
217 * It changes the interpretation of the image.
218 * For example, in a copper layer in positive polarity a round flash generates a copper pad.
219 * In a copper layer in negative polarity it generates a clearance.
220 * Solder mask images usually represent solder mask openings and are then negative.
221 * This may be counter-intuitive.
222 */
223 int polarity = 0;
224
225 switch( aLayer )
226 {
227 case F_Adhes:
228 case B_Adhes:
229 case F_SilkS:
230 case B_SilkS:
231 case F_Paste:
232 case B_Paste:
233 polarity = 1;
234 break;
235
236 case F_Mask:
237 case B_Mask:
238 polarity = -1;
239 break;
240
241 default:
242 if( IsCopperLayer( aLayer ) )
243 polarity = 1;
244 break;
245 }
246
247 wxString filePolarity;
248
249 if( polarity == 1 )
250 filePolarity = wxT( "%TF.FilePolarity,Positive*%" );
251 if( polarity == -1 )
252 filePolarity = wxT( "%TF.FilePolarity,Negative*%" );
253
254 return filePolarity;
255}

References B_Adhes, B_Mask, B_Paste, B_SilkS, F_Adhes, F_Mask, F_Paste, F_SilkS, and IsCopperLayer().

Referenced by AddGerberX2Attribute().

◆ GetGerberProtelExtension()

const wxString GetGerberProtelExtension ( int  aLayer)
Returns
the appropriate Gerber file extension for aLayer

Definition at line 42 of file pcbplot.cpp.

43{
44 if( IsCopperLayer( aLayer ) )
45 {
46 if( aLayer == F_Cu )
47 return wxT( "gtl" );
48 else if( aLayer == B_Cu )
49 return wxT( "gbl" );
50 else
51 return wxString::Format( wxT( "g%d" ), aLayer+1 );
52 }
53 else
54 {
55 switch( aLayer )
56 {
57 case B_Adhes: return wxT( "gba" );
58 case F_Adhes: return wxT( "gta" );
59
60 case B_Paste: return wxT( "gbp" );
61 case F_Paste: return wxT( "gtp" );
62
63 case B_SilkS: return wxT( "gbo" );
64 case F_SilkS: return wxT( "gto" );
65
66 case B_Mask: return wxT( "gbs" );
67 case F_Mask: return wxT( "gts" );
68
69 case Edge_Cuts: return wxT( "gm1" );
70
71 case Dwgs_User:
72 case Cmts_User:
73 case Eco1_User:
74 case Eco2_User:
75 default: return wxT( "gbr" );
76 }
77 }
78}
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200

References B_Adhes, B_Cu, B_Mask, B_Paste, B_SilkS, Cmts_User, Dwgs_User, Eco1_User, Eco2_User, Edge_Cuts, F_Adhes, F_Cu, F_Mask, F_Paste, F_SilkS, Format(), and IsCopperLayer().

Referenced by PCBNEW_JOBS_HANDLER::JobExportGerbers(), PLOT_CONTROLLER::OpenPlotfile(), and DIALOG_PLOT::Plot().

◆ makeStringCompatX1()

static wxString & makeStringCompatX1 ( wxString &  aText,
bool  aUseX1CompatibilityMode 
)
static

Definition at line 264 of file pcbplot.cpp.

265{
266 if( aUseX1CompatibilityMode )
267 {
268 aText.Replace( wxT( "%" ), wxEmptyString );
269 aText.Prepend( wxT( "G04 #@! " ) );
270 }
271
272 return aText;
273}

Referenced by AddGerberX2Attribute(), and AddGerberX2Header().