2011-11-01 15:06:26 +00:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2011-10-07 14:41:30 +00:00
|
|
|
/**
|
|
|
|
* @file symbedit.cpp
|
|
|
|
* @brief Functions to load from and save to file component libraries and symbols.
|
|
|
|
*/
|
2007-05-06 16:03:28 +00:00
|
|
|
|
2012-01-23 04:33:36 +00:00
|
|
|
#include <fctsys.h>
|
|
|
|
#include <appl_wxstruct.h>
|
|
|
|
#include <class_drawpanel.h>
|
|
|
|
#include <confirm.h>
|
|
|
|
#include <kicad_string.h>
|
|
|
|
#include <gestfich.h>
|
|
|
|
#include <class_sch_screen.h>
|
2009-02-04 15:25:03 +00:00
|
|
|
|
2012-01-23 04:33:36 +00:00
|
|
|
#include <general.h>
|
|
|
|
#include <libeditframe.h>
|
|
|
|
#include <class_library.h>
|
2012-09-28 17:47:41 +00:00
|
|
|
#include <wildcards_and_files_ext.h>
|
2007-05-06 16:03:28 +00:00
|
|
|
|
2009-10-05 17:52:41 +00:00
|
|
|
#include <boost/foreach.hpp>
|
|
|
|
|
2007-05-06 16:03:28 +00:00
|
|
|
|
2011-10-07 14:41:30 +00:00
|
|
|
void LIB_EDIT_FRAME::LoadOneSymbol()
|
2007-05-06 16:03:28 +00:00
|
|
|
{
|
2009-09-25 18:49:04 +00:00
|
|
|
LIB_COMPONENT* Component;
|
|
|
|
wxString msg, err;
|
|
|
|
CMP_LIBRARY* Lib;
|
2009-08-27 11:41:56 +00:00
|
|
|
|
|
|
|
/* Exit if no library entry is selected or a command is in progress. */
|
2011-12-21 13:42:02 +00:00
|
|
|
if( m_component == NULL || ( m_drawItem && m_drawItem->GetFlags() ) )
|
2009-03-04 17:19:08 +00:00
|
|
|
return;
|
|
|
|
|
2011-12-29 20:11:42 +00:00
|
|
|
m_canvas->SetIgnoreMouseEvents( true );
|
2007-08-24 15:10:46 +00:00
|
|
|
|
2009-09-04 18:57:37 +00:00
|
|
|
wxString default_path = wxGetApp().ReturnLastVisitedLibraryPath();
|
|
|
|
|
|
|
|
wxFileDialog dlg( this, _( "Import Symbol Drawings" ), default_path,
|
2012-09-28 17:47:41 +00:00
|
|
|
wxEmptyString, SchematicSymbolFileWildcard,
|
2009-09-04 18:57:37 +00:00
|
|
|
wxFD_OPEN | wxFD_FILE_MUST_EXIST );
|
2009-04-14 16:45:22 +00:00
|
|
|
|
2009-09-04 18:57:37 +00:00
|
|
|
if( dlg.ShowModal() == wxID_CANCEL )
|
|
|
|
return;
|
2007-08-24 15:10:46 +00:00
|
|
|
|
2011-02-11 20:48:13 +00:00
|
|
|
GetScreen()->SetCrossHairPosition( wxPoint( 0, 0 ) );
|
2011-12-22 13:28:11 +00:00
|
|
|
m_canvas->MoveCursorToCrossHair();
|
2011-12-29 20:11:42 +00:00
|
|
|
m_canvas->SetIgnoreMouseEvents( false );
|
2007-08-24 15:10:46 +00:00
|
|
|
|
2009-09-04 18:57:37 +00:00
|
|
|
wxFileName fn = dlg.GetPath();
|
2009-08-27 11:41:56 +00:00
|
|
|
wxGetApp().SaveLastVisitedLibraryPath( fn.GetPath() );
|
2007-08-24 15:10:46 +00:00
|
|
|
|
2009-09-18 14:56:05 +00:00
|
|
|
Lib = new CMP_LIBRARY( LIBRARY_TYPE_SYMBOL, fn );
|
2009-08-27 11:41:56 +00:00
|
|
|
|
|
|
|
if( !Lib->Load( err ) )
|
|
|
|
{
|
|
|
|
msg.Printf( _( "Error <%s> occurred loading symbol library <%s>." ),
|
2009-10-16 17:18:23 +00:00
|
|
|
GetChars( err ), GetChars( fn.GetName() ) );
|
2009-08-27 11:41:56 +00:00
|
|
|
DisplayError( this, msg );
|
|
|
|
delete Lib;
|
|
|
|
return;
|
|
|
|
}
|
2007-08-24 15:10:46 +00:00
|
|
|
|
2009-08-27 11:41:56 +00:00
|
|
|
if( Lib->IsEmpty() )
|
|
|
|
{
|
|
|
|
msg.Printf( _( "No components found in symbol library <%s>." ),
|
2009-10-16 17:18:23 +00:00
|
|
|
GetChars( fn.GetName() ) );
|
2009-08-27 11:41:56 +00:00
|
|
|
delete Lib;
|
2007-08-24 15:10:46 +00:00
|
|
|
return;
|
2009-08-27 11:41:56 +00:00
|
|
|
}
|
2007-08-24 15:10:46 +00:00
|
|
|
|
2009-09-02 18:12:45 +00:00
|
|
|
if( Lib->GetCount() > 1 )
|
2009-12-15 21:11:05 +00:00
|
|
|
{
|
|
|
|
msg.Printf( _( "More than one part in symbol file <%s>." ),
|
|
|
|
GetChars( fn.GetName() ) );
|
|
|
|
wxMessageBox( msg, _( "Warning" ), wxOK | wxICON_EXCLAMATION, this );
|
|
|
|
}
|
2007-08-24 15:10:46 +00:00
|
|
|
|
2010-10-28 16:30:48 +00:00
|
|
|
Component = Lib->GetFirstEntry()->GetComponent();
|
2011-04-27 19:44:32 +00:00
|
|
|
LIB_ITEMS& drawList = Component->GetDrawItemList();
|
2007-08-24 15:10:46 +00:00
|
|
|
|
2011-04-27 19:44:32 +00:00
|
|
|
BOOST_FOREACH( LIB_ITEM& item, drawList )
|
2007-08-24 15:10:46 +00:00
|
|
|
{
|
2010-12-10 19:47:44 +00:00
|
|
|
if( item.Type() == LIB_FIELD_T )
|
2009-10-21 20:02:25 +00:00
|
|
|
continue;
|
2011-11-01 15:06:26 +00:00
|
|
|
|
2010-10-25 15:43:42 +00:00
|
|
|
if( item.GetUnit() )
|
|
|
|
item.SetUnit( m_unit );
|
2011-11-01 15:06:26 +00:00
|
|
|
|
2010-10-25 15:43:42 +00:00
|
|
|
if( item.GetConvert() )
|
|
|
|
item.SetConvert( m_convert );
|
2011-11-01 15:06:26 +00:00
|
|
|
|
2011-12-21 13:42:02 +00:00
|
|
|
item.SetFlags( IS_NEW | SELECTED );
|
2009-10-05 17:52:41 +00:00
|
|
|
|
2011-04-27 19:44:32 +00:00
|
|
|
LIB_ITEM* newItem = (LIB_ITEM*) item.Clone();
|
2009-09-29 18:38:21 +00:00
|
|
|
newItem->SetParent( m_component );
|
|
|
|
m_component->AddDrawItem( newItem );
|
2009-08-27 11:41:56 +00:00
|
|
|
}
|
2009-03-04 17:19:08 +00:00
|
|
|
|
2009-09-25 18:49:04 +00:00
|
|
|
m_component->RemoveDuplicateDrawItems();
|
2009-09-29 18:38:21 +00:00
|
|
|
m_component->ClearSelectedItems();
|
2007-08-24 15:10:46 +00:00
|
|
|
|
2011-12-21 13:42:02 +00:00
|
|
|
OnModify();
|
2011-12-22 13:28:11 +00:00
|
|
|
m_canvas->Refresh();
|
2009-08-27 11:41:56 +00:00
|
|
|
|
|
|
|
delete Lib;
|
2007-05-06 16:03:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-19 16:28:46 +00:00
|
|
|
void LIB_EDIT_FRAME::SaveOneSymbol()
|
2007-05-06 16:03:28 +00:00
|
|
|
{
|
2009-10-05 17:52:41 +00:00
|
|
|
wxString msg;
|
2007-08-24 15:10:46 +00:00
|
|
|
|
2009-10-05 17:52:41 +00:00
|
|
|
if( m_component->GetDrawItemList().empty() )
|
2007-08-24 15:10:46 +00:00
|
|
|
return;
|
|
|
|
|
2009-09-04 18:57:37 +00:00
|
|
|
wxString default_path = wxGetApp().ReturnLastVisitedLibraryPath();
|
|
|
|
|
|
|
|
wxFileDialog dlg( this, _( "Export Symbol Drawings" ), default_path,
|
2012-09-28 17:47:41 +00:00
|
|
|
m_component->GetName(), SchematicSymbolFileWildcard,
|
2009-09-04 18:57:37 +00:00
|
|
|
wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
|
|
|
|
|
|
|
|
if( dlg.ShowModal() == wxID_CANCEL )
|
2007-08-24 15:10:46 +00:00
|
|
|
return;
|
|
|
|
|
2009-09-04 18:57:37 +00:00
|
|
|
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() )
|
2012-09-28 17:47:41 +00:00
|
|
|
fn.SetExt( SchematicSymbolFileExtension );
|
2009-09-04 18:57:37 +00:00
|
|
|
|
2009-08-27 11:41:56 +00:00
|
|
|
wxGetApp().SaveLastVisitedLibraryPath( fn.GetPath() );
|
2009-04-14 16:45:22 +00:00
|
|
|
|
2009-12-15 21:11:05 +00:00
|
|
|
msg.Printf( _( "Saving symbol in [%s]" ), GetChars( fn.GetPath() ) );
|
2010-03-16 18:22:59 +00:00
|
|
|
SetStatusText( msg );
|
2007-08-24 15:10:46 +00:00
|
|
|
|
2009-12-15 21:11:05 +00:00
|
|
|
wxString line;
|
|
|
|
|
|
|
|
/* File header */
|
|
|
|
line << wxT( LIBFILE_IDENT ) << wxT( " " ) << LIB_VERSION_MAJOR
|
|
|
|
<< wxT( "." ) << LIB_VERSION_MINOR << wxT( " SYMBOL " )
|
|
|
|
<< wxT( "Date: " ) << DateAndTime() << wxT( "\n" );
|
2007-08-24 15:10:46 +00:00
|
|
|
|
2009-12-15 21:11:05 +00:00
|
|
|
/* Component name comment and definition. */
|
|
|
|
line << wxT( "# SYMBOL " ) << m_component->GetName() << wxT( "\n#\nDEF " )
|
|
|
|
<< m_component->GetName() << wxT( " " );
|
2007-08-24 15:10:46 +00:00
|
|
|
|
2013-03-18 19:36:07 +00:00
|
|
|
if( !m_component->GetReferenceField().GetText().IsEmpty() )
|
|
|
|
line << m_component->GetReferenceField().GetText() << wxT( " " );
|
2007-08-24 15:10:46 +00:00
|
|
|
else
|
2009-12-15 21:11:05 +00:00
|
|
|
line << wxT( "~ " );
|
2007-08-24 15:10:46 +00:00
|
|
|
|
2010-06-24 18:31:43 +00:00
|
|
|
line << 0 << wxT( " " ) << m_component->GetPinNameOffset() << wxT( " " );
|
2007-08-24 15:10:46 +00:00
|
|
|
|
2010-06-24 18:31:43 +00:00
|
|
|
if( m_component->ShowPinNumbers() )
|
2009-12-15 21:11:05 +00:00
|
|
|
line << wxT( "Y " );
|
|
|
|
else
|
|
|
|
line << wxT( "N " );
|
2007-05-06 16:03:28 +00:00
|
|
|
|
2010-06-24 18:31:43 +00:00
|
|
|
if( m_component->ShowPinNames() )
|
2009-12-15 21:11:05 +00:00
|
|
|
line << wxT( "Y " );
|
|
|
|
else
|
|
|
|
line << wxT( "N " );
|
2009-10-05 17:52:41 +00:00
|
|
|
|
2009-12-15 21:11:05 +00:00
|
|
|
line << wxT( "1 0 N\n" );
|
|
|
|
|
2011-11-01 15:06:26 +00:00
|
|
|
try
|
|
|
|
{
|
2012-10-25 15:41:20 +00:00
|
|
|
FILE_OUTPUTFORMATTER formatter( fn.GetFullPath() );
|
2009-12-15 21:11:05 +00:00
|
|
|
|
2012-10-25 15:41:20 +00:00
|
|
|
try
|
2011-11-01 15:06:26 +00:00
|
|
|
{
|
2012-10-25 15:41:20 +00:00
|
|
|
formatter.Print( 0, "%s", TO_UTF8( line ) );
|
|
|
|
m_component->GetReferenceField().Save( formatter );
|
|
|
|
m_component->GetValueField().Save( formatter );
|
|
|
|
formatter.Print( 0, "DRAW\n" );
|
2010-12-10 19:47:44 +00:00
|
|
|
|
2012-10-25 15:41:20 +00:00
|
|
|
LIB_ITEMS& drawList = m_component->GetDrawItemList();
|
2010-12-10 19:47:44 +00:00
|
|
|
|
2012-10-25 15:41:20 +00:00
|
|
|
BOOST_FOREACH( LIB_ITEM& item, drawList )
|
|
|
|
{
|
|
|
|
if( item.Type() == LIB_FIELD_T )
|
|
|
|
continue;
|
2009-10-05 17:52:41 +00:00
|
|
|
|
2012-10-25 15:41:20 +00:00
|
|
|
/* Don't save unused parts or alternate body styles. */
|
|
|
|
if( m_unit && item.GetUnit() && ( item.GetUnit() != m_unit ) )
|
|
|
|
continue;
|
2009-09-04 18:57:37 +00:00
|
|
|
|
2012-10-25 15:41:20 +00:00
|
|
|
if( m_convert && item.GetConvert() && ( item.GetConvert() != m_convert ) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
item.Save( formatter );
|
|
|
|
}
|
|
|
|
|
|
|
|
formatter.Print( 0, "ENDDRAW\n" );
|
|
|
|
formatter.Print( 0, "ENDDEF\n" );
|
|
|
|
}
|
|
|
|
catch( IO_ERROR ioe )
|
|
|
|
{
|
|
|
|
msg.Printf( _( "An error occurred attempting to save symbol file <%s>" ),
|
|
|
|
GetChars( fn.GetFullPath() ) );
|
|
|
|
DisplayError( this, msg );
|
|
|
|
}
|
2011-11-01 15:06:26 +00:00
|
|
|
}
|
|
|
|
catch( IO_ERROR ioe )
|
|
|
|
{
|
2012-10-25 15:41:20 +00:00
|
|
|
DisplayError( this, ioe.errorText );
|
|
|
|
return;
|
2011-11-01 15:06:26 +00:00
|
|
|
}
|
2007-05-06 16:03:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-10-07 14:41:30 +00:00
|
|
|
void LIB_EDIT_FRAME::PlaceAnchor()
|
2007-05-06 16:03:28 +00:00
|
|
|
{
|
2009-09-25 18:49:04 +00:00
|
|
|
if( m_component == NULL )
|
2007-08-24 15:10:46 +00:00
|
|
|
return;
|
|
|
|
|
2011-02-11 20:48:13 +00:00
|
|
|
wxPoint offset( -GetScreen()->GetCrossHairPosition().x, GetScreen()->GetCrossHairPosition().y );
|
2008-11-21 21:17:15 +00:00
|
|
|
|
2010-02-18 20:07:29 +00:00
|
|
|
OnModify( );
|
2007-08-24 15:10:46 +00:00
|
|
|
|
2009-09-25 18:49:04 +00:00
|
|
|
m_component->SetOffset( offset );
|
2007-08-24 15:10:46 +00:00
|
|
|
|
|
|
|
/* Redraw the symbol */
|
2011-02-11 20:48:13 +00:00
|
|
|
RedrawScreen( wxPoint( 0 , 0 ), true );
|
2011-12-22 13:28:11 +00:00
|
|
|
m_canvas->Refresh();
|
2007-05-06 16:03:28 +00:00
|
|
|
}
|