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 
31 #include <fctsys.h>
32 #include <kiway.h>
33 #include <pgm_base.h>
34 #include <class_drawpanel.h>
35 #include <confirm.h>
36 #include <kicad_string.h>
37 #include <gestfich.h>
38 #include <class_sch_screen.h>
39 
40 #include <general.h>
41 #include <libeditframe.h>
42 #include <class_library.h>
44 
45 
47 {
48  LIB_PART* part = GetCurPart();
49 
50  // Exit if no library entry is selected or a command is in progress.
51  if( !part || ( m_drawItem && m_drawItem->GetFlags() ) )
52  return;
53 
54  PROJECT& prj = Prj();
55  SEARCH_STACK* search = prj.SchSearchS();
56 
58 
59  wxString default_path = prj.GetRString( PROJECT::SCH_LIB_PATH );
60 
61  if( !default_path )
62  default_path = search->LastVisitedPath();
63 
64  wxFileDialog dlg( this, _( "Import Symbol Drawings" ), default_path,
65  wxEmptyString, SchematicSymbolFileWildcard(),
66  wxFD_OPEN | wxFD_FILE_MUST_EXIST );
67 
68  if( dlg.ShowModal() == wxID_CANCEL )
69  return;
70 
71  SetCrossHairPosition( wxPoint( 0, 0 ) );
74 
75  wxString filename = dlg.GetPath();
76 
77  prj.SetRString( PROJECT::SCH_LIB_PATH, filename );
78 
79  std::unique_ptr<PART_LIB> lib( new PART_LIB( LIBRARY_TYPE_SYMBOL, filename ) );
80 
81  wxString msg;
82 
83  try
84  {
85  if( lib->IsEmpty() )
86  {
87  msg.Printf( _( "No parts found in part file '%s'." ), GetChars( filename ) );
88  DisplayError( this, msg );
89  return;
90  }
91  }
92  catch( const IO_ERROR& exc )
93  {
94  msg.Printf( _( "Error '%s' occurred loading part file '%s'." ),
95  GetChars( exc.Problem() ), GetChars( filename ) );
96  DisplayError( this, msg );
97  return;
98  }
99 
100  if( lib->GetCount() > 1 )
101  {
102  msg.Printf( _( "More than one part in part file '%s'." ), GetChars( filename ) );
103  wxMessageBox( msg, _( "Warning" ), wxOK | wxICON_EXCLAMATION, this );
104  }
105 
106  wxArrayString aliasNames;
107 
108  lib->GetAliasNames( aliasNames );
109 
110  wxCHECK_RET( !aliasNames.IsEmpty(), "No aliases found in library " + filename );
111 
112  LIB_PART* first = lib->FindAlias( aliasNames[0] )->GetPart();
113  LIB_ITEMS_CONTAINER& drawList = first->GetDrawItems();
114 
115  for( LIB_ITEM& item : drawList )
116  {
117  if( item.Type() == LIB_FIELD_T )
118  continue;
119 
120  if( item.GetUnit() )
121  item.SetUnit( m_unit );
122 
123  if( item.GetConvert() )
124  item.SetConvert( m_convert );
125 
126  item.SetFlags( IS_NEW | SELECTED );
127 
128  LIB_ITEM* newItem = (LIB_ITEM*) item.Clone();
129 
130  newItem->SetParent( part );
131  part->AddDrawItem( newItem );
132  }
133 
134  part->RemoveDuplicateDrawItems();
135  part->ClearSelectedItems();
136 
137  OnModify();
138  m_canvas->Refresh();
139 }
140 
141 
143 {
144  wxString msg;
145  PROJECT& prj = Prj();
146  SEARCH_STACK* search = prj.SchSearchS();
147  LIB_PART* part = GetCurPart();
148 
149  if( !part || part->GetDrawItems().empty() )
150  return;
151 
152  wxString default_path = prj.GetRString( PROJECT::SCH_LIB_PATH );
153 
154  if( !default_path )
155  default_path = search->LastVisitedPath();
156 
157  wxFileDialog dlg( this, _( "Export Symbol Drawings" ), default_path,
158  part->GetName() + "." + SchematicSymbolFileExtension,
160  wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
161 
162  if( dlg.ShowModal() == wxID_CANCEL )
163  return;
164 
165  wxFileName fn = dlg.GetPath();
166 
167  /* The GTK file chooser doesn't return the file extension added to
168  * file name so add it here. */
169  if( fn.GetExt().IsEmpty() )
170  fn.SetExt( SchematicSymbolFileExtension );
171 
172  prj.SetRString( PROJECT::SCH_LIB_PATH, fn.GetPath() );
173 
174  msg.Printf( _( "Saving symbol in '%s'" ), GetChars( fn.GetPath() ) );
175  SetStatusText( msg );
176 
177  wxString line;
178 
179  // File header
180  line << wxT( LIBFILE_IDENT ) << wxT( " " ) << LIB_VERSION_MAJOR
181  << wxT( "." ) << LIB_VERSION_MINOR << wxT( " SYMBOL " )
182  << wxT( "Date: " ) << DateAndTime() << wxT( "\n" );
183 
184  // Component name comment and definition.
185  line << wxT( "# SYMBOL " ) << part->GetName() << wxT( "\n#\nDEF " )
186  << part->GetName() << wxT( " " );
187 
188  if( !part->GetReferenceField().GetText().IsEmpty() )
189  line << part->GetReferenceField().GetText() << wxT( " " );
190  else
191  line << wxT( "~ " );
192 
193  line << 0 << wxT( " " ) << part->GetPinNameOffset() << wxT( " " );
194 
195  if( part->ShowPinNumbers() )
196  line << wxT( "Y " );
197  else
198  line << wxT( "N " );
199 
200  if( part->ShowPinNames() )
201  line << wxT( "Y " );
202  else
203  line << wxT( "N " );
204 
205  line << wxT( "1 0 N\n" );
206 
207  try
208  {
209  FILE_OUTPUTFORMATTER formatter( fn.GetFullPath() );
210 
211  try
212  {
213  formatter.Print( 0, "%s", TO_UTF8( line ) );
214  part->GetReferenceField().Save( formatter );
215  part->GetValueField().Save( formatter );
216  formatter.Print( 0, "DRAW\n" );
217 
218  LIB_ITEMS_CONTAINER& drawList = part->GetDrawItems();
219 
220  for( LIB_ITEM& item : drawList )
221  {
222  if( item.Type() == LIB_FIELD_T )
223  continue;
224 
225  // Don't save unused parts or alternate body styles.
226  if( m_unit && item.GetUnit() && ( item.GetUnit() != m_unit ) )
227  continue;
228 
229  if( m_convert && item.GetConvert() && ( item.GetConvert() != m_convert ) )
230  continue;
231 
232  item.Save( formatter );
233  }
234 
235  formatter.Print( 0, "ENDDRAW\n" );
236  formatter.Print( 0, "ENDDEF\n" );
237  }
238  catch( const IO_ERROR& )
239  {
240  msg.Printf( _( "An error occurred attempting to save symbol file '%s'" ),
241  GetChars( fn.GetFullPath() ) );
242  DisplayError( this, msg );
243  }
244  }
245  catch( const IO_ERROR& ioe )
246  {
247  DisplayError( this, ioe.What() );
248  return;
249  }
250 }
251 
252 
254 {
255  if( LIB_PART* part = GetCurPart() )
256  {
257  const wxPoint& cross_hair = GetCrossHairPosition();
258 
259  wxPoint offset( -cross_hair.x, cross_hair.y );
260 
261  OnModify( );
262 
263  part->SetOffset( offset );
264 
265  // Redraw the symbol
266  RedrawScreen( wxPoint( 0 , 0 ), true );
267  m_canvas->Refresh();
268  }
269 }
wxString SchematicSymbolFileWildcard()
STATUS_FLAGS GetFlags() const
Definition: base_struct.h:269
void SaveOneSymbol()
Saves the current symbol to a symbol file.
Definition: symbedit.cpp:142
Class PROJECT holds project specific data.
Definition: project.h:56
int GetPinNameOffset()
static int m_convert
Definition: libeditframe.h:108
This file is part of the common library TODO brief description.
virtual void Refresh(bool eraseBackground=true, const wxRect *rect=NULL) override
Update the board display after modifying it bu a python script (note: it is automatically called by a...
Definition: draw_panel.cpp:325
This file is part of the common library.
#define LIBFILE_IDENT
Definition: class_library.h:59
static LIB_ITEM * m_drawItem
Definition: libeditframe.h:100
virtual EDA_ITEM * Clone() const
Function Clone creates a duplicate of this item with linked list members set to NULL.
bool empty(int aType=UNDEFINED_TYPE)
Definition: multivector.h:224
LIB_FIELD & GetValueField()
Return reference to the value field.
#define LIB_VERSION_MINOR
Definition: class_library.h:56
void OnModify()
Must be called after a schematic change in order to set the "modify" flag of the current screen...
void LoadOneSymbol()
Read a component symbol file (*.sym ) and add graphic items to the current component.
Definition: symbedit.cpp:46
Class SEARCH_STACK looks for files in a number of places.
Definition: search_stack.h:41
void ClearSelectedItems()
Clears all the draw items marked by a block select.
void MoveCursorToCrossHair()
Function MoveCursorToCrossHair warps the cursor to the current cross hair position.
Definition: draw_panel.cpp:347
bool Save(OUTPUTFORMATTER &aFormatter) override
Function Save writes draw item object to aFormatter in component library "*.lib" format.
Definition: lib_field.cpp:112
Class LIB_ITEM is the base class for drawable items used by schematic library components.
Definition: lib_draw_item.h:67
void AddDrawItem(LIB_ITEM *aItem)
Add a new draw aItem to the draw object list.
#define LIB_VERSION_MAJOR
Definition: class_library.h:55
#define IS_NEW
New item, just created.
Definition: base_struct.h:127
#define TO_UTF8(wxstring)
Macro TO_UTF8 converts a wxString to a UTF8 encoded C string for all wxWidgets build modes...
Definition: macros.h:47
bool ShowPinNames()
PROJECT & Prj() const
Function Prj returns a reference to the PROJECT "associated with" this KIWAY.
#define SELECTED
Definition: base_struct.h:134
bool ShowPinNumbers()
LIB_PART * GetCurPart() const
Return the current part being edited or NULL if none selected.
Definition: libeditframe.h:158
LIB_FIELD & GetReferenceField()
Return reference to the reference designator field.
virtual const wxString Problem() const
what was the problem?
Definition: exceptions.cpp:49
void SetParent(EDA_ITEM *aParent)
Definition: base_struct.h:227
Definitions for the Eeschema program SCH_SCREEN class.
const wxString & GetName() const
LIB_ITEMS_CONTAINER & GetDrawItems()
Return a reference to the draw item list.
const wxString & GetText() const
Function GetText returns the string associated with the text object.
Definition: eda_text.h:130
Class LIB_PART defines a library part object.
The common library.
void RemoveDuplicateDrawItems()
Remove duplicate draw items from list.
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:173
const wxString SchematicSymbolFileExtension
EDA_DRAW_PANEL * m_canvas
The area to draw on.
Definition: draw_frame.h:93
void RedrawScreen(const wxPoint &aCenterPoint, bool aWarpPointer)
Function RedrawScreen redraws the entire screen area by updating the scroll bars and mouse pointer in...
Definition: zoom.cpp:46
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:188
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:33
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
see class PGM_BASE
static int m_unit
Definition: libeditframe.h:104
const wxString LastVisitedPath(const wxString &aSubPathToSearch=wxEmptyString)
Function LastVisitedPath is a quirky function inherited from old code that seems to serve particular ...
void SetIgnoreMouseEvents(bool aIgnore)
Class FILE_OUTPUTFORMATTER may be used for text file output.
Definition: richio.h:492
void PlaceAnchor()
Places an anchor reference coordinate for the current component.
Definition: symbedit.cpp:253
Definition for part library class.
int PRINTF_FUNC Print(int nestLevel, const char *fmt,...)
Function Print formats and writes text to the output stream.
Definition: richio.cpp:404
void SetCrossHairPosition(const wxPoint &aPosition, bool aSnapToGrid=true)
Function SetCrossHairPosition sets the screen cross hair position to aPosition in logical (drawing) u...
Class PART_LIB is used to load, save, search, and otherwise manipulate part library files...
void DisplayError(wxWindow *parent, const wxString &text, int displaytime)
Function DisplayError displays an error or warning message box with aMessage.
Definition: confirm.cpp:73
wxPoint GetCrossHairPosition(bool aInvertY=false) const
Function GetCrossHairPosition return the current cross hair position in logical (drawing) coordinates...
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.
wxString DateAndTime()
Function DateAndTime.
Definition: string.cpp:229