summaryrefslogtreecommitdiff
path: root/pcbnew/dialogs/dialog_SVG_print.cpp
blob: 1fbe7d04fc4b8f057c20ba09115394696a19bba5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/**
 * @file pcbnew/dialogs/dialog_SVG_print.cpp
 */

/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 2012 Jean-Pierre Charras, jp.charras at wanadoo.fr
 * Copyright (C) 2012 KiCad Developers, see change_log.txt for contributors.
 *
 * 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
 */


#include <fctsys.h>
#include <kiface_i.h>
#include <common.h>
#include <class_drawpanel.h>
#include <wxBasePcbFrame.h>
#include <class_pcb_screen.h>
#include <base_units.h>
#include <convert_from_iu.h>
#include <wildcards_and_files_ext.h>
#include <macros.h>
#include <reporter.h>
#include <confirm.h>

#include <pcbnew.h>
#include <pcbplot.h>
#include <printout_controler.h>
#include <class_board.h>
#include <dialog_SVG_print_base.h>
#include <invoke_pcb_dialog.h>
#include <wx_html_report_panel.h>


class DIALOG_SVG_PRINT : public DIALOG_SVG_PRINT_base
{
public:
    DIALOG_SVG_PRINT( wxTopLevelWindow* aParent, BOARD* aBoard, PCB_PLOT_PARAMS* aSettings );

private:
    bool            m_did_print;
    BOARD*          m_board;
    PCB_PLOT_PARAMS* m_callers_params;
    wxConfigBase*   m_config;
    LSET            m_printMaskLayer;
    wxCheckBox*     m_boxSelectLayer[LAYER_ID_COUNT];
    bool            m_printBW;
    wxString        m_outputDirectory;
    bool            m_printMirror;
    bool            m_oneFileOnly;

    void initDialog();

    void OnCloseWindow( wxCloseEvent& event );
    void OnButtonPlot( wxCommandEvent& event );

    void OnButtonCloseClick( wxCommandEvent& event );

    void OnOutputDirectoryBrowseClicked( wxCommandEvent& event );
    void SetPenWidth();
    void ExportSVGFile( bool aOnlyOneFile );

    bool PageIsBoardBoundarySize()
    {
        return m_rbSvgPageSizeOpt->GetSelection() == 2;
    }

    bool PrintPageRef()
    {
        return m_rbSvgPageSizeOpt->GetSelection() == 0;
    }

    bool CreateSVGFile( const wxString& FullFileName, bool aOnlyOneFile );

    LSET getCheckBoxSelectedLayers() const;
};



// Keys for configuration
#define PLOTSVGMODECOLOR_KEY        wxT( "PlotSVGModeColor" )
#define PLOTSVGMODEMIRROR_KEY       wxT( "PlotSVGModeMirror" )
#define PLOTSVGMODEONEFILE_KEY      wxT( "PlotSVGModeOneFile" )
#define PLOTSVGPAGESIZEOPT_KEY      wxT( "PlotSVGPageOpt" )
#define PLOTSVGPLOT_BRD_EDGE_KEY    wxT( "PlotSVGBrdEdge" )

// reasonable values for default pen width
#define WIDTH_MAX_VALUE             (2 * IU_PER_MM)
#define WIDTH_MIN_VALUE             (0.05 * IU_PER_MM)

// Local variables:
static LSET s_SelectedLayers( 4, B_Cu, F_Cu, F_SilkS, B_SilkS );

/*
 * DIALOG_SVG_PRINT functions
 */
DIALOG_SVG_PRINT::DIALOG_SVG_PRINT( wxTopLevelWindow* aParent, BOARD* aBoard, PCB_PLOT_PARAMS* aSettings ) :
    DIALOG_SVG_PRINT_base( aParent ),
    m_did_print( false ),
    m_callers_params( aSettings )
{
    m_board  = aBoard;
    m_config = Kiface().KifaceSettings();

    memset( m_boxSelectLayer, 0, sizeof( m_boxSelectLayer ) );

    initDialog();
    GetSizer()->SetSizeHints( this );
    Centre();
}


