export_gencad.cpp 37.6 KB
Newer Older
1 2 3
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
4 5 6 7
 * Copyright (C) 2012 Jean-Pierre Charras, jean-pierre.charras@ujf-grenoble.fr
 * Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
 * Copyright (C) 2012 Wayne Stambaugh <stambaughw@verizon.net>
 * Copyright (C) 1992-2012 KiCad Developers, see AUTHORS.txt for contributors.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 *
 * 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
 */

27 28 29 30
/**
 * @file export_gencad.cpp
 * @brief Export GenCAD 1.4 format.
 */
31

32 33 34 35 36 37 38 39 40
#include <fctsys.h>
#include <class_drawpanel.h>
#include <confirm.h>
#include <gestfich.h>
#include <appl_wxstruct.h>
#include <wxPcbStruct.h>
#include <trigo.h>
#include <build_version.h>
#include <macros.h>
41

42
#include <pcbnew.h>
43

44 45 46 47
#include <class_board.h>
#include <class_module.h>
#include <class_track.h>
#include <class_edge_mod.h>
48

49

50
static bool CreateHeaderInfoData( FILE* aFile, PCB_EDIT_FRAME* frame );
51
static void CreateArtworksSection( FILE* aFile );
52 53 54 55 56 57 58 59
static void CreateTracksInfoData( FILE* aFile, BOARD* aPcb );
static void CreateBoardSection( FILE* aFile, BOARD* aPcb );
static void CreateComponentsSection( FILE* aFile, BOARD* aPcb );
static void CreateDevicesSection( FILE* aFile, BOARD* aPcb );
static void CreateRoutesSection( FILE* aFile, BOARD* aPcb );
static void CreateSignalsSection( FILE* aFile, BOARD* aPcb );
static void CreateShapesSection( FILE* aFile, BOARD* aPcb );
static void CreatePadsShapesSection( FILE* aFile, BOARD* aPcb );
charras's avatar
charras committed
60 61 62
static void FootprintWriteShape( FILE* File, MODULE* module );

// layer name for Gencad export
63
static const wxString GenCADLayerName[32] =
charras's avatar
charras committed
64
{
65 66 67 68 69 70
    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( "LAYER17" ),         wxT( "LAYER18" ),
71 72
    wxT( "SOLDERPASTE_BOTTOM" ), wxT( "SOLDERPASTE_TOP" ),
    wxT( "SILKSCREEN_BOTTOM" ),  wxT( "SILKSCREEN_TOP" ),
73 74 75
    wxT( "SOLDERMASK_BOTTOM" ),  wxT( "SOLDERMASK_TOP" ),  wxT( "LAYER25" ),
    wxT( "LAYER26" ),            wxT( "LAYER27" ),         wxT( "LAYER28" ),
    wxT( "LAYER29" ),            wxT( "LAYER30" ),         wxT( "LAYER31" ),
76
    wxT( "LAYER32" )
77 78
};

79 80 81
// flipped layer name for Gencad export (to make CAM350 imports correct)
static const wxString GenCADLayerNameFlipped[32] =
{
82 83 84 85 86 87 88 89 90 91 92
    wxT( "TOP" ),             wxT( "INNER14" ),            wxT( "INNER13" ),
    wxT( "INNER12" ),         wxT( "INNER11" ),            wxT( "INNER10" ),
    wxT( "INNER9" ),          wxT( "INNER8" ),             wxT( "INNER7" ),
    wxT( "INNER6" ),          wxT( "INNER5" ),             wxT( "INNER4" ),
    wxT( "INNER3" ),          wxT( "INNER2" ),             wxT( "INNER1" ),
    wxT( "BOTTOM" ),          wxT( "LAYER17" ),            wxT( "LAYER18" ),
    wxT( "SOLDERPASTE_TOP" ), wxT( "SOLDERPASTE_BOTTOM" ),
    wxT( "SILKSCREEN_TOP" ),  wxT( "SILKSCREEN_BOTTOM" ),
    wxT( "SOLDERMASK_TOP" ),  wxT( "SOLDERMASK_BOTTOM" ),  wxT( "LAYER25" ),
    wxT( "LAYER26" ),         wxT( "LAYER27" ),            wxT( "LAYER28" ),
    wxT( "LAYER29" ),         wxT( "LAYER30" ),            wxT( "LAYER31" ),
93 94 95 96 97
    wxT( "LAYER32" )
};

// These are the export origin (the auxiliary axis)
static int GencadOffsetX, GencadOffsetY;
98

Dick Hollenbeck's avatar
Dick Hollenbeck committed
99
/* GerbTool chokes on units different than INCH so this is the conversion
100
 *  factor */
101
const static double SCALE_FACTOR = 10000.0 * IU_PER_DECIMILS;
102

103 104 105

/* Two helper functions to calculate coordinates of modules in gencad values
 * (GenCAD Y axis from bottom to top)
charras's avatar
charras committed
106
 */
107
static double MapXTo( int aX )
108
{
109
    return (aX - GencadOffsetX) / SCALE_FACTOR;
110
}
111

112

113
static double MapYTo( int aY )
114
{
115
    return (GencadOffsetY - aY) / SCALE_FACTOR;
116 117
}

118

