KiCad PCB EDA Suite
AM_PARAM Class Reference

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

#include <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

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 am_param.h.

Constructor & Destructor Documentation

◆ AM_PARAM()

AM_PARAM::AM_PARAM ( )

Definition at line 46 of file am_param.cpp.

47 {
48  m_index = -1;
49 }
int m_index
Definition: am_param.h:289

References m_index.

Member Function Documentation

◆ GetIndex()

unsigned AM_PARAM::GetIndex ( ) const
inline

Definition at line 316 of file am_param.h.

317  {
318  return (unsigned) m_index;
319  }
int m_index
Definition: am_param.h:289

References m_index.

◆ GetValue()

double AM_PARAM::GetValue ( const D_CODE aDcode) const

Definition at line 71 of file am_param.cpp.

72 {
73  // In macros, actual values are sometimes given by an expression like:
74  // 0-$2/2-$4
75  // Because arithmetic predence is used, the parameters (values (double) and operators)
76  // are stored in a stack, with all numeric values converted to the actual values
77  // when they are defered parameters
78  // Each item is stored in a AM_PARAM_EVAL (a value or an operator)
79  //
80  // Then the stack with all values resolved is parsed and numeric values
81  // calculated according to the precedence of operators
82  double curr_value = 0.0;
83  parm_item_type op_code;
84 
86 
87  for( unsigned ii = 0; ii < m_paramStack.size(); ii++ )
88  {
89  AM_PARAM_ITEM item = m_paramStack[ii];
90 
91  switch( item.GetType() )
92  {
93  case ADD:
94  case SUB:
95  case MUL:
96  case DIV: // just an operator for next parameter value
97  case OPEN_PAR:
98  case CLOSE_PAR: // Priority modifiers: store in stack
99  op_code = item.GetType();
100  ops.push_back( AM_PARAM_EVAL( op_code ) );
101  break;
102 
103  case PUSHPARM:
104  // a defered value: get the actual parameter from the aDcode
105  if( aDcode ) // should be always true here
106  {
107  if( item.GetIndex() <= aDcode->GetParamCount() )
108  {
109  curr_value = aDcode->GetParam( item.GetIndex() );
110  }
111  else // Get parameter from local param definition
112  {
113  const APERTURE_MACRO * am_parent = aDcode->GetMacro();
114  curr_value = am_parent->GetLocalParam( aDcode, item.GetIndex() );
115  }
116  }
117  else
118  {
119  wxLogDebug( wxT( "AM_PARAM::GetValue(): NULL param aDcode\n" ) );
120  }
121 
122  ops.push_back( AM_PARAM_EVAL( curr_value ) );
123  break;
124 
125  case PUSHVALUE: // a value is on the stack:
126  curr_value = item.GetValue();
127  ops.push_back( AM_PARAM_EVAL( curr_value ) );
128  break;
129 
130  default:
131  wxLogDebug( "AM_PARAM::GetValue(): dcode %d prm %d/%d: unexpected type %d",
132  aDcode ? aDcode->m_Num_Dcode : -1, ii, m_paramStack.size(), item.GetType() );
133  break;
134  }
135  }
136 
137  double result = Evaluate( ops );
138 
139  return result;
140 }
std::vector< AM_PARAM_ITEM > m_paramStack
Definition: am_param.h:290
unsigned GetParamCount() const
GetParamCount() Returns the number of parameters stored in parameter list.
Definition: dcode.h:131
unsigned GetIndex() const
Definition: am_param.h:257
parm_item_type GetType() const
Definition: am_param.h:252
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
double GetLocalParam(const D_CODE *aDcode, unsigned aParamId) const
function GetLocalParam Usually, parameters are defined inside the aperture primitive using immediate ...
parm_item_type
Definition: am_param.h:148
int m_Num_Dcode
D code value ( >= 10 )
Definition: dcode.h:96
Definition: am_param.h:150
double GetParam(unsigned aIdx) const
GetParam() Returns a parameter stored in parameter list.
Definition: dcode.h:141
std::vector< AM_PARAM_EVAL > AM_PARAM_EVAL_STACK
Definition: am_param.h:210
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 GetValue() const
Definition: am_param.h:247
APERTURE_MACRO * GetMacro() const
Definition: dcode.h:157
Struct APERTURE_MACRO helps support the "aperture macro" defined within standard RS274X.
Definition: am_primitive.h:163

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().

◆ IsImmediate()

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 56 of file am_param.cpp.

