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 
49 static 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 
62 int 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." ),
69  LayerName( Edge_Cuts ) );
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  new 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 }
static PCB_BASE_EDIT_FRAME * fparent
BOARD * board() const
std::shared_ptr< PNS_LOG_VIEWER_OVERLAY > overlay
Definition: playground.cpp:36
static TOOL_ACTION autoplaceOffboardComponents
Definition: pcb_actions.h:468
void setTransitions() override
This method is meant to be overridden in order to specify handlers for events.
const EDA_RECT GetBoardEdgesBoundingBox() const
Return the board bounding box calculated using exclusively the board edges (graphics on Edge....
Definition: board.h:742
static TOOL_ACTION autoplaceSelectedComponents
Definition: pcb_actions.h:469
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
virtual void Revert() override
PCB_DRAW_PANEL_GAL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
int GetWidth() const
Definition: eda_rect.h:109
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
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 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).
std::shared_ptr< VIEW_OVERLAY > MakeOverlay()
Definition: view.cpp:1531
PCB_BASE_EDIT_FRAME * frame() const
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
Multi-thread safe progress reporter dialog, intended for use of tasks that parallel reporting back of...
const PCB_SELECTION & selection() const
~AUTOPLACE_TOOL()
Bind handlers to corresponding TOOL_ACTIONs.
Generic, UI-independent tool event.
Definition: tool_event.h:152
KIGFX::PCB_VIEW * view() const
#define _(s)
wxString LayerName(int aLayer)
Returns the default display name for a given layer.
Definition: layer_id.cpp:27
virtual KIGFX::PCB_VIEW * GetView() const override
Return a pointer to the #VIEW instance used in the panel.
void RemoveAllButtons()
Remove all the buttons that have been added by the user.
Definition: infobar.cpp:286
int GetHeight() const
Definition: eda_rect.h:110
int autoplace(std::vector< FOOTPRINT * > &aFootprints, bool aPlaceOffboard)
class FOOTPRINT, a footprint
Definition: typeinfo.h:88
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
int autoplaceOffboard(const TOOL_EVENT &aEvent)
Common, abstract interface for edit frames.
void SetProgressReporter(PROGRESS_REPORTER *aReporter)
Definition: ar_autoplacer.h:81
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...
Handle the component boundary box.
Definition: eda_rect.h:42
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:100
virtual void Push(const wxString &aMessage=wxT("A commit"), bool aCreateUndoEntry=true, bool aSetDirtyBit=true) override
Revert the commit by restoring the modified items state.
void Activate()
Run the tool.
WX_INFOBAR * GetInfoBar()
int autoplaceSelected(const TOOL_EVENT &aEvent)
static int refreshCallback(FOOTPRINT *aFootprint)
void MarkDirty()
Force redraw of view on the next rendering.
Definition: view.h:633
AR_RESULT AutoplaceFootprints(std::vector< FOOTPRINT * > &aFootprints, BOARD_COMMIT *aCommit, bool aPlaceOffboardModules=false)