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
21
22// For PCB parsing
25#include <richio.h>
26#include <board.h>
27#include <footprint.h>
28
30
31namespace KI_TEST
32{
33
34#ifndef QA_PCBNEW_DATA_LOCATION
35 #define QA_PCBNEW_DATA_LOCATION "???"
36#endif
37
39{
40 const char* env = std::getenv( "KICAD_TEST_PCBNEW_DATA_DIR" );
41 std::string fn;
42
43 if( !env )
44 {
45 // Use the compiled-in location of the data dir (i.e. where the files were at build time)
47 }
48 else
49 {
50 // Use whatever was given in the env var
51 fn = env;
52 }
53
54 // Ensure the string ends in / to force a directory interpretation
55 fn += "/";
56
57 return fn;
58}
59
60
61void DumpBoardToFile( BOARD& board, const std::string& aFilename )
62{
64 io.SaveBoard( aFilename, &board );
65}
66
67
68std::unique_ptr<BOARD_ITEM> ReadBoardItemFromStream( std::istream& aStream )
69{
70 // Take input from stdin
72 reader.SetStream( aStream );
73
74 PCB_IO_KICAD_SEXPR_PARSER parser( &reader, nullptr, nullptr );
75 std::unique_ptr<BOARD_ITEM> board;
76
77 try
78 {
79 board.reset( parser.Parse() );
80 }
81 catch( const IO_ERROR& e )
82 {
83 throw e;
84 }
85
86 return board;
87}
88
89
90std::unique_ptr<BOARD> ReadBoardFromFileOrStream( const std::string& aFilename,
91 std::istream& aFallback )
92{
93 std::istream* in_stream = nullptr;
94 std::ifstream file_stream;
95
96 if( aFilename.empty() )
97 {
98 // no file, read stdin
99 in_stream = &aFallback;
100 }
101 else
102 {
103 file_stream.open( aFilename );
104
105 if( !file_stream.is_open() )
106 {
107 return nullptr;
108 }
109
110 in_stream = &file_stream;
111 }
112
113 return ReadItemFromStream<BOARD>( *in_stream );
114}
115
116
117std::unique_ptr<FOOTPRINT> ReadFootprintFromFileOrStream( const std::string& aFilename,
118 std::istream& aFallback )
119{
120 std::istream* in_stream = nullptr;
121 std::ifstream file_stream;
122
123 if( aFilename.empty() )
124 {
125 // no file, read stdin
126 in_stream = &aFallback;
127 }
128 else
129 {
130 file_stream.open( aFilename );
131
132 if( !file_stream.is_open() )
133 {
134 return nullptr;
135 }
136
137 in_stream = &file_stream;
138 }
139
140 return ReadItemFromStream<FOOTPRINT>( *in_stream );
141}
142
143
144void DumpFootprintToFile( const FOOTPRINT& aFootprint, const std::string& aLibraryPath )
145{
147 io.FootprintSave( aLibraryPath, &aFootprint, nullptr );
148}
149
150
151} // 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:372
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Read a Pcbnew s-expression formatted LINE_READER object and returns the appropriate BOARD_ITEM object...
A #PLUGIN derivation for saving and loading Pcbnew s-expression formatted files.
void FootprintSave(const wxString &aLibraryPath, const FOOTPRINT *aFootprint, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Write aFootprint to an existing library located at aLibraryPath.
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...
LINE_READER that wraps a given std::istream instance.
void SetStream(std::istream &aStream)
Set the stream for this line reader.
std::string GetPcbnewTestDataDir()
Utility which returns a path to the data directory where the test board files are stored.
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 > ReadFootprintFromFileOrStream(const std::string &aFilename, std::istream &aFallback)
void DumpBoardToFile(BOARD &board, const std::string &aFilename)
Utility function to simply write a Board out to a file.
void DumpFootprintToFile(const FOOTPRINT &aFootprint, const std::string &aLibraryPath)
Same as DumpBoardToFile, but for footprints.
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.