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.

258 lines
7.6 KiB

4 years ago
4 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2012 Jean-Pierre Charras, jp.charras at wanadoo.fr
  5. * Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
  6. * Copyright (C) 2012 Wayne Stambaugh <stambaughw@verizon.net>
  7. * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 2
  12. * of the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, you may find one here:
  21. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  22. * or you may search the http://www.gnu.org website for the version 2 license,
  23. * or you may write to the Free Software Foundation, Inc.,
  24. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  25. */
  26. #include <bitmaps.h>
  27. #include <board.h>
  28. #include <board_design_settings.h>
  29. #include <pcb_target.h>
  30. #include <base_units.h>
  31. #include <settings/color_settings.h>
  32. #include <settings/settings_manager.h>
  33. #include <trigo.h>
  34. #include <i18n_utility.h>
  35. #include <geometry/shape_circle.h>
  36. #include <eda_draw_frame.h>
  37. #include <pcb_shape.h>
  38. PCB_TARGET::PCB_TARGET( BOARD_ITEM* aParent ) :
  39. BOARD_ITEM( aParent, PCB_TARGET_T )
  40. {
  41. m_shape = 0;
  42. m_size = pcbIUScale.mmToIU( 5 ); // Gives a decent size
  43. m_lineWidth = pcbIUScale.mmToIU( DEFAULT_COPPER_LINE_WIDTH );
  44. m_layer = Edge_Cuts; // a target is on all layers
  45. }
  46. PCB_TARGET::PCB_TARGET( BOARD_ITEM* aParent, int aShape, PCB_LAYER_ID aLayer, const VECTOR2I& aPos,
  47. int aSize, int aWidth ) :
  48. BOARD_ITEM( aParent, PCB_TARGET_T )
  49. {
  50. m_shape = aShape;
  51. m_layer = aLayer;
  52. m_pos = aPos;
  53. m_size = aSize;
  54. m_lineWidth = aWidth;
  55. }
  56. PCB_TARGET::~PCB_TARGET()
  57. {
  58. }
  59. bool PCB_TARGET::HitTest( const VECTOR2I& aPosition, int aAccuracy ) const
  60. {
  61. int dX = aPosition.x - m_pos.x;
  62. int dY = aPosition.y - m_pos.y;
  63. int radius = aAccuracy + ( m_size / 2 );
  64. return abs( dX ) <= radius && abs( dY ) <= radius;
  65. }
  66. bool PCB_TARGET::HitTest( const BOX2I& aRect, bool aContained, int aAccuracy ) const
  67. {
  68. BOX2I arect = aRect;
  69. arect.Inflate( aAccuracy );
  70. if( aContained )
  71. return arect.Contains( GetBoundingBox() );
  72. else
  73. return GetBoundingBox().Intersects( arect );
  74. }
  75. void PCB_TARGET::Rotate( const VECTOR2I& aRotCentre, const EDA_ANGLE& aAngle )
  76. {
  77. RotatePoint( m_pos, aRotCentre, aAngle );
  78. }
  79. void PCB_TARGET::Flip( const VECTOR2I& aCentre, FLIP_DIRECTION aFlipDirection )
  80. {
  81. if( aFlipDirection == FLIP_DIRECTION::LEFT_RIGHT )
  82. m_pos.x = aCentre.x - ( m_pos.x - aCentre.x );
  83. else
  84. m_pos.y = aCentre.y - ( m_pos.y - aCentre.y );
  85. SetLayer( GetBoard()->FlipLayer( GetLayer() ) );
  86. }
  87. const BOX2I PCB_TARGET::GetBoundingBox() const
  88. {
  89. BOX2I bBox;
  90. bBox.SetX( m_pos.x - m_size / 2 );
  91. bBox.SetY( m_pos.y - m_size / 2 );
  92. bBox.SetWidth( m_size );
  93. bBox.SetHeight( m_size );
  94. return bBox;
  95. }
  96. std::shared_ptr<SHAPE> PCB_TARGET::GetEffectiveShape( PCB_LAYER_ID aLayer, FLASHING aFlash ) const
  97. {
  98. return std::make_shared<SHAPE_CIRCLE>( m_pos, m_size / 2 );
  99. }
  100. wxString PCB_TARGET::GetItemDescription( UNITS_PROVIDER* aUnitsProvider, bool aFull ) const
  101. {
  102. // Targets are on *every* layer by definition
  103. return _( "Target" );
  104. }
  105. BITMAPS PCB_TARGET::GetMenuImage() const
  106. {
  107. return BITMAPS::add_pcb_target;
  108. }
  109. EDA_ITEM* PCB_TARGET::Clone() const
  110. {
  111. return new PCB_TARGET( *this );
  112. }
  113. void PCB_TARGET::swapData( BOARD_ITEM* aImage )
  114. {
  115. assert( aImage->Type() == PCB_TARGET_T );
  116. std::swap( *((PCB_TARGET*) this), *((PCB_TARGET*) aImage) );
  117. }
  118. void PCB_TARGET::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList )
  119. {
  120. aList.emplace_back( _( "PCB Target" ), wxEmptyString );
  121. aList.emplace_back( _( "Layer" ), GetLayerName() );
  122. aList.emplace_back( _( "Size" ), aFrame->MessageTextFromValue( GetSize() ) );
  123. aList.emplace_back( _( "Width" ), aFrame->MessageTextFromValue( GetWidth() ) );
  124. aList.emplace_back( _( "Shape" ), GetShape() == 0 ? wxT( "+" ) : wxT( "X" ) );
  125. }
  126. void PCB_TARGET::TransformShapeToPolygon( SHAPE_POLY_SET& aBuffer, PCB_LAYER_ID aLayer,
  127. int aClearance, int aError, ERROR_LOC aErrorLoc,
  128. bool ignoreLineWidth ) const
  129. {
  130. int size = GetShape() ? GetSize() / 1.5 : GetSize() / 2.0;
  131. int radius = GetShape() ? GetSize() / 2.0 : GetSize() / 3.0;
  132. PCB_SHAPE line1, line2;
  133. PCB_SHAPE circle( nullptr, SHAPE_T::CIRCLE );
  134. line1.SetStart( VECTOR2I( -size, 0.0 ) );
  135. line1.SetEnd( VECTOR2I( size, 0.0 ) );
  136. line2.SetStart( VECTOR2I( 0.0, -size ) );
  137. line2.SetEnd( VECTOR2I( 0.0, size ) );
  138. circle.SetEndX( radius );
  139. if( GetShape() ) // shape x
  140. {
  141. line1.Rotate( VECTOR2I(0,0), EDA_ANGLE( 45.0, DEGREES_T ) );
  142. line2.Rotate( VECTOR2I(0,0), EDA_ANGLE( 45.0, DEGREES_T ) );
  143. }
  144. for( PCB_SHAPE* item: { &line1, &line2, &circle } )
  145. {
  146. item->SetWidth( GetWidth() );
  147. item->Move( GetPosition() );
  148. item->TransformShapeToPolygon( aBuffer, aLayer, aClearance, aError, aErrorLoc,
  149. ignoreLineWidth );
  150. }
  151. }
  152. bool PCB_TARGET::operator==( const BOARD_ITEM& aBoardItem ) const
  153. {
  154. if( aBoardItem.Type() != Type() )
  155. return false;
  156. const PCB_TARGET& other = static_cast<const PCB_TARGET&>( aBoardItem );
  157. return *this == other;
  158. }
  159. bool PCB_TARGET::operator==( const PCB_TARGET& aOther ) const
  160. {
  161. return m_shape == aOther.m_shape
  162. && m_size == aOther.m_size
  163. && m_lineWidth == aOther.m_lineWidth
  164. && m_layer == aOther.m_layer
  165. && m_pos == aOther.m_pos;
  166. }
  167. double PCB_TARGET::Similarity( const BOARD_ITEM& aOther ) const
  168. {
  169. if( aOther.Type() != Type() )
  170. return 0.0;
  171. const PCB_TARGET& other = static_cast<const PCB_TARGET&>( aOther );
  172. double similarity = 1.0;
  173. if( GetShape() != other.GetShape() )
  174. similarity *= 0.9;
  175. if( GetSize() != other.GetSize() )
  176. similarity *= 0.9;
  177. if( GetWidth() != other.GetWidth() )
  178. similarity *= 0.9;
  179. if( GetLayer() != other.GetLayer() )
  180. similarity *= 0.9;
  181. if( GetPosition() != other.GetPosition() )
  182. similarity *= 0.9;
  183. return similarity;
  184. }
  185. static struct PCB_TARGET_DESC
  186. {
  187. PCB_TARGET_DESC()
  188. {
  189. PROPERTY_MANAGER& propMgr = PROPERTY_MANAGER::Instance();
  190. REGISTER_TYPE( PCB_TARGET );
  191. propMgr.InheritsAfter( TYPE_HASH( PCB_TARGET ), TYPE_HASH( BOARD_ITEM ) );
  192. propMgr.AddProperty( new PROPERTY<PCB_TARGET, int>( _HKI( "Size" ),
  193. &PCB_TARGET::SetSize, &PCB_TARGET::GetSize, PROPERTY_DISPLAY::PT_SIZE ) );
  194. propMgr.AddProperty( new PROPERTY<PCB_TARGET, int>( _HKI( "Width" ),
  195. &PCB_TARGET::SetWidth, &PCB_TARGET::GetWidth, PROPERTY_DISPLAY::PT_SIZE ) );
  196. auto shape = new PROPERTY<PCB_TARGET, int>( _HKI( "Shape" ),
  197. &PCB_TARGET::SetShape, &PCB_TARGET::GetShape );
  198. // TODO change the integer to an enum?
  199. //shape->SetValues( { { 0, _HKI( "Cross" ) }, { 1, ( "Plus" ) } } );
  200. propMgr.AddProperty( shape );
  201. }
  202. } _PCB_TARGET_DESC;