119 120
/* Driver function: processing starts here */
void PCB_EDIT_FRAME::ExportToGenCAD( wxCommandEvent& aEvent )
121
{
122
    wxFileName fn = GetBoard()->GetFileName();
charras's avatar
charras committed
123 124
    wxString   msg, ext, wildcard;
    FILE*      file;
125

126 127
    ext = wxT( "cad" );
    wildcard = _( "GenCAD 1.4 board files (.cad)|*.cad" );
128 129
    fn.SetExt( ext );

charras's avatar
charras committed
130
    wxFileDialog dlg( this, _( "Save GenCAD Board File" ), wxGetCwd(),
131 132 133 134
                      fn.GetFullName(), wildcard,
                      wxFD_SAVE | wxFD_OVERWRITE_PROMPT );

    if( dlg.ShowModal() == wxID_CANCEL )
135 136
        return;

137
    if( ( file = wxFopen( dlg.GetPath(), wxT( "wt" ) ) ) == NULL )
138
    {
139
        msg.Printf( _( "Unable to create <%s>" ), GetChars( dlg.GetPath() ) );
140 141 142
        DisplayError( this, msg ); return;
    }

143 144 145
    SetLocaleTo_C_standard(); // No pesky decimal separators in gencad

    // Update some board data, to ensure a reliable gencad export
146
    GetBoard()->ComputeBoundingBox();
147

148
    // Save the auxiliary origin for the rest of the module
149 150
    GencadOffsetX = GetOriginAxisPosition().x;
    GencadOffsetY = GetOriginAxisPosition().y;
151 152

    // No idea on *why* this should be needed... maybe to fix net names?
153
    Compile_Ratsnest( NULL, true );
154

155 156
    /* Temporary modification of footprints that are flipped (i.e. on bottom
     * layer) to convert them to non flipped footprints.
charras's avatar
charras committed
157 158 159 160
     *  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
     */
161
    BOARD*  pcb = GetBoard();
162
    MODULE* module;
163

164
    for( module = pcb->m_Modules; module != NULL; module = module->Next() )
165
    {
166
        module->SetFlag( 0 );
167

dickelbeck's avatar
dickelbeck committed
168
        if( module->GetLayer() == LAYER_N_BACK )
169
        {
170 171
            module->Flip( module->GetPosition() );
            module->SetFlag( 1 );
172 173 174
        }
    }

175
    /* Gencad has some mandatory and some optional sections: some importer
176 177
     *  need the padstack section (which is optional) anyway. Also the
     *  order of the section *is* important */
178

179 180
    CreateHeaderInfoData( file, this );     // Gencad header
    CreateBoardSection( file, pcb );        // Board perimeter
181

182 183
    CreatePadsShapesSection( file, pcb );   // Pads and padstacks
    CreateArtworksSection( file );          // Empty but mandatory
184

185
    /* Gencad splits a component info in shape, component and device.
186 187
     *  We don't do any sharing (it would be difficult since each module is
     *  customizable after placement) */
188 189 190
    CreateShapesSection( file, pcb );
    CreateComponentsSection( file, pcb );
    CreateDevicesSection( file, pcb );
191

Dick Hollenbeck's avatar
Dick Hollenbeck committed
192
    // In a similar way the netlist is split in net, track and route
193 194 195
    CreateSignalsSection( file, pcb );
    CreateTracksInfoData( file, pcb );
    CreateRoutesSection( file, pcb );
196 197

    fclose( file );
198
    SetLocaleTo_Default();  // revert to the current locale
199

Dick Hollenbeck's avatar
Dick Hollenbeck committed
200
    // Undo the footprints modifications (flipped footprints)
201
    for( module = pcb->m_Modules; module != NULL; module = module->Next() )
202
    {
203
        if( module->GetFlag() )
204
        {
205 206
            module->Flip( module->GetPosition() );
            module->SetFlag( 0 );
207 208
        }
    }
209 210
}

211

212 213
// Comparator for sorting pads with qsort
static int PadListSortByShape( const void* aRefptr, const void* aObjptr )
214
{
215 216
    const D_PAD* padref = *(D_PAD**) aRefptr;
    const D_PAD* padcmp = *(D_PAD**) aObjptr;
dickelbeck's avatar
dickelbeck committed
217

218
    return D_PAD::Compare( padref, padcmp );
219 220
}

221

222
// Sort vias for uniqueness
223
static int ViaSort( const void* aRefptr, const void* aObjptr )
224
{
225 226
    TRACK* padref = *(TRACK**) aRefptr;
    TRACK* padcmp = *(TRACK**) aObjptr;
227

228 229
    if( padref->GetWidth() != padcmp->GetWidth() )
        return padref->GetWidth() - padcmp->GetWidth();
Dick Hollenbeck's avatar
Dick Hollenbeck committed
230

231
    if( padref->GetDrillValue() != padcmp->GetDrillValue() )
232
        return padref->GetDrillValue() - padcmp->GetDrillValue();
233

234 235
    if( padref->GetLayerMask() != padcmp->GetLayerMask() )
        return padref->GetLayerMask() - padcmp->GetLayerMask();
236 237 238 239

    return 0;
}

240

241 242 243 244 245 246 247 248
// The ARTWORKS section is empty but (officially) mandatory
static void CreateArtworksSection( FILE* aFile )
{
    /* The artworks section is empty */
    fputs( "$ARTWORKS\n", aFile );
    fputs( "$ENDARTWORKS\n\n", aFile );
}

249

