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, see <https://www.gnu.org/licenses/>.
18 */
19
20
21#include <board_commit.h>
22#include <footprint.h>
23#include <tools/pcb_selection.h>
24#include <tool/tool_manager.h>
25#include <tools/pcb_actions.h>
26#include <widgets/wx_infobar.h>
28
29#include "ar_autoplacer.h"
30#include "autoplace_tool.h"
31
32
34{
35}
36
37
41
42
43// A helper call back function used by autoplace.
44// It is called by the autoplacer to update the view, when something must be displayed
45// especially each time a footprint is autoplaced,
47
48static int refreshCallback( FOOTPRINT* aFootprint )
49{
50 if( aFootprint )
51 fparent->GetCanvas()->GetView()->Update( aFootprint );
52
53 fparent->GetCanvas()->GetView()->MarkDirty();
54 fparent->GetCanvas()->Refresh();
55 wxSafeYield(); // Give a slice of time to refresh the display
56
57 return 0;
58}
59
60
61int AUTOPLACE_TOOL::autoplace( std::vector<FOOTPRINT*>& aFootprints )
62{
64
65 if( bbox.GetWidth() == 0 || bbox.GetHeight() == 0 )
66 {
67 wxString msg = wxString::Format( _( "Board edges must be defined on the %s layer." ),
69
70 frame()->GetInfoBar()->RemoveAllButtons();
71 frame()->GetInfoBar()->ShowMessageFor( msg, 5000, wxICON_ERROR );
72 return 0;
73 }
74
75 if( !frame()->GetOverrideLocks() )
76 {
77 aFootprints.erase( std::remove_if( aFootprints.begin(), aFootprints.end(),
78 []( FOOTPRINT* fp )
79 {
80 return fp->IsLocked();
81 } ),
82 aFootprints.end() );
83 }
84
85 Activate();
86
87 AR_AUTOPLACER autoplacer( board() );
88 BOARD_COMMIT commit( frame() );
89
90 std::shared_ptr<KIGFX::VIEW_OVERLAY> overlay = view()->MakeOverlay();
91 autoplacer.SetOverlay( overlay );
92
93 fparent = frame();
94 std::function<int( FOOTPRINT* aFootprint )> callback = refreshCallback;
95 autoplacer.SetRefreshCallback( callback );
96
97 WX_PROGRESS_REPORTER progressReporter( frame(), _( "Autoplace Footprints" ), 1, PR_CAN_ABORT );
98 autoplacer.SetProgressReporter( &progressReporter );
99
100 auto result = autoplacer.AutoplaceFootprints( aFootprints, &commit, false );
101
102 if( result == AR_COMPLETED )
103 commit.Push( _( "Autoplace Footprints" ) );
104 else
105 commit.Revert();
106
107 return 0;
108}
109
110
112{
113 std::vector<FOOTPRINT*> footprints;
114
115 for( EDA_ITEM* item : selection() )
116 {
117 if( item->Type() == PCB_FOOTPRINT_T )
118 {
119 FOOTPRINT* footprint = static_cast<FOOTPRINT*>( item );
120 footprints.push_back( footprint );
121 }
122 }
123
124 return autoplace( footprints );
125}
126
127
129{
130 SHAPE_POLY_SET boardShape;
131 board()->GetBoardPolygonOutlines( boardShape, true );
132
133 std::vector<FOOTPRINT*> footprints;
134
135 for( FOOTPRINT* footprint : board()->Footprints() )
136 {
137 if( !boardShape.Contains( footprint->GetPosition() ) )
138 footprints.push_back( footprint );
139 }
140
141 return autoplace( footprints );
142}
143
144
@ AR_COMPLETED
static int refreshCallback(FOOTPRINT *aFootprint)
static PCB_BASE_EDIT_FRAME * fparent
BOX2< VECTOR2I > BOX2I
Definition box2.h:918
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:1152
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:3372
constexpr size_type GetWidth() const
Definition box2.h:210
constexpr size_type GetHeight() const
Definition box2.h:211
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:96
std::shared_ptr< VIEW_OVERLAY > MakeOverlay()
Definition view.cpp:1849
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:167
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:108
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:79
#define PR_CAN_ABORT