KiCad PCB EDA Suite
Loading...
Searching...
No Matches
plugin.cpp
Go to the documentation of this file.
1#include "io_mgr.h"
2/*
3 * This program source code file is part of KiCad, a free EDA CAD application.
4 *
5 * Copyright (C) 2011-2012 SoftPLC Corporation, Dick Hollenbeck <[email protected]>
6 * Copyright (C) 2016-2023 KiCad Developers, see AUTHORS.txt for contributors.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, you may find one here:
20 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21 * or you may search the http://www.gnu.org website for the version 2 license,
22 * or you may write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 */
25
26#include <unordered_set>
27#include <io_mgr.h>
28#include <ki_exception.h>
29#include <string_utf8_map.h>
30#include <wx/log.h>
31#include <wx/filename.h>
32#include <wx/translation.h>
33#include <wx/dir.h>
34
35
36#define FMT_UNIMPLEMENTED wxT( "Plugin \"%s\" does not implement the \"%s\" function." )
37#define NOT_IMPLEMENTED( aCaller ) \
38 THROW_IO_ERROR( wxString::Format( FMT_UNIMPLEMENTED, \
39 PluginName(), \
40 wxString::FromUTF8( aCaller ) ) );
41
42
44{
45 return PLUGIN_FILE_DESC( wxEmptyString, {} );
46}
47
48
50{
51 return PLUGIN_FILE_DESC( wxEmptyString, {} );
52}
53
54
56{
57 return PLUGIN_FILE_DESC( wxEmptyString, {} );
58}
59
60
61bool PLUGIN::CanReadBoard( const wxString& aFileName ) const
62{
63 const std::vector<std::string>& exts = GetBoardFileDesc().m_FileExtensions;
64
65 wxString fileExt = wxFileName( aFileName ).GetExt().MakeLower();
66
67 for( const std::string& ext : exts )
68 {
69 if( fileExt == wxString( ext ).Lower() )
70 return true;
71 }
72
73 return false;
74}
75
76
77bool PLUGIN::CanReadFootprint( const wxString& aFileName ) const
78{
79 const std::vector<std::string>& exts = GetFootprintFileDesc().m_FileExtensions;
80
81 wxString fileExt = wxFileName( aFileName ).GetExt().MakeLower();
82
83 for( const std::string& ext : exts )
84 {
85 if( fileExt == wxString( ext ).Lower() )
86 return true;
87 }
88
89 return false;
90}
91
92
93bool PLUGIN::CanReadFootprintLib( const wxString& aFileName ) const
94{
96
97 if( desc.m_IsFile )
98 {
99 const std::vector<std::string>& exts = desc.m_FileExtensions;
100
101 wxString fileExt = wxFileName( aFileName ).GetExt().MakeLower();
102
103 for( const std::string& ext : exts )
104 {
105 if( fileExt == wxString( ext ).Lower() )
106 return true;
107 }
108 }
109 else
110 {
111 wxDir dir( aFileName );
112
113 if( !dir.IsOpened() )
114 return false;
115
116 std::vector<std::string> exts = desc.m_ExtensionsInDir;
117 std::unordered_set<wxString> lowerExts;
118
119 for( const std::string& ext : exts )
120 lowerExts.emplace( wxString( ext ).MakeLower() );
121
122 wxString filenameStr;
123
124 bool cont = dir.GetFirst( &filenameStr, wxEmptyString, wxDIR_FILES | wxDIR_HIDDEN );
125 while( cont )
126 {
127 wxString ext = wxS( "" );
128
129 int idx = filenameStr.Find( '.', true );
130 if( idx != -1 )
131 ext = filenameStr.Mid( idx + 1 ).MakeLower();
132
133 if( lowerExts.count( ext ) )
134 return true;
135
136 cont = dir.GetNext( &filenameStr );
137 }
138 }
139
140 return false;
141}
142
143
144BOARD* PLUGIN::LoadBoard( const wxString& aFileName, BOARD* aAppendToMe,
145 const STRING_UTF8_MAP* aProperties, PROJECT* aProject,
146 PROGRESS_REPORTER* aProgressReporter )
147{
148 NOT_IMPLEMENTED( __FUNCTION__ );
149}
150
151
153{
154 NOT_IMPLEMENTED( __FUNCTION__ );
155}
156
157
158void PLUGIN::SaveBoard( const wxString& aFileName, BOARD* aBoard,
159 const STRING_UTF8_MAP* aProperties, PROGRESS_REPORTER* aProgressReporter )
160{
161 // not pure virtual so that plugins only have to implement subset of the PLUGIN interface.
162 NOT_IMPLEMENTED( __FUNCTION__ );
163}
164
165
166void PLUGIN::FootprintEnumerate( wxArrayString& aFootprintNames, const wxString& aLibraryPath,
167 bool aBestEfforts, const STRING_UTF8_MAP* aProperties )
168{
169 // not pure virtual so that plugins only have to implement subset of the PLUGIN interface.
170 NOT_IMPLEMENTED( __FUNCTION__ );
171}
172
173
174void PLUGIN::PrefetchLib( const wxString&, const STRING_UTF8_MAP* )
175{
176}
177
178
179FOOTPRINT* PLUGIN::ImportFootprint( const wxString& aFootprintPath, wxString& aFootprintNameOut,
180 const STRING_UTF8_MAP* aProperties )
181{
182 wxArrayString footprintNames;
183
184 FootprintEnumerate( footprintNames, aFootprintPath, true, aProperties );
185
186 if( footprintNames.empty() )
187 return nullptr;
188
189 if( footprintNames.size() > 1 )
190 {
191 wxLogWarning( _( "Selected file contains multiple footprints. Only the first one will be "
192 "imported." ) );
193 }
194
195 aFootprintNameOut = footprintNames.front();
196
197 return FootprintLoad( aFootprintPath, aFootprintNameOut, false, aProperties );
198}
199
200
201const FOOTPRINT* PLUGIN::GetEnumeratedFootprint( const wxString& aLibraryPath,
202 const wxString& aFootprintName,
203 const STRING_UTF8_MAP* aProperties )
204{
205 // default implementation
206 return FootprintLoad( aLibraryPath, aFootprintName, false, aProperties );
207}
208
209
210bool PLUGIN::FootprintExists( const wxString& aLibraryPath, const wxString& aFootprintName,
211 const STRING_UTF8_MAP* aProperties )
212{
213 // default implementation
214 return FootprintLoad( aLibraryPath, aFootprintName, true, aProperties ) != nullptr;
215}
216
217
218FOOTPRINT* PLUGIN::FootprintLoad( const wxString& aLibraryPath, const wxString& aFootprintName,
219 bool aKeepUUID, const STRING_UTF8_MAP* aProperties )
220{
221 // not pure virtual so that plugins only have to implement subset of the PLUGIN interface.
222 NOT_IMPLEMENTED( __FUNCTION__ );
223}
224
225
226void PLUGIN::FootprintSave( const wxString& aLibraryPath, const FOOTPRINT* aFootprint,
227 const STRING_UTF8_MAP* aProperties )
228{
229 // not pure virtual so that plugins only have to implement subset of the PLUGIN interface.
230 NOT_IMPLEMENTED( __FUNCTION__ );
231}
232
233
234void PLUGIN::FootprintDelete( const wxString& aLibraryPath, const wxString& aFootprintName,
235 const STRING_UTF8_MAP* aProperties )
236{
237 // not pure virtual so that plugins only have to implement subset of the PLUGIN interface.
238 NOT_IMPLEMENTED( __FUNCTION__ );
239}
240
241
242void PLUGIN::FootprintLibCreate( const wxString& aLibraryPath, const STRING_UTF8_MAP* aProperties )
243{
244 // not pure virtual so that plugins only have to implement subset of the PLUGIN interface.
245 NOT_IMPLEMENTED( __FUNCTION__ );
246}
247
248
249bool PLUGIN::FootprintLibDelete( const wxString& aLibraryPath, const STRING_UTF8_MAP* aProperties )
250{
251 // not pure virtual so that plugins only have to implement subset of the PLUGIN interface.
252 NOT_IMPLEMENTED( __FUNCTION__ );
253}
254
255
256bool PLUGIN::IsFootprintLibWritable( const wxString& aLibraryPath )
257{
258 // not pure virtual so that plugins only have to implement subset of the PLUGIN interface.
259 NOT_IMPLEMENTED( __FUNCTION__ );
260}
261
262
263void PLUGIN::FootprintLibOptions( STRING_UTF8_MAP* aListToAppendTo ) const
264{
265 // disable all these in another couple of months, after everyone has seen them:
266#if 1
267 (*aListToAppendTo)["debug_level"] = UTF8( _( "Enable <b>debug</b> logging for Footprint*() "
268 "functions in this PLUGIN." ) );
269
270 (*aListToAppendTo)["read_filter_regex"] = UTF8( _( "Regular expression <b>footprint name</b> "
271 "filter." ) );
272
273 (*aListToAppendTo)["enable_transaction_logging"] = UTF8( _( "Enable transaction logging. The "
274 "mere presence of this option "
275 "turns on the logging, no need to "
276 "set a Value." ) );
277
278 (*aListToAppendTo)["username"] = UTF8( _( "User name for <b>login</b> to some special library "
279 "server." ) );
280
281 (*aListToAppendTo)["password"] = UTF8( _( "Password for <b>login</b> to some special library "
282 "server." ) );
283#endif
284
285#if 1
286 // Suitable for a C++ to python PLUGIN::Footprint*() adapter, move it to the adapter
287 // if and when implemented.
288 (*aListToAppendTo)["python_footprint_plugin"] = UTF8( _( "Enter the python module which "
289 "implements the PLUGIN::Footprint*() "
290 "functions." ) );
291#endif
292}
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:276
virtual bool CanReadFootprintLib(const wxString &aFileName) const
Checks if this PLUGIN can read footprint library from specified file or directory.
Definition: plugin.cpp:93
virtual const FOOTPRINT * GetEnumeratedFootprint(const wxString &aLibraryPath, const wxString &aFootprintName, const STRING_UTF8_MAP *aProperties=nullptr)
A version of FootprintLoad() for use after FootprintEnumerate() for more efficient cache management.
Definition: plugin.cpp:201
virtual void FootprintLibCreate(const wxString &aLibraryPath, const STRING_UTF8_MAP *aProperties=nullptr)
Create a new empty footprint library at aLibraryPath empty.
Definition: plugin.cpp:242
virtual bool FootprintLibDelete(const wxString &aLibraryPath, const STRING_UTF8_MAP *aProperties=nullptr)
Delete an existing footprint library and returns true, or if library does not exist returns false,...
Definition: plugin.cpp:249
virtual PLUGIN_FILE_DESC GetBoardFileDesc() const
Returns board file description for the PLUGIN.
Definition: plugin.cpp:43
virtual void FootprintSave(const wxString &aLibraryPath, const FOOTPRINT *aFootprint, const STRING_UTF8_MAP *aProperties=nullptr)
Write aFootprint to an existing library located at aLibraryPath.
Definition: plugin.cpp:226
virtual std::vector< FOOTPRINT * > GetImportedCachedLibraryFootprints()
Return a container with the cached library footprints generated in the last call to Load.
Definition: plugin.cpp:152
virtual PLUGIN_FILE_DESC GetFootprintFileDesc() const
Returns footprint file description for the PLUGIN.
Definition: plugin.cpp:49
virtual void SaveBoard(const wxString &aFileName, BOARD *aBoard, const STRING_UTF8_MAP *aProperties=nullptr, PROGRESS_REPORTER *aProgressReporter=nullptr)
Write aBoard to a storage file in a format that this PLUGIN implementation knows about or it can be u...
Definition: plugin.cpp:158
virtual void FootprintDelete(const wxString &aLibraryPath, const wxString &aFootprintName, const STRING_UTF8_MAP *aProperties=nullptr)
Delete aFootprintName from the library at aLibraryPath.
Definition: plugin.cpp:234
virtual bool FootprintExists(const wxString &aLibraryPath, const wxString &aFootprintName, const STRING_UTF8_MAP *aProperties=nullptr)
Check for the existence of a footprint.
Definition: plugin.cpp:210
virtual FOOTPRINT * FootprintLoad(const wxString &aLibraryPath, const wxString &aFootprintName, bool aKeepUUID=false, const STRING_UTF8_MAP *aProperties=nullptr)
Load a footprint having aFootprintName from the aLibraryPath containing a library format that this PL...
Definition: plugin.cpp:218
virtual bool IsFootprintLibWritable(const wxString &aLibraryPath)
Return true if the library at aLibraryPath is writable.
Definition: plugin.cpp:256
virtual bool CanReadFootprint(const wxString &aFileName) const
Checks if this PLUGIN can read a footprint from specified file or directory.
Definition: plugin.cpp:77
virtual bool CanReadBoard(const wxString &aFileName) const
Checks if this PLUGIN can read the specified board file.
Definition: plugin.cpp:61
virtual PLUGIN_FILE_DESC GetFootprintLibDesc() const
Returns footprint library description for the PLUGIN.
Definition: plugin.cpp:55
virtual void FootprintLibOptions(STRING_UTF8_MAP *aListToAppendTo) const
Append supported PLUGIN options to aListToAppenTo along with internationalized descriptions.
Definition: plugin.cpp:263
virtual void PrefetchLib(const wxString &aLibraryPath, const STRING_UTF8_MAP *aProperties=nullptr)
If possible, prefetches the specified library (e.g.
Definition: plugin.cpp:174
virtual FOOTPRINT * ImportFootprint(const wxString &aFootprintPath, wxString &aFootprintNameOut, const STRING_UTF8_MAP *aProperties=nullptr)
Load a single footprint from aFootprintPath and put its name in aFootprintNameOut.
Definition: plugin.cpp:179
virtual BOARD * LoadBoard(const wxString &aFileName, BOARD *aAppendToMe, const STRING_UTF8_MAP *aProperties=nullptr, PROJECT *aProject=nullptr, PROGRESS_REPORTER *aProgressReporter=nullptr)
Load information from some input file format that this PLUGIN implementation knows about into either ...
Definition: plugin.cpp:144
virtual void FootprintEnumerate(wxArrayString &aFootprintNames, const wxString &aLibraryPath, bool aBestEfforts, const STRING_UTF8_MAP *aProperties=nullptr)
Return a list of footprint names contained within the library at aLibraryPath.
Definition: plugin.cpp:166
A progress reporter interface for use in multi-threaded environments.
Container for project specific data.
Definition: project.h:62
A name/value tuple with unique names and optional values.
An 8 bit string that is assuredly encoded in UTF8, and supplies special conversion support to and fro...
Definition: utf8.h:71
#define _(s)
#define NOT_IMPLEMENTED(aCaller)
Definition: sch_plugin.cpp:33
Container that describes file type info.
std::vector< std::string > m_ExtensionsInDir
In case of folders: extensions of files inside.
bool m_IsFile
Whether the library is a folder or a file.
std::vector< std::string > m_FileExtensions
Filter used for file pickers if m_IsFile is true.