KiCad PCB EDA Suite
pcb_parser.h
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) 2012 CERN
5  * Copyright (C) 2012-2018 KiCad Developers, see AUTHORS.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 
30 #ifndef _PCBNEW_PARSER_H_
31 #define _PCBNEW_PARSER_H_
32 
33 #include <pcb_lexer.h>
34 #include <hashtables.h>
35 #include <layers_id_colors_and_visibility.h> // PCB_LAYER_ID
36 #include <common.h> // KiROUND
37 #include <convert_to_biu.h> // IU_PER_MM
38 
39 #include <unordered_map>
40 
41 
42 class BOARD;
43 class BOARD_ITEM;
44 class D_PAD;
45 class DIMENSION;
46 class DRAWSEGMENT;
47 class EDA_TEXT;
48 class EDGE_MODULE;
49 class TEXTE_MODULE;
50 class TEXTE_PCB;
51 class TRACK;
52 class MODULE;
53 class PCB_TARGET;
54 class VIA;
55 class ZONE_CONTAINER;
56 class MODULE_3D_SETTINGS;
57 struct LAYER;
58 
59 
65 class PCB_PARSER : public PCB_LEXER
66 {
67  typedef std::unordered_map< std::string, PCB_LAYER_ID > LAYER_ID_MAP;
68  typedef std::unordered_map< std::string, LSET > LSET_MAP;
69 
71  LAYER_ID_MAP m_layerIndices;
72  LSET_MAP m_layerMasks;
73  std::vector<int> m_netCodes;
74  bool m_tooRecent;
76 
79  inline int getNetCode( int aNetCode )
80  {
81  if( ( aNetCode >= 0 ) && ( aNetCode < (int) m_netCodes.size() ) )
82  return m_netCodes[aNetCode];
83 
84  return aNetCode;
85  }
86 
94  void pushValueIntoMap( int aIndex, int aValue );
95 
102  void init();
103 
104  void parseHeader();
105  void parseGeneralSection();
106  void parsePAGE_INFO();
107  void parseTITLE_BLOCK();
108 
109  void parseLayers();
110  void parseLayer( LAYER* aLayer );
111 
112  void parseSetup();
113  void parseNETINFO_ITEM();
114  void parseNETCLASS();
115 
120  DRAWSEGMENT* parseDRAWSEGMENT( bool aAllowCirclesZeroWidth = false );
123 
128  MODULE* parseMODULE_unchecked( wxArrayString* aInitialComments = 0 );
131  D_PAD* parseD_PAD( MODULE* aParent = NULL );
132  // Parse only the (option ...) inside a pad description
133  bool parseD_PAD_option( D_PAD* aPad );
134  TRACK* parseTRACK();
135  VIA* parseVIA();
138  BOARD* parseBOARD();
139 
145 
146 
157  template<class T, class M>
158  T lookUpLayer( const M& aMap );
159 
169 
179 
191  wxPoint parseXY();
192 
193  void parseXY( int* aX, int* aY );
194 
202  void parseEDA_TEXT( EDA_TEXT* aText );
203 
205 
214  double parseDouble();
215 
216  inline double parseDouble( const char* aExpected )
217  {
218  NeedNUMBER( aExpected );
219  return parseDouble();
220  }
221 
222  inline double parseDouble( PCB_KEYS_T::T aToken )
223  {
224  return parseDouble( GetTokenText( aToken ) );
225  }
226 
227  inline int parseBoardUnits()
228  {
229  // There should be no major rounding issues here, since the values in
230  // the file are in mm and get converted to nano-meters.
231  // See test program tools/test-nm-biu-to-ascii-mm-round-tripping.cpp
232  // to confirm or experiment. Use a similar strategy in both places, here
233  // and in the test program. Make that program with:
234  // $ make test-nm-biu-to-ascii-mm-round-tripping
235  return KiROUND( parseDouble() * IU_PER_MM );
236  }
237 
238  inline int parseBoardUnits( const char* aExpected )
239  {
240  // Use here KiROUND, not KIROUND (see comments about them)
241  // when having a function as argument, because it will be called twice
242  // with KIROUND
243  return KiROUND( parseDouble( aExpected ) * IU_PER_MM );
244  }
245 
246  inline int parseBoardUnits( PCB_KEYS_T::T aToken )
247  {
248  return parseBoardUnits( GetTokenText( aToken ) );
249  }
250 
251  inline int parseInt()
252  {
253  return (int)strtol( CurText(), NULL, 10 );
254  }
255 
256  inline int parseInt( const char* aExpected )
257  {
258  NeedNUMBER( aExpected );
259  return parseInt();
260  }
261 
262  inline long parseHex()
263  {
264  NextTok();
265  return strtol( CurText(), NULL, 16 );
266  }
267 
268  bool parseBool();
269 
274  int parseVersion();
275 
276 public:
277 
278  PCB_PARSER( LINE_READER* aReader = NULL ) :
279  PCB_LEXER( aReader ),
280  m_board( 0 )
281  {
282  init();
283  }
284 
285  // ~PCB_PARSER() {}
286 
294  {
295  LINE_READER* ret = PopReader();
296  PushReader( aReader );
297  return ret;
298  }
299 
300  void SetBoard( BOARD* aBoard )
301  {
302  init();
303  m_board = aBoard;
304  }
305 
306  BOARD_ITEM* Parse();
313  MODULE* parseMODULE( wxArrayString* aInitialComments = 0 );
314 
318  bool IsTooRecent()
319  {
320  return m_tooRecent;
321  }
322 
327  wxString GetRequiredVersion();
328 
329 };
330 
331 
332 #endif // _PCBNEW_PARSER_H_
void parseHeader()
Definition: pcb_parser.cpp:587
int parseVersion()
Parse a format version tag like (version 20160417) return the version.
Definition: pcb_parser.cpp:169
Class ZONE_CONTAINER handles a list of polygons defining a copper zone.
Definition: class_zone.h:60
Class LINE_READER is an abstract class from which implementation specific LINE_READERs may be derived...
Definition: richio.h:81
DRAWSEGMENT * parseDRAWSEGMENT(bool aAllowCirclesZeroWidth=false)
Read a DRAWSEGMENT description.
LSET parseBoardItemLayersAsMask()
Function parseBoardItemLayersAsMask parses the layers definition of a BOARD_ITEM object.
static int KiROUND(double v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: common.h:120
Class BOARD_ITEM is a base class for any item which can be embedded within the BOARD container class...
TRACK * parseTRACK()
VIA * parseVIA()
Class PCB_PARSER reads a Pcbnew s-expression formatted LINE_READER object and returns the appropriate...
Definition: pcb_parser.h:65
wxPoint parseXY()
Function parseXY parses a coordinate pair (xy X Y) in board units (mm).
Definition: pcb_parser.cpp:206
void parseNETINFO_ITEM()
BOARD * parseBOARD()
Definition: pcb_parser.cpp:481
bool parseD_PAD_option(D_PAD *aPad)
void parseEDA_TEXT(EDA_TEXT *aText)
Function parseEDA_TEXT parses the common settings for any object derived from EDA_TEXT.
Definition: pcb_parser.cpp:238
DIMENSION * parseDIMENSION()
bool m_tooRecent
true if version parses as later than supported
Definition: pcb_parser.h:74
void parseTITLE_BLOCK()
Definition: pcb_parser.cpp:725
int parseBoardUnits(PCB_KEYS_T::T aToken)
Definition: pcb_parser.h:246
int parseBoardUnits()
Definition: pcb_parser.h:227
void parseLayers()
Definition: pcb_parser.cpp:850
std::unordered_map< std::string, LSET > LSET_MAP
Definition: pcb_parser.h:68
std::unordered_map< std::string, PCB_LAYER_ID > LAYER_ID_MAP
Definition: pcb_parser.h:67
Class EDA_TEXT is a mix-in class (via multiple inheritance) that handles texts such as labels...
Definition: eda_text.h:127
void parsePAGE_INFO()
Definition: pcb_parser.cpp:669
bool parseBool()
Definition: pcb_parser.cpp:154
PCB_LAYER_ID
A quick note on layer IDs:
wxString GetRequiredVersion()
Return a string representing the version of kicad required to open this file.
Definition: pcb_parser.cpp:182
Class LSET is a set of PCB_LAYER_IDs.
void SetBoard(BOARD *aBoard)
Definition: pcb_parser.h:300
PCB_PARSER(LINE_READER *aReader=NULL)
Definition: pcb_parser.h:278
MODULE * parseMODULE_unchecked(wxArrayString *aInitialComments=0)
Function parseMODULE_unchecked Parse a module, but do not replace PARSE_ERROR with FUTURE_FORMAT_ERRO...
PCB_TARGET * parsePCB_TARGET()
PCB_LAYER_ID parseBoardItemLayer()
Function parseBoardItemLayer parses the layer definition of a BOARD_ITEM object.
Definition: pcb_parser.cpp:996
TEXTE_PCB * parseTEXTE_PCB()
BOARD_ITEM * Parse()
Definition: pcb_parser.cpp:442
void parseNETCLASS()
LINE_READER * SetLineReader(LINE_READER *aReader)
Function SetLineReader sets aLineReader into the parser, and returns the previous one...
Definition: pcb_parser.h:293
int parseInt(const char *aExpected)
Definition: pcb_parser.h:256
BOARD * parseBOARD_unchecked()
Function parseBOARD_unchecked Parse a module, but do not replace PARSE_ERROR with FUTURE_FORMAT_ERROR...
Definition: pcb_parser.cpp:497
ZONE_CONTAINER * parseZONE_CONTAINER()
void parseLayer(LAYER *aLayer)
Definition: pcb_parser.cpp:808
int m_requiredVersion
set to the KiCad format version this board requires
Definition: pcb_parser.h:75
bool IsTooRecent()
Return whether a version number, if any was parsed, was too recent.
Definition: pcb_parser.h:318
EDGE_MODULE * parseEDGE_MODULE()
Board layer functions and definitions.
Class LAYER holds information pertinent to a layer of a BOARD.
Definition: class_board.h:86
Class BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:170
std::vector< int > m_netCodes
net codes mapping for boards being loaded
Definition: pcb_parser.h:73
int getNetCode(int aNetCode)
Converts net code using the mapping table if available, otherwise returns unchanged net code if < 0 o...
Definition: pcb_parser.h:79
BOARD * m_board
Definition: pcb_parser.h:70
void parseGeneralSection()
Definition: pcb_parser.cpp:623
void init()
Function init clears and re-establishes m_layerMap with the default layer names.
Definition: pcb_parser.cpp:56
void pushValueIntoMap(int aIndex, int aValue)
function pushValueIntoMap Add aValue value in netcode mapping (m_netCodes) at index aIndex ensure the...
Definition: pcb_parser.cpp:113
double parseDouble(PCB_KEYS_T::T aToken)
Definition: pcb_parser.h:222
The common library.
double parseDouble()
Function parseDouble parses the current token as an ASCII numeric string with possible leading whites...
Definition: pcb_parser.cpp:124
const char * GetTokenText(T aTok)
Function GetTokenText is in the DSN namespace and returns the C string representing a SPECCTRA_DB::ke...
Definition: specctra.cpp:69
LAYER_ID_MAP m_layerIndices
map layer name to it&#39;s index
Definition: pcb_parser.h:71
TEXTE_MODULE * parseTEXTE_MODULE()
D_PAD * parseD_PAD(MODULE *aParent=NULL)
MODULE * parseMODULE(wxArrayString *aInitialComments=0)
Function parseMODULE.
long parseHex()
Definition: pcb_parser.h:262
int parseInt()
Definition: pcb_parser.h:251
LSET_MAP m_layerMasks
map layer names to their masks
Definition: pcb_parser.h:72
Class DIMENSION.
void parseSetup()
int parseBoardUnits(const char *aExpected)
Definition: pcb_parser.h:238
T lookUpLayer(const M &aMap)
Function lookUpLayer parses the current token for the layer definition of a BOARD_ITEM object...
Definition: pcb_parser.cpp:963
double parseDouble(const char *aExpected)
Definition: pcb_parser.h:216
MODULE_3D_SETTINGS * parse3DModel()
Definition: pcb_parser.cpp:350