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.

274 lines
7.9 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. /*
  2. * KiRouter - a push-and-(sometimes-)shove PCB router
  3. *
  4. * Copyright (C) 2013-2014 CERN
  5. * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
  6. * Author: Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
  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. #ifndef __PNS_ROUTER_H
  22. #define __PNS_ROUTER_H
  23. #include <list>
  24. #include <memory>
  25. #include <optional>
  26. #include <math/box2.h>
  27. #include "pns_routing_settings.h"
  28. #include "pns_sizes_settings.h"
  29. #include "pns_node.h"
  30. namespace KIGFX
  31. {
  32. class VIEW;
  33. class VIEW_GROUP;
  34. }
  35. namespace PNS {
  36. class DEBUG_DECORATOR;
  37. class NODE;
  38. class DIFF_PAIR_PLACER;
  39. class PLACEMENT_ALGO;
  40. class LINE_PLACER;
  41. class ITEM;
  42. class ARC;
  43. class LINE;
  44. class SOLID;
  45. class SEGMENT;
  46. class JOINT;
  47. class VIA;
  48. class RULE_RESOLVER;
  49. class SHOVE;
  50. class DRAGGER;
  51. class DRAG_ALGO;
  52. class LOGGER;
  53. enum ROUTER_MODE {
  54. PNS_MODE_ROUTE_SINGLE = 1,
  55. PNS_MODE_ROUTE_DIFF_PAIR,
  56. PNS_MODE_TUNE_SINGLE,
  57. PNS_MODE_TUNE_DIFF_PAIR,
  58. PNS_MODE_TUNE_DIFF_PAIR_SKEW
  59. };
  60. enum DRAG_MODE
  61. {
  62. DM_CORNER = 0x1,
  63. DM_SEGMENT = 0x2,
  64. DM_VIA = 0x4,
  65. DM_FREE_ANGLE = 0x8,
  66. DM_ARC = 0x10,
  67. DM_ANY = 0x17,
  68. DM_COMPONENT = 0x20
  69. };
  70. /**
  71. * ROUTER
  72. *
  73. * Main router class.
  74. */
  75. class ROUTER_IFACE
  76. {
  77. public:
  78. ROUTER_IFACE() {};
  79. virtual ~ROUTER_IFACE() {};
  80. virtual void SyncWorld( NODE* aNode ) = 0;
  81. virtual void AddItem( ITEM* aItem ) = 0;
  82. virtual void UpdateItem( ITEM* aItem ) = 0;
  83. virtual void RemoveItem( ITEM* aItem ) = 0;
  84. virtual bool IsAnyLayerVisible( const PNS_LAYER_RANGE& aLayer ) const = 0;
  85. virtual bool IsItemVisible( const PNS::ITEM* aItem ) const = 0;
  86. virtual bool IsFlashedOnLayer( const PNS::ITEM* aItem, int aLayer ) const = 0;
  87. virtual bool IsFlashedOnLayer( const PNS::ITEM* aItem, const PNS_LAYER_RANGE& aLayer ) const = 0;
  88. virtual bool IsPNSCopperLayer( int aPNSLayer ) const = 0;
  89. virtual void DisplayItem( const ITEM* aItem, int aClearance, bool aEdit = false,
  90. int aFlags = 0 ) = 0;
  91. virtual void DisplayPathLine( const SHAPE_LINE_CHAIN& aLine, int aImportance ) = 0;
  92. virtual void DisplayRatline( const SHAPE_LINE_CHAIN& aRatline, NET_HANDLE aNetCode ) = 0;
  93. virtual void HideItem( ITEM* aItem ) = 0;
  94. virtual void Commit() = 0;
  95. virtual bool ImportSizes( SIZES_SETTINGS& aSizes, ITEM* aStartItem, NET_HANDLE aNet,
  96. VECTOR2D aStartPosition ) = 0;
  97. virtual int StackupHeight( int aFirstLayer, int aSecondLayer ) const = 0;
  98. virtual void EraseView() = 0;
  99. virtual int GetNetCode( NET_HANDLE aNet ) const = 0;
  100. virtual wxString GetNetName( PNS::NET_HANDLE aNet ) const = 0;
  101. virtual void UpdateNet( NET_HANDLE aNet ) = 0;
  102. virtual NET_HANDLE GetOrphanedNetHandle() = 0;
  103. virtual PNS::NODE* GetWorld() const = 0;
  104. virtual RULE_RESOLVER* GetRuleResolver() = 0;
  105. virtual DEBUG_DECORATOR* GetDebugDecorator() = 0;
  106. virtual long long int CalculateRoutedPathLength( const ITEM_SET& aLine, const SOLID* aStartPad,
  107. const SOLID* aEndPad ) = 0;
  108. virtual PCB_LAYER_ID GetBoardLayerFromPNSLayer( int aLayer ) const = 0;
  109. virtual int GetPNSLayerFromBoardLayer( PCB_LAYER_ID aLayer ) const = 0;
  110. };
  111. class ROUTER
  112. {
  113. public:
  114. enum RouterState
  115. {
  116. IDLE,
  117. DRAG_SEGMENT,
  118. DRAG_COMPONENT,
  119. ROUTE_TRACK
  120. };
  121. public:
  122. ROUTER();
  123. ~ROUTER();
  124. void SetInterface( ROUTER_IFACE* aIface );
  125. void SetMode ( ROUTER_MODE aMode );
  126. ROUTER_MODE Mode() const { return m_mode; }
  127. RouterState GetState() const { return m_state; }
  128. DRAG_ALGO* GetDragger() { return m_dragger.get(); }
  129. static ROUTER* GetInstance();
  130. void ClearWorld();
  131. void SyncWorld();
  132. bool RoutingInProgress() const;
  133. bool StartRouting( const VECTOR2I& aP, ITEM* aItem, int aLayer );
  134. bool Move( const VECTOR2I& aP, ITEM* aItem );
  135. bool Finish();
  136. bool ContinueFromEnd( ITEM** aNewStartItem );
  137. bool FixRoute( const VECTOR2I& aP, ITEM* aItem, bool aForceFinish, bool aForceCommit );
  138. void BreakSegmentOrArc( ITEM *aItem, const VECTOR2I& aP );
  139. std::optional<VECTOR2I> UndoLastSegment();
  140. void CommitRouting();
  141. void GetUpdatedItems( std::vector<PNS::ITEM*>& aRemoved, std::vector<PNS::ITEM*>& aAdded,
  142. std::vector<PNS::ITEM*>& aHeads );
  143. void StopRouting();
  144. void ClearViewDecorations();
  145. NODE* GetWorld() const { return m_world.get(); }
  146. void FlipPosture();
  147. bool SwitchLayer( int layer );
  148. void ToggleViaPlacement();
  149. void SetOrthoMode( bool aEnable );
  150. void ToggleCornerMode();
  151. int GetCurrentLayer() const;
  152. const std::vector<NET_HANDLE> GetCurrentNets() const;
  153. LOGGER* Logger();
  154. RULE_RESOLVER* GetRuleResolver() const { return m_iface->GetRuleResolver(); }
  155. bool IsPlacingVia() const;
  156. const ITEM_SET QueryHoverItems( const VECTOR2I& aP, int aSlopRadius = 0 );
  157. bool StartDragging( const VECTOR2I& aP, ITEM* aItem, int aDragMode = DM_ANY );
  158. bool StartDragging( const VECTOR2I& aP, ITEM_SET aItems, int aDragMode = DM_COMPONENT );
  159. void SetIterLimit( int aX ) { m_iterLimit = aX; }
  160. int GetIterLimit() const { return m_iterLimit; };
  161. ROUTING_SETTINGS& Settings() { return *m_settings; }
  162. void CommitRouting( NODE* aNode );
  163. /**
  164. * Applies stored settings.
  165. * @see Settings()
  166. */
  167. void UpdateSizes( const SIZES_SETTINGS& aSizes );
  168. /**
  169. * Changes routing settings to ones passed in the parameter.
  170. * @param aSettings are the new settings.
  171. */
  172. void LoadSettings( ROUTING_SETTINGS* aSettings )
  173. {
  174. m_settings = aSettings;
  175. }
  176. SIZES_SETTINGS& Sizes() { return m_sizes; }
  177. void SetFailureReason( const wxString& aReason ) { m_failureReason = aReason; }
  178. const wxString& FailureReason() const { return m_failureReason; }
  179. PLACEMENT_ALGO* Placer() { return m_placer.get(); }
  180. ROUTER_IFACE* GetInterface() const { return m_iface; }
  181. void SetVisibleViewArea( const BOX2I& aExtents ) { m_visibleViewArea = aExtents; }
  182. const BOX2I& VisibleViewArea() const { return m_visibleViewArea; }
  183. std::vector<PNS::ITEM*> GetLastCommittedLeaderSegments();
  184. private:
  185. bool movePlacing( const VECTOR2I& aP, ITEM* aItem );
  186. bool moveDragging( const VECTOR2I& aP, ITEM* aItem );
  187. void updateView( NODE* aNode, ITEM_SET& aCurrent, bool aDragging = false );
  188. // optHoverItem queryHoverItemEx(const VECTOR2I& aP);
  189. void markViolations( NODE* aNode, ITEM_SET& aCurrent, NODE::ITEM_VECTOR& aRemoved );
  190. bool isStartingPointRoutable( const VECTOR2I& aWhere, ITEM* aItem, int aLayer );
  191. bool getNearestRatnestAnchor( VECTOR2I& aOtherEnd, PNS_LAYER_RANGE& aOtherEndLayers,
  192. ITEM*& aOtherEndItem );
  193. private:
  194. BOX2I m_visibleViewArea;
  195. RouterState m_state;
  196. std::unique_ptr<NODE> m_world;
  197. NODE* m_lastNode;
  198. std::unique_ptr<PLACEMENT_ALGO> m_placer;
  199. std::unique_ptr<DRAG_ALGO> m_dragger;
  200. std::unique_ptr<SHOVE> m_shove;
  201. std::vector<PNS::ITEM*> m_leaderSegments;
  202. ROUTER_IFACE* m_iface;
  203. int m_iterLimit;
  204. ROUTING_SETTINGS* m_settings;
  205. SIZES_SETTINGS m_sizes;
  206. ROUTER_MODE m_mode;
  207. LOGGER* m_logger;
  208. wxString m_toolStatusbarName;
  209. wxString m_failureReason;
  210. };
  211. }
  212. #endif