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( MODULE* aModule )
50 {
51  if( aModule )
52  fparent->GetCanvas()->GetView()->Update( aModule );
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<MODULE*>& aModules, 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( MODULE* aModule )> 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.AutoplaceModules( aModules, &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<MODULE*> mods;
106 
107  for( EDA_ITEM* item : selection() )
108  {
109  if( item->Type() == PCB_MODULE_T )
110  mods.push_back( static_cast<MODULE*>( item ) );
111  }
112 
113  return autoplace( mods, false );
114 }
115 
116 
118 {
119  std::vector<MODULE*> mods;
120 
121  return autoplace( mods, true );
122 }
123 
124 
126 {
129 }
void ShowMessageFor(const wxString &aMessage, int aTime, int aFlags=wxICON_INFORMATION)
Show the infobar with the provided message and icon for a specific period of time.
Definition: infobar.cpp:109
static PCB_BASE_EDIT_FRAME * fparent
BOARD * board() const
static TOOL_ACTION autoplaceOffboardComponents
Definition: pcb_actions.h:459
void setTransitions() override
Bind handlers to corresponding TOOL_ACTIONs
const EDA_RECT GetBoardEdgesBoundingBox() const
Returns the board bounding box calculated using exclusively the board edges (graphics on Edge....
Definition: class_board.h:782
static TOOL_ACTION autoplaceSelectedComponents
Definition: pcb_actions.h:460
virtual void Revert() override
Revertes the commit by restoring the modifed items state.
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:119
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))
Function Go()
std::shared_ptr< VIEW_OVERLAY > MakeOverlay()
Definition: view.cpp:1526
PCB_BASE_EDIT_FRAME * frame() const
class MODULE, a footprint
Definition: typeinfo.h:89
virtual void Update(const VIEW_ITEM *aItem, int aUpdateFlags) const override
For dynamic VIEWs, informs the associated VIEW that the graphical representation of this item has cha...
Definition: pcb_view.cpp:93
Multi-thread safe progress reporter dialog, intended for use of tasks that paralleize reporting back ...
const PCBNEW_SELECTION & selection() const
int autoplace(std::vector< MODULE * > &aModules, bool aPlaceOffboard)
TOOL_EVENT.
Definition: tool_event.h:171
KIGFX::PCB_VIEW * view() const
wxString LayerName(int aLayer)
Returns the default display name for a given layer.
Definition: layer_id.cpp:27
void SetRefreshCallback(std::function< int(MODULE *aModule)> aCallback)
a callback function to redraw on screen the view after changes, for instance after moving a footprint
Definition: ar_autoplacer.h:77
virtual KIGFX::PCB_VIEW * GetView() const override
Function GetView() Returns 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:226
int GetHeight() const
Definition: eda_rect.h:120
virtual void Refresh(bool aEraseBackground=true, const wxRect *aRect=NULL) override
Update the board display after modifying it by a python script (note: it is automatically called by a...
int autoplaceOffboard(const TOOL_EVENT &aEvent)
Common, abstract interface for edit frames.
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:201
#define _(s)
Definition: 3d_actions.cpp:33
void SetProgressReporter(PROGRESS_REPORTER *aReporter)
Definition: ar_autoplacer.h:82
EDA_RECT handles the component boundary box.
Definition: eda_rect.h:44
static int refreshCallback(MODULE *aModule)
EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boards.
Definition: eda_item.h:148
virtual void Push(const wxString &aMessage=wxT("A commit"), bool aCreateUndoEntry=true, bool aSetDirtyBit=true) override
Executes the changes.
void Activate()
Function Activate() Runs the tool.
WX_INFOBAR * GetInfoBar()
int autoplaceSelected(const TOOL_EVENT &aEvent)
AR_RESULT AutoplaceModules(std::vector< MODULE * > &aModules, BOARD_COMMIT *aCommit, bool aPlaceOffboardModules=false)
void MarkDirty()
Function MarkDirty() Forces redraw of view on the next rendering.
Definition: view.h:596