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.

523 lines
14 KiB

  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-2022 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 <plotters/plotter.h>
  26. #include <macros.h>
  27. #include <base_units.h>
  28. #include <widgets/msgpanel.h>
  29. #include <bitmaps.h>
  30. #include <eda_draw_frame.h>
  31. #include <general.h>
  32. #include <lib_shape.h>
  33. #include "plotters/plotter.h"
  34. LIB_SHAPE::LIB_SHAPE( LIB_SYMBOL* aParent, SHAPE_T aShape, int aLineWidth, FILL_T aFillType,
  35. KICAD_T aType ) :
  36. LIB_ITEM( aType, aParent ),
  37. EDA_SHAPE( aShape, aLineWidth, aFillType )
  38. {
  39. m_editState = 0;
  40. }
  41. bool LIB_SHAPE::HitTest( const VECTOR2I& aPosRef, int aAccuracy ) const
  42. {
  43. if( aAccuracy < schIUScale.MilsToIU( MINIMUM_SELECTION_DISTANCE ) )
  44. aAccuracy = schIUScale.MilsToIU( MINIMUM_SELECTION_DISTANCE );
  45. return hitTest( DefaultTransform.TransformCoordinate( aPosRef ), aAccuracy );
  46. }
  47. bool LIB_SHAPE::HitTest( const BOX2I& aRect, bool aContained, int aAccuracy ) const
  48. {
  49. if( m_flags & (STRUCT_DELETED | SKIP_STRUCT ) )
  50. return false;
  51. return hitTest( DefaultTransform.TransformCoordinate( aRect ), aContained, aAccuracy );
  52. }
  53. EDA_ITEM* LIB_SHAPE::Clone() const
  54. {
  55. return new LIB_SHAPE( *this );
  56. }
  57. int LIB_SHAPE::compare( const LIB_ITEM& aOther, int aCompareFlags ) const
  58. {
  59. int retv = LIB_ITEM::compare( aOther, aCompareFlags );
  60. if( retv )
  61. return retv;
  62. return EDA_SHAPE::Compare( &static_cast<const LIB_SHAPE&>( aOther ) );
  63. }
  64. void LIB_SHAPE::Offset( const VECTOR2I& aOffset )
  65. {
  66. move( aOffset );
  67. }
  68. void LIB_SHAPE::MoveTo( const VECTOR2I& aPosition )
  69. {
  70. setPosition( aPosition );
  71. }
  72. void LIB_SHAPE::Normalize()
  73. {
  74. if( GetShape() == SHAPE_T::RECT )
  75. {
  76. VECTOR2I size = GetEnd() - GetPosition();
  77. if( size.y > 0 )
  78. {
  79. SetStartY( GetStartY() + size.y );
  80. SetEndY( GetStartY() - size.y );
  81. }
  82. if( size.x < 0 )
  83. {
  84. SetStartX( GetStartX() + size.x );
  85. SetEndX( GetStartX() - size.x );
  86. }
  87. }
  88. }
  89. void LIB_SHAPE::MirrorHorizontal( const VECTOR2I& aCenter )
  90. {
  91. flip( aCenter, true );
  92. }
  93. void LIB_SHAPE::MirrorVertical( const VECTOR2I& aCenter )
  94. {
  95. flip( aCenter, false );
  96. }
  97. void LIB_SHAPE::Rotate( const VECTOR2I& aCenter, bool aRotateCCW )
  98. {
  99. EDA_ANGLE rot_angle = aRotateCCW ? -ANGLE_90 : ANGLE_90;
  100. rotate( aCenter, rot_angle );
  101. }
  102. void LIB_SHAPE::Plot( PLOTTER* aPlotter, bool aBackground, const VECTOR2I& aOffset,
  103. const TRANSFORM& aTransform, bool aDimmed ) const
  104. {
  105. if( IsPrivate() )
  106. return;
  107. VECTOR2I start = aTransform.TransformCoordinate( m_start ) + aOffset;
  108. VECTOR2I end = aTransform.TransformCoordinate( m_end ) + aOffset;
  109. VECTOR2I center = aTransform.TransformCoordinate( getCenter() ) + aOffset;
  110. static std::vector<VECTOR2I> cornerList;
  111. if( GetShape() == SHAPE_T::POLY )
  112. {
  113. const SHAPE_LINE_CHAIN& poly = m_poly.Outline( 0 );
  114. cornerList.clear();
  115. for( const VECTOR2I& pt : poly.CPoints() )
  116. cornerList.push_back( aTransform.TransformCoordinate( pt ) + aOffset );
  117. }
  118. else if( GetShape() == SHAPE_T::BEZIER )
  119. {
  120. cornerList.clear();
  121. for( const VECTOR2I& pt : m_bezierPoints )
  122. cornerList.push_back( aTransform.TransformCoordinate( pt ) + aOffset );
  123. }
  124. else if( GetShape() == SHAPE_T::ARC )
  125. {
  126. EDA_ANGLE t1, t2;
  127. CalcArcAngles( t1, t2 );
  128. // N.B. The order of evaluation is critical here as MapAngles will modify t1, t2
  129. // and the Normalize routine depends on these modifications for the correct output
  130. bool transformed = aTransform.MapAngles( &t1, &t2 );
  131. bool transformed2 = ( ( t1 - t2 ).Normalize180() > ANGLE_0 );
  132. if( transformed != transformed2 )
  133. std::swap( start, end );
  134. }
  135. int penWidth;
  136. COLOR4D color = GetStroke().GetColor();
  137. PLOT_DASH_TYPE lineStyle = GetStroke().GetPlotStyle();
  138. FILL_T fill = m_fill;
  139. if( aBackground )
  140. {
  141. if( !aPlotter->GetColorMode() )
  142. return;
  143. switch( m_fill )
  144. {
  145. case FILL_T::FILLED_SHAPE:
  146. return;
  147. case FILL_T::FILLED_WITH_COLOR:
  148. color = GetFillColor();
  149. break;
  150. case FILL_T::FILLED_WITH_BG_BODYCOLOR:
  151. color = aPlotter->RenderSettings()->GetLayerColor( LAYER_DEVICE_BACKGROUND );
  152. break;
  153. default:
  154. return;
  155. }
  156. penWidth = 0;
  157. lineStyle = PLOT_DASH_TYPE::SOLID;
  158. }
  159. else
  160. {
  161. if( !aPlotter->GetColorMode() || color == COLOR4D::UNSPECIFIED )
  162. color = aPlotter->RenderSettings()->GetLayerColor( LAYER_DEVICE );
  163. if( lineStyle == PLOT_DASH_TYPE::DEFAULT )
  164. lineStyle = PLOT_DASH_TYPE::SOLID;
  165. if( m_fill == FILL_T::FILLED_SHAPE )
  166. fill = m_fill;
  167. else
  168. fill = FILL_T::NO_FILL;
  169. penWidth = GetEffectivePenWidth( aPlotter->RenderSettings() );
  170. }
  171. COLOR4D bg = aPlotter->RenderSettings()->GetBackgroundColor();
  172. if( bg == COLOR4D::UNSPECIFIED || !aPlotter->GetColorMode() )
  173. bg = COLOR4D::WHITE;
  174. if( aDimmed )
  175. color = color.Mix( bg, 0.5f );
  176. aPlotter->SetColor( color );
  177. aPlotter->SetDash( penWidth, lineStyle );
  178. switch( GetShape() )
  179. {
  180. case SHAPE_T::ARC:
  181. {
  182. // In some plotters (not all) the arc is approximated by segments, and
  183. // a error max is needed. We try to approximate by 360/5 segments by 360 deg
  184. int arc2segment_error = CircleToEndSegmentDeltaRadius( GetRadius(), 360/5 );
  185. aPlotter->Arc( center, start, end, fill, penWidth, arc2segment_error );
  186. }
  187. break;
  188. case SHAPE_T::CIRCLE:
  189. aPlotter->Circle( center, GetRadius() * 2, fill, penWidth );
  190. break;
  191. case SHAPE_T::RECT:
  192. aPlotter->Rect( start, end, fill, penWidth );
  193. break;
  194. case SHAPE_T::POLY:
  195. case SHAPE_T::BEZIER:
  196. aPlotter->PlotPoly( cornerList, fill, penWidth );
  197. break;
  198. default:
  199. UNIMPLEMENTED_FOR( SHAPE_T_asString() );
  200. }
  201. aPlotter->SetDash( penWidth, PLOT_DASH_TYPE::SOLID );
  202. }
  203. int LIB_SHAPE::GetPenWidth() const
  204. {
  205. return GetWidth();
  206. }
  207. void LIB_SHAPE::print( const RENDER_SETTINGS* aSettings, const VECTOR2I& aOffset, void* aData,
  208. const TRANSFORM& aTransform, bool aDimmed )
  209. {
  210. if( IsPrivate() )
  211. return;
  212. bool forceNoFill = static_cast<bool>( aData );
  213. int penWidth = GetEffectivePenWidth( aSettings );
  214. if( forceNoFill && IsFilled() && penWidth == 0 )
  215. return;
  216. wxDC* DC = aSettings->GetPrintDC();
  217. VECTOR2I pt1 = aTransform.TransformCoordinate( m_start ) + aOffset;
  218. VECTOR2I pt2 = aTransform.TransformCoordinate( m_end ) + aOffset;
  219. VECTOR2I c;
  220. COLOR4D color = GetStroke().GetColor();
  221. if( color == COLOR4D::UNSPECIFIED )
  222. color = aSettings->GetLayerColor( LAYER_DEVICE );
  223. COLOR4D bg = aSettings->GetBackgroundColor();
  224. if( bg == COLOR4D::UNSPECIFIED || GetGRForceBlackPenState() )
  225. bg = COLOR4D::WHITE;
  226. if( aDimmed )
  227. color = color.Mix( bg, 0.5f );
  228. unsigned ptCount = 0;
  229. VECTOR2I* buffer = nullptr;
  230. if( GetShape() == SHAPE_T::POLY )
  231. {
  232. const SHAPE_LINE_CHAIN& poly = m_poly.Outline( 0 );
  233. ptCount = poly.GetPointCount();
  234. buffer = new VECTOR2I[ptCount];
  235. for( unsigned ii = 0; ii < ptCount; ++ii )
  236. buffer[ii] = aTransform.TransformCoordinate( poly.CPoint( ii ) ) + aOffset;
  237. }
  238. else if( GetShape() == SHAPE_T::BEZIER )
  239. {
  240. ptCount = m_bezierPoints.size();
  241. buffer = new VECTOR2I[ptCount];
  242. for( size_t ii = 0; ii < ptCount; ++ii )
  243. buffer[ii] = aTransform.TransformCoordinate( m_bezierPoints[ii] ) + aOffset;
  244. }
  245. else if( GetShape() == SHAPE_T::ARC )
  246. {
  247. c = aTransform.TransformCoordinate( getCenter() ) + aOffset;
  248. EDA_ANGLE t1, t2;
  249. CalcArcAngles( t1, t2 );
  250. // N.B. The order of evaluation is critical here as MapAngles will modify t1, t2
  251. // and the Normalize routine depends on these modifications for the correct output
  252. bool transformed = aTransform.MapAngles( &t1, &t2 );
  253. bool transformed2 = ( ( t1 - t2 ).Normalize180() > ANGLE_0 );
  254. if( transformed == transformed2 )
  255. std::swap( pt1, pt2 );
  256. }
  257. COLOR4D fillColor = COLOR4D::UNSPECIFIED;
  258. if( !forceNoFill )
  259. {
  260. if( GetFillMode() == FILL_T::FILLED_SHAPE )
  261. fillColor = color;
  262. else if( GetFillMode() == FILL_T::FILLED_WITH_BG_BODYCOLOR )
  263. fillColor = aSettings->GetLayerColor( LAYER_DEVICE_BACKGROUND );
  264. else if( GetFillMode() == FILL_T::FILLED_WITH_COLOR )
  265. fillColor = GetFillColor();
  266. }
  267. if( fillColor != COLOR4D::UNSPECIFIED )
  268. {
  269. switch( GetShape() )
  270. {
  271. case SHAPE_T::ARC:
  272. GRFilledArc( DC, pt1, pt2, c, 0, fillColor, fillColor );
  273. break;
  274. case SHAPE_T::CIRCLE:
  275. GRFilledCircle( DC, pt1, GetRadius(), 0, fillColor, fillColor );
  276. break;
  277. case SHAPE_T::RECT:
  278. GRFilledRect( DC, pt1, pt2, 0, fillColor, fillColor );
  279. break;
  280. case SHAPE_T::POLY:
  281. GRPoly( DC, ptCount, buffer, true, 0, fillColor, fillColor );
  282. break;
  283. case SHAPE_T::BEZIER:
  284. GRPoly( DC, ptCount, buffer, true, 0, fillColor, fillColor );
  285. break;
  286. default:
  287. UNIMPLEMENTED_FOR( SHAPE_T_asString() );
  288. }
  289. }
  290. penWidth = std::max( penWidth, aSettings->GetDefaultPenWidth() );
  291. if( GetEffectiveLineStyle() == PLOT_DASH_TYPE::SOLID )
  292. {
  293. switch( GetShape() )
  294. {
  295. case SHAPE_T::ARC:
  296. GRArc( DC, pt1, pt2, c, penWidth, color );
  297. break;
  298. case SHAPE_T::CIRCLE:
  299. GRCircle( DC, pt1, GetRadius(), penWidth, color );
  300. break;
  301. case SHAPE_T::RECT:
  302. GRRect( DC, pt1, pt2, penWidth, color );
  303. break;
  304. case SHAPE_T::POLY:
  305. GRPoly( DC, ptCount, buffer, false, penWidth, color, color );
  306. break;
  307. case SHAPE_T::BEZIER:
  308. GRPoly( DC, ptCount, buffer, false, penWidth, color, color );
  309. break;
  310. default:
  311. UNIMPLEMENTED_FOR( SHAPE_T_asString() );
  312. }
  313. }
  314. else
  315. {
  316. std::vector<SHAPE*> shapes = MakeEffectiveShapes( true );
  317. for( SHAPE* shape : shapes )
  318. {
  319. STROKE_PARAMS::Stroke( shape, GetEffectiveLineStyle(), penWidth, aSettings,
  320. [&]( const VECTOR2I& a, const VECTOR2I& b )
  321. {
  322. VECTOR2I pts = aTransform.TransformCoordinate( a ) + aOffset;
  323. VECTOR2I pte = aTransform.TransformCoordinate( b ) + aOffset;
  324. GRLine( DC, pts.x, pts.y, pte.x, pte.y, penWidth, color );
  325. } );
  326. }
  327. for( SHAPE* shape : shapes )
  328. delete shape;
  329. }
  330. delete[] buffer;
  331. }
  332. const BOX2I LIB_SHAPE::GetBoundingBox() const
  333. {
  334. BOX2I bbox = getBoundingBox();
  335. bbox.RevertYAxis();
  336. return bbox;
  337. }
  338. void LIB_SHAPE::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList )
  339. {
  340. LIB_ITEM::GetMsgPanelInfo( aFrame, aList );
  341. ShapeGetMsgPanelInfo( aFrame, aList );
  342. }
  343. wxString LIB_SHAPE::GetSelectMenuText( UNITS_PROVIDER* aUnitsProvider ) const
  344. {
  345. switch( GetShape() )
  346. {
  347. case SHAPE_T::ARC:
  348. return wxString::Format( _( "Arc, radius %s" ),
  349. aUnitsProvider->MessageTextFromValue( GetRadius() ) );
  350. case SHAPE_T::CIRCLE:
  351. return wxString::Format( _( "Circle, radius %s" ),
  352. aUnitsProvider->MessageTextFromValue( GetRadius() ) );
  353. case SHAPE_T::RECT:
  354. return wxString::Format( _( "Rectangle, width %s height %s" ),
  355. aUnitsProvider->MessageTextFromValue( std::abs( m_start.x - m_end.x ) ),
  356. aUnitsProvider->MessageTextFromValue( std::abs( m_start.y - m_end.y ) ) );
  357. case SHAPE_T::POLY:
  358. return wxString::Format( _( "Polyline, %d points" ),
  359. int( m_poly.Outline( 0 ).GetPointCount() ) );
  360. case SHAPE_T::BEZIER:
  361. return wxString::Format( _( "Bezier Curve, %d points" ),
  362. int( m_bezierPoints.size() ) );
  363. default:
  364. UNIMPLEMENTED_FOR( SHAPE_T_asString() );
  365. return wxEmptyString;
  366. }
  367. }
  368. BITMAPS LIB_SHAPE::GetMenuImage() const
  369. {
  370. switch( GetShape() )
  371. {
  372. case SHAPE_T::SEGMENT: return BITMAPS::add_line;
  373. case SHAPE_T::ARC: return BITMAPS::add_arc;
  374. case SHAPE_T::CIRCLE: return BITMAPS::add_circle;
  375. case SHAPE_T::RECT: return BITMAPS::add_rectangle;
  376. case SHAPE_T::POLY: return BITMAPS::add_graphical_segments;
  377. default:
  378. UNIMPLEMENTED_FOR( SHAPE_T_asString() );
  379. return BITMAPS::question_mark;
  380. }
  381. }
  382. void LIB_SHAPE::AddPoint( const VECTOR2I& aPosition )
  383. {
  384. if( GetShape() == SHAPE_T::POLY )
  385. {
  386. if( m_poly.IsEmpty() )
  387. m_poly.NewOutline();
  388. m_poly.Outline( 0 ).Append( aPosition, true );
  389. }
  390. else
  391. {
  392. UNIMPLEMENTED_FOR( SHAPE_T_asString() );
  393. }
  394. }
  395. void LIB_SHAPE::ViewGetLayers( int aLayers[], int& aCount ) const
  396. {
  397. aCount = 3;
  398. aLayers[0] = IsPrivate() ? LAYER_PRIVATE_NOTES : LAYER_DEVICE;
  399. aLayers[1] = IsPrivate() ? LAYER_NOTES_BACKGROUND : LAYER_DEVICE_BACKGROUND;
  400. aLayers[2] = LAYER_SELECTION_SHADOWS;
  401. }