KiCad PCB EDA Suite
gbr_metadata.cpp File Reference

helper functions to handle the gerber metadata in files, related to the netlist info and aperture attribute. More...

#include <fctsys.h>
#include <plot_auxiliary_data.h>

Go to the source code of this file.

Macros

#define NO_NET_NAME   wxT( "N/C" )
 
#define NO_PAD_NAME   wxT( "" )
 

Functions

wxString FormatStringFromGerber (const wxString &aString)
 This helper function make the inverse conversion of formatStringToGerber() It converts a "normalized" gerber string and convert it to a 16 bits sequence unicode. More...
 
std::string formatStringToGerber (const wxString &aString)
 This helper function "normalize" aString and convert it to a Gerber std::string Normalisation means convert any code > 0x7F and unautorized code to a hexadecimal 16 bits sequence unicode unautorized codes are ',' '*' '' '\'. More...
 
bool FormatNetAttribute (std::string &aPrintedText, std::string &aLastNetAttributes, GBR_NETLIST_METADATA *aData, bool &aClearPreviousAttributes)
 Generates the string to print to a gerber file, to set a net attribute for a graphic object. More...
 

Detailed Description

helper functions to handle the gerber metadata in files, related to the netlist info and aperture attribute.

Definition in file gbr_metadata.cpp.

Macro Definition Documentation

#define NO_NET_NAME   wxT( "N/C" )

Definition at line 208 of file gbr_metadata.cpp.

Referenced by FormatNetAttribute().

#define NO_PAD_NAME   wxT( "" )

Definition at line 209 of file gbr_metadata.cpp.

Referenced by FormatNetAttribute().

Function Documentation

bool FormatNetAttribute ( std::string &  aPrintedText,
std::string &  aLastNetAttributes,
GBR_NETLIST_METADATA aData,
bool &  aClearPreviousAttributes 
)

Generates the string to print to a gerber file, to set a net attribute for a graphic object.

