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.

382 lines
12 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2004-2018 Jean-Pierre Charras, jp.charras at wanadoo.fr
  5. * Copyright (C) 2011 Wayne Stambaugh <stambaughw@verizon.net>
  6. * Copyright (C) 1992-2022 KiCad Developers, see AUTHORS.txt for contributors.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, you may find one here:
  20. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  21. * or you may search the http://www.gnu.org website for the version 2 license,
  22. * or you may write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  24. */
  25. #include <reporter.h>
  26. #include <macros.h>
  27. #include <board_commit.h>
  28. #include <cleanup_item.h>
  29. #include <pcb_shape.h>
  30. #include <fp_shape.h>
  31. #include <pad.h>
  32. #include <footprint.h>
  33. #include <graphics_cleaner.h>
  34. #include <board_design_settings.h>
  35. #include <tool/tool_manager.h>
  36. #include <tools/pad_tool.h>
  37. GRAPHICS_CLEANER::GRAPHICS_CLEANER( DRAWINGS& aDrawings, FOOTPRINT* aParentFootprint,
  38. BOARD_COMMIT& aCommit, TOOL_MANAGER* aToolMgr ) :
  39. m_drawings( aDrawings ),
  40. m_parentFootprint( aParentFootprint ),
  41. m_commit( aCommit ),
  42. m_toolMgr( aToolMgr ),
  43. m_dryRun( true ),
  44. m_epsilon( 0 ),
  45. m_itemsList( nullptr )
  46. {
  47. }
  48. void GRAPHICS_CLEANER::CleanupBoard( bool aDryRun,
  49. std::vector<std::shared_ptr<CLEANUP_ITEM>>* aItemsList,
  50. bool aMergeRects, bool aDeleteRedundant, bool aMergePads )
  51. {
  52. m_dryRun = aDryRun;
  53. m_itemsList = aItemsList;
  54. m_epsilon = m_commit.GetBoard()->GetDesignSettings().m_MaxError;
  55. // Clear the flag used to mark some shapes as deleted, in dry run:
  56. for( BOARD_ITEM* drawing : m_drawings )
  57. drawing->ClearFlags( IS_DELETED );
  58. if( aDeleteRedundant )
  59. cleanupShapes();
  60. if( aMergeRects )
  61. mergeRects();
  62. if( aMergePads )
  63. mergePads();
  64. // Clear the flag used to mark some shapes:
  65. for( BOARD_ITEM* drawing : m_drawings )
  66. drawing->ClearFlags( IS_DELETED );
  67. }
  68. bool equivalent( const VECTOR2I& a, const VECTOR2I& b, int epsilon )
  69. {
  70. return abs( a.x - b.x ) < epsilon && abs( a.y - b.y ) < epsilon;
  71. };
  72. bool GRAPHICS_CLEANER::isNullShape( PCB_SHAPE* aShape )
  73. {
  74. switch( aShape->GetShape() )
  75. {
  76. case SHAPE_T::SEGMENT:
  77. case SHAPE_T::RECT:
  78. case SHAPE_T::ARC:
  79. return equivalent( aShape->GetStart(), aShape->GetEnd(), m_epsilon );
  80. case SHAPE_T::CIRCLE:
  81. return aShape->GetRadius() == 0;
  82. case SHAPE_T::POLY:
  83. return aShape->GetPointCount() == 0;
  84. case SHAPE_T::BEZIER:
  85. aShape->RebuildBezierToSegmentsPointsList( aShape->GetWidth() );
  86. // If the Bezier points list contains 2 points, it is equivalent to a segment
  87. if( aShape->GetBezierPoints().size() == 2 )
  88. return equivalent( aShape->GetStart(), aShape->GetEnd(), m_epsilon );
  89. // If the Bezier points list contains 1 points, it is equivalent to a point
  90. return aShape->GetBezierPoints().size() < 2;
  91. default:
  92. UNIMPLEMENTED_FOR( aShape->SHAPE_T_asString() );
  93. return false;
  94. }
  95. }
  96. bool GRAPHICS_CLEANER::areEquivalent( PCB_SHAPE* aShape1, PCB_SHAPE* aShape2 )
  97. {
  98. if( aShape1->GetShape() != aShape2->GetShape()
  99. || aShape1->GetLayer() != aShape2->GetLayer()
  100. || aShape1->GetWidth() != aShape2->GetWidth() )
  101. {
  102. return false;
  103. }
  104. switch( aShape1->GetShape() )
  105. {
  106. case SHAPE_T::SEGMENT:
  107. case SHAPE_T::RECT:
  108. case SHAPE_T::CIRCLE:
  109. return equivalent( aShape1->GetStart(), aShape2->GetStart(), m_epsilon )
  110. && equivalent( aShape1->GetEnd(), aShape2->GetEnd(), m_epsilon );
  111. case SHAPE_T::ARC:
  112. return equivalent( aShape1->GetCenter(), aShape2->GetCenter(), m_epsilon )
  113. && equivalent( aShape1->GetStart(), aShape2->GetStart(), m_epsilon )
  114. && equivalent( aShape1->GetEnd(), aShape2->GetEnd(), m_epsilon );
  115. case SHAPE_T::POLY:
  116. // TODO
  117. return false;
  118. case SHAPE_T::BEZIER:
  119. return equivalent( aShape1->GetStart(), aShape2->GetStart(), m_epsilon )
  120. && equivalent( aShape1->GetEnd(), aShape2->GetEnd(), m_epsilon )
  121. && equivalent( aShape1->GetBezierC1(), aShape2->GetBezierC1(), m_epsilon )
  122. && equivalent( aShape1->GetBezierC2(), aShape2->GetBezierC2(), m_epsilon );
  123. default:
  124. wxFAIL_MSG( wxT( "GRAPHICS_CLEANER::areEquivalent unimplemented for " )
  125. + aShape1->SHAPE_T_asString() );
  126. return false;
  127. }
  128. }
  129. void GRAPHICS_CLEANER::cleanupShapes()
  130. {
  131. // Remove duplicate shapes (2 superimposed identical shapes):
  132. for( auto it = m_drawings.begin(); it != m_drawings.end(); it++ )
  133. {
  134. PCB_SHAPE* shape = dynamic_cast<PCB_SHAPE*>( *it );
  135. if( !shape || shape->HasFlag( IS_DELETED ) )
  136. continue;
  137. if( isNullShape( shape ) )
  138. {
  139. std::shared_ptr<CLEANUP_ITEM> item = std::make_shared<CLEANUP_ITEM>( CLEANUP_NULL_GRAPHIC );
  140. item->SetItems( shape );
  141. m_itemsList->push_back( item );
  142. if( !m_dryRun )
  143. m_commit.Remove( shape );
  144. continue;
  145. }
  146. for( auto it2 = it + 1; it2 != m_drawings.end(); it2++ )
  147. {
  148. PCB_SHAPE* shape2 = dynamic_cast<PCB_SHAPE*>( *it2 );
  149. if( !shape2 || shape2->HasFlag( IS_DELETED ) )
  150. continue;
  151. if( areEquivalent( shape, shape2 ) )
  152. {
  153. std::shared_ptr<CLEANUP_ITEM> item = std::make_shared<CLEANUP_ITEM>( CLEANUP_DUPLICATE_GRAPHIC );
  154. item->SetItems( shape2 );
  155. m_itemsList->push_back( item );
  156. shape2->SetFlags(IS_DELETED );
  157. if( !m_dryRun )
  158. m_commit.Remove( shape2 );
  159. }
  160. }
  161. }
  162. }
  163. void GRAPHICS_CLEANER::mergeRects()
  164. {
  165. struct SIDE_CANDIDATE
  166. {
  167. SIDE_CANDIDATE( PCB_SHAPE* aShape ) :
  168. start( aShape->GetStart() ),
  169. end( aShape->GetEnd() ),
  170. shape( aShape )
  171. {
  172. if( start.x > end.x || start.y > end.y )
  173. std::swap( start, end );
  174. }
  175. VECTOR2I start;
  176. VECTOR2I end;
  177. PCB_SHAPE* shape;
  178. };
  179. std::vector<SIDE_CANDIDATE*> sides;
  180. std::map<VECTOR2I, std::vector<SIDE_CANDIDATE*>> ptMap;
  181. // First load all the candidates into the side vector and layer maps
  182. for( BOARD_ITEM* item : m_drawings )
  183. {
  184. PCB_SHAPE* shape = dynamic_cast<PCB_SHAPE*>( item );
  185. if( !shape || isNullShape( shape ) || shape->GetShape() != SHAPE_T::SEGMENT )
  186. continue;
  187. if( shape->GetStart().x == shape->GetEnd().x || shape->GetStart().y == shape->GetEnd().y )
  188. {
  189. sides.emplace_back( new SIDE_CANDIDATE( shape ) );
  190. ptMap[ sides.back()->start ].push_back( sides.back() );
  191. }
  192. }
  193. // Now go through the sides and try and match lines into rectangles
  194. for( SIDE_CANDIDATE* side : sides )
  195. {
  196. if( side->shape->HasFlag( IS_DELETED ) )
  197. continue;
  198. SIDE_CANDIDATE* left = nullptr;
  199. SIDE_CANDIDATE* top = nullptr;
  200. SIDE_CANDIDATE* right = nullptr;
  201. SIDE_CANDIDATE* bottom = nullptr;
  202. auto viable = [&]( SIDE_CANDIDATE* aCandidate ) -> bool
  203. {
  204. return aCandidate->shape->GetLayer() == side->shape->GetLayer()
  205. && aCandidate->shape->GetWidth() == side->shape->GetWidth()
  206. && !aCandidate->shape->HasFlag( IS_DELETED );
  207. };
  208. if( side->start.x == side->end.x )
  209. {
  210. // We've found a possible left; see if we have a top
  211. //
  212. left = side;
  213. for( SIDE_CANDIDATE* candidate : ptMap[ left->start ] )
  214. {
  215. if( candidate != left && viable( candidate ) )
  216. {
  217. top = candidate;
  218. break;
  219. }
  220. }
  221. }
  222. else if( side->start.y == side->end.y )
  223. {
  224. // We've found a possible top; see if we have a left
  225. //
  226. top = side;
  227. for( SIDE_CANDIDATE* candidate : ptMap[ top->start ] )
  228. {
  229. if( candidate != top && viable( candidate ) )
  230. {
  231. left = candidate;
  232. break;
  233. }
  234. }
  235. }
  236. if( top && left )
  237. {
  238. // See if we can fill in the other two sides
  239. //
  240. for( SIDE_CANDIDATE* candidate : ptMap[ top->end ] )
  241. {
  242. if( candidate != top && candidate != left && viable( candidate ) )
  243. {
  244. right = candidate;
  245. break;
  246. }
  247. }
  248. for( SIDE_CANDIDATE* candidate : ptMap[ left->end ] )
  249. {
  250. if( candidate != top && candidate != left && viable( candidate ) )
  251. {
  252. bottom = candidate;
  253. break;
  254. }
  255. }
  256. if( right && bottom && right->end == bottom->end )
  257. {
  258. left->shape->SetFlags( IS_DELETED );
  259. top->shape->SetFlags( IS_DELETED );
  260. right->shape->SetFlags( IS_DELETED );
  261. bottom->shape->SetFlags( IS_DELETED );
  262. std::shared_ptr<CLEANUP_ITEM> item = std::make_shared<CLEANUP_ITEM>( CLEANUP_LINES_TO_RECT );
  263. item->SetItems( left->shape, top->shape, right->shape, bottom->shape );
  264. m_itemsList->push_back( item );
  265. if( !m_dryRun )
  266. {
  267. PCB_SHAPE* rect;
  268. FP_SHAPE* fp_rect = nullptr;
  269. if( m_parentFootprint )
  270. rect = fp_rect = new FP_SHAPE( m_parentFootprint );
  271. else
  272. rect = new PCB_SHAPE();
  273. rect->SetShape( SHAPE_T::RECT );
  274. rect->SetFilled( false );
  275. rect->SetStart( top->start );
  276. rect->SetEnd( bottom->end );
  277. rect->SetLayer( top->shape->GetLayer() );
  278. rect->SetStroke( top->shape->GetStroke() );
  279. if( fp_rect )
  280. fp_rect->SetLocalCoord();
  281. m_commit.Add( rect );
  282. m_commit.Remove( left->shape );
  283. m_commit.Remove( top->shape );
  284. m_commit.Remove( right->shape );
  285. m_commit.Remove( bottom->shape );
  286. }
  287. }
  288. }
  289. }
  290. for( SIDE_CANDIDATE* side : sides )
  291. delete side;
  292. }
  293. void GRAPHICS_CLEANER::mergePads()
  294. {
  295. wxCHECK_MSG( m_parentFootprint, /*void*/, wxT( "mergePads() is FootprintEditor only" ) );
  296. PAD_TOOL* padTool = m_toolMgr->GetTool<PAD_TOOL>();
  297. std::map<wxString, int> padToNetTieGroupMap = m_parentFootprint->MapPadNumbersToNetTieGroups();
  298. for( PAD* pad : m_parentFootprint->Pads() )
  299. {
  300. // Don't merge a pad that's in a net-tie pad group. (We don't care which group.)
  301. if( padToNetTieGroupMap[ pad->GetNumber() ] >= 0 )
  302. continue;
  303. std::vector<FP_SHAPE*> shapes = padTool->RecombinePad( pad, m_dryRun, m_commit );
  304. if( !shapes.empty() )
  305. {
  306. std::shared_ptr<CLEANUP_ITEM> item = std::make_shared<CLEANUP_ITEM>( CLEANUP_MERGE_PAD );
  307. for( FP_SHAPE* shape : shapes )
  308. item->AddItem( shape );
  309. item->AddItem( pad );
  310. m_itemsList->push_back( item );
  311. }
  312. }
  313. }