KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_line_ending_load_save.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 <boost/test/unit_test.hpp>
25#include <qa_utils/file_utils.h>
27
28#include <footprint.h>
29#include <pcb_shape.h>
30
31#include <line_ending.h>
34
35#include <filesystem>
36
37
38BOOST_AUTO_TEST_SUITE( PcbLineEndingIo )
39
40
41namespace
42{
43
44void checkEnding( const LINE_ENDING& aEnding, LINE_ENDING_STYLE aStyle, int aLength, int aWidth,
45 int aStrokeWidth )
46{
47 BOOST_CHECK_EQUAL( static_cast<int>( aEnding.GetStyle() ), static_cast<int>( aStyle ) );
48 BOOST_CHECK_EQUAL( aEnding.GetLength(), aLength );
49 BOOST_CHECK_EQUAL( aEnding.GetWidth(), aWidth );
50 BOOST_CHECK_EQUAL( aEnding.GetStrokeWidth(), aStrokeWidth );
51}
52
53PCB_SHAPE* findFirstSegment( FOOTPRINT* aFootprint )
54{
55 for( BOARD_ITEM* item : aFootprint->GraphicalItems() )
56 {
57 PCB_SHAPE* shape = dynamic_cast<PCB_SHAPE*>( item );
58
59 if( shape && shape->GetShape() == SHAPE_T::SEGMENT )
60 return shape;
61 }
62
63 return nullptr;
64}
65
66
67} // namespace
68
69
70BOOST_AUTO_TEST_CASE( FootprintLineEndingsRoundTrip )
71{
72 FOOTPRINT footprint( nullptr );
73 footprint.SetReference( wxS( "U1" ) );
74 footprint.SetValue( wxS( "LineEndingFootprint" ) );
75
76 auto* line = new PCB_SHAPE( &footprint );
77 line->SetShape( SHAPE_T::SEGMENT );
78 line->SetStart( VECTOR2I( 0, 0 ) );
79 line->SetEnd( VECTOR2I( 1000000, 0 ) );
80 line->SetLayer( F_SilkS );
81 line->SetWidth( 100000 );
82
83 line->SetStartEndingStyle( LINE_ENDING_STYLE::SQUARE );
84 line->SetStartEndingLength( 300000 );
85 line->SetStartEndingWidth( 200000 );
86 line->SetStartEndingStrokeWidth( 50000 );
87
88 line->SetEndEndingStyle( LINE_ENDING_STYLE::ARROW_OPEN );
89 line->SetEndEndingLength( 500000 );
90 line->SetEndEndingWidth( 400000 );
91 line->SetEndEndingStrokeWidth( 100000 );
92
93 footprint.Add( line, ADD_MODE::APPEND, true );
94
95 KI_TEST::SCOPED_TEMP_DIR tempLib( "kicad_qa_fp_line_ending_io" );
96 const std::filesystem::path libPath = tempLib.CreateChildDir( "line_ending_roundtrip.pretty" );
97 const std::filesystem::path savePath = libPath / "line_ending_roundtrip.kicad_mod";
98
99 KI_TEST::DumpFootprintToFile( footprint, savePath.string() );
100 std::unique_ptr<FOOTPRINT> loadedFootprint =
101 KI_TEST::ReadFootprintFromFileOrStream( savePath.string() );
102
103 BOOST_REQUIRE( loadedFootprint );
104
105 PCB_SHAPE* loadedLine = findFirstSegment( loadedFootprint.get() );
106 BOOST_REQUIRE( loadedLine );
107 checkEnding( loadedLine->GetStartEnding(), LINE_ENDING_STYLE::SQUARE, 300000, 200000, 50000 );
108 checkEnding( loadedLine->GetEndEnding(), LINE_ENDING_STYLE::ARROW_OPEN, 500000, 400000, 100000 );
109}
110
111
112BOOST_AUTO_TEST_CASE( FootprintLineEndingEqualLengthWidthRoundTrip )
113{
114 FOOTPRINT footprint( nullptr );
115 footprint.SetReference( wxS( "U1" ) );
116 footprint.SetValue( wxS( "LineEndingEqualSizeFootprint" ) );
117
118 auto* line = new PCB_SHAPE( &footprint );
119 line->SetShape( SHAPE_T::SEGMENT );
120 line->SetStart( VECTOR2I( 0, 0 ) );
121 line->SetEnd( VECTOR2I( 1000000, 0 ) );
122 line->SetLayer( F_SilkS );
123 line->SetWidth( 100000 );
124
125 line->SetStartEndingStyle( LINE_ENDING_STYLE::SQUARE );
126 line->SetStartEndingLength( 300000 );
127 line->SetStartEndingWidth( 300000 );
128
129 footprint.Add( line, ADD_MODE::APPEND, true );
130
131 KI_TEST::SCOPED_TEMP_DIR tempLib( "kicad_qa_fp_line_ending_equal_size_io" );
132 const std::filesystem::path libPath =
133 tempLib.CreateChildDir( "line_ending_equal_size.pretty" );
134 const std::filesystem::path savePath = libPath / "line_ending_equal_size.kicad_mod";
135
136 KI_TEST::DumpFootprintToFile( footprint, savePath.string() );
137 std::unique_ptr<FOOTPRINT> loadedFootprint =
138 KI_TEST::ReadFootprintFromFileOrStream( savePath.string() );
139
140 BOOST_REQUIRE( loadedFootprint );
141
142 PCB_SHAPE* loadedLine = findFirstSegment( loadedFootprint.get() );
143 BOOST_REQUIRE( loadedLine );
144 checkEnding( loadedLine->GetStartEnding(), LINE_ENDING_STYLE::SQUARE, 300000, 300000, 0 );
145}
146
147
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
SHAPE_T GetShape() const
Definition eda_shape.h:175
const LINE_ENDING & GetStartEnding() const
Definition eda_shape.h:177
const LINE_ENDING & GetEndEnding() const
Definition eda_shape.h:180
void SetReference(const wxString &aReference)
Definition footprint.h:907
void SetValue(const wxString &aValue)
Definition footprint.h:930
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_MODE::INSERT, bool aSkipConnectivity=false) override
Removes an item from the container.
DRAWINGS & GraphicalItems()
Definition footprint.h:407
std::filesystem::path CreateChildDir(const wxString &aName) const
Create and return the path to a direct child directory.
Decorative shape (arrowhead, circle, square) at the start or end of a graphic line,...
Definition line_ending.h:62
int GetLength() const
Along-line size. 0 = auto.
Definition line_ending.h:84
int GetWidth() const
Perpendicular size. 0 = auto.
Definition line_ending.h:87
LINE_ENDING_STYLE GetStyle() const
Definition line_ending.h:81
int GetStrokeWidth() const
Outline stroke width.
@ SEGMENT
Definition eda_shape.h:56
@ F_SilkS
Definition layer_ids.h:96
LINE_ENDING_STYLE
Line ending styles for graphic lines, arcs, and beziers.
Definition line_ending.h:44
void DumpFootprintToFile(const FOOTPRINT &aFootprint, const std::filesystem::path &aLibraryPath)
Same as DumpBoardToFile, but for footprints.
std::unique_ptr< FOOTPRINT > ReadFootprintFromFileOrStream(const std::string &aFilename, std::istream &aFallback)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(FootprintLineEndingsRoundTrip)
BOOST_CHECK_EQUAL(result, "25.4")
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683