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  * Author: Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
6  *
7  * This program is free software: you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the
9  * Free Software Foundation, either version 3 of the License, or (at your
10  * option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #include <memory>
22 
23 #include "pns_line.h"
24 #include "pns_solid.h"
25 #include "pns_router.h"
26 
27 #include "pns_component_dragger.h"
28 #include "pns_debug_decorator.h"
29 
30 namespace PNS
31 {
32 
34 {
35  // ensure all variables are initialized
36  m_dragStatus = false;
37  m_currentNode = nullptr;
38 }
39 
40 
42 {
43 }
44 
45 
46 bool COMPONENT_DRAGGER::Start( const VECTOR2I& aP, ITEM_SET& aPrimitives )
47 {
48  m_currentNode = nullptr;
49  m_initialDraggedItems = aPrimitives;
50  m_p0 = aP;
51 
52  std::unordered_set<LINKED_ITEM*> seenItems;
53 
54  auto addLinked =
55  [&]( SOLID* aSolid, LINKED_ITEM* aItem, VECTOR2I aOffset = {} )
56  {
57  if( seenItems.count( aItem ) )
58  return;
59 
60  seenItems.insert( aItem );
61 
62  int segIndex;
64 
65  cn.origLine = m_world->AssembleLine( aItem, &segIndex );
66  cn.attachedPad = aSolid;
67  cn.offset = aOffset;
68 
69  m_conns.push_back( cn );
70  };
71 
72  for( auto item : aPrimitives.Items() )
73  {
74  if( item.item->Kind() != ITEM::SOLID_T )
75  continue;
76 
77  if( ! item.item->IsRoutable() )
78  continue;
79 
80  auto solid = static_cast<SOLID*>( item.item );
81  auto jt = m_world->FindJoint( solid->Pos(), solid );
82 
83  m_solids.insert( solid );
84 
85  for( auto link : jt->LinkList() )
86  {
87  if( link.item->OfKind( ITEM::SEGMENT_T | ITEM::ARC_T ) )
88  addLinked( solid, static_cast<LINKED_ITEM*>( link.item ) );
89  }
90 
91  std::vector<JOINT*> extraJoints;
92 
93  m_world->QueryJoints( solid->Hull().BBox(), extraJoints, solid->Layers(),
95 
96  for( JOINT* extraJoint : extraJoints )
97  {
98  if( extraJoint->Net() == jt->Net() && extraJoint->LinkCount() == 1 )
99  {
100  LINKED_ITEM* li = static_cast<LINKED_ITEM*>( extraJoint->LinkList()[0].item );
101 
102  if( li->Collide( solid, nullptr, m_world ) )
103  addLinked( solid, li, extraJoint->Pos() - solid->Pos() );
104  }
105  }
106  }
107 
108  return true;
109 }
110 
112 {
115 
116  for( auto item : m_initialDraggedItems.Items() )
117  m_currentNode->Remove( item );
118 
120 
121  for( auto item : m_solids )
122  {
123  SOLID* s = static_cast<SOLID*>( item );
124  auto p_next = aP - m_p0 + s->Pos();
125  std::unique_ptr<SOLID> snew( static_cast<SOLID*>( s->Clone() ) );
126  snew->SetPos( p_next );
127 
128  m_draggedItems.Add( snew.get() );
129  m_currentNode->Add( std::move( snew ) );
130 
131  for( auto& l : m_conns )
132  {
133  if( l.attachedPad == s )
134  {
135  l.p_orig = s->Pos() + l.offset;
136  l.p_next = p_next + l.offset;
137  }
138  }
139  }
140 
141  for( auto& cn : m_conns )
142  {
143  auto l_new( cn.origLine );
144  l_new.Unmark();
145  l_new.ClearLinks();
146  l_new.DragCorner( cn.p_next, cn.origLine.CLine().Find( cn.p_orig ) );
147 
148  PNS_DBG( Dbg(), AddLine, l_new.CLine(), BLUE, 100000, "cdrag-new-fanout" );
149  m_draggedItems.Add( l_new );
150 
151  auto l_orig( cn.origLine );
152  m_currentNode->Remove( l_orig );
153  m_currentNode->Add( l_new );
154  }
155 
156  return true;
157 }
158 
160 {
161  NODE* node = CurrentNode();
162 
163  if( node )
164  {
165  bool ok;
166 
167  if( Settings().AllowDRCViolations() )
168  ok = true;
169  else
170  ok = !node->CheckColliding( m_draggedItems );
171 
172  if( !ok )
173  return false;
174 
175  Router()->CommitRouting( node );
176  return true;
177  }
178 
179  return false;
180 }
181 
183 {
185 }
186 
188 {
189  return m_draggedItems;
190 }
191 
192 }; // namespace PNS
COMPONENT_DRAGGER(ROUTER *aRouter)
ROUTER * Router() const
Return current router settings.
Definition: pns_algo_base.h:54
Keep the router "world" - i.e.
Definition: pns_node.h:144
NODE * CurrentNode() const override
Function CurrentNode()
void CommitRouting()
Definition: pns_router.cpp:694
DRAG_ALGO.
Definition: pns_drag_algo.h:42
ENTRIES & Items()
Definition: pns_itemset.h:138
bool Start(const VECTOR2I &aP, ITEM_SET &aPrimitives) override
Function Start()
void Add(const LINE &aLine)
Definition: pns_itemset.cpp:32
NODE * Branch()
Create a lightweight copy (called branch) of self that tracks the changes (added/removed items) wrs t...
Definition: pns_node.cpp:127
void Remove(ARC *aArc)
Remove an item from this branch.
Definition: pns_node.cpp:810
std::set< SOLID * > m_solids
ROUTING_SETTINGS & Settings() const
Return the logger object, allowing to dump geometry to a file.
Represents a 2D point on a given set of layers and belonging to a certain net, that links together a ...
Definition: pns_joint.h:42
bool Drag(const VECTOR2I &aP) override
Function Drag()
void KillChildren()
Definition: pns_node.cpp:1369
const LINE AssembleLine(LINKED_ITEM *aSeg, int *aOriginSegmentIndex=NULL, bool aStopAtLockedJoints=false)
Follow the joint map to assemble a line connecting two non-trivial joints starting from segment aSeg.
Definition: pns_node.cpp:920
ITEM * Clone() const override
Return a deep copy of the item.
Definition: pns_solid.cpp:154
std::vector< DRAGGED_CONNECTION > m_conns
#define PNS_DBG(dbg, method,...)
DEBUG_DECORATOR * Dbg() const
Definition: pns_algo_base.h:78
bool Collide(const ITEM *aOther, const NODE *aNode, bool aDifferentNetsOnly=true) const
Check for a collision (clearance violation) with between us and item aOther.
Definition: pns_item.cpp:97
int QueryJoints(const BOX2I &aBox, std::vector< JOINT * > &aJoints, LAYER_RANGE aLayerMask=LAYER_RANGE::All(), int aKindMask=ITEM::ANY_T)
Definition: pns_node.cpp:1499
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:1104
const VECTOR2I & Pos() const
Definition: pns_solid.h:101
Definition: color4d.h:56
const ITEM_SET Traces() override
Function Traces()
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:433
Push and Shove diff pair dimensions (gap) settings dialog.
bool Add(std::unique_ptr< SEGMENT > aSegment, bool aAllowRedundant=false)
Add an item to the current node.
Definition: pns_node.cpp:621
bool FixRoute() override
Function FixRoute()