KiCad PCB EDA Suite
lib_export.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) 2016 Jean-Pierre Charras, jp.charras at wanadoo.fr
5  * Copyright (C) 2008 Wayne Stambaugh <stambaughw@gmail.com>
6  * Copyright (C) 2004-2019 KiCad Developers, see AUTHORS.txt for contributors.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25 
26 #include <fctsys.h>
27 #include <confirm.h>
28 #include <symbol_lib_table.h>
29 #include <lib_edit_frame.h>
30 #include <class_library.h>
32 #include <lib_manager.h>
33 #include <wx/filename.h>
34 
35 
37 {
38  wxString msg;
39  wxString libName = getTargetLib();
40 
41  if( !m_libMgr->LibraryExists( libName ) )
42  {
43  libName = SelectLibraryFromList();
44 
45  if( !m_libMgr->LibraryExists( libName ) )
46  return;
47  }
48 
49  wxFileDialog dlg( this, _( "Import Symbol" ), m_mruPath, wxEmptyString,
50  SchematicLibraryFileWildcard(), wxFD_OPEN | wxFD_FILE_MUST_EXIST );
51 
52  if( dlg.ShowModal() == wxID_CANCEL )
53  return;
54 
55  wxFileName fn = dlg.GetPath();
56  m_mruPath = fn.GetPath();
57 
58  wxArrayString symbols;
59  SCH_PLUGIN::SCH_PLUGIN_RELEASER pi( SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_LEGACY ) );
60 
61  // TODO dialog to select the part to be imported if there is more than one
62  try
63  {
64  pi->EnumerateSymbolLib( symbols, fn.GetFullPath() );
65  }
66  catch( const IO_ERROR& ioe )
67  {
68  msg.Printf( _( "Cannot import symbol library \"%s\"." ), fn.GetFullPath() );
69  DisplayErrorMessage( this, msg, ioe.What() );
70  return;
71  }
72 
73  if( symbols.empty() )
74  {
75  msg.Printf( _( "Symbol library file \"%s\" is empty." ), fn.GetFullPath() );
76  DisplayError( this, msg );
77  return;
78  }
79 
80  wxString symbolName = symbols[0];
81  LIB_ALIAS* entry = pi->LoadSymbol( fn.GetFullPath(), symbolName );
82 
83  if( m_libMgr->PartExists( symbols[0], libName ) )
84  {
85  msg.Printf( _( "Symbol \"%s\" already exists in library \"%s\"." ), symbolName, libName );
86  DisplayError( this, msg );
87  return;
88  }
89 
90  m_libMgr->UpdatePart( entry->GetPart(), libName );
91  SyncLibraries( false );
92  LoadPart( symbolName, libName, 1 );
93 }
94 
95 
97 {
98  wxString msg, title;
99  LIB_PART* part = getTargetPart();
100 
101  if( !part )
102  {
103  DisplayError( this, _( "There is no symbol selected to save." ) );
104  return;
105  }
106 
107  wxFileName fn;
108 
109  fn.SetName( part->GetName().Lower() );
110  fn.SetExt( SchematicLibraryFileExtension );
111 
112  wxFileDialog dlg( this, _( "Export Symbol" ), m_mruPath, fn.GetFullName(),
113  SchematicLibraryFileWildcard(), wxFD_SAVE );
114 
115  if( dlg.ShowModal() == wxID_CANCEL )
116  return;
117 
118  fn = dlg.GetPath();
119  fn.MakeAbsolute();
120 
121  LIB_PART* old_part = NULL;
122 
123  SCH_PLUGIN::SCH_PLUGIN_RELEASER pi( SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_LEGACY ) );
124 
125  if( fn.FileExists() )
126  {
127  try
128  {
129  LIB_ALIAS* alias = pi->LoadSymbol( fn.GetFullPath(), part->GetName() );
130 
131  if( alias )
132  old_part = alias->GetPart();
133  }
134  catch( const IO_ERROR& ioe )
135  {
136  msg.Printf( _( "Error occurred attempting to load symbol library file \"%s\"" ),
137  fn.GetFullPath() );
138  DisplayErrorMessage( this, msg, ioe.What() );
139  return;
140  }
141 
142  if( old_part )
143  {
144  msg.Printf( _( "Symbol \"%s\" already exists in \"%s\"." ),
145  part->GetName(),
146  fn.GetFullName() );
147 
148  KIDIALOG errorDlg( this, msg, _( "Confirmation" ), wxOK | wxCANCEL | wxICON_WARNING );
149  errorDlg.SetOKLabel( _( "Overwrite" ) );
150  errorDlg.DoNotShowCheckbox( __FILE__, __LINE__ );
151 
152  if( errorDlg.ShowModal() == wxID_CANCEL )
153  return;
154  }
155  }
156 
157  if( fn.Exists() && !fn.IsDirWritable() )
158  {
159  msg.Printf( _( "Write permissions are required to save library \"%s\"." ),
160  fn.GetFullPath() );
161  DisplayError( this, msg );
162  return;
163  }
164 
165  try
166  {
167  if( !fn.FileExists() )
168  pi->CreateSymbolLib( fn.GetFullPath() );
169 
170  pi->SaveSymbol( fn.GetFullPath(), new LIB_PART( *part ) );
171  }
172  catch( const IO_ERROR& ioe )
173  {
174  msg.Printf( _( "Failed to create symbol library file \"%s\"" ), fn.GetFullPath() );
175  DisplayErrorMessage( this, msg, ioe.What() );
176  msg.Printf( _( "Error creating symbol library \"%s\"" ), fn.GetFullName() );
177  SetStatusText( msg );
178  return;
179  }
180 
181  m_mruPath = fn.GetPath();
182 
183  msg.Printf( _( "Symbol \"%s\" saved in library \"%s\"" ), part->GetName(), fn.GetFullPath() );
184  SetStatusText( msg );
185 
186  // See if the user wants it added to a library table (global or project)
187  SYMBOL_LIB_TABLE* libTable = selectSymLibTable( true );
188 
189  if( libTable )
190  {
191  if( !m_libMgr->AddLibrary( fn.GetFullPath(), libTable ) )
192  {
193  DisplayError( this, _( "Could not open the library file." ) );
194  return;
195  }
196 
197  bool globalTable = ( libTable == &SYMBOL_LIB_TABLE::GetGlobalLibTable() );
198  saveSymbolLibTables( globalTable, !globalTable );
199  }
200 }
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:236
static SYMBOL_LIB_TABLE & GetGlobalLibTable()
Part library alias object definition.
void DoNotShowCheckbox(wxString file, int line)
Shows the 'do not show again' checkbox
Definition: confirm.cpp:53
wxString m_mruPath
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition: confirm.cpp:249
Helper class to create more flexible dialogs, including 'do not show again' checkbox handling.
Definition: confirm.h:44
This file is part of the common library.
void LoadPart(const wxString &aLibrary, const wxString &aPart, int Unit)
Definition: libedit.cpp:729
LIB_MANAGER * m_libMgr
wxString SelectLibraryFromList()
Display a list of loaded libraries in the symbol library and allows the user to select a library.
Definition: libedit.cpp:74
wxString SchematicLibraryFileWildcard()
bool AddLibrary(const wxString &aFilePath, SYMBOL_LIB_TABLE *aTable)
Adds an existing library.
Definition: lib_manager.h:146
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:33
const std::string SchematicLibraryFileExtension
Define a library symbol object.
Definition of file extensions used in Kicad.
bool saveSymbolLibTables(bool aGlobal, bool aProject)
Saves Symbol Library Tables to disk.
LIB_PART * GetPart() const
Get the shared LIB_PART.
bool PartExists(const wxString &aAlias, const wxString &aLibrary) const
Returns true if part with a specific alias exists in library (either original one or buffered).
#define _(s)
void ExportPart()
Definition: lib_export.cpp:96
bool UpdatePart(LIB_PART *aPart, const wxString &aLibrary)
Updates the part buffer with a new version of the part.
void ImportPart()
Definition: lib_export.cpp:36
Helper object to release a SCH_PLUGIN in the context of a potential thrown exception through its dest...
Definition: sch_io_mgr.h:520
void SyncLibraries(bool aShowProgress)
Synchronize the library manager to the symbol library table, and then the symbol tree to the library ...
const wxString & GetName() const
bool LibraryExists(const wxString &aLibrary, bool aCheckEnabled=false) const
Returns true if library exists.
LIB_PART * getTargetPart() const
Returns currently edited part.
int ShowModal() override
Definition: confirm.cpp:95
SYMBOL_LIB_TABLE * selectSymLibTable(bool aOptional=false)
Displays a dialog asking the user to select a symbol library table.
Definition for part library class.
Struct IO_ERROR is a class used to hold an error message and may be used when throwing exceptions con...
Definition: ki_exception.h:76
wxString getTargetLib() const
Returns either the library selected in the component tree, if context menu is active or the library t...