KiCad PCB EDA Suite
Loading...
Searching...
No Matches
dialog_plot_schematic.cpp
Go to the documentation of this file.
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 <[email protected]>
7 * Copyright The 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
27#include <bitmaps.h>
28#include <common.h> // For ExpandEnvVarSubstitutions
29#include "string_utils.h"
33#include <eeschema_settings.h>
34#include <kiface_base.h>
35#include <locale_io.h>
38#include <reporter.h>
39#include <trace_helpers.h>
41#include <wx_filename.h>
42#include <pgm_base.h>
43#include <sch_edit_frame.h>
44#include <sch_painter.h>
45#include <wx/dirdlg.h>
46#include <wx/msgdlg.h>
48
50#include <confirm.h>
51
52
54 DIALOG_PLOT_SCHEMATIC( aEditFrame, aEditFrame, nullptr )
55{
56}
57
58
60 JOB_EXPORT_SCH_PLOT* aJob ) :
61 DIALOG_PLOT_SCHEMATIC_BASE( aEditFrame ),
62 m_editFrame( aEditFrame ),
63 m_defaultLineWidth( aEditFrame, m_lineWidthLabel, m_lineWidthCtrl, m_lineWidthUnits ),
64 m_job( aJob )
65{
66 if( !m_job )
67 {
68 m_browseButton->SetBitmap( KiBitmapBundle( BITMAPS::small_folder ) );
69 m_MessagesBox->SetFileName( Prj().GetProjectPath() + wxT( "report.txt" ) );
70
71 SetupStandardButtons( { { wxID_OK, _( "Plot All Pages" ) },
72 { wxID_APPLY, _( "Plot Current Page" ) },
73 { wxID_CANCEL, _( "Close" ) } } );
74 }
75 else
76 {
77 SetTitle( m_job->GetSettingsDialogTitle() );
78
79 m_browseButton->Hide();
80 m_MessagesBox->Hide();
81
82 m_sdbSizer1Apply->Hide();
83 m_openFileAfterPlot->Hide();
84
86 }
87
88 // DIALOG_SHIM needs a unique hash_key because classname will be the same for both job and
89 // non-job versions.
90 m_hash_key = TO_UTF8( GetTitle() );
91
92 for( COLOR_SETTINGS* settings : Pgm().GetSettingsManager().GetColorSettingsList() )
93 m_colorTheme->Append( settings->GetName(), static_cast<void*>( settings ) );
94
95 // Now all widgets have the size fixed, call FinishDialogSettings
97}
98
99
101{
102 if( !m_job )
103 {
104 EESCHEMA_SETTINGS* cfg = dynamic_cast<EESCHEMA_SETTINGS*>( Kiface().KifaceSettings() );
105 wxASSERT( cfg );
106
107 if( m_lineWidthCtrl->GetValue().IsEmpty() && cfg )
108 {
109 // Set the default line width (pen width which should be used for items that do not have a
110 // pen size defined (like frame ref).
111 // The default line width is stored in mils in config
113 }
114 }
115 else
116 {
117 if( !m_colorTheme->SetStringSelection( m_job->m_theme ) )
118 m_colorTheme->SetSelection( 0 );
119
125
126 int paperSizeIndex = (int) m_job->m_pageSizeSelect;
127
128 if( paperSizeIndex >= 0 && paperSizeIndex < (int) m_paperSizeOption->GetCount() )
129 m_paperSizeOption->SetSelection( paperSizeIndex );
130
132 m_ModeColorOption->SetSelection( m_job->m_blackAndWhite ? 1 : 0 );
133
134 // Set the plot format
135 switch( m_job->m_plotFormat )
136 {
137 default:
138 case SCH_PLOT_FORMAT::POST: m_plotFormatOpt->SetSelection( 0 ); break;
139 case SCH_PLOT_FORMAT::PDF: m_plotFormatOpt->SetSelection( 1 ); break;
140 case SCH_PLOT_FORMAT::SVG: m_plotFormatOpt->SetSelection( 2 ); break;
141 case SCH_PLOT_FORMAT::DXF: m_plotFormatOpt->SetSelection( 3 ); break;
142 case SCH_PLOT_FORMAT::HPGL: /* no longer supported */ break;
143 }
144
145 // And then hide it
146 m_plotFormatOpt->Hide();
147
149 }
150
151 wxCommandEvent dummy;
153
154 return true;
155}
156
157
163{
164 // Build the absolute path of current output directory to preselect it in the file browser.
165 wxString path = ExpandEnvVarSubstitutions( m_outputPath->GetValue(), &Prj() );
166
167 // When editing a schematic that is not part of a project in the stand alone mode, the
168 // project path is not defined so point to the users document path to save the plot files.
169 if( Prj().IsNullProject() )
170 {
172 }
173 else
174 {
175 // Build the absolute path of current output directory to preselect it in the file browser.
176 path = ExpandEnvVarSubstitutions( m_outputPath->GetValue(), &Prj() );
177 path = Prj().AbsolutePath( path );
178 }
179
180 wxDirDialog dirDialog( this, _( "Select Output Directory" ), path );
181
182 if( dirDialog.ShowModal() == wxID_CANCEL )
183 return;
184
185 wxFileName dirName = wxFileName::DirName( dirDialog.GetPath() );
186
187 wxFileName fn( Prj().AbsolutePath( m_editFrame->Schematic().Root().GetFileName() ) );
188 wxString defaultPath = fn.GetPathWithSep();
189 wxString msg;
190 wxFileName relPathTest; // Used to test if we can make the path relative
191
192 relPathTest.Assign( dirDialog.GetPath() );
193
194 // Test if making the path relative is possible before asking the user if they want to do it
195 if( relPathTest.MakeRelativeTo( defaultPath ) )
196 {
197 if( IsOK( this, wxString::Format( _( "Do you want to use a path relative to\n'%s'?" ), defaultPath ) ) )
198 dirName.MakeRelativeTo( defaultPath );
199 }
200
201 m_outputPath->SetValue( dirName.GetFullPath() );
202}
203
204
206{
207 switch( m_plotFormatOpt->GetSelection() )
208 {
209 case 0: return PLOT_FORMAT::POST;
210 default:
211 case 1: return PLOT_FORMAT::PDF;
212 case 2: return PLOT_FORMAT::SVG;
213 case 3: return PLOT_FORMAT::DXF;
214 }
215}
216
217
218void DIALOG_PLOT_SCHEMATIC::onColorMode( wxCommandEvent& aEvent )
219{
220 bool backgroundColorAvailable = getPlotFileFormat() == PLOT_FORMAT::POST
221 || getPlotFileFormat() == PLOT_FORMAT::PDF
222 || getPlotFileFormat() == PLOT_FORMAT::SVG;
223
224 m_colorThemeLabel->Enable( getModeColor() );
225 m_colorTheme->Enable( getModeColor() );
226 m_plotBackgroundColor->Enable( backgroundColorAvailable && getModeColor() );
227
228 aEvent.Skip();
229}
230
231
233{
234 if( EESCHEMA_SETTINGS* cfg = dynamic_cast<EESCHEMA_SETTINGS*>( Kiface().KifaceSettings() ) )
235 aSettings->SetDefaultFont( cfg->m_Appearance.default_font );
236
238
239 aSettings->LoadColors( colors );
240 aSettings->SetMinPenWidth( (int) m_defaultLineWidth.GetValue() );
241
242 if( m_plotBackgroundColor->GetValue() )
244 else
245 aSettings->SetBackgroundColor( COLOR4D::UNSPECIFIED );
246}
247
248
250{
251 int selection = m_colorTheme->GetSelection();
252
253 if( selection < 0 )
254 return ::GetColorSettings( COLOR_SETTINGS::COLOR_BUILTIN_DEFAULT );
255
256 return static_cast<COLOR_SETTINGS*>( m_colorTheme->GetClientData( selection ) );
257}
258
259
261{
263
264 m_openFileAfterPlot->Enable( fmt == PLOT_FORMAT::PDF );
265 m_plotPDFPropertyPopups->Enable( fmt == PLOT_FORMAT::PDF );
266 m_plotPDFHierarchicalLinks->Enable( fmt == PLOT_FORMAT::PDF );
267 m_plotPDFMetadata->Enable( fmt == PLOT_FORMAT::PDF );
268
269 m_paperSizeOption->SetSelection( m_paperSizeOption->GetSelection() );
270
271 m_defaultLineWidth.Enable( fmt == PLOT_FORMAT::POST || fmt == PLOT_FORMAT::PDF || fmt == PLOT_FORMAT::SVG );
272
273 wxCommandEvent dummy;
275
276 event.Skip();
277}
278
279
280void DIALOG_PLOT_SCHEMATIC::OnPlotCurrent( wxCommandEvent& event )
281{
282 plotSchematic( false );
283}
284
285
286void DIALOG_PLOT_SCHEMATIC::OnPlotAll( wxCommandEvent& event )
287{
288 if( !m_job )
289 {
290 plotSchematic( true );
291 }
292 else
293 {
297 m_job->m_pageSizeSelect = static_cast<JOB_PAGE_SIZE>( m_paperSizeOption->GetSelection() );
302 m_job->m_plotAll = true;
305
306 event.Skip(); // Allow normal close action
307 }
308}
309
310
312{
313 wxBusyCursor dummy;
314
316 renderSettings.m_ShowHiddenPins = false;
317 renderSettings.m_ShowHiddenFields = false;
318
319 getPlotOptions( &renderSettings );
320
321 std::unique_ptr<SCH_PLOTTER> schPlotter = std::make_unique<SCH_PLOTTER>( m_editFrame );
322
323 SCH_PLOT_OPTS plotOpts;
324 plotOpts.m_plotDrawingSheet = m_plotDrawingSheet->GetValue();
325 plotOpts.m_plotAll = aPlotAll;
326 plotOpts.m_blackAndWhite = !getModeColor();
327 plotOpts.m_useBackgroundColor = m_plotBackgroundColor->GetValue();
328 plotOpts.m_theme = getColorSettings()->GetFilename();
329 plotOpts.m_PDFPropertyPopups = m_plotPDFPropertyPopups->GetValue();
331 plotOpts.m_PDFMetadata = m_plotPDFMetadata->GetValue();
332 plotOpts.m_outputDirectory = getOutputPath();
333 plotOpts.m_pageSizeSelect = m_paperSizeOption->GetSelection();
335
336 schPlotter->Plot( getPlotFileFormat(), plotOpts, &renderSettings, &m_MessagesBox->Reporter() );
337
338 if( getPlotFileFormat() == PLOT_FORMAT::PDF && m_openFileAfterPlot->GetValue() )
339 wxLaunchDefaultApplication( schPlotter->GetLastOutputFilePath() );
340}
341
342
344{
345 wxString extMsg = wxString::Format( _( "Falling back to user path '%s'." ),
347
348 wxFileName fn;
349
350 // Build the absolute path of current output directory to preselect it in the file browser.
351 std::function<bool( wxString* )> textResolver =
352 [&]( wxString* token ) -> bool
353 {
354 SCHEMATIC& schematic = m_editFrame->Schematic();
355 return schematic.ResolveTextVar( &schematic.CurrentSheet(), token, 0 );
356 };
357
358 wxString path = m_outputPath->GetValue();
359 path = ExpandTextVars( path, &textResolver );
361
362 fn.SetPath( path );
363
364 // If the contents of the path edit control results in an absolute path, return it as is.
365 if( fn.IsAbsolute() )
366 return path;
367
368 // When editing a schematic that is not part of a project in the stand alone mode, the
369 // project path is not defined.
370 if( Prj().IsNullProject() )
371 {
373
374 if( screen && !screen->GetFileName().IsEmpty() )
375 {
376 fn = screen->GetFileName();
377 path = fn.GetPathWithSep() + path;
378 fn.SetPath( path );
379
380 // Normalize always returns true for a non-empty file name so clear the file name
381 // and extension so that only the path is normalized.
382 fn.SetName( wxEmptyString );
383 fn.SetExt( wxEmptyString );
384
385 if( fn.Normalize( FN_NORMALIZE_FLAGS | wxPATH_NORM_ENV_VARS ) )
386 {
387 path = fn.GetPath();
388 }
389 else
390 {
391 DisplayErrorMessage( this, wxString::Format( _( "Cannot normalize path '%s'." ), path ), extMsg );
393 }
394 }
395 else
396 {
397 DisplayErrorMessage( this, _( "No project or path defined for the current schematic." ), extMsg );
398 // Always fall back to user's document path if no other absolute path can be normalized.
400 }
401 }
402 else
403 {
404 // Build the absolute path of current output directory and the project path.
405 path = Prj().GetProjectPath() + path;
406 fn.SetPath( path );
407
408 if( fn.Normalize( FN_NORMALIZE_FLAGS | wxPATH_NORM_ENV_VARS ) )
409 {
410 path = fn.GetPath();
411 }
412 else
413 {
414 DisplayErrorMessage( this, wxString::Format( _( "Cannot normalize path '%s'." ), path ), extMsg );
416 }
417 }
418
419 return path;
420}
constexpr EDA_IU_SCALE schIUScale
Definition: base_units.h:114
KIFACE_BASE & Kiface()
Global KIFACE_BASE "get" accessor.
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
Definition: bitmap.cpp:110
Color settings are a bit different than most of the settings objects in that there can be more than o...
static const wxString COLOR_BUILTIN_DEFAULT
COLOR4D GetColor(int aLayer) const
const wxString & GetName() const
Class DIALOG_PLOT_SCHEMATIC_BASE.
WX_HTML_REPORT_PANEL * m_MessagesBox
void OnPlotAll(wxCommandEvent &event) override
void onColorMode(wxCommandEvent &aEvent) override
void OnPlotCurrent(wxCommandEvent &event) override
void onPlotFormatSelection(wxCommandEvent &event) override
JOB_EXPORT_SCH_PLOT * m_job
SCH_EDIT_FRAME * m_editFrame
void getPlotOptions(RENDER_SETTINGS *aSettings)
COLOR_SETTINGS * getColorSettings()
bool TransferDataToWindow() override
void plotSchematic(bool aPlotAll)
void onOutputDirectoryBrowseClicked(wxCommandEvent &event) override
DIALOG_PLOT_SCHEMATIC(SCH_EDIT_FRAME *aEditFrame)
wxString getOutputPath()
Determine the best absolute path to plot files given the contents of the path edit control.
void SetupStandardButtons(std::map< int, wxString > aLabels={})
std::string m_hash_key
Definition: dialog_shim.h:236
void finishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
JOB_PAGE_SIZE m_pageSizeSelect
SCH_PLOT_FORMAT m_plotFormat
void SetConfiguredOutputPath(const wxString &aPath)
Sets the configured output path for the job, this path is always saved to file.
Definition: job.cpp:153
virtual wxString GetSettingsDialogTitle() const
Definition: job.cpp:80
wxString GetConfiguredOutputPath() const
Returns the configured output path for the job.
Definition: job.h:232
wxString GetFilename() const
Definition: json_settings.h:86
APP_SETTINGS_BASE * KifaceSettings() const
Definition: kiface_base.h:95
Container for all the knowledge about how graphical objects are drawn on any output surface/device.
virtual void LoadColors(const COLOR_SETTINGS *aSettings)
void SetDefaultFont(const wxString &aFont)
virtual void SetBackgroundColor(const COLOR4D &aColor)=0
Set the background color.
void SetMinPenWidth(int aWidth)
PROJECT & Prj() const
Return a reference to the PROJECT associated with this KIWAY.
virtual const wxString GetProjectPath() const
Return the full path of the project.
Definition: project.cpp:149
virtual const wxString AbsolutePath(const wxString &aFileName) const
Fix up aFileName if it is relative to the project's directory to be an absolute path and filename.
Definition: project.cpp:373
Holds all the data relating to one schematic.
Definition: schematic.h:88
SCHEMATIC_SETTINGS & Settings() const
Definition: schematic.cpp:356
SCH_SCREEN * RootScreen() const
Helper to retrieve the screen of the root sheet.
Definition: schematic.cpp:252
bool ResolveTextVar(const SCH_SHEET_PATH *aSheetPath, wxString *token, int aDepth) const
Definition: schematic.cpp:297
SCH_SHEET & Root() const
Definition: schematic.h:140
SCH_SHEET_PATH & CurrentSheet() const
Definition: schematic.h:171
SCH_RENDER_SETTINGS * GetRenderSettings()
Schematic editor (Eeschema) main window.
SCHEMATIC & Schematic() const
const wxString & GetFileName() const
Definition: sch_screen.h:152
wxString GetFileName() const
Return the filename corresponding to this sheet.
Definition: sch_sheet.h:321
void SetBitmap(const wxBitmapBundle &aBmp)
int GetIntValue()
Definition: unit_binder.h:134
virtual long long int GetValue()
Return the current value in Internal Units.
void Enable(bool aEnable)
Enable/disable the label, widget and units label.
virtual void SetValue(long long int aValue)
Set new value (in Internal Units) for the text field, taking care of units conversion.
void SetFileName(const wxString &aReportFileName)
Set the report full file name to the string.
REPORTER & Reporter()
Return the reporter object that reports to this panel.
const wxString ExpandEnvVarSubstitutions(const wxString &aString, const PROJECT *aProject)
Replace any environment variable & text variable references with their values.
Definition: common.cpp:355
wxString ExpandTextVars(const wxString &aSource, const PROJECT *aProject, int aFlags)
Definition: common.cpp:59
The common library.
bool IsOK(wxWindow *aParent, const wxString &aMessage)
Display a yes/no dialog with aMessage and returns the user response.
Definition: confirm.cpp:251
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition: confirm.cpp:194
This file is part of the common library.
#define _(s)
JOB_PAGE_SIZE
@ LAYER_SCHEMATIC_BACKGROUND
Definition: layer_ids.h:478
wxString GetDocumentsPath()
Retrieves the operating system specific path for a user's documents.
SETTINGS_MANAGER * GetSettingsManager()
PGM_BASE & Pgm()
The global program "get" accessor.
Definition: pgm_base.cpp:902
see class PGM_BASE
PLOT_FORMAT
The set of supported output plot formats.
Definition: plotter.h:64
Plotting engines similar to ps (PostScript, Gerber, svg)
std::vector< FAB_LAYER_COLOR > dummy
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: string_utils.h:429
constexpr int MilsToIU(int mils) const
Definition: base_units.h:97
wxString m_theme
Definition: sch_plotter.h:67
bool m_PDFPropertyPopups
Definition: sch_plotter.h:64
wxString m_outputDirectory
Definition: sch_plotter.h:69
int m_pageSizeSelect
Definition: sch_plotter.h:62
bool m_PDFMetadata
Definition: sch_plotter.h:66
bool m_blackAndWhite
Definition: sch_plotter.h:61
bool m_PDFHierarchicalLinks
Definition: sch_plotter.h:65
bool m_plotHopOver
Definition: sch_plotter.h:60
bool m_useBackgroundColor
Definition: sch_plotter.h:63
bool m_plotDrawingSheet
Definition: sch_plotter.h:57
wxLogTrace helper definitions.
#define FN_NORMALIZE_FLAGS
Default flags to pass to wxFileName::Normalize().
Definition: wx_filename.h:39