KiCad PCB EDA Suite
plot_schematic_DXF.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 Lorenzo Marcantonio
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 <locale_io.h>
29 #include <plotters_specific.h>
30 #include <sch_edit_frame.h>
31 #include <sch_sheet_path.h>
32 #include <schematic.h>
33 #include <project.h>
34 #include <pgm_base.h>
36 #include <sch_painter.h>
37 #include <dialog_plot_schematic.h>
38 #include <wx_html_report_panel.h>
39 
40 
41 void DIALOG_PLOT_SCHEMATIC::CreateDXFFile( bool aPlotAll, bool aPlotDrawingSheet,
42  RENDER_SETTINGS* aRenderSettings )
43 {
44  SCH_EDIT_FRAME* schframe = m_parent;
45  SCH_SHEET_PATH oldsheetpath = schframe->GetCurrentSheet();
46 
47  /* When printing all pages, the printed page is not the current page.
48  * In complex hierarchies, we must setup references and others parameters
49  * in the printed SCH_SCREEN
50  * because in complex hierarchies a SCH_SCREEN (a schematic drawings)
51  * is shared between many sheets
52  */
53  SCH_SHEET_LIST sheetList;
54 
55  if( aPlotAll )
56  {
57  sheetList.BuildSheetList( &schframe->Schematic().Root(), true );
58  sheetList.SortByPageNumbers();
59  }
60  else
61  {
62  sheetList.push_back( schframe->GetCurrentSheet() );
63  }
64 
65  REPORTER& reporter = m_MessagesBox->Reporter();
66 
67  for( unsigned i = 0; i < sheetList.size(); i++ )
68  {
69  schframe->SetCurrentSheet( sheetList[i] );
71  schframe->SetSheetNumberAndCount();
72 
73  SCH_SCREEN* screen = schframe->GetCurrentSheet().LastScreen();
74  wxPoint plot_offset;
75  wxString msg;
76 
77  try
78  {
79  wxString fname = schframe->GetUniqueFilenameForCurrentSheet();
80  // The sub sheet can be in a sub_hierarchy, but we plot the file in the
81  // main project folder (or the folder specified by the caller),
82  // so replace separators to create a unique filename:
83  fname.Replace("/", "_" );
84  fname.Replace("\\", "_" );
85  wxString ext = DXF_PLOTTER::GetDefaultFileExtension();
86  wxFileName plotFileName = createPlotFileName( fname, ext, &reporter );
87 
88  if( PlotOneSheetDXF( plotFileName.GetFullPath(), screen, aRenderSettings,
89  plot_offset, 1.0, aPlotDrawingSheet ) )
90  {
91  msg.Printf( _( "Plot: \"%s\" OK.\n" ), plotFileName.GetFullPath() );
92  reporter.Report( msg, RPT_SEVERITY_ACTION );
93  }
94  else // Error
95  {
96  msg.Printf( _( "Unable to create file \"%s\".\n" ), plotFileName.GetFullPath() );
97  reporter.Report( msg, RPT_SEVERITY_ERROR );
98  }
99  }
100  catch( IO_ERROR& e )
101  {
102  msg.Printf( wxT( "DXF Plotter exception: %s"), e.What() );
103  reporter.Report( msg, RPT_SEVERITY_ERROR );
104  schframe->SetCurrentSheet( oldsheetpath );
106  schframe->SetSheetNumberAndCount();
107  return;
108  }
109  }
110 
111  schframe->SetCurrentSheet( oldsheetpath );
113  schframe->SetSheetNumberAndCount();
114 }
115 
116 
117 bool DIALOG_PLOT_SCHEMATIC::PlotOneSheetDXF( const wxString& aFileName,
118  SCH_SCREEN* aScreen,
119  RENDER_SETTINGS* aRenderSettings,
120  wxPoint aPlotOffset,
121  double aScale,
122  bool aPlotFrameRef )
123 {
124  aRenderSettings->LoadColors( getColorSettings() );
125  aRenderSettings->SetDefaultPenWidth( 0 );
126 
127  const PAGE_INFO& pageInfo = aScreen->GetPageSettings();
128  DXF_PLOTTER* plotter = new DXF_PLOTTER();
129 
130  plotter->SetRenderSettings( aRenderSettings );
131  plotter->SetPageSettings( pageInfo );
132  plotter->SetColorMode( getModeColor() );
133  // Currently, plot units are in decimil
134  plotter->SetViewport( aPlotOffset, IU_PER_MILS/10, aScale, false );
135 
136  // Init :
137  plotter->SetCreator( wxT( "Eeschema-DXF" ) );
138 
139  if( ! plotter->OpenFile( aFileName ) )
140  {
141  delete plotter;
142  return false;
143  }
144 
145  LOCALE_IO toggle;
146 
147  plotter->StartPlot();
148 
149  if( aPlotFrameRef )
150  {
151  PlotDrawingSheet( plotter, &m_parent->Prj(), m_parent->GetTitleBlock(), pageInfo,
152  aScreen->GetPageNumber(), aScreen->GetPageCount(),
153  m_parent->GetScreenDesc(), aScreen->GetFileName(),
154  plotter->GetColorMode() ?
156  COLOR4D::BLACK, aScreen->GetVirtualPageNumber() == 1 );
157  }
158 
159  aScreen->Plot( plotter );
160 
161  // finish
162  plotter->EndPlot();
163  delete plotter;
164 
165  return true;
166 }
void SetCurrentSheet(const SCH_SHEET_PATH &aSheet)
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
const wxString & GetFileName() const
Definition: sch_screen.h:192
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.
Plotting engines (PostScript, Gerber, HPGL and DXF)
virtual void SetViewport(const wxPoint &aOffset, double aIusPerDecimil, double aScale, bool aMirror) override
Set the scale/position for the DXF plot The DXF engine doesn't support line widths and mirroring.
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
const COLOR4D & GetLayerColor(int aLayer) const
Return the color used to draw a layer.
COLOR_SETTINGS * getColorSettings()
int GetPageCount() const
Definition: base_screen.h:76
Schematic editor (Eeschema) main window.
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:187
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)=0
Report a string with a given severity.
void CreateDXFFile(bool aPlotAll, bool aPlotDrawingSheet, RENDER_SETTINGS *aRenderSettings)
const wxString & GetPageNumber() const
Definition: base_screen.cpp:78
static wxString GetDefaultFileExtension()
Definition: plotter_dxf.h:50
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.
virtual bool StartPlot() override
Opens the DXF plot with a skeleton header.
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
virtual bool EndPlot() override
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
void Plot(PLOTTER *aPlotter)
Plot all the schematic objects to aPlotter.
Definition: sch_screen.cpp:793
bool PlotOneSheetDXF(const wxString &aFileName, SCH_SCREEN *aScreen, RENDER_SETTINGS *aRenderSettings, wxPoint aPlotOffset, double aScale, bool aPlotFrameRef)
SCH_SHEET & Root() const
Definition: schematic.h:116
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.
#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)
Sorts the list of sheets by page number.
#define IU_PER_MILS
Definition: plotter.cpp:137
wxString GetUniqueFilenameForCurrentSheet()
SCH_SHEET_PATH & GetCurrentSheet() const
WX_HTML_REPORT_PANEL * m_MessagesBox
void SetDefaultPenWidth(int aWidth)
Definition of the SCH_SHEET_PATH and SCH_SHEET_LIST classes for Eeschema.
virtual void LoadColors(const COLOR_SETTINGS *aSettings)
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:75
virtual void SetPageSettings(const PAGE_INFO &aPageSettings)
Definition: plotter.h:157
bool GetColorMode() const
Definition: plotter.h:152