kicad/pcbnew/dialogs/dialog_export_svg.cpp

378 lines
12 KiB
C++
Raw Normal View History

/*
* 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>
* KIWAY Milestone A): Make major modules into DLL/DSOs. ! The initial testing of this commit should be done using a Debug build so that all the wxASSERT()s are enabled. Also, be sure and keep enabled the USE_KIWAY_DLLs option. The tree won't likely build without it. Turning it off is senseless anyways. If you want stable code, go back to a prior version, the one tagged with "stable". * Relocate all functionality out of the wxApp derivative into more finely targeted purposes: a) DLL/DSO specific b) PROJECT specific c) EXE or process specific d) configuration file specific data e) configuration file manipulations functions. All of this functionality was blended into an extremely large wxApp derivative and that was incompatible with the desire to support multiple concurrently loaded DLL/DSO's ("KIFACE")s and multiple concurrently open projects. An amazing amount of organization come from simply sorting each bit of functionality into the proper box. * Switch to wxConfigBase from wxConfig everywhere except instantiation. * Add classes KIWAY, KIFACE, KIFACE_I, SEARCH_STACK, PGM_BASE, PGM_KICAD, PGM_SINGLE_TOP, * Remove "Return" prefix on many function names. * Remove obvious comments from CMakeLists.txt files, and from else() and endif()s. * Fix building boost for use in a DSO on linux. * Remove some of the assumptions in the CMakeLists.txt files that windows had to be the host platform when building windows binaries. * Reduce the number of wxStrings being constructed at program load time via static construction. * Pass wxConfigBase* to all SaveSettings() and LoadSettings() functions so that these functions are useful even when the wxConfigBase comes from another source, as is the case in the KICAD_MANAGER_FRAME. * Move the setting of the KIPRJMOD environment variable into class PROJECT, so that it can be moved into a project variable soon, and out of FP_LIB_TABLE. * Add the KIWAY_PLAYER which is associated with a particular PROJECT, and all its child wxFrames and wxDialogs now have a Kiway() member function which returns a KIWAY& that that window tree branch is in support of. This is like wxWindows DNA in that child windows get this member with proper value at time of construction. * Anticipate some of the needs for milestones B) and C) and make code adjustments now in an effort to reduce work in those milestones. * No testing has been done for python scripting, since milestone C) has that being largely reworked and re-thought-out.
2014-03-20 00:42:08 +00:00
#include <kiface_i.h>
#include <common.h>
2018-01-29 15:39:40 +00:00
#include <pcb_base_frame.h>
2012-11-16 14:13:31 +00:00
#include <wildcards_and_files_ext.h>
#include <reporter.h>
#include <confirm.h>
#include <pcbnew.h>
#include <pcbplot.h>
#include <class_board.h>
#include <dialog_export_svg_base.h>
#include <invoke_pcb_dialog.h>
#include <wx_html_report_panel.h>
#include <bitmaps.h>
#include <widgets/unit_binder.h>
class DIALOG_EXPORT_SVG : public DIALOG_EXPORT_SVG_BASE
{
public:
DIALOG_EXPORT_SVG( PCB_BASE_FRAME* aParent, BOARD* aBoard );
~DIALOG_EXPORT_SVG() override;
private:
BOARD* m_board;
wxConfigBase* m_config;
LSET m_printMaskLayer;
2016-06-19 18:29:13 +00:00
// the list of existing board layers in wxCheckListBox, with the
// board layers id:
std::pair<wxCheckListBox*, int> m_boxSelectLayer[PCB_LAYER_ID_COUNT];
bool m_printBW;
wxString m_outputDirectory;
bool m_printMirror;
bool m_oneFileOnly;
UNIT_BINDER m_defaultPenWidth;
void initDialog();
2016-09-24 18:53:15 +00:00
void OnButtonPlot( wxCommandEvent& event ) override;
2016-09-24 18:53:15 +00:00
void OnOutputDirectoryBrowseClicked( wxCommandEvent& event ) override;
void ExportSVGFile( bool aOnlyOneFile );
bool CreateSVGFile( const wxString& FullFileName );
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" )
#define PLOTSVG_LAYERBASE wxT( "PlotSVGLayer_%d" )
#define PLOTSVG_DIR_KEY wxT( "PlotSVGDirectory" )
// reasonable values for default pen width
#define WIDTH_MAX_VALUE (2 * IU_PER_MM)
#define WIDTH_MIN_VALUE (0.05 * IU_PER_MM)
2012-11-16 14:13:31 +00:00
/*
* DIALOG_EXPORT_SVG functions
*/
DIALOG_EXPORT_SVG::DIALOG_EXPORT_SVG( PCB_BASE_FRAME* aParent, BOARD* aBoard ) :
DIALOG_EXPORT_SVG_BASE( aParent ),
m_defaultPenWidth( aParent, m_penWidthLabel, m_penWidthCtrl, m_penWidthUnits, true,
WIDTH_MIN_VALUE, WIDTH_MAX_VALUE)
{
m_board = aBoard;
m_config = Kiface().KifaceSettings();
memset( m_boxSelectLayer, 0, sizeof( m_boxSelectLayer ) );
m_browseButton->SetBitmap( KiBitmap( folder_xpm ) );
2011-12-16 20:12:49 +00:00
initDialog();
2016-06-19 18:29:13 +00:00
// We use a sdbSizer to get platform-dependent ordering of the action buttons, but
// that requires us to correct the button labels here.
m_sdbSizer1OK->SetLabel( _( "Export" ) );
m_sdbSizer1Cancel->SetLabel( _( "Close" ) );
m_sdbSizer1->Layout();
FinishDialogSettings();
}
DIALOG_EXPORT_SVG::~DIALOG_EXPORT_SVG()
{
g_DrawDefaultLineThickness = m_defaultPenWidth.GetValue();
m_printBW = m_ModeColorOption->GetSelection();
m_oneFileOnly = m_rbFileOpt->GetSelection() == 1;
m_outputDirectory = m_outputDirectoryName->GetValue();
m_outputDirectory.Replace( wxT( "\\" ), wxT( "/" ) );
if( m_config )
{
m_config->Write( PLOTSVG_DIR_KEY, m_outputDirectory );
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].first )
continue;
layerKey.Printf( PLOTSVG_LAYERBASE, layer );
m_config->Write( layerKey, m_boxSelectLayer[layer].first->IsChecked( m_boxSelectLayer[layer].second ) );
}
}
}
void DIALOG_EXPORT_SVG::initDialog()
{
2012-11-16 14:13:31 +00:00
if( m_config )
{
m_config->Read( PLOTSVG_DIR_KEY, &m_outputDirectory, wxEmptyString );
2012-11-16 14:13:31 +00:00
m_config->Read( PLOTSVGMODECOLOR_KEY, &m_printBW, false );
long ltmp;
2012-11-16 14:13:31 +00:00
m_config->Read( PLOTSVGPAGESIZEOPT_KEY, &ltmp, 0 );
m_rbSvgPageSizeOpt->SetSelection( ltmp );
m_config->Read( PLOTSVGMODEMIRROR_KEY, &m_printMirror, false );
m_config->Read( PLOTSVGMODEONEFILE_KEY, &m_oneFileOnly, false);
2012-11-16 14:13:31 +00:00
m_config->Read( PLOTSVGPLOT_BRD_EDGE_KEY, &ltmp, 1 );
m_PrintBoardEdgesCtrl->SetValue( ltmp );
}
2012-11-16 14:13:31 +00:00
m_outputDirectoryName->SetValue( m_outputDirectory );
m_ModeColorOption->SetSelection( m_printBW ? 1 : 0 );
m_printMirrorOpt->SetValue( m_printMirror );
m_rbFileOpt->SetSelection( m_oneFileOnly ? 1 : 0 );
m_defaultPenWidth.SetValue( g_DrawDefaultLineThickness );
for( LSEQ seq = m_board->GetEnabledLayers().UIOrder(); seq; ++seq )
{
PCB_LAYER_ID layer = *seq;
2016-06-19 18:29:13 +00:00
int checkIndex;
if( IsCopperLayer( layer ) )
2016-06-19 18:29:13 +00:00
{
checkIndex = m_CopperLayersList->Append( m_board->GetLayerName( layer ) );
m_boxSelectLayer[layer] = std::make_pair( m_CopperLayersList, checkIndex );
}
else
{
2016-06-19 18:29:13 +00:00
checkIndex = m_TechnicalLayersList->Append( m_board->GetLayerName( layer ) );
m_boxSelectLayer[layer] = std::make_pair( m_TechnicalLayersList, checkIndex );
}
2016-06-19 18:29:13 +00:00
if( m_config )
{
wxString layerKey;
layerKey.Printf( PLOTSVG_LAYERBASE, layer );
2016-06-19 18:29:13 +00:00
bool option;
2016-06-19 18:29:13 +00:00
if( m_config && m_config->Read( layerKey, &option ) )
m_boxSelectLayer[layer].first->Check( checkIndex, option );
}
}
}
LSET DIALOG_EXPORT_SVG::getCheckBoxSelectedLayers() const
{
LSET ret;
2016-06-19 18:29:13 +00:00
for( unsigned layer = 0; layer < DIM(m_boxSelectLayer); ++layer )
{
2016-06-19 18:29:13 +00:00
if( !m_boxSelectLayer[layer].first )
continue;
if( m_boxSelectLayer[layer].first->IsChecked( m_boxSelectLayer[layer].second ) )
ret.set( layer );
}
return ret;
}
void DIALOG_EXPORT_SVG::OnOutputDirectoryBrowseClicked( wxCommandEvent& event )
2012-11-16 14:13:31 +00:00
{
// 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() );
2012-11-16 14:13:31 +00:00
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" ),
2012-11-16 14:13:31 +00:00
wxYES_NO | wxICON_QUESTION | wxYES_DEFAULT );
if( dialog.ShowModal() == wxID_YES )
{
wxString boardFilePath = Prj().AbsolutePath( m_board->GetFileName() );
boardFilePath = wxPathOnly( boardFilePath );
2012-11-16 14:13:31 +00:00
if( !dirName.MakeRelativeTo( boardFilePath ) )
wxMessageBox( _( "Cannot make path relative (target volume different from board file volume)!" ),
2012-11-16 14:13:31 +00:00
_( "Plot Output Directory" ), wxOK | wxICON_ERROR );
}
m_outputDirectoryName->SetValue( dirName.GetFullPath() );
m_outputDirectory = m_outputDirectoryName->GetValue();
}
void DIALOG_EXPORT_SVG::ExportSVGFile( bool aOnlyOneFile )
{
2012-11-16 14:13:31 +00:00
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();
2012-11-16 14:13:31 +00:00
if( !EnsureFileDirectoryExists( &outputDir, boardFilename, &reporter ) )
{
wxString msg = wxString::Format( _( "Could not write plot files to folder \"%s\"." ),
outputDir.GetPath() );
DisplayError( this, msg );
2012-11-16 14:13:31 +00:00
return;
}
m_printMirror = m_printMirrorOpt->GetValue();
m_printBW = m_ModeColorOption->GetSelection();
g_DrawDefaultLineThickness = m_defaultPenWidth.GetValue();
LSET all_selected = getCheckBoxSelectedLayers();
for( LSEQ seq = all_selected.Seq(); seq; ++seq )
{
PCB_LAYER_ID layer = *seq;
wxFileName fn( boardFilename );
wxString suffix = aOnlyOneFile ? wxT( "brd" ) : m_board->GetStandardLayerName( layer );
2012-11-16 14:13:31 +00:00
BuildPlotFileName( &fn, outputDir.GetPath(), suffix, SVGFileExtension );
wxString path = fn.GetFullPath();
m_printMaskLayer = aOnlyOneFile ? all_selected : LSET( layer );
if( m_PrintBoardEdgesCtrl->IsChecked() )
m_printMaskLayer.set( Edge_Cuts );
if( CreateSVGFile( path ) )
{
reporter.Report( wxString::Format( _( "Exported \"%s\"." ), path ),
REPORTER::RPT_ACTION );
}
else // Error
{
reporter.Report( wxString::Format( _( "Unable to create file \"%s\"." ), path ),
REPORTER::RPT_ERROR );
}
if( aOnlyOneFile )
break;
}
}
// Actual SVG file export function.
bool DIALOG_EXPORT_SVG::CreateSVGFile( const wxString& aFullFileName )
{
PCB_PLOT_PARAMS plot_opts;
plot_opts.SetPlotFrameRef( m_rbSvgPageSizeOpt->GetSelection() == 0 );
// 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( m_rbSvgPageSizeOpt->GetSelection() == 2 ) // Page is board boundary size
{
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 );
2012-11-16 14:13:31 +00:00
m_board->SetPageSettings( currpageInfo );
plot_opts.SetUseAuxOrigin( true );
wxPoint origin = bbox.GetOrigin();
m_board->SetAuxOrigin( origin );
}
LOCALE_IO toggle;
2014-06-30 06:44:46 +00:00
SVG_PLOTTER* plotter = (SVG_PLOTTER*) StartPlotBoard( m_board, &plot_opts, UNDEFINED_LAYER,
aFullFileName, wxEmptyString );
if( plotter )
{
plotter->SetColorMode( !m_printBW );
for( LSEQ seq = m_printMaskLayer.SeqStackupBottom2Top(); seq; ++seq )
PlotOneBoardLayer( m_board, plotter, *seq, plot_opts );
2012-11-16 14:13:31 +00:00
plotter->EndPlot();
}
delete plotter;
m_board->SetAuxOrigin( axisorigin ); // reset to the values saved earlier
2012-11-16 14:13:31 +00:00
m_board->SetPageSettings( pageInfo );
return true;
}
void DIALOG_EXPORT_SVG::OnButtonPlot( wxCommandEvent& event )
{
m_oneFileOnly = m_rbFileOpt->GetSelection() == 1;
ExportSVGFile( m_oneFileOnly );
}
bool InvokeExportSVG( PCB_BASE_FRAME* aCaller, BOARD* aBoard )
{
DIALOG_EXPORT_SVG dlg( aCaller, aBoard );
dlg.ShowModal();
return true;
}