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>
42#include <wx_filename.h>
43#include <gestfich.h>
44#include <pgm_base.h>
45#include <sch_edit_frame.h>
46#include <sch_painter.h>
47#include <wx/dirdlg.h>
48#include <wx/msgdlg.h>
50
52#include <confirm.h>
53
54
56 DIALOG_PLOT_SCHEMATIC( aEditFrame, aEditFrame, nullptr )
57{
58}
59
60
62 JOB_EXPORT_SCH_PLOT* aJob ) :
63 DIALOG_PLOT_SCHEMATIC_BASE( aEditFrame ),
64 m_editFrame( aEditFrame ),
66 m_job( aJob )
67{
68 if( !m_job )
69 {
71 m_MessagesBox->SetFileName( Prj().GetProjectPath() + wxT( "report.txt" ) );
72
73 SetupStandardButtons( { { wxID_OK, _( "Plot All Pages" ) },
74 { wxID_APPLY, _( "Plot Current Page" ) },
75 { wxID_CANCEL, _( "Close" ) } } );
76 }
77 else
78 {
79 SetTitle( m_job->GetSettingsDialogTitle() );
80
81 m_browseButton->Hide();
82 m_MessagesBox->Hide();
83
84 m_sdbSizer1Apply->Hide();
85 m_openFileAfterPlot->Hide();
86
88 }
89
90 // DIALOG_SHIM needs a unique hash_key because classname will be the same for both job and
91 // non-job versions.
92 m_hash_key = TO_UTF8( GetTitle() );
93
94 for( COLOR_SETTINGS* settings : Pgm().GetSettingsManager().GetColorSettingsList() )
95 m_colorTheme->Append( settings->GetName(), static_cast<void*>( settings ) );
96
97 m_variantChoiceCtrl->Append( m_editFrame->Schematic().GetVariantNamesForUI() );
98 m_variantChoiceCtrl->Select( 0 );
99
100
101 // Now all widgets have the size fixed, call FinishDialogSettings
103}
104
105
107{
108 if( !m_job )
109 {
110 EESCHEMA_SETTINGS* cfg = dynamic_cast<EESCHEMA_SETTINGS*>( Kiface().KifaceSettings() );
111 wxASSERT( cfg );
112
113 if( m_lineWidthCtrl->GetValue().IsEmpty() && cfg )
114 {
115 // Set the default line width (pen width which should be used for items that do not have a
116 // pen size defined (like frame ref).
117 // The default line width is stored in mils in config
119 }
120 }
121 else
122 {
123 if( !m_colorTheme->SetStringSelection( m_job->m_theme ) )
124 m_colorTheme->SetSelection( 0 );
125
126 m_plotBackgroundColor->SetValue( m_job->m_useBackgroundColor );
127 m_defaultLineWidth.SetValue( m_job->m_minPenWidth );
128 m_plotPDFPropertyPopups->SetValue( m_job->m_PDFPropertyPopups );
129 m_plotPDFHierarchicalLinks->SetValue( m_job->m_PDFHierarchicalLinks );
130 m_plotPDFMetadata->SetValue( m_job->m_PDFMetadata );
131
132 int paperSizeIndex = (int) m_job->m_pageSizeSelect;
133
134 if( paperSizeIndex >= 0 && paperSizeIndex < (int) m_paperSizeOption->GetCount() )
135 m_paperSizeOption->SetSelection( paperSizeIndex );
136
137 m_plotDrawingSheet->SetValue( m_job->m_plotDrawingSheet );
138 m_ModeColorOption->SetSelection( m_job->m_blackAndWhite ? 1 : 0 );
139
140 // Set the plot format
141 switch( m_job->m_plotFormat )
142 {
143 default:
144 case SCH_PLOT_FORMAT::POST: m_plotFormatOpt->SetSelection( 0 ); break;
145 case SCH_PLOT_FORMAT::PDF: m_plotFormatOpt->SetSelection( 1 ); break;
146 case SCH_PLOT_FORMAT::SVG: m_plotFormatOpt->SetSelection( 2 ); break;
147 case SCH_PLOT_FORMAT::DXF: m_plotFormatOpt->SetSelection( 3 ); break;
148 case SCH_PLOT_FORMAT::HPGL: /* no longer supported */ break;
149 }
150
151 // And then hide it
152 m_plotFormatOpt->Hide();
153
154 m_outputPath->SetValue( m_job->GetConfiguredOutputPath() );
155
156 if( !m_job->m_variant.IsEmpty() )
157 {
158 int idx = m_variantChoiceCtrl->FindString( m_job->m_variant );
159
160 if( idx != wxNOT_FOUND )
161 m_variantChoiceCtrl->SetSelection( idx );
162 }
163
164 // Must readjust dialog sizing after hiding plot format
165 Layout();
166 GetSizer()->SetSizeHints( this );
167 }
168
169 wxCommandEvent dummy;
171
172 return true;
173}
174
175
181{
182 // Build the absolute path of current output directory to preselect it in the file browser.
183 wxString path = ExpandEnvVarSubstitutions( m_outputPath->GetValue(), &Prj() );
184
185 // When editing a schematic that is not part of a project in the stand alone mode, the
186 // project path is not defined so point to the users document path to save the plot files.
187 if( Prj().IsNullProject() )
188 {
190 }
191 else
192 {
193 // Build the absolute path of current output directory to preselect it in the file browser.
194 path = ExpandEnvVarSubstitutions( m_outputPath->GetValue(), &Prj() );
195 path = Prj().AbsolutePath( path );
196 }
197
198 wxDirDialog dirDialog( this, _( "Select Output Directory" ), path );
199
200 if( dirDialog.ShowModal() == wxID_CANCEL )
201 return;
202
203 wxFileName dirName = wxFileName::DirName( dirDialog.GetPath() );
204
205 wxFileName fn( Prj().AbsolutePath( m_editFrame->Schematic().Root().GetFileName() ) );
206 wxString defaultPath = fn.GetPathWithSep();
207 wxFileName relPathTest; // Used to test if we can make the path relative
208
209 relPathTest.Assign( dirDialog.GetPath() );
210
211 // Test if making the path relative is possible before asking the user if they want to do it
212 if( relPathTest.MakeRelativeTo( defaultPath ) )
213 {
214 if( IsOK( this, wxString::Format( _( "Do you want to use a path relative to\n'%s'?" ), defaultPath ) ) )
215 dirName.MakeRelativeTo( defaultPath );
216 }
217
218 m_outputPath->SetValue( dirName.GetFullPath() );
219}
220
221
223{
224 switch( m_plotFormatOpt->GetSelection() )
225 {
226 case 0: return PLOT_FORMAT::POST;
227 default:
228 case 1: return PLOT_FORMAT::PDF;
229 case 2: return PLOT_FORMAT::SVG;
230 case 3: return PLOT_FORMAT::DXF;
231 }
232}
233
234
235void DIALOG_PLOT_SCHEMATIC::onColorMode( wxCommandEvent& aEvent )
236{
237 bool backgroundColorAvailable = getPlotFileFormat() == PLOT_FORMAT::POST
240
241 m_colorThemeLabel->Enable( getModeColor() );
242 m_colorTheme->Enable( getModeColor() );
243 m_plotBackgroundColor->Enable( backgroundColorAvailable && getModeColor() );
244
245 aEvent.Skip();
246}
247
248
250{
251 if( EESCHEMA_SETTINGS* cfg = dynamic_cast<EESCHEMA_SETTINGS*>( Kiface().KifaceSettings() ) )
252 aSettings->SetDefaultFont( cfg->m_Appearance.default_font );
253
255
256 aSettings->LoadColors( colors );
257 aSettings->SetMinPenWidth( (int) m_defaultLineWidth.GetValue() );
258
259 if( m_plotBackgroundColor->GetValue() )
261 else
263}
264
265
267{
268 int selection = m_colorTheme->GetSelection();
269
270 if( selection < 0 )
271 return ::GetColorSettings( COLOR_SETTINGS::COLOR_BUILTIN_DEFAULT );
272
273 return static_cast<COLOR_SETTINGS*>( m_colorTheme->GetClientData( selection ) );
274}
275
276
278{
280
281 m_openFileAfterPlot->Enable( fmt == PLOT_FORMAT::PDF );
284 m_plotPDFMetadata->Enable( fmt == PLOT_FORMAT::PDF );
285
286 // Currently kicad-cli always exports in mm (also true in Pcbnew)
287 m_staticTextDXF->Enable( fmt == PLOT_FORMAT::DXF && m_job == nullptr );
288 m_DXF_plotUnits->Enable( fmt == PLOT_FORMAT::DXF && m_job == nullptr );
289
290 m_paperSizeOption->SetSelection( m_paperSizeOption->GetSelection() );
291
292 m_defaultLineWidth.Enable( fmt == PLOT_FORMAT::POST || fmt == PLOT_FORMAT::PDF || fmt == PLOT_FORMAT::SVG );
293
294 wxCommandEvent dummy;
296
297 event.Skip();
298}
299
300
301void DIALOG_PLOT_SCHEMATIC::OnPlotCurrent( wxCommandEvent& event )
302{
303 plotSchematic( false );
304}
305
306
307void DIALOG_PLOT_SCHEMATIC::OnPlotAll( wxCommandEvent& event )
308{
309 if( !m_job )
310 {
311 plotSchematic( true );
312 }
313 else
314 {
315 m_job->m_blackAndWhite = !getModeColor();
316 m_job->m_useBackgroundColor = m_plotBackgroundColor->GetValue();
317 m_job->m_minPenWidth = m_defaultLineWidth.GetIntValue();
318 m_job->m_pageSizeSelect = static_cast<JOB_PAGE_SIZE>( m_paperSizeOption->GetSelection() );
319 m_job->m_PDFPropertyPopups = m_plotPDFPropertyPopups->GetValue();
320 m_job->m_PDFHierarchicalLinks = m_plotPDFHierarchicalLinks->GetValue();
321 m_job->m_PDFMetadata = m_plotPDFMetadata->GetValue();
322 m_job->m_plotDrawingSheet = m_plotDrawingSheet->GetValue();
323 m_job->m_plotAll = true;
324 m_job->SetConfiguredOutputPath( m_outputPath->GetValue() );
325 m_job->m_theme = getColorSettings()->GetName();
326 m_job->m_variant = getSelectedVariant();
327
328 event.Skip(); // Allow normal close action
329 }
330}
331
332
334{
335 wxBusyCursor dummy;
336
337 SCH_RENDER_SETTINGS renderSettings( *m_editFrame->GetRenderSettings() );
338 renderSettings.m_ShowHiddenPins = false;
339 renderSettings.m_ShowHiddenFields = false;
340
341 getPlotOptions( &renderSettings );
342
343 std::unique_ptr<SCH_PLOTTER> schPlotter = std::make_unique<SCH_PLOTTER>( m_editFrame );
344
345 SCH_PLOT_OPTS plotOpts;
346 plotOpts.m_plotDrawingSheet = m_plotDrawingSheet->GetValue();
347 plotOpts.m_plotAll = aPlotAll;
348 plotOpts.m_blackAndWhite = !getModeColor();
349 plotOpts.m_useBackgroundColor = m_plotBackgroundColor->GetValue();
350 plotOpts.m_theme = getColorSettings()->GetFilename();
351 plotOpts.m_PDFPropertyPopups = m_plotPDFPropertyPopups->GetValue();
353 plotOpts.m_PDFMetadata = m_plotPDFMetadata->GetValue();
354 plotOpts.m_outputDirectory = getOutputPath();
355 plotOpts.m_pageSizeSelect = m_paperSizeOption->GetSelection();
356 plotOpts.m_plotHopOver = m_editFrame->Schematic().Settings().GetHopOverScale() > 0.0;
357
358 // Select the DXF file unit
359 plotOpts.m_DXF_File_Unit = m_DXF_plotUnits->GetSelection() == 0 ? DXF_UNITS::INCH : DXF_UNITS::MM;
360 plotOpts.m_variant = getSelectedVariant();
361 schPlotter->Plot( getPlotFileFormat(), plotOpts, &renderSettings, &m_MessagesBox->Reporter() );
362
364 OpenPDF( schPlotter->GetLastOutputFilePath() );
365}
366
367
369{
370 wxString extMsg = wxString::Format( _( "Falling back to user path '%s'." ),
372
373 wxFileName fn;
374
375 // Build the absolute path of current output directory to preselect it in the file browser.
376 std::function<bool( wxString* )> textResolver =
377 [&]( wxString* token ) -> bool
378 {
379 SCHEMATIC& schematic = m_editFrame->Schematic();
380 return schematic.ResolveTextVar( &schematic.CurrentSheet(), token, 0 );
381 };
382
383 wxString path = m_outputPath->GetValue();
384 path = ExpandTextVars( path, &textResolver );
386
387 fn.SetPath( path );
388
389 // If the contents of the path edit control results in an absolute path, return it as is.
390 if( fn.IsAbsolute() )
391 return path;
392
393 // When editing a schematic that is not part of a project in the stand alone mode, the
394 // project path is not defined.
395 if( Prj().IsNullProject() )
396 {
397 SCH_SCREEN* screen = m_editFrame->Schematic().RootScreen();
398
399 if( screen && !screen->GetFileName().IsEmpty() )
400 {
401 fn = screen->GetFileName();
402 path = fn.GetPathWithSep() + path;
403 fn.SetPath( path );
404
405 // Normalize always returns true for a non-empty file name so clear the file name
406 // and extension so that only the path is normalized.
407 fn.SetName( wxEmptyString );
408 fn.SetExt( wxEmptyString );
409
410 if( fn.Normalize( FN_NORMALIZE_FLAGS | wxPATH_NORM_ENV_VARS ) )
411 {
412 path = fn.GetPath();
413 }
414 else
415 {
416 DisplayErrorMessage( this, wxString::Format( _( "Cannot normalize path '%s'." ), path ), extMsg );
418 }
419 }
420 else
421 {
422 DisplayErrorMessage( this, _( "No project or path defined for the current schematic." ), extMsg );
423 // Always fall back to user's document path if no other absolute path can be normalized.
425 }
426 }
427 else
428 {
429 // Build the absolute path of current output directory and the project path.
430 path = Prj().GetProjectPath() + path;
431 fn.SetPath( path );
432
433 if( fn.Normalize( FN_NORMALIZE_FLAGS | wxPATH_NORM_ENV_VARS ) )
434 {
435 path = fn.GetPath();
436 }
437 else
438 {
439 DisplayErrorMessage( this, wxString::Format( _( "Cannot normalize path '%s'." ), path ), extMsg );
441 }
442 }
443
444 return path;
445}
446
447
449{
450 wxString variant;
451 int selection = m_variantChoiceCtrl->GetSelection();
452
453 if( ( selection != 0 ) && ( selection != wxNOT_FOUND ) )
454 variant = m_variantChoiceCtrl->GetString( selection );
455
456 return variant;
457}
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
static const COLOR4D UNSPECIFIED
For legacy support; used as a value to indicate color hasn't been set yet.
Definition color4d.h:402
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
DIALOG_PLOT_SCHEMATIC_BASE(wxWindow *parent, wxWindowID id=wxID_ANY, const wxString &title=_("Plot Schematic"), const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(-1,-1), long style=wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER)
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
void getPlotOptions(RENDER_SETTINGS *aSettings)
COLOR_SETTINGS * getColorSettings()
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
void finishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
wxString GetFilename() const
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:177
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:401
Holds all the data relating to one schematic.
Definition schematic.h:89
bool ResolveTextVar(const SCH_SHEET_PATH *aSheetPath, wxString *token, int aDepth) const
SCH_SHEET_PATH & CurrentSheet() const
Definition schematic.h:188
Schematic editor (Eeschema) main window.
const wxString & GetFileName() const
Definition sch_screen.h:154
const wxString ExpandEnvVarSubstitutions(const wxString &aString, const PROJECT *aProject)
Replace any environment variable & text variable references with their values.
Definition common.cpp:558
wxString ExpandTextVars(const wxString &aSource, const PROJECT *aProject, int aFlags)
Definition common.cpp:62
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:259
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition confirm.cpp:202
This file is part of the common library.
#define _(s)
bool OpenPDF(const wxString &file)
Run the PDF viewer and display a PDF file.
Definition gestfich.cpp:257
@ LAYER_SCHEMATIC_BACKGROUND
Definition layer_ids.h:490
wxString GetDocumentsPath()
Retrieves the operating system specific path for a user's documents.
PGM_BASE & Pgm()
The global program "get" accessor.
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.
wxString m_theme
Definition sch_plotter.h:67
DXF_UNITS m_DXF_File_Unit
Definition sch_plotter.h:74
bool m_PDFPropertyPopups
Definition sch_plotter.h:64
wxString m_outputDirectory
Definition sch_plotter.h:69
bool m_blackAndWhite
Definition sch_plotter.h:61
wxString m_variant
Definition sch_plotter.h:71
bool m_PDFHierarchicalLinks
Definition sch_plotter.h:65
bool m_useBackgroundColor
Definition sch_plotter.h:63
bool m_plotDrawingSheet
Definition sch_plotter.h:57
std::string path
wxLogTrace helper definitions.
#define FN_NORMALIZE_FLAGS
Default flags to pass to wxFileName::Normalize().
Definition wx_filename.h:39