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
10 KiB

3 years ago
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-2022 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_SpiceAdjustPassiveValues( false ),
  51. m_SpiceCurSheetAsRoot( false ),
  52. m_SpiceSaveAllVoltages( false ),
  53. m_SpiceSaveAllCurrents( false ),
  54. m_SpiceModelCurSheetAsRoot( true ),
  55. m_NgspiceSimulatorSettings( nullptr )
  56. {
  57. EESCHEMA_SETTINGS* appSettings = Pgm().GetSettingsManager().GetAppSettings<EESCHEMA_SETTINGS>();
  58. int defaultLineThickness =
  59. appSettings ? appSettings->m_Drawing.default_line_thickness : DEFAULT_LINE_WIDTH_MILS;
  60. int defaultTextSize =
  61. appSettings ? appSettings->m_Drawing.default_text_size : DEFAULT_TEXT_SIZE;
  62. int defaultPinSymbolSize =
  63. appSettings ? appSettings->m_Drawing.pin_symbol_size : DEFAULT_TEXT_SIZE / 2;
  64. int defaultJunctionSizeChoice =
  65. appSettings ? appSettings->m_Drawing.junction_size_choice : 3;
  66. bool defaultIntersheetsRefShow =
  67. appSettings ? appSettings->m_Drawing.intersheets_ref_show : false;
  68. bool defaultIntersheetsRefOwnPage =
  69. appSettings ? appSettings->m_Drawing.intersheets_ref_own_page : true;
  70. bool defaultIntersheetsRefFormatShort =
  71. appSettings ? appSettings->m_Drawing.intersheets_ref_short : false;
  72. wxString defaultIntersheetsRefPrefix =
  73. appSettings ? appSettings->m_Drawing.intersheets_ref_prefix : DEFAULT_IREF_PREFIX;
  74. wxString defaultIntersheetsRefSuffix =
  75. appSettings ? appSettings->m_Drawing.intersheets_ref_suffix : DEFAULT_IREF_SUFFIX;
  76. m_params.emplace_back( new PARAM<bool>( "drawing.intersheets_ref_show",
  77. &m_IntersheetRefsShow, defaultIntersheetsRefShow ) );
  78. m_params.emplace_back( new PARAM<bool>( "drawing.intersheets_ref_own_page",
  79. &m_IntersheetRefsListOwnPage, defaultIntersheetsRefOwnPage ) );
  80. m_params.emplace_back( new PARAM<bool>( "drawing.intersheets_ref_short",
  81. &m_IntersheetRefsFormatShort, defaultIntersheetsRefFormatShort ) );
  82. m_params.emplace_back( new PARAM<wxString>( "drawing.intersheets_ref_prefix",
  83. &m_IntersheetRefsPrefix, defaultIntersheetsRefPrefix ) );
  84. m_params.emplace_back( new PARAM<wxString>( "drawing.intersheets_ref_suffix",
  85. &m_IntersheetRefsSuffix, defaultIntersheetsRefSuffix ) );
  86. m_params.emplace_back( new PARAM<double>( "drawing.dashed_lines_dash_length_ratio",
  87. &m_DashedLineDashRatio, 12.0 ) ); // Default from ISO 128-2
  88. m_params.emplace_back( new PARAM<double>( "drawing.dashed_lines_gap_length_ratio",
  89. &m_DashedLineGapRatio, 3.0 ) ); // Default from ISO 128-2
  90. m_params.emplace_back( new PARAM_SCALED<int>( "drawing.default_line_thickness",
  91. &m_DefaultLineWidth, schIUScale.MilsToIU( defaultLineThickness ), schIUScale.MilsToIU( 5 ), schIUScale.MilsToIU( 1000 ),
  92. 1 / schIUScale.IU_PER_MILS ) );
  93. m_params.emplace_back( new PARAM_SCALED<int>( "drawing.default_text_size",
  94. &m_DefaultTextSize, schIUScale.MilsToIU( defaultTextSize ), schIUScale.MilsToIU( 5 ), schIUScale.MilsToIU( 1000 ),
  95. 1 / schIUScale.IU_PER_MILS ) );
  96. m_params.emplace_back( new PARAM<double>( "drawing.text_offset_ratio",
  97. &m_TextOffsetRatio, DEFAULT_TEXT_OFFSET_RATIO, 0.0, 2.0 ) );
  98. m_params.emplace_back( new PARAM<double>( "drawing.label_size_ratio",
  99. &m_LabelSizeRatio, DEFAULT_LABEL_SIZE_RATIO, 0.0, 2.0 ) );
  100. m_params.emplace_back( new PARAM_SCALED<int>( "drawing.pin_symbol_size",
  101. &m_PinSymbolSize, schIUScale.MilsToIU( defaultPinSymbolSize ), schIUScale.MilsToIU( 0 ), schIUScale.MilsToIU( 1000 ),
  102. 1 / schIUScale.IU_PER_MILS ) );
  103. // m_JunctionSize is only a run-time cache of the calculated size. Do not save it.
  104. // User choice for junction dot size ( e.g. none = 0, smallest = 1, small = 2, etc )
  105. m_params.emplace_back( new PARAM<int>( "drawing.junction_size_choice",
  106. &m_JunctionSizeChoice,
  107. defaultJunctionSizeChoice ) );
  108. m_params.emplace_back( new PARAM_LAMBDA<nlohmann::json>( "drawing.field_names",
  109. [&]() -> nlohmann::json
  110. {
  111. nlohmann::json ret = nlohmann::json::array();
  112. for( const TEMPLATE_FIELDNAME& field :
  113. m_TemplateFieldNames.GetTemplateFieldNames( false ) )
  114. {
  115. ret.push_back( nlohmann::json( {
  116. { "name", field.m_Name },
  117. { "visible", field.m_Visible },
  118. { "url", field.m_URL }
  119. } ) );
  120. }
  121. return ret;
  122. },
  123. [&]( const nlohmann::json& aJson )
  124. {
  125. if( !aJson.empty() && aJson.is_array() )
  126. {
  127. m_TemplateFieldNames.DeleteAllFieldNameTemplates( false );
  128. for( const nlohmann::json& entry : aJson )
  129. {
  130. if( !entry.contains( "name" ) || !entry.contains( "url" )
  131. || !entry.contains( "visible" ) )
  132. {
  133. continue;
  134. }
  135. TEMPLATE_FIELDNAME field( entry["name"].get<wxString>() );
  136. field.m_URL = entry["url"].get<bool>();
  137. field.m_Visible = entry["visible"].get<bool>();
  138. m_TemplateFieldNames.AddTemplateFieldName( field, false );
  139. }
  140. }
  141. auto* cfg = Pgm().GetSettingsManager().GetAppSettings<EESCHEMA_SETTINGS>();
  142. if( cfg )
  143. {
  144. // Read global fieldname templates
  145. wxString templateFieldNames = cfg->m_Drawing.field_names;
  146. if( !templateFieldNames.IsEmpty() )
  147. {
  148. TEMPLATE_FIELDNAMES_LEXER field_lexer( TO_UTF8( templateFieldNames ) );
  149. try
  150. {
  151. m_TemplateFieldNames.Parse( &field_lexer, true );
  152. }
  153. catch( const IO_ERROR& )
  154. {
  155. }
  156. }
  157. }
  158. }, {} ) );
  159. m_params.emplace_back( new PARAM<wxString>( "page_layout_descr_file",
  160. &m_SchDrawingSheetFileName, "" ) );
  161. m_params.emplace_back( new PARAM<wxString>( "plot_directory",
  162. &m_PlotDirectoryName, "" ) );
  163. m_params.emplace_back( new PARAM<wxString>( "net_format_name",
  164. &m_NetFormatName, "" ) );
  165. m_params.emplace_back( new PARAM<bool>( "spice_adjust_passive_values",
  166. &m_SpiceAdjustPassiveValues, false ) );
  167. m_params.emplace_back( new PARAM<bool>( "spice_save_all_voltages",
  168. &m_SpiceSaveAllVoltages, false ) );
  169. m_params.emplace_back( new PARAM<bool>( "spice_save_all_currents",
  170. &m_SpiceSaveAllCurrents, false ) );
  171. m_params.emplace_back( new PARAM<wxString>( "spice_external_command",
  172. &m_SpiceCommandString, "spice \"%I\"" ) );
  173. // TODO(JE) should we keep these LIB_SYMBOL:: things around?
  174. m_params.emplace_back( new PARAM<int>( "subpart_id_separator",
  175. LIB_SYMBOL::SubpartIdSeparatorPtr(), 0, 0, 126 ) );
  176. m_params.emplace_back( new PARAM<int>( "subpart_first_id",
  177. LIB_SYMBOL::SubpartFirstIdPtr(), 'A', '1', 'z' ) );
  178. m_params.emplace_back( new PARAM<int>( "annotate_start_num",
  179. &m_AnnotateStartNum, 0 ) );
  180. m_NgspiceSimulatorSettings =
  181. std::make_shared<NGSPICE_SIMULATOR_SETTINGS>( this, "ngspice" );
  182. registerMigration( 0, 1,
  183. [&]() -> bool
  184. {
  185. std::optional<double> tor = Get<double>( "drawing.text_offset_ratio" );
  186. if( tor )
  187. Set( "drawing.label_size_ratio", *tor );
  188. return true;
  189. } );
  190. }
  191. SCHEMATIC_SETTINGS::~SCHEMATIC_SETTINGS()
  192. {
  193. ReleaseNestedSettings( m_NgspiceSimulatorSettings.get() );
  194. m_NgspiceSimulatorSettings.reset();
  195. if( m_parent )
  196. {
  197. m_parent->ReleaseNestedSettings( this );
  198. m_parent = nullptr;
  199. }
  200. }