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 /*
93  * parse a TF command and fill m_Prms by the parameters found.
94  * aFile = a FILE* ptr to the current Gerber file.
95  * buff = the buffer containing current Gerber data (GERBER_BUFZ size)
96  * text = a pointer to the first char to read in Gerber data
97  */
98 bool X2_ATTRIBUTE::ParseAttribCmd( FILE* aFile, char *aBuffer, int aBuffSize, char* &aText )
99 {
100  bool ok = true;
101  wxString data;
102 
103  for( ; ; )
104  {
105  while( *aText )
106  {
107  switch( *aText )
108  {
109  case '%': // end of command
110  return ok; // success completion
111 
112  case ' ':
113  case '\r':
114  case '\n':
115  aText++;
116  break;
117 
118  case '*': // End of block
119  m_Prms.Add( data );
120  data.Empty();
121  aText++;
122  break;
123 
124  case ',': // End of parameter
125  aText++;
126  m_Prms.Add( data );
127  data.Empty();
128  break;
129 
130  default:
131  data.Append( *aText );
132  aText++;
133  break;
134  }
135  }
136 
137  // end of current line, read another one.
138  if( aBuffer )
139  {
140  if( fgets( aBuffer, aBuffSize, aFile ) == NULL )
141  {
142  // end of file
143  ok = false;
144  break;
145  }
146 
147  aText = aBuffer;
148  }
149  else
150  return ok;
151  }
152 
153  return ok;
154 }
155 
156 /*
157  * class X2_ATTRIBUTE_FILEFUNCTION ( from %TF.FileFunction in Gerber file)
158  * Example file function:
159  * %TF.FileFunction,Copper,L1,Top*%
160  * - Type. Such as copper, solder mask etc.
161  * - Position. Specifies where the file appears in the PCB layer structure.
162  * Corresponding position substring:
163  * Copper layer: L1, L2, L3...to indicate the layer position followed by Top, Inr or
164  * Bot. L1 is always the top copper layer. E.g. L2,Inr.
165  * Extra layer, e.g. solder mask: Top or Bot - defines the attachment of the layer.
166  * Drill/rout layer: E.g. 1,4 - where 1 is the start and 4 is the end copper layer. The
167  * pair 1,4 defines the span of the drill/rout file
168  * Optional index. This can be used in instances where for example there are two solder
169  * masks on the same side. The index counts from the PCB surface outwards.
170  */
172  : X2_ATTRIBUTE()
173 {
174  m_Prms = aAttributeBase.GetPrms();
175  m_z_order = 0;
176 
177  //ensure at least 5 parameters
178  while( GetPrmCount() < 5 )
179  m_Prms.Add( wxEmptyString );
180 
181  set_Z_Order();
182 }
183 
185 {
186  // the type of layer (Copper , Soldermask ... )
187  return m_Prms.Item( 1 );
188 }
189 
191 {
192  // the brd layer identifier: Ln (for Copper type) or Top, Bot
193  return m_Prms.Item( 2 );
194 }
195 
197 {
198  if( IsCopper() )
199  // the brd layer identifier: Top, Bot, Inr
200  return m_Prms.Item( 3 );
201  else
202  // the brd layer identifier: Top, Bot ( same as GetBrdLayerId() )
203  return m_Prms.Item( 2 );
204 }
205 
207 {
208  if( IsCopper() )
209  return m_Prms.Item( 4 );
210  else
211  return m_Prms.Item( 3 );
212 }
213 
214 
216 {
217  // the filefunction label, if any
218  return GetFileType().IsSameAs( wxT( "Copper" ), false );
219 }
220 
221 // Initialize the z order priority of the current file, from its attributes
222 // this priority is the order of layers from top to bottom to draw/display gerber images
223 // Stack up is( from external copper layer to external)
224 // copper, then solder paste, then solder mask, then silk screen.
225 // and global stackup is Front (top) layers then internal copper layers then Back (bottom) layers
227 {
228  m_z_order = -100; // low level
229  m_z_sub_order = 0;
230 
231  if( IsCopper() )
232  {
233  // Copper layer: the priority is the layer Id
234  m_z_order = 0;
235  wxString num = GetBrdLayerId().Mid( 1 );
236  long lnum;
237  if( num.ToLong( &lnum ) )
238  m_z_sub_order = -lnum;
239  }
240 
241  if( GetFileType().IsSameAs( wxT( "Paste" ), false ) )
242  {
243  // solder paste layer: the priority is top then bottom
244  m_z_order = 1; // for top
245 
246  if( GetBrdLayerId().IsSameAs( wxT( "Bot" ), false ) )
247  m_z_order = -m_z_order;
248  }
249 
250  if( GetFileType().IsSameAs( wxT( "Soldermask" ), false ) )
251  {
252  // solder mask layer: the priority is top then bottom
253  m_z_order = 2; // for top
254 
255  if( GetBrdLayerId().IsSameAs( wxT( "Bot" ), false ) )
256  m_z_order = -m_z_order;
257  }
258 
259  if( GetFileType().IsSameAs( wxT( "Legend" ), false ) )
260  {
261  // Silk screen layer: the priority is top then bottom
262  m_z_order = 3; // for top
263 
264  if( GetFileType().IsSameAs( wxT( "Legend" ), false ) )
265 
266  if( GetBrdLayerId().IsSameAs( wxT( "Bot" ), false ) )
267  m_z_order = -m_z_order;
268  }
269 }
270 
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.
bool ParseAttribCmd(FILE *aFile, char *aBuffer, int aBuffSize, char *&aText)
parse a TF command terminated with a % and fill m_Prms by the parameters found.
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
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 '.