2014-10-21 18:36:45 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2007-2014 Jean-Pierre Charras jp.charras at wanadoo.fr
|
2022-05-04 12:27:45 +00:00
|
|
|
* Copyright (C) 1992-2022 KiCad Developers, see AUTHORS.txt for contributors.
|
2014-10-21 18:36:45 +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
|
|
|
|
*/
|
|
|
|
|
2012-05-04 17:44:42 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2020-10-08 22:59:16 +00:00
|
|
|
#include <export_to_pcbnew.h>
|
|
|
|
|
2012-01-23 04:33:36 +00:00
|
|
|
#include <confirm.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>
|
2012-01-23 04:33:36 +00:00
|
|
|
#include <macros.h>
|
|
|
|
#include <trigo.h>
|
2014-06-27 17:07:42 +00:00
|
|
|
#include <gerbview_frame.h>
|
2018-01-29 12:26:58 +00:00
|
|
|
#include <gerber_file_image.h>
|
|
|
|
#include <gerber_file_image_list.h>
|
2012-04-18 07:07:13 +00:00
|
|
|
#include <build_version.h>
|
|
|
|
#include <wildcards_and_files_ext.h>
|
2020-06-06 15:35:46 +00:00
|
|
|
#include "excellon_image.h"
|
2015-09-25 19:38:09 +00:00
|
|
|
|
2014-07-14 18:59:41 +00:00
|
|
|
// Imported function
|
2021-07-21 23:14:56 +00:00
|
|
|
extern const wxString GetPCBDefaultLayerName( int aLayerNumber );
|
2014-07-14 18:59:41 +00:00
|
|
|
|
2012-05-04 17:44:42 +00:00
|
|
|
|
2012-04-17 02:58:03 +00:00
|
|
|
GBR_TO_PCB_EXPORTER::GBR_TO_PCB_EXPORTER( GERBVIEW_FRAME* aFrame, const wxString& aFileName )
|
2011-04-23 19:01:41 +00:00
|
|
|
{
|
2012-05-04 17:44:42 +00:00
|
|
|
m_gerbview_frame = aFrame;
|
|
|
|
m_pcb_file_name = aFileName;
|
2021-07-16 20:13:26 +00:00
|
|
|
m_fp = nullptr;
|
2015-02-17 16:32:47 +00:00
|
|
|
m_pcbCopperLayersCount = 2;
|
2011-04-23 19:01:41 +00:00
|
|
|
}
|
|
|
|
|
2011-11-08 16:37:25 +00:00
|
|
|
|
2011-04-23 19:01:41 +00:00
|
|
|
GBR_TO_PCB_EXPORTER::~GBR_TO_PCB_EXPORTER()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2007-05-28 18:09:49 +00:00
|
|
|
|
2021-07-21 23:14:56 +00:00
|
|
|
bool GBR_TO_PCB_EXPORTER::ExportPcb( const int* aLayerLookUpTable, int aCopperLayers )
|
2007-05-28 18:09:49 +00:00
|
|
|
{
|
2014-07-14 18:59:41 +00:00
|
|
|
LOCALE_IO toggle; // toggles on, then off, the C locale.
|
|
|
|
|
2012-05-04 17:44:42 +00:00
|
|
|
m_fp = wxFopen( m_pcb_file_name, wxT( "wt" ) );
|
2011-11-08 16:37:25 +00:00
|
|
|
|
2021-07-16 20:13:26 +00:00
|
|
|
if( m_fp == nullptr )
|
2012-05-04 17:44:42 +00:00
|
|
|
{
|
|
|
|
wxString msg;
|
2021-06-28 23:44:07 +00:00
|
|
|
msg.Printf( _( "Failed to create file '%s'." ), m_pcb_file_name );
|
2012-05-04 17:44:42 +00:00
|
|
|
DisplayError( m_gerbview_frame, msg );
|
|
|
|
return false;
|
2011-04-23 19:01:41 +00:00
|
|
|
}
|
|
|
|
|
2013-04-10 07:03:19 +00:00
|
|
|
m_pcbCopperLayersCount = aCopperLayers;
|
2011-11-08 16:37:25 +00:00
|
|
|
|
2014-07-14 18:59:41 +00:00
|
|
|
writePcbHeader( aLayerLookUpTable );
|
2007-08-23 04:28:46 +00:00
|
|
|
|
2007-10-30 21:30:58 +00:00
|
|
|
// create an image of gerber data
|
2016-05-26 11:57:43 +00:00
|
|
|
const int pcbCopperLayerMax = 31;
|
2016-06-17 18:00:29 +00:00
|
|
|
GERBER_FILE_IMAGE_LIST* images = m_gerbview_frame->GetGerberLayout()->GetImagesList();
|
2011-11-08 16:37:25 +00:00
|
|
|
|
2020-06-06 15:35:46 +00:00
|
|
|
// First collect all the holes. We'll use these to generate pads, vias, etc.
|
|
|
|
for( unsigned layer = 0; layer < images->ImagesMaxCount(); ++layer )
|
|
|
|
{
|
2022-10-31 11:01:09 +00:00
|
|
|
int pcb_layer_number = aLayerLookUpTable[layer];
|
2020-06-06 15:35:46 +00:00
|
|
|
EXCELLON_IMAGE* excellon = dynamic_cast<EXCELLON_IMAGE*>( images->GetGbrImage( layer ) );
|
2022-10-31 11:01:09 +00:00
|
|
|
GERBER_FILE_IMAGE* gerb = dynamic_cast<GERBER_FILE_IMAGE*>( images->GetGbrImage( layer ) );
|
|
|
|
if( excellon )
|
|
|
|
{
|
|
|
|
for( GERBER_DRAW_ITEM* gerb_item : excellon->GetItems() )
|
|
|
|
collect_hole( gerb_item );
|
|
|
|
}
|
2022-11-03 10:15:23 +00:00
|
|
|
else if( gerb && pcb_layer_number == UNDEFINED_LAYER ) // PCB_LAYER_ID doesn't have an entry for Hole Data,
|
|
|
|
// but the dialog returns UNDEFINED_LAYER for it
|
2022-10-31 11:01:09 +00:00
|
|
|
{
|
|
|
|
for( GERBER_DRAW_ITEM* gerb_item : gerb->GetItems() )
|
|
|
|
collect_hole( gerb_item );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-06-06 15:35:46 +00:00
|
|
|
continue;
|
2022-10-31 11:01:09 +00:00
|
|
|
}
|
2020-06-06 15:35:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Next: non copper layers:
|
2016-06-17 18:00:29 +00:00
|
|
|
for( unsigned layer = 0; layer < images->ImagesMaxCount(); ++layer )
|
2007-08-23 04:28:46 +00:00
|
|
|
{
|
2016-06-17 18:00:29 +00:00
|
|
|
GERBER_FILE_IMAGE* gerber = images->GetGbrImage( layer );
|
2016-05-26 11:57:43 +00:00
|
|
|
|
2021-07-16 20:13:26 +00:00
|
|
|
if( gerber == nullptr ) // Graphic layer not yet used
|
2016-05-26 11:57:43 +00:00
|
|
|
continue;
|
|
|
|
|
2021-07-21 23:14:56 +00:00
|
|
|
int pcb_layer_number = aLayerLookUpTable[layer];
|
2011-11-08 16:37:25 +00:00
|
|
|
|
2013-04-09 16:00:46 +00:00
|
|
|
if( !IsPcbLayer( pcb_layer_number ) )
|
2007-08-23 04:28:46 +00:00
|
|
|
continue;
|
2008-11-08 06:44:07 +00:00
|
|
|
|
2016-05-26 11:57:43 +00:00
|
|
|
if( pcb_layer_number <= pcbCopperLayerMax ) // copper layer
|
|
|
|
continue;
|
|
|
|
|
2021-07-21 23:14:56 +00:00
|
|
|
for( GERBER_DRAW_ITEM* gerb_item : gerber->GetItems() )
|
2011-04-23 19:01:41 +00:00
|
|
|
export_non_copper_item( gerb_item, pcb_layer_number );
|
2007-08-23 04:28:46 +00:00
|
|
|
}
|
|
|
|
|
2012-05-04 17:44:42 +00:00
|
|
|
// Copper layers
|
2016-06-17 18:00:29 +00:00
|
|
|
for( unsigned layer = 0; layer < images->ImagesMaxCount(); ++layer )
|
2012-04-17 02:58:03 +00:00
|
|
|
{
|
2016-06-17 18:00:29 +00:00
|
|
|
GERBER_FILE_IMAGE* gerber = images->GetGbrImage( layer );
|
2016-05-26 11:57:43 +00:00
|
|
|
|
2021-07-16 20:13:26 +00:00
|
|
|
if( gerber == nullptr ) // Graphic layer not yet used
|
2016-05-26 11:57:43 +00:00
|
|
|
continue;
|
|
|
|
|
2021-07-21 23:14:56 +00:00
|
|
|
int pcb_layer_number = aLayerLookUpTable[layer];
|
2012-04-18 07:07:13 +00:00
|
|
|
|
2014-06-27 17:07:42 +00:00
|
|
|
if( pcb_layer_number < 0 || pcb_layer_number > pcbCopperLayerMax )
|
2012-05-04 17:44:42 +00:00
|
|
|
continue;
|
2012-04-18 07:07:13 +00:00
|
|
|
|
2019-12-30 13:23:33 +00:00
|
|
|
for( GERBER_DRAW_ITEM* gerb_item : gerber->GetItems() )
|
2012-05-04 17:44:42 +00:00
|
|
|
export_copper_item( gerb_item, pcb_layer_number );
|
2012-04-17 02:58:03 +00:00
|
|
|
}
|
2011-04-23 19:01:41 +00:00
|
|
|
|
2020-06-06 15:35:46 +00:00
|
|
|
// Now write out the holes we collected earlier as vias
|
|
|
|
for( const EXPORT_VIA& via : m_vias )
|
|
|
|
export_via( via );
|
|
|
|
|
2014-07-14 18:59:41 +00:00
|
|
|
fprintf( m_fp, ")\n" );
|
2012-05-04 17:44:42 +00:00
|
|
|
|
|
|
|
fclose( m_fp );
|
2021-07-16 20:13:26 +00:00
|
|
|
m_fp = nullptr;
|
2011-04-23 19:01:41 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-04-17 02:58:03 +00:00
|
|
|
|
2021-07-21 23:14:56 +00:00
|
|
|
void GBR_TO_PCB_EXPORTER::export_non_copper_item( const GERBER_DRAW_ITEM* aGbrItem, int aLayer )
|
2011-04-23 19:01:41 +00:00
|
|
|
{
|
2020-05-20 18:50:13 +00:00
|
|
|
// used when a D_CODE is not found. default D_CODE to draw a flashed item
|
|
|
|
static D_CODE dummyD_CODE( 0 );
|
|
|
|
|
2022-01-01 06:04:08 +00:00
|
|
|
VECTOR2I seg_start = aGbrItem->m_Start;
|
|
|
|
VECTOR2I seg_end = aGbrItem->m_End;
|
2020-05-20 18:50:13 +00:00
|
|
|
D_CODE* d_codeDescr = aGbrItem->GetDcodeDescr();
|
|
|
|
SHAPE_POLY_SET polygon;
|
2011-04-23 19:01:41 +00:00
|
|
|
|
2021-07-16 20:13:26 +00:00
|
|
|
if( d_codeDescr == nullptr )
|
2020-05-20 18:50:13 +00:00
|
|
|
d_codeDescr = &dummyD_CODE;
|
2011-04-23 19:01:41 +00:00
|
|
|
|
2020-05-20 18:50:13 +00:00
|
|
|
switch( aGbrItem->m_Shape )
|
2018-03-31 16:43:12 +00:00
|
|
|
{
|
2020-05-20 18:50:13 +00:00
|
|
|
case GBR_POLYGON:
|
|
|
|
writePcbPolygon( aGbrItem->m_Polygon, aLayer );
|
|
|
|
break;
|
2018-03-31 16:43:12 +00:00
|
|
|
|
2020-05-20 18:50:13 +00:00
|
|
|
case GBR_SPOT_CIRCLE:
|
2021-07-16 20:13:26 +00:00
|
|
|
{
|
2021-06-12 07:18:48 +00:00
|
|
|
VECTOR2I center = aGbrItem->GetABPosition( seg_start );
|
|
|
|
int radius = d_codeDescr->m_Size.x / 2;
|
|
|
|
writePcbFilledCircle( center, radius, aLayer );
|
2021-07-16 20:13:26 +00:00
|
|
|
}
|
2021-06-12 07:18:48 +00:00
|
|
|
break;
|
|
|
|
|
2020-05-20 18:50:13 +00:00
|
|
|
case GBR_SPOT_RECT:
|
|
|
|
case GBR_SPOT_OVAL:
|
|
|
|
case GBR_SPOT_POLY:
|
|
|
|
case GBR_SPOT_MACRO:
|
2022-09-07 16:54:04 +00:00
|
|
|
d_codeDescr->ConvertShapeToPolygon( aGbrItem );
|
2020-05-20 18:50:13 +00:00
|
|
|
writePcbPolygon( d_codeDescr->m_Polygon, aLayer, aGbrItem->GetABPosition( seg_start ) );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GBR_ARC:
|
2011-04-23 19:01:41 +00:00
|
|
|
{
|
2020-05-20 18:50:13 +00:00
|
|
|
double a = atan2( (double) ( aGbrItem->m_Start.y - aGbrItem->m_ArcCentre.y ),
|
|
|
|
(double) ( aGbrItem->m_Start.x - aGbrItem->m_ArcCentre.x ) );
|
|
|
|
double b = atan2( (double) ( aGbrItem->m_End.y - aGbrItem->m_ArcCentre.y ),
|
|
|
|
(double) ( aGbrItem->m_End.x - aGbrItem->m_ArcCentre.x ) );
|
2011-04-23 19:01:41 +00:00
|
|
|
|
2020-05-20 18:50:13 +00:00
|
|
|
double angle = RAD2DEG(b - a);
|
|
|
|
seg_start = aGbrItem->m_ArcCentre;
|
2011-10-01 19:24:27 +00:00
|
|
|
|
2014-07-14 18:59:41 +00:00
|
|
|
// Ensure arc orientation is CCW
|
2012-05-04 17:44:42 +00:00
|
|
|
if( angle < 0 )
|
2014-07-14 18:59:41 +00:00
|
|
|
angle += 360.0;
|
2020-05-20 18:50:13 +00:00
|
|
|
|
|
|
|
// Reverse Y axis:
|
|
|
|
seg_start.y = -seg_start.y;
|
|
|
|
seg_end.y = -seg_end.y;
|
|
|
|
|
|
|
|
if( angle == 360.0 || angle == 0 )
|
|
|
|
{
|
|
|
|
fprintf( m_fp, "(gr_circle (center %s %s) (end %s %s) (layer %s) (width %s))\n",
|
2022-09-17 03:35:16 +00:00
|
|
|
FormatDouble2Str( MapToPcbUnits( seg_start.x ) ).c_str(),
|
|
|
|
FormatDouble2Str( MapToPcbUnits( seg_start.y ) ).c_str(),
|
|
|
|
FormatDouble2Str( MapToPcbUnits( seg_end.x ) ).c_str(),
|
|
|
|
FormatDouble2Str( MapToPcbUnits( seg_end.y ) ).c_str(),
|
2020-05-20 18:50:13 +00:00
|
|
|
TO_UTF8( GetPCBDefaultLayerName( aLayer ) ),
|
2022-09-17 03:35:16 +00:00
|
|
|
FormatDouble2Str( MapToPcbUnits( aGbrItem->m_Size.x ) ).c_str() );
|
2020-05-20 18:50:13 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf( m_fp, "(gr_arc (start %s %s) (end %s %s) (angle %s) (layer %s) (width %s))\n",
|
2022-09-17 03:35:16 +00:00
|
|
|
FormatDouble2Str( MapToPcbUnits( seg_start.x ) ).c_str(),
|
|
|
|
FormatDouble2Str( MapToPcbUnits( seg_start.y ) ).c_str(),
|
|
|
|
FormatDouble2Str( MapToPcbUnits( seg_end.x ) ).c_str(),
|
|
|
|
FormatDouble2Str( MapToPcbUnits( seg_end.y ) ).c_str(),
|
|
|
|
FormatDouble2Str( angle ).c_str(),
|
2020-05-20 18:50:13 +00:00
|
|
|
TO_UTF8( GetPCBDefaultLayerName( aLayer ) ),
|
2022-09-17 03:35:16 +00:00
|
|
|
FormatDouble2Str( MapToPcbUnits( aGbrItem->m_Size.x ) ).c_str() );
|
2020-05-20 18:50:13 +00:00
|
|
|
}
|
2007-08-23 04:28:46 +00:00
|
|
|
}
|
2020-05-20 18:50:13 +00:00
|
|
|
break;
|
2007-08-23 04:28:46 +00:00
|
|
|
|
2020-05-20 18:50:13 +00:00
|
|
|
case GBR_CIRCLE:
|
|
|
|
// Reverse Y axis:
|
|
|
|
seg_start.y = -seg_start.y;
|
|
|
|
seg_end.y = -seg_end.y;
|
|
|
|
|
|
|
|
fprintf( m_fp, "(gr_circle (start %s %s) (end %s %s) (layer %s) (width %s))\n",
|
2022-09-17 03:35:16 +00:00
|
|
|
FormatDouble2Str( MapToPcbUnits( seg_start.x ) ).c_str(),
|
|
|
|
FormatDouble2Str( MapToPcbUnits( seg_start.y ) ).c_str(),
|
|
|
|
FormatDouble2Str( MapToPcbUnits( seg_end.x ) ).c_str(),
|
|
|
|
FormatDouble2Str( MapToPcbUnits( seg_end.y ) ).c_str(),
|
2020-05-20 18:50:13 +00:00
|
|
|
TO_UTF8( GetPCBDefaultLayerName( aLayer ) ),
|
2022-09-17 03:35:16 +00:00
|
|
|
FormatDouble2Str( MapToPcbUnits( aGbrItem->m_Size.x ) ).c_str() );
|
2020-05-20 18:50:13 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GBR_SEGMENT:
|
|
|
|
// Reverse Y axis:
|
|
|
|
seg_start.y = -seg_start.y;
|
|
|
|
seg_end.y = -seg_end.y;
|
|
|
|
|
|
|
|
fprintf( m_fp, "(gr_line (start %s %s) (end %s %s) (layer %s) (width %s))\n",
|
2022-09-17 03:35:16 +00:00
|
|
|
FormatDouble2Str( MapToPcbUnits( seg_start.x ) ).c_str(),
|
|
|
|
FormatDouble2Str( MapToPcbUnits( seg_start.y ) ).c_str(),
|
|
|
|
FormatDouble2Str( MapToPcbUnits( seg_end.x ) ).c_str(),
|
|
|
|
FormatDouble2Str( MapToPcbUnits( seg_end.y ) ).c_str(),
|
2020-05-20 18:50:13 +00:00
|
|
|
TO_UTF8( GetPCBDefaultLayerName( aLayer ) ),
|
2022-09-17 03:35:16 +00:00
|
|
|
FormatDouble2Str( MapToPcbUnits( aGbrItem->m_Size.x ) ).c_str() );
|
2020-05-20 18:50:13 +00:00
|
|
|
break;
|
|
|
|
}
|
2011-04-23 19:01:41 +00:00
|
|
|
}
|
2008-11-08 06:44:07 +00:00
|
|
|
|
2011-11-08 16:37:25 +00:00
|
|
|
|
2020-11-18 19:50:36 +00:00
|
|
|
void GBR_TO_PCB_EXPORTER::collect_hole( const GERBER_DRAW_ITEM* aGbrItem )
|
2020-06-06 15:35:46 +00:00
|
|
|
{
|
|
|
|
int size = std::min( aGbrItem->m_Size.x, aGbrItem->m_Size.y );
|
|
|
|
m_vias.emplace_back( aGbrItem->m_Start, size + 1, size );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GBR_TO_PCB_EXPORTER::export_via( const EXPORT_VIA& aVia )
|
|
|
|
{
|
2022-01-01 06:04:08 +00:00
|
|
|
VECTOR2I via_pos = aVia.m_Pos;
|
2020-06-06 15:35:46 +00:00
|
|
|
|
|
|
|
// Reverse Y axis:
|
|
|
|
via_pos.y = -via_pos.y;
|
|
|
|
|
|
|
|
// Layers are Front to Back
|
|
|
|
fprintf( m_fp, " (via (at %s %s) (size %s) (drill %s)",
|
2022-09-17 03:35:16 +00:00
|
|
|
FormatDouble2Str( MapToPcbUnits( via_pos.x ) ).c_str(),
|
|
|
|
FormatDouble2Str( MapToPcbUnits( via_pos.y ) ).c_str(),
|
|
|
|
FormatDouble2Str( MapToPcbUnits( aVia.m_Size ) ).c_str(),
|
|
|
|
FormatDouble2Str( MapToPcbUnits( aVia.m_Drill ) ).c_str() );
|
2020-06-06 15:35:46 +00:00
|
|
|
|
|
|
|
fprintf( m_fp, " (layers %s %s))\n",
|
2021-09-09 20:49:16 +00:00
|
|
|
TO_UTF8( GetPCBDefaultLayerName( F_Cu ) ),
|
|
|
|
TO_UTF8( GetPCBDefaultLayerName( B_Cu ) ) );
|
2020-06-06 15:35:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-21 23:14:56 +00:00
|
|
|
void GBR_TO_PCB_EXPORTER::export_copper_item( const GERBER_DRAW_ITEM* aGbrItem, int aLayer )
|
2011-04-23 19:01:41 +00:00
|
|
|
{
|
|
|
|
switch( aGbrItem->m_Shape )
|
|
|
|
{
|
2012-05-04 17:44:42 +00:00
|
|
|
case GBR_SPOT_CIRCLE:
|
|
|
|
case GBR_SPOT_RECT:
|
|
|
|
case GBR_SPOT_OVAL:
|
2022-09-07 16:54:04 +00:00
|
|
|
case GBR_SPOT_POLY:
|
|
|
|
case GBR_SPOT_MACRO:
|
2020-06-06 15:35:46 +00:00
|
|
|
export_flashed_copper_item( aGbrItem, aLayer );
|
2012-05-04 17:44:42 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GBR_ARC:
|
|
|
|
export_segarc_copper_item( aGbrItem, aLayer );
|
|
|
|
break;
|
|
|
|
|
2018-03-31 16:43:12 +00:00
|
|
|
case GBR_POLYGON:
|
2019-12-15 10:50:19 +00:00
|
|
|
// One can use a polygon or a zone to output a Gerber region.
|
|
|
|
// none are perfect.
|
|
|
|
// The current way is use a polygon, as the zone export
|
2021-06-09 19:32:58 +00:00
|
|
|
// is experimental and only for tests.
|
2019-12-15 10:50:19 +00:00
|
|
|
#if 1
|
2020-05-20 18:50:13 +00:00
|
|
|
writePcbPolygon( aGbrItem->m_Polygon, aLayer );
|
2019-12-15 10:50:19 +00:00
|
|
|
#else
|
|
|
|
// Only for tests:
|
|
|
|
writePcbZoneItem( aGbrItem, aLayer );
|
|
|
|
#endif
|
2018-03-31 16:43:12 +00:00
|
|
|
break;
|
|
|
|
|
2012-05-04 17:44:42 +00:00
|
|
|
default:
|
|
|
|
export_segline_copper_item( aGbrItem, aLayer );
|
|
|
|
break;
|
2011-04-23 19:01:41 +00:00
|
|
|
}
|
|
|
|
}
|
2007-08-23 04:28:46 +00:00
|
|
|
|
2011-11-08 16:37:25 +00:00
|
|
|
|
2021-07-21 23:14:56 +00:00
|
|
|
void GBR_TO_PCB_EXPORTER::export_segline_copper_item( const GERBER_DRAW_ITEM* aGbrItem, int aLayer )
|
2011-04-23 19:01:41 +00:00
|
|
|
{
|
2022-01-01 06:04:08 +00:00
|
|
|
VECTOR2I seg_start, seg_end;
|
2012-05-04 17:44:42 +00:00
|
|
|
|
|
|
|
seg_start = aGbrItem->m_Start;
|
|
|
|
seg_end = aGbrItem->m_End;
|
2011-09-09 19:30:59 +00:00
|
|
|
|
|
|
|
// Reverse Y axis:
|
2015-06-26 13:41:56 +00:00
|
|
|
seg_start.y = -seg_start.y;
|
|
|
|
seg_end.y = -seg_end.y;
|
2011-09-09 19:30:59 +00:00
|
|
|
|
2014-07-14 18:59:41 +00:00
|
|
|
writeCopperLineItem( seg_start, seg_end, aGbrItem->m_Size.x, aLayer );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-01-01 06:04:08 +00:00
|
|
|
void GBR_TO_PCB_EXPORTER::writeCopperLineItem( const VECTOR2I& aStart, const VECTOR2I& aEnd,
|
2021-07-21 23:14:56 +00:00
|
|
|
int aWidth, int aLayer )
|
2014-07-14 18:59:41 +00:00
|
|
|
{
|
|
|
|
fprintf( m_fp, "(segment (start %s %s) (end %s %s) (width %s) (layer %s) (net 0))\n",
|
2022-09-17 03:35:16 +00:00
|
|
|
FormatDouble2Str( MapToPcbUnits(aStart.x) ).c_str(),
|
|
|
|
FormatDouble2Str( MapToPcbUnits(aStart.y) ).c_str(),
|
|
|
|
FormatDouble2Str( MapToPcbUnits(aEnd.x) ).c_str(),
|
|
|
|
FormatDouble2Str( MapToPcbUnits(aEnd.y) ).c_str(),
|
|
|
|
FormatDouble2Str( MapToPcbUnits( aWidth ) ).c_str(),
|
2021-09-09 20:49:16 +00:00
|
|
|
TO_UTF8( GetPCBDefaultLayerName( aLayer ) ) );
|
2011-04-23 19:01:41 +00:00
|
|
|
}
|
|
|
|
|
2011-11-08 16:37:25 +00:00
|
|
|
|
2021-07-21 23:14:56 +00:00
|
|
|
void GBR_TO_PCB_EXPORTER::export_segarc_copper_item( const GERBER_DRAW_ITEM* aGbrItem, int aLayer )
|
2011-04-23 19:01:41 +00:00
|
|
|
{
|
2012-05-04 17:44:42 +00:00
|
|
|
double a = atan2( (double) ( aGbrItem->m_Start.y - aGbrItem->m_ArcCentre.y ),
|
|
|
|
(double) ( aGbrItem->m_Start.x - aGbrItem->m_ArcCentre.x ) );
|
|
|
|
double b = atan2( (double) ( aGbrItem->m_End.y - aGbrItem->m_ArcCentre.y ),
|
|
|
|
(double) ( aGbrItem->m_End.x - aGbrItem->m_ArcCentre.x ) );
|
|
|
|
|
2022-01-01 06:04:08 +00:00
|
|
|
VECTOR2I start = aGbrItem->m_Start;
|
|
|
|
VECTOR2I end = aGbrItem->m_End;
|
2011-04-23 19:01:41 +00:00
|
|
|
|
|
|
|
/* Because Pcbnew does not know arcs in tracks,
|
2012-01-13 18:35:46 +00:00
|
|
|
* approximate arc by segments (SEG_COUNT__CIRCLE segment per 360 deg)
|
2014-07-14 18:59:41 +00:00
|
|
|
* The arc is drawn anticlockwise from the start point to the end point.
|
2011-04-23 19:01:41 +00:00
|
|
|
*/
|
2012-05-04 17:44:42 +00:00
|
|
|
#define SEG_COUNT_CIRCLE 16
|
|
|
|
#define DELTA_ANGLE 2 * M_PI / SEG_COUNT_CIRCLE
|
2011-11-08 16:37:25 +00:00
|
|
|
|
2012-01-13 18:35:46 +00:00
|
|
|
// calculate the number of segments from a to b.
|
|
|
|
// we want CNT_PER_360 segments fo a circle
|
|
|
|
if( a > b )
|
2012-05-04 17:44:42 +00:00
|
|
|
b += 2 * M_PI;
|
2011-11-08 16:37:25 +00:00
|
|
|
|
2022-01-01 06:04:08 +00:00
|
|
|
VECTOR2I curr_start = start;
|
|
|
|
VECTOR2I seg_start, seg_end;
|
2012-05-04 17:44:42 +00:00
|
|
|
|
|
|
|
int ii = 1;
|
2011-11-08 16:37:25 +00:00
|
|
|
|
2012-01-13 18:35:46 +00:00
|
|
|
for( double rot = a; rot < (b - DELTA_ANGLE); rot += DELTA_ANGLE, ii++ )
|
2011-04-23 19:01:41 +00:00
|
|
|
{
|
2012-05-04 17:44:42 +00:00
|
|
|
seg_start = curr_start;
|
2022-01-01 06:04:08 +00:00
|
|
|
VECTOR2I curr_end = start;
|
2022-01-18 02:14:09 +00:00
|
|
|
RotatePoint( curr_end, aGbrItem->m_ArcCentre, -EDA_ANGLE( DELTA_ANGLE, RADIANS_T ) * ii );
|
2012-05-04 17:44:42 +00:00
|
|
|
seg_end = curr_end;
|
2021-07-16 20:13:26 +00:00
|
|
|
|
2011-09-09 19:30:59 +00:00
|
|
|
// Reverse Y axis:
|
2015-06-26 13:41:56 +00:00
|
|
|
seg_start.y = -seg_start.y;
|
|
|
|
seg_end.y = -seg_end.y;
|
2014-07-14 18:59:41 +00:00
|
|
|
writeCopperLineItem( seg_start, seg_end, aGbrItem->m_Size.x, aLayer );
|
2011-04-23 19:01:41 +00:00
|
|
|
curr_start = curr_end;
|
|
|
|
}
|
2011-11-08 16:37:25 +00:00
|
|
|
|
2011-04-23 19:01:41 +00:00
|
|
|
if( end != curr_start )
|
|
|
|
{
|
2012-05-04 17:44:42 +00:00
|
|
|
seg_start = curr_start;
|
|
|
|
seg_end = end;
|
2021-07-16 20:13:26 +00:00
|
|
|
|
2011-09-09 19:30:59 +00:00
|
|
|
// Reverse Y axis:
|
2015-06-26 13:41:56 +00:00
|
|
|
seg_start.y = -seg_start.y;
|
|
|
|
seg_end.y = -seg_end.y;
|
2014-07-14 18:59:41 +00:00
|
|
|
writeCopperLineItem( seg_start, seg_end, aGbrItem->m_Size.x, aLayer );
|
2011-04-23 19:01:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-31 16:45:17 +00:00
|
|
|
#include <wx/log.h>
|
2021-07-21 23:14:56 +00:00
|
|
|
void GBR_TO_PCB_EXPORTER::export_flashed_copper_item( const GERBER_DRAW_ITEM* aGbrItem, int aLayer )
|
2011-04-23 19:01:41 +00:00
|
|
|
{
|
2020-06-06 15:35:46 +00:00
|
|
|
static D_CODE flashed_item_D_CODE( 0 );
|
|
|
|
|
|
|
|
D_CODE* d_codeDescr = aGbrItem->GetDcodeDescr();
|
2012-05-04 17:44:42 +00:00
|
|
|
|
2021-07-16 20:13:26 +00:00
|
|
|
if( d_codeDescr == nullptr )
|
2020-06-06 15:35:46 +00:00
|
|
|
d_codeDescr = &flashed_item_D_CODE;
|
2011-04-23 19:01:41 +00:00
|
|
|
|
2020-06-06 15:35:46 +00:00
|
|
|
if( aGbrItem->m_Shape == GBR_SPOT_CIRCLE )
|
|
|
|
{
|
|
|
|
// See if there's a via that we can enlarge to fit this flashed item
|
|
|
|
for( EXPORT_VIA& via : m_vias )
|
|
|
|
{
|
|
|
|
if( via.m_Pos == aGbrItem->m_Start )
|
|
|
|
{
|
|
|
|
via.m_Size = std::max( via.m_Size, aGbrItem->m_Size.x );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-05-04 17:44:42 +00:00
|
|
|
|
2022-01-01 06:04:08 +00:00
|
|
|
VECTOR2I offset = aGbrItem->GetABPosition( aGbrItem->m_Start );
|
2012-05-04 17:44:42 +00:00
|
|
|
|
2022-10-31 16:45:17 +00:00
|
|
|
if( aGbrItem->m_Shape == GBR_SPOT_CIRCLE ||
|
|
|
|
( aGbrItem->m_Shape == GBR_SPOT_OVAL && d_codeDescr->m_Size.x == d_codeDescr->m_Size.y ) )
|
2021-06-12 07:18:48 +00:00
|
|
|
{
|
2022-10-31 16:45:17 +00:00
|
|
|
// export it as filled circle
|
2021-06-12 07:18:48 +00:00
|
|
|
VECTOR2I center = offset;
|
|
|
|
int radius = d_codeDescr->m_Size.x / 2;
|
|
|
|
writePcbFilledCircle( center, radius, aLayer );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-09-07 16:54:04 +00:00
|
|
|
APERTURE_MACRO* macro = d_codeDescr->GetMacro();
|
2022-10-31 16:45:17 +00:00
|
|
|
|
|
|
|
if( macro ) // export a GBR_SPOT_MACRO
|
|
|
|
{
|
|
|
|
SHAPE_POLY_SET macroShape;
|
|
|
|
macroShape = *macro->GetApertureMacroShape( aGbrItem, VECTOR2I( 0, 0 ) );
|
|
|
|
|
|
|
|
// Compensate the Y axis orientation ( writePcbPolygon invert the Y coordinate )
|
|
|
|
macroShape.Outline( 0 ).Mirror( false, true );
|
|
|
|
|
|
|
|
writePcbPolygon( macroShape, aLayer, offset );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Should cover primitives: GBR_SPOT_RECT, GBR_SPOT_OVAL, GBR_SPOT_POLY
|
|
|
|
d_codeDescr->ConvertShapeToPolygon( aGbrItem );
|
|
|
|
writePcbPolygon( d_codeDescr->m_Polygon, aLayer, offset );
|
|
|
|
}
|
2014-07-14 18:59:41 +00:00
|
|
|
}
|
2012-05-04 17:44:42 +00:00
|
|
|
|
2020-06-06 15:35:46 +00:00
|
|
|
|
2021-06-12 07:18:48 +00:00
|
|
|
void GBR_TO_PCB_EXPORTER::writePcbFilledCircle( const VECTOR2I& aCenterPosition, int aRadius,
|
2021-07-21 23:14:56 +00:00
|
|
|
int aLayer )
|
2021-06-12 07:18:48 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
fprintf( m_fp, "(gr_circle (center %s %s) (end %s %s)",
|
2022-09-17 03:35:16 +00:00
|
|
|
FormatDouble2Str( MapToPcbUnits( aCenterPosition.x ) ).c_str(),
|
|
|
|
FormatDouble2Str( MapToPcbUnits( aCenterPosition.y ) ).c_str(),
|
|
|
|
FormatDouble2Str( MapToPcbUnits( aCenterPosition.x + aRadius ) ).c_str(),
|
|
|
|
FormatDouble2Str( MapToPcbUnits( aCenterPosition.y ) ).c_str() );
|
2021-06-12 07:18:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
fprintf( m_fp, "(layer %s) (width 0) (fill solid) )\n",
|
|
|
|
TO_UTF8( GetPCBDefaultLayerName( aLayer ) ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-21 23:14:56 +00:00
|
|
|
void GBR_TO_PCB_EXPORTER::writePcbHeader( const int* aLayerLookUpTable )
|
2014-07-14 18:59:41 +00:00
|
|
|
{
|
2021-06-08 21:11:48 +00:00
|
|
|
fprintf( m_fp, "(kicad_pcb (version 4) (generator gerbview)\n\n" );
|
2012-05-04 17:44:42 +00:00
|
|
|
|
2014-07-14 18:59:41 +00:00
|
|
|
// Write layers section
|
|
|
|
fprintf( m_fp, " (layers \n" );
|
2012-05-04 17:44:42 +00:00
|
|
|
|
2014-07-14 18:59:41 +00:00
|
|
|
for( int ii = 0; ii < m_pcbCopperLayersCount; ii++ )
|
|
|
|
{
|
|
|
|
int id = ii;
|
2012-05-04 17:44:42 +00:00
|
|
|
|
2014-07-14 18:59:41 +00:00
|
|
|
if( ii == m_pcbCopperLayersCount-1)
|
|
|
|
id = B_Cu;
|
2012-05-04 17:44:42 +00:00
|
|
|
|
2014-07-14 18:59:41 +00:00
|
|
|
fprintf( m_fp, " (%d %s signal)\n", id, TO_UTF8( GetPCBDefaultLayerName( id ) ) );
|
|
|
|
}
|
2012-05-04 17:44:42 +00:00
|
|
|
|
2017-03-13 03:19:33 +00:00
|
|
|
for( int ii = B_Adhes; ii < PCB_LAYER_ID_COUNT; ii++ )
|
2014-07-14 18:59:41 +00:00
|
|
|
{
|
2018-12-21 12:48:00 +00:00
|
|
|
if( GetPCBDefaultLayerName( ii ).IsEmpty() ) // Layer not available for export
|
|
|
|
continue;
|
|
|
|
|
2014-07-14 18:59:41 +00:00
|
|
|
fprintf( m_fp, " (%d %s user)\n", ii, TO_UTF8( GetPCBDefaultLayerName( ii ) ) );
|
|
|
|
}
|
2012-05-04 17:44:42 +00:00
|
|
|
|
2014-07-14 18:59:41 +00:00
|
|
|
fprintf( m_fp, " )\n\n" );
|
|
|
|
}
|
2012-05-04 17:44:42 +00:00
|
|
|
|
|
|
|
|
2021-07-21 23:14:56 +00:00
|
|
|
void GBR_TO_PCB_EXPORTER::writePcbPolygon( const SHAPE_POLY_SET& aPolys, int aLayer,
|
2022-01-01 06:04:08 +00:00
|
|
|
const VECTOR2I& aOffset )
|
2014-07-14 18:59:41 +00:00
|
|
|
{
|
2019-12-15 10:50:19 +00:00
|
|
|
// Ensure the polygon is valid:
|
2022-05-04 12:27:45 +00:00
|
|
|
if( aPolys.OutlineCount() < 1 )
|
2019-12-15 10:50:19 +00:00
|
|
|
return;
|
|
|
|
|
2022-05-04 12:27:45 +00:00
|
|
|
// aPolys is expected having only one outline and no hole
|
|
|
|
// (because it comes from a gerber file or is built from a aperture )
|
|
|
|
const SHAPE_LINE_CHAIN& poly = aPolys.COutline( 0 );
|
2018-03-31 16:43:12 +00:00
|
|
|
|
2019-12-15 10:50:19 +00:00
|
|
|
fprintf( m_fp, "(gr_poly (pts " );
|
|
|
|
|
2018-03-31 16:43:12 +00:00
|
|
|
#define MAX_COORD_CNT 4
|
|
|
|
int jj = MAX_COORD_CNT;
|
|
|
|
int cnt_max = poly.PointCount() -1;
|
|
|
|
|
|
|
|
// Do not generate last corner, if it is the same point as the first point:
|
2019-03-23 18:26:44 +00:00
|
|
|
if( poly.CPoint( 0 ) == poly.CPoint( cnt_max ) )
|
2018-03-31 16:43:12 +00:00
|
|
|
cnt_max--;
|
|
|
|
|
|
|
|
for( int ii = 0; ii <= cnt_max; ii++ )
|
|
|
|
{
|
|
|
|
if( --jj == 0 )
|
|
|
|
{
|
|
|
|
jj = MAX_COORD_CNT;
|
|
|
|
fprintf( m_fp, "\n" );
|
|
|
|
}
|
|
|
|
|
2020-05-20 18:50:13 +00:00
|
|
|
fprintf( m_fp, " (xy %s %s)",
|
2022-09-17 03:35:16 +00:00
|
|
|
FormatDouble2Str( MapToPcbUnits( poly.CPoint( ii ).x + aOffset.x ) ).c_str(),
|
|
|
|
FormatDouble2Str( MapToPcbUnits( -poly.CPoint( ii ).y + aOffset.y ) ).c_str() );
|
2018-03-31 16:43:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fprintf( m_fp, ")" );
|
|
|
|
|
|
|
|
if( jj != MAX_COORD_CNT )
|
|
|
|
fprintf( m_fp, "\n" );
|
|
|
|
|
2021-07-16 20:13:26 +00:00
|
|
|
fprintf( m_fp, "(layer %s) (width 0) )\n", TO_UTF8( GetPCBDefaultLayerName( aLayer ) ) );
|
2018-03-31 16:43:12 +00:00
|
|
|
}
|
2019-12-15 10:50:19 +00:00
|
|
|
|
|
|
|
|
2021-07-21 23:14:56 +00:00
|
|
|
void GBR_TO_PCB_EXPORTER::writePcbZoneItem( const GERBER_DRAW_ITEM* aGbrItem, int aLayer )
|
2019-12-15 10:50:19 +00:00
|
|
|
{
|
2022-03-14 15:52:12 +00:00
|
|
|
SHAPE_POLY_SET polys = aGbrItem->m_Polygon.CloneDropTriangulation();
|
2022-05-04 12:27:45 +00:00
|
|
|
polys.Simplify( SHAPE_POLY_SET::PM_FAST );
|
2019-12-15 10:50:19 +00:00
|
|
|
|
|
|
|
if( polys.OutlineCount() == 0 )
|
|
|
|
return;
|
|
|
|
|
|
|
|
fprintf( m_fp, "(zone (net 0) (net_name \"\") (layer %s) (tstamp 0000000) (hatch edge 0.508)\n",
|
2021-07-16 20:13:26 +00:00
|
|
|
TO_UTF8( GetPCBDefaultLayerName( aLayer ) ) );
|
2019-12-15 10:50:19 +00:00
|
|
|
|
|
|
|
fprintf( m_fp, " (connect_pads (clearance 0.0))\n" );
|
|
|
|
|
|
|
|
fprintf( m_fp, " (min_thickness 0.1) (filled_areas_thickness no)\n"
|
|
|
|
" (fill (thermal_gap 0.3) (thermal_bridge_width 0.3))\n" );
|
|
|
|
|
|
|
|
// Now, write the zone outlines with holes.
|
|
|
|
// first polygon is the main outline, next are holes
|
|
|
|
// One cannot know the initial zone outline.
|
|
|
|
// However most of (if not all) holes are just items with clearance,
|
|
|
|
// not really a hole in the initial zone outline.
|
|
|
|
// So we build a zone outline only with no hole.
|
|
|
|
fprintf( m_fp, " (polygon\n (pts" );
|
|
|
|
|
|
|
|
SHAPE_LINE_CHAIN& poly = polys.Outline( 0 );
|
|
|
|
|
|
|
|
#define MAX_COORD_CNT 4
|
|
|
|
int jj = MAX_COORD_CNT;
|
|
|
|
int cnt_max = poly.PointCount() -1;
|
|
|
|
|
|
|
|
// Do not generate last corner, if it is the same point as the first point:
|
|
|
|
if( poly.CPoint( 0 ) == poly.CPoint( cnt_max ) )
|
|
|
|
cnt_max--;
|
|
|
|
|
|
|
|
for( int ii = 0; ii <= cnt_max; ii++ )
|
|
|
|
{
|
|
|
|
if( --jj == 0 )
|
|
|
|
{
|
|
|
|
jj = MAX_COORD_CNT;
|
|
|
|
fprintf( m_fp, "\n " );
|
|
|
|
}
|
|
|
|
|
2022-09-17 03:35:16 +00:00
|
|
|
fprintf( m_fp, " (xy %s %s)", FormatDouble2Str( MapToPcbUnits( poly.CPoint( ii ).x ) ).c_str(),
|
|
|
|
FormatDouble2Str( MapToPcbUnits( -poly.CPoint( ii ).y ) ).c_str() );
|
2019-12-15 10:50:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fprintf( m_fp, ")\n" );
|
|
|
|
|
|
|
|
fprintf( m_fp, " )\n)\n" );
|
|
|
|
}
|