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.

225 lines
8.8 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 1992-2022 KiCad Developers, see AUTHORS.txt for contributors.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, you may find one here:
  18. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  19. * or you may search the http://www.gnu.org website for the version 2 license,
  20. * or you may write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  22. */
  23. #ifndef EDA_UNITS_H
  24. #define EDA_UNITS_H
  25. #include <wx/string.h>
  26. #include <geometry/eda_angle.h>
  27. #include <base_units.h>
  28. /**
  29. * The type of unit.
  30. */
  31. enum class EDA_DATA_TYPE
  32. {
  33. DISTANCE = 0,
  34. AREA = 1,
  35. VOLUME = 2
  36. };
  37. enum class EDA_UNITS
  38. {
  39. INCHES = 0,
  40. MILLIMETRES = 1,
  41. UNSCALED = 2,
  42. DEGREES = 3,
  43. PERCENT = 4,
  44. MILS = 5,
  45. };
  46. namespace EDA_UNIT_UTILS
  47. {
  48. bool IsImperialUnit( EDA_UNITS aUnit );
  49. bool IsMetricUnit( EDA_UNITS aUnit );
  50. /**
  51. * Convert mm to mils.
  52. */
  53. int Mm2mils( double aVal );
  54. /**
  55. * Convert mils to mm.
  56. */
  57. int Mils2mm( double aVal );
  58. /**
  59. * Writes any unit info found in the string to aUnits.
  60. * @return true - when unit was found, false - when unit could not be determined
  61. */
  62. bool FetchUnitsFromString( const wxString& aTextValue, EDA_UNITS& aUnits );
  63. /**
  64. * Get the units string for a given units type.
  65. *
  66. * This version is for appending to a value string.
  67. *
  68. * @param aUnits The units requested.
  69. * @param aType DISTANCE, AREA, or VOLUME
  70. * @return The human readable units string with appropriate separators.
  71. */
  72. wxString GetText( EDA_UNITS aUnits, EDA_DATA_TYPE aType = EDA_DATA_TYPE::DISTANCE );
  73. /**
  74. * Get the units string for a given units type.
  75. *
  76. * This version is for setting a wxStaticText label.
  77. *
  78. * @param aUnits The units requested.
  79. * @param aType DISTANCE, AREA, or VOLUME
  80. * @return The human readable units string.
  81. */
  82. wxString GetLabel( EDA_UNITS aUnits, EDA_DATA_TYPE aType = EDA_DATA_TYPE::DISTANCE );
  83. /**
  84. * Converts \a aAngle from board units to a string appropriate for writing to file.
  85. *
  86. * This should only be used for writing to files as it ignores locale
  87. *
  88. * @note Internal angles for board items can be either degrees or tenths of degree
  89. * on how KiCad is built.
  90. * @param aAngle A angle value to convert.
  91. * @return std::string object containing the converted angle.
  92. */
  93. std::string FormatAngle( const EDA_ANGLE& aAngle );
  94. /**
  95. * Converts \a aValue from internal units to a string appropriate for writing to file.
  96. *
  97. * This should only be used for writing to files as it ignores locale
  98. *
  99. * @note Internal units for board items can be either deci-mils or nanometers depending
  100. * on how KiCad is built.
  101. *
  102. * @param aValue A coordinate value to convert.
  103. * @return A std::string object containing the converted value.
  104. */
  105. std::string FormatInternalUnits( const EDA_IU_SCALE& aIuScale, int aValue );
  106. std::string FormatInternalUnits( const EDA_IU_SCALE& aIuScale, const VECTOR2I& aPoint );
  107. constexpr inline int Mils2IU( const EDA_IU_SCALE& aIuScale, int mils )
  108. {
  109. double x = mils * aIuScale.IU_PER_MILS;
  110. return int( x < 0 ? x - 0.5 : x + 0.5 );
  111. }
  112. namespace UI
  113. {
  114. /**
  115. * Function To_User_Unit
  116. * convert \a aValue in internal units to the appropriate user units defined by \a aUnit.
  117. *
  118. * @return The converted value, in double
  119. * @param aUnit The units to convert \a aValue to.
  120. * @param aValue The value in internal units to convert.
  121. */
  122. double ToUserUnit( const EDA_IU_SCALE& aIuScale, EDA_UNITS aUnit, double aValue );
  123. /**
  124. * Returns the string from \a aValue according to \a aUnits (inch, mm ...) for display.
  125. *
  126. * For readability, if the mantissa has 3 or more digits then any trailing 0's are removed.
  127. * This function should be used to display values in dialogs because a value entered in mm
  128. * (for instance 2.0 mm) could need up to 8 digits mantissa to preserve precision.
  129. *
  130. * @param aUnits Units (INCHES, MILLIMETRE ..)
  131. * @param aValue Value in internal units
  132. * @param aAddUnitsText Add units text with appropriate separators
  133. * @param aType DISTANCE, AREA, or VOLUME
  134. * @return A wxString object containing value and optionally the symbol unit (like 2.000 mm)
  135. */
  136. wxString StringFromValue( const EDA_IU_SCALE& aIuScale, EDA_UNITS aUnits, double aValue,
  137. bool aAddUnitsText = false,
  138. EDA_DATA_TYPE aType = EDA_DATA_TYPE::DISTANCE );
  139. /**
  140. * A helper to convert the \a double length \a aValue to a string in inches, millimeters,
  141. * or unscaled units.
  142. *
  143. * Should be used only to display a coordinate in status, but not in dialogs, files, etc.,
  144. * because the mantissa of the number displayed has 4 digits max for readability. The
  145. * actual internal value could need up to 8 digits to preserve precision.
  146. *
  147. * @param aUnits Units (INCHES, MILLIMETRE ..)
  148. * @param aValue The double value to convert.
  149. * @param aAddUnitsText If true, adds the unit label to the end of the string
  150. * @param aType DISTANCE, AREA, or VOLUME
  151. * @return The converted string for display in user interface elements.
  152. */
  153. wxString MessageTextFromValue( const EDA_IU_SCALE& aIuScale, EDA_UNITS aUnits,
  154. double aValue, bool aAddUnitsText = true,
  155. EDA_DATA_TYPE aType = EDA_DATA_TYPE::DISTANCE );
  156. wxString MessageTextFromValue( const EDA_IU_SCALE& aIuScale, EDA_UNITS aUnits,
  157. int aValue, bool aAddUnitLabel = true,
  158. EDA_DATA_TYPE aType = EDA_DATA_TYPE::DISTANCE );
  159. wxString MessageTextFromValue( const EDA_IU_SCALE& aIuScale, EDA_UNITS aUnits,
  160. long long int aValue, bool aAddUnitLabel = true,
  161. EDA_DATA_TYPE aType = EDA_DATA_TYPE::DISTANCE );
  162. wxString MessageTextFromValue( EDA_ANGLE aValue, bool aAddUnitLabel = true );
  163. /**
  164. * Return in internal units the value "val" given in a real unit such as "in", "mm" or "deg"
  165. */
  166. double FromUserUnit( const EDA_IU_SCALE& aIuScale, EDA_UNITS aUnit, double aValue );
  167. /**
  168. * Function DoubleValueFromString
  169. * converts \a aTextValue to a double
  170. * @warning This utilizes the current locale and will break if decimal formats differ
  171. *
  172. * @param aIuScale The internal units scale for the current frame/app.
  173. * @param aUnits The units of \a aTextValue.
  174. * @param aTextValue A reference to a wxString object containing the string to convert.
  175. * @return A double representing that value in internal units
  176. */
  177. double DoubleValueFromString( const EDA_IU_SCALE& aIuScale, EDA_UNITS aUnits,
  178. const wxString& aTextValue,
  179. EDA_DATA_TYPE aType = EDA_DATA_TYPE::DISTANCE );
  180. double DoubleValueFromString( const wxString& aTextValue );
  181. /**
  182. * Function ValueFromString
  183. * converts \a aTextValue in \a aUnits to internal units used by the application.
  184. * @warning This utilizes the current locale and will break if decimal formats differ
  185. *
  186. * @param aIuScale The internal units scale for the current frame/app.
  187. * @param aUnits The units of \a aTextValue.
  188. * @param aTextValue A reference to a wxString object containing the string to convert.
  189. * @return A long long int representing that value in internal units
  190. */
  191. long long int ValueFromString( const EDA_IU_SCALE& aIuScale, EDA_UNITS aUnits,
  192. const wxString& aTextValue,
  193. EDA_DATA_TYPE aType = EDA_DATA_TYPE::DISTANCE );
  194. long long int ValueFromString( const wxString& aTextValue);
  195. }
  196. }
  197. #endif