KiCad PCB EDA Suite
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 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, 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 ) ) ?
68 aItem->Anchor( 1 ) : aItem->Anchor( 0 );
69 JOINT* otherJoint = m_world->FindJoint( otherEnd, aItem->Layer(), aItem->Net() );
70
71 if( otherJoint && otherJoint->LinkCount( ITEM::SOLID_T ) )
72 {
73 for( const ITEM_SET::ENTRY& otherItem : otherJoint->LinkList() )
74 {
75 if( aPrimitives.Contains( otherItem.item ) )
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 JOINT* jA = m_world->FindJoint( line.CPoint( 0 ), aItem->Layer(), aItem->Net() );
93 JOINT* jB = m_world->FindJoint( line.CPoint( -1 ), aItem->Layer(), aItem->Net() );
94
95 wxASSERT( jA == aJoint || jB == aJoint );
96 JOINT* jSearch = ( jA == aJoint ) ? jB : jA;
97
98 if( jSearch && jSearch->LinkCount( ITEM::SOLID_T ) )
99 {
100 for( const ITEM_SET::ENTRY& otherItem : jSearch->LinkList() )
101 {
102 if( aPrimitives.Contains( otherItem.item ) )
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( auto item : aPrimitives.Items() )
116 {
117 if( item.item->Kind() != ITEM::SOLID_T )
118 continue;
119
120 SOLID* solid = static_cast<SOLID*>( item.item );
121
122 m_solids.insert( solid );
123
124 if( !item.item->IsRoutable() )
125 continue;
126
127 JOINT* jt = m_world->FindJoint( solid->Pos(), solid );
128
129 for( auto link : jt->LinkList() )
130 {
131 if( link.item->OfKind( ITEM::SEGMENT_T | ITEM::ARC_T ) )
132 addLinked( solid, jt, static_cast<LINKED_ITEM*>( link.item ) );
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()[0].item );
145
146 if( li->Collide( solid, nullptr, 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( const ITEM_SET::ENTRY& item : m_initialDraggedItems.Items() )
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
248{
249 NODE* node = CurrentNode();
250
251 if( node )
252 {
253 if( Settings().AllowDRCViolations() || !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
std::vector< DRAGGED_CONNECTION > m_conns
bool Start(const VECTOR2I &aP, ITEM_SET &aPrimitives) override
Function Start()
bool FixRoute() override
Function FixRoute()
std::set< SOLID * > m_solids
bool Drag(const VECTOR2I &aP) override
Function Drag()
DRAG_ALGO.
Definition: pns_drag_algo.h:43
void Add(const LINE &aLine)
Definition: pns_itemset.cpp:32
bool Contains(ITEM *aItem) const
Definition: pns_itemset.h:193
ENTRIES & Items()
Definition: pns_itemset.h:135
Base class for PNS router board items.
Definition: pns_item.h:56
bool Collide(const ITEM *aOther, const NODE *aNode, bool aDifferentNetsOnly=true, int aOverrideClearance=-1) const
Check for a collision (clearance violation) with between us and item aOther.
Definition: pns_item.cpp:157
@ SOLID_T
Definition: pns_item.h:63
@ SEGMENT_T
Definition: pns_item.h:66
const LAYER_RANGE & Layers() const
Definition: pns_item.h:154
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
int LinkCount(int aMask=-1) const
Definition: pns_joint.h:256
const LINKED_ITEMS & LinkList() const
Definition: pns_joint.h:241
int Net() const
Definition: pns_joint.h:236
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:99
const SHAPE_LINE_CHAIN & CLine() const
Definition: pns_line.h:137
void DragCorner(const VECTOR2I &aP, int aIndex, bool aFreeAngle=false)
Definition: pns_line.cpp:719
Keep the router "world" - i.e.
Definition: pns_node.h:150
NODE * Branch()
Create a lightweight copy (called branch) of self that tracks the changes (added/removed items) wrs t...
Definition: pns_node.cpp:139
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:468
int QueryJoints(const BOX2I &aBox, std::vector< JOINT * > &aJoints, LAYER_RANGE aLayerMask=LAYER_RANGE::All(), int aKindMask=ITEM::ANY_T)
Definition: pns_node.cpp:1581
bool Add(std::unique_ptr< SEGMENT > aSegment, bool aAllowRedundant=false)
Add an item to the current node.
Definition: pns_node.cpp:656
void KillChildren()
Definition: pns_node.cpp:1451
JOINT * FindJoint(const VECTOR2I &aPos, int aLayer, int aNet)
Search for a joint at a given position, layer and belonging to given net.
Definition: pns_node.cpp:1181
void Remove(ARC *aArc)
Remove an item from this branch.
Definition: pns_node.cpp:873
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:983
void CommitRouting()
Definition: pns_router.cpp:728
const SEG & Seg() const
Definition: pns_segment.h:84
SEGMENT * Clone() const override
Return a deep copy of the item.
Definition: pns_line.cpp:121
const SHAPE_LINE_CHAIN Hull(int aClearance=0, int aWalkaroundThickness=0, int aLayer=-1) const override
Definition: pns_solid.cpp:97
const VECTOR2I & Pos() const
Definition: pns_solid.h:100
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:523
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,...)