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_via.h"
26 #include "pns_router.h"
27 
28 #include "pns_component_dragger.h"
29 #include "pns_debug_decorator.h"
30 
31 namespace PNS
32 {
33 
35 {
36  // ensure all variables are initialized
37  m_dragStatus = false;
38  m_currentNode = nullptr;
39 }
40 
41 
43 {
44 }
45 
46 
47 bool COMPONENT_DRAGGER::Start( const VECTOR2I& aP, ITEM_SET& aPrimitives )
48 {
49  m_currentNode = nullptr;
50  m_initialDraggedItems = aPrimitives;
51  m_p0 = aP;
52 
53  std::unordered_set<LINKED_ITEM*> seenItems;
54 
55  auto addLinked =
56  [&]( SOLID* aSolid, LINKED_ITEM* aItem, VECTOR2I aOffset = {} )
57  {
58  if( seenItems.count( aItem ) )
59  return;
60 
61  seenItems.insert( aItem );
62 
63  int segIndex;
65 
66  cn.origLine = m_world->AssembleLine( aItem, &segIndex );
67  cn.attachedPad = aSolid;
68  cn.offset = aOffset;
69 
70  m_conns.push_back( cn );
71  };
72 
73  for( auto item : aPrimitives.Items() )
74  {
75  if( item.item->Kind() != ITEM::SOLID_T )
76  continue;
77 
78  if( ! item.item->IsRoutable() )
79  continue;
80 
81  auto solid = static_cast<SOLID*>( item.item );
82  auto jt = m_world->FindJoint( solid->Pos(), solid );
83 
84  m_solids.insert( solid );
85 
86  for( auto link : jt->LinkList() )
87  {
88  if( link.item->OfKind( ITEM::SEGMENT_T | ITEM::ARC_T ) )
89  addLinked( solid, static_cast<LINKED_ITEM*>( link.item ) );
90  }
91 
92  std::vector<JOINT*> extraJoints;
93 
94  m_world->QueryJoints( solid->Hull().BBox(), extraJoints, solid->Layers(),
96 
97  for( JOINT* extraJoint : extraJoints )
98  {
99  if( extraJoint->Net() == jt->Net() && extraJoint->LinkCount() == 1 )
100  {
101  LINKED_ITEM* li = static_cast<LINKED_ITEM*>( extraJoint->LinkList()[0].item );
102 
103  if( li->Collide( solid, 0, m_world ) )
104  addLinked( solid, li, extraJoint->Pos() - solid->Pos() );
105  }
106  }
107  }
108 
109  return true;
110 }
111 
113 {
116 
117  for( auto item : m_initialDraggedItems.Items() )
118  m_currentNode->Remove( item );
119 
121 
122  for( auto item : m_solids )
123  {
124  SOLID* s = static_cast<SOLID*>( item );
125  auto p_next = aP - m_p0 + s->Pos();
126  std::unique_ptr<SOLID> snew( static_cast<SOLID*>( s->Clone() ) );
127  snew->SetPos( p_next );
128 
129  m_draggedItems.Add( snew.get() );
130  m_currentNode->Add( std::move( snew ) );
131 
132  for( auto& l : m_conns )
133  {
134  if( l.attachedPad == s )
135  {
136  l.p_orig = s->Pos() + l.offset;
137  l.p_next = p_next + l.offset;
138  }
139  }
140  }
141 
142  for( auto& cn : m_conns )
143  {
144  auto l_new( cn.origLine );
145  l_new.Unmark();
146  l_new.ClearLinks();
147  l_new.DragCorner( cn.p_next, cn.origLine.CLine().Find( cn.p_orig ) );
148 
149  Dbg()->AddLine( l_new.CLine(), 4, 100000 );
150  m_draggedItems.Add( l_new );
151 
152  auto l_orig( cn.origLine );
153  m_currentNode->Remove( l_orig );
154  m_currentNode->Add( l_new );
155  }
156 
157  return true;
158 }
159 
161 {
162  NODE* node = CurrentNode();
163 
164  if( node )
165  {
166  bool ok;
167  if( Settings().CanViolateDRC() )
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:149
virtual void AddLine(const SHAPE_LINE_CHAIN &aLine, int aType=0, int aWidth=0, const std::string aName="")
NODE * CurrentNode() const override
Function CurrentNode()
void CommitRouting()
Definition: pns_router.cpp:655
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:124
void Remove(ARC *aArc)
Remove an item from this branch.
Definition: pns_node.cpp:804
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:1292
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:912
ITEM * Clone() const override
Return a deep copy of the item.
Definition: pns_solid.cpp:145
std::vector< DRAGGED_CONNECTION > m_conns
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:1422
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:1027
const VECTOR2I & Pos() const
Definition: pns_solid.h:101
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:427
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:615
bool FixRoute() override
Function FixRoute()