KiCad PCB EDA Suite
am_param.cpp
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-2017 Jean-Pierre Charras <jp.charras at wanadoo.fr>
9  * Copyright (C) 2010 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
10  * Copyright (C) 1992-2017 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, 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 #include <am_param.h>
31 #include <am_primitive.h>
32 
33 extern int ReadInt( char*& text, bool aSkipSeparator = true );
34 extern double ReadDouble( char*& text, bool aSkipSeparator = true );
35 extern double Evaluate( AM_PARAM_EVAL_STACK& aExp );
36 
37 /* Class AM_PARAM
38  * holds a parameter value for an "aperture macro" as defined within
39  * standard RS274X. The parameter can be a constant, i.e. "immediate" parameter,
40  * or depend on some defered values, defined in a D_CODE, by the ADD command.
41  * Note the actual value could need an evaluation from an arithmetical expression
42  * items in the expression are stored in .
43  * A simple definition is just a value stored in one item in m_paramStack
44  */
46 {
47  m_index = -1;
48 }
49 
56 {
57  bool is_immediate = true;
58  for( unsigned ii = 0; ii < m_paramStack.size(); ii++ )
59  {
60  if( m_paramStack[ii].IsDefered() )
61  { // a defered value is found in operand list,
62  // so the parameter is not immediate
63  is_immediate = false;
64  break;
65  }
66  }
67  return is_immediate;
68 }
69 
70 double AM_PARAM::GetValue( const D_CODE* aDcode ) const
71 {
72  // In macros, actual values are sometimes given by an expression like:
73  // 0-$2/2-$4
74  // Because arithmetic predence is used, the parameters (values (double) and operators)
75  // are stored in a stack, with all numeric values converted to the actual values
76  // when they are defered parameters
77  // Each item is stored in a AM_PARAM_EVAL (a value or an operator)
78  //
79  // Then the stack with all values resolved is parsed and numeric values
80  // calculated according to the precedence of operators
81  double curr_value = 0.0;
82  parm_item_type op_code;
83 
85 
86  for( unsigned ii = 0; ii < m_paramStack.size(); ii++ )
87  {
88  AM_PARAM_ITEM item = m_paramStack[ii];
89 
90  switch( item.GetType() )
91  {
92  case ADD:
93  case SUB:
94  case MUL:
95  case DIV: // just an operator for next parameter value
96  case OPEN_PAR:
97  case CLOSE_PAR: // Priority modifiers: store in stack
98  op_code = item.GetType();
99  ops.push_back( AM_PARAM_EVAL( op_code ) );
100  break;
101 
102  case PUSHPARM:
103  // a defered value: get the actual parameter from the aDcode
104  if( aDcode ) // should be always true here
105  {
106  if( item.GetIndex() <= aDcode->GetParamCount() )
107  {
108  curr_value = aDcode->GetParam( item.GetIndex() );
109  }
110  else // Get parameter from local param definition
111  {
112  const APERTURE_MACRO * am_parent = aDcode->GetMacro();
113  curr_value = am_parent->GetLocalParam( aDcode, item.GetIndex() );
114  }
115  }
116  else
117  {
118  wxLogDebug( wxT( "AM_PARAM::GetValue(): NULL param aDcode\n" ) );
119  }
120 
121  ops.push_back( AM_PARAM_EVAL( curr_value ) );
122  break;
123 
124  case PUSHVALUE: // a value is on the stack:
125  curr_value = item.GetValue();
126  ops.push_back( AM_PARAM_EVAL( curr_value ) );
127  break;
128 
129  default:
130  wxLogDebug( "AM_PARAM::GetValue(): dcode %d prm %d/%d: unexpected type %d",
131  aDcode ? aDcode->m_Num_Dcode : -1, ii, m_paramStack.size(), item.GetType() );
132  break;
133  }
134  }
135 
136  double result = Evaluate( ops );
137 
138  return result;
139 }
140 
146 void AM_PARAM::PushOperator( parm_item_type aType, double aValue )
147 {
148  AM_PARAM_ITEM item( aType, aValue);
149  m_paramStack.push_back( item );
150 }
151 
152 void AM_PARAM::PushOperator( parm_item_type aType, int aValue )
153 {
154  AM_PARAM_ITEM item( aType, aValue);
155  m_paramStack.push_back( item );
156 }
157 
171 bool AM_PARAM::ReadParam( char*& aText )
172 {
173  bool found = false;
174  int ivalue;
175  double dvalue;
176  bool end = false;
177 
178  while( !end )
179  {
180  switch( *aText )
181  {
182  case ',':
183  aText++;
184 
185  if( !found ) // happens when a string starts by ',' before any param
186  break; // just skip this separator
187  // fall through
188  case '\n':
189  case '\r':
190  case 0: // EOL
191  case '*': // Terminator in a gerber command
192  end = true;
193  break;
194 
195  case ' ':
196  aText++;
197  break;
198 
199  case '$':
200  // defered value defined later, in ADD command which define defered parameters
201  ++aText;
202  ivalue = ReadInt( aText, false );
203  if( m_index < 1 )
204  SetIndex( ivalue );
205  PushOperator( PUSHPARM, ivalue );
206  found = true;
207  break;
208 
209  case '/':
210  PushOperator( DIV );
211  aText++;
212  break;
213 
214  case '(': // Open a block to evaluate an expression between '(' and ')'
216  aText++;
217  break;
218 
219  case ')': // close a block between '(' and ')'
221  aText++;
222  break;
223 
224  case 'x':
225  case 'X':
226  PushOperator( MUL );
227  aText++;
228  break;
229 
230  case '-':
231  case '+':
232  // Test if this is an operator between 2 params, or the sign of a value
233  if( m_paramStack.size() > 0 && !m_paramStack.back().IsOperator() )
234  { // Seems an operator
235  PushOperator( *aText == '+' ? ADD : SUB );
236  aText++;
237  }
238  else
239  { // seems the sign of a value
240  dvalue = ReadDouble( aText, false );
241  PushOperator( PUSHVALUE, dvalue );
242  found = true;
243  }
244  break;
245 
246  case '=': // A local definition found like $4=$3/2
247  // At this point, one defered parameter is expected to be read.
248  // this parameter value (the index) is stored in m_index.
249  // The list of items is cleared
250  aText++;
251  m_paramStack.clear();
252  found = false;
253  break;
254 
255  default:
256  dvalue = ReadDouble( aText, false );
257  PushOperator( PUSHVALUE, dvalue );
258  found = true;
259  break;
260  }
261  }
262 
263  return found;
264 }
std::vector< AM_PARAM_ITEM > m_paramStack
Definition: am_param.h:290
Definition: am_param.h:150
double GetParam(unsigned aIdx) const
GetParam() Returns a parameter stored in parameter list.
Definition: dcode.h:141
unsigned GetIndex() const
Definition: am_param.h:257
double ReadDouble(char *&text, bool aSkipSeparator=true)
Function ReadDouble reads a double from an ASCII character buffer.
Class AM_PARAM holds an operand for an AM_PARAM as defined within standard RS274X.
Definition: am_param.h:220
double Evaluate(AM_PARAM_EVAL_STACK &aExp)
Evaluate an basic arithmetic expression (infix notation) with precedence The expression is a sequence...
Definition: evaluate.cpp:102
bool ReadParam(char *&aText)
Function ReadParam Read one aperture macro parameter a parameter can be: a number a reference to an a...
Definition: am_param.cpp:171
void PushOperator(parm_item_type aType, double aValue)
function PushOperator add an operator/operand to the current stack
Definition: am_param.cpp:146
parm_item_type
Definition: am_param.h:148
parm_item_type GetType() const
Definition: am_param.h:252
int m_Num_Dcode
D code value ( >= 10 )
Definition: dcode.h:96
Definition: am_param.h:150
double GetValue(const D_CODE *aDcode) const
Definition: am_param.cpp:70
unsigned GetParamCount() const
GetParamCount() Returns the number of parameters stored in parameter list.
Definition: dcode.h:131
Definition: am_param.h:150
std::vector< AM_PARAM_EVAL > AM_PARAM_EVAL_STACK
Definition: am_param.h:210
bool IsImmediate() const
Function IsImmediate tests if this AM_PARAM holds an immediate parameter or is a pointer into a param...
Definition: am_param.cpp:55
This helper class hold a value or an arithmetic operator to calculate the final value of a aperture m...
Definition: am_param.h:160
double GetLocalParam(const D_CODE *aDcode, unsigned aParamId) const
function GetLocalParam Usually, parameters are defined inside the aperture primitive using immediate ...
Class D_CODE holds a gerber DCODE (also called Aperture) definition.
Definition: dcode.h:82
int ReadInt(char *&text, bool aSkipSeparator=true)
Function ReadInt reads an int from an ASCII character buffer.
double GetValue() const
Definition: am_param.h:247
APERTURE_MACRO * GetMacro() const
Definition: dcode.h:157
void SetIndex(int aIndex)
Definition: am_param.h:321
Struct APERTURE_MACRO helps support the "aperture macro" defined within standard RS274X.
Definition: am_primitive.h:163
int m_index
Definition: am_param.h:289
Definition: am_param.h:150
AM_PARAM()
Definition: am_param.cpp:45