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.

230 lines
7.5 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2017 CERN
  5. * Copyright (C) 2020-2023 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_textbox.h>
  30. #include <pcb_shape.h>
  31. #include <pad.h>
  32. #include <functional>
  33. using namespace std;
  34. // Common calculation part for all BOARD_ITEMs
  35. static inline size_t hash_board_item( const BOARD_ITEM* aItem, int aFlags )
  36. {
  37. size_t ret = 0;
  38. if( aFlags & HASH_LAYER )
  39. ret = hash<unsigned long long>{}( aItem->GetLayerSet().to_ullong() );
  40. return ret;
  41. }
  42. size_t hash_fp_item( const EDA_ITEM* aItem, int aFlags )
  43. {
  44. size_t ret = 0;
  45. switch( aItem->Type() )
  46. {
  47. case PCB_FOOTPRINT_T:
  48. {
  49. const FOOTPRINT* footprint = static_cast<const FOOTPRINT*>( aItem );
  50. ret = hash_board_item( footprint, aFlags );
  51. if( aFlags & HASH_POS )
  52. hash_combine( ret, footprint->GetPosition().x, footprint->GetPosition().y );
  53. if( aFlags & HASH_ROT )
  54. hash_combine( ret, footprint->GetOrientation().AsDegrees() );
  55. for( BOARD_ITEM* item : footprint->GraphicalItems() )
  56. hash_combine( ret, hash_fp_item( item, aFlags ) );
  57. for( PAD* pad : footprint->Pads() )
  58. hash_combine( ret, hash_fp_item( static_cast<EDA_ITEM*>( pad ), aFlags ) );
  59. }
  60. break;
  61. case PCB_PAD_T:
  62. {
  63. const PAD* pad = static_cast<const PAD*>( aItem );
  64. ret = hash<int>{}( static_cast<int>( pad->GetShape() ) );
  65. hash_combine( ret, pad->GetDrillShape() );
  66. hash_combine( ret, pad->GetSize().x, pad->GetSize().y );
  67. hash_combine( ret, pad->GetOffset().x, pad->GetOffset().y );
  68. hash_combine( ret, pad->GetDelta().x, pad->GetDelta().y );
  69. hash_combine( ret, hash_board_item( pad, aFlags ) );
  70. if( aFlags & HASH_POS )
  71. {
  72. if( aFlags & REL_COORD )
  73. hash_combine( ret, pad->GetFPRelativePosition().x, pad->GetFPRelativePosition().y );
  74. else
  75. hash_combine( ret, pad->GetPosition().x, pad->GetPosition().y );
  76. }
  77. if( aFlags & HASH_ROT )
  78. hash_combine( ret, pad->GetOrientation().AsDegrees() );
  79. if( aFlags & HASH_NET )
  80. hash_combine( ret, pad->GetNetCode() );
  81. }
  82. break;
  83. case PCB_TEXT_T:
  84. {
  85. const PCB_TEXT* text = static_cast<const PCB_TEXT*>( aItem );
  86. if( !( aFlags & HASH_REF ) && text->GetType() == PCB_TEXT::TEXT_is_REFERENCE )
  87. break;
  88. if( !( aFlags & HASH_VALUE ) && text->GetType() == PCB_TEXT::TEXT_is_VALUE )
  89. break;
  90. ret = hash_board_item( text, aFlags );
  91. hash_combine( ret, text->GetText().ToStdString() );
  92. hash_combine( ret, text->IsItalic() );
  93. hash_combine( ret, text->IsBold() );
  94. hash_combine( ret, text->IsMirrored() );
  95. hash_combine( ret, text->GetTextWidth() );
  96. hash_combine( ret, text->GetTextHeight() );
  97. hash_combine( ret, text->GetHorizJustify() );
  98. hash_combine( ret, text->GetVertJustify() );
  99. if( aFlags & HASH_POS )
  100. {
  101. VECTOR2I pos = ( aFlags & REL_COORD ) ? text->GetFPRelativePosition()
  102. : text->GetPosition();
  103. hash_combine( ret, pos.x, pos.y );
  104. }
  105. if( aFlags & HASH_ROT )
  106. hash_combine( ret, text->GetTextAngle().AsDegrees() );
  107. }
  108. break;
  109. case PCB_SHAPE_T:
  110. {
  111. const PCB_SHAPE* shape = static_cast<const PCB_SHAPE*>( aItem );
  112. ret = hash_board_item( shape, aFlags );
  113. hash_combine( ret, shape->GetShape() );
  114. hash_combine( ret, shape->GetWidth() );
  115. hash_combine( ret, shape->IsFilled() );
  116. if( shape->GetShape() == SHAPE_T::ARC || shape->GetShape() == SHAPE_T::CIRCLE )
  117. hash_combine( ret, shape->GetRadius() );
  118. if( aFlags & HASH_POS )
  119. {
  120. VECTOR2I start = shape->GetStart();
  121. VECTOR2I end = shape->GetEnd();
  122. VECTOR2I center = shape->GetCenter();
  123. FOOTPRINT* parentFP = shape->GetParentFootprint();
  124. if( parentFP && ( aFlags & REL_COORD ) )
  125. {
  126. start -= parentFP->GetPosition();
  127. end -= parentFP->GetPosition();
  128. center -= parentFP->GetPosition();
  129. RotatePoint( start, -parentFP->GetOrientation() );
  130. RotatePoint( end, -parentFP->GetOrientation() );
  131. RotatePoint( center, -parentFP->GetOrientation() );
  132. }
  133. hash_combine( ret, start.x );
  134. hash_combine( ret, start.y );
  135. hash_combine( ret, end.x );
  136. hash_combine( ret, end.y );
  137. if( shape->GetShape() == SHAPE_T::ARC )
  138. {
  139. hash_combine( ret, center.x );
  140. hash_combine( ret, center.y );
  141. }
  142. }
  143. }
  144. break;
  145. case PCB_TEXTBOX_T:
  146. {
  147. const PCB_TEXTBOX* textbox = static_cast<const PCB_TEXTBOX*>( aItem );
  148. ret = hash_board_item( textbox, aFlags );
  149. hash_combine( ret, textbox->GetText().ToStdString() );
  150. hash_combine( ret, textbox->IsItalic() );
  151. hash_combine( ret, textbox->IsBold() );
  152. hash_combine( ret, textbox->IsMirrored() );
  153. hash_combine( ret, textbox->GetTextWidth() );
  154. hash_combine( ret, textbox->GetTextHeight() );
  155. hash_combine( ret, textbox->GetHorizJustify() );
  156. hash_combine( ret, textbox->GetVertJustify() );
  157. if( aFlags & HASH_ROT )
  158. hash_combine( ret, textbox->GetTextAngle().AsDegrees() );
  159. hash_combine( ret, textbox->GetShape() );
  160. hash_combine( ret, textbox->GetWidth() );
  161. if( aFlags & HASH_POS )
  162. {
  163. VECTOR2I start = textbox->GetStart();
  164. VECTOR2I end = textbox->GetEnd();
  165. FOOTPRINT* parentFP = textbox->GetParentFootprint();
  166. if( parentFP && ( aFlags & REL_COORD ) )
  167. {
  168. start -= parentFP->GetPosition();
  169. end -= parentFP->GetPosition();
  170. RotatePoint( start, -parentFP->GetOrientation() );
  171. RotatePoint( end, -parentFP->GetOrientation() );
  172. }
  173. hash_combine( ret, start.x );
  174. hash_combine( ret, start.y );
  175. hash_combine( ret, end.x );
  176. hash_combine( ret, end.y );
  177. }
  178. }
  179. break;
  180. default:
  181. wxASSERT_MSG( false, "Unhandled type in function hash_fp_item() (exporter_gencad.cpp)" );
  182. }
  183. return ret;
  184. }