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() )
71 else if( aName.Lower().EndsWith( wxS(
".zip" ) ) )
73 std::shared_ptr<wxZipEntry> entry;
74 wxZipInputStream
zip( aStream );
79 while( entry.reset(
zip.GetNextEntry() ), entry.get() != NULL )
81 wxString
name = entry->GetName();
99 wxFFileInputStream in( aFileName );
106 catch( nlohmann::json::exception& )
109 catch( std::exception& )
130 const wxString& aAliasName,
const std::map<std::string, UTF8>* aProperties )
133 std::map<wxString, int> namesCounter;
137 wxFFileInputStream in( aLibraryPath );
144 THROW_IO_ERROR( wxString::Format(
_(
"Unable to find a valid schematic file in '%s'" ),
168 for( wxString shap : dataStrDoc.
shape )
170 if( !shap.Contains( wxS(
"LIB" ) ) )
173 shap.Replace( wxS(
"#@$" ), wxS(
"\n" ) );
174 wxArrayString parts = wxSplit( shap,
'\n',
'\0' );
176 if( parts.size() < 1 )
179 wxArrayString paramsRoot = wxSplit( parts[0],
'~',
'\0' );
181 if( paramsRoot.size() < 1 )
184 wxString rootType = paramsRoot[0];
186 if( rootType == wxS(
"LIB" ) )
188 if( paramsRoot.size() < 4 )
192 parser.
Convert( paramsRoot[2] ) );
194 wxString symbolName = wxString::Format( wxS(
"Unknown_%s_%s" ),
195 paramsRoot[1], paramsRoot[2] );
197 wxArrayString paramParts = wxSplit( paramsRoot[3],
'`',
'\0' );
199 std::map<wxString, wxString> paramMap;
201 for(
size_t i = 1; i < paramParts.size(); i += 2 )
203 wxString key = paramParts[i - 1];
204 wxString value = paramParts[i];
206 if( key == wxS(
"spiceSymbolName" ) && !value.IsEmpty() )
209 paramMap[key] = value;
212 int& serial = namesCounter[symbolName];
215 symbolName << wxS(
"_" ) << serial;
219 paramMap[wxS(
"spiceSymbolName" )] = symbolName;
221 if( symbolName == aAliasName )
229 wxString origRef = refField.
GetText();
232 for(
size_t i = 0; i < origRef.size() && !wxIsdigit( origRef[i] ); i++ )
233 reference << origRef[i];
247 wxString symbolName = wxS(
"Unknown" );
249 std::optional<std::map<wxString, wxString>> c_para;
259 symbolName =
get_def( *c_para, wxS(
"name" ), symbolName );
261 int& serial = namesCounter[symbolName];
264 symbolName << wxS(
"_" ) << serial;
268 if( symbolName != aAliasName )
277 wxString origRef = refField.
GetText();
280 for(
size_t i = 0; i < origRef.size() && !wxIsdigit( origRef[i] ); i++ )
281 reference << origRef[i];
288 catch( nlohmann::json::exception& e )
290 THROW_IO_ERROR( wxString::Format(
_(
"Error loading symbol '%s' from library '%s': %s" ),
291 aAliasName, aLibraryPath, e.what() ) );
293 catch( std::exception& e )
295 THROW_IO_ERROR( wxString::Format(
_(
"Error loading symbol '%s' from library '%s': %s" ),
296 aAliasName, aLibraryPath, e.what() ) );
304 const wxString& aLibraryPath,
305 const std::map<std::string, UTF8>* aProperties )
307 std::map<wxString, int> namesCounter;
314 wxFFileInputStream in( aLibraryPath );
321 THROW_IO_ERROR( wxString::Format(
_(
"Unable to find a valid schematic file in '%s'" ),
345 for( wxString shap : dataStrDoc.
shape )
347 if( !shap.Contains( wxS(
"LIB" ) ) )
350 shap.Replace( wxS(
"#@$" ), wxS(
"\n" ) );
351 wxArrayString parts = wxSplit( shap,
'\n',
'\0' );
353 if( parts.size() < 1 )
356 wxArrayString paramsRoot = wxSplit( parts[0],
'~',
'\0' );
358 if( paramsRoot.size() < 1 )
361 wxString rootType = paramsRoot[0];
363 if( rootType == wxS(
"LIB" ) )
365 if( paramsRoot.size() < 4 )
368 wxString symbolName = wxString::Format( wxS(
"Unknown_%s_%s" ),
369 paramsRoot[1], paramsRoot[2] );
371 wxArrayString paramParts = wxSplit( paramsRoot[3],
'`',
'\0' );
373 std::map<wxString, wxString> paramMap;
375 for(
size_t i = 1; i < paramParts.size(); i += 2 )
377 wxString key = paramParts[i - 1];
378 wxString value = paramParts[i];
380 if( key == wxS(
"spiceSymbolName" ) && !value.IsEmpty() )
383 paramMap[key] = value;
386 int& serial = namesCounter[symbolName];
389 symbolName << wxS(
"_" ) << serial;
393 aSymbolNameList.Add( symbolName );
402 wxString packageName = wxS(
"Unknown" );
406 packageName =
get_def( *symDoc.
c_para, wxS(
"name" ), packageName );
413 aSymbolNameList.Add( packageName );
416 catch( nlohmann::json::exception& e )
418 THROW_IO_ERROR( wxString::Format(
_(
"Error enumerating symbol library '%s': %s" ),
419 aLibraryPath, e.what() ) );
421 catch( std::exception& e )
423 THROW_IO_ERROR( wxString::Format(
_(
"Error enumerating symbol library '%s': %s" ),
424 aLibraryPath, e.what() ) );
430 const wxString& aLibraryPath,
431 const std::map<std::string, UTF8>* aProperties )
433 wxFFileInputStream in( aLibraryPath );
440 THROW_IO_ERROR( wxString::Format(
_(
"Unable to find a valid schematic file in '%s'" ),
446 wxArrayString symbolNameList;
450 for(
const wxString& symbolName : symbolNameList )
455 aSymbolList.push_back( sym );
458 catch( nlohmann::json::exception& e )
460 THROW_IO_ERROR( wxString::Format(
_(
"Error enumerating symbol library '%s': %s" ),
461 aLibraryPath, e.what() ) );
463 catch( std::exception& e )
465 THROW_IO_ERROR( wxString::Format(
_(
"Error enumerating symbol library '%s': %s" ),
466 aLibraryPath, e.what() ) );
472 const wxString& aAliasName,
473 const std::map<std::string, UTF8>* aProperties )
477 wxFFileInputStream in( aLibraryPath );
484 THROW_IO_ERROR( wxString::Format(
_(
"Unable to find a valid schematic file in '%s'" ),
488 return loadSymbol( aLibraryPath, js, aAliasName, aProperties );
490 catch( nlohmann::json::exception& e )
492 THROW_IO_ERROR( wxString::Format(
_(
"Error loading symbol '%s' from library '%s': %s" ),
493 aAliasName, aLibraryPath, e.what() ) );
495 catch( std::exception& e )
497 THROW_IO_ERROR( wxString::Format(
_(
"Error loading symbol '%s' from library '%s': %s" ),
498 aAliasName, aLibraryPath, e.what() ) );
509 wxFFileInputStream in( aFileName );
516 THROW_IO_ERROR( wxString::Format(
_(
"Unable to find a valid schematic file in '%s'" ),
543 wxString sheetTitle =
544 !subDoc.
title.empty() ? subDoc.
title : ( wxString() << pageNum );
548 wxFileName sheetFname( aFileName );
549 sheetFname.SetFullName(
550 sheetBaseName + wxS(
"." )
553 wxFileName relSheetPath( sheetFname );
554 relSheetPath.MakeRelativeTo(
555 wxFileName( aRootSheet->
GetFileName() ).GetPath() );
557 std::unique_ptr<SCH_SHEET> subSheet = std::make_unique<SCH_SHEET>( aSchematic );
558 subSheet->SetFileName( relSheetPath.GetFullPath() );
559 subSheet->SetName( sheetTitle );
564 subSheet->SetScreen( screen );
569 + ( subSheet->GetSize().y +
schIUScale.MilsToIU( 200 ) )
572 subSheet->SetPosition( pos );
594 catch( nlohmann::json::exception& e )
597 wxString::Format(
_(
"Error loading schematic '%s': %s" ), aFileName, e.what() ) );
599 catch( std::exception& e )
602 wxString::Format(
_(
"Error loading schematic '%s': %s" ), aFileName, e.what() ) );
609 const std::map<std::string, UTF8>* aProperties )
611 wxCHECK( !aFileName.IsEmpty() && aSchematic,
nullptr );
620 wxCHECK_MSG( aSchematic->
IsValid(),
nullptr,
621 wxS(
"Can't append to a schematic with no root!" ) );
623 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.
Define a library symbol object.
SCH_FIELD & GetReferenceField()
Return reference to the reference designator field.
static LOAD_INFO_REPORTER & GetInstance()
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)
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)
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)
wxString GetFileName() const
Return the filename corresponding to this sheet.
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_ERROR(msg)
macro which captures the "call site" values of FILE_, __FUNCTION & LINE
wxString get_def(const std::map< wxString, wxString > &aMap, const char *aKey, const char *aDefval="")
static void LoadSchematic(SCHEMATIC *aSchematic, SCH_SHEET *aRootSheet, const wxString &aFileName)
LIB_SYMBOL * loadSymbol(const wxString &aLibraryPath, nlohmann::json aFileData, const wxString &aAliasName, const std::map< std::string, UTF8 > *aProperties)
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.