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>
30#include <refdes_tracker.h>
31#include <schematic_settings.h>
33#include <settings/parameters.h>
36#include <sim/spice_settings.h>
37
38
39// Lists containing the scaling factors to apply for the junction/hop over sizes
40const std::vector<double> hopover_size_mult_list = { 0.0, 1.7, 4.0, 6.0, 9.0, 12.0 };
41const std::vector<double> junction_size_mult_list = { 0.0, 1.7, 4.0, 6.0, 9.0, 12.0 };
42
44
45
46SCHEMATIC_SETTINGS::SCHEMATIC_SETTINGS( JSON_SETTINGS* aParent, const std::string& aPath ) :
47 NESTED_SETTINGS( "schematic", schSettingsSchemaVersion, aParent, aPath, false ),
59 m_IntersheetRefsShow( false ),
67 m_OPO_VRange( wxS( "~V" ) ),
69 m_OPO_IRange( wxS( "~A" ) ),
71 m_NgspiceSettings( nullptr ),
72 m_refDesTracker( nullptr )
73{
75
76 int defaultLineThickness = cfg ? cfg->m_Drawing.default_line_thickness : DEFAULT_LINE_WIDTH_MILS;
77 int defaultTextSize = cfg ? cfg->m_Drawing.default_text_size : DEFAULT_TEXT_SIZE;
78 int defaultPinSymbolSize = cfg ? cfg->m_Drawing.pin_symbol_size : DEFAULT_TEXT_SIZE / 2;
79 int defaultJunctionSizeChoice = cfg ? cfg->m_Drawing.junction_size_choice : 3;
80 int defaultHopOverSizeChoice = cfg ? cfg->m_Drawing.hop_over_size_choice : 0;
81 bool defaultIntersheetsRefShow = cfg ? cfg->m_Drawing.intersheets_ref_show : false;
82 bool defaultIntersheetsRefOwnPage = cfg ? cfg->m_Drawing.intersheets_ref_own_page : true;
83 bool defaultIntersheetsRefFormatShort = cfg ? cfg->m_Drawing.intersheets_ref_short : false;
84 wxString defaultIntersheetsRefPrefix = cfg ? cfg->m_Drawing.intersheets_ref_prefix
85 : wxString( wxS( DEFAULT_IREF_PREFIX ) );
86 wxString defaultIntersheetsRefSuffix = cfg ? cfg->m_Drawing.intersheets_ref_suffix
87 : wxString( wxS( DEFAULT_IREF_SUFFIX ) );
88
89 m_params.emplace_back( new PARAM<bool>( "drawing.intersheets_ref_show",
90 &m_IntersheetRefsShow, defaultIntersheetsRefShow ) );
91
92 m_params.emplace_back( new PARAM<bool>( "drawing.intersheets_ref_own_page",
93 &m_IntersheetRefsListOwnPage, defaultIntersheetsRefOwnPage ) );
94
95 m_params.emplace_back( new PARAM<bool>( "drawing.intersheets_ref_short",
96 &m_IntersheetRefsFormatShort, defaultIntersheetsRefFormatShort ) );
97
98 m_params.emplace_back( new PARAM<wxString>( "drawing.intersheets_ref_prefix",
99 &m_IntersheetRefsPrefix, defaultIntersheetsRefPrefix ) );
100
101 m_params.emplace_back( new PARAM<wxString>( "drawing.intersheets_ref_suffix",
102 &m_IntersheetRefsSuffix, defaultIntersheetsRefSuffix ) );
103
104 m_params.emplace_back( new PARAM<double>( "drawing.dashed_lines_dash_length_ratio",
105 &m_DashedLineDashRatio, 12.0 ) ); // Default from ISO 128-2
106
107 m_params.emplace_back( new PARAM<double>( "drawing.dashed_lines_gap_length_ratio",
108 &m_DashedLineGapRatio, 3.0 ) ); // Default from ISO 128-2
109
110 m_params.emplace_back( new PARAM<int>( "drawing.operating_point_overlay_v_precision",
111 &m_OPO_VPrecision, 3 ) );
112
113 m_params.emplace_back( new PARAM<wxString>( "drawing.operating_point_overlay_v_range",
114 &m_OPO_VRange, wxS( "~V" ) ) );
115
116 m_params.emplace_back( new PARAM<int>( "drawing.operating_point_overlay_i_precision",
117 &m_OPO_IPrecision, 3 ) );
118
119 m_params.emplace_back( new PARAM<wxString>( "drawing.operating_point_overlay_i_range",
120 &m_OPO_IRange, wxS( "~A" ) ) );
121
122 m_params.emplace_back( new PARAM_SCALED<int>( "drawing.default_line_thickness",
123 &m_DefaultLineWidth, schIUScale.MilsToIU( defaultLineThickness ),
124 schIUScale.MilsToIU( 5 ), schIUScale.MilsToIU( 1000 ), 1 / schIUScale.IU_PER_MILS ) );
125
126 m_params.emplace_back( new PARAM_SCALED<int>( "drawing.default_text_size",
127 &m_DefaultTextSize, schIUScale.MilsToIU( defaultTextSize ),
128 schIUScale.MilsToIU( 5 ), schIUScale.MilsToIU( 1000 ), 1 / schIUScale.IU_PER_MILS ) );
129
130 m_params.emplace_back( new PARAM<double>( "drawing.text_offset_ratio",
132
133 m_params.emplace_back( new PARAM<double>( "drawing.label_size_ratio",
135
136 m_params.emplace_back( new PARAM<double>( "drawing.overbar_offset_ratio",
137 &m_FontMetrics.m_OverbarHeight, m_FontMetrics.m_OverbarHeight ) );
138
139 m_params.emplace_back( new PARAM_SCALED<int>( "drawing.pin_symbol_size",
140 &m_PinSymbolSize, schIUScale.MilsToIU( defaultPinSymbolSize ),
141 schIUScale.MilsToIU( 0 ), schIUScale.MilsToIU( 1000 ), 1 / schIUScale.IU_PER_MILS ) );
142
143 m_params.emplace_back( new PARAM_SCALED<int>( "connection_grid_size",
145 schIUScale.MilsToIU( MIN_CONNECTION_GRID_MILS ), schIUScale.MilsToIU( 10000 ),
146 1 / schIUScale.IU_PER_MILS ) );
147
148 // User choice for junction dot size ( e.g. none = 0, smallest = 1, small = 2, etc )
149 m_params.emplace_back( new PARAM<int>( "drawing.junction_size_choice",
150 &m_JunctionSizeChoice, defaultJunctionSizeChoice ) );
151
152 m_params.emplace_back( new PARAM<int>( "drawing.hop_over_size_choice",
153 &m_HopOverSizeChoice, defaultHopOverSizeChoice ) );
154
155 m_params.emplace_back( new PARAM_LAMBDA<nlohmann::json>( "drawing.field_names",
156 [&]() -> nlohmann::json
157 {
158 nlohmann::json ret = nlohmann::json::array();
159
160 for( const TEMPLATE_FIELDNAME& field :
161 m_TemplateFieldNames.GetTemplateFieldNames( false ) )
162 {
163 ret.push_back( nlohmann::json( {
164 { "name", field.m_Name },
165 { "visible", field.m_Visible },
166 { "url", field.m_URL }
167 } ) );
168 }
169
170 return ret;
171 },
172 [&]( const nlohmann::json& aJson )
173 {
174 if( !aJson.empty() && aJson.is_array() )
175 {
176 m_TemplateFieldNames.DeleteAllFieldNameTemplates( false );
177
178 for( const nlohmann::json& entry : aJson )
179 {
180 if( !entry.contains( "name" ) || !entry.contains( "url" )
181 || !entry.contains( "visible" ) )
182 {
183 continue;
184 }
185
186 TEMPLATE_FIELDNAME field( entry["name"].get<wxString>() );
187 field.m_URL = entry["url"].get<bool>();
188 field.m_Visible = entry["visible"].get<bool>();
189 m_TemplateFieldNames.AddTemplateFieldName( field, false );
190 }
191 }
192
193 // Read global fieldname templates
194 if( EESCHEMA_SETTINGS* curr_cfg = GetAppSettings<EESCHEMA_SETTINGS>( "eeschema" ) )
195 {
196 if( !curr_cfg->m_Drawing.field_names.IsEmpty() )
197 m_TemplateFieldNames.AddTemplateFieldNames( curr_cfg->m_Drawing.field_names );
198 }
199 }, {} ) );
200
201 m_params.emplace_back( new PARAM<wxString>( "bom_export_filename",
202 &m_BomExportFileName, "${PROJECTNAME}.csv" ) );
203
204 m_params.emplace_back(
206 m_params.emplace_back( new PARAM_LIST<BOM_PRESET>( "bom_presets",
207 &m_BomPresets, {} ) );
208
209 m_params.emplace_back( new PARAM<BOM_FMT_PRESET>( "bom_fmt_settings",
211 m_params.emplace_back( new PARAM_LIST<BOM_FMT_PRESET>( "bom_fmt_presets",
212 &m_BomFmtPresets, {} ) );
213
214 m_params.emplace_back( new PARAM<wxString>( "page_layout_descr_file",
216
217 m_params.emplace_back( new PARAM<wxString>( "plot_directory",
218 &m_PlotDirectoryName, "" ) );
219
220 m_params.emplace_back( new PARAM<int>( "subpart_id_separator",
221 &m_SubpartIdSeparator, 0, 0, 126 ) );
222
223 m_params.emplace_back( new PARAM<int>( "subpart_first_id",
224 &m_SubpartFirstId, 'A', '1', 'z' ) );
225
226 m_params.emplace_back( new PARAM<int>( "annotate_start_num",
227 &m_AnnotateStartNum, 0 ) );
228
229 m_params.emplace_back( new PARAM<int>( "annotation.sort_order",
230 &m_AnnotateSortOrder, 0, 0, 1 ) );
231
232 m_params.emplace_back( new PARAM<int>( "annotation.method",
233 &m_AnnotateMethod, 0, 0, 2 ) );
234
235 m_NgspiceSettings = std::make_shared<NGSPICE_SETTINGS>( this, "ngspice" );
236
237 m_params.emplace_back( new PARAM_LAMBDA<bool>( "reuse_designators",
238 [&]() -> bool
239 {
240 return m_refDesTracker ? m_refDesTracker->GetReuseRefDes() : false;
241 },
242 [&]( bool aReuse )
243 {
244 if( !m_refDesTracker )
245 m_refDesTracker = std::make_shared<REFDES_TRACKER>();
246
247 m_refDesTracker->SetReuseRefDes( aReuse );
248 }, true ) );
249
250 m_params.emplace_back( new PARAM_LAMBDA<std::string>( "used_designators",
251 [&]() -> std::string
252 {
253 if( m_refDesTracker )
254 return m_refDesTracker->Serialize();
255
256 return std::string();
257 },
258 [&]( const std::string& aData )
259 {
260 if( !m_refDesTracker )
261 m_refDesTracker = std::make_shared<REFDES_TRACKER>();
262
263 m_refDesTracker->Deserialize( aData );
264 }, {} ) );
265
266 m_params.emplace_back( new PARAM_LAMBDA<nlohmann::json>( "variants",
267 [&]() -> nlohmann::json
268 {
269 nlohmann::json ret = nlohmann::json::array();
270
271 for( const auto& [name, description] : m_VariantDescriptions )
272 {
273 nlohmann::json entry;
274 entry["name"] = name;
275
276 if( !description.IsEmpty() )
277 entry["description"] = description;
278
279 ret.push_back( entry );
280 }
281
282 return ret;
283 },
284 [&]( const nlohmann::json& aJson )
285 {
286 m_VariantDescriptions.clear();
287
288 if( aJson.is_array() )
289 {
290 for( const auto& entry : aJson )
291 {
292 if( entry.contains( "name" ) )
293 {
294 wxString name = entry["name"].get<wxString>();
295 wxString desc;
296
297 if( entry.contains( "description" ) )
298 desc = entry["description"].get<wxString>();
299
301 }
302 }
303 }
304 }, nlohmann::json::array() ) );
305
306 registerMigration( 0, 1,
307 [&]() -> bool
308 {
309 std::optional<double> tor = Get<double>( "drawing.text_offset_ratio" );
310
311 if( tor )
312 Set( "drawing.label_size_ratio", *tor );
313
314 return true;
315 } );
316}
317
318
320{
322 m_NgspiceSettings.reset();
323
324 if( m_parent )
325 {
326 m_parent->ReleaseNestedSettings( this );
327 m_parent = nullptr;
328 }
329}
330
331
332wxString SCHEMATIC_SETTINGS::SubReference( int aUnit, bool aAddSeparator ) const
333{
334 wxString subRef;
335
336 if( aUnit < 1 )
337 return subRef;
338
339 if( m_SubpartIdSeparator != 0 && aAddSeparator )
340 subRef << wxChar( m_SubpartIdSeparator );
341
342 if( m_SubpartFirstId >= '0' && m_SubpartFirstId <= '9' )
343 subRef << aUnit;
344 else
346
347 return subRef;
348}
349
350
352{
354
356 int dotSize = KiROUND( projectFile.NetSettings()->GetDefaultNetclass()->GetWireWidth() * multiplier );
357
358 return std::max( dotSize, 1 );
359}
360
361
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
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:990
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)
int GetWireWidth() const
Definition netclass.h:201
std::shared_ptr< NETCLASS > GetDefaultNetclass()
Gets the default netclass for the project.
Like a normal param, but with custom getter and setter functions.
Definition parameters.h:297
Represents a parameter that has a scaling factor between the value in the file and the value used int...
Definition parameters.h:394
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition pgm_base.h:130
The backing store for a PROJECT, in JSON format.
std::shared_ptr< NET_SETTINGS > & NetSettings()
virtual PROJECT_FILE & GetProjectFile() const
Definition project.h:204
double GetHopOverScale()
Accessor that computes the current hop-over size.
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
int GetJunctionSize()
Accessor that computes the current junction size.
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).
PROJECT & Prj() const
A helper while we are not MDI-capable – return the one and only project.
#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_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.
PGM_BASE & Pgm()
The global program "get" accessor.
see class PGM_BASE
#define IU_PER_MILS
Definition plotter.cpp:131
const std::vector< double > junction_size_mult_list
const std::vector< double > hopover_size_mult_list
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.