Dick Hollenbeck's avatar
Dick Hollenbeck committed
250
// Emit PADS and PADSTACKS. They are sorted and emitted uniquely.
251 252
// Via name is synthesized from their attributes, pads are numbered
static void CreatePadsShapesSection( FILE* aFile, BOARD* aPcb )
253
{
254
    std::vector<D_PAD*> pads;
255 256 257
    std::vector<D_PAD*> padstacks;
    std::vector<TRACK*> vias;
    std::vector<TRACK*> viastacks;
258
    padstacks.resize( 1 ); // We count pads from 1
259

260
    // The master layermask (i.e. the enabled layers) for padstack generation
261
    LAYER_MSK master_layermask = aPcb->GetDesignSettings().GetEnabledLayers();
262

263
    fputs( "$PADS\n", aFile );
264

265
    // Enumerate and sort the pads
266
    if( aPcb->GetPadCount() > 0 )
267
    {
268 269
        pads = aPcb->GetPads();
        qsort( &pads[0], aPcb->GetPadCount(), sizeof( D_PAD* ),
270
               PadListSortByShape );
271
    }
272

273
    // The same for vias
Dick Hollenbeck's avatar
Dick Hollenbeck committed
274 275 276
    for( TRACK* track = aPcb->m_Track; track != NULL; track = track->Next() )
    {
        if( track->Type() == PCB_VIA_T )
277 278 279
        {
            vias.push_back( track );
        }
280
    }
281

282
    qsort( &vias[0], vias.size(), sizeof(TRACK*), ViaSort );
283

284 285
    // Emit vias pads
    TRACK* old_via = 0;
Dick Hollenbeck's avatar
Dick Hollenbeck committed
286
    for( unsigned i = 0; i < vias.size(); i++ )
287
    {
288 289 290 291 292 293 294
        TRACK* via = vias[i];
        if( old_via && 0 == ViaSort( &old_via, &via ) )
            continue;

        old_via = via;
        viastacks.push_back( via );
        fprintf( aFile, "PAD V%d.%d.%X ROUND %g\nCIRCLE 0 0 %g\n",
295
                via->GetWidth(), via->GetDrillValue(),
296
                via->GetLayerMask(),
297
                via->GetDrillValue() / SCALE_FACTOR,
298
                via->GetWidth() / (SCALE_FACTOR * 2) );
299 300 301 302 303
    }

    // Emit component pads
    D_PAD* old_pad = 0;
    int    pad_name_number = 0;
304
    for( unsigned i = 0; i<pads.size(); ++i )
305
    {
306 307
        D_PAD* pad = pads[i];

308
        pad->SetSubRatsnest( pad_name_number );
309

dickelbeck's avatar
dickelbeck committed
310
        if( old_pad && 0==D_PAD::Compare( old_pad, pad ) )
311
            continue;  // already created
312 313 314 315

        old_pad = pad;

        pad_name_number++;
316
        pad->SetSubRatsnest( pad_name_number );
317

318
        fprintf( aFile, "PAD P%d", pad->GetSubRatsnest() );
319

320
        padstacks.push_back( pad ); // Will have its own padstack later
Dick Hollenbeck's avatar
Dick Hollenbeck committed
321 322
        int dx = pad->GetSize().x / 2;
        int dy = pad->GetSize().y / 2;
323

Dick Hollenbeck's avatar
Dick Hollenbeck committed
324
        switch( pad->GetShape() )
325 326
        {
        default:
dickelbeck's avatar
dickelbeck committed
327
        case PAD_CIRCLE:
Dick Hollenbeck's avatar
Dick Hollenbeck committed
328
            fprintf( aFile, " ROUND %g\n",
Dick Hollenbeck's avatar
Dick Hollenbeck committed
329
                     pad->GetDrillSize().x / SCALE_FACTOR );
330
            /* Circle is center, radius */
331
            fprintf( aFile, "CIRCLE %g %g %g\n",
Dick Hollenbeck's avatar
Dick Hollenbeck committed
332 333 334
                    pad->GetOffset().x / SCALE_FACTOR,
                    -pad->GetOffset().y / SCALE_FACTOR,
                    pad->GetSize().x / (SCALE_FACTOR * 2) );
335 336
            break;

dickelbeck's avatar
dickelbeck committed
337
        case PAD_RECT:
Dick Hollenbeck's avatar
Dick Hollenbeck committed
338
            fprintf( aFile, " RECTANGULAR %g\n",
Dick Hollenbeck's avatar
Dick Hollenbeck committed
339
                     pad->GetDrillSize().x / SCALE_FACTOR );
340 341

            // Rectangle is begin, size *not* begin, end!
342
            fprintf( aFile, "RECTANGLE %g %g %g %g\n",
Dick Hollenbeck's avatar
Dick Hollenbeck committed
343 344
                    (-dx + pad->GetOffset().x ) / SCALE_FACTOR,
                    (-dy - pad->GetOffset().y ) / SCALE_FACTOR,
345
                    dx / (SCALE_FACTOR / 2), dy / (SCALE_FACTOR / 2) );
346 347
            break;

Dick Hollenbeck's avatar
Dick Hollenbeck committed
348
        case PAD_OVAL:     // Create outline by 2 lines and 2 arcs
349
        {
350
            // OrCAD Layout call them OVAL or OBLONG - GenCAD call them FINGERs
Dick Hollenbeck's avatar
Dick Hollenbeck committed
351
            fprintf( aFile, " FINGER %g\n",
Dick Hollenbeck's avatar
Dick Hollenbeck committed
352
                     pad->GetDrillSize().x / SCALE_FACTOR );
353
            int dr = dx - dy;
354

charras's avatar
charras committed
355
            if( dr >= 0 )       // Horizontal oval
356
            {
357
                int radius = dy;
358
                fprintf( aFile, "LINE %g %g %g %g\n",
Dick Hollenbeck's avatar
Dick Hollenbeck committed
359 360 361 362
                         (-dr + pad->GetOffset().x) / SCALE_FACTOR,
                         (-pad->GetOffset().y - radius) / SCALE_FACTOR,
                         (dr + pad->GetOffset().x ) / SCALE_FACTOR,
                         (-pad->GetOffset().y - radius) / SCALE_FACTOR );
363 364

                // GenCAD arcs are (start, end, center)
365
                fprintf( aFile, "ARC %g %g %g %g %g %g\n",
Dick Hollenbeck's avatar
Dick Hollenbeck committed
366 367 368 369 370 371
                         (dr + pad->GetOffset().x) / SCALE_FACTOR,
                         (-pad->GetOffset().y - radius) / SCALE_FACTOR,
                         (dr + pad->GetOffset().x) / SCALE_FACTOR,
                         (-pad->GetOffset().y + radius) / SCALE_FACTOR,
                         (dr + pad->GetOffset().x) / SCALE_FACTOR,
                         -pad->GetOffset().y / SCALE_FACTOR );
372 373

                fprintf( aFile, "LINE %g %g %g %g\n",
Dick Hollenbeck's avatar
Dick Hollenbeck committed
374 375 376 377
                         (dr + pad->GetOffset().x) / SCALE_FACTOR,
                         (-pad->GetOffset().y + radius) / SCALE_FACTOR,
                         (-dr + pad->GetOffset().x) / SCALE_FACTOR,
                         (-pad->GetOffset().y + radius) / SCALE_FACTOR );
378
                fprintf( aFile, "ARC %g %g %g %g %g %g\n",
Dick Hollenbeck's avatar
Dick Hollenbeck committed
379 380 381 382 383 384
                         (-dr + pad->GetOffset().x) / SCALE_FACTOR,
                         (-pad->GetOffset().y + radius) / SCALE_FACTOR,
                         (-dr + pad->GetOffset().x) / SCALE_FACTOR,
                         (-pad->GetOffset().y - radius) / SCALE_FACTOR,
                         (-dr + pad->GetOffset().x) / SCALE_FACTOR,
                         -pad->GetOffset().y / SCALE_FACTOR );
385
            }
charras's avatar
charras committed
386
            else        // Vertical oval
387 388
            {
                dr = -dr;
389
                int radius = dx;
390
                fprintf( aFile, "LINE %g %g %g %g\n",
Dick Hollenbeck's avatar
Dick Hollenbeck committed
391 392 393 394
                         (-radius + pad->GetOffset().x) / SCALE_FACTOR,
                         (-pad->GetOffset().y - dr) / SCALE_FACTOR,
                         (-radius + pad->GetOffset().x ) / SCALE_FACTOR,
                         (-pad->GetOffset().y + dr) / SCALE_FACTOR );
395
                fprintf( aFile, "ARC %g %g %g %g %g %g\n",
Dick Hollenbeck's avatar
Dick Hollenbeck committed
396 397 398 399 400 401
                         (-radius + pad->GetOffset().x ) / SCALE_FACTOR,
                         (-pad->GetOffset().y + dr) / SCALE_FACTOR,
                         (radius + pad->GetOffset().x ) / SCALE_FACTOR,
                         (-pad->GetOffset().y + dr) / SCALE_FACTOR,
                         pad->GetOffset().x / SCALE_FACTOR,
                         (-pad->GetOffset().y + dr) / SCALE_FACTOR );
402 403

                fprintf( aFile, "LINE %g %g %g %g\n",
Dick Hollenbeck's avatar
Dick Hollenbeck committed
404 405 406 407
                         (radius + pad->GetOffset().x) / SCALE_FACTOR,
                         (-pad->GetOffset().y + dr) / SCALE_FACTOR,
                         (radius + pad->GetOffset().x) / SCALE_FACTOR,
                         (-pad->GetOffset().y - dr) / SCALE_FACTOR );
408
                fprintf( aFile, "ARC %g %g %g %g %g %g\n",
Dick Hollenbeck's avatar
Dick Hollenbeck committed
409 410 411 412 413 414
                         (radius + pad->GetOffset().x) / SCALE_FACTOR,
                         (-pad->GetOffset().y - dr) / SCALE_FACTOR,
                         (-radius + pad->GetOffset().x) / SCALE_FACTOR,
                         (-pad->GetOffset().y - dr) / SCALE_FACTOR,
                         pad->GetOffset().x / SCALE_FACTOR,
                         (-pad->GetOffset().y - dr) / SCALE_FACTOR );
415 416 417 418
            }
            break;
        }

dickelbeck's avatar
dickelbeck committed
419
        case PAD_TRAPEZOID:
Dick Hollenbeck's avatar
Dick Hollenbeck committed
420
            fprintf( aFile, " POLYGON %g\n",
Dick Hollenbeck's avatar
Dick Hollenbeck committed
421
                     pad->GetDrillSize().x / SCALE_FACTOR );
422 423

            // XXX TO BE IMPLEMENTED! and I don't know if it could be actually imported by something
424 425 426
            break;
        }
    }
427

428
    fputs( "\n$ENDPADS\n\n", aFile );
429

430 431
    // Now emit the padstacks definitions, using the combined layer masks
    fputs( "$PADSTACKS\n", aFile );
432

433 434 435
    // Via padstacks
    for( unsigned i = 0; i < viastacks.size(); i++ )
    {
436
        TRACK*   via  = viastacks[i];
437
        LAYER_MSK mask = via->GetLayerMask() & master_layermask;
438
        fprintf( aFile, "PADSTACK VIA%d.%d.%X %g\n",
439
                 via->GetWidth(), via->GetDrillValue(), mask,
440
                 via->GetDrillValue() / SCALE_FACTOR );
Dick Hollenbeck's avatar
Dick Hollenbeck committed
441

442
        for( LAYER_NUM layer = FIRST_LAYER; layer < NB_LAYERS; ++layer )
Dick Hollenbeck's avatar
Dick Hollenbeck committed
443
        {
444
            if( mask & GetLayerMask( layer ) )
445 446
            {
                fprintf( aFile, "PAD V%d.%d.%X %s 0 0\n",
447
                        via->GetWidth(), via->GetDrillValue(),
448 449 450
                        mask,
                        TO_UTF8( GenCADLayerName[layer] ) );
            }
Dick Hollenbeck's avatar
Dick Hollenbeck committed
451
        }
452
    }
453

Dick Hollenbeck's avatar
Dick Hollenbeck committed
454
    /* Component padstacks
455 456 457 458
     *  CAM350 don't apply correctly the FLIP semantics for padstacks, i.e. doesn't
     *  swap the top and bottom layers... so I need to define the shape as MIRRORX
     *  and define a separate 'flipped' padstack... until it appears yet another
     *  noncompliant importer */
459 460
    for( unsigned i = 1; i < padstacks.size(); i++ )
    {
461
        D_PAD* pad = padstacks[i];
462

463 464
        // Straight padstack
        fprintf( aFile, "PADSTACK PAD%d %g\n", i,
Dick Hollenbeck's avatar
Dick Hollenbeck committed
465
                 pad->GetDrillSize().x / SCALE_FACTOR );
466
        for( LAYER_NUM layer = FIRST_LAYER; layer < NB_LAYERS; ++layer )
Dick Hollenbeck's avatar
Dick Hollenbeck committed
467
        {
468
            if( pad->GetLayerMask() & GetLayerMask( layer ) & master_layermask )
469 470 471 472
            {
                fprintf( aFile, "PAD P%d %s 0 0\n", i,
                        TO_UTF8( GenCADLayerName[layer] ) );
            }
Dick Hollenbeck's avatar
Dick Hollenbeck committed
473 474
        }

475 476
        // Flipped padstack
        fprintf( aFile, "PADSTACK PAD%dF %g\n", i,
Dick Hollenbeck's avatar
Dick Hollenbeck committed
477
                 pad->GetDrillSize().x / SCALE_FACTOR );
478
        for( LAYER_NUM layer = FIRST_LAYER; layer < NB_LAYERS; ++layer )
479
        {
480
            if( pad->GetLayerMask() & GetLayerMask( layer ) & master_layermask )
481 482 483 484
            {
                fprintf( aFile, "PAD P%d %s 0 0\n", i,
                        TO_UTF8( GenCADLayerNameFlipped[layer] ) );
            }
Dick Hollenbeck's avatar
Dick Hollenbeck committed
485 486
        }
    }
487

488
    fputs( "$ENDPADSTACKS\n\n", aFile );
489 490 491
}


