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