KiCad PCB EDA Suite
cadstar_sch_archive_plugin.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2020 Roberto Fernandez Bautista <[email protected]>
5 * Copyright (C) 2020-2021 KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
29
30#include <string_utf8_map.h>
31#include <sch_screen.h>
32#include <sch_sheet.h>
33#include <schematic.h>
35
36
38{
39 return wxT( "CADSTAR Schematic Archive" );
40}
41
42
44{
45 return wxT( "csa" );
46}
47
48
50{
51 return wxT( "lib" );
52}
53
54
56{
57 return 0;
58}
59
60
61SCH_SHEET* CADSTAR_SCH_ARCHIVE_PLUGIN::Load( const wxString& aFileName, SCHEMATIC* aSchematic,
62 SCH_SHEET* aAppendToMe, const STRING_UTF8_MAP* aProperties )
63{
64 wxASSERT( !aFileName || aSchematic != NULL );
65
66 SCH_SHEET* rootSheet = nullptr;
67
68
69 if( aAppendToMe )
70 {
71 wxCHECK_MSG( aSchematic->IsValid(), nullptr, "Can't append to a schematic with no root!" );
72 rootSheet = &aSchematic->Root();
73 }
74 else
75 {
76 rootSheet = new SCH_SHEET( aSchematic );
77 rootSheet->SetFileName( aFileName );
78 aSchematic->SetRoot( rootSheet );
79 }
80
81
82 if( !rootSheet->GetScreen() )
83 {
84 SCH_SCREEN* screen = new SCH_SCREEN( aSchematic );
85 screen->SetFileName( aFileName );
86 rootSheet->SetScreen( screen );
87 }
88
89 SYMBOL_LIB_TABLE* libTable = aSchematic->Prj().SchSymbolLibTable();
90
91 wxCHECK_MSG( libTable, NULL, "Could not load symbol lib table." );
92
93 // Lets come up with a nice library name
94 wxString libName = aSchematic->Prj().GetProjectName();
95
96 if( libName.IsEmpty() )
97 {
98 wxFileName fn( rootSheet->GetFileName() );
99 libName = fn.GetName();
100 }
101
102 if( libName.IsEmpty() )
103 libName = "noname";
104
105 libName = LIB_ID::FixIllegalChars( libName, true );
106
107 wxFileName libFileName(
108 aSchematic->Prj().GetProjectPath(), libName, KiCadSymbolLibFileExtension );
109
111 sch_plugin.set( SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_KICAD ) );
112
113 if( !libTable->HasLibrary( libName ) )
114 {
115 // Create a new empty symbol library.
116 sch_plugin->CreateSymbolLib( libFileName.GetFullPath() );
117 wxString libTableUri = "${KIPRJMOD}/" + libFileName.GetFullName();
118
119 // Add the new library to the project symbol library table.
120 libTable->InsertRow(
121 new SYMBOL_LIB_TABLE_ROW( libName, libTableUri, wxString( "KiCad" ) ) );
122
123 // Save project symbol library table.
124 wxFileName fn(
126
127 // So output formatter goes out of scope and closes the file before reloading.
128 {
129 FILE_OUTPUTFORMATTER formatter( fn.GetFullPath() );
130 libTable->Format( &formatter, 0 );
131 }
132
133 // Relaod the symbol library table.
134 aSchematic->Prj().SetElem( PROJECT::ELEM_SYMBOL_LIB_TABLE, NULL );
135 aSchematic->Prj().SchSymbolLibTable();
136 }
137
139 csaFile.Load( aSchematic, rootSheet, &sch_plugin, libFileName );
140
141 sch_plugin->SaveLibrary( libFileName.GetFullPath() );
142
143 return rootSheet;
144}
145
146
147bool CADSTAR_SCH_ARCHIVE_PLUGIN::CheckHeader( const wxString& aFileName )
148{
149 // TODO: write a parser for the cpa header. For now assume it is valid
150 // and throw exceptions when parsing
151 return true;
152}
Loads a csa file into a KiCad SCHEMATIC object.
void Load(SCHEMATIC *aSchematic, SCH_SHEET *aRootSheet, SCH_PLUGIN::SCH_PLUGIN_RELEASER *aSchPlugin, const wxFileName &aLibraryFileName)
Loads a CADSTAR PCB Archive file into the KiCad BOARD object given.
bool CheckHeader(const wxString &aFileName) override
Return true if the first line in aFileName begins with the expected header.
const wxString GetLibraryFileExtension() const override
Return the library file extension for the SCH_PLUGIN object.
const wxString GetFileExtension() const override
Return the file extension for the SCH_PLUGIN.
const wxString GetName() const override
Return a brief hard coded name for this SCH_PLUGIN.
int GetModifyHash() const override
Return the modification hash from the library cache.
SCH_SHEET * Load(const wxString &aFileName, SCHEMATIC *aSchematic, SCH_SHEET *aAppendToMe=nullptr, const STRING_UTF8_MAP *aProperties=nullptr) override
Load information from some input file format that this SCH_PLUGIN implementation knows about,...
Used for text file output.
Definition: richio.h:457
static UTF8 FixIllegalChars(const UTF8 &aLibItemName, bool aLib)
Replace illegal LIB_ID item name characters with underscores '_'.
Definition: lib_id.cpp:190
bool HasLibrary(const wxString &aNickname, bool aCheckEnabled=false) const
Test for the existence of aNickname in the library table.
bool InsertRow(LIB_TABLE_ROW *aRow, bool doReplace=false)
Adds aRow if it does not already exist or if doReplace is true.
virtual const wxString GetProjectPath() const
Return the full path of the project.
Definition: project.cpp:126
virtual const wxString GetProjectName() const
Return the short name of the project.
Definition: project.cpp:132
virtual void SetElem(ELEM_T aIndex, _ELEM *aElem)
Definition: project.cpp:294
@ ELEM_SYMBOL_LIB_TABLE
Definition: project.h:211
Holds all the data relating to one schematic.
Definition: schematic.h:61
void SetRoot(SCH_SHEET *aRootSheet)
Initialize the schematic with a new root sheet.
Definition: schematic.cpp:104
bool IsValid() const
A simple test if the schematic is loaded, not a complete one.
Definition: schematic.h:107
SCH_SHEET & Root() const
Definition: schematic.h:91
PROJECT & Prj() const override
Return a reference to the project this schematic is part of.
Definition: schematic.h:76
Helper object to release a SCH_PLUGIN in the context of a potential thrown exception through its dest...
Definition: sch_io_mgr.h:535
void set(SCH_PLUGIN *aPlugin)
Definition: sch_io_mgr.h:562
virtual void SaveLibrary(const wxString &aFileName, const STRING_UTF8_MAP *aProperties=nullptr)
Definition: sch_plugin.cpp:45
virtual void CreateSymbolLib(const wxString &aLibraryPath, const STRING_UTF8_MAP *aProperties=nullptr)
Create a new empty symbol library at aLibraryPath.
Definition: sch_plugin.cpp:110
void SetFileName(const wxString &aFileName)
Set the file name for this screen to aFileName.
Definition: sch_screen.cpp:110
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition: sch_sheet.h:55
void SetFileName(const wxString &aFilename)
Definition: sch_sheet.h:306
wxString GetFileName() const
Return the filename corresponding to this sheet.
Definition: sch_sheet.h:300
SCH_SCREEN * GetScreen() const
Definition: sch_sheet.h:104
void SetScreen(SCH_SCREEN *aScreen)
Set the SCH_SCREEN associated with this sheet to aScreen.
Definition: sch_sheet.cpp:162
A name/value tuple with unique names and optional values.
Hold a record identifying a symbol library accessed by the appropriate symbol library SCH_PLUGIN obje...
static const wxString & GetSymbolLibTableFileName()
virtual void Format(OUTPUTFORMATTER *aOutput, int aIndentLevel) const override
Generate the table in s-expression format to aOutput with an indentation level of aIndentLevel.
const std::string KiCadSymbolLibFileExtension
Definition of file extensions used in Kicad.