KiCad PCB EDA Suite
import_project.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) 2017-2021 KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * @author Russell Oliver <[email protected]>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, you may find one here:
20 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21 * or you may search the http://www.gnu.org website for the version 2 license,
22 * or you may write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 */
25
31#include <wx/dir.h>
32#include <wx/filedlg.h>
33#include <wx/dirdlg.h>
34
35#include <confirm.h>
37
38#include <io_mgr.h>
39#include <sch_io_mgr.h>
40
41#include "kicad_manager_frame.h"
42#include <import_proj.h>
43
44
45void KICAD_MANAGER_FRAME::ImportNonKiCadProject( const wxString& aWindowTitle,
46 const wxString& aFilesWildcard,
47 const wxString& aSchFileExtension,
48 const wxString& aPcbFileExtension,
49 int aSchFileType, int aPcbFileType )
50{
51 wxString msg;
52 wxString default_dir = GetMruPath();
53 int style = wxFD_OPEN | wxFD_FILE_MUST_EXIST;
54
55 wxFileDialog schdlg( this, aWindowTitle, default_dir, wxEmptyString, aFilesWildcard, style );
56
57 if( schdlg.ShowModal() == wxID_CANCEL )
58 return;
59
60 // OK, we got a new project to open. Time to close any existing project before we go on
61 // to collect info about where to put the new one, etc. Otherwise the workflow is kind of
62 // disjoint.
63 CloseProject( true );
64
65 IMPORT_PROJ_HELPER importProj( this, schdlg.GetPath(), aSchFileExtension, aPcbFileExtension );
66
67 wxString protitle = _( "KiCad Project Destination" );
68
69 // Don't use wxFileDialog here. On GTK builds, the default path is returned unless a
70 // file is actually selected.
71 wxDirDialog prodlg( this, protitle, importProj.GetProjPath(), wxDD_DEFAULT_STYLE );
72
73 if( prodlg.ShowModal() == wxID_CANCEL )
74 return;
75
76 importProj.SetProjPath( prodlg.GetPath() );
77
78 // Check if the project directory is empty
79 wxDir directory( importProj.GetProjPath() );
80
81 if( directory.HasFiles() )
82 {
83 msg = _( "The selected directory is not empty. We recommend you "
84 "create projects in their own clean directory.\n\nDo you "
85 "want to create a new empty directory for the project?" );
86
87 KIDIALOG dlg( this, msg, _( "Confirmation" ), wxYES_NO | wxICON_WARNING );
88 dlg.DoNotShowCheckbox( __FILE__, __LINE__ );
89
90 if( dlg.ShowModal() == wxID_YES )
91 {
92 // Append a new directory with the same name of the project file
93 // Keep iterating until we find an empty directory
94 importProj.CreateEmptyDirForProject();
95
96 if( !wxMkdir( importProj.GetProjPath() ) )
97 {
98 msg = _( "Error creating new directory. Please try a different path. The "
99 "project cannot be imported." );
100
101 wxMessageDialog dirErrorDlg( this, msg, _( "Error" ), wxOK_DEFAULT | wxICON_ERROR );
102 dirErrorDlg.ShowModal();
103 return;
104 }
105 }
106 }
107
108 std::string packet;
109
110 importProj.SetProjAbsolutePath();
111
112 if( !importProj.CopyImportedFiles() )
113 return;
114
115 CreateNewProject( importProj.GetProjFullPath(), false /* Don't create stub files */ );
116 LoadProject( importProj.GetProj() );
117
118 importProj.AssociateFilesWithProj( aSchFileType, aPcbFileType );
119
121 m_active_project = true;
122}
123
124
126{
127 ImportNonKiCadProject( _( "Import CADSTAR Archive Project Files" ),
128 CadstarArchiveFilesWildcard(), "csa", "cpa", SCH_IO_MGR::SCH_CADSTAR_ARCHIVE,
130}
131
132
133void KICAD_MANAGER_FRAME::OnImportEagleFiles( wxCommandEvent& event )
134{
135 ImportNonKiCadProject( _( "Import Eagle Project Files" ), EagleFilesWildcard(),
137 SCH_IO_MGR::SCH_EAGLE, IO_MGR::EAGLE );
138}
wxString GetMruPath() const
A helper class to import non Kicad project.
Definition: import_proj.h:12
void AssociateFilesWithProj(int aImportedSchFileType, int aImportedPcbFileType)
Converts imported files to kicad type files.
wxString GetProjFullPath()
Definition: import_proj.cpp:62
wxString GetProjPath()
Definition: import_proj.cpp:50
const wxFileName & GetProj()
Definition: import_proj.cpp:44
void SetProjPath(const wxString aPath)
Definition: import_proj.cpp:56
void CreateEmptyDirForProject()
Appends a new directory with the name of the project file Keep iterating until an empty directory is ...
Definition: import_proj.cpp:74
void SetProjAbsolutePath()
Definition: import_proj.cpp:92
bool CopyImportedFiles(bool displayError=true)
Copies project files to the destination directory.
@ EAGLE
Definition: io_mgr.h:57
@ CADSTAR_PCB_ARCHIVE
Definition: io_mgr.h:63
void CreateNewProject(const wxFileName &aProjectFileName, bool aCreateStubFiles=true)
Creates a new project by setting up and initial project, schematic, and board files.
void OnImportEagleFiles(wxCommandEvent &event)
Open dialog to import Eagle schematic and board files.
void LoadProject(const wxFileName &aProjectFileName)
void ImportNonKiCadProject(const wxString &aWindowTitle, const wxString &aFilesWildcard, const wxString &aSchFileExtension, const wxString &aPcbFileExtension, int aSchFileType, int aPcbFileType)
Creates a project and imports a non-KiCad Schematic and PCB.
bool CloseProject(bool aSave)
Closes the project, and saves it if aSave is true;.
void OnImportCadstarArchiveFiles(wxCommandEvent &event)
Open dialog to import CADSTAR Schematic and PCB Archive files.
Helper class to create more flexible dialogs, including 'do not show again' checkbox handling.
Definition: confirm.h:46
void DoNotShowCheckbox(wxString file, int line)
Checks the 'do not show again' setting for the dialog.
Definition: confirm.cpp:56
int ShowModal() override
Definition: confirm.cpp:100
This file is part of the common library.
#define _(s)
const std::string LegacyPcbFileExtension
const std::string LegacySchematicFileExtension
wxString EagleFilesWildcard()
wxString CadstarArchiveFilesWildcard()
Definition of file extensions used in Kicad.