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.

290 lines
7.8 KiB

6 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2017 Jean-Pierre Charras, jp.charras at wanadoo.fr
  5. * Copyright (C) 2004-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. #include <sch_draw_panel.h>
  25. #include <plotter.h>
  26. #include <trigo.h>
  27. #include <base_units.h>
  28. #include <widgets/msgpanel.h>
  29. #include <bitmaps.h>
  30. #include <math/util.h> // for KiROUND
  31. #include <eda_draw_frame.h>
  32. #include <general.h>
  33. #include <lib_circle.h>
  34. #include <settings/color_settings.h>
  35. #include <transform.h>
  36. LIB_CIRCLE::LIB_CIRCLE( LIB_PART* aParent ) :
  37. LIB_ITEM( LIB_CIRCLE_T, aParent )
  38. {
  39. m_Width = 0;
  40. m_fill = FILL_TYPE::NO_FILL;
  41. m_isFillable = true;
  42. }
  43. bool LIB_CIRCLE::HitTest( const wxPoint& aPosRef, int aAccuracy ) const
  44. {
  45. int mindist = std::max( aAccuracy + GetPenWidth() / 2,
  46. Mils2iu( MINIMUM_SELECTION_DISTANCE ) );
  47. int dist = KiROUND( GetLineLength( aPosRef, DefaultTransform.TransformCoordinate( m_Pos ) ) );
  48. if( abs( dist - GetRadius() ) <= mindist )
  49. return true;
  50. if( m_fill == FILL_TYPE::NO_FILL )
  51. return false;
  52. return dist <= GetRadius();
  53. }
  54. bool LIB_CIRCLE::HitTest( const EDA_RECT& aRect, bool aContained, int aAccuracy ) const
  55. {
  56. if( m_flags & (STRUCT_DELETED | SKIP_STRUCT ) )
  57. return false;
  58. wxPoint center = DefaultTransform.TransformCoordinate( GetPosition() );
  59. int radius = GetRadius();
  60. int lineWidth = GetWidth();
  61. EDA_RECT sel = aRect ;
  62. if ( aAccuracy )
  63. sel.Inflate( aAccuracy );
  64. if( aContained )
  65. return sel.Contains( GetBoundingBox() );
  66. // If the rectangle does not intersect the bounding box, this is a much quicker test
  67. if( !sel.Intersects( GetBoundingBox() ) )
  68. return false;
  69. else
  70. return sel.IntersectsCircleEdge( center, radius, lineWidth );
  71. }
  72. EDA_ITEM* LIB_CIRCLE::Clone() const
  73. {
  74. return new LIB_CIRCLE( *this );
  75. }
  76. int LIB_CIRCLE::compare( const LIB_ITEM& aOther, LIB_ITEM::COMPARE_FLAGS aCompareFlags ) const
  77. {
  78. wxASSERT( aOther.Type() == LIB_CIRCLE_T );
  79. int retv = LIB_ITEM::compare( aOther, aCompareFlags );
  80. if( retv )
  81. return retv;
  82. const LIB_CIRCLE* tmp = ( LIB_CIRCLE* ) &aOther;
  83. if( m_Pos.x != tmp->m_Pos.x )
  84. return m_Pos.x - tmp->m_Pos.x;
  85. if( m_Pos.y != tmp->m_Pos.y )
  86. return m_Pos.y - tmp->m_Pos.y;
  87. if( m_EndPos.x != tmp->m_EndPos.x )
  88. return m_EndPos.x - tmp->m_EndPos.x;
  89. if( m_EndPos.y != tmp->m_EndPos.y )
  90. return m_EndPos.y - tmp->m_EndPos.y;
  91. return 0;
  92. }
  93. void LIB_CIRCLE::Offset( const wxPoint& aOffset )
  94. {
  95. m_Pos += aOffset;
  96. m_EndPos += aOffset;
  97. }
  98. void LIB_CIRCLE::MoveTo( const wxPoint& aPosition )
  99. {
  100. Offset( aPosition - m_Pos );
  101. }
  102. void LIB_CIRCLE::MirrorHorizontal( const wxPoint& aCenter )
  103. {
  104. m_Pos.x -= aCenter.x;
  105. m_Pos.x *= -1;
  106. m_Pos.x += aCenter.x;
  107. m_EndPos.x -= aCenter.x;
  108. m_EndPos.x *= -1;
  109. m_EndPos.x += aCenter.x;
  110. }
  111. void LIB_CIRCLE::MirrorVertical( const wxPoint& aCenter )
  112. {
  113. m_Pos.y -= aCenter.y;
  114. m_Pos.y *= -1;
  115. m_Pos.y += aCenter.y;
  116. m_EndPos.y -= aCenter.y;
  117. m_EndPos.y *= -1;
  118. m_EndPos.y += aCenter.y;
  119. }
  120. void LIB_CIRCLE::Rotate( const wxPoint& aCenter, bool aRotateCCW )
  121. {
  122. int rot_angle = aRotateCCW ? -900 : 900;
  123. RotatePoint( &m_Pos, aCenter, rot_angle );
  124. RotatePoint( &m_EndPos, aCenter, rot_angle );
  125. }
  126. void LIB_CIRCLE::Plot( PLOTTER* aPlotter, const wxPoint& aOffset, bool aFill,
  127. const TRANSFORM& aTransform )
  128. {
  129. wxPoint pos = aTransform.TransformCoordinate( m_Pos ) + aOffset;
  130. if( aFill && m_fill == FILL_TYPE::FILLED_WITH_BG_BODYCOLOR )
  131. {
  132. aPlotter->SetColor( aPlotter->RenderSettings()->GetLayerColor( LAYER_DEVICE_BACKGROUND ) );
  133. aPlotter->Circle( pos, GetRadius() * 2, FILL_TYPE::FILLED_WITH_BG_BODYCOLOR, 0 );
  134. }
  135. bool already_filled = m_fill == FILL_TYPE::FILLED_WITH_BG_BODYCOLOR;
  136. int pen_size = GetPenWidth();
  137. if( !already_filled || pen_size > 0 )
  138. {
  139. pen_size = std::max( pen_size, aPlotter->RenderSettings()->GetMinPenWidth() );
  140. aPlotter->SetColor( aPlotter->RenderSettings()->GetLayerColor( LAYER_DEVICE ) );
  141. aPlotter->Circle( pos, GetRadius() * 2, already_filled ? FILL_TYPE::NO_FILL : m_fill,
  142. pen_size );
  143. }
  144. }
  145. int LIB_CIRCLE::GetPenWidth() const
  146. {
  147. // Historically 0 meant "default width" and negative numbers meant "don't stroke".
  148. if( m_Width < 0 && GetFillMode() != FILL_TYPE::NO_FILL )
  149. return 0;
  150. else
  151. return std::max( m_Width, 1 );
  152. }
  153. void LIB_CIRCLE::print( const RENDER_SETTINGS* aSettings, const wxPoint& aOffset,
  154. void* aData, const TRANSFORM& aTransform )
  155. {
  156. bool forceNoFill = static_cast<bool>( aData );
  157. int penWidth = GetPenWidth();
  158. if( forceNoFill && m_fill != FILL_TYPE::NO_FILL && penWidth == 0 )
  159. return;
  160. wxDC* DC = aSettings->GetPrintDC();
  161. wxPoint pos1 = aTransform.TransformCoordinate( m_Pos ) + aOffset;
  162. COLOR4D color = aSettings->GetLayerColor( LAYER_DEVICE );
  163. if( forceNoFill || m_fill == FILL_TYPE::NO_FILL )
  164. {
  165. penWidth = std::max( penWidth, aSettings->GetDefaultPenWidth() );
  166. GRCircle( nullptr, DC, pos1.x, pos1.y, GetRadius(), penWidth, color );
  167. }
  168. else
  169. {
  170. if( m_fill == FILL_TYPE::FILLED_WITH_BG_BODYCOLOR )
  171. color = aSettings->GetLayerColor( LAYER_DEVICE_BACKGROUND );
  172. GRFilledCircle( nullptr, DC, pos1.x, pos1.y, GetRadius(), 0, color, color );
  173. }
  174. }
  175. const EDA_RECT LIB_CIRCLE::GetBoundingBox() const
  176. {
  177. EDA_RECT rect;
  178. int radius = GetRadius();
  179. rect.SetOrigin( m_Pos.x - radius, m_Pos.y - radius );
  180. rect.SetEnd( m_Pos.x + radius, m_Pos.y + radius );
  181. rect.Inflate( ( GetPenWidth() / 2 ) + 1 );
  182. rect.RevertYAxis();
  183. return rect;
  184. }
  185. void LIB_CIRCLE::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, MSG_PANEL_ITEMS& aList )
  186. {
  187. wxString msg;
  188. EDA_RECT bBox = GetBoundingBox();
  189. LIB_ITEM::GetMsgPanelInfo( aFrame, aList );
  190. msg = MessageTextFromValue( aFrame->GetUserUnits(), m_Width );
  191. aList.push_back( MSG_PANEL_ITEM( _( "Line Width" ), msg ) );
  192. msg = MessageTextFromValue( aFrame->GetUserUnits(), GetRadius() );
  193. aList.push_back( MSG_PANEL_ITEM( _( "Radius" ), msg ) );
  194. msg.Printf( wxT( "(%d, %d, %d, %d)" ),
  195. bBox.GetOrigin().x,
  196. bBox.GetOrigin().y,
  197. bBox.GetEnd().x,
  198. bBox.GetEnd().y );
  199. aList.push_back( MSG_PANEL_ITEM( _( "Bounding Box" ), msg ) );
  200. }
  201. wxString LIB_CIRCLE::GetSelectMenuText( EDA_UNITS aUnits ) const
  202. {
  203. return wxString::Format( _( "Circle, radius %s" ),
  204. MessageTextFromValue( aUnits, GetRadius() ) );
  205. }
  206. BITMAP_DEF LIB_CIRCLE::GetMenuImage() const
  207. {
  208. return add_circle_xpm;
  209. }
  210. void LIB_CIRCLE::BeginEdit( const wxPoint aPosition )
  211. {
  212. m_Pos = aPosition;
  213. }
  214. void LIB_CIRCLE::CalcEdit( const wxPoint& aPosition )
  215. {
  216. SetEnd( aPosition );
  217. }