You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

369 lines
12 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 1992-2018 Jean-Pierre Charras jp.charras at wanadoo.fr
  5. * Copyright (C) 1992-2010 Lorenzo Marcantonio
  6. * Copyright (C) 2011 Wayne Stambaugh <stambaughw@gmail.com>
  7. * Copyright (C) 1992-2020 KiCad Developers, see AUTHORS.txt for contributors.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 2
  12. * of the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, you may find one here:
  21. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  22. * or you may search the http://www.gnu.org website for the version 2 license,
  23. * or you may write to the Free Software Foundation, Inc.,
  24. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  25. */
  26. #include <bitmaps.h>
  27. #include <dialog_plot_schematic.h>
  28. #include <eeschema_settings.h>
  29. #include <kiface_i.h>
  30. #include <pgm_base.h>
  31. #include <settings/settings_manager.h>
  32. #include <ws_painter.h>
  33. #include <sch_painter.h>
  34. #include <schematic.h>
  35. // static members (static to remember last state):
  36. int DIALOG_PLOT_SCHEMATIC::m_pageSizeSelect = PAGE_SIZE_AUTO;
  37. int DIALOG_PLOT_SCHEMATIC::m_HPGLPaperSizeSelect = PAGE_SIZE_AUTO;
  38. DIALOG_PLOT_SCHEMATIC::DIALOG_PLOT_SCHEMATIC( SCH_EDIT_FRAME* parent )
  39. : DIALOG_PLOT_SCHEMATIC_BASE( parent ),
  40. m_parent( parent ),
  41. m_plotFormat( PLOT_FORMAT::UNDEFINED ),
  42. m_HPGLPenSize( 1.0 ),
  43. m_defaultLineWidth( parent, m_lineWidthLabel, m_lineWidthCtrl, m_lineWidthUnits, true ),
  44. m_penWidth( parent, m_penWidthLabel, m_penWidthCtrl, m_penWidthUnits, true )
  45. {
  46. m_configChanged = false;
  47. m_browseButton->SetBitmap( KiBitmap( folder_xpm ) );
  48. // We use a sdbSizer to get platform-dependent ordering of the action buttons, but
  49. // that requires us to correct the button labels here.
  50. m_sdbSizer1OK->SetLabel( _( "Plot All Pages" ) );
  51. m_sdbSizer1Apply->SetLabel( _( "Plot Current Page" ) );
  52. m_sdbSizer1Cancel->SetLabel( _( "Close" ) );
  53. m_sdbSizer1->Layout();
  54. m_sdbSizer1OK->SetDefault();
  55. initDlg();
  56. // Now all widgets have the size fixed, call FinishDialogSettings
  57. FinishDialogSettings();
  58. }
  59. // Initialize the dialog options:
  60. void DIALOG_PLOT_SCHEMATIC::initDlg()
  61. {
  62. auto cfg = dynamic_cast<EESCHEMA_SETTINGS*>( Kiface().KifaceSettings() );
  63. wxASSERT( cfg );
  64. if( cfg )
  65. {
  66. for( COLOR_SETTINGS* settings : Pgm().GetSettingsManager().GetColorSettingsList() )
  67. {
  68. int idx = m_colorTheme->Append( settings->GetName(), static_cast<void*>( settings ) );
  69. if( settings->GetFilename() == cfg->m_PlotPanel.color_theme )
  70. m_colorTheme->SetSelection( idx );
  71. }
  72. m_colorTheme->Enable( cfg->m_PlotPanel.color );
  73. m_plotBackgroundColor->Enable( cfg->m_PlotPanel.color );
  74. m_plotBackgroundColor->SetValue( cfg->m_PlotPanel.background_color );
  75. // Set color or B&W plot option
  76. setModeColor( cfg->m_PlotPanel.color );
  77. // Set plot or not frame reference option
  78. setPlotFrameRef( cfg->m_PlotPanel.frame_reference );
  79. // Set HPGL plot origin to center of paper of left bottom corner
  80. SetPlotOriginCenter( cfg->m_PlotPanel.hpgl_origin );
  81. m_HPGLPaperSizeSelect = cfg->m_PlotPanel.hpgl_paper_size;
  82. // HPGL Pen Size is stored in mm in config
  83. m_HPGLPenSize = cfg->m_PlotPanel.hpgl_pen_size * IU_PER_MM;
  84. // Switch to the last save plot format
  85. PLOT_FORMAT fmt = static_cast<PLOT_FORMAT>( cfg->m_PlotPanel.format );
  86. switch( fmt )
  87. {
  88. default:
  89. case PLOT_FORMAT::POST: m_plotFormatOpt->SetSelection( 0 ); break;
  90. case PLOT_FORMAT::PDF: m_plotFormatOpt->SetSelection( 1 ); break;
  91. case PLOT_FORMAT::SVG: m_plotFormatOpt->SetSelection( 2 ); break;
  92. case PLOT_FORMAT::DXF: m_plotFormatOpt->SetSelection( 3 ); break;
  93. case PLOT_FORMAT::HPGL: m_plotFormatOpt->SetSelection( 4 ); break;
  94. }
  95. if( fmt == PLOT_FORMAT::DXF || fmt == PLOT_FORMAT::HPGL )
  96. m_plotBackgroundColor->Disable();
  97. // Set the default line width (pen width which should be used for
  98. // items that do not have a pen size defined (like frame ref)
  99. // the default line width is stored in mils in config
  100. m_defaultLineWidth.SetValue( Mils2iu( cfg->m_Drawing.default_line_thickness ) );
  101. }
  102. // Initialize HPGL specific widgets
  103. m_penWidth.SetValue( m_HPGLPenSize );
  104. // Plot directory
  105. SCHEMATIC_SETTINGS& settings = m_parent->Schematic().Settings();
  106. wxString path = settings.m_PlotDirectoryName;
  107. #ifdef __WINDOWS__
  108. path.Replace( '/', '\\' );
  109. #endif
  110. m_outputDirectoryName->SetValue( path );
  111. }
  112. /**
  113. * @todo Copy of DIALOG_PLOT::OnOutputDirectoryBrowseClicked in dialog_plot.cpp, maybe merge to
  114. * a common method.
  115. */
  116. void DIALOG_PLOT_SCHEMATIC::OnOutputDirectoryBrowseClicked( wxCommandEvent& event )
  117. {
  118. // Build the absolute path of current output directory to preselect it in the file browser.
  119. wxString path = ExpandEnvVarSubstitutions( m_outputDirectoryName->GetValue(), &Prj() );
  120. path = Prj().AbsolutePath( path );
  121. wxDirDialog dirDialog( this, _( "Select Output Directory" ), path );
  122. if( dirDialog.ShowModal() == wxID_CANCEL )
  123. return;
  124. wxFileName dirName = wxFileName::DirName( dirDialog.GetPath() );
  125. wxFileName fn( Prj().AbsolutePath( m_parent->Schematic().Root().GetFileName() ) );
  126. wxString defaultPath = fn.GetPathWithSep();
  127. wxString msg;
  128. msg.Printf( _( "Do you want to use a path relative to\n\"%s\"" ), GetChars( defaultPath ) );
  129. wxMessageDialog dialog( this, msg, _( "Plot Output Directory" ),
  130. wxYES_NO | wxICON_QUESTION | wxYES_DEFAULT );
  131. // relative directory selected
  132. if( dialog.ShowModal() == wxID_YES )
  133. {
  134. if( !dirName.MakeRelativeTo( defaultPath ) )
  135. wxMessageBox( _( "Cannot make path relative (target volume different from file "
  136. "volume)!" ), _( "Plot Output Directory" ), wxOK | wxICON_ERROR );
  137. }
  138. m_outputDirectoryName->SetValue( dirName.GetFullPath() );
  139. }
  140. PLOT_FORMAT DIALOG_PLOT_SCHEMATIC::GetPlotFileFormat()
  141. {
  142. switch( m_plotFormatOpt->GetSelection() )
  143. {
  144. default:
  145. case 0:
  146. return PLOT_FORMAT::POST;
  147. case 1:
  148. return PLOT_FORMAT::PDF;
  149. case 2:
  150. return PLOT_FORMAT::SVG;
  151. case 3:
  152. return PLOT_FORMAT::DXF;
  153. case 4:
  154. return PLOT_FORMAT::HPGL;
  155. }
  156. }
  157. void DIALOG_PLOT_SCHEMATIC::OnPageSizeSelected( wxCommandEvent& event )
  158. {
  159. if( GetPlotFileFormat() == PLOT_FORMAT::HPGL )
  160. m_HPGLPaperSizeSelect = m_paperSizeOption->GetSelection();
  161. else
  162. m_pageSizeSelect = m_paperSizeOption->GetSelection();
  163. }
  164. void DIALOG_PLOT_SCHEMATIC::OnUpdateUI( wxUpdateUIEvent& event )
  165. {
  166. PLOT_FORMAT fmt = GetPlotFileFormat();
  167. if( fmt != m_plotFormat )
  168. {
  169. m_plotFormat = fmt;
  170. wxArrayString paperSizes;
  171. paperSizes.push_back( _( "Schematic size" ) );
  172. int selection;
  173. if( fmt == PLOT_FORMAT::HPGL )
  174. {
  175. paperSizes.push_back( _( "A4" ) );
  176. paperSizes.push_back( _( "A3" ) );
  177. paperSizes.push_back( _( "A2" ) );
  178. paperSizes.push_back( _( "A1" ) );
  179. paperSizes.push_back( _( "A0" ) );
  180. paperSizes.push_back( _( "A" ) );
  181. paperSizes.push_back( _( "B" ) );
  182. paperSizes.push_back( _( "C" ) );
  183. paperSizes.push_back( _( "D" ) );
  184. paperSizes.push_back( _( "E" ) );
  185. selection = m_HPGLPaperSizeSelect;
  186. }
  187. else
  188. {
  189. paperSizes.push_back( _( "A4" ) );
  190. paperSizes.push_back( _( "A" ) );
  191. selection = m_pageSizeSelect;
  192. }
  193. m_paperSizeOption->Set( paperSizes );
  194. m_paperSizeOption->SetSelection( selection );
  195. m_defaultLineWidth.Enable(
  196. fmt == PLOT_FORMAT::POST || fmt == PLOT_FORMAT::PDF || fmt == PLOT_FORMAT::SVG );
  197. m_plotOriginTitle->Enable( fmt == PLOT_FORMAT::HPGL );
  198. m_plotOriginOpt->Enable( fmt == PLOT_FORMAT::HPGL );
  199. m_penWidth.Enable( fmt == PLOT_FORMAT::HPGL );
  200. m_plotBackgroundColor->Enable(
  201. fmt == PLOT_FORMAT::POST || fmt == PLOT_FORMAT::PDF || fmt == PLOT_FORMAT::SVG );
  202. m_colorTheme->Enable( fmt != PLOT_FORMAT::HPGL );
  203. m_ModeColorOption->Enable( fmt != PLOT_FORMAT::HPGL );
  204. }
  205. }
  206. void DIALOG_PLOT_SCHEMATIC::getPlotOptions( RENDER_SETTINGS* aSettings )
  207. {
  208. m_HPGLPenSize = m_penWidth.GetValue();
  209. EESCHEMA_SETTINGS* cfg = dynamic_cast<EESCHEMA_SETTINGS*>( Kiface().KifaceSettings() );
  210. wxASSERT( cfg );
  211. COLOR_SETTINGS* colors = getColorSettings();
  212. if( cfg )
  213. {
  214. cfg->m_PlotPanel.background_color = m_plotBackgroundColor->GetValue();
  215. cfg->m_PlotPanel.color = getModeColor();
  216. cfg->m_PlotPanel.color_theme = colors->GetFilename();
  217. cfg->m_PlotPanel.frame_reference = getPlotFrameRef();
  218. cfg->m_PlotPanel.format = static_cast<int>( GetPlotFileFormat() );
  219. cfg->m_PlotPanel.hpgl_origin = GetPlotOriginCenter();
  220. cfg->m_PlotPanel.hpgl_paper_size = m_HPGLPaperSizeSelect;
  221. // HPGL Pen Size is stored in mm in config
  222. cfg->m_PlotPanel.hpgl_pen_size = m_HPGLPenSize / IU_PER_MM;
  223. }
  224. aSettings->LoadColors( colors );
  225. aSettings->SetDefaultPenWidth( (int) m_defaultLineWidth.GetValue() );
  226. if( m_plotBackgroundColor->GetValue() )
  227. aSettings->SetBackgroundColor( colors->GetColor( LAYER_SCHEMATIC_BACKGROUND ) );
  228. else
  229. aSettings->SetBackgroundColor( COLOR4D::UNSPECIFIED );
  230. // Plot directory
  231. wxString path = m_outputDirectoryName->GetValue();
  232. path.Replace( '\\', '/' );
  233. SCHEMATIC_SETTINGS& settings = m_parent->Schematic().Settings();
  234. if( settings.m_PlotDirectoryName != path )
  235. m_configChanged = true;
  236. settings.m_PlotDirectoryName = path;
  237. }
  238. COLOR_SETTINGS* DIALOG_PLOT_SCHEMATIC::getColorSettings()
  239. {
  240. int selection = m_colorTheme->GetSelection();
  241. return static_cast<COLOR_SETTINGS*>( m_colorTheme->GetClientData( selection ) );
  242. }
  243. void DIALOG_PLOT_SCHEMATIC::OnPlotCurrent( wxCommandEvent& event )
  244. {
  245. PlotSchematic( false );
  246. }
  247. void DIALOG_PLOT_SCHEMATIC::OnPlotAll( wxCommandEvent& event )
  248. {
  249. PlotSchematic( true );
  250. }
  251. void DIALOG_PLOT_SCHEMATIC::PlotSchematic( bool aPlotAll )
  252. {
  253. KIGFX::SCH_RENDER_SETTINGS renderSettings( *m_parent->GetRenderSettings() );
  254. getPlotOptions( &renderSettings );
  255. switch( GetPlotFileFormat() )
  256. {
  257. default:
  258. case PLOT_FORMAT::POST:
  259. createPSFile( aPlotAll, getPlotFrameRef(), &renderSettings );
  260. break;
  261. case PLOT_FORMAT::DXF:
  262. CreateDXFFile( aPlotAll, getPlotFrameRef(), &renderSettings );
  263. break;
  264. case PLOT_FORMAT::PDF:
  265. createPDFFile( aPlotAll, getPlotFrameRef(), &renderSettings );
  266. break;
  267. case PLOT_FORMAT::SVG:
  268. createSVGFile( aPlotAll, getPlotFrameRef(), &renderSettings );
  269. break;
  270. case PLOT_FORMAT::HPGL:
  271. createHPGLFile( aPlotAll, getPlotFrameRef(), &renderSettings );
  272. break;
  273. }
  274. }
  275. wxFileName DIALOG_PLOT_SCHEMATIC::createPlotFileName( wxString& aPlotFileName,
  276. wxString& aExtension,
  277. REPORTER* aReporter )
  278. {
  279. wxString path = ExpandEnvVarSubstitutions( m_outputDirectoryName->GetValue(), &Prj() );
  280. wxFileName outputDir = wxFileName::DirName( path );
  281. wxString plotFileName = Prj().AbsolutePath( aPlotFileName + wxT( "." ) + aExtension);
  282. if( !EnsureFileDirectoryExists( &outputDir, plotFileName, aReporter ) )
  283. {
  284. wxString msg = wxString::Format( _( "Could not write plot files to folder \"%s\"." ),
  285. outputDir.GetPath() );
  286. aReporter->Report( msg, RPT_SEVERITY_ERROR );
  287. }
  288. wxFileName fn( plotFileName );
  289. fn.SetPath( outputDir.GetFullPath() );
  290. return fn;
  291. }