KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_gerber_plotter.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 2
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, see <https://www.gnu.org/licenses/>.
18 */
19
20#include <boost/test/unit_test.hpp>
21
22#include <string>
23
24#include <wx/filename.h>
25#include <wx/ffile.h>
26
29
30
31BOOST_AUTO_TEST_SUITE( GerberPlotter )
32
33
34// Regression test for https://gitlab.com/kicad/code/kicad/-/issues/24131
35//
36// A zero-length thick segment (start == end) must produce a single flash of the
37// segment's aperture, not a stroked outline of a phantom 0-width aperture.
38// The pre-fix code delegated to PLOTTER::ThickSegment with width set to the
39// DO_NOT_SET_LINE_WIDTH sentinel (-2). PLOTTER::ThickSegment then called
40// Circle( start, -2, FILLED, 0 ), which the GERBER_PLOTTER turned into a tiny
41// polyArc with line width 0, producing an ADD11C,0 aperture and ~76 D01
42// commands sampling a degenerate radius.
43BOOST_AUTO_TEST_CASE( ZeroLengthSegmentEmitsSingleFlash )
44{
45 GERBER_PLOTTER plotter;
46 SIMPLE_RENDER_SETTINGS renderSettings;
47
48 plotter.SetRenderSettings( &renderSettings );
49
50 wxString gbrPath = wxFileName::CreateTempFileName( wxT( "kicad_gbr_zero_seg" ) );
51 BOOST_REQUIRE( !gbrPath.IsEmpty() );
52 BOOST_TEST_MESSAGE( "Gerber output: " << gbrPath.ToStdString() );
53 BOOST_REQUIRE( plotter.OpenFile( gbrPath ) );
54
55 // PCB internal units: 1 IU = 1 nm, so IUs per decimil = 2540.
56 plotter.SetViewport( VECTOR2I( 0, 0 ), 2540.0, 1.0, false );
57 BOOST_REQUIRE( plotter.StartPlot( wxT( "1" ) ) );
58
59 const int width = 2 * 1000000; // 2 mm in nm
60 const VECTOR2I origin( 0, 0 );
61
62 plotter.ThickSegment( origin, origin, width, nullptr );
63 BOOST_REQUIRE( plotter.EndPlot() );
64
65 // Slurp the resulting gerber file.
66 wxFFile file( gbrPath, wxT( "rb" ) );
67 BOOST_REQUIRE( file.IsOpened() );
68 wxFileOffset len = file.Length();
69 BOOST_REQUIRE_GT( len, 0 );
70
71 std::string buffer;
72 buffer.resize( static_cast<size_t>( len ) );
73 BOOST_REQUIRE_EQUAL( file.Read( buffer.data(), len ), static_cast<size_t>( len ) );
74 file.Close();
75
76 // The 2 mm aperture must be present.
77 BOOST_CHECK_MESSAGE( buffer.find( "C,2.000000" ) != std::string::npos,
78 "Expected 2.000000 mm circle aperture in gerber output" );
79
80 // No phantom 0 mm aperture should leak out.
81 BOOST_CHECK_MESSAGE( buffer.find( "C,0.000000" ) == std::string::npos,
82 "Spurious 0.000000 mm aperture present (issue 24131 regression)" );
83
84 // No circular interpolation commands either; the degenerate point should be
85 // a flash, not an arc.
86 BOOST_CHECK_MESSAGE( buffer.find( "G02" ) == std::string::npos
87 && buffer.find( "G03" ) == std::string::npos,
88 "Unexpected circular interpolation in zero-length segment plot" );
89
90 // The pre-fix output had about 76 D01 commands. The expected v9.0.9 output is
91 // exactly one D02 (move) immediately followed by one D01 (flash) at the origin.
92 int d01Count = CountOccurrences( buffer, "D01*" );
93 BOOST_CHECK_EQUAL( d01Count, 1 );
94 BOOST_CHECK_MESSAGE( buffer.find( "X0Y0D02*" ) != std::string::npos
95 && buffer.find( "X0Y0D01*" ) != std::string::npos,
96 "Expected single zero-length segment flash at origin" );
97
98 MaybeRemoveFile( gbrPath );
99}
100
101
virtual void ThickSegment(const VECTOR2I &start, const VECTOR2I &end, int width, void *aData) override
virtual void SetViewport(const VECTOR2I &aOffset, double aIusPerDecimil, double aScale, bool aMirror) override
Set the plot offset and scaling for the current plot.
virtual bool EndPlot() override
virtual bool StartPlot(const wxString &pageNumber) override
Write GERBER header to file initialize global variable g_Plot_PlotOutputFile.
virtual bool OpenFile(const wxString &aFullFilename)
Open or create the plot file aFullFilename.
Definition plotter.cpp:73
void SetRenderSettings(RENDER_SETTINGS *aSettings)
Definition plotter.h:163
Minimal concrete render settings suitable for plotters in tests.
int CountOccurrences(const std::string &aHaystack, const std::string &aNeedle)
Count occurrences of a substring in a string (overlapping allowed).
void MaybeRemoveFile(const wxString &aPath, const wxString &aEnvVar=wxT("KICAD_KEEP_TEST_PDF"))
Remove a file unless the given environment variable is set (defaults to KICAD_KEEP_TEST_PDF).
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
BOOST_CHECK_MESSAGE(totalMismatches==0, std::to_string(totalMismatches)+" board(s) with strategy disagreements")
BOOST_TEST_MESSAGE("\n=== Real-World Polygon PIP Benchmark ===\n"<< formatTable(table))
BOOST_CHECK_EQUAL(result, "25.4")
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683