placement_tool.cpp 11.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 2014 CERN
 * @author Maciej Suminski <maciej.suminski@cern.ch>
 *
 * 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
 */

#include "placement_tool.h"
#include "common_actions.h"
#include "selection_tool.h"

#include <wxPcbStruct.h>
#include <class_board.h>
#include <ratsnest_data.h>

#include <confirm.h>
#include <boost/foreach.hpp>

PLACEMENT_TOOL::PLACEMENT_TOOL() :
    TOOL_INTERACTIVE( "pcbnew.Placement" )
{
}

PLACEMENT_TOOL::~PLACEMENT_TOOL()
{
}


bool PLACEMENT_TOOL::Init()
{
    // Find the selection tool, so they can cooperate
    m_selectionTool = static_cast<SELECTION_TOOL*>( m_toolMgr->FindTool( "pcbnew.InteractiveSelection" ) );

    if( !m_selectionTool )
    {
        DisplayError( NULL, wxT( "pcbnew.InteractiveSelection tool is not available" ) );
        return false;
    }

57 58 59 60 61 62 63 64 65
    // Create a context menu and make it available through selection tool
    CONTEXT_MENU* menu = new CONTEXT_MENU;
    menu->Add( COMMON_ACTIONS::alignTop );
    menu->Add( COMMON_ACTIONS::alignBottom );
    menu->Add( COMMON_ACTIONS::alignLeft );
    menu->Add( COMMON_ACTIONS::alignRight );
    menu->AppendSeparator();
    menu->Add( COMMON_ACTIONS::distributeHorizontally );
    menu->Add( COMMON_ACTIONS::distributeVertically );
66
    m_selectionTool->AddSubMenu( menu, _( "Align/distribute" ),
67
                                 SELECTION_CONDITIONS::MoreThan( 1 ) );
68 69 70 71 72 73 74 75 76

    setTransitions();

    return true;
}


int PLACEMENT_TOOL::AlignTop( TOOL_EVENT& aEvent )
{
77
    const SELECTION& selection = m_selectionTool->GetSelection();
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119

    if( selection.Size() > 1 )
    {
        PCB_BASE_FRAME* editFrame = getEditFrame<PCB_BASE_FRAME>();
        RN_DATA* ratsnest = getModel<BOARD>()->GetRatsnest();

        editFrame->OnModify();
        editFrame->SaveCopyInUndoList( selection.items, UR_CHANGED );

        // Compute the highest point of selection - it will be the edge of alignment
        int top = selection.Item<BOARD_ITEM>( 0 )->GetBoundingBox().GetY();

        for( int i = 1; i < selection.Size(); ++i )
        {
            int currentTop = selection.Item<BOARD_ITEM>( i )->GetBoundingBox().GetY();

            if( top > currentTop )      // Y decreases when going up
                top = currentTop;
        }

        // Move the selected items
        for( int i = 0; i < selection.Size(); ++i )
        {
            BOARD_ITEM* item = selection.Item<BOARD_ITEM>( i );
            int difference = top - item->GetBoundingBox().GetY();

            item->Move( wxPoint( 0, difference ) );
            item->ViewUpdate();
            ratsnest->Update( item );
        }

        getModel<BOARD>()->GetRatsnest()->Recalculate();
    }

    setTransitions();

    return 0;
}


int PLACEMENT_TOOL::AlignBottom( TOOL_EVENT& aEvent )
{
120
    const SELECTION& selection = m_selectionTool->GetSelection();
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162

    if( selection.Size() > 1 )
    {
        PCB_BASE_FRAME* editFrame = getEditFrame<PCB_BASE_FRAME>();
        RN_DATA* ratsnest = getModel<BOARD>()->GetRatsnest();

        editFrame->OnModify();
        editFrame->SaveCopyInUndoList( selection.items, UR_CHANGED );

        // Compute the lowest point of selection - it will be the edge of alignment
        int bottom = selection.Item<BOARD_ITEM>( 0 )->GetBoundingBox().GetBottom();

        for( int i = 1; i < selection.Size(); ++i )
        {
            int currentBottom = selection.Item<BOARD_ITEM>( i )->GetBoundingBox().GetBottom();

            if( bottom < currentBottom )      // Y increases when going down
                bottom = currentBottom;
        }

        // Move the selected items
        for( int i = 0; i < selection.Size(); ++i )
        {
            BOARD_ITEM* item = selection.Item<BOARD_ITEM>( i );
            int difference = bottom - item->GetBoundingBox().GetBottom();

            item->Move( wxPoint( 0, difference ) );
            item->ViewUpdate();
            ratsnest->Update( item );
        }

        getModel<BOARD>()->GetRatsnest()->Recalculate();
    }

    setTransitions();

    return 0;
}


