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-2021 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 
32 #include <plotter.h>
33 #include "gbr_plotter_apertures.h"
34 
35 class SHAPE_ARC;
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 
55  virtual bool StartPlot() override;
56  virtual bool EndPlot() override;
57  virtual void SetCurrentLineWidth( int width, void* aData = nullptr ) override;
58 
59  // RS274X has no dashing, nor colors
60  virtual void SetDash( PLOT_DASH_TYPE dashed ) override
61  {
62  }
63 
64  virtual void SetColor( const COLOR4D& color ) override {}
65 
66  // Currently, aScale and aMirror are not used in gerber plotter
67  virtual void SetViewport( const wxPoint& aOffset, double aIusPerDecimil,
68  double aScale, bool aMirror ) override;
69 
70  // Basic plot primitives
71  virtual void Rect( const wxPoint& p1, const wxPoint& p2, FILL_TYPE fill,
72  int width = USE_DEFAULT_LINE_WIDTH ) override;
73  virtual void Circle( const wxPoint& pos, int diametre, FILL_TYPE fill,
74  int width = USE_DEFAULT_LINE_WIDTH ) override;
75  virtual void Arc( const wxPoint& aCenter, double aStAngle, double aEndAngle,
76  int aRadius, FILL_TYPE aFill, int aWidth = USE_DEFAULT_LINE_WIDTH ) override;
77 
78  virtual void Arc( const SHAPE_ARC& aArc ) override;
79 
80  // These functions plot an item and manage X2 gerber attributes
81  virtual void ThickSegment( const wxPoint& start, const wxPoint& end, int width,
82  OUTLINE_MODE tracemode, void* aData ) override;
83 
84  virtual void ThickArc( const wxPoint& centre, double StAngle, double EndAngle,
85  int rayon, int width, OUTLINE_MODE tracemode, void* aData ) override;
86  virtual void ThickRect( const wxPoint& p1, const wxPoint& p2, int width,
87  OUTLINE_MODE tracemode, void* aData ) override;
88  virtual void ThickCircle( const wxPoint& pos, int diametre, int width,
89  OUTLINE_MODE tracemode, void* aData ) override;
90  virtual void FilledCircle( const wxPoint& pos, int diametre,
91  OUTLINE_MODE tracemode, void* aData ) override;
92 
97  virtual void PlotPoly( const std::vector< wxPoint >& aCornerList, FILL_TYPE aFill,
98  int aWidth = USE_DEFAULT_LINE_WIDTH, void* aData = nullptr ) override;
99 
100  virtual void PlotPoly( const SHAPE_LINE_CHAIN& aCornerList, FILL_TYPE aFill,
101  int aWidth = USE_DEFAULT_LINE_WIDTH, void* aData = nullptr ) override;
102 
103  virtual void PenTo( const wxPoint& pos, char plume ) override;
104 
105  virtual void Text( const wxPoint& aPos,
106  const COLOR4D& aColor,
107  const wxString& aText,
108  double aOrient,
109  const wxSize& aSize,
110  enum EDA_TEXT_HJUSTIFY_T aH_justify,
111  enum EDA_TEXT_VJUSTIFY_T aV_justify,
112  int aWidth,
113  bool aItalic,
114  bool aBold,
115  bool aMultilineAllowed = false,
116  void* aData = nullptr ) override;
117 
121  virtual void FlashPadCircle( const wxPoint& pos, int diametre,
122  OUTLINE_MODE trace_mode, void* aData ) override;
123 
124  virtual void FlashPadOval( const wxPoint& aPadPos, const wxSize& size, double orient,
125  OUTLINE_MODE trace_mode, void* aData ) override;
126 
127  virtual void FlashPadRect( const wxPoint& aPadPos, const wxSize& size,
128  double orient, OUTLINE_MODE trace_mode, void* aData ) override;
129 
130  virtual void FlashPadRoundRect( const wxPoint& aPadPos, const wxSize& aSize,
131  int aCornerRadius, double aOrient,
132  OUTLINE_MODE aTraceMode, void* aData ) override;
133  virtual void FlashPadCustom( const wxPoint& aPadPos, const wxSize& aSize,
134  double aPadOrient, SHAPE_POLY_SET* aPolygons,
135  OUTLINE_MODE aTraceMode, void* aData ) override;
136 
137  virtual void FlashPadTrapez( const wxPoint& aPadPos, const wxPoint *aCorners,
138  double aPadOrient, OUTLINE_MODE aTraceMode, void* aData ) override;
139 
140  virtual void FlashRegularPolygon( const wxPoint& aShapePos, int aDiameter, int aCornerCount,
141  double aOrient, OUTLINE_MODE aTraceMode, void* aData ) override;
142 
160  void FlashPadChamferRoundRect( const wxPoint& aShapePos, const wxSize& aPadSize,
161  int aCornerRadius, double aChamferRatio,
162  int aChamferPositions, double aPadOrient,
163  OUTLINE_MODE aPlotMode, void* aData );
164 
170  void PlotGerberRegion( const std::vector< wxPoint >& aCornerList, void* aData = nullptr );
171 
172  void PlotGerberRegion( const SHAPE_LINE_CHAIN& aPoly, void* aData = nullptr );
173 
179  virtual void SetLayerPolarity( bool aPositive ) override;
180 
191  virtual void SetGerberCoordinatesFormat( int aResolution, bool aUseInches = false ) override;
192 
193  void UseX2format( bool aEnable ) { m_useX2format = aEnable; }
194  void UseX2NetAttributes( bool aEnable ) { m_useNetAttributes = aEnable; }
195 
203  void DisableApertMacros( bool aDisable ) { m_gerberDisableApertMacros = aDisable; }
204 
211  virtual void StartBlock( void* aData ) override;
212 
220  virtual void EndBlock( void* aData ) override;
221 
226  void ClearAllAttributes();
227 
238  int GetOrCreateAperture( const wxSize& aSize, int aRadius, double aRotDegree,
239  APERTURE::APERTURE_TYPE aType, int aApertureAttribute );
240 
250  int GetOrCreateAperture( const std::vector<wxPoint>& aCorners, double aRotDegree,
251  APERTURE::APERTURE_TYPE aType, int aApertureAttribute );
252 
253 protected:
265  void plotRoundRectAsRegion( const wxPoint& aRectCenter, const wxSize& aSize,
266  int aCornerRadius, double aOrient );
277  void plotArc( const wxPoint& aCenter, double aStAngle, double aEndAngle,
278  int aRadius, bool aPlotInRegion );
279  void plotArc( const SHAPE_ARC& aArc, bool aPlotInRegion );
280 
286  void selectAperture( const wxSize& aSize, int aRadius, double aRotDegree,
287  APERTURE::APERTURE_TYPE aType, int aApertureAttribute );
295  void selectAperture( const std::vector<wxPoint>& aCorners, double aPolygonRotation,
296  APERTURE::APERTURE_TYPE aType, int aApertureAttribute );
297 
306  void selectAperture( int aDiameter, double aRotDegree,
307  APERTURE::APERTURE_TYPE aType, int aApertureAttribute );
308 
315  void emitDcode( const DPOINT& pt, int dcode );
316 
327 
333  void clearNetAttribute();
334 
335  // the attributes dictionary created/modified by %TO, attached to objects, when they are created
336  // by D01, D03, G36/G37 commands
337  // standard attributes are .P, .C and .N
338  // this is used by gerber readers when creating a new object. Cleared by %TD command
339  // Note: m_objectAttributesDictionary can store more than one attribute
340  // the string stores the line(s) actually written to the gerber file
341  // it can store a .P, .C or .N attribute, or 2 or 3 attributes, separated by a \n char (EOL)
343 
344  // The last aperture attribute generated (only one aperture attribute can be set)
346 
347  FILE* workFile;
348  FILE* finalFile;
349  wxString m_workFilename;
350 
354  void writeApertureList();
355 
356  std::vector<APERTURE> m_apertures; // The list of available apertures
357  int m_currentApertureIdx; // The index of the current aperture in m_apertures
358  bool m_hasApertureRoundRect; // true is at least one round rect aperture is in use
359  bool m_hasApertureRotOval; // true is at least one oval rotated aperture is in use
360  bool m_hasApertureRotRect; // true is at least one rect. rotated aperture is in use
361  bool m_hasApertureOutline4P; // true is at least one 4 corners outline (free polygon
362  // with 4 corners) aperture is in use
363  bool m_hasApertureChamferedRect; // true is at least one chamfered rect is in use
364  // (with no rounded corner)
365 
366  bool m_gerberUnitInch; // true if the gerber units are inches, false for mm
367  int m_gerberUnitFmt; // number of digits in mantissa.
368  // usually 6 in Inches and 5 or 6 in mm
369  bool m_gerberDisableApertMacros; // True to disable Aperture Macro (AM) command,
370  // for broken Gerber Readers
371  // Regions will be used instead of AM shapes
372  bool m_useX2format; // Add X2 file header attributes. If false, attributes
373  // will be added as comments.
374  bool m_useNetAttributes; // In recent gerber files, netlist info can be added.
375  // It will be added if this param is true, using X2 or
376  // X1 format
377 
378  // A list of aperture macros defined "on the fly" because the number of parameters is not
379  // defined: this is the case of the macro using the primitive 4 to create a polygon.
380  // The number of vertices is not known for free polygonal shapes, and an aperture macro
381  // must be created for each specific polygon
383 };
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.
virtual void EndBlock(void *aData) override
Define the end of a group of drawing items the group is started by StartBlock().
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.
std::string m_objectAttributesDictionary
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
Write GERBER header to file initialize global variable g_Plot_PlotOutputFile.
bool m_hasApertureRoundRect
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.
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 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=nullptr) override
Draw text with the plotter.
virtual void SetCurrentLineWidth(int width, void *aData=nullptr) override
Set the line width for the next drawing.
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.
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.
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
virtual bool EndPlot() override
SHAPE_LINE_CHAIN.
virtual void SetGerberCoordinatesFormat(int aResolution, bool aUseInches=false) override
Selection of Gerber units and resolution (number of digits in mantissa).
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.
void PlotGerberRegion(const std::vector< wxPoint > &aCornerList, void *aData=nullptr)
Plot a Gerber region: similar to PlotPoly but plot only filled polygon, and add the TA....
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
virtual void SetColor(const COLOR4D &color) override
Plotting engine (Gerber)
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:103