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.

149 lines
4.5 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2018 Jean-Pierre Charras, jp.charras at wanadoo.fr
  5. * Copyright (C) 2018-2020 KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. /**
  25. * @file marker_base.cpp
  26. * @brief Implementation of MARKER_BASE class.
  27. * Markers are used to show something (usually a drc/erc problem).
  28. * Markers in Pcbnew and Eeschema are derived from this base class.
  29. */
  30. #include "base_screen.h"
  31. #include "marker_base.h"
  32. #include <core/arraydim.h>
  33. #include <geometry/shape_line_chain.h>
  34. #include <render_settings.h>
  35. #include "dialog_display_info_HTML_base.h"
  36. /* The graphic shape of markers is a polygon.
  37. * MarkerShapeCorners contains the coordinates of corners of the polygonal default shape
  38. * they are arbitrary units to make coding shape easy.
  39. * internal units coordinates are these values scaled by .m_ScalingFactor
  40. */
  41. static const VECTOR2I MarkerShapeCorners[] =
  42. {
  43. VECTOR2I( 0, 0 ),
  44. VECTOR2I( 8, 1 ),
  45. VECTOR2I( 4, 3 ),
  46. VECTOR2I( 13, 8 ),
  47. VECTOR2I( 9, 9 ),
  48. VECTOR2I( 8, 13 ),
  49. VECTOR2I( 3, 4 ),
  50. VECTOR2I( 1, 8 ),
  51. VECTOR2I( 0, 0 )
  52. };
  53. const unsigned CORNERS_COUNT = arrayDim( MarkerShapeCorners );
  54. MARKER_BASE::MARKER_BASE( int aScalingFactor, std::shared_ptr<RC_ITEM> aItem, TYPEMARKER aType ) :
  55. m_markerType( aType ),
  56. m_excluded( false ),
  57. m_rcItem( aItem ),
  58. m_scalingFactor( aScalingFactor )
  59. {
  60. const VECTOR2I* point_shape = MarkerShapeCorners;
  61. wxPoint start( point_shape->x, point_shape->y );
  62. wxPoint end = start;
  63. for( unsigned ii = 1; ii < CORNERS_COUNT; ii++ )
  64. {
  65. ++point_shape;
  66. start.x = std::min( start.x, point_shape->x);
  67. start.y = std::min( start.y, point_shape->y);
  68. end.x = std::max( end.x, point_shape->x);
  69. end.y = std::max( end.y, point_shape->y);
  70. }
  71. m_shapeBoundingBox.SetOrigin( start);
  72. m_shapeBoundingBox.SetEnd( end);
  73. }
  74. MARKER_BASE::~MARKER_BASE()
  75. {
  76. }
  77. bool MARKER_BASE::HitTestMarker( const wxPoint& aHitPosition, int aAccuracy ) const
  78. {
  79. EDA_RECT bbox = GetBoundingBoxMarker();
  80. bbox.Inflate( aAccuracy );
  81. // Fast hit test using boundary box. A finer test will be made if requested
  82. bool hit = bbox.Contains( aHitPosition );
  83. if( hit ) // Fine test
  84. {
  85. SHAPE_LINE_CHAIN polygon;
  86. ShapeToPolygon( polygon );
  87. VECTOR2I rel_pos( aHitPosition - m_Pos );
  88. hit = polygon.PointInside( rel_pos, aAccuracy );
  89. }
  90. return hit;
  91. }
  92. void MARKER_BASE::ShapeToPolygon( SHAPE_LINE_CHAIN& aPolygon, int aScale ) const
  93. {
  94. if( aScale < 0 )
  95. aScale = MarkerScale();
  96. for( const VECTOR2I& corner : MarkerShapeCorners )
  97. aPolygon.Append( corner * aScale );
  98. // Be sure aPolygon is seen as a closed polyline:
  99. aPolygon.SetClosed( true );
  100. }
  101. EDA_RECT MARKER_BASE::GetBoundingBoxMarker() const
  102. {
  103. wxSize size_iu = m_shapeBoundingBox.GetSize();
  104. wxPoint position_iu = m_shapeBoundingBox.GetPosition();
  105. size_iu.x *= m_scalingFactor;
  106. size_iu.y *= m_scalingFactor;
  107. position_iu.x *= m_scalingFactor;
  108. position_iu.y *= m_scalingFactor;
  109. position_iu += m_Pos;
  110. return EDA_RECT( position_iu, size_iu );
  111. }
  112. void MARKER_BASE::PrintMarker( RENDER_SETTINGS* aSettings, const wxPoint& aOffset )
  113. {
  114. wxDC* DC = aSettings->GetPrintDC();
  115. // Build the marker shape polygon in internal units:
  116. std::vector<wxPoint> shape;
  117. shape.reserve( CORNERS_COUNT );
  118. for( const VECTOR2I& corner : MarkerShapeCorners )
  119. shape.emplace_back( corner * MarkerScale() + m_Pos + aOffset );
  120. GRClosedPoly( nullptr, DC, CORNERS_COUNT, &shape[0], true, 0, getColor(), getColor() );
  121. }