KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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-2022 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 "plotter.h"
31
32class SHAPE_ARC;
33class GBR_METADATA;
34
35class GERBER_PLOTTER : public PLOTTER
36{
37public:
39
40 virtual PLOT_FORMAT GetPlotterType() const override
41 {
42 return PLOT_FORMAT::GERBER;
43 }
44
45 static wxString GetDefaultFileExtension()
46 {
47 return wxString( wxT( "gbr" ) );
48 }
49
53 virtual bool StartPlot( const wxString& pageNumber ) override;
54 virtual bool EndPlot() override;
55 virtual void SetCurrentLineWidth( int aLineWidth, void* aData = nullptr ) override;
56
57 // RS274X has no dashing, nor colors
58 virtual void SetDash( int aLineWidth, PLOT_DASH_TYPE aLineStyle ) override
59 {
60 }
61
62 virtual void SetColor( const COLOR4D& aColor ) override {}
63
64 // Currently, aScale and aMirror are not used in gerber plotter
65 virtual void SetViewport( const VECTOR2I& aOffset, double aIusPerDecimil,
66 double aScale, bool aMirror ) override;
67
68 // Basic plot primitives
69 virtual void Rect( const VECTOR2I& p1, const VECTOR2I& p2, FILL_T fill,
70 int width = USE_DEFAULT_LINE_WIDTH ) override;
71 virtual void Circle( const VECTOR2I& pos, int diametre, FILL_T fill,
72 int width = USE_DEFAULT_LINE_WIDTH ) override;
73
74 // These functions plot an item and manage X2 gerber attributes
75 virtual void ThickSegment( const VECTOR2I& start, const VECTOR2I& end, int width,
76 OUTLINE_MODE tracemode, void* aData ) override;
77
78 virtual void ThickArc( const VECTOR2I& aCentre, const VECTOR2I& aStart,
79 const VECTOR2I& aEnd, int aWidth,
80 OUTLINE_MODE aTraceMode, void* aData ) override;
81
82 virtual void ThickArc( const EDA_SHAPE& aArcShape,
83 OUTLINE_MODE aTraceMode, void* aData ) override;
84
85 virtual void ThickRect( const VECTOR2I& p1, const VECTOR2I& p2, int width,
86 OUTLINE_MODE tracemode, void* aData ) override;
87
88 virtual void ThickCircle( const VECTOR2I& pos, int diametre, int width,
89 OUTLINE_MODE tracemode, void* aData ) override;
90
91 virtual void FilledCircle( const VECTOR2I& pos, int diametre,
92 OUTLINE_MODE tracemode, void* aData ) override;
93
98 virtual void PlotPoly( const std::vector<VECTOR2I>& aCornerList, FILL_T aFill,
99 int aWidth = USE_DEFAULT_LINE_WIDTH, void* aData = nullptr ) override;
100
101 virtual void PlotPoly( const SHAPE_LINE_CHAIN& aCornerList, FILL_T aFill,
102 int aWidth = USE_DEFAULT_LINE_WIDTH, void* aData = nullptr ) override;
103
108 void PlotPolyAsRegion( const SHAPE_LINE_CHAIN& aPoly, FILL_T aFill,
109 int aWidth, GBR_METADATA* aGbrMetadata );
110
111 virtual void PenTo( const VECTOR2I& pos, char plume ) override;
112
113 virtual void Text( const VECTOR2I& aPos,
114 const COLOR4D& aColor,
115 const wxString& aText,
116 const EDA_ANGLE& aOrient,
117 const VECTOR2I& aSize,
118 enum GR_TEXT_H_ALIGN_T aH_justify,
119 enum GR_TEXT_V_ALIGN_T aV_justify,
120 int aWidth,
121 bool aItalic,
122 bool aBold,
123 bool aMultilineAllowed = false,
124 KIFONT::FONT* aFont = nullptr,
125 void* aData = nullptr ) override;
126
127
128 virtual void PlotText( const VECTOR2I& aPos,
129 const COLOR4D& aColor,
130 const wxString& aText,
131 const TEXT_ATTRIBUTES& aAttributes,
132 KIFONT::FONT* aFont,
133 void* aData = nullptr ) override;
134
138 virtual void FlashPadCircle( const VECTOR2I& pos, int diametre,
139 OUTLINE_MODE trace_mode, void* aData ) override;
140
141 virtual void FlashPadOval( const VECTOR2I& aPadPos, const VECTOR2I& aSize,
142 const EDA_ANGLE& aOrient, OUTLINE_MODE aTraceMode,
143 void* aData ) override;
144
145 virtual void FlashPadRect( const VECTOR2I& aPadPos, const VECTOR2I& aSize,
146 const EDA_ANGLE& aOrient, OUTLINE_MODE aTraceMode,
147 void* aData ) override;
148
149 virtual void FlashPadRoundRect( const VECTOR2I& aPadPos, const VECTOR2I& aSize,
150 int aCornerRadius, const EDA_ANGLE& aOrient,
151 OUTLINE_MODE aTraceMode, void* aData ) override;
152 virtual void FlashPadCustom( const VECTOR2I& aPadPos, const VECTOR2I& aSize,
153 const EDA_ANGLE& aPadOrient, SHAPE_POLY_SET* aPolygons,
154 OUTLINE_MODE aTraceMode, void* aData ) override;
155
156 virtual void FlashPadTrapez( const VECTOR2I& aPadPos, const VECTOR2I* aCorners,
157 const EDA_ANGLE& aPadOrient, OUTLINE_MODE aTraceMode,
158 void* aData ) override;
159
160 virtual void FlashRegularPolygon( const VECTOR2I& aShapePos, int aDiameter, int aCornerCount,
161 const EDA_ANGLE& aOrient, OUTLINE_MODE aTraceMode,
162 void* aData ) override;
163
181 void FlashPadChamferRoundRect( const VECTOR2I& aShapePos, const VECTOR2I& aPadSize,
182 int aCornerRadius, double aChamferRatio,
183 int aChamferPositions, const EDA_ANGLE& aPadOrient,
184 OUTLINE_MODE aPlotMode, void* aData );
185
191 void PlotGerberRegion( const std::vector<VECTOR2I>& aCornerList, GBR_METADATA* aGbrMetadata );
192
193 void PlotGerberRegion( const SHAPE_LINE_CHAIN& aPoly, GBR_METADATA* aGbrMetadata );
194
200 virtual void SetLayerPolarity( bool aPositive ) override;
201
212 virtual void SetGerberCoordinatesFormat( int aResolution, bool aUseInches = false ) override;
213
214 void UseX2format( bool aEnable ) { m_useX2format = aEnable; }
215 void UseX2NetAttributes( bool aEnable ) { m_useNetAttributes = aEnable; }
216
224 void DisableApertMacros( bool aDisable ) { m_gerberDisableApertMacros = aDisable; }
225
232 virtual void StartBlock( void* aData ) override;
233
241 virtual void EndBlock( void* aData ) override;
242
247 void ClearAllAttributes();
248
259 int GetOrCreateAperture( const VECTOR2I& aSize, int aRadius, const EDA_ANGLE& aRotation,
260 APERTURE::APERTURE_TYPE aType, int aApertureAttribute );
261
271 int GetOrCreateAperture( const std::vector<VECTOR2I>& aCorners, const EDA_ANGLE& aRotation,
272 APERTURE::APERTURE_TYPE aType, int aApertureAttribute );
273
274protected:
275 virtual void Arc( const VECTOR2I& aCenter, const EDA_ANGLE& aStartAngle,
276 const EDA_ANGLE& aEndAngle, int aRadius, FILL_T aFill,
277 int aWidth = USE_DEFAULT_LINE_WIDTH ) override;
278
279 virtual void ThickArc( const VECTOR2I& aCentre, const EDA_ANGLE& aStartAngle,
280 const EDA_ANGLE& aEndAngle, int aRadius, int aWidth,
281 OUTLINE_MODE aTraceMode, void* aData ) override;
282
294 void plotRoundRectAsRegion( const VECTOR2I& aRectCenter, const VECTOR2I& aSize,
295 int aCornerRadius, const EDA_ANGLE& aOrient );
306 void plotArc( const VECTOR2I& aCenter, const EDA_ANGLE& aStartAngle, const EDA_ANGLE& aEndAngle,
307 int aRadius, bool aPlotInRegion );
308 void plotArc( const SHAPE_ARC& aArc, bool aPlotInRegion );
309
315 void selectAperture( const VECTOR2I& aSize, int aRadius, const EDA_ANGLE& aRotation,
316 APERTURE::APERTURE_TYPE aType, int aApertureAttribute );
324 void selectAperture( const std::vector<VECTOR2I>& aCorners, const EDA_ANGLE& aPolygonRotation,
325 APERTURE::APERTURE_TYPE aType, int aApertureAttribute );
326
335 void selectAperture( int aDiameter, const EDA_ANGLE& aRotation,
336 APERTURE::APERTURE_TYPE aType, int aApertureAttribute );
337
344 void emitDcode( const VECTOR2D& pt, int dcode );
345
356
362 void clearNetAttribute();
363
364 // the attributes dictionary created/modified by %TO, attached to objects, when they are created
365 // by D01, D03, G36/G37 commands
366 // standard attributes are .P, .C and .N
367 // this is used by gerber readers when creating a new object. Cleared by %TD command
368 // Note: m_objectAttributesDictionary can store more than one attribute
369 // the string stores the line(s) actually written to the gerber file
370 // it can store a .P, .C or .N attribute, or 2 or 3 attributes, separated by a \n char (EOL)
372
373 // The last aperture attribute generated (only one aperture attribute can be set)
375
376 FILE* workFile;
379
383 void writeApertureList();
384
385 std::vector<APERTURE> m_apertures; // The list of available apertures
386 int m_currentApertureIdx; // The index of the current aperture in m_apertures
387 bool m_hasApertureRoundRect; // true is at least one round rect aperture is in use
388 bool m_hasApertureRotOval; // true is at least one oval rotated aperture is in use
389 bool m_hasApertureRotRect; // true is at least one rect. rotated aperture is in use
390 bool m_hasApertureOutline4P; // true is at least one 4 corners outline (free polygon
391 // with 4 corners) aperture is in use
392 bool m_hasApertureChamferedRect; // true is at least one chamfered rect is in use
393 // (with no rounded corner)
394
395 bool m_gerberUnitInch; // true if the gerber units are inches, false for mm
396 int m_gerberUnitFmt; // number of digits in mantissa.
397 // usually 6 in Inches and 5 or 6 in mm
398 bool m_gerberDisableApertMacros; // True to disable Aperture Macro (AM) command,
399 // for broken Gerber Readers
400 // Regions will be used instead of AM shapes
401 bool m_useX2format; // Add X2 file header attributes. If false, attributes
402 // will be added as comments.
403 bool m_useNetAttributes; // In recent gerber files, netlist info can be added.
404 // It will be added if this param is true, using X2 or
405 // X1 format
406
407 // A list of aperture macros defined "on the fly" because the number of parameters is not
408 // defined: this is the case of the macro using the primitive 4 to create a polygon.
409 // The number of vertices is not known for free polygonal shapes, and an aperture macro
410 // must be created for each specific polygon
412};
Metadata which can be added in a gerber file as attribute in X2 format.
Definition: gbr_metadata.h:205
Information which can be added in a gerber file as attribute of an object.
virtual void Circle(const VECTOR2I &pos, int diametre, FILL_T fill, int width=USE_DEFAULT_LINE_WIDTH) override
virtual void SetColor(const COLOR4D &aColor) override
std::string m_objectAttributesDictionary
virtual void Arc(const VECTOR2I &aCenter, const EDA_ANGLE &aStartAngle, const EDA_ANGLE &aEndAngle, int aRadius, FILL_T aFill, int aWidth=USE_DEFAULT_LINE_WIDTH) override
Generic fallback: arc rendered as a polyline.
virtual void SetGerberCoordinatesFormat(int aResolution, bool aUseInches=false) override
Selection of Gerber units and resolution (number of digits in mantissa).
virtual void ThickCircle(const VECTOR2I &pos, int diametre, int width, OUTLINE_MODE tracemode, void *aData) override
void ClearAllAttributes()
Remove (clear) all attributes from object attributes dictionary (TO.
void selectAperture(const VECTOR2I &aSize, int aRadius, const EDA_ANGLE &aRotation, APERTURE::APERTURE_TYPE aType, int aApertureAttribute)
Pick an existing aperture or create a new one, matching the size, type and attributes.
virtual void FlashPadCustom(const VECTOR2I &aPadPos, const VECTOR2I &aSize, const EDA_ANGLE &aPadOrient, SHAPE_POLY_SET *aPolygons, OUTLINE_MODE aTraceMode, void *aData) override
int GetOrCreateAperture(const VECTOR2I &aSize, int aRadius, const EDA_ANGLE &aRotation, APERTURE::APERTURE_TYPE aType, int aApertureAttribute)
void PlotPolyAsRegion(const SHAPE_LINE_CHAIN &aPoly, FILL_T aFill, int aWidth, GBR_METADATA *aGbrMetadata)
Similar to PlotPoly(), plot a filled polygon using Gerber region, therefore adding X2 attributes to t...
virtual void PenTo(const VECTOR2I &pos, char plume) override
Moveto/lineto primitive, moves the 'pen' to the specified direction.
void emitDcode(const VECTOR2D &pt, int dcode)
Emit a D-Code record, using proper conversions to format a leading zero omitted gerber coordinate.
APER_MACRO_FREEPOLY_LIST m_am_freepoly_list
void plotArc(const VECTOR2I &aCenter, const EDA_ANGLE &aStartAngle, const EDA_ANGLE &aEndAngle, int aRadius, bool aPlotInRegion)
Plot a Gerber arc.
virtual void Rect(const VECTOR2I &p1, const VECTOR2I &p2, FILL_T fill, int width=USE_DEFAULT_LINE_WIDTH) override
virtual void FlashPadCircle(const VECTOR2I &pos, int diametre, OUTLINE_MODE trace_mode, void *aData) override
Filled circular flashes are stored as apertures.
std::vector< APERTURE > m_apertures
bool m_hasApertureChamferedRect
virtual void FlashPadTrapez(const VECTOR2I &aPadPos, const VECTOR2I *aCorners, const EDA_ANGLE &aPadOrient, OUTLINE_MODE aTraceMode, void *aData) override
Flash a trapezoidal pad.
bool m_hasApertureOutline4P
virtual void SetCurrentLineWidth(int aLineWidth, void *aData=nullptr) override
Set the line width for the next drawing.
void writeApertureList()
Generate the table of D codes.
void plotRoundRectAsRegion(const VECTOR2I &aRectCenter, const VECTOR2I &aSize, int aCornerRadius, const EDA_ANGLE &aOrient)
Plot a round rect (a round rect shape in fact) as a Gerber region using lines and arcs for corners.
virtual void FlashPadRect(const VECTOR2I &aPadPos, const VECTOR2I &aSize, const EDA_ANGLE &aOrient, OUTLINE_MODE aTraceMode, void *aData) override
void FlashPadChamferRoundRect(const VECTOR2I &aShapePos, const VECTOR2I &aPadSize, int aCornerRadius, double aChamferRatio, int aChamferPositions, const EDA_ANGLE &aPadOrient, OUTLINE_MODE aPlotMode, void *aData)
Flash a chamfered round rect pad.
wxString m_workFilename
bool m_hasApertureRoundRect
static wxString GetDefaultFileExtension()
bool m_gerberDisableApertMacros
virtual void Text(const VECTOR2I &aPos, const COLOR4D &aColor, const wxString &aText, const EDA_ANGLE &aOrient, const VECTOR2I &aSize, enum GR_TEXT_H_ALIGN_T aH_justify, enum GR_TEXT_V_ALIGN_T aV_justify, int aWidth, bool aItalic, bool aBold, bool aMultilineAllowed=false, KIFONT::FONT *aFont=nullptr, void *aData=nullptr) override
Draw text with the plotter.
virtual void SetViewport(const VECTOR2I &aOffset, double aIusPerDecimil, double aScale, bool aMirror) override
Set the plot offset and scaling for the current plot.
virtual bool EndPlot() override
virtual void PlotText(const VECTOR2I &aPos, const COLOR4D &aColor, const wxString &aText, const TEXT_ATTRIBUTES &aAttributes, KIFONT::FONT *aFont, void *aData=nullptr) override
void formatNetAttribute(GBR_NETLIST_METADATA *aData)
Print a Gerber net attribute object record.
virtual PLOT_FORMAT GetPlotterType() const override
Returns the effective plot engine in use.
void UseX2format(bool aEnable)
virtual void SetLayerPolarity(bool aPositive) override
Change the plot polarity and begin a new layer.
virtual void FlashPadOval(const VECTOR2I &aPadPos, const VECTOR2I &aSize, const EDA_ANGLE &aOrient, OUTLINE_MODE aTraceMode, void *aData) override
void UseX2NetAttributes(bool aEnable)
virtual void PlotPoly(const std::vector< VECTOR2I > &aCornerList, FILL_T 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 FlashPadRoundRect(const VECTOR2I &aPadPos, const VECTOR2I &aSize, int aCornerRadius, const EDA_ANGLE &aOrient, OUTLINE_MODE aTraceMode, void *aData) override
void PlotGerberRegion(const std::vector< VECTOR2I > &aCornerList, GBR_METADATA *aGbrMetadata)
Plot a Gerber region: similar to PlotPoly but plot only filled polygon, and add the TA....
virtual void EndBlock(void *aData) override
Define the end of a group of drawing items the group is started by StartBlock().
virtual void ThickRect(const VECTOR2I &p1, const VECTOR2I &p2, int width, OUTLINE_MODE tracemode, void *aData) override
virtual void ThickSegment(const VECTOR2I &start, const VECTOR2I &end, int width, OUTLINE_MODE tracemode, void *aData) override
void clearNetAttribute()
Clear a Gerber net attribute record (clear object attribute dictionary) and output the clear object a...
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...
virtual void FlashRegularPolygon(const VECTOR2I &aShapePos, int aDiameter, int aCornerCount, const EDA_ANGLE &aOrient, OUTLINE_MODE aTraceMode, void *aData) override
Flash a regular polygon.
virtual void SetDash(int aLineWidth, PLOT_DASH_TYPE aLineStyle) override
virtual bool StartPlot(const wxString &pageNumber) override
Write GERBER header to file initialize global variable g_Plot_PlotOutputFile.
virtual void ThickArc(const VECTOR2I &aCentre, const VECTOR2I &aStart, const VECTOR2I &aEnd, int aWidth, OUTLINE_MODE aTraceMode, void *aData) override
virtual void FilledCircle(const VECTOR2I &pos, int diametre, OUTLINE_MODE tracemode, void *aData) override
void DisableApertMacros(bool aDisable)
Disable Aperture Macro (AM) command, only for broken Gerber Readers.
FONT is an abstract base class for both outline and stroke fonts.
Definition: font.h:105
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:103
Base plotter engine class.
Definition: plotter.h:110
static const int USE_DEFAULT_LINE_WIDTH
Definition: plotter.h:114
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
Represent a set of closed polygons.
FILL_T
Definition: eda_shape.h:54
Plotting engine (Gerber)
OUTLINE_MODE
Definition: outline_mode.h:25
Plot settings, and plotting engines (PostScript, Gerber, HPGL and DXF)
PLOT_FORMAT
The set of supported output plot formats.
Definition: plotter.h:70
PLOT_DASH_TYPE
Dashed line types.
Definition: stroke_params.h:48
GR_TEXT_H_ALIGN_T
GR_TEXT_V_ALIGN_T