KiCad PCB EDA Suite
Loading...
Searching...
No Matches
diff_job_output.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 3
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * http://www.gnu.org/licenses/gpl-3.0.html
19 * or you may search the http://www.gnu.org website for the version 3 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
25
28
29#include <cli/exit_codes.h>
30#include <eda_units.h>
31#include <reporter.h>
32
33#include <wx/translation.h>
34
35#include <string>
36
37
38namespace KICAD_DIFF
39{
40
41DIFF_EMIT_OPTIONS MakeEmitOptions( const JOB_DIFF_BASE& aJob, const wxString& aLabelA,
42 const wxString& aLabelB )
43{
45 opts.format = aJob.m_format;
46 opts.outputPath = aJob.GetFullOutputPath( nullptr );
47 opts.labelA = aLabelA;
48 opts.labelB = aLabelB;
49
50 return opts;
51}
52
53
58
59
60int EmitDiffResult( const DOCUMENT_DIFF& aResult, const DIFF_EMIT_OPTIONS& aOptions,
61 int aDiffExitCode, REPORTER& aReporter )
62{
64
65 // PNG / SVG render through the plotter and write straight to the output
66 // path, bypassing the JSON/text string path.
67 if( aOptions.format == FORMAT::PNG || aOptions.format == FORMAT::SVG )
68 {
69 DIFF_COLOR_THEME theme;
70 DIFF_SCENE scene = BuildScene( aResult, theme );
71 scene.docKind = aOptions.docKind;
72
73 if( aOptions.referenceGeometry )
74 scene.referenceGeometry = aOptions.referenceGeometry( theme.reference );
75
76 if( aOptions.comparisonGeometry )
77 scene.comparisonGeometry = aOptions.comparisonGeometry( theme.comparison );
78
80 opts.theme = theme;
81
82 bool ok = ( aOptions.format == FORMAT::PNG )
83 ? RenderSceneToPng( scene, aOptions.outputPath, opts )
84 : RenderSceneToSvg( scene, aOptions.outputPath, opts );
85
86 if( !ok )
87 {
88 aReporter.Report( _( "Failed to render diff image\n" ), RPT_SEVERITY_ERROR );
90 }
91
92 return aDiffExitCode;
93 }
94
95 // Text output renders distance/coordinate/angle deltas in user units. Units
96 // are not yet configurable on the diff jobs, so default to mm; the IU scale
97 // tracks the source document kind (PCB vs schematic) already threaded here.
98 std::string output =
99 ( aOptions.format == FORMAT::JSON )
100 ? aResult.ToJson().dump( 2 )
101 : FormatDiffAsText( aResult, aOptions.labelA, aOptions.labelB, EDA_UNITS::MM,
102 IuScaleForDocKind( aOptions.docKind ) );
103
104 if( !WriteDiffOutput( output, aOptions.outputPath ) )
106
107 return aDiffExitCode;
108}
109
110} // namespace KICAD_DIFF
Shared base for the document/library diff jobs (PCB, schematic, symbol and footprint library).
OUTPUT_FORMAT m_format
wxString GetFullOutputPath(PROJECT *aProject) const
Returns the full output path for the job, taking into account the configured output path,...
Definition job.cpp:156
A pure virtual class used to derive REPORTER objects from.
Definition reporter.h:71
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)
Report a string with a given severity.
Definition reporter.h:100
#define _(s)
static const int ERR_RC_VIOLATIONS
Rules check violation count was greater than 0.
Definition exit_codes.h:37
static const int SUCCESS
Definition exit_codes.h:29
static const int ERR_INVALID_OUTPUT_CONFLICT
Definition exit_codes.h:34
DIFF_SCENE BuildScene(const DOCUMENT_DIFF &aDiff, const DIFF_COLOR_THEME &aTheme)
Build a DIFF_SCENE from a DOCUMENT_DIFF, populating the shape lists and computing the union bbox.
bool WriteDiffOutput(const std::string &aContent, const wxString &aOutputPath)
Write diff/merge text output to aOutputPath, or to stdout when the path is empty.
const EDA_IU_SCALE & IuScaleForDocKind(DOC_KIND aKind)
Internal-unit scale a document kind's coordinates are expressed in.
Definition diff_scene.h:253
bool RenderSceneToSvg(const DIFF_SCENE &aScene, const wxString &aOutputPath, const PLOTTER_RENDER_OPTIONS &aOptions)
Render a DIFF_SCENE to an SVG file.
bool RenderSceneToPng(const DIFF_SCENE &aScene, const wxString &aOutputPath, const PLOTTER_RENDER_OPTIONS &aOptions)
Render a DIFF_SCENE to a PNG file.
DIFF_EMIT_OPTIONS MakeEmitOptions(const JOB_DIFF_BASE &aJob, const wxString &aLabelA, const wxString &aLabelB)
Build a DIFF_EMIT_OPTIONS pre-filled from the job's format, resolved output path and the supplied per...
int EmitDiffResult(const DOCUMENT_DIFF &aResult, const DIFF_EMIT_OPTIONS &aOptions, int aDiffExitCode, REPORTER &aReporter)
Emit a computed DOCUMENT_DIFF in the requested format.
std::string FormatDiffAsText(const DOCUMENT_DIFF &aDiff, const wxString &aLabelA, const wxString &aLabelB, EDA_UNITS aUnits, const EDA_IU_SCALE &aScale)
Render a DOCUMENT_DIFF as the human-readable text report shared by the diff jobs and CLI: a diff <lab...
int DiffExitCode(const DOCUMENT_DIFF &aResult)
Map a computed diff onto its CLI exit code – SUCCESS when empty, otherwise ERR_RC_VIOLATIONS.
@ RPT_SEVERITY_ERROR
KIGFX::COLOR4D reference
Default color for source-document context geometry.
Definition diff_scene.h:287
Describes how a computed DOCUMENT_DIFF should be emitted by a diff job.
std::function< DOCUMENT_GEOMETRY(const KIGFX::COLOR4D &)> comparisonGeometry
JOB_DIFF_BASE::OUTPUT_FORMAT format
DOC_KIND docKind
Source document type, propagated onto the scene so the PNG/SVG renderer sizes its viewport with the m...
std::function< DOCUMENT_GEOMETRY(const KIGFX::COLOR4D &)> referenceGeometry
wxString outputPath
empty -> stdout (JSON/text only)
DOCUMENT_GEOMETRY referenceGeometry
Background geometry from the two source documents.
Definition diff_scene.h:239
DOCUMENT_GEOMETRY comparisonGeometry
Definition diff_scene.h:240
DOC_KIND docKind
Source document type.
Definition diff_scene.h:227
The full set of changes between two parsed documents of one type.
nlohmann::json ToJson() const
Options controlling the headless PNG/SVG renderer.
nlohmann::json output