eeschema: allow exporting netlists through UI-less KIFACE API function

This commit is contained in:
Tomasz Wlostowski 2020-09-18 16:19:47 +02:00
parent aebfe4c9f0
commit a01e4f19b4
2 changed files with 69 additions and 1 deletions

View File

@ -44,6 +44,11 @@
#include <settings/settings_manager.h>
#include <sexpr/sexpr.h>
#include <sexpr/sexpr_parser.h>
#include <kiface_ids.h>
#include <netlist_exporters/netlist_exporter_kicad.h>
#include <schematic.h>
#include <connection_graph.h>
// 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<SCHEMATIC> readSchematicFromFile( const std::string& aFilename )
{
auto pi = SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_KICAD );
std::unique_ptr<SCHEMATIC> 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> 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;
}

View File

@ -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