2012-06-11 00:47:15 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 CERN
|
2019-02-15 07:47:03 +00:00
|
|
|
* Copyright (C) 2012-2019 KiCad Developers, see AUTHORS.txt for contributors.
|
2012-06-11 00:47:15 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file pcb_parser.h
|
|
|
|
* @brief Pcbnew s-expression file format parser definition.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _PCBNEW_PARSER_H_
|
|
|
|
#define _PCBNEW_PARSER_H_
|
|
|
|
|
|
|
|
#include <pcb_lexer.h>
|
2012-11-14 07:15:59 +00:00
|
|
|
#include <hashtables.h>
|
2017-03-13 03:19:33 +00:00
|
|
|
#include <layers_id_colors_and_visibility.h> // PCB_LAYER_ID
|
2013-09-21 07:30:23 +00:00
|
|
|
#include <common.h> // KiROUND
|
2015-05-15 12:49:11 +00:00
|
|
|
#include <convert_to_biu.h> // IU_PER_MM
|
2012-06-11 00:47:15 +00:00
|
|
|
|
2017-11-01 11:14:16 +00:00
|
|
|
#include <unordered_map>
|
2016-08-15 15:16:50 +00:00
|
|
|
|
2012-06-11 00:47:15 +00:00
|
|
|
|
|
|
|
class BOARD;
|
|
|
|
class BOARD_ITEM;
|
|
|
|
class D_PAD;
|
|
|
|
class DIMENSION;
|
|
|
|
class DRAWSEGMENT;
|
2013-09-21 07:30:23 +00:00
|
|
|
class EDA_TEXT;
|
2012-06-11 00:47:15 +00:00
|
|
|
class EDGE_MODULE;
|
|
|
|
class TEXTE_MODULE;
|
|
|
|
class TEXTE_PCB;
|
2013-09-21 07:30:23 +00:00
|
|
|
class TRACK;
|
2012-06-11 00:47:15 +00:00
|
|
|
class MODULE;
|
|
|
|
class PCB_TARGET;
|
2014-04-25 06:00:04 +00:00
|
|
|
class VIA;
|
2012-06-11 00:47:15 +00:00
|
|
|
class ZONE_CONTAINER;
|
2017-10-27 14:25:20 +00:00
|
|
|
class MODULE_3D_SETTINGS;
|
2014-06-24 16:17:18 +00:00
|
|
|
struct LAYER;
|
2012-06-11 00:47:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class PCB_PARSER
|
2012-07-05 17:02:45 +00:00
|
|
|
* reads a Pcbnew s-expression formatted #LINE_READER object and returns the appropriate
|
2012-06-11 00:47:15 +00:00
|
|
|
* #BOARD_ITEM object.
|
|
|
|
*/
|
|
|
|
class PCB_PARSER : public PCB_LEXER
|
|
|
|
{
|
2017-11-01 11:14:16 +00:00
|
|
|
typedef std::unordered_map< std::string, PCB_LAYER_ID > LAYER_ID_MAP;
|
|
|
|
typedef std::unordered_map< std::string, LSET > LSET_MAP;
|
2012-11-14 07:15:59 +00:00
|
|
|
|
2014-06-02 09:41:54 +00:00
|
|
|
BOARD* m_board;
|
2014-06-24 16:17:18 +00:00
|
|
|
LAYER_ID_MAP m_layerIndices; ///< map layer name to it's index
|
|
|
|
LSET_MAP m_layerMasks; ///< map layer names to their masks
|
2018-12-07 00:24:27 +00:00
|
|
|
std::set<wxString> m_undefinedLayers; ///< set of layers not defined in layers section
|
2014-06-02 09:41:54 +00:00
|
|
|
std::vector<int> m_netCodes; ///< net codes mapping for boards being loaded
|
2016-05-10 19:07:35 +00:00
|
|
|
bool m_tooRecent; ///< true if version parses as later than supported
|
|
|
|
int m_requiredVersion; ///< set to the KiCad format version this board requires
|
2012-10-24 18:20:33 +00:00
|
|
|
|
2014-06-03 07:32:57 +00:00
|
|
|
///> Converts net code using the mapping table if available,
|
2015-01-22 12:06:34 +00:00
|
|
|
///> otherwise returns unchanged net code if < 0 or if is is out of range
|
2014-06-03 07:32:57 +00:00
|
|
|
inline int getNetCode( int aNetCode )
|
|
|
|
{
|
2015-01-22 12:06:34 +00:00
|
|
|
if( ( aNetCode >= 0 ) && ( aNetCode < (int) m_netCodes.size() ) )
|
2014-06-03 07:32:57 +00:00
|
|
|
return m_netCodes[aNetCode];
|
|
|
|
|
|
|
|
return aNetCode;
|
|
|
|
}
|
|
|
|
|
2015-01-22 12:06:34 +00:00
|
|
|
/**
|
|
|
|
* function pushValueIntoMap
|
|
|
|
* Add aValue value in netcode mapping (m_netCodes) at index aIndex
|
|
|
|
* ensure there is room in m_netCodes for that, and add room if needed.
|
|
|
|
* @param aIndex = the index ( expected >=0 )of the location to use in m_netCodes
|
|
|
|
* @param aValue = the netcode value to map
|
|
|
|
*/
|
|
|
|
void pushValueIntoMap( int aIndex, int aValue );
|
|
|
|
|
2012-10-24 18:20:33 +00:00
|
|
|
/**
|
|
|
|
* Function init
|
|
|
|
* clears and re-establishes m_layerMap with the default layer names.
|
|
|
|
* m_layerMap will have some of its entries overwritten whenever a (new) board
|
|
|
|
* is encountered.
|
|
|
|
*/
|
|
|
|
void init();
|
|
|
|
|
2017-06-08 21:47:21 +00:00
|
|
|
void parseHeader();
|
|
|
|
void parseGeneralSection();
|
|
|
|
void parsePAGE_INFO();
|
|
|
|
void parseTITLE_BLOCK();
|
2014-06-24 16:17:18 +00:00
|
|
|
|
2017-06-08 21:47:21 +00:00
|
|
|
void parseLayers();
|
|
|
|
void parseLayer( LAYER* aLayer );
|
2014-06-24 16:17:18 +00:00
|
|
|
|
2017-06-08 21:47:21 +00:00
|
|
|
void parseSetup();
|
|
|
|
void parseNETINFO_ITEM();
|
|
|
|
void parseNETCLASS();
|
2013-06-23 19:18:33 +00:00
|
|
|
|
2018-09-16 13:10:48 +00:00
|
|
|
/** Read a DRAWSEGMENT description.
|
|
|
|
* @param aAllowCirclesZeroWidth = true to allow items with 0 width
|
|
|
|
* Only used in custom pad shapes for filled circles.
|
|
|
|
*/
|
|
|
|
DRAWSEGMENT* parseDRAWSEGMENT( bool aAllowCirclesZeroWidth = false );
|
2017-06-08 21:47:21 +00:00
|
|
|
TEXTE_PCB* parseTEXTE_PCB();
|
|
|
|
DIMENSION* parseDIMENSION();
|
2016-05-10 19:07:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Function parseMODULE_unchecked
|
|
|
|
* Parse a module, but do not replace PARSE_ERROR with FUTURE_FORMAT_ERROR automatically.
|
|
|
|
*/
|
2017-06-08 21:47:21 +00:00
|
|
|
MODULE* parseMODULE_unchecked( wxArrayString* aInitialComments = 0 );
|
|
|
|
TEXTE_MODULE* parseTEXTE_MODULE();
|
|
|
|
EDGE_MODULE* parseEDGE_MODULE();
|
|
|
|
D_PAD* parseD_PAD( MODULE* aParent = NULL );
|
2017-01-13 17:51:22 +00:00
|
|
|
// Parse only the (option ...) inside a pad description
|
|
|
|
bool parseD_PAD_option( D_PAD* aPad );
|
2017-06-08 21:47:21 +00:00
|
|
|
TRACK* parseTRACK();
|
|
|
|
VIA* parseVIA();
|
|
|
|
ZONE_CONTAINER* parseZONE_CONTAINER();
|
|
|
|
PCB_TARGET* parsePCB_TARGET();
|
|
|
|
BOARD* parseBOARD();
|
2016-05-10 19:07:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Function parseBOARD_unchecked
|
|
|
|
* Parse a module, but do not replace PARSE_ERROR with FUTURE_FORMAT_ERROR automatically.
|
|
|
|
*/
|
2017-06-08 21:47:21 +00:00
|
|
|
BOARD* parseBOARD_unchecked();
|
2012-06-11 00:47:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function lookUpLayer
|
|
|
|
* parses the current token for the layer definition of a #BOARD_ITEM object.
|
|
|
|
*
|
2013-03-30 17:24:04 +00:00
|
|
|
* @param aMap is the LAYER_{NUM|MSK}_MAP to use for the lookup.
|
2012-11-14 07:15:59 +00:00
|
|
|
*
|
2012-06-11 00:47:15 +00:00
|
|
|
* @throw IO_ERROR if the layer is not valid.
|
|
|
|
* @throw PARSE_ERROR if the layer syntax is incorrect.
|
2012-11-14 07:15:59 +00:00
|
|
|
* @return int - The result of the parsed #BOARD_ITEM layer or set designator.
|
2012-06-11 00:47:15 +00:00
|
|
|
*/
|
2013-03-30 17:24:04 +00:00
|
|
|
template<class T, class M>
|
2017-06-08 21:47:21 +00:00
|
|
|
T lookUpLayer( const M& aMap );
|
2012-06-11 00:47:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Function parseBoardItemLayer
|
|
|
|
* parses the layer definition of a #BOARD_ITEM object.
|
|
|
|
*
|
|
|
|
* @throw IO_ERROR if the layer is not valid.
|
|
|
|
* @throw PARSE_ERROR if the layer syntax is incorrect.
|
|
|
|
* @return The index the parsed #BOARD_ITEM layer.
|
|
|
|
*/
|
2017-06-08 21:47:21 +00:00
|
|
|
PCB_LAYER_ID parseBoardItemLayer();
|
2012-06-11 00:47:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Function parseBoardItemLayersAsMask
|
|
|
|
* parses the layers definition of a #BOARD_ITEM object.
|
|
|
|
*
|
|
|
|
* @throw IO_ERROR if any of the layers is not valid.
|
|
|
|
* @throw PARSE_ERROR if the layers syntax is incorrect.
|
|
|
|
* @return The mask of layers the parsed #BOARD_ITEM is on.
|
|
|
|
*/
|
2017-06-08 21:47:21 +00:00
|
|
|
LSET parseBoardItemLayersAsMask();
|
2012-06-11 00:47:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Function parseXY
|
|
|
|
* parses a coordinate pair (xy X Y) in board units (mm).
|
|
|
|
*
|
|
|
|
* The parser checks if the previous token was T_LEFT and parses the remainder of
|
2012-07-05 17:02:45 +00:00
|
|
|
* the token syntax. This is used when parsing a list of coordinate points. This
|
2012-06-11 00:47:15 +00:00
|
|
|
* way the parser can be used in either case.
|
|
|
|
*
|
|
|
|
* @throw PARSE_ERROR if the coordinate pair syntax is incorrect.
|
|
|
|
* @return A wxPoint object containing the coordinate pair.
|
|
|
|
*/
|
2017-06-08 21:47:21 +00:00
|
|
|
wxPoint parseXY();
|
2012-06-11 00:47:15 +00:00
|
|
|
|
2017-06-08 21:47:21 +00:00
|
|
|
void parseXY( int* aX, int* aY );
|
2012-06-11 00:47:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Function parseEDA_TEXT
|
|
|
|
* parses the common settings for any object derived from #EDA_TEXT.
|
|
|
|
*
|
|
|
|
* @throw PARSE_ERROR if the text syntax is not valid.
|
|
|
|
* @param aText A point to the #EDA_TEXT object to save the parsed settings into.
|
|
|
|
*/
|
2017-06-08 21:47:21 +00:00
|
|
|
void parseEDA_TEXT( EDA_TEXT* aText );
|
2012-06-11 00:47:15 +00:00
|
|
|
|
2017-10-27 14:25:20 +00:00
|
|
|
MODULE_3D_SETTINGS* parse3DModel();
|
2012-06-11 00:47:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Function parseDouble
|
2013-03-27 14:01:50 +00:00
|
|
|
* parses the current token as an ASCII numeric string with possible leading
|
|
|
|
* whitespace into a double precision floating point number.
|
2012-06-11 00:47:15 +00:00
|
|
|
*
|
|
|
|
* @throw IO_ERROR if an error occurs attempting to convert the current token.
|
|
|
|
* @return The result of the parsed token.
|
|
|
|
*/
|
2017-06-08 21:47:21 +00:00
|
|
|
double parseDouble();
|
2012-06-11 00:47:15 +00:00
|
|
|
|
2017-06-08 21:47:21 +00:00
|
|
|
inline double parseDouble( const char* aExpected )
|
2012-06-11 00:47:15 +00:00
|
|
|
{
|
|
|
|
NeedNUMBER( aExpected );
|
|
|
|
return parseDouble();
|
|
|
|
}
|
|
|
|
|
2017-06-08 21:47:21 +00:00
|
|
|
inline double parseDouble( PCB_KEYS_T::T aToken )
|
2012-06-11 00:47:15 +00:00
|
|
|
{
|
|
|
|
return parseDouble( GetTokenText( aToken ) );
|
|
|
|
}
|
|
|
|
|
2017-06-08 21:47:21 +00:00
|
|
|
inline int parseBoardUnits()
|
2012-06-11 00:47:15 +00:00
|
|
|
{
|
2013-03-27 14:01:50 +00:00
|
|
|
// There should be no major rounding issues here, since the values in
|
|
|
|
// the file are in mm and get converted to nano-meters.
|
|
|
|
// See test program tools/test-nm-biu-to-ascii-mm-round-tripping.cpp
|
|
|
|
// to confirm or experiment. Use a similar strategy in both places, here
|
|
|
|
// and in the test program. Make that program with:
|
|
|
|
// $ make test-nm-biu-to-ascii-mm-round-tripping
|
2019-02-14 04:27:57 +00:00
|
|
|
auto retval = parseDouble() * IU_PER_MM;
|
|
|
|
|
|
|
|
// N.B. we currently represent board units as integers. Any values that are
|
|
|
|
// larger or smaller than those board units represent undefined behavior for
|
|
|
|
// the system. We limit values to the largest that is visible on the screen
|
|
|
|
// This is the diagonal distance of the full screen ~1.5m
|
2019-02-15 07:47:03 +00:00
|
|
|
double int_limit = std::numeric_limits<int>::max() * 0.7071; // 0.7071 = roughly 1/sqrt(2)
|
2019-02-14 04:27:57 +00:00
|
|
|
return KiROUND( Clamp<double>( -int_limit, retval, int_limit ) );
|
2012-06-11 00:47:15 +00:00
|
|
|
}
|
|
|
|
|
2017-06-08 21:47:21 +00:00
|
|
|
inline int parseBoardUnits( const char* aExpected )
|
2012-06-11 00:47:15 +00:00
|
|
|
{
|
2019-02-14 04:27:57 +00:00
|
|
|
auto retval = parseDouble( aExpected ) * IU_PER_MM;
|
|
|
|
|
|
|
|
// N.B. we currently represent board units as integers. Any values that are
|
|
|
|
// larger or smaller than those board units represent undefined behavior for
|
|
|
|
// the system. We limit values to the largest that is visible on the screen
|
2019-02-15 07:47:03 +00:00
|
|
|
double int_limit = std::numeric_limits<int>::max() * 0.7071;
|
2019-02-14 04:27:57 +00:00
|
|
|
|
2012-06-14 06:29:54 +00:00
|
|
|
// Use here KiROUND, not KIROUND (see comments about them)
|
|
|
|
// when having a function as argument, because it will be called twice
|
|
|
|
// with KIROUND
|
2019-02-14 04:27:57 +00:00
|
|
|
return KiROUND( Clamp<double>( -int_limit, retval, int_limit ) );
|
2012-06-11 00:47:15 +00:00
|
|
|
}
|
|
|
|
|
2017-06-08 21:47:21 +00:00
|
|
|
inline int parseBoardUnits( PCB_KEYS_T::T aToken )
|
2012-06-11 00:47:15 +00:00
|
|
|
{
|
|
|
|
return parseBoardUnits( GetTokenText( aToken ) );
|
|
|
|
}
|
|
|
|
|
2017-06-08 21:47:21 +00:00
|
|
|
inline int parseInt()
|
2012-06-11 00:47:15 +00:00
|
|
|
{
|
|
|
|
return (int)strtol( CurText(), NULL, 10 );
|
|
|
|
}
|
|
|
|
|
2017-06-08 21:47:21 +00:00
|
|
|
inline int parseInt( const char* aExpected )
|
2012-06-11 00:47:15 +00:00
|
|
|
{
|
|
|
|
NeedNUMBER( aExpected );
|
|
|
|
return parseInt();
|
|
|
|
}
|
|
|
|
|
2017-06-08 21:47:21 +00:00
|
|
|
inline long parseHex()
|
2012-06-11 00:47:15 +00:00
|
|
|
{
|
2012-06-13 23:04:42 +00:00
|
|
|
NextTok();
|
|
|
|
return strtol( CurText(), NULL, 16 );
|
2012-06-11 00:47:15 +00:00
|
|
|
}
|
|
|
|
|
2017-06-08 21:47:21 +00:00
|
|
|
bool parseBool();
|
2012-06-11 00:47:15 +00:00
|
|
|
|
2016-05-10 19:07:35 +00:00
|
|
|
/**
|
|
|
|
* Parse a format version tag like (version 20160417) return the version.
|
|
|
|
* Expects to start on 'version', and eats the closing paren.
|
|
|
|
*/
|
2017-06-08 21:47:21 +00:00
|
|
|
int parseVersion();
|
2012-10-24 18:20:33 +00:00
|
|
|
|
2012-06-11 00:47:15 +00:00
|
|
|
public:
|
2012-10-24 18:20:33 +00:00
|
|
|
|
|
|
|
PCB_PARSER( LINE_READER* aReader = NULL ) :
|
2012-06-11 00:47:15 +00:00
|
|
|
PCB_LEXER( aReader ),
|
2012-10-24 18:20:33 +00:00
|
|
|
m_board( 0 )
|
|
|
|
{
|
2012-11-09 06:58:00 +00:00
|
|
|
init();
|
2012-10-24 18:20:33 +00:00
|
|
|
}
|
|
|
|
|
2012-11-14 07:15:59 +00:00
|
|
|
// ~PCB_PARSER() {}
|
2012-10-24 18:20:33 +00:00
|
|
|
|
|
|
|
/**
|
2012-11-14 07:15:59 +00:00
|
|
|
* Function SetLineReader
|
2012-10-24 18:20:33 +00:00
|
|
|
* sets @a aLineReader into the parser, and returns the previous one, if any.
|
|
|
|
* @param aReader is what to read from for tokens, no ownership is received.
|
|
|
|
* @return LINE_READER* - previous LINE_READER or NULL if none.
|
|
|
|
*/
|
|
|
|
LINE_READER* SetLineReader( LINE_READER* aReader )
|
|
|
|
{
|
|
|
|
LINE_READER* ret = PopReader();
|
|
|
|
PushReader( aReader );
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetBoard( BOARD* aBoard )
|
2012-06-11 00:47:15 +00:00
|
|
|
{
|
2012-10-24 18:20:33 +00:00
|
|
|
init();
|
|
|
|
m_board = aBoard;
|
2012-06-11 00:47:15 +00:00
|
|
|
}
|
|
|
|
|
2017-06-08 21:47:21 +00:00
|
|
|
BOARD_ITEM* Parse();
|
2017-09-17 12:04:57 +00:00
|
|
|
/**
|
|
|
|
* Function parseMODULE
|
|
|
|
* @param aInitialComments may be a pointer to a heap allocated initial comment block
|
|
|
|
* or NULL. If not NULL, then caller has given ownership of a wxArrayString to
|
|
|
|
* this function and care must be taken to delete it even on exception.
|
|
|
|
*/
|
|
|
|
MODULE* parseMODULE( wxArrayString* aInitialComments = 0 );
|
2016-05-10 19:07:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return whether a version number, if any was parsed, was too recent
|
|
|
|
*/
|
|
|
|
bool IsTooRecent()
|
|
|
|
{
|
|
|
|
return m_tooRecent;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a string representing the version of kicad required to open this
|
|
|
|
* file. Not particularly meaningful if IsTooRecent() returns false.
|
|
|
|
*/
|
|
|
|
wxString GetRequiredVersion();
|
|
|
|
|
2012-06-11 00:47:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#endif // _PCBNEW_PARSER_H_
|