KiCad PCB EDA Suite
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
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 The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * @author Russell Oliver <roliver8143@gmail.com>
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>
36#include <kidialog.h>
38
39#include <sch_io/sch_io_mgr.h>
40#include <pcb_io/pcb_io_mgr.h>
41
42#include "kicad_manager_frame.h"
43#include <import_proj.h>
44
45
46void KICAD_MANAGER_FRAME::ImportNonKiCadProject( const wxString& aWindowTitle,
47 const wxString& aFilesWildcard,
48 const std::vector<std::string>& aSchFileExtensions,
49 const std::vector<std::string>& aPcbFileExtensions,
50 int aSchFileType, int aPcbFileType )
51{
52 wxString msg;
53 wxString default_dir = GetMruPath();
54 int style = wxFD_OPEN | wxFD_FILE_MUST_EXIST;
55
56 wxFileDialog inputdlg( this, aWindowTitle, default_dir, wxEmptyString, aFilesWildcard, style );
57
58 if( inputdlg.ShowModal() == wxID_CANCEL )
59 return;
60
61 // OK, we got a new project to open. Time to close any existing project before we go on
62 // to collect info about where to put the new one, etc. Otherwise the workflow is kind of
63 // disjoint.
64 if( !CloseProject( true ) )
65 return;
66
67 std::vector<wxString> schFileExts( aSchFileExtensions.begin(), aSchFileExtensions.end() );
68 std::vector<wxString> pcbFileExts( aPcbFileExtensions.begin(), aPcbFileExtensions.end() );
69
70 IMPORT_PROJ_HELPER importProj( this, schFileExts, pcbFileExts );
71 importProj.m_InputFile = inputdlg.GetPath();
72
73 // Don't use wxFileDialog here. On GTK builds, the default path is returned unless a
74 // file is actually selected.
75 wxDirDialog prodlg( this, _( "KiCad Project Destination" ), importProj.m_InputFile.GetPath(),
76 wxDD_DEFAULT_STYLE );
77
78 if( prodlg.ShowModal() == wxID_CANCEL )
79 return;
80
81 wxString targetDir = prodlg.GetPath();
82
83 importProj.m_TargetProj.SetPath( targetDir );
84 importProj.m_TargetProj.SetName( importProj.m_InputFile.GetName() );
86 importProj.m_TargetProj.MakeAbsolute();
87
88 // Check if the project directory exists and is empty
89 if( !importProj.m_TargetProj.DirExists() )
90 {
91 if( !importProj.m_TargetProj.Mkdir() )
92 {
93 msg.Printf( _( "Folder '%s' could not be created.\n\n"
94 "Make sure you have write permissions and try again." ),
95 importProj.m_TargetProj.GetPath() );
96 DisplayErrorMessage( this, msg );
97 return;
98 }
99 }
100 else
101 {
102 wxDir targetDirTest( targetDir );
103 if( targetDirTest.IsOpened() && targetDirTest.HasFiles() )
104 {
105 msg = _( "The selected directory is not empty. We recommend you "
106 "create projects in their own clean directory.\n\nDo you "
107 "want to create a new empty directory for the project?" );
108
109 KIDIALOG dlg( this, msg, _( "Confirmation" ), wxYES_NO | wxICON_WARNING );
110 dlg.DoNotShowCheckbox( __FILE__, __LINE__ );
111
112 if( dlg.ShowModal() == wxID_YES )
113 {
114 // Append a new directory with the same name of the project file
115 // Keep iterating until we find an empty directory
116 importProj.FindEmptyTargetDir();
117
118 if( !wxMkdir( importProj.m_TargetProj.GetPath() ) )
119 {
120 msg = _( "Error creating new directory. Please try a different path. The "
121 "project cannot be imported." );
122
123 wxMessageDialog dirErrorDlg( this, msg, _( "Error" ),
124 wxOK_DEFAULT | wxICON_ERROR );
125 dirErrorDlg.ShowModal();
126 return;
127 }
128 }
129 }
130
131 targetDirTest.Close();
132 }
133
134 CreateNewProject( importProj.m_TargetProj.GetFullPath(), false /* Don't create stub files */ );
135 LoadProject( importProj.m_TargetProj );
136
137 importProj.ImportFiles( aSchFileType, aPcbFileType );
138
140 m_active_project = true;
141}
142
143
145{
146 ImportNonKiCadProject( _( "Import Altium Project Files" ),
147 FILEEXT::AltiumProjectFilesWildcard(), { "SchDoc" }, { "PcbDoc" },
148 SCH_IO_MGR::SCH_ALTIUM, PCB_IO_MGR::ALTIUM_DESIGNER );
149}
150
151
153{
154 ImportNonKiCadProject( _( "Import CADSTAR Archive Project Files" ),
155 FILEEXT::CadstarArchiveFilesWildcard(), { "csa" }, { "cpa" },
156 SCH_IO_MGR::SCH_CADSTAR_ARCHIVE, PCB_IO_MGR::CADSTAR_PCB_ARCHIVE );
157}
158
159
160void KICAD_MANAGER_FRAME::OnImportEagleFiles( wxCommandEvent& event )
161{
162 ImportNonKiCadProject( _( "Import Eagle Project Files" ), FILEEXT::EagleFilesWildcard(), { "sch" },
163 { "brd" }, SCH_IO_MGR::SCH_EAGLE, PCB_IO_MGR::EAGLE );
164}
165
166
167void KICAD_MANAGER_FRAME::OnImportEasyEdaFiles( wxCommandEvent& event )
168{
169 ImportNonKiCadProject( _( "Import EasyEDA Std Backup" ), FILEEXT::EasyEdaArchiveWildcard(), { "INPUT" },
170 { "INPUT" }, SCH_IO_MGR::SCH_EASYEDA, PCB_IO_MGR::EASYEDA );
171}
172
173
175{
176 ImportNonKiCadProject( _( "Import EasyEDA Pro Project" ), FILEEXT::EasyEdaProFileWildcard(), { "INPUT" },
177 { "INPUT" }, SCH_IO_MGR::SCH_EASYEDAPRO, PCB_IO_MGR::EASYEDAPRO );
178}
wxString GetMruPath() const
A helper class to import non Kicad project.
Definition: import_proj.h:37
wxFileName m_TargetProj
Definition: import_proj.h:58
void FindEmptyTargetDir()
Appends a new directory with the name of the project file Keep iterating until an empty directory is ...
Definition: import_proj.cpp:62
void ImportFiles(int aImportedSchFileType, int aImportedPcbFileType)
Converts imported files to kicad type files.
wxFileName m_InputFile
Definition: import_proj.h:57
void CreateNewProject(const wxFileName &aProjectFileName, bool aCreateStubFiles=true)
Creates a new project by setting up and initial project, schematic, and board files.
void OnImportEasyEdaProFiles(wxCommandEvent &event)
Open dialog to import EasyEDA Pro schematic and board files.
void OnImportEasyEdaFiles(wxCommandEvent &event)
Open dialog to import EasyEDA Std schematic and board files.
void OnImportAltiumProjectFiles(wxCommandEvent &event)
Open dialog to import Altium project files.
void OnImportEagleFiles(wxCommandEvent &event)
Open dialog to import Eagle schematic and board files.
void LoadProject(const wxFileName &aProjectFileName)
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.
void ImportNonKiCadProject(const wxString &aWindowTitle, const wxString &aFilesWildcard, const std::vector< std::string > &aSchFileExtensions, const std::vector< std::string > &aPcbFileExtensions, int aSchFileType, int aPcbFileType)
Creates a project and imports a non-KiCad Schematic and PCB.
Helper class to create more flexible dialogs, including 'do not show again' checkbox handling.
Definition: kidialog.h:43
void DoNotShowCheckbox(wxString file, int line)
Shows the 'do not show again' checkbox.
Definition: kidialog.cpp:51
int ShowModal() override
Definition: kidialog.cpp:95
@ ALTIUM_DESIGNER
Definition: pcb_io_mgr.h:62
@ CADSTAR_PCB_ARCHIVE
Definition: pcb_io_mgr.h:63
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition: confirm.cpp:194
This file is part of the common library.
#define _(s)
static const std::string ProjectFileExtension
static wxString EasyEdaArchiveWildcard()
static wxString CadstarArchiveFilesWildcard()
static wxString EasyEdaProFileWildcard()
static wxString AltiumProjectFilesWildcard()
static wxString EagleFilesWildcard()
This file is part of the common library.
Definition of file extensions used in Kicad.