KiCad PCB EDA Suite
class_X2_gerber_attributes.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) 2010-2014 Jean-Pierre Charras jp.charras at wanadoo.fr
5  * Copyright (C) 1992-2014 KiCad Developers, see change_log.txt for contributors.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
29 /*
30  * Manage the gerber extensions (attributes) in the new X2 version
31  * only few extensions are handled
32  * See http://www.ucamco.com/files/downloads/file/81/the_gerber_file_format_specification.pdf
33  *
34  * gerber attributes in the new X2 version look like:
35  * %TF.FileFunction,Copper,L1,Top*%
36  *
37  * Currently:
38  * .FileFunction .FileFunction Identifies the file's function in the PCB.
39  * Other Standard Attributes, not yet used in Gerbview:
40  * .Part Identifies the part the file represents, e.g. a single PCB
41  * .MD5 Sets the MD5 file signature or checksum.
42  */
43 
44 #include <wx/log.h>
46 
47 /*
48  * class X2_ATTRIBUTE
49  * The attribute value consists of a number of substrings separated by a comma
50  */
51 
53 {
54 }
55 
57 {
58 }
59 
60 /* return the attribute name (for instance .FileFunction)
61  * which is given by TF command.
62  */
63 const wxString& X2_ATTRIBUTE::GetAttribute()
64 {
65  return m_Prms.Item( 0 );
66 }
67 
68 /* return a parameter
69  * aIdx = the index of the parameter
70  * aIdx = 0 is the parameter read after the TF function
71  * (the same as GetAttribute())
72  */
73 const wxString& X2_ATTRIBUTE::GetPrm( int aIdx)
74 {
75  static const wxString dummy;
76 
77  if( GetPrmCount() > aIdx && aIdx >= 0 )
78  return m_Prms.Item( aIdx );
79 
80  return dummy;
81 }
82 
83 // Debug function: pring using wxLogMessage the list of parameters
85 {
86  wxLogMessage( wxT("prms count %d"), GetPrmCount() );
87 
88  for( int ii = 0; ii < GetPrmCount(); ii++ )
89  wxLogMessage( m_Prms.Item( ii ) );
90 }
91 
92 bool X2_ATTRIBUTE::ParseAttribCmd( FILE* aFile, char *aBuffer, int aBuffSize, char* &aText,
93  int& aLineNum )
94 {
95  // parse a TF command and fill m_Prms by the parameters found.
96  // the "%TF" (start of command) is already read by the caller
97 
98  bool ok = true;
99  wxString data;
100 
101  for( ; ; )
102  {
103  while( *aText )
104  {
105  switch( *aText )
106  {
107  case '%': // end of command
108  return ok; // success completion
109 
110  case ' ':
111  case '\r':
112  case '\n':
113  aText++;
114  break;
115 
116  case '*': // End of block
117  m_Prms.Add( data );
118  data.Empty();
119  aText++;
120  break;
121 
122  case ',': // End of parameter (separator)
123  aText++;
124  m_Prms.Add( data );
125  data.Empty();
126  break;
127 
128  default:
129  data.Append( *aText );
130  aText++;
131  break;
132  }
133  }
134 
135  // end of current line, read another one.
136  if( aBuffer )
137  {
138  if( fgets( aBuffer, aBuffSize, aFile ) == NULL )
139  {
140  // end of file
141  ok = false;
142  break;
143  }
144 
145  aLineNum++;
146  aText = aBuffer;
147  }
148  else
149  return ok;
150  }
151 
152  return ok;
153 }
154 
155 /*
156  * class X2_ATTRIBUTE_FILEFUNCTION ( from %TF.FileFunction in Gerber file)
157  * Example file function:
158  * %TF.FileFunction,Copper,L1,Top*%
159  * - Type. Such as copper, solder mask etc.
160  * - Position. Specifies where the file appears in the PCB layer structure.
161  * Corresponding position substring:
162  * Copper layer: L1, L2, L3...to indicate the layer position followed by Top, Inr or
163  * Bot. L1 is always the top copper layer. E.g. L2,Inr.
164  * Extra layer, e.g. solder mask: Top or Bot - defines the attachment of the layer.
165  * Drill/rout layer: E.g. 1,4 - where 1 is the start and 4 is the end copper layer. The
166  * pair 1,4 defines the span of the drill/rout file
167  * Optional index. This can be used in instances where for example there are two solder
168  * masks on the same side. The index counts from the PCB surface outwards.
169  */
171  : X2_ATTRIBUTE()
172 {
173  m_Prms = aAttributeBase.GetPrms();
174  m_z_order = 0;
175 
176  // ensure at least 7 parameters exist.
177  while( GetPrmCount() < 7 )
178  m_Prms.Add( wxEmptyString );
179 
180  set_Z_Order();
181 }
182 
184 {
185  // the type of layer (Copper, Soldermask ... )
186  return m_Prms.Item( 1 );
187 }
188 
190 {
191  // the brd layer identifier: Ln (for Copper type) or Top, Bot
192  return m_Prms.Item( 2 );
193 }
194 
196 {
197  // the layer pair identifiers, for drill files, i.e.
198  // with m_Prms.Item( 1 ) = "Plated" or "NonPlated"
199  wxString lpair = m_Prms.Item( 2 ) + ',' + m_Prms.Item( 3 );
200  return lpair;
201 }
202 
204 {
205  if( IsCopper() )
206  // the brd layer identifier: Top, Bot, Inr
207  return m_Prms.Item( 3 );
208  else
209  // the brd layer identifier: Top, Bot ( same as GetBrdLayerId() )
210  return m_Prms.Item( 2 );
211 }
212 
213 
215 {
216  if( IsCopper() )
217  return m_Prms.Item( 4 );
218  else
219  return m_Prms.Item( 3 );
220 }
221 
222 
224 {
225  // Only for drill files: the Layer Pair type (PTH, NPTH, Blind or Buried)
226  return m_Prms.Item( 4 );
227 }
228 
229 
231 {
232  // Only for drill files: the drill/routing type(Drill, Route, Mixed)
233  return m_Prms.Item( 5 );
234 }
235 
236 
238 {
239  // the filefunction label, if any
240  return GetFileType().IsSameAs( wxT( "Copper" ), false );
241 }
242 
243 
245 {
246  // the filefunction label, if any
247  return GetFileType().IsSameAs( wxT( "Plated" ), false )
248  || GetFileType().IsSameAs( wxT( "NonPlated" ), false );
249 }
250 
251 
252 // Initialize the z order priority of the current file, from its attributes
253 // this priority is the order of layers from top to bottom to draw/display gerber images
254 // Stack up is( from external copper layer to external)
255 // copper, then solder paste, then solder mask, then silk screen.
256 // and global stackup is Front (top) layers then internal copper layers then Back (bottom) layers
258 {
259  m_z_order = -100; // low level
260  m_z_sub_order = 0;
261 
262  if( IsCopper() )
263  {
264  // Copper layer: the priority is the layer Id
265  m_z_order = 0;
266  wxString num = GetBrdLayerId().Mid( 1 );
267  long lnum;
268  if( num.ToLong( &lnum ) )
269  m_z_sub_order = -lnum;
270  }
271 
272  if( GetFileType().IsSameAs( wxT( "Paste" ), false ) )
273  {
274  // solder paste layer: the priority is top then bottom
275  m_z_order = 1; // for top
276 
277  if( GetBrdLayerId().IsSameAs( wxT( "Bot" ), false ) )
278  m_z_order = -m_z_order;
279  }
280 
281  if( GetFileType().IsSameAs( wxT( "Soldermask" ), false ) )
282  {
283  // solder mask layer: the priority is top then bottom
284  m_z_order = 2; // for top
285 
286  if( GetBrdLayerId().IsSameAs( wxT( "Bot" ), false ) )
287  m_z_order = -m_z_order;
288  }
289 
290  if( GetFileType().IsSameAs( wxT( "Legend" ), false ) )
291  {
292  // Silk screen layer: the priority is top then bottom
293  m_z_order = 3; // for top
294 
295  if( GetFileType().IsSameAs( wxT( "Legend" ), false ) )
296 
297  if( GetBrdLayerId().IsSameAs( wxT( "Bot" ), false ) )
298  m_z_order = -m_z_order;
299  }
300 }
301 
const wxString & GetFileType()
the type of layer (Copper, Soldermask ... )
class X2_ATTRIBUTE The attribute value consists of a number of substrings separated by a comma ...
const wxString & GetPrm(int aIdx)
bool IsCopper()
return true if the filefunction type is "Copper"
wxArrayString & GetPrms()
void set_Z_Order()
Initialize the z order priority of the current file, from its attributes.
X2_ATTRIBUTE_FILEFUNCTION(X2_ATTRIBUTE &aAttributeBase)
const wxString & GetLabel()
the filefunction label, if any
const wxString & GetBrdLayerId()
the brd layer identifier: Ln, only for Copper type or Top, Bot for other types
static LIB_PART * dummy()
Used when a LIB_PART is not found in library to draw a dummy shape This component is a 400 mils squar...
const wxString & GetBrdLayerSide()
the brd layer Pos: Top, Bot, Inr same as GetBrdLayerId() for non copper type
bool ParseAttribCmd(FILE *aFile, char *aBuffer, int aBuffSize, char *&aText, int &aLineNum)
parse a TF command terminated with a % and fill m_Prms by the parameters found.
void DbgListPrms()
Debug function: pring using wxLogMessage le list of parameters.
const wxString & GetAttribute()
wxArrayString m_Prms
the list of parameters (after TF) in gbr file the first one is the attribute name, if starting by '.