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.

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