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 );
111 const wxString& aReportFile,
const IMPORT_REPORT_DATA& aData )
120 nlohmann::json report;
122 report[
"source_file"] = aData.m_sourceFile.ToStdString();
123 report[
"source_format"] = aData.m_sourceFormat.ToStdString();
124 report[
"output_file"] = aData.m_outputFile.ToStdString();
126 nlohmann::json statistics = nlohmann::json::object();
128 for(
const auto& [key, value] : aData.m_statistics )
129 statistics[key.ToStdString()] = value;
131 report[
"statistics"] = statistics;
133 for(
auto it = aData.m_extraJson.begin(); it != aData.m_extraJson.end(); ++it )
134 report[it.key()] = it.value();
136 nlohmann::json warningsJson = nlohmann::json::array();
138 for(
const wxString& warning : aData.m_warnings )
139 warningsJson.push_back( warning.ToStdString() );
141 report[
"warnings"] = warningsJson;
143 nlohmann::json errorsJson = nlohmann::json::array();
145 for(
const wxString& error : aData.m_errors )
146 errorsJson.push_back( error.ToStdString() );
148 report[
"errors"] = errorsJson;
150 output = wxString::FromUTF8( report.dump( 2 ) );
154 output += wxS(
"Import Report\n" );
155 output += wxS(
"=============\n\n" );
156 output += wxString::Format( wxS(
"Source file: %s\n" ), aData.m_sourceFile );
157 output += wxString::Format( wxS(
"Source format: %s\n" ), aData.m_sourceFormat );
158 output += wxString::Format( wxS(
"Output file: %s\n\n" ), aData.m_outputFile );
159 output += wxS(
"Statistics:\n" );
161 for(
const auto& [key, value] : aData.m_statistics )
163 wxString label = key.Left( 1 ).Upper() + key.Mid( 1 );
164 output += wxString::Format( wxS(
" %s: %zu\n" ), label, value );
167 if( !aData.m_warnings.empty() )
169 output += wxS(
"\nWarnings:\n" );
171 for(
const wxString& warning : aData.m_warnings )
172 output += wxString::Format( wxS(
" - %s\n" ), warning );
176 if( !aReportFile.IsEmpty() )
178 wxFile file( aReportFile, wxFile::write );
180 if( file.IsOpened() )
187 aReporter->
Report( wxString::Format(
_(
"Failed to write import report to '%s'\n" ),
void 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.