kicad/pcbnew/automove.cpp

317 lines
8.0 KiB
C++
Raw Normal View History

/****************************************************************/
/* Routines for automatic displacement and rotation of modules. */
/****************************************************************/
2010-11-26 17:47:35 +00:00
#include <algorithm>
#include "fctsys.h"
#include "class_drawpanel.h"
#include "confirm.h"
#include "kicad_string.h"
#include "pcbnew.h"
2009-07-30 11:04:07 +00:00
#include "wxPcbStruct.h"
#include "kicad_device_context.h"
#include "autorout.h"
#include "cell.h"
#include "pcbnew_id.h"
#include "class_board.h"
#include "class_module.h"
typedef enum {
FIXE_MODULE,
FREE_MODULE,
FIXE_ALL_MODULES,
FREE_ALL_MODULES
} SelectFixeFct;
2010-11-26 17:47:35 +00:00
static bool sortModulesbySize( MODULE* ref, MODULE* compare );
wxString ModulesMaskSelection = wxT( "*" );
2009-10-23 15:28:45 +00:00
/* Called on events (popup menus) relative to automove and autoplace footprints
*/
void PCB_EDIT_FRAME::AutoPlace( wxCommandEvent& event )
{
int id = event.GetId();
bool on_state;
if( m_HToolBar == NULL )
return;
INSTALL_UNBUFFERED_DC( dc, DrawPanel );
2009-10-23 15:28:45 +00:00
switch( id )
{
case ID_TOOLBARH_PCB_MODE_MODULE:
on_state = m_HToolBar->GetToolState( ID_TOOLBARH_PCB_MODE_MODULE );
if( on_state )
{
m_HToolBar->ToggleTool( ID_TOOLBARH_PCB_MODE_TRACKS, false );
m_HTOOL_current_state = ID_TOOLBARH_PCB_MODE_MODULE;
}
else
{
m_HTOOL_current_state = 0;
}
2010-08-12 05:57:14 +00:00
return;
case ID_TOOLBARH_PCB_MODE_TRACKS:
on_state = m_HToolBar->GetToolState( ID_TOOLBARH_PCB_MODE_TRACKS );
if( on_state )
{
m_HToolBar->ToggleTool( ID_TOOLBARH_PCB_MODE_MODULE, false );
m_HTOOL_current_state = ID_TOOLBARH_PCB_MODE_TRACKS;
}
else
{
m_HTOOL_current_state = 0;
}
2010-08-12 05:57:14 +00:00
return;
case ID_POPUP_PCB_AUTOROUTE_SELECT_LAYERS:
return;
case ID_POPUP_PCB_AUTOPLACE_FIXE_MODULE:
2011-03-09 14:30:39 +00:00
LockModule( (MODULE*) GetScreen()->GetCurItem(), true );
2010-08-12 05:57:14 +00:00
return;
case ID_POPUP_PCB_AUTOPLACE_FREE_MODULE:
LockModule( (MODULE*) GetScreen()->GetCurItem(), false );
2010-08-12 05:57:14 +00:00
return;
case ID_POPUP_PCB_AUTOPLACE_FREE_ALL_MODULES:
LockModule( NULL, false );
2010-08-12 05:57:14 +00:00
return;
case ID_POPUP_PCB_AUTOPLACE_FIXE_ALL_MODULES:
2011-03-09 14:30:39 +00:00
LockModule( NULL, true );
2010-08-12 05:57:14 +00:00
return;
case ID_POPUP_CANCEL_CURRENT_COMMAND:
if( DrawPanel->IsMouseCaptured() )
2010-08-12 05:57:14 +00:00
{
DrawPanel->m_endMouseCaptureCallback( DrawPanel, &dc );
2010-08-12 05:57:14 +00:00
}
break;
2010-08-12 05:57:14 +00:00
default: // Abort a current command (if any)
DrawPanel->EndMouseCapture( ID_NO_TOOL_SELECTED, DrawPanel->GetDefaultCursor() );
2010-08-12 05:57:14 +00:00
break;
}
/* Erase ratsnest if needed */
if( GetBoard()->IsElementVisible(RATSNEST_VISIBLE) )
DrawGeneralRatsnest( &dc );
2010-08-12 05:57:14 +00:00
GetBoard()->m_Status_Pcb |= DO_NOT_SHOW_GENERAL_RASTNEST;
switch( id )
{
case ID_POPUP_PCB_AUTOPLACE_CURRENT_MODULE:
AutoPlaceModule( (MODULE*) GetScreen()->GetCurItem(), PLACE_1_MODULE, &dc );
break;
case ID_POPUP_PCB_AUTOPLACE_ALL_MODULES:
AutoPlaceModule( NULL, PLACE_ALL, &dc );
break;
case ID_POPUP_PCB_AUTOPLACE_NEW_MODULES:
AutoPlaceModule( NULL, PLACE_OUT_OF_BOARD, &dc );
break;
case ID_POPUP_PCB_AUTOPLACE_NEXT_MODULE:
AutoPlaceModule( NULL, PLACE_INCREMENTAL, &dc );
break;
case ID_POPUP_PCB_AUTOMOVE_ALL_MODULES:
AutoMoveModulesOnPcb( false );
break;
case ID_POPUP_PCB_AUTOMOVE_NEW_MODULES:
2011-03-09 14:30:39 +00:00
AutoMoveModulesOnPcb( true );
break;
case ID_POPUP_PCB_AUTOROUTE_ALL_MODULES:
Autoroute( &dc, ROUTE_ALL );
break;
case ID_POPUP_PCB_AUTOROUTE_MODULE:
Autoroute( &dc, ROUTE_MODULE );
break;
case ID_POPUP_PCB_AUTOROUTE_PAD:
Autoroute( &dc, ROUTE_PAD );
break;
case ID_POPUP_PCB_AUTOROUTE_NET:
Autoroute( &dc, ROUTE_NET );
break;
case ID_POPUP_PCB_AUTOROUTE_RESET_UNROUTED:
Reset_Noroutable( &dc );
break;
default:
2011-03-09 14:30:39 +00:00
wxMessageBox( wxT( "AutoPlace command error" ) );
break;
}
GetBoard()->m_Status_Pcb &= ~DO_NOT_SHOW_GENERAL_RASTNEST;
Compile_Ratsnest( &dc, true );
}
2010-08-11 18:17:12 +00:00
/* Function to move components in a rectangular area format 4 / 3,
* starting from the mouse cursor
* The components with the FIXED status set are not moved
*/
void PCB_EDIT_FRAME::AutoMoveModulesOnPcb( bool PlaceModulesHorsPcb )
{
2010-11-26 17:47:35 +00:00
std::vector <MODULE*> moduleList;
wxPoint start, current;
int Ymax_size, Xsize_allowed;
int pas_grille = (int) GetScreen()->GetGridSize().x;
2010-11-26 17:47:35 +00:00
bool edgesExists;
double surface;
if( GetBoard()->m_Modules == NULL )
{
DisplayError( this, _( "No modules found!" ) );
return;
}
/* Confirmation */
if( !IsOK( this, _( "Move modules?" ) ) )
return;
edgesExists = GetBoard()->ComputeBoundingBox( true );
2010-11-26 17:47:35 +00:00
if( PlaceModulesHorsPcb && !edgesExists )
{
DisplayError( this,
2010-06-19 10:58:50 +00:00
_( "Could not automatically place modules. No board outlines detected." ) );
return;
}
2010-11-26 17:47:35 +00:00
// Build sorted footprints list (sort by decreasing size )
MODULE* Module = GetBoard()->m_Modules;
for( ; Module != NULL; Module = Module->Next() )
{
Module->CalculateBoundingBox();
2010-11-26 17:47:35 +00:00
moduleList.push_back(Module);
}
2010-11-26 17:47:35 +00:00
sort( moduleList.begin(), moduleList.end(), sortModulesbySize );
2010-11-26 17:47:35 +00:00
/* to move modules outside the board, the cursor is placed below
* the current board, to avoid placing components in board area.
*/
2010-11-26 17:47:35 +00:00
if( PlaceModulesHorsPcb && edgesExists )
{
if( GetScreen()->GetCrossHairPosition().y < (GetBoard()->m_BoundaryBox.GetBottom() + 2000) )
2011-02-16 19:16:38 +00:00
{
wxPoint pos = GetScreen()->GetCrossHairPosition();
pos.y = GetBoard()->m_BoundaryBox.GetBottom() + 2000;
GetScreen()->SetCrossHairPosition( pos );
}
}
2010-11-26 17:47:35 +00:00
/* calculate the area needed by footprints */
surface = 0.0;
2010-11-26 17:47:35 +00:00
for( unsigned ii = 0; ii < moduleList.size(); ii++ )
{
2010-11-26 17:47:35 +00:00
Module = moduleList[ii];
2010-11-26 17:47:35 +00:00
if( PlaceModulesHorsPcb && edgesExists )
{
if( GetBoard()->m_BoundaryBox.Contains( Module->m_Pos ) )
continue;
}
surface += Module->m_Surface;
}
Xsize_allowed = (int) ( sqrt( surface ) * 4.0 / 3.0 );
start = current = GetScreen()->GetCrossHairPosition();
Ymax_size = 0;
2010-11-26 17:47:35 +00:00
for( unsigned ii = 0; ii < moduleList.size(); ii++ )
{
2010-11-26 17:47:35 +00:00
Module = moduleList[ii];
2007-09-09 02:27:03 +00:00
if( Module->IsLocked() )
continue;
2010-11-26 17:47:35 +00:00
if( PlaceModulesHorsPcb && edgesExists )
{
if( GetBoard()->m_BoundaryBox.Contains( Module->m_Pos ) )
continue;
}
if( current.x > (Xsize_allowed + start.x) )
{
current.x = start.x;
current.y += Ymax_size + pas_grille;
Ymax_size = 0;
}
GetScreen()->SetCrossHairPosition( current + Module->m_Pos -
Module->m_BoundaryBox.GetPosition() );
Ymax_size = MAX( Ymax_size, Module->m_BoundaryBox.GetHeight() );
PlaceModule( Module, NULL, true );
current.x += Module->m_BoundaryBox.GetWidth() + pas_grille;
}
2009-10-23 15:28:45 +00:00
DrawPanel->Refresh();
}
/* Set or reset (true or false) Lock attribute of aModule or all modules if aModule == NULL
*/
2011-03-09 14:30:39 +00:00
void PCB_EDIT_FRAME::LockModule( MODULE* aModule, bool aLocked )
{
2011-03-09 14:30:39 +00:00
if( aModule )
{
2011-03-09 14:30:39 +00:00
aModule->SetLocked( aLocked );
2008-04-01 05:21:50 +00:00
2011-03-09 14:30:39 +00:00
aModule->DisplayInfo( this );
OnModify();
}
else
{
2011-03-09 14:30:39 +00:00
aModule = GetBoard()->m_Modules;
2011-03-09 14:30:39 +00:00
for( ; aModule != NULL; aModule = aModule->Next() )
{
if( WildCompareString( ModulesMaskSelection, aModule->m_Reference->m_Text ) )
{
2011-03-09 14:30:39 +00:00
aModule->SetLocked( aLocked );
OnModify();
}
}
}
}
2010-11-26 17:47:35 +00:00
static bool sortModulesbySize( MODULE* ref, MODULE* compare )
{
2010-11-26 17:47:35 +00:00
return compare->m_Surface < ref->m_Surface;
}