KiCad PCB EDA Suite
Loading...
Searching...
No Matches
dialog_import_graphics.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
29#include <base_units.h>
30#include <kiface_base.h>
31#include <locale_io.h>
34#include <bitmaps.h>
36#include <map>
37#include <footprint.h>
38#include <wx/filedlg.h>
39#include <wx/msgdlg.h>
40#include <kiplatform/ui.h>
41
42#include <memory>
43
44
45const std::map<DXF_IMPORT_UNITS, wxString> dxfUnitsMap = {
46 { DXF_IMPORT_UNITS::INCH, _( "Inches" ) },
47 { DXF_IMPORT_UNITS::MM, _( "Millimeters" ) },
48 { DXF_IMPORT_UNITS::MILS, _( "Mils" ) },
49 { DXF_IMPORT_UNITS::CM, _( "Centimeter" ) },
50 { DXF_IMPORT_UNITS::FEET, _( "Feet" ) },
51};
52
53
56 m_parent( aParent ),
57 m_xOrigin( aParent, nullptr, m_xCtrl, nullptr ),
58 m_yOrigin( aParent, m_yLabel, m_yCtrl, m_yUnits ),
61{
62 // The SVG import has currently a flaw: all SVG shapes are imported as curves and
63 // converted to a lot of segments. A better approach is to convert to polylines
64 // (not yet existing in Pcbnew) and keep arcs and circles as primitives (not yet
65 // possible with tinysvg library).
66
67 m_importer = std::make_unique<GRAPHICS_IMPORTER_PCBNEW>( aParent->GetModel() );
68 m_gfxImportMgr = std::make_unique<GRAPHICS_IMPORT_MGR>();
69
70 // Configure the layers list selector
71 m_SelLayerBox->SetLayersHotkeys( false ); // Do not display hotkeys
72 m_SelLayerBox->SetBoardFrame( m_parent );
73 m_SelLayerBox->Resync();
74
75 for( const std::pair<const DXF_IMPORT_UNITS, wxString>& unitEntry : dxfUnitsMap )
76 m_dxfUnitsChoice->Append( unitEntry.second );
77
79
80 wxCommandEvent dummy;
82
85
86 GetSizer()->Fit( this );
87 GetSizer()->SetSizeHints( this );
88 Centre();
89
90 m_textCtrlFileName->Connect( wxEVT_COMMAND_TEXT_UPDATED,
91 wxCommandEventHandler( DIALOG_IMPORT_GRAPHICS::onFilename ),
92 nullptr, this );
93}
94
95
97{
98 m_textCtrlFileName->Disconnect( wxEVT_COMMAND_TEXT_UPDATED,
99 wxCommandEventHandler( DIALOG_IMPORT_GRAPHICS::onFilename ),
100 nullptr, this );
101}
102
103
104void DIALOG_IMPORT_GRAPHICS::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_GRAPHICS::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_dxfUnitsLabel->Enable( enableDXFControls );
133 m_dxfUnitsChoice->Enable( enableDXFControls );
134}
135
136
137void DIALOG_IMPORT_GRAPHICS::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
168
169 if( dlg.ShowModal() == wxID_OK && !dlg.GetPath().IsEmpty() )
170 m_textCtrlFileName->SetValue( dlg.GetPath() );
171}
172
173
175{
176 if( !wxDialog::TransferDataFromWindow() )
177 return false;
178
179 if( m_textCtrlFileName->GetValue().IsEmpty() )
180 {
181 wxMessageBox( _( "Please select a file to import." ) );
182 return false;
183 }
184
185 if( m_setLayerCheckbox->GetValue() && m_SelLayerBox->GetLayerSelection() < 0 )
186 {
187 wxMessageBox( _( "Please select a valid layer." ) );
188 return false;
189 }
190
191 PCBNEW_SETTINGS* cfg = m_parent->GetPcbNewSettings();
192 wxString ext = wxFileName( m_textCtrlFileName->GetValue() ).GetExt();
194 double xscale = scale;
195 double yscale = scale;
196
198 xscale *= -1.0;
199
201 yscale *= -1.0;
202
203 VECTOR2D origin( m_xOrigin.GetDoubleValue() / xscale, m_yOrigin.GetDoubleValue() / yscale );
204
205 if( std::unique_ptr<GRAPHICS_IMPORT_PLUGIN> plugin = m_gfxImportMgr->GetPluginByExt( ext ) )
206 {
207 if( DXF_IMPORT_PLUGIN* dxfPlugin = dynamic_cast<DXF_IMPORT_PLUGIN*>( plugin.get() ) )
208 {
209 auto it = dxfUnitsMap.begin();
210 std::advance( it, m_dxfUnitsChoice->GetSelection() );
211
212 if( it == dxfUnitsMap.end() )
213 dxfPlugin->SetUnit( DXF_IMPORT_UNITS::DEFAULT );
214 else
215 dxfPlugin->SetUnit( it->first );
216
217 m_importer->SetLineWidthMM( pcbIUScale.IUTomm( m_defaultLineWidth.GetIntValue() ) );
218 }
219 else
220 {
221 m_importer->SetLineWidthMM( 0.0 );
222 }
223
224 m_importer->SetPlugin( std::move( plugin ) );
225
226 if( m_setLayerCheckbox->GetValue() )
227 m_importer->SetLayer( PCB_LAYER_ID( m_SelLayerBox->GetLayerSelection() ) );
228 else
229 m_importer->SetLayer( m_parent->GetActiveLayer() );
230
231 m_importer->SetImportOffsetMM( origin * pcbIUScale.IUTomm( 1 ) );
232
233 LOCALE_IO dummy; // Ensure floats can be read.
234
235 if( m_importer->Load( m_textCtrlFileName->GetValue() ) )
236 m_importer->Import( VECTOR2D( scale, scale ) );
237
238 // Get warning messages:
239 wxString warnings = m_importer->GetMessages();
240
241 // This isn't a fatal error so allow the dialog to close with wxID_OK.
242 if( !warnings.empty() )
243 {
244 HTML_MESSAGE_BOX dlg( this, _( "Warning" ) );
245 dlg.MessageSet( _( "Items in the imported file could not be handled properly." ) );
246 warnings.Replace( wxT( "\n" ), wxT( "<br/>" ) );
247 dlg.AddHTML_Text( warnings );
248 dlg.ShowModal();
249 }
250
251 return true;
252 }
253 else
254 {
255 wxMessageBox( _( "There is no plugin to handle this file type." ) );
256 return false;
257 }
258}
259
260
261void DIALOG_IMPORT_GRAPHICS::onUpdateUI( wxUpdateUIEvent& event )
262{
263 m_xOrigin.Enable( m_placeAtCheckbox->GetValue() );
264 m_yOrigin.Enable( m_placeAtCheckbox->GetValue() );
265
266 m_tolerance.Enable( m_rbFixDiscontinuities->GetValue() );
267
268 m_SelLayerBox->Enable( m_setLayerCheckbox->GetValue() );
269}
270
271
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:112
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
Definition bitmap.cpp:110
DIALOG_IMPORT_GRAPHICS_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)
std::unique_ptr< GRAPHICS_IMPORT_MGR > m_gfxImportMgr
void onFilename(wxCommandEvent &event)
std::unique_ptr< GRAPHICS_IMPORTER_PCBNEW > m_importer
void onBrowseFiles(wxCommandEvent &event) override
void SetFilenameOverride(const wxString &aFilenameOverride)
Set the filename override to be applied in TransferDataToWindow.
void onUpdateUI(wxUpdateUIEvent &event) override
DIALOG_IMPORT_GRAPHICS(PCB_BASE_FRAME *aParent)
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
DISPLAY_OPTIONS m_Display
Base PCB main window class for Pcbnew, Gerbview, and CvPcb footprint viewer.
virtual BOARD_ITEM_CONTAINER * GetModel() const =0
const std::map< DXF_IMPORT_UNITS, wxString > dxfUnitsMap
#define _(s)
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:60
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:717
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.