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.

194 lines
6.8 KiB

13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
  1. /** @file plot_schematic_PDF.cpp
  2. */
  3. /*
  4. * This program source code file is part of KiCad, a free EDA CAD application.
  5. *
  6. * Copyright (C) 1992-2010 Jean-Pierre Charras <jean-pierre.charras@gipsa-lab.inpg.fr
  7. * Copyright (C) 1992-2016 KiCad Developers, see change_log.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 <fctsys.h>
  27. #include <plot_common.h>
  28. #include <class_sch_screen.h>
  29. #include <schframe.h>
  30. #include <base_units.h>
  31. #include <sch_sheet_path.h>
  32. #include <project.h>
  33. #include <reporter.h>
  34. #include <dialog_plot_schematic.h>
  35. #include <wx_html_report_panel.h>
  36. void DIALOG_PLOT_SCHEMATIC::createPDFFile( bool aPlotAll, bool aPlotFrameRef )
  37. {
  38. SCH_SCREEN* screen = m_parent->GetScreen();
  39. SCH_SHEET_PATH oldsheetpath = m_parent->GetCurrentSheet(); // sheetpath is saved here
  40. /* When printing all pages, the printed page is not the current page. In
  41. * complex hierarchies, we must update component references and others
  42. * parameters in the given printed SCH_SCREEN, accordint to the sheet path
  43. * because in complex hierarchies a SCH_SCREEN (a drawing ) is shared
  44. * between many sheets and component references depend on the actual sheet
  45. * path used
  46. */
  47. SCH_SHEET_LIST sheetList;
  48. if( aPlotAll )
  49. sheetList.BuildSheetList( g_RootSheet );
  50. else
  51. sheetList.push_back( m_parent->GetCurrentSheet() );
  52. // Allocate the plotter and set the job level parameter
  53. PDF_PLOTTER* plotter = new PDF_PLOTTER();
  54. plotter->SetDefaultLineWidth( GetDefaultLineThickness() );
  55. plotter->SetColorMode( getModeColor() );
  56. plotter->SetCreator( wxT( "Eeschema-PDF" ) );
  57. wxString msg;
  58. wxFileName plotFileName;
  59. REPORTER& reporter = m_MessagesBox->Reporter();
  60. LOCALE_IO toggle; // Switch the locale to standard C
  61. for( unsigned i = 0; i < sheetList.size(); i++ )
  62. {
  63. m_parent->SetCurrentSheet( sheetList[i] );
  64. m_parent->GetCurrentSheet().UpdateAllScreenReferences();
  65. m_parent->SetSheetNumberAndCount();
  66. screen = m_parent->GetCurrentSheet().LastScreen();
  67. if( i == 0 )
  68. {
  69. try
  70. {
  71. wxString fname = m_parent->GetUniqueFilenameForCurrentSheet();
  72. wxString ext = PDF_PLOTTER::GetDefaultFileExtension();
  73. plotFileName = createPlotFileName( m_outputDirectoryName,
  74. fname, ext, &reporter );
  75. if( !plotter->OpenFile( plotFileName.GetFullPath() ) )
  76. {
  77. msg.Printf( _( "Unable to create file '%s'.\n" ),
  78. GetChars( plotFileName.GetFullPath() ) );
  79. reporter.Report( msg, REPORTER::RPT_ERROR );
  80. delete plotter;
  81. return;
  82. }
  83. // Open the plotter and do the first page
  84. setupPlotPagePDF( plotter, screen );
  85. plotter->StartPlot();
  86. }
  87. catch( const IO_ERROR& e )
  88. {
  89. // Cannot plot PDF file
  90. msg.Printf( wxT( "PDF Plotter exception: %s" ), GetChars( e.What() ) );
  91. reporter.Report( msg, REPORTER::RPT_ERROR );
  92. restoreEnvironment( plotter, oldsheetpath );
  93. return;
  94. }
  95. }
  96. else
  97. {
  98. /* For the following pages you need to close the (finished) page,
  99. * reconfigure, and then start a new one */
  100. plotter->ClosePage();
  101. setupPlotPagePDF( plotter, screen );
  102. plotter->StartPage();
  103. }
  104. plotOneSheetPDF( plotter, screen, aPlotFrameRef );
  105. }
  106. // Everything done, close the plot and restore the environment
  107. msg.Printf( _( "Plot: '%s' OK.\n" ), GetChars( plotFileName.GetFullPath() ) );
  108. reporter.Report( msg, REPORTER::RPT_ACTION );
  109. restoreEnvironment( plotter, oldsheetpath );
  110. }
  111. void DIALOG_PLOT_SCHEMATIC::restoreEnvironment( PDF_PLOTTER* aPlotter,
  112. SCH_SHEET_PATH& aOldsheetpath )
  113. {
  114. aPlotter->EndPlot();
  115. delete aPlotter;
  116. // Restore the previous sheet
  117. m_parent->SetCurrentSheet( aOldsheetpath );
  118. m_parent->GetCurrentSheet().UpdateAllScreenReferences();
  119. m_parent->SetSheetNumberAndCount();
  120. }
  121. void DIALOG_PLOT_SCHEMATIC::plotOneSheetPDF( PLOTTER* aPlotter,
  122. SCH_SCREEN* aScreen,
  123. bool aPlotFrameRef )
  124. {
  125. if( aPlotFrameRef )
  126. {
  127. aPlotter->SetColor( BLACK );
  128. PlotWorkSheet( aPlotter, m_parent->GetTitleBlock(),
  129. m_parent->GetPageSettings(),
  130. aScreen->m_ScreenNumber, aScreen->m_NumberOfScreens,
  131. m_parent->GetScreenDesc(),
  132. aScreen->GetFileName() );
  133. }
  134. aScreen->Plot( aPlotter );
  135. }
  136. void DIALOG_PLOT_SCHEMATIC::setupPlotPagePDF( PLOTTER * aPlotter, SCH_SCREEN* aScreen )
  137. {
  138. PAGE_INFO plotPage; // page size selected to plot
  139. // Considerations on page size and scaling requests
  140. PAGE_INFO actualPage = aScreen->GetPageSettings(); // page size selected in schematic
  141. switch( m_pageSizeSelect )
  142. {
  143. case PAGE_SIZE_A:
  144. plotPage.SetType( wxT( "A" ) );
  145. plotPage.SetPortrait( actualPage.IsPortrait() );
  146. break;
  147. case PAGE_SIZE_A4:
  148. plotPage.SetType( wxT( "A4" ) );
  149. plotPage.SetPortrait( actualPage.IsPortrait() );
  150. break;
  151. case PAGE_SIZE_AUTO:
  152. default:
  153. plotPage = actualPage;
  154. break;
  155. }
  156. double scalex = (double) plotPage.GetWidthMils() / actualPage.GetWidthMils();
  157. double scaley = (double) plotPage.GetHeightMils() / actualPage.GetHeightMils();
  158. double scale = std::min( scalex, scaley );
  159. aPlotter->SetPageSettings( plotPage );
  160. // Currently, plot units are in decimil
  161. aPlotter->SetViewport( wxPoint( 0, 0 ), IU_PER_MILS/10, scale, false );
  162. }