KiCad PCB EDA Suite
Loading...
Searching...
No Matches
schematic_settings.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2020 CERN
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 * @author Jon Evans <[email protected]>
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
22#include <base_screen.h>
23#include <lib_symbol.h>
24#include <default_values.h>
25#include <eeschema_settings.h>
26#include <macros.h>
27#include <pgm_base.h>
28#include <refdes_tracker.h>
29#include <schematic_settings.h>
31#include <settings/parameters.h>
34#include <sim/spice_settings.h>
35
36
38
39
40SCHEMATIC_SETTINGS::SCHEMATIC_SETTINGS( JSON_SETTINGS* aParent, const std::string& aPath ) :
41 NESTED_SETTINGS( "schematic", schSettingsSchemaVersion, aParent, aPath, false ),
50 m_HopOverScale( 0.0 ),
55 m_IntersheetRefsShow( false ),
63 m_OPO_VRange( wxS( "~V" ) ),
65 m_OPO_IRange( wxS( "~A" ) ),
67 m_NgspiceSettings( nullptr ),
68 m_refDesTracker( nullptr )
69{
71
72 int defaultLineThickness = cfg ? cfg->m_Drawing.default_line_thickness : DEFAULT_LINE_WIDTH_MILS;
73 int defaultTextSize = cfg ? cfg->m_Drawing.default_text_size : DEFAULT_TEXT_SIZE;
74 int defaultPinSymbolSize = cfg ? cfg->m_Drawing.pin_symbol_size : DEFAULT_TEXT_SIZE / 2;
75 int defaultJunctionSizeChoice = cfg ? cfg->m_Drawing.junction_size_choice : 3;
76 int defaultHopOverSizeChoice = cfg ? cfg->m_Drawing.hop_over_size_choice : 0;
77 bool defaultIntersheetsRefShow = cfg ? cfg->m_Drawing.intersheets_ref_show : false;
78 bool defaultIntersheetsRefOwnPage = cfg ? cfg->m_Drawing.intersheets_ref_own_page : true;
79 bool defaultIntersheetsRefFormatShort = cfg ? cfg->m_Drawing.intersheets_ref_short : false;
80 wxString defaultIntersheetsRefPrefix = cfg ? cfg->m_Drawing.intersheets_ref_prefix
81 : wxString( wxS( DEFAULT_IREF_PREFIX ) );
82 wxString defaultIntersheetsRefSuffix = cfg ? cfg->m_Drawing.intersheets_ref_suffix
83 : wxString( wxS( DEFAULT_IREF_SUFFIX ) );
84
85 m_params.emplace_back( new PARAM<bool>( "drawing.intersheets_ref_show",
86 &m_IntersheetRefsShow, defaultIntersheetsRefShow ) );
87
88 m_params.emplace_back( new PARAM<bool>( "drawing.intersheets_ref_own_page",
89 &m_IntersheetRefsListOwnPage, defaultIntersheetsRefOwnPage ) );
90
91 m_params.emplace_back( new PARAM<bool>( "drawing.intersheets_ref_short",
92 &m_IntersheetRefsFormatShort, defaultIntersheetsRefFormatShort ) );
93
94 m_params.emplace_back( new PARAM<wxString>( "drawing.intersheets_ref_prefix",
95 &m_IntersheetRefsPrefix, defaultIntersheetsRefPrefix ) );
96
97 m_params.emplace_back( new PARAM<wxString>( "drawing.intersheets_ref_suffix",
98 &m_IntersheetRefsSuffix, defaultIntersheetsRefSuffix ) );
99
100 m_params.emplace_back( new PARAM<double>( "drawing.dashed_lines_dash_length_ratio",
101 &m_DashedLineDashRatio, 12.0 ) ); // Default from ISO 128-2
102
103 m_params.emplace_back( new PARAM<double>( "drawing.dashed_lines_gap_length_ratio",
104 &m_DashedLineGapRatio, 3.0 ) ); // Default from ISO 128-2
105
106 m_params.emplace_back( new PARAM<int>( "drawing.operating_point_overlay_v_precision",
107 &m_OPO_VPrecision, 3 ) );
108
109 m_params.emplace_back( new PARAM<wxString>( "drawing.operating_point_overlay_v_range",
110 &m_OPO_VRange, wxS( "~V" ) ) );
111
112 m_params.emplace_back( new PARAM<int>( "drawing.operating_point_overlay_i_precision",
113 &m_OPO_IPrecision, 3 ) );
114
115 m_params.emplace_back( new PARAM<wxString>( "drawing.operating_point_overlay_i_range",
116 &m_OPO_IRange, wxS( "~A" ) ) );
117
118 m_params.emplace_back( new PARAM_SCALED<int>( "drawing.default_line_thickness",
119 &m_DefaultLineWidth, schIUScale.MilsToIU( defaultLineThickness ),
120 schIUScale.MilsToIU( 5 ), schIUScale.MilsToIU( 1000 ), 1 / schIUScale.IU_PER_MILS ) );
121
122 m_params.emplace_back( new PARAM_SCALED<int>( "drawing.default_text_size",
123 &m_DefaultTextSize, schIUScale.MilsToIU( defaultTextSize ),
124 schIUScale.MilsToIU( 5 ), schIUScale.MilsToIU( 1000 ), 1 / schIUScale.IU_PER_MILS ) );
125
126 m_params.emplace_back( new PARAM<double>( "drawing.text_offset_ratio",
128
129 m_params.emplace_back( new PARAM<double>( "drawing.label_size_ratio",
131
132 m_params.emplace_back( new PARAM<double>( "drawing.overbar_offset_ratio",
133 &m_FontMetrics.m_OverbarHeight, m_FontMetrics.m_OverbarHeight ) );
134
135 m_params.emplace_back( new PARAM_SCALED<int>( "drawing.pin_symbol_size",
136 &m_PinSymbolSize, schIUScale.MilsToIU( defaultPinSymbolSize ),
137 schIUScale.MilsToIU( 0 ), schIUScale.MilsToIU( 1000 ), 1 / schIUScale.IU_PER_MILS ) );
138
139 m_params.emplace_back( new PARAM_SCALED<int>( "connection_grid_size",
141 schIUScale.MilsToIU( MIN_CONNECTION_GRID_MILS ), schIUScale.MilsToIU( 10000 ),
142 1 / schIUScale.IU_PER_MILS ) );
143
144 // m_JunctionSize is only a run-time cache of the calculated size. Do not save it.
145
146 // User choice for junction dot size ( e.g. none = 0, smallest = 1, small = 2, etc )
147 m_params.emplace_back( new PARAM<int>( "drawing.junction_size_choice",
148 &m_JunctionSizeChoice, defaultJunctionSizeChoice ) );
149
150 m_params.emplace_back( new PARAM<int>( "drawing.hop_over_size_choice",
151 &m_HopOverSizeChoice, defaultHopOverSizeChoice ) );
152
153 m_params.emplace_back( new PARAM_LAMBDA<nlohmann::json>( "drawing.field_names",
154 [&]() -> nlohmann::json
155 {
156 nlohmann::json ret = nlohmann::json::array();
157
158 for( const TEMPLATE_FIELDNAME& field :
159 m_TemplateFieldNames.GetTemplateFieldNames( false ) )
160 {
161 ret.push_back( nlohmann::json( {
162 { "name", field.m_Name },
163 { "visible", field.m_Visible },
164 { "url", field.m_URL }
165 } ) );
166 }
167
168 return ret;
169 },
170 [&]( const nlohmann::json& aJson )
171 {
172 if( !aJson.empty() && aJson.is_array() )
173 {
174 m_TemplateFieldNames.DeleteAllFieldNameTemplates( false );
175
176 for( const nlohmann::json& entry : aJson )
177 {
178 if( !entry.contains( "name" ) || !entry.contains( "url" )
179 || !entry.contains( "visible" ) )
180 {
181 continue;
182 }
183
184 TEMPLATE_FIELDNAME field( entry["name"].get<wxString>() );
185 field.m_URL = entry["url"].get<bool>();
186 field.m_Visible = entry["visible"].get<bool>();
187 m_TemplateFieldNames.AddTemplateFieldName( field, false );
188 }
189 }
190
191 // Read global fieldname templates
192 if( EESCHEMA_SETTINGS* curr_cfg = GetAppSettings<EESCHEMA_SETTINGS>( "eeschema" ) )
193 {
194 if( !curr_cfg->m_Drawing.field_names.IsEmpty() )
195 m_TemplateFieldNames.AddTemplateFieldNames( curr_cfg->m_Drawing.field_names );
196 }
197 }, {} ) );
198
199 m_params.emplace_back( new PARAM<wxString>( "bom_export_filename",
200 &m_BomExportFileName, "${PROJECTNAME}.csv" ) );
201
202 m_params.emplace_back(
204 m_params.emplace_back( new PARAM_LIST<BOM_PRESET>( "bom_presets",
205 &m_BomPresets, {} ) );
206
207 m_params.emplace_back( new PARAM<BOM_FMT_PRESET>( "bom_fmt_settings",
209 m_params.emplace_back( new PARAM_LIST<BOM_FMT_PRESET>( "bom_fmt_presets",
210 &m_BomFmtPresets, {} ) );
211
212 m_params.emplace_back( new PARAM<wxString>( "page_layout_descr_file",
214
215 m_params.emplace_back( new PARAM<wxString>( "plot_directory",
216 &m_PlotDirectoryName, "" ) );
217
218 m_params.emplace_back( new PARAM<int>( "subpart_id_separator",
219 &m_SubpartIdSeparator, 0, 0, 126 ) );
220
221 m_params.emplace_back( new PARAM<int>( "subpart_first_id",
222 &m_SubpartFirstId, 'A', '1', 'z' ) );
223
224 m_params.emplace_back( new PARAM<int>( "annotate_start_num",
225 &m_AnnotateStartNum, 0 ) );
226
227 m_params.emplace_back( new PARAM<int>( "annotation.sort_order",
228 &m_AnnotateSortOrder, 0, 0, 1 ) );
229
230 m_params.emplace_back( new PARAM<int>( "annotation.method",
231 &m_AnnotateMethod, 0, 0, 2 ) );
232
233 m_NgspiceSettings = std::make_shared<NGSPICE_SETTINGS>( this, "ngspice" );
234
235 m_params.emplace_back( new PARAM_LAMBDA<bool>( "reuse_designators",
236 [&]() -> bool
237 {
238 return m_refDesTracker ? m_refDesTracker->GetReuseRefDes() : false;
239 },
240 [&]( bool aReuse )
241 {
242 if( !m_refDesTracker )
243 m_refDesTracker = std::make_shared<REFDES_TRACKER>();
244
245 m_refDesTracker->SetReuseRefDes( aReuse );
246 }, true ) );
247
248 m_params.emplace_back( new PARAM_LAMBDA<std::string>( "used_designators",
249 [&]() -> std::string
250 {
251 if( m_refDesTracker )
252 return m_refDesTracker->Serialize();
253
254 return std::string();
255 },
256 [&]( const std::string& aData )
257 {
258 if( !m_refDesTracker )
259 m_refDesTracker = std::make_shared<REFDES_TRACKER>();
260
261 m_refDesTracker->Deserialize( aData );
262 }, {} ) );
263
264 m_params.emplace_back( new PARAM_LAMBDA<nlohmann::json>( "variants",
265 [&]() -> nlohmann::json
266 {
267 nlohmann::json ret = nlohmann::json::array();
268
269 for( const auto& [name, description] : m_VariantDescriptions )
270 {
271 nlohmann::json entry;
272 entry["name"] = name;
273
274 if( !description.IsEmpty() )
275 entry["description"] = description;
276
277 ret.push_back( entry );
278 }
279
280 return ret;
281 },
282 [&]( const nlohmann::json& aJson )
283 {
284 m_VariantDescriptions.clear();
285
286 if( aJson.is_array() )
287 {
288 for( const auto& entry : aJson )
289 {
290 if( entry.contains( "name" ) )
291 {
292 wxString name = entry["name"].get<wxString>();
293 wxString desc;
294
295 if( entry.contains( "description" ) )
296 desc = entry["description"].get<wxString>();
297
299 }
300 }
301 }
302 }, nlohmann::json::array() ) );
303
304 registerMigration( 0, 1,
305 [&]() -> bool
306 {
307 std::optional<double> tor = Get<double>( "drawing.text_offset_ratio" );
308
309 if( tor )
310 Set( "drawing.label_size_ratio", *tor );
311
312 return true;
313 } );
314}
315
316
318{
320 m_NgspiceSettings.reset();
321
322 if( m_parent )
323 {
324 m_parent->ReleaseNestedSettings( this );
325 m_parent = nullptr;
326 }
327}
328
329
330wxString SCHEMATIC_SETTINGS::SubReference( int aUnit, bool aAddSeparator ) const
331{
332 wxString subRef;
333
334 if( aUnit < 1 )
335 return subRef;
336
337 if( m_SubpartIdSeparator != 0 && aAddSeparator )
338 subRef << wxChar( m_SubpartIdSeparator );
339
340 if( m_SubpartFirstId >= '0' && m_SubpartFirstId <= '9' )
341 subRef << aUnit;
342 else
344
345 return subRef;
346}
const char * name
BASE_SCREEN class implementation.
constexpr EDA_IU_SCALE schIUScale
Definition base_units.h:114
constexpr double ARC_LOW_DEF_MM
Definition base_units.h:118
void Set(const std::string &aPath, ValueType aVal)
Stores a value into the JSON document Will throw an exception if ValueType isn't something that the l...
std::optional< ValueType > Get(const std::string &aPath) const
Fetches a value from within the JSON document.
std::vector< PARAM_BASE * > m_params
The list of parameters (owned by this object)
void registerMigration(int aOldSchemaVersion, int aNewSchemaVersion, std::function< bool(void)> aMigrator)
Registers a migration from one schema version to another.
void ReleaseNestedSettings(NESTED_SETTINGS *aSettings)
Saves and frees a nested settings object, if it exists within this one.
JSON_SETTINGS(const wxString &aFilename, SETTINGS_LOC aLocation, int aSchemaVersion)
static wxString LetterSubReference(int aUnit, wxChar aInitialLetter)
JSON_SETTINGS * m_parent
A pointer to the parent object to load and store from.
NESTED_SETTINGS(const std::string &aName, int aSchemaVersion, JSON_SETTINGS *aParent, const std::string &aPath, bool aLoadFromFile=true)
Like a normal param, but with custom getter and setter functions.
Definition parameters.h:296
Represents a parameter that has a scaling factor between the value in the file and the value used int...
Definition parameters.h:393
BOM_FMT_PRESET m_BomFmtSettings
List of stored BOM format presets.
wxString SubReference(int aUnit, bool aAddSeparator=true) const
SCHEMATIC_SETTINGS(JSON_SETTINGS *aParent, const std::string &aPath)
std::shared_ptr< REFDES_TRACKER > m_refDesTracker
A list of previously used schematic reference designators.
std::vector< BOM_PRESET > m_BomPresets
std::map< wxString, wxString > m_VariantDescriptions
A map of variant names to their descriptions.
std::vector< BOM_FMT_PRESET > m_BomFmtPresets
KIFONT::METRICS m_FontMetrics
std::shared_ptr< NGSPICE_SETTINGS > m_NgspiceSettings
Ngspice simulator settings.
BOM_PRESET m_BomSettings
List of stored BOM presets.
int m_MaxError
Max deviation allowable when approximating circles and curves (in IU).
#define DEFAULT_IREF_PREFIX
The intersheets references suffix string.
#define DEFAULT_LABEL_SIZE_RATIO
The offset of the pin name string from the end of the pin in mils.
#define DEFAULT_JUNCTION_DIAM
The default bus and wire entry size in mils.
#define DEFAULT_LINE_WIDTH_MILS
The default wire width in mils. (can be changed in preference menu)
#define DEFAULT_TEXT_OFFSET_RATIO
Ratio of the font height to space around global labels.
#define DEFAULT_IREF_SUFFIX
Radius of snap "gravity well".
#define DEFAULT_TEXT_SIZE
Ratio of the font height to the baseline of the text above the wire.
This file contains miscellaneous commonly used macros and functions.
see class PGM_BASE
#define IU_PER_MILS
Definition plotter.cpp:131
const int schSettingsSchemaVersion
#define MIN_CONNECTION_GRID_MILS
#define DEFAULT_CONNECTION_GRID_MILS
T * GetAppSettings(const char *aFilename)
static BOM_FMT_PRESET CSV()
static BOM_PRESET DefaultEditing()
Hold a name of a symbol's field, field value, and default visibility.
static constexpr double IU_PER_MM
Mock up a conversion function.