You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

268 lines
9.1 KiB

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