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.

341 lines
11 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 <graphics_cleaner.h>
  32. #include <board_design_settings.h>
  33. GRAPHICS_CLEANER::GRAPHICS_CLEANER( DRAWINGS& aDrawings, FOOTPRINT* aParentFootprint,
  34. BOARD_COMMIT& aCommit ) :
  35. m_drawings( aDrawings ),
  36. m_parentFootprint( aParentFootprint ),
  37. m_commit( aCommit ),
  38. m_dryRun( true ),
  39. m_epsilon( 0 ),
  40. m_itemsList( nullptr )
  41. {
  42. }
  43. void GRAPHICS_CLEANER::CleanupBoard( bool aDryRun,
  44. std::vector<std::shared_ptr<CLEANUP_ITEM>>* aItemsList,
  45. bool aMergeRects, bool aDeleteRedundant )
  46. {
  47. m_dryRun = aDryRun;
  48. m_itemsList = aItemsList;
  49. m_epsilon = m_commit.GetBoard()->GetDesignSettings().m_MaxError;
  50. // Clear the flag used to mark some shapes as deleted, in dry run:
  51. for( BOARD_ITEM* drawing : m_drawings )
  52. drawing->ClearFlags( IS_DELETED );
  53. if( aDeleteRedundant )
  54. cleanupShapes();
  55. if( aMergeRects )
  56. mergeRects();
  57. // Clear the flag used to mark some shapes:
  58. for( BOARD_ITEM* drawing : m_drawings )
  59. drawing->ClearFlags( IS_DELETED );
  60. }
  61. bool equivalent( const VECTOR2I& a, const VECTOR2I& b, int epsilon )
  62. {
  63. return abs( a.x - b.x ) < epsilon && abs( a.y - b.y ) < epsilon;
  64. };
  65. bool GRAPHICS_CLEANER::isNullShape( PCB_SHAPE* aShape )
  66. {
  67. switch( aShape->GetShape() )
  68. {
  69. case SHAPE_T::SEGMENT:
  70. case SHAPE_T::RECT:
  71. case SHAPE_T::ARC:
  72. return equivalent( aShape->GetStart(), aShape->GetEnd(), m_epsilon );
  73. case SHAPE_T::CIRCLE:
  74. return aShape->GetRadius() == 0;
  75. case SHAPE_T::POLY:
  76. return aShape->GetPointCount() == 0;
  77. case SHAPE_T::BEZIER:
  78. aShape->RebuildBezierToSegmentsPointsList( aShape->GetWidth() );
  79. return aShape->GetBezierPoints().empty();
  80. default:
  81. UNIMPLEMENTED_FOR( aShape->SHAPE_T_asString() );
  82. return false;
  83. }
  84. }
  85. bool GRAPHICS_CLEANER::areEquivalent( PCB_SHAPE* aShape1, PCB_SHAPE* aShape2 )
  86. {
  87. if( aShape1->GetShape() != aShape2->GetShape()
  88. || aShape1->GetLayer() != aShape2->GetLayer()
  89. || aShape1->GetWidth() != aShape2->GetWidth() )
  90. {
  91. return false;
  92. }
  93. switch( aShape1->GetShape() )
  94. {
  95. case SHAPE_T::SEGMENT:
  96. case SHAPE_T::RECT:
  97. case SHAPE_T::CIRCLE:
  98. return equivalent( aShape1->GetStart(), aShape2->GetStart(), m_epsilon )
  99. && equivalent( aShape1->GetEnd(), aShape2->GetEnd(), m_epsilon );
  100. case SHAPE_T::ARC:
  101. return equivalent( aShape1->GetCenter(), aShape2->GetCenter(), m_epsilon )
  102. && equivalent( aShape1->GetStart(), aShape2->GetStart(), m_epsilon )
  103. && equivalent( aShape1->GetEnd(), aShape2->GetEnd(), m_epsilon );
  104. case SHAPE_T::POLY:
  105. // TODO
  106. return false;
  107. case SHAPE_T::BEZIER:
  108. return equivalent( aShape1->GetStart(), aShape2->GetStart(), m_epsilon )
  109. && equivalent( aShape1->GetEnd(), aShape2->GetEnd(), m_epsilon )
  110. && equivalent( aShape1->GetBezierC1(), aShape2->GetBezierC1(), m_epsilon )
  111. && equivalent( aShape1->GetBezierC2(), aShape2->GetBezierC2(), m_epsilon );
  112. default:
  113. wxFAIL_MSG( wxT( "GRAPHICS_CLEANER::areEquivalent unimplemented for " )
  114. + aShape1->SHAPE_T_asString() );
  115. return false;
  116. }
  117. }
  118. void GRAPHICS_CLEANER::cleanupShapes()
  119. {
  120. // Remove duplicate shapes (2 superimposed identical shapes):
  121. for( auto it = m_drawings.begin(); it != m_drawings.end(); it++ )
  122. {
  123. PCB_SHAPE* shape = dynamic_cast<PCB_SHAPE*>( *it );
  124. if( !shape || shape->HasFlag( IS_DELETED ) )
  125. continue;
  126. if( isNullShape( shape ) )
  127. {
  128. std::shared_ptr<CLEANUP_ITEM> item = std::make_shared<CLEANUP_ITEM>( CLEANUP_NULL_GRAPHIC );
  129. item->SetItems( shape );
  130. m_itemsList->push_back( item );
  131. if( !m_dryRun )
  132. m_commit.Remove( shape );
  133. continue;
  134. }
  135. for( auto it2 = it + 1; it2 != m_drawings.end(); it2++ )
  136. {
  137. PCB_SHAPE* shape2 = dynamic_cast<PCB_SHAPE*>( *it2 );
  138. if( !shape2 || shape2->HasFlag( IS_DELETED ) )
  139. continue;
  140. if( areEquivalent( shape, shape2 ) )
  141. {
  142. std::shared_ptr<CLEANUP_ITEM> item = std::make_shared<CLEANUP_ITEM>( CLEANUP_DUPLICATE_GRAPHIC );
  143. item->SetItems( shape2 );
  144. m_itemsList->push_back( item );
  145. shape2->SetFlags(IS_DELETED );
  146. if( !m_dryRun )
  147. m_commit.Remove( shape2 );
  148. }
  149. }
  150. }
  151. }
  152. void GRAPHICS_CLEANER::mergeRects()
  153. {
  154. struct SIDE_CANDIDATE
  155. {
  156. SIDE_CANDIDATE( PCB_SHAPE* aShape ) :
  157. start( aShape->GetStart() ),
  158. end( aShape->GetEnd() ),
  159. shape( aShape )
  160. {
  161. if( start.x > end.x || start.y > end.y )
  162. std::swap( start, end );
  163. }
  164. VECTOR2I start;
  165. VECTOR2I end;
  166. PCB_SHAPE* shape;
  167. };
  168. std::vector<SIDE_CANDIDATE*> sides;
  169. std::map<VECTOR2I, std::vector<SIDE_CANDIDATE*>> ptMap;
  170. // First load all the candidates into the side vector and layer maps
  171. for( BOARD_ITEM* item : m_drawings )
  172. {
  173. PCB_SHAPE* shape = dynamic_cast<PCB_SHAPE*>( item );
  174. if( !shape || isNullShape( shape ) || shape->GetShape() != SHAPE_T::SEGMENT )
  175. continue;
  176. if( shape->GetStart().x == shape->GetEnd().x || shape->GetStart().y == shape->GetEnd().y )
  177. {
  178. sides.emplace_back( new SIDE_CANDIDATE( shape ) );
  179. ptMap[ sides.back()->start ].push_back( sides.back() );
  180. }
  181. }
  182. // Now go through the sides and try and match lines into rectangles
  183. for( SIDE_CANDIDATE* side : sides )
  184. {
  185. if( side->shape->HasFlag( IS_DELETED ) )
  186. continue;
  187. SIDE_CANDIDATE* left = nullptr;
  188. SIDE_CANDIDATE* top = nullptr;
  189. SIDE_CANDIDATE* right = nullptr;
  190. SIDE_CANDIDATE* bottom = nullptr;
  191. auto viable = [&]( SIDE_CANDIDATE* aCandidate ) -> bool
  192. {
  193. return aCandidate->shape->GetLayer() == side->shape->GetLayer()
  194. && aCandidate->shape->GetWidth() == side->shape->GetWidth()
  195. && !aCandidate->shape->HasFlag( IS_DELETED );
  196. };
  197. if( side->start.x == side->end.x )
  198. {
  199. // We've found a possible left; see if we have a top
  200. //
  201. left = side;
  202. for( SIDE_CANDIDATE* candidate : ptMap[ left->start ] )
  203. {
  204. if( candidate != left && viable( candidate ) )
  205. {
  206. top = candidate;
  207. break;
  208. }
  209. }
  210. }
  211. else if( side->start.y == side->end.y )
  212. {
  213. // We've found a possible top; see if we have a left
  214. //
  215. top = side;
  216. for( SIDE_CANDIDATE* candidate : ptMap[ top->start ] )
  217. {
  218. if( candidate != top && viable( candidate ) )
  219. {
  220. left = candidate;
  221. break;
  222. }
  223. }
  224. }
  225. if( top && left )
  226. {
  227. // See if we can fill in the other two sides
  228. //
  229. for( SIDE_CANDIDATE* candidate : ptMap[ top->end ] )
  230. {
  231. if( candidate != top && candidate != left && viable( candidate ) )
  232. {
  233. right = candidate;
  234. break;
  235. }
  236. }
  237. for( SIDE_CANDIDATE* candidate : ptMap[ left->end ] )
  238. {
  239. if( candidate != top && candidate != left && viable( candidate ) )
  240. {
  241. bottom = candidate;
  242. break;
  243. }
  244. }
  245. if( right && bottom && right->end == bottom->end )
  246. {
  247. left->shape->SetFlags( IS_DELETED );
  248. top->shape->SetFlags( IS_DELETED );
  249. right->shape->SetFlags( IS_DELETED );
  250. bottom->shape->SetFlags( IS_DELETED );
  251. std::shared_ptr<CLEANUP_ITEM> item = std::make_shared<CLEANUP_ITEM>( CLEANUP_LINES_TO_RECT );
  252. item->SetItems( left->shape, top->shape, right->shape, bottom->shape );
  253. m_itemsList->push_back( item );
  254. if( !m_dryRun )
  255. {
  256. PCB_SHAPE* rect;
  257. FP_SHAPE* fp_rect = nullptr;
  258. if( m_parentFootprint )
  259. rect = fp_rect = new FP_SHAPE( m_parentFootprint );
  260. else
  261. rect = new PCB_SHAPE();
  262. rect->SetShape( SHAPE_T::RECT );
  263. rect->SetFilled( false );
  264. rect->SetStart( top->start );
  265. rect->SetEnd( bottom->end );
  266. rect->SetLayer( top->shape->GetLayer() );
  267. rect->SetStroke( top->shape->GetStroke() );
  268. if( fp_rect )
  269. fp_rect->SetLocalCoord();
  270. m_commit.Add( rect );
  271. m_commit.Remove( left->shape );
  272. m_commit.Remove( top->shape );
  273. m_commit.Remove( right->shape );
  274. m_commit.Remove( bottom->shape );
  275. }
  276. }
  277. }
  278. }
  279. for( SIDE_CANDIDATE* side : sides )
  280. delete side;
  281. }