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-2017 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 
32 #include <fctsys.h>
33 #include <class_drawpanel.h>
34 #include <confirm.h>
35 
36 #include <general.h>
37 #include <lib_edit_frame.h>
38 #include <class_library.h>
40 #include <eeschema_id.h>
41 #include <lib_manager.h>
42 
43 #include <wx/filename.h>
44 
45 
46 void LIB_EDIT_FRAME::OnImportPart( wxCommandEvent& event )
47 {
48  wxString msg;
49  m_lastDrawItem = NULL;
50  wxString libName = getTargetLib();
51 
52  if( !m_libMgr->LibraryExists( libName ) )
53  {
54  libName = SelectLibraryFromList();
55 
56  if( !m_libMgr->LibraryExists( libName ) )
57  return;
58  }
59 
60  wxFileDialog dlg( this, _( "Import Symbol" ), m_mruPath,
61  wxEmptyString, SchematicLibraryFileWildcard(),
62  wxFD_OPEN | wxFD_FILE_MUST_EXIST );
63 
64  if( dlg.ShowModal() == wxID_CANCEL )
65  return;
66 
67  wxFileName fn = dlg.GetPath();
68  m_mruPath = fn.GetPath();
69 
70  wxArrayString symbols;
71  SCH_PLUGIN::SCH_PLUGIN_RELEASER pi( SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_LEGACY ) );
72 
73  // TODO dialog to select the part to be imported if there is more than one
74  try
75  {
76  pi->EnumerateSymbolLib( symbols, fn.GetFullPath() );
77  }
78  catch( const IO_ERROR& ioe )
79  {
80  msg.Printf( _( "Cannot import symbol library \"%s\"." ), fn.GetFullPath() );
81  DisplayErrorMessage( this, msg, ioe.What() );
82  return;
83  }
84 
85  if( symbols.empty() )
86  {
87  msg.Printf( _( "Symbol library file \"%s\" is empty." ), fn.GetFullPath() );
88  DisplayError( this, msg );
89  return;
90  }
91 
92  wxString symbolName = symbols[0];
93  LIB_ALIAS* entry = pi->LoadSymbol( fn.GetFullPath(), symbolName );
94 
95  if( m_libMgr->PartExists( symbols[0], libName ) )
96  {
97  msg.Printf( _( "Symbol \"%s\" already exists in library \"%s\"." ), symbolName, libName );
98  DisplayError( this, msg );
99  return;
100  }
101 
102  m_libMgr->UpdatePart( entry->GetPart(), libName );
103  loadPart( symbolName, libName, 1 );
104 }
105 
106 
107 void LIB_EDIT_FRAME::OnExportPart( wxCommandEvent& event )
108 {
109  wxString msg, title;
110  LIB_PART* part = getTargetPart();
111 
112  if( !part )
113  {
114  DisplayError( this, _( "There is no symbol selected to save." ) );
115  return;
116  }
117 
118  wxFileName fn;
119 
120  fn.SetName( part->GetName().Lower() );
121  fn.SetExt( SchematicLibraryFileExtension );
122 
123  wxFileDialog dlg( this, _( "Export Symbol" ), m_mruPath, fn.GetFullName(),
124  SchematicLibraryFileWildcard(), wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
125 
126  if( dlg.ShowModal() == wxID_CANCEL )
127  return;
128 
129  fn = dlg.GetPath();
130  fn.MakeAbsolute();
131 
132  LIB_PART* old_part = NULL;
133 
134  SCH_PLUGIN::SCH_PLUGIN_RELEASER pi( SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_LEGACY ) );
135 
136  if( fn.FileExists() )
137  {
138  try
139  {
140  LIB_ALIAS* alias = pi->LoadSymbol( fn.GetFullPath(), part->GetName() );
141 
142  if( alias )
143  old_part = alias->GetPart();
144  }
145  catch( const IO_ERROR& ioe )
146  {
147  msg.Printf( _( "Error occurred attempting to load symbol library file \"%s\"" ),
148  fn.GetFullPath() );
149  DisplayErrorMessage( this, msg, ioe.What() );
150  return;
151  }
152 
153  if( old_part )
154  {
155  msg.Printf( _( "Symbol \"%s\" already exists. Overwrite it?" ), part->GetName() );
156 
157  if( !IsOK( this, msg ) )
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 }
virtual void CreateSymbolLib(const wxString &aLibraryPath, const PROPERTIES *aProperties=NULL)
Create a new empty symbol library at aLibraryPath.
Definition: sch_plugin.cpp:126
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
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:107
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Function DisplayErrorMessage displays an error message with aMessage.
Definition: confirm.cpp:199
wxString SelectLibraryFromList()
Dispaly a list of loaded libraries in the symbol library and allows the user to select a library...
Definition: selpart.cpp:76
This file is part of the common library.
void OnImportPart(wxCommandEvent &event)
Routine to read one part.
Definition: lib_export.cpp:46
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()
void SetDrawItem(LIB_ITEM *drawItem)
bool LibraryExists(const wxString &aLibrary) const
Returns true if library exists.
Define a library symbol object.
The common library.
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
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)...
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
Definition for part library class.
const wxString SchematicLibraryFileExtension
void DisplayError(wxWindow *parent, const wxString &text, int displaytime)
Function DisplayError displays an error or warning message box with aMessage.
Definition: confirm.cpp:185
Struct IO_ERROR is a class used to hold an error message and may be used when throwing exceptions con...
Definition: ki_exception.h:47
bool IsOK(wxWindow *aParent, const wxString &aMessage)
Function IsOK displays a yes/no dialog with aMessage and returns the user response.
Definition: confirm.cpp:233
LIB_PART * getTargetPart() const
Returns currently edited part.
void loadPart(const wxString &aLibrary, const wxString &aPart, int Unit)
Definition: libedit.cpp:482