charras's avatar
charras committed
492
/* Creates the footprint shape list.
493 494
 * Since module shape is customizable after the placement we cannot share them;
 * instead we opt for the one-module-one-shape-one-component-one-device approach
495
 */
496
static void CreateShapesSection( FILE* aFile, BOARD* aPcb )
497
{
charras's avatar
charras committed
498 499 500 501 502
    MODULE*     module;
    D_PAD*      pad;
    const char* layer;
    wxString    pinname;
    const char* mirror = "0";
503

504
    fputs( "$SHAPES\n", aFile );
505

506
    for( module = aPcb->m_Modules; module != NULL; module = module->Next() )
507
    {
508
        FootprintWriteShape( aFile, module );
509

510
        for( pad = module->Pads(); pad != NULL; pad = pad->Next() )
511
        {
512 513 514 515 516
            /* Funny thing: GenCAD requires the pad side even if you use
             *  padstacks (which are theorically optional but gerbtools
             *requires* them). Now the trouble thing is that 'BOTTOM'
             *  is interpreted by someone as a padstack flip even
             *  if the spec explicitly says it's not... */
517
            layer = "ALL";
518

Dick Hollenbeck's avatar
Dick Hollenbeck committed
519
            if( ( pad->GetLayerMask() & ALL_CU_LAYERS ) == LAYER_BACK )
520
            {
521
                layer = ( module->GetFlag() ) ? "TOP" : "BOTTOM";
522
            }
Dick Hollenbeck's avatar
Dick Hollenbeck committed
523
            else if( ( pad->GetLayerMask() & ALL_CU_LAYERS ) == LAYER_FRONT )
524
            {
525
                layer = ( module->GetFlag() ) ? "BOTTOM" : "TOP";
526 527 528
            }

            pad->ReturnStringPadName( pinname );
529

530
            if( pinname.IsEmpty() )
531
                pinname = wxT( "none" );
532

533
            double orient = pad->GetOrientation() - module->GetOrientation();
534
            NORMALIZE_ANGLE_POS( orient );
535

536
            // Bottom side modules use the flipped padstack
537
            fprintf( aFile, (module->GetFlag()) ?
538 539 540
                     "PIN %s PAD%dF %g %g %s %g %s\n" :
                     "PIN %s PAD%d %g %g %s %g %s\n",
                     TO_UTF8( pinname ), pad->GetSubRatsnest(),
Dick Hollenbeck's avatar
Dick Hollenbeck committed
541 542
                     pad->GetPos0().x / SCALE_FACTOR,
                     -pad->GetPos0().y / SCALE_FACTOR,
543
                     layer, orient / 10.0, mirror );
544 545 546
        }
    }

547
    fputs( "$ENDSHAPES\n\n", aFile );
548 549
}

