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 <roberto.fer.bau@gmail.com>
5  * Copyright (C) 2020 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, "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  }
79 
80 
81  if( !rootSheet->GetScreen() )
82  {
83  SCH_SCREEN* screen = new SCH_SCREEN( aSchematic );
84  screen->SetFileName( aFileName );
85  rootSheet->SetScreen( screen );
86  }
87 
88  SYMBOL_LIB_TABLE* libTable = aSchematic->Prj().SchSymbolLibTable();
89 
90  wxCHECK_MSG( libTable, NULL, "Could not load symbol lib table." );
91 
92  // Lets come up with a nice library name
93  wxString libName = aSchematic->Prj().GetProjectName();
94 
95  if( libName.IsEmpty() )
96  {
97  wxFileName fn( rootSheet->GetFileName() );
98  libName = fn.GetName();
99  }
100 
101  if( libName.IsEmpty() )
102  libName = "noname";
103 
104  libName = LIB_ID::FixIllegalChars( libName, true );
105 
106  wxFileName libFileName(
107  aSchematic->Prj().GetProjectPath(), libName, KiCadSymbolLibFileExtension );
108 
110  sch_plugin.set( SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_KICAD ) );
111 
112  if( !libTable->HasLibrary( libName ) )
113  {
114  // Create a new empty symbol library.
115  sch_plugin->CreateSymbolLib( libFileName.GetFullPath() );
116  wxString libTableUri = "${KIPRJMOD}/" + libFileName.GetFullName();
117 
118  // Add the new library to the project symbol library table.
119  libTable->InsertRow(
120  new SYMBOL_LIB_TABLE_ROW( libName, libTableUri, wxString( "KiCad" ) ) );
121 
122  // Save project symbol library table.
123  wxFileName fn(
125 
126  // So output formatter goes out of scope and closes the file before reloading.
127  {
128  FILE_OUTPUTFORMATTER formatter( fn.GetFullPath() );
129  libTable->Format( &formatter, 0 );
130  }
131 
132  // Relaod the symbol library table.
133  aSchematic->Prj().SetElem( PROJECT::ELEM_SYMBOL_LIB_TABLE, NULL );
134  aSchematic->Prj().SchSymbolLibTable();
135  }
136 
138  csaFile.Load( aSchematic, rootSheet, &sch_plugin, libFileName );
139 
140  sch_plugin->SaveLibrary( libFileName.GetFullPath() );
141 
142  return rootSheet;
143 }
144 
145 
146 bool CADSTAR_SCH_ARCHIVE_PLUGIN::CheckHeader( const wxString& aFileName )
147 {
148  // TODO: write a parser for the cpa header. For now assume it is valid
149  // and throw exceptions when parsing
150  return true;
151 }
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:321
void SetScreen(SCH_SCREEN *aScreen)
Set the SCH_SCREEN associated with this sheet to aScreen.
Definition: sch_sheet.cpp:156
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:103
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
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:315
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