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.

253 lines
11 KiB

3 years ago
3 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2020 CERN
  5. * Copyright (C) 2021-2023 KiCad Developers, see AUTHORS.txt for contributors.
  6. * @author Jon Evans <jon@craftyjon.com>
  7. *
  8. * This program is free software: you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation, either version 3 of the License, or (at your
  11. * option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include <base_screen.h>
  22. #include <lib_symbol.h>
  23. #include <default_values.h>
  24. #include <eeschema_settings.h>
  25. #include <macros.h>
  26. #include <pgm_base.h>
  27. #include <schematic_settings.h>
  28. #include <settings/json_settings_internals.h>
  29. #include <settings/parameters.h>
  30. #include <settings/settings_manager.h>
  31. #include <sim/spice_settings.h>
  32. const int schSettingsSchemaVersion = 1;
  33. SCHEMATIC_SETTINGS::SCHEMATIC_SETTINGS( JSON_SETTINGS* aParent, const std::string& aPath ) :
  34. NESTED_SETTINGS( "schematic", schSettingsSchemaVersion, aParent, aPath ),
  35. m_DefaultLineWidth( DEFAULT_LINE_WIDTH_MILS * schIUScale.IU_PER_MILS ),
  36. m_DefaultTextSize( DEFAULT_TEXT_SIZE * schIUScale.IU_PER_MILS ),
  37. m_LabelSizeRatio( DEFAULT_LABEL_SIZE_RATIO ),
  38. m_TextOffsetRatio( DEFAULT_TEXT_OFFSET_RATIO ),
  39. m_PinSymbolSize( DEFAULT_TEXT_SIZE * schIUScale.IU_PER_MILS / 2 ),
  40. m_JunctionSizeChoice( 3 ),
  41. m_JunctionSize( DEFAULT_JUNCTION_DIAM * schIUScale.IU_PER_MILS ),
  42. m_AnnotateStartNum( 0 ),
  43. m_IntersheetRefsShow( false ),
  44. m_IntersheetRefsListOwnPage( true ),
  45. m_IntersheetRefsFormatShort( false ),
  46. m_IntersheetRefsPrefix( DEFAULT_IREF_PREFIX ),
  47. m_IntersheetRefsSuffix( DEFAULT_IREF_SUFFIX ),
  48. m_DashedLineDashRatio( 12.0 ),
  49. m_DashedLineGapRatio( 3.0 ),
  50. m_OPO_VPrecision( 3 ),
  51. m_OPO_VRange( wxS( "~V" ) ),
  52. m_OPO_IPrecision( 3 ),
  53. m_OPO_IRange( wxS( "~A" ) ),
  54. m_SpiceCurSheetAsRoot( false ),
  55. m_SpiceSaveAllVoltages( false ),
  56. m_SpiceSaveAllCurrents( false ),
  57. m_SpiceSaveAllDissipations( false ),
  58. m_SpiceModelCurSheetAsRoot( true ),
  59. m_NgspiceSimulatorSettings( nullptr )
  60. {
  61. EESCHEMA_SETTINGS* appSettings = Pgm().GetSettingsManager().GetAppSettings<EESCHEMA_SETTINGS>();
  62. int defaultLineThickness =
  63. appSettings ? appSettings->m_Drawing.default_line_thickness : DEFAULT_LINE_WIDTH_MILS;
  64. int defaultTextSize =
  65. appSettings ? appSettings->m_Drawing.default_text_size : DEFAULT_TEXT_SIZE;
  66. int defaultPinSymbolSize =
  67. appSettings ? appSettings->m_Drawing.pin_symbol_size : DEFAULT_TEXT_SIZE / 2;
  68. int defaultJunctionSizeChoice =
  69. appSettings ? appSettings->m_Drawing.junction_size_choice : 3;
  70. bool defaultIntersheetsRefShow =
  71. appSettings ? appSettings->m_Drawing.intersheets_ref_show : false;
  72. bool defaultIntersheetsRefOwnPage =
  73. appSettings ? appSettings->m_Drawing.intersheets_ref_own_page : true;
  74. bool defaultIntersheetsRefFormatShort =
  75. appSettings ? appSettings->m_Drawing.intersheets_ref_short : false;
  76. wxString defaultIntersheetsRefPrefix =
  77. appSettings ? appSettings->m_Drawing.intersheets_ref_prefix : wxString( wxS( DEFAULT_IREF_PREFIX ) );
  78. wxString defaultIntersheetsRefSuffix =
  79. appSettings ? appSettings->m_Drawing.intersheets_ref_suffix : wxString( wxS( DEFAULT_IREF_SUFFIX ) );
  80. m_params.emplace_back( new PARAM<bool>( "drawing.intersheets_ref_show",
  81. &m_IntersheetRefsShow, defaultIntersheetsRefShow ) );
  82. m_params.emplace_back( new PARAM<bool>( "drawing.intersheets_ref_own_page",
  83. &m_IntersheetRefsListOwnPage, defaultIntersheetsRefOwnPage ) );
  84. m_params.emplace_back( new PARAM<bool>( "drawing.intersheets_ref_short",
  85. &m_IntersheetRefsFormatShort, defaultIntersheetsRefFormatShort ) );
  86. m_params.emplace_back( new PARAM<wxString>( "drawing.intersheets_ref_prefix",
  87. &m_IntersheetRefsPrefix, defaultIntersheetsRefPrefix ) );
  88. m_params.emplace_back( new PARAM<wxString>( "drawing.intersheets_ref_suffix",
  89. &m_IntersheetRefsSuffix, defaultIntersheetsRefSuffix ) );
  90. m_params.emplace_back( new PARAM<double>( "drawing.dashed_lines_dash_length_ratio",
  91. &m_DashedLineDashRatio, 12.0 ) ); // Default from ISO 128-2
  92. m_params.emplace_back( new PARAM<double>( "drawing.dashed_lines_gap_length_ratio",
  93. &m_DashedLineGapRatio, 3.0 ) ); // Default from ISO 128-2
  94. m_params.emplace_back( new PARAM<int>( "drawing.operating_point_overlay_v_precision",
  95. &m_OPO_VPrecision, 3 ) );
  96. m_params.emplace_back( new PARAM<wxString>( "drawing.operating_point_overlay_v_range",
  97. &m_OPO_VRange, wxS( "~V" ) ) );
  98. m_params.emplace_back( new PARAM<int>( "drawing.operating_point_overlay_i_precision",
  99. &m_OPO_IPrecision, 3 ) );
  100. m_params.emplace_back( new PARAM<wxString>( "drawing.operating_point_overlay_i_range",
  101. &m_OPO_IRange, wxS( "~A" ) ) );
  102. m_params.emplace_back( new PARAM_SCALED<int>( "drawing.default_line_thickness",
  103. &m_DefaultLineWidth, schIUScale.MilsToIU( defaultLineThickness ), schIUScale.MilsToIU( 5 ), schIUScale.MilsToIU( 1000 ),
  104. 1 / schIUScale.IU_PER_MILS ) );
  105. m_params.emplace_back( new PARAM_SCALED<int>( "drawing.default_text_size",
  106. &m_DefaultTextSize, schIUScale.MilsToIU( defaultTextSize ), schIUScale.MilsToIU( 5 ), schIUScale.MilsToIU( 1000 ),
  107. 1 / schIUScale.IU_PER_MILS ) );
  108. m_params.emplace_back( new PARAM<double>( "drawing.text_offset_ratio",
  109. &m_TextOffsetRatio, DEFAULT_TEXT_OFFSET_RATIO, 0.0, 2.0 ) );
  110. m_params.emplace_back( new PARAM<double>( "drawing.label_size_ratio",
  111. &m_LabelSizeRatio, DEFAULT_LABEL_SIZE_RATIO, 0.0, 2.0 ) );
  112. m_params.emplace_back( new PARAM_SCALED<int>( "drawing.pin_symbol_size",
  113. &m_PinSymbolSize, schIUScale.MilsToIU( defaultPinSymbolSize ), schIUScale.MilsToIU( 0 ), schIUScale.MilsToIU( 1000 ),
  114. 1 / schIUScale.IU_PER_MILS ) );
  115. // m_JunctionSize is only a run-time cache of the calculated size. Do not save it.
  116. // User choice for junction dot size ( e.g. none = 0, smallest = 1, small = 2, etc )
  117. m_params.emplace_back( new PARAM<int>( "drawing.junction_size_choice",
  118. &m_JunctionSizeChoice,
  119. defaultJunctionSizeChoice ) );
  120. m_params.emplace_back( new PARAM_LAMBDA<nlohmann::json>( "drawing.field_names",
  121. [&]() -> nlohmann::json
  122. {
  123. nlohmann::json ret = nlohmann::json::array();
  124. for( const TEMPLATE_FIELDNAME& field :
  125. m_TemplateFieldNames.GetTemplateFieldNames( false ) )
  126. {
  127. ret.push_back( nlohmann::json( {
  128. { "name", field.m_Name },
  129. { "visible", field.m_Visible },
  130. { "url", field.m_URL }
  131. } ) );
  132. }
  133. return ret;
  134. },
  135. [&]( const nlohmann::json& aJson )
  136. {
  137. if( !aJson.empty() && aJson.is_array() )
  138. {
  139. m_TemplateFieldNames.DeleteAllFieldNameTemplates( false );
  140. for( const nlohmann::json& entry : aJson )
  141. {
  142. if( !entry.contains( "name" ) || !entry.contains( "url" )
  143. || !entry.contains( "visible" ) )
  144. {
  145. continue;
  146. }
  147. TEMPLATE_FIELDNAME field( entry["name"].get<wxString>() );
  148. field.m_URL = entry["url"].get<bool>();
  149. field.m_Visible = entry["visible"].get<bool>();
  150. m_TemplateFieldNames.AddTemplateFieldName( field, false );
  151. }
  152. }
  153. // Read global fieldname templates
  154. auto* cfg = Pgm().GetSettingsManager().GetAppSettings<EESCHEMA_SETTINGS>();
  155. if( cfg && !cfg->m_Drawing.field_names.IsEmpty() )
  156. m_TemplateFieldNames.AddTemplateFieldNames( cfg->m_Drawing.field_names );
  157. }, {} ) );
  158. m_params.emplace_back( new PARAM<wxString>( "page_layout_descr_file",
  159. &m_SchDrawingSheetFileName, "" ) );
  160. m_params.emplace_back( new PARAM<wxString>( "plot_directory",
  161. &m_PlotDirectoryName, "" ) );
  162. m_params.emplace_back( new PARAM<wxString>( "net_format_name",
  163. &m_NetFormatName, "" ) );
  164. m_params.emplace_back( new PARAM<bool>( "spice_current_sheet_as_root",
  165. &m_SpiceCurSheetAsRoot, false ) );
  166. m_params.emplace_back( new PARAM<bool>( "spice_save_all_voltages",
  167. &m_SpiceSaveAllVoltages, false ) );
  168. m_params.emplace_back( new PARAM<bool>( "spice_save_all_currents",
  169. &m_SpiceSaveAllCurrents, false ) );
  170. m_params.emplace_back( new PARAM<bool>( "spice_save_all_dissipations",
  171. &m_SpiceSaveAllDissipations, false ) );
  172. m_params.emplace_back( new PARAM<bool>( "spice_model_current_sheet_as_root",
  173. &m_SpiceModelCurSheetAsRoot, true ) );
  174. m_params.emplace_back( new PARAM<wxString>( "spice_external_command",
  175. &m_SpiceCommandString, "spice \"%I\"" ) );
  176. // TODO(JE) should we keep these LIB_SYMBOL:: things around?
  177. m_params.emplace_back( new PARAM<int>( "subpart_id_separator",
  178. LIB_SYMBOL::SubpartIdSeparatorPtr(), 0, 0, 126 ) );
  179. m_params.emplace_back( new PARAM<int>( "subpart_first_id",
  180. LIB_SYMBOL::SubpartFirstIdPtr(), 'A', '1', 'z' ) );
  181. m_params.emplace_back( new PARAM<int>( "annotate_start_num",
  182. &m_AnnotateStartNum, 0 ) );
  183. m_NgspiceSimulatorSettings =
  184. std::make_shared<NGSPICE_SIMULATOR_SETTINGS>( this, "ngspice" );
  185. registerMigration( 0, 1,
  186. [&]() -> bool
  187. {
  188. std::optional<double> tor = Get<double>( "drawing.text_offset_ratio" );
  189. if( tor )
  190. Set( "drawing.label_size_ratio", *tor );
  191. return true;
  192. } );
  193. }
  194. SCHEMATIC_SETTINGS::~SCHEMATIC_SETTINGS()
  195. {
  196. ReleaseNestedSettings( m_NgspiceSimulatorSettings.get() );
  197. m_NgspiceSimulatorSettings.reset();
  198. if( m_parent )
  199. {
  200. m_parent->ReleaseNestedSettings( this );
  201. m_parent = nullptr;
  202. }
  203. }