550

charras's avatar
charras committed
551
/* Creates the section $COMPONENTS (Footprints placement)
552 553 554
 * Bottom side components are difficult to handle: shapes must be mirrored or
 * flipped, silk layers need to be handled correctly and so on. Also it seems
 * that *noone* follows the specs...
555
 */
556
static void CreateComponentsSection( FILE* aFile, BOARD* aPcb )
557
{
558
    fputs( "$COMPONENTS\n", aFile );
559

560
    for( MODULE* module = aPcb->m_Modules; module != NULL; module = module->Next() )
561
    {
562 563 564
        TEXTE_MODULE* textmod;
        const char*   mirror;
        const char*   flip;
565
        double        orient = module->GetOrientation();
566

567
        if( module->GetFlag() )
568
        {
Dick Hollenbeck's avatar
Dick Hollenbeck committed
569 570
            mirror = "0";
            flip   = "FLIP";
571 572 573 574 575 576 577 578
            NEGATE_AND_NORMALIZE_ANGLE_POS( orient );
        }
        else
        {
            mirror = "0";
            flip   = "0";
        }

Dick Hollenbeck's avatar
Dick Hollenbeck committed
579
        fprintf( aFile, "\nCOMPONENT %s\n",
580
                 TO_UTF8( module->GetReference() ) );
Dick Hollenbeck's avatar
Dick Hollenbeck committed
581
        fprintf( aFile, "DEVICE %s_%s\n",
582 583
                 TO_UTF8( module->GetReference() ),
                 TO_UTF8( module->GetValue() ) );
Dick Hollenbeck's avatar
Dick Hollenbeck committed
584
        fprintf( aFile, "PLACE %g %g\n",
585 586
                 MapXTo( module->GetPosition().x ),
                 MapYTo( module->GetPosition().y ) );
Dick Hollenbeck's avatar
Dick Hollenbeck committed
587
        fprintf( aFile, "LAYER %s\n",
588
                 (module->GetFlag()) ? "BOTTOM" : "TOP" );
Dick Hollenbeck's avatar
Dick Hollenbeck committed
589
        fprintf( aFile, "ROTATION %g\n",
590
                 orient / 10.0 );
Dick Hollenbeck's avatar
Dick Hollenbeck committed
591
        fprintf( aFile, "SHAPE %s %s %s\n",
592
                 TO_UTF8( module->GetReference() ),
593
                 mirror, flip );
594 595

        // Text on silk layer: RefDes and value (are they actually useful?)
596
        textmod = &module->Reference();
597 598

        for( int ii = 0; ii < 2; ii++ )
599
        {
600
            double   orient = textmod->GetOrientation();
601
            wxString layer  = GenCADLayerName[(module->GetFlag()) ?
602
                                              SILKSCREEN_N_BACK : SILKSCREEN_N_FRONT];
603 604

            fprintf( aFile, "TEXT %g %g %g %g %s %s \"%s\"",
605
                     textmod->GetPos0().x / SCALE_FACTOR,
Dick Hollenbeck's avatar
Dick Hollenbeck committed
606
                    -textmod->GetPos0().y / SCALE_FACTOR,
607 608 609 610 611
                     textmod->GetSize().x / SCALE_FACTOR,
                     orient / 10.0,
                     mirror,
                     TO_UTF8( layer ),
                     TO_UTF8( textmod->GetText() ) );
612 613

            // Please note, the width is approx
614
            fprintf( aFile, " 0 0 %g %g\n",
615
                     ( textmod->GetSize().x * textmod->GetLength() ) / SCALE_FACTOR,
Dick Hollenbeck's avatar
Dick Hollenbeck committed
616
                     textmod->GetSize().y / SCALE_FACTOR );
617

618
            textmod = &module->Value(); // Dirty trick for the second iteration
619 620
        }

621 622
        // The SHEET is a 'generic description' for referencing the component
        fprintf( aFile, "SHEET \"RefDes: %s, Value: %s\"\n",
623 624
                 TO_UTF8( module->GetReference() ),
                 TO_UTF8( module->GetValue() ) );
625 626
    }

627
    fputs( "$ENDCOMPONENTS\n\n", aFile );
628 629
}

630

631 632 633
/* Emit the netlist (which is actually the thing for which GenCAD is used these
 * days!); tracks are handled later */
static void CreateSignalsSection( FILE* aFile, BOARD* aPcb )
634
{
charras's avatar
charras committed
635
    wxString      msg;
636
    NETINFO_ITEM* net;
charras's avatar
charras committed
637 638 639
    D_PAD*        pad;
    MODULE*       module;
    int           NbNoConn = 1;
640

641
    fputs( "$SIGNALS\n", aFile );
642

643
    for( unsigned ii = 0; ii < aPcb->GetNetCount(); ii++ )
644
    {
645
        net = aPcb->FindNet( ii );
646 647

        if( net->GetNetname() == wxEmptyString ) // dummy netlist (no connection)
648
        {
649
            wxString msg; msg << wxT( "NoConnection" ) << NbNoConn++;
650
            net->SetNetname( msg );
651 652
        }

653
        if( net->GetNet() <= 0 )  // dummy netlist (no connection)
654 655
            continue;

656
        msg = wxT( "SIGNAL " ) + net->GetNetname();
657

658 659
        fputs( TO_UTF8( msg ), aFile );
        fputs( "\n", aFile );
660

661
        for( module = aPcb->m_Modules; module != NULL; module = module->Next() )
662
        {
663
            for( pad = module->Pads(); pad != NULL; pad = pad->Next() )
664 665
            {
                wxString padname;
666

667
                if( pad->GetNet() != net->GetNet() )
668
                    continue;
669

670
                pad->ReturnStringPadName( padname );
671
                msg.Printf( wxT( "NODE %s %s" ),
672 673
                            GetChars( module->GetReference() ),
                            GetChars( padname ) );
674

675 676
                fputs( TO_UTF8( msg ), aFile );
                fputs( "\n", aFile );
677 678 679 680
            }
        }
    }

681
    fputs( "$ENDSIGNALS\n\n", aFile );
682 683
}

