KiCad PCB EDA Suite
Loading...
Searching...
No Matches
pns_component_dragger.cpp
Go to the documentation of this file.
1/*
2 * KiRouter - a push-and-(sometimes-)shove PCB router
3 *
4 * Copyright (C) 2013-2020 CERN
5 * Copyright (C) 2021-2024 KiCad Developers, see AUTHORS.txt for contributors.
6 * Author: Tomasz Wlostowski <[email protected]>
7 *
8 * This program is free software: you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation, either version 3 of the License, or (at your
11 * option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#include <memory>
23
24#include "pns_arc.h"
25#include "pns_line.h"
26#include "pns_solid.h"
27#include "pns_router.h"
28
30#include "pns_debug_decorator.h"
31
32namespace PNS
33{
34
36{
37 // ensure all variables are initialized
38 m_dragStatus = false;
39 m_currentNode = nullptr;
40}
41
42
44{
45}
46
47
48bool COMPONENT_DRAGGER::Start( const VECTOR2I& aP, ITEM_SET& aPrimitives )
49{
50 assert( m_world );
51
52 m_currentNode = nullptr;
53 m_initialDraggedItems = aPrimitives;
54 m_p0 = aP;
55
56 std::unordered_set<LINKED_ITEM*> seenItems;
57
58 auto addLinked =
59 [&]( SOLID* aSolid, const JOINT* aJoint, LINKED_ITEM* aItem, VECTOR2I aOffset = {} )
60 {
61 if( seenItems.count( aItem ) )
62 return;
63
64 seenItems.insert( aItem );
65
66 // Segments that go directly between two linked pads are special-cased
67 VECTOR2I otherEnd = ( aJoint->Pos() == aItem->Anchor( 0 ) ) ? aItem->Anchor( 1 )
68 : aItem->Anchor( 0 );
69 const JOINT* otherJoint = m_world->FindJoint( otherEnd, aItem->Layer(), aItem->Net() );
70
71 if( otherJoint && otherJoint->LinkCount( ITEM::SOLID_T ) )
72 {
73 for( ITEM* otherItem : otherJoint->LinkList() )
74 {
75 if( aPrimitives.Contains( otherItem ) )
76 {
77 m_fixedItems.insert( aItem );
78 return;
79 }
80 }
81 }
82
83 int segIndex;
85
86 cn.origLine = m_world->AssembleLine( aItem, &segIndex );
87 cn.attachedPad = aSolid;
88 cn.offset = aOffset;
89
90 // Lines that go directly between two linked pads are also special-cased
91 const SHAPE_LINE_CHAIN& line = cn.origLine.CLine();
92 const JOINT* jA = m_world->FindJoint( line.CPoint( 0 ), aItem->Layer(), aItem->Net() );
93 const JOINT* jB = m_world->FindJoint( line.CPoint( -1 ), aItem->Layer(), aItem->Net() );
94
95 wxASSERT( jA == aJoint || jB == aJoint );
96 const JOINT* jSearch = ( jA == aJoint ) ? jB : jA;
97
98 if( jSearch && jSearch->LinkCount( ITEM::SOLID_T ) )
99 {
100 for( ITEM* otherItem : jSearch->LinkList() )
101 {
102 if( aPrimitives.Contains( otherItem ) )
103 {
104 for( ITEM* item : cn.origLine.Links() )
105 m_fixedItems.insert( item );
106
107 return;
108 }
109 }
110 }
111
112 m_conns.push_back( cn );
113 };
114
115 for( ITEM* item : aPrimitives.Items() )
116 {
117 if( item->Kind() != ITEM::SOLID_T )
118 continue;
119
120 SOLID* solid = static_cast<SOLID*>( item );
121
122 m_solids.insert( solid );
123
124 if( !item->IsRoutable() )
125 continue;
126
127 const JOINT* jt = m_world->FindJoint( solid->Pos(), solid );
128
129 for( ITEM* link : jt->LinkList() )
130 {
131 if( link->OfKind( ITEM::SEGMENT_T | ITEM::ARC_T ) )
132 addLinked( solid, jt, static_cast<LINKED_ITEM*>( link ) );
133 }
134
135 std::vector<JOINT*> extraJoints;
136
137 m_world->QueryJoints( solid->Hull().BBox(), extraJoints, solid->Layers(),
139
140 for( JOINT* extraJoint : extraJoints )
141 {
142 if( extraJoint->Net() == jt->Net() && extraJoint->LinkCount() == 1 )
143 {
144 LINKED_ITEM* li = static_cast<LINKED_ITEM*>( extraJoint->LinkList().front() );
145
146 if( li->Collide( solid, m_world ) )
147 addLinked( solid, extraJoint, li, extraJoint->Pos() - solid->Pos() );
148 }
149 }
150 }
151
152 return true;
153}
154
155
157{
158 assert( m_world );
159
162
163 for( ITEM* item : m_initialDraggedItems )
164 m_currentNode->Remove( item );
165
167
168 for( SOLID* s : m_solids )
169 {
170 VECTOR2I p_next = aP - m_p0 + s->Pos();
171 std::unique_ptr<SOLID> snew( static_cast<SOLID*>( s->Clone() ) );
172 snew->SetPos( p_next );
173
174 m_draggedItems.Add( snew.get() );
175 m_currentNode->Add( std::move( snew ) );
176
177 if( !s->IsRoutable() )
178 continue;
179
180 for( DRAGGED_CONNECTION& l : m_conns )
181 {
182 if( l.attachedPad == s )
183 {
184 l.p_orig = s->Pos() + l.offset;
185 l.p_next = p_next + l.offset;
186 }
187 }
188 }
189
190 for( ITEM* item : m_fixedItems )
191 {
192 m_currentNode->Remove( item );
193
194 switch( item->Kind() )
195 {
196 case ITEM::SEGMENT_T:
197 {
198 SEGMENT* s = static_cast<SEGMENT*>( item );
199 std::unique_ptr<SEGMENT> s_new( s->Clone() );
200
201 SEG orig = s->Seg();
202 s_new->SetEnds( aP - m_p0 + orig.A, aP - m_p0 + orig.B );
203
204 m_draggedItems.Add( s_new.get() );
205 m_currentNode->Add( std::move( s_new ) );
206
207 break;
208 }
209
210 case ITEM::ARC_T:
211 {
212 ARC* a = static_cast<ARC*>( item );
213 std::unique_ptr<ARC> a_new( a->Clone() );
214
215 SHAPE_ARC& arc = a_new->Arc();
216 arc.Move( aP - m_p0 );
217
218 m_draggedItems.Add( a_new.get() );
219 m_currentNode->Add( std::move( a_new ) );
220 break;
221 }
222
223 default:
224 wxFAIL_MSG( wxT( "Unexpected item type in COMPONENT_DRAGGER::m_fixedItems" ) );
225 }
226 }
227
229 {
230 LINE l_new( cn.origLine );
231 l_new.Unmark();
232 l_new.ClearLinks();
233 l_new.DragCorner( cn.p_next, cn.origLine.CLine().Find( cn.p_orig ) );
234
235 PNS_DBG( Dbg(), AddItem, &l_new, BLUE, 0, wxT( "cdrag-new-fanout" ) );
236 m_draggedItems.Add( l_new );
237
238 LINE l_orig( cn.origLine );
239 m_currentNode->Remove( l_orig );
240 m_currentNode->Add( l_new );
241 }
242
243 return true;
244}
245
246
247bool COMPONENT_DRAGGER::FixRoute( bool aForceCommit )
248{
249 NODE* node = CurrentNode();
250
251 if( node )
252 {
253 if( Settings().AllowDRCViolations() || aForceCommit || !node->CheckColliding( m_draggedItems ) )
254 {
255 Router()->CommitRouting( node );
256 return true;
257 }
258 }
259
260 return false;
261}
262
263
265{
267}
268
269
271{
272 return m_draggedItems;
273}
274
275}; // namespace PNS
ROUTER * Router() const
Return current router settings.
Definition: pns_algo_base.h:54
ROUTING_SETTINGS & Settings() const
Return the logger object, allowing to dump geometry to a file.
DEBUG_DECORATOR * Dbg() const
Definition: pns_algo_base.h:78
ARC * Clone() const override
Return a deep copy of the item.
Definition: pns_arc.cpp:34
COMPONENT_DRAGGER(ROUTER *aRouter)
const ITEM_SET Traces() override
Function Traces()
NODE * CurrentNode() const override
Function CurrentNode()
std::set< ITEM * > m_fixedItems
bool FixRoute(bool aForceCommit) override
Function FixRoute()
std::vector< DRAGGED_CONNECTION > m_conns
bool Start(const VECTOR2I &aP, ITEM_SET &aPrimitives) override
Function Start()
std::set< SOLID * > m_solids
bool Drag(const VECTOR2I &aP) override
Function Drag()
DRAG_ALGO.
Definition: pns_drag_algo.h:44
void Add(const LINE &aLine)
Definition: pns_itemset.cpp:32
bool Contains(ITEM *aItem) const
Definition: pns_itemset.h:151
std::vector< ITEM * > & Items()
Definition: pns_itemset.h:87
Base class for PNS router board items.
Definition: pns_item.h:97
@ SEGMENT_T
Definition: pns_item.h:105
const LAYER_RANGE & Layers() const
Definition: pns_item.h:195
bool Collide(const ITEM *aHead, const NODE *aNode, COLLISION_SEARCH_CONTEXT *aCtx=nullptr) const
Check for a collision (clearance violation) with between us and item aOther.
Definition: pns_item.cpp:269
A 2D point on a given set of layers and belonging to a certain net, that links together a number of b...
Definition: pns_joint.h:43
const std::vector< ITEM * > & LinkList() const
Definition: pns_joint.h:274
NET_HANDLE Net() const override
Definition: pns_joint.h:269
int LinkCount(int aMask=-1) const
Definition: pns_joint.h:289
Represents a track on a PCB, connecting two non-trivial joints (that is, vias, pads,...
Definition: pns_line.h:61
virtual void Unmark(int aMarker=-1) const override
Definition: pns_line.cpp:119
const SHAPE_LINE_CHAIN & CLine() const
Definition: pns_line.h:136
void DragCorner(const VECTOR2I &aP, int aIndex, bool aFreeAngle=false)
Definition: pns_line.cpp:754
Keep the router "world" - i.e.
Definition: pns_node.h:207
NODE * Branch()
Create a lightweight copy (called branch) of self that tracks the changes (added/removed items) wrs t...
Definition: pns_node.cpp:143
OPT_OBSTACLE CheckColliding(const ITEM *aItem, int aKindMask=ITEM::ANY_T)
Check if the item collides with anything else in the world, and if found, returns the obstacle.
Definition: pns_node.cpp:410
const JOINT * FindJoint(const VECTOR2I &aPos, int aLayer, NET_HANDLE aNet) const
Search for a joint at a given position, layer and belonging to given net.
Definition: pns_node.cpp:1206
int QueryJoints(const BOX2I &aBox, std::vector< JOINT * > &aJoints, LAYER_RANGE aLayerMask=LAYER_RANGE::All(), int aKindMask=ITEM::ANY_T)
Definition: pns_node.cpp:1620
bool Add(std::unique_ptr< SEGMENT > aSegment, bool aAllowRedundant=false)
Add an item to the current node.
Definition: pns_node.cpp:657
void KillChildren()
Definition: pns_node.cpp:1490
void Remove(ARC *aArc)
Remove an item from this branch.
Definition: pns_node.cpp:878
const LINE AssembleLine(LINKED_ITEM *aSeg, int *aOriginSegmentIndex=nullptr, bool aStopAtLockedJoints=false, bool aFollowLockedSegments=false)
Follow the joint map to assemble a line connecting two non-trivial joints starting from segment aSeg.
Definition: pns_node.cpp:1008
void CommitRouting()
Definition: pns_router.cpp:896
const SEG & Seg() const
Definition: pns_segment.h:84
SEGMENT * Clone() const override
Return a deep copy of the item.
Definition: pns_line.cpp:139
const SHAPE_LINE_CHAIN Hull(int aClearance=0, int aWalkaroundThickness=0, int aLayer=-1) const override
Definition: pns_solid.cpp:39
const VECTOR2I & Pos() const
Definition: pns_solid.h:104
Definition: seg.h:42
VECTOR2I A
Definition: seg.h:49
VECTOR2I B
Definition: seg.h:50
void Move(const VECTOR2I &aVector) override
Definition: shape_arc.cpp:577
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
const VECTOR2I & CPoint(int aIndex) const
Return a reference to a given point in the line chain.
const BOX2I BBox(int aClearance=0) const override
Compute a bounding box of the shape, with a margin of aClearance a collision.
@ BLUE
Definition: color4d.h:56
Push and Shove diff pair dimensions (gap) settings dialog.
#define PNS_DBG(dbg, method,...)