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.

340 lines
10 KiB

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