31#include <wx/filename.h>
33#include <wx/translation.h>
36#include <wx/datetime.h>
53 default:
return wxString::Format(
_(
"UNKNOWN (%d)" ), aFileType );
61 if( aFileType ==
_(
"KiCad" ) )
75 default:
return nullptr;
85 if( parser.
Parse( aLibPath.ToStdString() ).has_value() )
99 const wxString& aOldFilePath,
100 const wxString& aNewFilePath )
111 wxArrayString dbNames;
112 wxFileName newFileName( aNewFilePath );
114 if( newFileName.HasExt() )
116 wxString extraDir = newFileName.GetFullName();
117 newFileName.ClearExt();
118 newFileName.SetName(
"" );
119 newFileName.AppendDir( extraDir );
122 if( !newFileName.DirExists() && !wxFileName::Mkdir( aNewFilePath, wxS_DIR_DEFAULT ) )
127 bool bestEfforts =
false;
128 oldFilePI->DesignBlockEnumerate( dbNames, aOldFilePath, bestEfforts, aOldFileProps );
130 for(
const wxString& dbName : dbNames )
132 std::unique_ptr<const DESIGN_BLOCK> db( oldFilePI->GetEnumeratedDesignBlock( aOldFilePath, dbName,
134 kicadPI->DesignBlockSave( aNewFilePath, db.get() );
155 wxDir libDir( aLibraryPath );
157 if( !libDir.IsOpened() )
163 bool hasMoreFiles = libDir.GetFirst( &filename, wxEmptyString, wxDIR_DIRS );
165 while( hasMoreFiles )
167 wxFileName blockDir( aLibraryPath, filename );
173 hasMoreFiles = libDir.GetNext( &filename );
181 const std::map<std::string, UTF8>* aProperties )
183 if( wxDir::Exists( aLibraryPath ) )
185 THROW_IO_ERROR( wxString::Format(
_(
"Cannot overwrite library path '%s'." ),
186 aLibraryPath.GetData() ) );
190 dir.SetPath( aLibraryPath );
195 wxString::Format(
_(
"Library path '%s' could not be created.\n\n"
196 "Make sure you have write permissions and try again." ),
203 const std::map<std::string, UTF8>* aProperties )
206 fn.SetPath( aLibraryPath );
209 if( !fn.DirExists() )
212 if( !fn.IsDirWritable() )
214 THROW_IO_ERROR( wxString::Format(
_(
"Insufficient permissions to delete folder '%s'." ),
215 aLibraryPath.GetData() ) );
218 wxDir dir( aLibraryPath );
223 THROW_IO_ERROR( wxString::Format(
_(
"Library folder '%s' has unexpected files." ),
224 aLibraryPath.GetData() ) );
228 if( dir.HasSubDirs() )
233 dir.GetAllFiles( aLibraryPath, &dirs, wxEmptyString, wxDIR_DIRS );
235 for(
size_t i = 0; i < dirs.GetCount(); i++ )
237 wxFileName tmp = dirs[i];
241 THROW_IO_ERROR( wxString::Format(
_(
"Unexpected folder '%s' found in library path '%s'." ),
242 dirs[i].GetData(), aLibraryPath.GetData() ) );
246 for(
size_t i = 0; i < dirs.GetCount(); i++ )
247 wxRemoveFile( dirs[i] );
251 aLibraryPath.GetData() );
255 if( !wxFileName::Rmdir( aLibraryPath, wxPATH_RMDIR_RECURSIVE ) )
257 THROW_IO_ERROR( wxString::Format(
_(
"Design block library '%s' cannot be deleted." ),
258 aLibraryPath.GetData() ) );
265 wxMilliSleep( 250L );
273 const wxString& aLibraryPath,
bool aBestEfforts,
274 const std::map<std::string, UTF8>* aProperties )
277 wxDir dir( aLibraryPath );
279 if( !dir.IsOpened() )
281 THROW_IO_ERROR( wxString::Format(
_(
"Design block '%s' does not exist." ), aLibraryPath ) );
286 bool cont = dir.GetFirst( &dirname, fileSpec, wxDIR_DIRS );
290 aDesignBlockNames.Add( dirname.Before( wxT(
'.' ) ) );
291 cont = dir.GetNext( &dirname );
297 const wxString& aDesignBlockName,
bool aKeepUUID,
298 const std::map<std::string, UTF8>* aProperties )
300 wxString dbPath = aLibraryPath + wxFileName::GetPathSeparator() + aDesignBlockName + wxT(
"." )
302 wxString dbSchPath = dbPath + aDesignBlockName + wxT(
"." )
307 if( !wxDir::Exists( dbPath ) )
308 THROW_IO_ERROR( wxString::Format(
_(
"Design block '%s' does not exist." ), dbPath ) );
316 if( wxFileExists( dbSchPath ) )
319 if( wxFileExists( dbPcbPath ) )
323 if( wxFileExists( dbMetadataPath ) )
327 nlohmann::ordered_json dbMetadata;
328 std::ifstream dbMetadataFile( dbMetadataPath.fn_str() );
330 dbMetadataFile >> dbMetadata;
332 if( dbMetadata.contains(
"description" ) )
335 if( dbMetadata.contains(
"keywords" ) )
336 newDB->
SetKeywords( dbMetadata[
"keywords"].get<std::string>() );
339 if( dbMetadata.contains(
"fields" ) )
341 for(
auto& item : dbMetadata[
"fields"].items() )
343 wxString
name = wxString::FromUTF8( item.key() );
344 wxString value = wxString::FromUTF8( item.value().get<std::string>() );
353 THROW_IO_ERROR( wxString::Format(
_(
"Design block metadata file '%s' could not be read." ),
363 const wxString& aDesignBlockName,
364 const std::map<std::string, UTF8>* aProperties )
366 wxString dbPath = aLibraryPath + wxFileName::GetPathSeparator() + aDesignBlockName + wxT(
"." )
369 return wxDir::Exists( dbPath );
375 const std::map<std::string, UTF8>* aProperties )
380 THROW_IO_ERROR(
_(
"Design block does not have a valid library ID." ) );
385 THROW_IO_ERROR(
_(
"Design block does not have a schematic or board file." ) );
391 if( !aDesignBlock->
GetSchematicFile().IsEmpty() && !schematicFile.FileExists() )
394 wxString::Format(
_(
"Schematic source file '%s' does not exist." ), schematicFile.GetFullPath() ) );
397 if( !aDesignBlock->
GetBoardFile().IsEmpty() && !boardFile.FileExists() )
399 THROW_IO_ERROR( wxString::Format(
_(
"Board source file '%s' does not exist." ), boardFile.GetFullPath() ) );
403 wxFileName dbFolder( aLibraryPath + wxFileName::GetPathSeparator()
406 + wxFileName::GetPathSeparator() );
408 if( !dbFolder.DirExists() )
410 if( !dbFolder.Mkdir() )
412 THROW_IO_ERROR( wxString::Format(
_(
"Design block folder '%s' could not be created." ),
413 dbFolder.GetFullPath().GetData() ) );
420 wxString dbSchematicFile = dbFolder.GetFullPath() + aDesignBlock->
GetLibId().
GetLibItemName() + wxT(
"." )
425 if( schematicFile.GetFullPath() != dbSchematicFile )
428 if( !wxCopyFile( schematicFile.GetFullPath(), dbSchematicFile ) )
431 wxString::Format(
_(
"Schematic file '%s' could not be saved as design block at '%s'." ),
432 schematicFile.GetFullPath(), dbSchematicFile ) );
445 if( boardFile.GetFullPath() != dbBoardFile )
448 if( !wxCopyFile( boardFile.GetFullPath(), dbBoardFile ) )
450 THROW_IO_ERROR( wxString::Format(
_(
"Board file '%s' could not be saved as design block at '%s'." ),
451 boardFile.GetFullPath(), dbBoardFile ) );
460 nlohmann::ordered_json dbMetadata;
462 dbMetadata[
"keywords"] = aDesignBlock->
GetKeywords();
463 dbMetadata[
"fields"] = aDesignBlock->
GetFields();
465 bool success =
false;
469 wxFFile mdFile( dbMetadataFile, wxT(
"wb" ) );
471 if( mdFile.IsOpened() )
472 success = mdFile.Write( dbMetadata.dump( 0 ) );
484 _(
"Design block metadata file '%s' could not be saved." ), dbMetadataFile ) );
490 const std::map<std::string, UTF8>* aProperties )
492 wxFileName dbDir = wxFileName( aLibPath + wxFileName::GetPathSeparator() + aDesignBlockName
495 if( !dbDir.DirExists() )
498 wxString::Format(
_(
"Design block '%s' does not exist." ), dbDir.GetFullName() ) );
502 if( !wxFileName::Rmdir( dbDir.GetFullPath(), wxPATH_RMDIR_RECURSIVE ) )
504 THROW_IO_ERROR( wxString::Format(
_(
"Design block folder '%s' could not be deleted." ),
505 dbDir.GetFullPath().GetData() ) );
512 wxFileName
path( aLibraryPath );
513 return path.IsOk() &&
path.IsDirWritable();
@ 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)
bool DesignBlockExists(const wxString &aLibraryPath, const wxString &aDesignBlockName, const std::map< std::string, UTF8 > *aProperties=nullptr)
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)
void SetKeywords(const wxString &aKeywords)
void SetSchematicFile(const wxString &aFile)
void SetBoardFile(const wxString &aFile)
const wxString & GetKeywords() const
const wxString & GetLibDescription() const
void SetLibId(const LIB_ID &aName)
const wxString & GetBoardFile() const
const wxString & GetSchematicFile() const
const LIB_ID & GetLibId() const
const nlohmann::ordered_map< wxString, wxString > & GetFields() const
tl::expected< LIBRARY_TABLE_IR, LIBRARY_PARSE_ERROR > Parse(const std::filesystem::path &aPath)
static const wxString TABLE_TYPE_NAME
A logical library item identifier and consists of various portions much like a URI.
bool IsValid() const
Check if this LID_ID is valid.
const UTF8 & GetLibItemName() const
static const std::string KiCadDesignBlockLibPathExtension
static const std::string KiCadDesignBlockPathExtension
static const std::string JsonFileExtension
static const std::string KiCadSchematicFileExtension
static const std::string KiCadPcbFileExtension
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.
#define THROW_IO_ERROR(msg)
macro which captures the "call site" values of FILE_, __FUNCTION & LINE
#define KICTL_NONKICAD_ONLY
chosen file is non-KiCad according to user
Container that describes file type info.
wxLogTrace helper definitions.
Definition of file extensions used in Kicad.