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