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, see <https://www.gnu.org/licenses/>.
20 */
21
26
27#include <wx/dir.h>
28#include <wx/filedlg.h>
29#include <wx/dirdlg.h>
30
31#include <confirm.h>
32#include <kidialog.h>
33#include <kiplatform/ui.h>
35#include <io/pads/pads_common.h>
36
37#include <sch_io/sch_io_mgr.h>
38#include <pcb_io/pcb_io_mgr.h>
39
40#include "kicad_manager_frame.h"
41#include <import_proj.h>
42
43
44void KICAD_MANAGER_FRAME::ImportNonKiCadProject( const wxString& aWindowTitle,
45 const wxString& aFilesWildcard,
46 const std::vector<std::string>& aSchFileExtensions,
47 const std::vector<std::string>& aPcbFileExtensions,
48 int aSchFileType, int aPcbFileType )
49{
50 wxString msg;
51 wxString default_dir = GetMruPath();
52 int style = wxFD_OPEN | wxFD_FILE_MUST_EXIST;
53
54 wxFileDialog inputdlg( this, aWindowTitle, default_dir, wxEmptyString, aFilesWildcard, style );
55
57
58 if( inputdlg.ShowModal() == wxID_CANCEL )
59 return;
60
61 wxString inputPath = inputdlg.GetPath();
62 bool isPadsProject = ( aSchFileType == SCH_IO_MGR::SCH_PADS
63 && aPcbFileType == PCB_IO_MGR::PADS );
64
65 if( isPadsProject
67 {
69 _( "The selected file does not appear to be a PADS ASCII schematic or PCB." ) );
70 return;
71 }
72
73 // OK, we got a new project to open. Time to close any existing project before we go on
74 // to collect info about where to put the new one, etc. Otherwise the workflow is kind of
75 // disjoint.
76 if( !CloseProject( true ) )
77 return;
78
79 std::vector<wxString> schFileExts( aSchFileExtensions.begin(), aSchFileExtensions.end() );
80 std::vector<wxString> pcbFileExts( aPcbFileExtensions.begin(), aPcbFileExtensions.end() );
81
82 IMPORT_PROJ_HELPER importProj( this, schFileExts, pcbFileExts );
83 importProj.m_InputFile = inputPath;
84
85 // Loop to allow the user to retry directory selection when cancelling the "not empty" warning
86 for( ;; )
87 {
88 // Don't use wxFileDialog here. On GTK builds, the default path is returned unless a
89 // file is actually selected.
90 wxDirDialog prodlg( this, _( "KiCad Project Destination" ),
91 importProj.m_InputFile.GetPath(), wxDD_DEFAULT_STYLE );
92
93 if( prodlg.ShowModal() == wxID_CANCEL )
94 return;
95
96 wxString targetDir = prodlg.GetPath();
97
98 importProj.m_TargetProj.SetPath( targetDir );
99 importProj.m_TargetProj.SetName( importProj.m_InputFile.GetName() );
100 importProj.m_TargetProj.SetExt( FILEEXT::ProjectFileExtension );
101 importProj.m_TargetProj.MakeAbsolute();
102
103 if( !importProj.m_TargetProj.DirExists() )
104 {
105 if( !importProj.m_TargetProj.Mkdir() )
106 {
107 msg.Printf( _( "Folder '%s' could not be created.\n\n"
108 "Make sure you have write permissions and try again." ),
109 importProj.m_TargetProj.GetPath() );
110 DisplayErrorMessage( this, msg );
111 continue;
112 }
113
114 break;
115 }
116
117 wxDir targetDirTest( targetDir );
118
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" ),
126 wxYES_NO | wxCANCEL | wxICON_WARNING );
127 dlg.DoNotShowCheckbox( __FILE__, __LINE__ );
128
129 int result = dlg.ShowModal();
130
131 if( result == wxID_YES )
132 {
133 importProj.FindEmptyTargetDir();
134
135 if( !wxMkdir( importProj.m_TargetProj.GetPath() ) )
136 {
137 msg = _( "Error creating new directory. Please try a different path. The "
138 "project cannot be imported." );
139
140 KICAD_MESSAGE_DIALOG dirErrorDlg( this, msg, _( "Error" ),
141 wxOK_DEFAULT | wxICON_ERROR );
142 dirErrorDlg.ShowModal();
143 continue;
144 }
145
146 break;
147 }
148 else if( result == wxID_NO )
149 {
150 break;
151 }
152
153 // wxID_CANCEL — go back to directory selection
154 continue;
155 }
156
157 targetDirTest.Close();
158 break;
159 }
160
161 CreateNewProject( importProj.m_TargetProj.GetFullPath(), false /* Don't create stub files */ );
162 LoadProject( importProj.m_TargetProj );
163
164 if( isPadsProject )
165 importProj.ImportPadsFiles();
166 else
167 importProj.ImportFiles( aSchFileType, aPcbFileType );
168
170 m_active_project = true;
171}
172
173
175{
176 ImportNonKiCadProject( _( "Import Altium Project Files" ),
178 { "PcbDoc", "CSPcbDoc", "CMPcbDoc", "SWPcbDoc" },
179 SCH_IO_MGR::SCH_ALTIUM, PCB_IO_MGR::ALTIUM_DESIGNER );
180}
181
182
184{
185 ImportNonKiCadProject( _( "Import CADSTAR Archive Project Files" ),
186 FILEEXT::CadstarArchiveFilesWildcard(), { "csa" }, { "cpa" },
187 SCH_IO_MGR::SCH_CADSTAR_ARCHIVE, PCB_IO_MGR::CADSTAR_PCB_ARCHIVE );
188}
189
190
191void KICAD_MANAGER_FRAME::OnImportEagleFiles( wxCommandEvent& event )
192{
193 ImportNonKiCadProject( _( "Import Eagle Project Files" ), FILEEXT::EagleFilesWildcard(), { "sch" },
194 { "brd" }, SCH_IO_MGR::SCH_EAGLE, PCB_IO_MGR::EAGLE );
195}
196
197
198void KICAD_MANAGER_FRAME::OnImportEasyEdaFiles( wxCommandEvent& event )
199{
200 ImportNonKiCadProject( _( "Import EasyEDA Std Backup" ), FILEEXT::EasyEdaArchiveWildcard(), { "INPUT" },
201 { "INPUT" }, SCH_IO_MGR::SCH_EASYEDA, PCB_IO_MGR::EASYEDA );
202}
203
204
206{
207 ImportNonKiCadProject( _( "Import EasyEDA Pro Project" ), FILEEXT::EasyEdaProFileWildcard(), { "INPUT" },
208 { "INPUT" }, SCH_IO_MGR::SCH_EASYEDAPRO, PCB_IO_MGR::EASYEDAPRO );
209}
210
211
213{
214 ImportNonKiCadProject( _( "Import PADS Project Files" ), FILEEXT::PADSProjectFilesWildcard(),
215 { "asc", "txt" }, { "asc", "txt" }, SCH_IO_MGR::SCH_PADS,
217}
218
219
220void KICAD_MANAGER_FRAME::OnImportGedaFiles( wxCommandEvent& event )
221{
222 ImportNonKiCadProject( _( "Import gEDA / Lepton EDA Project Files" ),
223 FILEEXT::GedaProjectFilesWildcard(), { "prj", "sch" }, { "pcb" },
224 SCH_IO_MGR::SCH_GEDA, PCB_IO_MGR::GEDA_PCB );
225}
226
227
229{
230 ImportNonKiCadProject( _( "Import DipTrace Project Files" ),
231 FILEEXT::DipTraceFilesWildcard(), { "dch" }, { "dip" },
232 SCH_IO_MGR::SCH_DIPTRACE, PCB_IO_MGR::DIPTRACE );
233}
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;.
void OnImportDipTraceFiles(wxCommandEvent &event)
Open dialog to import DipTrace schematic and board files.
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:38
void DoNotShowCheckbox(wxString file, int line)
Shows the 'do not show again' checkbox.
Definition kidialog.cpp:51
int ShowModal() override
Definition kidialog.cpp:89
@ GEDA_PCB
Geda PCB file formats.
Definition pcb_io_mgr.h:65
@ ALTIUM_DESIGNER
Definition pcb_io_mgr.h:59
@ CADSTAR_PCB_ARCHIVE
Definition pcb_io_mgr.h:60
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition confirm.cpp:217
This file is part of the common library.
#define KICAD_MESSAGE_DIALOG
Definition confirm.h:48
#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()
static wxString DipTraceFilesWildcard()
void AllowNetworkFileSystems(wxDialog *aDialog)
Configure a file dialog to show network and virtual file systems.
Definition wxgtk/ui.cpp:448
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.