void DIALOG_SVG_PRINT::initDialog()
{
    if( m_config )
    {
        m_config->Read( PLOTSVGMODECOLOR_KEY, &m_printBW, false );
        long ltmp;
        m_config->Read( PLOTSVGPAGESIZEOPT_KEY, &ltmp, 0 );
        m_config->Read( PLOTSVGMODEMIRROR_KEY, &m_printMirror, false );
        m_config->Read( PLOTSVGMODEONEFILE_KEY, &m_oneFileOnly, false);
        m_rbSvgPageSizeOpt->SetSelection( ltmp );
        m_config->Read( PLOTSVGPLOT_BRD_EDGE_KEY, &ltmp, 1 );
        m_PrintBoardEdgesCtrl->SetValue( ltmp );
    }

    m_outputDirectory = m_callers_params->GetOutputDirectory();
    m_outputDirectoryName->SetValue( m_outputDirectory );

    m_ModeColorOption->SetSelection( m_printBW ? 1 : 0 );
    m_printMirrorOpt->SetValue( m_printMirror );
    m_rbFileOpt->SetSelection( m_oneFileOnly ? 1 : 0 );

    AddUnitSymbol( *m_TextPenWidth, g_UserUnit );

    m_DialogDefaultPenSize->SetValue( StringFromValue( g_UserUnit, g_DrawDefaultLineThickness ) );

    LSEQ seq = m_board->GetEnabledLayers().UIOrder();

    for(  ;  seq;  ++seq )
    {
        LAYER_ID layer = *seq;

        // The layers in m_boxSelectLayer[] are in LAYER_ID order.  This may be
        // different than the order on screen.
        m_boxSelectLayer[layer] = new wxCheckBox( this, -1, m_board->GetLayerName( layer ) );

        if( s_SelectedLayers[layer] )
            m_boxSelectLayer[layer]->SetValue( true );

        if( IsCopperLayer( layer ) )
            m_CopperLayersBoxSizer->Add( m_boxSelectLayer[layer], 0, wxGROW | wxALL, 1 );
        else
            m_TechnicalBoxSizer->Add( m_boxSelectLayer[layer], 0, wxGROW | wxALL, 1 );
    }

    if( m_config )
    {
        wxString layerKey;

        for( seq.Rewind();  seq;  ++seq )
        {
            bool option;

            LAYER_NUM layer = *seq;

            layerKey.Printf( OPTKEY_LAYERBASE, layer );

            if( m_config->Read( layerKey, &option ) )
                m_boxSelectLayer[layer]->SetValue( option );
        }
    }
}


LSET DIALOG_SVG_PRINT::getCheckBoxSelectedLayers() const
{
    LSET ret;

    // the layers in m_boxSelectLayer[] are in LAYER_ID order.
    for( unsigned layer=0; layer<DIM(m_boxSelectLayer);  ++layer )
    {
        if( m_boxSelectLayer[layer] && m_boxSelectLayer[layer]->GetValue() )
            ret.set( layer );
    }

    return ret;
}


void DIALOG_SVG_PRINT::OnOutputDirectoryBrowseClicked( wxCommandEvent& event )
{
    // Build the absolute path of current output plot directory
    // to preselect it when opening the dialog.
    wxFileName  fn( m_outputDirectoryName->GetValue() );
    wxString    path = Prj().AbsolutePath( m_outputDirectoryName->GetValue() );

    wxDirDialog dirDialog( this, _( "Select Output Directory" ), path );

    if( dirDialog.ShowModal() == wxID_CANCEL )
        return;

    wxFileName      dirName = wxFileName::DirName( dirDialog.GetPath() );

    wxMessageDialog dialog( this, _( "Use a relative path? " ),
                            _( "Plot Output Directory" ),
                            wxYES_NO | wxICON_QUESTION | wxYES_DEFAULT );

    if( dialog.ShowModal() == wxID_YES )
    {
        wxString boardFilePath = Prj().AbsolutePath( m_board->GetFileName() );

        boardFilePath = wxPathOnly( boardFilePath );

        if( !dirName.MakeRelativeTo( boardFilePath ) )
            wxMessageBox( _( "Cannot make path relative (target volume different from board file volume)!" ),
                          _( "Plot Output Directory" ), wxOK | wxICON_ERROR );
    }

    m_outputDirectoryName->SetValue( dirName.GetFullPath() );
    m_outputDirectory = m_outputDirectoryName->GetValue();
}


