KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_odb_dimension_export.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 <filesystem>
21#include <fstream>
22#include <memory>
23
26#include <boost/test/unit_test.hpp>
27
28#include <base_units.h>
29#include <board.h>
30#include <kiid.h>
31#include <pcb_dimension.h>
32#include <pcb_shape.h>
35#include <core/utf8.h>
36
37namespace fs = std::filesystem;
38
47static int countOdbLineRecords( const fs::path& aFeaturesFile )
48{
49 int lineCount = 0;
50 std::ifstream stream( aFeaturesFile );
51 std::string line;
52
53 while( std::getline( stream, line ) )
54 {
55 if( line.rfind( "L ", 0 ) == 0 )
56 lineCount++;
57 }
58
59 return lineCount;
60}
61
62
75static int countSilkLineRecords( const fs::path& aRoot, bool& aFoundFile )
76{
77 int total = 0;
78 aFoundFile = false;
79
80 for( const fs::directory_entry& entry : fs::recursive_directory_iterator( aRoot ) )
81 {
82 if( !entry.is_regular_file() || entry.path().filename() != "features" )
83 continue;
84
85 const std::string layerDir = entry.path().parent_path().filename().string();
86
87 if( layerDir.find( "silk" ) == std::string::npos )
88 continue;
89
90 aFoundFile = true;
91 total += countOdbLineRecords( entry.path() );
92 }
93
94 return total;
95}
96
97
98BOOST_AUTO_TEST_CASE( OdbDimensionExport )
99{
100 SETTINGS_MANAGER settingsManager;
101 std::unique_ptr<BOARD> board = KI_TEST::ReadBoardFromFileOrStream(
102 KI_TEST::GetPcbnewTestDataDir() + "issue20249/dimension.kicad_pcb" );
103
104 BOOST_REQUIRE( board );
105
106 // A dimension with no geometry would export nothing and the test would pass
107 // for the wrong reason.
108 const PCB_DIMENSION_BASE* dimension = nullptr;
109
110 for( const BOARD_ITEM* item : board->Drawings() )
111 {
112 if( item->Type() == PCB_DIM_ALIGNED_T )
113 dimension = static_cast<const PCB_DIMENSION_BASE*>( item );
114 }
115
116 BOOST_REQUIRE( dimension );
117 BOOST_REQUIRE( !dimension->GetShapes().empty() );
118
119 const fs::path outDir = fs::temp_directory_path()
120 / ( "kicad_qa_odb_dimension_20249_" + KIID().AsString().ToStdString() );
121
122 BOOST_REQUIRE( fs::create_directory( outDir ) );
123
124 PCB_IO_ODBPP odbExporter;
125 std::map<std::string, UTF8> props;
126 props["units"] = "mm";
127 props["sigfig"] = "6";
128
129 BOOST_REQUIRE_NO_THROW( odbExporter.SaveBoard( outDir.string(), *board, &props ) );
130
131 BOOST_REQUIRE_MESSAGE( fs::exists( outDir ), "ODB++ export produced no output tree" );
132
133 bool foundSilk = false;
134 int lineCount = countSilkLineRecords( outDir, foundSilk );
135
136 BOOST_REQUIRE_MESSAGE( foundSilk, "ODB++ export produced no silkscreen features file" );
137
138 BOOST_CHECK_MESSAGE( lineCount > 0,
139 "Dimensions were dropped from the ODB++ export (no line features emitted)" );
140
141 fs::remove_all( outDir );
142}
General utilities for PCB file IO for QA programs.
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:84
Definition kiid.h:46
wxString AsString() const
Definition kiid.cpp:264
Abstract dimension API.
const std::vector< std::shared_ptr< SHAPE > > & GetShapes() const
void SaveBoard(const wxString &aFileName, BOARD &aBoard, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Write aBoard to a storage file in a format that this PCB_IO implementation knows about or it can be u...
std::string GetPcbnewTestDataDir()
Utility which returns a path to the data directory where the test board files are stored.
std::unique_ptr< BOARD > ReadBoardFromFileOrStream(const std::string &aFilename, std::istream &aFallback)
Read a board from a file, or another stream, as appropriate.
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
static int countOdbLineRecords(const fs::path &aFeaturesFile)
Count line ("L ") feature records in a single ODB++ "features" file.
BOOST_AUTO_TEST_CASE(OdbDimensionExport)
static int countSilkLineRecords(const fs::path &aRoot, bool &aFoundFile)
Sum line ("L ") feature records across every silkscreen "features" file in an ODB++ export tree.
@ PCB_DIM_ALIGNED_T
class PCB_DIM_ALIGNED, a linear dimension (graphic item)
Definition typeinfo.h:94