automove.cpp 8.69 KB
Newer Older
1 2 3
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
4 5
 * Copyright (C) 2012 Jean-Pierre Charras, jean-pierre.charras@ujf-grenoble.fr
 * Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
6
 * Copyright (C) 2011 Wayne Stambaugh <stambaughw@verizon.net>
7 8
 *
 * Copyright (C) 1992-2012 KiCad Developers, see AUTHORS.txt for contributors.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
 *
 * 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 automove.cpp
 * @brief Routines for automatic displacement and rotation of modules.
 */
32

33 34
#include <algorithm>

35 36 37 38 39 40 41 42 43 44 45 46 47
#include <fctsys.h>
#include <class_drawpanel.h>
#include <confirm.h>
#include <kicad_string.h>
#include <pcbnew.h>
#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>
48 49 50


typedef enum {
51 52 53 54 55
    FIXE_MODULE,
    FREE_MODULE,
    FIXE_ALL_MODULES,
    FREE_ALL_MODULES
} SelectFixeFct;
56 57


58
static bool sortModulesbySize( MODULE* ref, MODULE* compare );
59 60


stambaughw's avatar
stambaughw committed
61
wxString ModulesMaskSelection = wxT( "*" );
62

63

charras's avatar
charras committed
64
/* Called on events (popup menus) relative to automove and autoplace footprints
65
 */
66
void PCB_EDIT_FRAME::AutoPlace( wxCommandEvent& event )
67
{
68 69
    int        id = event.GetId();

70
    if( m_mainToolBar == NULL )
71 72
        return;

73
    INSTALL_UNBUFFERED_DC( dc, m_canvas );
74

charras's avatar
charras committed
75
    switch( id )
76
    {
77 78
    case ID_POPUP_PCB_AUTOROUTE_SELECT_LAYERS:
        return;
79 80

    case ID_POPUP_PCB_AUTOPLACE_FIXE_MODULE:
81
        LockModule( (MODULE*) GetScreen()->GetCurItem(), true );
82
        return;
83 84

    case ID_POPUP_PCB_AUTOPLACE_FREE_MODULE:
85
        LockModule( (MODULE*) GetScreen()->GetCurItem(), false );
86
        return;
87 88

    case ID_POPUP_PCB_AUTOPLACE_FREE_ALL_MODULES:
89
        LockModule( NULL, false );
90
        return;
91 92

    case ID_POPUP_PCB_AUTOPLACE_FIXE_ALL_MODULES:
93
        LockModule( NULL, true );
94 95 96
        return;

    case ID_POPUP_CANCEL_CURRENT_COMMAND:
97
        if( m_canvas->IsMouseCaptured() )
98
        {
99
            m_canvas->CallEndMouseCapture( &dc );
100
        }
101

102 103
        break;

104
    default:   // Abort a current command (if any)
105
        m_canvas->EndMouseCapture( ID_NO_TOOL_SELECTED, m_canvas->GetDefaultCursor() );
106 107 108
        break;
    }

109
    // Erase ratsnest if needed
110 111
    if( GetBoard()->IsElementVisible(RATSNEST_VISIBLE) )
        DrawGeneralRatsnest( &dc );
112

113 114 115 116
    GetBoard()->m_Status_Pcb |= DO_NOT_SHOW_GENERAL_RASTNEST;

    switch( id )
    {
117
    case ID_POPUP_PCB_AUTOPLACE_CURRENT_MODULE:
118
        AutoPlaceModule( (MODULE*) GetScreen()->GetCurItem(), PLACE_1_MODULE, &dc );
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
        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:
134
        AutoMoveModulesOnPcb( false );
135 136 137
        break;

    case ID_POPUP_PCB_AUTOMOVE_NEW_MODULES:
138
        AutoMoveModulesOnPcb( true );
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
        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:
162
        wxMessageBox( wxT( "AutoPlace command error" ) );
163 164 165
        break;
    }

166
    GetBoard()->m_Status_Pcb &= ~DO_NOT_SHOW_GENERAL_RASTNEST;
167
    Compile_Ratsnest( &dc, true );
168 169
}

170

jean-pierre charras's avatar
jean-pierre charras committed
171 172 173
/* 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
174
 */
