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.

358 lines
12 KiB

1 year ago
1 year ago
1 year ago
1 year ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2017 CERN
  5. * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
  6. * @author Maciej Suminski <maciej.suminski@cern.ch>
  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 <hash_eda.h>
  26. #include <hash.h>
  27. #include <footprint.h>
  28. #include <pcb_text.h>
  29. #include <pcb_table.h>
  30. #include <pcb_textbox.h>
  31. #include <pcb_shape.h>
  32. #include <pad.h>
  33. #include <pcb_track.h>
  34. #include <macros.h>
  35. #include <functional>
  36. #include <wx/log.h>
  37. using namespace std;
  38. // Common calculation part for all BOARD_ITEMs
  39. static inline size_t hash_board_item( const BOARD_ITEM* aItem, int aFlags )
  40. {
  41. size_t ret = 0;
  42. if( aFlags & HASH_LAYER )
  43. ret = hash<BASE_SET>{}( aItem->GetLayerSet() );
  44. return ret;
  45. }
  46. size_t hash_fp_item( const EDA_ITEM* aItem, int aFlags )
  47. {
  48. size_t ret = 0;
  49. switch( aItem->Type() )
  50. {
  51. case PCB_FOOTPRINT_T:
  52. {
  53. const FOOTPRINT* footprint = static_cast<const FOOTPRINT*>( aItem );
  54. ret = hash_board_item( footprint, aFlags );
  55. if( aFlags & HASH_POS )
  56. hash_combine( ret, footprint->GetPosition().x, footprint->GetPosition().y );
  57. if( aFlags & HASH_ROT )
  58. hash_combine( ret, footprint->GetOrientation().AsDegrees() );
  59. for( BOARD_ITEM* item : footprint->GraphicalItems() )
  60. hash_combine( ret, hash_fp_item( item, aFlags ) );
  61. for( PAD* pad : footprint->Pads() )
  62. hash_combine( ret, hash_fp_item( static_cast<EDA_ITEM*>( pad ), aFlags ) );
  63. }
  64. break;
  65. case PCB_VIA_T:
  66. {
  67. const PCB_VIA* via = static_cast<const PCB_VIA*>( aItem );
  68. ret = hash<int>{}( via->GetDrillValue() );
  69. hash_combine( ret, via->TopLayer() );
  70. hash_combine( ret, via->BottomLayer() );
  71. via->GetLayerSet().RunOnLayers(
  72. [&]( PCB_LAYER_ID layer )
  73. {
  74. hash_combine( ret, via->GetWidth( layer ) );
  75. hash_combine( ret, via->FlashLayer( layer ) );
  76. } );
  77. break;
  78. }
  79. case PCB_PAD_T:
  80. {
  81. const PAD* pad = static_cast<const PAD*>( aItem );
  82. ret = hash<int>{}( static_cast<int>( pad->GetAttribute() ) );
  83. auto hashPadLayer =
  84. [&]( PCB_LAYER_ID aLayer )
  85. {
  86. hash_combine( ret, pad->GetShape( aLayer ) );
  87. hash_combine( ret, pad->GetSize( aLayer ).x, pad->GetSize( aLayer ).y );
  88. hash_combine( ret, pad->GetOffset( aLayer ).x, pad->GetOffset( aLayer ).y );
  89. switch( pad->GetShape( PADSTACK::ALL_LAYERS ) )
  90. {
  91. case PAD_SHAPE::CHAMFERED_RECT:
  92. hash_combine( ret, pad->GetChamferPositions( aLayer ) );
  93. hash_combine( ret, pad->GetChamferRectRatio( aLayer ) );
  94. break;
  95. case PAD_SHAPE::ROUNDRECT:
  96. hash_combine( ret, pad->GetRoundRectCornerRadius( aLayer ) );
  97. break;
  98. case PAD_SHAPE::TRAPEZOID:
  99. hash_combine( ret, pad->GetDelta( aLayer ).x, pad->GetDelta( aLayer ).y );
  100. break;
  101. case PAD_SHAPE::CUSTOM:
  102. {
  103. auto poly = pad->GetEffectivePolygon( aLayer, ERROR_INSIDE );
  104. for( int ii = 0; ii < poly->VertexCount(); ++ii )
  105. {
  106. VECTOR2I point = poly->CVertex( ii ) - pad->GetPosition();
  107. hash_combine( ret, point.x, point.y );
  108. }
  109. break;
  110. }
  111. default:
  112. break;
  113. }
  114. };
  115. pad->Padstack().ForEachUniqueLayer( hashPadLayer );
  116. if( pad->GetAttribute() == PAD_ATTRIB::PTH || pad->GetAttribute() == PAD_ATTRIB::NPTH )
  117. {
  118. hash_combine( ret, pad->GetDrillSizeX(), pad->GetDrillSizeY() );
  119. hash_combine( ret, pad->GetDrillShape() );
  120. pad->GetLayerSet().RunOnLayers(
  121. [&]( PCB_LAYER_ID layer )
  122. {
  123. hash_combine( ret, pad->FlashLayer( layer ) );
  124. } );
  125. }
  126. hash_combine( ret, hash_board_item( pad, aFlags ) );
  127. if( aFlags & HASH_POS )
  128. {
  129. if( aFlags & REL_COORD )
  130. hash_combine( ret, pad->GetFPRelativePosition().x, pad->GetFPRelativePosition().y );
  131. else
  132. hash_combine( ret, pad->GetPosition().x, pad->GetPosition().y );
  133. }
  134. if( aFlags & HASH_ROT )
  135. hash_combine( ret, pad->GetOrientation().AsDegrees() );
  136. if( aFlags & HASH_NET )
  137. hash_combine( ret, pad->GetNetCode() );
  138. }
  139. break;
  140. case PCB_FIELD_T:
  141. if( !( aFlags & HASH_REF ) && static_cast<const PCB_FIELD*>( aItem )->IsReference() )
  142. break;
  143. if( !( aFlags & HASH_VALUE ) && static_cast<const PCB_FIELD*>( aItem )->IsValue() )
  144. break;
  145. KI_FALLTHROUGH;
  146. case PCB_TEXT_T:
  147. {
  148. const PCB_TEXT* text = static_cast<const PCB_TEXT*>( aItem );
  149. ret = hash_board_item( text, aFlags );
  150. hash_combine( ret, text->GetText().ToStdString() );
  151. hash_combine( ret, text->IsItalic() );
  152. hash_combine( ret, text->IsBold() );
  153. hash_combine( ret, text->IsMirrored() );
  154. hash_combine( ret, text->GetTextWidth() );
  155. hash_combine( ret, text->GetTextHeight() );
  156. hash_combine( ret, text->GetHorizJustify() );
  157. hash_combine( ret, text->GetVertJustify() );
  158. if( aFlags & HASH_POS )
  159. {
  160. VECTOR2I pos = ( aFlags & REL_COORD ) ? text->GetFPRelativePosition()
  161. : text->GetPosition();
  162. hash_combine( ret, pos.x, pos.y );
  163. }
  164. if( aFlags & HASH_ROT )
  165. hash_combine( ret, text->GetTextAngle().AsDegrees() );
  166. }
  167. break;
  168. case PCB_SHAPE_T:
  169. {
  170. const PCB_SHAPE* shape = static_cast<const PCB_SHAPE*>( aItem );
  171. ret = hash_board_item( shape, aFlags );
  172. hash_combine( ret, shape->GetShape() );
  173. hash_combine( ret, shape->GetWidth() );
  174. hash_combine( ret, shape->IsFilled() );
  175. hash_combine( ret, shape->GetLineStyle() );
  176. if( shape->GetShape() == SHAPE_T::ARC || shape->GetShape() == SHAPE_T::CIRCLE )
  177. hash_combine( ret, shape->GetRadius() );
  178. if( aFlags & HASH_POS )
  179. {
  180. std::vector<VECTOR2I> points;
  181. points.push_back( shape->GetStart() );
  182. points.push_back( shape->GetEnd() );
  183. if( shape->GetShape() == SHAPE_T::CIRCLE )
  184. points.push_back( shape->GetCenter() );
  185. if( shape->GetShape() == SHAPE_T::ARC )
  186. points.push_back( shape->GetArcMid() );
  187. FOOTPRINT* parentFP = shape->GetParentFootprint();
  188. if( shape->GetShape() == SHAPE_T::POLY )
  189. {
  190. const SHAPE_POLY_SET& poly = shape->GetPolyShape();
  191. for( auto it = poly.CIterateWithHoles(); it; it++ )
  192. points.push_back( *it );
  193. }
  194. if( shape->GetShape() == SHAPE_T::BEZIER )
  195. {
  196. points.push_back( shape->GetBezierC1() );
  197. points.push_back( shape->GetBezierC2() );
  198. }
  199. if( parentFP && ( aFlags & REL_COORD ) )
  200. {
  201. for( VECTOR2I& point : points )
  202. {
  203. point -= parentFP->GetPosition();
  204. RotatePoint( point, -parentFP->GetOrientation() );
  205. }
  206. }
  207. if( aFlags & REL_POS )
  208. {
  209. for( VECTOR2I& point : points )
  210. point -= shape->GetPosition();
  211. }
  212. //Basic sort of start/end points to try to always draw the same direction (left to right, down to up)
  213. //The hashes are summed, so it doesn't matter what order the lines are drawn, only that the same points are used
  214. if( points.size() > 1 )
  215. {
  216. if( points[0].x > points[1].x || points[0].y > points[1].y )
  217. {
  218. std::swap( points[0], points[1] );
  219. }
  220. }
  221. for( VECTOR2I& point : points )
  222. hash_combine( ret, point.x, point.y );
  223. }
  224. }
  225. break;
  226. case PCB_TABLECELL_T:
  227. case PCB_TEXTBOX_T:
  228. {
  229. const PCB_TEXTBOX* textbox = static_cast<const PCB_TEXTBOX*>( aItem );
  230. ret = hash_board_item( textbox, aFlags );
  231. hash_combine( ret, textbox->GetText().ToStdString() );
  232. hash_combine( ret, textbox->IsItalic() );
  233. hash_combine( ret, textbox->IsBold() );
  234. hash_combine( ret, textbox->IsMirrored() );
  235. hash_combine( ret, textbox->GetTextWidth() );
  236. hash_combine( ret, textbox->GetTextHeight() );
  237. hash_combine( ret, textbox->GetHorizJustify() );
  238. hash_combine( ret, textbox->GetVertJustify() );
  239. if( aFlags & HASH_ROT )
  240. hash_combine( ret, textbox->GetTextAngle().AsDegrees() );
  241. hash_combine( ret, textbox->GetShape() );
  242. hash_combine( ret, textbox->GetWidth() );
  243. hash_combine( ret, textbox->GetLineStyle() );
  244. if( aFlags & HASH_POS )
  245. {
  246. VECTOR2I start = textbox->GetStart();
  247. VECTOR2I end = textbox->GetEnd();
  248. FOOTPRINT* parentFP = textbox->GetParentFootprint();
  249. if( parentFP && ( aFlags & REL_COORD ) )
  250. {
  251. start -= parentFP->GetPosition();
  252. end -= parentFP->GetPosition();
  253. RotatePoint( start, -parentFP->GetOrientation() );
  254. RotatePoint( end, -parentFP->GetOrientation() );
  255. }
  256. hash_combine( ret, start.x );
  257. hash_combine( ret, start.y );
  258. hash_combine( ret, end.x );
  259. hash_combine( ret, end.y );
  260. }
  261. }
  262. break;
  263. case PCB_TABLE_T:
  264. {
  265. const PCB_TABLE* table = static_cast<const PCB_TABLE*>( aItem );
  266. ret = hash_board_item( table, aFlags );
  267. hash_combine( ret, table->StrokeExternal() );
  268. hash_combine( ret, table->StrokeHeaderSeparator() );
  269. hash_combine( ret, table->StrokeColumns() );
  270. hash_combine( ret, table->StrokeRows() );
  271. auto hash_stroke =
  272. [&]( const STROKE_PARAMS& stroke )
  273. {
  274. hash_combine( ret, stroke.GetColor() );
  275. hash_combine( ret, stroke.GetWidth() );
  276. hash_combine( ret, stroke.GetLineStyle() );
  277. };
  278. hash_stroke( table->GetSeparatorsStroke() );
  279. hash_stroke( table->GetBorderStroke() );
  280. }
  281. break;
  282. default:
  283. wxASSERT_MSG( false, "Unhandled type in function hash_fp_item() (exporter_gencad.cpp)" );
  284. }
  285. return ret;
  286. }