KiCad PCB EDA Suite
Loading...
Searching...
No Matches
dialog_import_gfx.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 (C) 1992-2023 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 "dialog_import_gfx.h"
28#include <base_units.h>
29#include <kiface_base.h>
30#include <locale_io.h>
33#include <bitmaps.h>
35#include <map>
36#include "dxf_import_plugin.h"
37#include <wx/filedlg.h>
38#include <wx/msgdlg.h>
39
40#include <memory>
41
42// Static members of DIALOG_IMPORT_GFX, to remember the user's choices during the session
45double DIALOG_IMPORT_GFX::m_importScale = 1.0; // Do not change the imported items size
46
47
48const std::map<DXF_IMPORT_UNITS, wxString> dxfUnitsMap = {
49 { DXF_IMPORT_UNITS::INCHES, _( "Inches" ) },
50 { DXF_IMPORT_UNITS::MILLIMETERS, _( "Millimeters" ) },
51 { DXF_IMPORT_UNITS::MILS, _( "Mils" ) },
52 { DXF_IMPORT_UNITS::CENTIMETERS, _( "Centimeter" ) },
53 { DXF_IMPORT_UNITS::FEET, _( "Feet" ) },
54};
55
56
57DIALOG_IMPORT_GFX::DIALOG_IMPORT_GFX( PCB_BASE_FRAME* aParent, bool aImportAsFootprintGraphic ) :
58 DIALOG_IMPORT_GFX_BASE( aParent ),
59 m_parent( aParent ),
60 m_xOrigin( aParent, m_xLabel, m_xCtrl, m_xUnits ),
61 m_yOrigin( aParent, m_yLabel, m_yCtrl, m_yUnits ),
62 m_defaultLineWidth( aParent, m_lineWidthLabel, m_lineWidthCtrl, m_lineWidthUnits )
63{
64 if( aImportAsFootprintGraphic )
65 m_importer = std::make_unique<GRAPHICS_IMPORTER_FOOTPRINT>( m_parent->GetBoard()->GetFirstFootprint() );
66 else
67 m_importer = std::make_unique<GRAPHICS_IMPORTER_BOARD>( m_parent->GetBoard() );
68
69 // construct an import manager with options from config
70 {
72 // Currently: all types are allowed, so the blacklist is empty
73 // (no GFX_FILE_T in the blacklist)
74 // To disable SVG import, enable these 2 lines
75 // if( !ADVANCED_CFG::GetCfg().m_enableSvgImport )
76 // blacklist.push_back( GRAPHICS_IMPORT_MGR::SVG );
77 // The SVG import has currently a flaw: all SVG shapes are imported as curves and
78 // converted to a lot of segments. A better approach is to convert to polylines
79 // (not yet existing in Pcbnew) and keep arcs and circles as primitives (not yet
80 // possible with tinysvg library).
81
82 m_gfxImportMgr = std::make_unique<GRAPHICS_IMPORT_MGR>( blacklist );
83 }
84
86
88
92
97
98 m_importScaleCtrl->SetValue( wxString::Format( wxT( "%f" ), m_importScale ) );
99
100 // Configure the layers list selector
101 m_SelLayerBox->SetLayersHotkeys( false ); // Do not display hotkeys
104
107
108 for( const std::pair<const DXF_IMPORT_UNITS, wxString>& unitEntry : dxfUnitsMap )
109 m_choiceDxfUnits->Append( unitEntry.second );
110
111 m_choiceDxfUnits->SetSelection( cfg->m_ImportGraphics.dxf_units );
112
113 m_browseButton->SetBitmap( KiBitmap( BITMAPS::small_folder ) );
114
115 wxCommandEvent dummy;
116 onFilename( dummy );
117
120
121 GetSizer()->Fit( this );
122 GetSizer()->SetSizeHints( this );
123 Centre();
124
125 m_textCtrlFileName->Connect( wxEVT_COMMAND_TEXT_UPDATED,
126 wxCommandEventHandler( DIALOG_IMPORT_GFX::onFilename ),
127 nullptr, this );
128}
129
130
132{
134
141 cfg->m_ImportGraphics.dxf_units = m_choiceDxfUnits->GetSelection();
142
144
145 m_textCtrlFileName->Disconnect( wxEVT_COMMAND_TEXT_UPDATED,
146 wxCommandEventHandler( DIALOG_IMPORT_GFX::onFilename ),
147 nullptr, this );
148}
149
150
151void DIALOG_IMPORT_GFX::onFilename( wxCommandEvent& event )
152{
153 bool enableDXFControls = true;
154 wxString ext = wxFileName( m_textCtrlFileName->GetValue() ).GetExt();
155
156 if( std::unique_ptr<GRAPHICS_IMPORT_PLUGIN> plugin = m_gfxImportMgr->GetPluginByExt( ext ) )
157 enableDXFControls = dynamic_cast<DXF_IMPORT_PLUGIN*>( plugin.get() ) != nullptr;
158
159 m_defaultLineWidth.Enable( enableDXFControls );
160
161 m_staticTextLineWidth1->Enable( enableDXFControls );
162 m_choiceDxfUnits->Enable( enableDXFControls );
163}
164
165
166void DIALOG_IMPORT_GFX::onBrowseFiles( wxCommandEvent& event )
167{
168 wxString path;
169 wxString filename = m_textCtrlFileName->GetValue();
170
171 if( !filename.IsEmpty() )
172 {
173 wxFileName fn( filename );
174 path = fn.GetPath();
175 filename = fn.GetFullName();
176 }
177
178 // Generate the list of handled file formats
179 wxString wildcardsDesc;
180 wxString allWildcards;
181
182 for( GRAPHICS_IMPORT_MGR::GFX_FILE_T pluginType : m_gfxImportMgr->GetImportableFileTypes() )
183 {
184 std::unique_ptr<GRAPHICS_IMPORT_PLUGIN> plugin = m_gfxImportMgr->GetPlugin( pluginType );
185 const std::vector<std::string> extensions = plugin->GetFileExtensions();
186
187 wildcardsDesc += wxT( "|" ) + plugin->GetName() + AddFileExtListToFilter( extensions );
188 allWildcards += plugin->GetWildcards() + wxT( ";" );
189 }
190
191 wildcardsDesc = _( "All supported formats" ) + wxT( "|" ) + allWildcards + wildcardsDesc;
192
193 wxFileDialog dlg( m_parent, _( "Open File" ), path, filename, wildcardsDesc,
194 wxFD_OPEN | wxFD_FILE_MUST_EXIST );
195
196 if( dlg.ShowModal() == wxID_OK && !dlg.GetPath().IsEmpty() )
197 m_textCtrlFileName->SetValue( dlg.GetPath() );
198}
199
200
202{
203 if( !wxDialog::TransferDataFromWindow() )
204 return false;
205
206 if( m_textCtrlFileName->GetValue().IsEmpty() )
207 {
208 wxMessageBox( _( "No file selected!" ) );
209 return false;
210 }
211
213 {
214 wxMessageBox( _( "Please select a valid layer." ) );
215 return false;
216 }
217
219 wxString ext = wxFileName( m_textCtrlFileName->GetValue() ).GetExt();
221 double xscale = scale;
222 double yscale = scale;
223
225 {
226 xscale *= -1.0;
227 }
228
230 {
231 yscale *= -1.0;
232 }
233
234 VECTOR2D origin( m_xOrigin.GetValue() / xscale, m_yOrigin.GetValue() / yscale );
235
236 if( std::unique_ptr<GRAPHICS_IMPORT_PLUGIN> plugin = m_gfxImportMgr->GetPluginByExt( ext ) )
237 {
238 if( DXF_IMPORT_PLUGIN* dxfPlugin = dynamic_cast<DXF_IMPORT_PLUGIN*>( plugin.get() ) )
239 {
240 auto it = dxfUnitsMap.begin();
241 std::advance( it, m_choiceDxfUnits->GetSelection() );
242
243 if( it == dxfUnitsMap.end() )
244 dxfPlugin->SetUnit( DXF_IMPORT_UNITS::DEFAULT );
245 else
246 dxfPlugin->SetUnit( it->first );
247
249 }
250 else
251 {
252 m_importer->SetLineWidthMM( 0.0 );
253 }
254
255 m_importer->SetPlugin( std::move( plugin ) );
257 m_importer->SetImportOffsetMM( { pcbIUScale.IUTomm( origin.x ), pcbIUScale.IUTomm( origin.y ) } );
258
259 LOCALE_IO dummy; // Ensure floats can be read.
260
261 if( m_importer->Load( m_textCtrlFileName->GetValue() ) )
262 m_importer->Import( scale );
263
264 // Get warning messages:
265 wxString warnings = m_importer->GetMessages();
266
267 // This isn't a fatal error so allow the dialog to close with wxID_OK.
268 if( !warnings.empty() )
269 {
270 HTML_MESSAGE_BOX dlg( this, _( "Warning" ) );
271 dlg.MessageSet( _( "Items in the imported file could not be handled properly." ) );
272 warnings.Replace( wxT( "\n" ), wxT( "<br/>" ) );
273 dlg.AddHTML_Text( warnings );
274 dlg.ShowModal();
275 }
276
277 return true;
278 }
279 else
280 {
281 wxMessageBox( _( "There is no plugin to handle this file type." ) );
282 return false;
283 }
284}
285
286
287void DIALOG_IMPORT_GFX::originOptionOnUpdateUI( wxUpdateUIEvent& event )
288{
291
294
297}
298
299
constexpr EDA_IU_SCALE pcbIUScale
Definition: base_units.h:109
wxBitmap KiBitmap(BITMAPS aBitmap, int aHeightTag)
Construct a wxBitmap from an image identifier Returns the image from the active theme if the image ha...
Definition: bitmap.cpp:106
FOOTPRINT * GetFirstFootprint() const
Get the first footprint on the board or nullptr.
Definition: board.h:405
Class DIALOG_IMPORT_GFX_BASE.
PCB_LAYER_BOX_SELECTOR * m_SelLayerBox
wxRadioButton * m_rbInteractivePlacement
wxRadioButton * m_rbAbsolutePlacement
wxStaticText * m_staticTextLineWidth1
STD_BITMAP_BUTTON * m_browseButton
void onFilename(wxCommandEvent &event)
static double m_importScale
bool TransferDataFromWindow() override
void originOptionOnUpdateUI(wxUpdateUIEvent &event) override
PCB_BASE_FRAME * m_parent
DIALOG_IMPORT_GFX(PCB_BASE_FRAME *aParent, bool aUseModuleItems=false)
std::unique_ptr< GRAPHICS_IMPORT_MGR > m_gfxImportMgr
static bool m_placementInteractive
UNIT_BINDER m_defaultLineWidth
static bool m_shouldGroupItems
void onBrowseFiles(wxCommandEvent &event) override
std::unique_ptr< GRAPHICS_IMPORTER_PCBNEW > m_importer
void SetInitialFocus(wxWindow *aWindow)
Sets the window (usually a wxTextCtrl) that should be focused when the dialog is shown.
Definition: dialog_shim.h:97
void SetupStandardButtons(std::map< int, wxString > aLabels={})
GFX_FILE_T
< List of handled file types.
std::vector< GFX_FILE_T > TYPE_LIST
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.
int SetLayerSelection(int layer)
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
DIALOG_IMPORT_GRAPHICS m_ImportGraphics
Base PCB main window class for Pcbnew, Gerbview, and CvPcb footprint viewer.
PCBNEW_SETTINGS * GetPcbNewSettings() const
BOARD * GetBoard() const
void SetBoardFrame(PCB_BASE_FRAME *aFrame)
void SetBitmap(const wxBitmapBundle &aBmp)
virtual long long int GetValue()
Return the current value in Internal Units.
void Enable(bool aEnable)
Enable/disable the label, widget and units label.
virtual void SetValue(long long int aValue)
Set new value (in Internal Units) for the text field, taking care of units conversion.
const std::map< DXF_IMPORT_UNITS, wxString > dxfUnitsMap
#define _(s)
wxString AddFileExtListToFilter(const std::vector< std::string > &aExts)
Build the wildcard extension file dialog wildcard filter to add to the base message dialog.
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:59
@ Dwgs_User
Definition: layer_ids.h:109
double DoubleValueFromString(const EDA_IU_SCALE &aIuScale, EDA_UNITS aUnits, const wxString &aTextValue, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE)
Function DoubleValueFromString converts aTextValue to a double.
Definition: eda_units.cpp:445
const int scale
std::vector< FAB_LAYER_COLOR > dummy
constexpr double IUTomm(int iu) const
Definition: base_units.h:87
const double IU_PER_MM
Definition: base_units.h:77
Definition of file extensions used in Kicad.