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