int PLACEMENT_TOOL::AlignLeft( TOOL_EVENT& aEvent )
{
163
    const SELECTION& selection = m_selectionTool->GetSelection();
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205

    if( selection.Size() > 1 )
    {
        PCB_BASE_FRAME* editFrame = getEditFrame<PCB_BASE_FRAME>();
        RN_DATA* ratsnest = getModel<BOARD>()->GetRatsnest();

        editFrame->OnModify();
        editFrame->SaveCopyInUndoList( selection.items, UR_CHANGED );

        // Compute the leftmost point of selection - it will be the edge of alignment
        int left = selection.Item<BOARD_ITEM>( 0 )->GetBoundingBox().GetX();

        for( int i = 1; i < selection.Size(); ++i )
        {
            int currentLeft = selection.Item<BOARD_ITEM>( i )->GetBoundingBox().GetX();

            if( left > currentLeft )      // X decreases when going left
                left = currentLeft;
        }

        // Move the selected items
        for( int i = 0; i < selection.Size(); ++i )
        {
            BOARD_ITEM* item = selection.Item<BOARD_ITEM>( i );
            int difference = left - item->GetBoundingBox().GetX();

            item->Move( wxPoint( difference, 0 ) );
            item->ViewUpdate();
            ratsnest->Update( item );
        }

        getModel<BOARD>()->GetRatsnest()->Recalculate();
    }

    setTransitions();

    return 0;
}


int PLACEMENT_TOOL::AlignRight( TOOL_EVENT& aEvent )
{
206
    const SELECTION& selection = m_selectionTool->GetSelection();
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260

    if( selection.Size() > 1 )
    {
        PCB_BASE_FRAME* editFrame = getEditFrame<PCB_BASE_FRAME>();
        RN_DATA* ratsnest = getModel<BOARD>()->GetRatsnest();

        editFrame->OnModify();
        editFrame->SaveCopyInUndoList( selection.items, UR_CHANGED );

        // Compute the rightmost point of selection - it will be the edge of alignment
        int right = selection.Item<BOARD_ITEM>( 0 )->GetBoundingBox().GetRight();

        for( int i = 1; i < selection.Size(); ++i )
        {
            int currentRight = selection.Item<BOARD_ITEM>( i )->GetBoundingBox().GetRight();

            if( right < currentRight )      // X increases when going right
                right = currentRight;
        }

        // Move the selected items
        for( int i = 0; i < selection.Size(); ++i )
        {
            BOARD_ITEM* item = selection.Item<BOARD_ITEM>( i );
            int difference = right - item->GetBoundingBox().GetRight();

            item->Move( wxPoint( difference, 0 ) );
            item->ViewUpdate();
            ratsnest->Update( item );
        }

        getModel<BOARD>()->GetRatsnest()->Recalculate();
    }

    setTransitions();

    return 0;
}


static bool compareX( const BOARD_ITEM* aA, const BOARD_ITEM* aB )
{
    return aA->GetBoundingBox().Centre().x < aB->GetBoundingBox().Centre().x;
}


static bool compareY( const BOARD_ITEM* aA, const BOARD_ITEM* aB )
{
    return aA->GetBoundingBox().Centre().y < aB->GetBoundingBox().Centre().y;
}


