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 (C) 2017-2023 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 <sch_io/sch_io_mgr.h>
39#include <pcb_io/pcb_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 std::vector<std::string>& aSchFileExtensions,
48 const std::vector<std::string>& aPcbFileExtensions,
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 inputdlg( this, aWindowTitle, default_dir, wxEmptyString, aFilesWildcard, style );
56
57 if( inputdlg.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 std::vector<wxString> schFileExts( aSchFileExtensions.begin(), aSchFileExtensions.end() );
67 std::vector<wxString> pcbFileExts( aPcbFileExtensions.begin(), aPcbFileExtensions.end() );
68
69 IMPORT_PROJ_HELPER importProj( this, schFileExts, pcbFileExts );
70 importProj.m_InputFile = inputdlg.GetPath();
71
72 // Don't use wxFileDialog here. On GTK builds, the default path is returned unless a
73 // file is actually selected.
74 wxDirDialog prodlg( this, _( "KiCad Project Destination" ), importProj.m_InputFile.GetPath(),
75 wxDD_DEFAULT_STYLE );
76
77 if( prodlg.ShowModal() == wxID_CANCEL )
78 return;
79
80 wxString targetDir = prodlg.GetPath();
81
82 importProj.m_TargetProj.SetPath( targetDir );
83 importProj.m_TargetProj.SetName( importProj.m_InputFile.GetName() );
85 importProj.m_TargetProj.MakeAbsolute();
86
87 // Check if the project directory exists and is empty
88 if( !importProj.m_TargetProj.DirExists() )
89 {
90 if( !importProj.m_TargetProj.Mkdir() )
91 {
92 msg.Printf( _( "Folder '%s' could not be created.\n\n"
93 "Make sure you have write permissions and try again." ),
94 importProj.m_TargetProj.GetPath() );
95 DisplayErrorMessage( this, msg );
96 return;
97 }
98 }
99 else
100 {
101 wxDir targetDirTest( targetDir );
102 if( targetDirTest.IsOpened() && targetDirTest.HasFiles() )
103 {
104 msg = _( "The selected directory is not empty. We recommend you "
105 "create projects in their own clean directory.\n\nDo you "
106 "want to create a new empty directory for the project?" );
107
108 KIDIALOG dlg( this, msg, _( "Confirmation" ), wxYES_NO | wxICON_WARNING );
109 dlg.DoNotShowCheckbox( __FILE__, __LINE__ );
110
111 if( dlg.ShowModal() == wxID_YES )
112 {
113 // Append a new directory with the same name of the project file
114 // Keep iterating until we find an empty directory
115 importProj.FindEmptyTargetDir();
116
117 if( !wxMkdir( importProj.m_TargetProj.GetPath() ) )
118 {
119 msg = _( "Error creating new directory. Please try a different path. The "
120 "project cannot be imported." );
121
122 wxMessageDialog dirErrorDlg( this, msg, _( "Error" ),
123 wxOK_DEFAULT | wxICON_ERROR );
124 dirErrorDlg.ShowModal();
125 return;
126 }
127 }
128 }
129
130 targetDirTest.Close();
131 }
132
133 CreateNewProject( importProj.m_TargetProj.GetFullPath(), false /* Don't create stub files */ );
134 LoadProject( importProj.m_TargetProj );
135
136 importProj.ImportFiles( aSchFileType, aPcbFileType );
137
139 m_active_project = true;
140}
141
142
144{
145 ImportNonKiCadProject( _( "Import CADSTAR Archive Project Files" ),
146 FILEEXT::CadstarArchiveFilesWildcard(), { "csa" }, { "cpa" },
147 SCH_IO_MGR::SCH_CADSTAR_ARCHIVE, PCB_IO_MGR::CADSTAR_PCB_ARCHIVE );
148}
149
150
151void KICAD_MANAGER_FRAME::OnImportEagleFiles( wxCommandEvent& event )
152{
153 ImportNonKiCadProject( _( "Import Eagle Project Files" ), FILEEXT::EagleFilesWildcard(), { "sch" },
154 { "brd" }, SCH_IO_MGR::SCH_EAGLE, PCB_IO_MGR::EAGLE );
155}
156
157
158void KICAD_MANAGER_FRAME::OnImportEasyEdaFiles( wxCommandEvent& event )
159{
160 ImportNonKiCadProject( _( "Import EasyEDA Std Backup" ), FILEEXT::EasyEdaArchiveWildcard(), { "INPUT" },
161 { "INPUT" }, SCH_IO_MGR::SCH_EASYEDA, PCB_IO_MGR::EASYEDA );
162}
163
164
166{
167 ImportNonKiCadProject( _( "Import EasyEDA Pro Project" ), FILEEXT::EasyEdaProFileWildcard(), { "INPUT" },
168 { "INPUT" }, SCH_IO_MGR::SCH_EASYEDAPRO, PCB_IO_MGR::EASYEDAPRO );
169}
wxString GetMruPath() const
A helper class to import non Kicad project.
Definition: import_proj.h:14
wxFileName m_TargetProj
Definition: import_proj.h:35
void FindEmptyTargetDir()
Appends a new directory with the name of the project file Keep iterating until an empty directory is ...
Definition: import_proj.cpp:53
void ImportFiles(int aImportedSchFileType, int aImportedPcbFileType)
Converts imported files to kicad type files.
wxFileName m_InputFile
Definition: import_proj.h:34
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 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: confirm.h:47
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
@ 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:305
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 EagleFilesWildcard()
Definition of file extensions used in Kicad.