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.

1388 lines
53 KiB

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