From a01e4f19b410ab0d0c47fb3ecefc859fcff75565 Mon Sep 17 00:00:00 2001 From: Tomasz Wlostowski Date: Fri, 18 Sep 2020 16:19:47 +0200 Subject: [PATCH] eeschema: allow exporting netlists through UI-less KIFACE API function --- eeschema/eeschema.cpp | 63 +++++++++++++++++++++++++++++++++++++++++++ include/kiface_ids.h | 7 ++++- 2 files changed, 69 insertions(+), 1 deletion(-) diff --git a/eeschema/eeschema.cpp b/eeschema/eeschema.cpp index ac1abe522f..44a0f84112 100644 --- a/eeschema/eeschema.cpp +++ b/eeschema/eeschema.cpp @@ -44,6 +44,11 @@ #include #include #include +#include +#include + +#include +#include // The main sheet of the project SCH_SHEET* g_RootSheet = NULL; @@ -54,6 +59,59 @@ TRANSFORM DefaultTransform = TRANSFORM( 1, 0, 0, -1 ); namespace SCH { + +static std::unique_ptr readSchematicFromFile( const std::string& aFilename ) +{ + auto pi = SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_KICAD ); + std::unique_ptr schematic( new SCHEMATIC ( nullptr ) ); + + auto &manager = Pgm().GetSettingsManager(); + + manager.LoadProject( "" ); + schematic->Reset(); + schematic->SetProject( &manager.Prj() ); + schematic->SetRoot( pi->Load( aFilename, schematic.get() ) ); + schematic->CurrentSheet().push_back( &schematic->Root() ); + + SCH_SCREENS screens( schematic->Root() ); + + for( SCH_SCREEN* screen = screens.GetFirst(); screen; screen = screens.GetNext() ) + screen->UpdateLocalLibSymbolLinks(); + + SCH_SHEET_LIST sheets = schematic->GetSheets(); + + // Restore all of the loaded symbol instances from the root sheet screen. + sheets.UpdateSymbolInstances( schematic->RootScreen()->GetSymbolInstances() ); + + sheets.AnnotatePowerSymbols(); + + // NOTE: This is required for multi-unit symbols to be correct + // Normally called from SCH_EDIT_FRAME::FixupJunctions() but could be refactored + for( SCH_SHEET_PATH& sheet : sheets ) + sheet.UpdateAllScreenReferences(); + + // NOTE: SchematicCleanUp is not called; QA schematics must already be clean or else + // SchematicCleanUp must be freed from its UI dependencies. + + schematic->ConnectionGraph()->Recalculate( sheets, true ); + + return schematic; +} + + +bool generateSchematicNetlist( const wxString& aFilename, wxString& aNetlist ) +{ + std::unique_ptr schematic ( readSchematicFromFile( (const char*) aFilename ) ); + NETLIST_EXPORTER_KICAD exporter( schematic.get() ); + STRING_FORMATTER formatter; + + exporter.Format( &formatter, GNL_ALL | GNL_OPT_KICAD ); + aNetlist = formatter.GetString(); + + return true; +} + + static struct IFACE : public KIFACE_I { // Of course all are virtual overloads, implementations of the KIFACE. @@ -123,6 +181,11 @@ static struct IFACE : public KIFACE_I */ void* IfaceOrAddress( int aDataId ) override { + switch( aDataId ) + { + case KIFACE_NETLIST_SCHEMATIC: + return (void*) generateSchematicNetlist; + } return NULL; } diff --git a/include/kiface_ids.h b/include/kiface_ids.h index 61e9f16537..49a5cad8b1 100644 --- a/include/kiface_ids.h +++ b/include/kiface_ids.h @@ -50,7 +50,12 @@ enum KIFACE_ADDR_ID : int * Type is FP_LIB_TABLE* * Caller does NOT own. */ - KIFACE_GLOBAL_FOOTPRINT_TABLE + KIFACE_GLOBAL_FOOTPRINT_TABLE, + + KIFACE_NEW_DRC_ENGINE, + KIFACE_LOAD_PCB, + KIFACE_LOAD_SCHEMATIC, + KIFACE_NETLIST_SCHEMATIC }; #endif // KIFACE_IDS