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_libentry.h>
31 #include <class_library.h>
33 #include <lib_manager.h>
34 #include <wx/filename.h>
35 
36 
38 {
39  wxString msg;
40  wxString libName = getTargetLib();
41 
42  if( !m_libMgr->LibraryExists( libName ) )
43  {
44  libName = SelectLibraryFromList();
45 
46  if( !m_libMgr->LibraryExists( libName ) )
47  return;
48  }
49 
50  wxFileDialog dlg( this, _( "Import Symbol" ), m_mruPath, wxEmptyString,
51  SchematicLibraryFileWildcard(), wxFD_OPEN | wxFD_FILE_MUST_EXIST );
52 
53  if( dlg.ShowModal() == wxID_CANCEL )
54  return;
55 
56  wxFileName fn = dlg.GetPath();
57  m_mruPath = fn.GetPath();
58 
59  wxArrayString symbols;
60  SCH_PLUGIN::SCH_PLUGIN_RELEASER pi( SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_LEGACY ) );
61 
62  // TODO dialog to select the part to be imported if there is more than one
63  try
64  {
65  pi->EnumerateSymbolLib( symbols, fn.GetFullPath() );
66  }
67  catch( const IO_ERROR& ioe )
68  {
69  msg.Printf( _( "Cannot import symbol library \"%s\"." ), fn.GetFullPath() );
70  DisplayErrorMessage( this, msg, ioe.What() );
71  return;
72  }
73 
74  if( symbols.empty() )
75  {
76  msg.Printf( _( "Symbol library file \"%s\" is empty." ), fn.GetFullPath() );
77  DisplayError( this, msg );
78  return;
79  }
80 
81  wxString symbolName = symbols[0];
82  LIB_PART* entry = pi->LoadSymbol( fn.GetFullPath(), symbolName );
83 
84  if( m_libMgr->PartExists( symbols[0], libName ) )
85  {
86  msg.Printf( _( "Symbol \"%s\" already exists in library \"%s\"." ), symbolName, libName );
87  DisplayError( this, msg );
88  return;
89  }
90 
91  m_libMgr->UpdatePart( entry, libName );
92  SyncLibraries( false );
93  LoadPart( symbolName, libName, 1 );
94 }
95 
96 
98 {
99  wxString msg, title;
100  LIB_PART* part = getTargetPart();
101 
102  if( !part )
103  {
104  DisplayError( this, _( "There is no symbol selected to save." ) );
105  return;
106  }
107 
108  wxFileName fn;
109 
110  fn.SetName( part->GetName().Lower() );
111  fn.SetExt( SchematicLibraryFileExtension );
112 
113  wxFileDialog dlg( this, _( "Export Symbol" ), m_mruPath, fn.GetFullName(),
114  SchematicLibraryFileWildcard(), wxFD_SAVE );
115 
116  if( dlg.ShowModal() == wxID_CANCEL )
117  return;
118 
119  fn = dlg.GetPath();
120  fn.MakeAbsolute();
121 
122  LIB_PART* old_part = NULL;
123 
124  SCH_PLUGIN::SCH_PLUGIN_RELEASER pi( SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_LEGACY ) );
125 
126  if( fn.FileExists() )
127  {
128  try
129  {
130  old_part = pi->LoadSymbol( fn.GetFullPath(), part->GetName() );
131  }
132  catch( const IO_ERROR& ioe )
133  {
134  msg.Printf( _( "Error occurred attempting to load symbol library file \"%s\"" ),
135  fn.GetFullPath() );
136  DisplayErrorMessage( this, msg, ioe.What() );
137  return;
138  }
139 
140  if( old_part )
141  {
142  msg.Printf( _( "Symbol \"%s\" already exists in \"%s\"." ),
143  part->GetName(),
144  fn.GetFullName() );
145 
146  KIDIALOG errorDlg( this, msg, _( "Confirmation" ), wxOK | wxCANCEL | wxICON_WARNING );
147  errorDlg.SetOKLabel( _( "Overwrite" ) );
148  errorDlg.DoNotShowCheckbox( __FILE__, __LINE__ );
149 
150  if( errorDlg.ShowModal() == wxID_CANCEL )
151  return;
152  }
153  }
154 
155  if( fn.Exists() && !fn.IsDirWritable() )
156  {
157  msg.Printf( _( "Write permissions are required to save library \"%s\"." ),
158  fn.GetFullPath() );
159  DisplayError( this, msg );
160  return;
161  }
162 
163  try
164  {
165  if( !fn.FileExists() )
166  pi->CreateSymbolLib( fn.GetFullPath() );
167 
168  pi->SaveSymbol( fn.GetFullPath(), new LIB_PART( *part ) );
169  }
170  catch( const IO_ERROR& ioe )
171  {
172  msg.Printf( _( "Failed to create symbol library file \"%s\"" ), fn.GetFullPath() );
173  DisplayErrorMessage( this, msg, ioe.What() );
174  msg.Printf( _( "Error creating symbol library \"%s\"" ), fn.GetFullName() );
175  SetStatusText( msg );
176  return;
177  }
178 
179  m_mruPath = fn.GetPath();
180 
181  msg.Printf( _( "Symbol \"%s\" saved in library \"%s\"" ), part->GetName(), fn.GetFullPath() );
182  SetStatusText( msg );
183 
184  // See if the user wants it added to a library table (global or project)
185  SYMBOL_LIB_TABLE* libTable = selectSymLibTable( true );
186 
187  if( libTable )
188  {
189  if( !m_libMgr->AddLibrary( fn.GetFullPath(), libTable ) )
190  {
191  DisplayError( this, _( "Could not open the library file." ) );
192  return;
193  }
194 
195  bool globalTable = ( libTable == &SYMBOL_LIB_TABLE::GetGlobalLibTable() );
196  saveSymbolLibTables( globalTable, !globalTable );
197  }
198 }
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:239
static SYMBOL_LIB_TABLE & GetGlobalLibTable()
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:252
wxString GetName() const override
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)
LIB_MANAGER * m_libMgr
wxString SelectLibraryFromList()
Display a list of loaded libraries in the symbol library and allows the user to select a library.
wxString SchematicLibraryFileWildcard()
bool AddLibrary(const wxString &aFilePath, SYMBOL_LIB_TABLE *aTable)
Adds an existing library.
Definition: lib_manager.h:146
#define NULL
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:33
const std::string SchematicLibraryFileExtension
Class LIB_PIN definition.
Define a library symbol object.
Definition of file extensions used in Kicad.
bool saveSymbolLibTables(bool aGlobal, bool aProject)
Saves Symbol Library Tables to disk.
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).
void ExportPart()
Definition: lib_export.cpp:97
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:37
Helper object to release a SCH_PLUGIN in the context of a potential thrown exception through its dest...
Definition: sch_io_mgr.h:516
void SyncLibraries(bool aShowProgress)
Synchronize the library manager to the symbol library table, and then the symbol tree to the library ...
#define _(s)
Definition: 3d_actions.cpp:33
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...