KiCad PCB EDA Suite
symbedit.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) 2004 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 
26 #include <fctsys.h>
27 #include <kiway.h>
28 #include <pgm_base.h>
29 #include <sch_draw_panel.h>
30 #include <confirm.h>
31 #include <kicad_string.h>
32 #include <gestfich.h>
33 #include <lib_pin.h>
34 #include <lib_edit_frame.h>
35 #include <class_libentry.h>
37 #include <sch_legacy_plugin.h>
38 #include <properties.h>
39 #include <view/view.h>
40 #include <tool/tool_manager.h>
42 
44 {
46 
47  // Exit if no library entry is selected or a command is in progress.
48  if( !m_my_part || !EE_CONDITIONS::Idle( selTool->GetSelection() ) )
49  return;
50 
51  PROJECT& prj = Prj();
52  SEARCH_STACK* search = prj.SchSearchS();
53 
54  wxString default_path = prj.GetRString( PROJECT::SCH_LIB_PATH );
55 
56  if( !default_path )
57  default_path = search->LastVisitedPath();
58 
59  wxFileDialog dlg( this, _( "Import Symbol" ), default_path,
60  wxEmptyString, SchematicSymbolFileWildcard(),
61  wxFD_OPEN | wxFD_FILE_MUST_EXIST );
62 
63  if( dlg.ShowModal() == wxID_CANCEL )
64  return;
65 
66  wxString filename = dlg.GetPath();
67 
68  prj.SetRString( PROJECT::SCH_LIB_PATH, filename );
69 
70  wxArrayString symbols;
71  SCH_PLUGIN::SCH_PLUGIN_RELEASER pi( SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_LEGACY ) );
72 
73  wxString msg;
74 
75  try
76  {
77  pi->EnumerateSymbolLib( symbols, filename );
78  }
79  catch( const IO_ERROR& ioe )
80  {
81  msg.Printf( _( "Cannot import symbol library \"%s\"." ), filename );
82  DisplayErrorMessage( this, msg, ioe.What() );
83  return;
84  }
85 
86  if( symbols.empty() )
87  {
88  msg.Printf( _( "Symbol library file \"%s\" is empty." ), filename );
89  DisplayError( this, msg );
90  return;
91  }
92 
93  if( symbols.GetCount() > 1 )
94  {
95  msg.Printf( _( "More than one symbol found in symbol file \"%s\"." ), filename );
96  wxMessageBox( msg, _( "Warning" ), wxOK | wxICON_EXCLAMATION, this );
97  }
98 
99  LIB_PART* alias = nullptr;
100 
101  try
102  {
103  alias = pi->LoadSymbol( filename, symbols[0] );
104  }
105  catch( const IO_ERROR& )
106  {
107  return;
108  }
109 
110  wxCHECK_RET( alias, "Invalid symbol." );
111 
113 
114  LIB_PART* first = alias;
115  LIB_ITEMS_CONTAINER& drawList = first->GetDrawItems();
116 
117  for( LIB_ITEM& item : drawList )
118  {
119  if( item.Type() == LIB_FIELD_T )
120  continue;
121 
122  if( item.GetUnit() )
123  item.SetUnit( m_unit );
124 
125  if( item.GetConvert() )
126  item.SetConvert( m_convert );
127 
128  item.SetFlags( IS_NEW | SELECTED );
129 
130  LIB_ITEM* newItem = (LIB_ITEM*) item.Clone();
131 
132  newItem->SetParent( m_my_part );
133  m_my_part->AddDrawItem( newItem );
134  item.ClearSelected();
135  }
136 
138 
139  OnModify();
140 }
141 
142 
144 {
145  wxCHECK( m_my_part, /* void */ );
146 
147  // Export the current part as a symbol (.sym file)
148  // this is the current part without its aliases and doc file
149  // because a .sym file is used to import graphics in a part being edited
150  if( m_my_part->GetDrawItems().empty() )
151  return;
152 
153  PROJECT& prj = Prj();
154  SEARCH_STACK* search = prj.SchSearchS();
155 
156  wxString default_path = prj.GetRString( PROJECT::SCH_LIB_PATH );
157 
158  if( !default_path )
159  default_path = search->LastVisitedPath();
160 
161  wxFileDialog dlg( this, _( "Export Symbol" ), default_path,
164  wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
165 
166  if( dlg.ShowModal() == wxID_CANCEL )
167  return;
168 
169  wxFileName fn = dlg.GetPath();
170 
171  /* The GTK file chooser doesn't return the file extension added to
172  * file name so add it here. */
173  if( fn.GetExt().IsEmpty() )
174  fn.SetExt( SchematicSymbolFileExtension );
175 
176  prj.SetRString( PROJECT::SCH_LIB_PATH, fn.GetPath() );
177 
178  if( fn.FileExists() )
179  wxRemove( fn.GetFullPath() );
180 
181  SetStatusText( wxString::Format( _( "Saving symbol in \"%s\"" ), fn.GetPath() ) );
182 
183  SCH_PLUGIN::SCH_PLUGIN_RELEASER plugin( SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_LEGACY ) );
184 
185  try
186  {
187  PROPERTIES nodoc_props; // Doc file is useless for a .sym file
188  nodoc_props[ SCH_LEGACY_PLUGIN::PropNoDocFile ] = "";
189  plugin->CreateSymbolLib( fn.GetFullPath(), &nodoc_props );
190 
191  // The part gets flattened by the LIB_PART copy constructor.
192  LIB_PART* saved_part = new LIB_PART( *m_my_part );
193  plugin->SaveSymbol( fn.GetFullPath(), saved_part, &nodoc_props );
194  }
195  catch( const IO_ERROR& ioe )
196  {
197  wxString msg = wxString::Format( _( "An error occurred saving symbol file \"%s\"" ),
198  fn.GetFullPath() );
199  DisplayErrorMessage( this, msg, ioe.What() );
200  }
201 }
202 
203 
205 {
207 
208  if( !m_my_part )
209  return;
210 
211  wxString msg = m_my_part->GetName();
212 
213  AppendMsgPanel( _( "Name" ), msg, BLUE, 8 );
214 
215  if( m_my_part->IsAlias() )
216  {
217  PART_SPTR parent = m_my_part->GetParent().lock();
218 
219  msg = parent ? parent->GetName() : _( "Undefined!" );
220  AppendMsgPanel( _( "Parent" ), msg, BROWN, 8 );
221  }
222 
223  static wxChar UnitLetter[] = wxT( "?ABCDEFGHIJKLMNOPQRSTUVWXYZ" );
224  msg = UnitLetter[m_unit];
225 
226  AppendMsgPanel( _( "Unit" ), msg, BROWN, 8 );
227 
228  if( m_convert > 1 )
229  msg = _( "Convert" );
230  else
231  msg = _( "Normal" );
232 
233  AppendMsgPanel( _( "Body" ), msg, GREEN, 8 );
234 
235  if( m_my_part->IsPower() )
236  msg = _( "Power Symbol" );
237  else
238  msg = _( "Symbol" );
239 
240  AppendMsgPanel( _( "Type" ), msg, MAGENTA, 8 );
241  AppendMsgPanel( _( "Description" ), m_my_part->GetDescription(), CYAN, 8 );
242  AppendMsgPanel( _( "Key words" ), m_my_part->GetKeyWords(), DARKDARKGRAY );
243  AppendMsgPanel( _( "Datasheet" ), m_my_part->GetDocFileName(), DARKDARKGRAY );
244 }
Definition: colors.h:57
wxString SchematicSymbolFileWildcard()
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:239
void SaveOneSymbol()
Saves the current symbol to a symbol file.
Definition: symbedit.cpp:143
PROJECT holds project specific data.
Definition: project.h:59
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition: confirm.cpp:252
This file is part of the common library TODO brief description.
wxString GetName() const override
This file is part of the common library.
Definition: colors.h:61
bool empty(int aType=UNDEFINED_TYPE)
Definition: multivector.h:237
const std::string SchematicSymbolFileExtension
void LoadOneSymbol()
Read a component symbol file (*.sym ) and add graphic items to the current component.
Definition: symbedit.cpp:43
SEARCH_STACK looks for files in a number of places.
Definition: search_stack.h:41
wxString GetKeyWords() const
static SELECTION_CONDITION Idle
PROPERTIES is a name/value tuple with unique names and optional values.
Definition: properties.h:34
LIB_PART * m_my_part
virtual EDA_ITEM * Clone() const
Function Clone creates a duplicate of this item with linked list members set to NULL.
The base class for drawable items used by schematic library components.
Definition: lib_item.h:60
void AddDrawItem(LIB_ITEM *aItem)
Add a new draw aItem to the draw object list.
EE_SELECTION & GetSelection()
Function GetSelection()
#define IS_NEW
New item, just created.
Definition: base_struct.h:120
#define SELECTED
Definition: base_struct.h:127
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:33
void SetParent(EDA_ITEM *aParent)
Definition: base_struct.h:218
LIB_ITEMS_CONTAINER & GetDrawItems()
Return a reference to the draw item list.
PROJECT & Prj() const
Function Prj returns a reference to the PROJECT "associated with" this KIWAY.
Class LIB_PIN definition.
std::shared_ptr< LIB_PART > PART_SPTR
shared pointer to LIB_PART
Definition: colors.h:59
PART_REF & GetParent()
Define a library symbol object.
Definition of file extensions used in Kicad.
wxString GetDocFileName() const
wxString GetDescription() override
static const char * PropNoDocFile
The property used internally by the plugin to disable writing the library documentation (....
void RemoveDuplicateDrawItems()
Remove duplicate draw items from list.
virtual void ClearMsgPanel()
Clear all messages from the message panel.
void DisplayCmpDoc()
Display the documentation of the selected component.
Definition: symbedit.cpp:204
VTBL_ENTRY void SetRString(RSTRING_T aStringId, const wxString &aString)
Function SetRString stores a "retained string", which is any session and project specific string iden...
Definition: project.cpp:198
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 AppendMsgPanel(const wxString &textUpper, const wxString &textLower, COLOR4D color, int pad=6)
Append a message to the message panel.
VTBL_ENTRY const wxString & GetRString(RSTRING_T aStringId)
Function GetRString returns a "retained string", which is any session and project specific string ide...
Definition: project.cpp:213
bool IsPower() const
Definition: colors.h:58
see class PGM_BASE
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
#define _(s)
Definition: 3d_actions.cpp:33
const wxString LastVisitedPath(const wxString &aSubPathToSearch=wxEmptyString)
Function LastVisitedPath is a quirky function inherited from old code that seems to serve particular ...
bool IsAlias() const
void OnModify() override
Must be called after a schematic change in order to set the "modify" flag of the current symbol.
TOOL_MANAGER * m_toolManager
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
void SaveCopyInUndoList(EDA_ITEM *ItemToCopy, UNDO_REDO_T undoType=UR_LIBEDIT, bool aAppend=false)
Create a copy of the current component, and save it in the undo list.
Definition: colors.h:62