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
25#include <boost/test/unit_test.hpp>
26
27#include <base_units.h>
28#include <board.h>
29#include <kiid.h>
30#include <pcb_dimension.h>
31#include <pcb_shape.h>
34#include <core/utf8.h>
35
36namespace fs = std::filesystem;
37
46static int countOdbLineRecords( const fs::path& aFeaturesFile )
47{
48 int lineCount = 0;
49 std::ifstream stream( aFeaturesFile );
50 std::string line;
51
52 while( std::getline( stream, line ) )
53 {
54 if( line.rfind( "L ", 0 ) == 0 )
55 lineCount++;
56 }
57
58 return lineCount;
59}
60
61
74static int countSilkLineRecords( const fs::path& aRoot, bool& aFoundFile )
75{
76 int total = 0;
77 aFoundFile = false;
78
79 for( const fs::directory_entry& entry : fs::recursive_directory_iterator( aRoot ) )
80 {
81 if( !entry.is_regular_file() || entry.path().filename() != "features" )
82 continue;
83
84 const std::string layerDir = entry.path().parent_path().filename().string();
85
86 if( layerDir.find( "silk" ) == std::string::npos )
87 continue;
88
89 aFoundFile = true;
90 total += countOdbLineRecords( entry.path() );
91 }
92
93 return total;
94}
95
96
97BOOST_AUTO_TEST_CASE( OdbDimensionExport )
98{
99 SETTINGS_MANAGER settingsManager;
100 std::unique_ptr<BOARD> board = std::make_unique<BOARD>();
101
102 const int side = pcbIUScale.mmToIU( 20 );
103
104 // Give the board a square Edge.Cuts outline so the export produces a valid board profile. Edge
105 // cuts land in their own layer feature file, not the silkscreen one we inspect, so they cannot
106 // mask a dropped dimension.
107 auto addEdge = [&]( const VECTOR2I& aStart, const VECTOR2I& aEnd )
108 {
109 PCB_SHAPE* edge = new PCB_SHAPE( board.get(), SHAPE_T::SEGMENT );
110 edge->SetLayer( Edge_Cuts );
111 edge->SetStart( aStart );
112 edge->SetEnd( aEnd );
113 edge->SetWidth( pcbIUScale.mmToIU( 0.1 ) );
114 board->Add( edge );
115 };
116
117 addEdge( { 0, 0 }, { side, 0 } );
118 addEdge( { side, 0 }, { side, side } );
119 addEdge( { side, side }, { 0, side } );
120 addEdge( { 0, side }, { 0, 0 } );
121
122 // The item under test: an aligned dimension on the front silkscreen.
123 PCB_DIM_ALIGNED* dimension = new PCB_DIM_ALIGNED( board.get(), PCB_DIM_ALIGNED_T );
124 dimension->SetLayer( F_SilkS );
125 dimension->SetStart( { 0, 0 } );
126 dimension->SetEnd( { side, 0 } );
127 dimension->SetHeight( pcbIUScale.mmToIU( 3 ) );
128 dimension->SetLineThickness( pcbIUScale.mmToIU( 0.15 ) );
129
130 // Update() must run after the feature points are set to populate the geometry returned by
131 // GetShapes(); otherwise the dimension has no shapes to plot.
132 dimension->Update();
133
134 BOOST_REQUIRE_MESSAGE( !dimension->GetShapes().empty(),
135 "Dimension produced no geometry to export" );
136
137 board->Add( dimension );
138
139 const fs::path outDir = fs::temp_directory_path()
140 / ( "kicad_qa_odb_dimension_20249_" + KIID().AsString().ToStdString() );
141
142 BOOST_REQUIRE( fs::create_directory( outDir ) );
143
144 PCB_IO_ODBPP odbExporter;
145 std::map<std::string, UTF8> props;
146 props["units"] = "mm";
147 props["sigfig"] = "6";
148
149 BOOST_REQUIRE_NO_THROW( odbExporter.SaveBoard( outDir.string(), board.get(), &props ) );
150
151 BOOST_REQUIRE_MESSAGE( fs::exists( outDir ), "ODB++ export produced no output tree" );
152
153 bool foundSilk = false;
154 int lineCount = countSilkLineRecords( outDir, foundSilk );
155
156 BOOST_REQUIRE_MESSAGE( foundSilk, "ODB++ export produced no silkscreen features file" );
157
158 BOOST_CHECK_MESSAGE( lineCount > 0,
159 "Dimensions were dropped from the ODB++ export (no line features emitted)" );
160
161 fs::remove_all( outDir );
162}
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:121
Definition kiid.h:44
wxString AsString() const
Definition kiid.cpp:242
For better understanding of the points that make a dimension:
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...
void SetWidth(int aWidth) override
void SetEnd(const VECTOR2I &aEnd) override
void SetLayer(PCB_LAYER_ID aLayer) override
Set the layer this item is on.
void SetStart(const VECTOR2I &aStart) override
@ SEGMENT
Definition eda_shape.h:46
@ Edge_Cuts
Definition layer_ids.h:108
@ F_SilkS
Definition layer_ids.h:96
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.
BOOST_CHECK_MESSAGE(totalMismatches==0, std::to_string(totalMismatches)+" board(s) with strategy disagreements")
@ PCB_DIM_ALIGNED_T
class PCB_DIM_ALIGNED, a linear dimension (graphic item)
Definition typeinfo.h:95
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683