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.

1260 lines
48 KiB

14 years ago
17 years ago
17 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
17 years ago
14 years ago
5 years ago
5 years ago
5 years ago
5 years ago
14 years ago
17 years ago
5 years ago
5 years ago
5 years ago
5 years ago
17 years ago
5 years ago
17 years ago
14 years ago
14 years ago
17 years ago
Clean up arc/circle polygonization. 1) For a while now we've been using a calculated seg count from a given maxError, and a correction factor to push the radius out so that all the error is outside the arc/circle. However, the second calculation (which pre-dates the first) is pretty much just the inverse of the first (and yields nothing more than maxError back). This is particularly sub-optimal given the cost of trig functions. 2) There are a lot of old optimizations to reduce segcounts in certain situations, someting that our error-based calculation compensates for anyway. (Smaller radii need fewer segments to meet the maxError condition.) But perhaps more importantly we now surface maxError in the UI and we don't really want to call it "Max deviation except when it's not". 3) We were also clamping the segCount twice: once in the calculation routine and once in most of it's callers. Furthermore, the caller clamping was inconsistent (both in being done and in the clamping value). We now clamp only in the calculation routine. 4) There's no reason to use the correction factors in the 3Dviewer; it's just a visualization and whether the polygonization error is inside or outside the shape isn't really material. 5) The arc-correction-disabling stuff (used for solder mask layer) was somewhat fragile in that it depended on the caller to turn it back on afterwards. It's now only exposed as a RAII object which automatically cleans up when it goes out of scope. 6) There were also bugs in a couple of the polygonization routines where we'd accumulate round-off error in adding up the segments and end up with an overly long last segment (which of course would voilate the error max). This was the cause of the linked bug and also some issues with vias that we had fudged in the past with extra clearance. Fixes https://gitlab.com/kicad/code/kicad/issues/5567
5 years ago
Clean up arc/circle polygonization. 1) For a while now we've been using a calculated seg count from a given maxError, and a correction factor to push the radius out so that all the error is outside the arc/circle. However, the second calculation (which pre-dates the first) is pretty much just the inverse of the first (and yields nothing more than maxError back). This is particularly sub-optimal given the cost of trig functions. 2) There are a lot of old optimizations to reduce segcounts in certain situations, someting that our error-based calculation compensates for anyway. (Smaller radii need fewer segments to meet the maxError condition.) But perhaps more importantly we now surface maxError in the UI and we don't really want to call it "Max deviation except when it's not". 3) We were also clamping the segCount twice: once in the calculation routine and once in most of it's callers. Furthermore, the caller clamping was inconsistent (both in being done and in the clamping value). We now clamp only in the calculation routine. 4) There's no reason to use the correction factors in the 3Dviewer; it's just a visualization and whether the polygonization error is inside or outside the shape isn't really material. 5) The arc-correction-disabling stuff (used for solder mask layer) was somewhat fragile in that it depended on the caller to turn it back on afterwards. It's now only exposed as a RAII object which automatically cleans up when it goes out of scope. 6) There were also bugs in a couple of the polygonization routines where we'd accumulate round-off error in adding up the segments and end up with an overly long last segment (which of course would voilate the error max). This was the cause of the linked bug and also some issues with vias that we had fudged in the past with extra clearance. Fixes https://gitlab.com/kicad/code/kicad/issues/5567
5 years ago
Clean up arc/circle polygonization. 1) For a while now we've been using a calculated seg count from a given maxError, and a correction factor to push the radius out so that all the error is outside the arc/circle. However, the second calculation (which pre-dates the first) is pretty much just the inverse of the first (and yields nothing more than maxError back). This is particularly sub-optimal given the cost of trig functions. 2) There are a lot of old optimizations to reduce segcounts in certain situations, someting that our error-based calculation compensates for anyway. (Smaller radii need fewer segments to meet the maxError condition.) But perhaps more importantly we now surface maxError in the UI and we don't really want to call it "Max deviation except when it's not". 3) We were also clamping the segCount twice: once in the calculation routine and once in most of it's callers. Furthermore, the caller clamping was inconsistent (both in being done and in the clamping value). We now clamp only in the calculation routine. 4) There's no reason to use the correction factors in the 3Dviewer; it's just a visualization and whether the polygonization error is inside or outside the shape isn't really material. 5) The arc-correction-disabling stuff (used for solder mask layer) was somewhat fragile in that it depended on the caller to turn it back on afterwards. It's now only exposed as a RAII object which automatically cleans up when it goes out of scope. 6) There were also bugs in a couple of the polygonization routines where we'd accumulate round-off error in adding up the segments and end up with an overly long last segment (which of course would voilate the error max). This was the cause of the linked bug and also some issues with vias that we had fudged in the past with extra clearance. Fixes https://gitlab.com/kicad/code/kicad/issues/5567
5 years ago
Clean up arc/circle polygonization. 1) For a while now we've been using a calculated seg count from a given maxError, and a correction factor to push the radius out so that all the error is outside the arc/circle. However, the second calculation (which pre-dates the first) is pretty much just the inverse of the first (and yields nothing more than maxError back). This is particularly sub-optimal given the cost of trig functions. 2) There are a lot of old optimizations to reduce segcounts in certain situations, someting that our error-based calculation compensates for anyway. (Smaller radii need fewer segments to meet the maxError condition.) But perhaps more importantly we now surface maxError in the UI and we don't really want to call it "Max deviation except when it's not". 3) We were also clamping the segCount twice: once in the calculation routine and once in most of it's callers. Furthermore, the caller clamping was inconsistent (both in being done and in the clamping value). We now clamp only in the calculation routine. 4) There's no reason to use the correction factors in the 3Dviewer; it's just a visualization and whether the polygonization error is inside or outside the shape isn't really material. 5) The arc-correction-disabling stuff (used for solder mask layer) was somewhat fragile in that it depended on the caller to turn it back on afterwards. It's now only exposed as a RAII object which automatically cleans up when it goes out of scope. 6) There were also bugs in a couple of the polygonization routines where we'd accumulate round-off error in adding up the segments and end up with an overly long last segment (which of course would voilate the error max). This was the cause of the linked bug and also some issues with vias that we had fudged in the past with extra clearance. Fixes https://gitlab.com/kicad/code/kicad/issues/5567
5 years ago
Clean up arc/circle polygonization. 1) For a while now we've been using a calculated seg count from a given maxError, and a correction factor to push the radius out so that all the error is outside the arc/circle. However, the second calculation (which pre-dates the first) is pretty much just the inverse of the first (and yields nothing more than maxError back). This is particularly sub-optimal given the cost of trig functions. 2) There are a lot of old optimizations to reduce segcounts in certain situations, someting that our error-based calculation compensates for anyway. (Smaller radii need fewer segments to meet the maxError condition.) But perhaps more importantly we now surface maxError in the UI and we don't really want to call it "Max deviation except when it's not". 3) We were also clamping the segCount twice: once in the calculation routine and once in most of it's callers. Furthermore, the caller clamping was inconsistent (both in being done and in the clamping value). We now clamp only in the calculation routine. 4) There's no reason to use the correction factors in the 3Dviewer; it's just a visualization and whether the polygonization error is inside or outside the shape isn't really material. 5) The arc-correction-disabling stuff (used for solder mask layer) was somewhat fragile in that it depended on the caller to turn it back on afterwards. It's now only exposed as a RAII object which automatically cleans up when it goes out of scope. 6) There were also bugs in a couple of the polygonization routines where we'd accumulate round-off error in adding up the segments and end up with an overly long last segment (which of course would voilate the error max). This was the cause of the linked bug and also some issues with vias that we had fudged in the past with extra clearance. Fixes https://gitlab.com/kicad/code/kicad/issues/5567
5 years ago
Clean up arc/circle polygonization. 1) For a while now we've been using a calculated seg count from a given maxError, and a correction factor to push the radius out so that all the error is outside the arc/circle. However, the second calculation (which pre-dates the first) is pretty much just the inverse of the first (and yields nothing more than maxError back). This is particularly sub-optimal given the cost of trig functions. 2) There are a lot of old optimizations to reduce segcounts in certain situations, someting that our error-based calculation compensates for anyway. (Smaller radii need fewer segments to meet the maxError condition.) But perhaps more importantly we now surface maxError in the UI and we don't really want to call it "Max deviation except when it's not". 3) We were also clamping the segCount twice: once in the calculation routine and once in most of it's callers. Furthermore, the caller clamping was inconsistent (both in being done and in the clamping value). We now clamp only in the calculation routine. 4) There's no reason to use the correction factors in the 3Dviewer; it's just a visualization and whether the polygonization error is inside or outside the shape isn't really material. 5) The arc-correction-disabling stuff (used for solder mask layer) was somewhat fragile in that it depended on the caller to turn it back on afterwards. It's now only exposed as a RAII object which automatically cleans up when it goes out of scope. 6) There were also bugs in a couple of the polygonization routines where we'd accumulate round-off error in adding up the segments and end up with an overly long last segment (which of course would voilate the error max). This was the cause of the linked bug and also some issues with vias that we had fudged in the past with extra clearance. Fixes https://gitlab.com/kicad/code/kicad/issues/5567
5 years ago
Clean up arc/circle polygonization. 1) For a while now we've been using a calculated seg count from a given maxError, and a correction factor to push the radius out so that all the error is outside the arc/circle. However, the second calculation (which pre-dates the first) is pretty much just the inverse of the first (and yields nothing more than maxError back). This is particularly sub-optimal given the cost of trig functions. 2) There are a lot of old optimizations to reduce segcounts in certain situations, someting that our error-based calculation compensates for anyway. (Smaller radii need fewer segments to meet the maxError condition.) But perhaps more importantly we now surface maxError in the UI and we don't really want to call it "Max deviation except when it's not". 3) We were also clamping the segCount twice: once in the calculation routine and once in most of it's callers. Furthermore, the caller clamping was inconsistent (both in being done and in the clamping value). We now clamp only in the calculation routine. 4) There's no reason to use the correction factors in the 3Dviewer; it's just a visualization and whether the polygonization error is inside or outside the shape isn't really material. 5) The arc-correction-disabling stuff (used for solder mask layer) was somewhat fragile in that it depended on the caller to turn it back on afterwards. It's now only exposed as a RAII object which automatically cleans up when it goes out of scope. 6) There were also bugs in a couple of the polygonization routines where we'd accumulate round-off error in adding up the segments and end up with an overly long last segment (which of course would voilate the error max). This was the cause of the linked bug and also some issues with vias that we had fudged in the past with extra clearance. Fixes https://gitlab.com/kicad/code/kicad/issues/5567
5 years ago
Clean up arc/circle polygonization. 1) For a while now we've been using a calculated seg count from a given maxError, and a correction factor to push the radius out so that all the error is outside the arc/circle. However, the second calculation (which pre-dates the first) is pretty much just the inverse of the first (and yields nothing more than maxError back). This is particularly sub-optimal given the cost of trig functions. 2) There are a lot of old optimizations to reduce segcounts in certain situations, someting that our error-based calculation compensates for anyway. (Smaller radii need fewer segments to meet the maxError condition.) But perhaps more importantly we now surface maxError in the UI and we don't really want to call it "Max deviation except when it's not". 3) We were also clamping the segCount twice: once in the calculation routine and once in most of it's callers. Furthermore, the caller clamping was inconsistent (both in being done and in the clamping value). We now clamp only in the calculation routine. 4) There's no reason to use the correction factors in the 3Dviewer; it's just a visualization and whether the polygonization error is inside or outside the shape isn't really material. 5) The arc-correction-disabling stuff (used for solder mask layer) was somewhat fragile in that it depended on the caller to turn it back on afterwards. It's now only exposed as a RAII object which automatically cleans up when it goes out of scope. 6) There were also bugs in a couple of the polygonization routines where we'd accumulate round-off error in adding up the segments and end up with an overly long last segment (which of course would voilate the error max). This was the cause of the linked bug and also some issues with vias that we had fudged in the past with extra clearance. Fixes https://gitlab.com/kicad/code/kicad/issues/5567
5 years ago
Clean up arc/circle polygonization. 1) For a while now we've been using a calculated seg count from a given maxError, and a correction factor to push the radius out so that all the error is outside the arc/circle. However, the second calculation (which pre-dates the first) is pretty much just the inverse of the first (and yields nothing more than maxError back). This is particularly sub-optimal given the cost of trig functions. 2) There are a lot of old optimizations to reduce segcounts in certain situations, someting that our error-based calculation compensates for anyway. (Smaller radii need fewer segments to meet the maxError condition.) But perhaps more importantly we now surface maxError in the UI and we don't really want to call it "Max deviation except when it's not". 3) We were also clamping the segCount twice: once in the calculation routine and once in most of it's callers. Furthermore, the caller clamping was inconsistent (both in being done and in the clamping value). We now clamp only in the calculation routine. 4) There's no reason to use the correction factors in the 3Dviewer; it's just a visualization and whether the polygonization error is inside or outside the shape isn't really material. 5) The arc-correction-disabling stuff (used for solder mask layer) was somewhat fragile in that it depended on the caller to turn it back on afterwards. It's now only exposed as a RAII object which automatically cleans up when it goes out of scope. 6) There were also bugs in a couple of the polygonization routines where we'd accumulate round-off error in adding up the segments and end up with an overly long last segment (which of course would voilate the error max). This was the cause of the linked bug and also some issues with vias that we had fudged in the past with extra clearance. Fixes https://gitlab.com/kicad/code/kicad/issues/5567
5 years ago
Clean up arc/circle polygonization. 1) For a while now we've been using a calculated seg count from a given maxError, and a correction factor to push the radius out so that all the error is outside the arc/circle. However, the second calculation (which pre-dates the first) is pretty much just the inverse of the first (and yields nothing more than maxError back). This is particularly sub-optimal given the cost of trig functions. 2) There are a lot of old optimizations to reduce segcounts in certain situations, someting that our error-based calculation compensates for anyway. (Smaller radii need fewer segments to meet the maxError condition.) But perhaps more importantly we now surface maxError in the UI and we don't really want to call it "Max deviation except when it's not". 3) We were also clamping the segCount twice: once in the calculation routine and once in most of it's callers. Furthermore, the caller clamping was inconsistent (both in being done and in the clamping value). We now clamp only in the calculation routine. 4) There's no reason to use the correction factors in the 3Dviewer; it's just a visualization and whether the polygonization error is inside or outside the shape isn't really material. 5) The arc-correction-disabling stuff (used for solder mask layer) was somewhat fragile in that it depended on the caller to turn it back on afterwards. It's now only exposed as a RAII object which automatically cleans up when it goes out of scope. 6) There were also bugs in a couple of the polygonization routines where we'd accumulate round-off error in adding up the segments and end up with an overly long last segment (which of course would voilate the error max). This was the cause of the linked bug and also some issues with vias that we had fudged in the past with extra clearance. Fixes https://gitlab.com/kicad/code/kicad/issues/5567
5 years ago
Clean up arc/circle polygonization. 1) For a while now we've been using a calculated seg count from a given maxError, and a correction factor to push the radius out so that all the error is outside the arc/circle. However, the second calculation (which pre-dates the first) is pretty much just the inverse of the first (and yields nothing more than maxError back). This is particularly sub-optimal given the cost of trig functions. 2) There are a lot of old optimizations to reduce segcounts in certain situations, someting that our error-based calculation compensates for anyway. (Smaller radii need fewer segments to meet the maxError condition.) But perhaps more importantly we now surface maxError in the UI and we don't really want to call it "Max deviation except when it's not". 3) We were also clamping the segCount twice: once in the calculation routine and once in most of it's callers. Furthermore, the caller clamping was inconsistent (both in being done and in the clamping value). We now clamp only in the calculation routine. 4) There's no reason to use the correction factors in the 3Dviewer; it's just a visualization and whether the polygonization error is inside or outside the shape isn't really material. 5) The arc-correction-disabling stuff (used for solder mask layer) was somewhat fragile in that it depended on the caller to turn it back on afterwards. It's now only exposed as a RAII object which automatically cleans up when it goes out of scope. 6) There were also bugs in a couple of the polygonization routines where we'd accumulate round-off error in adding up the segments and end up with an overly long last segment (which of course would voilate the error max). This was the cause of the linked bug and also some issues with vias that we had fudged in the past with extra clearance. Fixes https://gitlab.com/kicad/code/kicad/issues/5567
5 years ago
Clean up arc/circle polygonization. 1) For a while now we've been using a calculated seg count from a given maxError, and a correction factor to push the radius out so that all the error is outside the arc/circle. However, the second calculation (which pre-dates the first) is pretty much just the inverse of the first (and yields nothing more than maxError back). This is particularly sub-optimal given the cost of trig functions. 2) There are a lot of old optimizations to reduce segcounts in certain situations, someting that our error-based calculation compensates for anyway. (Smaller radii need fewer segments to meet the maxError condition.) But perhaps more importantly we now surface maxError in the UI and we don't really want to call it "Max deviation except when it's not". 3) We were also clamping the segCount twice: once in the calculation routine and once in most of it's callers. Furthermore, the caller clamping was inconsistent (both in being done and in the clamping value). We now clamp only in the calculation routine. 4) There's no reason to use the correction factors in the 3Dviewer; it's just a visualization and whether the polygonization error is inside or outside the shape isn't really material. 5) The arc-correction-disabling stuff (used for solder mask layer) was somewhat fragile in that it depended on the caller to turn it back on afterwards. It's now only exposed as a RAII object which automatically cleans up when it goes out of scope. 6) There were also bugs in a couple of the polygonization routines where we'd accumulate round-off error in adding up the segments and end up with an overly long last segment (which of course would voilate the error max). This was the cause of the linked bug and also some issues with vias that we had fudged in the past with extra clearance. Fixes https://gitlab.com/kicad/code/kicad/issues/5567
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) 1992-2023 KiCad Developers, see AUTHORS.txt for contributors.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, you may find one here:
  18. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  19. * or you may search the http://www.gnu.org website for the version 2 license,
  20. * or you may write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  22. */
  23. /**
  24. * @brief Functions to plot one board layer (silkscreen layers or other layers).
  25. * Silkscreen layers have specific requirement for pads (not filled) and texts
  26. * (with option to remove them from some copper areas (pads...)
  27. */
  28. #include <wx/log.h>
  29. #include <eda_item.h>
  30. #include <layer_ids.h>
  31. #include <geometry/geometry_utils.h>
  32. #include <geometry/shape_segment.h>
  33. #include <pcb_base_frame.h>
  34. #include <math/util.h> // for KiROUND
  35. #include <board.h>
  36. #include <footprint.h>
  37. #include <pcb_track.h>
  38. #include <pad.h>
  39. #include <zone.h>
  40. #include <pcb_shape.h>
  41. #include <pcb_target.h>
  42. #include <pcb_dimension.h>
  43. #include <pcbplot.h>
  44. #include <plotters/plotter_dxf.h>
  45. #include <plotters/plotter_hpgl.h>
  46. #include <plotters/plotter_gerber.h>
  47. #include <plotters/plotters_pslike.h>
  48. #include <pcb_painter.h>
  49. #include <gbr_metadata.h>
  50. #include <advanced_config.h>
  51. /*
  52. * Plot a solder mask layer. Solder mask layers have a minimum thickness value and cannot be
  53. * drawn like standard layers, unless the minimum thickness is 0.
  54. */
  55. static void PlotSolderMaskLayer( BOARD *aBoard, PLOTTER* aPlotter, LSET aLayerMask,
  56. const PCB_PLOT_PARAMS& aPlotOpt, int aMinThickness );
  57. void PlotBoardLayers( BOARD* aBoard, PLOTTER* aPlotter, const LSEQ& aLayers,
  58. const PCB_PLOT_PARAMS& aPlotOptions )
  59. {
  60. wxCHECK( aBoard && aPlotter && aLayers.size(), /* void */ );
  61. for( LSEQ seq = aLayers; seq; ++seq )
  62. PlotOneBoardLayer( aBoard, aPlotter, *seq, aPlotOptions );
  63. }
  64. void PlotInteractiveLayer( BOARD* aBoard, PLOTTER* aPlotter, const PCB_PLOT_PARAMS& aPlotOpt )
  65. {
  66. for( const FOOTPRINT* fp : aBoard->Footprints() )
  67. {
  68. if( fp->GetLayer() == F_Cu && !aPlotOpt.m_PDFFrontFPPropertyPopups )
  69. continue;
  70. if( fp->GetLayer() == B_Cu && !aPlotOpt.m_PDFBackFPPropertyPopups )
  71. continue;
  72. std::vector<wxString> properties;
  73. properties.emplace_back( wxString::Format( wxT( "!%s = %s" ),
  74. _( "Reference designator" ),
  75. fp->Reference().GetShownText( false ) ) );
  76. properties.emplace_back( wxString::Format( wxT( "!%s = %s" ),
  77. _( "Value" ),
  78. fp->Value().GetShownText( false ) ) );
  79. properties.emplace_back( wxString::Format( wxT( "!%s = %s" ),
  80. _( "Footprint" ),
  81. fp->GetFPID().GetUniStringLibItemName() ) );
  82. for( int i = 0; i < fp->GetFieldCount(); i++ )
  83. {
  84. PCB_FIELD* field = fp->GetFields().at( i );
  85. if( field->IsReference() || field->IsValue() || field->IsFootprint() )
  86. continue;
  87. if( field->GetText().IsEmpty() )
  88. continue;
  89. properties.emplace_back( wxString::Format( wxT( "!%s = %s" ),
  90. field->GetName(),
  91. field->GetText() ) );
  92. }
  93. // These 2 properties are not very useful in a plot file (like a PDF)
  94. #if 0
  95. properties.emplace_back( wxString::Format( wxT( "!%s = %s" ), _( "Library Description" ),
  96. fp->GetLibDescription() ) );
  97. properties.emplace_back( wxString::Format( wxT( "!%s = %s" ),
  98. _( "Keywords" ),
  99. fp->GetKeywords() ) );
  100. #endif
  101. aPlotter->HyperlinkMenu( fp->GetBoundingBox(), properties );
  102. aPlotter->Bookmark( fp->GetBoundingBox(), fp->GetReference(), _( "Footprints" ) );
  103. }
  104. }
  105. void PlotOneBoardLayer( BOARD *aBoard, PLOTTER* aPlotter, PCB_LAYER_ID aLayer,
  106. const PCB_PLOT_PARAMS& aPlotOpt )
  107. {
  108. PCB_PLOT_PARAMS plotOpt = aPlotOpt;
  109. int soldermask_min_thickness = aBoard->GetDesignSettings().m_SolderMaskMinWidth;
  110. // Set a default color and the text mode for this layer
  111. aPlotter->SetColor( BLACK );
  112. aPlotter->SetTextMode( aPlotOpt.GetTextMode() );
  113. // Specify that the contents of the "Edges Pcb" layer are to be plotted in addition to the
  114. // contents of the currently specified layer.
  115. LSET layer_mask( aLayer );
  116. if( IsCopperLayer( aLayer ) )
  117. {
  118. // Skip NPTH pads on copper layers ( only if hole size == pad size ):
  119. // Drill mark will be plotted if drill mark is SMALL_DRILL_SHAPE or FULL_DRILL_SHAPE
  120. if( plotOpt.GetFormat() == PLOT_FORMAT::DXF )
  121. {
  122. plotOpt.SetSkipPlotNPTH_Pads( false );
  123. PlotLayerOutlines( aBoard, aPlotter, layer_mask, plotOpt );
  124. }
  125. else
  126. {
  127. plotOpt.SetSkipPlotNPTH_Pads( true );
  128. PlotStandardLayer( aBoard, aPlotter, layer_mask, plotOpt );
  129. }
  130. }
  131. else
  132. {
  133. switch( aLayer )
  134. {
  135. case B_Mask:
  136. case F_Mask:
  137. plotOpt.SetSkipPlotNPTH_Pads( false );
  138. // Disable plot pad holes
  139. plotOpt.SetDrillMarksType( DRILL_MARKS::NO_DRILL_SHAPE );
  140. // Plot solder mask:
  141. if( soldermask_min_thickness == 0 )
  142. {
  143. if( plotOpt.GetFormat() == PLOT_FORMAT::DXF )
  144. PlotLayerOutlines( aBoard, aPlotter, layer_mask, plotOpt );
  145. else
  146. PlotStandardLayer( aBoard, aPlotter, layer_mask, plotOpt );
  147. }
  148. else
  149. {
  150. PlotSolderMaskLayer( aBoard, aPlotter, layer_mask, plotOpt,
  151. soldermask_min_thickness );
  152. }
  153. break;
  154. case B_Adhes:
  155. case F_Adhes:
  156. case B_Paste:
  157. case F_Paste:
  158. plotOpt.SetSkipPlotNPTH_Pads( false );
  159. // Disable plot pad holes
  160. plotOpt.SetDrillMarksType( DRILL_MARKS::NO_DRILL_SHAPE );
  161. if( plotOpt.GetFormat() == PLOT_FORMAT::DXF )
  162. PlotLayerOutlines( aBoard, aPlotter, layer_mask, plotOpt );
  163. else
  164. PlotStandardLayer( aBoard, aPlotter, layer_mask, plotOpt );
  165. break;
  166. case F_SilkS:
  167. case B_SilkS:
  168. if( plotOpt.GetFormat() == PLOT_FORMAT::DXF && plotOpt.GetDXFPlotPolygonMode() )
  169. // PlotLayerOutlines() is designed only for DXF plotters.
  170. // and must not be used for other plot formats
  171. PlotLayerOutlines( aBoard, aPlotter, layer_mask, plotOpt );
  172. else
  173. PlotStandardLayer( aBoard, aPlotter, layer_mask, plotOpt );
  174. // Gerber: Subtract soldermask from silkscreen if enabled
  175. if( aPlotter->GetPlotterType() == PLOT_FORMAT::GERBER
  176. && plotOpt.GetSubtractMaskFromSilk() )
  177. {
  178. if( aLayer == F_SilkS )
  179. layer_mask = LSET( F_Mask );
  180. else
  181. layer_mask = LSET( B_Mask );
  182. // Create the mask to subtract by creating a negative layer polarity
  183. aPlotter->SetLayerPolarity( false );
  184. // Disable plot pad holes
  185. plotOpt.SetDrillMarksType( DRILL_MARKS::NO_DRILL_SHAPE );
  186. // Plot the mask
  187. PlotStandardLayer( aBoard, aPlotter, layer_mask, plotOpt );
  188. // Disable the negative polarity
  189. aPlotter->SetLayerPolarity( true );
  190. }
  191. break;
  192. // These layers are plotted like silk screen layers.
  193. // Mainly, pads on these layers are not filled.
  194. // This is not necessary the best choice.
  195. case Dwgs_User:
  196. case Cmts_User:
  197. case Eco1_User:
  198. case Eco2_User:
  199. case Edge_Cuts:
  200. case Margin:
  201. case F_CrtYd:
  202. case B_CrtYd:
  203. case F_Fab:
  204. case B_Fab:
  205. plotOpt.SetSkipPlotNPTH_Pads( false );
  206. plotOpt.SetDrillMarksType( DRILL_MARKS::NO_DRILL_SHAPE );
  207. if( plotOpt.GetFormat() == PLOT_FORMAT::DXF && plotOpt.GetDXFPlotPolygonMode() )
  208. // PlotLayerOutlines() is designed only for DXF plotters.
  209. // and must not be used for other plot formats
  210. PlotLayerOutlines( aBoard, aPlotter, layer_mask, plotOpt );
  211. else
  212. PlotStandardLayer( aBoard, aPlotter, layer_mask, plotOpt );
  213. break;
  214. default:
  215. plotOpt.SetSkipPlotNPTH_Pads( false );
  216. plotOpt.SetDrillMarksType( DRILL_MARKS::NO_DRILL_SHAPE );
  217. if( plotOpt.GetFormat() == PLOT_FORMAT::DXF && plotOpt.GetDXFPlotPolygonMode() )
  218. // PlotLayerOutlines() is designed only for DXF plotters.
  219. // and must not be used for other plot formats
  220. PlotLayerOutlines( aBoard, aPlotter, layer_mask, plotOpt );
  221. else
  222. PlotStandardLayer( aBoard, aPlotter, layer_mask, plotOpt );
  223. break;
  224. }
  225. }
  226. }
  227. /**
  228. * Plot a copper layer or mask.
  229. *
  230. * Silk screen layers are not plotted here.
  231. */
  232. void PlotStandardLayer( BOARD* aBoard, PLOTTER* aPlotter, LSET aLayerMask,
  233. const PCB_PLOT_PARAMS& aPlotOpt )
  234. {
  235. BRDITEMS_PLOTTER itemplotter( aPlotter, aBoard, aPlotOpt );
  236. int maxError = aBoard->GetDesignSettings().m_MaxError;
  237. itemplotter.SetLayerSet( aLayerMask );
  238. OUTLINE_MODE plotMode = aPlotOpt.GetPlotMode();
  239. bool onCopperLayer = ( LSET::AllCuMask() & aLayerMask ).any();
  240. bool onSolderMaskLayer = ( LSET( 2, F_Mask, B_Mask ) & aLayerMask ).any();
  241. bool onSolderPasteLayer = ( LSET( 2, F_Paste, B_Paste ) & aLayerMask ).any();
  242. bool onFrontFab = ( LSET( F_Fab ) & aLayerMask ).any();
  243. bool onBackFab = ( LSET( B_Fab ) & aLayerMask ).any();
  244. bool sketchPads = ( onFrontFab || onBackFab ) && aPlotOpt.GetSketchPadsOnFabLayers();
  245. // Plot edge layer and graphic items
  246. for( const BOARD_ITEM* item : aBoard->Drawings() )
  247. itemplotter.PlotBoardGraphicItem( item );
  248. // Draw footprint texts:
  249. for( const FOOTPRINT* footprint : aBoard->Footprints() )
  250. itemplotter.PlotFootprintTextItems( footprint );
  251. // Draw footprint other graphic items:
  252. for( const FOOTPRINT* footprint : aBoard->Footprints() )
  253. itemplotter.PlotFootprintGraphicItems( footprint );
  254. // Plot footprint pads
  255. for( FOOTPRINT* footprint : aBoard->Footprints() )
  256. {
  257. aPlotter->StartBlock( nullptr );
  258. for( PAD* pad : footprint->Pads() )
  259. {
  260. OUTLINE_MODE padPlotMode = plotMode;
  261. if( !( pad->GetLayerSet() & aLayerMask ).any() )
  262. {
  263. if( sketchPads &&
  264. ( ( onFrontFab && pad->GetLayerSet().Contains( F_Cu ) ) ||
  265. ( onBackFab && pad->GetLayerSet().Contains( B_Cu ) ) ) )
  266. {
  267. padPlotMode = SKETCH;
  268. }
  269. else
  270. {
  271. continue;
  272. }
  273. }
  274. if( onCopperLayer && !pad->IsOnCopperLayer() )
  275. continue;
  276. /// pads not connected to copper are optionally not drawn
  277. if( onCopperLayer && !pad->FlashLayer( aLayerMask ) )
  278. continue;
  279. COLOR4D color = COLOR4D::BLACK;
  280. // If we're plotting a single layer, the color for that layer can be used directly.
  281. if( aLayerMask.count() == 1 )
  282. {
  283. color = aPlotOpt.ColorSettings()->GetColor( aLayerMask.Seq()[0] );
  284. }
  285. else
  286. {
  287. if( ( pad->GetLayerSet() & aLayerMask )[B_Cu] )
  288. color = aPlotOpt.ColorSettings()->GetColor( B_Cu );
  289. if( ( pad->GetLayerSet() & aLayerMask )[F_Cu] )
  290. color = color.LegacyMix( aPlotOpt.ColorSettings()->GetColor( F_Cu ) );
  291. if( sketchPads && aLayerMask[F_Fab] )
  292. color = aPlotOpt.ColorSettings()->GetColor( F_Fab );
  293. else if( sketchPads && aLayerMask[B_Fab] )
  294. color = aPlotOpt.ColorSettings()->GetColor( B_Fab );
  295. }
  296. VECTOR2I margin;
  297. int width_adj = 0;
  298. if( onCopperLayer )
  299. width_adj = itemplotter.getFineWidthAdj();
  300. if( onSolderMaskLayer )
  301. margin.x = margin.y = pad->GetSolderMaskExpansion();
  302. if( onSolderPasteLayer )
  303. margin = pad->GetSolderPasteMargin();
  304. // not all shapes can have a different margin for x and y axis
  305. // in fact only oval and rect shapes can have different values.
  306. // Round shape have always the same x,y margin
  307. // so define a unique value for other shapes that do not support different values
  308. int mask_clearance = margin.x;
  309. // Now offset the pad size by margin + width_adj
  310. VECTOR2I padPlotsSize = pad->GetSize() + margin * 2 + VECTOR2I( width_adj, width_adj );
  311. // Store these parameters that can be modified to plot inflated/deflated pads shape
  312. PAD_SHAPE padShape = pad->GetShape();
  313. VECTOR2I padSize = pad->GetSize();
  314. VECTOR2I padDelta = pad->GetDelta(); // has meaning only for trapezoidal pads
  315. double padCornerRadius = pad->GetRoundRectCornerRadius();
  316. // Don't draw a 0 sized pad.
  317. // Note: a custom pad can have its pad anchor with size = 0
  318. if( pad->GetShape() != PAD_SHAPE::CUSTOM
  319. && ( padPlotsSize.x <= 0 || padPlotsSize.y <= 0 ) )
  320. {
  321. continue;
  322. }
  323. switch( pad->GetShape() )
  324. {
  325. case PAD_SHAPE::CIRCLE:
  326. case PAD_SHAPE::OVAL:
  327. pad->SetSize( padPlotsSize );
  328. if( aPlotOpt.GetSkipPlotNPTH_Pads() &&
  329. ( aPlotOpt.GetDrillMarksType() == DRILL_MARKS::NO_DRILL_SHAPE ) &&
  330. ( pad->GetSize() == pad->GetDrillSize() ) &&
  331. ( pad->GetAttribute() == PAD_ATTRIB::NPTH ) )
  332. {
  333. break;
  334. }
  335. itemplotter.PlotPad( pad, color, padPlotMode );
  336. break;
  337. case PAD_SHAPE::RECTANGLE:
  338. pad->SetSize( padPlotsSize );
  339. if( mask_clearance > 0 )
  340. {
  341. pad->SetShape( PAD_SHAPE::ROUNDRECT );
  342. pad->SetRoundRectCornerRadius( mask_clearance );
  343. }
  344. itemplotter.PlotPad( pad, color, padPlotMode );
  345. break;
  346. case PAD_SHAPE::TRAPEZOID:
  347. // inflate/deflate a trapezoid is a bit complex.
  348. // so if the margin is not null, build a similar polygonal pad shape,
  349. // and inflate/deflate the polygonal shape
  350. // because inflating/deflating using different values for y and y
  351. // we are using only margin.x as inflate/deflate value
  352. if( mask_clearance == 0 )
  353. {
  354. itemplotter.PlotPad( pad, color, padPlotMode );
  355. }
  356. else
  357. {
  358. PAD dummy( *pad );
  359. dummy.SetAnchorPadShape( PAD_SHAPE::CIRCLE );
  360. dummy.SetShape( PAD_SHAPE::CUSTOM );
  361. SHAPE_POLY_SET outline;
  362. outline.NewOutline();
  363. int dx = padSize.x / 2;
  364. int dy = padSize.y / 2;
  365. int ddx = padDelta.x / 2;
  366. int ddy = padDelta.y / 2;
  367. outline.Append( -dx - ddy, dy + ddx );
  368. outline.Append( dx + ddy, dy - ddx );
  369. outline.Append( dx - ddy, -dy + ddx );
  370. outline.Append( -dx + ddy, -dy - ddx );
  371. // Shape polygon can have holes so use InflateWithLinkedHoles(), not Inflate()
  372. // which can create bad shapes if margin.x is < 0
  373. outline.InflateWithLinkedHoles( mask_clearance,
  374. CORNER_STRATEGY::ROUND_ALL_CORNERS, maxError,
  375. SHAPE_POLY_SET::PM_FAST );
  376. dummy.DeletePrimitivesList();
  377. dummy.AddPrimitivePoly( outline, 0, true );
  378. // Be sure the anchor pad is not bigger than the deflated shape because this
  379. // anchor will be added to the pad shape when plotting the pad. So now the
  380. // polygonal shape is built, we can clamp the anchor size
  381. dummy.SetSize( VECTOR2I( 0, 0 ) );
  382. itemplotter.PlotPad( &dummy, color, padPlotMode );
  383. }
  384. break;
  385. case PAD_SHAPE::ROUNDRECT:
  386. {
  387. // rounding is stored as a percent, but we have to update this ratio
  388. // to force recalculation of other values after size changing (we do not
  389. // really change the rounding percent value)
  390. double radius_ratio = pad->GetRoundRectRadiusRatio();
  391. pad->SetSize( padPlotsSize );
  392. pad->SetRoundRectRadiusRatio( radius_ratio );
  393. itemplotter.PlotPad( pad, color, padPlotMode );
  394. break;
  395. }
  396. case PAD_SHAPE::CHAMFERED_RECT:
  397. if( mask_clearance == 0 )
  398. {
  399. // the size can be slightly inflated by width_adj (PS/PDF only)
  400. pad->SetSize( padPlotsSize );
  401. itemplotter.PlotPad( pad, color, padPlotMode );
  402. }
  403. else
  404. {
  405. // Due to the polygonal shape of a CHAMFERED_RECT pad, the best way is to
  406. // convert the pad shape to a full polygon, inflate/deflate the polygon
  407. // and use a dummy CUSTOM pad to plot the final shape.
  408. PAD dummy( *pad );
  409. // Build the dummy pad outline with coordinates relative to the pad position
  410. // and orientation 0. The actual pos and rotation will be taken in account
  411. // later by the plot function
  412. dummy.SetPosition( VECTOR2I( 0, 0 ) );
  413. dummy.SetOrientation( ANGLE_0 );
  414. SHAPE_POLY_SET outline;
  415. dummy.TransformShapeToPolygon( outline, UNDEFINED_LAYER, 0, maxError,
  416. ERROR_INSIDE );
  417. outline.InflateWithLinkedHoles( mask_clearance,
  418. CORNER_STRATEGY::ROUND_ALL_CORNERS, maxError,
  419. SHAPE_POLY_SET::PM_FAST );
  420. // Initialize the dummy pad shape:
  421. dummy.SetAnchorPadShape( PAD_SHAPE::CIRCLE );
  422. dummy.SetShape( PAD_SHAPE::CUSTOM );
  423. dummy.DeletePrimitivesList();
  424. dummy.AddPrimitivePoly( outline, 0, true );
  425. // Be sure the anchor pad is not bigger than the deflated shape because this
  426. // anchor will be added to the pad shape when plotting the pad.
  427. // So we set the anchor size to 0
  428. dummy.SetSize( VECTOR2I( 0, 0 ) );
  429. dummy.SetPosition( pad->GetPosition() );
  430. dummy.SetOrientation( pad->GetOrientation() );
  431. itemplotter.PlotPad( &dummy, color, padPlotMode );
  432. }
  433. break;
  434. case PAD_SHAPE::CUSTOM:
  435. {
  436. // inflate/deflate a custom shape is a bit complex.
  437. // so build a similar pad shape, and inflate/deflate the polygonal shape
  438. PAD dummy( *pad );
  439. SHAPE_POLY_SET shape;
  440. pad->MergePrimitivesAsPolygon( &shape );
  441. // Shape polygon can have holes so use InflateWithLinkedHoles(), not Inflate()
  442. // which can create bad shapes if margin.x is < 0
  443. shape.InflateWithLinkedHoles( mask_clearance,
  444. CORNER_STRATEGY::ROUND_ALL_CORNERS, maxError,
  445. SHAPE_POLY_SET::PM_FAST );
  446. dummy.DeletePrimitivesList();
  447. dummy.AddPrimitivePoly( shape, 0, true );
  448. // Be sure the anchor pad is not bigger than the deflated shape because this
  449. // anchor will be added to the pad shape when plotting the pad. So now the
  450. // polygonal shape is built, we can clamp the anchor size
  451. if( mask_clearance < 0 ) // we expect margin.x = margin.y for custom pads
  452. dummy.SetSize( padPlotsSize );
  453. itemplotter.PlotPad( &dummy, color, padPlotMode );
  454. break;
  455. }
  456. }
  457. // Restore the pad parameters modified by the plot code
  458. pad->SetSize( padSize );
  459. pad->SetDelta( padDelta );
  460. pad->SetShape( padShape );
  461. pad->SetRoundRectCornerRadius( padCornerRadius );
  462. }
  463. aPlotter->EndBlock( nullptr );
  464. }
  465. // Plot vias on copper layers, and if aPlotOpt.GetPlotViaOnMaskLayer() is true,
  466. // plot them on solder mask
  467. GBR_METADATA gbr_metadata;
  468. bool isOnCopperLayer = ( aLayerMask & LSET::AllCuMask() ).any();
  469. if( isOnCopperLayer )
  470. {
  471. gbr_metadata.SetApertureAttrib( GBR_APERTURE_METADATA::GBR_APERTURE_ATTRIB_VIAPAD );
  472. gbr_metadata.SetNetAttribType( GBR_NETLIST_METADATA::GBR_NETINFO_NET );
  473. }
  474. aPlotter->StartBlock( nullptr );
  475. for( const PCB_TRACK* track : aBoard->Tracks() )
  476. {
  477. if( track->Type() != PCB_VIA_T )
  478. continue;
  479. const PCB_VIA* via = static_cast<const PCB_VIA*>( track );
  480. // vias are not plotted if not on selected layer
  481. LSET via_mask_layer = via->GetLayerSet();
  482. if( !( via_mask_layer & aLayerMask ).any() )
  483. continue;
  484. int via_margin = 0;
  485. double width_adj = 0;
  486. if( aLayerMask[B_Mask] || aLayerMask[F_Mask] )
  487. via_margin = via->GetSolderMaskExpansion();
  488. if( ( aLayerMask & LSET::AllCuMask() ).any() )
  489. width_adj = itemplotter.getFineWidthAdj();
  490. int diameter = via->GetWidth() + 2 * via_margin + width_adj;
  491. /// Vias not connected to copper are optionally not drawn
  492. if( onCopperLayer && !via->FlashLayer( aLayerMask ) )
  493. continue;
  494. // Don't draw a null size item :
  495. if( diameter <= 0 )
  496. continue;
  497. // Some vias can be not connected (no net).
  498. // Set the m_NotInNet for these vias to force a empty net name in gerber file
  499. gbr_metadata.m_NetlistMetadata.m_NotInNet = via->GetNetname().IsEmpty();
  500. gbr_metadata.SetNetName( via->GetNetname() );
  501. COLOR4D color = aPlotOpt.ColorSettings()->GetColor(
  502. LAYER_VIAS + static_cast<int>( via->GetViaType() ) );
  503. // Set plot color (change WHITE to LIGHTGRAY because the white items are not seen on a
  504. // white paper or screen
  505. aPlotter->SetColor( color != WHITE ? color : LIGHTGRAY );
  506. aPlotter->FlashPadCircle( via->GetStart(), diameter, plotMode, &gbr_metadata );
  507. }
  508. aPlotter->EndBlock( nullptr );
  509. aPlotter->StartBlock( nullptr );
  510. gbr_metadata.SetApertureAttrib( GBR_APERTURE_METADATA::GBR_APERTURE_ATTRIB_CONDUCTOR );
  511. // Plot tracks (not vias) :
  512. for( const PCB_TRACK* track : aBoard->Tracks() )
  513. {
  514. if( track->Type() == PCB_VIA_T )
  515. continue;
  516. if( !aLayerMask[track->GetLayer()] )
  517. continue;
  518. // Some track segments can be not connected (no net).
  519. // Set the m_NotInNet for these segments to force a empty net name in gerber file
  520. gbr_metadata.m_NetlistMetadata.m_NotInNet = track->GetNetname().IsEmpty();
  521. gbr_metadata.SetNetName( track->GetNetname() );
  522. int width = track->GetWidth() + itemplotter.getFineWidthAdj();
  523. aPlotter->SetColor( itemplotter.getColor( track->GetLayer() ) );
  524. if( track->Type() == PCB_ARC_T )
  525. {
  526. const PCB_ARC* arc = static_cast<const PCB_ARC*>( track );
  527. // Too small arcs cannot be really handled: arc center (and arc radius)
  528. // cannot be safely computed
  529. if( !arc->IsDegenerated( 10 /* in IU */ ) )
  530. {
  531. aPlotter->ThickArc( arc->GetCenter(), arc->GetArcAngleStart(), arc->GetAngle(),
  532. arc->GetRadius(), width, plotMode, &gbr_metadata );
  533. }
  534. else
  535. {
  536. // Approximate this very small arc by a segment.
  537. aPlotter->ThickSegment( track->GetStart(), track->GetEnd(), width, plotMode,
  538. &gbr_metadata );
  539. }
  540. }
  541. else
  542. {
  543. aPlotter->ThickSegment( track->GetStart(), track->GetEnd(), width, plotMode,
  544. &gbr_metadata );
  545. }
  546. }
  547. aPlotter->EndBlock( nullptr );
  548. // Plot filled ares
  549. aPlotter->StartBlock( nullptr );
  550. NETINFO_ITEM nonet( aBoard );
  551. for( const ZONE* zone : aBoard->Zones() )
  552. {
  553. for( PCB_LAYER_ID layer : zone->GetLayerSet().Seq() )
  554. {
  555. if( !aLayerMask[layer] )
  556. continue;
  557. SHAPE_POLY_SET mainArea = zone->GetFilledPolysList( layer )->CloneDropTriangulation();
  558. SHAPE_POLY_SET islands;
  559. for( int i = mainArea.OutlineCount() - 1; i >= 0; i-- )
  560. {
  561. if( zone->IsIsland( layer, i ) )
  562. {
  563. islands.AddOutline( mainArea.CPolygon( i )[0] );
  564. mainArea.DeletePolygon( i );
  565. }
  566. }
  567. itemplotter.PlotZone( zone, layer, mainArea );
  568. if( !islands.IsEmpty() )
  569. {
  570. ZONE dummy( *zone );
  571. dummy.SetNet( &nonet );
  572. itemplotter.PlotZone( &dummy, layer, islands );
  573. }
  574. }
  575. }
  576. aPlotter->EndBlock( nullptr );
  577. // Adding drill marks, if required and if the plotter is able to plot them:
  578. if( aPlotOpt.GetDrillMarksType() != DRILL_MARKS::NO_DRILL_SHAPE )
  579. itemplotter.PlotDrillMarks();
  580. }
  581. /**
  582. * Plot outlines of copper layer.
  583. */
  584. void PlotLayerOutlines( BOARD* aBoard, PLOTTER* aPlotter, LSET aLayerMask,
  585. const PCB_PLOT_PARAMS& aPlotOpt )
  586. {
  587. BRDITEMS_PLOTTER itemplotter( aPlotter, aBoard, aPlotOpt );
  588. itemplotter.SetLayerSet( aLayerMask );
  589. SHAPE_POLY_SET outlines;
  590. for( LSEQ seq = aLayerMask.Seq( aLayerMask.SeqStackupForPlotting() ); seq; ++seq )
  591. {
  592. PCB_LAYER_ID layer = *seq;
  593. outlines.RemoveAllContours();
  594. aBoard->ConvertBrdLayerToPolygonalContours( layer, outlines );
  595. outlines.Simplify( SHAPE_POLY_SET::PM_FAST );
  596. // Plot outlines
  597. std::vector<VECTOR2I> cornerList;
  598. // Now we have one or more basic polygons: plot each polygon
  599. for( int ii = 0; ii < outlines.OutlineCount(); ii++ )
  600. {
  601. for( int kk = 0; kk <= outlines.HoleCount(ii); kk++ )
  602. {
  603. cornerList.clear();
  604. const SHAPE_LINE_CHAIN& path = ( kk == 0 ) ? outlines.COutline( ii )
  605. : outlines.CHole( ii, kk - 1 );
  606. aPlotter->PlotPoly( path, FILL_T::NO_FILL );
  607. }
  608. }
  609. // Plot pad holes
  610. if( aPlotOpt.GetDrillMarksType() != DRILL_MARKS::NO_DRILL_SHAPE )
  611. {
  612. int smallDrill = ( aPlotOpt.GetDrillMarksType() == DRILL_MARKS::SMALL_DRILL_SHAPE )
  613. ? pcbIUScale.mmToIU( ADVANCED_CFG::GetCfg().m_SmallDrillMarkSize )
  614. : INT_MAX;
  615. for( FOOTPRINT* footprint : aBoard->Footprints() )
  616. {
  617. for( PAD* pad : footprint->Pads() )
  618. {
  619. if( pad->HasHole() )
  620. {
  621. std::shared_ptr<SHAPE_SEGMENT> slot = pad->GetEffectiveHoleShape();
  622. if( slot->GetSeg().A == slot->GetSeg().B ) // circular hole
  623. {
  624. int drill = std::min( smallDrill, slot->GetWidth() );
  625. aPlotter->Circle( pad->GetPosition(), drill, FILL_T::NO_FILL );
  626. }
  627. else
  628. {
  629. // Note: small drill marks have no significance when applied to slots
  630. aPlotter->ThickSegment( slot->GetSeg().A, slot->GetSeg().B,
  631. slot->GetWidth(), SKETCH, nullptr );
  632. }
  633. }
  634. }
  635. }
  636. }
  637. // Plot vias holes
  638. for( PCB_TRACK* track : aBoard->Tracks() )
  639. {
  640. if( track->Type() != PCB_VIA_T )
  641. continue;
  642. const PCB_VIA* via = static_cast<const PCB_VIA*>( track );
  643. if( via->IsOnLayer( layer ) ) // via holes can be not through holes
  644. aPlotter->Circle( via->GetPosition(), via->GetDrillValue(), FILL_T::NO_FILL );
  645. }
  646. }
  647. }
  648. /**
  649. * Plot a solder mask layer.
  650. *
  651. * Solder mask layers have a minimum thickness value and cannot be drawn like standard layers,
  652. * unless the minimum thickness is 0.
  653. *
  654. * The algorithm is somewhat complicated to allow for min web thickness while also preserving
  655. * pad attributes in Gerber.
  656. *
  657. * 1 - create initial polygons for every shape
  658. * 2 - inflate and deflate polygons with Min Thickness/2, and merges the result
  659. * 3 - substract all initial polygons from (2), leaving the areas where the thickness was less
  660. * than min thickness
  661. * 4 - plot all initial shapes by flashing (or using regions), including Gerber attribute data
  662. * 5 - plot remaining polygons from (2) (witout any Gerber attributes)
  663. */
  664. void PlotSolderMaskLayer( BOARD *aBoard, PLOTTER* aPlotter, LSET aLayerMask,
  665. const PCB_PLOT_PARAMS& aPlotOpt, int aMinThickness )
  666. {
  667. int maxError = aBoard->GetDesignSettings().m_MaxError;
  668. PCB_LAYER_ID layer = aLayerMask[B_Mask] ? B_Mask : F_Mask;
  669. SHAPE_POLY_SET buffer;
  670. SHAPE_POLY_SET* boardOutline = nullptr;
  671. if( aBoard->GetBoardPolygonOutlines( buffer ) )
  672. boardOutline = &buffer;
  673. // We remove 1nm as we expand both sides of the shapes, so allowing for a strictly greater
  674. // than or equal comparison in the shape separation (boolean add)
  675. int inflate = aMinThickness / 2 - 1;
  676. BRDITEMS_PLOTTER itemplotter( aPlotter, aBoard, aPlotOpt );
  677. itemplotter.SetLayerSet( aLayerMask );
  678. // Build polygons for each pad shape. The size of the shape on solder mask should be size
  679. // of pad + clearance around the pad, where clearance = solder mask clearance + extra margin.
  680. // Extra margin is half the min width for solder mask, which is used to merge too-close shapes
  681. // (distance < aMinThickness), and will be removed when creating the actual shapes.
  682. // Will contain shapes inflated by inflate value that will be merged and deflated by inflate
  683. // value to build final polygons
  684. SHAPE_POLY_SET areas;
  685. // Will contain exact shapes of all items on solder mask
  686. SHAPE_POLY_SET initialPolys;
  687. auto plotFPTextItem =
  688. [&]( const PCB_TEXT& aText )
  689. {
  690. if( !itemplotter.GetPlotFPText() )
  691. return;
  692. if( !aText.IsVisible() && !itemplotter.GetPlotInvisibleText() )
  693. return;
  694. if( aText.GetText() == wxT( "${REFERENCE}" ) && !itemplotter.GetPlotReference() )
  695. return;
  696. if( aText.GetText() == wxT( "${VALUE}" ) && !itemplotter.GetPlotValue() )
  697. return;
  698. // add shapes with their exact mask layer size in initialPolys
  699. aText.TransformTextToPolySet( initialPolys, 0, maxError, ERROR_OUTSIDE );
  700. // add shapes inflated by aMinThickness/2 in areas
  701. aText.TransformTextToPolySet( areas, inflate, maxError, ERROR_OUTSIDE );
  702. };
  703. // Generate polygons with arcs inside the shape or exact shape to minimize shape changes
  704. // created by arc to segment size correction.
  705. DISABLE_ARC_RADIUS_CORRECTION disabler;
  706. {
  707. // Plot footprint pads and graphics
  708. for( const FOOTPRINT* footprint : aBoard->Footprints() )
  709. {
  710. // add shapes with their exact mask layer size in initialPolys
  711. footprint->TransformPadsToPolySet( initialPolys, layer, 0, maxError, ERROR_OUTSIDE );
  712. // add shapes inflated by aMinThickness/2 in areas
  713. footprint->TransformPadsToPolySet( areas, layer, inflate, maxError, ERROR_OUTSIDE );
  714. if( itemplotter.GetPlotReference() && footprint->Reference().IsOnLayer( layer ) )
  715. plotFPTextItem( footprint->Reference() );
  716. if( itemplotter.GetPlotValue() && footprint->Value().IsOnLayer( layer ) )
  717. plotFPTextItem( footprint->Value() );
  718. for( const PCB_FIELD* field : footprint->Fields() )
  719. {
  720. if( field->IsOnLayer( layer ) )
  721. plotFPTextItem( static_cast<const PCB_TEXT&>( *field ) );
  722. }
  723. for( const BOARD_ITEM* item : footprint->GraphicalItems() )
  724. {
  725. if( item->IsOnLayer( layer ) )
  726. {
  727. if( item->Type() == PCB_TEXT_T )
  728. {
  729. plotFPTextItem( static_cast<const PCB_TEXT&>( *item ) );
  730. }
  731. else
  732. {
  733. // add shapes with their exact mask layer size in initialPolys
  734. item->TransformShapeToPolygon( initialPolys, layer, 0, maxError,
  735. ERROR_OUTSIDE );
  736. // add shapes inflated by aMinThickness/2 in areas
  737. item->TransformShapeToPolygon( areas, layer, inflate, maxError,
  738. ERROR_OUTSIDE );
  739. }
  740. }
  741. }
  742. }
  743. // Plot (untented) vias
  744. for( const PCB_TRACK* track : aBoard->Tracks() )
  745. {
  746. if( track->Type() != PCB_VIA_T )
  747. continue;
  748. const PCB_VIA* via = static_cast<const PCB_VIA*>( track );
  749. // Note: IsOnLayer() checks relevant mask layers of untented vias
  750. if( !via->IsOnLayer( layer ) )
  751. continue;
  752. int clearance = via->GetSolderMaskExpansion();
  753. // add shapes with their exact mask layer size in initialPolys
  754. via->TransformShapeToPolygon( initialPolys, layer, clearance, maxError, ERROR_OUTSIDE );
  755. // add shapes inflated by aMinThickness/2 in areas
  756. clearance += inflate;
  757. via->TransformShapeToPolygon( areas, layer, clearance, maxError, ERROR_OUTSIDE );
  758. }
  759. // Add filled zone areas.
  760. #if 0 // Set to 1 if a solder mask expansion must be applied to zones on solder mask
  761. int zone_margin = aBoard->GetDesignSettings().m_SolderMaskExpansion;
  762. #else
  763. int zone_margin = 0;
  764. #endif
  765. for( const BOARD_ITEM* item : aBoard->Drawings() )
  766. {
  767. if( item->IsOnLayer( layer ) )
  768. {
  769. if( item->Type() == PCB_TEXT_T )
  770. {
  771. const PCB_TEXT* text = static_cast<const PCB_TEXT*>( item );
  772. // add shapes with their exact mask layer size in initialPolys
  773. text->TransformTextToPolySet( initialPolys, 0, maxError, ERROR_OUTSIDE );
  774. // add shapes inflated by aMinThickness/2 in areas
  775. text->TransformTextToPolySet( areas, inflate, maxError, ERROR_OUTSIDE );
  776. }
  777. else
  778. {
  779. // add shapes with their exact mask layer size in initialPolys
  780. item->TransformShapeToPolygon( initialPolys, layer, 0, maxError,
  781. ERROR_OUTSIDE );
  782. // add shapes inflated by aMinThickness/2 in areas
  783. item->TransformShapeToPolygon( areas, layer, inflate, maxError,
  784. ERROR_OUTSIDE );
  785. }
  786. }
  787. }
  788. for( ZONE* zone : aBoard->Zones() )
  789. {
  790. if( !zone->IsOnLayer( layer ) )
  791. continue;
  792. // add shapes inflated by aMinThickness/2 in areas
  793. zone->TransformSmoothedOutlineToPolygon( areas, inflate + zone_margin, maxError,
  794. ERROR_OUTSIDE, boardOutline );
  795. // add shapes with their exact mask layer size in initialPolys
  796. zone->TransformSmoothedOutlineToPolygon( initialPolys, zone_margin, maxError,
  797. ERROR_OUTSIDE, boardOutline );
  798. }
  799. }
  800. // Merge all polygons: After deflating, not merged (not overlapping) polygons will have the
  801. // initial shape (with perhaps small changes due to deflating transform)
  802. areas.Simplify( SHAPE_POLY_SET::PM_STRICTLY_SIMPLE );
  803. areas.Deflate( inflate, CORNER_STRATEGY::CHAMFER_ALL_CORNERS, maxError );
  804. // To avoid a lot of code, use a ZONE to handle and plot polygons, because our polygons look
  805. // exactly like filled areas in zones.
  806. // Note, also this code is not optimized: it creates a lot of copy/duplicate data.
  807. // However it is not complex, and fast enough for plot purposes (copy/convert data is only a
  808. // very small calculation time for these calculations).
  809. ZONE zone( aBoard );
  810. zone.SetMinThickness( 0 ); // trace polygons only
  811. zone.SetLayer( layer );
  812. // Combine the current areas to initial areas. This is mandatory because inflate/deflate
  813. // transform is not perfect, and we want the initial areas perfectly kept
  814. areas.BooleanAdd( initialPolys, SHAPE_POLY_SET::PM_FAST );
  815. areas.Fracture( SHAPE_POLY_SET::PM_STRICTLY_SIMPLE );
  816. itemplotter.PlotZone( &zone, layer, areas );
  817. }
  818. /**
  819. * Set up most plot options for plotting a board (especially the viewport)
  820. * Important thing:
  821. * page size is the 'drawing' page size,
  822. * paper size is the physical page size
  823. */
  824. static void initializePlotter( PLOTTER* aPlotter, const BOARD* aBoard,
  825. const PCB_PLOT_PARAMS* aPlotOpts )
  826. {
  827. PAGE_INFO pageA4( wxT( "A4" ) );
  828. const PAGE_INFO& pageInfo = aBoard->GetPageSettings();
  829. const PAGE_INFO* sheet_info;
  830. double paperscale; // Page-to-paper ratio
  831. VECTOR2I paperSizeIU;
  832. VECTOR2I pageSizeIU( pageInfo.GetSizeIU( pcbIUScale.IU_PER_MILS ) );
  833. bool autocenter = false;
  834. // Special options: to fit the sheet to an A4 sheet replace the paper size. However there
  835. // is a difference between the autoscale and the a4paper option:
  836. // - Autoscale fits the board to the paper size
  837. // - A4paper fits the original paper size to an A4 sheet
  838. // - Both of them fit the board to an A4 sheet
  839. if( aPlotOpts->GetA4Output() )
  840. {
  841. sheet_info = &pageA4;
  842. paperSizeIU = pageA4.GetSizeIU( pcbIUScale.IU_PER_MILS );
  843. paperscale = (double) paperSizeIU.x / pageSizeIU.x;
  844. autocenter = true;
  845. }
  846. else
  847. {
  848. sheet_info = &pageInfo;
  849. paperSizeIU = pageSizeIU;
  850. paperscale = 1;
  851. // Need autocentering only if scale is not 1:1
  852. autocenter = (aPlotOpts->GetScale() != 1.0);
  853. }
  854. BOX2I bbox = aBoard->ComputeBoundingBox();
  855. VECTOR2I boardCenter = bbox.Centre();
  856. VECTOR2I boardSize = bbox.GetSize();
  857. double compound_scale;
  858. // Fit to 80% of the page if asked; it could be that the board is empty, in this case
  859. // regress to 1:1 scale
  860. if( aPlotOpts->GetAutoScale() && boardSize.x > 0 && boardSize.y > 0 )
  861. {
  862. double xscale = (paperSizeIU.x * 0.8) / boardSize.x;
  863. double yscale = (paperSizeIU.y * 0.8) / boardSize.y;
  864. compound_scale = std::min( xscale, yscale ) * paperscale;
  865. }
  866. else
  867. {
  868. compound_scale = aPlotOpts->GetScale() * paperscale;
  869. }
  870. // For the plot offset we have to keep in mind the auxiliary origin too: if autoscaling is
  871. // off we check that plot option (i.e. autoscaling overrides auxiliary origin)
  872. VECTOR2I offset( 0, 0);
  873. if( autocenter )
  874. {
  875. offset.x = KiROUND( boardCenter.x - ( paperSizeIU.x / 2.0 ) / compound_scale );
  876. offset.y = KiROUND( boardCenter.y - ( paperSizeIU.y / 2.0 ) / compound_scale );
  877. }
  878. else
  879. {
  880. if( aPlotOpts->GetUseAuxOrigin() )
  881. offset = aBoard->GetDesignSettings().GetAuxOrigin();
  882. }
  883. aPlotter->SetPageSettings( *sheet_info );
  884. aPlotter->SetViewport( offset, pcbIUScale.IU_PER_MILS/10, compound_scale, aPlotOpts->GetMirror() );
  885. // Has meaning only for gerber plotter. Must be called only after SetViewport
  886. aPlotter->SetGerberCoordinatesFormat( aPlotOpts->GetGerberPrecision() );
  887. // Has meaning only for SVG plotter. Must be called only after SetViewport
  888. aPlotter->SetSvgCoordinatesFormat( aPlotOpts->GetSvgPrecision() );
  889. aPlotter->SetCreator( wxT( "PCBNEW" ) );
  890. aPlotter->SetColorMode( !aPlotOpts->GetBlackAndWhite() ); // default is plot in Black and White.
  891. aPlotter->SetTextMode( aPlotOpts->GetTextMode() );
  892. }
  893. /**
  894. * Prefill in black an area a little bigger than the board to prepare for the negative plot
  895. */
  896. static void FillNegativeKnockout( PLOTTER *aPlotter, const BOX2I &aBbbox )
  897. {
  898. const int margin = 5 * pcbIUScale.IU_PER_MM; // Add a 5 mm margin around the board
  899. aPlotter->SetNegative( true );
  900. aPlotter->SetColor( WHITE ); // Which will be plotted as black
  901. BOX2I area = aBbbox;
  902. area.Inflate( margin );
  903. aPlotter->Rect( area.GetOrigin(), area.GetEnd(), FILL_T::FILLED_SHAPE );
  904. aPlotter->SetColor( BLACK );
  905. }
  906. /**
  907. * Calculate the effective size of HPGL pens and set them in the plotter object
  908. */
  909. static void ConfigureHPGLPenSizes( HPGL_PLOTTER *aPlotter, const PCB_PLOT_PARAMS *aPlotOpts )
  910. {
  911. // Compute penDiam (the value is given in mils) in pcb units, with plot scale (if Scale is 2,
  912. // penDiam value is always m_HPGLPenDiam so apparent penDiam is actually penDiam / Scale
  913. int penDiam = KiROUND( aPlotOpts->GetHPGLPenDiameter() * pcbIUScale.IU_PER_MILS / aPlotOpts->GetScale() );
  914. // Set HPGL-specific options and start
  915. aPlotter->SetPenSpeed( aPlotOpts->GetHPGLPenSpeed() );
  916. aPlotter->SetPenNumber( aPlotOpts->GetHPGLPenNum() );
  917. aPlotter->SetPenDiameter( penDiam );
  918. }
  919. /**
  920. * Open a new plotfile using the options (and especially the format) specified in the options
  921. * and prepare the page for plotting.
  922. *
  923. * @return the plotter object if OK, NULL if the file is not created (or has a problem).
  924. */
  925. PLOTTER* StartPlotBoard( BOARD *aBoard, const PCB_PLOT_PARAMS *aPlotOpts, int aLayer,
  926. const wxString& aFullFileName, const wxString& aSheetName,
  927. const wxString& aSheetPath )
  928. {
  929. // Create the plotter driver and set the few plotter specific options
  930. PLOTTER* plotter = nullptr;
  931. switch( aPlotOpts->GetFormat() )
  932. {
  933. case PLOT_FORMAT::DXF:
  934. DXF_PLOTTER* DXF_plotter;
  935. DXF_plotter = new DXF_PLOTTER();
  936. DXF_plotter->SetUnits( aPlotOpts->GetDXFPlotUnits() );
  937. plotter = DXF_plotter;
  938. break;
  939. case PLOT_FORMAT::POST:
  940. PS_PLOTTER* PS_plotter;
  941. PS_plotter = new PS_PLOTTER();
  942. PS_plotter->SetScaleAdjust( aPlotOpts->GetFineScaleAdjustX(),
  943. aPlotOpts->GetFineScaleAdjustY() );
  944. plotter = PS_plotter;
  945. break;
  946. case PLOT_FORMAT::PDF:
  947. plotter = new PDF_PLOTTER();
  948. break;
  949. case PLOT_FORMAT::HPGL:
  950. HPGL_PLOTTER* HPGL_plotter;
  951. HPGL_plotter = new HPGL_PLOTTER();
  952. // HPGL options are a little more convoluted to compute, so they get their own function
  953. ConfigureHPGLPenSizes( HPGL_plotter, aPlotOpts );
  954. plotter = HPGL_plotter;
  955. break;
  956. case PLOT_FORMAT::GERBER:
  957. // For Gerber plotter, a valid board layer must be set, in order to create a valid
  958. // Gerber header, especially the TF.FileFunction and .FilePolarity data
  959. if( aLayer < PCBNEW_LAYER_ID_START || aLayer >= PCB_LAYER_ID_COUNT )
  960. {
  961. wxLogError( wxString::Format(
  962. "Invalid board layer %d, cannot build a valid Gerber file header",
  963. aLayer ) );
  964. }
  965. plotter = new GERBER_PLOTTER();
  966. break;
  967. case PLOT_FORMAT::SVG:
  968. plotter = new SVG_PLOTTER();
  969. break;
  970. default:
  971. wxASSERT( false );
  972. return nullptr;
  973. }
  974. KIGFX::PCB_RENDER_SETTINGS* renderSettings = new KIGFX::PCB_RENDER_SETTINGS();
  975. renderSettings->LoadColors( aPlotOpts->ColorSettings() );
  976. renderSettings->SetDefaultPenWidth( pcbIUScale.mmToIU( 0.0212 ) ); // Hairline at 1200dpi
  977. if( aLayer >= 0 && aLayer < GAL_LAYER_ID_END )
  978. renderSettings->SetLayerName( aBoard->GetLayerName( ToLAYER_ID( aLayer ) ) );
  979. plotter->SetRenderSettings( renderSettings );
  980. // Compute the viewport and set the other options
  981. // page layout is not mirrored, so temporarily change mirror option for the page layout
  982. PCB_PLOT_PARAMS plotOpts = *aPlotOpts;
  983. if( plotOpts.GetPlotFrameRef() && plotOpts.GetMirror() )
  984. plotOpts.SetMirror( false );
  985. initializePlotter( plotter, aBoard, &plotOpts );
  986. if( plotter->OpenFile( aFullFileName ) )
  987. {
  988. plotter->ClearHeaderLinesList();
  989. // For the Gerber "file function" attribute, set the layer number
  990. if( plotter->GetPlotterType() == PLOT_FORMAT::GERBER )
  991. {
  992. bool useX2mode = plotOpts.GetUseGerberX2format();
  993. GERBER_PLOTTER* gbrplotter = static_cast <GERBER_PLOTTER*> ( plotter );
  994. gbrplotter->DisableApertMacros( plotOpts.GetDisableGerberMacros() );
  995. gbrplotter->UseX2format( useX2mode );
  996. gbrplotter->UseX2NetAttributes( plotOpts.GetIncludeGerberNetlistInfo() );
  997. // Attributes can be added using X2 format or as comment (X1 format)
  998. AddGerberX2Attribute( plotter, aBoard, aLayer, not useX2mode );
  999. }
  1000. if( plotter->StartPlot( wxT( "1" ) ) )
  1001. {
  1002. // Plot the frame reference if requested
  1003. if( aPlotOpts->GetPlotFrameRef() )
  1004. {
  1005. PlotDrawingSheet( plotter, aBoard->GetProject(), aBoard->GetTitleBlock(),
  1006. aBoard->GetPageSettings(), &aBoard->GetProperties(), wxT( "1" ),
  1007. 1, aSheetName, aSheetPath, aBoard->GetFileName(),
  1008. renderSettings->GetLayerColor( LAYER_DRAWINGSHEET ) );
  1009. if( aPlotOpts->GetMirror() )
  1010. initializePlotter( plotter, aBoard, aPlotOpts );
  1011. }
  1012. // When plotting a negative board: draw a black rectangle (background for plot board
  1013. // in white) and switch the current color to WHITE; note the color inversion is actually
  1014. // done in the driver (if supported)
  1015. if( aPlotOpts->GetNegative() )
  1016. {
  1017. BOX2I bbox = aBoard->ComputeBoundingBox();
  1018. FillNegativeKnockout( plotter, bbox );
  1019. }
  1020. return plotter;
  1021. }
  1022. }
  1023. delete plotter->RenderSettings();
  1024. delete plotter;
  1025. return nullptr;
  1026. }