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 ),
42 m_DefaultLineWidth( DEFAULT_LINE_WIDTH_MILS * schIUScale.IU_PER_MILS ),
43 m_DefaultTextSize( DEFAULT_TEXT_SIZE * schIUScale.IU_PER_MILS ),
44 m_LabelSizeRatio( DEFAULT_LABEL_SIZE_RATIO ),
45 m_TextOffsetRatio( DEFAULT_TEXT_OFFSET_RATIO ),
46 m_PinSymbolSize( DEFAULT_TEXT_SIZE * schIUScale.IU_PER_MILS / 2 ),
47 m_JunctionSizeChoice( 3 ),
49 m_HopOverSizeChoice( 0 ),
50 m_HopOverScale( 0.0 ),
51 m_ConnectionGridSize( DEFAULT_CONNECTION_GRID_MILS * schIUScale.IU_PER_MILS ),
52 m_AnnotateStartNum( 0 ),
53 m_IntersheetRefsShow( false ),
54 m_IntersheetRefsListOwnPage( true ),
55 m_IntersheetRefsFormatShort( false ),
56 m_IntersheetRefsPrefix( DEFAULT_IREF_PREFIX ),
57 m_IntersheetRefsSuffix( DEFAULT_IREF_SUFFIX ),
58 m_DashedLineDashRatio( 12.0 ),
59 m_DashedLineGapRatio( 3.0 ),
60 m_OPO_VPrecision( 3 ),
61 m_OPO_VRange( wxS( "~V" ) ),
62 m_OPO_IPrecision( 3 ),
63 m_OPO_IRange( wxS( "~A" ) ),
64 m_MaxError( ARC_LOW_DEF_MM * schIUScale.IU_PER_MM ),
65 m_NgspiceSettings( nullptr ),
66 m_refDesTracker( nullptr )
67{
68 EESCHEMA_SETTINGS* cfg = GetAppSettings<EESCHEMA_SETTINGS>( "eeschema" );
69
70 int defaultLineThickness = cfg ? cfg->m_Drawing.default_line_thickness : DEFAULT_LINE_WIDTH_MILS;
71 int defaultTextSize = cfg ? cfg->m_Drawing.default_text_size : DEFAULT_TEXT_SIZE;
72 int defaultPinSymbolSize = cfg ? cfg->m_Drawing.pin_symbol_size : DEFAULT_TEXT_SIZE / 2;
73 int defaultJunctionSizeChoice = cfg ? cfg->m_Drawing.junction_size_choice : 3;
74 int defaultHopOverSizeChoice = cfg ? cfg->m_Drawing.hop_over_size_choice : 0;
75 bool defaultIntersheetsRefShow = cfg ? cfg->m_Drawing.intersheets_ref_show : false;
76 bool defaultIntersheetsRefOwnPage = cfg ? cfg->m_Drawing.intersheets_ref_own_page : true;
77 bool defaultIntersheetsRefFormatShort = cfg ? cfg->m_Drawing.intersheets_ref_short : false;
78 wxString defaultIntersheetsRefPrefix = cfg ? cfg->m_Drawing.intersheets_ref_prefix
79 : wxString( wxS( DEFAULT_IREF_PREFIX ) );
80 wxString defaultIntersheetsRefSuffix = cfg ? cfg->m_Drawing.intersheets_ref_suffix
81 : wxString( wxS( DEFAULT_IREF_SUFFIX ) );
82
83 m_params.emplace_back( new PARAM<bool>( "drawing.intersheets_ref_show",
84 &m_IntersheetRefsShow, defaultIntersheetsRefShow ) );
85
86 m_params.emplace_back( new PARAM<bool>( "drawing.intersheets_ref_own_page",
87 &m_IntersheetRefsListOwnPage, defaultIntersheetsRefOwnPage ) );
88
89 m_params.emplace_back( new PARAM<bool>( "drawing.intersheets_ref_short",
90 &m_IntersheetRefsFormatShort, defaultIntersheetsRefFormatShort ) );
91
92 m_params.emplace_back( new PARAM<wxString>( "drawing.intersheets_ref_prefix",
93 &m_IntersheetRefsPrefix, defaultIntersheetsRefPrefix ) );
94
95 m_params.emplace_back( new PARAM<wxString>( "drawing.intersheets_ref_suffix",
96 &m_IntersheetRefsSuffix, defaultIntersheetsRefSuffix ) );
97
98 m_params.emplace_back( new PARAM<double>( "drawing.dashed_lines_dash_length_ratio",
99 &m_DashedLineDashRatio, 12.0 ) ); // Default from ISO 128-2
100
101 m_params.emplace_back( new PARAM<double>( "drawing.dashed_lines_gap_length_ratio",
102 &m_DashedLineGapRatio, 3.0 ) ); // Default from ISO 128-2
103
104 m_params.emplace_back( new PARAM<int>( "drawing.operating_point_overlay_v_precision",
105 &m_OPO_VPrecision, 3 ) );
106
107 m_params.emplace_back( new PARAM<wxString>( "drawing.operating_point_overlay_v_range",
108 &m_OPO_VRange, wxS( "~V" ) ) );
109
110 m_params.emplace_back( new PARAM<int>( "drawing.operating_point_overlay_i_precision",
111 &m_OPO_IPrecision, 3 ) );
112
113 m_params.emplace_back( new PARAM<wxString>( "drawing.operating_point_overlay_i_range",
114 &m_OPO_IRange, wxS( "~A" ) ) );
115
116 m_params.emplace_back( new PARAM_SCALED<int>( "drawing.default_line_thickness",
117 &m_DefaultLineWidth, schIUScale.MilsToIU( defaultLineThickness ),
119
120 m_params.emplace_back( new PARAM_SCALED<int>( "drawing.default_text_size",
121 &m_DefaultTextSize, schIUScale.MilsToIU( defaultTextSize ),
123
124 m_params.emplace_back( new PARAM<double>( "drawing.text_offset_ratio",
126
127 m_params.emplace_back( new PARAM<double>( "drawing.label_size_ratio",
129
130 m_params.emplace_back( new PARAM<double>( "drawing.overbar_offset_ratio",
132
133 m_params.emplace_back( new PARAM_SCALED<int>( "drawing.pin_symbol_size",
134 &m_PinSymbolSize, schIUScale.MilsToIU( defaultPinSymbolSize ),
136
137 m_params.emplace_back( new PARAM_SCALED<int>( "connection_grid_size",
140 1 / schIUScale.IU_PER_MILS ) );
141
142 // m_JunctionSize is only a run-time cache of the calculated size. Do not save it.
143
144 // User choice for junction dot size ( e.g. none = 0, smallest = 1, small = 2, etc )
145 m_params.emplace_back( new PARAM<int>( "drawing.junction_size_choice",
146 &m_JunctionSizeChoice, defaultJunctionSizeChoice ) );
147
148 m_params.emplace_back( new PARAM<int>( "drawing.hop_over_size_choice",
149 &m_HopOverSizeChoice, defaultHopOverSizeChoice ) );
150
151 m_params.emplace_back( new PARAM_LAMBDA<nlohmann::json>( "drawing.field_names",
152 [&]() -> nlohmann::json
153 {
154 nlohmann::json ret = nlohmann::json::array();
155
156 for( const TEMPLATE_FIELDNAME& field :
158 {
159 ret.push_back( nlohmann::json( {
160 { "name", field.m_Name },
161 { "visible", field.m_Visible },
162 { "url", field.m_URL }
163 } ) );
164 }
165
166 return ret;
167 },
168 [&]( const nlohmann::json& aJson )
169 {
170 if( !aJson.empty() && aJson.is_array() )
171 {
173
174 for( const nlohmann::json& entry : aJson )
175 {
176 if( !entry.contains( "name" ) || !entry.contains( "url" )
177 || !entry.contains( "visible" ) )
178 {
179 continue;
180 }
181
182 TEMPLATE_FIELDNAME field( entry["name"].get<wxString>() );
183 field.m_URL = entry["url"].get<bool>();
184 field.m_Visible = entry["visible"].get<bool>();
186 }
187 }
188
189 // Read global fieldname templates
190 if( EESCHEMA_SETTINGS* curr_cfg = GetAppSettings<EESCHEMA_SETTINGS>( "eeschema" ) )
191 {
192 if( !curr_cfg->m_Drawing.field_names.IsEmpty() )
193 m_TemplateFieldNames.AddTemplateFieldNames( curr_cfg->m_Drawing.field_names );
194 }
195 }, {} ) );
196
197 m_params.emplace_back( new PARAM<wxString>( "bom_export_filename",
198 &m_BomExportFileName, "${PROJECTNAME}.csv" ) );
199
200 m_params.emplace_back(
202 m_params.emplace_back( new PARAM_LIST<BOM_PRESET>( "bom_presets",
203 &m_BomPresets, {} ) );
204
205 m_params.emplace_back( new PARAM<BOM_FMT_PRESET>( "bom_fmt_settings",
207 m_params.emplace_back( new PARAM_LIST<BOM_FMT_PRESET>( "bom_fmt_presets",
208 &m_BomFmtPresets, {} ) );
209
210 m_params.emplace_back( new PARAM<wxString>( "page_layout_descr_file",
212
213 m_params.emplace_back( new PARAM<wxString>( "plot_directory",
214 &m_PlotDirectoryName, "" ) );
215
216 // TODO(JE) should we keep these LIB_SYMBOL:: things around?
217 m_params.emplace_back( new PARAM<int>( "subpart_id_separator",
218 &m_SubpartIdSeparator, 0, 0, 126 ) );
219
220 m_params.emplace_back( new PARAM<int>( "subpart_first_id",
221 &m_SubpartFirstId, 'A', '1', 'z' ) );
222
223 m_params.emplace_back( new PARAM<int>( "annotate_start_num",
224 &m_AnnotateStartNum, 0 ) );
225
226 m_NgspiceSettings = std::make_shared<NGSPICE_SETTINGS>( this, "ngspice" );
227
228 m_params.emplace_back( new PARAM_LAMBDA<bool>( "reuse_designators",
229 [&]() -> bool
230 {
231 return m_refDesTracker ? m_refDesTracker->GetReuseRefDes() : false;
232 },
233 [&]( bool aReuse )
234 {
235 if( !m_refDesTracker )
236 m_refDesTracker = std::make_shared<REFDES_TRACKER>();
237
238 m_refDesTracker->SetReuseRefDes( aReuse );
239 }, true ) );
240
241 m_params.emplace_back( new PARAM_LAMBDA<std::string>( "used_designators",
242 [&]() -> std::string
243 {
244 if( m_refDesTracker )
245 return m_refDesTracker->Serialize();
246
247 return std::string();
248 },
249 [&]( const std::string& aData )
250 {
251 if( !m_refDesTracker )
252 m_refDesTracker = std::make_shared<REFDES_TRACKER>();
253
254 m_refDesTracker->Deserialize( aData );
255 }, {} ) );
256
257 registerMigration( 0, 1,
258 [&]() -> bool
259 {
260 std::optional<double> tor = Get<double>( "drawing.text_offset_ratio" );
261
262 if( tor )
263 Set( "drawing.label_size_ratio", *tor );
264
265 return true;
266 } );
267}
268
269
271{
273 m_NgspiceSettings.reset();
274
275 if( m_parent )
276 {
278 m_parent = nullptr;
279 }
280}
281
282
283wxString SCHEMATIC_SETTINGS::SubReference( int aUnit, bool aAddSeparator ) const
284{
285 wxString subRef;
286
287 if( aUnit < 1 )
288 return subRef;
289
290 if( m_SubpartIdSeparator != 0 && aAddSeparator )
291 subRef << wxChar( m_SubpartIdSeparator );
292
293 if( m_SubpartFirstId >= '0' && m_SubpartFirstId <= '9' )
294 subRef << aUnit;
295 else
297
298 return subRef;
299}
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::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.
double m_OverbarHeight
Definition: font.h:123
static wxString LetterSubReference(int aUnit, wxChar aInitialLetter)
Definition: lib_symbol.cpp:517
NESTED_SETTINGS is a JSON_SETTINGS that lives inside a JSON_SETTINGS.
JSON_SETTINGS * m_parent
A pointer to the parent object to load and store from.
Like a normal param, but with custom getter and setter functions.
Definition: parameters.h:295
Represents a parameter that has a scaling factor between the value in the file and the value used int...
Definition: parameters.h:392
wxString m_SchDrawingSheetFileName
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.
TEMPLATES m_TemplateFieldNames
std::vector< BOM_PRESET > m_BomPresets
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.
void AddTemplateFieldName(const TEMPLATE_FIELDNAME &aFieldName, bool aGlobal)
Insert or append a wanted symbol field name into the field names template.
void AddTemplateFieldNames(const wxString &aSerializedFieldNames)
Add a serialized list of template field names.
const std::vector< TEMPLATE_FIELDNAME > & GetTemplateFieldNames()
Return a template field name list for read only access.
void DeleteAllFieldNameTemplates(bool aGlobal)
Delete the entire contents.
#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:129
const int schSettingsSchemaVersion
#define MIN_CONNECTION_GRID_MILS
#define DEFAULT_CONNECTION_GRID_MILS
static BOM_FMT_PRESET CSV()
static BOM_PRESET DefaultEditing()
const double IU_PER_MILS
Definition: base_units.h:77
constexpr int MilsToIU(int mils) const
Definition: base_units.h:97
Hold a name of a symbol's field, field value, and default visibility.
static constexpr double IU_PER_MM
Mock up a conversion function.