kicad/pcbnew/modules.cpp

527 lines
15 KiB
C++
Raw Normal View History

/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2012 Jean-Pierre Charras, jean-pierre.charras@ujf-grenoble.fr
* Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
* Copyright (C) 2012 Wayne Stambaugh <stambaughw@verizon.net>
* Copyright (C) 1992-2012 KiCad Developers, see AUTHORS.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 modules.cpp
*/
#include <fctsys.h>
#include <gr_basic.h>
#include <class_drawpanel.h>
#include <confirm.h>
#include <wxPcbStruct.h>
#include <trigo.h>
#include <macros.h>
#include <pcbcommon.h>
#include <class_board.h>
#include <class_module.h>
#include <pcbnew.h>
#include <protos.h>
#include <drag.h>
static void MoveFootprint( EDA_DRAW_PANEL* aPanel, wxDC* aDC,
const wxPoint& aPosition, bool aErase );
static void Abort_MoveOrCopyModule( EDA_DRAW_PANEL* Panel, wxDC* DC );
2010-07-20 18:11:34 +00:00
static MODULE* s_ModuleInitialCopy = NULL; /* Copy of module for
* abort/undo command
*/
static PICKED_ITEMS_LIST s_PickedList; /* a picked list to
* save initial module
* and dragged tracks
*/
/* Get a module name from user and return a pointer to the corresponding module
2007-08-10 19:14:51 +00:00
*/
MODULE* PCB_BASE_FRAME::GetModuleByName()
{
2010-07-20 18:11:34 +00:00
wxString moduleName;
MODULE* module = NULL;
2007-08-10 19:14:51 +00:00
2010-07-20 18:11:34 +00:00
wxTextEntryDialog dlg( this, _( "Name:" ), _( "Search footprint" ), moduleName );
2010-07-20 18:11:34 +00:00
if( dlg.ShowModal() != wxID_OK )
return NULL; //Aborted by user
moduleName = dlg.GetValue();
2010-07-20 18:11:34 +00:00
moduleName.Trim( true );
moduleName.Trim( false );
2010-07-20 18:11:34 +00:00
if( !moduleName.IsEmpty() )
2007-08-10 19:14:51 +00:00
{
module = GetBoard()->m_Modules;
2007-08-10 19:14:51 +00:00
while( module )
{
2010-07-20 18:11:34 +00:00
if( module->m_Reference->m_Text.CmpNoCase( moduleName ) == 0 )
2007-08-10 19:14:51 +00:00
break;
2007-08-10 19:14:51 +00:00
module = module->Next();
}
}
2007-08-10 19:14:51 +00:00
return module;
}
2007-08-10 19:14:51 +00:00
void PCB_EDIT_FRAME::StartMoveModule( MODULE* aModule, wxDC* aDC,
bool aDragConnectedTracks )
{
if( aModule == NULL )
2007-08-10 19:14:51 +00:00
return;
2009-08-06 07:11:04 +00:00
if( s_ModuleInitialCopy )
delete s_ModuleInitialCopy;
s_PickedList.ClearItemsList(); // Should be empty, but...
// Creates a copy of the current module, for abort and undo commands
s_ModuleInitialCopy = (MODULE*)aModule->Clone();
s_ModuleInitialCopy->SetParent( GetBoard() );
s_ModuleInitialCopy->ClearFlags();
2009-08-06 07:11:04 +00:00
SetCurItem( aModule );
GetBoard()->m_Status_Pcb &= ~RATSNEST_ITEM_LOCAL_OK;
aModule->SetFlags( IS_MOVED );
2007-08-10 19:14:51 +00:00
/* Show ratsnest. */
2010-07-20 18:11:34 +00:00
if( GetBoard()->IsElementVisible( RATSNEST_VISIBLE ) )
DrawGeneralRatsnest( aDC );
2007-08-10 19:14:51 +00:00
EraseDragList();
2010-10-26 11:47:16 +00:00
if( aDragConnectedTracks )
2007-08-10 19:14:51 +00:00
{
DRAG_LIST drglist( GetBoard() );
drglist.BuildDragListe( aModule );
ITEM_PICKER itemWrapper( NULL, UR_CHANGED );
for( unsigned ii = 0; ii < g_DragSegmentList.size(); ii++ )
2009-08-06 07:11:04 +00:00
{
TRACK* segm = g_DragSegmentList[ii].m_Track;
2012-02-05 13:02:46 +00:00
itemWrapper.SetItem( segm );
itemWrapper.SetLink( segm->Clone() );
itemWrapper.GetLink()->SetState( IN_EDIT, OFF );
s_PickedList.PushItem( itemWrapper );
2009-08-06 07:11:04 +00:00
}
UndrawAndMarkSegmentsToDrag( m_canvas, aDC );
2007-08-10 19:14:51 +00:00
}
GetBoard()->m_Status_Pcb |= DO_NOT_SHOW_GENERAL_RASTNEST;
m_canvas->SetMouseCapture( MoveFootprint, Abort_MoveOrCopyModule );
m_canvas->SetAutoPanRequest( true );
2007-08-10 19:14:51 +00:00
// Erase the module.
if( aDC )
2008-04-01 05:21:50 +00:00
{
aModule->SetFlags( DO_NOT_DRAW );
m_canvas->RefreshDrawingRect( aModule->GetBoundingBox() );
aModule->ClearFlags( DO_NOT_DRAW );
2008-04-01 05:21:50 +00:00
}
2007-08-10 19:14:51 +00:00
m_canvas->CallMouseCapture( aDC, wxDefaultPosition, false );
}
2007-08-10 19:14:51 +00:00
/* Called on a move or copy module command abort
*/
void Abort_MoveOrCopyModule( EDA_DRAW_PANEL* Panel, wxDC* DC )
{
TRACK* pt_segm;
MODULE* module;
PCB_EDIT_FRAME* pcbframe = (PCB_EDIT_FRAME*) Panel->GetParent();
2007-08-10 19:14:51 +00:00
module = (MODULE*) pcbframe->GetScreen()->GetCurItem();
pcbframe->GetBoard()->m_Status_Pcb &= ~RATSNEST_ITEM_LOCAL_OK;
Panel->SetMouseCapture( NULL, NULL );
2007-08-10 19:14:51 +00:00
if( module )
{
// Erase the current footprint on screen
2007-08-10 19:14:51 +00:00
DrawModuleOutlines( Panel, DC, module );
/* If a move command: return to old position
* If a copy command, delete the new footprint
*/
if( module->IsMoving() )
2007-08-10 19:14:51 +00:00
{
/* Restore old position for dragged tracks */
for( unsigned ii = 0; ii < g_DragSegmentList.size(); ii++ )
2007-08-10 19:14:51 +00:00
{
pt_segm = g_DragSegmentList[ii].m_Track;
pt_segm->Draw( Panel, DC, GR_XOR );
pt_segm->SetState( IN_EDIT, OFF );
g_DragSegmentList[ii].RestoreInitialValues();
2007-08-10 19:14:51 +00:00
pt_segm->Draw( Panel, DC, GR_OR );
}
EraseDragList();
module->ClearFlags( IS_MOVED );
2007-08-10 19:14:51 +00:00
}
if( module->IsNew() ) // Copy command: delete new footprint
2007-08-10 19:14:51 +00:00
{
module->DeleteStructure();
2007-08-10 19:14:51 +00:00
module = NULL;
pcbframe->GetBoard()->m_Status_Pcb = 0;
pcbframe->GetBoard()->BuildListOfNets();
2007-08-10 19:14:51 +00:00
}
}
/* Redraw the module. */
2009-08-06 07:11:04 +00:00
if( module && s_ModuleInitialCopy )
2007-08-10 19:14:51 +00:00
{
2009-08-06 07:11:04 +00:00
if( s_ModuleInitialCopy->m_Orient != module->m_Orient )
pcbframe->Rotate_Module( NULL, module, s_ModuleInitialCopy->m_Orient, false );
2009-08-06 07:11:04 +00:00
if( s_ModuleInitialCopy->GetLayer() != module->GetLayer() )
pcbframe->Change_Side_Module( module, NULL );
2008-04-01 05:21:50 +00:00
module->Draw( Panel, DC, GR_OR );
2007-08-10 19:14:51 +00:00
}
pcbframe->SetCurItem( NULL );
2009-08-06 07:11:04 +00:00
delete s_ModuleInitialCopy;
s_ModuleInitialCopy = NULL;
s_PickedList.ClearListAndDeleteItems();
// Display ratsnest is allowed
pcbframe->GetBoard()->m_Status_Pcb &= ~DO_NOT_SHOW_GENERAL_RASTNEST;
2010-07-20 18:11:34 +00:00
if( pcbframe->GetBoard()->IsElementVisible( RATSNEST_VISIBLE ) )
pcbframe->DrawGeneralRatsnest( DC );
#ifdef __WXMAC__
Panel->Refresh();
#endif
}
/* Redraw the footprint when moving the mouse.
2007-08-10 19:14:51 +00:00
*/
void MoveFootprint( EDA_DRAW_PANEL* aPanel, wxDC* aDC, const wxPoint& aPosition, bool aErase )
{
MODULE* module = (MODULE*) aPanel->GetScreen()->GetCurItem();
if( module == NULL )
2007-08-10 19:14:51 +00:00
return;
/* Erase current footprint. */
if( aErase )
2007-08-10 19:14:51 +00:00
{
DrawModuleOutlines( aPanel, aDC, module );
2007-08-10 19:14:51 +00:00
}
/* Redraw the module at the new position. */
g_Offset_Module = module->m_Pos - aPanel->GetScreen()->GetCrossHairPosition();
DrawModuleOutlines( aPanel, aDC, module );
DrawSegmentWhileMovingFootprint( aPanel, aDC );
}
bool PCB_EDIT_FRAME::Delete_Module( MODULE* aModule, wxDC* aDC, bool aAskBeforeDeleting )
{
wxString msg;
2007-08-10 19:14:51 +00:00
2010-12-29 17:47:32 +00:00
if( aModule == NULL )
return false;
2007-08-10 19:14:51 +00:00
2010-12-29 17:47:32 +00:00
aModule->DisplayInfo( this );
2007-08-10 19:14:51 +00:00
/* Confirm module delete. */
if( aAskBeforeDeleting )
2007-08-10 19:14:51 +00:00
{
2010-07-20 18:11:34 +00:00
msg.Printf( _( "Delete Module %s (value %s) ?" ),
GetChars( aModule->m_Reference->m_Text ),
GetChars( aModule->m_Value->m_Text ) );
if( !IsOK( this, msg ) )
{
return false;
}
2007-08-10 19:14:51 +00:00
}
OnModify();
2007-08-10 19:14:51 +00:00
/* Remove module from list, and put it in undo command list */
2010-12-29 17:47:32 +00:00
m_Pcb->m_Modules.Remove( aModule );
aModule->SetState( IS_DELETED, ON );
2010-12-29 17:47:32 +00:00
SaveCopyInUndoList( aModule, UR_DELETED );
2007-08-10 19:14:51 +00:00
2010-12-29 17:47:32 +00:00
if( aDC && GetBoard()->IsElementVisible( RATSNEST_VISIBLE ) )
Compile_Ratsnest( aDC, true );
// Redraw the full screen to ensure perfect display of board and ratsnest.
2010-12-29 17:47:32 +00:00
if( aDC )
m_canvas->Refresh();
return true;
}
2007-08-10 19:14:51 +00:00
void PCB_EDIT_FRAME::Change_Side_Module( MODULE* Module, wxDC* DC )
{
2007-08-10 19:14:51 +00:00
if( Module == NULL )
return;
if( ( Module->GetLayer() != LAYER_N_FRONT ) && ( Module->GetLayer() != LAYER_N_BACK ) )
2007-08-10 19:14:51 +00:00
return;
OnModify();
2007-08-10 19:14:51 +00:00
if( !Module->IsMoving() ) /* This is a simple flip, no other edition in progress */
2007-08-10 19:14:51 +00:00
{
GetBoard()->m_Status_Pcb &= ~( LISTE_RATSNEST_ITEM_OK | CONNEXION_OK );
2009-08-06 07:11:04 +00:00
if( DC )
2008-04-01 05:21:50 +00:00
{
Module->SetFlags( DO_NOT_DRAW );
m_canvas->RefreshDrawingRect( Module->GetBoundingBox() );
Module->ClearFlags( DO_NOT_DRAW );
2008-04-01 05:21:50 +00:00
}
2007-08-10 19:14:51 +00:00
/* Show ratsnest if necessary. */
2010-07-20 18:11:34 +00:00
if( DC && GetBoard()->IsElementVisible( RATSNEST_VISIBLE ) )
2009-08-06 07:11:04 +00:00
DrawGeneralRatsnest( DC );
2007-08-10 19:14:51 +00:00
g_Offset_Module.x = 0;
g_Offset_Module.y = 0;
}
else // Module is being moved.
2007-08-10 19:14:51 +00:00
{
/* Erase footprint and draw outline if it has been already drawn. */
2009-08-06 07:11:04 +00:00
if( DC )
2007-08-10 19:14:51 +00:00
{
DrawModuleOutlines( m_canvas, DC, Module );
DrawSegmentWhileMovingFootprint( m_canvas, DC );
2007-08-10 19:14:51 +00:00
}
}
2009-08-01 19:26:05 +00:00
/* Flip the module */
Module->Flip( Module->m_Pos );
2007-08-10 19:14:51 +00:00
2009-08-06 07:11:04 +00:00
Module->DisplayInfo( this );
2007-08-10 19:14:51 +00:00
if( !Module->IsMoving() ) /* Inversion simple */
2007-08-10 19:14:51 +00:00
{
2009-08-06 07:11:04 +00:00
if( DC )
2007-08-10 19:14:51 +00:00
{
Module->Draw( m_canvas, DC, GR_OR );
if( GetBoard()->IsElementVisible( RATSNEST_VISIBLE ) )
Compile_Ratsnest( DC, true );
2007-08-10 19:14:51 +00:00
}
}
else
{
2009-08-06 07:11:04 +00:00
if( DC )
2007-08-10 19:14:51 +00:00
{
DrawModuleOutlines( m_canvas, DC, Module );
DrawSegmentWhileMovingFootprint( m_canvas, DC );
2007-08-10 19:14:51 +00:00
}
2009-08-06 07:11:04 +00:00
GetBoard()->m_Status_Pcb &= ~RATSNEST_ITEM_LOCAL_OK;
2007-08-10 19:14:51 +00:00
}
}
void PCB_BASE_FRAME::PlaceModule( MODULE* aModule, wxDC* aDC, bool aDoNotRecreateRatsnest )
{
wxPoint newpos;
2007-08-10 19:14:51 +00:00
if( aModule == 0 )
2007-08-10 19:14:51 +00:00
return;
OnModify();
GetBoard()->m_Status_Pcb &= ~( LISTE_RATSNEST_ITEM_OK | CONNEXION_OK);
2007-08-10 19:14:51 +00:00
if( aModule->IsNew() )
2009-08-06 07:11:04 +00:00
{
SaveCopyInUndoList( aModule, UR_NEW );
2009-08-06 07:11:04 +00:00
}
else if( aModule->IsMoving() )
2009-08-06 07:11:04 +00:00
{
ITEM_PICKER picker( aModule, UR_CHANGED );
2012-02-05 13:02:46 +00:00
picker.SetLink( s_ModuleInitialCopy );
s_PickedList.PushItem( picker );
s_ModuleInitialCopy = NULL; // the picker is now owner of s_ModuleInitialCopy.
2009-08-06 07:11:04 +00:00
}
if( s_PickedList.GetCount() )
2009-08-06 07:11:04 +00:00
{
SaveCopyInUndoList( s_PickedList, UR_UNSPECIFIED );
// Clear list, but DO NOT delete items, because they are owned by the saved undo
// list and they therefore in use
2009-08-06 07:11:04 +00:00
s_PickedList.ClearItemsList();
}
if( g_Show_Module_Ratsnest && ( GetBoard()->m_Status_Pcb & LISTE_PAD_OK ) && aDC )
TraceModuleRatsNest( aDC );
2007-08-10 19:14:51 +00:00
newpos = GetScreen()->GetCrossHairPosition();
aModule->SetPosition( newpos );
aModule->ClearFlags();
delete s_ModuleInitialCopy;
s_ModuleInitialCopy = NULL;
if( aDC )
aModule->Draw( m_canvas, aDC, GR_OR );
2007-08-10 19:14:51 +00:00
// Redraw dragged track segments, if any
for( unsigned ii = 0; ii < g_DragSegmentList.size(); ii++ )
2007-08-10 19:14:51 +00:00
{
TRACK * track = g_DragSegmentList[ii].m_Track;
track->SetState( IN_EDIT, OFF );
if( aDC )
track->Draw( m_canvas, aDC, GR_OR );
2007-08-10 19:14:51 +00:00
}
// Delete drag list
EraseDragList();
m_canvas->SetMouseCapture( NULL, NULL );
if( GetBoard()->IsElementVisible( RATSNEST_VISIBLE ) && !aDoNotRecreateRatsnest )
Compile_Ratsnest( aDC, true );
2007-08-10 19:14:51 +00:00
if( aDC )
m_canvas->Refresh();
aModule->DisplayInfo( this );
}
/*
* Rotate the footprint angle degrees in the direction < 0.
* If incremental == true, the rotation is made from the last orientation,
* If the module is placed in the absolute orientation angle.
* If DC == NULL, the component does not redraw.
* Otherwise, it erases and redraws turns
2007-08-10 19:14:51 +00:00
*/
void PCB_BASE_FRAME::Rotate_Module( wxDC* DC, MODULE* module, int angle, bool incremental )
{
2007-08-10 19:14:51 +00:00
if( module == NULL )
return;
OnModify();
2007-08-10 19:14:51 +00:00
if( !module->IsMoving() ) /* This is a simple rotation, no other
* edition in progress */
2007-08-10 19:14:51 +00:00
{
if( DC ) // Erase footprint to screen
2007-08-10 19:14:51 +00:00
{
module->SetFlags( DO_NOT_DRAW );
m_canvas->RefreshDrawingRect( module->GetBoundingBox() );
module->ClearFlags( DO_NOT_DRAW );
2010-07-20 18:11:34 +00:00
if( GetBoard()->IsElementVisible( RATSNEST_VISIBLE ) )
2007-08-10 19:14:51 +00:00
DrawGeneralRatsnest( DC );
}
}
else
{
if( DC )
{
DrawModuleOutlines( m_canvas, DC, module );
DrawSegmentWhileMovingFootprint( m_canvas, DC );
2007-08-10 19:14:51 +00:00
}
}
GetBoard()->m_Status_Pcb &= ~( LISTE_RATSNEST_ITEM_OK | CONNEXION_OK );
2007-08-10 19:14:51 +00:00
if( incremental )
module->SetOrientation( module->m_Orient + angle );
else
module->SetOrientation( angle );
module->DisplayInfo( this );
2007-08-10 19:14:51 +00:00
if( DC )
{
if( !module->IsMoving() )
2010-07-20 18:11:34 +00:00
{
// not beiing moved: redraw the module and update ratsnest
module->Draw( m_canvas, DC, GR_OR );
if( GetBoard()->IsElementVisible( RATSNEST_VISIBLE ) )
Compile_Ratsnest( DC, true );
2007-08-10 19:14:51 +00:00
}
else
2010-07-20 18:11:34 +00:00
{
// Beiing moved: just redraw it
DrawModuleOutlines( m_canvas, DC, module );
DrawSegmentWhileMovingFootprint( m_canvas, DC );
2007-08-10 19:14:51 +00:00
}
if( module->GetFlags() == 0 ) // module not in edit: redraw full screen
m_canvas->Refresh();
2007-08-10 19:14:51 +00:00
}
}
/*************************************************/
2010-12-29 17:47:32 +00:00
/* Redraw in XOR mode the outlines of a module. */
/*************************************************/
void DrawModuleOutlines( EDA_DRAW_PANEL* panel, wxDC* DC, MODULE* module )
{
2007-08-10 19:14:51 +00:00
int pad_fill_tmp;
D_PAD* pt_pad;
if( module == NULL )
return;
2007-08-10 19:14:51 +00:00
module->DrawEdgesOnly( panel, DC, g_Offset_Module, GR_XOR );
// Show pads in sketch mode to speedu up drawings
pad_fill_tmp = DisplayOpt.DisplayPadFill;
DisplayOpt.DisplayPadFill = true;
pt_pad = module->m_Pads;
for( ; pt_pad != NULL; pt_pad = pt_pad->Next() )
pt_pad->Draw( panel, DC, GR_XOR, g_Offset_Module );
2007-08-10 19:14:51 +00:00
DisplayOpt.DisplayPadFill = pad_fill_tmp;
2007-08-10 19:14:51 +00:00
if( g_Show_Module_Ratsnest && panel )
{
PCB_BASE_FRAME* frame = (PCB_BASE_FRAME*) panel->GetParent();
2010-12-29 17:47:32 +00:00
frame->build_ratsnest_module( module );
frame->TraceModuleRatsNest( DC );
2007-08-10 19:14:51 +00:00
}
}