2018-10-13 18:37:28 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2018-11-05 16:04:05 +00:00
|
|
|
* Copyright (C) 2018 Jean-Pierre Charras, jp.charras at wanadoo.fr
|
2022-09-12 23:58:55 +00:00
|
|
|
* Copyright (C) 1992-2022 KiCad Developers, see AUTHORS.txt for contributors.
|
2018-10-13 18:37:28 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
2021-09-14 18:26:03 +00:00
|
|
|
#include <dialogs/html_message_box.h>
|
2019-09-06 18:57:04 +00:00
|
|
|
|
2018-12-12 12:26:03 +00:00
|
|
|
#include "dialog_import_gfx.h"
|
2022-08-27 17:23:43 +00:00
|
|
|
#include <base_units.h>
|
2021-09-14 22:45:14 +00:00
|
|
|
#include <kiface_base.h>
|
2020-10-24 01:38:50 +00:00
|
|
|
#include <locale_io.h>
|
2018-11-05 16:04:05 +00:00
|
|
|
#include <pcb_layer_box_selector.h>
|
|
|
|
#include <wildcards_and_files_ext.h>
|
2020-11-23 11:44:46 +00:00
|
|
|
#include <bitmaps.h>
|
2020-11-22 03:11:24 +00:00
|
|
|
#include <map>
|
|
|
|
#include "dxf_import_plugin.h"
|
2021-05-01 07:50:29 +00:00
|
|
|
#include <wx/filedlg.h>
|
2021-06-06 12:41:16 +00:00
|
|
|
#include <wx/msgdlg.h>
|
2018-10-13 18:37:28 +00:00
|
|
|
|
2019-12-05 13:43:55 +00:00
|
|
|
#include <memory>
|
|
|
|
|
2019-04-03 13:57:31 +00:00
|
|
|
// Static members of DIALOG_IMPORT_GFX, to remember the user's choices during the session
|
2022-09-12 23:58:55 +00:00
|
|
|
bool DIALOG_IMPORT_GFX::m_placementInteractive = true;
|
|
|
|
bool DIALOG_IMPORT_GFX::m_shouldGroupItems = true;
|
|
|
|
double DIALOG_IMPORT_GFX::m_importScale = 1.0; // Do not change the imported items size
|
|
|
|
|
2020-11-22 03:11:24 +00:00
|
|
|
|
|
|
|
const std::map<DXF_IMPORT_UNITS, wxString> dxfUnitsMap = {
|
2022-09-12 23:58:55 +00:00
|
|
|
{ DXF_IMPORT_UNITS::INCHES, _( "Inches" ) },
|
2020-11-22 03:11:24 +00:00
|
|
|
{ DXF_IMPORT_UNITS::MILLIMETERS, _( "Millimeters" ) },
|
2022-09-12 23:58:55 +00:00
|
|
|
{ DXF_IMPORT_UNITS::MILS, _( "Mils" ) },
|
2020-11-22 03:11:24 +00:00
|
|
|
{ DXF_IMPORT_UNITS::CENTIMETERS, _( "Centimeter" ) },
|
2022-09-12 23:58:55 +00:00
|
|
|
{ DXF_IMPORT_UNITS::FEET, _( "Feet" ) },
|
2020-11-22 03:11:24 +00:00
|
|
|
};
|
2019-04-03 13:57:31 +00:00
|
|
|
|
2018-10-13 18:37:28 +00:00
|
|
|
|
2022-09-12 23:58:55 +00:00
|
|
|
DIALOG_IMPORT_GFX::DIALOG_IMPORT_GFX( PCB_BASE_FRAME* aParent, bool aImportAsFootprintGraphic ) :
|
|
|
|
DIALOG_IMPORT_GFX_BASE( aParent ),
|
|
|
|
m_parent( aParent ),
|
|
|
|
m_xOrigin( aParent, m_xLabel, m_xCtrl, m_xUnits ),
|
|
|
|
m_yOrigin( aParent, m_yLabel, m_yCtrl, m_yUnits ),
|
|
|
|
m_defaultLineWidth( aParent, m_lineWidthLabel, m_lineWidthCtrl, m_lineWidthUnits )
|
2018-10-13 18:37:28 +00:00
|
|
|
{
|
2018-11-05 16:04:05 +00:00
|
|
|
if( aImportAsFootprintGraphic )
|
2020-11-13 01:33:30 +00:00
|
|
|
m_importer = std::make_unique<GRAPHICS_IMPORTER_FOOTPRINT>( m_parent->GetBoard()->GetFirstFootprint() );
|
2018-10-13 18:37:28 +00:00
|
|
|
else
|
2019-12-05 13:43:55 +00:00
|
|
|
m_importer = std::make_unique<GRAPHICS_IMPORTER_BOARD>( m_parent->GetBoard() );
|
2018-10-13 18:37:28 +00:00
|
|
|
|
2018-12-12 12:26:03 +00:00
|
|
|
// construct an import manager with options from config
|
|
|
|
{
|
|
|
|
GRAPHICS_IMPORT_MGR::TYPE_LIST blacklist;
|
2019-03-11 17:30:59 +00:00
|
|
|
// Currently: all types are allowed, so the blacklist is empty
|
|
|
|
// (no GFX_FILE_T in the blacklist)
|
2019-03-11 18:25:52 +00:00
|
|
|
// To disable SVG import, enable these 2 lines
|
|
|
|
// if( !ADVANCED_CFG::GetCfg().m_enableSvgImport )
|
|
|
|
// blacklist.push_back( GRAPHICS_IMPORT_MGR::SVG );
|
2019-07-16 01:31:07 +00:00
|
|
|
// The SVG import has currently a flaw: all SVG shapes are imported as curves and
|
|
|
|
// converted to a lot of segments. A better approach is to convert to polylines
|
|
|
|
// (not yet existing in Pcbnew) and keep arcs and circles as primitives (not yet
|
|
|
|
// possible with tinysvg library).
|
2019-03-11 18:25:52 +00:00
|
|
|
|
2018-12-12 12:26:03 +00:00
|
|
|
m_gfxImportMgr = std::make_unique<GRAPHICS_IMPORT_MGR>( blacklist );
|
|
|
|
}
|
|
|
|
|
2022-09-12 23:58:55 +00:00
|
|
|
PCBNEW_SETTINGS* cfg = m_parent->GetPcbNewSettings();
|
2018-10-13 18:37:28 +00:00
|
|
|
|
2020-01-13 01:44:19 +00:00
|
|
|
m_placementInteractive = cfg->m_ImportGraphics.interactive_placement;
|
2018-11-05 16:04:05 +00:00
|
|
|
|
2022-09-12 23:58:55 +00:00
|
|
|
m_xOrigin.SetValue( cfg->m_ImportGraphics.origin_x * IU_PER_MM );
|
|
|
|
m_yOrigin.SetValue( cfg->m_ImportGraphics.origin_y * IU_PER_MM );
|
|
|
|
m_defaultLineWidth.SetValue( cfg->m_ImportGraphics.dxf_line_width * IU_PER_MM );
|
2018-10-13 18:37:28 +00:00
|
|
|
|
2022-09-12 23:58:55 +00:00
|
|
|
m_textCtrlFileName->SetValue( cfg->m_ImportGraphics.last_file );
|
2018-11-05 16:04:05 +00:00
|
|
|
m_rbInteractivePlacement->SetValue( m_placementInteractive );
|
2022-09-12 23:58:55 +00:00
|
|
|
m_rbAbsolutePlacement->SetValue( !m_placementInteractive );
|
2020-08-20 00:22:48 +00:00
|
|
|
m_groupItems->SetValue( m_shouldGroupItems );
|
2018-11-05 16:04:05 +00:00
|
|
|
|
2022-09-12 23:58:55 +00:00
|
|
|
m_importScaleCtrl->SetValue( wxString::Format( wxT( "%f" ), m_importScale ) );
|
2018-10-13 18:37:28 +00:00
|
|
|
|
|
|
|
// Configure the layers list selector
|
2022-09-12 23:58:55 +00:00
|
|
|
m_SelLayerBox->SetLayersHotkeys( false ); // Do not display hotkeys
|
2018-10-13 18:37:28 +00:00
|
|
|
m_SelLayerBox->SetBoardFrame( m_parent );
|
|
|
|
m_SelLayerBox->Resync();
|
|
|
|
|
2022-09-12 23:58:55 +00:00
|
|
|
if( m_SelLayerBox->SetLayerSelection( cfg->m_ImportGraphics.layer ) < 0 )
|
|
|
|
m_SelLayerBox->SetLayerSelection( Dwgs_User );
|
2018-10-13 18:37:28 +00:00
|
|
|
|
2022-09-12 23:58:55 +00:00
|
|
|
for( const std::pair<const DXF_IMPORT_UNITS, wxString>& unitEntry : dxfUnitsMap )
|
2020-11-22 03:11:24 +00:00
|
|
|
m_choiceDxfUnits->Append( unitEntry.second );
|
|
|
|
|
2022-09-12 23:58:55 +00:00
|
|
|
m_choiceDxfUnits->SetSelection( cfg->m_ImportGraphics.dxf_units );
|
2020-11-22 03:11:24 +00:00
|
|
|
|
2021-03-08 02:59:07 +00:00
|
|
|
m_browseButton->SetBitmap( KiBitmap( BITMAPS::small_folder ) );
|
2020-11-23 11:44:46 +00:00
|
|
|
|
2022-07-18 16:49:50 +00:00
|
|
|
wxCommandEvent dummy;
|
|
|
|
onFilename( dummy );
|
|
|
|
|
2019-04-03 13:57:31 +00:00
|
|
|
SetInitialFocus( m_textCtrlFileName );
|
2021-11-16 19:39:58 +00:00
|
|
|
SetupStandardButtons();
|
|
|
|
|
2018-10-13 18:37:28 +00:00
|
|
|
GetSizer()->Fit( this );
|
|
|
|
GetSizer()->SetSizeHints( this );
|
|
|
|
Centre();
|
2022-07-18 16:49:50 +00:00
|
|
|
|
|
|
|
m_textCtrlFileName->Connect( wxEVT_COMMAND_TEXT_UPDATED,
|
|
|
|
wxCommandEventHandler( DIALOG_IMPORT_GFX::onFilename ),
|
2022-09-12 23:58:55 +00:00
|
|
|
nullptr, this );
|
2018-10-13 18:37:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DIALOG_IMPORT_GFX::~DIALOG_IMPORT_GFX()
|
|
|
|
{
|
2022-09-12 23:58:55 +00:00
|
|
|
PCBNEW_SETTINGS* cfg = m_parent->GetPcbNewSettings();
|
2020-01-13 01:44:19 +00:00
|
|
|
|
2022-09-12 23:58:55 +00:00
|
|
|
cfg->m_ImportGraphics.layer = m_SelLayerBox->GetLayerSelection();
|
2020-01-13 01:44:19 +00:00
|
|
|
cfg->m_ImportGraphics.interactive_placement = m_placementInteractive;
|
2022-09-12 23:58:55 +00:00
|
|
|
cfg->m_ImportGraphics.last_file = m_textCtrlFileName->GetValue();
|
|
|
|
cfg->m_ImportGraphics.dxf_line_width = Iu2Millimeter( m_defaultLineWidth.GetValue() );
|
|
|
|
cfg->m_ImportGraphics.origin_x = Iu2Millimeter( m_xOrigin.GetValue() );
|
|
|
|
cfg->m_ImportGraphics.origin_y = Iu2Millimeter( m_yOrigin.GetValue() );
|
|
|
|
cfg->m_ImportGraphics.dxf_units = m_choiceDxfUnits->GetSelection();
|
|
|
|
|
|
|
|
m_importScale = DoubleValueFromString( EDA_UNITS::UNSCALED, m_importScaleCtrl->GetValue() );
|
2022-07-18 16:49:50 +00:00
|
|
|
|
|
|
|
m_textCtrlFileName->Disconnect( wxEVT_COMMAND_TEXT_UPDATED,
|
|
|
|
wxCommandEventHandler( DIALOG_IMPORT_GFX::onFilename ),
|
2022-09-12 23:58:55 +00:00
|
|
|
nullptr, this );
|
2018-11-05 16:04:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-07-18 16:49:50 +00:00
|
|
|
void DIALOG_IMPORT_GFX::onFilename( wxCommandEvent& event )
|
|
|
|
{
|
|
|
|
bool enableDXFControls = true;
|
2022-09-12 23:58:55 +00:00
|
|
|
wxString ext = wxFileName( m_textCtrlFileName->GetValue() ).GetExt();
|
2022-07-18 16:49:50 +00:00
|
|
|
|
2022-09-12 23:58:55 +00:00
|
|
|
if( std::unique_ptr<GRAPHICS_IMPORT_PLUGIN> plugin = m_gfxImportMgr->GetPluginByExt( ext ) )
|
2022-07-18 16:49:50 +00:00
|
|
|
enableDXFControls = dynamic_cast<DXF_IMPORT_PLUGIN*>( plugin.get() ) != nullptr;
|
|
|
|
|
2022-09-12 23:58:55 +00:00
|
|
|
m_defaultLineWidth.Enable( enableDXFControls );
|
2022-07-18 16:49:50 +00:00
|
|
|
|
|
|
|
m_staticTextLineWidth1->Enable( enableDXFControls );
|
|
|
|
m_choiceDxfUnits->Enable( enableDXFControls );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-05 16:04:05 +00:00
|
|
|
void DIALOG_IMPORT_GFX::onBrowseFiles( wxCommandEvent& event )
|
2018-10-13 18:37:28 +00:00
|
|
|
{
|
|
|
|
wxString path;
|
2022-09-12 23:58:55 +00:00
|
|
|
wxString filename = m_textCtrlFileName->GetValue();
|
2018-10-13 18:37:28 +00:00
|
|
|
|
2022-09-12 23:58:55 +00:00
|
|
|
if( !filename.IsEmpty() )
|
2018-10-13 18:37:28 +00:00
|
|
|
{
|
2022-09-12 23:58:55 +00:00
|
|
|
wxFileName fn( filename );
|
2018-10-13 18:37:28 +00:00
|
|
|
path = fn.GetPath();
|
|
|
|
filename = fn.GetFullName();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate the list of handled file formats
|
|
|
|
wxString wildcardsDesc;
|
|
|
|
wxString allWildcards;
|
|
|
|
|
2022-07-18 16:49:50 +00:00
|
|
|
for( GRAPHICS_IMPORT_MGR::GFX_FILE_T pluginType : m_gfxImportMgr->GetImportableFileTypes() )
|
2018-10-13 18:37:28 +00:00
|
|
|
{
|
2022-07-18 16:49:50 +00:00
|
|
|
std::unique_ptr<GRAPHICS_IMPORT_PLUGIN> plugin = m_gfxImportMgr->GetPlugin( pluginType );
|
|
|
|
const std::vector<std::string> extensions = plugin->GetFileExtensions();
|
2018-10-13 18:37:28 +00:00
|
|
|
|
2022-02-05 13:25:43 +00:00
|
|
|
wildcardsDesc += wxT( "|" ) + plugin->GetName() + AddFileExtListToFilter( extensions );
|
|
|
|
allWildcards += plugin->GetWildcards() + wxT( ";" );
|
2018-10-13 18:37:28 +00:00
|
|
|
}
|
|
|
|
|
2019-04-03 13:57:31 +00:00
|
|
|
wildcardsDesc = _( "All supported formats|" ) + allWildcards + wildcardsDesc;
|
2018-10-13 18:37:28 +00:00
|
|
|
|
2019-07-16 01:31:07 +00:00
|
|
|
wxFileDialog dlg( m_parent, _( "Open File" ), path, filename, wildcardsDesc,
|
|
|
|
wxFD_OPEN | wxFD_FILE_MUST_EXIST );
|
2018-10-13 18:37:28 +00:00
|
|
|
|
2022-09-12 23:58:55 +00:00
|
|
|
if( dlg.ShowModal() == wxID_OK && !dlg.GetPath().IsEmpty() )
|
|
|
|
m_textCtrlFileName->SetValue( dlg.GetPath() );
|
2018-10-13 18:37:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-04-03 13:57:31 +00:00
|
|
|
bool DIALOG_IMPORT_GFX::TransferDataFromWindow()
|
2018-10-13 18:37:28 +00:00
|
|
|
{
|
2019-04-03 13:57:31 +00:00
|
|
|
if( !wxDialog::TransferDataFromWindow() )
|
|
|
|
return false;
|
|
|
|
|
2022-09-12 23:58:55 +00:00
|
|
|
if( m_textCtrlFileName->GetValue().IsEmpty() )
|
2018-11-05 16:04:05 +00:00
|
|
|
{
|
2019-04-03 13:57:31 +00:00
|
|
|
wxMessageBox( _( "No file selected!" ) );
|
|
|
|
return false;
|
2018-11-05 16:04:05 +00:00
|
|
|
}
|
2018-10-13 18:37:28 +00:00
|
|
|
|
2022-09-12 23:58:55 +00:00
|
|
|
if( m_SelLayerBox->GetLayerSelection() < 0 )
|
2018-10-13 18:37:28 +00:00
|
|
|
{
|
2019-04-03 13:57:31 +00:00
|
|
|
wxMessageBox( _( "Please select a valid layer." ) );
|
|
|
|
return false;
|
2018-10-13 18:37:28 +00:00
|
|
|
}
|
|
|
|
|
2022-09-12 23:58:55 +00:00
|
|
|
wxString ext = wxFileName( m_textCtrlFileName->GetValue() ).GetExt();
|
|
|
|
double scale = DoubleValueFromString( EDA_UNITS::UNSCALED, m_importScaleCtrl->GetValue() );
|
|
|
|
VECTOR2D origin( m_xOrigin.GetValue() / scale, m_yOrigin.GetValue() / scale );
|
2020-11-22 03:11:24 +00:00
|
|
|
|
2022-09-12 23:58:55 +00:00
|
|
|
if( std::unique_ptr<GRAPHICS_IMPORT_PLUGIN> plugin = m_gfxImportMgr->GetPluginByExt( ext ) )
|
2018-10-13 18:37:28 +00:00
|
|
|
{
|
2022-09-12 23:58:55 +00:00
|
|
|
if( DXF_IMPORT_PLUGIN* dxfPlugin = dynamic_cast<DXF_IMPORT_PLUGIN*>( plugin.get() ) )
|
2020-11-22 03:11:24 +00:00
|
|
|
{
|
|
|
|
auto it = dxfUnitsMap.begin();
|
2022-09-12 23:58:55 +00:00
|
|
|
std::advance( it, m_choiceDxfUnits->GetSelection() );
|
2020-11-22 03:11:24 +00:00
|
|
|
|
|
|
|
if( it == dxfUnitsMap.end() )
|
|
|
|
dxfPlugin->SetUnit( DXF_IMPORT_UNITS::DEFAULT );
|
|
|
|
else
|
|
|
|
dxfPlugin->SetUnit( it->first );
|
2022-07-18 16:49:50 +00:00
|
|
|
|
2022-09-12 23:58:55 +00:00
|
|
|
m_importer->SetLineWidthMM( Iu2Millimeter( m_defaultLineWidth.GetValue() ) );
|
2022-07-18 16:49:50 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_importer->SetLineWidthMM( 0.0 );
|
2020-11-22 03:11:24 +00:00
|
|
|
}
|
|
|
|
|
2018-10-13 18:37:28 +00:00
|
|
|
m_importer->SetPlugin( std::move( plugin ) );
|
2022-09-12 23:58:55 +00:00
|
|
|
m_importer->SetLayer( PCB_LAYER_ID( m_SelLayerBox->GetLayerSelection() ) );
|
|
|
|
m_importer->SetImportOffsetMM( { Iu2Millimeter( origin.x ), Iu2Millimeter( origin.y ) } );
|
2018-10-13 18:37:28 +00:00
|
|
|
|
2018-11-05 16:04:05 +00:00
|
|
|
LOCALE_IO dummy; // Ensure floats can be read.
|
|
|
|
|
2022-09-12 23:58:55 +00:00
|
|
|
if( m_importer->Load( m_textCtrlFileName->GetValue() ) )
|
|
|
|
m_importer->Import( scale );
|
2018-10-13 18:37:28 +00:00
|
|
|
|
2018-11-05 16:04:05 +00:00
|
|
|
// Get warning messages:
|
2019-09-06 18:57:04 +00:00
|
|
|
wxString warnings = m_importer->GetMessages();
|
2018-11-05 16:04:05 +00:00
|
|
|
|
2019-04-03 13:57:31 +00:00
|
|
|
// This isn't a fatal error so allow the dialog to close with wxID_OK.
|
2018-11-05 16:04:05 +00:00
|
|
|
if( !warnings.empty() )
|
2019-09-06 18:57:04 +00:00
|
|
|
{
|
|
|
|
HTML_MESSAGE_BOX dlg( this, _( "Warning" ) );
|
|
|
|
dlg.MessageSet( _( "Items in the imported file could not be handled properly." ) );
|
2022-02-05 13:25:43 +00:00
|
|
|
warnings.Replace( wxT( "\n" ), wxT( "<br/>" ) );
|
2019-09-06 18:57:04 +00:00
|
|
|
dlg.AddHTML_Text( warnings );
|
|
|
|
dlg.ShowModal();
|
|
|
|
}
|
2022-09-12 23:58:55 +00:00
|
|
|
|
|
|
|
return true;
|
2018-10-13 18:37:28 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-04-03 13:57:31 +00:00
|
|
|
wxMessageBox( _( "There is no plugin to handle this file type." ) );
|
|
|
|
return false;
|
2018-10-13 18:37:28 +00:00
|
|
|
}
|
2019-04-03 13:57:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DIALOG_IMPORT_GFX::originOptionOnUpdateUI( wxUpdateUIEvent& event )
|
|
|
|
{
|
|
|
|
if( m_rbInteractivePlacement->GetValue() != m_placementInteractive )
|
|
|
|
m_rbInteractivePlacement->SetValue( m_placementInteractive );
|
|
|
|
|
|
|
|
if( m_rbAbsolutePlacement->GetValue() == m_placementInteractive )
|
2022-09-12 23:58:55 +00:00
|
|
|
m_rbAbsolutePlacement->SetValue( !m_placementInteractive );
|
2019-04-03 13:57:31 +00:00
|
|
|
|
2022-09-12 23:58:55 +00:00
|
|
|
m_xOrigin.Enable( !m_placementInteractive );
|
|
|
|
m_yOrigin.Enable( !m_placementInteractive );
|
2018-10-13 18:37:28 +00:00
|
|
|
}
|
|
|
|
|
2018-11-05 16:04:05 +00:00
|
|
|
|