KiCad PCB EDA Suite
class_am_param.h
Go to the documentation of this file.
1 
5 /*
6  * This program source code file is part of KiCad, a free EDA CAD application.
7  *
8  * Copyright (C) 1992-2015 Jean-Pierre Charras <jean-pierre.charras@gipsa-lab.inpg.fr>
9  * Copyright (C) 2010 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
10  * Copyright (C) 1992-2015 KiCad Developers, see change_log.txt for contributors.
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, you may find one here:
24  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
25  * or you may search the http://www.gnu.org website for the version 2 license,
26  * or you may write to the Free Software Foundation, Inc.,
27  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
28  */
29 
30 #ifndef _AM_PARAM_H_
31 #define _AM_PARAM_H_
32 
33 /*
34  * An aperture macro defines a complex shape and is a list of aperture primitives.
35  * Each aperture primitive defines a simple shape (circle, rect, regular polygon...)
36  * Inside a given aperture primitive, a fixed list of parameters defines info
37  * about the shape: size, thickness, number of vertex ...
38  *
39  * Each parameter can be an immediate value or a defered value.
40  * When value is defered, it is defined when the aperture macro is instancied by
41  * an ADD macro command
42  *
43  * Actual values of a parameter can also be the result of an arithmetic operation.
44  *
45  * Here is some examples:
46  * An immediate value:
47  * 3.5
48  * A deferend value:
49  * $2 means: replace me by the second value given in the ADD command
50  * Actual value as arithmetic calculation:
51  * $2/2+1
52  *
53  * Note also a defered parameter can be defined in aperture macro,
54  * but outside aperture primitives. Example
55  * %AMRECTHERM*
56  * $4=$3/2* parameter $4 is half value of parameter $3
57  * 21,1,$1-$3,$2-$3,0-$1/2-$4,0-$2/2-$4,0*
58  * For the aperture primitive, parameters $1 to $3 will be defined in ADD command,
59  * and $4 is defined inside the macro
60  *
61  * Some examples of aperture macro definition
62  * A simple definition, no parameters:
63  * %AMMOIRE10*
64  * 6,0,0,0.350000,0.005,0.050,3,0.005,0.400000,0.0*%
65  * Example of instanciation:
66  * %ADD19MOIRE10*%
67  *
68  * A simple definition, one parameter:
69  * %AMCIRCLE*
70  * 1,1,$1,0,0*
71  * Example of instanciation:
72  * %ADD11CIRCLE,.5*%
73  *
74  * A definition, with parameters and arithmetic operations:
75  * %AMVECTOR*
76  * 2,1,$1,0,0,$2+1,$3,-135*%
77  * Example of instanciation:
78  * %ADD12VECTOR,0.05X0X0*%
79  *
80  * A more complicated aperture macro definition, with parameters and arihmetic operations:
81  * %AMRNDREC*
82  * 0 this is a comment*
83  * 21,1,$1+$1,$2+$2-$3-$3,0,0,0*
84  * 21,1,$1+$1-$3-$3,$2+$2,0,0,0*
85  * 1,1,$3+$3,$1-$3,$2-$3*
86  * 1,1,$3+$3,$3-$1,$2-$3*
87  * 1,1,$3+$3,$1-$3,$3-$2*
88  * 1,1,$3+$3,$3-$1,$3-$2*%
89  * Example of instanciation:
90  *
91  * A more complicated sample of aperture macro definition:
92  * G04 Rectangular Thermal Macro, params: W/2, H/2, T/2 *
93  * %AMRECTHERM*
94  * $4=$3/2*
95  * 21,1,$1-$3,$2-$3,0-$1/2-$4,0-$2/2-$4,0*
96  * 21,1,$1-$3,$2-$3,0-$1/2-$4,$2/2+$4,0*
97  * 21,1,$1-$3,$2-$3,$1/2+$4,0-$2/2-$4,0*
98  * 21,1,$1-$3,$2-$3,$1/2+$4,$2/2+$4,0*%
99  * Example of instanciation:
100  * %ADD28RECTHERM,0.035591X0.041496X0.005000*%
101  */
102 
103 #include <vector>
104 
105 #include <dcode.h>
106 /*
107 Values of a parameter can be the result of an arithmetic operation,
108 between immediate values and defered value.
109 From an idea found in Gerbv, here is the way to evaluate a parameter.
110 a AM_PARAM_ITEM holds info about operands and operators in a parameter definition
111 ( a AM_PARAM ) like $2+$2-$3-$3/2
112 
113 There is no precedence defined in gerber RS274X, so actual value is calculated step to step.
114 Parameter definition is described by a very primitive assembler.
115 This "program "should describe how to calculate the parameter.
116 The assembler consist of 8 instruction intended for a stackbased machine.
117 The instructions are:
118 NOP, PUSHVALUE, PUSHPARM, ADD, SUB, MUL, DIV, EQUATE
119 
120 The instructions
121 ----------------
122 NOP : The no operation. This is the default instruction and are
123  added as a security measure.
124 PUSHVALUE : Pushes an arithmetical value on the stack. This machine only works with floats
125  on the stack.
126 PUSHPARM: Pushes a defered parameter onto the stack. Gerber aperture macros accepts
127  parameters to be set when later declared, so the same macro can
128  be used at several instances. Which parameter to be set is an integer
129  and starts with 1. definition is like $1 or $3
130 ADD : The mathematical operation +. Takes the two uppermost values on the
131  the stack, adds them and pushes the result back onto the stack.
132 SUB : Same as ADD, but with -.
133 MUL : Same as ADD, but with *.
134 DIV : Same as ADD, but with /.
135 POPVALUE : used when evaluate the expression: store current calculated value
136 */
137 
139 {
141 };
142 
152 {
153 private:
154  parm_item_type m_type; // the type of item
155  double m_dvalue; // the value, for PUSHVALUE type item
156  int m_ivalue; // the integer value, for PUSHPARM type item
157 
158 public:
159  AM_PARAM_ITEM( parm_item_type aType, double aValue )
160  {
161  m_type = aType;
162  m_dvalue = aValue;
163  m_ivalue = 0;
164  }
165  AM_PARAM_ITEM( parm_item_type aType, int aValue )
166  {
167  m_type = aType;
168  m_dvalue = 0.0;
169  m_ivalue = aValue;
170  }
171 
172  void SetValue( double aValue )
173  {
174  m_dvalue = aValue;
175  }
176  double GetValue( ) const
177  {
178  return m_dvalue;
179  }
181  {
182  return m_type;
183  }
184  unsigned GetIndex() const
185  {
186  return (unsigned) m_ivalue;
187  }
188  bool IsOperator() const
189  {
190  return m_type == ADD || m_type == SUB || m_type == MUL || m_type == DIV;
191  }
192  bool IsOperand() const
193  {
194  return m_type == PUSHVALUE || m_type == PUSHPARM;
195  }
196  bool IsDefered() const
197  {
198  return m_type == PUSHPARM;
199  }
200 };
201 
211 class AM_PARAM
212 {
213 private:
214  int m_index; // has meaning to define parameter local to an aperture macro
215  std::vector<AM_PARAM_ITEM> m_paramStack; // list of operands/operators to evalutate the actual value
216  // if a par def is $3/2, there are 3 items in stack:
217  // 3 (type PUSHPARM) , / (type DIV), 2 (type PUSHVALUE)
218 
219 public:
220  AM_PARAM();
221 
228  void PushOperator( parm_item_type aType, double aValue );
229  void PushOperator( parm_item_type aType, int aValue = 0);
230 
231  double GetValue( const D_CODE* aDcode ) const;
232 
239  bool IsImmediate() const;
240 
241  unsigned GetIndex() const
242  {
243  return (unsigned) m_index;
244  }
245 
246  void SetIndex( int aIndex )
247  {
248  m_index = aIndex;
249  }
250 
263  bool ReadParam( char*& aText );
264 };
265 
266 typedef std::vector<AM_PARAM> AM_PARAMS;
267 
268 #endif // _AM_PARAM_H_
std::vector< AM_PARAM_ITEM > m_paramStack
unsigned GetIndex() const
Class AM_PARAM holds an operand for an AM_PARAM as defined within standard RS274X.
unsigned GetIndex() const
std::vector< AM_PARAM > AM_PARAMS
bool ReadParam(char *&aText)
Function ReadParam Read one aperture macro parameter a parameter can be: a number a reference to an a...
parm_item_type m_type
void PushOperator(parm_item_type aType, double aValue)
function PushOperator add an operator/operand to the current stack
parm_item_type GetType() const
Class AM_PARAM holds a parameter value for an "aperture macro" as defined within standard RS274X...
bool IsOperator() const
double GetValue(const D_CODE *aDcode) const
bool IsImmediate() const
Function IsImmediate tests if this AM_PARAM holds an immediate parameter or is a pointer into a param...
bool IsDefered() const
AM_PARAM_ITEM(parm_item_type aType, int aValue)
void SetValue(double aValue)
Class D_CODE holds a gerber DCODE (also called Aperture) definition.
Definition: dcode.h:81
parm_item_type
AM_PARAM_ITEM(parm_item_type aType, double aValue)
double GetValue() const
void SetIndex(int aIndex)
bool IsOperand() const