void DIALOG_SVG_PRINT::SetPenWidth()
{
    int pensize = ValueFromTextCtrl( *m_DialogDefaultPenSize );

    if( pensize > WIDTH_MAX_VALUE )
    {
        pensize = WIDTH_MAX_VALUE;
    }

    if( pensize < WIDTH_MIN_VALUE )
    {
        pensize = WIDTH_MIN_VALUE;
    }

    g_DrawDefaultLineThickness = pensize;
    m_DialogDefaultPenSize->SetValue( StringFromValue( g_UserUnit, pensize ) );
}


void DIALOG_SVG_PRINT::ExportSVGFile( bool aOnlyOneFile )
{
    m_outputDirectory = m_outputDirectoryName->GetValue();

    // Create output directory if it does not exist (also transform it in
    // absolute form). Bail if it fails
    wxFileName outputDir = wxFileName::DirName( m_outputDirectory );
    wxString boardFilename = m_board->GetFileName();

    REPORTER& reporter = m_messagesPanel->Reporter();

    if( !EnsureFileDirectoryExists( &outputDir, boardFilename, &reporter ) )
    {
        wxString msg = wxString::Format(
                _( "Could not write plot files to folder '%s'." ),
                GetChars( outputDir.GetPath() )
                );
        DisplayError( this, msg );
        return;
    }

    m_printMirror = m_printMirrorOpt->GetValue();
    m_printBW = m_ModeColorOption->GetSelection();
    SetPenWidth();

    LSET all_selected = getCheckBoxSelectedLayers();

    for( LSEQ seq = all_selected.Seq();  seq;  ++seq )
    {
        LAYER_ID layer = *seq;

        wxFileName fn( boardFilename );

        wxString suffix = aOnlyOneFile ? wxT( "brd" ) : m_board->GetStandardLayerName( layer );

        BuildPlotFileName( &fn, outputDir.GetPath(), suffix, SVGFileExtension );

        m_printMaskLayer = aOnlyOneFile ? all_selected : LSET( layer );

        if( m_PrintBoardEdgesCtrl->IsChecked() )
            m_printMaskLayer.set( Edge_Cuts );

        if( CreateSVGFile( fn.GetFullPath(), aOnlyOneFile ) )
        {
            reporter.Report (
                    wxString::Format( _( "Plot: '%s' OK." ), GetChars( fn.GetFullPath() ) ),
                    REPORTER::RPT_ACTION );
        }
        else    // Error
        {
            reporter.Report (
                    wxString::Format( _( "Unable to create file '%s'." ), GetChars( fn.GetFullPath() ) ),
                    REPORTER::RPT_ERROR );
        }

        if( aOnlyOneFile )
            break;
    }
}


