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, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 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
24#include <boost/test/unit_test.hpp>
25
26#include <string>
27
28#include <wx/filename.h>
29#include <wx/ffile.h>
30
33
34
35BOOST_AUTO_TEST_SUITE( GerberPlotter )
36
37
38// Regression test for https://gitlab.com/kicad/code/kicad/-/issues/24131
39//
40// A zero-length thick segment (start == end) must produce a single flash of the
41// segment's aperture, not a stroked outline of a phantom 0-width aperture.
42// The pre-fix code delegated to PLOTTER::ThickSegment with width set to the
43// DO_NOT_SET_LINE_WIDTH sentinel (-2). PLOTTER::ThickSegment then called
44// Circle( start, -2, FILLED, 0 ), which the GERBER_PLOTTER turned into a tiny
45// polyArc with line width 0, producing an ADD11C,0 aperture and ~76 D01
46// commands sampling a degenerate radius.
47BOOST_AUTO_TEST_CASE( ZeroLengthSegmentEmitsSingleFlash )
48{
49 GERBER_PLOTTER plotter;
50 SIMPLE_RENDER_SETTINGS renderSettings;
51
52 plotter.SetRenderSettings( &renderSettings );
53
54 wxString gbrPath = wxFileName::CreateTempFileName( wxT( "kicad_gbr_zero_seg" ) );
55 BOOST_REQUIRE( !gbrPath.IsEmpty() );
56 BOOST_TEST_MESSAGE( "Gerber output: " << gbrPath.ToStdString() );
57 BOOST_REQUIRE( plotter.OpenFile( gbrPath ) );
58
59 // PCB internal units: 1 IU = 1 nm, so IUs per decimil = 2540.
60 plotter.SetViewport( VECTOR2I( 0, 0 ), 2540.0, 1.0, false );
61 BOOST_REQUIRE( plotter.StartPlot( wxT( "1" ) ) );
62
63 const int width = 2 * 1000000; // 2 mm in nm
64 const VECTOR2I origin( 0, 0 );
65
66 plotter.ThickSegment( origin, origin, width, nullptr );
67 BOOST_REQUIRE( plotter.EndPlot() );
68
69 // Slurp the resulting gerber file.
70 wxFFile file( gbrPath, wxT( "rb" ) );
71 BOOST_REQUIRE( file.IsOpened() );
72 wxFileOffset len = file.Length();
73 BOOST_REQUIRE_GT( len, 0 );
74
75 std::string buffer;
76 buffer.resize( static_cast<size_t>( len ) );
77 BOOST_REQUIRE_EQUAL( file.Read( buffer.data(), len ), static_cast<size_t>( len ) );
78 file.Close();
79
80 // The 2 mm aperture must be present.
81 BOOST_CHECK_MESSAGE( buffer.find( "C,2.000000" ) != std::string::npos,
82 "Expected 2.000000 mm circle aperture in gerber output" );
83
84 // No phantom 0 mm aperture should leak out.
85 BOOST_CHECK_MESSAGE( buffer.find( "C,0.000000" ) == std::string::npos,
86 "Spurious 0.000000 mm aperture present (issue 24131 regression)" );
87
88 // No circular interpolation commands either; the degenerate point should be
89 // a flash, not an arc.
90 BOOST_CHECK_MESSAGE( buffer.find( "G02" ) == std::string::npos
91 && buffer.find( "G03" ) == std::string::npos,
92 "Unexpected circular interpolation in zero-length segment plot" );
93
94 // The pre-fix output had about 76 D01 commands. The expected v9.0.9 output is
95 // exactly one D02 (move) immediately followed by one D01 (flash) at the origin.
96 int d01Count = CountOccurrences( buffer, "D01*" );
97 BOOST_CHECK_EQUAL( d01Count, 1 );
98 BOOST_CHECK_MESSAGE( buffer.find( "X0Y0D02*" ) != std::string::npos
99 && buffer.find( "X0Y0D01*" ) != std::string::npos,
100 "Expected single zero-length segment flash at origin" );
101
102 MaybeRemoveFile( gbrPath );
103}
104
105
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:77
void SetRenderSettings(RENDER_SETTINGS *aSettings)
Definition plotter.h:167
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:687