KiCad PCB EDA Suite
AM_PARAM Class Reference

Class AM_PARAM holds a parameter value for an "aperture macro" as defined within standard RS274X. More...

#include <class_am_param.h>

Public Member Functions

 AM_PARAM ()
 
void PushOperator (parm_item_type aType, double aValue)
 function PushOperator add an operator/operand to the current stack More...
 
void PushOperator (parm_item_type aType, int aValue=0)
 
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 parameter held by an owning D_CODE. More...
 
unsigned GetIndex () const
 
void SetIndex (int aIndex)
 
bool ReadParam (char *&aText)
 Function ReadParam Read one aperture macro parameter a parameter can be: a number a reference to an aperture definition parameter value: $1 ot $3 ... More...
 

Private Attributes

int m_index
 
std::vector< AM_PARAM_ITEMm_paramStack
 

Detailed Description

Class AM_PARAM holds 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 defered 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

Definition at line 286 of file class_am_param.h.

Constructor & Destructor Documentation

AM_PARAM::AM_PARAM ( )

Definition at line 45 of file class_am_param.cpp.

References m_index.

46 {
47  m_index = -1;
48 }

Member Function Documentation

unsigned AM_PARAM::GetIndex ( ) const
inline

Definition at line 316 of file class_am_param.h.

References m_index.

317  {
318  return (unsigned) m_index;
319  }
double AM_PARAM::GetValue ( const D_CODE aDcode) const

Definition at line 70 of file class_am_param.cpp.

References ADD, CLOSE_PAR, DIV, Evaluate(), AM_PARAM_ITEM::GetIndex(), APERTURE_MACRO::GetLocalParam(), D_CODE::GetMacro(), D_CODE::GetParam(), D_CODE::GetParamCount(), AM_PARAM_ITEM::GetType(), AM_PARAM_ITEM::GetValue(), D_CODE::m_Num_Dcode, m_paramStack, MUL, OPEN_PAR, PUSHPARM, PUSHVALUE, and SUB.

Referenced by APERTURE_MACRO::GetLocalParam().

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 }
std::vector< AM_PARAM_ITEM > m_paramStack
std::vector< AM_PARAM_EVAL > AM_PARAM_EVAL_STACK
double GetParam(unsigned aIdx) const
GetParam() Returns a parameter stored in parameter list.
Definition: dcode.h:141
unsigned GetIndex() const
Class AM_PARAM holds an operand for an AM_PARAM as defined within standard RS274X.
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
parm_item_type GetType() const
int m_Num_Dcode
D code value ( >= 10 )
Definition: dcode.h:96
unsigned GetParamCount() const
GetParamCount() Returns the number of parameters stored in parameter list.
Definition: dcode.h:131
This helper class hold a value or an arithmetic operator to calculate the final value of a aperture m...
double GetLocalParam(const D_CODE *aDcode, unsigned aParamId) const
function GetLocalParam Usually, parameters are defined inside the aperture primitive using immediate ...
parm_item_type
double GetValue() const
APERTURE_MACRO * GetMacro() const
Definition: dcode.h:157
Struct APERTURE_MACRO helps support the "aperture macro" defined within standard RS274X.
bool AM_PARAM::IsImmediate ( ) const

Function IsImmediate tests if this AM_PARAM holds an immediate parameter or is a pointer into a parameter held by an owning D_CODE.

Function IsImmediate tests if this AM_PARAM holds an immediate parameter or has parameter held by an owning D_CODE.

Returns
true if the value is immediate, i.e. no defered value in operands used in its definition

Definition at line 55 of file class_am_param.cpp.

References m_paramStack.

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 }
std::vector< AM_PARAM_ITEM > m_paramStack
void AM_PARAM::PushOperator ( parm_item_type  aType,
double  aValue 
)

function PushOperator add an operator/operand to the current stack

add an operator/operand to the current stack aType = NOP, PUSHVALUE, PUSHPARM, ADD, SUB, MUL, DIV, EQUATE aValue required only for PUSHVALUE (double) or PUSHPARM (int) aType.

Parameters
aType= the type of item (NOP, PUSHVALUE, PUSHPARM, ADD, SUB, MUL, DIV, EQUATE)
aValue= the item value, double for PUSHVALUE or int for PUSHPARM type.

Definition at line 146 of file class_am_param.cpp.

References m_paramStack.

Referenced by ReadParam().

147 {
148  AM_PARAM_ITEM item( aType, aValue);
149  m_paramStack.push_back( item );
150 }
std::vector< AM_PARAM_ITEM > m_paramStack
Class AM_PARAM holds an operand for an AM_PARAM as defined within standard RS274X.
void AM_PARAM::PushOperator ( parm_item_type  aType,
int  aValue = 0 
)

