KiCad PCB EDA Suite
plotter_gerber.h
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2020 Jean-Pierre Charras, jp.charras at wanadoo.fr
5  * Copyright (C) 2016-2020 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * This program is free software: you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the
9  * Free Software Foundation, either version 3 of the License, or (at your
10  * option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
27 #pragma once
28 
29 #include <vector>
30 #include <math/box2.h>
31 #include <eda_item.h> // FILL_TYPE
32 
33 #include <plotter.h>
34 #include "gbr_plotter_apertures.h"
35 
36 
37 class GERBER_PLOTTER : public PLOTTER
38 {
39 public:
41 
42  virtual PLOT_FORMAT GetPlotterType() const override
43  {
44  return PLOT_FORMAT::GERBER;
45  }
46 
47  static wxString GetDefaultFileExtension()
48  {
49  return wxString( wxT( "gbr" ) );
50  }
51 
57  virtual bool StartPlot() override;
58  virtual bool EndPlot() override;
59  virtual void SetCurrentLineWidth( int width, void* aData = NULL ) override;
60 
61  // RS274X has no dashing, nor colours
62  virtual void SetDash( PLOT_DASH_TYPE dashed ) override
63  {
64  }
65 
66  virtual void SetColor( COLOR4D color ) override {}
67  // Currently, aScale and aMirror are not used in gerber plotter
68  virtual void SetViewport( const wxPoint& aOffset, double aIusPerDecimil,
69  double aScale, bool aMirror ) override;
70 
71  // Basic plot primitives
72  virtual void Rect( const wxPoint& p1, const wxPoint& p2, FILL_TYPE fill,
73  int width = USE_DEFAULT_LINE_WIDTH ) override;
74  virtual void Circle( const wxPoint& pos, int diametre, FILL_TYPE fill,
75  int width = USE_DEFAULT_LINE_WIDTH ) override;
76  virtual void Arc( const wxPoint& aCenter, double aStAngle, double aEndAngle,
77  int aRadius, FILL_TYPE aFill, int aWidth = USE_DEFAULT_LINE_WIDTH ) override;
78 
79  // These functions plot an item and manage X2 gerber attributes
80  virtual void ThickSegment( const wxPoint& start, const wxPoint& end, int width,
81  OUTLINE_MODE tracemode, void* aData ) override;
82 
83  virtual void ThickArc( const wxPoint& centre, double StAngle, double EndAngle,
84  int rayon, int width, OUTLINE_MODE tracemode, void* aData ) override;
85  virtual void ThickRect( const wxPoint& p1, const wxPoint& p2, int width,
86  OUTLINE_MODE tracemode, void* aData ) override;
87  virtual void ThickCircle( const wxPoint& pos, int diametre, int width,
88  OUTLINE_MODE tracemode, void* aData ) override;
89  virtual void FilledCircle( const wxPoint& pos, int diametre,
90  OUTLINE_MODE tracemode, void* aData ) override;
91 
96  virtual void PlotPoly( const std::vector< wxPoint >& aCornerList,
97  FILL_TYPE aFill, int aWidth = USE_DEFAULT_LINE_WIDTH,
98  void* aData = nullptr ) override;
99 
100  virtual void PenTo( const wxPoint& pos, char plume ) override;
101 
102  virtual void Text( const wxPoint& aPos,
103  const COLOR4D aColor,
104  const wxString& aText,
105  double aOrient,
106  const wxSize& aSize,
107  enum EDA_TEXT_HJUSTIFY_T aH_justify,
108  enum EDA_TEXT_VJUSTIFY_T aV_justify,
109  int aWidth,
110  bool aItalic,
111  bool aBold,
112  bool aMultilineAllowed = false,
113  void* aData = NULL ) override;
114 
118  virtual void FlashPadCircle( const wxPoint& pos, int diametre,
119  OUTLINE_MODE trace_mode, void* aData ) override;
120 
121  virtual void FlashPadOval( const wxPoint& aPadPos, const wxSize& size, double orient,
122  OUTLINE_MODE trace_mode, void* aData ) override;
123 
124  virtual void FlashPadRect( const wxPoint& aPadPos, const wxSize& size,
125  double orient, OUTLINE_MODE trace_mode, void* aData ) override;
126 
127  virtual void FlashPadRoundRect( const wxPoint& aPadPos, const wxSize& aSize,
128  int aCornerRadius, double aOrient,
129  OUTLINE_MODE aTraceMode, void* aData ) override;
130  virtual void FlashPadCustom( const wxPoint& aPadPos, const wxSize& aSize,
131  double aPadOrient, SHAPE_POLY_SET* aPolygons,
132  OUTLINE_MODE aTraceMode, void* aData ) override;
133 
134  virtual void FlashPadTrapez( const wxPoint& aPadPos, const wxPoint *aCorners,
135  double aPadOrient, OUTLINE_MODE aTraceMode, void* aData ) override;
136 
137  virtual void FlashRegularPolygon( const wxPoint& aShapePos, int aDiameter, int aCornerCount,
138  double aOrient, OUTLINE_MODE aTraceMode, void* aData ) override;
139 
156  void FlashPadChamferRoundRect( const wxPoint& aShapePos, const wxSize& aPadSize,
157  int aCornerRadius, double aChamferRatio,
158  int aChamferPositions,
159  double aPadOrient, OUTLINE_MODE aPlotMode, void* aData );
160 
166  void PlotGerberRegion( const std::vector< wxPoint >& aCornerList,
167  void * aData = NULL );
168 
173  virtual void SetLayerPolarity( bool aPositive ) override;
174 
185  virtual void SetGerberCoordinatesFormat( int aResolution, bool aUseInches = false ) override;
186 
187  void UseX2format( bool aEnable ) { m_useX2format = aEnable; }
188  void UseX2NetAttributes( bool aEnable ) { m_useNetAttributes = aEnable; }
189 
194  void DisableApertMacros( bool aDisable ) { m_gerberDisableApertMacros = aDisable; }
195 
201  virtual void StartBlock( void* aData ) override;
202 
209  virtual void EndBlock( void* aData ) override;
210 
214  void ClearAllAttributes();
215 
226  int GetOrCreateAperture( const wxSize& aSize, int aRadius, double aRotDegree,
227  APERTURE::APERTURE_TYPE aType, int aApertureAttribute );
228 
238  int GetOrCreateAperture( const std::vector<wxPoint>& aCorners, double aRotDegree,
239  APERTURE::APERTURE_TYPE aType, int aApertureAttribute );
240 
241 protected:
250  void plotRoundRectAsRegion( const wxPoint& aRectCenter, const wxSize& aSize,
251  int aCornerRadius, double aOrient );
263  void plotArc( const wxPoint& aCenter, double aStAngle, double aEndAngle,
264  int aRadius, bool aPlotInRegion );
265 
271  void selectAperture( const wxSize& aSize, int aRadius, double aRotDegree,
273  int aApertureAttribute );
280  void selectAperture( const std::vector<wxPoint>& aCorners, double aPolygonRotation,
281  APERTURE::APERTURE_TYPE aType, int aApertureAttribute );
282 
290  void selectAperture( int aDiameter, double aRotDegree,
291  APERTURE::APERTURE_TYPE aType, int aApertureAttribute );
292 
298  void emitDcode( const DPOINT& pt, int dcode );
299 
308 
314  void clearNetAttribute();
315 
316  // the attributes dictionary created/modifed by %TO, attached to objects, when they are created
317  // by D01, D03, G36/G37 commands
318  // standard attributes are .P, .C and .N
319  // this is used by gerber readers when creating a new object. Cleared by %TD command
320  // Note: m_objectAttributesDictionnary can store more than one attribute
321  // the string stores the line(s) actually written to the gerber file
322  // it can store a .P, .C or .N attribute, or 2 or 3 attributes, separated by a \n char (EOL)
324 
325  // The last aperture attribute generated (only one aperture attribute can be set)
327 
328  FILE* workFile;
329  FILE* finalFile;
330  wxString m_workFilename;
331 
335  void writeApertureList();
336 
337  std::vector<APERTURE> m_apertures; // The list of available apertures
338  int m_currentApertureIdx; // The index of the current aperture in m_apertures
339  bool m_hasApertureRoundRect; // true is at least one round rect aperture is in use
340  bool m_hasApertureRotOval; // true is at least one oval rotated aperture is in use
341  bool m_hasApertureRotRect; // true is at least one rect. rotated aperture is in use
342  bool m_hasApertureOutline4P; // true is at least one 4 corners outline (free polygon
343  // with 4 corners) aperture is in use
344  bool m_hasApertureChamferedRect; // true is at least one chamfered rect is in use
345  // (with no rounded corner)
346 
347  bool m_gerberUnitInch; // true if the gerber units are inches, false for mm
348  int m_gerberUnitFmt; // number of digits in mantissa.
349  // usually 6 in Inches and 5 or 6 in mm
350  bool m_gerberDisableApertMacros; // True to disable Aperture Macro (AM) command,
351  // for broken Gerber Readers
352  // Regions will be used instead of AM shapes
353  bool m_useX2format; // Add X2 file header attributes. If false, attributes
354  // will be added as comments.
355  bool m_useNetAttributes; // In recent gerber files, netlist info can be added.
356  // It will be added if this param is true, using X2 or
357  // X1 format
358 
359  // A list of aperture macros defined "on the fly" because the number of parameters is not
360  // defined: this is the case of the macro using the primitive 4 to create a polygon.
361  // The number of vertices is not known for free polygonal shapes, and an aperture macro
362  // must be created for each specific polygon
364 };
virtual void FlashRegularPolygon(const wxPoint &aShapePos, int aDiameter, int aCornerCount, double aOrient, OUTLINE_MODE aTraceMode, void *aData) override
Flash a regular polygon.
OUTLINE_MODE
Definition: outline_mode.h:24
void writeApertureList()
Generate the table of D codes.
void clearNetAttribute()
clear a Gerber net attribute record (clear object attribute dictionary) and output the clear object a...
Plot settings, and plotting engines (PostScript, Gerber, HPGL and DXF)
EDA_TEXT_HJUSTIFY_T
Definition: eda_text.h:61
virtual void PenTo(const wxPoint &pos, char plume) override
moveto/lineto primitive, moves the 'pen' to the specified direction
void UseX2NetAttributes(bool aEnable)
void plotRoundRectAsRegion(const wxPoint &aRectCenter, const wxSize &aSize, int aCornerRadius, double aOrient)
Plot a round rect (a round rect shape in fact) as a Gerber region using lines and arcs for corners.
FILL_TYPE
The set of fill types used in plotting or drawing enclosed areas.
Definition: fill_type.h:28
virtual void SetLayerPolarity(bool aPositive) override
Change the plot polarity and begin a new layer Used to 'scratch off' silk screen away from solder mas...
virtual void EndBlock(void *aData) override
calling this function allows one to define the end of a group of drawing items the group is started b...
bool m_hasApertureChamferedRect
virtual void ThickSegment(const wxPoint &start, const wxPoint &end, int width, OUTLINE_MODE tracemode, void *aData) override
void FlashPadChamferRoundRect(const wxPoint &aShapePos, const wxSize &aPadSize, int aCornerRadius, double aChamferRatio, int aChamferPositions, double aPadOrient, OUTLINE_MODE aPlotMode, void *aData)
flash a chamfered round rect pad.
void formatNetAttribute(GBR_NETLIST_METADATA *aData)
print a Gerber net attribute object record.
virtual void FlashPadTrapez(const wxPoint &aPadPos, const wxPoint *aCorners, double aPadOrient, OUTLINE_MODE aTraceMode, void *aData) override
Flash a trapezoidal pad.
int color
Definition: DXF_plotter.cpp:60
void UseX2format(bool aEnable)
void plotArc(const wxPoint &aCenter, double aStAngle, double aEndAngle, int aRadius, bool aPlotInRegion)
Plot a Gerber arc.
virtual void ThickArc(const wxPoint &centre, double StAngle, double EndAngle, int rayon, int width, OUTLINE_MODE tracemode, void *aData) override
APER_MACRO_FREEPOLY_LIST m_am_freepoly_list
Information which can be added in a gerber file as attribute of an object.
static wxString GetDefaultFileExtension()
virtual void FlashPadRect(const wxPoint &aPadPos, const wxSize &size, double orient, OUTLINE_MODE trace_mode, void *aData) override
bool m_hasApertureOutline4P
virtual bool StartPlot() override
Function StartPlot Write GERBER header to file initialize global variable g_Plot_PlotOutputFile.
bool m_hasApertureRoundRect
virtual void Text(const wxPoint &aPos, const COLOR4D aColor, const wxString &aText, double aOrient, const wxSize &aSize, enum EDA_TEXT_HJUSTIFY_T aH_justify, enum EDA_TEXT_VJUSTIFY_T aV_justify, int aWidth, bool aItalic, bool aBold, bool aMultilineAllowed=false, void *aData=NULL) override
Draws text with the plotter.
virtual void PlotPoly(const std::vector< wxPoint > &aCornerList, FILL_TYPE aFill, int aWidth=USE_DEFAULT_LINE_WIDTH, void *aData=nullptr) override
Gerber polygon: they can (and should) be filled with the appropriate G36/G37 sequence.
virtual void FlashPadCustom(const wxPoint &aPadPos, const wxSize &aSize, double aPadOrient, SHAPE_POLY_SET *aPolygons, OUTLINE_MODE aTraceMode, void *aData) override
static const int USE_DEFAULT_LINE_WIDTH
Definition: plotter.h:126
std::vector< APERTURE > m_apertures
virtual void Arc(const wxPoint &aCenter, double aStAngle, double aEndAngle, int aRadius, FILL_TYPE aFill, int aWidth=USE_DEFAULT_LINE_WIDTH) override
Generic fallback: arc rendered as a polyline.
void ClearAllAttributes()
Remove (clear) all attributes from object attributes dictionary (TO.
#define NULL
virtual void SetCurrentLineWidth(int width, void *aData=NULL) override
Set the line width for the next drawing.
virtual void FlashPadRoundRect(const wxPoint &aPadPos, const wxSize &aSize, int aCornerRadius, double aOrient, OUTLINE_MODE aTraceMode, void *aData) override
virtual void ThickCircle(const wxPoint &pos, int diametre, int width, OUTLINE_MODE tracemode, void *aData) override
Represent a set of closed polygons.
PLOT_FORMAT
The set of supported output plot formats.
Definition: plotter.h:67
virtual void Circle(const wxPoint &pos, int diametre, FILL_TYPE fill, int width=USE_DEFAULT_LINE_WIDTH) override
virtual void SetViewport(const wxPoint &aOffset, double aIusPerDecimil, double aScale, bool aMirror) override
Set the plot offset and scaling for the current plot.
virtual void FlashPadOval(const wxPoint &aPadPos, const wxSize &size, double orient, OUTLINE_MODE trace_mode, void *aData) override
void emitDcode(const DPOINT &pt, int dcode)
Emit a D-Code record, using proper conversions to format a leading zero omitted gerber coordinate (fo...
virtual PLOT_FORMAT GetPlotterType() const override
Returns the effective plot engine in use.
void selectAperture(const wxSize &aSize, int aRadius, double aRotDegree, APERTURE::APERTURE_TYPE aType, int aApertureAttribute)
Pick an existing aperture or create a new one, matching the size, type and attributes.
PLOT_DASH_TYPE
Dashed line types.
Definition: plotter.h:104
EDA_TEXT_VJUSTIFY_T
Definition: eda_text.h:68
void DisableApertMacros(bool aDisable)
Disable Aperture Macro (AM) command, only for broken Gerber Readers Regions will be used instead of A...
Base plotter engine class.
Definition: plotter.h:121
virtual void ThickRect(const wxPoint &p1, const wxPoint &p2, int width, OUTLINE_MODE tracemode, void *aData) override
virtual void StartBlock(void *aData) override
calling this function allows one to define the beginning of a group of drawing items (used in X2 form...
int GetOrCreateAperture(const wxSize &aSize, int aRadius, double aRotDegree, APERTURE::APERTURE_TYPE aType, int aApertureAttribute)
virtual void Rect(const wxPoint &p1, const wxPoint &p2, FILL_TYPE fill, int width=USE_DEFAULT_LINE_WIDTH) override
void PlotGerberRegion(const std::vector< wxPoint > &aCornerList, void *aData=NULL)
Plot a Gerber region: similar to PlotPoly but plot only filled polygon, and add the TA....
virtual bool EndPlot() override
virtual void SetColor(COLOR4D color) override
std::string m_objectAttributesDictionnary
virtual void SetGerberCoordinatesFormat(int aResolution, bool aUseInches=false) override
Function SetGerberCoordinatesFormat selection of Gerber units and resolution (number of digits in man...
wxString m_workFilename
virtual void FlashPadCircle(const wxPoint &pos, int diametre, OUTLINE_MODE trace_mode, void *aData) override
Filled circular flashes are stored as apertures.
virtual void FilledCircle(const wxPoint &pos, int diametre, OUTLINE_MODE tracemode, void *aData) override
bool m_gerberDisableApertMacros
virtual void SetDash(PLOT_DASH_TYPE dashed) override
Plotting engine (Gerber)
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:98