KiCad PCB EDA Suite
Loading...
Searching...
No Matches
pcb_io_kicad_sexpr_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 The 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, see <https://www.gnu.org/licenses/>.
19 */
20
25
26#ifndef _PCBNEW_PARSER_H_
27#define _PCBNEW_PARSER_H_
28
29#include <eda_units.h>
30#include <core/wx_stl_compat.h>
31#include <hashtables.h>
32#include <lib_id.h>
33#include <layer_ids.h> // PCB_LAYER_ID
34#include <lset.h>
35#include <pcb_lexer.h>
36#include <kiid.h>
37#include <math/box2.h>
38#include <optional>
40#include <string_any_map.h>
41#include <padstack.h>
43
44#include <chrono>
45#include <unordered_map>
46
47
48class PCB_ARC;
49class BOARD;
50class BOARD_ITEM;
51class ZONE_SETTINGS;
54class PAD;
57class PCB_SHAPE;
59class EDA_TEXT;
60class PCB_TEXT;
61class PCB_TEXTBOX;
62class PCB_TRACK;
63class PCB_TABLE;
64class PCB_TABLECELL;
65class FOOTPRINT;
66class PCB_GROUP;
67class PCB_POINT;
68class PCB_TARGET;
69class PCB_VIA;
70class ZONE;
72class PCB_BARCODE;
73class FP_3DMODEL;
75struct LAYER;
78
79
84class PCB_IO_KICAD_SEXPR_PARSER : public PCB_LEXER
85{
86public:
87
88 typedef std::unordered_map< std::string, PCB_LAYER_ID > LAYER_ID_MAP;
89 typedef std::unordered_map< std::string, LSET > LSET_MAP;
90 typedef std::unordered_map< wxString, KIID > KIID_MAP;
91
93 std::function<bool( wxString, int, wxString, wxString )> aQueryUserCallback,
94 PROGRESS_REPORTER* aProgressReporter = nullptr, unsigned aLineCount = 0,
95 bool aPreserveDestinationStackup = false ) :
96 PCB_LEXER( aReader ),
97 m_board( aAppendToMe ),
98 m_appendToExisting( aAppendToMe != nullptr ),
99 m_preserveDestinationStackup( aPreserveDestinationStackup ),
100 m_progressReporter( aProgressReporter ),
101 m_lastProgressTime( std::chrono::steady_clock::now() ),
102 m_lineCount( aLineCount ),
103 m_queryUserCallback( std::move( aQueryUserCallback ) )
104 {
105 init();
106 }
107
108 // ~PCB_IO_KICAD_SEXPR_PARSER() {}
109
110 BOARD_ITEM* Parse();
111
118 FOOTPRINT* parseFOOTPRINT( wxArrayString* aInitialComments = nullptr );
119
124 {
125 return m_tooRecent;
126 }
127
132 wxString GetRequiredVersion();
133
138 bool IsValidBoardHeader();
139
144 const std::vector<wxString>& GetParseWarnings() const { return m_parseWarnings; }
145
147 void SetLayerMappingHandler( LAYER_MAPPING_HANDLER aHandler ) { m_layerMappingHandler = std::move( aHandler ); }
148
149private:
150
151 // Group membership info refers to other Uuids in the file.
152 // We don't want to rely on group declarations being last in the file, so
153 // we store info about the group declarations here during parsing and then resolve
154 // them into BOARD_ITEM* after we've parsed the rest of the file.
156 {
157 virtual ~GROUP_INFO() = default; // Make polymorphic
158
160 wxString name;
161 bool locked;
164 std::vector<KIID> memberUuids;
165 };
166
173
177 {
178 BOARD_ITEM* parent = nullptr;
181 std::vector<CONSTRAINT_MEMBER> members;
182 std::optional<double> value;
183 bool driving = true;
184 };
185
188 inline int getNetCode( int aNetCode )
189 {
190 if( ( aNetCode >= 0 ) && ( aNetCode < (int) m_netCodes.size() ) )
191 return m_netCodes[aNetCode];
192
193 return aNetCode;
194 }
195
204 void pushValueIntoMap( int aIndex, int aValue );
205
212 void init();
213
214 void checkpoint();
215
223 void createOldLayerMapping( std::unordered_map< std::string, std::string >& aMap );
224
229 void skipCurrent();
230
231 void parseHeader();
232 void parseGeneralSection();
233 void parsePAGE_INFO();
234 void parseTITLE_BLOCK();
235
236 void parseLayers();
237
239 void remapAppendedLayers( const std::vector<LAYER>& aSourceLayers, const LSET& aDestInitialEnabled,
240 int aDestInitialCopperCount );
241 void parseLayer( LAYER* aLayer );
242
243 void parseBoardStackup();
244
245 void parseSetup();
246 void parseDefaults( BOARD_DESIGN_SETTINGS& aSettings );
247 void parseDefaultTextDims( BOARD_DESIGN_SETTINGS& aSettings, int aLayer );
248 void parseNETINFO_ITEM();
249 void parseNETCLASS();
251
253
254 void parseTextBoxContent( PCB_TEXTBOX* aTextBox );
255
256 void bakeTextBoxLib( PCB_TEXTBOX* aTextBox );
257
259 PCB_TEXT* parsePCB_TEXT( BOARD_ITEM* aParent, PCB_TEXT* aBaseText = nullptr );
260 void parsePCB_TEXT_effects( PCB_TEXT* aText, PCB_TEXT* aBaseText = nullptr );
267
268 // Parse a footprint, but do not replace PARSE_ERROR with FUTURE_FORMAT_ERROR automatically.
269 FOOTPRINT* parseFOOTPRINT_unchecked( wxArrayString* aInitialComments = nullptr );
270 void parseFootprintStackup( FOOTPRINT& aFootprint );
271
272 PAD* parsePAD( FOOTPRINT* aParent = nullptr );
273
274 // Parse only the (option ...) inside a pad description
275 bool parsePAD_option( PAD* aPad );
277
278 void parsePadstack( PAD* aPad );
279
280 PCB_ARC* parseARC();
283 void parseViastack( PCB_VIA* aVia );
287 BOARD* parseBOARD();
288 void parseGROUP_members( GROUP_INFO& aGroupInfo );
289 void parseGROUP( BOARD_ITEM* aParent );
290 void parseCONSTRAINT( BOARD_ITEM* aParent );
291 void parseGENERATOR( BOARD_ITEM* aParent );
292
293 // Parse a board, but do not replace PARSE_ERROR with FUTURE_FORMAT_ERROR automatically.
295
305 LSET lookUpLayerSet( const LSET_MAP& aMap );
306
315
324
334
346
347 void parseXY( int* aX, int* aY );
348
349 void parseMargins( int& aLeft, int& aTop, int& aRight, int& aBottom );
350
351 void parseZoneDefaults( ZONE_SETTINGS& aZoneSettings );
352
353 void parseZoneLayerProperty( std::map<PCB_LAYER_ID, ZONE_LAYER_PROPERTIES>& aProperties );
354
355 std::pair<wxString, wxString> parseBoardProperty();
356
357 void parseVariants();
358 void parseFootprintVariant( FOOTPRINT* aFootprint );
359
368
375 void parseEDA_TEXT( EDA_TEXT* aText );
376
384
385 FP_3DMODEL* parse3DModel( bool aFileNameAlreadyParsed = false );
386
394
395 int parseBoardUnits();
396
397 int parseBoardUnits( const char* aExpected, EDA_DATA_TYPE aDataType );
398
399 inline int parseBoardUnits( const PCB_KEYS_T::T aToken, const EDA_DATA_TYPE aDataType = EDA_DATA_TYPE::DISTANCE )
400 {
401 return parseBoardUnits( GetTokenText( aToken ), aDataType );
402 }
403
404 inline int parseInt()
405 {
406 return (int)strtol( CurText(), nullptr, 10 );
407 }
408
409 inline int parseInt( const char* aExpected )
410 {
411 NeedNUMBER( aExpected );
412 return parseInt();
413 }
414
415 inline long parseHex()
416 {
417 NextTok();
418 return strtol( CurText(), nullptr, 16 );
419 }
420
421 bool parseBool();
422
423 std::optional<bool> parseOptBool();
424
435 bool parseMaybeAbsentBool( bool aDefaultValue );
436
437 std::pair<std::optional<bool>, std::optional<bool>> parseFrontBackOptBool( bool aAllowLegacyFormat = false );
438
439 void parseNet( BOARD_CONNECTED_ITEM* aItem );
440
441 /*
442 * @return if m_appendToExisting, returns new KIID(), otherwise returns CurStr() as KIID.
443 */
445
450 void resolveGroups( BOARD_ITEM* aParent );
451 void resolveConstraints( BOARD_ITEM* aParent );
452
454 using TIMEOUT = std::chrono::milliseconds;
455
457 using CLOCK = std::chrono::steady_clock;
458
460 using TIME_PT = std::chrono::time_point<CLOCK>;
461
465 std::set<wxString> m_undefinedLayers;
466 std::vector<int> m_netCodes;
473
476
479
482 unsigned m_lineCount;
483
484 std::vector<GROUP_INFO> m_groupInfos;
485 std::vector<GENERATOR_INFO> m_generatorInfos;
486 std::vector<CONSTRAINT_INFO> m_constraintInfos;
487
488 std::function<bool( wxString aTitle, int aIcon, wxString aMsg, wxString aAction )> m_queryUserCallback;
489
490 std::vector<wxString> m_parseWarnings;
491};
492
493
494#endif // _PCBNEW_PARSER_H_
A base class derived from BOARD_ITEM for items that can be connected and have a net,...
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:83
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:373
A mix-in class (via multiple inheritance) that handles texts such as labels, parts,...
Definition eda_text.h:89
Definition kiid.h:46
A logical library item identifier and consists of various portions much like a URI.
Definition lib_id.h:45
An abstract class from which implementation specific LINE_READERs may be derived to read single lines...
Definition richio.h:62
LSET is a set of PCB_LAYER_IDs.
Definition lset.h:37
Definition pad.h:61
Abstract dimension API.
A set of BOARD_ITEMs (i.e., without duplicates).
Definition pcb_group.h:51
void parseCONSTRAINT(BOARD_ITEM *aParent)
wxString m_generatorVersion
Set to the generator version this board requires.
PCB_TABLECELL * parsePCB_TABLECELL(BOARD_ITEM *aParent)
std::unordered_map< std::string, PCB_LAYER_ID > LAYER_ID_MAP
std::vector< int > m_netCodes
net codes mapping for boards being loaded
void parseOutlinePoints(SHAPE_LINE_CHAIN &aPoly)
Parses possible outline points and stores them into aPoly.
std::set< wxString > m_undefinedLayers
set of layers not defined in layers section
LAYER_MAPPING_HANDLER m_layerMappingHandler
optional remap of appended layers onto dest
std::vector< CONSTRAINT_INFO > m_constraintInfos
void parseZoneLayerProperty(std::map< PCB_LAYER_ID, ZONE_LAYER_PROPERTIES > &aProperties)
PROGRESS_REPORTER * m_progressReporter
optional; may be nullptr
void parseFootprintStackup(FOOTPRINT &aFootprint)
void createOldLayerMapping(std::unordered_map< std::string, std::string > &aMap)
Create a mapping from the (short-lived) bug where layer names were translated.
void parseZoneDefaults(ZONE_SETTINGS &aZoneSettings)
std::unordered_map< std::string, LSET > LSET_MAP
void parseEDA_TEXT(EDA_TEXT *aText)
Parse the common settings for any object derived from EDA_TEXT.
int parseInt(const char *aExpected)
bool m_tooRecent
true if version parses as later than supported
PCB_LAYER_ID lookUpLayer(const LAYER_ID_MAP &aMap)
Parse the current token for the layer definition of a BOARD_ITEM object.
void remapAppendedLayers(const std::vector< LAYER > &aSourceLayers, const LSET &aDestInitialEnabled, int aDestInitialCopperCount)
Remap the appended layers onto the destination using m_layerMappingHandler, on mismatch.
PCB_REFERENCE_IMAGE * parsePCB_REFERENCE_IMAGE(BOARD_ITEM *aParent)
LAYER_ID_MAP m_layerIndices
map layer name to it's index
const std::vector< wxString > & GetParseWarnings() const
Return any non-fatal parse warnings that occurred during parsing.
void parsePostMachining(PADSTACK::POST_MACHINING_PROPS &aProps)
FP_3DMODEL * parse3DModel(bool aFileNameAlreadyParsed=false)
void parseTextBoxContent(PCB_TEXTBOX *aTextBox)
FOOTPRINT * parseFOOTPRINT(wxArrayString *aInitialComments=nullptr)
void pushValueIntoMap(int aIndex, int aValue)
Add aValue value in netcode mapping (m_netCodes) at aIndex.
bool m_preserveDestinationStackup
append keeps destination stackup
std::chrono::steady_clock CLOCK
The type of the time stamps.
void init()
Clear and re-establish m_layerMap with the default layer names.
std::pair< std::optional< bool >, std::optional< bool > > parseFrontBackOptBool(bool aAllowLegacyFormat=false)
void skipCurrent()
Skip the current token level, i.e search for the RIGHT parenthesis which closes the current descripti...
void parseMargins(int &aLeft, int &aTop, int &aRight, int &aBottom)
PCB_LAYER_ID parseBoardItemLayer()
Parse the layer definition of a BOARD_ITEM object.
LSET parseLayersForCuItemWithSoldermask()
Parse the layers definition of a BOARD_ITEM object that has a single copper layer and optional solder...
void parseGENERATOR(BOARD_ITEM *aParent)
PCB_IO_KICAD_SEXPR_PARSER(LINE_READER *aReader, BOARD *aAppendToMe, std::function< bool(wxString, int, wxString, wxString)> aQueryUserCallback, PROGRESS_REPORTER *aProgressReporter=nullptr, unsigned aLineCount=0, bool aPreserveDestinationStackup=false)
std::chrono::milliseconds TIMEOUT
The clock used for the timestamp (guaranteed to be monotonic).
LSET parseBoardItemLayersAsMask()
Parse the layers definition of a BOARD_ITEM object.
void resolveGroups(BOARD_ITEM *aParent)
Called after parsing a footprint definition or board to build the group membership lists.
void parseDefaultTextDims(BOARD_DESIGN_SETTINGS &aSettings, int aLayer)
std::vector< GROUP_INFO > m_groupInfos
ZONE * parseZONE(BOARD_ITEM_CONTAINER *aParent)
PCB_TABLE * parsePCB_TABLE(BOARD_ITEM *aParent)
std::vector< GENERATOR_INFO > m_generatorInfos
PCB_TEXTBOX * parsePCB_TEXTBOX(BOARD_ITEM *aParent)
std::chrono::time_point< CLOCK > TIME_PT
PCB_TEXT * parsePCB_TEXT(BOARD_ITEM *aParent, PCB_TEXT *aBaseText=nullptr)
unsigned m_lineCount
for progress reporting
VECTOR2I parseXY()
Parse a coordinate pair (xy X Y) in board units (mm).
void parseTEARDROP_PARAMETERS(TEARDROP_PARAMETERS *tdParams)
int m_requiredVersion
set to the KiCad format version this board requires
PAD * parsePAD(FOOTPRINT *aParent=nullptr)
void resolveConstraints(BOARD_ITEM *aParent)
The type of progress bar timeout.
std::function< bool(wxString aTitle, int aIcon, wxString aMsg, wxString aAction)> m_queryUserCallback
void parseNet(BOARD_CONNECTED_ITEM *aItem)
FOOTPRINT * parseFOOTPRINT_unchecked(wxArrayString *aInitialComments=nullptr)
void SetLayerMappingHandler(LAYER_MAPPING_HANDLER aHandler)
Handler to remap an appended board's layers onto the destination board, used on mismatch.
void parseRenderCache(EDA_TEXT *text)
Parse the render cache for any object derived from EDA_TEXT.
int parseBoardUnits(const PCB_KEYS_T::T aToken, const EDA_DATA_TYPE aDataType=EDA_DATA_TYPE::DISTANCE)
TIME_PT m_lastProgressTime
for progress reporting
void parseGROUP_members(GROUP_INFO &aGroupInfo)
bool IsTooRecent()
Return whether a version number, if any was parsed, was too recent.
bool IsValidBoardHeader()
Partially parse the input and check if it matches expected header.
void parseFootprintVariant(FOOTPRINT *aFootprint)
std::pair< wxString, wxString > parseBoardProperty()
LSET_MAP m_layerMasks
map layer names to their masks
bool parseMaybeAbsentBool(bool aDefaultValue)
Parses a boolean flag inside a list that existed before boolean normalization.
int parseBoardUnits()
Parse the current token as an ASCII numeric string with possible leading whitespace into a double pre...
void bakeTextBoxLib(PCB_TEXTBOX *aTextBox)
Lift disk-parsed values into PCB_TEXTBOX lib storage for new format files.
std::unordered_map< wxString, KIID > KIID_MAP
PCB_DIMENSION_BASE * parseDIMENSION(BOARD_ITEM *aParent)
LSET lookUpLayerSet(const LSET_MAP &aMap)
std::vector< wxString > m_parseWarnings
Non-fatal warnings collected during parsing.
bool m_appendToExisting
reading into an existing board; reset UUIDs
void parsePCB_TEXT_effects(PCB_TEXT *aText, PCB_TEXT *aBaseText=nullptr)
PCB_SHAPE * parsePCB_SHAPE(BOARD_ITEM *aParent)
void parseDefaults(BOARD_DESIGN_SETTINGS &aSettings)
wxString GetRequiredVersion()
Return a string representing the version of KiCad required to open this file.
PCB_BARCODE * parsePCB_BARCODE(BOARD_ITEM *aParent)
A PCB_POINT is a 0-dimensional point that is used to mark a position on a PCB, or more usually a foot...
Definition pcb_point.h:39
Object to handle a bitmap image that can be inserted in a PCB.
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...
A name/value tuple with unique names and wxAny values.
TEARDROP_PARAMETARS is a helper class to handle parameters needed to build teardrops for a board thes...
ZONE_SETTINGS handles zones parameters.
Handle a list of polygons defining a copper zone.
Definition zone.h:70
#define LAYER(n, l)
EDA_DATA_TYPE
The type of unit.
Definition eda_units.h:34
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:56
STL namespace.
PCB_CONSTRAINT_TYPE
The geometric relationship a PCB_CONSTRAINT enforces between its members.
std::function< std::map< wxString, PCB_LAYER_ID >(const std::vector< INPUT_LAYER_DESC > &)> LAYER_MAPPING_HANDLER
Pointer to a function that takes a map of source and KiCad layers and returns a re-mapped version.
Deferred constraint, resolved against the parsed items once the whole file is read,...
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683