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