37#include <wx/stdstream.h>
38#include <wx/zipstrm.h>
40#include <nlohmann/json.hpp>
42#include <wx/wfstream.h>
49 if( aName.Lower().EndsWith( wxS(
".json" ) ) )
51 wxStdInputStream sin( aStream );
52 nlohmann::json js = nlohmann::json::parse( sin,
nullptr,
false );
54 if( js.is_discarded() )
74 else if( aName.Lower().EndsWith( wxS(
".zip" ) ) )
76 std::shared_ptr<wxZipEntry> entry;
77 wxZipInputStream
zip( aStream );
82 while( entry.reset(
zip.GetNextEntry() ), entry.get() != NULL )
84 wxString
name = entry->GetName();
102 wxFFileInputStream in( aFileName );
109 catch( nlohmann::json::exception& )
112 catch( std::exception& )
133 const wxString& aAliasName,
const std::map<std::string, UTF8>* aProperties )
136 std::map<wxString, int> namesCounter;
140 wxFFileInputStream in( aLibraryPath );
147 THROW_IO_ERROR( wxString::Format(
_(
"Unable to find a valid schematic file in '%s'" ),
171 for( wxString shap : dataStrDoc.
shape )
173 if( !shap.Contains( wxS(
"LIB" ) ) )
176 shap.Replace( wxS(
"#@$" ), wxS(
"\n" ) );
177 wxArrayString parts = wxSplit( shap,
'\n',
'\0' );
179 if( parts.size() < 1 )
182 wxArrayString paramsRoot = wxSplit( parts[0],
'~',
'\0' );
184 if( paramsRoot.size() < 1 )
187 wxString rootType = paramsRoot[0];
189 if( rootType == wxS(
"LIB" ) )
191 if( paramsRoot.size() < 4 )
195 parser.
Convert( paramsRoot[2] ) );
197 wxString symbolName = wxString::Format( wxS(
"Unknown_%s_%s" ),
198 paramsRoot[1], paramsRoot[2] );
200 wxArrayString paramParts = wxSplit( paramsRoot[3],
'`',
'\0' );
202 std::map<wxString, wxString> paramMap;
204 for(
size_t i = 1; i < paramParts.size(); i += 2 )
206 wxString key = paramParts[i - 1];
207 wxString value = paramParts[i];
209 if( key == wxS(
"spiceSymbolName" ) && !value.IsEmpty() )
212 paramMap[key] = value;
215 int& serial = namesCounter[symbolName];
218 symbolName << wxS(
"_" ) << serial;
222 paramMap[wxS(
"spiceSymbolName" )] = symbolName;
224 if( symbolName == aAliasName )
232 wxString origRef = refField.
GetText();
235 for(
size_t i = 0; i < origRef.size() && !wxIsdigit( origRef[i] ); i++ )
236 reference << origRef[i];
250 wxString symbolName = wxS(
"Unknown" );
252 std::optional<std::map<wxString, wxString>> c_para;
262 symbolName =
get_def( *c_para, wxS(
"name" ), symbolName );
264 int& serial = namesCounter[symbolName];
267 symbolName << wxS(
"_" ) << serial;
271 if( symbolName != aAliasName )
280 wxString origRef = refField.
GetText();
283 for(
size_t i = 0; i < origRef.size() && !wxIsdigit( origRef[i] ); i++ )
284 reference << origRef[i];
291 catch( nlohmann::json::exception& e )
293 THROW_IO_ERROR( wxString::Format(
_(
"Error loading symbol '%s' from library '%s': %s" ),
294 aAliasName, aLibraryPath, e.what() ) );
296 catch( std::exception& e )
298 THROW_IO_ERROR( wxString::Format(
_(
"Error loading symbol '%s' from library '%s': %s" ),
299 aAliasName, aLibraryPath, e.what() ) );
307 const wxString& aLibraryPath,
308 const std::map<std::string, UTF8>* aProperties )
310 std::map<wxString, int> namesCounter;
317 wxFFileInputStream in( aLibraryPath );
324 THROW_IO_ERROR( wxString::Format(
_(
"Unable to find a valid schematic file in '%s'" ),
348 for( wxString shap : dataStrDoc.
shape )
350 if( !shap.Contains( wxS(
"LIB" ) ) )
353 shap.Replace( wxS(
"#@$" ), wxS(
"\n" ) );
354 wxArrayString parts = wxSplit( shap,
'\n',
'\0' );
356 if( parts.size() < 1 )
359 wxArrayString paramsRoot = wxSplit( parts[0],
'~',
'\0' );
361 if( paramsRoot.size() < 1 )
364 wxString rootType = paramsRoot[0];
366 if( rootType == wxS(
"LIB" ) )
368 if( paramsRoot.size() < 4 )
371 wxString symbolName = wxString::Format( wxS(
"Unknown_%s_%s" ),
372 paramsRoot[1], paramsRoot[2] );
374 wxArrayString paramParts = wxSplit( paramsRoot[3],
'`',
'\0' );
376 std::map<wxString, wxString> paramMap;
378 for(
size_t i = 1; i < paramParts.size(); i += 2 )
380 wxString key = paramParts[i - 1];
381 wxString value = paramParts[i];
383 if( key == wxS(
"spiceSymbolName" ) && !value.IsEmpty() )
386 paramMap[key] = value;
389 int& serial = namesCounter[symbolName];
392 symbolName << wxS(
"_" ) << serial;
396 aSymbolNameList.Add( symbolName );
405 wxString packageName = wxS(
"Unknown" );
409 packageName =
get_def( *symDoc.
c_para, wxS(
"name" ), packageName );
416 aSymbolNameList.Add( packageName );
419 catch( nlohmann::json::exception& e )
421 THROW_IO_ERROR( wxString::Format(
_(
"Error enumerating symbol library '%s': %s" ),
422 aLibraryPath, e.what() ) );
424 catch( std::exception& e )
426 THROW_IO_ERROR( wxString::Format(
_(
"Error enumerating symbol library '%s': %s" ),
427 aLibraryPath, e.what() ) );
433 const wxString& aLibraryPath,
434 const std::map<std::string, UTF8>* aProperties )
436 wxFFileInputStream in( aLibraryPath );
443 THROW_IO_ERROR( wxString::Format(
_(
"Unable to find a valid schematic file in '%s'" ),
449 wxArrayString symbolNameList;
453 for(
const wxString& symbolName : symbolNameList )
458 aSymbolList.push_back( sym );
461 catch( nlohmann::json::exception& e )
463 THROW_IO_ERROR( wxString::Format(
_(
"Error enumerating symbol library '%s': %s" ),
464 aLibraryPath, e.what() ) );
466 catch( std::exception& e )
468 THROW_IO_ERROR( wxString::Format(
_(
"Error enumerating symbol library '%s': %s" ),
469 aLibraryPath, e.what() ) );
475 const wxString& aAliasName,
476 const std::map<std::string, UTF8>* aProperties )
480 wxFFileInputStream in( aLibraryPath );
487 THROW_IO_ERROR( wxString::Format(
_(
"Unable to find a valid schematic file in '%s'" ),
491 return loadSymbol( aLibraryPath, js, aAliasName, aProperties );
493 catch( nlohmann::json::exception& e )
495 THROW_IO_ERROR( wxString::Format(
_(
"Error loading symbol '%s' from library '%s': %s" ),
496 aAliasName, aLibraryPath, e.what() ) );
498 catch( std::exception& e )
500 THROW_IO_ERROR( wxString::Format(
_(
"Error loading symbol '%s' from library '%s': %s" ),
501 aAliasName, aLibraryPath, e.what() ) );
514 wxFFileInputStream in( aFileName );
521 THROW_IO_ERROR( wxString::Format(
_(
"Unable to find a valid schematic file in '%s'" ),
548 wxString sheetTitle =
549 !subDoc.
title.empty() ? subDoc.
title : ( wxString() << pageNum );
553 wxFileName sheetFname( aFileName );
554 sheetFname.SetFullName(
555 sheetBaseName + wxS(
"." )
558 wxFileName relSheetPath( sheetFname );
559 relSheetPath.MakeRelativeTo(
560 wxFileName( aRootSheet->
GetFileName() ).GetPath() );
562 std::unique_ptr<SCH_SHEET> subSheet = std::make_unique<SCH_SHEET>( aSchematic );
563 subSheet->SetFileName( relSheetPath.GetFullPath() );
564 subSheet->SetName( sheetTitle );
569 subSheet->SetScreen( screen );
577 subSheet->SetPosition( pos );
599 catch( nlohmann::json::exception& e )
602 wxString::Format(
_(
"Error loading schematic '%s': %s" ), aFileName, e.what() ) );
604 catch( std::exception& e )
607 wxString::Format(
_(
"Error loading schematic '%s': %s" ), aFileName, e.what() ) );
614 const std::map<std::string, UTF8>* aProperties )
616 wxCHECK( !aFileName.IsEmpty() && aSchematic,
nullptr );
625 wxCHECK_MSG( aSchematic->
IsValid(),
nullptr,
626 wxS(
"Can't append to a schematic with no root!" ) );
628 rootSheet = &aSchematic->
Root();
634 aSchematic->
SetRoot( rootSheet );
647 wxCHECK_MSG( libTable,
nullptr, wxS(
"Could not load symbol lib table." ) );
constexpr EDA_IU_SCALE schIUScale
void SetPageNumber(const wxString &aPageNumber)
static double Convert(const wxString &aValue)
virtual const wxString & GetText() const
Return the string associated with the text object.
Define a library symbol object.
SCH_FIELD & GetReferenceField() const
Return reference to the reference designator field.
static SYMBOL_LIB_TABLE * SchSymbolLibTable(PROJECT *aProject)
Accessor for project symbol library table.
Holds all the data relating to one schematic.
SCH_SHEET_PATH & CurrentSheet() const override
void SetCurrentSheet(const SCH_SHEET_PATH &aPath) override
void SetRoot(SCH_SHEET *aRootSheet)
Initialize the schematic with a new root sheet.
bool IsValid() const
A simple test if the schematic is loaded, not a complete one.
PROJECT & Prj() const override
Return a reference to the project this schematic is part of.
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)
Instances are attached to a symbol or sheet and provide a place for the symbol's value,...
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.
SCH_SCREEN * GetScreen() const
void SetScreen(SCH_SCREEN *aScreen)
Set the SCH_SCREEN associated with this sheet to aScreen.
static REPORTER & GetInstance()
static void SetReporter(REPORTER *aReporter)
Set the reporter to use for reporting font substitution warnings.
static const std::string KiCadSchematicFileExtension
#define THROW_IO_ERROR(msg)
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
constexpr int MilsToIU(int mils) const
Definition of file extensions used in Kicad.