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.

1021 lines
36 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-2023 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 <wx/log.h>
  27. #include <common.h>
  28. #include <sch_plotter.h>
  29. #include <locale_io.h>
  30. #include <plotters/plotter_hpgl.h>
  31. #include <plotters/plotter_dxf.h>
  32. #include <plotters/plotters_pslike.h>
  33. #include <pgm_base.h>
  34. #include <trace_helpers.h>
  35. #include <sch_edit_frame.h>
  36. #include <sch_painter.h>
  37. #include <schematic.h>
  38. #include <sch_screen.h>
  39. #include <settings/settings_manager.h>
  40. // Note:
  41. // We need to switch between sheets to plot a hierarchy and update references and sheet number
  42. // Use SCHEMATIC::SetCurrentSheet( xxx ) to switch to a sheet.
  43. // Do not use SCH_EDIT_FRAME::SetCurrentSheet( xxx ) to switch to a sheet, because the new sheet
  44. // is not displayed, but SCH_EDIT_FRAME::SetCurrentSheet() has side effects to the current VIEW
  45. // (clear some data used to show the sheet on screen) and does not fully restore the "old" screen
  46. static const wxChar* plot_sheet_list( HPGL_PAGE_SIZE aSize )
  47. {
  48. switch( aSize )
  49. {
  50. default:
  51. case HPGL_PAGE_SIZE::DEFAULT: return nullptr;
  52. case HPGL_PAGE_SIZE::SIZE_A5: return wxT( "A5" );
  53. case HPGL_PAGE_SIZE::SIZE_A4: return wxT( "A4" );
  54. case HPGL_PAGE_SIZE::SIZE_A3: return wxT( "A3" );
  55. case HPGL_PAGE_SIZE::SIZE_A2: return wxT( "A2" );
  56. case HPGL_PAGE_SIZE::SIZE_A1: return wxT( "A1" );
  57. case HPGL_PAGE_SIZE::SIZE_A0: return wxT( "A0" );
  58. case HPGL_PAGE_SIZE::SIZE_A: return wxT( "A" );
  59. case HPGL_PAGE_SIZE::SIZE_B: return wxT( "B" );
  60. case HPGL_PAGE_SIZE::SIZE_C: return wxT( "C" );
  61. case HPGL_PAGE_SIZE::SIZE_D: return wxT( "D" );
  62. case HPGL_PAGE_SIZE::SIZE_E: return wxT( "E" );
  63. }
  64. }
  65. SCH_PLOTTER::SCH_PLOTTER( SCHEMATIC* aSchematic ) :
  66. m_schematic( aSchematic )
  67. {
  68. m_colorSettings = nullptr;
  69. }
  70. SCH_PLOTTER::SCH_PLOTTER( SCH_EDIT_FRAME* aFrame ) :
  71. m_schematic( &aFrame->Schematic() )
  72. {
  73. m_colorSettings = nullptr;
  74. }
  75. wxFileName SCH_PLOTTER::getOutputFilenameSingle( const SCH_PLOT_SETTINGS& aPlotSettings,
  76. REPORTER* aReporter, const wxString& aExt )
  77. {
  78. if( !aPlotSettings.m_outputFile.empty() )
  79. return aPlotSettings.m_outputFile;
  80. else
  81. {
  82. wxString fname = m_schematic->GetUniqueFilenameForCurrentSheet();
  83. // The sub sheet can be in a sub_hierarchy, but we plot the file in the main
  84. // project folder (or the folder specified by the caller), so replace separators
  85. // to create a unique filename:
  86. fname.Replace( "/", "_" );
  87. fname.Replace( "\\", "_" );
  88. return createPlotFileName( aPlotSettings, fname, aExt, aReporter );
  89. }
  90. }
  91. void SCH_PLOTTER::createPDFFile( const SCH_PLOT_SETTINGS& aPlotSettings,
  92. RENDER_SETTINGS* aRenderSettings, REPORTER* aReporter )
  93. {
  94. SCH_SHEET_PATH oldsheetpath = m_schematic->CurrentSheet(); // sheetpath is saved here
  95. /* When printing all pages, the printed page is not the current page. In complex hierarchies,
  96. * we must update symbol references and other parameters in the given printed SCH_SCREEN,
  97. * according to the sheet path because in complex hierarchies a SCH_SCREEN (a drawing ) is
  98. * shared between many sheets and symbol references depend on the actual sheet path used.
  99. */
  100. SCH_SHEET_LIST sheetList;
  101. if( aPlotSettings.m_plotAll )
  102. {
  103. sheetList.BuildSheetList( &m_schematic->Root(), true );
  104. sheetList.SortByPageNumbers();
  105. }
  106. else
  107. {
  108. sheetList.push_back( m_schematic->CurrentSheet() );
  109. }
  110. // Allocate the plotter and set the job level parameter
  111. PDF_PLOTTER* plotter = new PDF_PLOTTER();
  112. plotter->SetRenderSettings( aRenderSettings );
  113. plotter->SetColorMode( !aPlotSettings.m_blackAndWhite );
  114. plotter->SetCreator( wxT( "Eeschema-PDF" ) );
  115. plotter->SetTitle( ExpandTextVars( m_schematic->RootScreen()->GetTitleBlock().GetTitle(),
  116. &m_schematic->Prj() ) );
  117. wxString msg;
  118. wxFileName plotFileName;
  119. LOCALE_IO toggle; // Switch the locale to standard C
  120. for( unsigned i = 0; i < sheetList.size(); i++ )
  121. {
  122. m_schematic->SetCurrentSheet( sheetList[i] );
  123. m_schematic->CurrentSheet().UpdateAllScreenReferences();
  124. m_schematic->SetSheetNumberAndCount();
  125. SCH_SCREEN* screen = m_schematic->CurrentSheet().LastScreen();
  126. if( i == 0 )
  127. {
  128. try
  129. {
  130. wxString ext = PDF_PLOTTER::GetDefaultFileExtension();
  131. plotFileName = getOutputFilenameSingle( aPlotSettings, aReporter, ext );
  132. m_lastOutputFilePath = plotFileName.GetFullPath();
  133. if( !plotFileName.IsOk() )
  134. return;
  135. if( !plotter->OpenFile( plotFileName.GetFullPath() ) )
  136. {
  137. if( aReporter )
  138. {
  139. msg.Printf( _( "Failed to create file '%s'." ),
  140. plotFileName.GetFullPath() );
  141. aReporter->Report( msg, RPT_SEVERITY_ERROR );
  142. }
  143. delete plotter;
  144. return;
  145. }
  146. // Open the plotter and do the first page
  147. setupPlotPagePDF( plotter, screen, aPlotSettings );
  148. plotter->StartPlot( sheetList[i].GetPageNumber(), _( "Root" ) );
  149. }
  150. catch( const IO_ERROR& e )
  151. {
  152. // Cannot plot PDF file
  153. if( aReporter )
  154. {
  155. msg.Printf( wxT( "PDF Plotter exception: %s" ), e.What() );
  156. aReporter->Report( msg, RPT_SEVERITY_ERROR );
  157. }
  158. restoreEnvironment( plotter, oldsheetpath );
  159. return;
  160. }
  161. }
  162. else
  163. {
  164. /* For the following pages you need to close the (finished) page,
  165. * reconfigure, and then start a new one */
  166. plotter->ClosePage();
  167. setupPlotPagePDF( plotter, screen, aPlotSettings );
  168. plotter->StartPage( sheetList[i].GetPageNumber(),
  169. sheetList[i].Last()->GetFields()[SHEETNAME].GetShownText( false ) );
  170. }
  171. plotOneSheetPDF( plotter, screen, aPlotSettings );
  172. }
  173. // Everything done, close the plot and restore the environment
  174. if( aReporter )
  175. {
  176. msg.Printf( _( "Plotted to '%s'.\n" ), plotFileName.GetFullPath() );
  177. aReporter->Report( msg, RPT_SEVERITY_ACTION );
  178. aReporter->ReportTail( _( "Done." ), RPT_SEVERITY_INFO );
  179. }
  180. restoreEnvironment( plotter, oldsheetpath );
  181. }
  182. void SCH_PLOTTER::plotOneSheetPDF( PLOTTER* aPlotter, SCH_SCREEN* aScreen,
  183. const SCH_PLOT_SETTINGS& aPlotSettings )
  184. {
  185. if( aPlotSettings.m_useBackgroundColor && aPlotter->GetColorMode() )
  186. {
  187. aPlotter->SetColor( aPlotter->RenderSettings()->GetBackgroundColor() );
  188. VECTOR2I end( aPlotter->PageSettings().GetWidthIU( schIUScale.IU_PER_MILS ),
  189. aPlotter->PageSettings().GetHeightIU( schIUScale.IU_PER_MILS ) );
  190. aPlotter->Rect( VECTOR2I( 0, 0 ), end, FILL_T::FILLED_SHAPE, 1.0 );
  191. }
  192. if( aPlotSettings.m_plotDrawingSheet )
  193. {
  194. COLOR4D color = COLOR4D::BLACK;
  195. if( aPlotter->GetColorMode() )
  196. color = aPlotter->RenderSettings()->GetLayerColor( LAYER_SCHEMATIC_DRAWINGSHEET );
  197. wxString sheetName = m_schematic->CurrentSheet().Last()->GetName();
  198. wxString sheetPath = m_schematic->CurrentSheet().PathHumanReadable();
  199. const PAGE_INFO& actualPage = aScreen->GetPageSettings(); // page size selected in schematic
  200. PlotDrawingSheet( aPlotter, &aScreen->Schematic()->Prj(),
  201. aScreen->GetTitleBlock(),
  202. actualPage,
  203. aScreen->Schematic()->GetProperties(),
  204. aScreen->GetPageNumber(), aScreen->GetPageCount(), sheetName, sheetPath,
  205. aScreen->GetFileName(), color, aScreen->GetVirtualPageNumber() == 1 );
  206. }
  207. aScreen->Plot( aPlotter );
  208. }
  209. void SCH_PLOTTER::setupPlotPagePDF( PLOTTER* aPlotter, SCH_SCREEN* aScreen,
  210. const SCH_PLOT_SETTINGS& aPlotSettings )
  211. {
  212. PAGE_INFO plotPage; // page size selected to plot
  213. // Considerations on page size and scaling requests
  214. const PAGE_INFO& actualPage = aScreen->GetPageSettings(); // page size selected in schematic
  215. switch( aPlotSettings.m_pageSizeSelect )
  216. {
  217. case PAGE_SIZE_A:
  218. plotPage.SetType( wxT( "A" ) );
  219. plotPage.SetPortrait( actualPage.IsPortrait() );
  220. break;
  221. case PAGE_SIZE_A4:
  222. plotPage.SetType( wxT( "A4" ) );
  223. plotPage.SetPortrait( actualPage.IsPortrait() );
  224. break;
  225. case PAGE_SIZE_AUTO:
  226. default:
  227. plotPage = actualPage;
  228. break;
  229. }
  230. double scalex = (double) plotPage.GetWidthMils() / actualPage.GetWidthMils();
  231. double scaley = (double) plotPage.GetHeightMils() / actualPage.GetHeightMils();
  232. double scale = std::min( scalex, scaley );
  233. aPlotter->SetPageSettings( plotPage );
  234. // Currently, plot units are in decimil
  235. aPlotter->SetViewport( VECTOR2I( 0, 0 ), schIUScale.IU_PER_MILS / 10, scale, false );
  236. }
  237. void SCH_PLOTTER::createPSFiles( const SCH_PLOT_SETTINGS& aPlotSettings,
  238. RENDER_SETTINGS* aRenderSettings, REPORTER* aReporter )
  239. {
  240. SCH_SHEET_PATH oldsheetpath = m_schematic->CurrentSheet(); // sheetpath is saved here
  241. PAGE_INFO plotPage; // page size selected to plot
  242. wxString msg;
  243. /* When printing all pages, the printed page is not the current page.
  244. * In complex hierarchies, we must update symbol references and other parameters in the
  245. * given printed SCH_SCREEN, accordant to the sheet path because in complex hierarchies
  246. * a SCH_SCREEN (a drawing ) is shared between many sheets and symbol references
  247. * depend on the actual sheet path used.
  248. */
  249. SCH_SHEET_LIST sheetList;
  250. if( aPlotSettings.m_plotAll )
  251. {
  252. sheetList.BuildSheetList( &m_schematic->Root(), true );
  253. sheetList.SortByPageNumbers();
  254. }
  255. else
  256. {
  257. sheetList.push_back( m_schematic->CurrentSheet() );
  258. }
  259. for( unsigned i = 0; i < sheetList.size(); i++ )
  260. {
  261. m_schematic->SetCurrentSheet( sheetList[i] );
  262. m_schematic->CurrentSheet().UpdateAllScreenReferences();
  263. m_schematic->SetSheetNumberAndCount();
  264. SCH_SCREEN* screen = m_schematic->CurrentSheet().LastScreen();
  265. PAGE_INFO actualPage = screen->GetPageSettings();
  266. switch( aPlotSettings.m_pageSizeSelect )
  267. {
  268. case PAGE_SIZE_A:
  269. plotPage.SetType( wxT( "A" ) );
  270. plotPage.SetPortrait( actualPage.IsPortrait() );
  271. break;
  272. case PAGE_SIZE_A4:
  273. plotPage.SetType( wxT( "A4" ) );
  274. plotPage.SetPortrait( actualPage.IsPortrait() );
  275. break;
  276. case PAGE_SIZE_AUTO:
  277. default: plotPage = actualPage; break;
  278. }
  279. double scalex = (double) plotPage.GetWidthMils() / actualPage.GetWidthMils();
  280. double scaley = (double) plotPage.GetHeightMils() / actualPage.GetHeightMils();
  281. double scale = std::min( scalex, scaley );
  282. VECTOR2I plot_offset;
  283. try
  284. {
  285. wxString fname = m_schematic->GetUniqueFilenameForCurrentSheet();
  286. // The sub sheet can be in a sub_hierarchy, but we plot the file in the
  287. // main project folder (or the folder specified by the caller),
  288. // so replace separators to create a unique filename:
  289. fname.Replace( "/", "_" );
  290. fname.Replace( "\\", "_" );
  291. wxString ext = PS_PLOTTER::GetDefaultFileExtension();
  292. wxFileName plotFileName = createPlotFileName( aPlotSettings, fname, ext, aReporter );
  293. m_lastOutputFilePath = plotFileName.GetFullPath();
  294. if( !plotFileName.IsOk() )
  295. return;
  296. if( plotOneSheetPS( plotFileName.GetFullPath(), screen, aRenderSettings, actualPage,
  297. plot_offset, scale, aPlotSettings ) )
  298. {
  299. if( aReporter )
  300. {
  301. msg.Printf( _( "Plotted to '%s'." ), plotFileName.GetFullPath() );
  302. aReporter->Report( msg, RPT_SEVERITY_ACTION );
  303. }
  304. }
  305. else
  306. {
  307. if( aReporter )
  308. {
  309. // Error
  310. msg.Printf( _( "Failed to create file '%s'." ), plotFileName.GetFullPath() );
  311. aReporter->Report( msg, RPT_SEVERITY_ERROR );
  312. }
  313. }
  314. }
  315. catch( IO_ERROR& e )
  316. {
  317. if( aReporter )
  318. {
  319. msg.Printf( wxT( "PS Plotter exception: %s" ), e.What() );
  320. aReporter->Report( msg, RPT_SEVERITY_ERROR );
  321. }
  322. }
  323. }
  324. if( aReporter )
  325. {
  326. aReporter->ReportTail( _( "Done." ), RPT_SEVERITY_INFO );
  327. }
  328. restoreEnvironment( nullptr, oldsheetpath );
  329. }
  330. bool SCH_PLOTTER::plotOneSheetPS( const wxString& aFileName, SCH_SCREEN* aScreen,
  331. RENDER_SETTINGS* aRenderSettings,
  332. const PAGE_INFO& aPageInfo, const VECTOR2I& aPlot0ffset,
  333. double aScale, const SCH_PLOT_SETTINGS& aPlotSettings )
  334. {
  335. PS_PLOTTER* plotter = new PS_PLOTTER();
  336. plotter->SetRenderSettings( aRenderSettings );
  337. plotter->SetPageSettings( aPageInfo );
  338. plotter->SetColorMode( !aPlotSettings.m_blackAndWhite );
  339. // Currently, plot units are in decimil
  340. plotter->SetViewport( aPlot0ffset, schIUScale.IU_PER_MILS / 10, aScale, false );
  341. // Init :
  342. plotter->SetCreator( wxT( "Eeschema-PS" ) );
  343. if( !plotter->OpenFile( aFileName ) )
  344. {
  345. delete plotter;
  346. return false;
  347. }
  348. LOCALE_IO toggle; // Switch the locale to standard C
  349. plotter->StartPlot( m_schematic->CurrentSheet().GetPageNumber() );
  350. if( aPlotSettings.m_useBackgroundColor && plotter->GetColorMode() )
  351. {
  352. plotter->SetColor( plotter->RenderSettings()->GetLayerColor( LAYER_SCHEMATIC_BACKGROUND ) );
  353. VECTOR2I end( plotter->PageSettings().GetWidthIU( schIUScale.IU_PER_MILS ),
  354. plotter->PageSettings().GetHeightIU( schIUScale.IU_PER_MILS ) );
  355. plotter->Rect( VECTOR2I( 0, 0 ), end, FILL_T::FILLED_SHAPE, 1.0 );
  356. }
  357. if( aPlotSettings.m_plotDrawingSheet )
  358. {
  359. wxString sheetName = m_schematic->CurrentSheet().Last()->GetName();
  360. wxString sheetPath = m_schematic->CurrentSheet().PathHumanReadable();
  361. COLOR4D color = plotter->RenderSettings()->GetLayerColor( LAYER_SCHEMATIC_DRAWINGSHEET );
  362. PlotDrawingSheet( plotter, &aScreen->Schematic()->Prj(),
  363. aScreen->GetTitleBlock(),
  364. aPageInfo, aScreen->Schematic()->GetProperties(),
  365. aScreen->GetPageNumber(), aScreen->GetPageCount(), sheetName, sheetPath,
  366. aScreen->GetFileName(), plotter->GetColorMode() ? color : COLOR4D::BLACK,
  367. aScreen->GetVirtualPageNumber() == 1 );
  368. }
  369. aScreen->Plot( plotter );
  370. plotter->EndPlot();
  371. delete plotter;
  372. return true;
  373. }
  374. void SCH_PLOTTER::createSVGFiles( const SCH_PLOT_SETTINGS& aPlotSettings,
  375. RENDER_SETTINGS* aRenderSettings, REPORTER* aReporter )
  376. {
  377. wxString msg;
  378. SCH_SHEET_PATH oldsheetpath = m_schematic->CurrentSheet();
  379. SCH_SHEET_LIST sheetList;
  380. if( aPlotSettings.m_plotAll )
  381. {
  382. sheetList.BuildSheetList( &m_schematic->Root(), true );
  383. sheetList.SortByPageNumbers();
  384. }
  385. else
  386. {
  387. sheetList.push_back( m_schematic->CurrentSheet() );
  388. }
  389. for( unsigned i = 0; i < sheetList.size(); i++ )
  390. {
  391. SCH_SCREEN* screen;
  392. m_schematic->SetCurrentSheet( sheetList[i] );
  393. m_schematic->CurrentSheet().UpdateAllScreenReferences();
  394. m_schematic->SetSheetNumberAndCount();
  395. screen = m_schematic->CurrentSheet().LastScreen();
  396. try
  397. {
  398. wxString fname = m_schematic->GetUniqueFilenameForCurrentSheet();
  399. // The sub sheet can be in a sub_hierarchy, but we plot the file in the
  400. // main project folder (or the folder specified by the caller),
  401. // so replace separators to create a unique filename:
  402. fname.Replace( "/", "_" );
  403. fname.Replace( "\\", "_" );
  404. wxString ext = SVG_PLOTTER::GetDefaultFileExtension();
  405. wxFileName plotFileName = createPlotFileName( aPlotSettings, fname, ext, aReporter );
  406. m_lastOutputFilePath = plotFileName.GetFullPath();
  407. if( !plotFileName.IsOk() )
  408. return;
  409. bool success = plotOneSheetSVG( plotFileName.GetFullPath(), screen, aRenderSettings,
  410. aPlotSettings );
  411. if( !success )
  412. {
  413. if( aReporter )
  414. {
  415. msg.Printf( _( "Failed to create file '%s'." ), plotFileName.GetFullPath() );
  416. aReporter->Report( msg, RPT_SEVERITY_ERROR );
  417. }
  418. }
  419. else
  420. {
  421. if( aReporter )
  422. {
  423. msg.Printf( _( "Plotted to '%s'." ), plotFileName.GetFullPath() );
  424. aReporter->Report( msg, RPT_SEVERITY_ACTION );
  425. }
  426. }
  427. }
  428. catch( const IO_ERROR& e )
  429. {
  430. if( aReporter )
  431. {
  432. // Cannot plot SVG file
  433. msg.Printf( wxT( "SVG Plotter exception: %s" ), e.What() );
  434. aReporter->Report( msg, RPT_SEVERITY_ERROR );
  435. }
  436. break;
  437. }
  438. }
  439. if( aReporter )
  440. {
  441. aReporter->ReportTail( _( "Done" ), RPT_SEVERITY_INFO );
  442. }
  443. restoreEnvironment( nullptr, oldsheetpath );
  444. }
  445. bool SCH_PLOTTER::plotOneSheetSVG( const wxString& aFileName, SCH_SCREEN* aScreen,
  446. RENDER_SETTINGS* aRenderSettings,
  447. const SCH_PLOT_SETTINGS& aPlotSettings )
  448. {
  449. PAGE_INFO plotPage;
  450. // Adjust page size and scaling requests
  451. const PAGE_INFO& actualPage = aScreen->GetPageSettings(); // page size selected in schematic
  452. switch( aPlotSettings.m_pageSizeSelect )
  453. {
  454. case PAGE_SIZE_A:
  455. plotPage.SetType( wxT( "A" ) );
  456. plotPage.SetPortrait( actualPage.IsPortrait() );
  457. break;
  458. case PAGE_SIZE_A4:
  459. plotPage.SetType( wxT( "A4" ) );
  460. plotPage.SetPortrait( actualPage.IsPortrait() );
  461. break;
  462. case PAGE_SIZE_AUTO:
  463. default:
  464. plotPage = actualPage;
  465. break;
  466. }
  467. SVG_PLOTTER* plotter = new SVG_PLOTTER();
  468. plotter->SetRenderSettings( aRenderSettings );
  469. plotter->SetPageSettings( plotPage );
  470. plotter->SetColorMode( aPlotSettings.m_blackAndWhite ? false : true );
  471. VECTOR2I plot_offset;
  472. double scalex = (double) plotPage.GetWidthMils() / actualPage.GetWidthMils();
  473. double scaley = (double) plotPage.GetHeightMils() / actualPage.GetHeightMils();
  474. double scale = std::min( scalex, scaley );
  475. // Currently, plot units are in decimil
  476. plotter->SetViewport( plot_offset, schIUScale.IU_PER_MILS / 10, scale, false );
  477. // Init :
  478. plotter->SetCreator( wxT( "Eeschema-SVG" ) );
  479. if( !plotter->OpenFile( aFileName ) )
  480. {
  481. delete plotter;
  482. return false;
  483. }
  484. LOCALE_IO toggle;
  485. plotter->StartPlot( m_schematic->CurrentSheet().GetPageNumber() );
  486. if( aPlotSettings.m_useBackgroundColor && plotter->GetColorMode() )
  487. {
  488. plotter->SetColor( plotter->RenderSettings()->GetLayerColor( LAYER_SCHEMATIC_BACKGROUND ) );
  489. VECTOR2I end( plotter->PageSettings().GetWidthIU( schIUScale.IU_PER_MILS ),
  490. plotter->PageSettings().GetHeightIU( schIUScale.IU_PER_MILS ) );
  491. plotter->Rect( VECTOR2I( 0, 0 ), end, FILL_T::FILLED_SHAPE, 1.0 );
  492. }
  493. if( aPlotSettings.m_plotDrawingSheet )
  494. {
  495. wxString sheetName = m_schematic->CurrentSheet().Last()->GetName();
  496. wxString sheetPath = m_schematic->CurrentSheet().PathHumanReadable();
  497. COLOR4D color = plotter->RenderSettings()->GetLayerColor( LAYER_SCHEMATIC_DRAWINGSHEET );
  498. PlotDrawingSheet( plotter, &aScreen->Schematic()->Prj(),
  499. aScreen->GetTitleBlock(),
  500. actualPage, aScreen->Schematic()->GetProperties(), aScreen->GetPageNumber(),
  501. aScreen->GetPageCount(), sheetName, sheetPath, aScreen->GetFileName(),
  502. plotter->GetColorMode() ? color : COLOR4D::BLACK,
  503. aScreen->GetVirtualPageNumber() == 1 );
  504. }
  505. aScreen->Plot( plotter );
  506. plotter->EndPlot();
  507. delete plotter;
  508. return true;
  509. }
  510. void SCH_PLOTTER::createHPGLFiles( const SCH_PLOT_SETTINGS& aPlotSettings,
  511. RENDER_SETTINGS* aRenderSettings, REPORTER* aReporter )
  512. {
  513. SCH_SCREEN* screen = m_schematic->RootScreen();
  514. SCH_SHEET_PATH oldsheetpath = m_schematic->CurrentSheet();
  515. /* When printing all pages, the printed page is not the current page. In complex hierarchies,
  516. * we must update symbol references and other parameters in the given printed SCH_SCREEN,
  517. * according to the sheet path because in complex hierarchies a SCH_SCREEN (a drawing ) is
  518. * shared between many sheets and symbol references depend on the actual sheet path used.
  519. */
  520. SCH_SHEET_LIST sheetList;
  521. if( aPlotSettings.m_plotAll )
  522. {
  523. sheetList.BuildSheetList( &m_schematic->Root(), true );
  524. sheetList.SortByPageNumbers();
  525. }
  526. else
  527. {
  528. sheetList.push_back( m_schematic->CurrentSheet() );
  529. }
  530. for( unsigned i = 0; i < sheetList.size(); i++ )
  531. {
  532. m_schematic->SetCurrentSheet( sheetList[i] );
  533. m_schematic->CurrentSheet().UpdateAllScreenReferences();
  534. m_schematic->SetSheetNumberAndCount();
  535. screen = m_schematic->CurrentSheet().LastScreen();
  536. if( !screen ) // LastScreen() may return NULL
  537. screen = m_schematic->RootScreen();
  538. const PAGE_INFO& curPage = screen->GetPageSettings();
  539. PAGE_INFO plotPage = curPage;
  540. // if plotting on a page size other than curPage
  541. plotPage.SetType( plot_sheet_list( aPlotSettings.m_HPGLPaperSizeSelect ) );
  542. // Calculation of conversion scales.
  543. double plot_scale = (double) plotPage.GetWidthMils() / curPage.GetWidthMils();
  544. // Calculate offsets
  545. VECTOR2I plotOffset;
  546. wxString msg;
  547. if( aPlotSettings.m_HPGLPlotOrigin == HPGL_PLOT_ORIGIN_AND_UNITS::PLOTTER_CENTER )
  548. {
  549. plotOffset.x = plotPage.GetWidthIU( schIUScale.IU_PER_MILS ) / 2;
  550. plotOffset.y = -plotPage.GetHeightIU( schIUScale.IU_PER_MILS ) / 2;
  551. }
  552. try
  553. {
  554. wxString fname = m_schematic->GetUniqueFilenameForCurrentSheet();
  555. // The sub sheet can be in a sub_hierarchy, but we plot the file in the
  556. // main project folder (or the folder specified by the caller),
  557. // so replace separators to create a unique filename:
  558. fname.Replace( "/", "_" );
  559. fname.Replace( "\\", "_" );
  560. wxString ext = HPGL_PLOTTER::GetDefaultFileExtension();
  561. wxFileName plotFileName = createPlotFileName( aPlotSettings, fname, ext, aReporter );
  562. if( !plotFileName.IsOk() )
  563. return;
  564. LOCALE_IO toggle;
  565. if( plotOneSheetHpgl( plotFileName.GetFullPath(), screen, curPage, aRenderSettings,
  566. plotOffset, plot_scale, aPlotSettings ) )
  567. {
  568. if( aReporter )
  569. {
  570. msg.Printf( _( "Plotted to '%s'." ), plotFileName.GetFullPath() );
  571. aReporter->Report( msg, RPT_SEVERITY_ACTION );
  572. }
  573. }
  574. else
  575. {
  576. if( aReporter )
  577. {
  578. msg.Printf( _( "Failed to create file '%s'." ), plotFileName.GetFullPath() );
  579. aReporter->Report( msg, RPT_SEVERITY_ERROR );
  580. }
  581. }
  582. }
  583. catch( IO_ERROR& e )
  584. {
  585. if( aReporter )
  586. {
  587. msg.Printf( wxT( "HPGL Plotter exception: %s" ), e.What() );
  588. aReporter->Report( msg, RPT_SEVERITY_ERROR );
  589. }
  590. }
  591. }
  592. if( aReporter )
  593. {
  594. aReporter->ReportTail( _( "Done." ), RPT_SEVERITY_INFO );
  595. }
  596. restoreEnvironment( nullptr, oldsheetpath );
  597. }
  598. bool SCH_PLOTTER::plotOneSheetHpgl( const wxString& aFileName,
  599. SCH_SCREEN* aScreen,
  600. const PAGE_INFO& aPageInfo,
  601. RENDER_SETTINGS* aRenderSettings,
  602. const VECTOR2I& aPlot0ffset,
  603. double aScale,
  604. const SCH_PLOT_SETTINGS& aPlotSettings )
  605. {
  606. HPGL_PLOTTER* plotter = new HPGL_PLOTTER();
  607. // Currently, plot units are in decimil
  608. plotter->SetPageSettings( aPageInfo );
  609. plotter->SetRenderSettings( aRenderSettings );
  610. plotter->RenderSettings()->LoadColors( m_colorSettings );
  611. plotter->SetColorMode( !aPlotSettings.m_blackAndWhite );
  612. plotter->SetViewport( aPlot0ffset, schIUScale.IU_PER_MILS/10, aScale, false );
  613. // TODO this could be configurable
  614. plotter->SetTargetChordLength( schIUScale.mmToIU( 0.6 ) );
  615. switch( aPlotSettings.m_HPGLPlotOrigin )
  616. {
  617. case HPGL_PLOT_ORIGIN_AND_UNITS::PLOTTER_BOT_LEFT:
  618. case HPGL_PLOT_ORIGIN_AND_UNITS::PLOTTER_CENTER:
  619. default:
  620. plotter->SetUserCoords( false );
  621. break;
  622. case HPGL_PLOT_ORIGIN_AND_UNITS::USER_FIT_PAGE:
  623. plotter->SetUserCoords( true );
  624. plotter->SetUserCoordsFit( false );
  625. break;
  626. case HPGL_PLOT_ORIGIN_AND_UNITS::USER_FIT_CONTENT:
  627. plotter->SetUserCoords( true );
  628. plotter->SetUserCoordsFit( true );
  629. break;
  630. }
  631. // Init :
  632. plotter->SetCreator( wxT( "Eeschema-HPGL" ) );
  633. if( !plotter->OpenFile( aFileName ) )
  634. {
  635. delete plotter;
  636. return false;
  637. }
  638. LOCALE_IO toggle;
  639. // Pen num and pen speed are not initialized here.
  640. // Default HPGL driver values are used
  641. plotter->SetPenDiameter( aPlotSettings.m_HPGLPenSize );
  642. plotter->StartPlot( m_schematic->CurrentSheet().GetPageNumber() );
  643. if( aPlotSettings.m_plotDrawingSheet )
  644. {
  645. wxString sheetName = m_schematic->CurrentSheet().Last()->GetName();
  646. wxString sheetPath = m_schematic->CurrentSheet().PathHumanReadable();
  647. PlotDrawingSheet( plotter, &m_schematic->Prj(),
  648. aScreen->GetTitleBlock(),
  649. aPageInfo,
  650. aScreen->Schematic()->GetProperties(), aScreen->GetPageNumber(),
  651. aScreen->GetPageCount(), sheetName, sheetPath, aScreen->GetFileName(),
  652. COLOR4D::BLACK, aScreen->GetVirtualPageNumber() == 1 );
  653. }
  654. aScreen->Plot( plotter );
  655. plotter->EndPlot();
  656. delete plotter;
  657. return true;
  658. }
  659. void SCH_PLOTTER::createDXFFiles( const SCH_PLOT_SETTINGS& aPlotSettings,
  660. RENDER_SETTINGS* aRenderSettings, REPORTER* aReporter )
  661. {
  662. SCH_SHEET_PATH oldsheetpath = m_schematic->CurrentSheet();
  663. /* When printing all pages, the printed page is not the current page. In complex hierarchies,
  664. * we must update symbol references and other parameters in the given printed SCH_SCREEN,
  665. * according to the sheet path because in complex hierarchies a SCH_SCREEN (a drawing ) is
  666. * shared between many sheets and symbol references depend on the actual sheet path used.
  667. */
  668. SCH_SHEET_LIST sheetList;
  669. if( aPlotSettings.m_plotAll )
  670. {
  671. sheetList.BuildSheetList( &m_schematic->Root(), true );
  672. sheetList.SortByPageNumbers();
  673. }
  674. else
  675. {
  676. sheetList.push_back( m_schematic->CurrentSheet() );
  677. }
  678. for( unsigned i = 0; i < sheetList.size(); i++ )
  679. {
  680. m_schematic->SetCurrentSheet( sheetList[i] );
  681. m_schematic->CurrentSheet().UpdateAllScreenReferences();
  682. m_schematic->SetSheetNumberAndCount();
  683. SCH_SCREEN* screen = m_schematic->CurrentSheet().LastScreen();
  684. VECTOR2I plot_offset;
  685. wxString msg;
  686. try
  687. {
  688. wxString fname = m_schematic->GetUniqueFilenameForCurrentSheet();
  689. // The sub sheet can be in a sub_hierarchy, but we plot the file in the
  690. // main project folder (or the folder specified by the caller),
  691. // so replace separators to create a unique filename:
  692. fname.Replace( "/", "_" );
  693. fname.Replace( "\\", "_" );
  694. wxString ext = DXF_PLOTTER::GetDefaultFileExtension();
  695. wxFileName plotFileName = createPlotFileName( aPlotSettings, fname, ext, aReporter );
  696. m_lastOutputFilePath = plotFileName.GetFullPath();
  697. if( !plotFileName.IsOk() )
  698. return;
  699. if( plotOneSheetDXF( plotFileName.GetFullPath(), screen, aRenderSettings, plot_offset,
  700. 1.0, aPlotSettings ) )
  701. {
  702. if( aReporter )
  703. {
  704. msg.Printf( _( "Plotted to '%s'." ), plotFileName.GetFullPath() );
  705. aReporter->Report( msg, RPT_SEVERITY_ACTION );
  706. }
  707. }
  708. else // Error
  709. {
  710. if( aReporter )
  711. {
  712. msg.Printf( _( "Failed to create file '%s'." ), plotFileName.GetFullPath() );
  713. aReporter->Report( msg, RPT_SEVERITY_ERROR );
  714. }
  715. }
  716. }
  717. catch( IO_ERROR& e )
  718. {
  719. if( aReporter )
  720. {
  721. msg.Printf( wxT( "DXF Plotter exception: %s" ), e.What() );
  722. aReporter->Report( msg, RPT_SEVERITY_ERROR );
  723. }
  724. m_schematic->SetCurrentSheet( oldsheetpath );
  725. m_schematic->CurrentSheet().UpdateAllScreenReferences();
  726. m_schematic->SetSheetNumberAndCount();
  727. return;
  728. }
  729. }
  730. if( aReporter )
  731. aReporter->ReportTail( _( "Done." ), RPT_SEVERITY_INFO );
  732. restoreEnvironment( nullptr, oldsheetpath );
  733. }
  734. bool SCH_PLOTTER::plotOneSheetDXF( const wxString& aFileName, SCH_SCREEN* aScreen,
  735. RENDER_SETTINGS* aRenderSettings, const VECTOR2I& aPlotOffset,
  736. double aScale, const SCH_PLOT_SETTINGS& aPlotSettings )
  737. {
  738. aRenderSettings->LoadColors( m_colorSettings );
  739. aRenderSettings->SetDefaultPenWidth( 0 );
  740. const PAGE_INFO& pageInfo = aScreen->GetPageSettings();
  741. DXF_PLOTTER* plotter = new DXF_PLOTTER();
  742. plotter->SetRenderSettings( aRenderSettings );
  743. plotter->SetPageSettings( pageInfo );
  744. plotter->SetColorMode( !aPlotSettings.m_blackAndWhite );
  745. // Currently, plot units are in decimil
  746. plotter->SetViewport( aPlotOffset, schIUScale.IU_PER_MILS / 10, aScale, false );
  747. // Init :
  748. plotter->SetCreator( wxT( "Eeschema-DXF" ) );
  749. if( !plotter->OpenFile( aFileName ) )
  750. {
  751. delete plotter;
  752. return false;
  753. }
  754. LOCALE_IO toggle;
  755. plotter->StartPlot( m_schematic->CurrentSheet().GetPageNumber() );
  756. if( aPlotSettings.m_plotDrawingSheet )
  757. {
  758. wxString sheetName = m_schematic->CurrentSheet().Last()->GetName();
  759. wxString sheetPath = m_schematic->CurrentSheet().PathHumanReadable();
  760. COLOR4D color = plotter->RenderSettings()->GetLayerColor( LAYER_SCHEMATIC_DRAWINGSHEET );
  761. PlotDrawingSheet( plotter, &m_schematic->Prj(),
  762. aScreen->GetTitleBlock(),
  763. pageInfo,
  764. aScreen->Schematic()->GetProperties(), aScreen->GetPageNumber(),
  765. aScreen->GetPageCount(), sheetName, sheetPath, aScreen->GetFileName(),
  766. plotter->GetColorMode() ? color : COLOR4D::BLACK,
  767. aScreen->GetVirtualPageNumber() == 1 );
  768. }
  769. aScreen->Plot( plotter );
  770. // finish
  771. plotter->EndPlot();
  772. delete plotter;
  773. return true;
  774. }
  775. void SCH_PLOTTER::restoreEnvironment( PDF_PLOTTER* aPlotter, SCH_SHEET_PATH& aOldsheetpath )
  776. {
  777. if( aPlotter )
  778. {
  779. aPlotter->EndPlot();
  780. delete aPlotter;
  781. }
  782. // Restore the initial sheet
  783. m_schematic->SetCurrentSheet( aOldsheetpath );
  784. m_schematic->CurrentSheet().UpdateAllScreenReferences();
  785. m_schematic->SetSheetNumberAndCount();
  786. }
  787. wxFileName SCH_PLOTTER::createPlotFileName( const SCH_PLOT_SETTINGS& aPlotSettings,
  788. const wxString& aPlotFileName,
  789. const wxString& aExtension, REPORTER* aReporter )
  790. {
  791. wxFileName retv;
  792. wxFileName tmp;
  793. tmp.SetPath( aPlotSettings.m_outputDirectory );
  794. retv.SetPath( tmp.GetPath() );
  795. if( !aPlotFileName.IsEmpty() )
  796. retv.SetName( aPlotFileName );
  797. else
  798. retv.SetName( _( "Schematic" ) );
  799. retv.SetExt( aExtension );
  800. if( !EnsureFileDirectoryExists( &tmp, retv.GetFullName(), aReporter ) || !tmp.IsDirWritable() )
  801. {
  802. if( aReporter )
  803. {
  804. wxString msg = wxString::Format( _( "Failed to write plot files to folder '%s'." ),
  805. tmp.GetPath() );
  806. aReporter->Report( msg, RPT_SEVERITY_ERROR );
  807. }
  808. retv.Clear();
  809. SCHEMATIC_SETTINGS& settings = m_schematic->Settings();
  810. settings.m_PlotDirectoryName.Clear();
  811. }
  812. else
  813. {
  814. retv.SetPath( tmp.GetPath() );
  815. }
  816. wxLogTrace( tracePathsAndFiles, "Writing plot file '%s'.", retv.GetFullPath() );
  817. return retv;
  818. }
  819. void SCH_PLOTTER::Plot( PLOT_FORMAT aPlotFormat, const SCH_PLOT_SETTINGS& aPlotSettings,
  820. RENDER_SETTINGS* aRenderSettings, REPORTER* aReporter )
  821. {
  822. SETTINGS_MANAGER& settingsMgr = Pgm().GetSettingsManager();
  823. m_colorSettings = settingsMgr.GetColorSettings( aPlotSettings.m_theme );
  824. switch( aPlotFormat )
  825. {
  826. default:
  827. case PLOT_FORMAT::POST: createPSFiles( aPlotSettings, aRenderSettings, aReporter ); break;
  828. case PLOT_FORMAT::DXF: createDXFFiles( aPlotSettings, aRenderSettings, aReporter ); break;
  829. case PLOT_FORMAT::PDF: createPDFFile( aPlotSettings, aRenderSettings, aReporter ); break;
  830. case PLOT_FORMAT::SVG: createSVGFiles( aPlotSettings, aRenderSettings, aReporter ); break;
  831. case PLOT_FORMAT::HPGL: createHPGLFiles( aPlotSettings, aRenderSettings, aReporter ); break;
  832. }
  833. }