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#include <qa_utils/file_utils.h>
22
23#include <string>
24
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 KI_TEST::SCOPED_TEMP_DIR tempDir( "kicad_gbr_zero_seg" );
51 const wxString gbrPath = tempDir.CreateChildFileStr( "plot.gbr" );
52
53 BOOST_REQUIRE( !gbrPath.IsEmpty() );
54 BOOST_TEST_MESSAGE( "Gerber output: " << gbrPath.ToStdString() );
55 BOOST_REQUIRE( plotter.OpenFile( gbrPath ) );
56
57 // PCB internal units: 1 IU = 1 nm, so IUs per decimil = 2540.
58 plotter.SetViewport( VECTOR2I( 0, 0 ), 2540.0, 1.0, false );
59 BOOST_REQUIRE( plotter.StartPlot( wxT( "1" ) ) );
60
61 const int width = 2 * 1000000; // 2 mm in nm
62 const VECTOR2I origin( 0, 0 );
63
64 plotter.ThickSegment( origin, origin, width, nullptr );
65 BOOST_REQUIRE( plotter.EndPlot() );
66
67 // Slurp the resulting gerber file.
68 wxFFile file( gbrPath, wxT( "rb" ) );
69 BOOST_REQUIRE( file.IsOpened() );
70 wxFileOffset len = file.Length();
71 BOOST_REQUIRE_GT( len, 0 );
72
73 std::string buffer;
74 buffer.resize( static_cast<size_t>( len ) );
75 BOOST_REQUIRE_EQUAL( file.Read( buffer.data(), len ), static_cast<size_t>( len ) );
76 file.Close();
77
78 // The 2 mm aperture must be present.
79 BOOST_CHECK_MESSAGE( buffer.find( "C,2.000000" ) != std::string::npos,
80 "Expected 2.000000 mm circle aperture in gerber output" );
81
82 // No phantom 0 mm aperture should leak out.
83 BOOST_CHECK_MESSAGE( buffer.find( "C,0.000000" ) == std::string::npos,
84 "Spurious 0.000000 mm aperture present (issue 24131 regression)" );
85
86 // No circular interpolation commands either; the degenerate point should be
87 // a flash, not an arc.
88 BOOST_CHECK_MESSAGE( buffer.find( "G02" ) == std::string::npos
89 && buffer.find( "G03" ) == std::string::npos,
90 "Unexpected circular interpolation in zero-length segment plot" );
91
92 // The pre-fix output had about 76 D01 commands. The expected v9.0.9 output is
93 // exactly one D02 (move) immediately followed by one D01 (flash) at the origin.
94 int d01Count = CountOccurrences( buffer, "D01*" );
95 BOOST_CHECK_EQUAL( d01Count, 1 );
96 BOOST_CHECK_MESSAGE( buffer.find( "X0Y0D02*" ) != std::string::npos
97 && buffer.find( "X0Y0D01*" ) != std::string::npos,
98 "Expected single zero-length segment flash at origin" );
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.
wxString CreateChildFileStr(const wxString &aName) const
Create and return the path to a direct child file as a wxString.
virtual bool OpenFile(const wxString &aFullFilename)
Open or create the plot file aFullFilename.
Definition plotter.cpp:75
void SetRenderSettings(RENDER_SETTINGS *aSettings)
Definition plotter.h:166
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).
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_TEST_MESSAGE("Polyline has "<< chain.PointCount()<< " points")
BOOST_CHECK_EQUAL(result, "25.4")
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683