int PLACEMENT_TOOL::DistributeHorizontally( TOOL_EVENT& aEvent )
{
261
    const SELECTION& selection = m_selectionTool->GetSelection();
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309

    if( selection.Size() > 1 )
    {
        PCB_BASE_FRAME* editFrame = getEditFrame<PCB_BASE_FRAME>();
        RN_DATA* ratsnest = getModel<BOARD>()->GetRatsnest();

        editFrame->OnModify();
        editFrame->SaveCopyInUndoList( selection.items, UR_CHANGED );

        // Prepare a list, so the items can be sorted by their X coordinate
        std::list<BOARD_ITEM*> itemsList;
        for( int i = 0; i < selection.Size(); ++i )
            itemsList.push_back( selection.Item<BOARD_ITEM>( i ) );

        // Sort items by X coordinate
        itemsList.sort( compareX );

        // Expected X coordinate for the next item (=minX)
        int position = (*itemsList.begin())->GetBoundingBox().Centre().x;

        // X coordinate for the last item
        const int maxX = (*itemsList.rbegin())->GetBoundingBox().Centre().x;

        // Distance between items
        const int distance = ( maxX - position ) / ( itemsList.size() - 1 );

        BOOST_FOREACH( BOARD_ITEM* item, itemsList )
        {
            int difference = position - item->GetBoundingBox().Centre().x;

            item->Move( wxPoint( difference, 0 ) );
            item->ViewUpdate();
            ratsnest->Update( item );

            position += distance;
        }

        getModel<BOARD>()->GetRatsnest()->Recalculate();
    }

    setTransitions();

    return 0;
}


int PLACEMENT_TOOL::DistributeVertically( TOOL_EVENT& aEvent )
{
310
    const SELECTION& selection = m_selectionTool->GetSelection();
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366

    if( selection.Size() > 1 )
    {
        PCB_BASE_FRAME* editFrame = getEditFrame<PCB_BASE_FRAME>();
        RN_DATA* ratsnest = getModel<BOARD>()->GetRatsnest();

        editFrame->OnModify();
        editFrame->SaveCopyInUndoList( selection.items, UR_CHANGED );

        // Prepare a list, so the items can be sorted by their Y coordinate
        std::list<BOARD_ITEM*> itemsList;
        for( int i = 0; i < selection.Size(); ++i )
            itemsList.push_back( selection.Item<BOARD_ITEM>( i ) );

        // Sort items by Y coordinate
        itemsList.sort( compareY );

        // Expected Y coordinate for the next item (=minY)
        int position = (*itemsList.begin())->GetBoundingBox().Centre().y;

        // Y coordinate for the last item
        const int maxY = (*itemsList.rbegin())->GetBoundingBox().Centre().y;

        // Distance between items
        const int distance = ( maxY - position ) / ( itemsList.size() - 1 );

        BOOST_FOREACH( BOARD_ITEM* item, itemsList )
        {
            int difference = position - item->GetBoundingBox().Centre().y;

            item->Move( wxPoint( 0, difference ) );
            item->ViewUpdate();
            ratsnest->Update( item );

            position += distance;
        }

        getModel<BOARD>()->GetRatsnest()->Recalculate();
    }

    setTransitions();

    return 0;
}


void PLACEMENT_TOOL::setTransitions()
{
    Go( &PLACEMENT_TOOL::AlignTop,    COMMON_ACTIONS::alignTop.MakeEvent() );
    Go( &PLACEMENT_TOOL::AlignBottom, COMMON_ACTIONS::alignBottom.MakeEvent() );
    Go( &PLACEMENT_TOOL::AlignLeft,   COMMON_ACTIONS::alignLeft.MakeEvent() );
    Go( &PLACEMENT_TOOL::AlignRight,  COMMON_ACTIONS::alignRight.MakeEvent() );

    Go( &PLACEMENT_TOOL::DistributeHorizontally,  COMMON_ACTIONS::distributeHorizontally.MakeEvent() );
    Go( &PLACEMENT_TOOL::DistributeVertically,    COMMON_ACTIONS::distributeVertically.MakeEvent() );
}