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.

263 lines
10 KiB

  1. /**
  2. * @file connect.h
  3. * @brief helper classes to find track to track and track to pad connections.
  4. */
  5. #ifndef CONNECT_H
  6. #define CONNECT_H
  7. /*
  8. * This program source code file is part of KiCad, a free EDA CAD application.
  9. *
  10. * Copyright (C) 2012 Jean-Pierre Charras, jp.charras at wanadoo.fr
  11. * Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
  12. * Copyright (C) 1992-2012 KiCad Developers, see AUTHORS.txt for contributors.
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License
  16. * as published by the Free Software Foundation; either version 2
  17. * of the License, or (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, you may find one here:
  26. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  27. * or you may search the http://www.gnu.org website for the version 2 license,
  28. * or you may write to the Free Software Foundation, Inc.,
  29. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  30. */
  31. #include <class_track.h>
  32. #include <class_board.h>
  33. // Helper classes to handle connection points (i.e. candidates) for tracks
  34. /* class CONNECTED_POINT describes a coordinate having a track or pad parent.
  35. * when a pad is the parent, the coordinate is (obviously) the connection point
  36. * when a track is the parent, the coordinate is the staring point
  37. * or the ending point.
  38. * therefore when building a list of CONNECTED_POINT, a pad or via generates one item,
  39. * and a track generates 2 items.
  40. */
  41. class CONNECTED_POINT
  42. {
  43. private:
  44. BOARD_CONNECTED_ITEM * m_item; // a link to the parent item (track, via or pad)
  45. wxPoint m_point; // coordinates of this connected point
  46. public:
  47. // ctor to build a CONNECTED_POINT instance, when the parent is a track or via
  48. CONNECTED_POINT( TRACK * aTrack, const wxPoint & aPoint)
  49. {
  50. m_item = aTrack;
  51. m_point = aPoint;
  52. }
  53. // ctor to build a CONNECTED_POINT instance, when the parent is a pad
  54. CONNECTED_POINT( D_PAD * aPad, const wxPoint & aPoint)
  55. {
  56. m_item = aPad;
  57. m_point = aPoint;
  58. }
  59. /**
  60. * Function GetTrack
  61. * @return the parent track or via of this connected point,
  62. * or null if the parent is a pad
  63. */
  64. TRACK * GetTrack() const
  65. {
  66. return m_item->Type() != PCB_PAD_T ? (TRACK*) m_item : NULL ;
  67. }
  68. /**
  69. * Function GetPad
  70. * @return the parent pad of this connected point,
  71. * or null if the parent is a track or via
  72. */
  73. D_PAD * GetPad() const
  74. {
  75. return m_item->Type() == PCB_PAD_T ? (D_PAD*) m_item : NULL;
  76. }
  77. const wxPoint & GetPoint() const { return m_point; }
  78. };
  79. // A helper class to handle connections calculations:
  80. class CONNECTIONS
  81. {
  82. private:
  83. std::vector <TRACK*> m_connected; // List of connected tracks/vias
  84. // to a given track or via
  85. std::vector <CONNECTED_POINT> m_candidates; // List of points to test
  86. // (end points of tracks or vias location )
  87. BOARD * m_brd; // the master board.
  88. const TRACK * m_firstTrack; // The first track used to build m_Candidates
  89. const TRACK * m_lastTrack; // The last track used to build m_Candidates
  90. std::vector<D_PAD*> m_sortedPads; // list of sorted pads by X (then Y) coordinate
  91. public:
  92. CONNECTIONS( BOARD * aBrd );
  93. ~CONNECTIONS() {};
  94. /**
  95. * Function BuildPadsList
  96. * Fills m_sortedPads with all pads that be connected to tracks
  97. * pads are sorted by > then Y coordinates to allow fast binary search in list
  98. * @param aNetcode = net code to use to filter pads
  99. * if aNetcode < 0, all pads will be put in list (default)
  100. */
  101. void BuildPadsList( int aNetcode = -1 );
  102. /**
  103. * Function GetPadsList
  104. * @return the pads list used in connections calculations
  105. */
  106. std::vector<D_PAD*>& GetPadsList() { return m_sortedPads; }
  107. /**
  108. * Function Build_CurrNet_SubNets_Connections
  109. * should be called after a track change (delete or add a track):
  110. * Connections to pads and to tracks are recalculated
  111. * If a track is deleted, the other pointers to pads do not change.
  112. * When a new track is added in track list, its pointers to pads are already initialized
  113. * Builds the subnets inside a net (tracks from aFirstTrack to aFirstTrack).
  114. * subnets are clusters of pads and tracks that are connected together.
  115. * When all tracks are created relative to the net, there is only a cluster
  116. * when not tracks there are a cluster per pad
  117. * @param aFirstTrack = first track of the given net
  118. * @param aLastTrack = last track of the given net
  119. * @param aNetcode = the netcode of the given net
  120. */
  121. void Build_CurrNet_SubNets_Connections( TRACK* aFirstTrack, TRACK* aLastTrack, int aNetcode );
  122. /**
  123. * Function BuildTracksCandidatesList
  124. * Fills m_Candidates with all connecting points (track ends or via location)
  125. * with tracks from aBegin to aEnd.
  126. * @param aBegin = first track to store in list (should not be NULL)
  127. * @param aEnd = last track to store in list
  128. * if aEnd == NULL, uses all tracks from aBegin
  129. */
  130. void BuildTracksCandidatesList( TRACK * aBegin, TRACK * aEnd = NULL);
  131. /**
  132. * Function BuildPadsCandidatesList
  133. * Populates m_candidates with all pads connecting points (pads position)
  134. * m_sortedPads is expected to be populated by the pad candidates list
  135. */
  136. void BuildPadsCandidatesList();
  137. /**
  138. * function SearchConnectedTracks
  139. * Populates .m_connected with tracks/vias connected to aTrack
  140. * m_candidates is expected to be populated by the track candidates ends list
  141. * @param aTrack = track or via to use as reference
  142. */
  143. int SearchConnectedTracks( const TRACK * aTrack );
  144. /**
  145. * Function GetConnectedTracks
  146. * Copy m_Connected that contains the list of tracks connected
  147. * calculated by SearchConnectedTracks
  148. * in aTrack->m_TracksConnected
  149. * @param aTrack = track or via to fill with connected tracks
  150. */
  151. void GetConnectedTracks(TRACK * aTrack)
  152. {
  153. aTrack->m_TracksConnected = m_connected;
  154. }
  155. /**
  156. * function SearchConnectionsPadsToIntersectingPads
  157. * Explores the list of pads and adds to m_PadsConnected member
  158. * of each pad pads connected to
  159. * Here, connections are due to intersecting pads, not tracks
  160. * m_sortedPads must be initialized
  161. */
  162. void SearchConnectionsPadsToIntersectingPads();
  163. /**
  164. * function SearchTracksConnectedToPads
  165. * Explores the list of pads.
  166. * if( add_to_padlist )
  167. * adds to m_PadsConnected member of each track the pad(s) connected to
  168. * if add_to_tracklist
  169. * adds to m_TracksConnected member of each pad the track(s) connected to
  170. * D_PAD::m_TracksConnected is cleared before adding items
  171. * TRACK::m_PadsConnected is not cleared
  172. * @param add_to_padlist = true to fill m_PadsConnected member of each track
  173. * @param add_to_tracklist = true to fill m_TracksConnected member of each pad
  174. */
  175. void SearchTracksConnectedToPads( bool add_to_padlist = true, bool add_to_tracklist = true);
  176. /**
  177. * function CollectItemsNearTo
  178. * Used by SearchTracksConnectedToPads
  179. * Fills aList with pads near to aPosition
  180. * near means aPosition to pad position <= aDistMax
  181. * @param aList = list to fill
  182. * @param aPosition = aPosition to use as reference
  183. * @param aDistMax = dist max from aPosition to a candidate to select it
  184. */
  185. void CollectItemsNearTo( std::vector<CONNECTED_POINT*>& aList,
  186. const wxPoint& aPosition, int aDistMax );
  187. /**
  188. * Function Propagate_SubNets
  189. * Test a list of tracks, to create or propagate a sub netcode to pads and
  190. * segments connected together.
  191. * The track list must be sorted by nets, and all segments
  192. * from m_firstTrack to m_lastTrack have the same net.
  193. * When 2 items are connected (a track to a pad, or a track to an other track),
  194. * they are grouped in a cluster.
  195. * For pads, this is the .m_physical_connexion member which is a cluster identifier
  196. * For tracks, this is the .m_Subnet member which is a cluster identifier
  197. * For a given net, if all tracks are created, there is only one cluster.
  198. * but if not all tracks are created, there are more than one cluster,
  199. * and some ratsnests will be left active.
  200. */
  201. void Propagate_SubNets();
  202. private:
  203. /**
  204. * function searchEntryPointInCandidatesList
  205. * Search an item in m_Connected connected to aPoint
  206. * note m_Connected containts usually more than one candidate
  207. * and searchEntryPointInCandidatesList returns an index to one of these candidates
  208. * Others are neightbor of the indexed item.
  209. * @param aPoint is the reference coordinates
  210. * @return the index of item found or -1 if no candidate
  211. */
  212. int searchEntryPointInCandidatesList( const wxPoint & aPoint);
  213. /**
  214. * Function Merge_SubNets
  215. * Change a subnet old value to a new value, for tracks and pads which are connected to
  216. * tracks from m_firstTrack to m_lastTrack and their connected pads.
  217. * and modify the subnet parameter (change the old value to the new value).
  218. * After that, 2 cluster (or subnets) are merged into only one.
  219. * Note: the resulting sub net value is the smallest between aOldSubNet and aNewSubNet
  220. * @return modification count
  221. * @param aOldSubNet = subnet value to modify
  222. * @param aNewSubNet = new subnet value for each item which have old_val as subnet value
  223. */
  224. int Merge_SubNets( int aOldSubNet, int aNewSubNet );
  225. /**
  226. * Function Merge_PadsSubNets
  227. * Change a subnet value to a new value, in m_sortedPads pad list
  228. * After that, 2 cluster (or subnets) are merged into only one.
  229. * Note: the resulting subnet value is the smallest between aOldSubNet et aNewSubNet
  230. * @return modification count
  231. * @param aOldSubNet = subnet value to modify
  232. * @param aNewSubNet = new subnet value for each item which have old_val as subnet value
  233. */
  234. int Merge_PadsSubNets( int aOldSubNet, int aNewSubNet );
  235. };
  236. #endif // ifndef CONNECT_H