34#include <wx/stdstream.h>
35#include <wx/zipstrm.h>
39#include <wx/wfstream.h>
46 if( aName.Lower().EndsWith( wxS(
".json" ) ) )
48 wxStdInputStream sin( aStream );
49 nlohmann::json js = nlohmann::json::parse( sin,
nullptr,
false );
51 if( js.is_discarded() )
73 else if( aName.Lower().EndsWith( wxS(
".zip" ) ) )
75 std::shared_ptr<wxZipEntry> entry;
76 wxZipInputStream
zip( aStream );
81 while( entry.reset(
zip.GetNextEntry() ), entry.get() != NULL )
83 wxString
name = entry->GetName();
95static std::vector<EASYEDA::DOCUMENT>
SheetsOf(
const nlohmann::json& aJson )
117 wxFFileInputStream in( aFileName );
124 catch( nlohmann::json::exception& )
127 catch( std::exception& )
148 const wxString& aAliasName,
const std::map<std::string, UTF8>* aProperties,
152 std::map<wxString, int> namesCounter;
156 wxFFileInputStream in( aLibraryPath );
162 THROW_IO_ERRORF(
_(
"Unable to find a valid schematic file in '%s'" ), aLibraryPath );
182 for( wxString shap : dataStrDoc.
shape )
184 if( !shap.Contains( wxS(
"LIB" ) ) )
187 shap.Replace( wxS(
"#@$" ), wxS(
"\n" ) );
188 wxArrayString parts = wxSplit( shap,
'\n',
'\0' );
190 if( parts.size() < 1 )
193 wxArrayString paramsRoot = wxSplit( parts[0],
'~',
'\0' );
195 if( paramsRoot.size() < 1 )
198 wxString rootType = paramsRoot[0];
200 if( rootType == wxS(
"LIB" ) )
202 if( paramsRoot.size() < 4 )
206 parser.
Convert( paramsRoot[2] ) );
208 wxString symbolName = wxString::Format( wxS(
"Unknown_%s_%s" ),
209 paramsRoot[1], paramsRoot[2] );
211 wxArrayString paramParts = wxSplit( paramsRoot[3],
'`',
'\0' );
213 std::map<wxString, wxString> paramMap;
215 for(
size_t i = 1; i < paramParts.size(); i += 2 )
217 wxString key = paramParts[i - 1];
218 wxString value = paramParts[i];
220 if( key == wxS(
"spiceSymbolName" ) && !value.IsEmpty() )
223 paramMap[key] = value;
226 int& serial = namesCounter[symbolName];
229 symbolName << wxS(
"_" ) << serial;
233 paramMap[wxS(
"spiceSymbolName" )] = symbolName;
235 if( symbolName == aAliasName )
243 wxString origRef = refField.
GetText();
246 for(
size_t i = 0; i < origRef.size() && !wxIsdigit( origRef[i] ); i++ )
247 reference << origRef[i];
261 wxString symbolName = wxS(
"Unknown" );
263 std::optional<std::map<wxString, wxString>> c_para;
273 symbolName =
get_def( *c_para, wxS(
"name" ), symbolName );
275 int& serial = namesCounter[symbolName];
278 symbolName << wxS(
"_" ) << serial;
282 if( symbolName != aAliasName )
291 wxString origRef = refField.
GetText();
294 for(
size_t i = 0; i < origRef.size() && !wxIsdigit( origRef[i] ); i++ )
295 reference << origRef[i];
302 catch( nlohmann::json::exception& e )
309 catch( std::exception& e )
322 const wxString& aLibraryPath,
323 const std::map<std::string, UTF8>* aProperties )
325 std::map<wxString, int> namesCounter;
332 wxFFileInputStream in( aLibraryPath );
338 THROW_IO_ERRORF(
_(
"Unable to find a valid schematic file in '%s'" ), aLibraryPath );
358 for( wxString shap : dataStrDoc.
shape )
360 if( !shap.Contains( wxS(
"LIB" ) ) )
363 shap.Replace( wxS(
"#@$" ), wxS(
"\n" ) );
364 wxArrayString parts = wxSplit( shap,
'\n',
'\0' );
366 if( parts.size() < 1 )
369 wxArrayString paramsRoot = wxSplit( parts[0],
'~',
'\0' );
371 if( paramsRoot.size() < 1 )
374 wxString rootType = paramsRoot[0];
376 if( rootType == wxS(
"LIB" ) )
378 if( paramsRoot.size() < 4 )
381 wxString symbolName = wxString::Format( wxS(
"Unknown_%s_%s" ),
382 paramsRoot[1], paramsRoot[2] );
384 wxArrayString paramParts = wxSplit( paramsRoot[3],
'`',
'\0' );
386 std::map<wxString, wxString> paramMap;
388 for(
size_t i = 1; i < paramParts.size(); i += 2 )
390 wxString key = paramParts[i - 1];
391 wxString value = paramParts[i];
393 if( key == wxS(
"spiceSymbolName" ) && !value.IsEmpty() )
396 paramMap[key] = value;
399 int& serial = namesCounter[symbolName];
402 symbolName << wxS(
"_" ) << serial;
406 aSymbolNameList.Add( symbolName );
415 wxString packageName = wxS(
"Unknown" );
419 packageName =
get_def( *symDoc.
c_para, wxS(
"name" ), packageName );
426 aSymbolNameList.Add( packageName );
429 catch( nlohmann::json::exception& e )
431 THROW_IO_ERRORF(
_(
"Error enumerating symbol library '%s': %s" ), aLibraryPath, e.what() );
433 catch( std::exception& e )
435 THROW_IO_ERRORF(
_(
"Error enumerating symbol library '%s': %s" ), aLibraryPath, e.what() );
441 const wxString& aLibraryPath,
442 const std::map<std::string, UTF8>* aProperties )
444 wxFFileInputStream in( aLibraryPath );
450 THROW_IO_ERRORF(
_(
"Unable to find a valid schematic file in '%s'" ), aLibraryPath );
454 wxArrayString symbolNameList;
458 for(
const wxString& symbolName : symbolNameList )
463 aSymbolList.push_back( sym );
466 catch( nlohmann::json::exception& e )
468 THROW_IO_ERRORF(
_(
"Error enumerating symbol library '%s': %s" ), aLibraryPath, e.what() );
470 catch( std::exception& e )
472 THROW_IO_ERRORF(
_(
"Error enumerating symbol library '%s': %s" ), aLibraryPath, e.what() );
478 const wxString& aAliasName,
479 const std::map<std::string, UTF8>* aProperties )
483 wxFFileInputStream in( aLibraryPath );
489 THROW_IO_ERRORF(
_(
"Unable to find a valid schematic file in '%s'" ), aLibraryPath );
493 catch( nlohmann::json::exception& e )
495 THROW_IO_ERRORF(
_(
"Error loading symbol '%s' from library '%s': %s" ), aAliasName, aLibraryPath, e.what() );
497 catch( std::exception& e )
499 THROW_IO_ERRORF(
_(
"Error loading symbol '%s' from library '%s': %s" ), aAliasName, aLibraryPath, e.what() );
511 wxFFileInputStream in( aFileName );
517 THROW_IO_ERRORF(
_(
"Unable to find a valid schematic file in '%s'" ), aFileName );
548 wxString sheetTitle = !subDoc.
title.empty() ? subDoc.
title : ( wxString() << pageNum );
552 wxFileName sheetFname( aFileName );
553 sheetFname.SetFullName( sheetBaseName + wxS(
"." )
556 wxFileName relSheetPath( sheetFname );
557 relSheetPath.MakeRelativeTo( wxFileName( aRootSheet->
GetFileName() ).GetPath() );
559 std::unique_ptr<SCH_SHEET> subSheet = std::make_unique<SCH_SHEET>( aSchematic );
560 subSheet->SetFileName( relSheetPath.GetFullPath() );
561 subSheet->SetName( sheetTitle );
566 subSheet->SetScreen( screen );
571 + ( subSheet->GetSize().y +
schIUScale.MilsToIU( 200 ) ) * ( pageNum - 1 );
573 subSheet->SetPosition( pos );
594 catch( nlohmann::json::exception& e )
596 THROW_IO_ERRORF(
_(
"Error loading schematic '%s': %s" ), aFileName, e.what() );
598 catch( std::exception& e )
600 THROW_IO_ERRORF(
_(
"Error loading schematic '%s': %s" ), aFileName, e.what() );
607 const std::map<std::string, UTF8>* aProperties )
609 wxCHECK( !aFileName.IsEmpty() && aSchematic,
nullptr );
618 wxCHECK_MSG( aSchematic->
IsValid(),
nullptr,
619 wxS(
"Can't append to a schematic with no root!" ) );
621 rootSheet = aAppendToMe;
constexpr EDA_IU_SCALE schIUScale
void SetPageNumber(const wxString &aPageNumber)
static double Convert(const wxString &aValue)
RAII class to set and restore the fontconfig reporter.
REPORTER * m_reporter
Reporter to log errors/warnings to, may be nullptr.
Define a library symbol object.
SCH_FIELD & GetReferenceField()
static LOAD_INFO_REPORTER & GetInstance()
A pure virtual class used to derive REPORTER objects from.
Holds all the data relating to one schematic.
void SetCurrentSheet(const SCH_SHEET_PATH &aPath)
bool IsValid() const
A simple test if the schematic is loaded, not a complete one.
void SetTopLevelSheets(const std::vector< SCH_SHEET * > &aSheets)
Replace the top level sheets, rebuilding the hierarchy and connectivity around them.
SCH_SHEET_PATH & CurrentSheet() const
void ParseSchematic(SCHEMATIC *aSchematic, SCH_SHEET *aRootSheet, const wxString &aFileName, wxArrayString aShapes)
LIB_SYMBOL * ParseSymbol(const VECTOR2D &aOrigin, std::map< wxString, wxString > aParams, wxArrayString aShapes)
virtual const wxString & GetText() const override
Return the string associated with the text object.
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.
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,...
int GetModifyHash() const override
Return the modification hash from the library cache.
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.
bool CanReadSchematicFile(const wxString &aFileName) const override
Checks if this SCH_IO can read the specified schematic file.
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...
virtual bool CanReadSchematicFile(const wxString &aFileName) const
Checks if this SCH_IO can read the specified schematic file.
void Append(SCH_ITEM *aItem, bool aUpdateLibSymbol=true)
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)
wxString GetFileName() const
Return the filename corresponding to this sheet.
void SyncUuidToScreen()
Take the identity of the screen this sheet owns.
SCH_SCREEN * GetScreen() const
void SetScreen(SCH_SCREEN *aScreen)
Set the SCH_SCREEN associated with this sheet to aScreen.
#define THROW_IO_ERRORF(msg,...)
static const std::string KiCadSchematicFileExtension
wxString get_def(const std::map< wxString, wxString > &aMap, const char *aKey, const char *aDefval="")
bool UnwrapDataStr(nlohmann::json &aJson)
Replace a document the EasyEDA server wraps in a metadata envelope with the document it carries.
static void LoadSchematic(SCHEMATIC *aSchematic, SCH_SHEET *aRootSheet, const wxString &aFileName, REPORTER *aReporter)
LIB_SYMBOL * loadSymbol(const wxString &aLibraryPath, nlohmann::json aFileData, const wxString &aAliasName, const std::map< std::string, UTF8 > *aProperties, REPORTER *aReporter)
static std::vector< EASYEDA::DOCUMENT > SheetsOf(const nlohmann::json &aJson)
static bool FindSchFileInStream(const wxString &aName, wxInputStream &aStream, nlohmann::json &aOut, EASYEDA::DOCUMENT &aDoc, EASYEDA::DOC_TYPE &aDocType)
wxString EscapeString(const wxString &aSource, ESCAPE_CONTEXT aContext)
The Escape/Unescape routines use HTML-entity-reference-style encoding to handle characters which are:...
std::optional< std::vector< DOCUMENT > > schematics
std::optional< std::map< wxString, wxString > > c_para
std::optional< nlohmann::json > dataStr
std::optional< DOC_TYPE > docType
std::optional< std::map< wxString, wxString > > c_para
VECTOR2< int32_t > VECTOR2I
VECTOR2< double > VECTOR2D
Definition of file extensions used in Kicad.