43#include <wx/filename.h>
51 wxFileName fileName( aFileName );
53 if( fileName.IsFileReadable() && fileName.GetModificationTime().IsValid() )
54 return fileName.GetModificationTime().GetValue().GetValue();
62 const wxString& aSymbolName,
63 const std::map<wxString, EASYEDAPRO::BLOB>& aBlobs,
71 static const std::map<wxString, wxString> emptyAttributes;
89 wxString fpTitle = *fpUuid;
92 if(
index.contains(
"footprints" ) &&
index.at(
"footprints" ).is_object() )
94 const nlohmann::json& footprints =
index.at(
"footprints" );
95 const std::string key( fpUuid->ToUTF8() );
97 if( footprints.contains( key ) )
108 if( !keywords.empty() )
117 SCH_IO( wxS(
"EasyEDA Pro (JLCEDA) Schematic v3" ) )
158 std::vector<LIB_SYMBOL*>
result;
170 const std::map<std::string, UTF8>* aProperties )
178 aSymbolNameList.Add(
name );
184 const std::map<std::string, UTF8>* aProperties )
196 for(
const auto& [symbolName, item] : items )
201 aSymbolList.push_back( symbol );
203 catch( nlohmann::json::exception& e )
206 wxString::Format(
_(
"EasyEDA Pro v3 symbol '%s' in '%s' was skipped due to parse error: %s" ),
214 const std::map<std::string, UTF8>* aProperties )
221 auto itemIt = items.find( aAliasName );
223 if( itemIt == items.end() )
235 catch( nlohmann::json::exception& e )
237 THROW_IO_ERRORF(
_(
"Cannot load symbol '%s' from '%s': %s" ), aAliasName, aLibraryPath, e.what() );
244 const std::map<std::string, UTF8>* aProperties )
246 wxCHECK( !aFileName.IsEmpty() && aSchematic,
nullptr );
254 wxCHECK_MSG( aSchematic->
IsValid(),
nullptr, wxS(
"Can't append to a schematic with no root!" ) );
256 rootSheet = aAppendToMe;
280 std::map<wxString, EASYEDAPRO::PRJ_SCHEMATIC> prjSchematics =
project.at(
"schematics" );
282 wxString schematicToLoad;
284 if( aProperties && aProperties->contains(
"sch_id" ) )
286 schematicToLoad = wxString::FromUTF8( aProperties->at(
"sch_id" ) );
288 else if( prjSchematics.size() == 1 )
290 schematicToLoad = prjSchematics.begin()->first;
294 std::vector<IMPORT_PROJECT_DESC> chosen =
297 if( !chosen.empty() )
298 schematicToLoad = chosen[0].SchematicId;
300 else if( !prjSchematics.empty() )
302 schematicToLoad = prjSchematics.begin()->first;
305 if( schematicToLoad.empty() )
308 auto prjSchIt = prjSchematics.find( schematicToLoad );
310 if( prjSchIt == prjSchematics.end() )
311 THROW_IO_ERRORF(
_(
"Schematic document '%s' not found in '%s'" ), schematicToLoad, aFileName );
313 wxFileName sourceName( aFileName );
318 wxFileName rootFname( aFileName );
321 rootSheet->
SetName( prjSchIt->second.name );
327 std::map<wxString, EASYEDAPRO::PRJ_SYMBOL> prjSymbols;
328 std::map<wxString, EASYEDAPRO::PRJ_FOOTPRINT> prjFootprints;
329 std::map<wxString, EASYEDAPRO::PRJ_DEVICE> prjDevices;
333 if(
project.contains(
"symbols" ) )
334 prjSymbols =
project.at(
"symbols" );
336 if(
project.contains(
"footprints" ) )
337 prjFootprints =
project.at(
"footprints" );
339 if(
project.contains(
"devices" ) )
340 prjDevices =
project.at(
"devices" );
342 catch( nlohmann::json::exception& e )
344 THROW_IO_ERRORF(
_(
"Failed to parse EasyEDA Pro v3 symbol/device metadata: %s" ), e.what() );
349 std::set<wxString> usedLibNames;
350 std::map<wxString, wxString> deviceLibNames =
353 std::map<wxString, EASYEDAPRO::SYM_INFO> symbols;
356 for(
const auto& [symbolUuid, rawDoc] :
v3.GetRawDocs( wxS(
"SYMBOL" ) ) )
358 auto symMetaIt = prjSymbols.find( symbolUuid );
360 if( symMetaIt == prjSymbols.end() )
370 catch( nlohmann::json::exception& e )
372 Report( wxString::Format(
_(
"EasyEDA Pro v3 symbol '%s' was skipped due to parse error: %s" ),
388 if( !keywords.empty() )
391 symbols.emplace( symbolUuid, std::move( symInfo ) );
394 const std::vector<EASYEDAPRO::PRJ_SHEET>& prjSchematicSheets = prjSchIt->second.sheets;
396 bool rootAssigned =
false;
397 int subSheetIndex = 0;
405 Report( wxString::Format(
_(
"EasyEDA Pro v3 schematic page '%s' was not found and was skipped." ),
410 wxString sheetId = wxString::Format(
"%d", prjSheet.id );
425 catch( nlohmann::json::exception& e )
428 wxString::Format(
_(
"EasyEDA Pro v3 schematic page '%s' was skipped due to parse error: %s" ),
433 rootSheet->
SetName( prjSheet.name );
441 wxFileName sheetFname( aFileName );
442 sheetFname.SetFullName( sheetBaseName + wxS(
"." )
445 wxFileName relSheetPath( sheetFname );
446 relSheetPath.MakeRelativeTo( rootFname.GetPath() );
448 std::unique_ptr<SCH_SHEET> subSheet = std::make_unique<SCH_SHEET>( aSchematic );
449 subSheet->SetFileName( relSheetPath.GetFullPath() );
450 subSheet->SetName( prjSheet.name );
455 subSheet->SetScreen( screen );
459 pos.
y =
schIUScale.MilsToIU( 200 ) + ( subSheet->GetSize().y +
schIUScale.MilsToIU( 200 ) ) * subSheetIndex;
461 subSheet->SetPosition( pos );
473 catch( nlohmann::json::exception& e )
476 wxString::Format(
_(
"EasyEDA Pro v3 schematic page '%s' was skipped due to parse error: %s" ),
488 std::set<wxString> symbolsFromDevices;
491 for(
const auto& [devUuid, device] : prjDevices )
493 auto symbolAttr =
get_opt( device.attributes,
"Symbol" );
507 deviceSymInfo = parser.
ParseSymbol( *symbolDoc, device.attributes, blobs );
509 catch( nlohmann::json::exception& e )
511 Report( wxString::Format(
_(
"EasyEDA Pro v3 device '%s' was skipped due to parse error: %s" ),
519 auto nameIt = deviceLibNames.find( devUuid );
520 wxString itemName = ( nameIt != deviceLibNames.end() ) ? nameIt->second : device.title;
526 if(
auto fpUuid =
get_opt( device.attributes,
"Footprint" ) )
528 if(
auto prjFp =
get_opt( prjFootprints, *fpUuid ) )
532 wxString description = device.description;
534 if( description.empty() )
535 description =
get_def( device.attributes, wxS(
"Description" ), wxEmptyString );
537 if( !description.empty() )
545 if( !keywords.empty() )
549 symbolsFromDevices.insert( *symbolAttr );
553 for(
auto& [symbolUuid, symInfo] : symbols )
555 if( !symInfo.libSymbol || symbolsFromDevices.contains( symbolUuid ) )
561 symInfo.libSymbol->SetLibId( libID );
562 symInfo.libSymbol->SetName( itemName );
570 if(
v3.GetSkippedCount() > 0 )
572 Report( wxString::Format(
_(
"EasyEDA (JLCEDA) Pro v3 import skipped %d unsupported object(s)." ),
constexpr EDA_IU_SCALE schIUScale
void SetPageNumber(const wxString &aPageNumber)
Parses EasyEDA Pro v3 .epro2 archives.
const V3_DOC_RAW * FindRawDoc(const wxString &aDocType, const wxString &aUuid) const
static bool IsV3Library(const wxString &aFileName, const wxString &aRequiredDocType=wxEmptyString)
Return true if aFileName looks like an EasyEDA Pro v3 library archive.
const nlohmann::json & GetLibraryIndex() const
static bool IsV3Archive(const wxString &aFileName)
Return true if aFileName looks like an EasyEDA Pro v3 archive.
RAII class to set and restore the fontconfig reporter.
const wxString & GetName() const
Return a brief hard coded name for this IO interface.
virtual void Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) const
virtual bool CanReadLibrary(const wxString &aFileName) const
Checks if this IO object can read the specified library file/directory.
A logical library item identifier and consists of various portions much like a URI.
static UTF8 FixIllegalChars(const UTF8 &aLibItemName, bool aLib)
Replace illegal LIB_ID item name characters with underscores '_'.
Define a library symbol object.
SCH_FIELD & GetFootprintField()
Return reference to the footprint field.
void SetDescription(const wxString &aDescription)
Gets the Description field text value */.
void SetKeyWords(const wxString &aKeyWords)
void SetLibId(const LIB_ID &aLibId)
virtual void SetName(const wxString &aName)
static LOAD_INFO_REPORTER & GetInstance()
CHOOSE_PROJECT_HANDLER m_choose_project_handler
Callback to choose projects to import.
Holds all the data relating to one schematic.
void SetCurrentSheet(const SCH_SHEET_PATH &aPath)
int FixupJunctionsAfterImport()
Add junctions to this schematic where required.
bool IsValid() const
A simple test if the schematic is loaded, not a complete one.
void SetTopLevelSheets(const std::vector< SCH_SHEET * > &aSheets)
SCH_SHEET_PATH & CurrentSheet() const
void ParseSchematic(SCHEMATIC *aSchematic, SCH_SHEET *aRootSheet, const nlohmann::json &aProject, std::map< wxString, EASYEDAPRO::SYM_INFO > &aSymbolMap, const std::map< wxString, EASYEDAPRO::BLOB > &aBlobMap, const EASYEDAPRO::V3_DOC_RAW &aDoc, const wxString &aLibName)
EASYEDAPRO::SYM_INFO ParseSymbol(const EASYEDAPRO::V3_DOC_RAW &aDoc, const std::map< wxString, wxString > &aDeviceAttributes, const std::map< wxString, EASYEDAPRO::BLOB > &aBlobMap={})
void SetText(const wxString &aText) override
bool CanReadLibrary(const wxString &aFileName) const override
Checks if this IO object can read the specified library file/directory.
long long m_cachedLibraryTimestamp
std::vector< LIB_SYMBOL * > GetImportedCachedLibrarySymbols() override
Return the canonical symbol definitions produced by the last LoadSchematicFile().
std::unique_ptr< EASYEDAPRO::V3_DOC_PARSER > m_cachedLibraryParser
SCH_SHEET * LoadSchematicFile(const wxString &aFileName, SCHEMATIC *aSchematic, SCH_SHEET *aAppendToMe=nullptr, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Load information from some input file format that this SCH_IO implementation knows about,...
const EASYEDAPRO::V3_DOC_PARSER & getCachedLibraryParser(const wxString &aLibraryPath) const
~SCH_IO_EASYEDAPRO_V3() override
wxString m_cachedLibraryPath
bool CanReadSchematicFile(const wxString &aFileName) const override
Checks if this SCH_IO can read the specified schematic file.
std::vector< std::unique_ptr< LIB_SYMBOL > > m_importedLibSymbols
Definitions from the last LoadSchematicFile, cloned out to the import reconciler on request.
LIB_SYMBOL * LoadSymbol(const wxString &aLibraryPath, const wxString &aAliasName, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Load a LIB_SYMBOL object having aPartName from the aLibraryPath containing a library format that this...
void EnumerateSymbolLib(wxArrayString &aSymbolNameList, const wxString &aLibraryPath, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Populate a list of LIB_SYMBOL alias names contained within the library aLibraryPath.
SCH_IO(const wxString &aName)
void Append(SCH_ITEM *aItem, bool aUpdateLibSymbol=true)
const KIID & GetUuid() const
void SetFileName(const wxString &aFileName)
Set the file name for this screen to aFileName.
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
void UpdateAllScreenReferences() const
Update all the symbol references for this sheet path.
void SetPageNumber(const wxString &aPageNumber)
Set the sheet instance user definable page number.
void push_back(SCH_SHEET *aSheet)
Forwarded method from std::vector.
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
void SetFileName(const wxString &aFilename)
void SetName(const wxString &aName)
SCH_SCREEN * GetScreen() const
void SetScreen(SCH_SCREEN *aScreen)
Set the SCH_SCREEN associated with this sheet to aScreen.
static const std::string KiCadSchematicFileExtension
#define THROW_IO_ERRORF(msg,...)
wxString get_def(const std::map< wxString, wxString > &aMap, const char *aKey, const char *aDefval="")
std::optional< V > get_opt(const std::map< wxString, V > &aMap, const wxString &aKey)
nlohmann::json BuildV3ProjectIndexFromRawDocs(const V3_DOC_PARSER &aParser, bool aIncludeLibraryMetadata=true)
Build a minimal legacy-style project index from parsed v3 raw documents.
LIB_ID ToKiCadLibID(const wxString &aLibName, const wxString &aLibReference)
wxString MakeUniqueLibName(std::set< wxString > &aUsedNames, const wxString &aName, const wxString &aFallback)
Allocate a unique library item name, recording it in aUsedNames.
wxString KeywordsFromV3Tags(const nlohmann::json &aTags)
Build KiCad keywords from a v3 tags object (parent_tag / child_tag name fields).
wxString GetV3LibraryItemTitle(const nlohmann::json &aMetadata, const wxString &aUuid)
wxString ResolveDeviceFieldVariables(const wxString &aInput, const std::map< wxString, wxString > &aDeviceAttributes)
Resolve EasyEDA ={Var} / ={A}text{B} field expressions against device attributes.
wxString NormalizeEasyEDAText(wxString aText)
Replace EasyEDA temperature glyph (℃) with °C.
std::vector< IMPORT_PROJECT_DESC > ProjectToSelectorDialog(const nlohmann::json &aProject, bool aPcbOnly=false, bool aSchOnly=false)
wxString ShortenLibName(wxString aProjectName)
std::map< wxString, V3_SYMBOL_LIB_ITEM > BuildV3SymbolLibraryMap(const V3_DOC_PARSER &aParser)
std::map< wxString, BLOB > BuildV3BlobMap(const V3_DOC_PARSER &aParser)
std::map< wxString, wxString > BuildV3DeviceLibNames(nlohmann::json &aProject, const std::map< wxString, PRJ_DEVICE > &aDevices, std::set< wxString > *aUsedNames=nullptr)
Stable project-lib item names keyed by Device UUID (one entry per Device).
static long long getFileTimestamp(const wxString &aFileName)
static LIB_SYMBOL * LoadV3LibrarySymbolItem(const EASYEDAPRO::V3_DOC_PARSER &aParser, const EASYEDAPRO::V3_SYMBOL_LIB_ITEM &aItem, const wxString &aLibName, const wxString &aSymbolName, const std::map< wxString, EASYEDAPRO::BLOB > &aBlobs, SCH_EASYEDAPRO_V3_PARSER &aSymbolParser)
wxString EscapeString(const wxString &aSource, ESCAPE_CONTEXT aContext)
The Escape/Unescape routines use HTML-entity-reference-style encoding to handle characters which are:...
std::map< wxString, wxString > attributes
std::unique_ptr< LIB_SYMBOL > libSymbol
Raw parsed document from an EasyEDA Pro v3 .epru stream.
Build the schematic-library name map for a v3 .elibz2.
wxString result
Test unit parsing edge cases and error handling.
VECTOR2< int32_t > VECTOR2I
Definition of file extensions used in Kicad.