57 {
58  bool is_immediate = true;
59  for( unsigned ii = 0; ii < m_paramStack.size(); ii++ )
60  {
61  if( m_paramStack[ii].IsDefered() )
62  { // a defered value is found in operand list,
63  // so the parameter is not immediate
64  is_immediate = false;
65  break;
66  }
67  }
68  return is_immediate;
69 }
std::vector< AM_PARAM_ITEM > m_paramStack
Definition: am_param.h:290

References m_paramStack.

◆ PushOperator() [1/2]

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 147 of file am_param.cpp.

148 {
149  AM_PARAM_ITEM item( aType, aValue);
150  m_paramStack.push_back( item );
151 }
std::vector< AM_PARAM_ITEM > m_paramStack
Definition: am_param.h:290
AM_PARAM holds an operand for an AM_PARAM as defined within standard RS274X.
Definition: am_param.h:220

References m_paramStack.

Referenced by ReadParam().

◆ PushOperator() [2/2]

void AM_PARAM::PushOperator ( parm_item_type  aType,
int  aValue = 0 
)

Definition at line 153 of file am_param.cpp.

154 {
155  AM_PARAM_ITEM item( aType, aValue);
156  m_paramStack.push_back( item );
157 }
std::vector< AM_PARAM_ITEM > m_paramStack
Definition: am_param.h:290
AM_PARAM holds an operand for an AM_PARAM as defined within standard RS274X.
Definition: am_param.h:220

References m_paramStack.

◆ ReadParam()

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 172 of file am_param.cpp.

173 {
174  bool found = false;
175  int ivalue;
176  double dvalue;
177  bool end = false;
178 
179  while( !end )
180  {
181  switch( *aText )
182  {
183  case ',':
184  aText++;
185 
186  if( !found ) // happens when a string starts by ',' before any param
187  break; // just skip this separator
188 
190 
191  case '\n':
192  case '\r':
193  case 0: // EOL
194  case '*': // Terminator in a gerber command
195  end = true;
196  break;
197 
198  case ' ':
199  aText++;
200  break;
201 
202  case '$':
203  // defered value defined later, in ADD command which define defered parameters
204  ++aText;
205  ivalue = ReadInt( aText, false );
206  if( m_index < 1 )
207  SetIndex( ivalue );
208  PushOperator( PUSHPARM, ivalue );
209  found = true;
210  break;
211 
212  case '/':
213  PushOperator( DIV );
214  aText++;
215  break;
216 
217  case '(': // Open a block to evaluate an expression between '(' and ')'
219  aText++;
220  break;
221 
222  case ')': // close a block between '(' and ')'
224  aText++;
225  break;
226 
227  case 'x':
228  case 'X':
229  PushOperator( MUL );
230  aText++;
231  break;
232 
233  case '-':
234  case '+':
235  // Test if this is an operator between 2 params, or the sign of a value
236  if( m_paramStack.size() > 0 && !m_paramStack.back().IsOperator() )
237  { // Seems an operator
238  PushOperator( *aText == '+' ? ADD : SUB );
239  aText++;
240  }
241  else
242  { // seems the sign of a value
243  dvalue = ReadDouble( aText, false );
244  PushOperator( PUSHVALUE, dvalue );
245  found = true;
246  }
247  break;
248 
249  case '=': // A local definition found like $4=$3/2
250  // At this point, one defered parameter is expected to be read.
251  // this parameter value (the index) is stored in m_index.
252  // The list of items is cleared
253  aText++;
254  m_paramStack.clear();
255  found = false;
256  break;
257 
258  default:
259  dvalue = ReadDouble( aText, false );
260  PushOperator( PUSHVALUE, dvalue );
261  found = true;
262  break;
263  }
264  }
265 
266  return found;
267 }
std::vector< AM_PARAM_ITEM > m_paramStack
Definition: am_param.h:290
#define KI_FALLTHROUGH
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
Definition: am_param.cpp:147
Definition: am_param.h:150
int ReadInt(char *&text, bool aSkipSeparator=true)
Function ReadInt reads an int from an ASCII character buffer.
void SetIndex(int aIndex)
Definition: am_param.h:321
int m_index
Definition: am_param.h:289

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

Referenced by GERBER_FILE_IMAGE::ReadApertureMacro().

◆ SetIndex()

void AM_PARAM::SetIndex ( int  aIndex)
inline

Definition at line 321 of file am_param.h.

322  {
323  m_index = aIndex;
324  }
int m_index
Definition: am_param.h:289

References m_index.

Referenced by ReadParam().

Member Data Documentation

◆ m_index

int AM_PARAM::m_index
private

Definition at line 289 of file am_param.h.

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

◆ m_paramStack

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

Definition at line 290 of file am_param.h.

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


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