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 <libeditframe.h>
38 #include <class_library.h>
40 
41 #include <wx/filename.h>
42 
43 
44 extern int ExportPartId;
45 
46 
47 void LIB_EDIT_FRAME::OnImportPart( wxCommandEvent& event )
48 {
49  wxString msg;
50  m_lastDrawItem = NULL;
51 
52  wxFileDialog dlg( this, _( "Import Symbol" ), m_mruPath,
53  wxEmptyString, SchematicLibraryFileWildcard(),
54  wxFD_OPEN | wxFD_FILE_MUST_EXIST );
55 
56  if( dlg.ShowModal() == wxID_CANCEL )
57  return;
58 
59  wxFileName fn = dlg.GetPath();
60 
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  try
67  {
68  pi->EnumerateSymbolLib( symbols, fn.GetFullPath() );
69  }
70  catch( const IO_ERROR& ioe )
71  {
72  msg.Printf( _( "Cannot import symbol library '%s'." ), fn.GetFullPath() );
73  DisplayErrorMessage( this, msg, ioe.What() );
74  return;
75  }
76 
77  if( symbols.empty() )
78  {
79  msg.Printf( _( "Symbol library file '%s' is empty." ), fn.GetFullPath() );
80  DisplayError( this, msg );
81  return;
82  }
83 
84  LIB_ALIAS* entry = pi->LoadSymbol( fn.GetFullPath(), symbols[0] );
85 
86  if( LoadOneLibraryPartAux( entry, fn.GetFullPath() ) )
87  {
90  Zoom_Automatique( false );
91 
92  // This effectively adds a new symbol to the library. Set the modified flag so the
93  // save library toolbar button and menu entry are enabled.
94  OnModify();
95  }
96 }
97 
98 
99 void LIB_EDIT_FRAME::OnExportPart( wxCommandEvent& event )
100 {
101  wxString msg, title;
102  bool createLib = ( event.GetId() == ExportPartId ) ? false : true;
103 
104  LIB_PART* part = GetCurPart();
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  title = createLib ? _( "New Symbol Library" ) : _( "Export Symbol" );
118 
119  wxFileDialog dlg( this, title, m_mruPath, fn.GetFullName(),
120  SchematicLibraryFileWildcard(), wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
121 
122  if( dlg.ShowModal() == wxID_CANCEL )
123  return;
124 
125  fn = dlg.GetPath();
126  fn.MakeAbsolute();
127 
128  LIB_PART* old_part = NULL;
129 
130  SCH_PLUGIN::SCH_PLUGIN_RELEASER pi( SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_LEGACY ) );
131 
132  if( fn.FileExists() )
133  {
134  try
135  {
136  LIB_ALIAS* alias = pi->LoadSymbol( fn.GetFullPath(), part->GetName() );
137 
138  if( alias )
139  old_part = alias->GetPart();
140  }
141  catch( const IO_ERROR& ioe )
142  {
143  msg.Printf( _( "Error occurred attempting to load symbol library file '%s'" ),
144  fn.GetFullPath() );
145  DisplayErrorMessage( this, msg, ioe.What() );
146  return;
147  }
148 
149  if( old_part )
150  {
151  msg.Printf( _( "Symbol '%s' already exists. Overwrite it?" ), part->GetName() );
152 
153  if( !IsOK( this, msg ) )
154  return;
155  }
156  }
157 
158  if( fn.Exists() && !fn.IsDirWritable() )
159  {
160  msg.Printf( _( "Write permissions are required to save library '%s'." ), 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 = _( "Failed to create symbol library file " ) + 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 
184  DisplayInfoMessage( this, _( "This library will not be available until it is added to the "
185  "symbol library table." ) );
186 
187  GetScreen()->ClrModify();
188  m_drawItem = m_lastDrawItem = NULL;
189 
190  msg.Printf( _( "Symbol '%s' saved in library '%s'" ), part->GetName(), fn.GetFullPath() );
191  SetStatusText( msg );
192 }
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.
Definition: wxstruct.h:165
void OnExportPart(wxCommandEvent &event)
Function OnExportPart creates a new library and backup the current component in this library or expor...
Definition: lib_export.cpp:99
This file is part of the common library.
static LIB_ITEM * m_drawItem
Definition: libeditframe.h:92
void OnImportPart(wxCommandEvent &event)
Routine to read one part.
Definition: lib_export.cpp:47
SCH_SCREEN * GetScreen() const override
Function GetScreen returns a pointer to a BASE_SCREEN or one of its derivatives.
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 OnModify()
Must be called after a schematic change in order to set the "modify" flag of the current screen...
Definition: libeditframe.h:352
int ExportPartId
void DisplayLibInfos()
Updates the main window title bar with the current library name and read only status of the library...
Definition: libedit.cpp:56
void Zoom_Automatique(bool aWarpPointer)
Function Zoom_Automatique redraws the screen with best zoom level and the best centering that shows a...
Definition: zoom.cpp:77
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString aExtraInfo)
Function DisplayErrorMessage displays an error message with aMessage.
Definition: confirm.cpp:85
const wxString & GetName() const
virtual void ClearUndoRedoList()
Function ClearUndoRedoList clear undo and redo list, using ClearUndoORRedoList() picked items are del...
Class LIB_PART defines a library part object.
The common library.
void DisplayInfoMessage(wxWindow *aParent, const wxString &aMessage, const wxString aExtraInfo)
Function DisplayInfoMessage displays an informational message box with aMessage.
Definition: confirm.cpp:102
static LIB_ITEM * m_lastDrawItem
Definition: libeditframe.h:91
LIB_PART(const wxString &aName, PART_LIB *aLibrary=NULL)
LIB_PART * GetPart() const
Function GetPart gets 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:513
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:33
bool LoadOneLibraryPartAux(LIB_ALIAS *aLibEntry, const wxString &aLibrary)
Create a copy of aLibEntry into memory.
Definition: libedit.cpp:191
LIB_PART * GetCurPart()
Return the current part being edited or NULL if none selected.
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:71
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:129
Definition of class LIB_EDIT_FRAME.