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.

246 lines
7.2 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2023 Mike Williams <mike@mikebwilliams.com>
  5. * Copyright (C) 2023 KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software: you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation, either version 3 of the License, or (at your
  10. * option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <settings/bom_settings.h>
  21. #include <nlohmann/json.hpp>
  22. #include <core/json_serializers.h>
  23. #include <wx/translation.h>
  24. // Implementations for BOM_FMT_PRESET
  25. bool BOM_FIELD::operator==( const BOM_FIELD& rhs ) const
  26. {
  27. return this->name == rhs.name && this->label == rhs.label && this->show == rhs.show
  28. && this->groupBy == rhs.groupBy;
  29. }
  30. bool operator!=( const BOM_FIELD& lhs, const BOM_FIELD& rhs )
  31. {
  32. return !( lhs == rhs );
  33. }
  34. bool operator<( const BOM_FIELD& lhs, const BOM_FIELD& rhs )
  35. {
  36. return lhs.name < rhs.name;
  37. }
  38. void to_json( nlohmann::json& j, const BOM_FIELD& f )
  39. {
  40. j = nlohmann::json{
  41. { "name", f.name }, { "label", f.label }, { "show", f.show }, { "group_by", f.groupBy }
  42. };
  43. }
  44. void from_json( const nlohmann::json& j, BOM_FIELD& f )
  45. {
  46. j.at( "name" ).get_to( f.name );
  47. j.at( "label" ).get_to( f.label );
  48. j.at( "show" ).get_to( f.show );
  49. j.at( "group_by" ).get_to( f.groupBy );
  50. }
  51. bool operator!=( const BOM_PRESET& lhs, const BOM_PRESET& rhs )
  52. {
  53. return !( lhs == rhs );
  54. }
  55. bool operator<( const BOM_PRESET& lhs, const BOM_PRESET& rhs )
  56. {
  57. return lhs.name < rhs.name;
  58. }
  59. void to_json( nlohmann::json& j, const BOM_PRESET& p )
  60. {
  61. j = nlohmann::json{
  62. { "name", p.name },
  63. { "sort_field", p.sortField },
  64. { "sort_asc", p.sortAsc },
  65. { "filter_string", p.filterString },
  66. { "group_symbols", p.groupSymbols },
  67. { "exclude_dnp", p.excludeDNP },
  68. };
  69. if( p.fieldsOrdered.size() > 0 )
  70. j["fields_ordered"] = p.fieldsOrdered;
  71. }
  72. void from_json( const nlohmann::json& j, BOM_PRESET& f )
  73. {
  74. j.at( "name" ).get_to( f.name );
  75. j.at( "fields_ordered" ).get_to( f.fieldsOrdered );
  76. j.at( "sort_field" ).get_to( f.sortField );
  77. j.at( "sort_asc" ).get_to( f.sortAsc );
  78. j.at( "filter_string" ).get_to( f.filterString );
  79. j.at( "group_symbols" ).get_to( f.groupSymbols );
  80. j.at( "exclude_dnp" ).get_to( f.excludeDNP );
  81. }
  82. // Implementations for BOM_PRESET
  83. bool BOM_PRESET::operator==( const BOM_PRESET& rhs ) const
  84. {
  85. return this->name == rhs.name
  86. && this->readOnly == rhs.readOnly
  87. && this->fieldsOrdered == rhs.fieldsOrdered
  88. && this->sortField == rhs.sortField
  89. && this->sortAsc == rhs.sortAsc
  90. && this->groupSymbols == rhs.groupSymbols
  91. && this->excludeDNP == rhs.excludeDNP;
  92. }
  93. BOM_PRESET BOM_PRESET::GroupedByValue()
  94. {
  95. BOM_PRESET p{ _HKI( "Grouped By Value" ), true, {}, _( "Reference" ), true, "", true, false };
  96. p.fieldsOrdered = std::vector<BOM_FIELD>{
  97. { "Reference", "Reference", true, false },
  98. { "Value", "Value", true, true },
  99. { "Datasheet", "Datasheet", true, false },
  100. { "Footprint", "Footprint", true, false },
  101. { "${QUANTITY}", "Qty", true, false },
  102. { "${DNP}", "DNP", true, true },
  103. };
  104. return p;
  105. }
  106. BOM_PRESET BOM_PRESET::GroupedByValueFootprint()
  107. {
  108. BOM_PRESET p{
  109. _HKI( "Grouped By Value and Footprint" ), true, {}, _( "Reference" ), true, "", true, false
  110. };
  111. p.fieldsOrdered = std::vector<BOM_FIELD>{
  112. { "Reference", "Reference", true, false },
  113. { "Value", "Value", true, true },
  114. { "Datasheet", "Datasheet", true, false },
  115. { "Footprint", "Footprint", true, true },
  116. { "${QUANTITY}", "Qty", true, false },
  117. { "${DNP}", "DNP", true, true },
  118. };
  119. return p;
  120. }
  121. BOM_PRESET BOM_PRESET::Attributes()
  122. {
  123. BOM_PRESET p{
  124. _HKI( "Attributes" ), true, {}, _( "Reference" ), true, "", true, false
  125. };
  126. p.fieldsOrdered = std::vector<BOM_FIELD>{
  127. { "Reference", "Reference", true, false },
  128. { "Value", "Value", true, true },
  129. { "Datasheet", "Datasheet", false, false },
  130. { "Footprint", "Footprint", false, true },
  131. { "${DNP}", "Do Not Place", true, false },
  132. { "${EXCLUDE_FROM_BOM}", "Exclude from BOM", true, false },
  133. { "${EXCLUDE_FROM_BOARD}", "Exclude from Board", true, false },
  134. { "${EXCLUDE_FROM_SIM}", "Exclude from Simulation", true, false },
  135. };
  136. return p;
  137. }
  138. std::vector<BOM_PRESET> BOM_PRESET::BuiltInPresets()
  139. {
  140. return { BOM_PRESET::GroupedByValue(), BOM_PRESET::GroupedByValueFootprint(),
  141. BOM_PRESET::Attributes() };
  142. }
  143. //Implementations for BOM_FMT_PRESET
  144. bool BOM_FMT_PRESET::operator==( const BOM_FMT_PRESET& rhs ) const
  145. {
  146. return this->name == rhs.name && this->readOnly == rhs.readOnly
  147. && this->fieldDelimiter == rhs.fieldDelimiter
  148. && this->stringDelimiter == rhs.stringDelimiter && this->refDelimiter == rhs.refDelimiter
  149. && this->refRangeDelimiter == rhs.refRangeDelimiter && this->keepTabs == rhs.keepTabs
  150. && this->keepLineBreaks == rhs.keepLineBreaks;
  151. }
  152. bool operator!=( const BOM_FMT_PRESET& lhs, const BOM_FMT_PRESET& rhs )
  153. {
  154. return !( lhs == rhs );
  155. }
  156. bool operator<( const BOM_FMT_PRESET& lhs, const BOM_FMT_PRESET& rhs )
  157. {
  158. return lhs.name < rhs.name;
  159. }
  160. void to_json( nlohmann::json& j, const BOM_FMT_PRESET& p )
  161. {
  162. j = nlohmann::json{ { "name", p.name },
  163. { "field_delimiter", p.fieldDelimiter },
  164. { "string_delimiter", p.stringDelimiter },
  165. { "ref_delimiter", p.refDelimiter },
  166. { "ref_range_delimiter", p.refRangeDelimiter },
  167. { "keep_tabs", p.keepTabs },
  168. { "keep_line_breaks", p.keepLineBreaks } };
  169. }
  170. void from_json( const nlohmann::json& j, BOM_FMT_PRESET& f )
  171. {
  172. j.at( "name" ).get_to( f.name );
  173. j.at( "field_delimiter" ).get_to( f.fieldDelimiter );
  174. j.at( "string_delimiter" ).get_to( f.stringDelimiter );
  175. j.at( "ref_delimiter" ).get_to( f.refDelimiter );
  176. j.at( "ref_range_delimiter" ).get_to( f.refRangeDelimiter );
  177. j.at( "keep_tabs" ).get_to( f.keepTabs );
  178. j.at( "keep_line_breaks" ).get_to( f.keepLineBreaks );
  179. }
  180. BOM_FMT_PRESET BOM_FMT_PRESET::CSV()
  181. {
  182. return { _HKI( "CSV" ), true, wxS( "," ), wxT( "\"" ), wxT( "," ), wxT( "" ), false, false };
  183. }
  184. BOM_FMT_PRESET BOM_FMT_PRESET::TSV()
  185. {
  186. return { _HKI( "TSV" ), true, wxS( "\t" ), wxT( "" ), wxT( "," ), wxT( "" ), false, false };
  187. }
  188. BOM_FMT_PRESET BOM_FMT_PRESET::Semicolons()
  189. {
  190. return {
  191. _HKI( "Semicolons" ), true, wxS( ";" ), wxT( "'" ), wxT( "," ), wxT( "" ), false, false
  192. };
  193. }
  194. std::vector<BOM_FMT_PRESET> BOM_FMT_PRESET::BuiltInPresets()
  195. {
  196. return { BOM_FMT_PRESET::CSV(), BOM_FMT_PRESET::TSV(), BOM_FMT_PRESET::Semicolons() };
  197. }