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#include <io/pads/pads_common.h>
40
41#include <sch_io/sch_io_mgr.h>
42#include <pcb_io/pcb_io_mgr.h>
43
44#include "kicad_manager_frame.h"
45#include <import_proj.h>
46
47
48void KICAD_MANAGER_FRAME::ImportNonKiCadProject( const wxString& aWindowTitle,
49 const wxString& aFilesWildcard,
50 const std::vector<std::string>& aSchFileExtensions,
51 const std::vector<std::string>& aPcbFileExtensions,
52 int aSchFileType, int aPcbFileType )
53{
54 wxString msg;
55 wxString default_dir = GetMruPath();
56 int style = wxFD_OPEN | wxFD_FILE_MUST_EXIST;
57
58 wxFileDialog inputdlg( this, aWindowTitle, default_dir, wxEmptyString, aFilesWildcard, style );
59
61
62 if( inputdlg.ShowModal() == wxID_CANCEL )
63 return;
64
65 wxString inputPath = inputdlg.GetPath();
66 bool isPadsProject = ( aSchFileType == SCH_IO_MGR::SCH_PADS
67 && aPcbFileType == PCB_IO_MGR::PADS );
68
69 if( isPadsProject
71 {
73 _( "The selected file does not appear to be a PADS ASCII schematic or PCB." ) );
74 return;
75 }
76
77 // OK, we got a new project to open. Time to close any existing project before we go on
78 // to collect info about where to put the new one, etc. Otherwise the workflow is kind of
79 // disjoint.
80 if( !CloseProject( true ) )
81 return;
82
83 std::vector<wxString> schFileExts( aSchFileExtensions.begin(), aSchFileExtensions.end() );
84 std::vector<wxString> pcbFileExts( aPcbFileExtensions.begin(), aPcbFileExtensions.end() );
85
86 IMPORT_PROJ_HELPER importProj( this, schFileExts, pcbFileExts );
87 importProj.m_InputFile = inputPath;
88
89 // Don't use wxFileDialog here. On GTK builds, the default path is returned unless a
90 // file is actually selected.
91 wxDirDialog prodlg( this, _( "KiCad Project Destination" ), importProj.m_InputFile.GetPath(),
92 wxDD_DEFAULT_STYLE );
93
94 if( prodlg.ShowModal() == wxID_CANCEL )
95 return;
96
97 wxString targetDir = prodlg.GetPath();
98
99 importProj.m_TargetProj.SetPath( targetDir );
100 importProj.m_TargetProj.SetName( importProj.m_InputFile.GetName() );
101 importProj.m_TargetProj.SetExt( FILEEXT::ProjectFileExtension );
102 importProj.m_TargetProj.MakeAbsolute();
103
104 // Check if the project directory exists and is empty
105 if( !importProj.m_TargetProj.DirExists() )
106 {
107 if( !importProj.m_TargetProj.Mkdir() )
108 {
109 msg.Printf( _( "Folder '%s' could not be created.\n\n"
110 "Make sure you have write permissions and try again." ),
111 importProj.m_TargetProj.GetPath() );
112 DisplayErrorMessage( this, msg );
113 return;
114 }
115 }
116 else
117 {
118 wxDir targetDirTest( targetDir );
119 if( targetDirTest.IsOpened() && targetDirTest.HasFiles() )
120 {
121 msg = _( "The selected directory is not empty. We recommend you "
122 "create projects in their own clean directory.\n\nDo you "
123 "want to create a new empty directory for the project?" );
124
125 KIDIALOG dlg( this, msg, _( "Confirmation" ), wxYES_NO | wxICON_WARNING );
126 dlg.DoNotShowCheckbox( __FILE__, __LINE__ );
127
128 if( dlg.ShowModal() == wxID_YES )
129 {
130 // Append a new directory with the same name of the project file
131 // Keep iterating until we find an empty directory
132 importProj.FindEmptyTargetDir();
133
134 if( !wxMkdir( importProj.m_TargetProj.GetPath() ) )
135 {
136 msg = _( "Error creating new directory. Please try a different path. The "
137 "project cannot be imported." );
138
139 KICAD_MESSAGE_DIALOG dirErrorDlg( this, msg, _( "Error" ),
140 wxOK_DEFAULT | wxICON_ERROR );
141 dirErrorDlg.ShowModal();
142 return;
143 }
144 }
145 }
146
147 targetDirTest.Close();
148 }
149
150 CreateNewProject( importProj.m_TargetProj.GetFullPath(), false /* Don't create stub files */ );
151 LoadProject( importProj.m_TargetProj );
152
153 if( isPadsProject )
154 importProj.ImportPadsFiles();
155 else
156 importProj.ImportFiles( aSchFileType, aPcbFileType );
157
159 m_active_project = true;
160}
161
162
164{
165 ImportNonKiCadProject( _( "Import Altium Project Files" ),
166 FILEEXT::AltiumProjectFilesWildcard(), { "SchDoc" }, { "PcbDoc" },
167 SCH_IO_MGR::SCH_ALTIUM, PCB_IO_MGR::ALTIUM_DESIGNER );
168}
169
170
172{
173 ImportNonKiCadProject( _( "Import CADSTAR Archive Project Files" ),
174 FILEEXT::CadstarArchiveFilesWildcard(), { "csa" }, { "cpa" },
175 SCH_IO_MGR::SCH_CADSTAR_ARCHIVE, PCB_IO_MGR::CADSTAR_PCB_ARCHIVE );
176}
177
178
179void KICAD_MANAGER_FRAME::OnImportEagleFiles( wxCommandEvent& event )
180{
181 ImportNonKiCadProject( _( "Import Eagle Project Files" ), FILEEXT::EagleFilesWildcard(), { "sch" },
182 { "brd" }, SCH_IO_MGR::SCH_EAGLE, PCB_IO_MGR::EAGLE );
183}
184
185
186void KICAD_MANAGER_FRAME::OnImportEasyEdaFiles( wxCommandEvent& event )
187{
188 ImportNonKiCadProject( _( "Import EasyEDA Std Backup" ), FILEEXT::EasyEdaArchiveWildcard(), { "INPUT" },
189 { "INPUT" }, SCH_IO_MGR::SCH_EASYEDA, PCB_IO_MGR::EASYEDA );
190}
191
192
194{
195 ImportNonKiCadProject( _( "Import EasyEDA Pro Project" ), FILEEXT::EasyEdaProFileWildcard(), { "INPUT" },
196 { "INPUT" }, SCH_IO_MGR::SCH_EASYEDAPRO, PCB_IO_MGR::EASYEDAPRO );
197}
198
199
201{
202 ImportNonKiCadProject( _( "Import PADS Project Files" ), FILEEXT::PADSProjectFilesWildcard(),
203 { "asc", "txt" }, { "asc", "txt" }, SCH_IO_MGR::SCH_PADS,
205}
206
207
208void KICAD_MANAGER_FRAME::OnImportGedaFiles( wxCommandEvent& event )
209{
210 ImportNonKiCadProject( _( "Import gEDA / Lepton EDA Project Files" ),
211 FILEEXT::GedaProjectFilesWildcard(), { "prj", "sch" }, { "pcb" },
212 SCH_IO_MGR::SCH_GEDA, PCB_IO_MGR::GEDA_PCB );
213}
wxString GetMruPath() const
A helper class to import non Kicad project.
Definition import_proj.h:37
wxFileName m_TargetProj
Definition import_proj.h:63
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:62
void ImportPadsFiles()
Converts PADS ASCII schematic and PCB files to KiCad type files.
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 OnImportPadsProjectFiles(wxCommandEvent &event)
Open dialog to import PADS Logic schematic and PCB 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 OnImportGedaFiles(wxCommandEvent &event)
Open dialog to import gEDA/gaf schematic and PCB files.
bool CloseProject(bool aSave)
Closes the project, and saves it if aSave is true;.
bool LoadProject(const wxFileName &aProjectFileName)
Loads a new project.
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
@ GEDA_PCB
Geda PCB file formats.
Definition pcb_io_mgr.h:69
@ ALTIUM_DESIGNER
Definition pcb_io_mgr.h:63
@ CADSTAR_PCB_ARCHIVE
Definition pcb_io_mgr.h:64
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 GedaProjectFilesWildcard()
static wxString CadstarArchiveFilesWildcard()
static wxString EasyEdaProFileWildcard()
static wxString PADSProjectFilesWildcard()
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:435
PADS_FILE_TYPE DetectPadsFileType(const wxString &aFilePath)
Detect the type of a PADS file by examining its header.
Common utilities and types for parsing PADS file formats.
Definition of file extensions used in Kicad.