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 );
143 THROW_IO_ERRORF(
_(
"Unable to find a valid schematic file in '%s'" ), aLibraryPath );
165 for( wxString shap : dataStrDoc.
shape )
167 if( !shap.Contains( wxS(
"LIB" ) ) )
170 shap.Replace( wxS(
"#@$" ), wxS(
"\n" ) );
171 wxArrayString parts = wxSplit( shap,
'\n',
'\0' );
173 if( parts.size() < 1 )
176 wxArrayString paramsRoot = wxSplit( parts[0],
'~',
'\0' );
178 if( paramsRoot.size() < 1 )
181 wxString rootType = paramsRoot[0];
183 if( rootType == wxS(
"LIB" ) )
185 if( paramsRoot.size() < 4 )
189 parser.
Convert( paramsRoot[2] ) );
191 wxString symbolName = wxString::Format( wxS(
"Unknown_%s_%s" ),
192 paramsRoot[1], paramsRoot[2] );
194 wxArrayString paramParts = wxSplit( paramsRoot[3],
'`',
'\0' );
196 std::map<wxString, wxString> paramMap;
198 for(
size_t i = 1; i < paramParts.size(); i += 2 )
200 wxString key = paramParts[i - 1];
201 wxString value = paramParts[i];
203 if( key == wxS(
"spiceSymbolName" ) && !value.IsEmpty() )
206 paramMap[key] = value;
209 int& serial = namesCounter[symbolName];
212 symbolName << wxS(
"_" ) << serial;
216 paramMap[wxS(
"spiceSymbolName" )] = symbolName;
218 if( symbolName == aAliasName )
226 wxString origRef = refField.
GetText();
229 for(
size_t i = 0; i < origRef.size() && !wxIsdigit( origRef[i] ); i++ )
230 reference << origRef[i];
244 wxString symbolName = wxS(
"Unknown" );
246 std::optional<std::map<wxString, wxString>> c_para;
256 symbolName =
get_def( *c_para, wxS(
"name" ), symbolName );
258 int& serial = namesCounter[symbolName];
261 symbolName << wxS(
"_" ) << serial;
265 if( symbolName != aAliasName )
274 wxString origRef = refField.
GetText();
277 for(
size_t i = 0; i < origRef.size() && !wxIsdigit( origRef[i] ); i++ )
278 reference << origRef[i];
285 catch( nlohmann::json::exception& e )
292 catch( std::exception& e )
305 const wxString& aLibraryPath,
306 const std::map<std::string, UTF8>* aProperties )
308 std::map<wxString, int> namesCounter;
315 wxFFileInputStream in( aLibraryPath );
321 THROW_IO_ERRORF(
_(
"Unable to find a valid schematic file in '%s'" ), aLibraryPath );
343 for( wxString shap : dataStrDoc.
shape )
345 if( !shap.Contains( wxS(
"LIB" ) ) )
348 shap.Replace( wxS(
"#@$" ), wxS(
"\n" ) );
349 wxArrayString parts = wxSplit( shap,
'\n',
'\0' );
351 if( parts.size() < 1 )
354 wxArrayString paramsRoot = wxSplit( parts[0],
'~',
'\0' );
356 if( paramsRoot.size() < 1 )
359 wxString rootType = paramsRoot[0];
361 if( rootType == wxS(
"LIB" ) )
363 if( paramsRoot.size() < 4 )
366 wxString symbolName = wxString::Format( wxS(
"Unknown_%s_%s" ),
367 paramsRoot[1], paramsRoot[2] );
369 wxArrayString paramParts = wxSplit( paramsRoot[3],
'`',
'\0' );
371 std::map<wxString, wxString> paramMap;
373 for(
size_t i = 1; i < paramParts.size(); i += 2 )
375 wxString key = paramParts[i - 1];
376 wxString value = paramParts[i];
378 if( key == wxS(
"spiceSymbolName" ) && !value.IsEmpty() )
381 paramMap[key] = value;
384 int& serial = namesCounter[symbolName];
387 symbolName << wxS(
"_" ) << serial;
391 aSymbolNameList.Add( symbolName );
400 wxString packageName = wxS(
"Unknown" );
404 packageName =
get_def( *symDoc.
c_para, wxS(
"name" ), packageName );
411 aSymbolNameList.Add( packageName );
414 catch( nlohmann::json::exception& e )
416 THROW_IO_ERRORF(
_(
"Error enumerating symbol library '%s': %s" ), aLibraryPath, e.what() );
418 catch( std::exception& e )
420 THROW_IO_ERRORF(
_(
"Error enumerating symbol library '%s': %s" ), aLibraryPath, e.what() );
426 const wxString& aLibraryPath,
427 const std::map<std::string, UTF8>* aProperties )
429 wxFFileInputStream in( aLibraryPath );
435 THROW_IO_ERRORF(
_(
"Unable to find a valid schematic file in '%s'" ), aLibraryPath );
439 wxArrayString symbolNameList;
443 for(
const wxString& symbolName : symbolNameList )
448 aSymbolList.push_back( sym );
451 catch( nlohmann::json::exception& e )
453 THROW_IO_ERRORF(
_(
"Error enumerating symbol library '%s': %s" ), aLibraryPath, e.what() );
455 catch( std::exception& e )
457 THROW_IO_ERRORF(
_(
"Error enumerating symbol library '%s': %s" ), aLibraryPath, e.what() );
463 const wxString& aAliasName,
464 const std::map<std::string, UTF8>* aProperties )
468 wxFFileInputStream in( aLibraryPath );
474 THROW_IO_ERRORF(
_(
"Unable to find a valid schematic file in '%s'" ), aLibraryPath );
476 return loadSymbol( aLibraryPath, js, aAliasName, aProperties );
478 catch( nlohmann::json::exception& e )
480 THROW_IO_ERRORF(
_(
"Error loading symbol '%s' from library '%s': %s" ), aAliasName, aLibraryPath, e.what() );
482 catch( std::exception& e )
484 THROW_IO_ERRORF(
_(
"Error loading symbol '%s' from library '%s': %s" ), aAliasName, aLibraryPath, e.what() );
495 wxFFileInputStream in( aFileName );
501 THROW_IO_ERRORF(
_(
"Unable to find a valid schematic file in '%s'" ), aFileName );
525 wxString sheetTitle = !subDoc.
title.empty() ? subDoc.
title : ( wxString() << pageNum );
529 wxFileName sheetFname( aFileName );
530 sheetFname.SetFullName( sheetBaseName + wxS(
"." )
533 wxFileName relSheetPath( sheetFname );
534 relSheetPath.MakeRelativeTo( wxFileName( aRootSheet->
GetFileName() ).GetPath() );
536 std::unique_ptr<SCH_SHEET> subSheet = std::make_unique<SCH_SHEET>( aSchematic );
537 subSheet->SetFileName( relSheetPath.GetFullPath() );
538 subSheet->SetName( sheetTitle );
543 subSheet->SetScreen( screen );
548 + ( subSheet->GetSize().y +
schIUScale.MilsToIU( 200 ) ) * ( pageNum - 1 );
550 subSheet->SetPosition( pos );
571 catch( nlohmann::json::exception& e )
573 THROW_IO_ERRORF(
_(
"Error loading schematic '%s': %s" ), aFileName, e.what() );
575 catch( std::exception& e )
577 THROW_IO_ERRORF(
_(
"Error loading schematic '%s': %s" ), aFileName, e.what() );
584 const std::map<std::string, UTF8>* aProperties )
586 wxCHECK( !aFileName.IsEmpty() && aSchematic,
nullptr );
595 wxCHECK_MSG( aSchematic->
IsValid(),
nullptr,
596 wxS(
"Can't append to a schematic with no root!" ) );
598 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_ERRORF(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
VECTOR2< int32_t > VECTOR2I
VECTOR2< double > VECTOR2D
Definition of file extensions used in Kicad.