From e842a788db3dc87d2f1347cad34a847a91696080 Mon Sep 17 00:00:00 2001 From: Marek Roszko Date: Sun, 13 Aug 2023 17:31:59 -0400 Subject: [PATCH] Split ERC report to its own class like drc --- eeschema/CMakeLists.txt | 1 + eeschema/dialogs/dialog_erc.cpp | 69 +------------ eeschema/erc_report.cpp | 167 ++++++++++++++++++++++++++++++++ eeschema/erc_report.h | 44 +++++++++ 4 files changed, 217 insertions(+), 64 deletions(-) create mode 100644 eeschema/erc_report.cpp create mode 100644 eeschema/erc_report.h diff --git a/eeschema/CMakeLists.txt b/eeschema/CMakeLists.txt index 9e9ba2237f..187e7c089f 100644 --- a/eeschema/CMakeLists.txt +++ b/eeschema/CMakeLists.txt @@ -289,6 +289,7 @@ set( EESCHEMA_SRCS eeschema_settings.cpp erc.cpp erc_item.cpp + erc_report.cpp erc_sch_pin_context.cpp erc_settings.cpp fields_data_model.cpp diff --git a/eeschema/dialogs/dialog_erc.cpp b/eeschema/dialogs/dialog_erc.cpp index e281972fc0..958fa64500 100644 --- a/eeschema/dialogs/dialog_erc.cpp +++ b/eeschema/dialogs/dialog_erc.cpp @@ -38,6 +38,7 @@ #include #include #include +#include #include #include #include @@ -976,7 +977,9 @@ void DIALOG_ERC::OnSaveReport( wxCommandEvent& aEvent ) fn.MakeAbsolute( prj_path ); } - if( writeReport( fn.GetFullPath() ) ) + ERC_REPORT reportWriter( &m_parent->Schematic(), m_parent->GetUserUnits() ); + + if( reportWriter.WriteTextReport( fn.GetFullPath() ) ) { m_messages->Report( wxString::Format( _( "Report file '%s' created." ), fn.GetFullPath() ) ); @@ -986,66 +989,4 @@ void DIALOG_ERC::OnSaveReport( wxCommandEvent& aEvent ) DisplayError( this, wxString::Format( _( "Failed to create file '%s'." ), fn.GetFullPath() ) ); } -} - - -bool DIALOG_ERC::writeReport( const wxString& aFullFileName ) -{ - wxFFile file( aFullFileName, wxT( "wt" ) ); - - if( !file.IsOpened() ) - return false; - - wxString msg = wxString::Format( _( "ERC report (%s, Encoding UTF8)\n" ), GetISO8601CurrentDateTime() ); - - std::map itemMap; - - int err_count = 0; - int warn_count = 0; - int total_count = 0; - SCH_SHEET_LIST sheetList = m_parent->Schematic().GetSheets(); - - sheetList.FillItemMap( itemMap ); - - ERC_SETTINGS& settings = m_parent->Schematic().ErcSettings(); - - for( unsigned i = 0; i < sheetList.size(); i++ ) - { - msg << wxString::Format( _( "\n***** Sheet %s\n" ), sheetList[i].PathHumanReadable() ); - - for( SCH_ITEM* aItem : sheetList[i].LastScreen()->Items().OfType( SCH_MARKER_T ) ) - { - const SCH_MARKER* marker = static_cast( aItem ); - RC_ITEM* item = marker->GetRCItem().get(); - SEVERITY severity = settings.GetSeverity( item->GetErrorCode() ); - - if( marker->GetMarkerType() != MARKER_BASE::MARKER_ERC ) - continue; - - total_count++; - - switch( severity ) - { - case RPT_SEVERITY_ERROR: err_count++; break; - case RPT_SEVERITY_WARNING: warn_count++; break; - default: break; - } - - msg << marker->GetRCItem()->ShowReport( m_parent, severity, itemMap ); - } - } - - msg << wxString::Format( _( "\n ** ERC messages: %d Errors %d Warnings %d\n" ), - total_count, err_count, warn_count ); - - // Currently: write report using UTF8 (as usual in Kicad). - // TODO: see if we can use the current encoding page (mainly for Windows users), - // Or other format (HTML?) - file.Write( msg ); - - // wxFFile dtor will close the file. - - return true; -} - - +} \ No newline at end of file diff --git a/eeschema/erc_report.cpp b/eeschema/erc_report.cpp new file mode 100644 index 0000000000..526b3d6cff --- /dev/null +++ b/eeschema/erc_report.cpp @@ -0,0 +1,167 @@ +/* + * This program source code file is part of KiCad, a free EDA CAD application. + * + * Copyright (C) 2023 KiCad Developers, see AUTHORS.txt for contributors. + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#include +#include + +#include +#include +#include +#include +#include +#include "erc_report.h" +#include +#include +#include +#include +#include +#include + + +ERC_REPORT::ERC_REPORT( SCHEMATIC* aSchematic, EDA_UNITS aReportUnits ) : + m_sch( aSchematic ), + m_reportUnits( aReportUnits ) +{ +} + + +bool ERC_REPORT::WriteTextReport( const wxString& aFullFileName ) +{ + wxFFile file( aFullFileName, wxT( "wt" ) ); + + if( !file.IsOpened() ) + return false; + + UNITS_PROVIDER unitsProvider( schIUScale, m_reportUnits ); + + wxString msg = wxString::Format( _( "ERC report (%s, Encoding UTF8)\n" ), + GetISO8601CurrentDateTime() ); + + std::map itemMap; + + int err_count = 0; + int warn_count = 0; + int total_count = 0; + SCH_SHEET_LIST sheetList = m_sch->GetSheets(); + + sheetList.FillItemMap( itemMap ); + + ERC_SETTINGS& settings = m_sch->ErcSettings(); + + for( unsigned i = 0; i < sheetList.size(); i++ ) + { + msg << wxString::Format( _( "\n***** Sheet %s\n" ), sheetList[i].PathHumanReadable() ); + + for( SCH_ITEM* aItem : sheetList[i].LastScreen()->Items().OfType( SCH_MARKER_T ) ) + { + const SCH_MARKER* marker = static_cast( aItem ); + RC_ITEM* item = marker->GetRCItem().get(); + SEVERITY severity = settings.GetSeverity( item->GetErrorCode() ); + + if( marker->GetMarkerType() != MARKER_BASE::MARKER_ERC ) + continue; + + total_count++; + + switch( severity ) + { + case RPT_SEVERITY_ERROR: err_count++; break; + case RPT_SEVERITY_WARNING: warn_count++; break; + default: break; + } + + msg << marker->GetRCItem()->ShowReport( &unitsProvider, severity, itemMap ); + } + } + + msg << wxString::Format( _( "\n ** ERC messages: %d Errors %d Warnings %d\n" ), total_count, + err_count, warn_count ); + + // Currently: write report using UTF8 (as usual in Kicad). + // TODO: see if we can use the current encoding page (mainly for Windows users), + // Or other format (HTML?) + file.Write( msg ); + + // wxFFile dtor will close the file. + + return true; +} + + +bool ERC_REPORT::WriteJsonReport( const wxString& aFullFileName ) +{ + std::ofstream jsonFileStream( aFullFileName.fn_str() ); + + UNITS_PROVIDER unitsProvider( pcbIUScale, m_reportUnits ); + std::map itemMap; + + RC_JSON::ERC_REPORT reportHead; + reportHead.source = m_sch->GetFileName(); + reportHead.date = GetISO8601CurrentDateTime(); + reportHead.kicad_version = GetMajorMinorPatchVersion(); + reportHead.coordinate_units = EDA_UNIT_UTILS::GetLabel( m_reportUnits ); + + int err_count = 0; + int warn_count = 0; + int total_count = 0; + SCH_SHEET_LIST sheetList = m_sch->GetSheets(); + sheetList.FillItemMap( itemMap ); + + ERC_SETTINGS& settings = m_sch->ErcSettings(); + + for( unsigned i = 0; i < sheetList.size(); i++ ) + { + RC_JSON::ERC_SHEET jsonSheet; + jsonSheet.path = sheetList[i].PathHumanReadable(); + + for( SCH_ITEM* aItem : sheetList[i].LastScreen()->Items().OfType( SCH_MARKER_T ) ) + { + const SCH_MARKER* marker = static_cast( aItem ); + RC_ITEM* item = marker->GetRCItem().get(); + SEVERITY severity = settings.GetSeverity( item->GetErrorCode() ); + + if( marker->GetMarkerType() != MARKER_BASE::MARKER_ERC ) + continue; + + total_count++; + + switch( severity ) + { + case RPT_SEVERITY_ERROR: err_count++; break; + case RPT_SEVERITY_WARNING: warn_count++; break; + default: break; + } + + RC_JSON::VIOLATION violation; + marker->GetRCItem()->GetJsonViolation( violation, &unitsProvider, severity, itemMap ); + + jsonSheet.violations.push_back( violation ); + } + + reportHead.sheets.push_back( jsonSheet ); + } + + + nlohmann::json saveJson = nlohmann::json( reportHead ); + jsonFileStream << std::setw( 4 ) << saveJson << std::endl; + jsonFileStream.flush(); + jsonFileStream.close(); + + return true; +} \ No newline at end of file diff --git a/eeschema/erc_report.h b/eeschema/erc_report.h new file mode 100644 index 0000000000..d19cc50462 --- /dev/null +++ b/eeschema/erc_report.h @@ -0,0 +1,44 @@ +/* + * This program source code file is part of KiCad, a free EDA CAD application. + * + * Copyright (C) 2023 KiCad Developers, see AUTHORS.txt for contributors. + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#ifndef ERC_REPORT_H +#define ERC_REPORT_H + +#include +#include +#include + +class SCHEMATIC; +class RC_ITEMS_PROVIDER; + +class ERC_REPORT +{ +public: + ERC_REPORT( SCHEMATIC* aSchematic, EDA_UNITS aReportUnits ); + + bool WriteTextReport( const wxString& aFullFileName ); + bool WriteJsonReport( const wxString& aFullFileName ); + +private: + SCHEMATIC* m_sch; + EDA_UNITS m_reportUnits; +}; + + +#endif \ No newline at end of file