7
mirror of https://gitlab.com/kicad/code/kicad.git synced 2024-11-24 16:15:01 +00:00
kicad/eeschema/erc/erc_report.cpp
Wayne Stambaugh a6923b3de3 Cache full schematic sheet list sorted by sheet page number.
The sheet list is returned as a copy of the cached list rather than a
reference to prevent external code from changing the list.  While not as
performant, it eliminates the risk of the sheet list being altered in
ways that could break the schematic.  The sheet list should only be
updated by calling SCHEMATIC::RefreshHierarchy() when any appropriate
sheet changes are made.

Note to developers: there is something inherently different about how the
QA tests are loading and handling schematics versus the schematic editor.
Using the cached sheet list for the SCHEMATIC object will cause some QA
test to fail.  This is why SCHEMATIC::Hierarchy() has not replaced
SCHEMATIC::BuildSheetListSortedByPageNumbers() everywhere.
2024-10-09 09:58:50 -04:00

160 lines
5.0 KiB
C++

/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2023, 2024 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 <http://www.gnu.org/licenses/>.
*/
#include <wx/ffile.h>
#include <wx/string.h>
#include <sch_screen.h>
#include <sch_marker.h>
#include <schematic.h>
#include <string_utils.h>
#include <build_version.h>
#include "erc_report.h"
#include <eda_units.h>
#include <erc/erc.h>
#include <fstream>
#include <macros.h>
#include <nlohmann/json.hpp>
#include <rc_json_schema.h>
ERC_REPORT::ERC_REPORT( SCHEMATIC* aSchematic, EDA_UNITS aReportUnits ) :
m_sch( aSchematic ),
m_reportUnits( aReportUnits )
{
}
wxString ERC_REPORT::GetTextReport()
{
UNITS_PROVIDER unitsProvider( schIUScale, m_reportUnits );
wxString msg = wxString::Format( _( "ERC report (%s, Encoding UTF8)\n" ),
GetISO8601CurrentDateTime() );
std::map<KIID, EDA_ITEM*> itemMap;
int err_count = 0;
int warn_count = 0;
int total_count = 0;
SCH_SHEET_LIST sheetList = m_sch->BuildSheetListSortedByPageNumbers();
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<const SCH_MARKER*>( 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 );
return msg;
}
bool ERC_REPORT::WriteTextReport( const wxString& aFullFileName )
{
wxFFile file( aFullFileName, wxT( "wt" ) );
if( !file.IsOpened() )
return false;
file.Write( GetTextReport() );
// 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<KIID, EDA_ITEM*> itemMap;
RC_JSON::ERC_REPORT reportHead;
wxFileName fn( m_sch->GetFileName() );
reportHead.$schema = "https://schemas.kicad.org/erc.v1.json";
reportHead.source = fn.GetFullName();
reportHead.date = GetISO8601CurrentDateTime();
reportHead.kicad_version = GetMajorMinorPatchVersion();
reportHead.coordinate_units = EDA_UNIT_UTILS::GetLabel( m_reportUnits );
SCH_SHEET_LIST sheetList = m_sch->Hierarchy();
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();
jsonSheet.uuid_path = sheetList[i].Path().AsString();
for( SCH_ITEM* aItem : sheetList[i].LastScreen()->Items().OfType( SCH_MARKER_T ) )
{
const SCH_MARKER* marker = static_cast<const SCH_MARKER*>( aItem );
RC_ITEM* item = marker->GetRCItem().get();
SEVERITY severity = settings.GetSeverity( item->GetErrorCode() );
if( marker->GetMarkerType() != MARKER_BASE::MARKER_ERC )
continue;
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;
}