684

685
// Creates the header section
686
static bool CreateHeaderInfoData( FILE* aFile, PCB_EDIT_FRAME* aFrame )
687
{
688
    wxString    msg;
689
    BOARD *board = aFrame->GetBoard();
690 691 692 693 694 695

    fputs( "$HEADER\n", aFile );
    fputs( "GENCAD 1.4\n", aFile );

    // Please note: GenCAD syntax requires quoted strings if they can contain spaces
    msg.Printf( wxT( "USER \"%s %s\"\n" ),
696 697
               GetChars( wxGetApp().GetAppName() ),
               GetChars( GetBuildVersion() ) );
Dick Hollenbeck's avatar
Dick Hollenbeck committed
698
    fputs( TO_UTF8( msg ), aFile );
699

700
    msg = wxT( "DRAWING \"" ) + board->GetFileName() + wxT( "\"\n" );
Dick Hollenbeck's avatar
Dick Hollenbeck committed
701
    fputs( TO_UTF8( msg ), aFile );
702 703 704 705 706

    const TITLE_BLOCK&  tb = aFrame->GetTitleBlock();

    msg = wxT( "REVISION \"" ) + tb.GetRevision() + wxT( " " ) + tb.GetDate() + wxT( "\"\n" );

707
    fputs( TO_UTF8( msg ), aFile );
708
    fputs( "UNITS INCH\n", aFile );
709

Dick Hollenbeck's avatar
Dick Hollenbeck committed
710
    msg.Printf( wxT( "ORIGIN %g %g\n" ),
711 712
                MapXTo( aFrame->GetOriginAxisPosition().x ),
                MapYTo( aFrame->GetOriginAxisPosition().y ) );
713
    fputs( TO_UTF8( msg ), aFile );
714

715 716
    fputs( "INTERTRACK 0\n", aFile );
    fputs( "$ENDHEADER\n\n", aFile );
717

718
    return true;
719 720
}

721

722
/*
charras's avatar
charras committed
723 724
 *  Sort function used to sort tracks segments:
 *   items are sorted by netcode, then by width then by layer
725
 */
726
static int TrackListSortByNetcode( const void* refptr, const void* objptr )
727
{
728 729 730 731 732
    const TRACK* ref, * cmp;
    int          diff;

    ref = *( (TRACK**) refptr );
    cmp = *( (TRACK**) objptr );
733

charras's avatar
charras committed
734
    if( ( diff = ref->GetNet() - cmp->GetNet() ) )
735
        return diff;
736

737
    if( ( diff = ref->GetWidth() - cmp->GetWidth() ) )
738
        return diff;
739

charras's avatar
charras committed
740
    if( ( diff = ref->GetLayer() - cmp->GetLayer() ) )
741 742 743
        return diff;

    return 0;
744
}
745 746


747 748
/* Creates the section ROUTES
 * that handles tracks, vias
charras's avatar
charras committed
749
 * TODO: add zones
750 751 752 753
 *  section:
 *  $ROUTE
 *  ...
 *  $ENROUTE
charras's avatar
charras committed
754
 *  Track segments must be sorted by nets
755
 */
756
static void CreateRoutesSection( FILE* aFile, BOARD* aPcb )
757
{
758 759 760 761
    TRACK*   track, ** tracklist;
    int      vianum = 1;
    int      old_netcode, old_width, old_layer;
    int      nbitems, ii;
762
    LAYER_MSK master_layermask = aPcb->GetDesignSettings().GetEnabledLayers();
763

charras's avatar
charras committed
764
    // Count items
765
    nbitems = 0;
766

767
    for( track = aPcb->m_Track; track != NULL; track = track->Next() )
768 769
        nbitems++;

770
    for( track = aPcb->m_Zone; track != NULL; track = track->Next() )
771
    {
772
        if( track->Type() == PCB_ZONE_T )
773 774 775
            nbitems++;
    }

776
    tracklist = (TRACK**) operator new( (nbitems + 1)* sizeof( TRACK* ) );
777 778

    nbitems = 0;
779

780
    for( track = aPcb->m_Track; track != NULL; track = track->Next() )
781 782
        tracklist[nbitems++] = track;

783
    for( track = aPcb->m_Zone; track != NULL; track = track->Next() )
784
    {
785
        if( track->Type() == PCB_ZONE_T )
786 787 788 789 790
            tracklist[nbitems++] = track;
    }

    tracklist[nbitems] = NULL;

791
    qsort( tracklist, nbitems, sizeof(TRACK*), TrackListSortByNetcode );
792

793
    fputs( "$ROUTES\n", aFile );
794 795

    old_netcode = -1; old_width = -1; old_layer = -1;
796

797 798 799
    for( ii = 0; ii < nbitems; ii++ )
    {
        track = tracklist[ii];
800

801
        if( old_netcode != track->GetNet() )
802
        {
803
            old_netcode = track->GetNet();
804
            NETINFO_ITEM* net = aPcb->FindNet( track->GetNet() );
charras's avatar
charras committed
805
            wxString      netname;
806

807 808
            if( net && (net->GetNetname() != wxEmptyString) )
                netname = net->GetNetname();
809 810
            else
                netname = wxT( "_noname_" );
811

812
            fprintf( aFile, "ROUTE %s\n", TO_UTF8( netname ) );
813 814
        }

815
        if( old_width != track->GetWidth() )
816
        {
817 818
            old_width = track->GetWidth();
            fprintf( aFile, "TRACK TRACK%d\n", track->GetWidth() );
819 820
        }

821
        if( (track->Type() == PCB_TRACE_T) || (track->Type() == PCB_ZONE_T) )
822 823 824 825
        {
            if( old_layer != track->GetLayer() )
            {
                old_layer = track->GetLayer();
826
                fprintf( aFile, "LAYER %s\n",
827
                        TO_UTF8( GenCADLayerName[track->GetLayer() & 0x1F] ) );
828 829
            }

830
            fprintf( aFile, "LINE %g %g %g %g\n",
831 832
                    MapXTo( track->GetStart().x ), MapYTo( track->GetStart().y ),
                    MapXTo( track->GetEnd().x ), MapYTo( track->GetEnd().y ) );
833
        }
834
        if( track->Type() == PCB_VIA_T )
835
        {
836
            fprintf( aFile, "VIA VIA%d.%d.%X %g %g ALL %g via%d\n",
837
                     track->GetWidth(), track->GetDrillValue(),
838
                     track->GetLayerMask() & master_layermask,
839
                     MapXTo( track->GetStart().x ), MapYTo( track->GetStart().y ),
840
                     track->GetDrillValue() / SCALE_FACTOR, vianum++ );
841 842 843
        }
    }

844
    fputs( "$ENDROUTES\n\n", aFile );
845

846
    delete tracklist;
847 848 849
}


