export_to_pcbnew.cpp 12.8 KB
Newer Older
1 2
/**
 * @file export_to_pcbnew.cpp
3
 * @brief Export the layers to Pcbnew.
4
 */
5

6 7
#include <vector>

8 9
#include <fctsys.h>
#include <common.h>
10
// #include <class_drawpanel.h>
11 12 13 14 15 16 17 18
#include <confirm.h>
#include <macros.h>
#include <kicad_string.h>
#include <gestfich.h>
#include <trigo.h>
#include <gerbview.h>
#include <class_gerber_draw_item.h>
#include <select_layers_to_pcb.h>
19 20
#include <build_version.h>
#include <wildcards_and_files_ext.h>
21

22 23 24 25
#define TO_PCB_UNIT( x ) KiROUND( x / IU_PER_DECIMILS )

#define TRACK_TYPE  0
#define VIA_TYPE    1
26

27
/* A helper class to export a Gerber set of files to Pcbnew
28
 */
29 30
class GBR_TO_PCB_EXPORTER
{
31 32 33 34 35 36 37 38
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
39
public:
40
    GBR_TO_PCB_EXPORTER( GERBVIEW_FRAME* aFrame, const wxString& aFileName );
41
    ~GBR_TO_PCB_EXPORTER();
42 43 44

    /**
     * Function ExportPcb
45
     * saves a board from a set of Gerber images.
46
     */
47
    bool    ExportPcb( LAYER_NUM* LayerLookUpTable, int aCopperLayers );
48 49

private:
50 51 52 53 54 55
    /**
     * Function export_non_copper_item
     * 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
     */
56
    void    export_non_copper_item( GERBER_DRAW_ITEM* aGbrItem, LAYER_NUM aLayer );
57 58 59 60 61 62 63

    /**
     * Function export_copper_item
     * 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
     */
64
    void    export_copper_item( GERBER_DRAW_ITEM* aGbrItem, LAYER_NUM aLayer );
65 66 67 68 69 70

    /**
     * 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
     */
71
    void    export_flashed_copper_item( GERBER_DRAW_ITEM* aGbrItem );
72 73 74 75 76 77 78

    /**
     * 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
     */
79
    void    export_segline_copper_item( GERBER_DRAW_ITEM* aGbrItem, LAYER_NUM aLayer );
80 81 82 83 84 85 86 87

    /**
     * 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
     */
88
    void    export_segarc_copper_item( GERBER_DRAW_ITEM* aGbrItem, LAYER_NUM aLayer );
89 90 91 92 93 94 95

    /**
     * function writePcbLineItem
     * basic write function to write a DRAWSEGMENT item or a TRACK/VIA item
     * to the board file
     */
    void    writePcbLineItem( int aShape, int aType, wxPoint& aStart, wxPoint& aEnd,
96
                              int aWidth, LAYER_NUM aLayer, int aDrill, int aAngle = 0 );
97 98 99 100 101 102

    /**
     * function writePcbHeader
     * Write a very basic header to the board file
     */
    void    writePcbHeader();
103 104
};

105

106
GBR_TO_PCB_EXPORTER::GBR_TO_PCB_EXPORTER( GERBVIEW_FRAME* aFrame, const wxString& aFileName )
107
{
108 109
    m_gerbview_frame    = aFrame;
    m_pcb_file_name     = aFileName;
110 111
}

112

113 114 115 116
GBR_TO_PCB_EXPORTER::~GBR_TO_PCB_EXPORTER()
{
}

117

118
/* Export data in Pcbnew format
119
 * remember Pcbnew uses a Y reversed axis, so we must negate all Y coordinates
120
 */
121
void GERBVIEW_FRAME::ExportDataInPcbnewFormat( wxCommandEvent& event )
122
{
123
    int layercount = 0;
124

125
    // Count the Gerber layers which are actually currently used
126
    for( LAYER_NUM ii = FIRST_LAYER; ii < NB_GERBER_LAYERS; ++ii )
127
    {
128
        if( g_GERBER_List[ii] != NULL )
129
            layercount++;
130 131
    }

132
    if( layercount == 0 )
133
    {
134
        DisplayInfoMessage( this,
135
                            _( "None of the Gerber layers contain any data" ) );
136 137 138
        return;
    }

139 140
    wxString        fileName;
    wxString        path = wxGetCwd();;
141

142 143
    wxFileDialog    filedlg( this, _( "Board file name:" ),
                             path, fileName, LegacyPcbFileWildcard,
144
                             wxFD_SAVE );
145 146

    if( filedlg.ShowModal() == wxID_CANCEL )
147 148
        return;

149 150
    fileName = filedlg.GetPath();

151
    /* Install a dialog frame to choose the mapping
152
     * between gerber layers and Pcbnew layers
153
     */
154 155 156
    LAYERS_MAP_DIALOG* layerdlg = new LAYERS_MAP_DIALOG( this );
    int ok = layerdlg->ShowModal();
    layerdlg->Destroy();
157

158 159 160
    if( ok != wxID_OK )
        return;

161
    if( wxFileExists( fileName ) )
162
    {
163
        if( !IsOK( this, _( "OK to change the existing file ?" ) ) )
164 165
            return;
    }
166

167
    GBR_TO_PCB_EXPORTER gbr_exporter( this, fileName );
168

169 170
    gbr_exporter.ExportPcb( layerdlg->GetLayersLookUpTable(),
                            layerdlg->GetCopperLayersCount() );
171 172
}

