KiCad PCB EDA Suite
Loading...
Searching...
No Matches
generator_tool_pns_proxy.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) 2023 Alex Shvartzkop <[email protected]>
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
22
23#include <pad.h>
24#include <pcb_shape.h>
26
28#include <router/pns_solid.h>
29#include <router/pns_router.h>
30#include <pcbnew_settings.h>
33
34
36{
37public:
38 void SetHostTool( PCB_TOOL_BASE* aTool ) override
39 {
40 m_tool = aTool;
41 m_commit = nullptr;
42
44 }
45
46 void Commit() override
47 { //
48 m_changes.emplace_back();
49 }
50
52 {
53 m_changes.clear();
54 m_changes.emplace_back();
55 m_createdItems.clear();
56 }
57
58 // The base implementations build ROUTER_PREVIEW_ITEMs and touch the GAL view, which do
59 // not exist in headless sessions; skip display entirely there.
60 void DisplayItem( const PNS::ITEM* aItem, int aClearance, bool aEdit = false,
61 int aFlags = 0 ) override
62 {
63 if( !m_view )
64 return;
65
66 PNS_KICAD_IFACE::DisplayItem( aItem, aClearance, aEdit, aFlags );
67 }
68
69 void EraseView() override
70 {
71 if( !m_view )
72 return;
73
75 }
76
77 void HideItem( PNS::ITEM* aItem ) override
78 {
79 if( !m_view )
80 return;
81
83 }
84
85 void DisplayPathLine( const SHAPE_LINE_CHAIN& aLine, int aImportance ) override
86 {
87 if( !m_view )
88 return;
89
90 PNS_KICAD_IFACE::DisplayPathLine( aLine, aImportance );
91 }
92
93 void DisplayRatline( const SHAPE_LINE_CHAIN& aRatline, PNS::NET_HANDLE aNet ) override
94 {
95 if( !m_view )
96 return;
97
98 PNS_KICAD_IFACE::DisplayRatline( aRatline, aNet );
99 }
100
101 void AddItem( PNS::ITEM* aItem ) override
102 {
103 BOARD_ITEM* brdItem = createBoardItem( aItem );
104
105 if( brdItem )
106 {
107 aItem->SetParent( brdItem );
108 brdItem->ClearFlags();
109
110 m_createdItems.insert( brdItem );
111 m_changes.back().addedItems.emplace( brdItem );
112 }
113 }
114
115 void UpdateItem( PNS::ITEM* aItem ) override
116 { //
117 modifyBoardItem( aItem );
118 }
119
120 void RemoveItem( PNS::ITEM* aItem ) override
121 {
122 BOARD_ITEM* parent = aItem->Parent();
123
124 if( aItem->OfKind( PNS::ITEM::SOLID_T ) )
125 {
126 PAD* pad = static_cast<PAD*>( parent );
127 VECTOR2I pos = static_cast<PNS::SOLID*>( aItem )->Pos();
128
129 m_fpOffsets[pad].p_old = pos;
130 return;
131 }
132
133 if( parent )
134 {
135 m_changes.back().removedItems.emplace( parent );
136 }
137 }
138
139 std::vector<GENERATOR_PNS_CHANGES>& Changes() { return m_changes; };
140
141 bool IsGeneratedItem( BOARD_ITEM* aItem ) const { return m_createdItems.contains( aItem ); }
142
143private:
144 std::set<BOARD_ITEM*> m_createdItems;
145
146 std::vector<GENERATOR_PNS_CHANGES> m_changes;
147};
148
149
151{
152 static_cast<PNS_KICAD_IFACE_GENERATOR*>( GetInterface() )->ClearCommits();
153}
154
155
156const std::vector<GENERATOR_PNS_CHANGES>& GENERATOR_TOOL_PNS_PROXY::GetRouterChanges()
157{
158 return static_cast<PNS_KICAD_IFACE_GENERATOR*>( GetInterface() )->Changes();
159}
160
161
163{
164 return static_cast<PNS_KICAD_IFACE_GENERATOR*>( GetInterface() )->IsGeneratedItem( aItem );
165}
166
167
169 PNS::TOOL_BASE( aToolName )
170{
171}
172
173
177
178
180{
181 delete m_gridHelper;
182 delete m_router;
183 delete m_iface; // Delete after m_router because PNS::NODE dtor needs m_ruleResolver
184
185 if( aReason == RESET_REASON::SHUTDOWN )
186 {
187 m_iface = nullptr;
188 m_router = nullptr;
189 m_gridHelper = nullptr;
190 return;
191 }
192
194 m_iface->SetBoard( board() );
195 m_iface->SetView( getView() );
196 m_iface->SetHostTool( this );
197
198 m_router = new PNS::ROUTER;
199 m_router->SetInterface( m_iface );
200 m_router->ClearWorld();
201 m_router->SyncWorld();
202
203 m_router->UpdateSizes( m_savedSizes );
204
205 // Headless sessions (kicad-cli api-server) have no frame; fall back to the global
206 // settings store and default magnetic settings.
207 PCBNEW_SETTINGS* settings = GetAppSettings<PCBNEW_SETTINGS>( "pcbnew" );
208
209 if( !settings->m_PnsSettings )
210 settings->m_PnsSettings = std::make_unique<PNS::ROUTING_SETTINGS>( settings, "tools.pns" );
211
212 m_router->LoadSettings( settings->m_PnsSettings.get() );
213
214 // Only build the grid helper in GUI mode where a view exists
215 if( getView() )
216 {
217 static MAGNETIC_SETTINGS defaultMagnetic;
218 m_gridHelper = new PCB_GRID_HELPER( m_toolMgr, &defaultMagnetic );
219 }
220}
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:84
void ClearFlags(EDA_ITEM_FLAGS aMask=EDA_ITEM_ALL_FLAGS)
Definition eda_item.h:160
void Reset(RESET_REASON aReason) override
Bring the tool to a known, initial state.
GENERATOR_TOOL_PNS_PROXY(const std::string &aToolName)
const std::vector< GENERATOR_PNS_CHANGES > & GetRouterChanges()
bool ItemCreatedBySession(BOARD_ITEM *aItem) const
Returns true if the given board item was created by the router interface during the current generator...
Definition pad.h:61
std::unique_ptr< PNS::ROUTING_SETTINGS > m_PnsSettings
BOARD * board() const
Base class for PNS router board items.
Definition pns_item.h:98
BOARD_ITEM * Parent() const
Definition pns_item.h:199
void SetParent(BOARD_ITEM *aParent)
Definition pns_item.h:191
bool OfKind(int aKindMask) const
Definition pns_item.h:181
PNS_KICAD_IFACE * GetInterface() const
SIZES_SETTINGS m_savedSizes
PNS_KICAD_IFACE * m_iface
TOOL_BASE(const std::string &aToolName)
ROUTER * m_router
PCB_GRID_HELPER * m_gridHelper
std::vector< GENERATOR_PNS_CHANGES > m_changes
void SetHostTool(PCB_TOOL_BASE *aTool) override
std::set< BOARD_ITEM * > m_createdItems
void DisplayPathLine(const SHAPE_LINE_CHAIN &aLine, int aImportance) override
void AddItem(PNS::ITEM *aItem) override
void DisplayItem(const PNS::ITEM *aItem, int aClearance, bool aEdit=false, int aFlags=0) override
void UpdateItem(PNS::ITEM *aItem) override
void HideItem(PNS::ITEM *aItem) override
void RemoveItem(PNS::ITEM *aItem) override
bool IsGeneratedItem(BOARD_ITEM *aItem) const
std::vector< GENERATOR_PNS_CHANGES > & Changes()
void DisplayRatline(const SHAPE_LINE_CHAIN &aRatline, PNS::NET_HANDLE aNet) override
std::map< PAD *, OFFSET > m_fpOffsets
void DisplayItem(const PNS::ITEM *aItem, int aClearance, bool aEdit=false, int aFlags=0) override
std::unique_ptr< BOARD_COMMIT > m_commit
void EraseView() override
void HideItem(PNS::ITEM *aItem) override
BOARD_CONNECTED_ITEM * createBoardItem(PNS::ITEM *aItem)
KIGFX::VIEW * m_view
void DisplayPathLine(const SHAPE_LINE_CHAIN &aLine, int aImportance) override
PCB_TOOL_BASE * m_tool
void modifyBoardItem(PNS::ITEM *aItem)
void DisplayRatline(const SHAPE_LINE_CHAIN &aRatline, PNS::NET_HANDLE aNet) override
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
TOOL_MANAGER * m_toolMgr
Definition tool_base.h:220
KIGFX::VIEW * getView() const
Return the instance of KIGFX::VIEW object used in the application.
Definition tool_base.cpp:34
RESET_REASON
Determine the reason of reset for a tool.
Definition tool_base.h:74
@ SHUTDOWN
Tool is being shut down.
Definition tool_base.h:80
Push and Shove diff pair dimensions (gap) settings dialog.
void * NET_HANDLE
Definition pns_item.h:55
T * GetAppSettings(const char *aFilename)
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708