KiCad PCB EDA Suite
plot_schematic_HPGL.cpp
Go to the documentation of this file.
1 
4 /*
5  * This program source code file is part of KiCad, a free EDA CAD application.
6  *
7  * Copyright (C) 1992-2010 Jean-Pierre Charras jp.charras at wanadoo.fr
8  * Copyright (C) 1992-2020 KiCad Developers, see AUTHORS.txt for contributors.
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, you may find one here:
22  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
23  * or you may search the http://www.gnu.org website for the version 2 license,
24  * or you may write to the Free Software Foundation, Inc.,
25  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
26  */
27 
28 #include <plotters_specific.h>
29 #include <sch_edit_frame.h>
30 #include <base_units.h>
31 #include <locale_io.h>
32 #include <sch_sheet_path.h>
33 #include <schematic.h>
34 #include <project.h>
35 #include <pgm_base.h>
37 
38 #include <dialog_plot_schematic.h>
39 #include <wx_html_report_panel.h>
40 
54 };
55 
56 
57 static const wxChar* plot_sheet_list( int aSize )
58 {
59  switch( aSize )
60  {
61  default:
62  case PAGE_DEFAULT: return nullptr;
63  case HPGL_PAGE_SIZE_A5: return wxT( "A5" );
64  case HPGL_PAGE_SIZE_A4: return wxT( "A4" );
65  case HPGL_PAGE_SIZE_A3: return wxT( "A3" );
66  case HPGL_PAGE_SIZE_A2: return wxT( "A2" );
67  case HPGL_PAGE_SIZE_A1: return wxT( "A1" );
68  case HPGL_PAGE_SIZE_A0: return wxT( "A0" );
69  case HPGL_PAGE_SIZE_A: return wxT( "A" );
70  case HPGL_PAGE_SIZE_B: return wxT( "B" );
71  case HPGL_PAGE_SIZE_C: return wxT( "C" );
72  case HPGL_PAGE_SIZE_D: return wxT( "D" );
73  case HPGL_PAGE_SIZE_E: return wxT( "E" );
74  }
75 }
76 
77 
79 {
81 
82  if( m_HPGLPenSize > Millimeter2iu( 2 ) )
84 
85  if( m_HPGLPenSize < Millimeter2iu( 0.01 ) )
86  m_HPGLPenSize = Millimeter2iu( 0.01 );
87 }
88 
89 
90 void DIALOG_PLOT_SCHEMATIC::createHPGLFile( bool aPlotAll, bool aPlotFrameRef,
91  RENDER_SETTINGS* aRenderSettings )
92 {
93  SCH_SCREEN* screen = m_parent->GetScreen();
94  SCH_SHEET_PATH oldsheetpath = m_parent->GetCurrentSheet();
95 
96  /* When printing all pages, the printed page is not the current page.
97  * In complex hierarchies, we must setup references and other parameters
98  * in the printed SCH_SCREEN
99  * because in complex hierarchies a SCH_SCREEN (a schematic drawings)
100  * is shared between many sheets
101  */
102  SCH_SHEET_LIST sheetList;
103 
104  if( aPlotAll )
105  {
106  sheetList.BuildSheetList( &m_parent->Schematic().Root(), true );
107  sheetList.SortByPageNumbers();
108  }
109  else
110  {
111  sheetList.push_back( m_parent->GetCurrentSheet() );
112  }
113 
114  REPORTER& reporter = m_MessagesBox->Reporter();
115 
116  SetHPGLPenWidth();
117 
118  for( unsigned i = 0; i < sheetList.size(); i++ )
119  {
120  m_parent->SetCurrentSheet( sheetList[i] );
123 
124  screen = m_parent->GetCurrentSheet().LastScreen();
125 
126  if( !screen ) // LastScreen() may return NULL
127  screen = m_parent->GetScreen();
128 
129  const PAGE_INFO& curPage = screen->GetPageSettings();
130 
131  PAGE_INFO plotPage = curPage;
132 
133  // if plotting on a page size other than curPage
134  if( m_paperSizeOption->GetSelection() != PAGE_DEFAULT )
135  plotPage.SetType( plot_sheet_list( m_paperSizeOption->GetSelection() ) );
136 
137  // Calculation of conversion scales.
138  double plot_scale = (double) plotPage.GetWidthMils() / curPage.GetWidthMils();
139 
140  // Calculate offsets
141  wxPoint plotOffset;
142  wxString msg;
143 
145  {
146  plotOffset.x = plotPage.GetWidthIU() / 2;
147  plotOffset.y = -plotPage.GetHeightIU() / 2;
148  }
149 
150  try
151  {
152  wxString fname = m_parent->GetUniqueFilenameForCurrentSheet();
153  // The sub sheet can be in a sub_hierarchy, but we plot the file in the
154  // main project folder (or the folder specified by the caller),
155  // so replace separators to create a unique filename:
156  fname.Replace("/", "_" );
157  fname.Replace("\\", "_" );
158  wxString ext = HPGL_PLOTTER::GetDefaultFileExtension();
159  wxFileName plotFileName = createPlotFileName( fname, ext, &reporter );
160 
161  LOCALE_IO toggle;
162 
163  if( Plot_1_Page_HPGL( plotFileName.GetFullPath(), screen, plotPage, aRenderSettings,
164  plotOffset, plot_scale, aPlotFrameRef, GetPlotOriginAndUnits() ) )
165  {
166  msg.Printf( _( "Plot: \"%s\" OK.\n" ), plotFileName.GetFullPath() );
167  reporter.Report( msg, RPT_SEVERITY_ACTION );
168  }
169  else
170  {
171  msg.Printf( _( "Unable to create file \"%s\".\n" ), plotFileName.GetFullPath() );
172  reporter.Report( msg, RPT_SEVERITY_ERROR );
173  }
174  }
175  catch( IO_ERROR& e )
176  {
177  msg.Printf( wxT( "HPGL Plotter exception: %s"), e.What() );
178  reporter.Report( msg, RPT_SEVERITY_ERROR );
179  }
180 
181  }
182 
183  m_parent->SetCurrentSheet( oldsheetpath );
186 }
187 
188 
189 bool DIALOG_PLOT_SCHEMATIC::Plot_1_Page_HPGL( const wxString& aFileName,
190  SCH_SCREEN* aScreen,
191  const PAGE_INFO& aPageInfo,
192  RENDER_SETTINGS* aRenderSettings,
193  wxPoint aPlot0ffset,
194  double aScale,
195  bool aPlotFrameRef,
196  HPGL_PLOT_ORIGIN_AND_UNITS aOriginAndUnits )
197 {
198  HPGL_PLOTTER* plotter = new HPGL_PLOTTER();
199  // Currently, plot units are in decimil
200 
201  plotter->SetPageSettings( aPageInfo );
202  plotter->SetRenderSettings( aRenderSettings );
203  plotter->RenderSettings()->LoadColors( getColorSettings() );
204  plotter->SetColorMode( getModeColor() );
205  plotter->SetViewport( aPlot0ffset, IU_PER_MILS/10, aScale, false );
206 
207  // TODO this could be configurable
208  plotter->SetTargetChordLength( Millimeter2iu( 0.6 ) );
209 
210  switch( aOriginAndUnits )
211  {
214  default:
215  plotter->SetUserCoords( false );
216  break;
218  plotter->SetUserCoords( true );
219  plotter->SetUserCoordsFit( false );
220  break;
222  plotter->SetUserCoords( true );
223  plotter->SetUserCoordsFit( true );
224  break;
225  }
226 
227  // Init :
228  plotter->SetCreator( wxT( "Eeschema-HPGL" ) );
229 
230  if( ! plotter->OpenFile( aFileName ) )
231  {
232  delete plotter;
233  return false;
234  }
235 
236  LOCALE_IO toggle;
237 
238  // Pen num and pen speed are not initialized here.
239  // Default HPGL driver values are used
240  plotter->SetPenDiameter( m_HPGLPenSize );
241  plotter->StartPlot();
242 
243  if( aPlotFrameRef )
244  {
245  PlotDrawingSheet( plotter, &m_parent->Prj(), m_parent->GetTitleBlock(), aPageInfo,
246  aScreen->GetPageNumber(), aScreen->GetPageCount(),
248  aScreen->GetVirtualPageNumber() == 1 );
249  }
250 
251  aScreen->Plot( plotter );
252 
253  plotter->EndPlot();
254  delete plotter;
255 
256  return true;
257 }
void SetCurrentSheet(const SCH_SHEET_PATH &aSheet)
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
virtual bool EndPlot() override
HPGL end of plot: sort and emit graphics, pen return and release.
void Plot(PLOTTER *aPlotter) const
Plot all the schematic objects to aPlotter.
Definition: sch_screen.cpp:755
const wxString & GetFileName() const
Definition: sch_screen.h:133
virtual void SetCreator(const wxString &aCreator)
Definition: plotter.h:172
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition: locale_io.h:40
Container for all the knowledge about how graphical objects are drawn on any output surface/device.
Implementation of conversion functions that require both schematic and board internal units.
Plotting engines (PostScript, Gerber, HPGL and DXF)
virtual bool OpenFile(const wxString &aFullFilename)
Open or create the plot file aFullFilename.
Definition: plotter.cpp:76
virtual void SetColorMode(bool aColorMode)
Plot in B/W or color.
Definition: plotter.h:151
int GetVirtualPageNumber() const
Definition: base_screen.h:79
void SetRenderSettings(RENDER_SETTINGS *aSettings)
Definition: plotter.h:154
bool SetType(const wxString &aStandardPageDescriptionName, bool aIsPortrait=false)
Set the name of the page type and also the sizes and margins commonly associated with that type name.
Definition: page_info.cpp:119
virtual void SetPenDiameter(double diameter)
COLOR_SETTINGS * getColorSettings()
int GetPageCount() const
Definition: base_screen.h:76
Definition: color4d.h:44
A pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:64
void UpdateAllScreenReferences()
Update all the symbol references for this sheet path.
const PAGE_INFO & GetPageSettings() const
Definition: sch_screen.h:128
virtual bool StartPlot() override
At the start of the HPGL plot pen speed and number are requested.
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)=0
Report a string with a given severity.
HPGL_PLOT_ORIGIN_AND_UNITS
const wxString & GetPageNumber() const
Definition: base_screen.cpp:78
virtual void SetViewport(const wxPoint &aOffset, double aIusPerDecimil, double aScale, bool aMirror) override
Set the plot offset and scaling for the current plot.
void SetUserCoordsFit(bool user_coords_fit)
Set whether the user coordinate system is fit to content.
Definition: plotter_hpgl.h:61
SCH_SCREEN * GetScreen() const override
Return a pointer to a BASE_SCREEN or one of its derivatives.
wxString GetScreenDesc() const override
Return a human-readable description of the current screen.
wxFileName createPlotFileName(const wxString &aPlotFileName, const wxString &aExtension, REPORTER *aReporter=NULL)
Create a file name with an absolute path name.
static const wxChar * plot_sheet_list(int aSize)
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:29
PROJECT & Prj() const
Return a reference to the PROJECT associated with this KIWAY.
Describe the page size and margins of a paper page on which to eventually print or plot.
Definition: page_info.h:53
void BuildSheetList(SCH_SHEET *aSheet, bool aCheckIntegrity)
Build the list of sheets and their sheet path from aSheet.
SCHEMATIC & Schematic() const
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
HPGL_PLOT_ORIGIN_AND_UNITS GetPlotOriginAndUnits()
bool Plot_1_Page_HPGL(const wxString &aFileName, SCH_SCREEN *aScreen, const PAGE_INFO &aPageInfo, RENDER_SETTINGS *aRenderSettings, wxPoint aPlot0ffset, double aScale, bool aPlotFrameRef, HPGL_PLOT_ORIGIN_AND_UNITS aOriginAndUnits)
void SetTargetChordLength(double chord_len)
Set the target length of chords used to draw approximated circles and arcs.
SCH_SHEET & Root() const
Definition: schematic.h:92
RENDER_SETTINGS * RenderSettings()
Definition: plotter.h:155
see class PGM_BASE
SCH_SCREEN * LastScreen()
void SetSheetNumberAndCount()
Set the m_ScreenNumber and m_NumberOfScreens members for screens.
void SetUserCoords(bool user_coords)
Switch to the user coordinate system.
Definition: plotter_hpgl.h:58
#define _(s)
Definition: 3d_actions.cpp:33
const TITLE_BLOCK & GetTitleBlock() const override
void PlotDrawingSheet(PLOTTER *plotter, const PROJECT *aProject, const TITLE_BLOCK &aTitleBlock, const PAGE_INFO &aPageInfo, const wxString &aSheetNumber, int aSheetCount, const wxString &aSheetDesc, const wxString &aFilename, COLOR4D aColor, bool aIsFirstPage)
void SortByPageNumbers(bool aUpdateVirtualPageNums=true)
Sort the list of sheets by page number.
static wxString GetDefaultFileExtension()
Definition: plotter_hpgl.h:46
#define IU_PER_MILS
Definition: plotter.cpp:137
wxString GetUniqueFilenameForCurrentSheet()
virtual long long int GetValue()
Return the current value in Internal Units.
void createHPGLFile(bool aPlotAll, bool aPlotFrameRef, RENDER_SETTINGS *aRenderSettings)
SCH_SHEET_PATH & GetCurrentSheet() const
WX_HTML_REPORT_PANEL * m_MessagesBox
Definition of the SCH_SHEET_PATH and SCH_SHEET_LIST classes for Eeschema.
virtual void LoadColors(const COLOR_SETTINGS *aSettings)
static constexpr int Millimeter2iu(double mm)
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:75
int GetWidthMils() const
Definition: page_info.h:130
virtual void SetPageSettings(const PAGE_INFO &aPageSettings)
Definition: plotter.h:157