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.

346 lines
10 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2015 Jean-Pierre Charras, jean-pierre.charras@ujf-grenoble.fr
  5. * Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
  6. * Copyright (C) 2012 Wayne Stambaugh <stambaughw@verizon.net>
  7. * Copyright (C) 1992-2015 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 <core/wx_stl_compat.h>
  27. #include <bitmaps.h>
  28. #include <core/mirror.h>
  29. #include <macros.h>
  30. #include <math/util.h> // for KiROUND
  31. #include <settings/color_settings.h>
  32. #include <settings/settings_manager.h>
  33. #include <pcb_edit_frame.h>
  34. #include <footprint.h>
  35. #include <fp_shape.h>
  36. #include <view/view.h>
  37. FP_SHAPE::FP_SHAPE( FOOTPRINT* parent, PCB_SHAPE_TYPE_T aShape ) :
  38. PCB_SHAPE( parent, PCB_FP_SHAPE_T )
  39. {
  40. m_shape = aShape;
  41. m_angle = 0;
  42. m_layer = F_SilkS;
  43. }
  44. FP_SHAPE::~FP_SHAPE()
  45. {
  46. }
  47. void FP_SHAPE::SetLocalCoord()
  48. {
  49. FOOTPRINT* fp = static_cast<FOOTPRINT*>( m_parent );
  50. if( fp == NULL )
  51. {
  52. m_Start0 = m_start;
  53. m_End0 = m_end;
  54. m_ThirdPoint0 = m_thirdPoint;
  55. m_Bezier0_C1 = m_bezierC1;
  56. m_Bezier0_C2 = m_bezierC2;
  57. return;
  58. }
  59. m_Start0 = m_start - fp->GetPosition();
  60. m_End0 = m_end - fp->GetPosition();
  61. m_ThirdPoint0 = m_thirdPoint - fp->GetPosition();
  62. m_Bezier0_C1 = m_bezierC1 - fp->GetPosition();
  63. m_Bezier0_C2 = m_bezierC2 - fp->GetPosition();
  64. double angle = fp->GetOrientation();
  65. RotatePoint( &m_Start0.x, &m_Start0.y, -angle );
  66. RotatePoint( &m_End0.x, &m_End0.y, -angle );
  67. RotatePoint( &m_ThirdPoint0.x, &m_ThirdPoint0.y, -angle );
  68. RotatePoint( &m_Bezier0_C1.x, &m_Bezier0_C1.y, -angle );
  69. RotatePoint( &m_Bezier0_C2.x, &m_Bezier0_C2.y, -angle );
  70. }
  71. void FP_SHAPE::SetDrawCoord()
  72. {
  73. FOOTPRINT* fp = static_cast<FOOTPRINT*>( m_parent );
  74. m_start = m_Start0;
  75. m_end = m_End0;
  76. m_thirdPoint = m_ThirdPoint0;
  77. m_bezierC1 = m_Bezier0_C1;
  78. m_bezierC2 = m_Bezier0_C2;
  79. if( fp )
  80. {
  81. RotatePoint( &m_start.x, &m_start.y, fp->GetOrientation() );
  82. RotatePoint( &m_end.x, &m_end.y, fp->GetOrientation() );
  83. RotatePoint( &m_thirdPoint.x, &m_thirdPoint.y, fp->GetOrientation() );
  84. RotatePoint( &m_bezierC1.x, &m_bezierC1.y, fp->GetOrientation() );
  85. RotatePoint( &m_bezierC2.x, &m_bezierC2.y, fp->GetOrientation() );
  86. m_start += fp->GetPosition();
  87. m_end += fp->GetPosition();
  88. m_thirdPoint += fp->GetPosition();
  89. m_bezierC1 += fp->GetPosition();
  90. m_bezierC2 += fp->GetPosition();
  91. }
  92. RebuildBezierToSegmentsPointsList( m_width );
  93. }
  94. void FP_SHAPE::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList )
  95. {
  96. FOOTPRINT* fp = static_cast<FOOTPRINT*>( m_parent );
  97. aList.emplace_back( _( "Footprint" ), fp ? fp->GetReference() : _( "<invalid>" ) );
  98. // append the features shared with the base class
  99. PCB_SHAPE::GetMsgPanelInfo( aFrame, aList );
  100. }
  101. wxString FP_SHAPE::GetSelectMenuText( EDA_UNITS aUnits ) const
  102. {
  103. return wxString::Format( _( "%s on %s" ),
  104. ShowShape( m_shape ),
  105. GetLayerName() );
  106. }
  107. BITMAPS FP_SHAPE::GetMenuImage() const
  108. {
  109. return BITMAPS::show_mod_edge;
  110. }
  111. EDA_ITEM* FP_SHAPE::Clone() const
  112. {
  113. return new FP_SHAPE( *this );
  114. }
  115. void FP_SHAPE::SetAngle( double aAngle, bool aUpdateEnd )
  116. {
  117. // Mark as depreciated.
  118. // m_Angle does not define the arc anymore
  119. // Update the parent class (updates the global m_ThirdPoint)
  120. PCB_SHAPE::SetAngle( aAngle, aUpdateEnd );
  121. // Also update the local m_ThirdPoint0 if requested
  122. if( aUpdateEnd )
  123. {
  124. m_ThirdPoint0 = m_End0;
  125. RotatePoint( &m_ThirdPoint0, m_Start0, -m_angle );
  126. }
  127. }
  128. void FP_SHAPE::Flip( const wxPoint& aCentre, bool aFlipLeftRight )
  129. {
  130. wxPoint pt( 0, 0 );
  131. switch( GetShape() )
  132. {
  133. case S_ARC:
  134. // Update arc angle but do not yet update m_ThirdPoint0 and m_thirdPoint,
  135. // arc center and start point must be updated before calculation arc end.
  136. SetAngle( -GetAngle(), false );
  137. KI_FALLTHROUGH;
  138. default:
  139. case S_SEGMENT:
  140. case S_CURVE:
  141. // If Start0 and Start are equal (ie: Footprint Editor), then flip both sets around the
  142. // centre point.
  143. if( m_start == m_Start0 )
  144. pt = aCentre;
  145. if( aFlipLeftRight )
  146. {
  147. MIRROR( m_start.x, aCentre.x );
  148. MIRROR( m_end.x, aCentre.x );
  149. MIRROR( m_thirdPoint.x, aCentre.x );
  150. MIRROR( m_bezierC1.x, aCentre.x );
  151. MIRROR( m_bezierC2.x, aCentre.x );
  152. MIRROR( m_Start0.x, pt.x );
  153. MIRROR( m_End0.x, pt.x );
  154. MIRROR( m_ThirdPoint0.x, pt.x );
  155. MIRROR( m_Bezier0_C1.x, pt.x );
  156. MIRROR( m_Bezier0_C2.x, pt.x );
  157. }
  158. else
  159. {
  160. MIRROR( m_start.y, aCentre.y );
  161. MIRROR( m_end.y, aCentre.y );
  162. MIRROR( m_thirdPoint.y, aCentre.y );
  163. MIRROR( m_bezierC1.y, aCentre.y );
  164. MIRROR( m_bezierC2.y, aCentre.y );
  165. MIRROR( m_Start0.y, pt.y );
  166. MIRROR( m_End0.y, pt.y );
  167. MIRROR( m_ThirdPoint0.y, pt.y );
  168. MIRROR( m_Bezier0_C1.y, pt.y );
  169. MIRROR( m_Bezier0_C2.y, pt.y );
  170. }
  171. RebuildBezierToSegmentsPointsList( m_width );
  172. break;
  173. case S_POLYGON:
  174. // polygon corners coordinates are relative to the footprint position, orientation 0
  175. m_poly.Mirror( aFlipLeftRight, !aFlipLeftRight );
  176. break;
  177. }
  178. SetLayer( FlipLayer( GetLayer(), GetBoard()->GetCopperLayerCount() ) );
  179. }
  180. bool FP_SHAPE::IsParentFlipped() const
  181. {
  182. if( GetParent() && GetParent()->GetLayer() == B_Cu )
  183. return true;
  184. return false;
  185. }
  186. void FP_SHAPE::Mirror( const wxPoint& aCentre, bool aMirrorAroundXAxis )
  187. {
  188. // Mirror an edge of the footprint. the layer is not modified
  189. // This is a footprint shape modification.
  190. switch( GetShape() )
  191. {
  192. case S_ARC:
  193. // Update arc angle but do not yet update m_ThirdPoint0 and m_thirdPoint,
  194. // arc center and start point must be updated before calculation arc end.
  195. SetAngle( -GetAngle(), false );
  196. KI_FALLTHROUGH;
  197. default:
  198. case S_CURVE:
  199. case S_SEGMENT:
  200. if( aMirrorAroundXAxis )
  201. {
  202. MIRROR( m_Start0.y, aCentre.y );
  203. MIRROR( m_End0.y, aCentre.y );
  204. MIRROR( m_ThirdPoint0.y, aCentre.y );
  205. MIRROR( m_Bezier0_C1.y, aCentre.y );
  206. MIRROR( m_Bezier0_C2.y, aCentre.y );
  207. }
  208. else
  209. {
  210. MIRROR( m_Start0.x, aCentre.x );
  211. MIRROR( m_End0.x, aCentre.x );
  212. MIRROR( m_ThirdPoint0.x, aCentre.x );
  213. MIRROR( m_Bezier0_C1.x, aCentre.x );
  214. MIRROR( m_Bezier0_C2.x, aCentre.x );
  215. }
  216. for( unsigned ii = 0; ii < m_bezierPoints.size(); ii++ )
  217. {
  218. if( aMirrorAroundXAxis )
  219. MIRROR( m_bezierPoints[ii].y, aCentre.y );
  220. else
  221. MIRROR( m_bezierPoints[ii].x, aCentre.x );
  222. }
  223. break;
  224. case S_POLYGON:
  225. // polygon corners coordinates are always relative to the
  226. // footprint position, orientation 0
  227. m_poly.Mirror( !aMirrorAroundXAxis, aMirrorAroundXAxis );
  228. break;
  229. }
  230. SetDrawCoord();
  231. }
  232. void FP_SHAPE::Rotate( const wxPoint& aRotCentre, double aAngle )
  233. {
  234. // We should rotate the relative coordinates, but to avoid duplicate code do the base class
  235. // rotation of draw coordinates, which is acceptable because in the footprint editor
  236. // m_Pos0 = m_Pos
  237. PCB_SHAPE::Rotate( aRotCentre, aAngle );
  238. // and now update the relative coordinates, which are the reference in most transforms.
  239. SetLocalCoord();
  240. }
  241. void FP_SHAPE::Move( const wxPoint& aMoveVector )
  242. {
  243. // Move an edge of the footprint.
  244. // This is a footprint shape modification.
  245. m_Start0 += aMoveVector;
  246. m_End0 += aMoveVector;
  247. m_ThirdPoint0 += aMoveVector;
  248. m_Bezier0_C1 += aMoveVector;
  249. m_Bezier0_C2 += aMoveVector;
  250. switch( GetShape() )
  251. {
  252. default:
  253. break;
  254. case S_POLYGON:
  255. // polygon corners coordinates are always relative to the
  256. // footprint position, orientation 0
  257. m_poly.Move( VECTOR2I( aMoveVector ) );
  258. break;
  259. }
  260. SetDrawCoord();
  261. }
  262. double FP_SHAPE::ViewGetLOD( int aLayer, KIGFX::VIEW* aView ) const
  263. {
  264. constexpr double HIDE = std::numeric_limits<double>::max();
  265. if( !aView )
  266. return 0;
  267. // Handle Render tab switches
  268. if( !IsParentFlipped() && !aView->IsLayerVisible( LAYER_MOD_FR ) )
  269. return HIDE;
  270. if( IsParentFlipped() && !aView->IsLayerVisible( LAYER_MOD_BK ) )
  271. return HIDE;
  272. // Other layers are shown without any conditions
  273. return 0.0;
  274. }
  275. static struct FP_SHAPE_DESC
  276. {
  277. FP_SHAPE_DESC()
  278. {
  279. PROPERTY_MANAGER& propMgr = PROPERTY_MANAGER::Instance();
  280. REGISTER_TYPE( FP_SHAPE );
  281. propMgr.InheritsAfter( TYPE_HASH( FP_SHAPE ), TYPE_HASH( PCB_SHAPE ) );
  282. propMgr.AddProperty( new PROPERTY<FP_SHAPE, wxString>( _HKI( "Parent" ),
  283. NO_SETTER( FP_SHAPE, wxString ), &FP_SHAPE::GetParentAsString ) );
  284. }
  285. } _FP_SHAPE_DESC;