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-2022 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 <core/wx_stl_compat.h>
34#include <hashtables.h>
35#include <layer_ids.h> // PCB_LAYER_ID
36#include <pcb_lexer.h>
37#include <kiid.h>
38#include <math/box2.h>
39
40#include <chrono>
41#include <unordered_map>
42
43
44class PCB_ARC;
45class BOARD;
46class BOARD_ITEM;
48class PAD;
51class PCB_SHAPE;
52class PCB_BITMAP;
53class EDA_TEXT;
54class FP_SHAPE;
55class FP_TEXT;
56class PCB_TEXT;
57class PCB_TRACK;
58class FOOTPRINT;
59class PCB_GROUP;
60class PCB_TARGET;
61class PCB_VIA;
62class ZONE;
63class FP_3DMODEL;
65struct LAYER;
67
68
73class PCB_PARSER : public PCB_LEXER
74{
75public:
76 PCB_PARSER( LINE_READER* aReader, BOARD* aAppendToMe,
77 std::function<bool( wxString, int, wxString, wxString )>* aQueryUserCallback,
78 PROGRESS_REPORTER* aProgressReporter = nullptr, unsigned aLineCount = 0 ) :
79 PCB_LEXER( aReader ),
80 m_board( aAppendToMe ),
81 m_appendToExisting( aAppendToMe != nullptr ),
82 m_progressReporter( aProgressReporter ),
83 m_lastProgressTime( std::chrono::steady_clock::now() ),
84 m_lineCount( aLineCount ),
85 m_queryUserCallback( aQueryUserCallback )
86 {
87 init();
88 }
89
90 // ~PCB_PARSER() {}
91
93
100 FOOTPRINT* parseFOOTPRINT( wxArrayString* aInitialComments = nullptr );
101
106 {
107 return m_tooRecent;
108 }
109
114 wxString GetRequiredVersion();
115
116private:
119 inline int getNetCode( int aNetCode )
120 {
121 if( ( aNetCode >= 0 ) && ( aNetCode < (int) m_netCodes.size() ) )
122 return m_netCodes[aNetCode];
123
124 return aNetCode;
125 }
126
135 void pushValueIntoMap( int aIndex, int aValue );
136
143 void init();
144
145 void checkpoint();
146
154 void createOldLayerMapping( std::unordered_map< std::string, std::string >& aMap );
155
160 void skipCurrent();
161
162 void parseHeader();
163 void parseGeneralSection();
164 void parsePAGE_INFO();
165 void parseTITLE_BLOCK();
166
167 void parseLayers();
168 void parseLayer( LAYER* aLayer );
169
170 void parseBoardStackup();
171
172 void parseSetup();
173 void parseDefaults( BOARD_DESIGN_SETTINGS& aSettings );
174 void parseDefaultTextDims( BOARD_DESIGN_SETTINGS& aSettings, int aLayer );
175 void parseNETINFO_ITEM();
176 void parseNETCLASS();
177
182 PCB_DIMENSION_BASE* parseDIMENSION( BOARD_ITEM* aParent, bool aInFP );
183
184 // Parse a footprint, but do not replace PARSE_ERROR with FUTURE_FORMAT_ERROR automatically.
185 FOOTPRINT* parseFOOTPRINT_unchecked( wxArrayString* aInitialComments = nullptr );
186
190 PAD* parsePAD( FOOTPRINT* aParent = nullptr );
191
192 // Parse only the (option ...) inside a pad description
193 bool parsePAD_option( PAD* aPad );
194
195 PCB_ARC* parseARC();
200 BOARD* parseBOARD();
201 void parseGROUP( BOARD_ITEM* aParent );
202
203 // Parse a board, but do not replace PARSE_ERROR with FUTURE_FORMAT_ERROR automatically.
205
214 template<class T, class M>
215 T lookUpLayer( const M& aMap );
216
225
234
246
247 void parseXY( int* aX, int* aY );
248
249 std::pair<wxString, wxString> parseProperty();
250
259
266 void parseEDA_TEXT( EDA_TEXT* aText );
267
275
277
286 int parseBoardUnits();
287
288 int parseBoardUnits( const char* aExpected );
289
290 inline int parseBoardUnits( PCB_KEYS_T::T aToken )
291 {
292 return parseBoardUnits( GetTokenText( aToken ) );
293 }
294
295 inline int parseInt()
296 {
297 return (int)strtol( CurText(), nullptr, 10 );
298 }
299
300 inline int parseInt( const char* aExpected )
301 {
302 NeedNUMBER( aExpected );
303 return parseInt();
304 }
305
306 inline long parseHex()
307 {
308 NextTok();
309 return strtol( CurText(), nullptr, 16 );
310 }
311
312 bool parseBool();
313
314 /*
315 * @return if m_appendToExisting, returns new KIID(), otherwise returns CurStr() as KIID.
316 */
318
323 void resolveGroups( BOARD_ITEM* aParent );
324
325 typedef std::unordered_map< std::string, PCB_LAYER_ID > LAYER_ID_MAP;
326 typedef std::unordered_map< std::string, LSET > LSET_MAP;
327 typedef std::unordered_map< wxString, KIID > KIID_MAP;
328
330 using TIMEOUT = std::chrono::milliseconds;
331
333 using CLOCK = std::chrono::steady_clock;
334
336 using TIME_PT = std::chrono::time_point<CLOCK>;
337
341 std::set<wxString> m_undefinedLayers;
342 std::vector<int> m_netCodes;
346
349
352
355 unsigned m_lineCount;
356
357 // Group membership info refers to other Uuids in the file.
358 // We don't want to rely on group declarations being last in the file, so
359 // we store info about the group declarations here during parsing and then resolve
360 // them into BOARD_ITEM* after we've parsed the rest of the file.
362 {
364 wxString name;
365 bool locked;
367 std::vector<KIID> memberUuids;
368 };
369
370 std::vector<GROUP_INFO> m_groupInfos;
371
372 std::function<bool( wxString aTitle, int aIcon, wxString aMsg, wxString aAction )>* m_queryUserCallback;
373};
374
375
376#endif // _PCBNEW_PARSER_H_
Container for design settings for a BOARD object.
Abstract interface for BOARD_ITEMs capable of storing other items inside.
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:50
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:265
A mix-in class (via multiple inheritance) that handles texts such as labels, parts,...
Definition: eda_text.h:72
Definition: kiid.h:47
An abstract class from which implementation specific LINE_READERs may be derived to read single lines...
Definition: richio.h:81
LSET is a set of PCB_LAYER_IDs.
Definition: layer_ids.h:530
Definition: pad.h:58
Object to handle a bitmap image that can be inserted in a PCB.
Definition: pcb_bitmap.h:42
Abstract dimension API.
Definition: pcb_dimension.h:96
A set of BOARD_ITEMs (i.e., without duplicates).
Definition: pcb_group.h:51
Read a Pcbnew s-expression formatted LINE_READER object and returns the appropriate BOARD_ITEM object...
Definition: pcb_parser.h:74
bool parsePAD_option(PAD *aPad)
PCB_TRACK * parsePCB_TRACK()
PCB_LAYER_ID parseBoardItemLayer()
Parse the layer definition of a BOARD_ITEM object.
void parseRenderCache(EDA_TEXT *text)
Parse the render cache for any object derived from EDA_TEXT.
Definition: pcb_parser.cpp:525
PCB_SHAPE * parsePCB_SHAPE()
int parseInt(const char *aExpected)
Definition: pcb_parser.h:300
LSET_MAP m_layerMasks
map layer names to their masks
Definition: pcb_parser.h:340
void init()
Clear and re-establish m_layerMap with the default layer names.
Definition: pcb_parser.cpp:77
PAD * parsePAD(FOOTPRINT *aParent=nullptr)
ZONE * parseZONE(BOARD_ITEM_CONTAINER *aParent)
FOOTPRINT * parseFOOTPRINT_unchecked(wxArrayString *aInitialComments=nullptr)
void parseEDA_TEXT(EDA_TEXT *aText)
Parse the common settings for any object derived from EDA_TEXT.
Definition: pcb_parser.cpp:384
PCB_TEXTBOX * parsePCB_TEXTBOX()
int m_requiredVersion
set to the KiCad format version this board requires
Definition: pcb_parser.h:344
int parseBoardUnits()
Parse the current token as an ASCII numeric string with possible leading whitespace into a double pre...
Definition: pcb_parser.cpp:178
std::pair< wxString, wxString > parseProperty()
Definition: pcb_parser.cpp:369
bool IsTooRecent()
Return whether a version number, if any was parsed, was too recent.
Definition: pcb_parser.h:105
TIME_PT m_lastProgressTime
for progress reporting
Definition: pcb_parser.h:354
PCB_VIA * parsePCB_VIA()
std::chrono::milliseconds TIMEOUT
The clock used for the timestamp (guaranteed to be monotonic).
Definition: pcb_parser.h:332
FP_SHAPE * parseFP_SHAPE()
std::unordered_map< std::string, LSET > LSET_MAP
Definition: pcb_parser.h:326
std::vector< GROUP_INFO > m_groupInfos
Definition: pcb_parser.h:370
void parseBoardStackup()
BOARD * parseBOARD()
Definition: pcb_parser.cpp:731
BOARD_ITEM * Parse()
Definition: pcb_parser.cpp:681
BOARD * parseBOARD_unchecked()
Definition: pcb_parser.cpp:747
void parseSetup()
int getNetCode(int aNetCode)
< Convert net code using the mapping table if available, otherwise returns unchanged net code if < 0 ...
Definition: pcb_parser.h:119
PCB_TEXT * parsePCB_TEXT()
bool m_appendToExisting
reading into an existing board; reset UUIDs
Definition: pcb_parser.h:345
std::set< wxString > m_undefinedLayers
set of layers not defined in layers section
Definition: pcb_parser.h:341
void parseDefaults(BOARD_DESIGN_SETTINGS &aSettings)
void parseOutlinePoints(SHAPE_LINE_CHAIN &aPoly)
Parses possible outline points and stores them into aPoly.
Definition: pcb_parser.cpp:272
bool m_tooRecent
true if version parses as later than supported
Definition: pcb_parser.h:343
std::function< bool(wxString aTitle, int aIcon, wxString aMsg, wxString aAction)> * m_queryUserCallback
Definition: pcb_parser.h:372
void parseHeader()
FP_TEXTBOX * parseFP_TEXTBOX()
bool m_showLegacySegmentZoneWarning
Definition: pcb_parser.h:350
PCB_TARGET * parsePCB_TARGET()
FP_3DMODEL * parse3DModel()
Definition: pcb_parser.cpp:575
void parseGROUP(BOARD_ITEM *aParent)
KIID_MAP m_resetKIIDMap
Definition: pcb_parser.h:348
void checkpoint()
Definition: pcb_parser.cpp:123
LSET parseBoardItemLayersAsMask()
Parse the layers definition of a BOARD_ITEM object.
VECTOR2I parseXY()
Parse a coordinate pair (xy X Y) in board units (mm).
Definition: pcb_parser.cpp:252
PCB_ARC * parseARC()
std::unordered_map< std::string, PCB_LAYER_ID > LAYER_ID_MAP
Definition: pcb_parser.h:325
KIID CurStrToKIID()
std::chrono::time_point< CLOCK > TIME_PT
Definition: pcb_parser.h:336
std::unordered_map< wxString, KIID > KIID_MAP
The type of progress bar timeout.
Definition: pcb_parser.h:327
PCB_PARSER(LINE_READER *aReader, BOARD *aAppendToMe, std::function< bool(wxString, int, wxString, wxString)> *aQueryUserCallback, PROGRESS_REPORTER *aProgressReporter=nullptr, unsigned aLineCount=0)
Definition: pcb_parser.h:76
FP_TEXT * parseFP_TEXT()
PCB_BITMAP * parsePCB_BITMAP(BOARD_ITEM *aParent)
PCB_DIMENSION_BASE * parseDIMENSION(BOARD_ITEM *aParent, bool aInFP)
wxString GetRequiredVersion()
Return a string representing the version of KiCad required to open this file.
Definition: pcb_parser.cpp:228
bool parseBool()
Definition: pcb_parser.cpp:213
bool m_showLegacy5ZoneWarning
Definition: pcb_parser.h:351
void parseLayers()
FOOTPRINT * parseFOOTPRINT(wxArrayString *aInitialComments=nullptr)
void resolveGroups(BOARD_ITEM *aParent)
Called after parsing a footprint definition or board to build the group membership lists.
void parseNETINFO_ITEM()
PROGRESS_REPORTER * m_progressReporter
optional; may be nullptr
Definition: pcb_parser.h:353
T lookUpLayer(const M &aMap)
Parse the current token for the layer definition of a BOARD_ITEM object.
std::vector< int > m_netCodes
net codes mapping for boards being loaded
Definition: pcb_parser.h:342
int parseInt()
Definition: pcb_parser.h:295
std::chrono::steady_clock CLOCK
The type of the time stamps.
Definition: pcb_parser.h:335
void parseGeneralSection()
void createOldLayerMapping(std::unordered_map< std::string, std::string > &aMap)
Create a mapping from the (short-lived) bug where layer names were translated.
BOARD * m_board
Definition: pcb_parser.h:338
void parseDefaultTextDims(BOARD_DESIGN_SETTINGS &aSettings, int aLayer)
void parseLayer(LAYER *aLayer)
void parseTITLE_BLOCK()
unsigned m_lineCount
for progress reporting
Definition: pcb_parser.h:355
void skipCurrent()
Skip the current token level, i.e search for the RIGHT parenthesis which closes the current descripti...
Definition: pcb_parser.cpp:145
void parsePAGE_INFO()
long parseHex()
Definition: pcb_parser.h:306
LAYER_ID_MAP m_layerIndices
map layer name to it's index
Definition: pcb_parser.h:339
void parseNETCLASS()
void pushValueIntoMap(int aIndex, int aValue)
Add aValue value in netcode mapping (m_netCodes) at aIndex.
Definition: pcb_parser.cpp:166
int parseBoardUnits(PCB_KEYS_T::T aToken)
Definition: pcb_parser.h:290
A progress reporter interface for use in multi-threaded environments.
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
Handle a list of polygons defining a copper zone.
Definition: zone.h:57
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:59
const char * GetTokenText(T aTok)
The DSN namespace and returns the C string representing a SPECCTRA_DB::keyword.
Definition: specctra.cpp:70
Definition: bitmap.cpp:64
Container to hold information pertinent to a layer of a BOARD.
Definition: board.h:156
std::vector< KIID > memberUuids
Definition: pcb_parser.h:367
BOARD_ITEM * parent
Definition: pcb_parser.h:363