kicad/kicad/import_project.cpp

175 lines
5.3 KiB
C++

/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2017-2020 KiCad Developers, see AUTHORS.txt for contributors.
*
* @author Russell Oliver <roliver8143@gmail.com>
*
* 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
*/
/**
* @file import_project.cpp
* @brief routines for importing a non-KiCad project
*/
#include <wx/filename.h>
#include <wx/dir.h>
#include <common.h>
#include <confirm.h>
#include <kiway.h>
#include <richio.h>
#include <wildcards_and_files_ext.h>
#include <kiway_player.h>
#include <stdexcept>
#include "pgm_kicad.h"
#include <io_mgr.h>
#include <sch_io_mgr.h>
#include "kicad_manager_frame.h"
void KICAD_MANAGER_FRAME::ImportNonKiCadProject( wxString aWindowTitle, wxString aFilesWildcard,
wxString aSchFileExtension, wxString aPcbFileExtension, int aSchFileType, int aPcbFileType )
{
int style = wxFD_OPEN | wxFD_FILE_MUST_EXIST;
wxString default_dir = GetMruPath();
ClearMsg();
wxFileDialog schdlg( this, aWindowTitle, default_dir, wxEmptyString, aFilesWildcard, style );
if( schdlg.ShowModal() == wxID_CANCEL )
return;
wxFileName sch( schdlg.GetPath() );
sch.SetExt( aSchFileExtension );
wxFileName pro = sch;
pro.SetExt( ProjectFileExtension );
wxString protitle = _( "KiCad Project Destination" );
// Don't use wxFileDialog here. On GTK builds, the default path is returned unless a
// file is actually selected.
wxDirDialog prodlg( this, protitle, pro.GetPath(), wxDD_DEFAULT_STYLE );
if( prodlg.ShowModal() == wxID_CANCEL )
return;
pro.SetPath( prodlg.GetPath() );
// Check if the project directory is empty
wxDir directory( pro.GetPath() );
if( directory.HasFiles() )
{
wxString msg =
_( "The selected directory is not empty. We recommend you "
"create projects in their own clean directory.\n\nDo you "
"want to create a new empty directory for the project?" );
KIDIALOG dlg( this, msg, _( "Confirmation" ), wxYES_NO | wxICON_WARNING );
dlg.DoNotShowCheckbox( __FILE__, __LINE__ );
if( dlg.ShowModal() == wxID_YES )
{
// Append a new directory with the same name of the project file
// and try to create it
pro.AppendDir( pro.GetName() );
if( !wxMkdir( pro.GetPath() ) )
// There was a problem, undo
pro.RemoveLastDir();
}
}
wxFileName pcb( sch );
std::string packet;
pro.SetExt( ProjectFileExtension );
pcb.SetExt( aPcbFileExtension ); // enforce extension
if( !pro.IsAbsolute() )
pro.MakeAbsolute();
// Close the project and make the new one
CloseProject( true );
CreateNewProject( pro.GetFullPath(), false /* Don't create stub files */ );
LoadProject( pro );
if( sch.FileExists() )
{
KIWAY_PLAYER* schframe = Kiway().Player( FRAME_SCH, true );
packet = StrPrintf( "%d\n%s", aSchFileType, TO_UTF8( sch.GetFullPath() ) );
schframe->Kiway().ExpressMail( FRAME_SCH, MAIL_IMPORT_FILE, packet, this );
if( !schframe->IsShown() )
schframe->Show( true );
// On Windows, Raise() does not bring the window on screen, when iconized
if( schframe->IsIconized() )
schframe->Iconize( false );
schframe->Raise();
}
if( pcb.FileExists() )
{
KIWAY_PLAYER* pcbframe = Kiway().Player( FRAME_PCB_EDITOR, true );
if( !pcbframe->IsVisible() )
pcbframe->Show( true );
packet = StrPrintf( "%d\n%s", aPcbFileType, TO_UTF8( pcb.GetFullPath() ) );
pcbframe->Kiway().ExpressMail( FRAME_PCB_EDITOR, MAIL_IMPORT_FILE, packet, this );
// On Windows, Raise() does not bring the window on screen, when iconized
if( pcbframe->IsIconized() )
pcbframe->Iconize( false );
pcbframe->Raise();
}
ReCreateTreePrj();
m_active_project = true;
}
void KICAD_MANAGER_FRAME::OnImportCadstarArchiveFiles( wxCommandEvent& event )
{
ImportNonKiCadProject( _( "Import CADSTAR Archive Project Files" ),
CadstarArchiveFilesWildcard(), "csa", "cpa", SCH_IO_MGR::SCH_CADSTAR_ARCHIVE,
IO_MGR::CADSTAR_PCB_ARCHIVE );
}
void KICAD_MANAGER_FRAME::OnImportEagleFiles( wxCommandEvent& event )
{
ImportNonKiCadProject( _( "Import Eagle Project Files" ), EagleFilesWildcard(),
LegacySchematicFileExtension, LegacyPcbFileExtension,
SCH_IO_MGR::SCH_EAGLE, IO_MGR::EAGLE );
}