KiCad PCB EDA Suite
Loading...
Searching...
No Matches
board_file_utils.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/tools/context.hpp>
21#include <memory>
22
23#include <boost/test/unit_test.hpp>
24
26
27// For PCB parsing
30#include <richio.h>
31#include <board.h>
32#include <footprint.h>
33
35
36namespace KI_TEST
37{
38
39#ifndef QA_PCBNEW_DATA_LOCATION
40 #define QA_PCBNEW_DATA_LOCATION "???"
41#endif
42
44{
45 const char* env = std::getenv( "KICAD_TEST_PCBNEW_DATA_DIR" );
46 std::string fn;
47
48 if( !env )
49 {
50 // Use the compiled-in location of the data dir (i.e. where the files were at build time)
52 }
53 else
54 {
55 // Use whatever was given in the env var
56 fn = env;
57 }
58
59 // Ensure the string ends in / to force a directory interpretation
60 fn += "/";
61
62 return fn;
63}
64
65
66void DumpBoardToFile( BOARD& board, const std::filesystem::path& aFilename )
67{
69 io.SaveBoard( aFilename.string(), board );
70}
71
72
73std::unique_ptr<BOARD_ITEM> ReadBoardItemFromStream( std::istream& aStream )
74{
75 // Take input from stdin
77 reader.SetStream( aStream );
78
79 PCB_IO_KICAD_SEXPR_PARSER parser( &reader, nullptr, nullptr );
80 std::unique_ptr<BOARD_ITEM> board;
81
82 try
83 {
84 board.reset( parser.Parse() );
85 }
86 catch( const IO_ERROR& e )
87 {
88 throw e;
89 }
90
91 return board;
92}
93
94
95std::unique_ptr<BOARD> ReadBoardFromFileOrStream( const std::string& aFilename,
96 std::istream& aFallback )
97{
98 std::istream* in_stream = nullptr;
99 std::ifstream file_stream;
100
101 if( aFilename.empty() )
102 {
103 // no file, read stdin
104 in_stream = &aFallback;
105 }
106 else
107 {
108 file_stream.open( aFilename );
109
110 if( !file_stream.is_open() )
111 {
112 return nullptr;
113 }
114
115 in_stream = &file_stream;
116 }
117
118 return ReadItemFromStream<BOARD>( *in_stream );
119}
120
121
122std::unique_ptr<BOARD> ReadBoardFromFile( const std::filesystem::path& aFilename )
123{
124 return ReadBoardFromFileOrStream( aFilename.string() );
125}
126
127
128std::unique_ptr<BOARD> RequireBoardFromPcbnewQAData( const std::filesystem::path& aFilename )
129{
130 std::unique_ptr<BOARD> board;
131
132 // Make it really clear in the test context which board is being loaded
133 BOOST_TEST_CONTEXT( "Loading board " << aFilename )
134 {
135 board = ReadBoardFromFile( KI_TEST::GetPcbnewTestDataDir() + aFilename.string() );
136
137 BOOST_REQUIRE( board );
138 }
139
140 return board;
141}
142
143
144std::unique_ptr<FOOTPRINT> ReadFootprintFromFile( const std::filesystem::path& aFilename,
145 const wxString& aFootprintName )
146{
148 wxString libPath;
149 wxFileName fn( aFilename.string() );
150
151 if( fn.GetExt().Lower() == FILEEXT::KiCadFootprintFileExtension )
152 libPath = fn.GetPath();
153 else
154 libPath = aFilename.string();
155
156 try
157 {
158 return io.FootprintLoad( libPath, aFootprintName, true );
159 }
160 catch( const IO_ERROR& exc )
161 {
162 BOOST_TEST_CHECKPOINT( "Exception '" << exc.what() << "' occurred reading footprint file '" << aFilename );
163 }
164
165 return nullptr;
166}
167
168
169void DumpFootprintToFile( const FOOTPRINT& aFootprint, const std::filesystem::path& aLibraryPath )
170{
172
173 try
174 {
175 PRETTIFIED_FILE_OUTPUTFORMATTER formatter( aLibraryPath.string() );
176
177 io.SetOutputFormatter( &formatter );
178 io.FormatFootprintLibFile( const_cast<FOOTPRINT*>( &aFootprint ) );
179 formatter.Finish();
180 }
181 catch( const IO_ERROR& exc )
182 {
183 BOOST_TEST_CHECKPOINT( "Exception '" << exc.what() << "' occurred writing footprint file '" << aLibraryPath );
184 }
185}
186
187
188} // namespace KI_TEST
#define QA_PCBNEW_DATA_LOCATION
General utilities for PCB file IO for QA programs.
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:411
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
virtual const char * what() const override
std::exception interface, returned as UTF-8
Read a Pcbnew s-expression formatted LINE_READER object and returns the appropriate BOARD_ITEM object...
A PCB_IO derivation for saving and loading Pcbnew s-expression formatted files.
void FormatFootprintLibFile(FOOTPRINT *aFootprint) const
Output aFootprint as a complete footprint library file.
std::unique_ptr< FOOTPRINT > FootprintLoad(const wxString &aLibraryPath, const wxString &aFootprintName, bool aKeepUUID=false, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Load a footprint having aFootprintName from the aLibraryPath containing a library format that this PC...
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 SetOutputFormatter(OUTPUTFORMATTER *aFormatter)
bool Finish() override
Runs prettification over the buffered bytes, writes them to the sibling temp file,...
Definition richio.cpp:710
LINE_READER that wraps a given std::istream instance.
void SetStream(std::istream &aStream)
Set the stream for this line reader.
static const std::string KiCadFootprintFileExtension
Test utilities for timestamps.
std::unique_ptr< BOARD > ReadBoardFromFile(const std::filesystem::path &aFilename)
std::string GetPcbnewTestDataDir()
Utility which returns a path to the data directory where the test board files are stored.
void DumpFootprintToFile(const FOOTPRINT &aFootprint, const std::filesystem::path &aLibraryPath)
Same as DumpBoardToFile, but for footprints.
std::unique_ptr< BOARD > RequireBoardFromPcbnewQAData(const std::filesystem::path &aFilename)
Require a board from the path relative to the pcbnew QA data directory.
std::unique_ptr< ITEM > ReadItemFromStream(std::istream &aStream)
Read a specific kind of BOARD_ITEM from a stream.
std::unique_ptr< BOARD > ReadBoardFromFileOrStream(const std::string &aFilename, std::istream &aFallback)
Read a board from a file, or another stream, as appropriate.
std::unique_ptr< FOOTPRINT > ReadFootprintFromFile(const std::filesystem::path &aFilename, const wxString &aFootprintName)
Read a footprint object aFootprintName from aFileName.
void DumpBoardToFile(BOARD &board, const std::filesystem::path &aFilename)
Utility function to simply write a Board out to a file.
std::unique_ptr< BOARD_ITEM > ReadBoardItemFromStream(std::istream &aStream)
Utility function to read a BOARD_ITEM (probably a FOOTPRINT or a BOARD) from a file.
Pcbnew s-expression file format parser definition.
BOOST_REQUIRE(res.columns()==1)
BOOST_TEST_CONTEXT("Test Clearance")