KiCad PCB EDA Suite
Loading...
Searching...
No Matches
dialog_import_gfx_sch.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) 2018 Jean-Pierre Charras, jp.charras at wanadoo.fr
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
22
26
27#include <base_units.h>
28#include <kiface_base.h>
29#include <locale_io.h>
30#include <bitmaps.h>
32
33#include <eeschema_settings.h>
34#include <sch_edit_frame.h>
36#include <symbol_edit_frame.h>
37
38#include <wx/filedlg.h>
39#include <wx/msgdlg.h>
40#include <kiplatform/ui.h>
41
44
45#include <memory>
46
47// Static members of DIALOG_IMPORT_GFX_SCH, to remember the user's choices during the session
49double DIALOG_IMPORT_GFX_SCH::m_importScale = 1.0; // Do not change the imported items size
50
51
54 m_parent( aParent ),
55 m_xOrigin( aParent, m_xLabel, m_xCtrl, m_xUnits ),
56 m_yOrigin( aParent, m_yLabel, m_yCtrl, m_yUnits ),
58{
61
62 if( SYMBOL_EDIT_FRAME* symFrame = dynamic_cast<SYMBOL_EDIT_FRAME*>( aParent ) )
63 m_importer = std::make_unique<GRAPHICS_IMPORTER_LIB_SYMBOL>( symFrame->GetCurSymbol(), symFrame->GetUnit() );
64 else if( dynamic_cast<SCH_EDIT_FRAME*>( aParent ) )
65 m_importer = std::make_unique<GRAPHICS_IMPORTER_SCH>();
66
67 m_gfxImportMgr = std::make_unique<GRAPHICS_IMPORT_MGR>();
68
69 wxCommandEvent dummy;
71
74
75 GetSizer()->Fit( this );
76 GetSizer()->SetSizeHints( this );
77 Centre();
78
79 m_textCtrlFileName->Connect( wxEVT_COMMAND_TEXT_UPDATED,
80 wxCommandEventHandler( DIALOG_IMPORT_GFX_SCH::onFilename ), nullptr, this );
81}
82
83
85{
86 m_textCtrlFileName->Disconnect( wxEVT_COMMAND_TEXT_UPDATED,
87 wxCommandEventHandler( DIALOG_IMPORT_GFX_SCH::onFilename ), nullptr, this );
88}
89
90
91void DIALOG_IMPORT_GFX_SCH::SetFilenameOverride( const wxString& aFilenameOverride )
92{
93 m_filenameOverride = aFilenameOverride;
94}
95
96
98{
99 DIALOG_SHIM::TransferDataToWindow();
100
101 // We have to set the filename field value here, otherwise it gets overwritten by state loading
102 if( !m_filenameOverride.IsEmpty() )
104
105 return true;
106}
107
108
109void DIALOG_IMPORT_GFX_SCH::onFilename( wxCommandEvent& event )
110{
111 bool enableDXFControls = true;
112 wxString ext = wxFileName( m_textCtrlFileName->GetValue() ).GetExt();
113
114 if( std::unique_ptr<GRAPHICS_IMPORT_PLUGIN> plugin = m_gfxImportMgr->GetPluginByExt( ext ) )
115 enableDXFControls = dynamic_cast<DXF_IMPORT_PLUGIN*>( plugin.get() ) != nullptr;
116
117 m_defaultLineWidth.Enable( enableDXFControls );
118
119 m_staticTextLineWidth1->Enable( enableDXFControls );
120 m_choiceDxfUnits->Enable( enableDXFControls );
121}
122
123
124void DIALOG_IMPORT_GFX_SCH::onBrowseFiles( wxCommandEvent& event )
125{
126 wxString path;
127 wxString filename = m_textCtrlFileName->GetValue();
128
129 if( !filename.IsEmpty() )
130 {
131 wxFileName fn( filename );
132 path = fn.GetPath();
133 filename = fn.GetFullName();
134 }
135
136 // Generate the list of handled file formats
137 wxString wildcardsDesc;
138 wxString allWildcards;
139
140 for( GRAPHICS_IMPORT_MGR::GFX_FILE_T pluginType : m_gfxImportMgr->GetImportableFileTypes() )
141 {
142 std::unique_ptr<GRAPHICS_IMPORT_PLUGIN> plugin = m_gfxImportMgr->GetPlugin( pluginType );
143 const std::vector<std::string> extensions = plugin->GetFileExtensions();
144
145 wildcardsDesc += wxT( "|" ) + plugin->GetName() + AddFileExtListToFilter( extensions );
146 allWildcards += plugin->GetWildcards() + wxT( ";" );
147 }
148
149 wildcardsDesc = _( "All supported formats" ) + wxT( "|" ) + allWildcards + wildcardsDesc;
150
151 wxFileDialog dlg( m_parent, _( "Import Graphics" ), path, filename, wildcardsDesc,
152 wxFD_OPEN | wxFD_FILE_MUST_EXIST );
153
155
156 if( dlg.ShowModal() == wxID_OK && !dlg.GetPath().IsEmpty() )
157 m_textCtrlFileName->SetValue( dlg.GetPath() );
158}
159
160
162{
163 if( !wxDialog::TransferDataFromWindow() )
164 return false;
165
166 if( m_textCtrlFileName->GetValue().IsEmpty() )
167 {
168 wxMessageBox( _( "No file selected!" ) );
169 return false;
170 }
171
172 wxString ext = wxFileName( m_textCtrlFileName->GetValue() ).GetExt();
174
175 if( scale <= 0.0 )
176 {
177 wxMessageBox( _( "Import scale must be a positive number." ) );
178 return false;
179 }
180
181 double xscale = scale;
182 double yscale = scale;
183
184 VECTOR2D origin( m_xOrigin.GetValue() / xscale, m_yOrigin.GetValue() / yscale );
185
186 if( std::unique_ptr<GRAPHICS_IMPORT_PLUGIN> plugin = m_gfxImportMgr->GetPluginByExt( ext ) )
187 {
188 if( DXF_IMPORT_PLUGIN* dxfPlugin = dynamic_cast<DXF_IMPORT_PLUGIN*>( plugin.get() ) )
189 {
190 dxfPlugin->SetUnit( DxfImportUnitFromChoice( m_choiceDxfUnits->GetSelection() ) );
191 m_importer->SetLineWidthMM( schIUScale.IUTomm( m_defaultLineWidth.GetValue() ) );
192 }
193 else
194 {
195 m_importer->SetLineWidthMM( 0.0 );
196 }
197
198 m_importer->SetPlugin( std::move( plugin ) );
199 m_importer->SetImportOffsetMM( { schIUScale.IUTomm( origin.x ), schIUScale.IUTomm( origin.y ) } );
200
201 LOCALE_IO dummy; // Ensure floats can be read.
202
203 if( m_importer->Load( m_textCtrlFileName->GetValue() ) )
204 m_importer->Import( VECTOR2D( xscale, yscale ) );
205
206 // Get warning messages:
207 wxString warnings = m_importer->GetMessages();
208
209 // This isn't a fatal error so allow the dialog to close with wxID_OK.
210 if( !warnings.empty() )
211 {
212 HTML_MESSAGE_BOX dlg( this, _( "Warning" ) );
213 dlg.MessageSet( _( "Items in the imported file could not be handled properly." ) );
214 warnings.Replace( wxT( "\n" ), wxT( "<br/>" ) );
215 dlg.AddHTML_Text( warnings );
216 dlg.ShowModal();
217 }
218
219 return true;
220 }
221 else
222 {
223 wxMessageBox( _( "There is no plugin to handle this file type." ) );
224 return false;
225 }
226}
227
228
240
241
constexpr EDA_IU_SCALE schIUScale
Definition base_units.h:123
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
Definition bitmap.cpp:106
DIALOG_IMPORT_GFX_SCH_BASE(wxWindow *parent, wxWindowID id=wxID_ANY, const wxString &title=_("Import Vector Graphics File"), const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(-1,-1), long style=wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER)
void onBrowseFiles(wxCommandEvent &event) override
std::unique_ptr< GRAPHICS_IMPORTER > m_importer
void onFilename(wxCommandEvent &event)
DIALOG_IMPORT_GFX_SCH(SCH_BASE_FRAME *aParent)
void originOptionOnUpdateUI(wxUpdateUIEvent &event) override
void SetFilenameOverride(const wxString &aFilenameOverride)
Set the filename override to be applied in TransferDataToWindow.
std::unique_ptr< GRAPHICS_IMPORT_MGR > m_gfxImportMgr
void SetInitialFocus(wxWindow *aWindow)
Sets the window (usually a wxTextCtrl) that should be focused when the dialog is shown.
Definition dialog_shim.h:94
void SetupStandardButtons(std::map< int, wxString > aLabels={})
int ShowModal() override
GFX_FILE_T
List of handled file types.
void MessageSet(const wxString &message)
Add a message (in bold) to message list.
void AddHTML_Text(const wxString &message)
Add HTML text (without any change) to message list.
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition locale_io.h:37
A shim class between EDA_DRAW_FRAME and several derived classes: SYMBOL_EDIT_FRAME,...
Schematic editor (Eeschema) main window.
The symbol library editor main window.
wxArrayString GetDxfImportUnitChoices()
DXF_IMPORT_UNITS DxfImportUnitFromChoice(int aSelection)
#define _(s)
KICOMMON_API double DoubleValueFromString(const EDA_IU_SCALE &aIuScale, EDA_UNITS aUnits, const wxString &aTextValue, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE)
Convert aTextValue to a double.
void AllowNetworkFileSystems(wxDialog *aDialog)
Configure a file dialog to show network and virtual file systems.
Definition wxgtk/ui.cpp:521
const int scale
std::vector< FAB_LAYER_COLOR > dummy
std::string path
VECTOR2< double > VECTOR2D
Definition vector2d.h:682
wxString AddFileExtListToFilter(const std::vector< std::string > &aExts)
Build the wildcard extension file dialog wildcard filter to add to the base message dialog.
Definition of file extensions used in Kicad.