KiCad PCB EDA Suite
Loading...
Searching...
No Matches
design_block_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) 2024 Mike Williams <[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, you may find one here:
19 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20 * or you may search the http://www.gnu.org website for the version 2 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
25#include <common.h>
26#include <i18n_utility.h>
27#include <wx/dir.h>
28#include <wx/ffile.h>
29#include <wx/filename.h>
30#include <wx/log.h>
31#include <wx/translation.h>
32#include <wx/string.h>
33#include <wx/arrstr.h>
34#include <wx/datetime.h>
36#include <kiway_player.h>
37#include <design_block_io.h>
38#include <design_block.h>
39#include <ki_exception.h>
40#include <trace_helpers.h>
41#include <fstream>
42
44{
45 switch( aFileType )
46 {
47 case KICAD_SEXP: return _( "KiCad" );
48 default: return wxString::Format( _( "UNKNOWN (%d)" ), aFileType );
49 }
50}
51
52
54DESIGN_BLOCK_IO_MGR::EnumFromStr( const wxString& aFileType )
55{
56 if( aFileType == _( "KiCad" ) )
58
60}
61
62
64{
65 switch( aFileType )
66 {
67 case KICAD_SEXP: return new DESIGN_BLOCK_IO();
68 default: return nullptr;
69 }
70}
71
72
74DESIGN_BLOCK_IO_MGR::GuessPluginTypeFromLibPath( const wxString& aLibPath, int aCtl )
75{
76 if( IO_RELEASER<DESIGN_BLOCK_IO>( FindPlugin( KICAD_SEXP ) )->CanReadLibrary( aLibPath )
77 && aCtl != KICTL_NONKICAD_ONLY )
78 {
79 return KICAD_SEXP;
80 }
81
83}
84
85
86bool DESIGN_BLOCK_IO_MGR::ConvertLibrary( std::map<std::string, UTF8>* aOldFileProps,
87 const wxString& aOldFilePath,
88 const wxString& aNewFilePath )
89{
92
93 if( oldFileType == DESIGN_BLOCK_IO_MGR::FILE_TYPE_NONE )
94 return false;
95
96
100 wxArrayString dbNames;
101 wxFileName newFileName( aNewFilePath );
102
103 if( newFileName.HasExt() )
104 {
105 wxString extraDir = newFileName.GetFullName();
106 newFileName.ClearExt();
107 newFileName.SetName( "" );
108 newFileName.AppendDir( extraDir );
109 }
110
111 if( !newFileName.DirExists() && !wxFileName::Mkdir( aNewFilePath, wxS_DIR_DEFAULT ) )
112 return false;
113
114 try
115 {
116 bool bestEfforts = false; // throw on first error
117 oldFilePI->DesignBlockEnumerate( dbNames, aOldFilePath, bestEfforts, aOldFileProps );
118
119 for( const wxString& dbName : dbNames )
120 {
121 std::unique_ptr<const DESIGN_BLOCK> db(
122 oldFilePI->GetEnumeratedDesignBlock( aOldFilePath, dbName, aOldFileProps ) );
123 kicadPI->DesignBlockSave( aNewFilePath, db.get() );
124 }
125 }
126 catch( ... )
127 {
128 return false;
129 }
130
131 return true;
132}
133
134
136{
137 return IO_BASE::IO_FILE_DESC( _HKI( "KiCad Design Block folders" ), {},
139}
140
141
142long long DESIGN_BLOCK_IO::GetLibraryTimestamp( const wxString& aLibraryPath ) const
143{
144 wxDir libDir( aLibraryPath );
145
146 if( !libDir.IsOpened() )
147 return 0;
148
149 long long ts = 0;
150
151 wxString filename;
152 bool hasMoreFiles = libDir.GetFirst( &filename, wxEmptyString, wxDIR_DIRS );
153
154 while( hasMoreFiles )
155 {
156 wxFileName blockDir( aLibraryPath, filename );
157
158 // Check if the directory ends with ".kicad_block", if so hash all the files in it.
159 if( blockDir.GetFullName().EndsWith( FILEEXT::KiCadDesignBlockPathExtension ) )
160 ts += TimestampDir( blockDir.GetFullPath(), wxT( "*" ) );
161
162 hasMoreFiles = libDir.GetNext( &filename );
163 }
164
165 return ts;
166}
167
168
169void DESIGN_BLOCK_IO::CreateLibrary( const wxString& aLibraryPath,
170 const std::map<std::string, UTF8>* aProperties )
171{
172 if( wxDir::Exists( aLibraryPath ) )
173 {
174 THROW_IO_ERROR( wxString::Format( _( "Cannot overwrite library path '%s'." ),
175 aLibraryPath.GetData() ) );
176 }
177
178 wxFileName dir;
179 dir.SetPath( aLibraryPath );
180
181 if( !dir.Mkdir() )
182 {
184 wxString::Format( _( "Library path '%s' could not be created.\n\n"
185 "Make sure you have write permissions and try again." ),
186 dir.GetPath() ) );
187 }
188}
189
190
191bool DESIGN_BLOCK_IO::DeleteLibrary( const wxString& aLibraryPath,
192 const std::map<std::string, UTF8>* aProperties )
193{
194 wxFileName fn;
195 fn.SetPath( aLibraryPath );
196
197 // Return if there is no library path to delete.
198 if( !fn.DirExists() )
199 return false;
200
201 if( !fn.IsDirWritable() )
202 {
203 THROW_IO_ERROR( wxString::Format( _( "Insufficient permissions to delete folder '%s'." ),
204 aLibraryPath.GetData() ) );
205 }
206
207 wxDir dir( aLibraryPath );
208
209 // Design block folders should only contain sub-folders for each design block
210 if( dir.HasFiles() )
211 {
212 THROW_IO_ERROR( wxString::Format( _( "Library folder '%s' has unexpected files." ),
213 aLibraryPath.GetData() ) );
214 }
215
216 // Must delete all sub-directories before deleting the library directory
217 if( dir.HasSubDirs() )
218 {
219 wxArrayString dirs;
220
221 // Get all sub-directories in the library path
222 dir.GetAllFiles( aLibraryPath, &dirs, wxEmptyString, wxDIR_DIRS );
223
224 for( size_t i = 0; i < dirs.GetCount(); i++ )
225 {
226 wxFileName tmp = dirs[i];
227
229 {
230 THROW_IO_ERROR( wxString::Format( _( "Unexpected folder '%s' found in library "
231 "path '%s'." ),
232 dirs[i].GetData(), aLibraryPath.GetData() ) );
233 }
234 }
235
236 for( size_t i = 0; i < dirs.GetCount(); i++ )
237 wxRemoveFile( dirs[i] );
238 }
239
240 wxLogTrace( traceDesignBlocks, wxT( "Removing design block library '%s'." ),
241 aLibraryPath.GetData() );
242
243 // Some of the more elaborate wxRemoveFile() crap puts up its own wxLog dialog
244 // we don't want that. we want bare metal portability with no UI here.
245 if( !wxFileName::Rmdir( aLibraryPath, wxPATH_RMDIR_RECURSIVE ) )
246 {
247 THROW_IO_ERROR( wxString::Format( _( "Design block library '%s' cannot be deleted." ),
248 aLibraryPath.GetData() ) );
249 }
250
251 // For some reason removing a directory in Windows is not immediately updated. This delay
252 // prevents an error when attempting to immediately recreate the same directory when over
253 // writing an existing library.
254#ifdef __WINDOWS__
255 wxMilliSleep( 250L );
256#endif
257
258 return true;
259}
260
261
262void DESIGN_BLOCK_IO::DesignBlockEnumerate( wxArrayString& aDesignBlockNames,
263 const wxString& aLibraryPath, bool aBestEfforts,
264 const std::map<std::string, UTF8>* aProperties )
265{
266 // From the starting directory, look for all directories ending in the .kicad_block extension
267 wxDir dir( aLibraryPath );
268
269 if( !dir.IsOpened() )
270 return;
271
272 wxString dirname;
273 wxString fileSpec = wxT( "*." ) + wxString( FILEEXT::KiCadDesignBlockPathExtension );
274 bool cont = dir.GetFirst( &dirname, fileSpec, wxDIR_DIRS );
275
276 while( cont )
277 {
278 aDesignBlockNames.Add( dirname.Before( wxT( '.' ) ) );
279 cont = dir.GetNext( &dirname );
280 }
281}
282
283
284DESIGN_BLOCK* DESIGN_BLOCK_IO::DesignBlockLoad( const wxString& aLibraryPath,
285 const wxString& aDesignBlockName, bool aKeepUUID,
286 const std::map<std::string, UTF8>* aProperties )
287{
288 wxString dbPath = aLibraryPath + wxFileName::GetPathSeparator() + aDesignBlockName + wxT( "." )
289 + FILEEXT::KiCadDesignBlockPathExtension + wxFileName::GetPathSeparator();
290 wxString dbSchPath = dbPath + aDesignBlockName + wxT( "." )
292 wxString dbMetadataPath = dbPath + aDesignBlockName + wxT( "." ) + FILEEXT::JsonFileExtension;
293
294 if( !wxFileExists( dbSchPath ) )
295 return nullptr;
296
297 DESIGN_BLOCK* newDB = new DESIGN_BLOCK();
298
299 // Library name needs to be empty for when we fill it in with the correct library nickname
300 // one layer above
301 newDB->SetLibId( LIB_ID( wxEmptyString, aDesignBlockName ) );
302 newDB->SetSchematicFile( dbSchPath );
303
304 // Parse the JSON file if it exists
305 if( wxFileExists( dbMetadataPath ) )
306 {
307 try
308 {
309 nlohmann::ordered_json dbMetadata;
310 std::ifstream dbMetadataFile( dbMetadataPath.fn_str() );
311
312 dbMetadataFile >> dbMetadata;
313
314 if( dbMetadata.contains( "description" ) )
315 newDB->SetLibDescription( dbMetadata["description"] );
316
317 if( dbMetadata.contains( "keywords" ) )
318 newDB->SetKeywords( dbMetadata["keywords"] );
319
320 nlohmann::ordered_map<wxString, wxString> fields;
321
322 // Read the "fields" object from the JSON
323 if( dbMetadata.contains( "fields" ) )
324 {
325 for( auto& item : dbMetadata["fields"].items() )
326 {
327 wxString name = wxString::FromUTF8( item.key() );
328 wxString value = wxString::FromUTF8( item.value().get<std::string>() );
329
330 fields[name] = value;
331 }
332
333 newDB->SetFields( fields );
334 }
335 }
336 catch( ... )
337 {
338 THROW_IO_ERROR( wxString::Format(
339 _( "Design block metadata file '%s' could not be read." ), dbMetadataPath ) );
340 }
341 }
342
343
344 return newDB;
345}
346
347
348void DESIGN_BLOCK_IO::DesignBlockSave( const wxString& aLibraryPath,
349 const DESIGN_BLOCK* aDesignBlock,
350 const std::map<std::string, UTF8>* aProperties )
351{
352 // Make sure we have a valid LIB_ID or we can't save the design block
353 if( !aDesignBlock->GetLibId().IsValid() )
354 {
355 THROW_IO_ERROR( _( "Design block does not have a valid library ID." ) );
356 }
357
358 wxFileName schematicFile( aDesignBlock->GetSchematicFile() );
359
360 if( !schematicFile.FileExists() )
361 {
362 THROW_IO_ERROR( wxString::Format( _( "Schematic source file '%s' does not exist." ),
363 schematicFile.GetFullPath() ) );
364 }
365
366 // Create the design block folder
367 wxFileName dbFolder( aLibraryPath + wxFileName::GetPathSeparator()
368 + aDesignBlock->GetLibId().GetLibItemName() + wxT( "." )
370 + wxFileName::GetPathSeparator() );
371
372 if( !dbFolder.DirExists() )
373 {
374 if( !dbFolder.Mkdir() )
375 {
376 THROW_IO_ERROR( wxString::Format( _( "Design block folder '%s' could not be created." ),
377 dbFolder.GetFullPath().GetData() ) );
378 }
379 }
380
381 // The new schematic file name is based on the design block name, not the source sheet name
382 wxString dbSchematicFile = dbFolder.GetFullPath() + aDesignBlock->GetLibId().GetLibItemName()
384
385 // If the source and destination files are the same, then we don't need to copy the file
386 // as we are just updating the metadata
387 if( schematicFile.GetFullPath() != dbSchematicFile )
388 {
389 // Copy the source sheet file to the design block folder, under the design block name
390 if( !wxCopyFile( schematicFile.GetFullPath(), dbSchematicFile ) )
391 {
392 THROW_IO_ERROR( wxString::Format(
393 _( "Schematic file '%s' could not be saved as design block at '%s'." ),
394 schematicFile.GetFullPath(), dbSchematicFile ) );
395 }
396 }
397
398
399 wxString dbMetadataFile = dbFolder.GetFullPath() + aDesignBlock->GetLibId().GetLibItemName()
400 + wxT( "." ) + FILEEXT::JsonFileExtension;
401
402 // Write the metadata file
403 nlohmann::ordered_json dbMetadata;
404 dbMetadata["description"] = aDesignBlock->GetLibDescription();
405 dbMetadata["keywords"] = aDesignBlock->GetKeywords();
406 dbMetadata["fields"] = aDesignBlock->GetFields();
407
408 bool success = false;
409
410 try
411 {
412 wxFFile mdFile( dbMetadataFile, wxT( "wb" ) );
413
414 if( mdFile.IsOpened() )
415 success = mdFile.Write( dbMetadata.dump( 0 ) );
416
417 // wxFFile dtor will close the file
418 }
419 catch( ... )
420 {
421 success = false;
422 }
423
424 if( !success )
425 {
426 THROW_IO_ERROR( wxString::Format(
427 _( "Design block metadata file '%s' could not be saved." ), dbMetadataFile ) );
428 }
429}
430
431
432void DESIGN_BLOCK_IO::DesignBlockDelete( const wxString& aLibPath, const wxString& aDesignBlockName,
433 const std::map<std::string, UTF8>* aProperties )
434{
435 wxFileName dbDir = wxFileName( aLibPath + wxFileName::GetPathSeparator() + aDesignBlockName
437
438 if( !dbDir.DirExists() )
439 {
441 wxString::Format( _( "Design block '%s' does not exist." ), dbDir.GetFullName() ) );
442 }
443
444 // Delete the whole design block folder
445 if( !wxFileName::Rmdir( dbDir.GetFullPath(), wxPATH_RMDIR_RECURSIVE ) )
446 {
447 THROW_IO_ERROR( wxString::Format( _( "Design block folder '%s' could not be deleted." ),
448 dbDir.GetFullPath().GetData() ) );
449 }
450}
451
452
453bool DESIGN_BLOCK_IO::IsLibraryWritable( const wxString& aLibraryPath )
454{
455 wxFileName path( aLibraryPath );
456 return path.IsOk() && path.IsDirWritable();
457}
const char * name
Definition: DXF_plotter.cpp:59
@ KICAD_SEXP
S-expression KiCad file format.
@ DESIGN_BLOCK_FILE_UNKNOWN
0 is not a legal menu id on Mac
static const wxString ShowType(DESIGN_BLOCK_FILE_T aFileType)
static DESIGN_BLOCK_FILE_T GuessPluginTypeFromLibPath(const wxString &aLibPath, int aCtl=0)
static DESIGN_BLOCK_FILE_T EnumFromStr(const wxString &aFileType)
static bool ConvertLibrary(std::map< std::string, UTF8 > *aOldFileProps, const wxString &aOldFilePath, const wxString &aNewFilePath)
Convert a design block library to the latest KiCad format.
static DESIGN_BLOCK_IO * FindPlugin(DESIGN_BLOCK_FILE_T aFileType)
long long GetLibraryTimestamp(const wxString &aLibraryPath) const
void DesignBlockDelete(const wxString &aLibraryPath, const wxString &aDesignBlockName, const std::map< std::string, UTF8 > *aProperties=nullptr)
DESIGN_BLOCK * DesignBlockLoad(const wxString &aLibraryPath, const wxString &aDesignBlockName, bool aKeepUUID=false, const std::map< std::string, UTF8 > *aProperties=nullptr)
void DesignBlockSave(const wxString &aLibraryPath, const DESIGN_BLOCK *aDesignBlock, const std::map< std::string, UTF8 > *aProperties=nullptr)
void DesignBlockEnumerate(wxArrayString &aDesignBlockNames, const wxString &aLibraryPath, bool aBestEfforts, const std::map< std::string, UTF8 > *aProperties=nullptr)
virtual bool DeleteLibrary(const wxString &aLibraryPath, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Delete an existing library and returns true, or if library does not exist returns false,...
void CreateLibrary(const wxString &aLibraryPath, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Create a new empty library at aLibraryPath empty.
bool IsLibraryWritable(const wxString &aLibraryPath) override
Return true if the library at aLibraryPath is writable.
const IO_BASE::IO_FILE_DESC GetLibraryDesc() const override
Get the descriptor for the library container that this IO plugin operates on.
void SetLibDescription(const wxString &aDesc)
Definition: design_block.h:36
void SetKeywords(const wxString &aKeywords)
Definition: design_block.h:39
void SetSchematicFile(const wxString &aFile)
Definition: design_block.h:42
const wxString & GetKeywords() const
Definition: design_block.h:38
const wxString & GetLibDescription() const
Definition: design_block.h:35
void SetLibId(const LIB_ID &aName)
Definition: design_block.h:32
const wxString & GetSchematicFile() const
Definition: design_block.h:41
void SetFields(nlohmann::ordered_map< wxString, wxString > &aFields)
Definition: design_block.h:44
const LIB_ID & GetLibId() const
Definition: design_block.h:33
const nlohmann::ordered_map< wxString, wxString > & GetFields() const
Definition: design_block.h:49
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:49
bool IsValid() const
Check if this LID_ID is valid.
Definition: lib_id.h:172
const UTF8 & GetLibItemName() const
Definition: lib_id.h:102
long long TimestampDir(const wxString &aDirPath, const wxString &aFilespec)
A copy of ConvertFileTimeToWx() because wxWidgets left it as a static function private to src/common/...
Definition: common.cpp:600
The common library.
#define _HKI(x)
#define _(s)
static const std::string KiCadDesignBlockLibPathExtension
static const std::string KiCadDesignBlockPathExtension
static const std::string JsonFileExtension
static const std::string KiCadSchematicFileExtension
const wxChar *const traceDesignBlocks
Some functions to handle hotkeys in KiCad.
std::unique_ptr< T > IO_RELEASER
Helper to hold and release an IO_BASE object when exceptions are thrown.
Definition: io_mgr.h:33
#define THROW_IO_ERROR(msg)
Definition: ki_exception.h:39
#define KICTL_NONKICAD_ONLY
chosen file is non-KiCad according to user
Definition: kiway_player.h:74
Container that describes file type info.
Definition: io_base.h:43
wxLogTrace helper definitions.
Definition of file extensions used in Kicad.