2013-05-26 04:36:44 +00:00
|
|
|
/**
|
2018-01-29 12:26:58 +00:00
|
|
|
* @file am_param.h
|
2013-05-26 04:36:44 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2017-04-09 14:31:14 +00:00
|
|
|
* Copyright (C) 1992-2017 Jean-Pierre Charras <jp.charras at wanadoo.fr>
|
2013-05-26 04:36:44 +00:00
|
|
|
* Copyright (C) 2010 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
|
2021-06-03 18:32:24 +00:00
|
|
|
* Copyright (C) 1992-2021 KiCad Developers, see change_log.txt for contributors.
|
2013-05-26 04:36:44 +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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _AM_PARAM_H_
|
|
|
|
#define _AM_PARAM_H_
|
|
|
|
|
|
|
|
/*
|
|
|
|
* An aperture macro defines a complex shape and is a list of aperture primitives.
|
|
|
|
* Each aperture primitive defines a simple shape (circle, rect, regular polygon...)
|
|
|
|
* Inside a given aperture primitive, a fixed list of parameters defines info
|
|
|
|
* about the shape: size, thickness, number of vertex ...
|
|
|
|
*
|
2021-06-03 18:32:24 +00:00
|
|
|
* Each parameter can be an immediate value or a deferred value.
|
|
|
|
* When value is deferred, it is defined when the aperture macro is instanced by
|
2013-05-26 04:36:44 +00:00
|
|
|
* an ADD macro command
|
|
|
|
*
|
|
|
|
* Actual values of a parameter can also be the result of an arithmetic operation.
|
|
|
|
*
|
|
|
|
* Here is some examples:
|
|
|
|
* An immediate value:
|
|
|
|
* 3.5
|
2021-06-03 18:32:24 +00:00
|
|
|
* A deferred value:
|
2013-05-26 04:36:44 +00:00
|
|
|
* $2 means: replace me by the second value given in the ADD command
|
|
|
|
* Actual value as arithmetic calculation:
|
|
|
|
* $2/2+1
|
|
|
|
*
|
2021-06-03 18:32:24 +00:00
|
|
|
* Note also a defer ed parameter can be defined in aperture macro,
|
2013-05-26 04:36:44 +00:00
|
|
|
* but outside aperture primitives. Example
|
|
|
|
* %AMRECTHERM*
|
|
|
|
* $4=$3/2* parameter $4 is half value of parameter $3
|
|
|
|
* 21,1,$1-$3,$2-$3,0-$1/2-$4,0-$2/2-$4,0*
|
|
|
|
* For the aperture primitive, parameters $1 to $3 will be defined in ADD command,
|
|
|
|
* and $4 is defined inside the macro
|
|
|
|
*
|
|
|
|
* Some examples of aperture macro definition
|
|
|
|
* A simple definition, no parameters:
|
|
|
|
* %AMMOIRE10*
|
|
|
|
* 6,0,0,0.350000,0.005,0.050,3,0.005,0.400000,0.0*%
|
2021-06-09 19:32:58 +00:00
|
|
|
* Example of instantiation:
|
2016-10-13 08:26:49 +00:00
|
|
|
* %ADD19MOIRE10*%
|
2013-05-26 04:36:44 +00:00
|
|
|
*
|
|
|
|
* A simple definition, one parameter:
|
|
|
|
* %AMCIRCLE*
|
|
|
|
* 1,1,$1,0,0*
|
2021-06-09 19:32:58 +00:00
|
|
|
* Example of instantiation:
|
2013-05-26 04:36:44 +00:00
|
|
|
* %ADD11CIRCLE,.5*%
|
|
|
|
*
|
|
|
|
* A definition, with parameters and arithmetic operations:
|
|
|
|
* %AMVECTOR*
|
|
|
|
* 2,1,$1,0,0,$2+1,$3,-135*%
|
2021-06-09 19:32:58 +00:00
|
|
|
* Example of instantiation:
|
2013-05-26 04:36:44 +00:00
|
|
|
* %ADD12VECTOR,0.05X0X0*%
|
|
|
|
*
|
2021-06-03 18:32:24 +00:00
|
|
|
* A more complicated aperture macro definition, with parameters and arithmetic operations:
|
2013-05-26 04:36:44 +00:00
|
|
|
* %AMRNDREC*
|
|
|
|
* 0 this is a comment*
|
|
|
|
* 21,1,$1+$1,$2+$2-$3-$3,0,0,0*
|
|
|
|
* 21,1,$1+$1-$3-$3,$2+$2,0,0,0*
|
|
|
|
* 1,1,$3+$3,$1-$3,$2-$3*
|
|
|
|
* 1,1,$3+$3,$3-$1,$2-$3*
|
|
|
|
* 1,1,$3+$3,$1-$3,$3-$2*
|
|
|
|
* 1,1,$3+$3,$3-$1,$3-$2*%
|
2021-06-09 19:32:58 +00:00
|
|
|
* Example of instantiation:
|
2013-05-26 04:36:44 +00:00
|
|
|
*
|
|
|
|
* A more complicated sample of aperture macro definition:
|
|
|
|
* G04 Rectangular Thermal Macro, params: W/2, H/2, T/2 *
|
|
|
|
* %AMRECTHERM*
|
|
|
|
* $4=$3/2*
|
|
|
|
* 21,1,$1-$3,$2-$3,0-$1/2-$4,0-$2/2-$4,0*
|
|
|
|
* 21,1,$1-$3,$2-$3,0-$1/2-$4,$2/2+$4,0*
|
|
|
|
* 21,1,$1-$3,$2-$3,$1/2+$4,0-$2/2-$4,0*
|
|
|
|
* 21,1,$1-$3,$2-$3,$1/2+$4,$2/2+$4,0*%
|
2021-06-09 19:32:58 +00:00
|
|
|
* Example of instantiation:
|
2013-05-26 04:36:44 +00:00
|
|
|
* %ADD28RECTHERM,0.035591X0.041496X0.005000*%
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include <dcode.h>
|
|
|
|
/*
|
|
|
|
Values of a parameter can be the result of an arithmetic operation,
|
2021-06-03 18:32:24 +00:00
|
|
|
between immediate values and deferred value.
|
2013-05-26 04:36:44 +00:00
|
|
|
From an idea found in Gerbv, here is the way to evaluate a parameter.
|
|
|
|
a AM_PARAM_ITEM holds info about operands and operators in a parameter definition
|
|
|
|
( a AM_PARAM ) like $2+$2-$3-$3/2
|
|
|
|
|
2017-04-09 14:31:14 +00:00
|
|
|
Precedence was recently actually defined in gerber RS274X
|
|
|
|
(Previously, there was no actual info about this precedence)
|
2021-06-03 18:32:24 +00:00
|
|
|
This is the usual arithmetic precedence between + - x / ( ), the only ones used in Gerber
|
2017-04-09 14:31:14 +00:00
|
|
|
|
2017-04-10 15:29:25 +00:00
|
|
|
Before 2015 apr 10, actual value was calculated step to step:
|
2017-04-09 14:31:14 +00:00
|
|
|
no precedence, and '(' ')' are ignored.
|
|
|
|
|
2017-04-10 15:29:25 +00:00
|
|
|
Since 2015 apr 10 precedence is in use.
|
|
|
|
|
2013-05-26 04:36:44 +00:00
|
|
|
Parameter definition is described by a very primitive assembler.
|
|
|
|
This "program "should describe how to calculate the parameter.
|
2017-04-09 14:31:14 +00:00
|
|
|
The assembler consist of 10 instruction intended for a stackbased machine.
|
2013-05-26 04:36:44 +00:00
|
|
|
The instructions are:
|
2017-04-09 14:31:14 +00:00
|
|
|
NOP, PUSHVALUE, PUSHPARM, ADD, SUB, MUL, DIV, OPEN_PAR, CLOSE_PAR, EQUATE
|
2013-05-26 04:36:44 +00:00
|
|
|
|
|
|
|
The instructions
|
|
|
|
----------------
|
|
|
|
NOP : The no operation. This is the default instruction and are
|
|
|
|
added as a security measure.
|
|
|
|
PUSHVALUE : Pushes an arithmetical value on the stack. This machine only works with floats
|
|
|
|
on the stack.
|
2021-06-03 18:32:24 +00:00
|
|
|
PUSHPARM: Pushes a deferred parameter onto the stack. Gerber aperture macros accepts
|
2013-05-26 04:36:44 +00:00
|
|
|
parameters to be set when later declared, so the same macro can
|
|
|
|
be used at several instances. Which parameter to be set is an integer
|
|
|
|
and starts with 1. definition is like $1 or $3
|
|
|
|
ADD : The mathematical operation +. Takes the two uppermost values on the
|
|
|
|
the stack, adds them and pushes the result back onto the stack.
|
|
|
|
SUB : Same as ADD, but with -.
|
|
|
|
MUL : Same as ADD, but with *.
|
|
|
|
DIV : Same as ADD, but with /.
|
2017-04-09 14:31:14 +00:00
|
|
|
OPEN_PAR : Opening parenthesis: modify the precedence of operators by opening a local block.
|
|
|
|
CLOSE_PAR : Closing parenthesis: modify the precedence of operators by closing the local block.
|
2013-05-26 04:36:44 +00:00
|
|
|
POPVALUE : used when evaluate the expression: store current calculated value
|
|
|
|
*/
|
|
|
|
|
|
|
|
enum parm_item_type
|
|
|
|
{
|
2017-04-09 14:31:14 +00:00
|
|
|
NOP, PUSHVALUE, PUSHPARM, ADD, SUB, MUL, DIV, OPEN_PAR, CLOSE_PAR, POPVALUE
|
2013-05-26 04:36:44 +00:00
|
|
|
};
|
|
|
|
|
2017-04-10 15:29:25 +00:00
|
|
|
/**
|
|
|
|
* This helper class hold a value or an arithmetic operator to calculate
|
|
|
|
* the final value of a aperture macro parameter, using usual
|
|
|
|
* arithmetic operator precedence
|
|
|
|
* Only operators ADD, SUB, MUL, DIV, OPEN_PAR, CLOSE_PAR have meaning when calculating
|
|
|
|
* a value
|
|
|
|
*/
|
|
|
|
class AM_PARAM_EVAL
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AM_PARAM_EVAL( parm_item_type aType )
|
|
|
|
: m_type( aType), m_dvalue( 0.0 )
|
|
|
|
{}
|
|
|
|
|
|
|
|
AM_PARAM_EVAL( double aValue )
|
|
|
|
: m_type( parm_item_type::NOP ), m_dvalue( aValue )
|
|
|
|
{}
|
|
|
|
|
|
|
|
parm_item_type GetType() const
|
|
|
|
{
|
|
|
|
return m_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsOperator() const { return m_type != NOP; }
|
|
|
|
double GetValue() const { return m_dvalue; }
|
|
|
|
parm_item_type GetOperator() const { return m_type; }
|
|
|
|
int GetPriority() const { return GetPriority( GetOperator() ); }
|
|
|
|
|
|
|
|
static int GetPriority( parm_item_type aType )
|
|
|
|
{
|
|
|
|
switch( aType )
|
|
|
|
{
|
|
|
|
case ADD:
|
|
|
|
case SUB:
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
case MUL:
|
|
|
|
case DIV:
|
|
|
|
return 2;
|
|
|
|
|
|
|
|
case OPEN_PAR:
|
|
|
|
case CLOSE_PAR:
|
|
|
|
return 3;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
parm_item_type m_type; // the type of item
|
|
|
|
double m_dvalue; // the value, for a numerical value
|
|
|
|
// used only when m_type == NOP
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef std::vector<AM_PARAM_EVAL> AM_PARAM_EVAL_STACK;
|
|
|
|
|
2013-05-26 04:36:44 +00:00
|
|
|
/**
|
2021-06-03 18:32:24 +00:00
|
|
|
* Hold an operand for an AM_PARAM as defined within standard RS274X.
|
|
|
|
*
|
|
|
|
* The \a value field can be a constant, i.e. "immediate" parameter or it may not be used if
|
|
|
|
* this param is going to defer to the referencing aperture macro. In that case, the \a index
|
|
|
|
* field is an index into the aperture macro's parameters.
|
2013-05-26 04:36:44 +00:00
|
|
|
*/
|
|
|
|
class AM_PARAM_ITEM
|
|
|
|
{
|
|
|
|
public:
|
2015-07-03 08:42:47 +00:00
|
|
|
AM_PARAM_ITEM( parm_item_type aType, double aValue )
|
2013-05-26 04:36:44 +00:00
|
|
|
{
|
|
|
|
m_type = aType;
|
|
|
|
m_dvalue = aValue;
|
|
|
|
m_ivalue = 0;
|
|
|
|
}
|
2017-04-08 10:53:40 +00:00
|
|
|
|
2015-07-03 08:42:47 +00:00
|
|
|
AM_PARAM_ITEM( parm_item_type aType, int aValue )
|
2013-05-26 04:36:44 +00:00
|
|
|
{
|
|
|
|
m_type = aType;
|
|
|
|
m_dvalue = 0.0;
|
|
|
|
m_ivalue = aValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetValue( double aValue )
|
|
|
|
{
|
|
|
|
m_dvalue = aValue;
|
|
|
|
}
|
2017-04-08 10:53:40 +00:00
|
|
|
|
2013-05-26 04:36:44 +00:00
|
|
|
double GetValue( ) const
|
|
|
|
{
|
|
|
|
return m_dvalue;
|
|
|
|
}
|
2017-04-08 10:53:40 +00:00
|
|
|
|
2013-05-26 04:36:44 +00:00
|
|
|
parm_item_type GetType() const
|
|
|
|
{
|
|
|
|
return m_type;
|
|
|
|
}
|
2017-04-08 10:53:40 +00:00
|
|
|
|
2013-05-26 04:36:44 +00:00
|
|
|
unsigned GetIndex() const
|
|
|
|
{
|
|
|
|
return (unsigned) m_ivalue;
|
|
|
|
}
|
2017-04-08 10:53:40 +00:00
|
|
|
|
2013-05-26 04:36:44 +00:00
|
|
|
bool IsOperator() const
|
|
|
|
{
|
|
|
|
return m_type == ADD || m_type == SUB || m_type == MUL || m_type == DIV;
|
|
|
|
}
|
|
|
|
bool IsOperand() const
|
|
|
|
{
|
|
|
|
return m_type == PUSHVALUE || m_type == PUSHPARM;
|
|
|
|
}
|
2017-04-08 10:53:40 +00:00
|
|
|
|
2013-05-26 04:36:44 +00:00
|
|
|
bool IsDefered() const
|
|
|
|
{
|
|
|
|
return m_type == PUSHPARM;
|
|
|
|
}
|
2021-06-03 18:32:24 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
parm_item_type m_type; // the type of item
|
|
|
|
double m_dvalue; // the value, for PUSHVALUE type item
|
|
|
|
int m_ivalue; // the integer value, for PUSHPARM type item
|
2013-05-26 04:36:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2021-06-03 18:32:24 +00:00
|
|
|
* Hold a parameter value for an "aperture macro" as defined within standard RS274X.
|
|
|
|
*
|
|
|
|
* The parameter can be a constant, i.e. "immediate" parameter, or depend on some deferred
|
|
|
|
* values, defined in a D_CODE, by the ADD command. Note the actual value could need an
|
|
|
|
* evaluation from an arithmetical expression items in the expression are stored in. A
|
|
|
|
* simple definition is just a value stored in one item in m_paramStack.
|
2013-05-26 04:36:44 +00:00
|
|
|
*/
|
|
|
|
class AM_PARAM
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AM_PARAM();
|
|
|
|
|
|
|
|
/**
|
2021-06-03 18:32:24 +00:00
|
|
|
* Add an operator/operand to the current stack.
|
|
|
|
*
|
|
|
|
* @param aType is the type of item (NOP, PUSHVALUE, PUSHPARM, ADD, SUB, MUL, DIV, EQUATE)
|
|
|
|
* @param aValue is the item value, double for PUSHVALUE or int for PUSHPARM type.
|
2013-05-26 04:36:44 +00:00
|
|
|
*/
|
|
|
|
void PushOperator( parm_item_type aType, double aValue );
|
|
|
|
void PushOperator( parm_item_type aType, int aValue = 0);
|
|
|
|
|
2023-03-24 11:02:49 +00:00
|
|
|
double GetValueFromMacro( APERTURE_MACRO* aApertureMacro ) const;
|
2013-05-26 04:36:44 +00:00
|
|
|
|
|
|
|
/**
|
2021-06-03 18:32:24 +00:00
|
|
|
* Test if this AM_PARAM holds an immediate parameter or is a pointer into a parameter held
|
|
|
|
* by an owning D_CODE.
|
|
|
|
*
|
|
|
|
* @return true if the value is immediate, i.e. no deferred value in operands used in its
|
|
|
|
* definition.
|
2013-05-26 04:36:44 +00:00
|
|
|
*/
|
|
|
|
bool IsImmediate() const;
|
|
|
|
|
|
|
|
unsigned GetIndex() const
|
|
|
|
{
|
|
|
|
return (unsigned) m_index;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetIndex( int aIndex )
|
|
|
|
{
|
|
|
|
m_index = aIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-06-03 18:32:24 +00:00
|
|
|
* Read one aperture macro parameter.
|
|
|
|
*
|
2013-05-26 04:36:44 +00:00
|
|
|
* a parameter can be:
|
|
|
|
* a number
|
2021-06-03 18:32:24 +00:00
|
|
|
* a reference to an aperture definition parameter value: $1 to $3 ...
|
|
|
|
* a parameter definition can be complex and have operators between numbers and/or other
|
|
|
|
* parameter
|
2013-05-26 04:36:44 +00:00
|
|
|
* like $1+3 or $2x2..
|
|
|
|
* Parameters are separated by a comma ( of finish by *)
|
|
|
|
* @param aText = pointer to the parameter to read. Will be modified to point to the next field
|
|
|
|
* @return true if a param is read, or false
|
|
|
|
*/
|
2023-01-28 15:39:48 +00:00
|
|
|
bool ReadParamFromAmDef( char*& aText );
|
2021-06-03 18:32:24 +00:00
|
|
|
|
|
|
|
private:
|
2023-01-28 15:39:48 +00:00
|
|
|
/**
|
|
|
|
* has meaning to define parameter local to an aperture macro
|
|
|
|
* this is the id of a parameter defined like
|
|
|
|
* $n = ....
|
|
|
|
* n is the index
|
|
|
|
*/
|
|
|
|
int m_index;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* List of operands/operators to evaluate the actual value if a par def is $3/2,
|
|
|
|
* there are 3 items in stack: 3 (type PUSHPARM) , / (type DIV), 2 (type PUSHVALUE).
|
|
|
|
*/
|
|
|
|
std::vector<AM_PARAM_ITEM> m_paramStack;
|
2021-06-03 18:32:24 +00:00
|
|
|
|
2013-05-26 04:36:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef std::vector<AM_PARAM> AM_PARAMS;
|
|
|
|
|
|
|
|
#endif // _AM_PARAM_H_
|