850
/* Creates the section $DEVICES
charras's avatar
charras committed
851 852
 * This is a list of footprints properties
 *  ( Shapes are in section $SHAPE )
853
 */
854
static void CreateDevicesSection( FILE* aFile, BOARD* aPcb )
855
{
856 857
    MODULE* module;

858
    fputs( "$DEVICES\n", aFile );
859

860
    for( module = aPcb->m_Modules; module != NULL; module = module->Next() )
861
    {
862 863 864
        fprintf( aFile, "DEVICE \"%s\"\n", TO_UTF8( module->GetReference() ) );
        fprintf( aFile, "PART \"%s\"\n", TO_UTF8( module->GetValue() ) );
        fprintf( aFile, "PACKAGE \"%s\"\n", TO_UTF8( module->GetLibRef() ) );
865 866 867

        // The TYPE attribute is almost freeform
        const char* ty = "TH";
868
        if( module->GetAttributes() & MOD_CMS )
869
            ty = "SMD";
870
        if( module->GetAttributes() & MOD_VIRTUAL )
871 872
            ty = "VIRTUAL";
        fprintf( aFile, "TYPE %s\n", ty );
873 874
    }

875
    fputs( "$ENDDEVICES\n\n", aFile );
876 877 878
}


879
/* Creates the section $BOARD.
880
 *  We output here only the board perimeter
881
 */
882
static void CreateBoardSection( FILE* aFile, BOARD* aPcb )
883
{
884
    fputs( "$BOARD\n", aFile );
885

Dick Hollenbeck's avatar
Dick Hollenbeck committed
886
    // Extract the board edges
887
    for( EDA_ITEM* drawing = aPcb->m_Drawings;
Dick Hollenbeck's avatar
Dick Hollenbeck committed
888 889
        drawing != 0;
        drawing = drawing->Next() )
890
    {
891
        if( drawing->Type() == PCB_LINE_T )
Dick Hollenbeck's avatar
Dick Hollenbeck committed
892
        {
893 894 895 896 897
            DRAWSEGMENT* drawseg = dynamic_cast<DRAWSEGMENT*>( drawing );
            if( drawseg->GetLayer() == EDGE_N )
            {
                // XXX GenCAD supports arc boundaries but I've seen nothing that reads them
                fprintf( aFile, "LINE %g %g %g %g\n",
Dick Hollenbeck's avatar
Dick Hollenbeck committed
898 899
                        MapXTo( drawseg->GetStart().x ), MapYTo( drawseg->GetStart().y ),
                        MapXTo( drawseg->GetEnd().x ), MapYTo( drawseg->GetEnd().y ) );
900
            }
Dick Hollenbeck's avatar
Dick Hollenbeck committed
901 902
        }
    }
903

904
    fputs( "$ENDBOARD\n\n", aFile );
905 906 907
}


charras's avatar
charras committed
908 909
/* Creates the section "$TRACKS"
 *  This sections give the list of widths (tools) used in tracks and vias
910 911 912 913
 *  format:
 *  $TRACK
 *  TRACK <name> <width>
 *  $ENDTRACK
914
 *
charras's avatar
charras committed
915 916
 *  Each tool name is build like this: "TRACK" + track width.
 *  For instance for a width = 120 : name = "TRACK120".
917
 */
918
static void CreateTracksInfoData( FILE* aFile, BOARD* aPcb )
919
{
920
    TRACK* track;
charras's avatar
charras committed
921
    int    last_width = -1;
922

923 924
    // Find thickness used for traces
    // XXX could use the same sorting approach used for pads
925

charras's avatar
charras committed
926
    std::vector <int> trackinfo;
927

charras's avatar
charras committed
928
    unsigned          ii;
929

930
    for( track = aPcb->m_Track; track != NULL; track = track->Next() )
931
    {
932
        if( last_width != track->GetWidth() ) // Find a thickness already used.
933
        {
934
            for( ii = 0; ii < trackinfo.size(); ii++ )
935
            {
936
                if( trackinfo[ii] == track->GetWidth() )
charras's avatar
charras committed
937
                    break;
938 939
            }

charras's avatar
charras committed
940
            if( ii == trackinfo.size() )    // not found
941
                trackinfo.push_back( track->GetWidth() );
charras's avatar
charras committed
942

943
            last_width = track->GetWidth();
charras's avatar
charras committed
944
        }
945 946
    }

947
    for( track = aPcb->m_Zone; track != NULL; track = track->Next() )
948
    {
949
        if( last_width != track->GetWidth() ) // Find a thickness already used.
950
        {
951
            for( ii = 0; ii < trackinfo.size(); ii++ )
952
            {
953
                if( trackinfo[ii] == track->GetWidth() )
charras's avatar
charras committed
954
                    break;
955 956
            }

charras's avatar
charras committed
957
            if( ii == trackinfo.size() )    // not found
958
                trackinfo.push_back( track->GetWidth() );
charras's avatar
charras committed
959

960
            last_width = track->GetWidth();
961 962 963 964
        }
    }

    // Write data
965
    fputs( "$TRACKS\n", aFile );
966

967
    for( ii = 0; ii < trackinfo.size(); ii++ )
968
    {
Dick Hollenbeck's avatar
Dick Hollenbeck committed
969
        fprintf( aFile, "TRACK TRACK%d %g\n", trackinfo[ii],
970
                 trackinfo[ii] / SCALE_FACTOR );
971 972
    }

973
    fputs( "$ENDTRACKS\n\n", aFile );
974 975 976
}


charras's avatar
charras committed
977 978
/* Creates the shape of a footprint (section SHAPE)
 *  The shape is always given "normal" (Orient 0, not mirrored)
979 980
 * It's almost guaranteed that the silk layer will be imported wrong but
 * the shape also contains the pads!
981
 */
