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.

189 lines
6.2 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-2021 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 <footprint.h>
  27. #include <fp_text.h>
  28. #include <fp_shape.h>
  29. #include <pad.h>
  30. #include <functional>
  31. using namespace std;
  32. // Common calculation part for all BOARD_ITEMs
  33. static inline size_t hash_board_item( const BOARD_ITEM* aItem, int aFlags )
  34. {
  35. size_t ret = 0;
  36. if( aFlags & HASH_LAYER )
  37. ret = hash<unsigned long long>{}( aItem->GetLayerSet().to_ullong() );
  38. return ret;
  39. }
  40. size_t hash_fp_item( const EDA_ITEM* aItem, int aFlags )
  41. {
  42. size_t ret = 0;
  43. switch( aItem->Type() )
  44. {
  45. case PCB_FOOTPRINT_T:
  46. {
  47. const FOOTPRINT* footprint = static_cast<const FOOTPRINT*>( aItem );
  48. ret = hash_board_item( footprint, aFlags );
  49. if( aFlags & HASH_POS )
  50. hash_combine( ret, footprint->GetPosition().x, footprint->GetPosition().y );
  51. if( aFlags & HASH_ROT )
  52. hash_combine( ret, footprint->GetOrientation() );
  53. for( BOARD_ITEM* item : footprint->GraphicalItems() )
  54. hash_combine( ret, hash_fp_item( item, aFlags ) );
  55. for( PAD* pad : footprint->Pads() )
  56. hash_combine( ret, hash_fp_item( static_cast<EDA_ITEM*>( pad ), aFlags ) );
  57. }
  58. break;
  59. case PCB_PAD_T:
  60. {
  61. const PAD* pad = static_cast<const PAD*>( aItem );
  62. ret = hash<int>{}( static_cast<int>( pad->GetShape() ) << 16 );
  63. hash_combine( ret, pad->GetDrillShape() << 18 );
  64. hash_combine( ret, pad->GetSize().x << 8 );
  65. hash_combine( ret, pad->GetSize().y << 9 );
  66. hash_combine( ret, pad->GetOffset().x << 6 );
  67. hash_combine( ret, pad->GetOffset().y << 7 );
  68. hash_combine( ret, pad->GetDelta().x << 4 );
  69. hash_combine( ret, pad->GetDelta().y << 5 );
  70. hash_combine( ret, hash_board_item( pad, aFlags ) );
  71. if( aFlags & HASH_POS )
  72. {
  73. if( aFlags & REL_COORD )
  74. hash_combine( ret, pad->GetPos0().x, pad->GetPos0().y );
  75. else
  76. hash_combine( ret, pad->GetPosition().x, pad->GetPosition().y );
  77. }
  78. if( aFlags & HASH_ROT )
  79. hash_combine( ret, pad->GetOrientation() );
  80. if( aFlags & HASH_NET )
  81. hash_combine( ret, pad->GetNetCode() );
  82. }
  83. break;
  84. case PCB_FP_TEXT_T:
  85. {
  86. const FP_TEXT* text = static_cast<const FP_TEXT*>( aItem );
  87. if( !( aFlags & HASH_REF ) && text->GetType() == FP_TEXT::TEXT_is_REFERENCE )
  88. break;
  89. if( !( aFlags & HASH_VALUE ) && text->GetType() == FP_TEXT::TEXT_is_VALUE )
  90. break;
  91. ret = hash_board_item( text, aFlags );
  92. hash_combine( ret, text->GetText().ToStdString() );
  93. hash_combine( ret, text->IsItalic() );
  94. hash_combine( ret, text->IsBold() );
  95. hash_combine( ret, text->IsMirrored() );
  96. hash_combine( ret, text->GetTextWidth() );
  97. hash_combine( ret, text->GetTextHeight() );
  98. hash_combine( ret, text->GetHorizJustify() );
  99. hash_combine( ret, text->GetVertJustify() );
  100. if( aFlags & HASH_POS )
  101. {
  102. if( aFlags & REL_COORD )
  103. hash_combine( ret, text->GetPos0().x, text->GetPos0().y );
  104. else
  105. hash_combine( ret, text->GetPosition().x, text->GetPosition().y );
  106. }
  107. if( aFlags & HASH_ROT )
  108. hash_combine( ret, text->GetTextAngle() );
  109. }
  110. break;
  111. case PCB_FP_SHAPE_T:
  112. {
  113. const FP_SHAPE* shape = static_cast<const FP_SHAPE*>( aItem );
  114. ret = hash_board_item( shape, aFlags );
  115. hash_combine( ret, shape->GetShape() );
  116. hash_combine( ret, shape->GetWidth() );
  117. hash_combine( ret, shape->IsFilled() );
  118. if( shape->GetShape() == SHAPE_T::ARC || shape->GetShape() == SHAPE_T::CIRCLE )
  119. hash_combine( ret, shape->GetRadius() );
  120. if( aFlags & HASH_POS )
  121. {
  122. if( aFlags & REL_COORD )
  123. {
  124. hash_combine( ret, shape->GetStart0().x );
  125. hash_combine( ret, shape->GetStart0().y );
  126. hash_combine( ret, shape->GetEnd0().x );
  127. hash_combine( ret, shape->GetEnd0().y );
  128. if( shape->GetShape() == SHAPE_T::ARC )
  129. {
  130. hash_combine( ret, shape->GetCenter0().x );
  131. hash_combine( ret, shape->GetCenter0().y );
  132. hash_combine( ret, shape->GetArcAngle() );
  133. }
  134. }
  135. else
  136. {
  137. hash_combine( ret, shape->GetStart().x );
  138. hash_combine( ret, shape->GetStart().y );
  139. hash_combine( ret, shape->GetEnd().x );
  140. hash_combine( ret, shape->GetEnd().y );
  141. if( shape->GetShape() == SHAPE_T::ARC )
  142. {
  143. hash_combine( ret, shape->GetCenter().x );
  144. hash_combine( ret, shape->GetCenter().y );
  145. hash_combine( ret, shape->GetArcAngle() );
  146. }
  147. }
  148. }
  149. }
  150. break;
  151. default:
  152. wxASSERT_MSG( false, "Unhandled type in function hash_fp_item() (exporter_gencad.cpp)" );
  153. }
  154. return ret;
  155. }