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.

542 lines
15 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. EDA_ANGLE arc_angle = ( t1 - t2 ).Normalize180();
  132. bool transformed2 = ( arc_angle > ANGLE_0 ) && ( arc_angle < ANGLE_180 );
  133. if( transformed != transformed2 )
  134. std::swap( start, end );
  135. }
  136. int penWidth;
  137. COLOR4D color = GetStroke().GetColor();
  138. PLOT_DASH_TYPE lineStyle = GetStroke().GetPlotStyle();
  139. FILL_T fill = m_fill;
  140. if( aBackground )
  141. {
  142. if( !aPlotter->GetColorMode() )
  143. return;
  144. switch( m_fill )
  145. {
  146. case FILL_T::FILLED_SHAPE:
  147. return;
  148. case FILL_T::FILLED_WITH_COLOR:
  149. color = GetFillColor();
  150. break;
  151. case FILL_T::FILLED_WITH_BG_BODYCOLOR:
  152. color = aPlotter->RenderSettings()->GetLayerColor( LAYER_DEVICE_BACKGROUND );
  153. break;
  154. default:
  155. return;
  156. }
  157. penWidth = 0;
  158. lineStyle = PLOT_DASH_TYPE::SOLID;
  159. }
  160. else
  161. {
  162. if( !aPlotter->GetColorMode() || color == COLOR4D::UNSPECIFIED )
  163. color = aPlotter->RenderSettings()->GetLayerColor( LAYER_DEVICE );
  164. if( lineStyle == PLOT_DASH_TYPE::DEFAULT )
  165. lineStyle = PLOT_DASH_TYPE::SOLID;
  166. if( m_fill == FILL_T::FILLED_SHAPE )
  167. fill = m_fill;
  168. else
  169. fill = FILL_T::NO_FILL;
  170. penWidth = GetEffectivePenWidth( aPlotter->RenderSettings() );
  171. }
  172. COLOR4D bg = aPlotter->RenderSettings()->GetBackgroundColor();
  173. if( bg == COLOR4D::UNSPECIFIED || !aPlotter->GetColorMode() )
  174. bg = COLOR4D::WHITE;
  175. if( aDimmed )
  176. {
  177. color.Desaturate( );
  178. color = color.Mix( bg, 0.5f );
  179. }
  180. aPlotter->SetColor( color );
  181. aPlotter->SetDash( penWidth, lineStyle );
  182. switch( GetShape() )
  183. {
  184. case SHAPE_T::ARC:
  185. {
  186. // In some plotters (not all) the arc is approximated by segments, and
  187. // a error max is needed. We try to approximate by 360/5 segments by 360 deg
  188. int arc2segment_error = CircleToEndSegmentDeltaRadius( GetRadius(), 360/5 );
  189. aPlotter->Arc( center, start, end, fill, penWidth, arc2segment_error );
  190. }
  191. break;
  192. case SHAPE_T::CIRCLE:
  193. aPlotter->Circle( center, GetRadius() * 2, fill, penWidth );
  194. break;
  195. case SHAPE_T::RECT:
  196. aPlotter->Rect( start, end, fill, penWidth );
  197. break;
  198. case SHAPE_T::POLY:
  199. case SHAPE_T::BEZIER:
  200. aPlotter->PlotPoly( cornerList, fill, penWidth );
  201. break;
  202. default:
  203. UNIMPLEMENTED_FOR( SHAPE_T_asString() );
  204. }
  205. aPlotter->SetDash( penWidth, PLOT_DASH_TYPE::SOLID );
  206. }
  207. int LIB_SHAPE::GetPenWidth() const
  208. {
  209. return GetWidth();
  210. }
  211. void LIB_SHAPE::print( const RENDER_SETTINGS* aSettings, const VECTOR2I& aOffset, void* aData,
  212. const TRANSFORM& aTransform, bool aDimmed )
  213. {
  214. if( IsPrivate() )
  215. return;
  216. bool forceNoFill = static_cast<bool>( aData );
  217. int penWidth = GetEffectivePenWidth( aSettings );
  218. if( forceNoFill && IsFilled() && penWidth == 0 )
  219. return;
  220. wxDC* DC = aSettings->GetPrintDC();
  221. VECTOR2I pt1 = aTransform.TransformCoordinate( m_start ) + aOffset;
  222. VECTOR2I pt2 = aTransform.TransformCoordinate( m_end ) + aOffset;
  223. VECTOR2I c;
  224. COLOR4D color = GetStroke().GetColor();
  225. if( color == COLOR4D::UNSPECIFIED )
  226. color = aSettings->GetLayerColor( LAYER_DEVICE );
  227. COLOR4D bg = aSettings->GetBackgroundColor();
  228. if( bg == COLOR4D::UNSPECIFIED || GetGRForceBlackPenState() )
  229. bg = COLOR4D::WHITE;
  230. if( aDimmed )
  231. {
  232. color.Desaturate( );
  233. color = color.Mix( bg, 0.5f );
  234. }
  235. unsigned ptCount = 0;
  236. VECTOR2I* buffer = nullptr;
  237. if( GetShape() == SHAPE_T::POLY )
  238. {
  239. const SHAPE_LINE_CHAIN& poly = m_poly.Outline( 0 );
  240. ptCount = poly.GetPointCount();
  241. buffer = new VECTOR2I[ptCount];
  242. for( unsigned ii = 0; ii < ptCount; ++ii )
  243. buffer[ii] = aTransform.TransformCoordinate( poly.CPoint( ii ) ) + aOffset;
  244. }
  245. else if( GetShape() == SHAPE_T::BEZIER )
  246. {
  247. ptCount = m_bezierPoints.size();
  248. buffer = new VECTOR2I[ptCount];
  249. for( size_t ii = 0; ii < ptCount; ++ii )
  250. buffer[ii] = aTransform.TransformCoordinate( m_bezierPoints[ii] ) + aOffset;
  251. }
  252. else if( GetShape() == SHAPE_T::ARC )
  253. {
  254. c = aTransform.TransformCoordinate( getCenter() ) + aOffset;
  255. EDA_ANGLE t1, t2;
  256. CalcArcAngles( t1, t2 );
  257. // N.B. The order of evaluation is critical here as MapAngles will modify t1, t2
  258. // and the Normalize routine depends on these modifications for the correct output
  259. bool transformed = aTransform.MapAngles( &t1, &t2 );
  260. EDA_ANGLE arc_angle = ( t1 - t2 ).Normalize180();
  261. bool transformed2 = ( arc_angle > ANGLE_0 ) && ( arc_angle < ANGLE_180 );
  262. if( transformed == transformed2 )
  263. std::swap( pt1, pt2 );
  264. }
  265. COLOR4D fillColor = COLOR4D::UNSPECIFIED;
  266. if( !forceNoFill )
  267. {
  268. if( GetFillMode() == FILL_T::FILLED_SHAPE )
  269. fillColor = color;
  270. else if( GetFillMode() == FILL_T::FILLED_WITH_BG_BODYCOLOR )
  271. fillColor = aSettings->GetLayerColor( LAYER_DEVICE_BACKGROUND );
  272. else if( GetFillMode() == FILL_T::FILLED_WITH_COLOR )
  273. fillColor = GetFillColor();
  274. }
  275. if( fillColor != COLOR4D::UNSPECIFIED )
  276. {
  277. if( aDimmed )
  278. {
  279. fillColor.Desaturate( );
  280. fillColor = fillColor.Mix( bg, 0.5f );
  281. }
  282. switch( GetShape() )
  283. {
  284. case SHAPE_T::ARC:
  285. GRFilledArc( DC, pt1, pt2, c, 0, fillColor, fillColor );
  286. break;
  287. case SHAPE_T::CIRCLE:
  288. GRFilledCircle( DC, pt1, GetRadius(), 0, fillColor, fillColor );
  289. break;
  290. case SHAPE_T::RECT:
  291. GRFilledRect( DC, pt1, pt2, 0, fillColor, fillColor );
  292. break;
  293. case SHAPE_T::POLY:
  294. GRPoly( DC, ptCount, buffer, true, 0, fillColor, fillColor );
  295. break;
  296. case SHAPE_T::BEZIER:
  297. GRPoly( DC, ptCount, buffer, true, 0, fillColor, fillColor );
  298. break;
  299. default:
  300. UNIMPLEMENTED_FOR( SHAPE_T_asString() );
  301. }
  302. }
  303. else
  304. {
  305. penWidth = std::max( penWidth, aSettings->GetDefaultPenWidth() );
  306. }
  307. if( penWidth > 0 )
  308. {
  309. if( GetEffectiveLineStyle() == PLOT_DASH_TYPE::SOLID )
  310. {
  311. switch( GetShape() )
  312. {
  313. case SHAPE_T::ARC:
  314. GRArc( DC, pt1, pt2, c, penWidth, color );
  315. break;
  316. case SHAPE_T::CIRCLE:
  317. GRCircle( DC, pt1, GetRadius(), penWidth, color );
  318. break;
  319. case SHAPE_T::RECT:
  320. GRRect( DC, pt1, pt2, penWidth, color );
  321. break;
  322. case SHAPE_T::POLY:
  323. GRPoly( DC, ptCount, buffer, false, penWidth, color, color );
  324. break;
  325. case SHAPE_T::BEZIER:
  326. GRPoly( DC, ptCount, buffer, false, penWidth, color, color );
  327. break;
  328. default:
  329. UNIMPLEMENTED_FOR( SHAPE_T_asString() );
  330. }
  331. }
  332. else
  333. {
  334. std::vector<SHAPE*> shapes = MakeEffectiveShapes( true );
  335. for( SHAPE* shape : shapes )
  336. {
  337. STROKE_PARAMS::Stroke( shape, GetEffectiveLineStyle(), penWidth, aSettings,
  338. [&]( const VECTOR2I& a, const VECTOR2I& b )
  339. {
  340. VECTOR2I pts = aTransform.TransformCoordinate( a ) + aOffset;
  341. VECTOR2I pte = aTransform.TransformCoordinate( b ) + aOffset;
  342. GRLine( DC, pts.x, pts.y, pte.x, pte.y, penWidth, color );
  343. } );
  344. }
  345. for( SHAPE* shape : shapes )
  346. delete shape;
  347. }
  348. }
  349. delete[] buffer;
  350. }
  351. const BOX2I LIB_SHAPE::GetBoundingBox() const
  352. {
  353. BOX2I bbox = getBoundingBox();
  354. bbox.RevertYAxis();
  355. return bbox;
  356. }
  357. void LIB_SHAPE::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList )
  358. {
  359. LIB_ITEM::GetMsgPanelInfo( aFrame, aList );
  360. ShapeGetMsgPanelInfo( aFrame, aList );
  361. }
  362. wxString LIB_SHAPE::GetItemDescription( UNITS_PROVIDER* aUnitsProvider ) const
  363. {
  364. switch( GetShape() )
  365. {
  366. case SHAPE_T::ARC:
  367. return wxString::Format( _( "Arc, radius %s" ),
  368. aUnitsProvider->MessageTextFromValue( GetRadius() ) );
  369. case SHAPE_T::CIRCLE:
  370. return wxString::Format( _( "Circle, radius %s" ),
  371. aUnitsProvider->MessageTextFromValue( GetRadius() ) );
  372. case SHAPE_T::RECT:
  373. return wxString::Format( _( "Rectangle, width %s height %s" ),
  374. aUnitsProvider->MessageTextFromValue( std::abs( m_start.x - m_end.x ) ),
  375. aUnitsProvider->MessageTextFromValue( std::abs( m_start.y - m_end.y ) ) );
  376. case SHAPE_T::POLY:
  377. return wxString::Format( _( "Polyline, %d points" ),
  378. int( m_poly.Outline( 0 ).GetPointCount() ) );
  379. case SHAPE_T::BEZIER:
  380. return wxString::Format( _( "Bezier Curve, %d points" ),
  381. int( m_bezierPoints.size() ) );
  382. default:
  383. UNIMPLEMENTED_FOR( SHAPE_T_asString() );
  384. return wxEmptyString;
  385. }
  386. }
  387. BITMAPS LIB_SHAPE::GetMenuImage() const
  388. {
  389. switch( GetShape() )
  390. {
  391. case SHAPE_T::SEGMENT: return BITMAPS::add_line;
  392. case SHAPE_T::ARC: return BITMAPS::add_arc;
  393. case SHAPE_T::CIRCLE: return BITMAPS::add_circle;
  394. case SHAPE_T::RECT: return BITMAPS::add_rectangle;
  395. case SHAPE_T::POLY: return BITMAPS::add_graphical_segments;
  396. default:
  397. UNIMPLEMENTED_FOR( SHAPE_T_asString() );
  398. return BITMAPS::question_mark;
  399. }
  400. }
  401. void LIB_SHAPE::AddPoint( const VECTOR2I& aPosition )
  402. {
  403. if( GetShape() == SHAPE_T::POLY )
  404. {
  405. if( m_poly.IsEmpty() )
  406. m_poly.NewOutline();
  407. m_poly.Outline( 0 ).Append( aPosition, true );
  408. }
  409. else
  410. {
  411. UNIMPLEMENTED_FOR( SHAPE_T_asString() );
  412. }
  413. }
  414. void LIB_SHAPE::ViewGetLayers( int aLayers[], int& aCount ) const
  415. {
  416. aCount = 3;
  417. aLayers[0] = IsPrivate() ? LAYER_PRIVATE_NOTES : LAYER_DEVICE;
  418. aLayers[1] = IsPrivate() ? LAYER_NOTES_BACKGROUND : LAYER_DEVICE_BACKGROUND;
  419. aLayers[2] = LAYER_SELECTION_SHADOWS;
  420. }