2017-04-28 10:53:30 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2017 Jean_Pierre Charras <jp.charras at wanadoo.fr>
|
2021-03-07 16:44:59 +00:00
|
|
|
* Copyright (C) 1992-2021 KiCad Developers, see AUTHORS.txt for contributors.
|
2017-04-28 10:53:30 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file gendrill_gerber_writer.cpp
|
|
|
|
* @brief Functions to create drill files in gerber X2 format.
|
|
|
|
*/
|
|
|
|
|
2021-08-18 20:38:14 +00:00
|
|
|
#include <plotters/plotter_gerber.h>
|
2021-07-29 09:56:22 +00:00
|
|
|
#include <string_utils.h>
|
2020-10-24 01:38:50 +00:00
|
|
|
#include <locale_io.h>
|
2020-11-12 20:19:22 +00:00
|
|
|
#include <board.h>
|
2021-06-03 18:05:43 +00:00
|
|
|
#include <footprint.h>
|
2021-06-11 21:07:02 +00:00
|
|
|
#include <pcb_track.h>
|
2021-06-06 19:03:10 +00:00
|
|
|
#include <pad.h>
|
2017-04-28 10:53:30 +00:00
|
|
|
#include <pcbplot.h>
|
|
|
|
#include <gendrill_gerber_writer.h>
|
|
|
|
#include <reporter.h>
|
2018-05-17 05:49:38 +00:00
|
|
|
#include <gbr_metadata.h>
|
2017-04-28 10:53:30 +00:00
|
|
|
|
2021-03-07 16:44:59 +00:00
|
|
|
// set to 1 to use flashed oblong holes, 0 to draw them by a line.
|
|
|
|
#define FLASH_OVAL_HOLE 1
|
|
|
|
|
2017-04-28 10:53:30 +00:00
|
|
|
|
|
|
|
GERBER_WRITER::GERBER_WRITER( BOARD* aPcb )
|
|
|
|
: GENDRILL_WRITER_BASE( aPcb )
|
|
|
|
{
|
|
|
|
m_zeroFormat = SUPPRESS_LEADING;
|
|
|
|
m_conversionUnits = 1.0;
|
2017-12-20 16:48:53 +00:00
|
|
|
m_unitsMetric = true;
|
2017-04-28 10:53:30 +00:00
|
|
|
m_drillFileExtension = "gbr";
|
|
|
|
m_merge_PTH_NPTH = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-11-12 20:19:22 +00:00
|
|
|
void GERBER_WRITER::CreateDrillandMapFilesSet( const wxString& aPlotDirectory, bool aGenDrill,
|
|
|
|
bool aGenMap, REPORTER* aReporter )
|
2017-04-28 10:53:30 +00:00
|
|
|
{
|
|
|
|
// Note: In Gerber drill files, NPTH and PTH are always separate files
|
|
|
|
m_merge_PTH_NPTH = false;
|
|
|
|
|
|
|
|
wxFileName fn;
|
|
|
|
wxString msg;
|
|
|
|
|
|
|
|
std::vector<DRILL_LAYER_PAIR> hole_sets = getUniqueLayerPairs();
|
|
|
|
|
|
|
|
// append a pair representing the NPTH set of holes, for separate drill files.
|
|
|
|
// (Gerber drill files are separate files for PTH and NPTH)
|
2019-12-05 15:41:21 +00:00
|
|
|
hole_sets.emplace_back( F_Cu, B_Cu );
|
2017-04-28 10:53:30 +00:00
|
|
|
|
|
|
|
for( std::vector<DRILL_LAYER_PAIR>::const_iterator it = hole_sets.begin();
|
|
|
|
it != hole_sets.end(); ++it )
|
|
|
|
{
|
|
|
|
DRILL_LAYER_PAIR pair = *it;
|
|
|
|
// For separate drill files, the last layer pair is the NPTH drill file.
|
|
|
|
bool doing_npth = ( it == hole_sets.end() - 1 );
|
|
|
|
|
|
|
|
buildHolesList( pair, doing_npth );
|
|
|
|
|
|
|
|
// The file is created if it has holes, or if it is the non plated drill file
|
|
|
|
// to be sure the NPTH file is up to date in separate files mode.
|
2021-07-19 23:56:05 +00:00
|
|
|
// Also a PTH drill/map file is always created, to be sure at least one plated hole drill
|
|
|
|
// file is created (do not create any PTH drill file can be seen as not working drill
|
|
|
|
// generator).
|
2020-09-10 14:42:04 +00:00
|
|
|
if( getHolesCount() > 0 || doing_npth || pair == DRILL_LAYER_PAIR( F_Cu, B_Cu ) )
|
2017-04-28 10:53:30 +00:00
|
|
|
{
|
|
|
|
fn = getDrillFileName( pair, doing_npth, false );
|
|
|
|
fn.SetPath( aPlotDirectory );
|
|
|
|
|
|
|
|
if( aGenDrill )
|
|
|
|
{
|
|
|
|
wxString fullFilename = fn.GetFullPath();
|
|
|
|
|
2018-11-21 14:33:17 +00:00
|
|
|
int result = createDrillFile( fullFilename, doing_npth, pair );
|
2017-04-28 10:53:30 +00:00
|
|
|
|
|
|
|
if( result < 0 )
|
|
|
|
{
|
|
|
|
if( aReporter )
|
|
|
|
{
|
2021-07-05 12:40:38 +00:00
|
|
|
msg.Printf( _( "Failed to create file '%s'." ), fullFilename );
|
|
|
|
aReporter->Report( msg, RPT_SEVERITY_ERROR );
|
2017-04-28 10:53:30 +00:00
|
|
|
}
|
2021-07-19 23:56:05 +00:00
|
|
|
|
2017-04-28 10:53:30 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( aReporter )
|
|
|
|
{
|
2021-06-28 23:44:07 +00:00
|
|
|
msg.Printf( _( "Created file '%s'." ), fullFilename );
|
2021-09-19 18:25:41 +00:00
|
|
|
aReporter->Report( msg, RPT_SEVERITY_ACTION );
|
2017-04-28 10:53:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( aGenMap )
|
|
|
|
CreateMapFilesSet( aPlotDirectory, aReporter );
|
2021-09-19 18:25:41 +00:00
|
|
|
|
2021-09-20 07:13:51 +00:00
|
|
|
if( aReporter )
|
|
|
|
aReporter->ReportTail( _( "Done." ), RPT_SEVERITY_INFO );
|
2017-04-28 10:53:30 +00:00
|
|
|
}
|
|
|
|
|
2021-07-19 23:56:05 +00:00
|
|
|
|
2021-03-07 16:44:59 +00:00
|
|
|
#if !FLASH_OVAL_HOLE
|
2017-04-28 10:53:30 +00:00
|
|
|
// A helper class to transform an oblong hole to a segment
|
2022-01-11 00:49:49 +00:00
|
|
|
static void convertOblong2Segment( wxSize aSize, double aOrient, VECTOR2I& aStart, VECTOR2I& aEnd );
|
2021-03-07 16:44:59 +00:00
|
|
|
#endif
|
2017-04-28 10:53:30 +00:00
|
|
|
|
2021-07-19 23:56:05 +00:00
|
|
|
|
2017-04-28 10:53:30 +00:00
|
|
|
int GERBER_WRITER::createDrillFile( wxString& aFullFilename, bool aIsNpth,
|
2018-11-21 14:33:17 +00:00
|
|
|
DRILL_LAYER_PAIR aLayerPair )
|
2017-04-28 10:53:30 +00:00
|
|
|
{
|
|
|
|
int holes_count;
|
|
|
|
|
|
|
|
LOCALE_IO dummy; // Use the standard notation for double numbers
|
|
|
|
|
|
|
|
GERBER_PLOTTER plotter;
|
|
|
|
|
|
|
|
// Gerber drill file imply X2 format:
|
2018-12-09 09:23:07 +00:00
|
|
|
plotter.UseX2format( true );
|
2017-04-28 10:53:30 +00:00
|
|
|
plotter.UseX2NetAttributes( true );
|
2021-03-07 16:44:59 +00:00
|
|
|
plotter.DisableApertMacros( false );
|
2017-04-28 10:53:30 +00:00
|
|
|
|
|
|
|
// Add the standard X2 header, without FileFunction
|
|
|
|
AddGerberX2Header( &plotter, m_pcb );
|
|
|
|
plotter.SetViewport( m_offset, IU_PER_MILS/10, /* scale */ 1.0, /* mirror */false );
|
2021-07-19 23:56:05 +00:00
|
|
|
|
2017-04-28 10:53:30 +00:00
|
|
|
// has meaning only for gerber plotter. Must be called only after SetViewport
|
|
|
|
plotter.SetGerberCoordinatesFormat( 6 );
|
|
|
|
plotter.SetCreator( wxT( "PCBNEW" ) );
|
|
|
|
|
|
|
|
// Add the standard X2 FileFunction for drill files
|
|
|
|
// %TF.FileFunction,Plated[NonPlated],layer1num,layer2num,PTH[NPTH][Blind][Buried],Drill[Route][Mixed]*%
|
2021-03-14 11:38:03 +00:00
|
|
|
wxString text = BuildFileFunctionAttributeString( aLayerPair,
|
|
|
|
aIsNpth
|
|
|
|
? TYPE_FILE::NPTH_FILE
|
|
|
|
: TYPE_FILE::PTH_FILE );
|
2017-04-28 10:53:30 +00:00
|
|
|
plotter.AddLineToHeader( text );
|
|
|
|
|
2017-06-30 16:36:26 +00:00
|
|
|
// Add file polarity (positive)
|
|
|
|
text = "%TF.FilePolarity,Positive*%";
|
|
|
|
plotter.AddLineToHeader( text );
|
|
|
|
|
2017-04-28 10:53:30 +00:00
|
|
|
if( !plotter.OpenFile( aFullFilename ) )
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
plotter.StartPlot();
|
|
|
|
|
|
|
|
holes_count = 0;
|
|
|
|
|
2021-12-29 21:30:11 +00:00
|
|
|
VECTOR2I hole_pos;
|
2017-05-16 10:59:22 +00:00
|
|
|
bool last_item_is_via = true; // a flag to clear object attributes when a via hole is created.
|
2017-04-28 10:53:30 +00:00
|
|
|
|
|
|
|
for( unsigned ii = 0; ii < m_holeListBuffer.size(); ii++ )
|
|
|
|
{
|
|
|
|
HOLE_INFO& hole_descr = m_holeListBuffer[ii];
|
|
|
|
hole_pos = hole_descr.m_Hole_Pos;
|
|
|
|
|
|
|
|
// Manage the aperture attributes: in drill files 3 attributes can be used:
|
|
|
|
// "ViaDrill", only for vias, not pads
|
|
|
|
// "ComponentDrill", only for Through Holes pads
|
|
|
|
// "Slot" for oblong holes;
|
|
|
|
GBR_METADATA gbr_metadata;
|
|
|
|
|
2021-06-11 21:07:02 +00:00
|
|
|
if( dyn_cast<const PCB_VIA*>( hole_descr.m_ItemParent ) )
|
2017-05-16 10:59:22 +00:00
|
|
|
{
|
2017-04-28 10:53:30 +00:00
|
|
|
gbr_metadata.SetApertureAttrib( GBR_APERTURE_METADATA::GBR_APERTURE_ATTRIB_VIADRILL );
|
2017-05-16 10:59:22 +00:00
|
|
|
|
|
|
|
if( !last_item_is_via )
|
|
|
|
{
|
|
|
|
// be sure the current object attribute is cleared for vias
|
2021-07-19 23:56:05 +00:00
|
|
|
plotter.EndBlock( nullptr );
|
2017-05-16 10:59:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
last_item_is_via = true;
|
|
|
|
}
|
2020-11-12 22:30:02 +00:00
|
|
|
else if( dyn_cast<const PAD*>( hole_descr.m_ItemParent ) )
|
2017-04-28 10:53:30 +00:00
|
|
|
{
|
2017-05-16 10:59:22 +00:00
|
|
|
last_item_is_via = false;
|
2020-11-12 22:30:02 +00:00
|
|
|
const PAD* pad = dyn_cast<const PAD*>( hole_descr.m_ItemParent );
|
2017-05-16 10:59:22 +00:00
|
|
|
|
2021-05-01 14:58:30 +00:00
|
|
|
if( pad->GetProperty() == PAD_PROP::CASTELLATED )
|
2021-07-19 23:56:05 +00:00
|
|
|
{
|
2021-08-17 19:41:08 +00:00
|
|
|
gbr_metadata.SetApertureAttrib(
|
|
|
|
GBR_APERTURE_METADATA::GBR_APERTURE_ATTRIB_CASTELLATEDDRILL );
|
2021-07-19 23:56:05 +00:00
|
|
|
}
|
2017-04-28 10:53:30 +00:00
|
|
|
else
|
2019-12-11 10:36:45 +00:00
|
|
|
{
|
2021-07-19 23:56:05 +00:00
|
|
|
// Good practice of oblong pad holes (slots) is to use a specific aperture for
|
|
|
|
// routing, not used in drill commands.
|
2019-12-11 10:36:45 +00:00
|
|
|
if( hole_descr.m_Hole_Shape )
|
2021-08-17 19:41:08 +00:00
|
|
|
gbr_metadata.SetApertureAttrib(
|
|
|
|
GBR_APERTURE_METADATA::GBR_APERTURE_ATTRIB_CMP_OBLONG_DRILL );
|
2019-12-11 10:36:45 +00:00
|
|
|
else
|
2021-08-17 19:41:08 +00:00
|
|
|
gbr_metadata.SetApertureAttrib(
|
|
|
|
GBR_APERTURE_METADATA::GBR_APERTURE_ATTRIB_CMP_DRILL );
|
2019-12-11 10:36:45 +00:00
|
|
|
}
|
2017-04-28 10:53:30 +00:00
|
|
|
|
2021-06-09 19:32:58 +00:00
|
|
|
// Add object attribute: component reference to pads (mainly useful for users)
|
2017-05-16 10:59:22 +00:00
|
|
|
wxString ref = pad->GetParent()->GetReference();
|
|
|
|
|
|
|
|
gbr_metadata.SetCmpReference( ref );
|
2017-04-28 10:53:30 +00:00
|
|
|
gbr_metadata.SetNetAttribType( GBR_NETLIST_METADATA::GBR_NETINFO_CMP );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( hole_descr.m_Hole_Shape )
|
|
|
|
{
|
2021-07-19 23:56:05 +00:00
|
|
|
#if FLASH_OVAL_HOLE // set to 1 to use flashed oblong holes,
|
2021-03-07 16:44:59 +00:00
|
|
|
// 0 to draw them as a line.
|
2022-01-16 01:06:25 +00:00
|
|
|
plotter.FlashPadOval( hole_pos, hole_descr.m_Hole_Size, hole_descr.m_Hole_Orient,
|
|
|
|
FILLED, &gbr_metadata );
|
2021-07-19 23:56:05 +00:00
|
|
|
#else
|
2017-04-28 10:53:30 +00:00
|
|
|
// Use routing for oblong hole (Slots)
|
2022-01-11 00:49:49 +00:00
|
|
|
VECTOR2I start, end;
|
2021-07-19 23:56:05 +00:00
|
|
|
convertOblong2Segment( hole_descr.m_Hole_Size, hole_descr.m_Hole_Orient, start, end );
|
2017-04-28 10:53:30 +00:00
|
|
|
int width = std::min( hole_descr.m_Hole_Size.x, hole_descr.m_Hole_Size.y );
|
2018-06-17 12:37:49 +00:00
|
|
|
|
|
|
|
if ( width == 0 )
|
|
|
|
continue;
|
|
|
|
|
2021-07-19 23:56:05 +00:00
|
|
|
plotter.ThickSegment( start+hole_pos, end+hole_pos, width, FILLED, &gbr_metadata );
|
|
|
|
#endif
|
2017-04-28 10:53:30 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int diam = std::min( hole_descr.m_Hole_Size.x, hole_descr.m_Hole_Size.y );
|
|
|
|
plotter.FlashPadCircle( hole_pos, diam, FILLED, &gbr_metadata );
|
|
|
|
}
|
|
|
|
|
|
|
|
holes_count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
plotter.EndPlot();
|
|
|
|
|
|
|
|
return holes_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-03-07 16:44:59 +00:00
|
|
|
#if !FLASH_OVAL_HOLE
|
2022-01-16 01:06:25 +00:00
|
|
|
void convertOblong2Segment( wxSize aSize, const EDA_ANGLE& aOrient, VECTOR2I& aStart,
|
|
|
|
VECTOR2I& aEnd )
|
2017-04-28 10:53:30 +00:00
|
|
|
{
|
2022-01-16 01:06:25 +00:00
|
|
|
wxSize size( aSize );
|
|
|
|
EDA_ANGLE orient( aOrient );
|
2017-04-28 10:53:30 +00:00
|
|
|
|
|
|
|
/* The pad will be drawn as an oblong shape with size.y > size.x
|
|
|
|
* (Oval vertical orientation 0)
|
|
|
|
*/
|
|
|
|
if( size.x > size.y )
|
|
|
|
{
|
|
|
|
std::swap( size.x, size.y );
|
2022-01-16 01:06:25 +00:00
|
|
|
orient += ANGLE_90;
|
2017-04-28 10:53:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int deltaxy = size.y - size.x; // distance between centers of the oval
|
2022-01-16 01:06:25 +00:00
|
|
|
aStart = VECTOR2I( 0, deltaxy / 2 );
|
|
|
|
RotatePoint( aStart, orient );
|
2017-04-28 10:53:30 +00:00
|
|
|
|
2022-01-16 01:06:25 +00:00
|
|
|
aEnd = VECTOR2I( 0, -deltaxy / 2 );
|
|
|
|
RotatePoint( aEnd, orient );
|
2017-04-28 10:53:30 +00:00
|
|
|
}
|
2021-03-07 16:44:59 +00:00
|
|
|
#endif
|
2017-04-28 10:53:30 +00:00
|
|
|
|
2021-07-19 23:56:05 +00:00
|
|
|
|
2017-04-28 10:53:30 +00:00
|
|
|
void GERBER_WRITER::SetFormat( int aRightDigits )
|
|
|
|
{
|
|
|
|
/* Set conversion scale depending on drill file units */
|
|
|
|
m_conversionUnits = 1.0 / IU_PER_MM; // Gerber units = mm
|
|
|
|
|
2021-06-09 19:32:58 +00:00
|
|
|
// Set precision (unit is mm).
|
2020-11-16 00:04:55 +00:00
|
|
|
m_precision.m_Lhs = 4;
|
|
|
|
m_precision.m_Rhs = aRightDigits == 6 ? 6 : 5;
|
2017-04-28 10:53:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const wxString GERBER_WRITER::getDrillFileName( DRILL_LAYER_PAIR aPair, bool aNPTH,
|
2021-04-12 09:08:50 +00:00
|
|
|
bool aMerge_PTH_NPTH ) const
|
2017-04-28 10:53:30 +00:00
|
|
|
{
|
|
|
|
// Gerber files extension is always .gbr.
|
|
|
|
// Therefore, to mark drill files, add "-drl" to the filename.
|
|
|
|
wxFileName fname( GENDRILL_WRITER_BASE::getDrillFileName( aPair, aNPTH, aMerge_PTH_NPTH ) );
|
|
|
|
fname.SetName( fname.GetName() + "-drl" );
|
|
|
|
|
|
|
|
return fname.GetFullPath();
|
|
|
|
}
|