You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

663 lines
23 KiB

5 years ago
5 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2020 CERN
  5. * Copyright (C) 2021-2022 KiCad Developers, see AUTHORS.txt for contributors.
  6. * @author Jon Evans <jon@craftyjon.com>
  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. #include <project.h>
  22. #include <project/net_settings.h>
  23. #include <settings/json_settings_internals.h>
  24. #include <project/project_file.h>
  25. #include <settings/common_settings.h>
  26. #include <settings/parameters.h>
  27. #include <wildcards_and_files_ext.h>
  28. #include <wx/config.h>
  29. #include <wx/log.h>
  30. ///! Update the schema version whenever a migration is required
  31. const int projectFileSchemaVersion = 1;
  32. PROJECT_FILE::PROJECT_FILE( const wxString& aFullPath ) :
  33. JSON_SETTINGS( aFullPath, SETTINGS_LOC::PROJECT, projectFileSchemaVersion ),
  34. m_ErcSettings( nullptr ),
  35. m_SchematicSettings( nullptr ),
  36. m_BoardSettings(),
  37. m_sheets(),
  38. m_boards(),
  39. m_project( nullptr )
  40. {
  41. // Keep old files around
  42. m_deleteLegacyAfterMigration = false;
  43. m_params.emplace_back( new PARAM_LIST<FILE_INFO_PAIR>( "sheets", &m_sheets, {} ) );
  44. m_params.emplace_back( new PARAM_LIST<FILE_INFO_PAIR>( "boards", &m_boards, {} ) );
  45. m_params.emplace_back( new PARAM_WXSTRING_MAP( "text_variables",
  46. &m_TextVars, {}, false, true /* array behavior, even though stored as a map */ ) );
  47. m_params.emplace_back( new PARAM_LIST<wxString>( "libraries.pinned_symbol_libs",
  48. &m_PinnedSymbolLibs, {} ) );
  49. m_params.emplace_back( new PARAM_LIST<wxString>( "libraries.pinned_footprint_libs",
  50. &m_PinnedFootprintLibs, {} ) );
  51. m_params.emplace_back( new PARAM_PATH_LIST( "cvpcb.equivalence_files",
  52. &m_EquivalenceFiles, {} ) );
  53. m_params.emplace_back( new PARAM_PATH( "pcbnew.page_layout_descr_file",
  54. &m_BoardDrawingSheetFile, "" ) );
  55. m_params.emplace_back( new PARAM_PATH( "pcbnew.last_paths.netlist",
  56. &m_PcbLastPath[LAST_PATH_NETLIST], "" ) );
  57. m_params.emplace_back( new PARAM_PATH( "pcbnew.last_paths.step",
  58. &m_PcbLastPath[LAST_PATH_STEP], "" ) );
  59. m_params.emplace_back( new PARAM_PATH( "pcbnew.last_paths.idf",
  60. &m_PcbLastPath[LAST_PATH_IDF], "" ) );
  61. m_params.emplace_back( new PARAM_PATH( "pcbnew.last_paths.vrml",
  62. &m_PcbLastPath[LAST_PATH_VRML], "" ) );
  63. m_params.emplace_back( new PARAM_PATH( "pcbnew.last_paths.specctra_dsn",
  64. &m_PcbLastPath[LAST_PATH_SPECCTRADSN], "" ) );
  65. m_params.emplace_back( new PARAM_PATH( "pcbnew.last_paths.gencad",
  66. &m_PcbLastPath[LAST_PATH_GENCAD], "" ) );
  67. m_params.emplace_back( new PARAM_PATH( "pcbnew.last_paths.pos_files",
  68. &m_PcbLastPath[LAST_PATH_POS_FILES], "" ) );
  69. m_params.emplace_back( new PARAM_PATH( "pcbnew.last_paths.svg",
  70. &m_PcbLastPath[LAST_PATH_SVG], "" ) );
  71. m_params.emplace_back( new PARAM_PATH( "pcbnew.last_paths.plot",
  72. &m_PcbLastPath[LAST_PATH_PLOT], "" ) );
  73. m_params.emplace_back( new PARAM<wxString>( "schematic.legacy_lib_dir",
  74. &m_LegacyLibDir, "" ) );
  75. m_params.emplace_back( new PARAM_LAMBDA<nlohmann::json>( "schematic.legacy_lib_list",
  76. [&]() -> nlohmann::json
  77. {
  78. nlohmann::json ret = nlohmann::json::array();
  79. for( const wxString& libName : m_LegacyLibNames )
  80. ret.push_back( libName );
  81. return ret;
  82. },
  83. [&]( const nlohmann::json& aJson )
  84. {
  85. if( aJson.empty() || !aJson.is_array() )
  86. return;
  87. m_LegacyLibNames.clear();
  88. for( const nlohmann::json& entry : aJson )
  89. m_LegacyLibNames.push_back( entry.get<wxString>() );
  90. }, {} ) );
  91. m_NetSettings = std::make_shared<NET_SETTINGS>( this, "net_settings" );
  92. m_params.emplace_back( new PARAM_LAYER_PRESET( "board.layer_presets", &m_LayerPresets ) );
  93. m_params.emplace_back( new PARAM_VIEWPORT( "board.viewports", &m_Viewports ) );
  94. m_params.emplace_back( new PARAM_VIEWPORT3D( "board.3dviewports", &m_Viewports3D ) );
  95. m_params.emplace_back( new PARAM<wxString>( "board.ipc2581.internal_id",
  96. &m_IP2581Bom.id, wxEmptyString ) );
  97. m_params.emplace_back( new PARAM<wxString>( "board.ipc2581.mpn",
  98. &m_IP2581Bom.MPN, wxEmptyString ) );
  99. m_params.emplace_back( new PARAM<wxString>( "board.ipc2581.mfg",
  100. &m_IP2581Bom.mfg, wxEmptyString ) );
  101. m_params.emplace_back( new PARAM<wxString>( "board.ipc2581.distpn",
  102. &m_IP2581Bom.distPN, wxEmptyString ) );
  103. m_params.emplace_back( new PARAM<wxString>( "board.ipc2581.dist",
  104. &m_IP2581Bom.dist, wxEmptyString ) );
  105. }
  106. bool PROJECT_FILE::MigrateFromLegacy( wxConfigBase* aCfg )
  107. {
  108. bool ret = true;
  109. wxString str;
  110. long index = 0;
  111. std::set<wxString> group_blacklist;
  112. // Legacy files don't store board info; they assume board matches project name
  113. // We will leave m_boards empty here so it can be populated with other code
  114. // First handle migration of data that will be stored locally in this object
  115. auto loadPinnedLibs =
  116. [&]( const std::string& aDest )
  117. {
  118. int libIndex = 1;
  119. wxString libKey = wxT( "PinnedItems" );
  120. libKey << libIndex;
  121. nlohmann::json libs = nlohmann::json::array();
  122. while( aCfg->Read( libKey, &str ) )
  123. {
  124. libs.push_back( str );
  125. aCfg->DeleteEntry( libKey, true );
  126. libKey = wxT( "PinnedItems" );
  127. libKey << ++libIndex;
  128. }
  129. Set( aDest, libs );
  130. };
  131. aCfg->SetPath( wxT( "/LibeditFrame" ) );
  132. loadPinnedLibs( "libraries.pinned_symbol_libs" );
  133. aCfg->SetPath( wxT( "/ModEditFrame" ) );
  134. loadPinnedLibs( "libraries.pinned_footprint_libs" );
  135. aCfg->SetPath( wxT( "/cvpcb/equfiles" ) );
  136. {
  137. int eqIdx = 1;
  138. wxString eqKey = wxT( "EquName" );
  139. eqKey << eqIdx;
  140. nlohmann::json eqs = nlohmann::json::array();
  141. while( aCfg->Read( eqKey, &str ) )
  142. {
  143. eqs.push_back( str );
  144. eqKey = wxT( "EquName" );
  145. eqKey << ++eqIdx;
  146. }
  147. Set( "cvpcb.equivalence_files", eqs );
  148. }
  149. // All CvPcb params that we want to keep have been migrated above
  150. group_blacklist.insert( wxT( "/cvpcb" ) );
  151. aCfg->SetPath( wxT( "/eeschema" ) );
  152. fromLegacyString( aCfg, "LibDir", "schematic.legacy_lib_dir" );
  153. aCfg->SetPath( wxT( "/eeschema/libraries" ) );
  154. {
  155. int libIdx = 1;
  156. wxString libKey = wxT( "LibName" );
  157. libKey << libIdx;
  158. nlohmann::json libs = nlohmann::json::array();
  159. while( aCfg->Read( libKey, &str ) )
  160. {
  161. libs.push_back( str );
  162. libKey = wxT( "LibName" );
  163. libKey << ++libIdx;
  164. }
  165. Set( "schematic.legacy_lib_list", libs );
  166. }
  167. group_blacklist.insert( wxT( "/eeschema" ) );
  168. aCfg->SetPath( wxT( "/text_variables" ) );
  169. {
  170. int txtIdx = 1;
  171. wxString txtKey;
  172. txtKey << txtIdx;
  173. nlohmann::json vars = nlohmann::json();
  174. while( aCfg->Read( txtKey, &str ) )
  175. {
  176. wxArrayString tokens = wxSplit( str, ':' );
  177. if( tokens.size() == 2 )
  178. vars[ tokens[0].ToStdString() ] = tokens[1];
  179. txtKey.clear();
  180. txtKey << ++txtIdx;
  181. }
  182. Set( "text_variables", vars );
  183. }
  184. group_blacklist.insert( wxT( "/text_variables" ) );
  185. aCfg->SetPath( wxT( "/schematic_editor" ) );
  186. fromLegacyString( aCfg, "PageLayoutDescrFile", "schematic.page_layout_descr_file" );
  187. fromLegacyString( aCfg, "PlotDirectoryName", "schematic.plot_directory" );
  188. fromLegacyString( aCfg, "NetFmtName", "schematic.net_format_name" );
  189. fromLegacy<bool>( aCfg, "SpiceAjustPassiveValues", "schematic.spice_adjust_passive_values" );
  190. fromLegacy<int>( aCfg, "SubpartIdSeparator", "schematic.subpart_id_separator" );
  191. fromLegacy<int>( aCfg, "SubpartFirstId", "schematic.subpart_first_id" );
  192. fromLegacy<int>( aCfg, "LineThickness", "schematic.drawing.default_line_thickness" );
  193. fromLegacy<int>( aCfg, "WireThickness", "schematic.drawing.default_wire_thickness" );
  194. fromLegacy<int>( aCfg, "BusThickness", "schematic.drawing.default_bus_thickness" );
  195. fromLegacy<int>( aCfg, "LabSize", "schematic.drawing.default_text_size" );
  196. if( !fromLegacy<int>( aCfg, "PinSymbolSize", "schematic.drawing.pin_symbol_size" ) )
  197. {
  198. // Use the default symbol size algorithm of Eeschema V5 (based on pin name/number size)
  199. Set( "schematic.drawing.pin_symbol_size", 0 );
  200. }
  201. fromLegacy<int>( aCfg, "JunctionSize", "schematic.drawing.default_junction_size" );
  202. fromLegacyString( aCfg, "FieldNameTemplates", "schematic.drawing.field_names" );
  203. if( !fromLegacy<double>( aCfg, "TextOffsetRatio", "schematic.drawing.text_offset_ratio" ) )
  204. {
  205. // Use the spacing of Eeschema V5
  206. Set( "schematic.drawing.text_offset_ratio", 0.08 );
  207. Set( "schematic.drawing.label_size_ratio", 0.25 );
  208. }
  209. // All schematic_editor keys we keep are migrated above
  210. group_blacklist.insert( wxT( "/schematic_editor" ) );
  211. aCfg->SetPath( wxT( "/pcbnew" ) );
  212. fromLegacyString( aCfg, "PageLayoutDescrFile", "pcbnew.page_layout_descr_file" );
  213. fromLegacyString( aCfg, "LastNetListRead", "pcbnew.last_paths.netlist" );
  214. fromLegacyString( aCfg, "LastSTEPExportPath", "pcbnew.last_paths.step" );
  215. fromLegacyString( aCfg, "LastIDFExportPath", "pcbnew.last_paths.idf" );
  216. fromLegacyString( aCfg, "LastVRMLExportPath", "pcbnew.last_paths.vmrl" );
  217. fromLegacyString( aCfg, "LastSpecctraDSNExportPath", "pcbnew.last_paths.specctra_dsn" );
  218. fromLegacyString( aCfg, "LastGenCADExportPath", "pcbnew.last_paths.gencad" );
  219. std::string bp = "board.design_settings.";
  220. {
  221. int idx = 1;
  222. wxString key = wxT( "DRCExclusion" );
  223. key << idx;
  224. nlohmann::json exclusions = nlohmann::json::array();
  225. while( aCfg->Read( key, &str ) )
  226. {
  227. exclusions.push_back( str );
  228. key = wxT( "DRCExclusion" );
  229. key << ++idx;
  230. }
  231. Set( bp + "drc_exclusions", exclusions );
  232. }
  233. fromLegacy<bool>( aCfg, "AllowMicroVias", bp + "rules.allow_microvias" );
  234. fromLegacy<bool>( aCfg, "AllowBlindVias", bp + "rules.allow_blind_buried_vias" );
  235. fromLegacy<double>( aCfg, "MinClearance", bp + "rules.min_clearance" );
  236. fromLegacy<double>( aCfg, "MinTrackWidth", bp + "rules.min_track_width" );
  237. fromLegacy<double>( aCfg, "MinViaAnnulus", bp + "rules.min_via_annulus" );
  238. fromLegacy<double>( aCfg, "MinViaDiameter", bp + "rules.min_via_diameter" );
  239. if( !fromLegacy<double>( aCfg, "MinThroughDrill", bp + "rules.min_through_hole_diameter" ) )
  240. fromLegacy<double>( aCfg, "MinViaDrill", bp + "rules.min_through_hole_diameter" );
  241. fromLegacy<double>( aCfg, "MinMicroViaDiameter", bp + "rules.min_microvia_diameter" );
  242. fromLegacy<double>( aCfg, "MinMicroViaDrill", bp + "rules.min_microvia_drill" );
  243. fromLegacy<double>( aCfg, "MinHoleToHole", bp + "rules.min_hole_to_hole" );
  244. fromLegacy<double>( aCfg, "CopperEdgeClearance", bp + "rules.min_copper_edge_clearance" );
  245. fromLegacy<double>( aCfg, "SolderMaskClearance", bp + "rules.solder_mask_clearance" );
  246. fromLegacy<double>( aCfg, "SolderMaskMinWidth", bp + "rules.solder_mask_min_width" );
  247. fromLegacy<double>( aCfg, "SolderPasteClearance", bp + "rules.solder_paste_clearance" );
  248. fromLegacy<double>( aCfg, "SolderPasteRatio", bp + "rules.solder_paste_margin_ratio" );
  249. if( !fromLegacy<double>( aCfg, "SilkLineWidth", bp + "defaults.silk_line_width" ) )
  250. fromLegacy<double>( aCfg, "ModuleOutlineThickness", bp + "defaults.silk_line_width" );
  251. if( !fromLegacy<double>( aCfg, "SilkTextSizeV", bp + "defaults.silk_text_size_v" ) )
  252. fromLegacy<double>( aCfg, "ModuleTextSizeV", bp + "defaults.silk_text_size_v" );
  253. if( !fromLegacy<double>( aCfg, "SilkTextSizeH", bp + "defaults.silk_text_size_h" ) )
  254. fromLegacy<double>( aCfg, "ModuleTextSizeH", bp + "defaults.silk_text_size_h" );
  255. if( !fromLegacy<double>( aCfg, "SilkTextSizeThickness", bp + "defaults.silk_text_thickness" ) )
  256. fromLegacy<double>( aCfg, "ModuleTextSizeThickness", bp + "defaults.silk_text_thickness" );
  257. fromLegacy<bool>( aCfg, "SilkTextItalic", bp + "defaults.silk_text_italic" );
  258. fromLegacy<bool>( aCfg, "SilkTextUpright", bp + "defaults.silk_text_upright" );
  259. if( !fromLegacy<double>( aCfg, "CopperLineWidth", bp + "defaults.copper_line_width" ) )
  260. fromLegacy<double>( aCfg, "DrawSegmentWidth", bp + "defaults.copper_line_width" );
  261. if( !fromLegacy<double>( aCfg, "CopperTextSizeV", bp + "defaults.copper_text_size_v" ) )
  262. fromLegacy<double>( aCfg, "PcbTextSizeV", bp + "defaults.copper_text_size_v" );
  263. if( !fromLegacy<double>( aCfg, "CopperTextSizeH", bp + "defaults.copper_text_size_h" ) )
  264. fromLegacy<double>( aCfg, "PcbTextSizeH", bp + "defaults.copper_text_size_h" );
  265. if( !fromLegacy<double>( aCfg, "CopperTextThickness", bp + "defaults.copper_text_thickness" ) )
  266. fromLegacy<double>( aCfg, "PcbTextThickness", bp + "defaults.copper_text_thickness" );
  267. fromLegacy<bool>( aCfg, "CopperTextItalic", bp + "defaults.copper_text_italic" );
  268. fromLegacy<bool>( aCfg, "CopperTextUpright", bp + "defaults.copper_text_upright" );
  269. if( !fromLegacy<double>( aCfg, "EdgeCutLineWidth", bp + "defaults.board_outline_line_width" ) )
  270. fromLegacy<double>( aCfg, "BoardOutlineThickness", bp + "defaults.board_outline_line_width" );
  271. fromLegacy<double>( aCfg, "CourtyardLineWidth", bp + "defaults.courtyard_line_width" );
  272. fromLegacy<double>( aCfg, "FabLineWidth", bp + "defaults.fab_line_width" );
  273. fromLegacy<double>( aCfg, "FabTextSizeV", bp + "defaults.fab_text_size_v" );
  274. fromLegacy<double>( aCfg, "FabTextSizeH", bp + "defaults.fab_text_size_h" );
  275. fromLegacy<double>( aCfg, "FabTextSizeThickness", bp + "defaults.fab_text_thickness" );
  276. fromLegacy<bool>( aCfg, "FabTextItalic", bp + "defaults.fab_text_italic" );
  277. fromLegacy<bool>( aCfg, "FabTextUpright", bp + "defaults.fab_text_upright" );
  278. if( !fromLegacy<double>( aCfg, "OthersLineWidth", bp + "defaults.other_line_width" ) )
  279. fromLegacy<double>( aCfg, "ModuleOutlineThickness", bp + "defaults.other_line_width" );
  280. fromLegacy<double>( aCfg, "OthersTextSizeV", bp + "defaults.other_text_size_v" );
  281. fromLegacy<double>( aCfg, "OthersTextSizeH", bp + "defaults.other_text_size_h" );
  282. fromLegacy<double>( aCfg, "OthersTextSizeThickness", bp + "defaults.other_text_thickness" );
  283. fromLegacy<bool>( aCfg, "OthersTextItalic", bp + "defaults.other_text_italic" );
  284. fromLegacy<bool>( aCfg, "OthersTextUpright", bp + "defaults.other_text_upright" );
  285. fromLegacy<int>( aCfg, "DimensionUnits", bp + "defaults.dimension_units" );
  286. fromLegacy<int>( aCfg, "DimensionPrecision", bp + "defaults.dimension_precision" );
  287. std::string sev = bp + "rule_severities";
  288. fromLegacy<bool>( aCfg, "RequireCourtyardDefinitions", sev + "legacy_no_courtyard_defined" );
  289. fromLegacy<bool>( aCfg, "ProhibitOverlappingCourtyards", sev + "legacy_courtyards_overlap" );
  290. {
  291. int idx = 1;
  292. wxString keyBase = "TrackWidth";
  293. wxString key = keyBase;
  294. double val;
  295. nlohmann::json widths = nlohmann::json::array();
  296. key << idx;
  297. while( aCfg->Read( key, &val ) )
  298. {
  299. widths.push_back( val );
  300. key = keyBase;
  301. key << ++idx;
  302. }
  303. Set( bp + "track_widths", widths );
  304. }
  305. {
  306. int idx = 1;
  307. wxString keyBase = "ViaDiameter";
  308. wxString key = keyBase;
  309. double diameter;
  310. double drill = 1.0;
  311. nlohmann::json vias = nlohmann::json::array();
  312. key << idx;
  313. while( aCfg->Read( key, &diameter ) )
  314. {
  315. key = "ViaDrill";
  316. aCfg->Read( key << idx, &drill );
  317. nlohmann::json via = { { "diameter", diameter }, { "drill", drill } };
  318. vias.push_back( via );
  319. key = keyBase;
  320. key << ++idx;
  321. }
  322. Set( bp + "via_dimensions", vias );
  323. }
  324. {
  325. int idx = 1;
  326. wxString keyBase = "dPairWidth";
  327. wxString key = keyBase;
  328. double width;
  329. double gap = 1.0;
  330. double via_gap = 1.0;
  331. nlohmann::json pairs = nlohmann::json::array();
  332. key << idx;
  333. while( aCfg->Read( key, &width ) )
  334. {
  335. key = "dPairGap";
  336. aCfg->Read( key << idx, &gap );
  337. key = "dPairViaGap";
  338. aCfg->Read( key << idx, &via_gap );
  339. nlohmann::json pair = { { "width", width }, { "gap", gap }, { "via_gap", via_gap } };
  340. pairs.push_back( pair );
  341. key = keyBase;
  342. key << ++idx;
  343. }
  344. Set( bp + "diff_pair_dimensions", pairs );
  345. }
  346. group_blacklist.insert( wxT( "/pcbnew" ) );
  347. // General group is unused these days, we can throw it away
  348. group_blacklist.insert( wxT( "/general" ) );
  349. // Next load sheet names and put all other legacy data in the legacy dict
  350. aCfg->SetPath( wxT( "/" ) );
  351. auto loadSheetNames =
  352. [&]() -> bool
  353. {
  354. int sheet = 1;
  355. wxString entry;
  356. nlohmann::json arr = nlohmann::json::array();
  357. wxLogTrace( traceSettings, wxT( "Migrating sheet names" ) );
  358. aCfg->SetPath( wxT( "/sheetnames" ) );
  359. while( aCfg->Read( wxString::Format( "%d", sheet++ ), &entry ) )
  360. {
  361. wxArrayString tokens = wxSplit( entry, ':' );
  362. if( tokens.size() == 2 )
  363. {
  364. wxLogTrace( traceSettings, wxT( "%d: %s = %s" ), sheet, tokens[0],
  365. tokens[1] );
  366. arr.push_back( nlohmann::json::array( { tokens[0], tokens[1] } ) );
  367. }
  368. }
  369. Set( "sheets", arr );
  370. aCfg->SetPath( "/" );
  371. // TODO: any reason we want to fail on this?
  372. return true;
  373. };
  374. std::vector<wxString> groups;
  375. groups.emplace_back( wxEmptyString );
  376. auto loadLegacyPairs =
  377. [&]( const std::string& aGroup ) -> bool
  378. {
  379. wxLogTrace( traceSettings, wxT( "Migrating group %s" ), aGroup );
  380. bool success = true;
  381. wxString keyStr;
  382. wxString val;
  383. index = 0;
  384. while( aCfg->GetNextEntry( keyStr, index ) )
  385. {
  386. if( !aCfg->Read( keyStr, &val ) )
  387. continue;
  388. std::string key( keyStr.ToUTF8() );
  389. wxLogTrace( traceSettings, wxT( " %s = %s" ), key, val );
  390. try
  391. {
  392. Set( "legacy." + aGroup + "." + key, val );
  393. }
  394. catch( ... )
  395. {
  396. success = false;
  397. }
  398. }
  399. return success;
  400. };
  401. for( size_t i = 0; i < groups.size(); i++ )
  402. {
  403. aCfg->SetPath( groups[i] );
  404. if( groups[i] == wxT( "/sheetnames" ) )
  405. {
  406. ret |= loadSheetNames();
  407. continue;
  408. }
  409. aCfg->DeleteEntry( wxT( "last_client" ), true );
  410. aCfg->DeleteEntry( wxT( "update" ), true );
  411. aCfg->DeleteEntry( wxT( "version" ), true );
  412. ret &= loadLegacyPairs( groups[i].ToStdString() );
  413. index = 0;
  414. while( aCfg->GetNextGroup( str, index ) )
  415. {
  416. wxString group = groups[i] + "/" + str;
  417. if( !group_blacklist.count( group ) )
  418. groups.emplace_back( group );
  419. }
  420. aCfg->SetPath( "/" );
  421. }
  422. return ret;
  423. }
  424. bool PROJECT_FILE::SaveToFile( const wxString& aDirectory, bool aForce )
  425. {
  426. wxASSERT( m_project );
  427. Set( "meta.filename", m_project->GetProjectName() + "." + FILEEXT::ProjectFileExtension );
  428. return JSON_SETTINGS::SaveToFile( aDirectory, aForce );
  429. }
  430. bool PROJECT_FILE::SaveAs( const wxString& aDirectory, const wxString& aFile )
  431. {
  432. wxFileName oldFilename( GetFilename() );
  433. wxString oldProjectName = oldFilename.GetName();
  434. wxString oldProjectPath = oldFilename.GetPath();
  435. Set( "meta.filename", aFile + "." + FILEEXT::ProjectFileExtension );
  436. SetFilename( aFile );
  437. auto updatePath =
  438. [&]( wxString& aPath )
  439. {
  440. if( aPath.StartsWith( oldProjectName + wxS( "." ) ) )
  441. aPath.Replace( oldProjectName, aFile, false );
  442. else if( aPath.StartsWith( oldProjectPath + wxS( "/" ) ) )
  443. aPath.Replace( oldProjectPath, aDirectory, false );
  444. };
  445. updatePath( m_BoardDrawingSheetFile );
  446. for( int ii = LAST_PATH_FIRST; ii < (int) LAST_PATH_SIZE; ++ii )
  447. updatePath( m_PcbLastPath[ ii ] );
  448. auto updatePathByPtr =
  449. [&]( const std::string& aPtr )
  450. {
  451. if( std::optional<wxString> path = Get<wxString>( aPtr ) )
  452. {
  453. updatePath( path.value() );
  454. Set( aPtr, path.value() );
  455. }
  456. };
  457. updatePathByPtr( "schematic.page_layout_descr_file" );
  458. updatePathByPtr( "schematic.plot_directory" );
  459. updatePathByPtr( "schematic.ngspice.workbook_filename" );
  460. updatePathByPtr( "pcbnew.page_layout_descr_file" );
  461. // While performing Save As, we have already checked that we can write to the directory
  462. // so don't carry the previous flag
  463. SetReadOnly( false );
  464. return JSON_SETTINGS::SaveToFile( aDirectory, true );
  465. }
  466. wxString PROJECT_FILE::getFileExt() const
  467. {
  468. return FILEEXT::ProjectFileExtension;
  469. }
  470. wxString PROJECT_FILE::getLegacyFileExt() const
  471. {
  472. return FILEEXT::LegacyProjectFileExtension;
  473. }
  474. void to_json( nlohmann::json& aJson, const FILE_INFO_PAIR& aPair )
  475. {
  476. aJson = nlohmann::json::array( { aPair.first.AsString().ToUTF8(), aPair.second.ToUTF8() } );
  477. }
  478. void from_json( const nlohmann::json& aJson, FILE_INFO_PAIR& aPair )
  479. {
  480. wxCHECK( aJson.is_array() && aJson.size() == 2, /* void */ );
  481. aPair.first = KIID( wxString( aJson[0].get<std::string>().c_str(), wxConvUTF8 ) );
  482. aPair.second = wxString( aJson[1].get<std::string>().c_str(), wxConvUTF8 );
  483. }