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, see <https://www.gnu.org/licenses/>.
21 */
22
23#include <bitmaps.h>
24#include <common.h> // For ExpandEnvVarSubstitutions
25#include "string_utils.h"
29#include <eeschema_settings.h>
30#include <kiface_base.h>
31#include <locale_io.h>
34#include <reporter.h>
35#include <trace_helpers.h>
38#include <wx_filename.h>
39#include <gestfich.h>
40#include <pgm_base.h>
41#include <sch_edit_frame.h>
42#include <sch_painter.h>
43#include <wx/dirdlg.h>
44#include <wx/msgdlg.h>
46
48#include <confirm.h>
49
50
52 DIALOG_PLOT_SCHEMATIC( aEditFrame, aEditFrame, nullptr )
53{
54}
55
56
58 JOB_EXPORT_SCH_PLOT* aJob ) :
59 DIALOG_PLOT_SCHEMATIC_BASE( aEditFrame ),
60 m_editFrame( aEditFrame ),
62 m_job( aJob )
63{
64 if( !m_job )
65 {
67 m_MessagesBox->SetFileName( Prj().GetProjectPath() + wxT( "report.txt" ) );
68
69 SetupStandardButtons( { { wxID_OK, _( "Plot All Pages" ) },
70 { wxID_APPLY, _( "Plot Current Page" ) },
71 { wxID_CANCEL, _( "Close" ) } } );
72 }
73 else
74 {
75 SetTitle( m_job->GetSettingsDialogTitle() );
76
77 m_browseButton->Hide();
78 m_MessagesBox->Hide();
79
80 m_sdbSizer1Apply->Hide();
81 m_openFileAfterPlot->Hide();
82
84 }
85
86 // DIALOG_SHIM needs a unique hash_key because classname will be the same for both job and
87 // non-job versions.
88 m_hash_key = TO_UTF8( GetTitle() );
89
90 for( COLOR_SETTINGS* settings : Pgm().GetSettingsManager().GetColorSettingsList() )
91 m_colorTheme->Append( settings->GetName(), static_cast<void*>( settings ) );
92
93 m_variantChoiceCtrl->Append( m_editFrame->Schematic().GetVariantNamesForUI() );
94 m_variantChoiceCtrl->Select( 0 );
95
96
97 // Now all widgets have the size fixed, call FinishDialogSettings
99}
100
101
103{
104 if( !m_job )
105 {
106 EESCHEMA_SETTINGS* cfg = dynamic_cast<EESCHEMA_SETTINGS*>( Kiface().KifaceSettings() );
107 wxASSERT( cfg );
108
109 if( m_lineWidthCtrl->GetValue().IsEmpty() && cfg )
110 {
111 // Set the default line width (pen width which should be used for items that do not have a
112 // pen size defined (like frame ref).
113 // The default line width is stored in mils in config
115 }
116 }
117 else
118 {
119 if( !m_colorTheme->SetStringSelection( m_job->m_theme ) )
120 m_colorTheme->SetSelection( 0 );
121
122 m_plotBackgroundColor->SetValue( m_job->m_useBackgroundColor );
123 m_defaultLineWidth.SetValue( m_job->m_minPenWidth );
124 m_plotPDFPropertyPopups->SetValue( m_job->m_PDFPropertyPopups );
125 m_plotPDFHierarchicalLinks->SetValue( m_job->m_PDFHierarchicalLinks );
126 m_plotPDFMetadata->SetValue( m_job->m_PDFMetadata );
127
128 int paperSizeIndex = (int) m_job->m_pageSizeSelect;
129
130 if( paperSizeIndex >= 0 && paperSizeIndex < (int) m_paperSizeOption->GetCount() )
131 m_paperSizeOption->SetSelection( paperSizeIndex );
132
133 m_plotDrawingSheet->SetValue( m_job->m_plotDrawingSheet );
134 m_ModeColorOption->SetSelection( m_job->m_blackAndWhite ? 1 : 0 );
135
136 // Set the plot format
137 switch( m_job->m_plotFormat )
138 {
139 default:
140 case SCH_PLOT_FORMAT::POST: m_plotFormatOpt->SetSelection( 0 ); break;
141 case SCH_PLOT_FORMAT::PDF: m_plotFormatOpt->SetSelection( 1 ); break;
142 case SCH_PLOT_FORMAT::SVG: m_plotFormatOpt->SetSelection( 2 ); break;
143 case SCH_PLOT_FORMAT::DXF: m_plotFormatOpt->SetSelection( 3 ); break;
144 case SCH_PLOT_FORMAT::PNG: m_plotFormatOpt->SetSelection( 4 ); break;
145 case SCH_PLOT_FORMAT::HPGL: /* no longer supported */ break;
146 }
147
148 if( JOB_EXPORT_SCH_PLOT_PNG* pngJob = dynamic_cast<JOB_EXPORT_SCH_PLOT_PNG*>( m_job ) )
149 {
150 m_pngDPI->SetValue( pngJob->m_dpi );
151 m_pngAntialias->SetValue( pngJob->m_antialias );
152 }
153
154 // And then hide it
155 m_plotFormatOpt->Hide();
156
157 m_outputPath->SetValue( m_job->GetConfiguredOutputPath() );
158
159 // The job exposes both a scalar (m_variant, dialog-edited) and a list (m_variantNames,
160 // CLI-populated). Prefer the scalar when present, fall back to the first list entry.
161 wxString selectedVariant = m_job->m_variant;
162
163 if( selectedVariant.IsEmpty() && !m_job->m_variantNames.empty() )
164 selectedVariant = m_job->m_variantNames.front();
165
166 if( !selectedVariant.IsEmpty() )
167 {
168 int idx = m_variantChoiceCtrl->FindString( selectedVariant );
169
170 if( idx != wxNOT_FOUND )
171 m_variantChoiceCtrl->SetSelection( idx );
172 }
173
174 // Must readjust dialog sizing after hiding plot format
175 Layout();
176 GetSizer()->SetSizeHints( this );
177 }
178
179 wxCommandEvent dummy;
181
182 return true;
183}
184
185
191{
192 // Build the absolute path of current output directory to preselect it in the file browser.
193 wxString path = ExpandEnvVarSubstitutions( m_outputPath->GetValue(), &Prj() );
194
195 // When editing a schematic that is not part of a project in the stand alone mode, the
196 // project path is not defined so point to the users document path to save the plot files.
197 if( Prj().IsNullProject() )
198 {
200 }
201 else
202 {
203 // Build the absolute path of current output directory to preselect it in the file browser.
204 path = ExpandEnvVarSubstitutions( m_outputPath->GetValue(), &Prj() );
205 path = Prj().AbsolutePath( path );
206 }
207
208 wxDirDialog dirDialog( this, _( "Select Output Directory" ), path );
209
210 if( dirDialog.ShowModal() == wxID_CANCEL )
211 return;
212
213 wxFileName dirName = wxFileName::DirName( dirDialog.GetPath() );
214
215 wxFileName fn( Prj().AbsolutePath( m_editFrame->Schematic().Root().GetFileName() ) );
216 wxString defaultPath = fn.GetPathWithSep();
217 wxFileName relPathTest; // Used to test if we can make the path relative
218
219 relPathTest.Assign( dirDialog.GetPath() );
220
221 // Test if making the path relative is possible before asking the user if they want to do it
222 if( relPathTest.MakeRelativeTo( defaultPath ) )
223 {
224 if( IsOK( this, wxString::Format( _( "Do you want to use a path relative to\n'%s'?" ), defaultPath ) ) )
225 dirName.MakeRelativeTo( defaultPath );
226 }
227
228 m_outputPath->SetValue( dirName.GetFullPath() );
229}
230
231
233{
234 switch( m_plotFormatOpt->GetSelection() )
235 {
236 case 0: return PLOT_FORMAT::POST;
237 default:
238 case 1: return PLOT_FORMAT::PDF;
239 case 2: return PLOT_FORMAT::SVG;
240 case 3: return PLOT_FORMAT::DXF;
241 case 4: return PLOT_FORMAT::PNG;
242 }
243}
244
245
246void DIALOG_PLOT_SCHEMATIC::onColorMode( wxCommandEvent& aEvent )
247{
248 bool backgroundColorAvailable = getPlotFileFormat() == PLOT_FORMAT::POST
252
253 m_colorThemeLabel->Enable( getModeColor() );
254 m_colorTheme->Enable( getModeColor() );
255 m_plotBackgroundColor->Enable( backgroundColorAvailable && getModeColor() );
256
257 aEvent.Skip();
258}
259
260
262{
263 if( EESCHEMA_SETTINGS* cfg = dynamic_cast<EESCHEMA_SETTINGS*>( Kiface().KifaceSettings() ) )
264 aSettings->SetDefaultFont( cfg->m_Appearance.default_font );
265
267
268 aSettings->LoadColors( colors );
269 aSettings->SetMinPenWidth( (int) m_defaultLineWidth.GetValue() );
270
271 if( m_plotBackgroundColor->GetValue() )
273 else
275}
276
277
279{
280 int selection = m_colorTheme->GetSelection();
281
282 if( selection < 0 )
283 return ::GetColorSettings( COLOR_SETTINGS::COLOR_BUILTIN_DEFAULT );
284
285 return static_cast<COLOR_SETTINGS*>( m_colorTheme->GetClientData( selection ) );
286}
287
288
290{
292
293 m_openFileAfterPlot->Enable( fmt == PLOT_FORMAT::PDF );
296 m_plotPDFMetadata->Enable( fmt == PLOT_FORMAT::PDF );
297
298 // Currently kicad-cli always exports in mm (also true in Pcbnew)
299 m_staticTextDXF->Enable( fmt == PLOT_FORMAT::DXF && m_job == nullptr );
300 m_DXF_plotUnits->Enable( fmt == PLOT_FORMAT::DXF && m_job == nullptr );
301
302 m_paperSizeOption->SetSelection( m_paperSizeOption->GetSelection() );
303
305 || fmt == PLOT_FORMAT::SVG || fmt == PLOT_FORMAT::PNG );
306
307 m_sizerPNGOptions->GetStaticBox()->Show( fmt == PLOT_FORMAT::PNG );
308 m_sizerPNGOptions->Show( fmt == PLOT_FORMAT::PNG );
309
310 Layout();
311
312 wxCommandEvent dummy;
314
315 event.Skip();
316}
317
318
319void DIALOG_PLOT_SCHEMATIC::OnPlotCurrent( wxCommandEvent& event )
320{
321 plotSchematic( false );
322}
323
324
325void DIALOG_PLOT_SCHEMATIC::OnPlotAll( wxCommandEvent& event )
326{
327 if( !m_job )
328 {
329 plotSchematic( true );
330 }
331 else
332 {
333 m_job->m_blackAndWhite = !getModeColor();
334 m_job->m_useBackgroundColor = m_plotBackgroundColor->GetValue();
335 m_job->m_minPenWidth = m_defaultLineWidth.GetIntValue();
336 m_job->m_pageSizeSelect = static_cast<JOB_PAGE_SIZE>( m_paperSizeOption->GetSelection() );
337 m_job->m_PDFPropertyPopups = m_plotPDFPropertyPopups->GetValue();
338 m_job->m_PDFHierarchicalLinks = m_plotPDFHierarchicalLinks->GetValue();
339 m_job->m_PDFMetadata = m_plotPDFMetadata->GetValue();
340 m_job->m_plotDrawingSheet = m_plotDrawingSheet->GetValue();
341 m_job->m_plotAll = true;
342 m_job->SetConfiguredOutputPath( m_outputPath->GetValue() );
343 m_job->m_theme = getColorSettings()->GetName();
344 // Keep m_variant (scalar) and m_variantNames (list) in sync so reload + edit + run
345 // honors the dialog selection regardless of which field the executor consults.
346 m_job->m_variant = getSelectedVariant();
347 m_job->m_variantNames.clear();
348
349 if( !m_job->m_variant.IsEmpty() )
350 m_job->m_variantNames.push_back( m_job->m_variant );
351
352 if( JOB_EXPORT_SCH_PLOT_PNG* pngJob = dynamic_cast<JOB_EXPORT_SCH_PLOT_PNG*>( m_job ) )
353 {
354 pngJob->m_dpi = m_pngDPI->GetValue();
355 pngJob->m_antialias = m_pngAntialias->GetValue();
356 }
357
358 event.Skip(); // Allow normal close action
359 }
360}
361
362
364{
365 wxBusyCursor dummy;
366
367 SCH_RENDER_SETTINGS renderSettings( *m_editFrame->GetRenderSettings() );
368 renderSettings.m_ShowHiddenPins = false;
369 renderSettings.m_ShowHiddenFields = false;
370
371 getPlotOptions( &renderSettings );
372
373 std::unique_ptr<SCH_PLOTTER> schPlotter = std::make_unique<SCH_PLOTTER>( m_editFrame );
374
375 SCH_PLOT_OPTS plotOpts;
376 plotOpts.m_plotDrawingSheet = m_plotDrawingSheet->GetValue();
377 plotOpts.m_plotAll = aPlotAll;
378 plotOpts.m_blackAndWhite = !getModeColor();
379 plotOpts.m_useBackgroundColor = m_plotBackgroundColor->GetValue();
380 plotOpts.m_theme = getColorSettings()->GetFilename();
381 plotOpts.m_PDFPropertyPopups = m_plotPDFPropertyPopups->GetValue();
383 plotOpts.m_PDFMetadata = m_plotPDFMetadata->GetValue();
384 plotOpts.m_outputDirectory = getOutputPath();
385 plotOpts.m_pageSizeSelect = m_paperSizeOption->GetSelection();
386 plotOpts.m_plotHopOver = m_editFrame->Schematic().Settings().GetHopOverScale() > 0.0;
387
388 // Select the DXF file unit
389 plotOpts.m_DXF_File_Unit = m_DXF_plotUnits->GetSelection() == 0 ? DXF_UNITS::INCH : DXF_UNITS::MM;
390 plotOpts.m_pngDPI = m_pngDPI->GetValue();
391 plotOpts.m_pngAntialias = m_pngAntialias->GetValue();
392 plotOpts.m_variant = getSelectedVariant();
393 schPlotter->Plot( getPlotFileFormat(), plotOpts, &renderSettings, &m_MessagesBox->Reporter() );
394
396 OpenPDF( schPlotter->GetLastOutputFilePath() );
397}
398
399
401{
402 wxString extMsg = wxString::Format( _( "Falling back to user path '%s'." ),
404
405 wxFileName fn;
406
407 // Build the absolute path of current output directory to preselect it in the file browser.
408 std::function<bool( wxString* )> textResolver =
409 [&]( wxString* token ) -> bool
410 {
411 SCHEMATIC& schematic = m_editFrame->Schematic();
412 return schematic.ResolveTextVar( &schematic.CurrentSheet(), token, 0 );
413 };
414
415 wxString path = m_outputPath->GetValue();
416 path = ExpandTextVars( path, &textResolver );
418
419 fn.SetPath( path );
420
421 // If the contents of the path edit control results in an absolute path, return it as is.
422 if( fn.IsAbsolute() )
423 return path;
424
425 // When editing a schematic that is not part of a project in the stand alone mode, the
426 // project path is not defined.
427 if( Prj().IsNullProject() )
428 {
429 SCH_SCREEN* screen = m_editFrame->Schematic().RootScreen();
430
431 if( screen && !screen->GetFileName().IsEmpty() )
432 {
433 fn = screen->GetFileName();
434 path = fn.GetPathWithSep() + path;
435 fn.SetPath( path );
436
437 // Normalize always returns true for a non-empty file name so clear the file name
438 // and extension so that only the path is normalized.
439 fn.SetName( wxEmptyString );
440 fn.SetExt( wxEmptyString );
441
442 if( fn.Normalize( FN_NORMALIZE_FLAGS | wxPATH_NORM_ENV_VARS ) )
443 {
444 path = fn.GetPath();
445 }
446 else
447 {
448 DisplayErrorMessage( this, wxString::Format( _( "Cannot normalize path '%s'." ), path ), extMsg );
450 }
451 }
452 else
453 {
454 DisplayErrorMessage( this, _( "No project or path defined for the current schematic." ), extMsg );
455 // Always fall back to user's document path if no other absolute path can be normalized.
457 }
458 }
459 else
460 {
461 // Build the absolute path of current output directory and the project path.
462 path = Prj().GetProjectPath() + path;
463 fn.SetPath( path );
464
465 if( fn.Normalize( FN_NORMALIZE_FLAGS | wxPATH_NORM_ENV_VARS ) )
466 {
467 path = fn.GetPath();
468 }
469 else
470 {
471 DisplayErrorMessage( this, wxString::Format( _( "Cannot normalize path '%s'." ), path ), extMsg );
473 }
474 }
475
476 return path;
477}
478
479
481{
482 wxString variant;
483 int selection = m_variantChoiceCtrl->GetSelection();
484
485 if( ( selection != 0 ) && ( selection != wxNOT_FOUND ) )
486 variant = m_variantChoiceCtrl->GetString( selection );
487
488 return variant;
489}
constexpr EDA_IU_SCALE schIUScale
Definition base_units.h:123
KIFACE_BASE & Kiface()
Global KIFACE_BASE "get" accessor.
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
Definition bitmap.cpp:106
static const COLOR4D UNSPECIFIED
For legacy support; used as a value to indicate color hasn't been set yet.
Definition color4d.h:398
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:91
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:183
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:407
Holds all the data relating to one schematic.
Definition schematic.h:90
Schematic editor (Eeschema) main window.
const wxString & GetFileName() const
Definition sch_screen.h:150
const wxString ExpandEnvVarSubstitutions(const wxString &aString, const PROJECT *aProject)
Replace any environment variable & text variable references with their values.
Definition common.cpp:704
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:274
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition confirm.cpp:217
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:271
@ LAYER_SCHEMATIC_BACKGROUND
Definition layer_ids.h:486
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:60
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:64
DXF_UNITS m_DXF_File_Unit
Definition sch_plotter.h:71
bool m_PDFPropertyPopups
Definition sch_plotter.h:61
wxString m_outputDirectory
Definition sch_plotter.h:66
bool m_pngAntialias
Definition sch_plotter.h:74
bool m_blackAndWhite
Definition sch_plotter.h:58
wxString m_variant
Definition sch_plotter.h:68
bool m_PDFHierarchicalLinks
Definition sch_plotter.h:62
bool m_useBackgroundColor
Definition sch_plotter.h:60
bool m_plotDrawingSheet
Definition sch_plotter.h:54
std::string path
wxLogTrace helper definitions.
#define FN_NORMALIZE_FLAGS
Default flags to pass to wxFileName::Normalize().
Definition wx_filename.h:35