982
static void FootprintWriteShape( FILE* aFile, MODULE* module )
983
{
984 985 986
    EDGE_MODULE* PtEdge;
    EDA_ITEM*    PtStruct;

987
    // Control Y axis change sign for flipped modules
988
    int          Yaxis_sign = -1;
989

Dick Hollenbeck's avatar
Dick Hollenbeck committed
990
    // Flip for bottom side components
991
    if( module->GetFlag() )
992
        Yaxis_sign = 1;
993

charras's avatar
charras committed
994
    /* creates header: */
995
    fprintf( aFile, "\nSHAPE %s\n", TO_UTF8( module->GetReference() ) );
996

997
    if( module->GetAttributes() & MOD_VIRTUAL )
998
    {
999
        fprintf( aFile, "INSERT SMD\n" );
1000 1001 1002
    }
    else
    {
1003
        if( module->GetAttributes() & MOD_CMS )
1004 1005
        {
            fprintf( aFile, "INSERT SMD\n" );
1006 1007 1008 1009 1010 1011 1012 1013 1014
        }
        else
        {
            fprintf( aFile, "INSERT TH\n" );
        }
    }

#if 0 /* ATTRIBUTE name and value is unspecified and the original exporter
       * got the syntax wrong, so CAM350 rejected the whole shape! */
1015 1016 1017

    if( module->m_Attributs != MOD_DEFAULT )
    {
1018
        fprintf( aFile, "ATTRIBUTE" );
1019

1020
        if( module->m_Attributs & MOD_CMS )
1021
            fprintf( aFile, " PAD_SMD" );
1022

1023
        if( module->m_Attributs & MOD_VIRTUAL )
1024
            fprintf( aFile, " VIRTUAL" );
1025

1026
        fprintf( aFile, "\n" );
1027
    }
1028
#endif
1029

1030 1031 1032 1033
    // Silk outline; wildly interpreted by various importers:
    // CAM350 read it right but only closed shapes
    // ProntoPlace double-flip it (at least the pads are correct)
    // GerberTool usually get it right...
1034
    for( PtStruct = module->GraphicalItems(); PtStruct != NULL; PtStruct = PtStruct->Next() )
1035
    {
1036
        switch( PtStruct->Type() )
1037
        {
1038
        case PCB_MODULE_TEXT_T:
1039 1040

            // If we wanted to export text, this is not the correct section
1041 1042
            break;

1043
        case PCB_MODULE_EDGE_T:
1044
            PtEdge = (EDGE_MODULE*) PtStruct;
Dick Hollenbeck's avatar
Dick Hollenbeck committed
1045
            if( PtEdge->GetLayer() == SILKSCREEN_N_FRONT
1046
                || PtEdge->GetLayer() == SILKSCREEN_N_BACK )
1047
            {
Dick Hollenbeck's avatar
Dick Hollenbeck committed
1048
                switch( PtEdge->GetShape() )
1049 1050
                {
                case S_SEGMENT:
1051
                    fprintf( aFile, "LINE %g %g %g %g\n",
1052 1053 1054 1055 1056
                             (PtEdge->m_Start0.x) / SCALE_FACTOR,
                             (Yaxis_sign * PtEdge->m_Start0.y) / SCALE_FACTOR,
                             (PtEdge->m_End0.x) / SCALE_FACTOR,
                             (Yaxis_sign * PtEdge->m_End0.y ) / SCALE_FACTOR );
                    break;
1057

1058 1059
                case S_CIRCLE:
                {
1060 1061
                    int radius = KiROUND( GetLineLength( PtEdge->m_End0,
                                                         PtEdge->m_Start0 ) );
1062 1063 1064 1065 1066 1067
                    fprintf( aFile, "CIRCLE %g %g %g\n",
                             PtEdge->m_Start0.x / SCALE_FACTOR,
                             Yaxis_sign * PtEdge->m_Start0.y / SCALE_FACTOR,
                             radius / SCALE_FACTOR );
                    break;
                }
1068

Dick Hollenbeck's avatar
Dick Hollenbeck committed
1069
                case S_ARC:
1070 1071 1072 1073
                {
                    int arcendx, arcendy;
                    arcendx = PtEdge->m_End0.x - PtEdge->m_Start0.x;
                    arcendy = PtEdge->m_End0.y - PtEdge->m_Start0.y;
Dick Hollenbeck's avatar
Dick Hollenbeck committed
1074 1075 1076
                    RotatePoint( &arcendx, &arcendy, -PtEdge->GetAngle() );
                    arcendx += PtEdge->GetStart0().x;
                    arcendy += PtEdge->GetStart0().y;
1077
                    if( Yaxis_sign == -1 )
1078
                    {
1079 1080 1081 1082 1083
                        // Flipping Y flips the arc direction too
                        fprintf( aFile, "ARC %g %g %g %g %g %g\n",
                                 (arcendx) / SCALE_FACTOR,
                                 (Yaxis_sign * arcendy) / SCALE_FACTOR,
                                 (PtEdge->m_End0.x) / SCALE_FACTOR,
Dick Hollenbeck's avatar
Dick Hollenbeck committed
1084 1085 1086
                                 (Yaxis_sign * PtEdge->GetEnd0().y) / SCALE_FACTOR,
                                 (PtEdge->GetStart0().x) / SCALE_FACTOR,
                                 (Yaxis_sign * PtEdge->GetStart0().y) / SCALE_FACTOR );
1087 1088 1089 1090
                    }
                    else
                    {
                        fprintf( aFile, "ARC %g %g %g %g %g %g\n",
Dick Hollenbeck's avatar
Dick Hollenbeck committed
1091 1092
                                 (PtEdge->GetEnd0().x) / SCALE_FACTOR,
                                 (Yaxis_sign * PtEdge->GetEnd0().y) / SCALE_FACTOR,
1093 1094
                                 (arcendx) / SCALE_FACTOR,
                                 (Yaxis_sign * arcendy) / SCALE_FACTOR,
Dick Hollenbeck's avatar
Dick Hollenbeck committed
1095 1096
                                 (PtEdge->GetStart0().x) / SCALE_FACTOR,
                                 (Yaxis_sign * PtEdge->GetStart0().y) / SCALE_FACTOR );
1097 1098 1099
                    }
                    break;
                }
1100

1101 1102 1103
                default:
                    DisplayError( NULL, wxT( "Type Edge Module invalid." ) );
                    break;
Dick Hollenbeck's avatar
Dick Hollenbeck committed
1104
                }
1105
            }
1106 1107 1108 1109
            break;

        default:
            break;
Dick Hollenbeck's avatar
Dick Hollenbeck committed
1110
        }
1111
    }
1112
}