KiCad PCB EDA Suite
Loading...
Searching...
No Matches
project_file.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 <project.h>
30#include <settings/parameters.h>
33#include <wx/config.h>
34#include <wx/filename.h>
35#include <wx/log.h>
36
37
40
41
42PROJECT_FILE::PROJECT_FILE( const wxString& aFullPath ) :
44 m_ErcSettings( nullptr ),
45 m_SchematicSettings( nullptr ),
47 m_sheets(),
49 m_boards(),
50 m_project( nullptr ),
51 m_wasMigrated( false )
52{
53 // Keep old files around
55
56 m_params.emplace_back( new PARAM_LIST<FILE_INFO_PAIR>( "sheets", &m_sheets, {} ) );
57
58 m_params.emplace_back( new PARAM_LIST<TOP_LEVEL_SHEET_INFO>( "schematic.top_level_sheets",
59 &m_topLevelSheets, {} ) );
60
61 m_params.emplace_back( new PARAM_LIST<FILE_INFO_PAIR>( "boards", &m_boards, {} ) );
62
63 m_params.emplace_back( new PARAM_WXSTRING_MAP( "text_variables",
64 &m_TextVars, {}, false, true /* array behavior, even though stored as a map */ ) );
65
66 m_params.emplace_back( new PARAM_LIST<wxString>( "libraries.pinned_symbol_libs",
67 &m_PinnedSymbolLibs, {} ) );
68
69 m_params.emplace_back( new PARAM_LIST<wxString>( "libraries.pinned_footprint_libs",
70 &m_PinnedFootprintLibs, {} ) );
71
72 m_params.emplace_back( new PARAM_PATH_LIST( "cvpcb.equivalence_files",
73 &m_EquivalenceFiles, {} ) );
74
75 m_params.emplace_back( new PARAM_PATH( "pcbnew.page_layout_descr_file",
77
78 m_params.emplace_back( new PARAM_PATH( "pcbnew.last_paths.netlist",
80
81 m_params.emplace_back( new PARAM_PATH( "pcbnew.last_paths.idf",
83
84 m_params.emplace_back( new PARAM_PATH( "pcbnew.last_paths.vrml",
86
87 m_params.emplace_back( new PARAM_PATH( "pcbnew.last_paths.specctra_dsn",
89
90 m_params.emplace_back( new PARAM_PATH( "pcbnew.last_paths.plot",
92
93 m_params.emplace_back( new PARAM<wxString>( "schematic.legacy_lib_dir",
94 &m_LegacyLibDir, "" ) );
95
96 m_params.emplace_back( new PARAM_LAMBDA<nlohmann::json>( "schematic.legacy_lib_list",
97 [&]() -> nlohmann::json
98 {
99 nlohmann::json ret = nlohmann::json::array();
100
101 for( const wxString& libName : m_LegacyLibNames )
102 ret.push_back( libName );
103
104 return ret;
105 },
106 [&]( const nlohmann::json& aJson )
107 {
108 if( aJson.empty() || !aJson.is_array() )
109 return;
110
111 m_LegacyLibNames.clear();
112
113 for( const nlohmann::json& entry : aJson )
114 m_LegacyLibNames.push_back( entry.get<wxString>() );
115 }, {} ) );
116
117 m_params.emplace_back( new PARAM_LAMBDA<nlohmann::json>( "schematic.bus_aliases",
118 [&]() -> nlohmann::json
119 {
120 nlohmann::json ret = nlohmann::json::object();
121
122 for( const auto& alias : m_BusAliases )
123 {
124 nlohmann::json members = nlohmann::json::array();
125
126 for( const wxString& member : alias.second )
127 members.push_back( member );
128
129 ret[ alias.first.ToStdString() ] = members;
130 }
131
132 return ret;
133 },
134 [&]( const nlohmann::json& aJson )
135 {
136 if( aJson.empty() || !aJson.is_object() )
137 return;
138
139 m_BusAliases.clear();
140
141 for( auto it = aJson.begin(); it != aJson.end(); ++it )
142 {
143 const nlohmann::json& membersJson = it.value();
144
145 if( !membersJson.is_array() )
146 continue;
147
148 std::vector<wxString> members;
149
150 for( const nlohmann::json& entry : membersJson )
151 {
152 if( entry.is_string() )
153 {
154 wxString member = entry.get<wxString>().Strip( wxString::both );
155
156 if( !member.IsEmpty() )
157 members.push_back( member );
158 }
159 }
160
161 wxString name = wxString::FromUTF8( it.key().c_str() ).Strip( wxString::both );
162
163 if( !name.IsEmpty() )
164 m_BusAliases.emplace( name, std::move( members ) );
165 }
166 }, {} ) );
167
168 m_NetSettings = std::make_shared<NET_SETTINGS>( this, "net_settings" );
169
171 std::make_shared<COMPONENT_CLASS_SETTINGS>( this, "component_class_settings" );
172
173 m_tuningProfileParameters = std::make_shared<TUNING_PROFILES>( this, "tuning_profiles" );
174
175 m_params.emplace_back( new PARAM_LAYER_PRESET( "board.layer_presets", &m_LayerPresets ) );
176
177 m_params.emplace_back( new PARAM_VIEWPORT( "board.viewports", &m_Viewports ) );
178
179 m_params.emplace_back( new PARAM_VIEWPORT3D( "board.3dviewports", &m_Viewports3D ) );
180
181 m_params.emplace_back( new PARAM_LAYER_PAIRS( "board.layer_pairs", m_LayerPairInfos ) );
182
183 m_params.emplace_back( new PARAM<wxString>( "board.ipc2581.internal_id",
184 &m_IP2581Bom.id, wxEmptyString ) );
185
186 m_params.emplace_back( new PARAM<wxString>( "board.ipc2581.mpn",
187 &m_IP2581Bom.MPN, wxEmptyString ) );
188
189 m_params.emplace_back( new PARAM<wxString>( "board.ipc2581.mfg",
190 &m_IP2581Bom.mfg, wxEmptyString ) );
191
192 m_params.emplace_back( new PARAM<wxString>( "board.ipc2581.distpn",
193 &m_IP2581Bom.distPN, wxEmptyString ) );
194
195 m_params.emplace_back( new PARAM<wxString>( "board.ipc2581.dist",
196 &m_IP2581Bom.dist, wxEmptyString ) );
197
198 m_params.emplace_back( new PARAM<wxString>( "board.ipc2581.bom_rev",
199 &m_IP2581Bom.bomRev, wxEmptyString ) );
200
201 m_params.emplace_back( new PARAM<wxString>( "board.ipc2581.sch_revision",
202 &m_IP2581Bom.schRevision, wxEmptyString ) );
203
204
205 registerMigration( 1, 2, std::bind( &PROJECT_FILE::migrateSchema1To2, this ) );
206 registerMigration( 2, 3, std::bind( &PROJECT_FILE::migrateSchema2To3, this ) );
207}
208
209
211{
212 auto p( "/board/layer_presets"_json_pointer );
213
214 if( !m_internals->contains( p ) || !m_internals->at( p ).is_array() )
215 return true;
216
217 nlohmann::json& presets = m_internals->at( p );
218
219 for( nlohmann::json& entry : presets )
221
222 m_wasMigrated = true;
223
224 return true;
225}
226
227
229{
230 auto p( "/board/layer_presets"_json_pointer );
231
232 if( !m_internals->contains( p ) || !m_internals->at( p ).is_array() )
233 return true;
234
235 nlohmann::json& presets = m_internals->at( p );
236
237 for( nlohmann::json& entry : presets )
239
240 m_wasMigrated = true;
241
242 return true;
243}
244
245
246bool PROJECT_FILE::MigrateFromLegacy( wxConfigBase* aCfg )
247{
248 bool ret = true;
249 wxString str;
250 long index = 0;
251
252 std::set<wxString> group_blacklist;
253
254 // Legacy files don't store board info; they assume board matches project name
255 // We will leave m_boards empty here so it can be populated with other code
256
257 // First handle migration of data that will be stored locally in this object
258
259 auto loadPinnedLibs =
260 [&]( const std::string& aDest )
261 {
262 int libIndex = 1;
263 wxString libKey = wxT( "PinnedItems" );
264 libKey << libIndex;
265
266 nlohmann::json libs = nlohmann::json::array();
267
268 while( aCfg->Read( libKey, &str ) )
269 {
270 libs.push_back( str );
271
272 aCfg->DeleteEntry( libKey, true );
273
274 libKey = wxT( "PinnedItems" );
275 libKey << ++libIndex;
276 }
277
278 Set( aDest, libs );
279 };
280
281 aCfg->SetPath( wxT( "/LibeditFrame" ) );
282 loadPinnedLibs( "libraries.pinned_symbol_libs" );
283
284 aCfg->SetPath( wxT( "/ModEditFrame" ) );
285 loadPinnedLibs( "libraries.pinned_footprint_libs" );
286
287 aCfg->SetPath( wxT( "/cvpcb/equfiles" ) );
288
289 {
290 int eqIdx = 1;
291 wxString eqKey = wxT( "EquName" );
292 eqKey << eqIdx;
293
294 nlohmann::json eqs = nlohmann::json::array();
295
296 while( aCfg->Read( eqKey, &str ) )
297 {
298 eqs.push_back( str );
299
300 eqKey = wxT( "EquName" );
301 eqKey << ++eqIdx;
302 }
303
304 Set( "cvpcb.equivalence_files", eqs );
305 }
306
307 // All CvPcb params that we want to keep have been migrated above
308 group_blacklist.insert( wxT( "/cvpcb" ) );
309
310 aCfg->SetPath( wxT( "/eeschema" ) );
311 fromLegacyString( aCfg, "LibDir", "schematic.legacy_lib_dir" );
312
313 aCfg->SetPath( wxT( "/eeschema/libraries" ) );
314
315 {
316 int libIdx = 1;
317 wxString libKey = wxT( "LibName" );
318 libKey << libIdx;
319
320 nlohmann::json libs = nlohmann::json::array();
321
322 while( aCfg->Read( libKey, &str ) )
323 {
324 libs.push_back( str );
325
326 libKey = wxT( "LibName" );
327 libKey << ++libIdx;
328 }
329
330 Set( "schematic.legacy_lib_list", libs );
331 }
332
333 group_blacklist.insert( wxT( "/eeschema" ) );
334
335 aCfg->SetPath( wxT( "/text_variables" ) );
336
337 {
338 int txtIdx = 1;
339 wxString txtKey;
340 txtKey << txtIdx;
341
342 nlohmann::json vars = nlohmann::json();
343
344 while( aCfg->Read( txtKey, &str ) )
345 {
346 wxArrayString tokens = wxSplit( str, ':' );
347
348 if( tokens.size() == 2 )
349 vars[ tokens[0].ToStdString() ] = tokens[1];
350
351 txtKey.clear();
352 txtKey << ++txtIdx;
353 }
354
355 Set( "text_variables", vars );
356 }
357
358 group_blacklist.insert( wxT( "/text_variables" ) );
359
360 aCfg->SetPath( wxT( "/schematic_editor" ) );
361
362 fromLegacyString( aCfg, "PageLayoutDescrFile", "schematic.page_layout_descr_file" );
363 fromLegacyString( aCfg, "PlotDirectoryName", "schematic.plot_directory" );
364 fromLegacyString( aCfg, "NetFmtName", "schematic.net_format_name" );
365 fromLegacy<bool>( aCfg, "SpiceAjustPassiveValues", "schematic.spice_adjust_passive_values" );
366 fromLegacy<int>( aCfg, "SubpartIdSeparator", "schematic.subpart_id_separator" );
367 fromLegacy<int>( aCfg, "SubpartFirstId", "schematic.subpart_first_id" );
368
369 fromLegacy<int>( aCfg, "LineThickness", "schematic.drawing.default_line_thickness" );
370 fromLegacy<int>( aCfg, "WireThickness", "schematic.drawing.default_wire_thickness" );
371 fromLegacy<int>( aCfg, "BusThickness", "schematic.drawing.default_bus_thickness" );
372 fromLegacy<int>( aCfg, "LabSize", "schematic.drawing.default_text_size" );
373
374 if( !fromLegacy<int>( aCfg, "PinSymbolSize", "schematic.drawing.pin_symbol_size" ) )
375 {
376 // Use the default symbol size algorithm of Eeschema V5 (based on pin name/number size)
377 Set( "schematic.drawing.pin_symbol_size", 0 );
378 }
379
380 fromLegacy<int>( aCfg, "JunctionSize", "schematic.drawing.default_junction_size" );
381
382 fromLegacyString( aCfg, "FieldNameTemplates", "schematic.drawing.field_names" );
383
384 if( !fromLegacy<double>( aCfg, "TextOffsetRatio", "schematic.drawing.text_offset_ratio" ) )
385 {
386 // Use the spacing of Eeschema V5
387 Set( "schematic.drawing.text_offset_ratio", 0.08 );
388 Set( "schematic.drawing.label_size_ratio", 0.25 );
389 }
390
391 // All schematic_editor keys we keep are migrated above
392 group_blacklist.insert( wxT( "/schematic_editor" ) );
393
394 aCfg->SetPath( wxT( "/pcbnew" ) );
395
396 fromLegacyString( aCfg, "PageLayoutDescrFile", "pcbnew.page_layout_descr_file" );
397 fromLegacyString( aCfg, "LastNetListRead", "pcbnew.last_paths.netlist" );
398 fromLegacyString( aCfg, "LastSTEPExportPath", "pcbnew.last_paths.step" );
399 fromLegacyString( aCfg, "LastIDFExportPath", "pcbnew.last_paths.idf" );
400 fromLegacyString( aCfg, "LastVRMLExportPath", "pcbnew.last_paths.vmrl" );
401 fromLegacyString( aCfg, "LastSpecctraDSNExportPath", "pcbnew.last_paths.specctra_dsn" );
402 fromLegacyString( aCfg, "LastGenCADExportPath", "pcbnew.last_paths.gencad" );
403
404 std::string bp = "board.design_settings.";
405
406 {
407 int idx = 1;
408 wxString key = wxT( "DRCExclusion" );
409 key << idx;
410
411 nlohmann::json exclusions = nlohmann::json::array();
412
413 while( aCfg->Read( key, &str ) )
414 {
415 exclusions.push_back( str );
416
417 key = wxT( "DRCExclusion" );
418 key << ++idx;
419 }
420
421 Set( bp + "drc_exclusions", exclusions );
422 }
423
424 fromLegacy<bool>( aCfg, "AllowMicroVias", bp + "rules.allow_microvias" );
425 fromLegacy<bool>( aCfg, "AllowBlindVias", bp + "rules.allow_blind_buried_vias" );
426 fromLegacy<double>( aCfg, "MinClearance", bp + "rules.min_clearance" );
427 fromLegacy<double>( aCfg, "MinTrackWidth", bp + "rules.min_track_width" );
428 fromLegacy<double>( aCfg, "MinViaAnnulus", bp + "rules.min_via_annulus" );
429 fromLegacy<double>( aCfg, "MinViaDiameter", bp + "rules.min_via_diameter" );
430
431 if( !fromLegacy<double>( aCfg, "MinThroughDrill", bp + "rules.min_through_hole_diameter" ) )
432 fromLegacy<double>( aCfg, "MinViaDrill", bp + "rules.min_through_hole_diameter" );
433
434 fromLegacy<double>( aCfg, "MinMicroViaDiameter", bp + "rules.min_microvia_diameter" );
435 fromLegacy<double>( aCfg, "MinMicroViaDrill", bp + "rules.min_microvia_drill" );
436 fromLegacy<double>( aCfg, "MinHoleToHole", bp + "rules.min_hole_to_hole" );
437 fromLegacy<double>( aCfg, "CopperEdgeClearance", bp + "rules.min_copper_edge_clearance" );
438 fromLegacy<double>( aCfg, "SolderMaskClearance", bp + "rules.solder_mask_clearance" );
439 fromLegacy<double>( aCfg, "SolderMaskMinWidth", bp + "rules.solder_mask_min_width" );
440 fromLegacy<double>( aCfg, "SolderPasteClearance", bp + "rules.solder_paste_clearance" );
441 fromLegacy<double>( aCfg, "SolderPasteRatio", bp + "rules.solder_paste_margin_ratio" );
442
443 if( !fromLegacy<double>( aCfg, "SilkLineWidth", bp + "defaults.silk_line_width" ) )
444 fromLegacy<double>( aCfg, "ModuleOutlineThickness", bp + "defaults.silk_line_width" );
445
446 if( !fromLegacy<double>( aCfg, "SilkTextSizeV", bp + "defaults.silk_text_size_v" ) )
447 fromLegacy<double>( aCfg, "ModuleTextSizeV", bp + "defaults.silk_text_size_v" );
448
449 if( !fromLegacy<double>( aCfg, "SilkTextSizeH", bp + "defaults.silk_text_size_h" ) )
450 fromLegacy<double>( aCfg, "ModuleTextSizeH", bp + "defaults.silk_text_size_h" );
451
452 if( !fromLegacy<double>( aCfg, "SilkTextSizeThickness", bp + "defaults.silk_text_thickness" ) )
453 fromLegacy<double>( aCfg, "ModuleTextSizeThickness", bp + "defaults.silk_text_thickness" );
454
455 fromLegacy<bool>( aCfg, "SilkTextItalic", bp + "defaults.silk_text_italic" );
456 fromLegacy<bool>( aCfg, "SilkTextUpright", bp + "defaults.silk_text_upright" );
457
458 if( !fromLegacy<double>( aCfg, "CopperLineWidth", bp + "defaults.copper_line_width" ) )
459 fromLegacy<double>( aCfg, "DrawSegmentWidth", bp + "defaults.copper_line_width" );
460
461 if( !fromLegacy<double>( aCfg, "CopperTextSizeV", bp + "defaults.copper_text_size_v" ) )
462 fromLegacy<double>( aCfg, "PcbTextSizeV", bp + "defaults.copper_text_size_v" );
463
464 if( !fromLegacy<double>( aCfg, "CopperTextSizeH", bp + "defaults.copper_text_size_h" ) )
465 fromLegacy<double>( aCfg, "PcbTextSizeH", bp + "defaults.copper_text_size_h" );
466
467 if( !fromLegacy<double>( aCfg, "CopperTextThickness", bp + "defaults.copper_text_thickness" ) )
468 fromLegacy<double>( aCfg, "PcbTextThickness", bp + "defaults.copper_text_thickness" );
469
470 fromLegacy<bool>( aCfg, "CopperTextItalic", bp + "defaults.copper_text_italic" );
471 fromLegacy<bool>( aCfg, "CopperTextUpright", bp + "defaults.copper_text_upright" );
472
473 if( !fromLegacy<double>( aCfg, "EdgeCutLineWidth", bp + "defaults.board_outline_line_width" ) )
474 fromLegacy<double>( aCfg, "BoardOutlineThickness",
475 bp + "defaults.board_outline_line_width" );
476
477 fromLegacy<double>( aCfg, "CourtyardLineWidth", bp + "defaults.courtyard_line_width" );
478
479 fromLegacy<double>( aCfg, "FabLineWidth", bp + "defaults.fab_line_width" );
480 fromLegacy<double>( aCfg, "FabTextSizeV", bp + "defaults.fab_text_size_v" );
481 fromLegacy<double>( aCfg, "FabTextSizeH", bp + "defaults.fab_text_size_h" );
482 fromLegacy<double>( aCfg, "FabTextSizeThickness", bp + "defaults.fab_text_thickness" );
483 fromLegacy<bool>( aCfg, "FabTextItalic", bp + "defaults.fab_text_italic" );
484 fromLegacy<bool>( aCfg, "FabTextUpright", bp + "defaults.fab_text_upright" );
485
486 if( !fromLegacy<double>( aCfg, "OthersLineWidth", bp + "defaults.other_line_width" ) )
487 fromLegacy<double>( aCfg, "ModuleOutlineThickness", bp + "defaults.other_line_width" );
488
489 fromLegacy<double>( aCfg, "OthersTextSizeV", bp + "defaults.other_text_size_v" );
490 fromLegacy<double>( aCfg, "OthersTextSizeH", bp + "defaults.other_text_size_h" );
491 fromLegacy<double>( aCfg, "OthersTextSizeThickness", bp + "defaults.other_text_thickness" );
492 fromLegacy<bool>( aCfg, "OthersTextItalic", bp + "defaults.other_text_italic" );
493 fromLegacy<bool>( aCfg, "OthersTextUpright", bp + "defaults.other_text_upright" );
494
495 fromLegacy<int>( aCfg, "DimensionUnits", bp + "defaults.dimension_units" );
496 fromLegacy<int>( aCfg, "DimensionPrecision", bp + "defaults.dimension_precision" );
497
498 std::string sev = bp + "rule_severities";
499
500 fromLegacy<bool>( aCfg, "RequireCourtyardDefinitions", sev + "legacy_no_courtyard_defined" );
501
502 fromLegacy<bool>( aCfg, "ProhibitOverlappingCourtyards", sev + "legacy_courtyards_overlap" );
503
504 {
505 int idx = 1;
506 wxString keyBase = "TrackWidth";
507 wxString key = keyBase;
508 double val;
509
510 nlohmann::json widths = nlohmann::json::array();
511
512 key << idx;
513
514 while( aCfg->Read( key, &val ) )
515 {
516 widths.push_back( val );
517 key = keyBase;
518 key << ++idx;
519 }
520
521 Set( bp + "track_widths", widths );
522 }
523
524 {
525 int idx = 1;
526 wxString keyBase = "ViaDiameter";
527 wxString key = keyBase;
528 double diameter;
529 double drill = 1.0;
530
531 nlohmann::json vias = nlohmann::json::array();
532
533 key << idx;
534
535 while( aCfg->Read( key, &diameter ) )
536 {
537 key = "ViaDrill";
538 aCfg->Read( key << idx, &drill );
539
540 nlohmann::json via = { { "diameter", diameter }, { "drill", drill } };
541 vias.push_back( via );
542
543 key = keyBase;
544 key << ++idx;
545 }
546
547 Set( bp + "via_dimensions", vias );
548 }
549
550 {
551 int idx = 1;
552 wxString keyBase = "dPairWidth";
553 wxString key = keyBase;
554 double width;
555 double gap = 1.0;
556 double via_gap = 1.0;
557
558 nlohmann::json pairs = nlohmann::json::array();
559
560 key << idx;
561
562 while( aCfg->Read( key, &width ) )
563 {
564 key = "dPairGap";
565 aCfg->Read( key << idx, &gap );
566
567 key = "dPairViaGap";
568 aCfg->Read( key << idx, &via_gap );
569
570 nlohmann::json pair = { { "width", width }, { "gap", gap }, { "via_gap", via_gap } };
571 pairs.push_back( pair );
572
573 key = keyBase;
574 key << ++idx;
575 }
576
577 Set( bp + "diff_pair_dimensions", pairs );
578 }
579
580 group_blacklist.insert( wxT( "/pcbnew" ) );
581
582 // General group is unused these days, we can throw it away
583 group_blacklist.insert( wxT( "/general" ) );
584
585 // Next load sheet names and put all other legacy data in the legacy dict
586 aCfg->SetPath( wxT( "/" ) );
587
588 auto loadSheetNames =
589 [&]() -> bool
590 {
591 int sheet = 1;
592 wxString entry;
593 nlohmann::json arr = nlohmann::json::array();
594
595 wxLogTrace( traceSettings, wxT( "Migrating sheet names" ) );
596
597 aCfg->SetPath( wxT( "/sheetnames" ) );
598
599 while( aCfg->Read( wxString::Format( "%d", sheet++ ), &entry ) )
600 {
601 wxArrayString tokens = wxSplit( entry, ':' );
602
603 if( tokens.size() == 2 )
604 {
605 wxLogTrace( traceSettings, wxT( "%d: %s = %s" ), sheet, tokens[0],
606 tokens[1] );
607 arr.push_back( nlohmann::json::array( { tokens[0], tokens[1] } ) );
608 }
609 }
610
611 Set( "sheets", arr );
612
613 aCfg->SetPath( "/" );
614
615 return true;
616 };
617
618 std::vector<wxString> groups;
619
620 groups.emplace_back( wxEmptyString );
621
622 auto loadLegacyPairs =
623 [&]( const std::string& aGroup ) -> bool
624 {
625 wxLogTrace( traceSettings, wxT( "Migrating group %s" ), aGroup );
626 bool success = true;
627 wxString keyStr;
628 wxString val;
629
630 index = 0;
631
632 while( aCfg->GetNextEntry( keyStr, index ) )
633 {
634 if( !aCfg->Read( keyStr, &val ) )
635 continue;
636
637 std::string key( keyStr.ToUTF8() );
638
639 wxLogTrace( traceSettings, wxT( " %s = %s" ), key, val );
640
641 try
642 {
643 Set( "legacy." + aGroup + "." + key, val );
644 }
645 catch( ... )
646 {
647 success = false;
648 }
649 }
650
651 return success;
652 };
653
654 for( size_t i = 0; i < groups.size(); i++ )
655 {
656 aCfg->SetPath( groups[i] );
657
658 if( groups[i] == wxT( "/sheetnames" ) )
659 {
660 ret |= loadSheetNames();
661 continue;
662 }
663
664 aCfg->DeleteEntry( wxT( "last_client" ), true );
665 aCfg->DeleteEntry( wxT( "update" ), true );
666 aCfg->DeleteEntry( wxT( "version" ), true );
667
668 ret &= loadLegacyPairs( groups[i].ToStdString() );
669
670 index = 0;
671
672 while( aCfg->GetNextGroup( str, index ) )
673 {
674 wxString group = groups[i] + "/" + str;
675
676 if( !group_blacklist.count( group ) )
677 groups.emplace_back( group );
678 }
679
680 aCfg->SetPath( "/" );
681 }
682
683 return ret;
684}
685
686
687bool PROJECT_FILE::LoadFromFile( const wxString& aDirectory )
688{
689 bool success = JSON_SETTINGS::LoadFromFile( aDirectory );
690
691 if( success )
692 {
693 // Migrate from old single-root format to top_level_sheets format
694 if( m_topLevelSheets.empty() && m_project )
695 {
696 // Create a default top-level sheet entry based on the project name
697 wxString projectName = m_project->GetProjectName();
698
699 TOP_LEVEL_SHEET_INFO defaultSheet;
700 defaultSheet.uuid = niluuid; // Use niluuid for the first/default sheet
701 defaultSheet.name = projectName;
702 defaultSheet.filename = projectName + ".kicad_sch";
703
704 m_topLevelSheets.push_back( std::move( defaultSheet ) );
705
706 // Mark as migrated so it will be saved with the new format
707 m_wasMigrated = true;
708
709 wxLogTrace( traceSettings, wxT( "PROJECT_FILE: Migrated old single-root format to top_level_sheets" ) );
710 }
711
712 // When a project is created from a template, the top_level_sheets entries may
713 // still reference the template's schematic filenames rather than the new project's.
714 // The template copy renames files on disk but doesn't update the .kicad_pro content.
715 // Detect this and fix the references so the schematic can be found.
716 if( !m_topLevelSheets.empty() && m_project )
717 {
718 wxString projectPath = m_project->GetProjectPath();
719 wxString projectName = m_project->GetProjectName();
720
721 for( TOP_LEVEL_SHEET_INFO& sheetInfo : m_topLevelSheets )
722 {
723 wxFileName referencedFile( projectPath, sheetInfo.filename );
724
725 if( referencedFile.FileExists() )
726 continue;
727
728 // Try the project-name-based filename
729 wxString expectedFile =
730 projectName + wxS( "." ) + FILEEXT::KiCadSchematicFileExtension;
731
732 wxFileName candidateFile( projectPath, expectedFile );
733
734 if( candidateFile.FileExists() )
735 {
736 wxLogTrace( traceSettings,
737 wxT( "PROJECT_FILE: Fixing stale top_level_sheets reference "
738 "'%s' -> '%s'" ),
739 sheetInfo.filename, expectedFile );
740
741 sheetInfo.filename = expectedFile;
742 sheetInfo.name = projectName;
743 m_wasMigrated = true;
744 }
745 }
746 }
747 }
748
749 return success;
750}
751
752
753bool PROJECT_FILE::SaveToFile( const wxString& aDirectory, bool aForce )
754{
755 wxASSERT( m_project );
756
757 Set( "meta.filename", m_project->GetProjectName() + "." + FILEEXT::ProjectFileExtension );
758
759 // Even if parameters were not modified, we should resave after migration
760 bool force = aForce || m_wasMigrated;
761
762 // If we're actually going ahead and doing the save, the flag that keeps code from doing the
763 // save should be cleared at this.
764 m_wasMigrated = false;
765
766 return JSON_SETTINGS::SaveToFile( aDirectory, force );
767}
768
769
770bool PROJECT_FILE::SaveAs( const wxString& aDirectory, const wxString& aFile )
771{
772 wxFileName oldFilename( GetFilename() );
773 wxString oldProjectName = oldFilename.GetName();
774 wxString oldProjectPath = oldFilename.GetPath();
775
776 Set( "meta.filename", aFile + "." + FILEEXT::ProjectFileExtension );
777 SetFilename( aFile );
778
779 auto updatePath =
780 [&]( wxString& aPath )
781 {
782 if( aPath.StartsWith( oldProjectName + wxS( "." ) ) )
783 aPath.Replace( oldProjectName, aFile, false );
784 else if( aPath.StartsWith( oldProjectPath + wxS( "/" ) ) )
785 aPath.Replace( oldProjectPath, aDirectory, false );
786 };
787
788 updatePath( m_BoardDrawingSheetFile );
789
790 for( int ii = LAST_PATH_FIRST; ii < (int) LAST_PATH_SIZE; ++ii )
791 updatePath( m_PcbLastPath[ ii ] );
792
793 auto updatePathByPtr =
794 [&]( const std::string& aPtr )
795 {
796 if( std::optional<wxString> path = Get<wxString>( aPtr ) )
797 {
798 updatePath( path.value() );
799 Set( aPtr, path.value() );
800 }
801 };
802
803 updatePathByPtr( "schematic.page_layout_descr_file" );
804 updatePathByPtr( "schematic.plot_directory" );
805 updatePathByPtr( "schematic.ngspice.workbook_filename" );
806 updatePathByPtr( "pcbnew.page_layout_descr_file" );
807
808 for( auto& sheetInfo : m_topLevelSheets )
809 {
810 updatePath( sheetInfo.filename );
811
812 // Also update the display name if it matches the old project name
813 if( sheetInfo.name == oldProjectName )
814 sheetInfo.name = aFile;
815 }
816
817 // If we're actually going ahead and doing the save, the flag that keeps code from doing the save
818 // should be cleared at this point
819 m_wasMigrated = false;
820
821 // While performing Save As, we have already checked that we can write to the directory
822 // so don't carry the previous flag
823 SetReadOnly( false );
824 return JSON_SETTINGS::SaveToFile( aDirectory, true );
825}
826
827
829{
831}
832
833
838
839
840void to_json( nlohmann::json& aJson, const FILE_INFO_PAIR& aPair )
841{
842 aJson = nlohmann::json::array( { aPair.first.AsString().ToUTF8(), aPair.second.ToUTF8() } );
843}
844
845
846void from_json( const nlohmann::json& aJson, FILE_INFO_PAIR& aPair )
847{
848 wxCHECK( aJson.is_array() && aJson.size() == 2, /* void */ );
849 aPair.first = KIID( wxString( aJson[0].get<std::string>().c_str(), wxConvUTF8 ) );
850 aPair.second = wxString( aJson[1].get<std::string>().c_str(), wxConvUTF8 );
851}
852
853
854void to_json( nlohmann::json& aJson, const TOP_LEVEL_SHEET_INFO& aInfo )
855{
856 aJson = nlohmann::json::object();
857 aJson["uuid"] = aInfo.uuid.AsString().ToUTF8();
858 aJson["name"] = aInfo.name.ToUTF8();
859 aJson["filename"] = aInfo.filename.ToUTF8();
860}
861
862
863void from_json( const nlohmann::json& aJson, TOP_LEVEL_SHEET_INFO& aInfo )
864{
865 wxCHECK( aJson.is_object(), /* void */ );
866
867 if( aJson.contains( "uuid" ) )
868 aInfo.uuid = KIID( wxString( aJson["uuid"].get<std::string>().c_str(), wxConvUTF8 ) );
869
870 if( aJson.contains( "name" ) )
871 aInfo.name = wxString( aJson["name"].get<std::string>().c_str(), wxConvUTF8 );
872
873 if( aJson.contains( "filename" ) )
874 aInfo.filename = wxString( aJson["filename"].get<std::string>().c_str(), wxConvUTF8 );
875}
int index
const char * name
bool fromLegacyString(wxConfigBase *aConfig, const std::string &aKey, const std::string &aDest)
Translates a legacy wxConfig string value to a given JSON pointer value.
bool fromLegacy(wxConfigBase *aConfig, const std::string &aKey, const std::string &aDest)
Translates a legacy wxConfig value to a given JSON pointer value.
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...
virtual bool LoadFromFile(const wxString &aDirectory="")
Loads the backing file from disk and then calls Load()
void SetReadOnly(bool aReadOnly)
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.
JSON_SETTINGS(const wxString &aFilename, SETTINGS_LOC aLocation, int aSchemaVersion)
bool m_deleteLegacyAfterMigration
Whether or not to delete legacy file after migration.
std::unique_ptr< JSON_SETTINGS_INTERNALS > m_internals
void SetFilename(const wxString &aFilename)
virtual bool SaveToFile(const wxString &aDirectory="", bool aForce=false)
Calls Store() and then writes the contents of the JSON document to a file.
wxString GetFilename() const
Definition kiid.h:49
wxString AsString() const
Definition kiid.cpp:244
Like a normal param, but with custom getter and setter functions.
Definition parameters.h:297
static void MigrateToV9Layers(nlohmann::json &aJson)
static void MigrateToNamedRenderLayers(nlohmann::json &aJson)
Represents a list of strings holding directory paths.
Definition parameters.h:677
Stores a path as a string with directory separators normalized to unix-style.
Definition parameters.h:176
A helper for <wxString, wxString> maps.
Definition parameters.h:824
std::map< wxString, wxString > m_TextVars
wxString getFileExt() const override
std::vector< LAYER_PAIR_INFO > m_LayerPairInfos
List of stored 3D viewports (view matrixes)
ERC_SETTINGS * m_ErcSettings
Eeschema params.
wxString m_LegacyLibDir
SCHEMATIC_SETTINGS * m_SchematicSettings
bool migrateSchema1To2()
IPC-2581 BOM settings.
wxString m_BoardDrawingSheetFile
PcbNew params.
std::shared_ptr< NET_SETTINGS > m_NetSettings
Net settings for this project (owned here)
struct IP2581_BOM m_IP2581Bom
Layer pair list for the board.
wxString m_PcbLastPath[LAST_PATH_SIZE]
MRU path storage.
PROJECT * m_project
A link to the owning PROJECT.
std::vector< TOP_LEVEL_SHEET_INFO > m_topLevelSheets
A list of top-level schematic sheets in this project.
std::vector< VIEWPORT > m_Viewports
List of stored layer presets.
BOARD_DESIGN_SETTINGS * m_BoardSettings
Board design settings for this project's board.
bool SaveAs(const wxString &aDirectory, const wxString &aFile)
std::map< wxString, std::vector< wxString > > m_BusAliases
Bus alias definitions for the schematic project.
std::vector< wxString > m_EquivalenceFiles
CvPcb params.
bool migrateSchema2To3()
Schema version 3: move layer presets to use named render layers.
wxString getLegacyFileExt() const override
std::vector< wxString > m_PinnedFootprintLibs
The list of pinned footprint libraries.
bool LoadFromFile(const wxString &aDirectory="") override
Loads the backing file from disk and then calls Load()
std::vector< FILE_INFO_PAIR > m_sheets
An list of schematic sheets in this project.
virtual bool MigrateFromLegacy(wxConfigBase *aCfg) override
Migrates from wxConfig to JSON-based configuration.
std::vector< LAYER_PRESET > m_LayerPresets
std::vector< FILE_INFO_PAIR > m_boards
A list of board files in this project.
std::shared_ptr< TUNING_PROFILES > m_tuningProfileParameters
Tuning profile parameters for this project.
wxArrayString m_LegacyLibNames
std::vector< wxString > m_PinnedSymbolLibs
Below are project-level settings that have not been moved to a dedicated file.
std::vector< VIEWPORT3D > m_Viewports3D
List of stored viewports (pos + zoom)
bool SaveToFile(const wxString &aDirectory="", bool aForce=false) override
Calls Store() and then writes the contents of the JSON document to a file.
PROJECT_FILE(const wxString &aFullPath)
Construct the project file for a project.
std::shared_ptr< COMPONENT_CLASS_SETTINGS > m_ComponentClassSettings
Component class settings for the project (owned here)
Container for project specific data.
Definition project.h:65
static const std::string ProjectFileExtension
static const std::string LegacyProjectFileExtension
static const std::string KiCadSchematicFileExtension
SETTINGS_LOC
#define traceSettings
KIID niluuid(0)
void to_json(nlohmann::json &aJson, const FILE_INFO_PAIR &aPair)
void from_json(const nlohmann::json &aJson, FILE_INFO_PAIR &aPair)
const int projectFileSchemaVersion
! Update the schema version whenever a migration is required
@ LAST_PATH_PLOT
@ LAST_PATH_SPECCTRADSN
@ LAST_PATH_SIZE
@ LAST_PATH_FIRST
@ LAST_PATH_IDF
@ LAST_PATH_VRML
@ LAST_PATH_NETLIST
std::pair< KIID, wxString > FILE_INFO_PAIR
For files like sheets and boards, a pair of that object KIID and display name Display name is typical...
Information about a top-level schematic sheet.
KIID uuid
Unique identifier for the sheet.
wxString name
Display name for the sheet.
wxString filename
Relative path to the sheet file.
std::string path
Definition of file extensions used in Kicad.