// Actual SVG file export  function.
bool DIALOG_SVG_PRINT::CreateSVGFile( const wxString& aFullFileName, bool aOnlyOneFile )
{
    PCB_PLOT_PARAMS plot_opts;

    plot_opts.SetPlotFrameRef( PrintPageRef() );

    // Adding drill marks, for copper layers
    if( ( m_printMaskLayer & LSET::AllCuMask() ).any() )
        plot_opts.SetDrillMarksType( PCB_PLOT_PARAMS::FULL_DRILL_SHAPE );
    else
        plot_opts.SetDrillMarksType( PCB_PLOT_PARAMS::NO_DRILL_SHAPE );

    plot_opts.SetSkipPlotNPTH_Pads( false );

    plot_opts.SetMirror( m_printMirror );
    plot_opts.SetFormat( PLOT_FORMAT_SVG );

    PAGE_INFO   pageInfo = m_board->GetPageSettings();
    wxPoint     axisorigin = m_board->GetAuxOrigin();

    if( PageIsBoardBoundarySize() )
    {
        EDA_RECT    bbox = m_board->ComputeBoundingBox();
        PAGE_INFO   currpageInfo = m_board->GetPageSettings();

        currpageInfo.SetWidthMils(  bbox.GetWidth() / IU_PER_MILS );
        currpageInfo.SetHeightMils( bbox.GetHeight() / IU_PER_MILS );
        m_board->SetPageSettings( currpageInfo );
        plot_opts.SetUseAuxOrigin( true );
        wxPoint origin = bbox.GetOrigin();
        m_board->SetAuxOrigin( origin );
    }

    LOCALE_IO    toggle;

    SVG_PLOTTER* plotter = (SVG_PLOTTER*) StartPlotBoard( m_board,
                     &plot_opts, UNDEFINED_LAYER, aFullFileName, wxEmptyString );

    if( plotter )
    {
        plotter->SetColorMode( !m_printBW );
        if( aOnlyOneFile )
        {
            for( LSEQ seq = m_printMaskLayer.SeqStackupBottom2Top();  seq;  ++seq )
                PlotOneBoardLayer( m_board, plotter, *seq, plot_opts );
        }
        else
        {
            PlotStandardLayer( m_board, plotter, m_printMaskLayer, plot_opts );
        }
        plotter->EndPlot();
    }

    delete plotter;

    m_board->SetAuxOrigin( axisorigin );        // reset to the values saved earlier
    m_board->SetPageSettings( pageInfo );

    return true;
}


void DIALOG_SVG_PRINT::OnButtonPlot( wxCommandEvent& event )
{
    m_oneFileOnly = m_rbFileOpt->GetSelection() == 1;
    ExportSVGFile( m_oneFileOnly );

    m_did_print = true;
}


void DIALOG_SVG_PRINT::OnButtonCloseClick( wxCommandEvent& event )
{
    Close();
}


void DIALOG_SVG_PRINT::OnCloseWindow( wxCloseEvent& event )
{
    if( m_did_print )   // unless output was created, this is tantamount to a cancel.
    {
        SetPenWidth();
        m_printBW = m_ModeColorOption->GetSelection();
        m_oneFileOnly = m_rbFileOpt->GetSelection() == 1;

        // Why are SVG layer choices co-mingled with other plot layer choices in the config file?
        // The string OPTKEY_LAYERBASE is used in multiple places.
        // fix this.

        wxString dirStr = m_outputDirectoryName->GetValue();
        dirStr.Replace( wxT( "\\" ), wxT( "/" ) );

        m_callers_params->SetOutputDirectory( dirStr );

        if( m_config )
        {
            m_config->Write( PLOTSVGMODECOLOR_KEY, m_printBW );
            m_config->Write( PLOTSVGMODEMIRROR_KEY, m_printMirror );
            m_config->Write( PLOTSVGMODEONEFILE_KEY, m_oneFileOnly );
            m_config->Write( PLOTSVGPAGESIZEOPT_KEY, m_rbSvgPageSizeOpt->GetSelection() );
            m_config->Write( PLOTSVGPLOT_BRD_EDGE_KEY, m_PrintBoardEdgesCtrl->GetValue() );

            wxString layerKey;

            for( unsigned layer = 0; layer < DIM(m_boxSelectLayer);  ++layer )
            {
                if( !m_boxSelectLayer[layer] )
                    continue;

                layerKey.Printf( OPTKEY_LAYERBASE, layer );
                m_config->Write( layerKey, m_boxSelectLayer[layer]->IsChecked() );
            }
        }
    }

    EndModal( m_did_print ? wxID_OK : wxID_CANCEL );
}


bool InvokeSVGPrint( wxTopLevelWindow* aCaller, BOARD* aBoard, PCB_PLOT_PARAMS* aSettings )
{
    DIALOG_SVG_PRINT dlg( aCaller, aBoard, aSettings );

    return dlg.ShowModal() == wxID_OK;
}