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 // Loop to allow the user to retry directory selection when cancelling the "not empty" warning
90 for( ;; )
91 {
92 // Don't use wxFileDialog here. On GTK builds, the default path is returned unless a
93 // file is actually selected.
94 wxDirDialog prodlg( this, _( "KiCad Project Destination" ),
95 importProj.m_InputFile.GetPath(), wxDD_DEFAULT_STYLE );
96
97 if( prodlg.ShowModal() == wxID_CANCEL )
98 return;
99
100 wxString targetDir = prodlg.GetPath();
101
102 importProj.m_TargetProj.SetPath( targetDir );
103 importProj.m_TargetProj.SetName( importProj.m_InputFile.GetName() );
104 importProj.m_TargetProj.SetExt( FILEEXT::ProjectFileExtension );
105 importProj.m_TargetProj.MakeAbsolute();
106
107 if( !importProj.m_TargetProj.DirExists() )
108 {
109 if( !importProj.m_TargetProj.Mkdir() )
110 {
111 msg.Printf( _( "Folder '%s' could not be created.\n\n"
112 "Make sure you have write permissions and try again." ),
113 importProj.m_TargetProj.GetPath() );
114 DisplayErrorMessage( this, msg );
115 continue;
116 }
117
118 break;
119 }
120
121 wxDir targetDirTest( targetDir );
122
123 if( targetDirTest.IsOpened() && targetDirTest.HasFiles() )
124 {
125 msg = _( "The selected directory is not empty. We recommend you "
126 "create projects in their own clean directory.\n\nDo you "
127 "want to create a new empty directory for the project?" );
128
129 KIDIALOG dlg( this, msg, _( "Confirmation" ),
130 wxYES_NO | wxCANCEL | wxICON_WARNING );
131 dlg.DoNotShowCheckbox( __FILE__, __LINE__ );
132
133 int result = dlg.ShowModal();
134
135 if( result == wxID_YES )
136 {
137 importProj.FindEmptyTargetDir();
138
139 if( !wxMkdir( importProj.m_TargetProj.GetPath() ) )
140 {
141 msg = _( "Error creating new directory. Please try a different path. The "
142 "project cannot be imported." );
143
144 KICAD_MESSAGE_DIALOG dirErrorDlg( this, msg, _( "Error" ),
145 wxOK_DEFAULT | wxICON_ERROR );
146 dirErrorDlg.ShowModal();
147 continue;
148 }
149
150 break;
151 }
152 else if( result == wxID_NO )
153 {
154 break;
155 }
156
157 // wxID_CANCEL — go back to directory selection
158 continue;
159 }
160
161 targetDirTest.Close();
162 break;
163 }
164
165 CreateNewProject( importProj.m_TargetProj.GetFullPath(), false /* Don't create stub files */ );
166 LoadProject( importProj.m_TargetProj );
167
168 if( isPadsProject )
169 importProj.ImportPadsFiles();
170 else
171 importProj.ImportFiles( aSchFileType, aPcbFileType );
172
174 m_active_project = true;
175}
176
177
179{
180 ImportNonKiCadProject( _( "Import Altium Project Files" ),
182 { "PcbDoc", "CSPcbDoc", "CMPcbDoc", "SWPcbDoc" },
183 SCH_IO_MGR::SCH_ALTIUM, PCB_IO_MGR::ALTIUM_DESIGNER );
184}
185
186
188{
189 ImportNonKiCadProject( _( "Import CADSTAR Archive Project Files" ),
190 FILEEXT::CadstarArchiveFilesWildcard(), { "csa" }, { "cpa" },
191 SCH_IO_MGR::SCH_CADSTAR_ARCHIVE, PCB_IO_MGR::CADSTAR_PCB_ARCHIVE );
192}
193
194
195void KICAD_MANAGER_FRAME::OnImportEagleFiles( wxCommandEvent& event )
196{
197 ImportNonKiCadProject( _( "Import Eagle Project Files" ), FILEEXT::EagleFilesWildcard(), { "sch" },
198 { "brd" }, SCH_IO_MGR::SCH_EAGLE, PCB_IO_MGR::EAGLE );
199}
200
201
202void KICAD_MANAGER_FRAME::OnImportEasyEdaFiles( wxCommandEvent& event )
203{
204 ImportNonKiCadProject( _( "Import EasyEDA Std Backup" ), FILEEXT::EasyEdaArchiveWildcard(), { "INPUT" },
205 { "INPUT" }, SCH_IO_MGR::SCH_EASYEDA, PCB_IO_MGR::EASYEDA );
206}
207
208
210{
211 ImportNonKiCadProject( _( "Import EasyEDA Pro Project" ), FILEEXT::EasyEdaProFileWildcard(), { "INPUT" },
212 { "INPUT" }, SCH_IO_MGR::SCH_EASYEDAPRO, PCB_IO_MGR::EASYEDAPRO );
213}
214
215
217{
218 ImportNonKiCadProject( _( "Import PADS Project Files" ), FILEEXT::PADSProjectFilesWildcard(),
219 { "asc", "txt" }, { "asc", "txt" }, SCH_IO_MGR::SCH_PADS,
221}
222
223
224void KICAD_MANAGER_FRAME::OnImportGedaFiles( wxCommandEvent& event )
225{
226 ImportNonKiCadProject( _( "Import gEDA / Lepton EDA Project Files" ),
227 FILEEXT::GedaProjectFilesWildcard(), { "prj", "sch" }, { "pcb" },
228 SCH_IO_MGR::SCH_GEDA, PCB_IO_MGR::GEDA_PCB );
229}
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.
wxString result
Test unit parsing edge cases and error handling.
Definition of file extensions used in Kicad.