32#include <wx/filename.h>
34#include <wx/translation.h>
37#include <wx/datetime.h>
54 default:
return wxString::Format(
_(
"UNKNOWN (%d)" ), aFileType );
62 if( aFileType ==
_(
"KiCad" ) )
76 default:
return nullptr;
86 if( parser.
Parse( aLibPath.ToStdString() ).has_value() )
100 const wxString& aOldFilePath,
101 const wxString& aNewFilePath )
112 wxArrayString dbNames;
113 wxFileName newFileName( aNewFilePath );
115 if( newFileName.HasExt() )
117 wxString extraDir = newFileName.GetFullName();
118 newFileName.ClearExt();
119 newFileName.SetName(
"" );
120 newFileName.AppendDir( extraDir );
123 if( !newFileName.DirExists() && !wxFileName::Mkdir( aNewFilePath, wxS_DIR_DEFAULT ) )
128 bool bestEfforts =
false;
129 oldFilePI->DesignBlockEnumerate( dbNames, aOldFilePath, bestEfforts, aOldFileProps );
131 for(
const wxString& dbName : dbNames )
133 std::unique_ptr<const DESIGN_BLOCK> db( oldFilePI->GetEnumeratedDesignBlock( aOldFilePath, dbName,
135 kicadPI->DesignBlockSave( aNewFilePath, db.get() );
156 wxDir libDir( aLibraryPath );
158 if( !libDir.IsOpened() )
164 bool hasMoreFiles = libDir.GetFirst( &filename, wxEmptyString, wxDIR_DIRS );
166 while( hasMoreFiles )
168 wxFileName blockDir( aLibraryPath, filename );
174 hasMoreFiles = libDir.GetNext( &filename );
182 const std::map<std::string, UTF8>* aProperties )
184 if( wxDir::Exists( aLibraryPath ) )
186 THROW_IO_ERROR( wxString::Format(
_(
"Cannot overwrite library path '%s'." ),
187 aLibraryPath.GetData() ) );
191 dir.SetPath( aLibraryPath );
196 wxString::Format(
_(
"Library path '%s' could not be created.\n\n"
197 "Make sure you have write permissions and try again." ),
204 const std::map<std::string, UTF8>* aProperties )
207 fn.SetPath( aLibraryPath );
210 if( !fn.DirExists() )
213 if( !fn.IsDirWritable() )
215 THROW_IO_ERROR( wxString::Format(
_(
"Insufficient permissions to delete folder '%s'." ),
216 aLibraryPath.GetData() ) );
219 wxDir dir( aLibraryPath );
224 THROW_IO_ERROR( wxString::Format(
_(
"Library folder '%s' has unexpected files." ),
225 aLibraryPath.GetData() ) );
229 if( dir.HasSubDirs() )
236 for(
size_t i = 0; i < dirs.GetCount(); i++ )
238 wxFileName tmp = dirs[i];
242 THROW_IO_ERROR( wxString::Format(
_(
"Unexpected folder '%s' found in library path '%s'." ),
243 dirs[i].GetData(), aLibraryPath.GetData() ) );
247 for(
size_t i = 0; i < dirs.GetCount(); i++ )
248 wxRemoveFile( dirs[i] );
252 aLibraryPath.GetData() );
256 if( !wxFileName::Rmdir( aLibraryPath, wxPATH_RMDIR_RECURSIVE ) )
258 THROW_IO_ERROR( wxString::Format(
_(
"Design block library '%s' cannot be deleted." ),
259 aLibraryPath.GetData() ) );
266 wxMilliSleep( 250L );
274 const wxString& aLibraryPath,
bool aBestEfforts,
275 const std::map<std::string, UTF8>* aProperties )
278 wxDir dir( aLibraryPath );
280 if( !dir.IsOpened() )
282 THROW_IO_ERROR( wxString::Format(
_(
"Design block '%s' does not exist." ), aLibraryPath ) );
287 bool cont = dir.GetFirst( &dirname, fileSpec, wxDIR_DIRS );
291 aDesignBlockNames.Add( dirname.Before( wxT(
'.' ) ) );
292 cont = dir.GetNext( &dirname );
298 const wxString& aDesignBlockName,
bool aKeepUUID,
299 const std::map<std::string, UTF8>* aProperties )
301 wxString dbPath = aLibraryPath + wxFileName::GetPathSeparator() + aDesignBlockName + wxT(
"." )
303 wxString dbSchPath = dbPath + aDesignBlockName + wxT(
"." )
308 if( !wxDir::Exists( dbPath ) )
309 THROW_IO_ERROR( wxString::Format(
_(
"Design block '%s' does not exist." ), dbPath ) );
317 if( wxFileExists( dbSchPath ) )
320 if( wxFileExists( dbPcbPath ) )
324 if( wxFileExists( dbMetadataPath ) )
328 nlohmann::ordered_json dbMetadata;
329 std::ifstream dbMetadataFile( dbMetadataPath.fn_str() );
331 dbMetadataFile >> dbMetadata;
333 if( dbMetadata.contains(
"description" ) )
336 if( dbMetadata.contains(
"keywords" ) )
337 newDB->
SetKeywords( dbMetadata[
"keywords"].get<std::string>() );
340 if( dbMetadata.contains(
"fields" ) )
342 for(
auto& item : dbMetadata[
"fields"].items() )
344 wxString
name = wxString::FromUTF8( item.key() );
345 wxString value = wxString::FromUTF8( item.value().get<std::string>() );
354 THROW_IO_ERROR( wxString::Format(
_(
"Design block metadata file '%s' could not be read." ),
364 const wxString& aDesignBlockName,
365 const std::map<std::string, UTF8>* aProperties )
367 wxString dbPath = aLibraryPath + wxFileName::GetPathSeparator() + aDesignBlockName + wxT(
"." )
370 return wxDir::Exists( dbPath );
376 const std::map<std::string, UTF8>* aProperties )
381 THROW_IO_ERROR(
_(
"Design block does not have a valid library ID." ) );
386 THROW_IO_ERROR(
_(
"Design block does not have a schematic or board file." ) );
392 if( !aDesignBlock->
GetSchematicFile().IsEmpty() && !schematicFile.FileExists() )
395 wxString::Format(
_(
"Schematic source file '%s' does not exist." ), schematicFile.GetFullPath() ) );
398 if( !aDesignBlock->
GetBoardFile().IsEmpty() && !boardFile.FileExists() )
400 THROW_IO_ERROR( wxString::Format(
_(
"Board source file '%s' does not exist." ), boardFile.GetFullPath() ) );
404 wxFileName dbFolder( aLibraryPath + wxFileName::GetPathSeparator()
407 + wxFileName::GetPathSeparator() );
409 if( !dbFolder.DirExists() )
411 if( !dbFolder.Mkdir() )
413 THROW_IO_ERROR( wxString::Format(
_(
"Design block folder '%s' could not be created." ),
414 dbFolder.GetFullPath().GetData() ) );
421 wxString dbSchematicFile = dbFolder.GetFullPath() + aDesignBlock->
GetLibId().
GetLibItemName() + wxT(
"." )
426 if( schematicFile.GetFullPath() != dbSchematicFile )
429 if( !wxCopyFile( schematicFile.GetFullPath(), dbSchematicFile ) )
432 wxString::Format(
_(
"Schematic file '%s' could not be saved as design block at '%s'." ),
433 schematicFile.GetFullPath(), dbSchematicFile ) );
446 if( boardFile.GetFullPath() != dbBoardFile )
449 if( !wxCopyFile( boardFile.GetFullPath(), dbBoardFile ) )
451 THROW_IO_ERROR( wxString::Format(
_(
"Board file '%s' could not be saved as design block at '%s'." ),
452 boardFile.GetFullPath(), dbBoardFile ) );
461 nlohmann::ordered_json dbMetadata;
463 dbMetadata[
"keywords"] = aDesignBlock->
GetKeywords();
464 dbMetadata[
"fields"] = aDesignBlock->
GetFields();
466 bool success =
false;
470 std::string payload = dbMetadata.dump( 0 );
477 wxLogError(
_(
"Cannot save design block metadata '%s': %s" ), dbMetadataFile,
489 _(
"Design block metadata file '%s' could not be saved." ), dbMetadataFile ) );
495 const std::map<std::string, UTF8>* aProperties )
497 wxFileName dbDir = wxFileName( aLibPath + wxFileName::GetPathSeparator() + aDesignBlockName
500 if( !dbDir.DirExists() )
503 wxString::Format(
_(
"Design block '%s' does not exist." ), dbDir.GetFullName() ) );
507 if( !wxFileName::Rmdir( dbDir.GetFullPath(), wxPATH_RMDIR_RECURSIVE ) )
509 THROW_IO_ERROR( wxString::Format(
_(
"Design block folder '%s' could not be deleted." ),
510 dbDir.GetFullPath().GetData() ) );
517 wxFileName
path( aLibraryPath );
518 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
void CollectSubdirsLoopSafe(const wxString &aRoot, wxArrayString &aDirs, int aFlags)
Recursively collect every subdirectory under aRoot using the same loop detection as CollectFilesLoopS...
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.