KiCad PCB EDA Suite
pcbplot.cpp File Reference
#include <fctsys.h>
#include <plotter.h>
#include <confirm.h>
#include <pcb_edit_frame.h>
#include <pcbplot.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>
#include <gbr_metadata.h>

Go to the source code of this file.

Functions

const wxString GetGerberProtelExtension (LAYER_NUM aLayer)
 Function GetGerberProtelExtension. More...
 
const wxString GetGerberFileFunctionAttribute (const BOARD *aBoard, LAYER_NUM aLayer)
 Function GetGerberFileFunctionAttribute Returns the "file function" attribute for aLayer, as defined in the Gerber file format specification J1 (chapter 5). More...
 
static const wxString GetGerberFilePolarityAttribute (LAYER_NUM aLayer)
 
static wxString & makeStringCompatX1 (wxString &aText, bool aUseX1CompatibilityMode)
 
void AddGerberX2Header (PLOTTER *aPlotter, const BOARD *aBoard, bool aUseX1CompatibilityMode)
 Calculates 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. More...
 
void AddGerberX2Attribute (PLOTTER *aPlotter, const BOARD *aBoard, LAYER_NUM aLayer, bool aUseX1CompatibilityMode)
 Calculates 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. More...
 
void BuildPlotFileName (wxFileName *aFilename, const wxString &aOutputDir, const wxString &aSuffix, const wxString &aExtension)
 Function BuildPlotFileName (helper function) Complete a plot filename: forces the output directory, add a suffix to the name and sets the specified extension the suffix is usually the layer name replaces not allowed chars in suffix by '_'. More...
 

Function Documentation

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

Calculates 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
aPlotter= the current plotter.
aBoard= the board, needed to extract some info
aLayer= the layer number to create the attribute for
aUseX1CompatibilityMode= 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 361 of file pcbplot.cpp.

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

Referenced by BRDITEMS_PLOTTER::SetLayerSet(), and StartPlotBoard().

363 {
364  AddGerberX2Header( aPlotter, aBoard, aUseX1CompatibilityMode );
365 
366  wxString text;
367 
368  // Add the TF.FileFunction
369  text = GetGerberFileFunctionAttribute( aBoard, aLayer );
370  aPlotter->AddLineToHeader( makeStringCompatX1( text, aUseX1CompatibilityMode ) );
371 
372  // Add the TF.FilePolarity (for layers which support that)
373  text = GetGerberFilePolarityAttribute( aLayer );
374 
375  if( !text.IsEmpty() )
376  aPlotter->AddLineToHeader( makeStringCompatX1( text, aUseX1CompatibilityMode ) );
377 }
void AddGerberX2Header(PLOTTER *aPlotter, const BOARD *aBoard, bool aUseX1CompatibilityMode)
Calculates some X2 attributes, as defined in the Gerber file format specification J4 (chapter 5) and ...
Definition: pcbplot.cpp:280
void AddLineToHeader(const wxString &aExtraString)
Function AddLineToHeader Add a line to the list of free lines to print at the beginning of the file...
Definition: plotter.h:176
static const wxString GetGerberFilePolarityAttribute(LAYER_NUM aLayer)
Definition: pcbplot.cpp:209
static wxString & makeStringCompatX1(wxString &aText, bool aUseX1CompatibilityMode)
Definition: pcbplot.cpp:268
const wxString GetGerberFileFunctionAttribute(const BOARD *aBoard, LAYER_NUM aLayer)
Function GetGerberFileFunctionAttribute Returns the "file function" attribute for aLayer...
Definition: pcbplot.cpp:89
void AddGerberX2Header ( PLOTTER aPlotter,
const BOARD aBoard,
bool  aUseX1CompatibilityMode = false 
)

Calculates 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
aPlotter= the current plotter.
aBoard= the board, needed to extract some info
aUseX1CompatibilityMode= 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 280 of file pcbplot.cpp.

References PLOTTER::AddLineToHeader(), GbrMakeProjectGUIDfromString(), BOARD::GetAuxOrigin(), GetBuildVersion(), GetChars(), BOARD::GetFileName(), BOARD::GetPlotOptions(), PCB_PLOT_PARAMS::GetUseAuxOrigin(), makeStringCompatX1(), wxPoint::x, and wxPoint::y.

Referenced by AddGerberX2Attribute(), GERBER_WRITER::createDrillFile(), and BRDITEMS_PLOTTER::SetLayerSet().

