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 if( !CloseProject( true ) )
64 return;
65
66 IMPORT_PROJ_HELPER importProj( this, schdlg.GetPath(), aSchFileExtension, aPcbFileExtension );
67
68 wxString protitle = _( "KiCad Project Destination" );
69
70 // Don't use wxFileDialog here. On GTK builds, the default path is returned unless a
71 // file is actually selected.
72 wxDirDialog prodlg( this, protitle, importProj.GetProjPath(), wxDD_DEFAULT_STYLE );
73
74 if( prodlg.ShowModal() == wxID_CANCEL )
75 return;
76
77 importProj.SetProjPath( prodlg.GetPath() );
78
79 // Check if the project directory is empty
80 wxDir directory( importProj.GetProjPath() );
81
82 if( directory.HasFiles() )
83 {
84 msg = _( "The selected directory is not empty. We recommend you "
85 "create projects in their own clean directory.\n\nDo you "
86 "want to create a new empty directory for the project?" );
87
88 KIDIALOG dlg( this, msg, _( "Confirmation" ), wxYES_NO | wxICON_WARNING );
89 dlg.DoNotShowCheckbox( __FILE__, __LINE__ );
90
91 if( dlg.ShowModal() == wxID_YES )
92 {
93 // Append a new directory with the same name of the project file
94 // Keep iterating until we find an empty directory
95 importProj.CreateEmptyDirForProject();
96
97 if( !wxMkdir( importProj.GetProjPath() ) )
98 {
99 msg = _( "Error creating new directory. Please try a different path. The "
100 "project cannot be imported." );
101
102 wxMessageDialog dirErrorDlg( this, msg, _( "Error" ), wxOK_DEFAULT | wxICON_ERROR );
103 dirErrorDlg.ShowModal();
104 return;
105 }
106 }
107 }
108
109 std::string packet;
110
111 importProj.SetProjAbsolutePath();
112
113 if( !importProj.CopyImportedFiles() )
114 return;
115
116 CreateNewProject( importProj.GetProjFullPath(), false /* Don't create stub files */ );
117 LoadProject( importProj.GetProj() );
118
119 importProj.AssociateFilesWithProj( aSchFileType, aPcbFileType );
120
122 m_active_project = true;
123}
124
125
127{
128 ImportNonKiCadProject( _( "Import CADSTAR Archive Project Files" ),
129 CadstarArchiveFilesWildcard(), "csa", "cpa", SCH_IO_MGR::SCH_CADSTAR_ARCHIVE,
131}
132
133
134void KICAD_MANAGER_FRAME::OnImportEagleFiles( wxCommandEvent& event )
135{
136 ImportNonKiCadProject( _( "Import Eagle Project Files" ), EagleFilesWildcard(),
138 SCH_IO_MGR::SCH_EAGLE, IO_MGR::EAGLE );
139}
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:76
int ShowModal() override
Definition: confirm.cpp:120
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.