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
|
2017-11-12 00:31:38 +00:00
|
|
|
* Copyright (C) 1992-2017 KiCad Developers, see change_log.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
|
|
|
|
*/
|
|
|
|
|
2011-09-23 13:57:12 +00:00
|
|
|
/**
|
|
|
|
* @file export_to_pcbnew.cpp
|
2011-09-30 18:15:37 +00:00
|
|
|
* @brief Export the layers to Pcbnew.
|
2007-08-23 04:28:46 +00:00
|
|
|
*/
|
2007-05-28 18:09:49 +00:00
|
|
|
|
2012-05-04 17:44:42 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2012-01-23 04:33:36 +00:00
|
|
|
#include <fctsys.h>
|
|
|
|
#include <common.h>
|
|
|
|
#include <confirm.h>
|
|
|
|
#include <macros.h>
|
|
|
|
#include <trigo.h>
|
|
|
|
#include <gerbview.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-01-23 04:33:36 +00:00
|
|
|
#include <select_layers_to_pcb.h>
|
2012-04-18 07:07:13 +00:00
|
|
|
#include <build_version.h>
|
|
|
|
#include <wildcards_and_files_ext.h>
|
2011-04-23 19:01:41 +00:00
|
|
|
|
2015-09-25 19:38:09 +00:00
|
|
|
|
2014-07-14 18:59:41 +00:00
|
|
|
// Imported function
|
|
|
|
extern const wxString GetPCBDefaultLayerName( LAYER_NUM aLayerNumber );
|
|
|
|
|
2012-05-04 17:44:42 +00:00
|
|
|
|
2011-04-23 19:01:41 +00:00
|
|
|
/* A helper class to export a Gerber set of files to Pcbnew
|
2012-05-04 17:44:42 +00:00
|
|
|
*/
|
2011-04-23 19:01:41 +00:00
|
|
|
class GBR_TO_PCB_EXPORTER
|
|
|
|
{
|
2012-05-04 17:44:42 +00:00
|
|
|
private:
|
|
|
|
GERBVIEW_FRAME* m_gerbview_frame; // the main gerber frame
|
|
|
|
wxString m_pcb_file_name; // BOARD file to write to
|
|
|
|
FILE* m_fp; // the board file
|
|
|
|
int m_pcbCopperLayersCount;
|
|
|
|
std::vector<wxPoint> m_vias_coordinates; // list of already generated vias,
|
|
|
|
// used to export only once a via
|
|
|
|
// having a given coordinate
|
2011-04-23 19:01:41 +00:00
|
|
|
public:
|
2012-04-17 02:58:03 +00:00
|
|
|
GBR_TO_PCB_EXPORTER( GERBVIEW_FRAME* aFrame, const wxString& aFileName );
|
2011-04-23 19:01:41 +00:00
|
|
|
~GBR_TO_PCB_EXPORTER();
|
2012-04-17 02:58:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Function ExportPcb
|
2012-05-04 17:44:42 +00:00
|
|
|
* saves a board from a set of Gerber images.
|
2012-04-17 02:58:03 +00:00
|
|
|
*/
|
2014-07-14 18:59:41 +00:00
|
|
|
bool ExportPcb( LAYER_NUM* aLayerLookUpTable, int aCopperLayers );
|
2011-04-23 19:01:41 +00:00
|
|
|
|
|
|
|
private:
|
2012-05-04 17:44:42 +00:00
|
|
|
/**
|
|
|
|
* write a non copper line or arc to the board file.
|
|
|
|
* @param aGbrItem = the Gerber item (line, arc) to export
|
|
|
|
* @param aLayer = the technical layer to use
|
|
|
|
*/
|
2013-03-31 13:27:46 +00:00
|
|
|
void export_non_copper_item( GERBER_DRAW_ITEM* aGbrItem, LAYER_NUM aLayer );
|
2012-05-04 17:44:42 +00:00
|
|
|
|
|
|
|
/**
|
2019-12-15 10:50:19 +00:00
|
|
|
* write anot filled polygon item to the board file.
|
2018-03-31 16:43:12 +00:00
|
|
|
* @param aGbrItem = the Gerber item (line, arc) to export
|
|
|
|
* @param aLayer = the technical layer to use
|
|
|
|
*/
|
|
|
|
void writePcbPolygonItem( GERBER_DRAW_ITEM* aGbrItem, LAYER_NUM aLayer );
|
|
|
|
|
2019-12-15 10:50:19 +00:00
|
|
|
/**
|
|
|
|
* write a zone item to the board file.
|
|
|
|
* Currently: only experimental, for tests
|
|
|
|
* @param aGbrItem = the Gerber item (line, arc) to export
|
|
|
|
* @param aLayer = the technical layer to use
|
|
|
|
*/
|
|
|
|
void writePcbZoneItem( GERBER_DRAW_ITEM* aGbrItem, LAYER_NUM aLayer );
|
|
|
|
|
2018-03-31 16:43:12 +00:00
|
|
|
/**
|
2012-05-04 17:44:42 +00:00
|
|
|
* write a track or via) to the board file.
|
|
|
|
* @param aGbrItem = the Gerber item (line, arc, flashed) to export
|
|
|
|
* @param aLayer = the copper layer to use
|
|
|
|
*/
|
2013-03-31 13:27:46 +00:00
|
|
|
void export_copper_item( GERBER_DRAW_ITEM* aGbrItem, LAYER_NUM aLayer );
|
2012-05-04 17:44:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Function export_flashed_copper_item
|
|
|
|
* write a via to the board file (always uses a via through).
|
|
|
|
* @param aGbrItem = the flashed Gerber item to export
|
|
|
|
*/
|
2012-09-12 17:28:55 +00:00
|
|
|
void export_flashed_copper_item( GERBER_DRAW_ITEM* aGbrItem );
|
2012-05-04 17:44:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Function export_segline_copper_item
|
|
|
|
* write a track (not via) to the board file.
|
|
|
|
* @param aGbrItem = the Gerber item (line only) to export
|
|
|
|
* @param aLayer = the copper layer to use
|
|
|
|
*/
|
2013-03-31 13:27:46 +00:00
|
|
|
void export_segline_copper_item( GERBER_DRAW_ITEM* aGbrItem, LAYER_NUM aLayer );
|
2012-05-04 17:44:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Function export_segarc_copper_item
|
|
|
|
* write a set of tracks (arcs are approximated by track segments)
|
|
|
|
* to the board file.
|
|
|
|
* @param aGbrItem = the Gerber item (arc only) to export
|
|
|
|
* @param aLayer = the copper layer to use
|
|
|
|
*/
|
2013-03-31 13:27:46 +00:00
|
|
|
void export_segarc_copper_item( GERBER_DRAW_ITEM* aGbrItem, LAYER_NUM aLayer );
|
2012-05-04 17:44:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* function writePcbLineItem
|
2014-07-14 18:59:41 +00:00
|
|
|
* basic write function to write a DRAWSEGMENT item or a TRACK item
|
|
|
|
* to the board file, from a non flashed item
|
|
|
|
*/
|
|
|
|
void writePcbLineItem( bool aIsArc, wxPoint& aStart, wxPoint& aEnd,
|
|
|
|
int aWidth, LAYER_NUM aLayer, double aAngle = 0 );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* function writeCopperLineItem
|
|
|
|
* basic write function to write a a TRACK item
|
|
|
|
* to the board file, from a non flashed item
|
2012-05-04 17:44:42 +00:00
|
|
|
*/
|
2014-07-14 18:59:41 +00:00
|
|
|
void writeCopperLineItem( wxPoint& aStart, wxPoint& aEnd,
|
|
|
|
int aWidth, LAYER_NUM aLayer );
|
2012-05-04 17:44:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* function writePcbHeader
|
|
|
|
* Write a very basic header to the board file
|
|
|
|
*/
|
2014-07-14 18:59:41 +00:00
|
|
|
void writePcbHeader( LAYER_NUM* aLayerLookUpTable );
|
2018-12-21 12:48:00 +00:00
|
|
|
|
|
|
|
/** In Pcbnew files units are mm for coordinates.
|
|
|
|
* So MapToPcbUnits converts internal gerbview to mm any pcbnew value
|
|
|
|
* @param aValue is a coordinate value to convert in mm
|
|
|
|
*/
|
|
|
|
double MapToPcbUnits( int aValue )
|
|
|
|
{
|
|
|
|
return aValue / IU_PER_MM;
|
|
|
|
}
|
2011-04-23 19:01:41 +00:00
|
|
|
};
|
|
|
|
|
2011-11-08 16:37:25 +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;
|
2015-02-17 16:32:47 +00:00
|
|
|
m_fp = NULL;
|
|
|
|
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
|
|
|
|
2011-09-30 18:15:37 +00:00
|
|
|
/* Export data in Pcbnew format
|
2011-09-09 19:30:59 +00:00
|
|
|
* remember Pcbnew uses a Y reversed axis, so we must negate all Y coordinates
|
2007-08-23 04:28:46 +00:00
|
|
|
*/
|
2011-03-12 09:50:21 +00:00
|
|
|
void GERBVIEW_FRAME::ExportDataInPcbnewFormat( wxCommandEvent& event )
|
2007-05-28 18:09:49 +00:00
|
|
|
{
|
2011-11-24 19:57:41 +00:00
|
|
|
int layercount = 0;
|
2007-10-07 03:08:24 +00:00
|
|
|
|
2016-06-17 18:00:29 +00:00
|
|
|
GERBER_FILE_IMAGE_LIST* images = GetGerberLayout()->GetImagesList();
|
2017-11-12 00:31:38 +00:00
|
|
|
|
2011-11-24 19:57:41 +00:00
|
|
|
// Count the Gerber layers which are actually currently used
|
2016-06-17 18:00:29 +00:00
|
|
|
for( LAYER_NUM ii = 0; ii < (LAYER_NUM)images->ImagesMaxCount(); ++ii )
|
2007-11-22 00:40:17 +00:00
|
|
|
{
|
2016-06-17 18:00:29 +00:00
|
|
|
if( images->GetGbrImage( ii ) )
|
2011-11-24 19:57:41 +00:00
|
|
|
layercount++;
|
2007-10-07 03:08:24 +00:00
|
|
|
}
|
|
|
|
|
2011-11-24 19:57:41 +00:00
|
|
|
if( layercount == 0 )
|
2007-10-07 03:08:24 +00:00
|
|
|
{
|
2009-11-09 14:00:22 +00:00
|
|
|
DisplayInfoMessage( this,
|
2011-11-08 16:37:25 +00:00
|
|
|
_( "None of the Gerber layers contain any data" ) );
|
2007-10-07 03:08:24 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-05-04 17:44:42 +00:00
|
|
|
wxString fileName;
|
2015-09-25 19:38:09 +00:00
|
|
|
wxString path = m_mruPath;
|
2012-04-18 07:07:13 +00:00
|
|
|
|
2017-11-12 00:31:38 +00:00
|
|
|
wxFileDialog filedlg( this, _( "Board File Name" ),
|
|
|
|
path, fileName, PcbFileWildcard(),
|
2015-09-25 19:38:09 +00:00
|
|
|
wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
|
2012-04-18 07:07:13 +00:00
|
|
|
|
|
|
|
if( filedlg.ShowModal() == wxID_CANCEL )
|
2007-08-23 04:28:46 +00:00
|
|
|
return;
|
|
|
|
|
2012-04-18 07:07:13 +00:00
|
|
|
fileName = filedlg.GetPath();
|
|
|
|
|
2011-04-26 08:30:16 +00:00
|
|
|
/* Install a dialog frame to choose the mapping
|
2011-09-30 18:15:37 +00:00
|
|
|
* between gerber layers and Pcbnew layers
|
2011-04-26 08:30:16 +00:00
|
|
|
*/
|
2012-04-18 07:07:13 +00:00
|
|
|
LAYERS_MAP_DIALOG* layerdlg = new LAYERS_MAP_DIALOG( this );
|
|
|
|
int ok = layerdlg->ShowModal();
|
|
|
|
layerdlg->Destroy();
|
2011-11-08 16:37:25 +00:00
|
|
|
|
2011-04-26 08:30:16 +00:00
|
|
|
if( ok != wxID_OK )
|
|
|
|
return;
|
|
|
|
|
2015-09-25 19:38:09 +00:00
|
|
|
m_mruPath = wxFileName( fileName ).GetPath();
|
2011-04-26 08:30:16 +00:00
|
|
|
|
2012-05-04 17:44:42 +00:00
|
|
|
GBR_TO_PCB_EXPORTER gbr_exporter( this, fileName );
|
2011-11-08 16:37:25 +00:00
|
|
|
|
2015-09-25 19:38:09 +00:00
|
|
|
gbr_exporter.ExportPcb( layerdlg->GetLayersLookUpTable(), layerdlg->GetCopperLayersCount() );
|
2007-05-28 18:09:49 +00:00
|
|
|
}
|
|
|
|
|
2011-11-08 16:37:25 +00:00
|
|
|
|
2014-07-14 18:59:41 +00:00
|
|
|
bool GBR_TO_PCB_EXPORTER::ExportPcb( LAYER_NUM* 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
|
|
|
|
2012-05-04 17:44:42 +00:00
|
|
|
if( m_fp == NULL )
|
|
|
|
{
|
|
|
|
wxString msg;
|
2017-12-15 11:37:46 +00:00
|
|
|
msg.Printf( _( "Cannot create file \"%s\"" ), GetChars( 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
|
2012-05-04 17:44:42 +00:00
|
|
|
// First: non copper layers:
|
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
|
|
|
|
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
|
|
|
|
|
|
|
if( gerber == NULL ) // Graphic layer not yet used
|
|
|
|
continue;
|
|
|
|
|
2014-07-14 18:59:41 +00:00
|
|
|
LAYER_NUM 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;
|
|
|
|
|
2019-12-30 13:23:33 +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
|
|
|
|
|
|
|
if( gerber == NULL ) // Graphic layer not yet used
|
|
|
|
continue;
|
|
|
|
|
2014-07-14 18:59:41 +00:00
|
|
|
LAYER_NUM 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
|
|
|
|
2014-07-14 18:59:41 +00:00
|
|
|
fprintf( m_fp, ")\n" );
|
2012-05-04 17:44:42 +00:00
|
|
|
|
|
|
|
fclose( m_fp );
|
|
|
|
m_fp = NULL;
|
2011-04-23 19:01:41 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-04-17 02:58:03 +00:00
|
|
|
|
2013-03-31 13:27:46 +00:00
|
|
|
void GBR_TO_PCB_EXPORTER::export_non_copper_item( GERBER_DRAW_ITEM* aGbrItem, LAYER_NUM aLayer )
|
2011-04-23 19:01:41 +00:00
|
|
|
{
|
2014-07-14 18:59:41 +00:00
|
|
|
bool isArc = false;
|
2011-04-23 19:01:41 +00:00
|
|
|
|
2014-07-14 18:59:41 +00:00
|
|
|
double angle = 0;
|
|
|
|
wxPoint seg_start = aGbrItem->m_Start;
|
|
|
|
wxPoint seg_end = aGbrItem->m_End;
|
2011-04-23 19:01:41 +00:00
|
|
|
|
2018-03-31 16:43:12 +00:00
|
|
|
if( aGbrItem->m_Shape == GBR_POLYGON )
|
|
|
|
{
|
|
|
|
writePcbPolygonItem( aGbrItem, aLayer );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-04-23 19:01:41 +00:00
|
|
|
if( aGbrItem->m_Shape == GBR_ARC )
|
|
|
|
{
|
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 ) );
|
2011-04-23 19:01:41 +00:00
|
|
|
|
2014-07-14 18:59:41 +00:00
|
|
|
isArc = true;
|
|
|
|
angle = RAD2DEG(b - a);
|
2012-05-04 17:44:42 +00:00
|
|
|
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;
|
2007-08-23 04:28:46 +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
|
|
|
writePcbLineItem( isArc, seg_start, seg_end, aGbrItem->m_Size.x, aLayer, angle );
|
2011-04-23 19:01:41 +00:00
|
|
|
}
|
2008-11-08 06:44:07 +00:00
|
|
|
|
2011-11-08 16:37:25 +00:00
|
|
|
|
2013-03-31 13:27:46 +00:00
|
|
|
void GBR_TO_PCB_EXPORTER::export_copper_item( GERBER_DRAW_ITEM* aGbrItem, LAYER_NUM 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:
|
|
|
|
// replace spots with vias when possible
|
|
|
|
export_flashed_copper_item( aGbrItem );
|
|
|
|
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
|
|
|
|
// is exprimental and only for tests.
|
|
|
|
#if 1
|
2018-03-31 16:43:12 +00:00
|
|
|
writePcbPolygonItem( aGbrItem, 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
|
|
|
|
2013-03-31 13:27:46 +00:00
|
|
|
void GBR_TO_PCB_EXPORTER::export_segline_copper_item( GERBER_DRAW_ITEM* aGbrItem, LAYER_NUM aLayer )
|
2011-04-23 19:01:41 +00:00
|
|
|
{
|
2012-05-04 17:44:42 +00:00
|
|
|
wxPoint seg_start, seg_end;
|
|
|
|
|
|
|
|
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 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GBR_TO_PCB_EXPORTER::writeCopperLineItem( wxPoint& aStart, wxPoint& aEnd,
|
|
|
|
int aWidth, LAYER_NUM aLayer )
|
|
|
|
{
|
|
|
|
fprintf( m_fp, "(segment (start %s %s) (end %s %s) (width %s) (layer %s) (net 0))\n",
|
2018-12-21 12:48:00 +00:00
|
|
|
Double2Str( MapToPcbUnits(aStart.x) ).c_str(),
|
|
|
|
Double2Str( MapToPcbUnits(aStart.y) ).c_str(),
|
|
|
|
Double2Str( MapToPcbUnits(aEnd.x) ).c_str(),
|
|
|
|
Double2Str( MapToPcbUnits(aEnd.y) ).c_str(),
|
|
|
|
Double2Str( MapToPcbUnits( aWidth ) ).c_str(),
|
2014-07-14 18:59:41 +00:00
|
|
|
TO_UTF8( GetPCBDefaultLayerName( aLayer ) ) );
|
2011-04-23 19:01:41 +00:00
|
|
|
}
|
|
|
|
|
2011-11-08 16:37:25 +00:00
|
|
|
|
2013-03-31 13:27:46 +00:00
|
|
|
void GBR_TO_PCB_EXPORTER::export_segarc_copper_item( GERBER_DRAW_ITEM* aGbrItem, LAYER_NUM 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 ) );
|
|
|
|
|
|
|
|
wxPoint start = aGbrItem->m_Start;
|
|
|
|
wxPoint 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
|
|
|
|
2011-04-23 19:01:41 +00:00
|
|
|
wxPoint curr_start = start;
|
2012-05-04 17:44:42 +00:00
|
|
|
wxPoint seg_start, seg_end;
|
|
|
|
|
|
|
|
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;
|
2011-04-23 19:01:41 +00:00
|
|
|
wxPoint curr_end = start;
|
2013-05-01 17:32:36 +00:00
|
|
|
RotatePoint( &curr_end, aGbrItem->m_ArcCentre,
|
2013-05-02 18:06:58 +00:00
|
|
|
-RAD2DECIDEG( DELTA_ANGLE * ii ) );
|
2012-05-04 17:44:42 +00:00
|
|
|
seg_end = curr_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;
|
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;
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* creates a via from a flashed gerber item.
|
|
|
|
* Flashed items are usually pads or vias, so we try to export all of them
|
|
|
|
* using vias
|
|
|
|
*/
|
2012-05-04 17:44:42 +00:00
|
|
|
void GBR_TO_PCB_EXPORTER::export_flashed_copper_item( GERBER_DRAW_ITEM* aGbrItem )
|
2011-04-23 19:01:41 +00:00
|
|
|
{
|
2012-05-04 17:44:42 +00:00
|
|
|
// First, explore already created vias, before creating a new via
|
|
|
|
for( unsigned ii = 0; ii < m_vias_coordinates.size(); ii++ )
|
|
|
|
{
|
|
|
|
if( m_vias_coordinates[ii] == aGbrItem->m_Start ) // Already created
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_vias_coordinates.push_back( aGbrItem->m_Start );
|
2011-04-23 19:01:41 +00:00
|
|
|
|
2014-07-14 18:59:41 +00:00
|
|
|
wxPoint via_pos = aGbrItem->m_Start;
|
|
|
|
int width = (aGbrItem->m_Size.x + aGbrItem->m_Size.y) / 2;
|
2011-09-09 19:30:59 +00:00
|
|
|
// Reverse Y axis:
|
2015-06-26 13:41:56 +00:00
|
|
|
via_pos.y = -via_pos.y;
|
2012-05-04 17:44:42 +00:00
|
|
|
|
2014-07-14 18:59:41 +00:00
|
|
|
// Layers are Front to Back
|
|
|
|
fprintf( m_fp, " (via (at %s %s) (size %s)",
|
2018-12-21 12:48:00 +00:00
|
|
|
Double2Str( MapToPcbUnits(via_pos.x) ).c_str(),
|
|
|
|
Double2Str( MapToPcbUnits(via_pos.y) ).c_str(),
|
|
|
|
Double2Str( MapToPcbUnits( width ) ).c_str() );
|
2012-05-04 17:44:42 +00:00
|
|
|
|
2014-07-14 18:59:41 +00:00
|
|
|
fprintf( m_fp, " (layers %s %s))\n",
|
|
|
|
TO_UTF8( GetPCBDefaultLayerName( F_Cu ) ),
|
|
|
|
TO_UTF8( GetPCBDefaultLayerName( B_Cu ) ) );
|
|
|
|
}
|
2012-05-04 17:44:42 +00:00
|
|
|
|
2014-07-14 18:59:41 +00:00
|
|
|
void GBR_TO_PCB_EXPORTER::writePcbHeader( LAYER_NUM* aLayerLookUpTable )
|
|
|
|
{
|
|
|
|
fprintf( m_fp, "(kicad_pcb (version 4) (host Gerbview \"%s\")\n\n",
|
|
|
|
TO_UTF8( GetBuildVersion() ) );
|
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
|
|
|
|
|
|
|
|
2014-07-14 18:59:41 +00:00
|
|
|
void GBR_TO_PCB_EXPORTER::writePcbLineItem( bool aIsArc, wxPoint& aStart, wxPoint& aEnd,
|
|
|
|
int aWidth, LAYER_NUM aLayer, double aAngle )
|
|
|
|
{
|
|
|
|
if( aIsArc && ( aAngle == 360.0 || aAngle == 0 ) )
|
|
|
|
{
|
|
|
|
fprintf( m_fp, "(gr_circle (center %s %s) (end %s %s)(layer %s) (width %s))\n",
|
2018-12-21 12:48:00 +00:00
|
|
|
Double2Str( MapToPcbUnits(aStart.x) ).c_str(),
|
|
|
|
Double2Str( MapToPcbUnits(aStart.y) ).c_str(),
|
|
|
|
Double2Str( MapToPcbUnits(aEnd.x) ).c_str(),
|
|
|
|
Double2Str( MapToPcbUnits(aEnd.y) ).c_str(),
|
2014-07-14 18:59:41 +00:00
|
|
|
TO_UTF8( GetPCBDefaultLayerName( aLayer ) ),
|
2018-12-21 12:48:00 +00:00
|
|
|
Double2Str( MapToPcbUnits( aWidth ) ).c_str()
|
2014-07-14 18:59:41 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
else if( aIsArc )
|
|
|
|
{
|
|
|
|
fprintf( m_fp, "(gr_arc (start %s %s) (end %s %s) (angle %s)(layer %s) (width %s))\n",
|
2018-12-21 12:48:00 +00:00
|
|
|
Double2Str( MapToPcbUnits(aStart.x) ).c_str(),
|
|
|
|
Double2Str( MapToPcbUnits(aStart.y) ).c_str(),
|
|
|
|
Double2Str( MapToPcbUnits(aEnd.x) ).c_str(),
|
|
|
|
Double2Str( MapToPcbUnits(aEnd.y) ).c_str(),
|
2014-07-14 18:59:41 +00:00
|
|
|
Double2Str( aAngle ).c_str(),
|
|
|
|
TO_UTF8( GetPCBDefaultLayerName( aLayer ) ),
|
2018-12-21 12:48:00 +00:00
|
|
|
Double2Str( MapToPcbUnits( aWidth ) ).c_str()
|
2014-07-14 18:59:41 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf( m_fp, "(gr_line (start %s %s) (end %s %s)(layer %s) (width %s))\n",
|
2018-12-21 12:48:00 +00:00
|
|
|
Double2Str( MapToPcbUnits(aStart.x) ).c_str(),
|
|
|
|
Double2Str( MapToPcbUnits(aStart.y) ).c_str(),
|
|
|
|
Double2Str( MapToPcbUnits(aEnd.x) ).c_str(),
|
|
|
|
Double2Str( MapToPcbUnits(aEnd.y) ).c_str(),
|
2014-07-14 18:59:41 +00:00
|
|
|
TO_UTF8( GetPCBDefaultLayerName( aLayer ) ),
|
2018-12-21 12:48:00 +00:00
|
|
|
Double2Str( MapToPcbUnits( aWidth ) ).c_str()
|
2014-07-14 18:59:41 +00:00
|
|
|
);
|
|
|
|
}
|
2007-05-28 18:09:49 +00:00
|
|
|
}
|
2018-03-31 16:43:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
void GBR_TO_PCB_EXPORTER::writePcbPolygonItem( GERBER_DRAW_ITEM* aGbrItem, LAYER_NUM aLayer )
|
|
|
|
{
|
|
|
|
SHAPE_POLY_SET polys = aGbrItem->m_Polygon;
|
2019-12-15 10:50:19 +00:00
|
|
|
|
|
|
|
// Cleanup the polygon
|
|
|
|
polys.Simplify( SHAPE_POLY_SET::PM_STRICTLY_SIMPLE );
|
|
|
|
|
|
|
|
// Ensure the polygon is valid:
|
|
|
|
if( polys.OutlineCount() == 0 )
|
|
|
|
return;
|
|
|
|
|
|
|
|
polys.Fracture( SHAPE_POLY_SET::PM_STRICTLY_SIMPLE );
|
|
|
|
|
2018-03-31 16:43:12 +00:00
|
|
|
SHAPE_LINE_CHAIN& poly = polys.Outline( 0 );
|
|
|
|
|
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" );
|
|
|
|
}
|
|
|
|
|
2019-03-23 18:26:44 +00:00
|
|
|
fprintf( m_fp, " (xy %s %s)", Double2Str( MapToPcbUnits( poly.CPoint( ii ).x ) ).c_str(),
|
|
|
|
Double2Str( MapToPcbUnits( -poly.CPoint( ii ).y ) ).c_str() );
|
2018-03-31 16:43:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fprintf( m_fp, ")" );
|
|
|
|
|
|
|
|
if( jj != MAX_COORD_CNT )
|
|
|
|
fprintf( m_fp, "\n" );
|
|
|
|
|
|
|
|
fprintf( m_fp, "(layer %s) (width 0) )\n",
|
|
|
|
TO_UTF8( GetPCBDefaultLayerName( aLayer ) ) );
|
|
|
|
}
|
2019-12-15 10:50:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
void GBR_TO_PCB_EXPORTER::writePcbZoneItem( GERBER_DRAW_ITEM* aGbrItem, LAYER_NUM aLayer )
|
|
|
|
{
|
|
|
|
SHAPE_POLY_SET polys = aGbrItem->m_Polygon;
|
|
|
|
polys.Simplify( SHAPE_POLY_SET::PM_STRICTLY_SIMPLE );
|
|
|
|
|
|
|
|
if( polys.OutlineCount() == 0 )
|
|
|
|
return;
|
|
|
|
|
|
|
|
fprintf( m_fp, "(zone (net 0) (net_name \"\") (layer %s) (tstamp 0000000) (hatch edge 0.508)\n",
|
|
|
|
TO_UTF8( GetPCBDefaultLayerName( aLayer ) ) );
|
|
|
|
|
|
|
|
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 " );
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf( m_fp, " (xy %s %s)", Double2Str( MapToPcbUnits( poly.CPoint( ii ).x ) ).c_str(),
|
|
|
|
Double2Str( MapToPcbUnits( -poly.CPoint( ii ).y ) ).c_str() );
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf( m_fp, ")\n" );
|
|
|
|
|
|
|
|
fprintf( m_fp, " )\n)\n" );
|
|
|
|
}
|