282 {
283  wxString text;
284 
285  // Creates the TF,.GenerationSoftware. Format is:
286  // %TF,.GenerationSoftware,<vendor>,<application name>[,<application version>]*%
287  text.Printf( wxT( "%%TF.GenerationSoftware,KiCad,Pcbnew,%s*%%" ), GetBuildVersion() );
288  aPlotter->AddLineToHeader( makeStringCompatX1( text, aUseX1CompatibilityMode ) );
289 
290  // creates the TF.CreationDate ext:
291  // The attribute value must conform to the full version of the ISO 8601
292  // date and time format, including time and time zone. Note that this is
293  // the date the Gerber file was effectively created,
294  // not the time the project of PCB was started
295  wxDateTime date( wxDateTime::GetTimeNow() );
296  // Date format: see http://www.cplusplus.com/reference/ctime/strftime
297  wxString msg = date.Format( wxT( "%z" ) ); // Extract the time zone offset
298  // The time zone offset format is + (or -) mm or hhmm (mm = number of minutes, hh = number of hours)
299  // we want +(or -) hh:mm
300  if( msg.Len() > 3 )
301  msg.insert( 3, ":", 1 ),
302  text.Printf( wxT( "%%TF.CreationDate,%s%s*%%" ), GetChars( date.FormatISOCombined() ), GetChars( msg ) );
303  aPlotter->AddLineToHeader( makeStringCompatX1( text, aUseX1CompatibilityMode ) );
304 
305  // Creates the TF,.ProjectId. Format is (from Gerber file format doc):
306  // %TF.ProjectId,<project id>,<project GUID>,<revision id>*%
307  // <project id> is the name of the project, restricted to basic ASCII symbols only,
308  // Rem: <project id> accepts only ASCII 7 code (only basic ASCII codes are allowed in gerber files).
309  // and comma not accepted
310  // All illegal chars will be replaced by underscore
311  //
312  // <project GUID> is a string which is an unique id of a project.
313  // However Kicad does not handle such a project GUID, so it is built from the board name
314  wxFileName fn = aBoard->GetFileName();
315  msg = fn.GetFullName();
316 
317  // Build a <project GUID>, from the board name
318  wxString guid = GbrMakeProjectGUIDfromString( msg );
319 
320  // build the <project id> string: this is the board short filename (without ext)
321  // and all non ASCII chars and comma are replaced by '_'
322  msg = fn.GetName();
323  msg.Replace( wxT( "," ), wxT( "_" ) );
324 
325  // build the <rec> string. All non ASCII chars and comma are replaced by '_'
326  wxString rev = ((BOARD*)aBoard)->GetTitleBlock().GetRevision();
327  rev.Replace( wxT( "," ), wxT( "_" ) );
328 
329  if( rev.IsEmpty() )
330  rev = wxT( "rev?" );
331 
332  text.Printf( wxT( "%%TF.ProjectId,%s,%s,%s*%%" ), msg.ToAscii(), GetChars( guid ), rev.ToAscii() );
333  aPlotter->AddLineToHeader( makeStringCompatX1( text, aUseX1CompatibilityMode ) );
334 
335  // Add the TF.SameCoordinates, that specify all gerber files uses the same
336  // origin and orientation, and the registration between files is OK.
337  // The parameter of TF.SameCoordinates is a string that is common
338  // to all files using the same registration and has no special meaning:
339  // this is just a key
340  // Because there is no mirroring/rotation in Kicad, only the plot offset origin
341  // can create incorrect registration.
342  // So we create a key from plot offset options.
343  // and therefore for a given board, all Gerber files having the same key have the same
344  // plot origin and use the same registration
345  //
346  // Currently the key is "Original" when using absolute Pcbnew coordinates,
347  // and te PY ans PY position od auxiliary axis, when using it.
348  // Please, if absolute Pcbnew coordinates, one day, are set by user, change the way
349  // the key is built to ensure file only using the *same* axis have the same key.
350  wxString registration_id = "Original";
351  wxPoint auxOrigin = aBoard->GetAuxOrigin();
352 
353  if( aBoard->GetPlotOptions().GetUseAuxOrigin() && auxOrigin.x && auxOrigin.y )
354  registration_id.Printf( "PX%xPY%x", auxOrigin.x, auxOrigin.y );
355 
356  text.Printf( "%%TF.SameCoordinates,%s*%%", registration_id.GetData() );
357  aPlotter->AddLineToHeader( makeStringCompatX1( text, aUseX1CompatibilityMode ) );
358 }
bool GetUseAuxOrigin() const
void AddLineToHeader(const wxString &aExtraString)
Function AddLineToHeader Add a line to the list of free lines to print at the beginning of the file...
Definition: plotter.h:176
wxString GetBuildVersion()
Function GetBuildVersion Return the build version string.
wxString GbrMakeProjectGUIDfromString(wxString &aText)
A helper function to build a project GUID using format RFC4122 Version 1 or 4 from the project name...
const wxString & GetFileName() const
Definition: class_board.h:237
const PCB_PLOT_PARAMS & GetPlotOptions() const
Definition: class_board.h:556
const wxPoint & GetAuxOrigin() const
Definition: class_board.h:349
static const wxChar * GetChars(const wxString &s)
Function GetChars returns a wxChar* to the actual wxChar* data within a wxString, and is helpful for ...
Definition: macros.h:92
static wxString & makeStringCompatX1(wxString &aText, bool aUseX1CompatibilityMode)
Definition: pcbplot.cpp:268
Class BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:170
void BuildPlotFileName ( wxFileName *  aFilename,
const wxString &  aOutputDir,
const wxString &  aSuffix,
const wxString &  aExtension 
)

