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.

1317 lines
52 KiB

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