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 <properties.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 
61 SCH_SHEET* CADSTAR_SCH_ARCHIVE_PLUGIN::Load( const wxString& aFileName, SCHEMATIC* aSchematic,
62  SCH_SHEET* aAppendToMe, const PROPERTIES* 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, wxT( "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, wxT( "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 = wxT( "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 = wxT( "${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( wxT( "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 
147 bool 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 }
static const wxString & GetSymbolLibTableFileName()
const wxString GetFileExtension() const override
Return the file extension for the SCH_PLUGIN.
int GetModifyHash() const override
Return the modification hash from the library cache.
Hold a record identifying a symbol library accessed by the appropriate symbol library SCH_PLUGIN obje...
bool HasLibrary(const wxString &aNickname, bool aCheckEnabled=false) const
Test for the existence of aNickname in the library table.
static UTF8 FixIllegalChars(const UTF8 &aLibItemName, bool aLib)
Replace illegal LIB_ID item name characters with underscores '_'.
Definition: lib_id.cpp:189
Holds all the data relating to one schematic.
Definition: schematic.h:59
bool InsertRow(LIB_TABLE_ROW *aRow, bool doReplace=false)
Adds aRow if it does not already exist or if doReplace is true.
void SetFileName(const wxString &aFilename)
Definition: sch_sheet.h:320
void SetScreen(SCH_SCREEN *aScreen)
Set the SCH_SCREEN associated with this sheet to aScreen.
Definition: sch_sheet.cpp:175
bool IsValid() const
A simple test if the schematic is loaded, not a complete one.
Definition: schematic.h:108
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.
void set(SCH_PLUGIN *aPlugin)
Definition: sch_io_mgr.h:507
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.
A name/value tuple with unique names and optional values.
Definition: properties.h:33
SCH_SCREEN * GetScreen() const
Definition: sch_sheet.h:102
virtual const wxString GetProjectPath() const
Return the full path of the project.
Definition: project.cpp:122
Loads a csa file into a KiCad SCHEMATIC object.
virtual void SetElem(ELEM_T aIndex, _ELEM *aElem)
Definition: project.cpp:258
void SetRoot(SCH_SHEET *aRootSheet)
Initialize the schematic with a new root sheet.
Definition: schematic.cpp:104
SCH_SHEET * Load(const wxString &aFileName, SCHEMATIC *aSchematic, SCH_SHEET *aAppendToMe=nullptr, const PROPERTIES *aProperties=nullptr) override
Load information from some input file format that this SCH_PLUGIN implementation knows about,...
Definition of file extensions used in Kicad.
virtual void Format(OUTPUTFORMATTER *aOutput, int aIndentLevel) const override
Generate the table in s-expression format to aOutput with an indentation level of aIndentLevel.
wxString GetFileName() const
Return the filename corresponding to this sheet.
Definition: sch_sheet.h:314
const wxString GetName() const override
Return a brief hard coded name for this SCH_PLUGIN.
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition: sch_sheet.h:54
PROJECT & Prj() const override
Return a reference to the project this schematic is part of.
Definition: schematic.h:75
Helper object to release a SCH_PLUGIN in the context of a potential thrown exception through its dest...
Definition: sch_io_mgr.h:479
SCH_SHEET & Root() const
Definition: schematic.h:92
Used for text file output.
Definition: richio.h:456
virtual const wxString GetProjectName() const
Return the short name of the project.
Definition: project.cpp:128
void SetFileName(const wxString &aFileName)
Set the file name for this screen to aFileName.
Definition: sch_screen.cpp:110
const std::string KiCadSymbolLibFileExtension