Parameters
aPrintedTextis the string to print
aLastNetAttributesis the current full set of attributes.
aDatais the GBR_NETLIST_METADATA associated to the graphic object (can be NULL if no associated metadata, and aClearPreviousAttributes will be set to false)
aClearPreviousAttributesreturns true if the full set of attributes must be deleted from file before adding new attribute (happens when a previous attribute does not exist no more).
Returns
false if nothing can be done (GBR_NETLIST_METADATA has GBR_APERTURE_ATTRIB_NONE, and true if OK if the new attribute(s) is the same as current attribute(s), aPrintedText will be empty

Definition at line 211 of file gbr_metadata.cpp.

References formatStringToGerber(), GBR_NETLIST_METADATA::GBR_NETINFO_CMP, GBR_NETLIST_METADATA::GBR_NETINFO_NET, GBR_NETLIST_METADATA::GBR_NETINFO_PAD, GBR_NETLIST_METADATA::GBR_NETINFO_UNSPECIFIED, GBR_NETLIST_METADATA::m_Cmpref, GBR_NETLIST_METADATA::m_NetAttribType, GBR_NETLIST_METADATA::m_Netname, GBR_NETLIST_METADATA::m_NotInNet, GBR_NETLIST_METADATA::m_Padname, NO_NET_NAME, and NO_PAD_NAME.

Referenced by GERBER_PLOTTER::formatNetAttribute().

213 {
214  aClearPreviousAttributes = false;
215 
216  // print a Gerber net attribute record.
217  // it is added to the object attributes dictionnary
218  // On file, only modified or new attributes are printed.
219  if( aData == NULL )
220  return false;
221 
222  std::string pad_attribute_string;
223  std::string net_attribute_string;
224  std::string cmp_attribute_string;
225 
227  return false; // idle command: do nothing
228 
230  {
231  // print info associated to a flashed pad (cmpref, pad name)
232  // example: %TO.P,R5,3*%
233  pad_attribute_string = "%TO.P,";
234  pad_attribute_string += formatStringToGerber( aData->m_Cmpref ) + ",";
235 
236  if( aData->m_Padname.IsEmpty() )
237  // Happens for "mechanical" or never connected pads
238  pad_attribute_string += formatStringToGerber( NO_PAD_NAME );
239  else
240  pad_attribute_string += formatStringToGerber( aData->m_Padname );
241 
242  pad_attribute_string += "*%\n";
243  }
244 
246  {
247  // print info associated to a net
248  // example: %TO.N,Clk3*%
249  net_attribute_string = "%TO.N,";
250 
251  if( aData->m_Netname.IsEmpty() )
252  {
253  if( aData->m_NotInNet )
254  {
255  // Happens for not connectable pads: mechanical pads
256  // and pads with no padname/num
257  // In this case the net name must be left empty
258  }
259  else
260  {
261  // Happens for not connected pads: use a normalized
262  // dummy name
263  net_attribute_string += formatStringToGerber( NO_NET_NAME );
264  }
265  }
266  else
267  net_attribute_string += formatStringToGerber( aData->m_Netname );
268 
269  net_attribute_string += "*%\n";
270  }
271 
274  {
275  // print info associated to a footprint
276  // example: %TO.C,R2*%
277  // Because GBR_NETINFO_PAD option already contains this info, it is not
278  // created here for a GBR_NETINFO_PAD attribute
279  cmp_attribute_string = "%TO.C,";
280  cmp_attribute_string += formatStringToGerber( aData->m_Cmpref ) + "*%\n";
281  }
282 
283  // the full list of requested attributes:
284  std::string full_attribute_string = pad_attribute_string + net_attribute_string
285  + cmp_attribute_string;
286  // the short list of requested attributes
287  // (only modified or new attributes are stored here):
288  std::string short_attribute_string;
289 
290  if( aLastNetAttributes != full_attribute_string )
291  {
292  // first, remove no more existing attributes.
293  // Because in Kicad the full attribute list is evaluated for each object,
294  // the entire dictionnary is cleared
295  bool clearDict = false;
296 
297  if( aLastNetAttributes.find( "%TO.P," ) != std::string::npos )
298  {
299  if( pad_attribute_string.empty() ) // No more this attribute
300  clearDict = true;
301  else if( aLastNetAttributes.find( pad_attribute_string )
302  == std::string::npos ) // This attribute has changed
303  short_attribute_string += pad_attribute_string;
304  }
305  else // New attribute
306  short_attribute_string += pad_attribute_string;
307 
308  if( aLastNetAttributes.find( "%TO.N," ) != std::string::npos )
309  {
310  if( net_attribute_string.empty() ) // No more this attribute
311  clearDict = true;
312  else if( aLastNetAttributes.find( net_attribute_string )
313  == std::string::npos ) // This attribute has changed
314  short_attribute_string += net_attribute_string;
315  }
316  else // New attribute
317  short_attribute_string += net_attribute_string;
318 
319  if( aLastNetAttributes.find( "%TO.C," ) != std::string::npos )
320  {
321  if( cmp_attribute_string.empty() ) // No more this attribute
322  clearDict = true;
323  else if( aLastNetAttributes.find( cmp_attribute_string )
324  == std::string::npos ) // This attribute has changed
325  short_attribute_string += cmp_attribute_string;
326  }
327  else // New attribute
328  short_attribute_string += cmp_attribute_string;
329 
330  aClearPreviousAttributes = clearDict;
331 
332  aLastNetAttributes = full_attribute_string;
333 
334  if( clearDict )
335  aPrintedText = full_attribute_string;
336  else
337  aPrintedText = short_attribute_string;
338  }
339 
340  return true;
341 }
#define NO_NET_NAME
print info associated to a component (TO.C attribute)
wxString m_Cmpref
the component reference parent of the data
std::string formatStringToGerber(const wxString &aString)
This helper function "normalize" aString and convert it to a Gerber std::string Normalisation means c...
wxString m_Padname
for a flashed pad: the pad name ((TO.P attribute)
#define NO_PAD_NAME
wxString m_Netname
for items associated to a net: the netname
print info associated to a flashed pad (TO.P attribute)
bool m_NotInNet
true if a pad of a footprint cannot be connected (for instance a mechanical NPTH, ot a not named pad)...
print info associated to a net (TO.N attribute)
int m_NetAttribType
the type of net info (used to define the gerber string to create)
wxString FormatStringFromGerber ( const wxString &  aString)

This helper function make the inverse conversion of formatStringToGerber() It converts a "normalized" gerber string and convert it to a 16 bits sequence unicode.

Parameters
aString= the wxString compliant with a gerber string format
Returns
a wxString (unicode 16) from the gerber string

Definition at line 125 of file gbr_metadata.cpp.

Referenced by GERBER_JOBFILE_READER::parseTJLayerString().

126 {
127  // make the inverse conversion of formatStringToGerber()
128  // It converts a "normalized" gerber string and convert it to a 16 bits sequence unicode
129  // and return a wxString (unicode 16) from the gerber string
130  wxString txt;
131 
132  for( unsigned ii = 0; ii < aString.Length(); ++ii )
133  {
134  unsigned code = aString[ii];
135 
136  if( code == '\\' )
137  {
138  // Convert 4 hexadecimal digits to a 16 bit unicode
139  // (Gerber allows only 4 hexadecimal digits)
140  long value = 0;
141 
142  for( int jj = 0; jj < 4; jj++ )
143  {
144  value <<= 4;
145  code = aString[++ii];
146  // Very basic conversion, but it expects a valid gerber file
147  int hexa = (code <= '9' ? code - '0' : code - 'A' + 10) & 0xF;
148  value += hexa;
149  }
150 
151  txt.Append( wxChar( value ) );
152  }
153  else
154  txt.Append( aString[ii] );
155  }
156 
157  return txt;
158 }
std::string formatStringToGerber ( const wxString &  aString)

This helper function "normalize" aString and convert it to a Gerber std::string Normalisation means convert any code > 0x7F and unautorized code to a hexadecimal 16 bits sequence unicode unautorized codes are ',' '*' '' '\'.

Parameters
aString= the wxString to convert
Returns
a std::string (ASCII7 coded) compliant with a gerber string

Definition at line 161 of file gbr_metadata.cpp.

Referenced by GERBER_JOBFILE_WRITER::CreateJobFile(), and FormatNetAttribute().

162 {
163  /* format string means convert any code > 0x7F and unautorized code to a hexadecimal
164  * 16 bits sequence unicode
165  * unautorized codes are ',' '*' '%' '\'
166  */
167  std::string txt;
168 
169  txt.reserve( aString.Length() );
170 
171  for( unsigned ii = 0; ii < aString.Length(); ++ii )
172  {
173  unsigned code = aString[ii];
174  bool convert = false;
175 
176  switch( code )
177  {
178  case '\\':
179  case '%':
180  case '*':
181  case ',':
182  convert = true;
183  break;
184 
185  default:
186  break;
187  }
188 
189  if( convert || code > 0x7F )
190  {
191  txt += '\\';
192 
193  // Convert code to 4 hexadecimal digit
194  // (Gerber allows only 4 hexadecimal digit)
195  char hexa[32];
196  sprintf( hexa,"%4.4X", code & 0xFFFF);
197  txt += hexa;
198  }
199  else
200  txt += char( code );
201  }
202 
203  return txt;
204 }