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, you may find one here:
19 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20 * or you may search the http://www.gnu.org website for the version 2 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
26
27#include <map>
28
32
33#include <base_units.h>
34#include <kiface_base.h>
35#include <locale_io.h>
36#include <bitmaps.h>
38
39#include <eeschema_settings.h>
40#include <sch_edit_frame.h>
42#include <symbol_edit_frame.h>
43
44#include <wx/filedlg.h>
45#include <wx/msgdlg.h>
46
49
50#include <memory>
51
52// Static members of DIALOG_IMPORT_GFX_SCH, to remember the user's choices during the session
54double DIALOG_IMPORT_GFX_SCH::m_importScale = 1.0; // Do not change the imported items size
55
56
57const std::map<DXF_IMPORT_UNITS, wxString> dxfUnitsMap = {
58 { DXF_IMPORT_UNITS::INCH, _( "Inches" ) },
59 { DXF_IMPORT_UNITS::MM, _( "Millimeters" ) },
60 { DXF_IMPORT_UNITS::MILS, _( "Mils" ) },
61 { DXF_IMPORT_UNITS::CM, _( "Centimeter" ) },
62 { DXF_IMPORT_UNITS::FEET, _( "Feet" ) },
63};
64
65
68 m_parent( aParent ),
69 m_xOrigin( aParent, m_xLabel, m_xCtrl, m_xUnits ),
70 m_yOrigin( aParent, m_yLabel, m_yCtrl, m_yUnits ),
72{
74
75 if( SYMBOL_EDIT_FRAME* symFrame = dynamic_cast<SYMBOL_EDIT_FRAME*>( aParent ) )
76 m_importer = std::make_unique<GRAPHICS_IMPORTER_LIB_SYMBOL>( symFrame->GetCurSymbol(), symFrame->GetUnit() );
77 else if( dynamic_cast<SCH_EDIT_FRAME*>( aParent ) )
78 m_importer = std::make_unique<GRAPHICS_IMPORTER_SCH>();
79
80 m_gfxImportMgr = std::make_unique<GRAPHICS_IMPORT_MGR>();
81
82 wxCommandEvent dummy;
84
87
88 GetSizer()->Fit( this );
89 GetSizer()->SetSizeHints( this );
90 Centre();
91
92 m_textCtrlFileName->Connect( wxEVT_COMMAND_TEXT_UPDATED,
93 wxCommandEventHandler( DIALOG_IMPORT_GFX_SCH::onFilename ), nullptr, this );
94}
95
96
98{
99 m_textCtrlFileName->Disconnect( wxEVT_COMMAND_TEXT_UPDATED,
100 wxCommandEventHandler( DIALOG_IMPORT_GFX_SCH::onFilename ), nullptr, this );
101}
102
103
104void DIALOG_IMPORT_GFX_SCH::SetFilenameOverride( const wxString& aFilenameOverride )
105{
106 m_filenameOverride = aFilenameOverride;
107}
108
109
111{
112 DIALOG_SHIM::TransferDataToWindow();
113
114 // We have to set the filename field value here, otherwise it gets overwritten by state loading
115 if( !m_filenameOverride.IsEmpty() )
117
118 return true;
119}
120
121
122void DIALOG_IMPORT_GFX_SCH::onFilename( wxCommandEvent& event )
123{
124 bool enableDXFControls = true;
125 wxString ext = wxFileName( m_textCtrlFileName->GetValue() ).GetExt();
126
127 if( std::unique_ptr<GRAPHICS_IMPORT_PLUGIN> plugin = m_gfxImportMgr->GetPluginByExt( ext ) )
128 enableDXFControls = dynamic_cast<DXF_IMPORT_PLUGIN*>( plugin.get() ) != nullptr;
129
130 m_defaultLineWidth.Enable( enableDXFControls );
131
132 m_staticTextLineWidth1->Enable( enableDXFControls );
133 m_choiceDxfUnits->Enable( enableDXFControls );
134}
135
136
137void DIALOG_IMPORT_GFX_SCH::onBrowseFiles( wxCommandEvent& event )
138{
139 wxString path;
140 wxString filename = m_textCtrlFileName->GetValue();
141
142 if( !filename.IsEmpty() )
143 {
144 wxFileName fn( filename );
145 path = fn.GetPath();
146 filename = fn.GetFullName();
147 }
148
149 // Generate the list of handled file formats
150 wxString wildcardsDesc;
151 wxString allWildcards;
152
153 for( GRAPHICS_IMPORT_MGR::GFX_FILE_T pluginType : m_gfxImportMgr->GetImportableFileTypes() )
154 {
155 std::unique_ptr<GRAPHICS_IMPORT_PLUGIN> plugin = m_gfxImportMgr->GetPlugin( pluginType );
156 const std::vector<std::string> extensions = plugin->GetFileExtensions();
157
158 wildcardsDesc += wxT( "|" ) + plugin->GetName() + AddFileExtListToFilter( extensions );
159 allWildcards += plugin->GetWildcards() + wxT( ";" );
160 }
161
162 wildcardsDesc = _( "All supported formats" ) + wxT( "|" ) + allWildcards + wildcardsDesc;
163
164 wxFileDialog dlg( m_parent, _( "Import Graphics" ), path, filename, wildcardsDesc,
165 wxFD_OPEN | wxFD_FILE_MUST_EXIST );
166
167 if( dlg.ShowModal() == wxID_OK && !dlg.GetPath().IsEmpty() )
168 m_textCtrlFileName->SetValue( dlg.GetPath() );
169}
170
171
173{
174 if( !wxDialog::TransferDataFromWindow() )
175 return false;
176
177 if( m_textCtrlFileName->GetValue().IsEmpty() )
178 {
179 wxMessageBox( _( "No file selected!" ) );
180 return false;
181 }
182
183 wxString ext = wxFileName( m_textCtrlFileName->GetValue() ).GetExt();
185 double xscale = scale;
186 double yscale = scale;
187
188 VECTOR2D origin( m_xOrigin.GetValue() / xscale, m_yOrigin.GetValue() / yscale );
189
190 if( std::unique_ptr<GRAPHICS_IMPORT_PLUGIN> plugin = m_gfxImportMgr->GetPluginByExt( ext ) )
191 {
192 if( DXF_IMPORT_PLUGIN* dxfPlugin = dynamic_cast<DXF_IMPORT_PLUGIN*>( plugin.get() ) )
193 {
194 auto it = dxfUnitsMap.begin();
195 std::advance( it, m_choiceDxfUnits->GetSelection() );
196
197 if( it == dxfUnitsMap.end() )
198 dxfPlugin->SetUnit( DXF_IMPORT_UNITS::DEFAULT );
199 else
200 dxfPlugin->SetUnit( it->first );
201
202 m_importer->SetLineWidthMM( schIUScale.IUTomm( m_defaultLineWidth.GetValue() ) );
203 }
204 else
205 {
206 m_importer->SetLineWidthMM( 0.0 );
207 }
208
209 m_importer->SetPlugin( std::move( plugin ) );
210 m_importer->SetImportOffsetMM( { schIUScale.IUTomm( origin.x ), schIUScale.IUTomm( origin.y ) } );
211
212 LOCALE_IO dummy; // Ensure floats can be read.
213
214 if( m_importer->Load( m_textCtrlFileName->GetValue() ) )
215 m_importer->Import( VECTOR2D( xscale, yscale ) );
216
217 // Get warning messages:
218 wxString warnings = m_importer->GetMessages();
219
220 // This isn't a fatal error so allow the dialog to close with wxID_OK.
221 if( !warnings.empty() )
222 {
223 HTML_MESSAGE_BOX dlg( this, _( "Warning" ) );
224 dlg.MessageSet( _( "Items in the imported file could not be handled properly." ) );
225 warnings.Replace( wxT( "\n" ), wxT( "<br/>" ) );
226 dlg.AddHTML_Text( warnings );
227 dlg.ShowModal();
228 }
229
230 return true;
231 }
232 else
233 {
234 wxMessageBox( _( "There is no plugin to handle this file type." ) );
235 return false;
236 }
237}
238
239
251
252
constexpr EDA_IU_SCALE schIUScale
Definition base_units.h:114
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
Definition bitmap.cpp:110
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:82
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:41
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.
const std::map< DXF_IMPORT_UNITS, wxString > dxfUnitsMap
#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.
const int scale
std::vector< FAB_LAYER_COLOR > dummy
std::string path
VECTOR2< double > VECTOR2D
Definition vector2d.h:694
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.