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