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.

115 lines
4.5 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2022 KiCad Developers, see AUTHORS.txt for contributors.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, you may find one here:
  18. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  19. * or you may search the http://www.gnu.org website for the version 2 license,
  20. * or you may write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  22. */
  23. #ifndef KICAD_TRACKS_CLEANER_H
  24. #define KICAD_TRACKS_CLEANER_H
  25. #include <pcb_track.h>
  26. #include <board.h>
  27. class BOARD_COMMIT;
  28. class CLEANUP_ITEM;
  29. class REPORTER;
  30. // Helper class used to clean tracks and vias
  31. class TRACKS_CLEANER
  32. {
  33. public:
  34. TRACKS_CLEANER( BOARD* aPcb, BOARD_COMMIT& aCommit );
  35. /**
  36. * the cleanup function.
  37. * @param aDryRun = true to build changes list, false to modify the board
  38. * @param aItemsList = list of modified items
  39. * @param aCleanVias = true to remove superimposed vias
  40. * @param aRemoveMisConnected = true to remove segments connecting 2 different nets
  41. * (short circuits)
  42. * @param aMergeSegments = true to merge collinear segmenst and remove 0 len segm
  43. * @param aDeleteUnconnected = true to remove dangling tracks
  44. * @param aDeleteTracksinPad = true to remove tracks fully inside pads
  45. * @param aDeleteDanglingVias = true to remove a via that is only connected to a single layer
  46. * @param aReporter is a REPORTER to print activity and info
  47. */
  48. void CleanupBoard( bool aDryRun, std::vector<std::shared_ptr<CLEANUP_ITEM> >* aItemsList,
  49. bool aCleanVias, bool aRemoveMisConnected, bool aMergeSegments,
  50. bool aDeleteUnconnected, bool aDeleteTracksinPad, bool aDeleteDanglingVias,
  51. REPORTER* aReporter = nullptr );
  52. private:
  53. /*
  54. * Removes track segments which are connected to more than one net (short circuits).
  55. */
  56. void removeShortingTrackSegments();
  57. /**
  58. * Removes tracks or vias only connected on one end
  59. * @param aTrack if true, clean dangling tracks
  60. * @param aVia if true, clean dangling vias
  61. * @return true if any items were deleted
  62. */
  63. bool deleteDanglingTracks( bool aTrack, bool aVia );
  64. void deleteTracksInPads();
  65. /**
  66. * Geometry-based cleanup: duplicate items, null items, colinear items.
  67. */
  68. void cleanup( bool aDeleteDuplicateVias, bool aDeleteNullSegments,
  69. bool aDeleteDuplicateSegments, bool aMergeSegments );
  70. /**
  71. * helper function
  72. * merge aTrackRef and aCandidate, when possible,
  73. * i.e. when they are colinear, same width, and obviously same layer
  74. * @return true if the segments are merged, false if not
  75. * @param aSeg1 is the reference
  76. * @param aSeg2 is the candidate, and after merging, the removed segment
  77. */
  78. bool mergeCollinearSegments( PCB_TRACK* aSeg1, PCB_TRACK* aSeg2 );
  79. /**
  80. * @return true if a track end position is a node, i.e. a end connected
  81. * to more than one item.
  82. * @param aTrack is the track to test.
  83. * @param aTstStart = true ot test the start point of the track or false for end point
  84. */
  85. bool testTrackEndpointIsNode( PCB_TRACK* aTrack, bool aTstStart );
  86. void removeItems( std::set<BOARD_ITEM*>& aItems );
  87. const std::vector<BOARD_CONNECTED_ITEM*>& getConnectedItems( PCB_TRACK* aTrack );
  88. private:
  89. BOARD* m_brd;
  90. BOARD_COMMIT& m_commit; // caller owns
  91. bool m_dryRun;
  92. std::vector<std::shared_ptr<CLEANUP_ITEM>>* m_itemsList; // caller owns
  93. REPORTER* m_reporter;
  94. // Cache connections. O(n^2) is awful, but it beats O(2n^3).
  95. std::map<PCB_TRACK*, std::vector<BOARD_CONNECTED_ITEM*>> m_connectedItemsCache;
  96. };
  97. #endif //KICAD_TRACKS_CLEANER_H