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"
|
2007-05-06 16:03:28 +00:00
|
|
|
#include "common.h"
|
2009-02-04 15:25:03 +00:00
|
|
|
#include "class_drawpanel.h"
|
|
|
|
#include "confirm.h"
|
|
|
|
#include "kicad_string.h"
|
|
|
|
#include "gestfich.h"
|
|
|
|
|
2007-05-06 16:03:28 +00:00
|
|
|
#include "program.h"
|
|
|
|
#include "general.h"
|
|
|
|
#include "protos.h"
|
2009-09-22 12:27:57 +00:00
|
|
|
#include "libeditfrm.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>
|
|
|
|
|
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
|
|
|
*/
|
2009-08-27 11:41:56 +00:00
|
|
|
void WinEDA_LibeditFrame::LoadOneSymbol( void )
|
2007-05-06 16:03:28 +00:00
|
|
|
{
|
2009-09-25 18:49:04 +00:00
|
|
|
LIB_COMPONENT* Component;
|
|
|
|
FILE* ImportFile;
|
|
|
|
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
|
|
|
|
|
|
|
GetScreen()->m_Curseur = wxPoint( 0, 0 );
|
|
|
|
DrawPanel->MouseToCursorSchema();
|
|
|
|
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
|
|
|
|
|
|
|
/* Load data */
|
2009-09-04 18:57:37 +00:00
|
|
|
ImportFile = wxFopen( fn.GetFullPath(), wxT( "rt" ) );
|
2009-08-27 11:41:56 +00:00
|
|
|
|
2007-08-24 15:10:46 +00:00
|
|
|
if( ImportFile == NULL )
|
|
|
|
{
|
2009-08-27 11:41:56 +00:00
|
|
|
msg.Printf( _( "Failed to open Symbol File <%s>" ),
|
2009-10-16 17:18:23 +00:00
|
|
|
GetChars( fn.GetFullPath() ) );
|
2009-08-27 11:41:56 +00:00
|
|
|
DisplayError( this, msg );
|
2007-08-24 15:10:46 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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 );
|
|
|
|
fclose( ImportFile );
|
|
|
|
delete Lib;
|
|
|
|
return;
|
|
|
|
}
|
2007-08-24 15:10:46 +00:00
|
|
|
|
|
|
|
fclose( ImportFile );
|
|
|
|
|
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-10-05 17:52:41 +00:00
|
|
|
DisplayError( this, _( "Warning: more than 1 part in symbol file." ) );
|
2007-08-24 15:10:46 +00:00
|
|
|
|
2009-09-18 14:56:05 +00:00
|
|
|
Component = (LIB_COMPONENT*) Lib->GetFirstEntry();
|
2009-10-05 17:52:41 +00:00
|
|
|
LIB_DRAW_ITEM_LIST& drawList = Component->GetDrawItemList();
|
2007-08-24 15:10:46 +00:00
|
|
|
|
2009-10-05 17:52:41 +00:00
|
|
|
BOOST_FOREACH( LIB_DRAW_ITEM& item, drawList )
|
2007-08-24 15:10:46 +00:00
|
|
|
{
|
2009-10-21 20:02:25 +00:00
|
|
|
if( item.Type() == COMPONENT_FIELD_DRAW_TYPE )
|
|
|
|
continue;
|
2009-10-05 17:52:41 +00:00
|
|
|
if( item.m_Unit )
|
|
|
|
item.m_Unit = m_unit;
|
|
|
|
if( item.m_Convert )
|
|
|
|
item.m_Convert = m_convert;
|
|
|
|
item.m_Flags = IS_NEW;
|
|
|
|
item.m_Selected = IS_SELECTED;
|
|
|
|
|
|
|
|
LIB_DRAW_ITEM* newItem = item.GenCopy();
|
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-08-27 11:41:56 +00:00
|
|
|
// Remove duplicated drawings:
|
2009-09-25 18:49:04 +00:00
|
|
|
m_component->RemoveDuplicateDrawItems();
|
2007-08-24 15:10:46 +00:00
|
|
|
|
2009-08-27 11:41:56 +00:00
|
|
|
// Clear flags
|
2009-09-29 18:38:21 +00:00
|
|
|
m_component->ClearSelectedItems();
|
2007-08-24 15:10:46 +00:00
|
|
|
|
2009-08-27 11:41:56 +00:00
|
|
|
GetScreen()->SetModify();
|
|
|
|
DrawPanel->Refresh();
|
|
|
|
|
|
|
|
delete Lib;
|
2007-05-06 16:03:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-27 11:41:56 +00:00
|
|
|
/*
|
|
|
|
* Save in file the current symbol.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
*/
|
2009-08-27 11:41:56 +00:00
|
|
|
void WinEDA_LibeditFrame::SaveOneSymbol()
|
2007-05-06 16:03:28 +00:00
|
|
|
{
|
2009-10-05 17:52:41 +00:00
|
|
|
wxString msg;
|
|
|
|
FILE* ExportFile;
|
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-09-04 18:57:37 +00:00
|
|
|
ExportFile = wxFopen( fn.GetFullPath(), wxT( "wt" ) );
|
|
|
|
|
2007-08-24 15:10:46 +00:00
|
|
|
if( ExportFile == NULL )
|
|
|
|
{
|
2009-09-04 18:57:37 +00:00
|
|
|
msg.Printf( _( "Unable to create <%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-10-16 17:18:23 +00:00
|
|
|
msg.Printf( _( "Save Symbol in [%s]" ), GetChars( fn.GetPath() ) );
|
2007-08-24 15:10:46 +00:00
|
|
|
Affiche_Message( msg );
|
|
|
|
|
|
|
|
char Line[256];
|
|
|
|
fprintf( ExportFile, "%s %d.%d %s Date: %s\n", LIBFILE_IDENT,
|
2009-08-27 11:41:56 +00:00
|
|
|
LIB_VERSION_MAJOR, LIB_VERSION_MINOR,
|
|
|
|
"SYMBOL", DateAndTime( Line ) );
|
2007-08-24 15:10:46 +00:00
|
|
|
|
2009-11-04 20:46:53 +00:00
|
|
|
/* Component name. */
|
2007-08-24 15:10:46 +00:00
|
|
|
fprintf( ExportFile, "# SYMBOL %s\n#\n",
|
2009-10-21 20:02:25 +00:00
|
|
|
CONV_TO_UTF8( m_component->GetName() ) );
|
2007-08-24 15:10:46 +00:00
|
|
|
|
2009-08-27 11:41:56 +00:00
|
|
|
fprintf( ExportFile, "DEF %s",
|
2009-10-21 20:02:25 +00:00
|
|
|
CONV_TO_UTF8( m_component->GetName() ) );
|
|
|
|
if( !m_component->GetReferenceField().m_Text.IsEmpty() )
|
2009-08-27 11:41:56 +00:00
|
|
|
fprintf( ExportFile, " %s",
|
2009-10-21 20:02:25 +00:00
|
|
|
CONV_TO_UTF8( m_component->GetReferenceField().m_Text ) );
|
2007-08-24 15:10:46 +00:00
|
|
|
else
|
|
|
|
fprintf( ExportFile, " ~" );
|
|
|
|
|
|
|
|
fprintf( ExportFile, " %d %d %c %c %d %d %c\n",
|
|
|
|
0, /* unused */
|
2009-09-25 18:49:04 +00:00
|
|
|
m_component->m_TextInside,
|
|
|
|
m_component->m_DrawPinNum ? 'Y' : 'N',
|
|
|
|
m_component->m_DrawPinName ? 'Y' : 'N',
|
2007-08-24 15:10:46 +00:00
|
|
|
1, 0 /* unused */, 'N' );
|
|
|
|
|
2009-10-21 20:02:25 +00:00
|
|
|
m_component->GetReferenceField().Save( ExportFile );
|
|
|
|
m_component->GetValueField().Save( ExportFile );
|
2007-05-06 16:03:28 +00:00
|
|
|
|
2009-10-05 17:52:41 +00:00
|
|
|
LIB_DRAW_ITEM_LIST& drawList = m_component->GetDrawItemList();
|
|
|
|
|
|
|
|
fprintf( ExportFile, "DRAW\n" );
|
|
|
|
|
|
|
|
BOOST_FOREACH( LIB_DRAW_ITEM& item, drawList )
|
2007-08-24 15:10:46 +00:00
|
|
|
{
|
2009-10-21 20:02:25 +00:00
|
|
|
if( item.Type() == COMPONENT_FIELD_DRAW_TYPE )
|
|
|
|
continue;
|
|
|
|
/* Don't save unused parts or alternate body styles. */
|
2009-10-05 17:52:41 +00:00
|
|
|
if( m_unit && item.m_Unit && ( item.m_Unit != m_unit ) )
|
|
|
|
continue;
|
|
|
|
if( m_convert && item.m_Convert && ( item.m_Convert != m_convert ) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
item.Save( ExportFile );
|
2007-08-24 15:10:46 +00:00
|
|
|
}
|
2009-09-04 18:57:37 +00:00
|
|
|
|
2009-10-05 17:52:41 +00:00
|
|
|
fprintf( ExportFile, "ENDDRAW\n" );
|
2007-08-24 15:10:46 +00:00
|
|
|
fprintf( ExportFile, "ENDDEF\n" );
|
|
|
|
fclose( ExportFile );
|
2007-05-06 16:03:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-11-04 20:46:53 +00:00
|
|
|
/*
|
|
|
|
* Place anchor reference coordinators for current component
|
|
|
|
*
|
|
|
|
* All coordinates of the object are offset to the cursor position * /
|
|
|
|
*/
|
2007-09-01 12:00:30 +00:00
|
|
|
void WinEDA_LibeditFrame::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;
|
|
|
|
|
2009-09-14 13:24:17 +00:00
|
|
|
wxPoint offset( -GetScreen()->m_Curseur.x, GetScreen()->m_Curseur.y );
|
2008-11-21 21:17:15 +00:00
|
|
|
|
|
|
|
GetScreen()->SetModify();
|
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 */
|
2008-11-21 21:17:15 +00:00
|
|
|
GetScreen()->m_Curseur.x = GetScreen()->m_Curseur.y = 0;
|
2007-08-24 15:10:46 +00:00
|
|
|
Recadre_Trace( TRUE );
|
2008-11-21 21:17:15 +00:00
|
|
|
GetScreen()->SetRefreshReq();
|
2007-05-06 16:03:28 +00:00
|
|
|
}
|