173

174
bool GBR_TO_PCB_EXPORTER::ExportPcb( LAYER_NUM* LayerLookUpTable, int aCopperLayers )
175
{
176
    m_fp = wxFopen( m_pcb_file_name, wxT( "wt" ) );
177

178 179 180 181 182 183
    if( m_fp == NULL )
    {
        wxString msg;
        msg.Printf( _( "Cannot create file <%s>" ), GetChars( m_pcb_file_name ) );
        DisplayError( m_gerbview_frame, msg );
        return false;
184 185
    }

186
    m_pcbCopperLayersCount = aCopperLayers;
187

188
    writePcbHeader();
189

dickelbeck's avatar
dickelbeck committed
190
    // create an image of gerber data
191 192
    // First: non copper layers:
    GERBER_DRAW_ITEM* gerb_item = m_gerbview_frame->GetItemsList();
193

194
    for( ; gerb_item; gerb_item = gerb_item->Next() )
195
    {
196 197
        LAYER_NUM layer = gerb_item->GetLayer();
        LAYER_NUM pcb_layer_number = LayerLookUpTable[layer];
198

199
        if( !IsPcbLayer( pcb_layer_number ) )
200
            continue;
201

202
        if( pcb_layer_number > LAST_COPPER_LAYER )
203
            export_non_copper_item( gerb_item, pcb_layer_number );
204 205
    }

206 207 208
    // Copper layers
    fprintf( m_fp, "$TRACK\n" );
    gerb_item = m_gerbview_frame->GetItemsList();
209

210
    for( ; gerb_item; gerb_item = gerb_item->Next() )
211
    {
212 213
        LAYER_NUM layer = gerb_item->GetLayer();
        LAYER_NUM pcb_layer_number = LayerLookUpTable[layer];
214

215 216
        if( pcb_layer_number < 0 || pcb_layer_number > LAST_COPPER_LAYER )
            continue;
217

218 219
        else
            export_copper_item( gerb_item, pcb_layer_number );
220
    }
221

222 223 224 225 226
    fprintf( m_fp, "$EndTRACK\n" );
    fprintf( m_fp, "$EndBOARD\n" );

    fclose( m_fp );
    m_fp = NULL;
227 228 229
    return true;
}

230

231
void GBR_TO_PCB_EXPORTER::export_non_copper_item( GERBER_DRAW_ITEM* aGbrItem, LAYER_NUM aLayer )
232
{
233 234 235
    #define SEG_SHAPE   0
    #define ARC_SHAPE   2
    int     shape   = SEG_SHAPE;
236

237
    // please note: the old PCB format only has integer support for angles
238 239
    int     angle   = 0;
    wxPoint seg_start, seg_end;
240

241 242
    seg_start   = aGbrItem->m_Start;
    seg_end     = aGbrItem->m_End;
243 244 245

    if( aGbrItem->m_Shape == GBR_ARC )
    {
246 247 248 249
        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 ) );
250

251
        shape       = ARC_SHAPE;
252
        angle       = KiROUND( RAD2DECIDEG(a - b) );
253
        seg_start   = aGbrItem->m_ArcCentre;
254

255
        if( angle < 0 )
256
        {
257 258
            NEGATE( angle );
            seg_end = aGbrItem->m_Start;
259 260 261
        }
    }

262
    // Reverse Y axis:
263 264 265
    NEGATE( seg_start.y );
    NEGATE( seg_end.y );
    writePcbLineItem( shape, 0, seg_start, seg_end, aGbrItem->m_Size.x, aLayer, -2, angle );
266
}
267

268

269
void GBR_TO_PCB_EXPORTER::export_copper_item( GERBER_DRAW_ITEM* aGbrItem, LAYER_NUM aLayer )
270 271 272
{
    switch( aGbrItem->m_Shape )
    {
273 274 275 276 277 278 279 280 281 282 283 284 285 286
    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;

    default:
        export_segline_copper_item( aGbrItem, aLayer );
        break;
287 288
    }
}
289

290

291
void GBR_TO_PCB_EXPORTER::export_segline_copper_item( GERBER_DRAW_ITEM* aGbrItem, LAYER_NUM aLayer )
292
{
293 294 295 296
    wxPoint seg_start, seg_end;

    seg_start   = aGbrItem->m_Start;
    seg_end     = aGbrItem->m_End;
297 298

    // Reverse Y axis:
299 300
    NEGATE( seg_start.y );
    NEGATE( seg_end.y );
301

302
    writePcbLineItem( 0, TRACK_TYPE, seg_start, seg_end, aGbrItem->m_Size.x, aLayer, -1 );
303 304
}

305

