kicad/gerbview/export_to_pcbnew.cpp

245 lines
7.5 KiB
C++
Raw Normal View History

/* export_to_pcbnew.cpp */
2007-08-23 04:28:46 +00:00
/*
2007-09-01 12:00:30 +00:00
* Export the layers to pcbnew
2007-08-23 04:28:46 +00:00
*/
#include "fctsys.h"
#include "common.h"
#include "gerbview.h"
#include "protos.h"
/* Routines Locales : */
2007-08-23 04:28:46 +00:00
static int SavePcbFormatAscii( WinEDA_GerberFrame* frame,
FILE* File, int* LayerLookUpTable );
/* Variables Locales */
/************************************************************************/
2007-08-23 04:28:46 +00:00
void WinEDA_GerberFrame::ExportDataInPcbnewFormat( wxCommandEvent& event )
/************************************************************************/
2007-08-23 04:28:46 +00:00
/* Export data in pcbnew format
2007-08-23 04:28:46 +00:00
*/
{
2007-10-07 03:08:24 +00:00
int ii = 0;
bool no_used_layers = true; // Changed to false if any used layer found
// Check whether any of the Gerber layers are actually currently used
while( no_used_layers && ii < 32 )
{
if( g_GERBER_Descr_List[ii] != NULL )
no_used_layers = false;
2007-10-07 03:08:24 +00:00
ii++;
}
if( no_used_layers )
{
DisplayInfo( this, _( "None of the Gerber layers contain any data" ) );
return;
}
2007-08-23 04:28:46 +00:00
wxString FullFileName, msg;
wxString PcbExt( wxT( ".brd" ) );
FILE* dest;
msg = wxT( "*" ) + PcbExt;
FullFileName = EDA_FileSelector( _( "Board file name:" ),
wxEmptyString, /* Chemin par defaut */
wxEmptyString, /* nom fichier par defaut */
PcbExt, /* extension par defaut */
msg, /* Masque d'affichage */
this,
wxFD_SAVE,
FALSE
);
if( FullFileName == wxEmptyString )
return;
int* LayerLookUpTable;
if( ( LayerLookUpTable = InstallDialogLayerPairChoice( this ) ) != NULL )
{
if( wxFileExists( FullFileName ) )
{
if( !IsOK( this, _( "Ok to change the existing file ?" ) ) )
return;
}
dest = wxFopen( FullFileName, wxT( "wt" ) );
if( dest == 0 )
{
msg = _( "Unable to create " ) + FullFileName;
DisplayError( this, msg );
return;
}
GetScreen()->m_FileName = FullFileName;
SavePcbFormatAscii( this, dest, LayerLookUpTable );
fclose( dest );
}
}
/***************************************************************/
2007-08-23 04:28:46 +00:00
static int WriteSetup( FILE* File, BOARD* Pcb )
/***************************************************************/
{
2007-08-23 04:28:46 +00:00
char text[1024];
fprintf( File, "$SETUP\n" );
sprintf( text, "InternalUnit %f INCH\n", 1.0 / PCB_INTERNAL_UNIT );
fprintf( File, text );
2007-08-23 04:28:46 +00:00
Pcb->m_BoardSettings->m_CopperLayerCount = g_DesignSettings.m_CopperLayerCount;
fprintf( File, "Layers %d\n", g_DesignSettings.m_CopperLayerCount );
2007-08-23 04:28:46 +00:00
fprintf( File, "$EndSETUP\n\n" );
return 1;
}
2007-08-23 04:28:46 +00:00
/******************************************************/
2007-08-23 04:28:46 +00:00
static bool WriteGeneralDescrPcb( BOARD* Pcb, FILE* File )
/******************************************************/
{
2007-08-23 04:28:46 +00:00
int NbLayers;
/* Print the copper layer count */
2007-08-23 04:28:46 +00:00
NbLayers = Pcb->m_BoardSettings->m_CopperLayerCount;
fprintf( File, "$GENERAL\n" );
fprintf( File, "LayerCount %d\n", NbLayers );
/* Compute and print the board bounding box */
2007-08-23 04:28:46 +00:00
Pcb->ComputeBoundaryBox();
fprintf( File, "Di %d %d %d %d\n",
Pcb->m_BoundaryBox.GetX(), Pcb->m_BoundaryBox.GetY(),
Pcb->m_BoundaryBox.GetRight(),
Pcb->m_BoundaryBox.GetBottom() );
fprintf( File, "$EndGENERAL\n\n" );
return TRUE;
}
/*******************************************************************/
2007-10-30 21:30:58 +00:00
static int SavePcbFormatAscii( WinEDA_GerberFrame* frame, FILE* aFile,
2007-08-23 04:28:46 +00:00
int* LayerLookUpTable )
/*******************************************************************/
2007-08-23 04:28:46 +00:00
/* Routine to save the board
* @param frame = pointer to the main frame
* @param File = FILE * pointer to an already opened file
* @param LayerLookUpTable = look up table: pcbnew layer for each gerber layer
* @return 1 if OK, 0 if fail
2007-08-23 04:28:46 +00:00
*/
{
2007-10-30 21:30:58 +00:00
char line[256];
2007-09-01 12:00:30 +00:00
TRACK* track;
2007-10-30 21:30:58 +00:00
BOARD* gerberPcb = frame->m_Pcb;
BOARD* pcb;
2007-08-23 04:28:46 +00:00
wxBeginBusyCursor();
2007-10-30 21:30:58 +00:00
// create an image of gerber data
pcb = new BOARD( NULL, frame );
2007-08-23 04:28:46 +00:00
2007-10-30 21:30:58 +00:00
for( track = gerberPcb->m_Track; track; track = track->Next() )
2007-08-23 04:28:46 +00:00
{
int layer = track->GetLayer();
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;
2007-09-01 12:00:30 +00:00
if( pcb_layer_number > LAST_COPPER_LAYER )
2007-08-23 04:28:46 +00:00
{
2007-10-30 21:30:58 +00:00
DRAWSEGMENT* drawitem = new DRAWSEGMENT( pcb, TYPEDRAWSEGMENT );
2007-08-23 04:28:46 +00:00
drawitem->SetLayer( pcb_layer_number );
drawitem->m_Start = track->m_Start;
drawitem->m_End = track->m_End;
drawitem->m_Width = track->m_Width;
2007-10-30 21:30:58 +00:00
drawitem->Pnext = pcb->m_Drawings;
pcb->m_Drawings = drawitem;
2007-08-23 04:28:46 +00:00
}
else
{
2007-09-01 12:00:30 +00:00
TRACK* newtrack;
// replace spots with vias when possible
if( track->m_Shape == S_SPOT_CIRCLE
2007-10-30 21:30:58 +00:00
|| track->m_Shape == S_SPOT_RECT
|| track->m_Shape == S_SPOT_OVALE )
2007-09-01 12:00:30 +00:00
{
newtrack = new SEGVIA( (const SEGVIA&) *track );
// A spot is found, and can be a via: change it to via, and delete other
// spots at same location
newtrack->m_Shape = VIA_THROUGH;
2007-09-01 12:00:30 +00:00
newtrack->SetLayer( 0x0F ); // Layers are 0 to 15 (Cu/Cmp)
newtrack->SetDrillDefault();
2007-09-01 12:00:30 +00:00
// Compute the via position from track position ( Via position is the
// position of the middle of the track segment )
newtrack->m_Start.x = (newtrack->m_Start.x + newtrack->m_End.x) / 2;
newtrack->m_Start.y = (newtrack->m_Start.y + newtrack->m_End.y) / 2;
newtrack->m_End = newtrack->m_Start;
}
else // a true TRACK
{
2007-09-15 04:25:54 +00:00
newtrack = track->Copy();
2007-09-01 12:00:30 +00:00
newtrack->SetLayer( pcb_layer_number );
}
2007-10-30 21:30:58 +00:00
newtrack->Insert( pcb, NULL );
2007-08-23 04:28:46 +00:00
}
}
2007-09-01 12:00:30 +00:00
// delete redundant vias
2007-10-30 21:30:58 +00:00
for( track = pcb->m_Track; track; track = track->Next() )
2007-08-23 04:28:46 +00:00
{
if( track->m_Shape != VIA_THROUGH )
2007-08-23 04:28:46 +00:00
continue;
2007-09-01 12:00:30 +00:00
// Search and delete others vias
2007-10-30 21:30:58 +00:00
TRACK* next_track;
2007-08-23 04:28:46 +00:00
TRACK* alt_track = track->Next();
2007-10-30 21:30:58 +00:00
for( ; alt_track; alt_track = next_track )
2007-08-23 04:28:46 +00:00
{
2007-09-01 12:00:30 +00:00
next_track = alt_track->Next();
if( alt_track->m_Shape != VIA_THROUGH )
2007-08-23 04:28:46 +00:00
continue;
2007-09-01 12:00:30 +00:00
2007-08-23 04:28:46 +00:00
if( alt_track->m_Start != track->m_Start )
continue;
2007-09-01 12:00:30 +00:00
// delete track
2007-08-23 04:28:46 +00:00
alt_track->UnLink();
delete alt_track;
}
}
// Switch the locale to standard C (needed to print floating point numbers like 1.3)
setlocale( LC_NUMERIC, "C" );
2007-09-01 12:00:30 +00:00
// write the PCB heading
2007-10-30 21:30:58 +00:00
fprintf( aFile, "PCBNEW-BOARD Version %d date %s\n\n", g_CurrentVersionPCB,
DateAndTime( line ) );
WriteGeneralDescrPcb( pcb, aFile );
WriteSetup( aFile, pcb );
2007-08-23 04:28:46 +00:00
2007-09-01 12:00:30 +00:00
// write the useful part of the pcb
2007-10-30 21:30:58 +00:00
pcb->Save( aFile );
2007-08-23 04:28:46 +00:00
2007-10-30 21:30:58 +00:00
// the destructor should destroy all owned sub-objects
delete pcb;
2007-08-23 04:28:46 +00:00
setlocale( LC_NUMERIC, "" ); // revert to the current locale
2007-08-23 04:28:46 +00:00
wxEndBusyCursor();
return 1;
}