175
void PCB_EDIT_FRAME::AutoMoveModulesOnPcb( bool PlaceModulesHorsPcb )
176
{
177
    std::vector <MODULE*> moduleList;
178 179
    wxPoint  start, current;
    int      Ymax_size, Xsize_allowed;
stambaughw's avatar
stambaughw committed
180
    int      pas_grille = (int) GetScreen()->GetGridSize().x;
181
    double   surface;
182

183 184 185 186 187
    // Undo: init list
    PICKED_ITEMS_LIST  newList;
    newList.m_Status = UR_CHANGED;
    ITEM_PICKER        picker( NULL, UR_CHANGED );

188
    if( GetBoard()->m_Modules == NULL )
189
    {
stambaughw's avatar
stambaughw committed
190 191
        DisplayError( this, _( "No modules found!" ) );
        return;
192 193
    }

194
    // Confirmation
stambaughw's avatar
stambaughw committed
195
    if( !IsOK( this, _( "Move modules?" ) ) )
196 197
        return;

Dick Hollenbeck's avatar
Dick Hollenbeck committed
198
    EDA_RECT bbbox = GetBoard()->ComputeBoundingBox( true );
199

Dick Hollenbeck's avatar
Dick Hollenbeck committed
200 201 202 203
    bool     edgesExist = ( bbbox.GetWidth() || bbbox.GetHeight() );

    // no edges exist
    if( PlaceModulesHorsPcb && !edgesExist )
204 205
    {
        DisplayError( this,
206
                      _( "Could not automatically place modules. No board outlines detected." ) );
207 208 209
        return;
    }

210 211
    // Build sorted footprints list (sort by decreasing size )
    MODULE* Module = GetBoard()->m_Modules;
212

stambaughw's avatar
stambaughw committed
213
    for( ; Module != NULL; Module = Module->Next() )
214
    {
215
        Module->CalculateBoundingBox();
216
        moduleList.push_back(Module);
217
    }
218

219
    sort( moduleList.begin(), moduleList.end(), sortModulesbySize );
220

221 222
    /* to move modules outside the board, the cursor is placed below
     * the current board, to avoid placing components in board area.
223
     */
Dick Hollenbeck's avatar
Dick Hollenbeck committed
224
    if( PlaceModulesHorsPcb && edgesExist )
225
    {
226
        if( GetCrossHairPosition().y < (bbbox.GetBottom() + 2000) )
227
        {
228
            wxPoint pos = GetCrossHairPosition();
Dick Hollenbeck's avatar
Dick Hollenbeck committed
229
            pos.y = bbbox.GetBottom() + 2000;
230
            SetCrossHairPosition( pos );
231
        }
232 233
    }

234
    // calculate the area needed by footprints
235
    surface = 0.0;
236

237
    for( unsigned ii = 0; ii < moduleList.size(); ii++ )
238
    {
239
        Module = moduleList[ii];
240

Dick Hollenbeck's avatar
Dick Hollenbeck committed
241
        if( PlaceModulesHorsPcb && edgesExist )
242
        {
243
            if( bbbox.Contains( Module->GetPosition() ) )
244 245
                continue;
        }
246

247
        surface += Module->GetArea();
248 249
    }

stambaughw's avatar
stambaughw committed
250
    Xsize_allowed = (int) ( sqrt( surface ) * 4.0 / 3.0 );
251

252
    start     = current = GetCrossHairPosition();
253 254
    Ymax_size = 0;

255
    for( unsigned ii = 0; ii < moduleList.size(); ii++ )
256
    {
257
        Module = moduleList[ii];
258

dickelbeck's avatar
dickelbeck committed
259
        if( Module->IsLocked() )
260 261
            continue;

Dick Hollenbeck's avatar
Dick Hollenbeck committed
262
        if( PlaceModulesHorsPcb && edgesExist )
263
        {
264
            if( bbbox.Contains( Module->GetPosition() ) )
265 266 267
                continue;
        }

268
        // Undo: add copy of old Module to undo
269 270
        picker.SetItem( Module );
        picker.SetLink( Module->Clone() );
271

272 273 274 275 276 277 278
        if( current.x > (Xsize_allowed + start.x) )
        {
            current.x  = start.x;
            current.y += Ymax_size + pas_grille;
            Ymax_size  = 0;
        }

279 280
        SetCrossHairPosition( current + Module->GetPosition() -
            Module->GetBoundingBox().GetPosition() );
281

282
        Ymax_size = std::max( Ymax_size, Module->GetBoundingBox().GetHeight() );
283

284
        PlaceModule( Module, NULL, true );
285

286 287 288
        // Undo: add new Module to undo
        newList.PushItem( picker );

289
        current.x += Module->GetBoundingBox().GetWidth() + pas_grille;
290 291
    }

292 293 294 295
    // Undo: commit
    if( newList.GetCount() )
        SaveCopyInUndoList( newList, UR_CHANGED );

296
    m_canvas->Refresh();
297 298 299
}


300
/* Set or reset (true or false) Lock attribute of aModule or all modules if aModule == NULL
301
 */
302
void PCB_EDIT_FRAME::LockModule( MODULE* aModule, bool aLocked )
303
{
304
    if( aModule )
305
    {
306
        aModule->SetLocked( aLocked );
307
        SetMsgPanel( aModule );
308
        OnModify();
309 310 311
    }
    else
    {
312
        aModule = GetBoard()->m_Modules;
313

314
        for( ; aModule != NULL; aModule = aModule->Next() )
315
        {
316
            if( WildCompareString( ModulesMaskSelection, aModule->GetReference() ) )
317
            {
318
                aModule->SetLocked( aLocked );
319
                OnModify();
320 321 322
            }
        }
    }
323 324
}

325

326
static bool sortModulesbySize( MODULE* ref, MODULE* compare )
327
{
328
    return compare->GetArea() < ref->GetArea();
329
}