kicad/pcbnew/export_gencad.cpp

906 lines
29 KiB
C++
Raw Normal View History

/************************************************/
/* export_gencad.cpp - export GenCAD 1.4 format */
/************************************************/
#include "fctsys.h"
#include "common.h"
#include "class_drawpanel.h"
#include "confirm.h"
#include "gestfich.h"
#include "appl_wxstruct.h"
#include "pcbnew.h"
2009-07-30 11:04:07 +00:00
#include "wxPcbStruct.h"
#include "trigo.h"
#include "build_version.h"
2007-08-23 04:28:46 +00:00
bool CreateHeaderInfoData( FILE* file, WinEDA_PcbFrame* frame );
2009-05-23 15:53:47 +00:00
static void CreateTracksInfoData( FILE* file, BOARD* pcb );
2007-08-23 04:28:46 +00:00
static void CreateBoardSection( FILE* file, BOARD* pcb );
static void CreateComponentsSection( FILE* file, BOARD* pcb );
static void CreateDevicesSection( FILE* file, BOARD* pcb );
static void CreateRoutesSection( FILE* file, BOARD* pcb );
static void CreateSignalsSection( FILE* file, BOARD* pcb );
static void CreateShapesSection( FILE* file, BOARD* pcb );
static void CreatePadsShapesSection( FILE* file, BOARD* pcb );
static void CreatePadsStacksSection( FILE* file, BOARD* pcb );
2009-11-02 20:36:20 +00:00
static void FootprintWriteShape( FILE* File, MODULE* module );
// layer name for Gencad export
static const wxString GenCAD_Layer_Name[32] =
{
wxT( "BOTTOM" ), wxT( "INNER1" ), wxT( "INNER2" ),
wxT( "INNER3" ), wxT( "INNER4" ), wxT( "INNER5" ),
wxT( "INNER6" ), wxT( "INNER7" ), wxT( "INNER8" ),
wxT( "INNER9" ), wxT( "INNER10" ), wxT( "INNER11" ),
wxT( "INNER12" ), wxT( "INNER13" ), wxT( "INNER14" ),
wxT( "TOP" ), wxT( "adhecu" ), wxT( "adhecmp" ),
wxT( "SOLDERPASTE_BOTTOM" ), wxT( "SOLDERPASTE_TOP" ),
wxT( "SILKSCREEN_BOTTOM" ), wxT( "SILKSCREEN_TOP" ),
wxT( "SOLDERMASK_BOTTOM" ), wxT( "SOLDERMASK_TOP" ), wxT( "drawings" ),
wxT( "comments" ), wxT( "eco1" ), wxT( "eco2" ),
wxT( "edges" ), wxT( "--" ), wxT( "--" ),
wxT( "--" )
2007-08-23 04:28:46 +00:00
};
2009-11-02 20:36:20 +00:00
int offsetX, offsetY;
D_PAD* PadList;
/* 2 helper functions to calculate coordinates of modules in gencad values (
* GenCAD Y axis from bottom to top)
2009-11-02 20:36:20 +00:00
*/
2007-08-23 04:28:46 +00:00
static int mapXto( int x )
{
2007-08-23 04:28:46 +00:00
return x - offsetX;
}
2007-08-23 04:28:46 +00:00
static int mapYto( int y )
{
2007-08-23 04:28:46 +00:00
return offsetY - y;
}
/*
* Creates an Export file (format GenCAD 1.4) from the current board.
2007-08-23 04:28:46 +00:00
*/
void WinEDA_PcbFrame::ExportToGenCAD( wxCommandEvent& event )
{
wxFileName fn = GetScreen()->GetFileName();
2009-11-02 20:36:20 +00:00
wxString msg, ext, wildcard;
FILE* file;
2007-08-23 04:28:46 +00:00
ext = wxT( "cad" );
wildcard = _( "GenCAD 1.4 board files (.cad)|*.cad" );
fn.SetExt( ext );
2009-05-23 15:53:47 +00:00
wxFileDialog dlg( this, _( "Save GenCAD Board File" ), wxGetCwd(),
fn.GetFullName(), wildcard,
wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
if( dlg.ShowModal() == wxID_CANCEL )
2007-08-23 04:28:46 +00:00
return;
if( ( file = wxFopen( dlg.GetPath(), wxT( "wt" ) ) ) == NULL )
2007-08-23 04:28:46 +00:00
{
msg = _( "Unable to create " ) + dlg.GetPath();
2007-08-23 04:28:46 +00:00
DisplayError( this, msg ); return;
}
2009-11-02 20:36:20 +00:00
/* Update some board data, to ensure a reliable gencad export: */
GetBoard()->ComputeBoundingBox();
2007-08-23 04:28:46 +00:00
offsetX = m_Auxiliary_Axis_Position.x;
offsetY = m_Auxiliary_Axis_Position.y;
2009-12-20 19:48:58 +00:00
Compile_Ratsnest( NULL, TRUE );
2007-08-23 04:28:46 +00:00
/* Temporary modification of footprints that are flipped (i.e. on bottom
* layer) to convert them to non flipped footprints.
2009-11-02 20:36:20 +00:00
* This is necessary to easily export shapes to GenCAD,
* that are given as normal orientation (non flipped, rotation = 0))
* these changes will be undone later
*/
2007-08-23 04:28:46 +00:00
MODULE* module;
for( module = GetBoard()->m_Modules; module != NULL; module = module->Next() )
2007-08-23 04:28:46 +00:00
{
module->flag = 0;
if( module->GetLayer() == LAYER_N_BACK )
2007-08-23 04:28:46 +00:00
{
2009-08-06 07:11:04 +00:00
module->Flip( module->m_Pos );
2007-08-23 04:28:46 +00:00
module->flag = 1;
}
}
2009-11-02 20:36:20 +00:00
// Create file header:
2007-08-23 04:28:46 +00:00
CreateHeaderInfoData( file, this );
CreateBoardSection( file, GetBoard() );
2007-08-23 04:28:46 +00:00
2009-11-02 20:36:20 +00:00
/* Create TRACKS list
* This is the section $TRACK) (track width sizes) */
CreateTracksInfoData( file, GetBoard() );
2007-08-23 04:28:46 +00:00
2009-11-02 20:36:20 +00:00
/* Create the shapes list
* (shapes of pads and footprints */
CreatePadsShapesSection( file, GetBoard() ); /* Must be called
* before
* CreatePadsStacksSection
* and
* CreateShapesSection()
*/
CreatePadsStacksSection( file, GetBoard() );
CreateShapesSection( file, GetBoard() );
2007-08-23 04:28:46 +00:00
CreateDevicesSection( file, GetBoard() );
CreateComponentsSection( file, GetBoard() );
2009-11-02 20:36:20 +00:00
/* Create the list of Nets: */
CreateSignalsSection( file, GetBoard() );
2007-08-23 04:28:46 +00:00
// Creates the Routes section (i.e. the list of board tracks)
CreateRoutesSection( file, GetBoard() );
2007-08-23 04:28:46 +00:00
fclose( file );
2009-11-02 20:36:20 +00:00
/* Undo the footprints modifications (flipped footprints) */
for( module = GetBoard()->m_Modules; module != NULL; module = module->Next() )
2007-08-23 04:28:46 +00:00
{
if( module->flag )
{
2009-08-06 07:11:04 +00:00
module->Flip( module->m_Pos );
2007-08-23 04:28:46 +00:00
module->flag = 0;
}
}
}
2007-08-23 04:28:46 +00:00
static int Pad_list_Sort_by_Shapes( const void* refptr, const void* objptr )
{
2009-11-02 20:36:20 +00:00
const D_PAD* padref = *(D_PAD**) refptr;
const D_PAD* padcmp = *(D_PAD**) objptr;
2008-01-24 21:50:12 +00:00
return D_PAD::Compare( padref, padcmp );
}
2007-08-23 04:28:46 +00:00
2009-11-02 20:36:20 +00:00
/* Creates the pads shapes list ( 1 shape per pad )
* Uses .GetSubRatsnest member of class D_PAD, to handle the shape id (value 1
* ..n) for pads shapes PAD1 to PADn
*
* The PADS section is used to describe the shape of all the pads used on the
* printed circuit board. The PADS section must be included, even if only a
* default pad is described and used for all pads.
* The keywords used in the PADS section are:
* $PADS
* PAD <pad_name> <pad_type> <drill_size>
* LINE <line_ref>
* ARC <arc_ref>
* CIRCLE <circle_ref>
* RECTANGLE <rectangle_ref>
* ATTRIBUTE <attrib_ref>
* $ENDPADS
* $PADS and $ENDPADS mark the PADS section of the GenCAD file. Each pad
* description must start with a PAD keyword.
* The layer in which a pad lies is defined in the SHAPE section of the GenCAD
* specification.
* The pad is always placed on a shape at the pad origin, or in a pad stack at
* the pad stack origin.
2007-08-23 04:28:46 +00:00
*/
void CreatePadsShapesSection( FILE* file, BOARD* pcb )
{
std::vector<D_PAD*> pads;
2009-11-02 20:36:20 +00:00
const char* pad_type;
2007-08-23 04:28:46 +00:00
fputs( "$PADS\n", file );
2009-06-06 18:08:49 +00:00
if( pcb->GetPadsCount() > 0 )
{
2009-11-02 20:36:20 +00:00
pads.insert( pads.end(),
pcb->m_NetInfo->m_PadsFullList.begin(),
pcb->m_NetInfo->m_PadsFullList.end() );
qsort( &pads[0], pcb->GetPadsCount(), sizeof( D_PAD* ),
Pad_list_Sort_by_Shapes );
}
2007-08-23 04:28:46 +00:00
2009-11-02 20:36:20 +00:00
D_PAD* old_pad = NULL;
int pad_name_number = 0;
for( unsigned i = 0; i<pads.size(); ++i )
2007-08-23 04:28:46 +00:00
{
D_PAD* pad = pads[i];
pad->SetSubRatsnest( pad_name_number );
2007-08-23 04:28:46 +00:00
2008-01-24 21:50:12 +00:00
if( old_pad && 0==D_PAD::Compare( old_pad, pad ) )
continue; // already created
2007-08-23 04:28:46 +00:00
old_pad = pad;
pad_name_number++;
pad->SetSubRatsnest( pad_name_number );
2007-08-23 04:28:46 +00:00
fprintf( file, "PAD PAD%d", pad->GetSubRatsnest() );
2007-08-23 04:28:46 +00:00
int dx = pad->m_Size.x / 2;
int dy = pad->m_Size.y / 2;
2007-08-23 04:28:46 +00:00
switch( pad->m_PadShape )
{
default:
2008-01-05 17:30:56 +00:00
case PAD_CIRCLE:
2007-08-23 04:28:46 +00:00
pad_type = "ROUND";
fprintf( file, " %s %d\n", pad_type, pad->m_Drill.x );
fprintf( file, "CIRCLE %d %d %d\n",
2009-11-02 20:36:20 +00:00
pad->m_Offset.x, -pad->m_Offset.y, dx );
2007-08-23 04:28:46 +00:00
break;
2008-01-05 17:30:56 +00:00
case PAD_RECT:
2007-08-23 04:28:46 +00:00
pad_type = "RECTANGULAR";
fprintf( file, " %s %d\n", pad_type, pad->m_Drill.x );
fprintf( file, "RECTANGLE %d %d %d %d\n",
2010-10-26 11:47:16 +00:00
pad->m_Offset.x - dx, -pad->m_Offset.y - dy,
pad->m_Size.x, pad->m_Size.y );
2007-08-23 04:28:46 +00:00
break;
2009-11-02 20:36:20 +00:00
case PAD_OVAL: /* Create outline by 2 lines and 2 arcs */
2007-08-23 04:28:46 +00:00
{
pad_type = "FINGER";
fprintf( file, " %s %d\n", pad_type, pad->m_Drill.x );
int dr = dx - dy;
2009-11-02 20:36:20 +00:00
if( dr >= 0 ) // Horizontal oval
2007-08-23 04:28:46 +00:00
{
int rayon = dy;
fprintf( file, "LINE %d %d %d %d\n",
-dr + pad->m_Offset.x, -pad->m_Offset.y - rayon,
dr + pad->m_Offset.x, -pad->m_Offset.y - rayon );
fprintf( file, "ARC %d %d %d %d %d %d\n",
dr + pad->m_Offset.x, -pad->m_Offset.y - rayon,
dr + pad->m_Offset.x, -pad->m_Offset.y + rayon,
dr + pad->m_Offset.x, -pad->m_Offset.y );
fprintf( file, "LINE %d %d %d %d\n",
dr + pad->m_Offset.x, -pad->m_Offset.y + rayon,
-dr + pad->m_Offset.x, -pad->m_Offset.y + rayon );
fprintf( file, "ARC %d %d %d %d %d %d\n",
-dr + pad->m_Offset.x, -pad->m_Offset.y + rayon,
-dr + pad->m_Offset.x, -pad->m_Offset.y - rayon,
-dr + pad->m_Offset.x, -pad->m_Offset.y );
}
2009-11-02 20:36:20 +00:00
else // Vertical oval
2007-08-23 04:28:46 +00:00
{
dr = -dr;
int rayon = dx;
fprintf( file, "LINE %d %d %d %d\n",
-rayon + pad->m_Offset.x, -pad->m_Offset.y - dr,
-rayon + pad->m_Offset.x, -pad->m_Offset.y + dr );
fprintf( file, "ARC %d %d %d %d %d %d\n",
-rayon + pad->m_Offset.x, -pad->m_Offset.y + dr,
rayon + pad->m_Offset.x, -pad->m_Offset.y + dr,
pad->m_Offset.x, -pad->m_Offset.y + dr );
fprintf( file, "LINE %d %d %d %d\n",
rayon + pad->m_Offset.x, -pad->m_Offset.y + dr,
rayon + pad->m_Offset.x, -pad->m_Offset.y - dr );
fprintf( file, "ARC %d %d %d %d %d %d\n",
rayon + pad->m_Offset.x, -pad->m_Offset.y - dr,
-rayon + pad->m_Offset.x, -pad->m_Offset.y - dr,
pad->m_Offset.x, -pad->m_Offset.y - dr );
}
break;
}
2008-01-05 17:30:56 +00:00
case PAD_TRAPEZOID:
2007-08-23 04:28:46 +00:00
pad_type = "POLYGON";
break;
}
}
fputs( "$ENDPADS\n\n", file );
}
/*The PADSTACKS section is optional, and is used to describe how a group of
* pads are
* arranged. The keywords used in the PADSTACKS section are:
* $PADSTACKS
* PADSTACK <pad_name> <drill_size>
* PAD <pad_name> <layer> <rot> <mirror>
* ATTRIBUTE <attrib_ref>
* $ENDPADSTACKS
* $PADSTACKS and $ENDPADSTACKS mark the PADSTACKS section of the GenCAD file.
*/
void CreatePadsStacksSection( FILE* file, BOARD* pcb )
{
fputs( "$PADSTACKS\n", file );
fputs( "$ENDPADSTACKS\n\n", file );
}
2009-11-02 20:36:20 +00:00
/* Creates the footprint shape list.
* We must use one shape for identical footprint (i.e. come from the same
* footprint in lib)
2009-11-02 20:36:20 +00:00
* But because pads shapes and positions can be easily modified on board,
* a shape is created by footprint found.
* (todo : compare footprints shapes and creates only one shape for all
* footprints found having the same shape)
2009-11-02 20:36:20 +00:00
* The shape is always given in orientation 0, position 0 not flipped
*
* Syntax:
2007-08-23 04:28:46 +00:00
* $SHAPES
* SHAPE <shape_name>
* INSERT <string> here <string> = "TH"
2007-08-23 04:28:46 +00:00
* shape_descr (line, arc ..)
* PIN <pin_name> <pad_name> <x_y_ref> <layer> <rot> <mirror>
*
2007-08-23 04:28:46 +00:00
* SHAPE <shape_name>
* ..
* $ENDSHAPES
*/
void CreateShapesSection( FILE* file, BOARD* pcb )
{
2009-11-02 20:36:20 +00:00
MODULE* module;
D_PAD* pad;
const char* layer;
int orient;
wxString pinname;
const char* mirror = "0";
2007-08-23 04:28:46 +00:00
fputs( "$SHAPES\n", file );
for( module = pcb->m_Modules; module != NULL; module = module->Next() )
2007-08-23 04:28:46 +00:00
{
2009-11-02 20:36:20 +00:00
FootprintWriteShape( file, module );
for( pad = module->m_Pads; pad != NULL; pad = pad->Next() )
2007-08-23 04:28:46 +00:00
{
layer = "ALL";
if( ( pad->m_Masque_Layer & ALL_CU_LAYERS ) == LAYER_BACK )
2007-08-23 04:28:46 +00:00
{
if( module->GetLayer() == LAYER_N_FRONT )
2007-08-23 04:28:46 +00:00
layer = "BOTTOM";
else
layer = "TOP";
}
else if( ( pad->m_Masque_Layer & ALL_CU_LAYERS ) == LAYER_FRONT )
2007-08-23 04:28:46 +00:00
{
if( module->GetLayer() == LAYER_N_FRONT )
2007-08-23 04:28:46 +00:00
layer = "TOP";
else
layer = "BOTTOM";
}
pad->ReturnStringPadName( pinname );
if( pinname.IsEmpty() )
pinname = wxT( "noname" );
orient = pad->m_Orient - module->m_Orient;
NORMALIZE_ANGLE_POS( orient );
fprintf( file, "PIN %s PAD%d %d %d %s %d %s",
TO_UTF8( pinname ), pad->GetSubRatsnest(),
2007-08-23 04:28:46 +00:00
pad->m_Pos0.x, -pad->m_Pos0.y,
layer, orient / 10, mirror );
if( orient % 10 )
fprintf( file, " .%d", orient % 10 );
fprintf( file, "\n" );
}
}
fputs( "$ENDSHAPES\n\n", file );
}
2007-08-23 04:28:46 +00:00
2009-11-02 20:36:20 +00:00
/* Creates the section $COMPONENTS (Footprints placement)
* When a footprint is on bottom side of the board::
* shapes are given with option "FLIP" and "MIRRORX".
* - But shapes remain given like component not mirrored and not flipped
* - orientation is given like if where not mirrored and not flipped.
2007-08-23 04:28:46 +00:00
*/
void CreateComponentsSection( FILE* file, BOARD* pcb )
{
2007-08-23 04:28:46 +00:00
MODULE* module = pcb->m_Modules;
TEXTE_MODULE* PtTexte;
const char* mirror;
const char* flip;
2007-08-23 04:28:46 +00:00
int ii;
fputs( "$COMPONENTS\n", file );
for( ; module != NULL; module = module->Next() )
2007-08-23 04:28:46 +00:00
{
int orient = module->m_Orient;
if( module->flag )
{
2009-11-02 20:36:20 +00:00
mirror = "MIRRORX"; // Mirrored relative to X axis
flip = "FLIP"; // Normal shape description ( gencad
// viewer must show it flipped and
// mirrored)
2007-08-23 04:28:46 +00:00
NEGATE_AND_NORMALIZE_ANGLE_POS( orient );
}
else
{
mirror = "0";
flip = "0";
}
fprintf( file, "COMPONENT %s\n",
TO_UTF8( module->m_Reference->m_Text ) );
fprintf( file, "DEVICE %s\n",
TO_UTF8( module->m_Reference->m_Text ) );
fprintf( file, "PLACE %d %d\n", mapXto( module->m_Pos.x ),
mapYto( module->m_Pos.y ) );
2007-08-23 04:28:46 +00:00
fprintf( file, "LAYER %s\n", (module->flag) ? "BOTTOM" : "TOP" );
fprintf( file, "ROTATION %d", orient / 10 );
if( orient % 10 )
fprintf( file, ".%d", orient % 10 );
fputs( "\n", file );
fprintf( file, "SHAPE %s %s %s\n",
TO_UTF8( module->m_Reference->m_Text ), mirror, flip );
2007-08-23 04:28:46 +00:00
2009-11-02 20:36:20 +00:00
/* creates texts (ref and value) */
2007-08-23 04:28:46 +00:00
PtTexte = module->m_Reference;
for( ii = 0; ii < 2; ii++ )
{
int orient = PtTexte->m_Orient;
wxString layer = GenCAD_Layer_Name[SILKSCREEN_N_FRONT];
2007-08-23 04:28:46 +00:00
fprintf( file, "TEXT %d %d %d %d.%d %s %s \"%s\"",
PtTexte->m_Pos0.x, -PtTexte->m_Pos0.y,
PtTexte->m_Size.x,
orient / 10, orient % 10,
mirror,
TO_UTF8( layer ),
TO_UTF8( PtTexte->m_Text )
2007-08-23 04:28:46 +00:00
);
fprintf( file, " 0 0 %d %d\n",
(int) ( PtTexte->m_Size.x * PtTexte->m_Text.Len() ),
(int) PtTexte->m_Size.y );
PtTexte = module->m_Value;
}
2009-11-02 20:36:20 +00:00
//put a comment:
fprintf( file, "SHEET Part %s %s\n",
TO_UTF8( module->m_Reference->m_Text ),
TO_UTF8( module->m_Value->m_Text ) );
2007-08-23 04:28:46 +00:00
}
fputs( "$ENDCOMPONENTS\n\n", file );
}
2007-08-23 04:28:46 +00:00
2009-11-02 20:36:20 +00:00
/* Creates the list of Nets:
2007-08-23 04:28:46 +00:00
* $SIGNALS
* SIGNAL <net name>
2007-08-23 04:28:46 +00:00
* NODE <component name> <pin name>
* ...
* NODE <component name> <pin name>
* $ENDSIGNALS
*/
void CreateSignalsSection( FILE* file, BOARD* pcb )
{
2009-11-02 20:36:20 +00:00
wxString msg;
NETINFO_ITEM* net;
2009-11-02 20:36:20 +00:00
D_PAD* pad;
MODULE* module;
int NbNoConn = 1;
2007-08-23 04:28:46 +00:00
fputs( "$SIGNALS\n", file );
2009-11-02 20:36:20 +00:00
for( unsigned ii = 0; ii < pcb->m_NetInfo->GetCount(); ii++ )
2007-08-23 04:28:46 +00:00
{
2009-11-02 20:36:20 +00:00
net = pcb->m_NetInfo->GetNetItem( ii );
if( net->GetNetname() == wxEmptyString ) // dummy netlist (no
// connection)
2007-08-23 04:28:46 +00:00
{
wxString msg; msg << wxT( "NoConnection" ) << NbNoConn++;
2009-11-02 20:36:20 +00:00
net->SetNetname( msg );;
2007-08-23 04:28:46 +00:00
}
if( net->GetNet() <= 0 ) // dummy netlist (no connection)
2007-08-23 04:28:46 +00:00
continue;
msg = wxT( "SIGNAL " ) + net->GetNetname();
fputs( TO_UTF8( msg ), file );
2008-02-03 15:22:22 +00:00
fputs( "\n", file );
2007-08-23 04:28:46 +00:00
for( module = pcb->m_Modules; module != NULL; module = module->Next() )
2007-08-23 04:28:46 +00:00
{
for( pad = module->m_Pads; pad != NULL; pad = pad->Next() )
2007-08-23 04:28:46 +00:00
{
wxString padname;
if( pad->GetNet() != net->GetNet() )
2007-08-23 04:28:46 +00:00
continue;
2007-08-23 04:28:46 +00:00
pad->ReturnStringPadName( padname );
msg.Printf( wxT( "NODE %s %.4s" ),
GetChars( module->m_Reference->m_Text ),
GetChars( padname ) );
fputs( TO_UTF8( msg ), file );
2008-02-03 15:22:22 +00:00
fputs( "\n", file );
2007-08-23 04:28:46 +00:00
}
}
}
fputs( "$ENDSIGNALS\n\n", file );
}
2007-08-23 04:28:46 +00:00
2009-11-02 20:36:20 +00:00
/* Creates the section $HEADER ... $ENDHEADER
2007-08-23 04:28:46 +00:00
*/
bool CreateHeaderInfoData( FILE* file, WinEDA_PcbFrame* frame )
{
2007-08-23 04:28:46 +00:00
wxString msg;
PCB_SCREEN* screen = (PCB_SCREEN*) ( frame->GetScreen() );
2007-08-23 04:28:46 +00:00
fputs( "$HEADER\n", file );
fputs( "GENCAD 1.4\n", file );
msg = wxT( "USER " ) + wxGetApp().GetAppName() + wxT( " " ) +
2009-11-02 20:36:20 +00:00
GetBuildVersion();
fputs( TO_UTF8( msg ), file ); fputs( "\n", file );
msg = wxT( "DRAWING " ) + screen->GetFileName();
fputs( TO_UTF8( msg ), file ); fputs( "\n", file );
msg = wxT( "REVISION " ) + screen->m_Revision + wxT( " " ) +
screen->m_Date;
fputs( TO_UTF8( msg ), file ); fputs( "\n", file );
2007-08-23 04:28:46 +00:00
msg.Printf( wxT( "UNITS USER %d" ), PCB_INTERNAL_UNIT );
fputs( TO_UTF8( msg ), file ); fputs( "\n", file );
msg.Printf( wxT( "ORIGIN %d %d" ),
mapXto( frame->m_Auxiliary_Axis_Position.x ),
mapYto( frame->m_Auxiliary_Axis_Position.y ) );
fputs( TO_UTF8( msg ), file ); fputs( "\n", file );
2007-08-23 04:28:46 +00:00
fputs( "INTERTRACK 0\n", file );
fputs( "$ENDHEADER\n\n", file );
return TRUE;
}
/*
2009-11-02 20:36:20 +00:00
* Sort function used to sort tracks segments:
* items are sorted by netcode, then by width then by layer
2007-08-23 04:28:46 +00:00
*/
static int Track_list_Sort_by_Netcode( const void* refptr, const void* objptr )
{
2007-08-23 04:28:46 +00:00
const TRACK* ref, * cmp;
int diff;
ref = *( (TRACK**) refptr );
cmp = *( (TRACK**) objptr );
2009-11-02 20:36:20 +00:00
if( ( diff = ref->GetNet() - cmp->GetNet() ) )
2007-08-23 04:28:46 +00:00
return diff;
if( ( diff = ref->m_Width - cmp->m_Width ) )
2007-08-23 04:28:46 +00:00
return diff;
2009-11-02 20:36:20 +00:00
if( ( diff = ref->GetLayer() - cmp->GetLayer() ) )
2007-08-23 04:28:46 +00:00
return diff;
return 0;
}
2007-08-23 04:28:46 +00:00
/* Creates the section ROUTES
* that handles tracks, vias
2009-11-02 20:36:20 +00:00
* TODO: add zones
2007-08-23 04:28:46 +00:00
* section:
* $ROUTE
* ...
* $ENROUTE
2009-11-02 20:36:20 +00:00
* Track segments must be sorted by nets
2007-08-23 04:28:46 +00:00
*/
void CreateRoutesSection( FILE* file, BOARD* pcb )
{
2007-08-23 04:28:46 +00:00
TRACK* track, ** tracklist;
int vianum = 1;
int old_netcode, old_width, old_layer;
int nbitems, ii;
2009-11-02 20:36:20 +00:00
// Count items
2007-08-23 04:28:46 +00:00
nbitems = 0;
for( track = pcb->m_Track; track != NULL; track = track->Next() )
2007-08-23 04:28:46 +00:00
nbitems++;
for( track = pcb->m_Zone; track != NULL; track = track->Next() )
2007-08-23 04:28:46 +00:00
{
if( track->Type() == TYPE_ZONE )
2007-08-23 04:28:46 +00:00
nbitems++;
}
2009-11-02 20:36:20 +00:00
tracklist = (TRACK**) MyMalloc( (nbitems + 1) * sizeof(TRACK*) );
2007-08-23 04:28:46 +00:00
nbitems = 0;
for( track = pcb->m_Track; track != NULL; track = track->Next() )
2007-08-23 04:28:46 +00:00
tracklist[nbitems++] = track;
for( track = pcb->m_Zone; track != NULL; track = track->Next() )
2007-08-23 04:28:46 +00:00
{
if( track->Type() == TYPE_ZONE )
2007-08-23 04:28:46 +00:00
tracklist[nbitems++] = track;
}
tracklist[nbitems] = NULL;
2009-11-02 20:36:20 +00:00
qsort( tracklist, nbitems, sizeof(TRACK*), Track_list_Sort_by_Netcode );
2007-08-23 04:28:46 +00:00
fputs( "$ROUTES\n", file );
old_netcode = -1; old_width = -1; old_layer = -1;
for( ii = 0; ii < nbitems; ii++ )
{
track = tracklist[ii];
2007-10-13 06:18:44 +00:00
if( old_netcode != track->GetNet() )
2007-08-23 04:28:46 +00:00
{
2007-10-13 06:18:44 +00:00
old_netcode = track->GetNet();
NETINFO_ITEM* net = pcb->FindNet( track->GetNet() );
2009-11-02 20:36:20 +00:00
wxString netname;
if( net && (net->GetNetname() != wxEmptyString) )
netname = net->GetNetname();
2007-08-23 04:28:46 +00:00
else
netname = wxT( "_noname_" );
fprintf( file, "ROUTE %s\n", TO_UTF8( netname ) );
2007-08-23 04:28:46 +00:00
}
if( old_width != track->m_Width )
{
old_width = track->m_Width;
fprintf( file, "TRACK TRACK%d\n", track->m_Width );
}
if( (track->Type() == TYPE_TRACK) || (track->Type() == TYPE_ZONE) )
2007-08-23 04:28:46 +00:00
{
if( old_layer != track->GetLayer() )
{
old_layer = track->GetLayer();
fprintf( file, "LAYER %s\n",
TO_UTF8( GenCAD_Layer_Name[track->GetLayer() &
0x1F] ) );
2007-08-23 04:28:46 +00:00
}
fprintf( file, "LINE %d %d %d %d\n",
mapXto( track->m_Start.x ), mapYto( track->m_Start.y ),
mapXto( track->m_End.x ), mapYto( track->m_End.y ) );
2007-08-23 04:28:46 +00:00
}
if( track->Type() == TYPE_VIA )
2007-08-23 04:28:46 +00:00
{
fprintf( file, "VIA viapad%d %d %d ALL %d via%d\n",
track->m_Width,
mapXto( track->m_Start.x ), mapYto( track->m_Start.y ),
track->GetDrillValue(), vianum++ );
2007-08-23 04:28:46 +00:00
}
}
fputs( "$ENDROUTES\n\n", file );
free( tracklist );
}
/* Creates the section $DEVICES
2009-11-02 20:36:20 +00:00
* This is a list of footprints properties
* ( Shapes are in section $SHAPE )
2007-08-23 04:28:46 +00:00
*/
void CreateDevicesSection( FILE* file, BOARD* pcb )
{
2007-08-23 04:28:46 +00:00
MODULE* module;
D_PAD* pad;
fputs( "$DEVICES\n", file );
for( module = pcb->m_Modules; module != NULL; module = module->Next() )
2007-08-23 04:28:46 +00:00
{
fprintf( file, "DEVICE %s\n",
TO_UTF8( module->m_Reference->m_Text ) );
fprintf( file, "PART %s\n", TO_UTF8( module->m_LibRef ) );
2007-08-23 04:28:46 +00:00
fprintf( file, "TYPE %s\n", "UNKNOWN" );
for( pad = module->m_Pads; pad != NULL; pad = pad->Next() )
2007-08-23 04:28:46 +00:00
{
fprintf( file, "PINDESCR %.4s", pad->m_Padname );
if( pad->GetNetname() == wxEmptyString )
2007-08-23 04:28:46 +00:00
fputs( " NoConn\n", file );
else
fprintf( file, " %.4s\n", pad->m_Padname );
}
fprintf( file, "ATTRIBUTE %s\n",
TO_UTF8( module->m_Value->m_Text ) );
2007-08-23 04:28:46 +00:00
}
fputs( "$ENDDEVICES\n\n", file );
}
/* Creates the section $BOARD.
* We output here only the board boundary box
2007-08-23 04:28:46 +00:00
*/
void CreateBoardSection( FILE* file, BOARD* pcb )
{
2007-08-23 04:28:46 +00:00
fputs( "$BOARD\n", file );
fprintf( file, "LINE %d %d %d %d\n",
mapXto( pcb->m_BoundaryBox.m_Pos.x ),
mapYto( pcb->m_BoundaryBox.m_Pos.y ),
mapXto( pcb->m_BoundaryBox.GetRight() ),
mapYto( pcb->m_BoundaryBox.m_Pos.y ) );
2007-08-23 04:28:46 +00:00
fprintf( file, "LINE %d %d %d %d\n",
mapXto( pcb->m_BoundaryBox.GetRight() ),
mapYto( pcb->m_BoundaryBox.m_Pos.y ),
mapXto( pcb->m_BoundaryBox.GetRight() ),
mapYto( pcb->m_BoundaryBox.GetBottom() ) );
2007-08-23 04:28:46 +00:00
fprintf( file, "LINE %d %d %d %d\n",
mapXto( pcb->m_BoundaryBox.GetRight() ),
mapYto( pcb->m_BoundaryBox.GetBottom() ),
mapXto( pcb->m_BoundaryBox.m_Pos.x ),
mapYto( pcb->m_BoundaryBox.GetBottom() ) );
2007-08-23 04:28:46 +00:00
fprintf( file, "LINE %d %d %d %d\n",
mapXto( pcb->m_BoundaryBox.m_Pos.x ),
mapYto( pcb->m_BoundaryBox.GetBottom() ),
mapXto( pcb->m_BoundaryBox.m_Pos.x ),
mapYto( pcb->m_BoundaryBox.m_Pos.y ) );
2007-08-23 04:28:46 +00:00
fputs( "$ENDBOARD\n\n", file );
}
2009-11-02 20:36:20 +00:00
/* Creates the section "$TRACKS"
* This sections give the list of widths (tools) used in tracks and vias
2007-08-23 04:28:46 +00:00
* format:
* $TRACK
* TRACK <name> <width>
* $ENDTRACK
*
2009-11-02 20:36:20 +00:00
* Each tool name is build like this: "TRACK" + track width.
* For instance for a width = 120 : name = "TRACK120".
2007-08-23 04:28:46 +00:00
*/
void CreateTracksInfoData( FILE* file, BOARD* pcb )
{
2007-08-23 04:28:46 +00:00
TRACK* track;
2009-11-02 20:36:20 +00:00
int last_width = -1;
2007-08-23 04:28:46 +00:00
/* Find thickness used for traces. */
2007-08-23 04:28:46 +00:00
2009-05-23 15:53:47 +00:00
std::vector <int> trackinfo;
2007-08-23 04:28:46 +00:00
2009-11-02 20:36:20 +00:00
unsigned ii;
for( track = pcb->m_Track; track != NULL; track = track->Next() )
2007-08-23 04:28:46 +00:00
{
if( last_width != track->m_Width ) // Find a thickness already used.
2007-08-23 04:28:46 +00:00
{
for( ii = 0; ii < trackinfo.size(); ii++ )
2007-08-23 04:28:46 +00:00
{
2009-05-23 15:53:47 +00:00
if( trackinfo[ii] == track->m_Width )
2009-11-02 20:36:20 +00:00
break;
2007-08-23 04:28:46 +00:00
}
2009-11-02 20:36:20 +00:00
if( ii == trackinfo.size() ) // not found
trackinfo.push_back( track->m_Width );
2009-05-23 15:53:47 +00:00
last_width = track->m_Width;
2009-11-02 20:36:20 +00:00
}
2007-08-23 04:28:46 +00:00
}
for( track = pcb->m_Zone; track != NULL; track = track->Next() )
2007-08-23 04:28:46 +00:00
{
if( last_width != track->m_Width ) // Find a thickness already used.
2007-08-23 04:28:46 +00:00
{
for( ii = 0; ii < trackinfo.size(); ii++ )
2007-08-23 04:28:46 +00:00
{
2009-05-23 15:53:47 +00:00
if( trackinfo[ii] == track->m_Width )
2009-11-02 20:36:20 +00:00
break;
2007-08-23 04:28:46 +00:00
}
2009-11-02 20:36:20 +00:00
if( ii == trackinfo.size() ) // not found
trackinfo.push_back( track->m_Width );
2009-05-23 15:53:47 +00:00
last_width = track->m_Width;
2007-08-23 04:28:46 +00:00
}
}
// Write data
fputs( "$TRACKS\n", file );
for( ii = 0; ii < trackinfo.size(); ii++ )
2007-08-23 04:28:46 +00:00
{
2009-05-23 15:53:47 +00:00
fprintf( file, "TRACK TRACK%d %d\n", trackinfo[ii], trackinfo[ii] );
2007-08-23 04:28:46 +00:00
}
fputs( "$ENDTRACKS\n\n", file );
}
2009-11-02 20:36:20 +00:00
/* Creates the shape of a footprint (section SHAPE)
* The shape is always given "normal" (Orient 0, not mirrored)
* Syntax:
2007-08-23 04:28:46 +00:00
* SHAPE <shape_name>
* INSERT <string> here <string> = "TH"
2007-08-23 04:28:46 +00:00
* shape_descr (line, arc ..):
* LINE startX startY endX endY
2009-11-02 20:36:20 +00:00
* ARC startX startY endX endY centreX centreY
2008-01-05 17:30:56 +00:00
* PAD_CIRCLE centreX scentreY radius
2007-08-23 04:28:46 +00:00
*/
void FootprintWriteShape( FILE* file, MODULE* module )
{
EDGE_MODULE* edge;
EDA_ITEM* item;
int y_axis_sign = -1; // Control Y axis change sign (as normal
// module / mirror axis and conventions)
2007-08-23 04:28:46 +00:00
2009-11-02 20:36:20 +00:00
/* creates header: */
fprintf( file, "SHAPE %s\n", TO_UTF8( module->m_Reference->m_Text ) );
fprintf( file, "INSERT %s\n",
(module->m_Attributs & MOD_CMS) ? "SMD" : "TH" );
2007-08-23 04:28:46 +00:00
/* creates Attributes */
2007-08-23 04:28:46 +00:00
if( module->m_Attributs != MOD_DEFAULT )
{
fprintf( file, "ATTRIBUTE" );
if( module->m_Attributs & MOD_CMS )
2008-01-05 17:30:56 +00:00
fprintf( file, " PAD_SMD" );
2007-08-23 04:28:46 +00:00
if( module->m_Attributs & MOD_VIRTUAL )
fprintf( file, " VIRTUAL" );
fprintf( file, "\n" );
}
2009-11-02 20:36:20 +00:00
/* creates Drawing */
item = module->m_Drawings;
for( ; item != NULL; item = item->Next() )
2007-08-23 04:28:46 +00:00
{
switch( item->Type() )
2007-08-23 04:28:46 +00:00
{
case TYPE_TEXTE_MODULE:
2007-08-23 04:28:46 +00:00
break;
case TYPE_EDGE_MODULE:
edge = (EDGE_MODULE*) item;
2007-08-23 04:28:46 +00:00
switch( edge->m_Shape )
2007-08-23 04:28:46 +00:00
{
case S_SEGMENT:
fprintf( file, "LINE %d %d %d %d\n",
edge->m_Start0.x, y_axis_sign * edge->m_Start0.y,
edge->m_End0.x, y_axis_sign * edge->m_End0.y );
2007-08-23 04:28:46 +00:00
break;
case S_CIRCLE:
{
int rayon = (int) hypot(
(double) ( edge->m_End0.x - edge->m_Start0.x ),
(double) ( edge->m_End0.y - edge->m_Start0.y ) );
2007-08-23 04:28:46 +00:00
fprintf( file, "CIRCLE %d %d %d\n",
edge->m_Start0.x, y_axis_sign * edge->m_Start0.y,
2007-08-23 04:28:46 +00:00
rayon );
break;
}
case S_ARC: /* print ARC x,y start x,y end x,y center */
{ // Arcs are defined counter clockwise (positive trigonometric)
// from the start point to the end point (0 to 360 degrees)
wxPoint arcStart, arcEnd;
// edge->m_Start0 is the arc center relative to the shape position
// edge->m_End0 is the arc start point relative to the shape position
arcStart = edge->m_End0;
// calculate arcEnd arc end point relative to the shape position, in pcbnew coordinates
arcEnd = arcStart;
RotatePoint( &arcEnd, edge->m_Start0, -edge->m_Angle );
// due to difference between pcbnew and gencad, swap arc start and arc end
EXCHG(arcEnd, arcStart);
// print arc shape:
2007-08-23 04:28:46 +00:00
fprintf( file, "ARC %d %d %d %d %d %d\n",
arcStart.x, y_axis_sign * arcStart.y, // Start point
arcEnd.x, y_axis_sign * arcEnd.y, // End point
edge->m_Start0.x, y_axis_sign * edge->m_Start0.y );
2007-08-23 04:28:46 +00:00
break;
}
default:
DisplayError( NULL, wxT( "Type Edge Module invalid." ) );
2007-08-23 04:28:46 +00:00
break;
2009-11-02 20:36:20 +00:00
} /* end switch PtEdge->m_Shape */
2007-08-23 04:28:46 +00:00
break;
default:
break;
2009-11-02 20:36:20 +00:00
} /* End switch Items type */
2007-08-23 04:28:46 +00:00
}
}