KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 The KiCad Developers, see AUTHORS.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 <tool/tool_manager.h>
27#include <tools/pcb_actions.h>
29
30#include "ar_autoplacer.h"
31#include "autoplace_tool.h"
32
33
35{
36}
37
38
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
54 fparent->GetCanvas()->GetView()->MarkDirty();
55 fparent->GetCanvas()->Refresh();
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 )
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
71 frame()->GetInfoBar()->RemoveAllButtons();
72 frame()->GetInfoBar()->ShowMessageFor( msg, 5000, wxICON_ERROR );
73 return 0;
74 }
75
76 if( !frame()->GetOverrideLocks() )
77 {
78 aFootprints.erase( std::remove_if( aFootprints.begin(), aFootprints.end(),
79 []( FOOTPRINT* fp )
80 {
81 return fp->IsLocked();
82 } ),
83 aFootprints.end() );
84 }
85
86 Activate();
87
88 AR_AUTOPLACER autoplacer( board() );
89 BOARD_COMMIT commit( frame() );
90
91 std::shared_ptr<KIGFX::VIEW_OVERLAY> overlay = view()->MakeOverlay();
92 autoplacer.SetOverlay( overlay );
93
94 fparent = frame();
95 std::function<int( FOOTPRINT* aFootprint )> callback = refreshCallback;
96 autoplacer.SetRefreshCallback( callback );
97
98 WX_PROGRESS_REPORTER progressReporter( frame(), _( "Autoplace Footprints" ), 1, PR_CAN_ABORT );
99 autoplacer.SetProgressReporter( &progressReporter );
100
101 auto result = autoplacer.AutoplaceFootprints( aFootprints, &commit, false );
102
103 if( result == AR_COMPLETED )
104 commit.Push( _( "Autoplace Footprints" ) );
105 else
106 commit.Revert();
107
108 return 0;
109}
110
111
113{
114 std::vector<FOOTPRINT*> footprints;
115
116 for( EDA_ITEM* item : selection() )
117 {
118 if( item->Type() == PCB_FOOTPRINT_T )
119 {
120 FOOTPRINT* footprint = static_cast<FOOTPRINT*>( item );
121 footprints.push_back( footprint );
122 }
123 }
124
125 return autoplace( footprints );
126}
127
128
130{
131 SHAPE_POLY_SET boardShape;
132 board()->GetBoardPolygonOutlines( boardShape, true );
133
134 std::vector<FOOTPRINT*> footprints;
135
136 for( FOOTPRINT* footprint : board()->Footprints() )
137 {
138 if( !boardShape.Contains( footprint->GetPosition() ) )
139 footprints.push_back( footprint );
140 }
141
142 return autoplace( footprints );
143}
144
145
@ AR_COMPLETED
static int refreshCallback(FOOTPRINT *aFootprint)
static PCB_BASE_EDIT_FRAME * fparent
BOX2< VECTOR2I > BOX2I
Definition box2.h:922
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.
void SetRefreshCallback(std::function< int(FOOTPRINT *aFootprint)> aCallback)
Callback to redraw on screen the view after changes, for instance after moving a footprint.
void SetProgressReporter(PROGRESS_REPORTER *aReporter)
int autoplaceOffboard(const TOOL_EVENT &aEvent)
~AUTOPLACE_TOOL()
Bind handlers to corresponding TOOL_ACTIONs.
int autoplaceSelected(const TOOL_EVENT &aEvent)
int autoplace(std::vector< FOOTPRINT * > &aFootprints)
void setTransitions() override
This method is meant to be overridden in order to specify handlers for events.
virtual void Push(const wxString &aMessage=wxEmptyString, int aCommitFlags=0) override
Execute the changes.
virtual void Revert() 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:1023
bool GetBoardPolygonOutlines(SHAPE_POLY_SET &aOutlines, bool aInferOutlineIfNecessary, OUTLINE_ERROR_HANDLER *aErrorHandler=nullptr, bool aAllowUseArcsInPolygons=false, bool aIncludeNPTHAsOutlines=false)
Extract the board outlines and build a closed polygon from lines, arcs and circle items on edge cut l...
Definition board.cpp:2832
constexpr size_type GetWidth() const
Definition box2.h:214
constexpr size_type GetHeight() const
Definition box2.h:215
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:98
std::shared_ptr< VIEW_OVERLAY > MakeOverlay()
Definition view.cpp:1698
static TOOL_ACTION autoplaceOffboardComponents
static TOOL_ACTION autoplaceSelectedComponents
Common, abstract interface for edit frames.
T * frame() const
KIGFX::PCB_VIEW * view() const
PCB_TOOL_BASE(TOOL_ID aId, const std::string &aName)
Constructor.
BOARD * board() const
const PCB_SELECTION & selection() const
FOOTPRINT * footprint() const
Represent a set of closed polygons.
bool Contains(const VECTOR2I &aP, int aSubpolyIndex=-1, int aAccuracy=0, bool aUseBBoxCaches=false) const
Return true if a given subpolygon contains the point aP.
Generic, UI-independent tool event.
Definition tool_event.h:171
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.
Multi-thread safe progress reporter dialog, intended for use of tasks that parallel reporting back of...
#define _(s)
wxString LayerName(int aLayer)
Returns the default display name for a given layer.
Definition layer_id.cpp:31
@ Edge_Cuts
Definition layer_ids.h:112
std::shared_ptr< PNS_LOG_VIEWER_OVERLAY > overlay
wxString result
Test unit parsing edge cases and error handling.
@ PCB_FOOTPRINT_T
class FOOTPRINT, a footprint
Definition typeinfo.h:86
#define PR_CAN_ABORT