2007-08-24 15:10:46 +00:00
|
|
|
/*************************************************/
|
2009-11-04 20:46:53 +00:00
|
|
|
/* Functions to Load from file and save to file */
|
|
|
|
/* the graphic shapes used to draw a component */
|
2009-09-02 18:12:45 +00:00
|
|
|
/* When using the import/export symbol options */
|
2009-08-27 11:41:56 +00:00
|
|
|
/* files are the *.sym files */
|
2007-08-24 15:10:46 +00:00
|
|
|
/*************************************************/
|
2007-05-06 16:03:28 +00:00
|
|
|
|
|
|
|
#include "fctsys.h"
|
2009-04-14 16:45:22 +00:00
|
|
|
#include "appl_wxstruct.h"
|
2010-11-10 15:30:12 +00:00
|
|
|
#include "macros.h"
|
2009-02-04 15:25:03 +00:00
|
|
|
#include "class_drawpanel.h"
|
|
|
|
#include "confirm.h"
|
|
|
|
#include "kicad_string.h"
|
|
|
|
#include "gestfich.h"
|
2010-11-10 15:30:12 +00:00
|
|
|
#include "class_sch_screen.h"
|
2009-02-04 15:25:03 +00:00
|
|
|
|
2007-05-06 16:03:28 +00:00
|
|
|
#include "general.h"
|
|
|
|
#include "protos.h"
|
2010-02-16 16:21:52 +00:00
|
|
|
#include "libeditframe.h"
|
2009-09-25 18:49:04 +00:00
|
|
|
#include "class_library.h"
|
2007-05-06 16:03:28 +00:00
|
|
|
|
2009-10-05 17:52:41 +00:00
|
|
|
#include <boost/foreach.hpp>
|
2009-12-15 21:11:05 +00:00
|
|
|
#include <wx/ffile.h>
|
2009-10-05 17:52:41 +00:00
|
|
|
|
2007-05-06 16:03:28 +00:00
|
|
|
|
2009-08-27 11:41:56 +00:00
|
|
|
/*
|
2009-11-04 20:46:53 +00:00
|
|
|
* Read a component shape file (symbol file *.sym ) and add data (graphic
|
2009-08-27 11:41:56 +00:00
|
|
|
* items) to the current component.
|
|
|
|
*
|
|
|
|
* A symbol file *.sym has the same format as a library, and contains only
|
|
|
|
* one symbol
|
2007-08-24 15:10:46 +00:00
|
|
|
*/
|
2010-11-19 16:28:46 +00:00
|
|
|
void LIB_EDIT_FRAME::LoadOneSymbol( void )
|
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. */
|
2009-09-25 18:49:04 +00:00
|
|
|
if( m_component == NULL || ( m_drawItem && m_drawItem->m_Flags ) )
|
2009-03-04 17:19:08 +00:00
|
|
|
return;
|
|
|
|
|
2007-08-24 15:10:46 +00:00
|
|
|
DrawPanel->m_IgnoreMouseEvents = TRUE;
|
|
|
|
|
2009-09-04 18:57:37 +00:00
|
|
|
wxString default_path = wxGetApp().ReturnLastVisitedLibraryPath();
|
|
|
|
|
|
|
|
wxFileDialog dlg( this, _( "Import Symbol Drawings" ), default_path,
|
|
|
|
wxEmptyString, SymbolFileWildcard,
|
|
|
|
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 ) );
|
|
|
|
DrawPanel->MoveCursorToCrossHair();
|
2007-08-24 15:10:46 +00:00
|
|
|
DrawPanel->m_IgnoreMouseEvents = FALSE;
|
|
|
|
|
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;
|
2010-10-25 15:43:42 +00:00
|
|
|
if( item.GetUnit() )
|
|
|
|
item.SetUnit( m_unit );
|
|
|
|
if( item.GetConvert() )
|
|
|
|
item.SetConvert( m_convert );
|
2009-10-05 17:52:41 +00:00
|
|
|
item.m_Flags = IS_NEW;
|
|
|
|
item.m_Selected = IS_SELECTED;
|
|
|
|
|
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
|
|
|
|
2010-02-18 20:07:29 +00:00
|
|
|
OnModify( );
|
2009-08-27 11:41:56 +00:00
|
|
|
DrawPanel->Refresh();
|
|
|
|
|
|
|
|
delete Lib;
|
2007-05-06 16:03:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-27 11:41:56 +00:00
|
|
|
/*
|
2009-12-15 21:11:05 +00:00
|
|
|
* Save the current symbol to a file.
|
2009-08-27 11:41:56 +00:00
|
|
|
*
|
|
|
|
* The symbol file format is like the standard libraries, but there is only
|
|
|
|
* one symbol.
|
|
|
|
*
|
|
|
|
* Invisible pins are not saved
|
2007-08-24 15:10:46 +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,
|
2009-10-21 20:02:25 +00:00
|
|
|
m_component->GetName(), SymbolFileWildcard,
|
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() )
|
|
|
|
fn.SetExt( SymbolFileExtension );
|
|
|
|
|
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
|
|
|
wxFFile file( fn.GetFullPath(), wxT( "wt" ) );
|
2009-09-04 18:57:37 +00:00
|
|
|
|
2009-12-15 21:11:05 +00:00
|
|
|
if( !file.IsOpened() )
|
2007-08-24 15:10:46 +00:00
|
|
|
{
|
2009-12-15 21:11:05 +00:00
|
|
|
msg.Printf( _( "Unable to create file <%s>" ),
|
2009-10-16 17:18:23 +00:00
|
|
|
GetChars( fn.GetFullPath() ) );
|
2007-08-24 15:10:46 +00:00
|
|
|
DisplayError( this, msg );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2009-10-21 20:02:25 +00:00
|
|
|
if( !m_component->GetReferenceField().m_Text.IsEmpty() )
|
2009-12-15 21:11:05 +00:00
|
|
|
line << m_component->GetReferenceField().m_Text << 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" );
|
|
|
|
|
|
|
|
if( !file.Write( line )
|
|
|
|
|| !m_component->GetReferenceField().Save( file.fp() )
|
|
|
|
|| !m_component->GetValueField().Save( file.fp() )
|
|
|
|
|| !file.Write( wxT( "DRAW\n" ) ) )
|
|
|
|
return;
|
|
|
|
|
2011-04-27 19:44:32 +00:00
|
|
|
LIB_ITEMS& drawList = m_component->GetDrawItemList();
|
2009-10-05 17:52:41 +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;
|
2010-12-10 19:47:44 +00:00
|
|
|
|
2009-10-21 20:02:25 +00:00
|
|
|
/* Don't save unused parts or alternate body styles. */
|
2010-10-25 15:43:42 +00:00
|
|
|
if( m_unit && item.GetUnit() && ( item.GetUnit() != m_unit ) )
|
2009-10-05 17:52:41 +00:00
|
|
|
continue;
|
2010-12-10 19:47:44 +00:00
|
|
|
|
2010-10-25 15:43:42 +00:00
|
|
|
if( m_convert && item.GetConvert() && ( item.GetConvert() != m_convert ) )
|
2009-10-05 17:52:41 +00:00
|
|
|
continue;
|
|
|
|
|
2009-12-15 21:11:05 +00:00
|
|
|
if( !item.Save( file.fp() ) )
|
|
|
|
return;
|
2007-08-24 15:10:46 +00:00
|
|
|
}
|
2009-09-04 18:57:37 +00:00
|
|
|
|
2010-12-10 19:47:44 +00:00
|
|
|
if( !file.Write( wxT( "ENDDRAW\n" ) ) || !file.Write( wxT( "ENDDEF\n" ) ) )
|
2009-12-15 21:11:05 +00:00
|
|
|
return;
|
2007-05-06 16:03:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-11-04 20:46:53 +00:00
|
|
|
/*
|
|
|
|
* Place anchor reference coordinators for current component
|
|
|
|
*
|
2009-12-15 21:11:05 +00:00
|
|
|
* All coordinates of the object are offset to the cursor position.
|
2009-11-04 20:46:53 +00:00
|
|
|
*/
|
2010-11-19 16:28:46 +00:00
|
|
|
void LIB_EDIT_FRAME::PlaceAncre()
|
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 );
|
2010-10-23 13:49:14 +00:00
|
|
|
DrawPanel->Refresh();
|
2007-05-06 16:03:28 +00:00
|
|
|
}
|