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
41#include <memory>
42
43
44const std::map<DXF_IMPORT_UNITS, wxString> dxfUnitsMap = {
45 { DXF_IMPORT_UNITS::INCH, _( "Inches" ) },
46 { DXF_IMPORT_UNITS::MM, _( "Millimeters" ) },
47 { DXF_IMPORT_UNITS::MILS, _( "Mils" ) },
48 { DXF_IMPORT_UNITS::CM, _( "Centimeter" ) },
49 { DXF_IMPORT_UNITS::FEET, _( "Feet" ) },
50};
51
52
55 m_parent( aParent ),
56 m_xOrigin( aParent, nullptr, m_xCtrl, nullptr ),
57 m_yOrigin( aParent, m_yLabel, m_yCtrl, m_yUnits ),
58 m_defaultLineWidth( aParent, m_lineWidthLabel, m_lineWidthCtrl, m_lineWidthUnits ),
59 m_tolerance( aParent, m_toleranceLabel, m_toleranceCtrl, m_toleranceUnits )
60{
61 // The SVG import has currently a flaw: all SVG shapes are imported as curves and
62 // converted to a lot of segments. A better approach is to convert to polylines
63 // (not yet existing in Pcbnew) and keep arcs and circles as primitives (not yet
64 // possible with tinysvg library).
65
66 m_importer = std::make_unique<GRAPHICS_IMPORTER_PCBNEW>( aParent->GetModel() );
67 m_gfxImportMgr = std::make_unique<GRAPHICS_IMPORT_MGR>();
68
69 // Configure the layers list selector
70 m_SelLayerBox->SetLayersHotkeys( false ); // Do not display hotkeys
73
74 for( const std::pair<const DXF_IMPORT_UNITS, wxString>& unitEntry : dxfUnitsMap )
75 m_dxfUnitsChoice->Append( unitEntry.second );
76
77 m_browseButton->SetBitmap( KiBitmapBundle( BITMAPS::small_folder ) );
78
79 wxCommandEvent dummy;
81
84
85 GetSizer()->Fit( this );
86 GetSizer()->SetSizeHints( this );
87 Centre();
88
89 m_textCtrlFileName->Connect( wxEVT_COMMAND_TEXT_UPDATED,
90 wxCommandEventHandler( DIALOG_IMPORT_GRAPHICS::onFilename ),
91 nullptr, this );
92}
93
94
96{
97 m_textCtrlFileName->Disconnect( wxEVT_COMMAND_TEXT_UPDATED,
98 wxCommandEventHandler( DIALOG_IMPORT_GRAPHICS::onFilename ),
99 nullptr, this );
100}
101
102
103void DIALOG_IMPORT_GRAPHICS::onFilename( wxCommandEvent& event )
104{
105 bool enableDXFControls = true;
106 wxString ext = wxFileName( m_textCtrlFileName->GetValue() ).GetExt();
107
108 if( std::unique_ptr<GRAPHICS_IMPORT_PLUGIN> plugin = m_gfxImportMgr->GetPluginByExt( ext ) )
109 enableDXFControls = dynamic_cast<DXF_IMPORT_PLUGIN*>( plugin.get() ) != nullptr;
110
111 m_defaultLineWidth.Enable( enableDXFControls );
112
113 m_dxfUnitsLabel->Enable( enableDXFControls );
114 m_dxfUnitsChoice->Enable( enableDXFControls );
115}
116
117
118void DIALOG_IMPORT_GRAPHICS::onBrowseFiles( wxCommandEvent& event )
119{
120 wxString path;
121 wxString filename = m_textCtrlFileName->GetValue();
122
123 if( !filename.IsEmpty() )
124 {
125 wxFileName fn( filename );
126 path = fn.GetPath();
127 filename = fn.GetFullName();
128 }
129
130 // Generate the list of handled file formats
131 wxString wildcardsDesc;
132 wxString allWildcards;
133
134 for( GRAPHICS_IMPORT_MGR::GFX_FILE_T pluginType : m_gfxImportMgr->GetImportableFileTypes() )
135 {
136 std::unique_ptr<GRAPHICS_IMPORT_PLUGIN> plugin = m_gfxImportMgr->GetPlugin( pluginType );
137 const std::vector<std::string> extensions = plugin->GetFileExtensions();
138
139 wildcardsDesc += wxT( "|" ) + plugin->GetName() + AddFileExtListToFilter( extensions );
140 allWildcards += plugin->GetWildcards() + wxT( ";" );
141 }
142
143 wildcardsDesc = _( "All supported formats" ) + wxT( "|" ) + allWildcards + wildcardsDesc;
144
145 wxFileDialog dlg( m_parent, _( "Import Graphics" ), path, filename, wildcardsDesc,
146 wxFD_OPEN | wxFD_FILE_MUST_EXIST );
147
148 if( dlg.ShowModal() == wxID_OK && !dlg.GetPath().IsEmpty() )
149 m_textCtrlFileName->SetValue( dlg.GetPath() );
150}
151
152
154{
155 if( !wxDialog::TransferDataFromWindow() )
156 return false;
157
158 if( m_textCtrlFileName->GetValue().IsEmpty() )
159 {
160 wxMessageBox( _( "Please select a file to import." ) );
161 return false;
162 }
163
164 if( m_setLayerCheckbox->GetValue() && m_SelLayerBox->GetLayerSelection() < 0 )
165 {
166 wxMessageBox( _( "Please select a valid layer." ) );
167 return false;
168 }
169
171 wxString ext = wxFileName( m_textCtrlFileName->GetValue() ).GetExt();
173 double xscale = scale;
174 double yscale = scale;
175
177 xscale *= -1.0;
178
180 yscale *= -1.0;
181
182 VECTOR2D origin( m_xOrigin.GetDoubleValue() / xscale, m_yOrigin.GetDoubleValue() / yscale );
183
184 if( std::unique_ptr<GRAPHICS_IMPORT_PLUGIN> plugin = m_gfxImportMgr->GetPluginByExt( ext ) )
185 {
186 if( DXF_IMPORT_PLUGIN* dxfPlugin = dynamic_cast<DXF_IMPORT_PLUGIN*>( plugin.get() ) )
187 {
188 auto it = dxfUnitsMap.begin();
189 std::advance( it, m_dxfUnitsChoice->GetSelection() );
190
191 if( it == dxfUnitsMap.end() )
192 dxfPlugin->SetUnit( DXF_IMPORT_UNITS::DEFAULT );
193 else
194 dxfPlugin->SetUnit( it->first );
195
197 }
198 else
199 {
200 m_importer->SetLineWidthMM( 0.0 );
201 }
202
203 m_importer->SetPlugin( std::move( plugin ) );
204
205 if( m_setLayerCheckbox->GetValue() )
207 else
208 m_importer->SetLayer( m_parent->GetActiveLayer() );
209
210 m_importer->SetImportOffsetMM( origin * pcbIUScale.IUTomm( 1 ) );
211
212 LOCALE_IO dummy; // Ensure floats can be read.
213
214 if( m_importer->Load( m_textCtrlFileName->GetValue() ) )
215 m_importer->Import( VECTOR2D( scale, scale ) );
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
240void DIALOG_IMPORT_GRAPHICS::onUpdateUI( wxUpdateUIEvent& event )
241{
242 m_xOrigin.Enable( m_placeAtCheckbox->GetValue() );
243 m_yOrigin.Enable( m_placeAtCheckbox->GetValue() );
244
246
247 m_SelLayerBox->Enable( m_setLayerCheckbox->GetValue() );
248}
249
250
constexpr EDA_IU_SCALE pcbIUScale
Definition: base_units.h:112
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
Definition: bitmap.cpp:110
Class DIALOG_IMPORT_GRAPHICS_BASE.
PCB_LAYER_BOX_SELECTOR * m_SelLayerBox
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 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:75
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.
bool SetLayersHotkeys(bool value)
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.
PCBNEW_SETTINGS * GetPcbNewSettings() const
virtual PCB_LAYER_ID GetActiveLayer() const
virtual BOARD_ITEM_CONTAINER * GetModel() const =0
void SetBoardFrame(PCB_BASE_FRAME *aFrame)
void SetBitmap(const wxBitmapBundle &aBmp)
int GetIntValue()
Definition: unit_binder.h:134
void Enable(bool aEnable)
Enable/disable the label, widget and units label.
virtual double GetDoubleValue()
Return the current value in Internal Units.
const std::map< DXF_IMPORT_UNITS, wxString > dxfUnitsMap
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.
Definition: eda_units.cpp:569
const int scale
std::vector< FAB_LAYER_COLOR > dummy
constexpr double IUTomm(int iu) const
Definition: base_units.h:90
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.