KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 <[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
30
31#include <wx/dir.h>
32#include <wx/filedlg.h>
33#include <wx/dirdlg.h>
34
35#include <confirm.h>
36#include <kidialog.h>
37#include <kiplatform/ui.h>
39
40#include <sch_io/sch_io_mgr.h>
41#include <pcb_io/pcb_io_mgr.h>
42
43#include "kicad_manager_frame.h"
44#include <import_proj.h>
45
46
47void KICAD_MANAGER_FRAME::ImportNonKiCadProject( const wxString& aWindowTitle,
48 const wxString& aFilesWildcard,
49 const std::vector<std::string>& aSchFileExtensions,
50 const std::vector<std::string>& aPcbFileExtensions,
51 int aSchFileType, int aPcbFileType )
52{
53 wxString msg;
54 wxString default_dir = GetMruPath();
55 int style = wxFD_OPEN | wxFD_FILE_MUST_EXIST;
56
57 wxFileDialog inputdlg( this, aWindowTitle, default_dir, wxEmptyString, aFilesWildcard, style );
58
60
61 if( inputdlg.ShowModal() == wxID_CANCEL )
62 return;
63
64 // OK, we got a new project to open. Time to close any existing project before we go on
65 // to collect info about where to put the new one, etc. Otherwise the workflow is kind of
66 // disjoint.
67 if( !CloseProject( true ) )
68 return;
69
70 std::vector<wxString> schFileExts( aSchFileExtensions.begin(), aSchFileExtensions.end() );
71 std::vector<wxString> pcbFileExts( aPcbFileExtensions.begin(), aPcbFileExtensions.end() );
72
73 IMPORT_PROJ_HELPER importProj( this, schFileExts, pcbFileExts );
74 importProj.m_InputFile = inputdlg.GetPath();
75
76 // Don't use wxFileDialog here. On GTK builds, the default path is returned unless a
77 // file is actually selected.
78 wxDirDialog prodlg( this, _( "KiCad Project Destination" ), importProj.m_InputFile.GetPath(),
79 wxDD_DEFAULT_STYLE );
80
81 if( prodlg.ShowModal() == wxID_CANCEL )
82 return;
83
84 wxString targetDir = prodlg.GetPath();
85
86 importProj.m_TargetProj.SetPath( targetDir );
87 importProj.m_TargetProj.SetName( importProj.m_InputFile.GetName() );
89 importProj.m_TargetProj.MakeAbsolute();
90
91 // Check if the project directory exists and is empty
92 if( !importProj.m_TargetProj.DirExists() )
93 {
94 if( !importProj.m_TargetProj.Mkdir() )
95 {
96 msg.Printf( _( "Folder '%s' could not be created.\n\n"
97 "Make sure you have write permissions and try again." ),
98 importProj.m_TargetProj.GetPath() );
99 DisplayErrorMessage( this, msg );
100 return;
101 }
102 }
103 else
104 {
105 wxDir targetDirTest( targetDir );
106 if( targetDirTest.IsOpened() && targetDirTest.HasFiles() )
107 {
108 msg = _( "The selected directory is not empty. We recommend you "
109 "create projects in their own clean directory.\n\nDo you "
110 "want to create a new empty directory for the project?" );
111
112 KIDIALOG dlg( this, msg, _( "Confirmation" ), wxYES_NO | wxICON_WARNING );
113 dlg.DoNotShowCheckbox( __FILE__, __LINE__ );
114
115 if( dlg.ShowModal() == wxID_YES )
116 {
117 // Append a new directory with the same name of the project file
118 // Keep iterating until we find an empty directory
119 importProj.FindEmptyTargetDir();
120
121 if( !wxMkdir( importProj.m_TargetProj.GetPath() ) )
122 {
123 msg = _( "Error creating new directory. Please try a different path. The "
124 "project cannot be imported." );
125
126 KICAD_MESSAGE_DIALOG dirErrorDlg( this, msg, _( "Error" ),
127 wxOK_DEFAULT | wxICON_ERROR );
128 dirErrorDlg.ShowModal();
129 return;
130 }
131 }
132 }
133
134 targetDirTest.Close();
135 }
136
137 CreateNewProject( importProj.m_TargetProj.GetFullPath(), false /* Don't create stub files */ );
138 LoadProject( importProj.m_TargetProj );
139
140 importProj.ImportFiles( aSchFileType, aPcbFileType );
141
143 m_active_project = true;
144}
145
146
148{
149 ImportNonKiCadProject( _( "Import Altium Project Files" ),
150 FILEEXT::AltiumProjectFilesWildcard(), { "SchDoc" }, { "PcbDoc" },
151 SCH_IO_MGR::SCH_ALTIUM, PCB_IO_MGR::ALTIUM_DESIGNER );
152}
153
154
156{
157 ImportNonKiCadProject( _( "Import CADSTAR Archive Project Files" ),
158 FILEEXT::CadstarArchiveFilesWildcard(), { "csa" }, { "cpa" },
159 SCH_IO_MGR::SCH_CADSTAR_ARCHIVE, PCB_IO_MGR::CADSTAR_PCB_ARCHIVE );
160}
161
162
163void KICAD_MANAGER_FRAME::OnImportEagleFiles( wxCommandEvent& event )
164{
165 ImportNonKiCadProject( _( "Import Eagle Project Files" ), FILEEXT::EagleFilesWildcard(), { "sch" },
166 { "brd" }, SCH_IO_MGR::SCH_EAGLE, PCB_IO_MGR::EAGLE );
167}
168
169
170void KICAD_MANAGER_FRAME::OnImportEasyEdaFiles( wxCommandEvent& event )
171{
172 ImportNonKiCadProject( _( "Import EasyEDA Std Backup" ), FILEEXT::EasyEdaArchiveWildcard(), { "INPUT" },
173 { "INPUT" }, SCH_IO_MGR::SCH_EASYEDA, PCB_IO_MGR::EASYEDA );
174}
175
176
178{
179 ImportNonKiCadProject( _( "Import EasyEDA Pro Project" ), FILEEXT::EasyEdaProFileWildcard(), { "INPUT" },
180 { "INPUT" }, SCH_IO_MGR::SCH_EASYEDAPRO, PCB_IO_MGR::EASYEDAPRO );
181}
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 ...
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:42
void DoNotShowCheckbox(wxString file, int line)
Shows the 'do not show again' checkbox.
Definition kidialog.cpp:55
int ShowModal() override
Definition kidialog.cpp:93
@ 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:202
This file is part of the common library.
#define KICAD_MESSAGE_DIALOG
Definition confirm.h:52
#define _(s)
static const std::string ProjectFileExtension
static wxString EasyEdaArchiveWildcard()
static wxString CadstarArchiveFilesWildcard()
static wxString EasyEdaProFileWildcard()
static wxString AltiumProjectFilesWildcard()
static wxString EagleFilesWildcard()
void AllowNetworkFileSystems(wxDialog *aDialog)
Configure a file dialog to show network and virtual file systems.
Definition wxgtk/ui.cpp:717
Definition of file extensions used in Kicad.