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-2017 Wayne Stambaugh <stambaughw@verizon.net>
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  if( !default_path )
154  default_path = search->LastVisitedPath();
155 
156  wxFileDialog dlg( this, _( "Export Symbol Drawings" ), default_path,
158  wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
159 
160  if( dlg.ShowModal() == wxID_CANCEL )
161  return;
162 
163  wxFileName fn = dlg.GetPath();
164 
165  /* The GTK file chooser doesn't return the file extension added to
166  * file name so add it here. */
167  if( fn.GetExt().IsEmpty() )
168  fn.SetExt( SchematicSymbolFileExtension );
169 
170  prj.SetRString( PROJECT::SCH_LIB_PATH, fn.GetPath() );
171 
172  msg.Printf( _( "Saving symbol in '%s'" ), GetChars( fn.GetPath() ) );
173  SetStatusText( msg );
174 
175  wxString line;
176 
177  // File header
178  line << wxT( LIBFILE_IDENT ) << wxT( " " ) << LIB_VERSION_MAJOR
179  << wxT( "." ) << LIB_VERSION_MINOR << wxT( " SYMBOL " )
180  << wxT( "Date: " ) << DateAndTime() << wxT( "\n" );
181 
182  // Component name comment and definition.
183  line << wxT( "# SYMBOL " ) << part->GetName() << wxT( "\n#\nDEF " )
184  << part->GetName() << wxT( " " );
185 
186  if( !part->GetReferenceField().GetText().IsEmpty() )
187  line << part->GetReferenceField().GetText() << wxT( " " );
188  else
189  line << wxT( "~ " );
190 
191  line << 0 << wxT( " " ) << part->GetPinNameOffset() << wxT( " " );
192 
193  if( part->ShowPinNumbers() )
194  line << wxT( "Y " );
195  else
196  line << wxT( "N " );
197 
198  if( part->ShowPinNames() )
199  line << wxT( "Y " );
200  else
201  line << wxT( "N " );
202 
203  line << wxT( "1 0 N\n" );
204 
205  try
206  {
207  FILE_OUTPUTFORMATTER formatter( fn.GetFullPath() );
208 
209  try
210  {
211  formatter.Print( 0, "%s", TO_UTF8( line ) );
212  part->GetReferenceField().Save( formatter );
213  part->GetValueField().Save( formatter );
214  formatter.Print( 0, "DRAW\n" );
215 
216  LIB_ITEMS_CONTAINER& drawList = part->GetDrawItems();
217 
218  for( LIB_ITEM& item : drawList )
219  {
220  if( item.Type() == LIB_FIELD_T )
221  continue;
222 
223  // Don't save unused parts or alternate body styles.
224  if( m_unit && item.GetUnit() && ( item.GetUnit() != m_unit ) )
225  continue;
226 
227  if( m_convert && item.GetConvert() && ( item.GetConvert() != m_convert ) )
228  continue;
229 
230  item.Save( formatter );
231  }
232 
233  formatter.Print( 0, "ENDDRAW\n" );
234  formatter.Print( 0, "ENDDEF\n" );
235  }
236  catch( const IO_ERROR& )
237  {
238  msg.Printf( _( "An error occurred attempting to save symbol file '%s'" ),
239  GetChars( fn.GetFullPath() ) );
240  DisplayError( this, msg );
241  }
242  }
243  catch( const IO_ERROR& ioe )
244  {
245  DisplayError( this, ioe.What() );
246  return;
247  }
248 }
249 
250 
252 {
253  if( LIB_PART* part = GetCurPart() )
254  {
255  const wxPoint& cross_hair = GetCrossHairPosition();
256 
257  wxPoint offset( -cross_hair.x, cross_hair.y );
258 
259  OnModify( );
260 
261  part->SetOffset( offset );
262 
263  // Redraw the symbol
264  RedrawScreen( wxPoint( 0 , 0 ), true );
265  m_canvas->Refresh();
266  }
267 }
STATUS_FLAGS GetFlags() const
Definition: base_struct.h:269
void SaveOneSymbol()
Function SaveOneSymbol saves the current component 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:99
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
const wxString SchematicSymbolFileExtension
File extension definitions.
This file is part of the common library.
#define LIBFILE_IDENT
Definition: class_library.h:59
static LIB_ITEM * m_drawItem
Definition: libeditframe.h:91
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()
Function OnModify Must be called after a schematic change in order to set the "modify" flag of the cu...
Definition: libeditframe.h:369
void LoadOneSymbol()
Function LoadOneSymbol read a component symbol file (*.sym ) and add graphic items to the current com...
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_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:162
EDA_DRAW_PANEL * m_canvas
The area to draw on.
Definition: draw_frame.h:92
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:177
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:95
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)
LIB_PART * GetCurPart()
Function GetCurPart returns the current part being edited, or NULL if none selected.
Class FILE_OUTPUTFORMATTER may be used for text file output.
Definition: richio.h:492
void PlaceAnchor()
Function PlaceAnchor places an anchor reference coordinate for the current component.
Definition: symbedit.cpp:251
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:408
void SetCrossHairPosition(const wxPoint &aPosition, bool aSnapToGrid=true)
Function SetCrossHairPosition sets the screen cross hair position to aPosition in logical (drawing) u...
const wxString SchematicSymbolFileWildcard
Proper wxFileDialog wild card definitions.
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:71
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