28#include <wx/filename.h>
30#include <wx/translation.h>
33#include <wx/datetime.h>
52 default:
return wxString::Format( wxT(
"UNKNOWN (%d)" ), aFileType );
60 if( aFileType.CmpNoCase( wxT(
"KiCad" ) ) == 0 )
64 else if( aFileType ==
_(
"KiCad" ) )
80 default:
return nullptr;
90 if( parser.
Parse( aLibPath.ToStdString() ).has_value() )
104 const wxString& aOldFilePath,
105 const wxString& aNewFilePath )
121 if( !oldFilePI || !kicadPI )
124 wxArrayString dbNames;
125 wxFileName newFileName( aNewFilePath );
127 if( newFileName.HasExt() )
129 wxString extraDir = newFileName.GetFullName();
130 newFileName.ClearExt();
131 newFileName.SetName(
"" );
132 newFileName.AppendDir( extraDir );
135 if( !newFileName.DirExists() && !wxFileName::Mkdir( aNewFilePath, wxS_DIR_DEFAULT ) )
140 bool bestEfforts =
false;
141 oldFilePI->DesignBlockEnumerate( dbNames, aOldFilePath, bestEfforts, aOldFileProps );
143 for(
const wxString& dbName : dbNames )
145 std::unique_ptr<const DESIGN_BLOCK> db( oldFilePI->GetEnumeratedDesignBlock( aOldFilePath, dbName,
147 kicadPI->DesignBlockSave( aNewFilePath, db.get() );
168 wxDir libDir( aLibraryPath );
170 if( !libDir.IsOpened() )
176 bool hasMoreFiles = libDir.GetFirst( &filename, wxEmptyString, wxDIR_DIRS );
178 while( hasMoreFiles )
180 wxFileName blockDir( aLibraryPath, filename );
186 hasMoreFiles = libDir.GetNext( &filename );
194 const std::map<std::string, UTF8>* aProperties )
196 if( wxDir::Exists( aLibraryPath ) )
198 THROW_IO_ERROR( wxString::Format(
_(
"Cannot overwrite library path '%s'." ),
199 aLibraryPath.GetData() ) );
203 dir.SetPath( aLibraryPath );
208 wxString::Format(
_(
"Library path '%s' could not be created.\n\n"
209 "Make sure you have write permissions and try again." ),
216 const std::map<std::string, UTF8>* aProperties )
219 fn.SetPath( aLibraryPath );
222 if( !fn.DirExists() )
225 if( !fn.IsDirWritable() )
227 THROW_IO_ERROR( wxString::Format(
_(
"Insufficient permissions to delete folder '%s'." ),
228 aLibraryPath.GetData() ) );
231 wxDir dir( aLibraryPath );
236 THROW_IO_ERROR( wxString::Format(
_(
"Library folder '%s' has unexpected files." ),
237 aLibraryPath.GetData() ) );
241 if( dir.HasSubDirs() )
248 for(
size_t i = 0; i < dirs.GetCount(); i++ )
250 wxFileName tmp = dirs[i];
254 THROW_IO_ERROR( wxString::Format(
_(
"Unexpected folder '%s' found in library path '%s'." ),
255 dirs[i].GetData(), aLibraryPath.GetData() ) );
259 for(
size_t i = 0; i < dirs.GetCount(); i++ )
260 wxRemoveFile( dirs[i] );
264 aLibraryPath.GetData() );
268 if( !wxFileName::Rmdir( aLibraryPath, wxPATH_RMDIR_RECURSIVE ) )
270 THROW_IO_ERROR( wxString::Format(
_(
"Design block library '%s' cannot be deleted." ),
271 aLibraryPath.GetData() ) );
278 wxMilliSleep( 250L );
286 const wxString& aLibraryPath,
bool aBestEfforts,
287 const std::map<std::string, UTF8>* aProperties )
290 wxDir dir( aLibraryPath );
292 if( !dir.IsOpened() )
294 THROW_IO_ERROR( wxString::Format(
_(
"Design block '%s' does not exist." ), aLibraryPath ) );
299 bool cont = dir.GetFirst( &dirname, fileSpec, wxDIR_DIRS );
303 aDesignBlockNames.Add( dirname.Before( wxT(
'.' ) ) );
304 cont = dir.GetNext( &dirname );
310 const wxString& aDesignBlockName,
bool aKeepUUID,
311 const std::map<std::string, UTF8>* aProperties )
313 wxString dbPath = aLibraryPath + wxFileName::GetPathSeparator() + aDesignBlockName + wxT(
"." )
315 wxString dbSchPath = dbPath + aDesignBlockName + wxT(
"." )
320 if( !wxDir::Exists( dbPath ) )
321 THROW_IO_ERROR( wxString::Format(
_(
"Design block '%s' does not exist." ), dbPath ) );
329 if( wxFileExists( dbSchPath ) )
332 if( wxFileExists( dbPcbPath ) )
336 if( wxFileExists( dbMetadataPath ) )
340 nlohmann::ordered_json dbMetadata;
341 std::ifstream dbMetadataFile( dbMetadataPath.fn_str() );
343 dbMetadataFile >> dbMetadata;
345 if( dbMetadata.contains(
"description" ) )
348 if( dbMetadata.contains(
"keywords" ) )
349 newDB->
SetKeywords( dbMetadata[
"keywords"].get<std::string>() );
352 if( dbMetadata.contains(
"fields" ) )
354 for(
auto& item : dbMetadata[
"fields"].items() )
356 wxString
name = wxString::FromUTF8( item.key() );
357 wxString value = wxString::FromUTF8( item.value().get<std::string>() );
366 THROW_IO_ERROR( wxString::Format(
_(
"Design block metadata file '%s' could not be read." ),
376 const wxString& aDesignBlockName,
377 const std::map<std::string, UTF8>* aProperties )
379 wxString dbPath = aLibraryPath + wxFileName::GetPathSeparator() + aDesignBlockName + wxT(
"." )
382 return wxDir::Exists( dbPath );
388 const std::map<std::string, UTF8>* aProperties )
393 THROW_IO_ERROR(
_(
"Design block does not have a valid library ID." ) );
398 THROW_IO_ERROR(
_(
"Design block does not have a schematic or board file." ) );
404 if( !aDesignBlock->
GetSchematicFile().IsEmpty() && !schematicFile.FileExists() )
407 wxString::Format(
_(
"Schematic source file '%s' does not exist." ), schematicFile.GetFullPath() ) );
410 if( !aDesignBlock->
GetBoardFile().IsEmpty() && !boardFile.FileExists() )
412 THROW_IO_ERROR( wxString::Format(
_(
"Board source file '%s' does not exist." ), boardFile.GetFullPath() ) );
416 wxFileName dbFolder( aLibraryPath + wxFileName::GetPathSeparator()
419 + wxFileName::GetPathSeparator() );
421 if( !dbFolder.DirExists() )
423 if( !dbFolder.Mkdir() )
425 THROW_IO_ERROR( wxString::Format(
_(
"Design block folder '%s' could not be created." ),
426 dbFolder.GetFullPath().GetData() ) );
433 wxString dbSchematicFile = dbFolder.GetFullPath() + aDesignBlock->
GetLibId().
GetLibItemName() + wxT(
"." )
438 if( schematicFile.GetFullPath() != dbSchematicFile )
441 if( !wxCopyFile( schematicFile.GetFullPath(), dbSchematicFile ) )
444 wxString::Format(
_(
"Schematic file '%s' could not be saved as design block at '%s'." ),
445 schematicFile.GetFullPath(), dbSchematicFile ) );
458 if( boardFile.GetFullPath() != dbBoardFile )
461 if( !wxCopyFile( boardFile.GetFullPath(), dbBoardFile ) )
463 THROW_IO_ERROR( wxString::Format(
_(
"Board file '%s' could not be saved as design block at '%s'." ),
464 boardFile.GetFullPath(), dbBoardFile ) );
473 nlohmann::ordered_json dbMetadata;
475 dbMetadata[
"keywords"] = aDesignBlock->
GetKeywords();
476 dbMetadata[
"fields"] = aDesignBlock->
GetFields();
478 bool success =
false;
482 std::string payload = dbMetadata.dump( 0 );
489 wxLogError(
_(
"Cannot save design block metadata '%s': %s" ), dbMetadataFile,
501 _(
"Design block metadata file '%s' could not be saved." ), dbMetadataFile ) );
507 const std::map<std::string, UTF8>* aProperties )
509 wxFileName dbDir = wxFileName( aLibPath + wxFileName::GetPathSeparator() + aDesignBlockName
512 if( !dbDir.DirExists() )
515 wxString::Format(
_(
"Design block '%s' does not exist." ), dbDir.GetFullName() ) );
519 if( !wxFileName::Rmdir( dbDir.GetFullPath(), wxPATH_RMDIR_RECURSIVE ) )
521 THROW_IO_ERROR( wxString::Format(
_(
"Design block folder '%s' could not be deleted." ),
522 dbDir.GetFullPath().GetData() ) );
529 wxFileName
path( aLibraryPath );
530 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.