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-2018 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 <sch_draw_panel.h>
28 #include <confirm.h>
29 #include <symbol_lib_table.h>
30 #include <general.h>
31 #include <lib_edit_frame.h>
32 #include <class_library.h>
34 #include <eeschema_id.h>
35 #include <lib_manager.h>
36 #include <wx/filename.h>
37 
38 
39 void LIB_EDIT_FRAME::OnImportPart( wxCommandEvent& event )
40 {
41  wxString msg;
42  m_lastDrawItem = NULL;
43  wxString libName = getTargetLib();
44 
45  if( !m_libMgr->LibraryExists( libName ) )
46  {
47  libName = SelectLibraryFromList();
48 
49  if( !m_libMgr->LibraryExists( libName ) )
50  return;
51  }
52 
53  wxFileDialog dlg( this, _( "Import Symbol" ), m_mruPath,
54  wxEmptyString, SchematicLibraryFileWildcard(),
55  wxFD_OPEN | wxFD_FILE_MUST_EXIST );
56 
57  if( dlg.ShowModal() == wxID_CANCEL )
58  return;
59 
60  wxFileName fn = dlg.GetPath();
61  m_mruPath = fn.GetPath();
62 
63  wxArrayString symbols;
64  SCH_PLUGIN::SCH_PLUGIN_RELEASER pi( SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_LEGACY ) );
65 
66  // TODO dialog to select the part to be imported if there is more than one
67  try
68  {
69  pi->EnumerateSymbolLib( symbols, fn.GetFullPath() );
70  }
71  catch( const IO_ERROR& ioe )
72  {
73  msg.Printf( _( "Cannot import symbol library \"%s\"." ), fn.GetFullPath() );
74  DisplayErrorMessage( this, msg, ioe.What() );
75  return;
76  }
77 
78  if( symbols.empty() )
79  {
80  msg.Printf( _( "Symbol library file \"%s\" is empty." ), fn.GetFullPath() );
81  DisplayError( this, msg );
82  return;
83  }
84 
85  wxString symbolName = symbols[0];
86  LIB_ALIAS* entry = pi->LoadSymbol( fn.GetFullPath(), symbolName );
87 
88  if( m_libMgr->PartExists( symbols[0], libName ) )
89  {
90  msg.Printf( _( "Symbol \"%s\" already exists in library \"%s\"." ), symbolName, libName );
91  DisplayError( this, msg );
92  return;
93  }
94 
95  m_libMgr->UpdatePart( entry->GetPart(), libName );
96  SyncLibraries( false );
97  loadPart( symbolName, libName, 1 );
98 }
99 
100 
101 void LIB_EDIT_FRAME::OnExportPart( wxCommandEvent& event )
102 {
103  wxString msg, title;
104  LIB_PART* part = getTargetPart();
105 
106  if( !part )
107  {
108  DisplayError( this, _( "There is no symbol selected to save." ) );
109  return;
110  }
111 
112  wxFileName fn;
113 
114  fn.SetName( part->GetName().Lower() );
115  fn.SetExt( SchematicLibraryFileExtension );
116 
117  wxFileDialog dlg( this, _( "Export Symbol" ), m_mruPath, fn.GetFullName(),
118  SchematicLibraryFileWildcard(), wxFD_SAVE );
119 
120  if( dlg.ShowModal() == wxID_CANCEL )
121  return;
122 
123  fn = dlg.GetPath();
124  fn.MakeAbsolute();
125 
126  LIB_PART* old_part = NULL;
127 
128  SCH_PLUGIN::SCH_PLUGIN_RELEASER pi( SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_LEGACY ) );
129 
130  if( fn.FileExists() )
131  {
132  try
133  {
134  LIB_ALIAS* alias = pi->LoadSymbol( fn.GetFullPath(), part->GetName() );
135 
136  if( alias )
137  old_part = alias->GetPart();
138  }
139  catch( const IO_ERROR& ioe )
140  {
141  msg.Printf( _( "Error occurred attempting to load symbol library file \"%s\"" ),
142  fn.GetFullPath() );
143  DisplayErrorMessage( this, msg, ioe.What() );
144  return;
145  }
146 
147  if( old_part )
148  {
149  msg.Printf( _( "Symbol \"%s\" already exists in \"%s\"." ),
150  part->GetName(),
151  fn.GetFullName() );
152 
153  KIDIALOG errorDlg( this, msg, _( "Confirmation" ), wxOK | wxCANCEL | wxICON_WARNING );
154  errorDlg.SetOKLabel( _( "Overwrite" ) );
155  errorDlg.DoNotShowCheckbox( __FILE__, __LINE__ );
156 
157  if( errorDlg.ShowModal() == wxID_CANCEL )
158  return;
159  }
160  }
161 
162  if( fn.Exists() && !fn.IsDirWritable() )
163  {
164  msg.Printf( _( "Write permissions are required to save library \"%s\"." ), fn.GetFullPath() );
165  DisplayError( this, msg );
166  return;
167  }
168 
169  try
170  {
171  if( !fn.FileExists() )
172  pi->CreateSymbolLib( fn.GetFullPath() );
173 
174  pi->SaveSymbol( fn.GetFullPath(), new LIB_PART( *part ) );
175  }
176  catch( const IO_ERROR& ioe )
177  {
178  msg = _( "Failed to create symbol library file " ) + fn.GetFullPath();
179  DisplayErrorMessage( this, msg, ioe.What() );
180  msg.Printf( _( "Error creating symbol library \"%s\"" ), fn.GetFullName() );
181  SetStatusText( msg );
182  return;
183  }
184 
185  m_mruPath = fn.GetPath();
186  m_lastDrawItem = NULL;
187  SetDrawItem( NULL );
188 
189  msg.Printf( _( "Symbol \"%s\" saved in library \"%s\"" ), part->GetName(), fn.GetFullPath() );
190  SetStatusText( msg );
191 
192  // See if the user wants it added to a library table (global or project)
193  SYMBOL_LIB_TABLE* libTable = selectSymLibTable( true );
194 
195  if( libTable )
196  {
197  if( !m_libMgr->AddLibrary( fn.GetFullPath(), libTable ) )
198  {
199  DisplayError( this, _( "Could not open the library file." ) );
200  return;
201  }
202 
203  bool globalTable = ( libTable == &SYMBOL_LIB_TABLE::GetGlobalLibTable() );
204  saveSymbolLibTables( globalTable, !globalTable );
205  }
206 }
virtual void CreateSymbolLib(const wxString &aLibraryPath, const PROPERTIES *aProperties=NULL)
Create a new empty symbol library at aLibraryPath.
Definition: sch_plugin.cpp:126
static SYMBOL_LIB_TABLE & GetGlobalLibTable()
Part library alias object definition.
virtual LIB_ALIAS * LoadSymbol(const wxString &aLibraryPath, const wxString &aAliasName, const PROPERTIES *aProperties=NULL)
Load a LIB_ALIAS object having aAliasName from the aLibraryPath containing a library format that this...
Definition: sch_plugin.cpp:93
void DoNotShowCheckbox(wxString file, int line)
Shows the &#39;do not show again&#39; checkbox
Definition: confirm.cpp:60
wxString m_mruPath
Most recently used path.
void OnExportPart(wxCommandEvent &event)
Creates a new library and backup the current component in this library or exports the component of th...
Definition: lib_export.cpp:101
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Function DisplayErrorMessage displays an error message with aMessage.
Definition: confirm.cpp:259
wxString SelectLibraryFromList()
Dispaly a list of loaded libraries in the symbol library and allows the user to select a library...
Definition: selpart.cpp:76
Helper class to create more flexible dialogs, including &#39;do not show again&#39; checkbox handling...
Definition: confirm.h:44
This file is part of the common library.
void OnImportPart(wxCommandEvent &event)
Routine to read one part.
Definition: lib_export.cpp:39
LIB_MANAGER * m_libMgr
manager taking care of temporary modificatoins
virtual void EnumerateSymbolLib(wxArrayString &aAliasNameList, const wxString &aLibraryPath, const PROPERTIES *aProperties=NULL)
Populate a list of LIB_PART alias names contained within the library aLibraryPath.
Definition: sch_plugin.cpp:75
wxString SchematicLibraryFileWildcard()
bool AddLibrary(const wxString &aFilePath, SYMBOL_LIB_TABLE *aTable)
Adds an existing library.
Definition: lib_manager.h:99
void SetDrawItem(LIB_ITEM *drawItem)
const std::string SchematicLibraryFileExtension
Define a library symbol object.
The common library.
bool saveSymbolLibTables(bool aGlobal, bool aProject)
Saves Symbol Library Tables to disk.
static LIB_ITEM * m_lastDrawItem
bool UpdatePart(LIB_PART *aPart, const wxString &aLibrary)
Updates the part buffer with a new version of the part.
LIB_PART * GetPart() const
Get the shared LIB_PART.
Helper object to release a SCH_PLUGIN in the context of a potential thrown exception through its dest...
Definition: sch_io_mgr.h:523
void SyncLibraries(bool aShowProgress)
Synchronize the library manager to the symbol library table, and then the symbol tree to the library ...
wxString getTargetLib() const
Returns either the library selected in the component tree, if context menu is active or the library t...
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:33
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)...
bool LibraryExists(const wxString &aLibrary, bool aCheckEnabled=false) const
Returns true if library exists.
int ShowModal() override
Definition: confirm.cpp:102
const wxString & GetName() const
Definition of class LIB_EDIT_FRAME.
virtual void SaveSymbol(const wxString &aLibraryPath, const LIB_PART *aSymbol, const PROPERTIES *aProperties=NULL)
Write aSymbol to an existing library located at aLibraryPath.
Definition: sch_plugin.cpp:102
SYMBOL_LIB_TABLE * selectSymLibTable(bool aOptional=false)
Displays a dialog asking the user to select a symbol library table.
Definition for part library class.
void DisplayError(wxWindow *parent, const wxString &text, int displaytime)
Function DisplayError displays an error or warning message box with aMessage.
Definition: confirm.cpp:245
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
LIB_PART * getTargetPart() const
Returns currently edited part.
void loadPart(const wxString &aLibrary, const wxString &aPart, int Unit)
Definition: libedit.cpp:617