KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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-2023 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 PCB_TEXT;
56class PCB_TRACK;
57class FOOTPRINT;
58class PCB_GROUP;
59class PCB_TARGET;
60class PCB_VIA;
61class ZONE;
62class FP_3DMODEL;
64struct 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( std::move( 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
120 bool IsValidBoardHeader();
121
122private:
125 inline int getNetCode( int aNetCode )
126 {
127 if( ( aNetCode >= 0 ) && ( aNetCode < (int) m_netCodes.size() ) )
128 return m_netCodes[aNetCode];
129
130 return aNetCode;
131 }
132
141 void pushValueIntoMap( int aIndex, int aValue );
142
149 void init();
150
151 void checkpoint();
152
160 void createOldLayerMapping( std::unordered_map< std::string, std::string >& aMap );
161
166 void skipCurrent();
167
168 void parseHeader();
169 void parseGeneralSection();
170 void parsePAGE_INFO();
171 void parseTITLE_BLOCK();
172
173 void parseLayers();
174 void parseLayer( LAYER* aLayer );
175
176 void parseBoardStackup();
177
178 void parseSetup();
179 void parseDefaults( BOARD_DESIGN_SETTINGS& aSettings );
180 void parseDefaultTextDims( BOARD_DESIGN_SETTINGS& aSettings, int aLayer );
181 void parseNETINFO_ITEM();
182 void parseNETCLASS();
183
185
187 PCB_TEXT* parsePCB_TEXT( BOARD_ITEM* aParent );
188 void parsePCB_TEXT_effects( PCB_TEXT* aText );
192
193 // Parse a footprint, but do not replace PARSE_ERROR with FUTURE_FORMAT_ERROR automatically.
194 FOOTPRINT* parseFOOTPRINT_unchecked( wxArrayString* aInitialComments = nullptr );
195
196 PAD* parsePAD( FOOTPRINT* aParent = nullptr );
197
198 // Parse only the (option ...) inside a pad description
199 bool parsePAD_option( PAD* aPad );
200
201 PCB_ARC* parseARC();
206 BOARD* parseBOARD();
207 void parseGROUP( BOARD_ITEM* aParent );
208
209 // Parse a board, but do not replace PARSE_ERROR with FUTURE_FORMAT_ERROR automatically.
211
220 template<class T, class M>
221 T lookUpLayer( const M& aMap );
222
231
240
252
253 void parseXY( int* aX, int* aY );
254
255 std::pair<wxString, wxString> parseBoardProperty();
256
265
272 void parseEDA_TEXT( EDA_TEXT* aText );
273
281
283
292 int parseBoardUnits();
293
294 int parseBoardUnits( const char* aExpected );
295
296 inline int parseBoardUnits( PCB_KEYS_T::T aToken )
297 {
298 return parseBoardUnits( GetTokenText( aToken ) );
299 }
300
301 inline int parseInt()
302 {
303 return (int)strtol( CurText(), nullptr, 10 );
304 }
305
306 inline int parseInt( const char* aExpected )
307 {
308 NeedNUMBER( aExpected );
309 return parseInt();
310 }
311
312 inline long parseHex()
313 {
314 NextTok();
315 return strtol( CurText(), nullptr, 16 );
316 }
317
318 bool parseBool();
319
320 /*
321 * @return if m_appendToExisting, returns new KIID(), otherwise returns CurStr() as KIID.
322 */
324
329 void resolveGroups( BOARD_ITEM* aParent );
330
331 typedef std::unordered_map< std::string, PCB_LAYER_ID > LAYER_ID_MAP;
332 typedef std::unordered_map< std::string, LSET > LSET_MAP;
333 typedef std::unordered_map< wxString, KIID > KIID_MAP;
334
336 using TIMEOUT = std::chrono::milliseconds;
337
339 using CLOCK = std::chrono::steady_clock;
340
342 using TIME_PT = std::chrono::time_point<CLOCK>;
343
347 std::set<wxString> m_undefinedLayers;
348 std::vector<int> m_netCodes;
352
355
358
361 unsigned m_lineCount;
362
363 // Group membership info refers to other Uuids in the file.
364 // We don't want to rely on group declarations being last in the file, so
365 // we store info about the group declarations here during parsing and then resolve
366 // them into BOARD_ITEM* after we've parsed the rest of the file.
368 {
370 wxString name;
371 bool locked;
373 std::vector<KIID> memberUuids;
374 };
375
376 std::vector<GROUP_INFO> m_groupInfos;
377
378 std::function<bool( wxString aTitle, int aIcon, wxString aMsg, wxString aAction )> m_queryUserCallback;
379};
380
381
382#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:77
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:271
A mix-in class (via multiple inheritance) that handles texts such as labels, parts,...
Definition: eda_text.h:80
Definition: kiid.h:49
An abstract class from which implementation specific LINE_READERs may be derived to read single lines...
Definition: richio.h:93
LSET is a set of PCB_LAYER_IDs.
Definition: layer_ids.h:552
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.
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:589
int parseInt(const char *aExpected)
Definition: pcb_parser.h:306
LSET_MAP m_layerMasks
map layer names to their masks
Definition: pcb_parser.h:346
void init()
Clear and re-establish m_layerMap with the default layer names.
Definition: pcb_parser.cpp:85
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:448
int m_requiredVersion
set to the KiCad format version this board requires
Definition: pcb_parser.h:350
int parseBoardUnits()
Parse the current token as an ASCII numeric string with possible leading whitespace into a double pre...
Definition: pcb_parser.cpp:186
bool IsTooRecent()
Return whether a version number, if any was parsed, was too recent.
Definition: pcb_parser.h:105
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
TIME_PT m_lastProgressTime
for progress reporting
Definition: pcb_parser.h:360
PCB_VIA * parsePCB_VIA()
std::pair< wxString, wxString > parseBoardProperty()
Definition: pcb_parser.cpp:370
std::chrono::milliseconds TIMEOUT
The clock used for the timestamp (guaranteed to be monotonic).
Definition: pcb_parser.h:338
void parseTEARDROP_PARAMETERS(TEARDROP_PARAMETERS *tdParams)
Definition: pcb_parser.cpp:385
std::unordered_map< std::string, LSET > LSET_MAP
Definition: pcb_parser.h:332
std::vector< GROUP_INFO > m_groupInfos
Definition: pcb_parser.h:376
void parseBoardStackup()
BOARD * parseBOARD()
Definition: pcb_parser.cpp:815
BOARD_ITEM * Parse()
Definition: pcb_parser.cpp:765
BOARD * parseBOARD_unchecked()
Definition: pcb_parser.cpp:831
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:125
bool m_appendToExisting
reading into an existing board; reset UUIDs
Definition: pcb_parser.h:351
std::set< wxString > m_undefinedLayers
set of layers not defined in layers section
Definition: pcb_parser.h:347
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:273
bool m_tooRecent
true if version parses as later than supported
Definition: pcb_parser.h:349
void parseHeader()
bool m_showLegacySegmentZoneWarning
Definition: pcb_parser.h:356
PCB_TARGET * parsePCB_TARGET()
FP_3DMODEL * parse3DModel()
Definition: pcb_parser.cpp:639
void parseGROUP(BOARD_ITEM *aParent)
KIID_MAP m_resetKIIDMap
Definition: pcb_parser.h:354
void checkpoint()
Definition: pcb_parser.cpp:131
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:253
PCB_DIMENSION_BASE * parseDIMENSION(BOARD_ITEM *aParent)
PCB_ARC * parseARC()
std::unordered_map< std::string, PCB_LAYER_ID > LAYER_ID_MAP
Definition: pcb_parser.h:331
std::function< bool(wxString aTitle, int aIcon, wxString aMsg, wxString aAction)> m_queryUserCallback
Definition: pcb_parser.h:378
KIID CurStrToKIID()
std::chrono::time_point< CLOCK > TIME_PT
Definition: pcb_parser.h:342
std::unordered_map< wxString, KIID > KIID_MAP
The type of progress bar timeout.
Definition: pcb_parser.h:333
PCB_BITMAP * parsePCB_BITMAP(BOARD_ITEM *aParent)
wxString GetRequiredVersion()
Return a string representing the version of KiCad required to open this file.
Definition: pcb_parser.cpp:229
bool parseBool()
Definition: pcb_parser.cpp:214
void parsePCB_TEXT_effects(PCB_TEXT *aText)
bool m_showLegacy5ZoneWarning
Definition: pcb_parser.h:357
void parseLayers()
PCB_SHAPE * parsePCB_SHAPE(BOARD_ITEM *aParent)
FOOTPRINT * parseFOOTPRINT(wxArrayString *aInitialComments=nullptr)
PCB_TEXT * parsePCB_TEXT(BOARD_ITEM *aParent)
PCB_TEXTBOX * parsePCB_TEXTBOX(BOARD_ITEM *aParent)
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:359
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:348
int parseInt()
Definition: pcb_parser.h:301
std::chrono::steady_clock CLOCK
The type of the time stamps.
Definition: pcb_parser.h:341
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:344
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:361
void skipCurrent()
Skip the current token level, i.e search for the RIGHT parenthesis which closes the current descripti...
Definition: pcb_parser.cpp:153
void parsePAGE_INFO()
long parseHex()
Definition: pcb_parser.h:312
LAYER_ID_MAP m_layerIndices
map layer name to it's index
Definition: pcb_parser.h:345
void parseNETCLASS()
bool IsValidBoardHeader()
Partially parse the input and check if it matches expected header.
Definition: pcb_parser.cpp:745
void pushValueIntoMap(int aIndex, int aValue)
Add aValue value in netcode mapping (m_netCodes) at aIndex.
Definition: pcb_parser.cpp:174
int parseBoardUnits(PCB_KEYS_T::T aToken)
Definition: pcb_parser.h:296
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...
TEARDROP_PARAMETARS is a helper class to handle parameters needed to build teardrops for a board thes...
Handle a list of polygons defining a copper zone.
Definition: zone.h:72
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:60
STL namespace.
Container to hold information pertinent to a layer of a BOARD.
Definition: board.h:161
std::vector< KIID > memberUuids
Definition: pcb_parser.h:373
BOARD_ITEM * parent
Definition: pcb_parser.h:369