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.

1335 lines
45 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. PlotDrawingSheet( plotter, &m_parent->Prj(), m_parent->GetTitleBlock(), pageInfo,
  453. aScreen->GetPageNumber(), aScreen->GetPageCount(),
  454. m_parent->GetScreenDesc(), aScreen->GetFileName(),
  455. plotter->GetColorMode() ?
  456. plotter->RenderSettings()->GetLayerColor( LAYER_SCHEMATIC_DRAWINGSHEET ) :
  457. COLOR4D::BLACK, aScreen->GetVirtualPageNumber() == 1 );
  458. }
  459. aScreen->Plot( plotter );
  460. // finish
  461. plotter->EndPlot();
  462. delete plotter;
  463. return true;
  464. }
  465. void DIALOG_PLOT_SCHEMATIC::setHpglPenWidth()
  466. {
  467. m_HPGLPenSize = m_penWidth.GetValue();
  468. if( m_HPGLPenSize > Millimeter2iu( 2 ) )
  469. m_HPGLPenSize = Millimeter2iu( 2 );
  470. if( m_HPGLPenSize < Millimeter2iu( 0.01 ) )
  471. m_HPGLPenSize = Millimeter2iu( 0.01 );
  472. }
  473. void DIALOG_PLOT_SCHEMATIC::createHPGLFiles( bool aPlotAll, bool aPlotFrameRef,
  474. RENDER_SETTINGS* aRenderSettings )
  475. {
  476. SCH_SCREEN* screen = m_parent->GetScreen();
  477. SCH_SHEET_PATH oldsheetpath = m_parent->GetCurrentSheet();
  478. /* When printing all pages, the printed page is not the current page. In complex hierarchies,
  479. * we must update symbol references and other parameters in the given printed SCH_SCREEN,
  480. * according to the sheet path because in complex hierarchies a SCH_SCREEN (a drawing ) is
  481. * shared between many sheets and symbol references depend on the actual sheet path used.
  482. */
  483. SCH_SHEET_LIST sheetList;
  484. if( aPlotAll )
  485. {
  486. sheetList.BuildSheetList( &m_parent->Schematic().Root(), true );
  487. sheetList.SortByPageNumbers();
  488. }
  489. else
  490. {
  491. sheetList.push_back( m_parent->GetCurrentSheet() );
  492. }
  493. REPORTER& reporter = m_MessagesBox->Reporter();
  494. setHpglPenWidth();
  495. for( unsigned i = 0; i < sheetList.size(); i++ )
  496. {
  497. m_parent->SetCurrentSheet( sheetList[i] );
  498. m_parent->GetCurrentSheet().UpdateAllScreenReferences();
  499. m_parent->SetSheetNumberAndCount();
  500. screen = m_parent->GetCurrentSheet().LastScreen();
  501. if( !screen ) // LastScreen() may return NULL
  502. screen = m_parent->GetScreen();
  503. const PAGE_INFO& curPage = screen->GetPageSettings();
  504. PAGE_INFO plotPage = curPage;
  505. // if plotting on a page size other than curPage
  506. if( m_paperSizeOption->GetSelection() != PAGE_DEFAULT )
  507. plotPage.SetType( plot_sheet_list( m_paperSizeOption->GetSelection() ) );
  508. // Calculation of conversion scales.
  509. double plot_scale = (double) plotPage.GetWidthMils() / curPage.GetWidthMils();
  510. // Calculate offsets
  511. wxPoint plotOffset;
  512. wxString msg;
  513. if( getPlotOriginAndUnits() == HPGL_PLOT_ORIGIN_AND_UNITS::PLOTTER_CENTER )
  514. {
  515. plotOffset.x = plotPage.GetWidthIU() / 2;
  516. plotOffset.y = -plotPage.GetHeightIU() / 2;
  517. }
  518. try
  519. {
  520. wxString fname = m_parent->GetUniqueFilenameForCurrentSheet();
  521. // The sub sheet can be in a sub_hierarchy, but we plot the file in the
  522. // main project folder (or the folder specified by the caller),
  523. // so replace separators to create a unique filename:
  524. fname.Replace( "/", "_" );
  525. fname.Replace( "\\", "_" );
  526. wxString ext = HPGL_PLOTTER::GetDefaultFileExtension();
  527. wxFileName plotFileName = createPlotFileName( fname, ext, &reporter );
  528. if( !plotFileName.IsOk() )
  529. return;
  530. LOCALE_IO toggle;
  531. if( plotOneSheetHpgl( plotFileName.GetFullPath(), screen, plotPage, aRenderSettings,
  532. plotOffset, plot_scale, aPlotFrameRef, getPlotOriginAndUnits() ) )
  533. {
  534. msg.Printf( _( "Plotted to '%s'." ), plotFileName.GetFullPath() );
  535. reporter.Report( msg, RPT_SEVERITY_ACTION );
  536. }
  537. else
  538. {
  539. msg.Printf( _( "Failed to create file '%s'." ), plotFileName.GetFullPath() );
  540. reporter.Report( msg, RPT_SEVERITY_ERROR );
  541. }
  542. }
  543. catch( IO_ERROR& e )
  544. {
  545. msg.Printf( wxT( "HPGL Plotter exception: %s"), e.What() );
  546. reporter.Report( msg, RPT_SEVERITY_ERROR );
  547. }
  548. }
  549. reporter.ReportTail( _( "Done." ), RPT_SEVERITY_INFO );
  550. m_parent->SetCurrentSheet( oldsheetpath );
  551. m_parent->GetCurrentSheet().UpdateAllScreenReferences();
  552. m_parent->SetSheetNumberAndCount();
  553. }
  554. bool DIALOG_PLOT_SCHEMATIC::plotOneSheetHpgl( const wxString& aFileName,
  555. SCH_SCREEN* aScreen,
  556. const PAGE_INFO& aPageInfo,
  557. RENDER_SETTINGS* aRenderSettings,
  558. const wxPoint& aPlot0ffset,
  559. double aScale,
  560. bool aPlotFrameRef,
  561. HPGL_PLOT_ORIGIN_AND_UNITS aOriginAndUnits )
  562. {
  563. HPGL_PLOTTER* plotter = new HPGL_PLOTTER();
  564. // Currently, plot units are in decimil
  565. plotter->SetPageSettings( aPageInfo );
  566. plotter->SetRenderSettings( aRenderSettings );
  567. plotter->RenderSettings()->LoadColors( getColorSettings() );
  568. plotter->SetColorMode( getModeColor() );
  569. plotter->SetViewport( aPlot0ffset, IU_PER_MILS/10, aScale, false );
  570. // TODO this could be configurable
  571. plotter->SetTargetChordLength( Millimeter2iu( 0.6 ) );
  572. switch( aOriginAndUnits )
  573. {
  574. case HPGL_PLOT_ORIGIN_AND_UNITS::PLOTTER_BOT_LEFT:
  575. case HPGL_PLOT_ORIGIN_AND_UNITS::PLOTTER_CENTER:
  576. default:
  577. plotter->SetUserCoords( false );
  578. break;
  579. case HPGL_PLOT_ORIGIN_AND_UNITS::USER_FIT_PAGE:
  580. plotter->SetUserCoords( true );
  581. plotter->SetUserCoordsFit( false );
  582. break;
  583. case HPGL_PLOT_ORIGIN_AND_UNITS::USER_FIT_CONTENT:
  584. plotter->SetUserCoords( true );
  585. plotter->SetUserCoordsFit( true );
  586. break;
  587. }
  588. // Init :
  589. plotter->SetCreator( wxT( "Eeschema-HPGL" ) );
  590. if( !plotter->OpenFile( aFileName ) )
  591. {
  592. delete plotter;
  593. return false;
  594. }
  595. LOCALE_IO toggle;
  596. // Pen num and pen speed are not initialized here.
  597. // Default HPGL driver values are used
  598. plotter->SetPenDiameter( m_HPGLPenSize );
  599. plotter->StartPlot();
  600. if( aPlotFrameRef )
  601. {
  602. PlotDrawingSheet( plotter, &m_parent->Prj(), m_parent->GetTitleBlock(), aPageInfo,
  603. aScreen->GetPageNumber(), aScreen->GetPageCount(),
  604. m_parent->GetScreenDesc(), aScreen->GetFileName(), COLOR4D::BLACK,
  605. aScreen->GetVirtualPageNumber() == 1 );
  606. }
  607. aScreen->Plot( plotter );
  608. plotter->EndPlot();
  609. delete plotter;
  610. return true;
  611. }
  612. void DIALOG_PLOT_SCHEMATIC::createPDFFile( bool aPlotAll, bool aPlotDrawingSheet,
  613. RENDER_SETTINGS* aRenderSettings )
  614. {
  615. SCH_SHEET_PATH oldsheetpath = m_parent->GetCurrentSheet(); // sheetpath is saved here
  616. /* When printing all pages, the printed page is not the current page. In complex hierarchies,
  617. * we must update symbol references and other parameters in the given printed SCH_SCREEN,
  618. * according to the sheet path because in complex hierarchies a SCH_SCREEN (a drawing ) is
  619. * shared between many sheets and symbol references depend on the actual sheet path used.
  620. */
  621. SCH_SHEET_LIST sheetList;
  622. if( aPlotAll )
  623. {
  624. sheetList.BuildSheetList( &m_parent->Schematic().Root(), true );
  625. sheetList.SortByPageNumbers();
  626. }
  627. else
  628. {
  629. sheetList.push_back( m_parent->GetCurrentSheet() );
  630. }
  631. // Allocate the plotter and set the job level parameter
  632. PDF_PLOTTER* plotter = new PDF_PLOTTER();
  633. plotter->SetRenderSettings( aRenderSettings );
  634. plotter->SetColorMode( getModeColor() );
  635. plotter->SetCreator( wxT( "Eeschema-PDF" ) );
  636. plotter->SetTitle( m_parent->GetTitleBlock().GetTitle() );
  637. wxString msg;
  638. wxFileName plotFileName;
  639. REPORTER& reporter = m_MessagesBox->Reporter();
  640. LOCALE_IO toggle; // Switch the locale to standard C
  641. for( unsigned i = 0; i < sheetList.size(); i++ )
  642. {
  643. m_parent->SetCurrentSheet( sheetList[i] );
  644. m_parent->GetCurrentSheet().UpdateAllScreenReferences();
  645. m_parent->SetSheetNumberAndCount();
  646. SCH_SCREEN* screen = m_parent->GetCurrentSheet().LastScreen();
  647. if( i == 0 )
  648. {
  649. try
  650. {
  651. wxString fname = m_parent->GetUniqueFilenameForCurrentSheet();
  652. // The sub sheet can be in a sub_hierarchy, but we plot the file in the main
  653. // project folder (or the folder specified by the caller), so replace separators
  654. // to create a unique filename:
  655. fname.Replace( "/", "_" );
  656. fname.Replace( "\\", "_" );
  657. wxString ext = PDF_PLOTTER::GetDefaultFileExtension();
  658. plotFileName = createPlotFileName( fname, ext, &reporter );
  659. if( !plotFileName.IsOk() )
  660. return;
  661. if( !plotter->OpenFile( plotFileName.GetFullPath() ) )
  662. {
  663. msg.Printf( _( "Failed to create file '%s'." ), plotFileName.GetFullPath() );
  664. reporter.Report( msg, RPT_SEVERITY_ERROR );
  665. delete plotter;
  666. return;
  667. }
  668. // Open the plotter and do the first page
  669. setupPlotPagePDF( plotter, screen );
  670. plotter->StartPlot();
  671. }
  672. catch( const IO_ERROR& e )
  673. {
  674. // Cannot plot PDF file
  675. msg.Printf( wxT( "PDF Plotter exception: %s" ), e.What() );
  676. reporter.Report( msg, RPT_SEVERITY_ERROR );
  677. restoreEnvironment( plotter, oldsheetpath );
  678. return;
  679. }
  680. }
  681. else
  682. {
  683. /* For the following pages you need to close the (finished) page,
  684. * reconfigure, and then start a new one */
  685. plotter->ClosePage();
  686. setupPlotPagePDF( plotter, screen );
  687. plotter->StartPage();
  688. }
  689. plotOneSheetPDF( plotter, screen, aPlotDrawingSheet );
  690. }
  691. // Everything done, close the plot and restore the environment
  692. msg.Printf( _( "Plotted to '%s'.\n" ), plotFileName.GetFullPath() );
  693. reporter.Report( msg, RPT_SEVERITY_ACTION );
  694. reporter.ReportTail( _( "Done." ), RPT_SEVERITY_INFO );
  695. restoreEnvironment( plotter, oldsheetpath );
  696. }
  697. void DIALOG_PLOT_SCHEMATIC::restoreEnvironment( PDF_PLOTTER* aPlotter,
  698. SCH_SHEET_PATH& aOldsheetpath )
  699. {
  700. aPlotter->EndPlot();
  701. delete aPlotter;
  702. // Restore the previous sheet
  703. m_parent->SetCurrentSheet( aOldsheetpath );
  704. m_parent->GetCurrentSheet().UpdateAllScreenReferences();
  705. m_parent->SetSheetNumberAndCount();
  706. }
  707. void DIALOG_PLOT_SCHEMATIC::plotOneSheetPDF( PLOTTER* aPlotter, SCH_SCREEN* aScreen,
  708. bool aPlotDrawingSheet )
  709. {
  710. if( m_plotBackgroundColor->GetValue() && aPlotter->GetColorMode() )
  711. {
  712. aPlotter->SetColor( aPlotter->RenderSettings()->GetBackgroundColor() );
  713. wxPoint end( aPlotter->PageSettings().GetWidthIU(),
  714. aPlotter->PageSettings().GetHeightIU() );
  715. aPlotter->Rect( wxPoint( 0, 0 ), end, FILL_T::FILLED_SHAPE, 1.0 );
  716. }
  717. if( aPlotDrawingSheet )
  718. {
  719. COLOR4D color = COLOR4D::BLACK;
  720. if( aPlotter->GetColorMode() )
  721. color = aPlotter->RenderSettings()->GetLayerColor( LAYER_SCHEMATIC_DRAWINGSHEET );
  722. PlotDrawingSheet( aPlotter, &aScreen->Schematic()->Prj(), m_parent->GetTitleBlock(),
  723. m_parent->GetPageSettings(), aScreen->GetPageNumber(),
  724. aScreen->GetPageCount(), m_parent->GetScreenDesc(),
  725. aScreen->GetFileName(), color, aScreen->GetVirtualPageNumber() == 1 );
  726. }
  727. aScreen->Plot( aPlotter );
  728. }
  729. void DIALOG_PLOT_SCHEMATIC::setupPlotPagePDF( PLOTTER* aPlotter, SCH_SCREEN* aScreen )
  730. {
  731. PAGE_INFO plotPage; // page size selected to plot
  732. // Considerations on page size and scaling requests
  733. const PAGE_INFO& actualPage = aScreen->GetPageSettings(); // page size selected in schematic
  734. switch( m_pageSizeSelect )
  735. {
  736. case PAGE_SIZE_A:
  737. plotPage.SetType( wxT( "A" ) );
  738. plotPage.SetPortrait( actualPage.IsPortrait() );
  739. break;
  740. case PAGE_SIZE_A4:
  741. plotPage.SetType( wxT( "A4" ) );
  742. plotPage.SetPortrait( actualPage.IsPortrait() );
  743. break;
  744. case PAGE_SIZE_AUTO:
  745. default:
  746. plotPage = actualPage;
  747. break;
  748. }
  749. double scalex = (double) plotPage.GetWidthMils() / actualPage.GetWidthMils();
  750. double scaley = (double) plotPage.GetHeightMils() / actualPage.GetHeightMils();
  751. double scale = std::min( scalex, scaley );
  752. aPlotter->SetPageSettings( plotPage );
  753. // Currently, plot units are in decimil
  754. aPlotter->SetViewport( wxPoint( 0, 0 ), IU_PER_MILS/10, scale, false );
  755. }
  756. void DIALOG_PLOT_SCHEMATIC::createPSFiles( bool aPlotAll, bool aPlotFrameRef,
  757. RENDER_SETTINGS* aRenderSettings )
  758. {
  759. SCH_SHEET_PATH oldsheetpath = m_parent->GetCurrentSheet(); // sheetpath is saved here
  760. PAGE_INFO plotPage; // page size selected to plot
  761. wxString msg;
  762. REPORTER& reporter = m_MessagesBox->Reporter();
  763. /* When printing all pages, the printed page is not the current page.
  764. * In complex hierarchies, we must update symbol references and other parameters in the
  765. * given printed SCH_SCREEN, accordant to the sheet path because in complex hierarchies
  766. * a SCH_SCREEN (a drawing ) is shared between many sheets and symbol references
  767. * depend on the actual sheet path used.
  768. */
  769. SCH_SHEET_LIST sheetList;
  770. if( aPlotAll )
  771. {
  772. sheetList.BuildSheetList( &m_parent->Schematic().Root(), true );
  773. sheetList.SortByPageNumbers();
  774. }
  775. else
  776. {
  777. sheetList.push_back( m_parent->GetCurrentSheet() );
  778. }
  779. for( unsigned i = 0; i < sheetList.size(); i++ )
  780. {
  781. m_parent->SetCurrentSheet( sheetList[i] );
  782. m_parent->GetCurrentSheet().UpdateAllScreenReferences();
  783. m_parent->SetSheetNumberAndCount();
  784. SCH_SCREEN* screen = m_parent->GetCurrentSheet().LastScreen();
  785. PAGE_INFO actualPage = screen->GetPageSettings();
  786. switch( m_pageSizeSelect )
  787. {
  788. case PAGE_SIZE_A:
  789. plotPage.SetType( wxT( "A" ) );
  790. plotPage.SetPortrait( actualPage.IsPortrait() );
  791. break;
  792. case PAGE_SIZE_A4:
  793. plotPage.SetType( wxT( "A4" ) );
  794. plotPage.SetPortrait( actualPage.IsPortrait() );
  795. break;
  796. case PAGE_SIZE_AUTO:
  797. default:
  798. plotPage = actualPage;
  799. break;
  800. }
  801. double scalex = (double) plotPage.GetWidthMils() / actualPage.GetWidthMils();
  802. double scaley = (double) plotPage.GetHeightMils() / actualPage.GetHeightMils();
  803. double scale = std::min( scalex, scaley );
  804. wxPoint plot_offset;
  805. try
  806. {
  807. wxString fname = m_parent->GetUniqueFilenameForCurrentSheet();
  808. // The sub sheet can be in a sub_hierarchy, but we plot the file in the
  809. // main project folder (or the folder specified by the caller),
  810. // so replace separators to create a unique filename:
  811. fname.Replace( "/", "_" );
  812. fname.Replace ("\\", "_" );
  813. wxString ext = PS_PLOTTER::GetDefaultFileExtension();
  814. wxFileName plotFileName = createPlotFileName( fname, ext, &reporter );
  815. if( !plotFileName.IsOk() )
  816. return;
  817. if( plotOneSheetPS( plotFileName.GetFullPath(), screen, aRenderSettings, plotPage,
  818. plot_offset, scale, aPlotFrameRef ) )
  819. {
  820. msg.Printf( _( "Plotted to '%s'." ), plotFileName.GetFullPath() );
  821. reporter.Report( msg, RPT_SEVERITY_ACTION );
  822. }
  823. else
  824. {
  825. // Error
  826. msg.Printf( _( "Failed to create file '%s'." ), plotFileName.GetFullPath() );
  827. reporter.Report( msg, RPT_SEVERITY_ERROR );
  828. }
  829. }
  830. catch( IO_ERROR& e )
  831. {
  832. msg.Printf( wxT( "PS Plotter exception: %s"), e.What() );
  833. reporter.Report( msg, RPT_SEVERITY_ERROR );
  834. }
  835. }
  836. reporter.ReportTail( _( "Done." ), RPT_SEVERITY_INFO );
  837. m_parent->SetCurrentSheet( oldsheetpath );
  838. m_parent->GetCurrentSheet().UpdateAllScreenReferences();
  839. m_parent->SetSheetNumberAndCount();
  840. }
  841. bool DIALOG_PLOT_SCHEMATIC::plotOneSheetPS( const wxString& aFileName,
  842. SCH_SCREEN* aScreen,
  843. RENDER_SETTINGS* aRenderSettings,
  844. const PAGE_INFO& aPageInfo,
  845. const wxPoint& aPlot0ffset,
  846. double aScale,
  847. bool aPlotFrameRef )
  848. {
  849. PS_PLOTTER* plotter = new PS_PLOTTER();
  850. plotter->SetRenderSettings( aRenderSettings );
  851. plotter->SetPageSettings( aPageInfo );
  852. plotter->SetColorMode( getModeColor() );
  853. // Currently, plot units are in decimil
  854. plotter->SetViewport( aPlot0ffset, IU_PER_MILS/10, aScale, false );
  855. // Init :
  856. plotter->SetCreator( wxT( "Eeschema-PS" ) );
  857. if( ! plotter->OpenFile( aFileName ) )
  858. {
  859. delete plotter;
  860. return false;
  861. }
  862. LOCALE_IO toggle; // Switch the locale to standard C
  863. plotter->StartPlot();
  864. if( m_plotBackgroundColor->GetValue() && plotter->GetColorMode() )
  865. {
  866. plotter->SetColor( plotter->RenderSettings()->GetLayerColor( LAYER_SCHEMATIC_BACKGROUND ) );
  867. wxPoint end( plotter->PageSettings().GetWidthIU(), plotter->PageSettings().GetHeightIU() );
  868. plotter->Rect( wxPoint( 0, 0 ), end, FILL_T::FILLED_SHAPE, 1.0 );
  869. }
  870. if( aPlotFrameRef )
  871. {
  872. PlotDrawingSheet( plotter, &aScreen->Schematic()->Prj(), m_parent->GetTitleBlock(),
  873. aPageInfo, aScreen->GetPageNumber(), aScreen->GetPageCount(),
  874. m_parent->GetScreenDesc(), aScreen->GetFileName(),
  875. plotter->GetColorMode() ?
  876. plotter->RenderSettings()->GetLayerColor( LAYER_SCHEMATIC_DRAWINGSHEET ) :
  877. COLOR4D::BLACK, aScreen->GetVirtualPageNumber() == 1 );
  878. }
  879. aScreen->Plot( plotter );
  880. plotter->EndPlot();
  881. delete plotter;
  882. return true;
  883. }
  884. void DIALOG_PLOT_SCHEMATIC::createSVGFiles( bool aPrintAll, bool aPrintFrameRef,
  885. RENDER_SETTINGS* aRenderSettings )
  886. {
  887. wxString msg;
  888. REPORTER& reporter = m_MessagesBox->Reporter();
  889. SCH_SHEET_PATH oldsheetpath = m_parent->GetCurrentSheet();
  890. SCH_SHEET_LIST sheetList;
  891. if( aPrintAll )
  892. {
  893. sheetList.BuildSheetList( &m_parent->Schematic().Root(), true );
  894. sheetList.SortByPageNumbers();
  895. }
  896. else
  897. {
  898. sheetList.push_back( m_parent->GetCurrentSheet() );
  899. }
  900. for( unsigned i = 0; i < sheetList.size(); i++ )
  901. {
  902. SCH_SCREEN* screen;
  903. m_parent->SetCurrentSheet( sheetList[i] );
  904. m_parent->GetCurrentSheet().UpdateAllScreenReferences();
  905. m_parent->SetSheetNumberAndCount();
  906. screen = m_parent->GetCurrentSheet().LastScreen();
  907. try
  908. {
  909. wxString fname = m_parent->GetUniqueFilenameForCurrentSheet();
  910. // The sub sheet can be in a sub_hierarchy, but we plot the file in the
  911. // main project folder (or the folder specified by the caller),
  912. // so replace separators to create a unique filename:
  913. fname.Replace( "/", "_" );
  914. fname.Replace( "\\", "_" );
  915. wxString ext = SVG_PLOTTER::GetDefaultFileExtension();
  916. wxFileName plotFileName = createPlotFileName( fname, ext, &reporter );
  917. if( !plotFileName.IsOk() )
  918. return;
  919. bool success = plotOneSheetSVG( plotFileName.GetFullPath(), screen, aRenderSettings,
  920. getModeColor() ? false : true, aPrintFrameRef );
  921. if( !success )
  922. {
  923. msg.Printf( _( "Failed to create file '%s'." ), plotFileName.GetFullPath() );
  924. reporter.Report( msg, RPT_SEVERITY_ERROR );
  925. }
  926. else
  927. {
  928. msg.Printf( _( "Plotted to '%s'." ), plotFileName.GetFullPath() );
  929. reporter.Report( msg, RPT_SEVERITY_ACTION );
  930. }
  931. }
  932. catch( const IO_ERROR& e )
  933. {
  934. // Cannot plot SVG file
  935. msg.Printf( wxT( "SVG Plotter exception: %s" ), e.What() );
  936. reporter.Report( msg, RPT_SEVERITY_ERROR );
  937. break;
  938. }
  939. }
  940. reporter.ReportTail( _( "Done" ), RPT_SEVERITY_INFO );
  941. m_parent->SetCurrentSheet( oldsheetpath );
  942. m_parent->GetCurrentSheet().UpdateAllScreenReferences();
  943. m_parent->SetSheetNumberAndCount();
  944. }
  945. bool DIALOG_PLOT_SCHEMATIC::plotOneSheetSVG( const wxString& aFileName,
  946. SCH_SCREEN* aScreen,
  947. RENDER_SETTINGS* aRenderSettings,
  948. bool aPlotBlackAndWhite,
  949. bool aPlotFrameRef )
  950. {
  951. const PAGE_INFO& pageInfo = aScreen->GetPageSettings();
  952. SVG_PLOTTER* plotter = new SVG_PLOTTER();
  953. plotter->SetRenderSettings( aRenderSettings );
  954. plotter->SetPageSettings( pageInfo );
  955. plotter->SetColorMode( aPlotBlackAndWhite ? false : true );
  956. wxPoint plot_offset;
  957. double scale = 1.0;
  958. // Currently, plot units are in decimil
  959. plotter->SetViewport( plot_offset, IU_PER_MILS/10, scale, false );
  960. // Init :
  961. plotter->SetCreator( wxT( "Eeschema-SVG" ) );
  962. if( ! plotter->OpenFile( aFileName ) )
  963. {
  964. delete plotter;
  965. return false;
  966. }
  967. LOCALE_IO toggle;
  968. plotter->StartPlot();
  969. if( m_plotBackgroundColor->GetValue() && plotter->GetColorMode() )
  970. {
  971. plotter->SetColor( plotter->RenderSettings()->GetLayerColor( LAYER_SCHEMATIC_BACKGROUND ) );
  972. wxPoint end( plotter->PageSettings().GetWidthIU(),
  973. plotter->PageSettings().GetHeightIU() );
  974. plotter->Rect( wxPoint( 0, 0 ), end, FILL_T::FILLED_SHAPE, 1.0 );
  975. }
  976. if( aPlotFrameRef )
  977. {
  978. PlotDrawingSheet( plotter, &aScreen->Schematic()->Prj(), m_parent->GetTitleBlock(),
  979. pageInfo, aScreen->GetPageNumber(), aScreen->GetPageCount(),
  980. m_parent->GetScreenDesc(), aScreen->GetFileName(),
  981. plotter->GetColorMode() ?
  982. plotter->RenderSettings()->GetLayerColor( LAYER_SCHEMATIC_DRAWINGSHEET ) :
  983. COLOR4D::BLACK, aScreen->GetVirtualPageNumber() == 1 );
  984. }
  985. aScreen->Plot( plotter );
  986. plotter->EndPlot();
  987. delete plotter;
  988. return true;
  989. }
  990. wxString DIALOG_PLOT_SCHEMATIC::getOutputPath()
  991. {
  992. wxString msg;
  993. wxString extMsg;
  994. wxFileName fn;
  995. extMsg.Printf( _( "Falling back to user path '%s'." ),
  996. wxStandardPaths::Get().GetDocumentsDir() );
  997. // Build the absolute path of current output directory to preselect it in the file browser.
  998. std::function<bool( wxString* )> textResolver =
  999. [&]( wxString* token ) -> bool
  1000. {
  1001. return m_parent->Schematic().ResolveTextVar( token, 0 );
  1002. };
  1003. wxString path = m_outputDirectoryName->GetValue();
  1004. path = ExpandTextVars( path, &textResolver, nullptr, &Prj() );
  1005. path = ExpandEnvVarSubstitutions( path, &Prj() );
  1006. fn.SetPath( path );
  1007. // If the contents of the path edit control results in an absolute path, return it as is.
  1008. if( fn.IsAbsolute() )
  1009. return path;
  1010. // When editing a schematic that is not part of a project in the stand alone mode, the
  1011. // project path is not defined.
  1012. if( Prj().IsNullProject() )
  1013. {
  1014. SCH_SCREEN* screen = m_parent->Schematic().RootScreen();
  1015. if( screen && !screen->GetFileName().IsEmpty() )
  1016. {
  1017. fn = screen->GetFileName();
  1018. msg.Printf( _( "Cannot normalize path '%s%s'." ), fn.GetPathWithSep(), path );
  1019. fn.SetPath( fn.GetPathWithSep() + path );
  1020. // Normalize always returns true for a non-empty file name so clear the file name
  1021. // and extension so that only the path is normalized.
  1022. fn.SetName( wxEmptyString );
  1023. fn.SetExt( wxEmptyString );
  1024. if( fn.Normalize() )
  1025. {
  1026. path = fn.GetPath();
  1027. }
  1028. else
  1029. {
  1030. wxMessageDialog dlg( this, msg, _( "Warning" ), wxOK | wxCENTER | wxRESIZE_BORDER
  1031. | wxICON_EXCLAMATION | wxSTAY_ON_TOP );
  1032. dlg.SetExtendedMessage( extMsg );
  1033. dlg.ShowModal();
  1034. path = wxStandardPaths::Get().GetDocumentsDir();
  1035. }
  1036. }
  1037. else
  1038. {
  1039. msg = _( "No project or path defined for the current schematic." );
  1040. wxMessageDialog dlg( this, msg, _( "Warning" ), wxOK | wxCENTER | wxRESIZE_BORDER
  1041. | wxICON_EXCLAMATION | wxSTAY_ON_TOP );
  1042. dlg.SetExtendedMessage( extMsg );
  1043. dlg.ShowModal();
  1044. // Always fall back to user's document path if no other absolute path can be normalized.
  1045. path = wxStandardPaths::Get().GetDocumentsDir();
  1046. }
  1047. }
  1048. else
  1049. {
  1050. msg.Printf( _( "Cannot normalize path '%s%s'." ), Prj().GetProjectPath(), path );
  1051. // Build the absolute path of current output directory and the project path.
  1052. fn.SetPath( Prj().GetProjectPath() + path );
  1053. if( fn.Normalize() )
  1054. {
  1055. path = fn.GetPath();
  1056. }
  1057. else
  1058. {
  1059. wxMessageDialog dlg( this, msg, _( "Warning" ),
  1060. wxOK | wxCENTER | wxRESIZE_BORDER | wxICON_EXCLAMATION |
  1061. wxSTAY_ON_TOP );
  1062. dlg.SetExtendedMessage( extMsg );
  1063. dlg.ShowModal();
  1064. path = wxStandardPaths::Get().GetDocumentsDir();
  1065. }
  1066. }
  1067. return path;
  1068. }