KiCad PCB EDA Suite
kicad/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-2020 KiCad Developers, see AUTHORS.txt for contributors.
5  *
6  * @author Russell Oliver <roliver8143@gmail.com>
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 
32 #include <wx/filename.h>
33 #include <wx/dir.h>
34 
35 #include <common.h>
36 #include <confirm.h>
37 #include <kiway.h>
38 #include <richio.h>
40 #include <kiway_player.h>
41 #include <stdexcept>
42 #include "pgm_kicad.h"
43 
44 #include <io_mgr.h>
45 #include <sch_io_mgr.h>
46 
47 #include "kicad_manager_frame.h"
48 
49 
50 void KICAD_MANAGER_FRAME::ImportNonKiCadProject( wxString aWindowTitle, wxString aFilesWildcard,
51  wxString aSchFileExtension, wxString aPcbFileExtension, int aSchFileType, int aPcbFileType )
52 {
53  int style = wxFD_OPEN | wxFD_FILE_MUST_EXIST;
54  wxString default_dir = GetMruPath();
55 
56  ClearMsg();
57 
58  wxFileDialog schdlg( this, aWindowTitle, default_dir, wxEmptyString, aFilesWildcard, style );
59 
60  if( schdlg.ShowModal() == wxID_CANCEL )
61  return;
62 
63 
64  wxFileName sch( schdlg.GetPath() );
65 
66  sch.SetExt( aSchFileExtension );
67 
68  wxFileName pro = sch;
69 
70  pro.SetExt( ProjectFileExtension );
71 
72  wxString protitle = _( "KiCad Project Destination" );
73 
74  // Don't use wxFileDialog here. On GTK builds, the default path is returned unless a
75  // file is actually selected.
76  wxDirDialog prodlg( this, protitle, pro.GetPath(), wxDD_DEFAULT_STYLE );
77 
78  if( prodlg.ShowModal() == wxID_CANCEL )
79  return;
80 
81  pro.SetPath( prodlg.GetPath() );
82 
83  // Check if the project directory is empty
84  wxDir directory( pro.GetPath() );
85 
86  if( directory.HasFiles() )
87  {
88  wxString msg =
89  _( "The selected directory is not empty. We recommend you "
90  "create projects in their own clean directory.\n\nDo you "
91  "want to create a new empty directory for the project?" );
92 
93  KIDIALOG dlg( this, msg, _( "Confirmation" ), wxYES_NO | wxICON_WARNING );
94  dlg.DoNotShowCheckbox( __FILE__, __LINE__ );
95 
96  if( dlg.ShowModal() == wxID_YES )
97  {
98  // Append a new directory with the same name of the project file
99  // and try to create it
100  pro.AppendDir( pro.GetName() );
101 
102  if( !wxMkdir( pro.GetPath() ) )
103  // There was a problem, undo
104  pro.RemoveLastDir();
105  }
106  }
107 
108  wxFileName pcb( sch );
109  std::string packet;
110 
111  pro.SetExt( ProjectFileExtension );
112  pcb.SetExt( aPcbFileExtension ); // enforce extension
113 
114  if( !pro.IsAbsolute() )
115  pro.MakeAbsolute();
116 
117  // Close the project and make the new one
118  CloseProject( true );
119  CreateNewProject( pro.GetFullPath(), false /* Don't create stub files */ );
120  LoadProject( pro );
121 
122  if( sch.FileExists() )
123  {
124  KIWAY_PLAYER* schframe = Kiway().Player( FRAME_SCH, true );
125 
126  packet = StrPrintf( "%d\n%s", aSchFileType, TO_UTF8( sch.GetFullPath() ) );
127  schframe->Kiway().ExpressMail( FRAME_SCH, MAIL_IMPORT_FILE, packet, this );
128 
129  if( !schframe->IsShown() )
130  schframe->Show( true );
131 
132  // On Windows, Raise() does not bring the window on screen, when iconized
133  if( schframe->IsIconized() )
134  schframe->Iconize( false );
135 
136  schframe->Raise();
137  }
138 
139  if( pcb.FileExists() )
140  {
141  KIWAY_PLAYER* pcbframe = Kiway().Player( FRAME_PCB_EDITOR, true );
142 
143  if( !pcbframe->IsVisible() )
144  pcbframe->Show( true );
145 
146  packet = StrPrintf( "%d\n%s", aPcbFileType, TO_UTF8( pcb.GetFullPath() ) );
147  pcbframe->Kiway().ExpressMail( FRAME_PCB_EDITOR, MAIL_IMPORT_FILE, packet, this );
148 
149  // On Windows, Raise() does not bring the window on screen, when iconized
150  if( pcbframe->IsIconized() )
151  pcbframe->Iconize( false );
152 
153  pcbframe->Raise();
154  }
155 
156  ReCreateTreePrj();
157  m_active_project = true;
158 }
159 
160 
162 {
163  ImportNonKiCadProject( _( "Import CADSTAR Archive Project Files" ),
164  CadstarArchiveFilesWildcard(), "csa", "cpa", SCH_IO_MGR::SCH_CADSTAR_ARCHIVE,
166 }
167 
168 
169 void KICAD_MANAGER_FRAME::OnImportEagleFiles( wxCommandEvent& event )
170 {
171  ImportNonKiCadProject( _( "Import Eagle Project Files" ), EagleFilesWildcard(),
173  SCH_IO_MGR::SCH_EAGLE, IO_MGR::EAGLE );
174 }
KIWAY_PLAYER is a wxFrame capable of the OpenProjectFiles function, meaning it can load a portion of ...
Definition: kiway_player.h:61
void DoNotShowCheckbox(wxString file, int line)
Shows the 'do not show again' checkbox
Definition: confirm.cpp:54
KIWAY & Kiway() const
Function Kiway returns a reference to the KIWAY that this object has an opportunity to participate in...
Definition: kiway_holder.h:56
Helper class to create more flexible dialogs, including 'do not show again' checkbox handling.
Definition: confirm.h:44
This file is part of the common library.
const std::string ProjectFileExtension
const std::string LegacyPcbFileExtension
void ImportNonKiCadProject(wxString aWindowTitle, wxString aFilesWildcard, wxString aSchFileExtension, wxString aPcbFileExtension, int aSchFileType, int aPcbFileType)
Creates a project and imports a non-KiCad Schematic and PCB.
void OnImportEagleFiles(wxCommandEvent &event)
Open dialog to import Eagle schematic and board files.
int StrPrintf(std::string *result, const char *format,...)
Function StrPrintf is like sprintf() but the output is appended to a std::string instead of to a char...
Definition: richio.cpp:78
#define TO_UTF8(wxstring)
Macro TO_UTF8 converts a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: macros.h:95
void OnImportCadstarArchiveFiles(wxCommandEvent &event)
Open dialog to import CADSTAR Schematic and PCB Archive files.
wxString GetMruPath() const
VTBL_ENTRY KIWAY_PLAYER * Player(FRAME_T aFrameType, bool doCreate=true, wxTopLevelWindow *aParent=NULL)
Function Player returns the KIWAY_PLAYER* given a FRAME_T.
Definition: kiway.cpp:343
Definition of file extensions used in Kicad.
const std::string LegacySchematicFileExtension
void CreateNewProject(const wxFileName &aProjectFileName, bool aCreateStubFiles=true)
Creates a new project by setting up and initial project, schematic, and board files.
bool CloseProject(bool aSave)
Closes the project, and saves it if aSave is true;.
VTBL_ENTRY void ExpressMail(FRAME_T aDestination, MAIL_T aCommand, std::string &aPayload, wxWindow *aSource=NULL)
Function ExpressMail send aPayload to aDestination from aSource.
Definition: kiway.cpp:437
#define _(s)
Definition: 3d_actions.cpp:33
The common library.
int ShowModal() override
Definition: confirm.cpp:96
wxString EagleFilesWildcard()
void LoadProject(const wxFileName &aProjectFileName)
void ClearMsg()
Erase the text panel.
wxString CadstarArchiveFilesWildcard()