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 ) :
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
83 if( !m_job->GetOutputPathIsDirectory() )
84 {
85 m_outputPathLabel->SetLabel( _( "Output file:" ) );
86 m_outputPath->SetToolTip( _( "Target file for the plot. Can be absolute or relative to the schematic "
87 "main file location." ) );
88 }
89
91 }
92
93 // DIALOG_SHIM needs a unique hash_key because classname will be the same for both job and
94 // non-job versions.
95 m_hash_key = TO_UTF8( GetTitle() );
96
97 for( COLOR_SETTINGS* settings : Pgm().GetSettingsManager().GetColorSettingsList() )
98 m_colorTheme->Append( settings->GetName(), static_cast<void*>( settings ) );
99
100 m_variantChoiceCtrl->Append( m_editFrame->Schematic().GetVariantNamesForUI() );
101 m_variantChoiceCtrl->Select( 0 );
102
103
104 // Now all widgets have the size fixed, call FinishDialogSettings
106}
107
108
110{
111 if( !m_job )
112 {
113 EESCHEMA_SETTINGS* cfg = dynamic_cast<EESCHEMA_SETTINGS*>( Kiface().KifaceSettings() );
114 wxASSERT( cfg );
115
116 if( m_lineWidthCtrl->GetValue().IsEmpty() && cfg )
117 {
118 // Set the default line width (pen width which should be used for items that do not have a
119 // pen size defined (like frame ref).
120 // The default line width is stored in mils in config
122 }
123 }
124 else
125 {
126 if( !m_colorTheme->SetStringSelection( m_job->m_theme ) )
127 m_colorTheme->SetSelection( 0 );
128
129 m_plotBackgroundColor->SetValue( m_job->m_useBackgroundColor );
130 m_defaultLineWidth.SetValue( m_job->m_minPenWidth );
131 m_plotPDFPropertyPopups->SetValue( m_job->m_PDFPropertyPopups );
132 m_plotPDFHierarchicalLinks->SetValue( m_job->m_PDFHierarchicalLinks );
133 m_plotPDFMetadata->SetValue( m_job->m_PDFMetadata );
134
135 int paperSizeIndex = (int) m_job->m_pageSizeSelect;
136
137 if( paperSizeIndex >= 0 && paperSizeIndex < (int) m_paperSizeOption->GetCount() )
138 m_paperSizeOption->SetSelection( paperSizeIndex );
139
140 m_plotDrawingSheet->SetValue( m_job->m_plotDrawingSheet );
141 m_ModeColorOption->SetSelection( m_job->m_blackAndWhite ? 1 : 0 );
142
143 // Set the plot format
144 switch( m_job->m_plotFormat )
145 {
146 default:
147 case SCH_PLOT_FORMAT::POST: m_plotFormatOpt->SetSelection( 0 ); break;
148 case SCH_PLOT_FORMAT::PDF: m_plotFormatOpt->SetSelection( 1 ); break;
149 case SCH_PLOT_FORMAT::SVG: m_plotFormatOpt->SetSelection( 2 ); break;
150 case SCH_PLOT_FORMAT::DXF: m_plotFormatOpt->SetSelection( 3 ); break;
151 case SCH_PLOT_FORMAT::PNG: m_plotFormatOpt->SetSelection( 4 ); break;
152 case SCH_PLOT_FORMAT::HPGL: /* no longer supported */ break;
153 }
154
155 if( JOB_EXPORT_SCH_PLOT_PNG* pngJob = dynamic_cast<JOB_EXPORT_SCH_PLOT_PNG*>( m_job ) )
156 {
157 m_pngDPI->SetValue( pngJob->m_dpi );
158 m_pngAntialias->SetValue( pngJob->m_antialias );
159 }
160
161 // And then hide it
162 m_plotFormatOpt->Hide();
163
164 m_outputPath->SetValue( m_job->GetConfiguredOutputPath() );
165
166 // The job exposes both a scalar (m_variant, dialog-edited) and a list (m_variantNames,
167 // CLI-populated). Prefer the scalar when present, fall back to the first list entry.
168 wxString selectedVariant = m_job->m_variant;
169
170 if( selectedVariant.IsEmpty() && !m_job->m_variantNames.empty() )
171 selectedVariant = m_job->m_variantNames.front();
172
173 if( !selectedVariant.IsEmpty() )
174 {
175 int idx = m_variantChoiceCtrl->FindString( selectedVariant );
176
177 if( idx != wxNOT_FOUND )
178 m_variantChoiceCtrl->SetSelection( idx );
179 }
180
181 // Must readjust dialog sizing after hiding plot format
182 Layout();
183 GetSizer()->SetSizeHints( this );
184 }
185
186 wxCommandEvent dummy;
188
189 return true;
190}
191
192
198{
199 // Build the absolute path of current output directory to preselect it in the file browser.
200 wxString path;
201
202 // When editing a schematic that is not part of a project in the stand alone mode, the
203 // project path is not defined so point to the users document path to save the plot files.
204 if( Prj().IsNullProject() )
205 {
207 }
208 else
209 {
210 // Build the absolute path of current output directory to preselect it in the file browser.
211 path = ResolveProjectPath( m_outputPath->GetValue(), &Prj() );
212 }
213
214 wxDirDialog dirDialog( this, _( "Select Output Directory" ), path );
215
216 if( dirDialog.ShowModal() == wxID_CANCEL )
217 return;
218
219 wxFileName dirName = wxFileName::DirName( dirDialog.GetPath() );
220
221 wxFileName fn( Prj().AbsolutePath( m_editFrame->Schematic().Root().GetFileName() ) );
222 wxString defaultPath = fn.GetPathWithSep();
223 wxFileName relPathTest; // Used to test if we can make the path relative
224
225 relPathTest.Assign( dirDialog.GetPath() );
226
227 // Test if making the path relative is possible before asking the user if they want to do it
228 if( relPathTest.MakeRelativeTo( defaultPath ) )
229 {
230 if( IsOK( this, wxString::Format( _( "Do you want to use a path relative to\n'%s'?" ), defaultPath ) ) )
231 dirName.MakeRelativeTo( defaultPath );
232 }
233
234 m_outputPath->SetValue( dirName.GetFullPath() );
235}
236
237
239{
240 switch( m_plotFormatOpt->GetSelection() )
241 {
242 case 0: return PLOT_FORMAT::POST;
243 default:
244 case 1: return PLOT_FORMAT::PDF;
245 case 2: return PLOT_FORMAT::SVG;
246 case 3: return PLOT_FORMAT::DXF;
247 case 4: return PLOT_FORMAT::PNG;
248 }
249}
250
251
252void DIALOG_PLOT_SCHEMATIC::onColorMode( wxCommandEvent& aEvent )
253{
254 bool backgroundColorAvailable = getPlotFileFormat() == PLOT_FORMAT::POST
258
259 m_colorThemeLabel->Enable( getModeColor() );
260 m_colorTheme->Enable( getModeColor() );
261 m_plotBackgroundColor->Enable( backgroundColorAvailable && getModeColor() );
262
263 aEvent.Skip();
264}
265
266
268{
269 if( EESCHEMA_SETTINGS* cfg = dynamic_cast<EESCHEMA_SETTINGS*>( Kiface().KifaceSettings() ) )
270 aSettings->SetDefaultFont( cfg->m_Appearance.default_font );
271
273
274 aSettings->LoadColors( colors );
275 aSettings->SetMinPenWidth( (int) m_defaultLineWidth.GetValue() );
276
277 if( m_plotBackgroundColor->GetValue() )
279 else
281}
282
283
285{
286 int selection = m_colorTheme->GetSelection();
287
288 if( selection < 0 )
289 return ::GetColorSettings( COLOR_SETTINGS::COLOR_BUILTIN_DEFAULT );
290
291 return static_cast<COLOR_SETTINGS*>( m_colorTheme->GetClientData( selection ) );
292}
293
294
296{
298
299 m_openFileAfterPlot->Enable( fmt == PLOT_FORMAT::PDF );
302 m_plotPDFMetadata->Enable( fmt == PLOT_FORMAT::PDF );
303
304 // Currently kicad-cli always exports in mm (also true in Pcbnew)
305 m_staticTextDXF->Enable( fmt == PLOT_FORMAT::DXF && m_job == nullptr );
306 m_DXF_plotUnits->Enable( fmt == PLOT_FORMAT::DXF && m_job == nullptr );
307
308 m_paperSizeOption->SetSelection( m_paperSizeOption->GetSelection() );
309
311 || fmt == PLOT_FORMAT::SVG || fmt == PLOT_FORMAT::PNG );
312
313 m_sizerPNGOptions->GetStaticBox()->Show( fmt == PLOT_FORMAT::PNG );
314 m_sizerPNGOptions->Show( fmt == PLOT_FORMAT::PNG );
315
316 Layout();
317
318 wxCommandEvent dummy;
320
321 event.Skip();
322}
323
324
325void DIALOG_PLOT_SCHEMATIC::OnPlotCurrent( wxCommandEvent& event )
326{
327 plotSchematic( false );
328}
329
330
331void DIALOG_PLOT_SCHEMATIC::OnPlotAll( wxCommandEvent& event )
332{
333 if( !m_job )
334 {
335 plotSchematic( true );
336 }
337 else
338 {
339 m_job->m_blackAndWhite = !getModeColor();
340 m_job->m_useBackgroundColor = m_plotBackgroundColor->GetValue();
341 m_job->m_minPenWidth = m_defaultLineWidth.GetIntValue();
342 m_job->m_pageSizeSelect = static_cast<JOB_PAGE_SIZE>( m_paperSizeOption->GetSelection() );
343 m_job->m_PDFPropertyPopups = m_plotPDFPropertyPopups->GetValue();
344 m_job->m_PDFHierarchicalLinks = m_plotPDFHierarchicalLinks->GetValue();
345 m_job->m_PDFMetadata = m_plotPDFMetadata->GetValue();
346 m_job->m_plotDrawingSheet = m_plotDrawingSheet->GetValue();
347 m_job->m_plotAll = true;
348 m_job->SetConfiguredOutputPath( m_outputPath->GetValue() );
349 m_job->m_theme = getColorSettings()->GetName();
350 // Keep m_variant (scalar) and m_variantNames (list) in sync so reload + edit + run
351 // honors the dialog selection regardless of which field the executor consults.
352 m_job->m_variant = getSelectedVariant();
353 m_job->m_variantNames.clear();
354
355 if( !m_job->m_variant.IsEmpty() )
356 m_job->m_variantNames.push_back( m_job->m_variant );
357
358 if( JOB_EXPORT_SCH_PLOT_PNG* pngJob = dynamic_cast<JOB_EXPORT_SCH_PLOT_PNG*>( m_job ) )
359 {
360 pngJob->m_dpi = m_pngDPI->GetValue();
361 pngJob->m_antialias = m_pngAntialias->GetValue();
362 }
363
364 event.Skip(); // Allow normal close action
365 }
366}
367
368
370{
371 wxBusyCursor dummy;
372
373 SCH_RENDER_SETTINGS renderSettings( *m_editFrame->GetRenderSettings() );
374 renderSettings.m_ShowHiddenPins = false;
375 renderSettings.m_ShowHiddenFields = false;
376
377 getPlotOptions( &renderSettings );
378
379 std::unique_ptr<SCH_PLOTTER> schPlotter = std::make_unique<SCH_PLOTTER>( m_editFrame );
380
381 SCH_PLOT_OPTS plotOpts;
382 plotOpts.m_plotDrawingSheet = m_plotDrawingSheet->GetValue();
383 plotOpts.m_plotAll = aPlotAll;
384 plotOpts.m_blackAndWhite = !getModeColor();
385 plotOpts.m_useBackgroundColor = m_plotBackgroundColor->GetValue();
386 plotOpts.m_theme = getColorSettings()->GetFilename();
387 plotOpts.m_PDFPropertyPopups = m_plotPDFPropertyPopups->GetValue();
389 plotOpts.m_PDFMetadata = m_plotPDFMetadata->GetValue();
390 plotOpts.m_outputDirectory = getOutputPath();
391 plotOpts.m_pageSizeSelect = m_paperSizeOption->GetSelection();
392 plotOpts.m_plotHopOver = m_editFrame->Schematic().Settings().GetHopOverScale() > 0.0;
393
394 // Select the DXF file unit
395 plotOpts.m_DXF_File_Unit = m_DXF_plotUnits->GetSelection() == 0 ? DXF_UNITS::INCH : DXF_UNITS::MM;
396 plotOpts.m_pngDPI = m_pngDPI->GetValue();
397 plotOpts.m_pngAntialias = m_pngAntialias->GetValue();
398 plotOpts.m_variant = getSelectedVariant();
399 schPlotter->Plot( getPlotFileFormat(), plotOpts, &renderSettings, &m_MessagesBox->Reporter() );
400
402 OpenPDF( schPlotter->GetLastOutputFilePath() );
403}
404
405
407{
408 wxString extMsg = wxString::Format( _( "Falling back to user path '%s'." ),
410
411 wxFileName fn;
412
413 // Build the absolute path of current output directory to preselect it in the file browser.
414 std::function<bool( wxString* )> textResolver =
415 [&]( wxString* token ) -> bool
416 {
417 SCHEMATIC& schematic = m_editFrame->Schematic();
418 return schematic.ResolveTextVar( &schematic.CurrentSheet(), token, 0 );
419 };
420
421 wxString path = ExpandPathVariables( m_outputPath->GetValue(), &Prj(), &textResolver );
422
423 fn.SetPath( path );
424
425 // If the contents of the path edit control results in an absolute path, return it as is.
426 if( fn.IsAbsolute() )
427 return path;
428
429 // When editing a schematic that is not part of a project in the stand alone mode, the
430 // project path is not defined.
431 if( Prj().IsNullProject() )
432 {
433 SCH_SCREEN* screen = m_editFrame->Schematic().RootScreen();
434
435 if( screen && !screen->GetFileName().IsEmpty() )
436 {
437 fn = screen->GetFileName();
438 path = fn.GetPathWithSep() + path;
439 fn.SetPath( path );
440
441 // Normalize always returns true for a non-empty file name so clear the file name
442 // and extension so that only the path is normalized.
443 fn.SetName( wxEmptyString );
444 fn.SetExt( wxEmptyString );
445
446 if( fn.Normalize( FN_NORMALIZE_FLAGS | wxPATH_NORM_ENV_VARS ) )
447 {
448 path = fn.GetPath();
449 }
450 else
451 {
452 DisplayErrorMessage( this, wxString::Format( _( "Cannot normalize path '%s'." ), path ), extMsg );
454 }
455 }
456 else
457 {
458 DisplayErrorMessage( this, _( "No project or path defined for the current schematic." ), extMsg );
459 // Always fall back to user's document path if no other absolute path can be normalized.
461 }
462 }
463 else
464 {
465 // Build the absolute path of current output directory and the project path.
466 path = Prj().GetProjectPath() + path;
467 fn.SetPath( path );
468
469 if( fn.Normalize( FN_NORMALIZE_FLAGS | wxPATH_NORM_ENV_VARS ) )
470 {
471 path = fn.GetPath();
472 }
473 else
474 {
475 DisplayErrorMessage( this, wxString::Format( _( "Cannot normalize path '%s'." ), path ), extMsg );
477 }
478 }
479
480 return path;
481}
482
483
485{
486 wxString variant;
487 int selection = m_variantChoiceCtrl->GetSelection();
488
489 if( ( selection != 0 ) && ( selection != wxNOT_FOUND ) )
490 variant = m_variantChoiceCtrl->GetString( selection );
491
492 return variant;
493}
constexpr EDA_IU_SCALE schIUScale
Definition base_units.h:130
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:400
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:92
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:276
Holds all the data relating to one schematic.
Definition schematic.h:149
bool ResolveTextVar(const SCH_SHEET_PATH *aSheetPath, wxString *token, int aDepth) const
SCH_SHEET_PATH & CurrentSheet() const
Definition schematic.h:312
Schematic editor (Eeschema) main window.
const wxString & GetFileName() const
Definition sch_screen.h:157
wxString ExpandPathVariables(const wxString &aPath, const PROJECT *aProject, const std::function< bool(wxString *)> *aTextResolver)
Expand text variables, environment variables and a leading '~' in a user-entered file path.
Definition common.cpp:798
wxString ResolveProjectPath(const wxString &aPath, const PROJECT *aProject, const std::function< bool(wxString *)> *aTextResolver)
Expand a user-entered file path as ExpandPathVariables() does, then anchor a relative result at the p...
Definition common.cpp:817
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.
KIFACE_BASE & Kiface()
Global KIFACE_BASE "get" accessor.
Definition cvpcb.cpp:165
#define _(s)
bool OpenPDF(const wxString &file)
Run the PDF viewer and display a PDF file.
Definition gestfich.cpp:325
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition idf_helpers.h:37
@ LAYER_SCHEMATIC_BACKGROUND
Definition layer_ids.h:516
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:65
Plotting engines similar to ps (PostScript, Gerber, svg)
std::vector< FAB_LAYER_COLOR > dummy
wxString m_theme
Definition sch_plotter.h:69
DXF_UNITS m_DXF_File_Unit
Definition sch_plotter.h:76
bool m_PDFPropertyPopups
Definition sch_plotter.h:66
wxString m_outputDirectory
Definition sch_plotter.h:71
bool m_pngAntialias
Definition sch_plotter.h:79
bool m_blackAndWhite
Definition sch_plotter.h:63
wxString m_variant
Definition sch_plotter.h:73
bool m_PDFHierarchicalLinks
Definition sch_plotter.h:67
bool m_useBackgroundColor
Definition sch_plotter.h:65
bool m_plotDrawingSheet
Definition sch_plotter.h:56
std::string path
wxLogTrace helper definitions.
#define FN_NORMALIZE_FLAGS
Default flags to pass to wxFileName::Normalize().
Definition wx_filename.h:35