Function BuildPlotFileName (helper function) Complete a plot filename: forces the output directory, add a suffix to the name and sets the specified extension the suffix is usually the layer name replaces not allowed chars in suffix by '_'.

Parameters
aFilename= the wxFileName to initialize Contains the base filename
aOutputDir= the path
aSuffix= the suffix to add to the base filename
aExtension= the file extension

Definition at line 380 of file pcbplot.cpp.

Referenced by DIALOG_EXPORT_SVG::ExportSVGFile(), PLOT_CONTROLLER::OpenPlotfile(), DIALOG_PLOT::Plot(), and BRDITEMS_PLOTTER::SetLayerSet().

382 {
383  // aFilename contains the base filename only (without path and extension)
384  // when calling this function.
385  // It is expected to be a valid filename (this is usually the board filename)
386  aFilename->SetPath( aOutputDir );
387 
388  // Set the file extension
389  aFilename->SetExt( aExtension );
390 
391  // remove leading and trailing spaces if any from the suffix, if
392  // something survives add it to the name;
393  // also the suffix can contain some not allowed chars in filename (/ \ . :),
394  // so change them to underscore
395  // Remember it can be called from a python script, so the illegal chars
396  // have to be filtered here.
397  wxString suffix = aSuffix;
398  suffix.Trim( true );
399  suffix.Trim( false );
400 
401  wxString badchars = wxFileName::GetForbiddenChars(wxPATH_DOS);
402  badchars.Append( '%' );
403 
404  for( unsigned ii = 0; ii < badchars.Len(); ii++ )
405  suffix.Replace( badchars[ii], wxT("_") );
406 
407  if( !suffix.IsEmpty() )
408  aFilename->SetName( aFilename->GetName() + wxT( "-" ) + suffix );
409 }
const wxString GetGerberFileFunctionAttribute ( const BOARD aBoard,
LAYER_NUM  aLayer 
)

Function GetGerberFileFunctionAttribute Returns 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
aBoard= the board, needed to get the total count of copper layers
aLayer= the layer number to create the attribute for
Returns
The attribute, as a text string

Definition at line 89 of file pcbplot.cpp.

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, GetChars(), BOARD::GetCopperLayerCount(), BOARD::GetLayerType(), IsCopperLayer(), LT_MIXED, LT_POWER, LT_SIGNAL, and ToLAYER_ID().

Referenced by AddGerberX2Attribute(), and BRDITEMS_PLOTTER::SetLayerSet().

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

Definition at line 209 of file pcbplot.cpp.

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

Referenced by AddGerberX2Attribute().

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

Function GetGerberProtelExtension.

Returns
the appropriate Gerber file extension for aLayer used by Protel, and still sometimes in use (although the official Gerber Ext is now .gbr)

Definition at line 48 of file pcbplot.cpp.

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 PLOT_CONTROLLER::OpenPlotfile(), DIALOG_PLOT::Plot(), and BRDITEMS_PLOTTER::SetLayerSet().

49 {
50  if( IsCopperLayer( aLayer ) )
51  {
52  if( aLayer == F_Cu )
53  return wxT( "gtl" );
54  else if( aLayer == B_Cu )
55  return wxT( "gbl" );
56  else
57  {
58  return wxString::Format( wxT( "g%d" ), aLayer+1 );
59  }
60  }
61  else
62  {
63  switch( aLayer )
64  {
65  case B_Adhes: return wxT( "gba" );
66  case F_Adhes: return wxT( "gta" );
67 
68  case B_Paste: return wxT( "gbp" );
69  case F_Paste: return wxT( "gtp" );
70 
71  case B_SilkS: return wxT( "gbo" );
72  case F_SilkS: return wxT( "gto" );
73 
74  case B_Mask: return wxT( "gbs" );
75  case F_Mask: return wxT( "gts" );
76 
77  case Edge_Cuts: return wxT( "gm1" );
78 
79  case Dwgs_User:
80  case Cmts_User:
81  case Eco1_User:
82  case Eco2_User:
83  default: return wxT( "gbr" );
84  }
85  }
86 }
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
bool IsCopperLayer(LAYER_NUM aLayerId)
Function IsCopperLayer tests whether a layer is a copper layer.
static wxString& makeStringCompatX1 ( wxString &  aText,
bool  aUseX1CompatibilityMode 
)
static

Definition at line 268 of file pcbplot.cpp.

Referenced by AddGerberX2Attribute(), and AddGerberX2Header().

269 {
270  if( aUseX1CompatibilityMode )
271  {
272  aText.Replace( "%", "" );
273  aText.Prepend( "G04 #@! " );
274  }
275 
276  return aText;
277 }