KiCad PCB EDA Suite
autoplace_tool.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2017-2020 Kicad Developers, see change_log.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24
25#include <board_commit.h>
26#include <tools/pcb_actions.h>
27#include <widgets/infobar.h>
29
30#include "ar_autoplacer.h"
31#include "autoplace_tool.h"
32
33
35{
36}
37
38
40{
41}
42
43
44// A helper call back function used by autoplace.
45// It is called by the autoplacer to update the view, when something must be displayed
46// especially each time a footprint is autoplaced,
48
49static int refreshCallback( FOOTPRINT* aFootprint )
50{
51 if( aFootprint )
52 fparent->GetCanvas()->GetView()->Update( aFootprint );
53
56 wxSafeYield(); // Give a slice of time to refresh the display
57
58 return 0;
59}
60
61
62int AUTOPLACE_TOOL::autoplace( std::vector<FOOTPRINT*>& aFootprints, bool aPlaceOffboard )
63{
65
66 if( bbox.GetWidth() == 0 || bbox.GetHeight() == 0 )
67 {
68 wxString msg = wxString::Format( _( "Board edges must be defined on the %s layer." ),
70
72 frame()->GetInfoBar()->ShowMessageFor( msg, 5000, wxICON_ERROR );
73 return 0;
74 }
75
76 Activate();
77
78 AR_AUTOPLACER autoplacer( board() );
79 BOARD_COMMIT commit( frame() );
80
81 std::shared_ptr<KIGFX::VIEW_OVERLAY> overlay = view()->MakeOverlay();
82 autoplacer.SetOverlay( overlay );
83
84 fparent = frame();
85 std::function<int( FOOTPRINT* aFootprint )> callback = refreshCallback;
86 autoplacer.SetRefreshCallback( callback );
87
88 std::unique_ptr<WX_PROGRESS_REPORTER> progressReporter =
89 std::make_unique<WX_PROGRESS_REPORTER>( frame(), _( "Autoplace Components" ), 1 );
90
91 autoplacer.SetProgressReporter( progressReporter.get() );
92 auto result = autoplacer.AutoplaceFootprints( aFootprints, &commit, aPlaceOffboard );
93
94 if( result == AR_COMPLETED )
95 commit.Push( _( "Autoplace components" ) );
96 else
97 commit.Revert();
98
99 return 0;
100}
101
102
104{
105 std::vector<FOOTPRINT*> footprints;
106
107 for( EDA_ITEM* item : selection() )
108 {
109 if( item->Type() == PCB_FOOTPRINT_T )
110 footprints.push_back( static_cast<FOOTPRINT*>( item ) );
111 }
112
113 return autoplace( footprints, false );
114}
115
116
118{
119 std::vector<FOOTPRINT*> footprints;
120
121 return autoplace( footprints, true );
122}
123
124
126{
129}
@ AR_COMPLETED
Definition: ar_autoplacer.h:50
static int refreshCallback(FOOTPRINT *aFootprint)
static PCB_BASE_EDIT_FRAME * fparent
AR_RESULT AutoplaceFootprints(std::vector< FOOTPRINT * > &aFootprints, BOARD_COMMIT *aCommit, bool aPlaceOffboardModules=false)
void SetOverlay(std::shared_ptr< KIGFX::VIEW_OVERLAY > aOverlay)
Set a VIEW overlay to draw items during a autoplace session.
Definition: ar_autoplacer.h:68
void SetRefreshCallback(std::function< int(FOOTPRINT *aFootprint)> aCallback)
Callback to redraw on screen the view after changes, for instance after moving a footprint.
Definition: ar_autoplacer.h:76
void SetProgressReporter(PROGRESS_REPORTER *aReporter)
Definition: ar_autoplacer.h:81
int autoplaceOffboard(const TOOL_EVENT &aEvent)
~AUTOPLACE_TOOL()
Bind handlers to corresponding TOOL_ACTIONs.
int autoplace(std::vector< FOOTPRINT * > &aFootprints, bool aPlaceOffboard)
int autoplaceSelected(const TOOL_EVENT &aEvent)
void setTransitions() override
This method is meant to be overridden in order to specify handlers for events.
virtual void Revert() override
virtual void Push(const wxString &aMessage=wxT("A commit"), int aCommitFlags=0) override
Revert the commit by restoring the modified items state.
const BOX2I GetBoardEdgesBoundingBox() const
Return the board bounding box calculated using exclusively the board edges (graphics on Edge....
Definition: board.h:823
coord_type GetHeight() const
Definition: box2.h:188
coord_type GetWidth() const
Definition: box2.h:187
WX_INFOBAR * GetInfoBar()
virtual void Refresh(bool aEraseBackground=true, const wxRect *aRect=nullptr) override
Update the board display after modifying it by a python script (note: it is automatically called by a...
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:85
virtual void Update(const VIEW_ITEM *aItem, int aUpdateFlags) const override
For dynamic VIEWs, inform the associated VIEW that the graphical representation of this item has chan...
Definition: pcb_view.cpp:92
std::shared_ptr< VIEW_OVERLAY > MakeOverlay()
Definition: view.cpp:1599
void MarkDirty()
Force redraw of view on the next rendering.
Definition: view.h:641
static TOOL_ACTION autoplaceOffboardComponents
Definition: pcb_actions.h:516
static TOOL_ACTION autoplaceSelectedComponents
Definition: pcb_actions.h:517
Common, abstract interface for edit frames.
PCB_DRAW_PANEL_GAL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
virtual KIGFX::PCB_VIEW * GetView() const override
Return a pointer to the #VIEW instance used in the panel.
KIGFX::PCB_VIEW * view() const
PCB_BASE_EDIT_FRAME * frame() const
BOARD * board() const
const PCB_SELECTION & selection() const
Generic, UI-independent tool event.
Definition: tool_event.h:156
void Go(int(T::*aStateFunc)(const TOOL_EVENT &), const TOOL_EVENT_LIST &aConditions=TOOL_EVENT(TC_ANY, TA_ANY))
Define which state (aStateFunc) to go when a certain event arrives (aConditions).
void Activate()
Run the tool.
void RemoveAllButtons()
Remove all the buttons that have been added by the user.
Definition: infobar.cpp:286
void ShowMessageFor(const wxString &aMessage, int aTime, int aFlags=wxICON_INFORMATION, MESSAGE_TYPE aType=WX_INFOBAR::MESSAGE_TYPE::GENERIC)
Show the infobar with the provided message and icon for a specific period of time.
Definition: infobar.cpp:128
#define _(s)
wxString LayerName(int aLayer)
Returns the default display name for a given layer.
Definition: layer_id.cpp:30
@ Edge_Cuts
Definition: layer_ids.h:113
std::shared_ptr< PNS_LOG_VIEWER_OVERLAY > overlay
Definition: playground.cpp:36
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
@ PCB_FOOTPRINT_T
class FOOTPRINT, a footprint
Definition: typeinfo.h:86