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-2016 Wayne Stambaugh <stambaughw@verizon.net>
6  * Copyright (C) 2004-2016 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  m_lastDrawItem = NULL;
50 
51  wxFileDialog dlg( this, _( "Import Component" ), m_mruPath,
52  wxEmptyString, SchematicLibraryFileWildcard,
53  wxFD_OPEN | wxFD_FILE_MUST_EXIST );
54 
55  if( dlg.ShowModal() == wxID_CANCEL )
56  return;
57 
58  wxFileName fn = dlg.GetPath();
59 
60  m_mruPath = fn.GetPath();
61 
62  std::unique_ptr<PART_LIB> lib;
63 
64  try
65  {
66  std::unique_ptr<PART_LIB> new_lib( PART_LIB::LoadLibrary( fn.GetFullPath() ) );
67  lib = std::move( new_lib );
68  }
69  catch( const IO_ERROR& )
70  {
71  wxString msg = wxString::Format( _(
72  "Unable to import library '%s'. Error:\n"
73  "%s" ),
74  GetChars( fn.GetFullPath() )
75  );
76 
77  DisplayError( this, msg );
78  return;
79  }
80 
81  wxArrayString aliasNames;
82 
83  lib->GetAliasNames( aliasNames );
84 
85  if( aliasNames.IsEmpty() )
86  {
87  wxString msg = wxString::Format( _( "Part library file '%s' is empty." ),
88  GetChars( fn.GetFullPath() ) );
89  DisplayError( this, msg );
90  return;
91  }
92 
93  LIB_ALIAS* entry = lib->FindAlias( aliasNames[0] );
94 
95  if( LoadOneLibraryPartAux( entry, lib.get() ) )
96  {
99  Zoom_Automatique( false );
100  }
101 }
102 
103 
104 void LIB_EDIT_FRAME::OnExportPart( wxCommandEvent& event )
105 {
106  wxString msg, title;
107  bool createLib = ( event.GetId() == ExportPartId ) ? false : true;
108 
109  LIB_PART* part = GetCurPart();
110 
111  if( !part )
112  {
113  DisplayError( this, _( "There is no component selected to save." ) );
114  return;
115  }
116 
117  wxFileName fn = part->GetName().Lower();
118 
119  fn.SetExt( SchematicLibraryFileExtension );
120 
121  title = createLib ? _( "New Library" ) : _( "Export Component" );
122 
123  wxFileDialog dlg( this, title, 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 
131  std::unique_ptr<PART_LIB> temp_lib( new PART_LIB( LIBRARY_TYPE_EESCHEMA, fn.GetFullPath() ) );
132 
133  try
134  {
135  SaveOnePart( temp_lib.get() );
136  }
137  catch( ... /* IO_ERROR ioe */ )
138  {
139  fn.MakeAbsolute();
140  msg = wxT( "Failed to create symbol library file " ) + fn.GetFullPath();
141  DisplayError( this, msg );
142  msg.Printf( _( "Error creating symbol library '%s'" ), fn.GetFullName() );
143  SetStatusText( msg );
144  return;
145  }
146 
147  m_mruPath = fn.GetPath();
148 
149  msg.Printf( _( "'%s' - OK" ), GetChars( fn.GetFullPath() ) );
150  DisplayInfoMessage( this, _( "This library will not be available until it is loaded by "
151  "Eeschema.\n\n"
152  "Modify the Eeschema library configuration if you want to "
153  "include it as part of this project." ) );
154 
155  msg.Printf( _( "'%s' - Export OK" ), GetChars( fn.GetFullPath() ) );
156  SetStatusText( msg );
157 }
const wxString SchematicLibraryFileWildcard
Part library alias object definition.
wxString m_mruPath
Most recently used path.
Definition: wxstruct.h:155
void OnExportPart(wxCommandEvent &event)
Function OnExportPart creates a new library and backup the current component in this library or expor...
Definition: lib_export.cpp:104
This file is part of the common library.
void OnImportPart(wxCommandEvent &event)
Routine to read one part.
Definition: lib_export.cpp:47
bool LoadOneLibraryPartAux(LIB_ALIAS *aLibEntry, PART_LIB *aLibrary)
Function LoadOneLibraryPartAux loads a copy of aLibEntry from aLibrary into memory.
Definition: libedit.cpp:194
SCH_SCREEN * GetScreen() const override
Function GetScreen returns a pointer to a BASE_SCREEN or one of its derivatives.
int ExportPartId
void DisplayLibInfos()
Function DisplayLibInfos updates the main window title bar with the current library name and read onl...
Definition: libedit.cpp:55
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
const wxString & GetName() const
virtual void ClearUndoRedoList()
Function ClearUndoRedoList clear undo and redo list, using ClearUndoORRedoList() picked items are del...
static PART_LIB * LoadLibrary(const wxString &aFileName)
Function LoadLibrary allocates and loads a part library file.
Class LIB_PART defines a library part object.
The common library.
static LIB_ITEM * m_lastDrawItem
Definition: libeditframe.h:90
bool SaveOnePart(PART_LIB *aLib, bool aPromptUser=true)
Function SaveOnePart saves the current LIB_PART into the provided PART_LIB.
Definition: libedit.cpp:702
const wxString SchematicLibraryFileExtension
void DisplayInfoMessage(wxWindow *parent, const wxString &text, int displaytime)
Function DisplayInfoMessage displays an informational message box with aMessage.
Definition: confirm.cpp:89
static const wxChar * GetChars(const wxString &s)
Function GetChars returns a wxChar* to the actual wxChar* data within a wxString, and is helpful for ...
Definition: macros.h:92
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
LIB_PART * GetCurPart()
Function GetCurPart returns the current part being edited, or NULL if none selected.
friend class PART_LIB
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:69
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
Definition of class LIB_EDIT_FRAME.