306
void GBR_TO_PCB_EXPORTER::export_segarc_copper_item( GERBER_DRAW_ITEM* aGbrItem, LAYER_NUM aLayer )
307
{
308 309 310 311 312 313 314
    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;
315 316

    /* Because Pcbnew does not know arcs in tracks,
317 318
     * 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.
319
     */
320 321
    #define SEG_COUNT_CIRCLE    16
    #define DELTA_ANGLE         2 * M_PI / SEG_COUNT_CIRCLE
322

323 324 325
    // calculate the number of segments from a to b.
    // we want CNT_PER_360 segments fo a circle
    if( a > b )
326
        b += 2 * M_PI;
327

328
    wxPoint curr_start = start;
329 330 331
    wxPoint seg_start, seg_end;

    int     ii = 1;
332

333
    for( double rot = a; rot < (b - DELTA_ANGLE); rot += DELTA_ANGLE, ii++ )
334
    {
335
        seg_start = curr_start;
336
        wxPoint curr_end = start;
337
        RotatePoint( &curr_end, aGbrItem->m_ArcCentre,
338
                     -RAD2DECIDEG( DELTA_ANGLE * ii ) );
339
        seg_end = curr_end;
340
        // Reverse Y axis:
341 342 343
        NEGATE( seg_start.y );
        NEGATE( seg_end.y );
        writePcbLineItem( 0, TRACK_TYPE, seg_start, seg_end, aGbrItem->m_Size.x, aLayer, -1 );
344 345
        curr_start = curr_end;
    }
346

347 348
    if( end != curr_start )
    {
349 350
        seg_start   = curr_start;
        seg_end     = end;
351
        // Reverse Y axis:
352 353 354
        NEGATE( seg_start.y );
        NEGATE( seg_end.y );
        writePcbLineItem( 0, TRACK_TYPE, seg_start, seg_end, aGbrItem->m_Size.x, aLayer, -1 );
355 356 357 358 359 360 361 362 363
    }
}


/*
 * 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
 */
364
void GBR_TO_PCB_EXPORTER::export_flashed_copper_item( GERBER_DRAW_ITEM* aGbrItem )
365
{
366 367 368 369 370 371 372 373
    // 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 );
374

375 376 377 378 379
    wxPoint via_pos;
    int     width;

    via_pos = aGbrItem->m_Start;
    width   = (aGbrItem->m_Size.x + aGbrItem->m_Size.y) / 2;
380
    // Reverse Y axis:
381 382 383 384
    NEGATE( via_pos.y );
    // Layers are 0 to 15 (Cu/Cmp) = 0x0F
    #define IS_VIA 1
    #define SHAPE_VIA_THROUGH 3
385 386 387
    // XXX EVIL usage of LAYER
    writePcbLineItem( SHAPE_VIA_THROUGH, IS_VIA, via_pos, via_pos, width,
                      0x0F, -1 );
388 389 390 391 392 393 394 395 396 397 398 399 400 401
}


void GBR_TO_PCB_EXPORTER::writePcbHeader()
{
    fprintf( m_fp, "PCBNEW-BOARD Version 1 date %s\n\n# Created by GerbView %s\n\n",
             TO_UTF8( DateAndTime() ), TO_UTF8( GetBuildVersion() ) );
    fprintf( m_fp, "$GENERAL\n" );
    fprintf( m_fp, "encoding utf-8\n" );
    fprintf( m_fp, "Units deci-mils\n" );

    // Write copper layer count
    fprintf( m_fp, "LayerCount %d\n", m_pcbCopperLayersCount );
    // Write enabled layer mask:
402
    int lmask = ALL_NO_CU_LAYERS | EXTERNAL_CU_LAYERS;
403 404 405 406 407 408 409 410 411 412 413 414 415 416

    for( int ii = 0; ii < m_pcbCopperLayersCount - 2; ii++ )
        lmask |= 2 << ii;

    fprintf( m_fp, "EnabledLayers %08X\n", lmask );
    fprintf( m_fp, "$EndGENERAL\n\n" );

    // Creates void setup
    fprintf( m_fp, "$SETUP\n" );
    fprintf( m_fp, "$EndSETUP\n\n" );
}


void GBR_TO_PCB_EXPORTER::writePcbLineItem( int aShape, int aType, wxPoint& aStart, wxPoint& aEnd,
417
                                            int aWidth, LAYER_NUM aLayer, int aDrill, int aAngle )
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
{
    if( aDrill <= -2 )
        fprintf( m_fp, "$DRAWSEGMENT\n" );

    fprintf( m_fp, "Po %d %d %d %d %d %d\n", aShape,
             TO_PCB_UNIT( aStart.x ), TO_PCB_UNIT( aStart.y ),
             TO_PCB_UNIT( aEnd.x ), TO_PCB_UNIT( aEnd.y ),
             TO_PCB_UNIT( aWidth ) );
    fprintf( m_fp, "De %d %d %d %lX %X",
             aLayer, aType, aAngle, 0l, 0 );

    if( aDrill > -2 )
        fprintf( m_fp, " %d", aDrill );

    fprintf( m_fp, "\n" );

    if( aDrill <= -2 )
        fprintf( m_fp, "$EndDRAWSEGMENT\n" );
436
}