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.

585 lines
18 KiB

8 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2017 Jon Evans <jon@craftyjon.com>
  5. * Copyright (C) 2019 KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software: you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation, either version 3 of the License, or (at your
  10. * option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <gerbview_painter.h>
  21. #include <gal/graphics_abstraction_layer.h>
  22. #include <settings/color_settings.h>
  23. #include <convert_basic_shapes_to_polygon.h>
  24. #include <convert_to_biu.h>
  25. #include <gerbview.h>
  26. #include <dcode.h>
  27. #include <gerber_draw_item.h>
  28. #include <gerber_file_image.h>
  29. using namespace KIGFX;
  30. GERBVIEW_RENDER_SETTINGS::GERBVIEW_RENDER_SETTINGS()
  31. {
  32. m_backgroundColor = COLOR4D::BLACK;
  33. m_spotFill = true;
  34. m_lineFill = true;
  35. m_polygonFill = true;
  36. m_showNegativeItems = false;
  37. m_showCodes = false;
  38. m_diffMode = true;
  39. m_componentHighlightString = "";
  40. m_netHighlightString = "";
  41. m_attributeHighlightString = "";
  42. update();
  43. }
  44. void GERBVIEW_RENDER_SETTINGS::LoadColors( const COLOR_SETTINGS* aSettings )
  45. {
  46. size_t palette_size = aSettings->m_Palette.size();
  47. size_t palette_idx = 0;
  48. for( int i = GERBVIEW_LAYER_ID_START;
  49. i < GERBVIEW_LAYER_ID_START + GERBER_DRAWLAYERS_COUNT; i++ )
  50. {
  51. COLOR4D baseColor = aSettings->GetColor( i );
  52. if( baseColor == COLOR4D::UNSPECIFIED )
  53. baseColor = aSettings->m_Palette[ ( palette_idx++ ) % palette_size ];
  54. if( m_diffMode )
  55. baseColor.a = 0.75;
  56. m_layerColors[i] = baseColor;
  57. m_layerColorsHi[i] = baseColor.Brightened( 0.5 );
  58. m_layerColorsSel[i] = baseColor.Brightened( 0.8 );
  59. m_layerColorsDark[i] = baseColor.Darkened( 0.25 );
  60. }
  61. for( int i = LAYER_DCODES; i < GERBVIEW_LAYER_ID_END; i++ )
  62. m_layerColors[i] = aSettings->GetColor( i );
  63. for( int i = GAL_LAYER_ID_START; i < GAL_LAYER_ID_END; i++ )
  64. m_layerColors[i] = aSettings->GetColor( i );
  65. update();
  66. }
  67. void GERBVIEW_RENDER_SETTINGS::LoadDisplayOptions( const GBR_DISPLAY_OPTIONS& aOptions )
  68. {
  69. m_spotFill = aOptions.m_DisplayFlashedItemsFill;
  70. m_lineFill = aOptions.m_DisplayLinesFill;
  71. m_polygonFill = aOptions.m_DisplayPolygonsFill;
  72. m_showNegativeItems = aOptions.m_DisplayNegativeObjects;
  73. m_showCodes = aOptions.m_DisplayDCodes;
  74. m_diffMode = aOptions.m_DiffMode;
  75. m_hiContrastEnabled = aOptions.m_HighContrastMode;
  76. m_showPageLimits = aOptions.m_DisplayPageLimits;
  77. m_backgroundColor = aOptions.m_BgDrawColor;
  78. update();
  79. }
  80. const COLOR4D& GERBVIEW_RENDER_SETTINGS::GetColor( const VIEW_ITEM* aItem, int aLayer ) const
  81. {
  82. const EDA_ITEM* item = static_cast<const EDA_ITEM*>( aItem );
  83. static const COLOR4D transparent = COLOR4D( 0, 0, 0, 0 );
  84. const GERBER_DRAW_ITEM* gbrItem = nullptr;
  85. if( item && item->Type() == GERBER_DRAW_ITEM_T )
  86. gbrItem = static_cast<const GERBER_DRAW_ITEM*>( item );
  87. // All DCODE layers stored under a single color setting
  88. if( IsDCodeLayer( aLayer ) )
  89. return m_layerColors[ LAYER_DCODES ];
  90. if( item && item->IsSelected() )
  91. return m_layerColorsSel[aLayer];
  92. if( gbrItem && gbrItem->GetLayerPolarity() )
  93. {
  94. if( m_showNegativeItems )
  95. return m_layerColors[LAYER_NEGATIVE_OBJECTS];
  96. else
  97. return transparent;
  98. }
  99. if( !m_netHighlightString.IsEmpty() && gbrItem &&
  100. m_netHighlightString == gbrItem->GetNetAttributes().m_Netname )
  101. return m_layerColorsHi[aLayer];
  102. if( !m_componentHighlightString.IsEmpty() && gbrItem &&
  103. m_componentHighlightString == gbrItem->GetNetAttributes().m_Cmpref )
  104. return m_layerColorsHi[aLayer];
  105. if( !m_attributeHighlightString.IsEmpty() && gbrItem && gbrItem->GetDcodeDescr() &&
  106. m_attributeHighlightString == gbrItem->GetDcodeDescr()->m_AperFunction )
  107. return m_layerColorsHi[aLayer];
  108. // Return grayish color for non-highlighted layers in the high contrast mode
  109. if( m_hiContrastEnabled && m_activeLayers.count( aLayer ) == 0)
  110. return m_hiContrastColor[aLayer];
  111. // Catch the case when highlight and high-contraste modes are enabled
  112. // and we are drawing a not highlighted track
  113. if( m_highlightEnabled )
  114. return m_layerColorsDark[aLayer];
  115. // No special modificators enabled
  116. return m_layerColors[aLayer];
  117. }
  118. GERBVIEW_PAINTER::GERBVIEW_PAINTER( GAL* aGal ) :
  119. PAINTER( aGal )
  120. {
  121. }
  122. // TODO(JE): Pull up to PAINTER?
  123. int GERBVIEW_PAINTER::getLineThickness( int aActualThickness ) const
  124. {
  125. // if items have 0 thickness, draw them with the outline
  126. // width, otherwise respect the set value (which, no matter
  127. // how small will produce something)
  128. if( aActualThickness == 0 )
  129. return m_gerbviewSettings.m_outlineWidth;
  130. return aActualThickness;
  131. }
  132. bool GERBVIEW_PAINTER::Draw( const VIEW_ITEM* aItem, int aLayer )
  133. {
  134. const EDA_ITEM* item = static_cast<const EDA_ITEM*>( aItem );
  135. // the "cast" applied in here clarifies which overloaded draw() is called
  136. switch( item->Type() )
  137. {
  138. case GERBER_DRAW_ITEM_T:
  139. draw( static_cast<GERBER_DRAW_ITEM*>( const_cast<EDA_ITEM*>( item ) ), aLayer );
  140. break;
  141. default:
  142. // Painter does not know how to draw the object
  143. return false;
  144. }
  145. return true;
  146. }
  147. // TODO(JE) aItem can't be const because of GetDcodeDescr()
  148. // Probably that can be refactored in GERBER_DRAW_ITEM to allow const here.
  149. void GERBVIEW_PAINTER::draw( /*const*/ GERBER_DRAW_ITEM* aItem, int aLayer )
  150. {
  151. VECTOR2D start( aItem->GetABPosition( aItem->m_Start ) ); // TODO(JE) Getter
  152. VECTOR2D end( aItem->GetABPosition( aItem->m_End ) ); // TODO(JE) Getter
  153. int width = aItem->m_Size.x; // TODO(JE) Getter
  154. bool isFilled = true;
  155. COLOR4D color;
  156. // TODO(JE) This doesn't actually work properly for ImageNegative
  157. bool isNegative = ( aItem->GetLayerPolarity() ^ aItem->m_GerberImageFile->m_ImageNegative );
  158. // Draw DCODE overlay text
  159. if( IsDCodeLayer( aLayer ) )
  160. {
  161. wxString codeText;
  162. VECTOR2D textPosition;
  163. double textSize;
  164. double orient;
  165. if( !aItem->GetTextD_CodePrms( textSize, textPosition, orient ) )
  166. return;
  167. color = m_gerbviewSettings.GetColor( aItem, aLayer );
  168. codeText.Printf( "D%d", aItem->m_DCode );
  169. m_gal->SetIsStroke( true );
  170. m_gal->SetIsFill( false );
  171. m_gal->SetStrokeColor( color );
  172. m_gal->SetFillColor( COLOR4D( 0, 0, 0, 0 ) );
  173. m_gal->SetLineWidth( textSize/10 );
  174. m_gal->SetFontBold( false );
  175. m_gal->SetFontItalic( false );
  176. m_gal->SetTextMirrored( false );
  177. m_gal->SetGlyphSize( VECTOR2D( textSize, textSize) );
  178. m_gal->SetHorizontalJustify( GR_TEXT_HJUSTIFY_CENTER );
  179. m_gal->SetVerticalJustify( GR_TEXT_VJUSTIFY_CENTER );
  180. m_gal->BitmapText( codeText, textPosition, orient );
  181. return;
  182. }
  183. color = m_gerbviewSettings.GetColor( aItem, aLayer );
  184. // TODO: Should brightened color be a preference?
  185. if( aItem->IsBrightened() )
  186. color = COLOR4D( 0.0, 1.0, 0.0, 0.75 );
  187. m_gal->SetNegativeDrawMode( isNegative );
  188. m_gal->SetStrokeColor( color );
  189. m_gal->SetFillColor( color );
  190. m_gal->SetIsFill( isFilled );
  191. m_gal->SetIsStroke( !isFilled );
  192. switch( aItem->m_Shape )
  193. {
  194. case GBR_POLYGON:
  195. {
  196. isFilled = m_gerbviewSettings.m_polygonFill;
  197. m_gal->SetIsFill( isFilled );
  198. m_gal->SetIsStroke( !isFilled );
  199. if( isNegative && !isFilled )
  200. {
  201. m_gal->SetNegativeDrawMode( false );
  202. m_gal->SetStrokeColor( GetSettings()->GetColor( aItem, aLayer ) );
  203. }
  204. if( !isFilled )
  205. m_gal->SetLineWidth( m_gerbviewSettings.m_outlineWidth );
  206. std::vector<VECTOR2I> pts = aItem->m_Polygon.COutline( 0 ).CPoints();
  207. for( auto& pt : pts )
  208. pt = aItem->GetABPosition( pt );
  209. SHAPE_POLY_SET absolutePolygon;
  210. SHAPE_LINE_CHAIN chain( pts );
  211. chain.SetClosed( true );
  212. absolutePolygon.AddOutline( chain );
  213. // Degenerated polygons (having < 3 points) are drawn as lines
  214. // to avoid issues in draw polygon functions
  215. if( !isFilled || absolutePolygon.COutline( 0 ).PointCount() < 3 )
  216. m_gal->DrawPolyline( absolutePolygon.COutline( 0 ) );
  217. else
  218. {
  219. // On Opengl, a not convex filled polygon is usually drawn by using triangles as primitives.
  220. // CacheTriangulation() can create basic triangle primitives to draw the polygon solid shape
  221. // on Opengl
  222. if( m_gal->IsOpenGlEngine() )
  223. absolutePolygon.CacheTriangulation();
  224. m_gal->DrawPolygon( absolutePolygon );
  225. }
  226. break;
  227. }
  228. case GBR_CIRCLE:
  229. {
  230. isFilled = m_gerbviewSettings.m_lineFill;
  231. double radius = GetLineLength( aItem->m_Start, aItem->m_End );
  232. m_gal->DrawCircle( start, radius );
  233. break;
  234. }
  235. case GBR_ARC:
  236. {
  237. isFilled = m_gerbviewSettings.m_lineFill;
  238. // These are swapped because wxDC fills arcs counterclockwise and GAL
  239. // fills them clockwise.
  240. wxPoint arcStart = aItem->m_End;
  241. wxPoint arcEnd = aItem->m_Start;
  242. // Gerber arcs are 3-point (start, center, end)
  243. // GAL needs center, radius, start angle, end angle
  244. double radius = GetLineLength( arcStart, aItem->m_ArcCentre );
  245. VECTOR2D center = aItem->GetABPosition( aItem->m_ArcCentre );
  246. VECTOR2D startVec = VECTOR2D( aItem->GetABPosition( arcStart ) ) - center;
  247. VECTOR2D endVec = VECTOR2D( aItem->GetABPosition( arcEnd ) ) - center;
  248. m_gal->SetIsFill( isFilled );
  249. m_gal->SetIsStroke( !isFilled );
  250. m_gal->SetLineWidth( isFilled ? width : m_gerbviewSettings.m_outlineWidth );
  251. double startAngle = startVec.Angle();
  252. double endAngle = endVec.Angle();
  253. // GAL fills in direction of increasing angle, so we have to convert
  254. // the angle from the -PI to PI domain of atan2() to ensure that
  255. // the arc goes in the right direction
  256. if( startAngle > endAngle )
  257. endAngle += (2 * M_PI);
  258. // In Gerber, 360-degree arcs are stored in the file with start equal to end
  259. if( arcStart == arcEnd )
  260. {
  261. endAngle = startAngle + 2*M_PI;
  262. }
  263. m_gal->DrawArcSegment( center, radius, startAngle, endAngle, width );
  264. #if 0 // Arc Debugging only
  265. m_gal->SetIsFill( false );
  266. m_gal->SetIsStroke( true );
  267. m_gal->SetLineWidth( 5 );
  268. m_gal->SetStrokeColor( COLOR4D( 0.1, 0.5, 0.0, 0.5 ) );
  269. m_gal->DrawLine( center, aItem->GetABPosition( arcStart ) );
  270. m_gal->SetStrokeColor( COLOR4D( 0.6, 0.1, 0.0, 0.5 ) );
  271. m_gal->DrawLine( center, aItem->GetABPosition( arcEnd ) );
  272. #endif
  273. #if 0 // Bbox arc Debugging only
  274. m_gal->SetIsFill( false );
  275. m_gal->SetIsStroke( true );
  276. EDA_RECT box = aItem->GetBoundingBox();
  277. m_gal->SetLineWidth( 5 );
  278. m_gal->SetStrokeColor( COLOR4D(0.9, 0.9, 0, 0.4) );
  279. // box coordinates are already in AB position.
  280. m_gal->DrawRectangle( box.GetOrigin(), box.GetEnd() );
  281. #endif
  282. break;
  283. }
  284. case GBR_SPOT_CIRCLE:
  285. case GBR_SPOT_RECT:
  286. case GBR_SPOT_OVAL:
  287. case GBR_SPOT_POLY:
  288. case GBR_SPOT_MACRO:
  289. {
  290. isFilled = m_gerbviewSettings.m_spotFill;
  291. drawFlashedShape( aItem, isFilled );
  292. break;
  293. }
  294. case GBR_SEGMENT:
  295. {
  296. /* Plot a line from m_Start to m_End.
  297. * Usually, a round pen is used, but some gerber files use a rectangular pen
  298. * In fact, any aperture can be used to plot a line.
  299. * currently: only a square pen is handled (I believe using a polygon gives a strange plot).
  300. */
  301. isFilled = m_gerbviewSettings.m_lineFill;
  302. m_gal->SetIsFill( isFilled );
  303. m_gal->SetIsStroke( !isFilled );
  304. if( isNegative && !isFilled )
  305. m_gal->SetStrokeColor( GetSettings()->GetColor( aItem, aLayer ) );
  306. // TODO(JE) Refactor this to allow const aItem
  307. D_CODE* code = aItem->GetDcodeDescr();
  308. if( code && code->m_Shape == APT_RECT )
  309. {
  310. if( aItem->m_Polygon.OutlineCount() == 0 )
  311. aItem->ConvertSegmentToPolygon();
  312. drawPolygon( aItem, aItem->m_Polygon, isFilled );
  313. }
  314. else
  315. {
  316. if( !isFilled )
  317. m_gal->SetLineWidth( m_gerbviewSettings.m_outlineWidth );
  318. m_gal->DrawSegment( start, end, width );
  319. }
  320. break;
  321. }
  322. default:
  323. wxASSERT_MSG( false, "GERBER_DRAW_ITEM shape is unknown!" );
  324. break;
  325. }
  326. // Enable for bounding box debugging
  327. #if 0
  328. const BOX2I& bb = aItem->ViewBBox();
  329. m_gal->SetIsStroke( true );
  330. m_gal->SetIsFill( true );
  331. m_gal->SetLineWidth( 3 );
  332. m_gal->SetStrokeColor( COLOR4D(0.9, 0.9, 0, 0.4) );
  333. m_gal->SetFillColor( COLOR4D(0.9, 0.9, 0, 0.1) );
  334. m_gal->DrawRectangle( bb.GetOrigin(), bb.GetEnd() );
  335. #endif
  336. }
  337. void GERBVIEW_PAINTER::drawPolygon(
  338. GERBER_DRAW_ITEM* aParent, const SHAPE_POLY_SET& aPolygon, bool aFilled, bool aShift )
  339. {
  340. wxASSERT( aPolygon.OutlineCount() == 1 );
  341. if( aPolygon.OutlineCount() == 0 )
  342. return;
  343. SHAPE_POLY_SET poly;
  344. poly.NewOutline();
  345. const std::vector<VECTOR2I> pts = aPolygon.COutline( 0 ).CPoints();
  346. VECTOR2I offset = aShift ? VECTOR2I( aParent->m_Start ) : VECTOR2I( 0, 0 );
  347. for( auto& pt : pts )
  348. poly.Append( aParent->GetABPosition( pt + offset ) );
  349. if( !m_gerbviewSettings.m_polygonFill )
  350. m_gal->SetLineWidth( m_gerbviewSettings.m_outlineWidth );
  351. if( !aFilled )
  352. {
  353. m_gal->DrawPolyline( poly.COutline( 0 ) );
  354. }
  355. else
  356. m_gal->DrawPolygon( poly );
  357. }
  358. void GERBVIEW_PAINTER::drawFlashedShape( GERBER_DRAW_ITEM* aItem, bool aFilled )
  359. {
  360. D_CODE* code = aItem->GetDcodeDescr();
  361. wxASSERT_MSG( code, "drawFlashedShape: Item has no D_CODE!" );
  362. if( !code )
  363. return;
  364. m_gal->SetIsFill( aFilled );
  365. m_gal->SetIsStroke( !aFilled );
  366. m_gal->SetLineWidth( m_gerbviewSettings.m_outlineWidth );
  367. switch( aItem->m_Shape )
  368. {
  369. case GBR_SPOT_CIRCLE:
  370. {
  371. int radius = code->m_Size.x >> 1;
  372. VECTOR2D start( aItem->GetABPosition( aItem->m_Start ) );
  373. if( !aFilled || code->m_DrillShape == APT_DEF_NO_HOLE )
  374. {
  375. m_gal->DrawCircle( start, radius );
  376. }
  377. else // rectangular hole
  378. {
  379. if( code->m_Polygon.OutlineCount() == 0 )
  380. code->ConvertShapeToPolygon();
  381. drawPolygon( aItem, code->m_Polygon, aFilled, true );
  382. }
  383. break;
  384. }
  385. case GBR_SPOT_RECT:
  386. {
  387. wxPoint codeStart;
  388. wxPoint aShapePos = aItem->m_Start;
  389. codeStart.x = aShapePos.x - code->m_Size.x / 2;
  390. codeStart.y = aShapePos.y - code->m_Size.y / 2;
  391. wxPoint codeEnd = codeStart + code->m_Size;
  392. codeStart = aItem->GetABPosition( codeStart );
  393. codeEnd = aItem->GetABPosition( codeEnd );
  394. if( !aFilled || code->m_DrillShape == APT_DEF_NO_HOLE )
  395. {
  396. m_gal->DrawRectangle( VECTOR2D( codeStart ), VECTOR2D( codeEnd ) );
  397. }
  398. else
  399. {
  400. if( code->m_Polygon.OutlineCount() == 0 )
  401. code->ConvertShapeToPolygon();
  402. drawPolygon( aItem, code->m_Polygon, aFilled, true );
  403. }
  404. break;
  405. }
  406. case GBR_SPOT_OVAL:
  407. {
  408. int radius = 0;
  409. wxPoint codeStart = aItem->m_Start;
  410. wxPoint codeEnd = aItem->m_Start;
  411. if( code->m_Size.x > code->m_Size.y ) // horizontal oval
  412. {
  413. int delta = (code->m_Size.x - code->m_Size.y) / 2;
  414. codeStart.x -= delta;
  415. codeEnd.x += delta;
  416. radius = code->m_Size.y;
  417. }
  418. else // horizontal oval
  419. {
  420. int delta = (code->m_Size.y - code->m_Size.x) / 2;
  421. codeStart.y -= delta;
  422. codeEnd.y += delta;
  423. radius = code->m_Size.x;
  424. }
  425. codeStart = aItem->GetABPosition( codeStart );
  426. codeEnd = aItem->GetABPosition( codeEnd );
  427. if( !aFilled || code->m_DrillShape == APT_DEF_NO_HOLE )
  428. {
  429. m_gal->DrawSegment( codeStart, codeEnd, radius );
  430. }
  431. else
  432. {
  433. if( code->m_Polygon.OutlineCount() == 0 )
  434. code->ConvertShapeToPolygon();
  435. drawPolygon( aItem, code->m_Polygon, aFilled, true );
  436. }
  437. break;
  438. }
  439. case GBR_SPOT_POLY:
  440. {
  441. if( code->m_Polygon.OutlineCount() == 0 )
  442. code->ConvertShapeToPolygon();
  443. drawPolygon( aItem, code->m_Polygon, aFilled, true );
  444. break;
  445. }
  446. case GBR_SPOT_MACRO:
  447. drawApertureMacro( aItem, aFilled );
  448. break;
  449. default:
  450. wxASSERT_MSG( false, wxT( "Unknown Gerber flashed shape!" ) );
  451. break;
  452. }
  453. }
  454. void GERBVIEW_PAINTER::drawApertureMacro( GERBER_DRAW_ITEM* aParent, bool aFilled )
  455. {
  456. D_CODE* code = aParent->GetDcodeDescr();
  457. APERTURE_MACRO* macro = code->GetMacro();
  458. SHAPE_POLY_SET* macroShape = macro->GetApertureMacroShape( aParent, aParent->m_Start );
  459. if( !m_gerbviewSettings.m_polygonFill )
  460. m_gal->SetLineWidth( m_gerbviewSettings.m_outlineWidth );
  461. if( !aFilled )
  462. {
  463. for( int i = 0; i < macroShape->OutlineCount(); i++ )
  464. m_gal->DrawPolyline( macroShape->COutline( i ) );
  465. }
  466. else
  467. m_gal->DrawPolygon( *macroShape );
  468. }
  469. const double GERBVIEW_RENDER_SETTINGS::MAX_FONT_SIZE = Millimeter2iu( 10.0 );