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.

1278 lines
49 KiB

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