KiCad PCB EDA Suite
Loading...
Searching...
No Matches
pcb_io.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 (C) 2011-2012 SoftPLC Corporation, Dick Hollenbeck <[email protected]>
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21#include <pcb_io/pcb_io.h>
22#include <pcb_io/pcb_io_mgr.h>
23#include <ki_exception.h>
24#include <wx/log.h>
25#include <wx/filename.h>
26#include <wx/translation.h>
27#include <wx/dir.h>
28
29#include <board.h>
30#include <footprint.h>
31
32
33#define FMT_UNIMPLEMENTED wxT( "Plugin \"%s\" does not implement the \"%s\" function." )
34#define NOT_IMPLEMENTED( aCaller ) \
35 THROW_IO_ERROR( wxString::Format( FMT_UNIMPLEMENTED, \
36 GetName(), \
37 wxString::FromUTF8( aCaller ) ) );
38
39
40bool PCB_IO::CanReadBoard( const wxString& aFileName ) const
41{
42 const std::vector<std::string>& exts = GetBoardFileDesc().m_FileExtensions;
43
44 wxString fileExt = wxFileName( aFileName ).GetExt().MakeLower();
45
46 for( const std::string& ext : exts )
47 {
48 if( fileExt == wxString( ext ).Lower() )
49 return true;
50 }
51
52 return false;
53}
54
55
56bool PCB_IO::CanReadFootprint( const wxString& aFileName ) const
57{
58 const std::vector<std::string>& exts = GetLibraryFileDesc().m_FileExtensions;
59
60 wxString fileExt = wxFileName( aFileName ).GetExt().MakeLower();
61
62 for( const std::string& ext : exts )
63 {
64 if( fileExt == wxString( ext ).Lower() )
65 return true;
66 }
67
68 return false;
69}
70
71
72std::unique_ptr<BOARD> PCB_IO::LoadBoard( const wxString& aFileName, const std::map<std::string, UTF8>* aProperties,
73 PROJECT* aProject )
74{
75 std::unique_ptr<BOARD> newBoard = std::make_unique<BOARD>();
76
77 newBoard->SetFileName( aFileName );
78
79 loadBoard( aFileName, *newBoard, /*aIsNewLoad = */ true, aProperties, aProject );
80
81 return newBoard;
82}
83
84
85void PCB_IO::LoadAndAppendBoard( const wxString& aFileName, BOARD& aAppendToMe,
86 const std::map<std::string, UTF8>* aProperties, PROJECT* aProject )
87{
88 loadBoard( aFileName, aAppendToMe, /*aIsNewLoad = */ false, aProperties, aProject );
89}
90
91
92void PCB_IO::loadBoard( const wxString& aFileName, BOARD& aBoard, bool aIsNewLoad,
93 const std::map<std::string, UTF8>* aProperties, PROJECT* aProject )
94{
95 NOT_IMPLEMENTED( __FUNCTION__ );
96}
97
98
100{
101 NOT_IMPLEMENTED( __FUNCTION__ );
102}
103
104
105void PCB_IO::SaveBoard( const wxString& aFileName, BOARD& aBoard, const std::map<std::string, UTF8>* aProperties )
106{
107 // not pure virtual so that plugins only have to implement subset of the PLUGIN interface.
108 NOT_IMPLEMENTED( __FUNCTION__ );
109}
110
111
112void PCB_IO::FootprintEnumerate( wxArrayString& aFootprintNames, const wxString& aLibraryPath,
113 bool aBestEfforts, const std::map<std::string, UTF8>* aProperties )
114{
115 // not pure virtual so that plugins only have to implement subset of the PLUGIN interface.
116 NOT_IMPLEMENTED( __FUNCTION__ );
117}
118
119
120std::unique_ptr<FOOTPRINT> PCB_IO::ImportFootprint( const wxString& aFootprintPath, wxString& aFootprintNameOut,
121 const std::map<std::string, UTF8>* aProperties )
122{
123 wxArrayString footprintNames;
124
125 FootprintEnumerate( footprintNames, aFootprintPath, true, aProperties );
126
127 if( footprintNames.empty() )
128 return nullptr;
129
130 if( footprintNames.size() > 1 )
131 {
132 Report( _( "Selected file contains multiple footprints. Only the first one will be "
133 "imported.\nTo load all footprints, add it as a library using Preferences "
134 "-> Manage Footprint "
135 "Libraries..." ) , RPT_SEVERITY_WARNING );
136 }
137
138 aFootprintNameOut = footprintNames.front();
139
140 return FootprintLoad( aFootprintPath, aFootprintNameOut, false, aProperties );
141}
142
143
144const FOOTPRINT* PCB_IO::GetEnumeratedFootprint( const wxString& aLibraryPath, const wxString& aFootprintName,
145 const std::map<std::string, UTF8>* aProperties )
146{
147 // default implementation returns CALLER-OWNED pointer to the footprint
148 return FootprintLoad( aLibraryPath, aFootprintName, false, aProperties ).release();
149}
150
151
152bool PCB_IO::FootprintExists( const wxString& aLibraryPath, const wxString& aFootprintName,
153 const std::map<std::string, UTF8>* aProperties )
154{
155 // default implementation
156 return FootprintLoad( aLibraryPath, aFootprintName, true, aProperties ) != nullptr;
157}
158
159
160std::unique_ptr<FOOTPRINT> PCB_IO::FootprintLoad( const wxString& aLibraryPath, const wxString& aFootprintName,
161 bool aKeepUUID, const std::map<std::string, UTF8>* aProperties )
162{
163 // not pure virtual so that plugins only have to implement subset of the PLUGIN interface.
164 NOT_IMPLEMENTED( __FUNCTION__ );
165 return nullptr;
166}
167
168
169void PCB_IO::FootprintSave( const wxString& aLibraryPath, const FOOTPRINT* aFootprint,
170 const std::map<std::string, UTF8>* aProperties )
171{
172 // not pure virtual so that plugins only have to implement subset of the PLUGIN interface.
173 NOT_IMPLEMENTED( __FUNCTION__ );
174}
175
176
177void PCB_IO::FootprintDelete( const wxString& aLibraryPath, const wxString& aFootprintName,
178 const std::map<std::string, UTF8>* aProperties )
179{
180 // not pure virtual so that plugins only have to implement subset of the PLUGIN interface.
181 NOT_IMPLEMENTED( __FUNCTION__ );
182}
183
184
185void PCB_IO::GetLibraryOptions( std::map<std::string, UTF8>* aListToAppendTo ) const
186{
187 // Get base options first
188 IO_BASE::GetLibraryOptions( aListToAppendTo );
189
190 // disable all these in another couple of months, after everyone has seen them:
191#if 1
192 (*aListToAppendTo)["debug_level"] = UTF8( _( "Enable <b>debug</b> logging for Footprint*() "
193 "functions in this PCB_IO." ) );
194
195 (*aListToAppendTo)["read_filter_regex"] = UTF8( _( "Regular expression <b>footprint name</b> "
196 "filter." ) );
197
198 (*aListToAppendTo)["enable_transaction_logging"] = UTF8( _( "Enable transaction logging. The "
199 "mere presence of this option "
200 "turns on the logging, no need to "
201 "set a Value." ) );
202
203 (*aListToAppendTo)["username"] = UTF8( _( "User name for <b>login</b> to some special library "
204 "server." ) );
205
206 (*aListToAppendTo)["password"] = UTF8( _( "Password for <b>login</b> to some special library "
207 "server." ) );
208#endif
209
210#if 1
211 // Suitable for a C++ to python PCB_IO::Footprint*() adapter, move it to the adapter
212 // if and when implemented.
213 (*aListToAppendTo)["python_footprint_plugin"] = UTF8( _( "Enter the python module which "
214 "implements the PCB_IO::Footprint*() "
215 "functions." ) );
216#endif
217}
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:409
virtual void Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) const
Definition io_base.cpp:124
virtual const IO_FILE_DESC GetLibraryFileDesc() const
Get the descriptor for the individual library elements that this IO plugin operates on.
Definition io_base.h:117
virtual void GetLibraryOptions(std::map< std::string, UTF8 > *aListToAppendTo) const
Append supported IO options to aListToAppenTo along with internationalized descriptions.
Definition io_base.cpp:65
virtual bool CanReadFootprint(const wxString &aFileName) const
Checks if this PCB_IO can read a footprint from specified file or directory.
Definition pcb_io.cpp:56
virtual std::unique_ptr< FOOTPRINT > FootprintLoad(const wxString &aLibraryPath, const wxString &aFootprintName, bool aKeepUUID=false, const std::map< std::string, UTF8 > *aProperties=nullptr)
Load a footprint having aFootprintName from the aLibraryPath containing a library format that this PC...
Definition pcb_io.cpp:160
void LoadAndAppendBoard(const wxString &aFileName, BOARD &aAppendToMe, const std::map< std::string, UTF8 > *aProperties=nullptr, PROJECT *aProject=nullptr)
Same as LoadBoard(), but appends the loaded board to an existing board, which must already exist.
Definition pcb_io.cpp:85
virtual bool CanReadBoard(const wxString &aFileName) const
Checks if this PCB_IO can read the specified board file.
Definition pcb_io.cpp:40
virtual void SaveBoard(const wxString &aFileName, BOARD &aBoard, const std::map< std::string, UTF8 > *aProperties=nullptr)
Write aBoard to a storage file in a format that this PCB_IO implementation knows about or it can be u...
Definition pcb_io.cpp:105
virtual void FootprintEnumerate(wxArrayString &aFootprintNames, const wxString &aLibraryPath, bool aBestEfforts, const std::map< std::string, UTF8 > *aProperties=nullptr)
Return a list of footprint names contained within the library at aLibraryPath.
Definition pcb_io.cpp:112
virtual std::unique_ptr< FOOTPRINT > ImportFootprint(const wxString &aFootprintPath, wxString &aFootprintNameOut, const std::map< std::string, UTF8 > *aProperties=nullptr)
Load a single footprint from aFootprintPath and put its name in aFootprintNameOut.
Definition pcb_io.cpp:120
virtual void GetLibraryOptions(std::map< std::string, UTF8 > *aListToAppendTo) const override
Append supported PLUGIN options to aListToAppenTo along with internationalized descriptions.
Definition pcb_io.cpp:185
std::unique_ptr< BOARD > LoadBoard(const wxString &aFileName, const std::map< std::string, UTF8 > *aProperties=nullptr, PROJECT *aProject=nullptr)
Load information from some input file format that this PCB_IO implementation knows about into new BOA...
Definition pcb_io.cpp:72
virtual bool FootprintExists(const wxString &aLibraryPath, const wxString &aFootprintName, const std::map< std::string, UTF8 > *aProperties=nullptr)
Check for the existence of a footprint.
Definition pcb_io.cpp:152
virtual std::vector< FOOTPRINT * > GetImportedCachedLibraryFootprints()
Return a container with the cached library footprints generated in the last call to Load.
Definition pcb_io.cpp:99
virtual void loadBoard(const wxString &aFileName, BOARD &aBoard, bool aIsNewLoad, const std::map< std::string, UTF8 > *aProperties, PROJECT *aProject)
Parse aFileName into aBoard.
Definition pcb_io.cpp:92
virtual const FOOTPRINT * GetEnumeratedFootprint(const wxString &aLibraryPath, const wxString &aFootprintName, const std::map< std::string, UTF8 > *aProperties=nullptr)
A version of FootprintLoad() for use after FootprintEnumerate() for more efficient cache management i...
Definition pcb_io.cpp:144
virtual const IO_BASE::IO_FILE_DESC GetBoardFileDesc() const
Returns board file description for the PCB_IO.
Definition pcb_io.h:81
virtual void FootprintDelete(const wxString &aLibraryPath, const wxString &aFootprintName, const std::map< std::string, UTF8 > *aProperties=nullptr)
Delete aFootprintName from the library at aLibraryPath.
Definition pcb_io.cpp:177
virtual void FootprintSave(const wxString &aLibraryPath, const FOOTPRINT *aFootprint, const std::map< std::string, UTF8 > *aProperties=nullptr)
Write aFootprint to an existing library located at aLibraryPath.
Definition pcb_io.cpp:169
Container for project specific data.
Definition project.h:63
An 8 bit string that is assuredly encoded in UTF8, and supplies special conversion support to and fro...
Definition utf8.h:67
#define _(s)
#define NOT_IMPLEMENTED(aCaller)
Definition io_base.cpp:34
@ RPT_SEVERITY_WARNING
std::vector< std::string > m_FileExtensions
Filter used for file pickers if m_IsFile is true.
Definition io_base.h:47