kicad/eeschema/getpart.cpp

489 lines
14 KiB
C++

/*************************************************/
/* Module to handle Get & Place Library Part */
/*************************************************/
#include "fctsys.h"
#include "appl_wxstruct.h"
#include "gr_basic.h"
#include "common.h"
#include "class_drawpanel.h"
#include "confirm.h"
#include "program.h"
#include "general.h"
#include "protos.h"
#include "class_library.h"
#include "libviewfrm.h"
#include "get_component_dialog.h"
#include <boost/foreach.hpp>
/* Routines Locales */
static void ShowWhileMoving( WinEDA_DrawPanel* panel, wxDC* DC, bool erase );
static void ExitPlaceCmp( WinEDA_DrawPanel* Panel, wxDC* DC );
/* Variables locales */
static int OldTransMat[2][2];
static wxPoint OldPos;
wxString SelectFromLibBrowser( WinEDA_DrawFrame* parent )
{
WinEDA_ViewlibFrame* Viewer;
wxSemaphore semaphore( 0, 1 );
WinEDA_SchematicFrame* frame;
frame = (WinEDA_SchematicFrame*) wxGetApp().GetTopWindow();
Viewer = frame->m_ViewlibFrame;
/* Close the current Lib browser, if open, and open a new one, in
* "modal" mode */
if( Viewer )
Viewer->Destroy();
Viewer = frame->m_ViewlibFrame = new WinEDA_ViewlibFrame( frame, NULL,
&semaphore );
Viewer->AdjustScrollBars();
// Show the library viewer frame until it is closed
while( semaphore.TryWait() == wxSEMA_BUSY ) // Wait for viewer closing event
{
wxYield();
wxMilliSleep( 50 );
}
return Viewer->GetEntryName();
}
/*
* load from a library and place a component
* if libname != "", search in lib "libname"
* else search in all loaded libs
*/
SCH_COMPONENT* WinEDA_SchematicFrame::Load_Component( wxDC* DC,
const wxString& libname,
wxArrayString& HistoryList,
bool UseLibBrowser )
{
int ii, CmpCount = 0;
LIB_COMPONENT* Entry = NULL;
SCH_COMPONENT* Component = NULL;
CMP_LIBRARY* Library = NULL;
wxString Name, keys, msg;
bool AllowWildSeach = TRUE;
g_ItemToRepeat = NULL;
DrawPanel->m_IgnoreMouseEvents = TRUE;
if( !libname.IsEmpty() )
{
Library = CMP_LIBRARY::FindLibrary( libname );
if( Library != NULL )
CmpCount = Library->GetCount();
}
else
{
BOOST_FOREACH( CMP_LIBRARY& lib, CMP_LIBRARY::GetLibraryList() )
{
CmpCount += lib.GetCount();
}
}
/* Ask for a component name or key words */
msg.Printf( _( "component selection (%d items loaded):" ), CmpCount );
Name = GetComponentName( this, HistoryList, msg,
UseLibBrowser ? SelectFromLibBrowser : NULL );
Name.MakeUpper();
if( Name.IsEmpty() )
{
DrawPanel->m_IgnoreMouseEvents = FALSE;
DrawPanel->MouseToCursorSchema();
return NULL; /* annulation de commande */
}
if( Name.GetChar( 0 ) == '=' )
{
AllowWildSeach = FALSE;
keys = Name.AfterFirst( '=' );
Name = DataBaseGetName( this, keys, Name );
if( Name.IsEmpty() )
{
DrawPanel->m_IgnoreMouseEvents = FALSE;
DrawPanel->MouseToCursorSchema();
return NULL;
}
}
else if( Name == wxT( "*" ) )
{
AllowWildSeach = FALSE;
if( GetNameOfPartToLoad( this, Library, Name ) == 0 )
{
DrawPanel->m_IgnoreMouseEvents = FALSE;
DrawPanel->MouseToCursorSchema();
return NULL;
}
}
else if( Name.Contains( wxT( "?" ) ) || Name.Contains( wxT( "*" ) ) )
{
AllowWildSeach = FALSE;
Name = DataBaseGetName( this, keys, Name );
if( Name.IsEmpty() )
{
DrawPanel->m_IgnoreMouseEvents = FALSE;
DrawPanel->MouseToCursorSchema();
return NULL;
}
}
Entry = CMP_LIBRARY::FindLibraryComponent( Name, libname );
if( (Entry == NULL) && AllowWildSeach ) /* Attemp to search with wildcard */
{
AllowWildSeach = FALSE;
wxString wildname = wxChar( '*' ) + Name + wxChar( '*' );
Name = wildname;
Name = DataBaseGetName( this, keys, Name );
if( !Name.IsEmpty() )
Entry = CMP_LIBRARY::FindLibraryComponent( Name, libname );
if( Entry == NULL )
{
DrawPanel->m_IgnoreMouseEvents = FALSE;
DrawPanel->MouseToCursorSchema();
return NULL;
}
}
DrawPanel->m_IgnoreMouseEvents = FALSE;
DrawPanel->MouseToCursorSchema();
if( Entry == NULL )
{
msg = _( "Failed to find part " ) + Name + _( " in library" );
DisplayError( this, msg, 10 );
return NULL;
}
AddHistoryComponentName( HistoryList, Name );
DrawPanel->ManageCurseur = ShowWhileMoving;
DrawPanel->ForceCloseManageCurseur = ExitPlaceCmp;
Component = new SCH_COMPONENT( GetScreen()->m_Curseur );
Component->m_Multi = 1; /* Selection de l'unite 1 dans le boitier */
Component->m_Convert = 1;
Component->m_ChipName = Name;
Component->m_TimeStamp = GetTimeStamp();
Component->m_Flags = IS_NEW | IS_MOVED;
/* Init champ Valeur */
Component->GetField( VALUE )->m_Pos = Entry->m_Name.m_Pos + Component->m_Pos;
Component->GetField( VALUE )->ImportValues( Entry->m_Name );
Component->GetField( VALUE )->m_Text = Component->m_ChipName;
msg = Entry->m_Prefix.m_Text;
if( msg.IsEmpty() )
msg = wxT( "U" );
msg += wxT( "?" );
// update the reference -- just the prefix for now.
Component->SetRef( GetSheet(), msg );
/* Init champ Reference */
Component->GetField( REFERENCE )->m_Pos =
Entry->m_Prefix.m_Pos + Component->m_Pos;
Component->GetField( REFERENCE )->ImportValues( Entry->m_Prefix );
Component->m_PrefixString = Entry->m_Prefix.m_Text;
/* Init des autres champs si predefinis dans la librairie */
LIB_FIELD* EntryField;
for( EntryField = Entry->m_Fields; EntryField != NULL;
EntryField = EntryField->Next() )
{
if( EntryField->m_Text.IsEmpty() && EntryField->m_Name.IsEmpty() )
continue;
ii = EntryField->m_FieldId;
if( ii < 2 ) // Reference or value, already done
continue;
if( ii >= Component->GetFieldCount() )
{ // This entry has more than the default count: add extra fields
while( ii >= Component->GetFieldCount() )
{
int field_id = Component->GetFieldCount();
SCH_CMP_FIELD field( wxPoint( 0, 0 ), field_id, Component,
ReturnDefaultFieldName( ii ) );
Component->AddField( field );
}
}
SCH_CMP_FIELD* curr_field = Component->GetField( ii );
curr_field->m_Pos = Component->m_Pos + EntryField->m_Pos;
curr_field->ImportValues( *EntryField );
curr_field->m_Text = EntryField->m_Text;
curr_field->m_Name = ( ii < FIELD1 ) ? ReturnDefaultFieldName( ii ) : EntryField->m_Name;
}
DrawStructsInGhost( DrawPanel, DC, Component, wxPoint(0,0) );
ClearMsgPanel();
Component->DisplayInfo( this );
return Component;
}
/**************************************************************************/
/*** Routine de deplacement du composant. ***/
/*** Appele par GeneralControle grace a ActiveScreen->ManageCurseur. ***/
/**************************************************************************/
static void ShowWhileMoving( WinEDA_DrawPanel* panel, wxDC* DC, bool erase )
{
wxPoint move_vector;
SCH_SCREEN* screen = (SCH_SCREEN*) panel->GetScreen();
SCH_COMPONENT* Component = (SCH_COMPONENT*) screen->GetCurItem();
/* Effacement du composant */
if( erase )
{
DrawStructsInGhost( panel, DC, Component, wxPoint(0,0) );
}
move_vector = screen->m_Curseur - Component->m_Pos;
Component->Move( move_vector );
DrawStructsInGhost( panel, DC, Component, wxPoint(0,0) );
}
/*
* Routine permettant les rotations et les miroirs d'un composant
* Si DC = NULL : pas de redessin
*/
void WinEDA_SchematicFrame::CmpRotationMiroir( SCH_COMPONENT* DrawComponent,
wxDC* DC, int type_rotate )
{
if( DrawComponent == NULL )
return;
/* Efface le trace precedent */
if( DC )
{
DrawPanel->CursorOff( DC );
if( DrawComponent->m_Flags )
DrawStructsInGhost( DrawPanel, DC, DrawComponent, wxPoint( 0, 0 ) );
else
{
DrawPanel->PostDirtyRect( DrawComponent->GetBoundingBox() );
}
}
DrawComponent->SetRotationMiroir( type_rotate );
/* Redessine le composant dans la nouvelle position */
if( DC )
{
if( DrawComponent->m_Flags )
DrawStructsInGhost( DrawPanel, DC, DrawComponent, wxPoint(0,0) );
else
DrawComponent->Draw( DrawPanel, DC, wxPoint( 0, 0 ),
GR_DEFAULT_DRAWMODE );
DrawPanel->CursorOn( DC );
}
TestDanglingEnds( GetScreen()->EEDrawList, DC );
GetScreen()->SetModify();
}
/*
* Routine de sortie de la fonction de placement de composant
*/
static void ExitPlaceCmp( WinEDA_DrawPanel* Panel, wxDC* DC )
{
SCH_SCREEN* screen = (SCH_SCREEN*) Panel->GetScreen();
SCH_COMPONENT* Component = (SCH_COMPONENT*) screen->GetCurItem();
if( Component->m_Flags & IS_NEW ) /* Nouveau Placement en cours, on l'efface */
{
Component->m_Flags = 0;
SAFE_DELETE( Component );
}
else if( Component ) /* Deplacement ancien composant en cours */
{
wxPoint move_vector = OldPos - Component->m_Pos;
Component->Move( move_vector );
memcpy( Component->m_Transform, OldTransMat, sizeof(OldTransMat) );
Component->m_Flags = 0;
}
Panel->Refresh( TRUE );
Panel->ManageCurseur = NULL;
Panel->ForceCloseManageCurseur = NULL;
screen->SetCurItem( NULL );
}
/*
* Selection de l'unite dans les boitiers a multiples Parts
*/
void WinEDA_SchematicFrame::SelPartUnit( SCH_COMPONENT* DrawComponent,
int unit, wxDC* DC )
{
int m_UnitCount;
LIB_COMPONENT* LibEntry;
if( DrawComponent == NULL )
return;
LibEntry = CMP_LIBRARY::FindLibraryComponent( DrawComponent->m_ChipName );
if( LibEntry == NULL )
return;
m_UnitCount = LibEntry->GetPartCount();
if( m_UnitCount <= 1 )
return;
if( DrawComponent->m_Multi == unit )
return;
if( unit < 1 )
unit = 1;
if( unit > m_UnitCount )
unit = m_UnitCount;
/* Efface le trace precedent */
if( DrawComponent->m_Flags )
DrawStructsInGhost( DrawPanel, DC, DrawComponent, wxPoint( 0, 0 ) );
else
DrawComponent->Draw( DrawPanel, DC, wxPoint( 0, 0 ), g_XorMode );
/* Mise a jour du numero d'unite */
DrawComponent->SetUnitSelection( GetSheet(), unit );
DrawComponent->m_Multi = unit;
/* Redessine le composant dans la nouvelle position */
if( DrawComponent->m_Flags )
DrawStructsInGhost( DrawPanel, DC, DrawComponent, wxPoint( 0, 0 ) );
else
DrawComponent->Draw( DrawPanel, DC, wxPoint( 0, 0 ),
GR_DEFAULT_DRAWMODE );
TestDanglingEnds( GetScreen()->EEDrawList, DC );
GetScreen()->SetModify();
}
void WinEDA_SchematicFrame::ConvertPart( SCH_COMPONENT* DrawComponent,
wxDC* DC )
{
LIB_COMPONENT* LibEntry;
if( DrawComponent == NULL )
return;
LibEntry = CMP_LIBRARY::FindLibraryComponent( DrawComponent->m_ChipName );
if( LibEntry == NULL )
return;
if( !LibEntry->HasConversion() )
{
DisplayError( this, wxT( "No convert found" ) );
return;
}
/* Efface le trace precedent */
if( DrawComponent->m_Flags )
DrawStructsInGhost( DrawPanel, DC, DrawComponent, wxPoint( 0, 0 ) );
else
DrawComponent->Draw( DrawPanel, DC, wxPoint( 0, 0 ), g_XorMode );
DrawComponent->m_Convert++;
// ensure m_Convert = 0, 1 or 2
// 0 and 1 = shape 1 = not converted
// 2 = shape 2 = first converted shape
// > 2 is not used but could be used for more shapes
// like multiple shapes for a programmable component
// When m_Convert = val max, return to the first shape
if( DrawComponent->m_Convert > 2 )
DrawComponent->m_Convert = 1;
/* Redessine le composant dans la nouvelle position */
if( DrawComponent->m_Flags & IS_MOVED )
DrawStructsInGhost( DrawPanel, DC, DrawComponent, wxPoint( 0, 0 ) );
else
DrawComponent->Draw( DrawPanel, DC, wxPoint( 0, 0 ),
GR_DEFAULT_DRAWMODE );
TestDanglingEnds( GetScreen()->EEDrawList, DC );
GetScreen()->SetModify();
}
void WinEDA_SchematicFrame::StartMovePart( SCH_COMPONENT* Component,
wxDC* DC )
{
if( Component == NULL )
return;
if( Component->Type() != TYPE_SCH_COMPONENT )
return;
if( Component->m_Flags == 0 )
{
if( g_ItemToUndoCopy )
{
SAFE_DELETE( g_ItemToUndoCopy );
}
g_ItemToUndoCopy = Component->GenCopy();
}
DrawPanel->CursorOff( DC );
GetScreen()->m_Curseur = Component->m_Pos;
DrawPanel->MouseToCursorSchema();
DrawPanel->ManageCurseur = ShowWhileMoving;
DrawPanel->ForceCloseManageCurseur = ExitPlaceCmp;
GetScreen()->SetCurItem( Component );
OldPos = Component->m_Pos;
memcpy( OldTransMat, Component->m_Transform, sizeof(OldTransMat) );
#if 1
// switch from normal mode to xor mode for the duration of the move, first
// by erasing fully any "normal drawing mode" primitives with the PostDirtyRect(),
// then by drawing the first time in xor mode so that subsequent xor
// drawing modes will fully erase this first copy.
Component->m_Flags |= IS_MOVED; // omit redrawing the component, erase only
DrawPanel->PostDirtyRect( Component->GetBoundingBox() );
DrawStructsInGhost( DrawPanel, DC, Component, wxPoint(0,0) );
#else
RedrawOneStruct( DrawPanel, DC, Component, g_XorMode );
Component->m_Flags |= IS_MOVED;
DrawPanel->ManageCurseur( DrawPanel, DC, FALSE );
#endif
DrawPanel->m_AutoPAN_Request = TRUE;
DrawPanel->CursorOn( DC );
}