32#include <wx/filename.h>
37 wxFileName fn( aPath );
38 wxString ext = fn.GetExt().Lower();
39 return ext == wxS(
"drl" ) || ext == wxS(
"xln" ) || ext == wxS(
"exc" ) || ext == wxS(
"ncd" );
50 BOX2I itemBox = item->GetBoundingBox();
59 bbox.
Merge( itemBox );
68 wxArrayString* aMessages )
70 std::unique_ptr<GERBER_FILE_IMAGE>
image;
74 auto excellon = std::make_unique<EXCELLON_IMAGE>( 0 );
77 if( !excellon->LoadFile( aPath, &defaults ) )
80 *aErrorMsg = wxString::Format( wxS(
"Failed to load Excellon file: %s" ), aPath );
85 image = std::move( excellon );
89 image = std::make_unique<GERBER_FILE_IMAGE>( 0 );
91 if( !
image->LoadGerberFile( aPath ) )
94 *aErrorMsg = wxString::Format( wxS(
"Failed to load Gerber file: %s" ), aPath );
101 *aMessages =
image->GetMessages();
116 bool needsFlashOffset =
false;
149 needsFlashOffset =
true;
165 std::vector<VECTOR2I> pts;
168 for(
int j = 0; j < outline.
PointCount(); j++ )
171 if( pts.size() >= 3 )
188 double widthInches =
static_cast<double>( aBBox.
GetWidth() ) / iuPerInch;
189 double heightInches =
static_cast<double>( aBBox.
GetHeight() ) / iuPerInch;
191 vp.
width =
static_cast<int>( std::ceil( widthInches * aDpi ) );
192 vp.
height =
static_cast<int>( std::ceil( heightInches * aDpi ) );
200 else if( vp.
width == 0 )
215 vp.
plotScale = std::min( scaleX, scaleY );
223 wxString* aErrorMsg, wxArrayString* aMessages )
230 if(
image->GetItemsCount() == 0 )
233 *aErrorMsg = wxS(
"Gerber file contains no draw items" );
246 int ox =
static_cast<int>( std::round( aOptions.
originXMm * iuPerMm ) );
247 int oy =
static_cast<int>( std::round(
249 int w =
static_cast<int>( std::round( aOptions.
windowWidthMm * iuPerMm ) );
250 int h =
static_cast<int>( std::round( aOptions.
windowHeightMm * iuPerMm ) );
262 *aErrorMsg = wxS(
"Gerber file has zero-size bounding box" );
268 int reqWidth = aOptions.
width;
269 int reqHeight = aOptions.
height;
273 double mmPerInch = 25.4;
274 reqWidth =
static_cast<int>( std::ceil( aOptions.
windowWidthMm / mmPerInch * aOptions.
dpi ) );
275 reqHeight =
static_cast<int>( std::ceil( aOptions.
windowHeightMm / mmPerInch * aOptions.
dpi ) );
288 if( !plotter.
StartPlot( wxEmptyString ) )
291 *aErrorMsg = wxS(
"Failed to start PNG plotter" );
304 if( item->GetLayerPolarity() )
319 if( !plotter.
SaveFile( aOutputPath ) )
322 *aErrorMsg = wxString::Format( wxS(
"Failed to save PNG file: %s" ), aOutputPath );
332 wxString* aErrorMsg, wxArrayString* aMessages )
362 return RenderGerberToPng( aInputPath, aOutputPath, options, aErrorMsg, aMessages );
constexpr EDA_IU_SCALE gerbIUScale
constexpr double ARC_LOW_DEF_MM
constexpr size_type GetWidth() const
constexpr BOX2< Vec > & Merge(const BOX2< Vec > &aRect)
Modify the position and size of the rectangle in order to contain aRect.
constexpr size_type GetHeight() const
constexpr const Vec & GetOrigin() const
A gerber DCODE (also called Aperture) definition.
APERTURE_T m_ApertType
Aperture type ( Line, rectangle, circle, oval poly, macro )
void ConvertShapeToPolygon(const GERBER_DRAW_ITEM *aParent)
Convert a shape to an equivalent polygon.
D_CODE * GetDcodeDescr() const
Return the GetDcodeDescr of this object, or NULL.
VECTOR2I GetABPosition(const VECTOR2I &aXYPosition) const
Return the image position of aPosition for this object.
SHAPE_POLY_SET m_ShapeAsPolygon
void ConvertSegmentToPolygon()
Convert a line to an equivalent polygon.
GBR_BASIC_SHAPE_TYPE m_ShapeType
Hold the image data and parameters for one gerber file and layer parameters.
GERBER_DRAW_ITEMS & GetItems()
Job to convert Gerber/Excellon files to PNG images.
wxString m_backgroundColor
bool m_transparentBackground
wxString m_foregroundColor
A color representation with 4 components: red, green, blue, alpha.
static const COLOR4D WHITE
PNG rasterization plotter using Cairo graphics library.
void SetPixelSize(int aWidth, int aHeight)
Set the output image dimensions in pixels.
bool SaveFile(const wxString &aPath)
Save the rendered image to a PNG file.
void SetBackgroundColor(const COLOR4D &aColor)
Set the background color for the image.
void SetResolution(int aDPI)
Set the output resolution in dots per inch.
virtual bool EndPlot() override
void SetAntialias(bool aEnable)
Enable or disable anti-aliasing.
virtual void SetColor(const COLOR4D &aColor) override
virtual void PlotPoly(const std::vector< VECTOR2I > &aCornerList, FILL_T aFill, int aWidth, void *aData=nullptr) override
Draw a polygon ( filled or not ).
void SetClearCompositing(bool aClear)
Switch the Cairo compositing operator between CLEAR and OVER.
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 StartPlot(const wxString &aPageNumber) override
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
int PointCount() const
Return the number of points (vertices) in this line chain.
const VECTOR2I & CPoint(int aIndex) const
Return a reference to a given point in the line chain.
Represent a set of closed polygons.
int OutlineCount() const
Return the number of outlines in the set.
const SHAPE_LINE_CHAIN & COutline(int aIndex) const
void TransformOvalToPolygon(SHAPE_POLY_SET &aBuffer, const VECTOR2I &aStart, const VECTOR2I &aEnd, int aWidth, int aError, ERROR_LOC aErrorLoc, int aMinSegCount=0)
Convert a oblong shape to a polygon, using multiple segments.
@ FILLED_SHAPE
Fill with object color.
GERBER_PLOTTER_VIEWPORT CalculatePlotterViewport(const BOX2I &aBBox, int aDpi, int aWidth, int aHeight)
Compute pixel dimensions and plotter scale from a bounding box and DPI/size settings.
bool RenderGerberToPng(const wxString &aInputPath, const wxString &aOutputPath, const GERBER_RENDER_OPTIONS &aOptions, wxString *aErrorMsg, wxArrayString *aMessages)
Render a Gerber or Excellon file to PNG.
bool IsExcellonFile(const wxString &aPath)
Determine if a file is an Excellon drill file based on extension.
BOX2I CalculateGerberBoundingBox(GERBER_FILE_IMAGE *aImage)
Calculate bounding box for all draw items in a gerber image.
std::unique_ptr< GERBER_FILE_IMAGE > LoadGerberOrExcellon(const wxString &aPath, wxString *aErrorMsg, wxArrayString *aMessages)
Load a Gerber or Excellon file, auto-detecting by extension.
static constexpr int MIN_PIXEL_SIZE
management of default values used to read a Excellon (.nc) drill file Some important parameters are n...
Computed plotter viewport parameters from a bounding box and render settings.
Render options for Gerber to PNG conversion.
double windowWidthMm
Viewport width in mm (> 0 enables viewport mode)
KIGFX::COLOR4D foregroundColor
KIGFX::COLOR4D backgroundColor
Transparent white.
int height
0 = calculate from DPI
int width
0 = calculate from DPI
double originYMm
Viewport origin Y in mm.
bool HasViewportOverride() const
double windowHeightMm
Viewport height in mm (> 0 enables viewport mode)
double originXMm
Viewport origin X in mm.
VECTOR2< int32_t > VECTOR2I