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.

287 lines
7.7 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. return false;
  51. }
  52. bool LIB_CIRCLE::HitTest( const EDA_RECT& aRect, bool aContained, int aAccuracy ) const
  53. {
  54. if( m_flags & (STRUCT_DELETED | SKIP_STRUCT ) )
  55. return false;
  56. wxPoint center = DefaultTransform.TransformCoordinate( GetPosition() );
  57. int radius = GetRadius();
  58. int lineWidth = GetWidth();
  59. EDA_RECT sel = aRect ;
  60. if ( aAccuracy )
  61. sel.Inflate( aAccuracy );
  62. if( aContained )
  63. return sel.Contains( GetBoundingBox() );
  64. // If the rectangle does not intersect the bounding box, this is a much quicker test
  65. if( !sel.Intersects( GetBoundingBox() ) )
  66. return false;
  67. else
  68. return sel.IntersectsCircleEdge( center, radius, lineWidth );
  69. }
  70. EDA_ITEM* LIB_CIRCLE::Clone() const
  71. {
  72. return new LIB_CIRCLE( *this );
  73. }
  74. int LIB_CIRCLE::compare( const LIB_ITEM& aOther, LIB_ITEM::COMPARE_FLAGS aCompareFlags ) const
  75. {
  76. wxASSERT( aOther.Type() == LIB_CIRCLE_T );
  77. int retv = LIB_ITEM::compare( aOther, aCompareFlags );
  78. if( retv )
  79. return retv;
  80. const LIB_CIRCLE* tmp = ( LIB_CIRCLE* ) &aOther;
  81. if( m_Pos.x != tmp->m_Pos.x )
  82. return m_Pos.x - tmp->m_Pos.x;
  83. if( m_Pos.y != tmp->m_Pos.y )
  84. return m_Pos.y - tmp->m_Pos.y;
  85. if( m_EndPos.x != tmp->m_EndPos.x )
  86. return m_EndPos.x - tmp->m_EndPos.x;
  87. if( m_EndPos.y != tmp->m_EndPos.y )
  88. return m_EndPos.y - tmp->m_EndPos.y;
  89. return 0;
  90. }
  91. void LIB_CIRCLE::Offset( const wxPoint& aOffset )
  92. {
  93. m_Pos += aOffset;
  94. m_EndPos += aOffset;
  95. }
  96. void LIB_CIRCLE::MoveTo( const wxPoint& aPosition )
  97. {
  98. Offset( aPosition - m_Pos );
  99. }
  100. void LIB_CIRCLE::MirrorHorizontal( const wxPoint& aCenter )
  101. {
  102. m_Pos.x -= aCenter.x;
  103. m_Pos.x *= -1;
  104. m_Pos.x += aCenter.x;
  105. m_EndPos.x -= aCenter.x;
  106. m_EndPos.x *= -1;
  107. m_EndPos.x += aCenter.x;
  108. }
  109. void LIB_CIRCLE::MirrorVertical( const wxPoint& aCenter )
  110. {
  111. m_Pos.y -= aCenter.y;
  112. m_Pos.y *= -1;
  113. m_Pos.y += aCenter.y;
  114. m_EndPos.y -= aCenter.y;
  115. m_EndPos.y *= -1;
  116. m_EndPos.y += aCenter.y;
  117. }
  118. void LIB_CIRCLE::Rotate( const wxPoint& aCenter, bool aRotateCCW )
  119. {
  120. int rot_angle = aRotateCCW ? -900 : 900;
  121. RotatePoint( &m_Pos, aCenter, rot_angle );
  122. RotatePoint( &m_EndPos, aCenter, rot_angle );
  123. }
  124. void LIB_CIRCLE::Plot( PLOTTER* aPlotter, const wxPoint& aOffset, bool aFill,
  125. const TRANSFORM& aTransform )
  126. {
  127. wxPoint pos = aTransform.TransformCoordinate( m_Pos ) + aOffset;
  128. if( aFill && m_Fill == FILL_TYPE::FILLED_WITH_BG_BODYCOLOR )
  129. {
  130. aPlotter->SetColor( aPlotter->RenderSettings()->GetLayerColor( LAYER_DEVICE_BACKGROUND ) );
  131. aPlotter->Circle( pos, GetRadius() * 2, FILL_TYPE::FILLED_WITH_BG_BODYCOLOR, 0 );
  132. }
  133. bool already_filled = m_Fill == FILL_TYPE::FILLED_WITH_BG_BODYCOLOR;
  134. int pen_size = GetPenWidth();
  135. if( !already_filled || pen_size > 0 )
  136. {
  137. pen_size = std::max( pen_size, aPlotter->RenderSettings()->GetMinPenWidth() );
  138. aPlotter->SetColor( aPlotter->RenderSettings()->GetLayerColor( LAYER_DEVICE ) );
  139. aPlotter->Circle(
  140. pos, GetRadius() * 2, already_filled ? FILL_TYPE::NO_FILL : m_Fill, pen_size );
  141. }
  142. }
  143. int LIB_CIRCLE::GetPenWidth() const
  144. {
  145. // Historically 0 meant "default width" and negative numbers meant "don't stroke".
  146. if( m_Width < 0 && GetFillMode() != FILL_TYPE::NO_FILL )
  147. return 0;
  148. else
  149. return std::max( m_Width, 1 );
  150. }
  151. void LIB_CIRCLE::print( RENDER_SETTINGS* aSettings, const wxPoint& aOffset, void* aData,
  152. const TRANSFORM& aTransform )
  153. {
  154. bool forceNoFill = static_cast<bool>( aData );
  155. int penWidth = GetPenWidth();
  156. if( forceNoFill && m_Fill != FILL_TYPE::NO_FILL && penWidth == 0 )
  157. return;
  158. wxDC* DC = aSettings->GetPrintDC();
  159. wxPoint pos1 = aTransform.TransformCoordinate( m_Pos ) + aOffset;
  160. COLOR4D color = aSettings->GetLayerColor( LAYER_DEVICE );
  161. if( forceNoFill || m_Fill == FILL_TYPE::NO_FILL )
  162. {
  163. penWidth = std::max( penWidth, aSettings->GetDefaultPenWidth() );
  164. GRCircle( nullptr, DC, pos1.x, pos1.y, GetRadius(), penWidth, color );
  165. }
  166. else
  167. {
  168. if( m_Fill == FILL_TYPE::FILLED_WITH_BG_BODYCOLOR )
  169. color = aSettings->GetLayerColor( LAYER_DEVICE_BACKGROUND );
  170. GRFilledCircle( nullptr, DC, pos1.x, pos1.y, GetRadius(), 0, color, color );
  171. }
  172. }
  173. const EDA_RECT LIB_CIRCLE::GetBoundingBox() const
  174. {
  175. EDA_RECT rect;
  176. int radius = GetRadius();
  177. rect.SetOrigin( m_Pos.x - radius, m_Pos.y - radius );
  178. rect.SetEnd( m_Pos.x + radius, m_Pos.y + radius );
  179. rect.Inflate( ( GetPenWidth() / 2 ) + 1 );
  180. rect.RevertYAxis();
  181. return rect;
  182. }
  183. void LIB_CIRCLE::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, MSG_PANEL_ITEMS& aList )
  184. {
  185. wxString msg;
  186. EDA_RECT bBox = GetBoundingBox();
  187. LIB_ITEM::GetMsgPanelInfo( aFrame, aList );
  188. msg = MessageTextFromValue( aFrame->GetUserUnits(), m_Width );
  189. aList.push_back( MSG_PANEL_ITEM( _( "Line Width" ), msg ) );
  190. msg = MessageTextFromValue( aFrame->GetUserUnits(), GetRadius() );
  191. aList.push_back( MSG_PANEL_ITEM( _( "Radius" ), msg ) );
  192. msg.Printf( wxT( "(%d, %d, %d, %d)" ),
  193. bBox.GetOrigin().x,
  194. bBox.GetOrigin().y,
  195. bBox.GetEnd().x,
  196. bBox.GetEnd().y );
  197. aList.push_back( MSG_PANEL_ITEM( _( "Bounding Box" ), msg ) );
  198. }
  199. wxString LIB_CIRCLE::GetSelectMenuText( EDA_UNITS aUnits ) const
  200. {
  201. return wxString::Format( _( "Circle, radius %s" ),
  202. MessageTextFromValue( aUnits, GetRadius() ) );
  203. }
  204. BITMAP_DEF LIB_CIRCLE::GetMenuImage() const
  205. {
  206. return add_circle_xpm;
  207. }
  208. void LIB_CIRCLE::BeginEdit( const wxPoint aPosition )
  209. {
  210. m_Pos = aPosition;
  211. }
  212. void LIB_CIRCLE::CalcEdit( const wxPoint& aPosition )
  213. {
  214. SetEnd( aPosition );
  215. }