Definition at line 152 of file class_am_param.cpp.

References m_paramStack.

153 {
154  AM_PARAM_ITEM item( aType, aValue);
155  m_paramStack.push_back( item );
156 }
std::vector< AM_PARAM_ITEM > m_paramStack
Class AM_PARAM holds an operand for an AM_PARAM as defined within standard RS274X.
bool AM_PARAM::ReadParam ( char *&  aText)

Function ReadParam Read one aperture macro parameter a parameter can be: a number a reference to an aperture definition parameter value: $1 ot $3 ...

a parameter definition can be complex and have operators between numbers and/or other parameter like $1+3 or $2x2.. Parameters are separated by a comma ( of finish by *)

Parameters
aText= pointer to the parameter to read. Will be modified to point to the next field
Returns
true if a param is read, or false

a parameter definition can be complex and have operators between numbers and/or other parameter like $1+3 or $2x2.. Note minus sign is not always an operator. It can be the sign of a value. Parameters are separated by a comma ( of finish by *)

Parameters
aText= pointer to the parameter to read. Will be modified to point to the next field
Returns
true if a param is read, or false

Definition at line 171 of file class_am_param.cpp.

References ADD, CLOSE_PAR, DIV, m_index, m_paramStack, MUL, OPEN_PAR, PushOperator(), PUSHPARM, PUSHVALUE, ReadDouble(), ReadInt(), SetIndex(), and SUB.

Referenced by GERBER_FILE_IMAGE::ReadApertureMacro().

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  // fall through
185  case 0: // EOL
186  case '*': // Terminator in a gerber command
187  end = true;
188  break;
189 
190  case ' ':
191  aText++;
192  break;
193 
194  case '$':
195  // defered value defined later, in ADD command which define defered parameters
196  ++aText;
197  ivalue = ReadInt( aText, false );
198  if( m_index < 1 )
199  SetIndex( ivalue );
200  PushOperator( PUSHPARM, ivalue );
201  found = true;
202  break;
203 
204  case '/':
205  PushOperator( DIV );
206  aText++;
207  break;
208 
209  case '(': // Open a block to evaluate an expression between '(' and ')'
211  aText++;
212  break;
213 
214  case ')': // close a block between '(' and ')'
216  aText++;
217  break;
218 
219  case 'x':
220  case 'X':
221  PushOperator( MUL );
222  aText++;
223  break;
224 
225  case '-':
226  case '+':
227  // Test if this is an operator between 2 params, or the sign of a value
228  if( m_paramStack.size() > 0 && !m_paramStack.back().IsOperator() )
229  { // Seems an operator
230  PushOperator( *aText == '+' ? ADD : SUB );
231  aText++;
232  }
233  else
234  { // seems the sign of a value
235  dvalue = ReadDouble( aText, false );
236  PushOperator( PUSHVALUE, dvalue );
237  found = true;
238  }
239  break;
240 
241  case '=': // A local definition found like $4=$3/2
242  // At this point, one defered parameter is expected to be read.
243  // this parameter value (the index) is stored in m_index.
244  // The list of items is cleared
245  aText++;
246  m_paramStack.clear();
247  found = false;
248  break;
249 
250  default:
251  dvalue = ReadDouble( aText, false );
252  PushOperator( PUSHVALUE, dvalue );
253  found = true;
254  break;
255  }
256  }
257 
258  return found;
259 }
std::vector< AM_PARAM_ITEM > m_paramStack
double ReadDouble(char *&text, bool aSkipSeparator=true)
Function ReadDouble reads a double from an ASCII character buffer.
void PushOperator(parm_item_type aType, double aValue)
function PushOperator add an operator/operand to the current stack
int ReadInt(char *&text, bool aSkipSeparator=true)
Function ReadInt reads an int from an ASCII character buffer.
void SetIndex(int aIndex)
void AM_PARAM::SetIndex ( int  aIndex)
inline

Definition at line 321 of file class_am_param.h.

Referenced by ReadParam().

322  {
323  m_index = aIndex;
324  }

Member Data Documentation

int AM_PARAM::m_index
private

Definition at line 289 of file class_am_param.h.

Referenced by AM_PARAM(), GetIndex(), and ReadParam().

std::vector<AM_PARAM_ITEM> AM_PARAM::m_paramStack
private

Definition at line 290 of file class_am_param.h.

Referenced by GetValue(), IsImmediate(), PushOperator(), and ReadParam().


The documentation for this class was generated from the following files: