You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

191 lines
4.9 KiB

  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. #include <memory>
  21. #include "pns_line.h"
  22. #include "pns_solid.h"
  23. #include "pns_router.h"
  24. #include "pns_component_dragger.h"
  25. #include "pns_debug_decorator.h"
  26. namespace PNS
  27. {
  28. COMPONENT_DRAGGER::COMPONENT_DRAGGER( ROUTER* aRouter ) : DRAG_ALGO( aRouter )
  29. {
  30. // ensure all variables are initialized
  31. m_dragStatus = false;
  32. m_currentNode = nullptr;
  33. }
  34. COMPONENT_DRAGGER::~COMPONENT_DRAGGER()
  35. {
  36. }
  37. bool COMPONENT_DRAGGER::Start( const VECTOR2I& aP, ITEM_SET& aPrimitives )
  38. {
  39. m_currentNode = nullptr;
  40. m_initialDraggedItems = aPrimitives;
  41. m_p0 = aP;
  42. std::unordered_set<LINKED_ITEM*> seenItems;
  43. auto addLinked =
  44. [&]( SOLID* aSolid, LINKED_ITEM* aItem, VECTOR2I aOffset = {} )
  45. {
  46. if( seenItems.count( aItem ) )
  47. return;
  48. seenItems.insert( aItem );
  49. int segIndex;
  50. DRAGGED_CONNECTION cn;
  51. cn.origLine = m_world->AssembleLine( aItem, &segIndex );
  52. cn.attachedPad = aSolid;
  53. cn.offset = aOffset;
  54. m_conns.push_back( cn );
  55. };
  56. for( auto item : aPrimitives.Items() )
  57. {
  58. if( item.item->Kind() != ITEM::SOLID_T )
  59. continue;
  60. if( ! item.item->IsRoutable() )
  61. continue;
  62. auto solid = static_cast<SOLID*>( item.item );
  63. auto jt = m_world->FindJoint( solid->Pos(), solid );
  64. m_solids.insert( solid );
  65. for( auto link : jt->LinkList() )
  66. {
  67. if( link.item->OfKind( ITEM::SEGMENT_T | ITEM::ARC_T ) )
  68. addLinked( solid, static_cast<LINKED_ITEM*>( link.item ) );
  69. }
  70. std::vector<JOINT*> extraJoints;
  71. m_world->QueryJoints( solid->Hull().BBox(), extraJoints, solid->Layers(),
  72. ITEM::SEGMENT_T | ITEM::ARC_T );
  73. for( JOINT* extraJoint : extraJoints )
  74. {
  75. if( extraJoint->Net() == jt->Net() && extraJoint->LinkCount() == 1 )
  76. {
  77. LINKED_ITEM* li = static_cast<LINKED_ITEM*>( extraJoint->LinkList()[0].item );
  78. if( li->Collide( solid, 0, m_world ) )
  79. addLinked( solid, li, extraJoint->Pos() - solid->Pos() );
  80. }
  81. }
  82. }
  83. return true;
  84. }
  85. bool COMPONENT_DRAGGER::Drag( const VECTOR2I& aP )
  86. {
  87. m_world->KillChildren();
  88. m_currentNode = m_world->Branch();
  89. for( auto item : m_initialDraggedItems.Items() )
  90. m_currentNode->Remove( item );
  91. m_draggedItems.Clear();
  92. for( auto item : m_solids )
  93. {
  94. SOLID* s = static_cast<SOLID*>( item );
  95. auto p_next = aP - m_p0 + s->Pos();
  96. std::unique_ptr<SOLID> snew( static_cast<SOLID*>( s->Clone() ) );
  97. snew->SetPos( p_next );
  98. m_draggedItems.Add( snew.get() );
  99. m_currentNode->Add( std::move( snew ) );
  100. for( auto& l : m_conns )
  101. {
  102. if( l.attachedPad == s )
  103. {
  104. l.p_orig = s->Pos() + l.offset;
  105. l.p_next = p_next + l.offset;
  106. }
  107. }
  108. }
  109. for( auto& cn : m_conns )
  110. {
  111. auto l_new( cn.origLine );
  112. l_new.Unmark();
  113. l_new.ClearLinks();
  114. l_new.DragCorner( cn.p_next, cn.origLine.CLine().Find( cn.p_orig ) );
  115. Dbg()->AddLine( l_new.CLine(), 4, 100000 );
  116. m_draggedItems.Add( l_new );
  117. auto l_orig( cn.origLine );
  118. m_currentNode->Remove( l_orig );
  119. m_currentNode->Add( l_new );
  120. }
  121. return true;
  122. }
  123. bool COMPONENT_DRAGGER::FixRoute()
  124. {
  125. NODE* node = CurrentNode();
  126. if( node )
  127. {
  128. bool ok;
  129. if( Settings().CanViolateDRC() )
  130. ok = true;
  131. else
  132. ok = !node->CheckColliding( m_draggedItems );
  133. if( !ok )
  134. return false;
  135. Router()->CommitRouting( node );
  136. return true;
  137. }
  138. return false;
  139. }
  140. NODE* COMPONENT_DRAGGER::CurrentNode() const
  141. {
  142. return m_currentNode ? m_currentNode : m_world;
  143. }
  144. const ITEM_SET COMPONENT_DRAGGER::Traces()
  145. {
  146. return m_draggedItems;
  147. }
  148. }; // namespace PNS