KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_pdf_output_path.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 <filesystem>
23#include <fstream>
24#include <memory>
25
26#include <cli/exit_codes.h>
28#include <pcbnew_jobs_handler.h>
30
31#include <wx/string.h>
32
33
34BOOST_AUTO_TEST_CASE( ExportPdfSingleDocumentOutputPath )
35{
36 PCBNEW_JOBS_HANDLER handler( nullptr );
37
38 const std::filesystem::path boardPath =
39 std::filesystem::path( KI_TEST::GetPcbnewTestDataDir() ) / "api_kitchen_sink.kicad_pcb";
40
41 BOOST_REQUIRE( std::filesystem::exists( boardPath ) );
42
43 const std::filesystem::path outputRoot =
44 std::filesystem::temp_directory_path() / "kicad_pdf_single_doc_test";
45 const std::filesystem::path outputPath =
46 outputRoot / "Assembly" / "Assembly_output.pdf";
47
48 if( std::filesystem::exists( outputRoot ) )
49 std::filesystem::remove_all( outputRoot );
50
51 auto pdfJob = std::make_unique<JOB_EXPORT_PCB_PDF>();
52 pdfJob->m_filename = wxString::FromUTF8( boardPath.string().c_str() );
53 pdfJob->SetConfiguredOutputPath( wxString::FromUTF8( outputPath.string().c_str() ) );
54 pdfJob->m_plotDrawingSheet = false;
55 pdfJob->m_pdfSingle = true;
57 pdfJob->m_plotLayerSequence = LSEQ( { F_Cu } );
58
59 int result = handler.JobExportPdf( pdfJob.get() );
61
62 BOOST_CHECK( std::filesystem::exists( outputPath ) );
63 BOOST_CHECK( std::filesystem::is_regular_file( outputPath ) );
64
65 const std::filesystem::path nestedPdf =
66 outputPath / ( boardPath.stem().string() + ".pdf" );
67 BOOST_CHECK( !std::filesystem::exists( nestedPdf ) );
68
69 std::filesystem::remove_all( outputRoot );
70}
71
72
73BOOST_AUTO_TEST_CASE( ExportPdfSingleDocumentTrailingDisabledLayers )
74{
75 // 24845: trailing disabled copper layers added a blank page in single-document PDF mode.
76 PCBNEW_JOBS_HANDLER handler( nullptr );
77
78 const std::filesystem::path boardPath =
79 std::filesystem::path( KI_TEST::GetPcbnewTestDataDir() ) / "api_kitchen_sink.kicad_pcb";
80 BOOST_REQUIRE( std::filesystem::exists( boardPath ) );
81
82 const std::filesystem::path outputRoot =
83 std::filesystem::temp_directory_path() / "kicad_pdf_trailing_disabled_test";
84 const std::filesystem::path outputPath = outputRoot / "out.pdf";
85
86 if( std::filesystem::exists( outputRoot ) )
87 std::filesystem::remove_all( outputRoot );
88
89 auto pdfJob = std::make_unique<JOB_EXPORT_PCB_PDF>();
90 pdfJob->m_filename = wxString::FromUTF8( boardPath.string().c_str() );
91 pdfJob->SetConfiguredOutputPath( wxString::FromUTF8( outputPath.string().c_str() ) );
92 pdfJob->m_plotDrawingSheet = false;
93 pdfJob->m_pdfSingle = true;
95
96 // api_kitchen_sink is 2-layer, so In1_Cu is disabled and skipped, leaving 2 pages.
97 pdfJob->m_plotLayerSequence = LSEQ( { F_Cu, B_Cu, In1_Cu } );
98
99 int result = handler.JobExportPdf( pdfJob.get() );
101 BOOST_REQUIRE( std::filesystem::exists( outputPath ) );
102
103 // Each page object is written as "/Type /Page\n" (the tree root uses "/Type /Pages\n").
104 std::ifstream in( outputPath, std::ios::binary );
105 std::string pdf( ( std::istreambuf_iterator<char>( in ) ), std::istreambuf_iterator<char>() );
106
107 size_t pageCount = 0;
108
109 for( size_t pos = pdf.find( "/Type /Page\n" ); pos != std::string::npos;
110 pos = pdf.find( "/Type /Page\n", pos + 1 ) )
111 ++pageCount;
112
113 BOOST_CHECK_EQUAL( pageCount, 2u ); // 3 before the fix
114
115 std::filesystem::remove_all( outputRoot );
116}
General utilities for PCB file IO for QA programs.
@ ONE_PAGE_PER_LAYER_ONE_FILE
The most traditional output mode KiCad has had.
LSEQ is a sequence (and therefore also a set) of PCB_LAYER_IDs.
Definition lseq.h:47
@ B_Cu
Definition layer_ids.h:61
@ In1_Cu
Definition layer_ids.h:62
@ F_Cu
Definition layer_ids.h:60
static const int OK
Definition exit_codes.h:30
std::string GetPcbnewTestDataDir()
Utility which returns a path to the data directory where the test board files are stored.
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_CASE(ExportPdfSingleDocumentOutputPath)
wxString result
Test unit parsing edge cases and error handling.
BOOST_CHECK_EQUAL(result, "25.4")