KiCad PCB EDA Suite
Loading...
Searching...
No Matches
am_param.h
Go to the documentation of this file.
1
4
5/*
6 * This program source code file is part of KiCad, a free EDA CAD application.
7 *
8 * Copyright (C) 1992-2017 Jean-Pierre Charras <jp.charras at wanadoo.fr>
9 * Copyright (C) 2010 SoftPLC Corporation, Dick Hollenbeck <[email protected]>
10 * Copyright The KiCad Developers, see AUTHORS.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, see <https://www.gnu.org/licenses/>.
24 */
25
26#ifndef _AM_PARAM_H_
27#define _AM_PARAM_H_
28
29/*
30 * An aperture macro defines a complex shape and is a list of aperture primitives.
31 * Each aperture primitive defines a simple shape (circle, rect, regular polygon...)
32 * Inside a given aperture primitive, a fixed list of parameters defines info
33 * about the shape: size, thickness, number of vertex ...
34 *
35 * Each parameter can be an immediate value or a deferred value.
36 * When value is deferred, it is defined when the aperture macro is instanced by
37 * an ADD macro command
38 *
39 * Actual values of a parameter can also be the result of an arithmetic operation.
40 *
41 * Here is some examples:
42 * An immediate value:
43 * 3.5
44 * A deferred value:
45 * $2 means: replace me by the second value given in the ADD command
46 * Actual value as arithmetic calculation:
47 * $2/2+1
48 *
49 * Note also a defer ed parameter can be defined in aperture macro,
50 * but outside aperture primitives. Example
51 * %AMRECTHERM*
52 * $4=$3/2* parameter $4 is half value of parameter $3
53 * 21,1,$1-$3,$2-$3,0-$1/2-$4,0-$2/2-$4,0*
54 * For the aperture primitive, parameters $1 to $3 will be defined in ADD command,
55 * and $4 is defined inside the macro
56 *
57 * Some examples of aperture macro definition
58 * A simple definition, no parameters:
59 * %AMMOIRE10*
60 * 6,0,0,0.350000,0.005,0.050,3,0.005,0.400000,0.0*%
61 * Example of instantiation:
62 * %ADD19MOIRE10*%
63 *
64 * A simple definition, one parameter:
65 * %AMCIRCLE*
66 * 1,1,$1,0,0*
67 * Example of instantiation:
68 * %ADD11CIRCLE,.5*%
69 *
70 * A definition, with parameters and arithmetic operations:
71 * %AMVECTOR*
72 * 2,1,$1,0,0,$2+1,$3,-135*%
73 * Example of instantiation:
74 * %ADD12VECTOR,0.05X0X0*%
75 *
76 * A more complicated aperture macro definition, with parameters and arithmetic operations:
77 * %AMRNDREC*
78 * 0 this is a comment*
79 * 21,1,$1+$1,$2+$2-$3-$3,0,0,0*
80 * 21,1,$1+$1-$3-$3,$2+$2,0,0,0*
81 * 1,1,$3+$3,$1-$3,$2-$3*
82 * 1,1,$3+$3,$3-$1,$2-$3*
83 * 1,1,$3+$3,$1-$3,$3-$2*
84 * 1,1,$3+$3,$3-$1,$3-$2*%
85 * Example of instantiation:
86 *
87 * A more complicated sample of aperture macro definition:
88 * G04 Rectangular Thermal Macro, params: W/2, H/2, T/2 *
89 * %AMRECTHERM*
90 * $4=$3/2*
91 * 21,1,$1-$3,$2-$3,0-$1/2-$4,0-$2/2-$4,0*
92 * 21,1,$1-$3,$2-$3,0-$1/2-$4,$2/2+$4,0*
93 * 21,1,$1-$3,$2-$3,$1/2+$4,0-$2/2-$4,0*
94 * 21,1,$1-$3,$2-$3,$1/2+$4,$2/2+$4,0*%
95 * Example of instantiation:
96 * %ADD28RECTHERM,0.035591X0.041496X0.005000*%
97 */
98
99#include <vector>
100
101#include <dcode.h>
102/*
103Values of a parameter can be the result of an arithmetic operation,
104between immediate values and deferred value.
105From an idea found in Gerbv, here is the way to evaluate a parameter.
106a AM_PARAM_ITEM holds info about operands and operators in a parameter definition
107( a AM_PARAM ) like $2+$2-$3-$3/2
108
109Precedence was recently actually defined in gerber RS274X
110(Previously, there was no actual info about this precedence)
111This is the usual arithmetic precedence between + - x / ( ), the only ones used in Gerber
112
113Before 2015 apr 10, actual value was calculated step to step:
114no precedence, and '(' ')' are ignored.
115
116Since 2015 apr 10 precedence is in use.
117
118Parameter definition is described by a very primitive assembler.
119This "program "should describe how to calculate the parameter.
120The assembler consist of 10 instruction intended for a stackbased machine.
121The instructions are:
122NOP, PUSHVALUE, PUSHPARM, ADD, SUB, MUL, DIV, OPEN_PAR, CLOSE_PAR, EQUATE
123
124The instructions
125----------------
126NOP : The no operation. This is the default instruction and are
127 added as a security measure.
128PUSHVALUE : Pushes an arithmetical value on the stack. This machine only works with floats
129 on the stack.
130PUSHPARM: Pushes a deferred parameter onto the stack. Gerber aperture macros accepts
131 parameters to be set when later declared, so the same macro can
132 be used at several instances. Which parameter to be set is an integer
133 and starts with 1. definition is like $1 or $3
134ADD : The mathematical operation +. Takes the two uppermost values on the
135 the stack, adds them and pushes the result back onto the stack.
136SUB : Same as ADD, but with -.
137MUL : Same as ADD, but with *.
138DIV : Same as ADD, but with /.
139OPEN_PAR : Opening parenthesis: modify the precedence of operators by opening a local block.
140CLOSE_PAR : Closing parenthesis: modify the precedence of operators by closing the local block.
141POPVALUE : used when evaluate the expression: store current calculated value
142*/
143
148
157{
158public:
160 : m_type( aType), m_dvalue( 0.0 )
161 {}
162
163 AM_PARAM_EVAL( double aValue )
164 : m_type( parm_item_type::NOP ), m_dvalue( aValue )
165 {}
166
168 {
169 return m_type;
170 }
171
172 bool IsOperator() const { return m_type != NOP; }
173 double GetValue() const { return m_dvalue; }
175 int GetPriority() const { return GetPriority( GetOperator() ); }
176
177 static int GetPriority( parm_item_type aType )
178 {
179 switch( aType )
180 {
181 case ADD:
182 case SUB:
183 return 1;
184
185 case MUL:
186 case DIV:
187 return 2;
188
189 case OPEN_PAR:
190 case CLOSE_PAR:
191 return 3;
192
193 default:
194 break;
195 }
196
197 return 0;
198 }
199
200private:
201 parm_item_type m_type; // the type of item
202 double m_dvalue; // the value, for a numerical value
203 // used only when m_type == NOP
204};
205
206typedef std::vector<AM_PARAM_EVAL> AM_PARAM_EVAL_STACK;
207
216{
217public:
218 AM_PARAM_ITEM( parm_item_type aType, double aValue )
219 {
220 m_type = aType;
221 m_dvalue = aValue;
222 m_ivalue = 0;
223 }
224
225 AM_PARAM_ITEM( parm_item_type aType, int aValue )
226 {
227 m_type = aType;
228 m_dvalue = 0.0;
229 m_ivalue = aValue;
230 }
231
232 void SetValue( double aValue )
233 {
234 m_dvalue = aValue;
235 }
236
237 double GetValue( ) const
238 {
239 return m_dvalue;
240 }
241
243 {
244 return m_type;
245 }
246
247 unsigned GetIndex() const
248 {
249 return (unsigned) m_ivalue;
250 }
251
252 bool IsOperator() const
253 {
254 return m_type == ADD || m_type == SUB || m_type == MUL || m_type == DIV;
255 }
256 bool IsOperand() const
257 {
258 return m_type == PUSHVALUE || m_type == PUSHPARM;
259 }
260
261 bool IsDefered() const
262 {
263 return m_type == PUSHPARM;
264 }
265
266private:
267 parm_item_type m_type; // the type of item
268 double m_dvalue; // the value, for PUSHVALUE type item
269 int m_ivalue; // the integer value, for PUSHPARM type item
270};
271
281{
282public:
283 AM_PARAM();
284
291 void PushOperator( parm_item_type aType, double aValue );
292 void PushOperator( parm_item_type aType, int aValue = 0);
293
294 double GetValueFromMacro( APERTURE_MACRO* aApertureMacro ) const;
295
303 bool IsImmediate() const;
304
305 unsigned GetIndex() const
306 {
307 return (unsigned) m_index;
308 }
309
310 void SetIndex( int aIndex )
311 {
312 m_index = aIndex;
313 }
314
328 bool ReadParamFromAmDef( char*& aText );
329
330private:
338
343 std::vector<AM_PARAM_ITEM> m_paramStack;
344
345};
346
347typedef std::vector<AM_PARAM> AM_PARAMS;
348
349#endif // _AM_PARAM_H_
parm_item_type
Definition am_param.h:145
@ OPEN_PAR
Definition am_param.h:146
@ MUL
Definition am_param.h:146
@ SUB
Definition am_param.h:146
@ CLOSE_PAR
Definition am_param.h:146
@ DIV
Definition am_param.h:146
@ PUSHVALUE
Definition am_param.h:146
@ ADD
Definition am_param.h:146
@ PUSHPARM
Definition am_param.h:146
@ POPVALUE
Definition am_param.h:146
@ NOP
Definition am_param.h:146
std::vector< AM_PARAM > AM_PARAMS
Definition am_param.h:347
std::vector< AM_PARAM_EVAL > AM_PARAM_EVAL_STACK
Definition am_param.h:206
double m_dvalue
Definition am_param.h:202
AM_PARAM_EVAL(double aValue)
Definition am_param.h:163
parm_item_type GetOperator() const
Definition am_param.h:174
bool IsOperator() const
Definition am_param.h:172
parm_item_type m_type
Definition am_param.h:201
static int GetPriority(parm_item_type aType)
Definition am_param.h:177
double GetValue() const
Definition am_param.h:173
AM_PARAM_EVAL(parm_item_type aType)
Definition am_param.h:159
int GetPriority() const
Definition am_param.h:175
parm_item_type GetType() const
Definition am_param.h:167
parm_item_type m_type
Definition am_param.h:267
double GetValue() const
Definition am_param.h:237
unsigned GetIndex() const
Definition am_param.h:247
parm_item_type GetType() const
Definition am_param.h:242
AM_PARAM_ITEM(parm_item_type aType, double aValue)
Definition am_param.h:218
bool IsOperand() const
Definition am_param.h:256
double m_dvalue
Definition am_param.h:268
void SetValue(double aValue)
Definition am_param.h:232
bool IsDefered() const
Definition am_param.h:261
bool IsOperator() const
Definition am_param.h:252
AM_PARAM_ITEM(parm_item_type aType, int aValue)
Definition am_param.h:225
bool ReadParamFromAmDef(char *&aText)
Read one aperture macro parameter.
Definition am_param.cpp:164
void SetIndex(int aIndex)
Definition am_param.h:310
void PushOperator(parm_item_type aType, double aValue)
Add an operator/operand to the current stack.
Definition am_param.cpp:139
bool IsImmediate() const
Test if this AM_PARAM holds an immediate parameter or is a pointer into a parameter held by an owning...
Definition am_param.cpp:53
unsigned GetIndex() const
Definition am_param.h:305
std::vector< AM_PARAM_ITEM > m_paramStack
List of operands/operators to evaluate the actual value if a par def is $3/2, there are 3 items in st...
Definition am_param.h:343
int m_index
has meaning to define parameter local to an aperture macro this is the id of a parameter defined like...
Definition am_param.h:337
double GetValueFromMacro(APERTURE_MACRO *aApertureMacro) const
Definition am_param.cpp:69
Support the "aperture macro" defined within standard RS274X.