CADSTAR SCH: Separate out saving loaded lib symbols to library

This commit is contained in:
Roberto Fernandez Bautista 2023-02-26 17:27:25 +01:00
parent 7a6b64d371
commit 2da13a9e07
3 changed files with 50 additions and 31 deletions

View File

@ -53,10 +53,10 @@
const wxString PartNameFieldName = "Part Name";
void CADSTAR_SCH_ARCHIVE_LOADER::Load( SCHEMATIC* aSchematic, SCH_SHEET* aRootSheet,
SCH_PLUGIN::SCH_PLUGIN_RELEASER* aSchPlugin,
const wxFileName& aLibraryFileName )
void CADSTAR_SCH_ARCHIVE_LOADER::Load( SCHEMATIC* aSchematic, SCH_SHEET* aRootSheet )
{
wxCHECK( aSchematic );
if( m_progressReporter )
m_progressReporter->SetNumPhases( 3 ); // (0) Read file, (1) Parse file, (2) Load file
@ -88,10 +88,8 @@ void CADSTAR_SCH_ARCHIVE_LOADER::Load( SCHEMATIC* aSchematic, SCH_SHEET* aRootSh
// Assume the center at 0,0 since we are going to be translating the design afterwards anyway
m_designCenter = { 0, 0 };
m_schematic = aSchematic;
m_rootSheet = aRootSheet;
m_plugin = aSchPlugin;
m_libraryFileName = aLibraryFileName;
m_schematic = aSchematic;
m_rootSheet = aRootSheet;
if( m_progressReporter )
{
@ -414,12 +412,8 @@ void CADSTAR_SCH_ARCHIVE_LOADER::loadPartsLibrary()
if( ok && part.Definition.GateSymbols.size() != 0 )
{
( *m_plugin )->SaveSymbol( m_libraryFileName.GetFullPath(), kiPart );
LIB_SYMBOL* loadedPart =
( *m_plugin )->LoadSymbol( m_libraryFileName.GetFullPath(), kiPart->GetName() );
m_partMap.insert( { partID, loadedPart } );
m_loadedSymbols.push_back( kiPart );
m_partMap.insert( { partID, kiPart } );
}
else
{
@ -636,7 +630,7 @@ void CADSTAR_SCH_ARCHIVE_LOADER::loadSchematicSymbolInstances()
kiPart->GetReferenceField().SetText( "#PWR" );
kiPart->GetReferenceField().SetVisible( false );
( *m_plugin )->SaveSymbol( m_libraryFileName.GetFullPath(), kiPart );
m_loadedSymbols.push_back( kiPart );
m_powerSymLibMap.insert( { libPartName, kiPart } );
}
else
@ -1749,8 +1743,10 @@ SCH_SYMBOL* CADSTAR_SCH_ARCHIVE_LOADER::loadSchematicSymbol( const SYMBOL& aCads
const LIB_SYMBOL& aKiCadPart,
EDA_ANGLE& aComponentOrientation )
{
LIB_ID libId( m_libraryFileName.GetName(), aKiCadPart.GetName() );
int unit = getKiCadUnitNumberFromGate( aCadstarSymbol.GateID );
LIB_ID libId;
libId.SetLibItemName( aKiCadPart.GetName() );
int unit = getKiCadUnitNumberFromGate( aCadstarSymbol.GateID );
SCH_SHEET_PATH sheetpath;
SCH_SHEET* kiSheet = m_sheetMap.at( aCadstarSymbol.LayerID );

View File

@ -62,7 +62,6 @@ public:
{
m_schematic = nullptr;
m_rootSheet = nullptr;
m_plugin = nullptr;
m_designCenter.x = 0;
m_designCenter.y = 0;
m_reporter = aReporter;
@ -74,13 +73,14 @@ public:
{
}
const std::vector<LIB_SYMBOL*>& GetLoadedSymbols() const { return m_loadedSymbols; }
/**
* @brief Loads a CADSTAR PCB Archive file into the KiCad BOARD object given
* @brief Loads a CADSTAR Schematic Archive file into the KiCad SCHEMATIC object given
* @param aSchematic Schematic to add the design onto
* @param aRootSheet Root sheet to add the design onto
*/
void Load( SCHEMATIC* aSchematic, SCH_SHEET* aRootSheet,
SCH_PLUGIN::SCH_PLUGIN_RELEASER* aSchPlugin, const wxFileName& aLibraryFileName );
void Load( SCHEMATIC* aSchematic, SCH_SHEET* aRootSheet );
private:
@ -96,8 +96,6 @@ private:
REPORTER* m_reporter;
SCHEMATIC* m_schematic;
SCH_SHEET* m_rootSheet;
SCH_PLUGIN::SCH_PLUGIN_RELEASER* m_plugin;
wxFileName m_libraryFileName;
wxPoint m_designCenter; ///< Used for calculating the required
///< offset to apply to the Cadstar design
///< so that it fits in KiCad canvas
@ -105,6 +103,7 @@ private:
std::map<BLOCK_PIN_ID, SCH_HIERLABEL*>
m_sheetPinMap; ///< Map between Cadstar and KiCad Sheets Pins
std::map<PART_ID, LIB_SYMBOL*> m_partMap; ///< Map between Cadstar and KiCad Parts
std::vector<LIB_SYMBOL*> m_loadedSymbols; ///< Loaded symbols so far
std::map<PART_GATE_ID, SYMDEF_ID> m_partSymbolsMap; ///< Map holding the symbols loaded so far
/// for a particular PART_ID and GATE_ID
std::map<PART_ID, TERMINAL_TO_PINNUM_MAP> m_pinNumsMap; ///< Map of pin numbers in CADSTAR parts

View File

@ -31,6 +31,7 @@
#include <sch_screen.h>
#include <sch_sheet.h>
#include <schematic.h>
#include <sch_plugins/kicad/sch_sexpr_plugin.h>
#include <wildcards_and_files_ext.h>
@ -61,7 +62,7 @@ int CADSTAR_SCH_ARCHIVE_PLUGIN::GetModifyHash() const
SCH_SHEET* CADSTAR_SCH_ARCHIVE_PLUGIN::Load( const wxString& aFileName, SCHEMATIC* aSchematic,
SCH_SHEET* aAppendToMe, const STRING_UTF8_MAP* aProperties )
{
wxASSERT( !aFileName || aSchematic != NULL );
wxCHECK( !aFileName.IsEmpty() && aSchematic, nullptr );
SCH_SHEET* rootSheet = nullptr;
@ -78,7 +79,6 @@ SCH_SHEET* CADSTAR_SCH_ARCHIVE_PLUGIN::Load( const wxString& aFileName, SCHEMATI
aSchematic->SetRoot( rootSheet );
}
if( !rootSheet->GetScreen() )
{
SCH_SCREEN* screen = new SCH_SCREEN( aSchematic );
@ -86,9 +86,13 @@ SCH_SHEET* CADSTAR_SCH_ARCHIVE_PLUGIN::Load( const wxString& aFileName, SCHEMATI
rootSheet->SetScreen( screen );
}
CADSTAR_SCH_ARCHIVE_LOADER csaLoader( aFileName, m_reporter, m_progressReporter );
csaLoader.Load( aSchematic, rootSheet );
// SAVE SYMBOLS TO PROJECT LIBRARY:
SYMBOL_LIB_TABLE* libTable = aSchematic->Prj().SchSymbolLibTable();
wxCHECK_MSG( libTable, NULL, "Could not load symbol lib table." );
wxCHECK_MSG( libTable, nullptr, "Could not load symbol lib table." );
// Lets come up with a nice library name
wxString libName = aSchematic->Prj().GetProjectName();
@ -104,8 +108,8 @@ SCH_SHEET* CADSTAR_SCH_ARCHIVE_PLUGIN::Load( const wxString& aFileName, SCHEMATI
libName = LIB_ID::FixIllegalChars( libName, true );
wxFileName libFileName(
aSchematic->Prj().GetProjectPath(), libName, KiCadSymbolLibFileExtension );
wxFileName libFileName( aSchematic->Prj().GetProjectPath(), libName,
KiCadSymbolLibFileExtension );
SCH_PLUGIN::SCH_PLUGIN_RELEASER sch_plugin;
sch_plugin.set( SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_KICAD ) );
@ -121,8 +125,8 @@ SCH_SHEET* CADSTAR_SCH_ARCHIVE_PLUGIN::Load( const wxString& aFileName, SCHEMATI
new SYMBOL_LIB_TABLE_ROW( libName, libTableUri, wxString( "KiCad" ) ) );
// Save project symbol library table.
wxFileName fn(
aSchematic->Prj().GetProjectPath(), SYMBOL_LIB_TABLE::GetSymbolLibTableFileName() );
wxFileName fn( aSchematic->Prj().GetProjectPath(),
SYMBOL_LIB_TABLE::GetSymbolLibTableFileName() );
// So output formatter goes out of scope and closes the file before reloading.
{
@ -135,11 +139,31 @@ SCH_SHEET* CADSTAR_SCH_ARCHIVE_PLUGIN::Load( const wxString& aFileName, SCHEMATI
aSchematic->Prj().SchSymbolLibTable();
}
CADSTAR_SCH_ARCHIVE_LOADER csaFile( aFileName, m_reporter, m_progressReporter );
csaFile.Load( aSchematic, rootSheet, &sch_plugin, libFileName );
// set properties to prevent save file on every symbol save
STRING_UTF8_MAP properties;
properties.emplace( SCH_SEXPR_PLUGIN::PropBuffering, "" );
for( LIB_SYMBOL* const& symbol : csaLoader.GetLoadedSymbols() )
sch_plugin->SaveSymbol( libFileName.GetFullPath(), symbol, &properties );
sch_plugin->SaveLibrary( libFileName.GetFullPath() );
// Link up all symbols in the design to the newly created library
for( SCH_SHEET_PATH& sheet : aSchematic->GetSheets() )
{
for( SCH_ITEM* item : sheet.LastScreen()->Items().OfType( SCH_SYMBOL_T ) )
{
SCH_SYMBOL* sym = static_cast<SCH_SYMBOL*>( item );
if( sym->GetLibId().IsLegacy() )
{
LIB_ID libid = sym->GetLibId();
libid.SetLibNickname( libName );
sym->SetLibId( libid );
}
};
}
// Need to fix up junctions after import to retain connectivity
aSchematic->FixupJunctions();