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.

1350 lines
46 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-2021 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 <common.h> // For ExpandEnvVarSubstitutions
  28. #include <dialogs/wx_html_report_panel.h>
  29. #include <dialog_plot_schematic.h>
  30. #include <eeschema_settings.h>
  31. #include <kiface_base.h>
  32. #include <locale_io.h>
  33. #include <plotters/plotter_hpgl.h>
  34. #include <plotters/plotter_dxf.h>
  35. #include <plotters/plotters_pslike.h>
  36. #include <reporter.h>
  37. #include <trace_helpers.h>
  38. #include <settings/settings_manager.h>
  39. #include <drawing_sheet/ds_painter.h>
  40. #include <sch_edit_frame.h>
  41. #include <sch_painter.h>
  42. #include <schematic.h>
  43. #include <sch_screen.h>
  44. #include <wx/dirdlg.h>
  45. #include <wx/msgdlg.h>
  46. #include <wx/stdpaths.h>
  47. #include <wx/log.h>
  48. // static members (static to remember last state):
  49. int DIALOG_PLOT_SCHEMATIC::m_pageSizeSelect = PAGE_SIZE_AUTO;
  50. int DIALOG_PLOT_SCHEMATIC::m_HPGLPaperSizeSelect = PAGE_SIZE_AUTO;
  51. enum HPGL_PAGEZ_T {
  52. PAGE_DEFAULT = 0,
  53. HPGL_PAGE_SIZE_A5,
  54. HPGL_PAGE_SIZE_A4,
  55. HPGL_PAGE_SIZE_A3,
  56. HPGL_PAGE_SIZE_A2,
  57. HPGL_PAGE_SIZE_A1,
  58. HPGL_PAGE_SIZE_A0,
  59. HPGL_PAGE_SIZE_A,
  60. HPGL_PAGE_SIZE_B,
  61. HPGL_PAGE_SIZE_C,
  62. HPGL_PAGE_SIZE_D,
  63. HPGL_PAGE_SIZE_E,
  64. };
  65. static const wxChar* plot_sheet_list( int aSize )
  66. {
  67. switch( aSize )
  68. {
  69. default:
  70. case PAGE_DEFAULT: return nullptr;
  71. case HPGL_PAGE_SIZE_A5: return wxT( "A5" );
  72. case HPGL_PAGE_SIZE_A4: return wxT( "A4" );
  73. case HPGL_PAGE_SIZE_A3: return wxT( "A3" );
  74. case HPGL_PAGE_SIZE_A2: return wxT( "A2" );
  75. case HPGL_PAGE_SIZE_A1: return wxT( "A1" );
  76. case HPGL_PAGE_SIZE_A0: return wxT( "A0" );
  77. case HPGL_PAGE_SIZE_A: return wxT( "A" );
  78. case HPGL_PAGE_SIZE_B: return wxT( "B" );
  79. case HPGL_PAGE_SIZE_C: return wxT( "C" );
  80. case HPGL_PAGE_SIZE_D: return wxT( "D" );
  81. case HPGL_PAGE_SIZE_E: return wxT( "E" );
  82. }
  83. }
  84. DIALOG_PLOT_SCHEMATIC::DIALOG_PLOT_SCHEMATIC( SCH_EDIT_FRAME* parent )
  85. : DIALOG_PLOT_SCHEMATIC_BASE( parent ),
  86. m_parent( parent ),
  87. m_plotFormat( PLOT_FORMAT::UNDEFINED ),
  88. m_HPGLPenSize( 1.0 ),
  89. m_defaultLineWidth( parent, m_lineWidthLabel, m_lineWidthCtrl, m_lineWidthUnits ),
  90. m_penWidth( parent, m_penWidthLabel, m_penWidthCtrl, m_penWidthUnits )
  91. {
  92. m_configChanged = false;
  93. m_browseButton->SetBitmap( KiBitmap( BITMAPS::small_folder ) );
  94. m_MessagesBox->SetFileName( Prj().GetProjectPath() + wxT( "report.txt" ) );
  95. SetupStandardButtons( { { wxID_OK, _( "Plot All Pages" ) },
  96. { wxID_APPLY, _( "Plot Current Page" ) },
  97. { wxID_CANCEL, _( "Close" ) } } );
  98. initDlg();
  99. // Now all widgets have the size fixed, call FinishDialogSettings
  100. finishDialogSettings();
  101. }
  102. void DIALOG_PLOT_SCHEMATIC::initDlg()
  103. {
  104. auto cfg = dynamic_cast<EESCHEMA_SETTINGS*>( Kiface().KifaceSettings() );
  105. wxASSERT( cfg );
  106. if( cfg )
  107. {
  108. for( COLOR_SETTINGS* settings : m_parent->GetSettingsManager()->GetColorSettingsList() )
  109. {
  110. int idx = m_colorTheme->Append( settings->GetName(), static_cast<void*>( settings ) );
  111. if( settings->GetFilename() == cfg->m_PlotPanel.color_theme )
  112. m_colorTheme->SetSelection( idx );
  113. }
  114. m_colorTheme->Enable( cfg->m_PlotPanel.color );
  115. m_plotBackgroundColor->Enable( cfg->m_PlotPanel.color );
  116. m_plotBackgroundColor->SetValue( cfg->m_PlotPanel.background_color );
  117. // Set color or B&W plot option
  118. setModeColor( cfg->m_PlotPanel.color );
  119. // Set plot or not frame reference option
  120. setPlotDrawingSheet( cfg->m_PlotPanel.frame_reference );
  121. // HPGL plot origin and unit system configuration
  122. m_plotOriginOpt->SetSelection( cfg->m_PlotPanel.hpgl_origin );
  123. m_HPGLPaperSizeSelect = cfg->m_PlotPanel.hpgl_paper_size;
  124. // HPGL Pen Size is stored in mm in config
  125. m_HPGLPenSize = cfg->m_PlotPanel.hpgl_pen_size * IU_PER_MM;
  126. // Switch to the last save plot format
  127. PLOT_FORMAT fmt = static_cast<PLOT_FORMAT>( cfg->m_PlotPanel.format );
  128. switch( fmt )
  129. {
  130. default:
  131. case PLOT_FORMAT::POST: m_plotFormatOpt->SetSelection( 0 ); break;
  132. case PLOT_FORMAT::PDF: m_plotFormatOpt->SetSelection( 1 ); break;
  133. case PLOT_FORMAT::SVG: m_plotFormatOpt->SetSelection( 2 ); break;
  134. case PLOT_FORMAT::DXF: m_plotFormatOpt->SetSelection( 3 ); break;
  135. case PLOT_FORMAT::HPGL: m_plotFormatOpt->SetSelection( 4 ); break;
  136. }
  137. if( fmt == PLOT_FORMAT::DXF || fmt == PLOT_FORMAT::HPGL )
  138. m_plotBackgroundColor->Disable();
  139. // Set the default line width (pen width which should be used for
  140. // items that do not have a pen size defined (like frame ref)
  141. // the default line width is stored in mils in config
  142. m_defaultLineWidth.SetValue( Mils2iu( cfg->m_Drawing.default_line_thickness ) );
  143. }
  144. // Initialize HPGL specific widgets
  145. m_penWidth.SetValue( m_HPGLPenSize );
  146. // Plot directory
  147. SCHEMATIC_SETTINGS& settings = m_parent->Schematic().Settings();
  148. wxString path = settings.m_PlotDirectoryName;
  149. #ifdef __WINDOWS__
  150. path.Replace( '/', '\\' );
  151. #endif
  152. m_outputDirectoryName->SetValue( path );
  153. }
  154. /**
  155. * @todo Copy of DIALOG_PLOT::OnOutputDirectoryBrowseClicked in dialog_plot.cpp, maybe merge to
  156. * a common method.
  157. */
  158. void DIALOG_PLOT_SCHEMATIC::OnOutputDirectoryBrowseClicked( wxCommandEvent& event )
  159. {
  160. // Build the absolute path of current output directory to preselect it in the file browser.
  161. wxString path = ExpandEnvVarSubstitutions( m_outputDirectoryName->GetValue(), &Prj() );
  162. // When editing a schematic that is not part of a project in the stand alone mode, the
  163. // project path is not defined so point to the users document path to save the plot files.
  164. if( Prj().IsNullProject() )
  165. {
  166. path = wxStandardPaths::Get().GetDocumentsDir();
  167. }
  168. else
  169. {
  170. // Build the absolute path of current output directory to preselect it in the file browser.
  171. path = ExpandEnvVarSubstitutions( m_outputDirectoryName->GetValue(), &Prj() );
  172. path = Prj().AbsolutePath( path );
  173. }
  174. wxDirDialog dirDialog( this, _( "Select Output Directory" ), path );
  175. if( dirDialog.ShowModal() == wxID_CANCEL )
  176. return;
  177. wxFileName dirName = wxFileName::DirName( dirDialog.GetPath() );
  178. wxFileName fn( Prj().AbsolutePath( m_parent->Schematic().Root().GetFileName() ) );
  179. wxString defaultPath = fn.GetPathWithSep();
  180. wxString msg;
  181. wxFileName relPathTest; // Used to test if we can make the path relative
  182. relPathTest.Assign( dirDialog.GetPath() );
  183. // Test if making the path relative is possible before asking the user if they want to do it
  184. if( relPathTest.MakeRelativeTo( defaultPath ) )
  185. {
  186. msg.Printf( _( "Do you want to use a path relative to\n'%s'?" ), defaultPath );
  187. wxMessageDialog dialog( this, msg, _( "Plot Output Directory" ),
  188. wxYES_NO | wxICON_QUESTION | wxYES_DEFAULT );
  189. if( dialog.ShowModal() == wxID_YES )
  190. dirName.MakeRelativeTo( defaultPath );
  191. }
  192. m_outputDirectoryName->SetValue( dirName.GetFullPath() );
  193. }
  194. PLOT_FORMAT DIALOG_PLOT_SCHEMATIC::GetPlotFileFormat()
  195. {
  196. switch( m_plotFormatOpt->GetSelection() )
  197. {
  198. default:
  199. case 0: return PLOT_FORMAT::POST;
  200. case 1: return PLOT_FORMAT::PDF;
  201. case 2: return PLOT_FORMAT::SVG;
  202. case 3: return PLOT_FORMAT::DXF;
  203. case 4: return PLOT_FORMAT::HPGL;
  204. }
  205. }
  206. void DIALOG_PLOT_SCHEMATIC::OnPageSizeSelected( wxCommandEvent& event )
  207. {
  208. if( GetPlotFileFormat() == PLOT_FORMAT::HPGL )
  209. m_HPGLPaperSizeSelect = m_paperSizeOption->GetSelection();
  210. else
  211. m_pageSizeSelect = m_paperSizeOption->GetSelection();
  212. }
  213. void DIALOG_PLOT_SCHEMATIC::OnUpdateUI( wxUpdateUIEvent& event )
  214. {
  215. PLOT_FORMAT fmt = GetPlotFileFormat();
  216. if( fmt != m_plotFormat )
  217. {
  218. m_plotFormat = fmt;
  219. wxArrayString paperSizes;
  220. paperSizes.push_back( _( "Schematic size" ) );
  221. int selection;
  222. if( fmt == PLOT_FORMAT::HPGL )
  223. {
  224. paperSizes.push_back( _( "A5" ) );
  225. paperSizes.push_back( _( "A4" ) );
  226. paperSizes.push_back( _( "A3" ) );
  227. paperSizes.push_back( _( "A2" ) );
  228. paperSizes.push_back( _( "A1" ) );
  229. paperSizes.push_back( _( "A0" ) );
  230. paperSizes.push_back( _( "A" ) );
  231. paperSizes.push_back( _( "B" ) );
  232. paperSizes.push_back( _( "C" ) );
  233. paperSizes.push_back( _( "D" ) );
  234. paperSizes.push_back( _( "E" ) );
  235. selection = m_HPGLPaperSizeSelect;
  236. }
  237. else
  238. {
  239. paperSizes.push_back( _( "A4" ) );
  240. paperSizes.push_back( _( "A" ) );
  241. selection = m_pageSizeSelect;
  242. }
  243. m_paperSizeOption->Set( paperSizes );
  244. m_paperSizeOption->SetSelection( selection );
  245. m_defaultLineWidth.Enable(
  246. fmt == PLOT_FORMAT::POST || fmt == PLOT_FORMAT::PDF || fmt == PLOT_FORMAT::SVG );
  247. m_plotOriginTitle->Enable( fmt == PLOT_FORMAT::HPGL );
  248. m_plotOriginOpt->Enable( fmt == PLOT_FORMAT::HPGL );
  249. m_penWidth.Enable( fmt == PLOT_FORMAT::HPGL );
  250. m_plotBackgroundColor->Enable(
  251. fmt == PLOT_FORMAT::POST || fmt == PLOT_FORMAT::PDF || fmt == PLOT_FORMAT::SVG );
  252. m_colorTheme->Enable( fmt != PLOT_FORMAT::HPGL );
  253. m_ModeColorOption->Enable( fmt != PLOT_FORMAT::HPGL );
  254. }
  255. }
  256. void DIALOG_PLOT_SCHEMATIC::getPlotOptions( RENDER_SETTINGS* aSettings )
  257. {
  258. m_HPGLPenSize = m_penWidth.GetValue();
  259. EESCHEMA_SETTINGS* cfg = dynamic_cast<EESCHEMA_SETTINGS*>( Kiface().KifaceSettings() );
  260. wxASSERT( cfg );
  261. COLOR_SETTINGS* colors = getColorSettings();
  262. if( cfg )
  263. {
  264. cfg->m_PlotPanel.background_color = m_plotBackgroundColor->GetValue();
  265. cfg->m_PlotPanel.color = getModeColor();
  266. cfg->m_PlotPanel.color_theme = colors->GetFilename();
  267. cfg->m_PlotPanel.frame_reference = getPlotDrawingSheet();
  268. cfg->m_PlotPanel.format = static_cast<int>( GetPlotFileFormat() );
  269. cfg->m_PlotPanel.hpgl_origin = m_plotOriginOpt->GetSelection();
  270. cfg->m_PlotPanel.hpgl_paper_size = m_HPGLPaperSizeSelect;
  271. // HPGL Pen Size is stored in mm in config
  272. cfg->m_PlotPanel.hpgl_pen_size = m_HPGLPenSize / IU_PER_MM;
  273. }
  274. aSettings->LoadColors( colors );
  275. aSettings->SetMinPenWidth( (int) m_defaultLineWidth.GetValue() );
  276. if( m_plotBackgroundColor->GetValue() )
  277. aSettings->SetBackgroundColor( colors->GetColor( LAYER_SCHEMATIC_BACKGROUND ) );
  278. else
  279. aSettings->SetBackgroundColor( COLOR4D::UNSPECIFIED );
  280. // Plot directory
  281. wxString path = m_outputDirectoryName->GetValue();
  282. path.Replace( '\\', '/' );
  283. SCHEMATIC_SETTINGS& settings = m_parent->Schematic().Settings();
  284. if( settings.m_PlotDirectoryName != path )
  285. m_configChanged = true;
  286. settings.m_PlotDirectoryName = path;
  287. }
  288. COLOR_SETTINGS* DIALOG_PLOT_SCHEMATIC::getColorSettings()
  289. {
  290. int selection = m_colorTheme->GetSelection();
  291. if( selection < 0 )
  292. return m_parent->GetSettingsManager()->GetColorSettings( "_builtin_default" );
  293. return static_cast<COLOR_SETTINGS*>( m_colorTheme->GetClientData( selection ) );
  294. }
  295. void DIALOG_PLOT_SCHEMATIC::OnPlotCurrent( wxCommandEvent& event )
  296. {
  297. plotSchematic( false );
  298. }
  299. void DIALOG_PLOT_SCHEMATIC::OnPlotAll( wxCommandEvent& event )
  300. {
  301. plotSchematic( true );
  302. }
  303. void DIALOG_PLOT_SCHEMATIC::plotSchematic( bool aPlotAll )
  304. {
  305. KIGFX::SCH_RENDER_SETTINGS renderSettings( *m_parent->GetRenderSettings() );
  306. getPlotOptions( &renderSettings );
  307. switch( GetPlotFileFormat() )
  308. {
  309. default:
  310. case PLOT_FORMAT::POST:
  311. createPSFiles( aPlotAll, getPlotDrawingSheet(), &renderSettings );
  312. break;
  313. case PLOT_FORMAT::DXF:
  314. createDXFFiles( aPlotAll, getPlotDrawingSheet(), &renderSettings );
  315. break;
  316. case PLOT_FORMAT::PDF:
  317. createPDFFile( aPlotAll, getPlotDrawingSheet(), &renderSettings );
  318. break;
  319. case PLOT_FORMAT::SVG:
  320. createSVGFiles( aPlotAll, getPlotDrawingSheet(), &renderSettings );
  321. break;
  322. case PLOT_FORMAT::HPGL:
  323. createHPGLFiles( aPlotAll, getPlotDrawingSheet(), &renderSettings );
  324. break;
  325. }
  326. }
  327. wxFileName DIALOG_PLOT_SCHEMATIC::createPlotFileName( const wxString& aPlotFileName,
  328. const wxString& aExtension,
  329. REPORTER* aReporter )
  330. {
  331. wxFileName retv;
  332. wxFileName tmp;
  333. tmp.SetPath( getOutputPath() );
  334. retv.SetPath( tmp.GetPath() );
  335. if( !aPlotFileName.IsEmpty() )
  336. retv.SetName( aPlotFileName );
  337. else
  338. retv.SetName( _( "Schematic" ) );
  339. retv.SetExt( aExtension );
  340. if( !EnsureFileDirectoryExists( &tmp, retv.GetFullName(), aReporter ) || !tmp.IsDirWritable() )
  341. {
  342. wxString msg = wxString::Format( _( "Failed to write plot files to folder '%s'." ),
  343. tmp.GetPath() );
  344. aReporter->Report( msg, RPT_SEVERITY_ERROR );
  345. retv.Clear();
  346. SCHEMATIC_SETTINGS& settings = m_parent->Schematic().Settings();
  347. settings.m_PlotDirectoryName.Clear();
  348. m_configChanged = true;
  349. }
  350. else
  351. {
  352. retv.SetPath( tmp.GetPath() );
  353. }
  354. wxLogTrace( tracePathsAndFiles, "Writing plot file '%s'.", retv.GetFullPath() );
  355. return retv;
  356. }
  357. void DIALOG_PLOT_SCHEMATIC::createDXFFiles( bool aPlotAll, bool aPlotDrawingSheet,
  358. RENDER_SETTINGS* aRenderSettings )
  359. {
  360. SCH_EDIT_FRAME* schframe = m_parent;
  361. SCH_SHEET_PATH oldsheetpath = schframe->GetCurrentSheet();
  362. /* When printing all pages, the printed page is not the current page. In complex hierarchies,
  363. * we must update symbol references and other parameters in the given printed SCH_SCREEN,
  364. * according to the sheet path because in complex hierarchies a SCH_SCREEN (a drawing ) is
  365. * shared between many sheets and symbol references depend on the actual sheet path used.
  366. */
  367. SCH_SHEET_LIST sheetList;
  368. if( aPlotAll )
  369. {
  370. sheetList.BuildSheetList( &schframe->Schematic().Root(), true );
  371. sheetList.SortByPageNumbers();
  372. }
  373. else
  374. {
  375. sheetList.push_back( schframe->GetCurrentSheet() );
  376. }
  377. REPORTER& reporter = m_MessagesBox->Reporter();
  378. for( unsigned i = 0; i < sheetList.size(); i++ )
  379. {
  380. schframe->SetCurrentSheet( sheetList[i] );
  381. schframe->GetCurrentSheet().UpdateAllScreenReferences();
  382. schframe->SetSheetNumberAndCount();
  383. SCH_SCREEN* screen = schframe->GetCurrentSheet().LastScreen();
  384. wxPoint plot_offset;
  385. wxString msg;
  386. try
  387. {
  388. wxString fname = schframe->GetUniqueFilenameForCurrentSheet();
  389. // The sub sheet can be in a sub_hierarchy, but we plot the file in the
  390. // main project folder (or the folder specified by the caller),
  391. // so replace separators to create a unique filename:
  392. fname.Replace( "/", "_" );
  393. fname.Replace( "\\", "_" );
  394. wxString ext = DXF_PLOTTER::GetDefaultFileExtension();
  395. wxFileName plotFileName = createPlotFileName( fname, ext, &reporter );
  396. if( !plotFileName.IsOk() )
  397. return;
  398. if( plotOneSheetDXF( plotFileName.GetFullPath(), screen, aRenderSettings,
  399. plot_offset, 1.0, aPlotDrawingSheet ) )
  400. {
  401. msg.Printf( _( "Plotted to '%s'." ), plotFileName.GetFullPath() );
  402. reporter.Report( msg, RPT_SEVERITY_ACTION );
  403. }
  404. else // Error
  405. {
  406. msg.Printf( _( "Failed to create file '%s'." ), plotFileName.GetFullPath() );
  407. reporter.Report( msg, RPT_SEVERITY_ERROR );
  408. }
  409. }
  410. catch( IO_ERROR& e )
  411. {
  412. msg.Printf( wxT( "DXF Plotter exception: %s"), e.What() );
  413. reporter.Report( msg, RPT_SEVERITY_ERROR );
  414. schframe->SetCurrentSheet( oldsheetpath );
  415. schframe->GetCurrentSheet().UpdateAllScreenReferences();
  416. schframe->SetSheetNumberAndCount();
  417. return;
  418. }
  419. }
  420. reporter.ReportTail( _( "Done." ), RPT_SEVERITY_INFO );
  421. schframe->SetCurrentSheet( oldsheetpath );
  422. schframe->GetCurrentSheet().UpdateAllScreenReferences();
  423. schframe->SetSheetNumberAndCount();
  424. }
  425. bool DIALOG_PLOT_SCHEMATIC::plotOneSheetDXF( const wxString& aFileName,
  426. SCH_SCREEN* aScreen,
  427. RENDER_SETTINGS* aRenderSettings,
  428. const wxPoint& aPlotOffset,
  429. double aScale,
  430. bool aPlotFrameRef )
  431. {
  432. aRenderSettings->LoadColors( getColorSettings() );
  433. aRenderSettings->SetDefaultPenWidth( 0 );
  434. const PAGE_INFO& pageInfo = aScreen->GetPageSettings();
  435. DXF_PLOTTER* plotter = new DXF_PLOTTER();
  436. plotter->SetRenderSettings( aRenderSettings );
  437. plotter->SetPageSettings( pageInfo );
  438. plotter->SetColorMode( getModeColor() );
  439. // Currently, plot units are in decimil
  440. plotter->SetViewport( aPlotOffset, IU_PER_MILS/10, aScale, false );
  441. // Init :
  442. plotter->SetCreator( wxT( "Eeschema-DXF" ) );
  443. if( ! plotter->OpenFile( aFileName ) )
  444. {
  445. delete plotter;
  446. return false;
  447. }
  448. LOCALE_IO toggle;
  449. plotter->StartPlot();
  450. if( aPlotFrameRef )
  451. {
  452. wxString sheetName = m_parent->GetCurrentSheet().Last()->GetName();
  453. wxString sheetPath = m_parent->GetCurrentSheet().PathHumanReadable();
  454. PlotDrawingSheet( plotter, &m_parent->Prj(), m_parent->GetTitleBlock(), pageInfo,
  455. aScreen->GetPageNumber(), aScreen->GetPageCount(),
  456. sheetName, sheetPath, aScreen->GetFileName(),
  457. plotter->GetColorMode() ?
  458. plotter->RenderSettings()->GetLayerColor( LAYER_SCHEMATIC_DRAWINGSHEET ) :
  459. COLOR4D::BLACK, aScreen->GetVirtualPageNumber() == 1 );
  460. }
  461. aScreen->Plot( plotter );
  462. // finish
  463. plotter->EndPlot();
  464. delete plotter;
  465. return true;
  466. }
  467. void DIALOG_PLOT_SCHEMATIC::setHpglPenWidth()
  468. {
  469. m_HPGLPenSize = m_penWidth.GetValue();
  470. if( m_HPGLPenSize > Millimeter2iu( 2 ) )
  471. m_HPGLPenSize = Millimeter2iu( 2 );
  472. if( m_HPGLPenSize < Millimeter2iu( 0.01 ) )
  473. m_HPGLPenSize = Millimeter2iu( 0.01 );
  474. }
  475. void DIALOG_PLOT_SCHEMATIC::createHPGLFiles( bool aPlotAll, bool aPlotFrameRef,
  476. RENDER_SETTINGS* aRenderSettings )
  477. {
  478. SCH_SCREEN* screen = m_parent->GetScreen();
  479. SCH_SHEET_PATH oldsheetpath = m_parent->GetCurrentSheet();
  480. /* When printing all pages, the printed page is not the current page. In complex hierarchies,
  481. * we must update symbol references and other parameters in the given printed SCH_SCREEN,
  482. * according to the sheet path because in complex hierarchies a SCH_SCREEN (a drawing ) is
  483. * shared between many sheets and symbol references depend on the actual sheet path used.
  484. */
  485. SCH_SHEET_LIST sheetList;
  486. if( aPlotAll )
  487. {
  488. sheetList.BuildSheetList( &m_parent->Schematic().Root(), true );
  489. sheetList.SortByPageNumbers();
  490. }
  491. else
  492. {
  493. sheetList.push_back( m_parent->GetCurrentSheet() );
  494. }
  495. REPORTER& reporter = m_MessagesBox->Reporter();
  496. setHpglPenWidth();
  497. for( unsigned i = 0; i < sheetList.size(); i++ )
  498. {
  499. m_parent->SetCurrentSheet( sheetList[i] );
  500. m_parent->GetCurrentSheet().UpdateAllScreenReferences();
  501. m_parent->SetSheetNumberAndCount();
  502. screen = m_parent->GetCurrentSheet().LastScreen();
  503. if( !screen ) // LastScreen() may return NULL
  504. screen = m_parent->GetScreen();
  505. const PAGE_INFO& curPage = screen->GetPageSettings();
  506. PAGE_INFO plotPage = curPage;
  507. // if plotting on a page size other than curPage
  508. if( m_paperSizeOption->GetSelection() != PAGE_DEFAULT )
  509. plotPage.SetType( plot_sheet_list( m_paperSizeOption->GetSelection() ) );
  510. // Calculation of conversion scales.
  511. double plot_scale = (double) plotPage.GetWidthMils() / curPage.GetWidthMils();
  512. // Calculate offsets
  513. wxPoint plotOffset;
  514. wxString msg;
  515. if( getPlotOriginAndUnits() == HPGL_PLOT_ORIGIN_AND_UNITS::PLOTTER_CENTER )
  516. {
  517. plotOffset.x = plotPage.GetWidthIU() / 2;
  518. plotOffset.y = -plotPage.GetHeightIU() / 2;
  519. }
  520. try
  521. {
  522. wxString fname = m_parent->GetUniqueFilenameForCurrentSheet();
  523. // The sub sheet can be in a sub_hierarchy, but we plot the file in the
  524. // main project folder (or the folder specified by the caller),
  525. // so replace separators to create a unique filename:
  526. fname.Replace( "/", "_" );
  527. fname.Replace( "\\", "_" );
  528. wxString ext = HPGL_PLOTTER::GetDefaultFileExtension();
  529. wxFileName plotFileName = createPlotFileName( fname, ext, &reporter );
  530. if( !plotFileName.IsOk() )
  531. return;
  532. LOCALE_IO toggle;
  533. if( plotOneSheetHpgl( plotFileName.GetFullPath(), screen, plotPage, aRenderSettings,
  534. plotOffset, plot_scale, aPlotFrameRef, getPlotOriginAndUnits() ) )
  535. {
  536. msg.Printf( _( "Plotted to '%s'." ), plotFileName.GetFullPath() );
  537. reporter.Report( msg, RPT_SEVERITY_ACTION );
  538. }
  539. else
  540. {
  541. msg.Printf( _( "Failed to create file '%s'." ), plotFileName.GetFullPath() );
  542. reporter.Report( msg, RPT_SEVERITY_ERROR );
  543. }
  544. }
  545. catch( IO_ERROR& e )
  546. {
  547. msg.Printf( wxT( "HPGL Plotter exception: %s"), e.What() );
  548. reporter.Report( msg, RPT_SEVERITY_ERROR );
  549. }
  550. }
  551. reporter.ReportTail( _( "Done." ), RPT_SEVERITY_INFO );
  552. m_parent->SetCurrentSheet( oldsheetpath );
  553. m_parent->GetCurrentSheet().UpdateAllScreenReferences();
  554. m_parent->SetSheetNumberAndCount();
  555. }
  556. bool DIALOG_PLOT_SCHEMATIC::plotOneSheetHpgl( const wxString& aFileName,
  557. SCH_SCREEN* aScreen,
  558. const PAGE_INFO& aPageInfo,
  559. RENDER_SETTINGS* aRenderSettings,
  560. const wxPoint& aPlot0ffset,
  561. double aScale,
  562. bool aPlotFrameRef,
  563. HPGL_PLOT_ORIGIN_AND_UNITS aOriginAndUnits )
  564. {
  565. HPGL_PLOTTER* plotter = new HPGL_PLOTTER();
  566. // Currently, plot units are in decimil
  567. plotter->SetPageSettings( aPageInfo );
  568. plotter->SetRenderSettings( aRenderSettings );
  569. plotter->RenderSettings()->LoadColors( getColorSettings() );
  570. plotter->SetColorMode( getModeColor() );
  571. plotter->SetViewport( aPlot0ffset, IU_PER_MILS/10, aScale, false );
  572. // TODO this could be configurable
  573. plotter->SetTargetChordLength( Millimeter2iu( 0.6 ) );
  574. switch( aOriginAndUnits )
  575. {
  576. case HPGL_PLOT_ORIGIN_AND_UNITS::PLOTTER_BOT_LEFT:
  577. case HPGL_PLOT_ORIGIN_AND_UNITS::PLOTTER_CENTER:
  578. default:
  579. plotter->SetUserCoords( false );
  580. break;
  581. case HPGL_PLOT_ORIGIN_AND_UNITS::USER_FIT_PAGE:
  582. plotter->SetUserCoords( true );
  583. plotter->SetUserCoordsFit( false );
  584. break;
  585. case HPGL_PLOT_ORIGIN_AND_UNITS::USER_FIT_CONTENT:
  586. plotter->SetUserCoords( true );
  587. plotter->SetUserCoordsFit( true );
  588. break;
  589. }
  590. // Init :
  591. plotter->SetCreator( wxT( "Eeschema-HPGL" ) );
  592. if( !plotter->OpenFile( aFileName ) )
  593. {
  594. delete plotter;
  595. return false;
  596. }
  597. LOCALE_IO toggle;
  598. // Pen num and pen speed are not initialized here.
  599. // Default HPGL driver values are used
  600. plotter->SetPenDiameter( m_HPGLPenSize );
  601. plotter->StartPlot();
  602. if( aPlotFrameRef )
  603. {
  604. wxString sheetName = m_parent->GetCurrentSheet().Last()->GetName();
  605. wxString sheetPath = m_parent->GetCurrentSheet().PathHumanReadable();
  606. PlotDrawingSheet( plotter, &m_parent->Prj(), m_parent->GetTitleBlock(), aPageInfo,
  607. aScreen->GetPageNumber(), aScreen->GetPageCount(), sheetName, sheetPath,
  608. aScreen->GetFileName(), COLOR4D::BLACK,
  609. aScreen->GetVirtualPageNumber() == 1 );
  610. }
  611. aScreen->Plot( plotter );
  612. plotter->EndPlot();
  613. delete plotter;
  614. return true;
  615. }
  616. void DIALOG_PLOT_SCHEMATIC::createPDFFile( bool aPlotAll, bool aPlotDrawingSheet,
  617. RENDER_SETTINGS* aRenderSettings )
  618. {
  619. SCH_SHEET_PATH oldsheetpath = m_parent->GetCurrentSheet(); // sheetpath is saved here
  620. /* When printing all pages, the printed page is not the current page. In complex hierarchies,
  621. * we must update symbol references and other parameters in the given printed SCH_SCREEN,
  622. * according to the sheet path because in complex hierarchies a SCH_SCREEN (a drawing ) is
  623. * shared between many sheets and symbol references depend on the actual sheet path used.
  624. */
  625. SCH_SHEET_LIST sheetList;
  626. if( aPlotAll )
  627. {
  628. sheetList.BuildSheetList( &m_parent->Schematic().Root(), true );
  629. sheetList.SortByPageNumbers();
  630. }
  631. else
  632. {
  633. sheetList.push_back( m_parent->GetCurrentSheet() );
  634. }
  635. // Allocate the plotter and set the job level parameter
  636. PDF_PLOTTER* plotter = new PDF_PLOTTER();
  637. plotter->SetRenderSettings( aRenderSettings );
  638. plotter->SetColorMode( getModeColor() );
  639. plotter->SetCreator( wxT( "Eeschema-PDF" ) );
  640. plotter->SetTitle( m_parent->GetTitleBlock().GetTitle() );
  641. wxString msg;
  642. wxFileName plotFileName;
  643. REPORTER& reporter = m_MessagesBox->Reporter();
  644. LOCALE_IO toggle; // Switch the locale to standard C
  645. for( unsigned i = 0; i < sheetList.size(); i++ )
  646. {
  647. m_parent->SetCurrentSheet( sheetList[i] );
  648. m_parent->GetCurrentSheet().UpdateAllScreenReferences();
  649. m_parent->SetSheetNumberAndCount();
  650. SCH_SCREEN* screen = m_parent->GetCurrentSheet().LastScreen();
  651. if( i == 0 )
  652. {
  653. try
  654. {
  655. wxString fname = m_parent->GetUniqueFilenameForCurrentSheet();
  656. // The sub sheet can be in a sub_hierarchy, but we plot the file in the main
  657. // project folder (or the folder specified by the caller), so replace separators
  658. // to create a unique filename:
  659. fname.Replace( "/", "_" );
  660. fname.Replace( "\\", "_" );
  661. wxString ext = PDF_PLOTTER::GetDefaultFileExtension();
  662. plotFileName = createPlotFileName( fname, ext, &reporter );
  663. if( !plotFileName.IsOk() )
  664. return;
  665. if( !plotter->OpenFile( plotFileName.GetFullPath() ) )
  666. {
  667. msg.Printf( _( "Failed to create file '%s'." ), plotFileName.GetFullPath() );
  668. reporter.Report( msg, RPT_SEVERITY_ERROR );
  669. delete plotter;
  670. return;
  671. }
  672. // Open the plotter and do the first page
  673. setupPlotPagePDF( plotter, screen );
  674. plotter->StartPlot();
  675. }
  676. catch( const IO_ERROR& e )
  677. {
  678. // Cannot plot PDF file
  679. msg.Printf( wxT( "PDF Plotter exception: %s" ), e.What() );
  680. reporter.Report( msg, RPT_SEVERITY_ERROR );
  681. restoreEnvironment( plotter, oldsheetpath );
  682. return;
  683. }
  684. }
  685. else
  686. {
  687. /* For the following pages you need to close the (finished) page,
  688. * reconfigure, and then start a new one */
  689. plotter->ClosePage();
  690. setupPlotPagePDF( plotter, screen );
  691. plotter->StartPage();
  692. }
  693. plotOneSheetPDF( plotter, screen, aPlotDrawingSheet );
  694. }
  695. // Everything done, close the plot and restore the environment
  696. msg.Printf( _( "Plotted to '%s'.\n" ), plotFileName.GetFullPath() );
  697. reporter.Report( msg, RPT_SEVERITY_ACTION );
  698. reporter.ReportTail( _( "Done." ), RPT_SEVERITY_INFO );
  699. restoreEnvironment( plotter, oldsheetpath );
  700. }
  701. void DIALOG_PLOT_SCHEMATIC::restoreEnvironment( PDF_PLOTTER* aPlotter,
  702. SCH_SHEET_PATH& aOldsheetpath )
  703. {
  704. aPlotter->EndPlot();
  705. delete aPlotter;
  706. // Restore the previous sheet
  707. m_parent->SetCurrentSheet( aOldsheetpath );
  708. m_parent->GetCurrentSheet().UpdateAllScreenReferences();
  709. m_parent->SetSheetNumberAndCount();
  710. }
  711. void DIALOG_PLOT_SCHEMATIC::plotOneSheetPDF( PLOTTER* aPlotter, SCH_SCREEN* aScreen,
  712. bool aPlotDrawingSheet )
  713. {
  714. if( m_plotBackgroundColor->GetValue() && aPlotter->GetColorMode() )
  715. {
  716. aPlotter->SetColor( aPlotter->RenderSettings()->GetBackgroundColor() );
  717. wxPoint end( aPlotter->PageSettings().GetWidthIU(),
  718. aPlotter->PageSettings().GetHeightIU() );
  719. aPlotter->Rect( wxPoint( 0, 0 ), end, FILL_T::FILLED_SHAPE, 1.0 );
  720. }
  721. if( aPlotDrawingSheet )
  722. {
  723. COLOR4D color = COLOR4D::BLACK;
  724. if( aPlotter->GetColorMode() )
  725. color = aPlotter->RenderSettings()->GetLayerColor( LAYER_SCHEMATIC_DRAWINGSHEET );
  726. wxString sheetName = m_parent->GetCurrentSheet().Last()->GetName();
  727. wxString sheetPath = m_parent->GetCurrentSheet().PathHumanReadable();
  728. PlotDrawingSheet( aPlotter, &aScreen->Schematic()->Prj(), m_parent->GetTitleBlock(),
  729. m_parent->GetPageSettings(), aScreen->GetPageNumber(),
  730. aScreen->GetPageCount(), sheetName, sheetPath,
  731. aScreen->GetFileName(), color, aScreen->GetVirtualPageNumber() == 1 );
  732. }
  733. aScreen->Plot( aPlotter );
  734. }
  735. void DIALOG_PLOT_SCHEMATIC::setupPlotPagePDF( PLOTTER* aPlotter, SCH_SCREEN* aScreen )
  736. {
  737. PAGE_INFO plotPage; // page size selected to plot
  738. // Considerations on page size and scaling requests
  739. const PAGE_INFO& actualPage = aScreen->GetPageSettings(); // page size selected in schematic
  740. switch( m_pageSizeSelect )
  741. {
  742. case PAGE_SIZE_A:
  743. plotPage.SetType( wxT( "A" ) );
  744. plotPage.SetPortrait( actualPage.IsPortrait() );
  745. break;
  746. case PAGE_SIZE_A4:
  747. plotPage.SetType( wxT( "A4" ) );
  748. plotPage.SetPortrait( actualPage.IsPortrait() );
  749. break;
  750. case PAGE_SIZE_AUTO:
  751. default:
  752. plotPage = actualPage;
  753. break;
  754. }
  755. double scalex = (double) plotPage.GetWidthMils() / actualPage.GetWidthMils();
  756. double scaley = (double) plotPage.GetHeightMils() / actualPage.GetHeightMils();
  757. double scale = std::min( scalex, scaley );
  758. aPlotter->SetPageSettings( plotPage );
  759. // Currently, plot units are in decimil
  760. aPlotter->SetViewport( wxPoint( 0, 0 ), IU_PER_MILS/10, scale, false );
  761. }
  762. void DIALOG_PLOT_SCHEMATIC::createPSFiles( bool aPlotAll, bool aPlotFrameRef,
  763. RENDER_SETTINGS* aRenderSettings )
  764. {
  765. SCH_SHEET_PATH oldsheetpath = m_parent->GetCurrentSheet(); // sheetpath is saved here
  766. PAGE_INFO plotPage; // page size selected to plot
  767. wxString msg;
  768. REPORTER& reporter = m_MessagesBox->Reporter();
  769. /* When printing all pages, the printed page is not the current page.
  770. * In complex hierarchies, we must update symbol references and other parameters in the
  771. * given printed SCH_SCREEN, accordant to the sheet path because in complex hierarchies
  772. * a SCH_SCREEN (a drawing ) is shared between many sheets and symbol references
  773. * depend on the actual sheet path used.
  774. */
  775. SCH_SHEET_LIST sheetList;
  776. if( aPlotAll )
  777. {
  778. sheetList.BuildSheetList( &m_parent->Schematic().Root(), true );
  779. sheetList.SortByPageNumbers();
  780. }
  781. else
  782. {
  783. sheetList.push_back( m_parent->GetCurrentSheet() );
  784. }
  785. for( unsigned i = 0; i < sheetList.size(); i++ )
  786. {
  787. m_parent->SetCurrentSheet( sheetList[i] );
  788. m_parent->GetCurrentSheet().UpdateAllScreenReferences();
  789. m_parent->SetSheetNumberAndCount();
  790. SCH_SCREEN* screen = m_parent->GetCurrentSheet().LastScreen();
  791. PAGE_INFO actualPage = screen->GetPageSettings();
  792. switch( m_pageSizeSelect )
  793. {
  794. case PAGE_SIZE_A:
  795. plotPage.SetType( wxT( "A" ) );
  796. plotPage.SetPortrait( actualPage.IsPortrait() );
  797. break;
  798. case PAGE_SIZE_A4:
  799. plotPage.SetType( wxT( "A4" ) );
  800. plotPage.SetPortrait( actualPage.IsPortrait() );
  801. break;
  802. case PAGE_SIZE_AUTO:
  803. default:
  804. plotPage = actualPage;
  805. break;
  806. }
  807. double scalex = (double) plotPage.GetWidthMils() / actualPage.GetWidthMils();
  808. double scaley = (double) plotPage.GetHeightMils() / actualPage.GetHeightMils();
  809. double scale = std::min( scalex, scaley );
  810. wxPoint plot_offset;
  811. try
  812. {
  813. wxString fname = m_parent->GetUniqueFilenameForCurrentSheet();
  814. // The sub sheet can be in a sub_hierarchy, but we plot the file in the
  815. // main project folder (or the folder specified by the caller),
  816. // so replace separators to create a unique filename:
  817. fname.Replace( "/", "_" );
  818. fname.Replace ("\\", "_" );
  819. wxString ext = PS_PLOTTER::GetDefaultFileExtension();
  820. wxFileName plotFileName = createPlotFileName( fname, ext, &reporter );
  821. if( !plotFileName.IsOk() )
  822. return;
  823. if( plotOneSheetPS( plotFileName.GetFullPath(), screen, aRenderSettings, plotPage,
  824. plot_offset, scale, aPlotFrameRef ) )
  825. {
  826. msg.Printf( _( "Plotted to '%s'." ), plotFileName.GetFullPath() );
  827. reporter.Report( msg, RPT_SEVERITY_ACTION );
  828. }
  829. else
  830. {
  831. // Error
  832. msg.Printf( _( "Failed to create file '%s'." ), plotFileName.GetFullPath() );
  833. reporter.Report( msg, RPT_SEVERITY_ERROR );
  834. }
  835. }
  836. catch( IO_ERROR& e )
  837. {
  838. msg.Printf( wxT( "PS Plotter exception: %s"), e.What() );
  839. reporter.Report( msg, RPT_SEVERITY_ERROR );
  840. }
  841. }
  842. reporter.ReportTail( _( "Done." ), RPT_SEVERITY_INFO );
  843. m_parent->SetCurrentSheet( oldsheetpath );
  844. m_parent->GetCurrentSheet().UpdateAllScreenReferences();
  845. m_parent->SetSheetNumberAndCount();
  846. }
  847. bool DIALOG_PLOT_SCHEMATIC::plotOneSheetPS( const wxString& aFileName,
  848. SCH_SCREEN* aScreen,
  849. RENDER_SETTINGS* aRenderSettings,
  850. const PAGE_INFO& aPageInfo,
  851. const wxPoint& aPlot0ffset,
  852. double aScale,
  853. bool aPlotFrameRef )
  854. {
  855. PS_PLOTTER* plotter = new PS_PLOTTER();
  856. plotter->SetRenderSettings( aRenderSettings );
  857. plotter->SetPageSettings( aPageInfo );
  858. plotter->SetColorMode( getModeColor() );
  859. // Currently, plot units are in decimil
  860. plotter->SetViewport( aPlot0ffset, IU_PER_MILS/10, aScale, false );
  861. // Init :
  862. plotter->SetCreator( wxT( "Eeschema-PS" ) );
  863. if( ! plotter->OpenFile( aFileName ) )
  864. {
  865. delete plotter;
  866. return false;
  867. }
  868. LOCALE_IO toggle; // Switch the locale to standard C
  869. plotter->StartPlot();
  870. if( m_plotBackgroundColor->GetValue() && plotter->GetColorMode() )
  871. {
  872. plotter->SetColor( plotter->RenderSettings()->GetLayerColor( LAYER_SCHEMATIC_BACKGROUND ) );
  873. wxPoint end( plotter->PageSettings().GetWidthIU(), plotter->PageSettings().GetHeightIU() );
  874. plotter->Rect( wxPoint( 0, 0 ), end, FILL_T::FILLED_SHAPE, 1.0 );
  875. }
  876. if( aPlotFrameRef )
  877. {
  878. wxString sheetName = m_parent->GetCurrentSheet().Last()->GetName();
  879. wxString sheetPath = m_parent->GetCurrentSheet().PathHumanReadable();
  880. PlotDrawingSheet( plotter, &aScreen->Schematic()->Prj(), m_parent->GetTitleBlock(),
  881. aPageInfo, aScreen->GetPageNumber(), aScreen->GetPageCount(),
  882. sheetName, sheetPath, aScreen->GetFileName(),
  883. plotter->GetColorMode() ?
  884. plotter->RenderSettings()->GetLayerColor( LAYER_SCHEMATIC_DRAWINGSHEET ) :
  885. COLOR4D::BLACK, aScreen->GetVirtualPageNumber() == 1 );
  886. }
  887. aScreen->Plot( plotter );
  888. plotter->EndPlot();
  889. delete plotter;
  890. return true;
  891. }
  892. void DIALOG_PLOT_SCHEMATIC::createSVGFiles( bool aPrintAll, bool aPrintFrameRef,
  893. RENDER_SETTINGS* aRenderSettings )
  894. {
  895. wxString msg;
  896. REPORTER& reporter = m_MessagesBox->Reporter();
  897. SCH_SHEET_PATH oldsheetpath = m_parent->GetCurrentSheet();
  898. SCH_SHEET_LIST sheetList;
  899. if( aPrintAll )
  900. {
  901. sheetList.BuildSheetList( &m_parent->Schematic().Root(), true );
  902. sheetList.SortByPageNumbers();
  903. }
  904. else
  905. {
  906. sheetList.push_back( m_parent->GetCurrentSheet() );
  907. }
  908. for( unsigned i = 0; i < sheetList.size(); i++ )
  909. {
  910. SCH_SCREEN* screen;
  911. m_parent->SetCurrentSheet( sheetList[i] );
  912. m_parent->GetCurrentSheet().UpdateAllScreenReferences();
  913. m_parent->SetSheetNumberAndCount();
  914. screen = m_parent->GetCurrentSheet().LastScreen();
  915. try
  916. {
  917. wxString fname = m_parent->GetUniqueFilenameForCurrentSheet();
  918. // The sub sheet can be in a sub_hierarchy, but we plot the file in the
  919. // main project folder (or the folder specified by the caller),
  920. // so replace separators to create a unique filename:
  921. fname.Replace( "/", "_" );
  922. fname.Replace( "\\", "_" );
  923. wxString ext = SVG_PLOTTER::GetDefaultFileExtension();
  924. wxFileName plotFileName = createPlotFileName( fname, ext, &reporter );
  925. if( !plotFileName.IsOk() )
  926. return;
  927. bool success = plotOneSheetSVG( plotFileName.GetFullPath(), screen, aRenderSettings,
  928. getModeColor() ? false : true, aPrintFrameRef );
  929. if( !success )
  930. {
  931. msg.Printf( _( "Failed to create file '%s'." ), plotFileName.GetFullPath() );
  932. reporter.Report( msg, RPT_SEVERITY_ERROR );
  933. }
  934. else
  935. {
  936. msg.Printf( _( "Plotted to '%s'." ), plotFileName.GetFullPath() );
  937. reporter.Report( msg, RPT_SEVERITY_ACTION );
  938. }
  939. }
  940. catch( const IO_ERROR& e )
  941. {
  942. // Cannot plot SVG file
  943. msg.Printf( wxT( "SVG Plotter exception: %s" ), e.What() );
  944. reporter.Report( msg, RPT_SEVERITY_ERROR );
  945. break;
  946. }
  947. }
  948. reporter.ReportTail( _( "Done" ), RPT_SEVERITY_INFO );
  949. m_parent->SetCurrentSheet( oldsheetpath );
  950. m_parent->GetCurrentSheet().UpdateAllScreenReferences();
  951. m_parent->SetSheetNumberAndCount();
  952. }
  953. bool DIALOG_PLOT_SCHEMATIC::plotOneSheetSVG( const wxString& aFileName,
  954. SCH_SCREEN* aScreen,
  955. RENDER_SETTINGS* aRenderSettings,
  956. bool aPlotBlackAndWhite,
  957. bool aPlotFrameRef )
  958. {
  959. const PAGE_INFO& pageInfo = aScreen->GetPageSettings();
  960. SVG_PLOTTER* plotter = new SVG_PLOTTER();
  961. plotter->SetRenderSettings( aRenderSettings );
  962. plotter->SetPageSettings( pageInfo );
  963. plotter->SetColorMode( aPlotBlackAndWhite ? false : true );
  964. wxPoint plot_offset;
  965. double scale = 1.0;
  966. // Currently, plot units are in decimil
  967. plotter->SetViewport( plot_offset, IU_PER_MILS/10, scale, false );
  968. // Init :
  969. plotter->SetCreator( wxT( "Eeschema-SVG" ) );
  970. if( ! plotter->OpenFile( aFileName ) )
  971. {
  972. delete plotter;
  973. return false;
  974. }
  975. LOCALE_IO toggle;
  976. plotter->StartPlot();
  977. if( m_plotBackgroundColor->GetValue() && plotter->GetColorMode() )
  978. {
  979. plotter->SetColor( plotter->RenderSettings()->GetLayerColor( LAYER_SCHEMATIC_BACKGROUND ) );
  980. wxPoint end( plotter->PageSettings().GetWidthIU(),
  981. plotter->PageSettings().GetHeightIU() );
  982. plotter->Rect( wxPoint( 0, 0 ), end, FILL_T::FILLED_SHAPE, 1.0 );
  983. }
  984. if( aPlotFrameRef )
  985. {
  986. wxString sheetName = m_parent->GetCurrentSheet().Last()->GetName();
  987. wxString sheetPath = m_parent->GetCurrentSheet().PathHumanReadable();
  988. PlotDrawingSheet( plotter, &aScreen->Schematic()->Prj(), m_parent->GetTitleBlock(),
  989. pageInfo, aScreen->GetPageNumber(), aScreen->GetPageCount(),
  990. sheetName, sheetPath, aScreen->GetFileName(),
  991. plotter->GetColorMode() ?
  992. plotter->RenderSettings()->GetLayerColor( LAYER_SCHEMATIC_DRAWINGSHEET ) :
  993. COLOR4D::BLACK, aScreen->GetVirtualPageNumber() == 1 );
  994. }
  995. aScreen->Plot( plotter );
  996. plotter->EndPlot();
  997. delete plotter;
  998. return true;
  999. }
  1000. wxString DIALOG_PLOT_SCHEMATIC::getOutputPath()
  1001. {
  1002. wxString msg;
  1003. wxString extMsg;
  1004. wxFileName fn;
  1005. extMsg.Printf( _( "Falling back to user path '%s'." ),
  1006. wxStandardPaths::Get().GetDocumentsDir() );
  1007. // Build the absolute path of current output directory to preselect it in the file browser.
  1008. std::function<bool( wxString* )> textResolver =
  1009. [&]( wxString* token ) -> bool
  1010. {
  1011. return m_parent->Schematic().ResolveTextVar( token, 0 );
  1012. };
  1013. wxString path = m_outputDirectoryName->GetValue();
  1014. path = ExpandTextVars( path, &textResolver, nullptr, &Prj() );
  1015. path = ExpandEnvVarSubstitutions( path, &Prj() );
  1016. fn.SetPath( path );
  1017. // If the contents of the path edit control results in an absolute path, return it as is.
  1018. if( fn.IsAbsolute() )
  1019. return path;
  1020. // When editing a schematic that is not part of a project in the stand alone mode, the
  1021. // project path is not defined.
  1022. if( Prj().IsNullProject() )
  1023. {
  1024. SCH_SCREEN* screen = m_parent->Schematic().RootScreen();
  1025. if( screen && !screen->GetFileName().IsEmpty() )
  1026. {
  1027. fn = screen->GetFileName();
  1028. msg.Printf( _( "Cannot normalize path '%s%s'." ), fn.GetPathWithSep(), path );
  1029. fn.SetPath( fn.GetPathWithSep() + path );
  1030. // Normalize always returns true for a non-empty file name so clear the file name
  1031. // and extension so that only the path is normalized.
  1032. fn.SetName( wxEmptyString );
  1033. fn.SetExt( wxEmptyString );
  1034. if( fn.Normalize() )
  1035. {
  1036. path = fn.GetPath();
  1037. }
  1038. else
  1039. {
  1040. wxMessageDialog dlg( this, msg, _( "Warning" ), wxOK | wxCENTER | wxRESIZE_BORDER
  1041. | wxICON_EXCLAMATION | wxSTAY_ON_TOP );
  1042. dlg.SetExtendedMessage( extMsg );
  1043. dlg.ShowModal();
  1044. path = wxStandardPaths::Get().GetDocumentsDir();
  1045. }
  1046. }
  1047. else
  1048. {
  1049. msg = _( "No project or path defined for the current schematic." );
  1050. wxMessageDialog dlg( this, msg, _( "Warning" ), wxOK | wxCENTER | wxRESIZE_BORDER
  1051. | wxICON_EXCLAMATION | wxSTAY_ON_TOP );
  1052. dlg.SetExtendedMessage( extMsg );
  1053. dlg.ShowModal();
  1054. // Always fall back to user's document path if no other absolute path can be normalized.
  1055. path = wxStandardPaths::Get().GetDocumentsDir();
  1056. }
  1057. }
  1058. else
  1059. {
  1060. msg.Printf( _( "Cannot normalize path '%s%s'." ), Prj().GetProjectPath(), path );
  1061. // Build the absolute path of current output directory and the project path.
  1062. fn.SetPath( Prj().GetProjectPath() + path );
  1063. if( fn.Normalize() )
  1064. {
  1065. path = fn.GetPath();
  1066. }
  1067. else
  1068. {
  1069. wxMessageDialog dlg( this, msg, _( "Warning" ),
  1070. wxOK | wxCENTER | wxRESIZE_BORDER | wxICON_EXCLAMATION |
  1071. wxSTAY_ON_TOP );
  1072. dlg.SetExtendedMessage( extMsg );
  1073. dlg.ShowModal();
  1074. path = wxStandardPaths::Get().GetDocumentsDir();
  1075. }
  1076. }
  1077. return path;
  1078. }