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.

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