1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
|
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2004 Jean-Pierre Charras, jaen-pierre.charras@gipsa-lab.inpg.com
* Copyright (C) 2008-2011 Wayne Stambaugh <stambaughw@verizon.net>
* Copyright (C) 2004-2011 KiCad Developers, see change_log.txt for contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
/**
* @file symbedit.cpp
* @brief Functions to load from and save to file component libraries and symbols.
*/
#include <fctsys.h>
#include <kiway.h>
#include <pgm_base.h>
#include <class_drawpanel.h>
#include <confirm.h>
#include <kicad_string.h>
#include <gestfich.h>
#include <class_sch_screen.h>
#include <general.h>
#include <libeditframe.h>
#include <class_library.h>
#include <wildcards_and_files_ext.h>
#include <boost/foreach.hpp>
void LIB_EDIT_FRAME::LoadOneSymbol()
{
LIB_PART* part = GetCurPart();
// Exit if no library entry is selected or a command is in progress.
if( !part || ( m_drawItem && m_drawItem->GetFlags() ) )
return;
PROJECT& prj = Prj();
SEARCH_STACK* search = prj.SchSearchS();
m_canvas->SetIgnoreMouseEvents( true );
wxString default_path = prj.GetRString( PROJECT::SCH_LIB_PATH );
if( !default_path )
default_path = search->LastVisitedPath();
wxFileDialog dlg( this, _( "Import Symbol Drawings" ), default_path,
wxEmptyString, SchematicSymbolFileWildcard,
wxFD_OPEN | wxFD_FILE_MUST_EXIST );
if( dlg.ShowModal() == wxID_CANCEL )
return;
SetCrossHairPosition( wxPoint( 0, 0 ) );
m_canvas->MoveCursorToCrossHair();
m_canvas->SetIgnoreMouseEvents( false );
wxString filename = dlg.GetPath();
prj.SetRString( PROJECT::SCH_LIB_PATH, filename );
std::auto_ptr<PART_LIB> lib( new PART_LIB( LIBRARY_TYPE_SYMBOL, filename ) );
wxString err;
if( !lib->Load( err ) )
{
wxString msg = wxString::Format( _(
"Error '%s' occurred loading part file '%s'." ),
GetChars( err ),
GetChars( filename )
);
DisplayError( this, msg );
return;
}
if( lib->IsEmpty() )
{
wxString msg = wxString::Format( _(
"No parts found in part file '%s'." ),
GetChars( filename )
);
DisplayError( this, msg );
return;
}
if( lib->GetCount() > 1 )
{
wxString msg = wxString::Format( _(
"More than one part in part file '%s'." ),
GetChars( filename )
);
wxMessageBox( msg, _( "Warning" ), wxOK | wxICON_EXCLAMATION, this );
}
LIB_PART* first = lib->GetFirstEntry()->GetPart();
LIB_ITEMS& drawList = first->GetDrawItemList();
BOOST_FOREACH( LIB_ITEM& item, drawList )
{
if( item.Type() == LIB_FIELD_T )
continue;
if( item.GetUnit() )
item.SetUnit( m_unit );
if( item.GetConvert() )
item.SetConvert( m_convert );
item.SetFlags( IS_NEW | SELECTED );
LIB_ITEM* newItem = (LIB_ITEM*) item.Clone();
newItem->SetParent( part );
part->AddDrawItem( newItem );
}
part->RemoveDuplicateDrawItems();
part->ClearSelectedItems();
OnModify();
m_canvas->Refresh();
}
void LIB_EDIT_FRAME::SaveOneSymbol()
{
wxString msg;
PROJECT& prj = Prj();
SEARCH_STACK* search = prj.SchSearchS();
LIB_PART* part = GetCurPart();
if( !part || part->GetDrawItemList().empty() )
return;
wxString default_path = prj.GetRString( PROJECT::SCH_LIB_PATH );
if( !default_path )
default_path = search->LastVisitedPath();
wxFileDialog dlg( this, _( "Export Symbol Drawings" ), default_path,
part->GetName(), SchematicSymbolFileWildcard,
wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
if( dlg.ShowModal() == wxID_CANCEL )
return;
wxFileName fn = dlg.GetPath();
/* The GTK file chooser doesn't return the file extension added to
* file name so add it here. */
if( fn.GetExt().IsEmpty() )
fn.SetExt( SchematicSymbolFileExtension );
prj.SetRString( PROJECT::SCH_LIB_PATH, fn.GetPath() );
msg.Printf( _( "Saving symbol in '%s'" ), GetChars( fn.GetPath() ) );
SetStatusText( msg );
wxString line;
// File header
line << wxT( LIBFILE_IDENT ) << wxT( " " ) << LIB_VERSION_MAJOR
<< wxT( "." ) << LIB_VERSION_MINOR << wxT( " SYMBOL " )
<< wxT( "Date: " ) << DateAndTime() << wxT( "\n" );
// Component name comment and definition.
line << wxT( "# SYMBOL " ) << part->GetName() << wxT( "\n#\nDEF " )
<< part->GetName() << wxT( " " );
if( !part->GetReferenceField().GetText().IsEmpty() )
line << part->GetReferenceField().GetText() << wxT( " " );
else
line << wxT( "~ " );
line << 0 << wxT( " " ) << part->GetPinNameOffset() << wxT( " " );
if( part->ShowPinNumbers() )
line << wxT( "Y " );
else
line << wxT( "N " );
if( part->ShowPinNames() )
line << wxT( "Y " );
else
line << wxT( "N " );
line << wxT( "1 0 N\n" );
try
{
FILE_OUTPUTFORMATTER formatter( fn.GetFullPath() );
try
{
formatter.Print( 0, "%s", TO_UTF8( line ) );
part->GetReferenceField().Save( formatter );
part->GetValueField().Save( formatter );
formatter.Print( 0, "DRAW\n" );
LIB_ITEMS& drawList = part->GetDrawItemList();
BOOST_FOREACH( LIB_ITEM& item, drawList )
{
if( item.Type() == LIB_FIELD_T )
continue;
// Don't save unused parts or alternate body styles.
if( m_unit && item.GetUnit() && ( item.GetUnit() != m_unit ) )
continue;
if( m_convert && item.GetConvert() && ( item.GetConvert() != m_convert ) )
continue;
item.Save( formatter );
}
formatter.Print( 0, "ENDDRAW\n" );
formatter.Print( 0, "ENDDEF\n" );
}
catch( const IO_ERROR& ioe )
{
msg.Printf( _( "An error occurred attempting to save symbol file '%s'" ),
GetChars( fn.GetFullPath() ) );
DisplayError( this, msg );
}
}
catch( const IO_ERROR& ioe )
{
DisplayError( this, ioe.errorText );
return;
}
}
void LIB_EDIT_FRAME::PlaceAnchor()
{
if( LIB_PART* part = GetCurPart() )
{
const wxPoint& cross_hair = GetCrossHairPosition();
wxPoint offset( -cross_hair.x, cross_hair.y );
OnModify( );
part->SetOffset( offset );
// Redraw the symbol
RedrawScreen( wxPoint( 0 , 0 ), true );
m_canvas->Refresh();
}
}
|