53bool LoadLayerMapFile(
const wxString& aFile, std::map<wxString, wxString>& aMap, wxString& aError )
55 wxFFile file( aFile, wxS(
"rb" ) );
57 if( !file.IsOpened() )
59 aError = wxString::Format(
_(
"Could not open layer map file '%s'" ), aFile );
65 if( !file.ReadAll( &content ) )
67 aError = wxString::Format(
_(
"Could not read layer map file '%s'" ), aFile );
75 json = nlohmann::json::parse( content.ToStdString() );
77 catch(
const nlohmann::json::exception& e )
79 aError = wxString::Format(
_(
"Layer map file '%s' is not valid JSON: %s" ), aFile,
84 if( !
json.is_object() )
86 aError = wxString::Format(
_(
"Layer map file '%s' must be a JSON object mapping source "
87 "layer names to KiCad layer names" ), aFile );
91 std::map<wxString, wxString> parsed;
93 for(
auto it =
json.begin(); it !=
json.end(); ++it )
95 if( !it.value().is_string() )
97 aError = wxString::Format(
_(
"Layer map entry for '%s' must be a KiCad layer name "
98 "string" ),
From_UTF8( it.key().c_str() ) );
102 parsed[
From_UTF8( it.key().c_str() )] =
From_UTF8( it.value().get<std::string>().c_str() );
105 aMap = std::move( parsed );
167 const wxString& aReportFile,
const IMPORT_REPORT_DATA& aData )
176 nlohmann::json report;
178 report[
"source_file"] = aData.m_sourceFile.ToStdString();
179 report[
"source_format"] = aData.m_sourceFormat.ToStdString();
180 report[
"output_file"] = aData.m_outputFile.ToStdString();
182 nlohmann::json statistics = nlohmann::json::object();
184 for(
const auto& [key, value] : aData.m_statistics )
185 statistics[key.ToStdString()] = value;
187 report[
"statistics"] = statistics;
189 for(
auto it = aData.m_extraJson.begin(); it != aData.m_extraJson.end(); ++it )
190 report[it.key()] = it.value();
192 nlohmann::json warningsJson = nlohmann::json::array();
194 for(
const wxString& warning : aData.m_warnings )
195 warningsJson.push_back( warning.ToStdString() );
197 report[
"warnings"] = warningsJson;
199 nlohmann::json errorsJson = nlohmann::json::array();
201 for(
const wxString& error : aData.m_errors )
202 errorsJson.push_back( error.ToStdString() );
204 report[
"errors"] = errorsJson;
206 output = wxString::FromUTF8( report.dump( 2 ) );
210 output += wxS(
"Import Report\n" );
211 output += wxS(
"=============\n\n" );
212 output += wxString::Format( wxS(
"Source file: %s\n" ), aData.m_sourceFile );
213 output += wxString::Format( wxS(
"Source format: %s\n" ), aData.m_sourceFormat );
214 output += wxString::Format( wxS(
"Output file: %s\n\n" ), aData.m_outputFile );
215 output += wxS(
"Statistics:\n" );
217 for(
const auto& [key, value] : aData.m_statistics )
219 wxString label = key.Left( 1 ).Upper() + key.Mid( 1 );
220 output += wxString::Format( wxS(
" %s: %zu\n" ), label, value );
223 if( !aData.m_warnings.empty() )
225 output += wxS(
"\nWarnings:\n" );
227 for(
const wxString& warning : aData.m_warnings )
228 output += wxString::Format( wxS(
" - %s\n" ), warning );
231 if( !aData.m_errors.empty() )
233 output += wxS(
"\nErrors:\n" );
235 for(
const wxString& error : aData.m_errors )
236 output += wxString::Format( wxS(
" - %s\n" ), error );
240 if( aReportFile.IsEmpty() )
248 wxFile file( aReportFile, wxFile::write );
249 bool written = file.IsOpened() && file.Write( output );
251 written = file.Close() && written;
253 if( !written && aReporter )
255 aReporter->
Report( wxString::Format(
_(
"Failed to write import report to '%s'\n" ), aReportFile ),
bool WriteImportReport(REPORTER *aReporter, IMPORT_REPORT_FORMAT aFormat, const wxString &aReportFile, const IMPORT_REPORT_DATA &aData)
Emit an import report in the requested format to aReportFile, or to aReporter (at INFO severity) when...
bool ParseImportReportFormat(const wxString &aText, IMPORT_REPORT_FORMAT &aFormat)
Parse the user-facing report format name ("none", "json" or "text") into its enum.
bool LoadLayerMapFile(const wxString &aFile, std::map< wxString, wxString > &aMap, wxString &aError)
Load an explicit layer-mapping file into aMap.