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.

1206 lines
46 KiB

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