kicad/gerbview/export_to_pcbnew.cpp

353 lines
9.9 KiB
C++
Raw Normal View History

/**
* @file export_to_pcbnew.cpp
* @brief Export the layers to Pcbnew.
2007-08-23 04:28:46 +00:00
*/
#include <fctsys.h>
#include <common.h>
#include <class_drawpanel.h>
#include <confirm.h>
#include <macros.h>
#include <kicad_string.h>
#include <gestfich.h>
#include <trigo.h>
#include <../pcbnew/class_track.h>
#include <../pcbnew/class_drawsegment.h>
2012-04-17 02:58:03 +00:00
#include <io_mgr.h>
#include <gerbview.h>
#include <class_board_design_settings.h>
#include <class_gerber_draw_item.h>
#include <select_layers_to_pcb.h>
#include <build_version.h>
#include <wildcards_and_files_ext.h>
/* A helper class to export a Gerber set of files to Pcbnew
*/
class GBR_TO_PCB_EXPORTER
{
public:
2012-04-17 02:58:03 +00:00
GBR_TO_PCB_EXPORTER( GERBVIEW_FRAME* aFrame, const wxString& aFileName );
~GBR_TO_PCB_EXPORTER();
2012-04-17 02:58:03 +00:00
/**
* Function ExportPcb
* saves a board from a gerber load.
*/
bool ExportPcb( int* LayerLookUpTable );
BOARD* GetBoard() { return m_pcb; }
private:
void export_non_copper_item( GERBER_DRAW_ITEM* aGbrItem, int aLayer );
void export_copper_item( GERBER_DRAW_ITEM* aGbrItem, int aLayer );
void export_flashed_copper_item( GERBER_DRAW_ITEM* aGbrItem, int aLayer );
void export_segline_copper_item( GERBER_DRAW_ITEM* aGbrItem, int aLayer );
void export_segarc_copper_item( GERBER_DRAW_ITEM* aGbrItem, int aLayer );
void cleanBoard();
2012-04-17 02:58:03 +00:00
GERBVIEW_FRAME* m_gerbview_frame; // the maint gerber frame
wxString m_file_name; // BOARD file to write to
BOARD* m_pcb; // the board to populate and export
};
2012-04-17 02:58:03 +00:00
GBR_TO_PCB_EXPORTER::GBR_TO_PCB_EXPORTER( GERBVIEW_FRAME* aFrame, const wxString& aFileName )
{
m_gerbview_frame = aFrame;
2012-04-17 02:58:03 +00:00
m_file_name = aFileName;
++PCBNew * Removed Pcb_Frame argument from BOARD() constructor, since it precludes having a BOARD being edited by more than one editor, it was a bad design. And this meant removing m_PcbFrame from BOARD. * removed BOARD::SetWindowFrame(), and BOARD::m_PcbFrame * Removed the global BOARD_DESIGN_SETTINGS which was in class_board.cpp * added BOARD_DESIGN_SETTINGS to the BOARD class, a full instance * a couple dialogs now only change BOARD_DESIGN_SETTINGS when OK is pressed, such as dialog_mask_clearance, dialog_drc, etc. * Removed common/pcbcommon.cpp's int g_CurrentVersionPCB = 1 and replaced it with build_version.h's #define BOARD_FILE_VERSION, although there may be a better place for this constant. * Made the public functions in PARAM_CFG_ARRAY be type const. void SaveParam(..) const and void ReadParam(..) const * PARAM_CFG_BASE now has virtual destructor since we have various way of destroying the derived class and boost::ptr_vector must be told about this. * Pass const PARAM_CFG_ARRAY& instead of PARAM_CFG_ARRAY so that we can use an automatic PARAM_CFG_ARRAY which is on the stack.\ * PCB_EDIT_FRAME::GetProjectFileParameters() may no longer cache the array, since it has to access the current BOARD and the BOARD can change. Remember BOARD_DESIGN_SETTINGS are now in the BOARD. * Made the m_BoundingBox member private, this was a brutally hard task, and indicative of the lack of commitment to accessors and object oriented design on the part of KiCad developers. We must do better. Added BOARD::GetBoundingBox, SetBoundingBox(), ComputeBoundingBox(). * Added PCB_BASE_FRAME::GetBoardBoundingBox() which calls BOARD::ComputeBoundingBox()
2011-12-05 06:15:33 +00:00
m_pcb = new BOARD();
}
GBR_TO_PCB_EXPORTER::~GBR_TO_PCB_EXPORTER()
{
delete m_pcb;
}
/* Export data in Pcbnew format
* remember Pcbnew uses a Y reversed axis, so we must negate all Y coordinates
2007-08-23 04:28:46 +00:00
*/
void GERBVIEW_FRAME::ExportDataInPcbnewFormat( wxCommandEvent& event )
{
int layercount = 0;
2007-10-07 03:08:24 +00:00
// Count the Gerber layers which are actually currently used
for( int ii = 0; ii < 32; ii++ )
{
2008-11-08 06:44:07 +00:00
if( g_GERBER_List[ii] != NULL )
layercount++;
2007-10-07 03:08:24 +00:00
}
if( layercount == 0 )
2007-10-07 03:08:24 +00:00
{
DisplayInfoMessage( this,
_( "None of the Gerber layers contain any data" ) );
2007-10-07 03:08:24 +00:00
return;
}
wxString fileName;
wxString path = wxGetCwd();;
wxFileDialog filedlg( this, _( "Board file name:" ),
path, fileName, LegacyPcbFileWildcard,
wxFD_OPEN );
if( filedlg.ShowModal() == wxID_CANCEL )
2007-08-23 04:28:46 +00:00
return;
fileName = filedlg.GetPath();
/* Install a dialog frame to choose the mapping
* between gerber layers and Pcbnew layers
*/
LAYERS_MAP_DIALOG* layerdlg = new LAYERS_MAP_DIALOG( this );
int ok = layerdlg->ShowModal();
layerdlg->Destroy();
if( ok != wxID_OK )
return;
2012-04-17 02:58:03 +00:00
if( wxFileExists( fileName ) )
2007-08-23 04:28:46 +00:00
{
if( !IsOK( this, _( "Ok to change the existing file ?" ) ) )
2007-08-23 04:28:46 +00:00
return;
}
2012-04-17 02:58:03 +00:00
GBR_TO_PCB_EXPORTER gbr_exporter( this, fileName );
gbr_exporter.ExportPcb( layerdlg->GetLayersLookUpTable() );
}
void GBR_TO_PCB_EXPORTER::cleanBoard()
{
// delete redundant vias
for( TRACK * track = m_pcb->m_Track; track; track = track->Next() )
{
if( track->m_Shape != VIA_THROUGH )
continue;
// Search and delete others vias
TRACK* next_track;
TRACK* alt_track = track->Next();
for( ; alt_track; alt_track = next_track )
{
next_track = alt_track->Next();
if( alt_track->m_Shape != VIA_THROUGH )
continue;
if( alt_track->m_Start != track->m_Start )
continue;
2007-08-23 04:28:46 +00:00
// delete track
alt_track->UnLink();
delete alt_track;
}
}
}
bool GBR_TO_PCB_EXPORTER::ExportPcb( int* LayerLookUpTable )
{
BOARD* gerberPcb = m_gerbview_frame->GetBoard();
2007-08-23 04:28:46 +00:00
2007-10-30 21:30:58 +00:00
// create an image of gerber data
2010-09-28 14:42:05 +00:00
BOARD_ITEM* item = gerberPcb->m_Drawings;
2010-09-28 14:42:05 +00:00
for( ; item; item = item->Next() )
2007-08-23 04:28:46 +00:00
{
2010-09-28 14:42:05 +00:00
GERBER_DRAW_ITEM* gerb_item = (GERBER_DRAW_ITEM*) item;
2010-09-29 09:25:48 +00:00
int layer = gerb_item->GetLayer();
2007-08-23 04:28:46 +00:00
int pcb_layer_number = LayerLookUpTable[layer];
if( pcb_layer_number < 0 || pcb_layer_number > LAST_NO_COPPER_LAYER )
2007-08-23 04:28:46 +00:00
continue;
2008-11-08 06:44:07 +00:00
if( pcb_layer_number > LAST_COPPER_LAYER )
export_non_copper_item( gerb_item, pcb_layer_number );
else
export_copper_item( gerb_item, pcb_layer_number );
2007-08-23 04:28:46 +00:00
}
cleanBoard();
m_pcb->SetCopperLayerCount( LayerLookUpTable[32] );
2008-11-08 06:44:07 +00:00
2012-04-17 02:58:03 +00:00
try
{
wxFileName pcbFileName( m_file_name );
PROPERTIES props;
wxString header = wxString::Format(
wxT( "PCBNEW-BOARD Version %d date %s\n\n# Created by GerbView%s\n\n" ),
LEGACY_BOARD_FILE_VERSION, DateAndTime().GetData(),
GetBuildVersion().GetData() );
props["header"] = header;
2012-04-17 02:58:03 +00:00
PLUGIN::RELEASER pi( IO_MGR::PluginFind( IO_MGR::LEGACY ) );
pi->Save( m_file_name, m_pcb, &props );
2012-04-17 02:58:03 +00:00
}
catch( IO_ERROR ioe )
{
DisplayError( m_gerbview_frame, ioe.errorText );
return false;
}
return true;
}
2012-04-17 02:58:03 +00:00
void GBR_TO_PCB_EXPORTER::export_non_copper_item( GERBER_DRAW_ITEM* aGbrItem, int aLayer )
{
DRAWSEGMENT* drawitem = new DRAWSEGMENT( m_pcb, PCB_LINE_T );
drawitem->SetLayer( aLayer );
2011-12-14 04:29:25 +00:00
drawitem->SetStart( aGbrItem->m_Start );
drawitem->SetEnd( aGbrItem->m_End );
drawitem->SetWidth( aGbrItem->m_Size.x );
if( aGbrItem->m_Shape == GBR_ARC )
{
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-12-14 04:29:25 +00:00
drawitem->SetShape( S_ARC );
// Dick Hollenbeck's KiROUND R&D // This provides better project control over rounding to int from double // than wxRound() did. This scheme provides better logging in Debug builds // and it provides for compile time calculation of constants. #include <stdio.h> #include <assert.h> #include <limits.h> //-----<KiROUND KIT>------------------------------------------------------------ /** * KiROUND * rounds a floating point number to an int using * "round halfway cases away from zero". * In Debug build an assert fires if will not fit into an int. */ #if defined( DEBUG ) // DEBUG: a macro to capture line and file, then calls this inline static inline int KiRound( double v, int line, const char* filename ) { v = v < 0 ? v - 0.5 : v + 0.5; if( v > INT_MAX + 0.5 ) { printf( "%s: in file %s on line %d, val: %.16g too ' > 0 ' for int\n", __FUNCTION__, filename, line, v ); } else if( v < INT_MIN - 0.5 ) { printf( "%s: in file %s on line %d, val: %.16g too ' < 0 ' for int\n", __FUNCTION__, filename, line, v ); } return int( v ); } #define KiROUND( v ) KiRound( v, __LINE__, __FILE__ ) #else // RELEASE: a macro so compile can pre-compute constants. #define KiROUND( v ) int( (v) < 0 ? (v) - 0.5 : (v) + 0.5 ) #endif //-----</KiROUND KIT>----------------------------------------------------------- // Only a macro is compile time calculated, an inline function causes a static constructor // in a situation like this. // Therefore the Release build is best done with a MACRO not an inline function. int Computed = KiROUND( 14.3 * 8 ); int main( int argc, char** argv ) { for( double d = double(INT_MAX)-1; d < double(INT_MAX)+8; d += 2.0 ) { int i = KiROUND( d ); printf( "t: %d %.16g\n", i, d ); } return 0; }
2012-04-19 06:55:45 +00:00
drawitem->SetAngle( KiROUND( (a - b) / M_PI * 1800.0 ) );
2011-12-14 04:29:25 +00:00
drawitem->SetStart( aGbrItem->m_ArcCentre );
2011-12-14 04:29:25 +00:00
if( drawitem->GetAngle() < 0 )
{
2011-12-14 04:29:25 +00:00
drawitem->SetAngle( -drawitem->GetAngle() );
drawitem->SetEnd( aGbrItem->m_Start );
2007-08-23 04:28:46 +00:00
}
}
// Reverse Y axis:
2011-12-14 04:29:25 +00:00
drawitem->SetStartY( -drawitem->GetStart().y );
drawitem->SetEndY( -drawitem->GetEnd().y );
m_pcb->Add( drawitem );
}
2008-11-08 06:44:07 +00:00
void GBR_TO_PCB_EXPORTER::export_copper_item( GERBER_DRAW_ITEM* aGbrItem, int aLayer )
{
switch( aGbrItem->m_Shape )
{
case GBR_SPOT_CIRCLE:
case GBR_SPOT_RECT:
case GBR_SPOT_OVAL:
// replace spots with vias when possible
export_flashed_copper_item( aGbrItem, aLayer );
break;
2007-08-23 04:28:46 +00:00
case GBR_ARC:
export_segarc_copper_item( aGbrItem, aLayer );
break;
2007-08-23 04:28:46 +00:00
default:
export_segline_copper_item( aGbrItem, aLayer );
break;
}
}
2007-08-23 04:28:46 +00:00
void GBR_TO_PCB_EXPORTER::export_segline_copper_item( GERBER_DRAW_ITEM* aGbrItem, int aLayer )
{
TRACK * newtrack = new TRACK( m_pcb );
newtrack->SetLayer( aLayer );
newtrack->m_Start = aGbrItem->m_Start;
newtrack->m_End = aGbrItem->m_End;
newtrack->m_Width = aGbrItem->m_Size.x;
// Reverse Y axis:
NEGATE( newtrack->m_Start.y );
NEGATE( newtrack->m_End.y );
m_pcb->Add( newtrack );
}
void GBR_TO_PCB_EXPORTER::export_segarc_copper_item( GERBER_DRAW_ITEM* aGbrItem, int aLayer )
{
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;
/* Because Pcbnew does not know arcs in tracks,
* approximate arc by segments (SEG_COUNT__CIRCLE segment per 360 deg)
* The arc is drawn in an anticlockwise direction from the start point to the end point.
*/
#define SEG_COUNT_CIRCLE 16
#define DELTA_ANGLE 2*M_PI/SEG_COUNT_CIRCLE
// calculate the number of segments from a to b.
// we want CNT_PER_360 segments fo a circle
if( a > b )
b += 2*M_PI;
wxPoint curr_start = start;
int ii = 1;
for( double rot = a; rot < (b - DELTA_ANGLE); rot += DELTA_ANGLE, ii++ )
{
TRACK * newtrack = new TRACK( m_pcb );
newtrack->SetLayer( aLayer );
newtrack->m_Start = curr_start;
wxPoint curr_end = start;
RotatePoint( &curr_end, aGbrItem->m_ArcCentre, -(int)(DELTA_ANGLE * ii * 1800 / M_PI) );
newtrack->m_End = curr_end;
newtrack->m_Width = aGbrItem->m_Size.x;
// Reverse Y axis:
NEGATE( newtrack->m_Start.y );
NEGATE( newtrack->m_End.y );
m_pcb->Add( newtrack );
curr_start = curr_end;
}
if( end != curr_start )
{
TRACK * newtrack = new TRACK( m_pcb );
newtrack->SetLayer( aLayer );
newtrack->m_Start = curr_start;
newtrack->m_End = end;
newtrack->m_Width = aGbrItem->m_Size.x;
// Reverse Y axis:
NEGATE( newtrack->m_Start.y );
NEGATE( newtrack->m_End.y );
m_pcb->Add( newtrack );
}
}
/*
* 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
*/
void GBR_TO_PCB_EXPORTER::export_flashed_copper_item( GERBER_DRAW_ITEM* aGbrItem, int aLayer )
{
SEGVIA * newtrack = new SEGVIA( m_pcb );
newtrack->m_Shape = VIA_THROUGH;
newtrack->SetLayer( 0x0F ); // Layers are 0 to 15 (Cu/Cmp)
newtrack->SetDrillDefault();
newtrack->m_Start = newtrack->m_End = aGbrItem->m_Start;
newtrack->m_Width = (aGbrItem->m_Size.x + aGbrItem->m_Size.y) / 2;
// Reverse Y axis:
NEGATE( newtrack->m_Start.y );
NEGATE( newtrack->m_End.y );
m_pcb->Add( newtrack );
}