KiCad PCB EDA Suite
import_proj.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) 2019 CERN
5 * Copyright (C) 2019-2022 KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include "import_proj.h"
22#include <kiway.h>
23#include <kiway_player.h>
25#include <macros.h>
26#include <richio.h>
27
28#include <wx/msgdlg.h>
29
30
32 const wxString& aFile,
33 const wxString& aSchFileExtension,
34 const wxString& aPcbFileExtension ) :
35 m_frame( aFrame ),
36 m_sch( aFile ), m_pcb( m_sch ), m_pro( m_sch )
37{
38 m_sch.SetExt( aSchFileExtension );
39 m_pcb.SetExt( aPcbFileExtension );
41}
42
43
44const wxFileName& IMPORT_PROJ_HELPER::GetProj()
45{
46 return m_pro;
47}
48
49
51{
52 return m_pro.GetPath();
53}
54
55
56void IMPORT_PROJ_HELPER::SetProjPath( const wxString aPath )
57{
58 m_pro.SetPath( aPath );
59}
60
61
63{
64 return m_pro.GetFullPath();
65}
66
67
69{
70 return m_pro.GetName();
71}
72
73
75{
76 // Append a new directory with the same name of the project file
77 // Keep iterating until we find an empty directory
78 wxString newDir = m_pro.GetName();
79 int attempt = 0;
80
81 m_pro.AppendDir( newDir );
82
83 while( m_pro.DirExists() )
84 {
85 m_pro.RemoveLastDir();
86 wxString suffix = wxString::Format( "_%d", ++attempt );
87 m_pro.AppendDir( newDir + suffix );
88 }
89}
90
91
93{
95 if( !m_pro.IsAbsolute() )
96 m_pro.MakeAbsolute();
97}
98
99
100bool IMPORT_PROJ_HELPER::CopyImportedFile( KICAD_T aFT, bool displayError )
101{
102 wxASSERT( m_pro.GetExt() == ProjectFileExtension );
103
104 wxFileName fileCopy( m_pro );
105
106 wxFileName src, dest;
107 switch( aFT )
108 {
109 case SCHEMATIC_T: src = m_sch; break;
110
111 case PCB_T: src = m_pcb; break;
112
113 default: break;
114 }
115
116 fileCopy.SetExt( src.GetExt() );
117
118 if( src.Exists() && !fileCopy.SameAs( src ) )
119 {
120 if( !wxCopyFile( src.GetFullPath(), fileCopy.GetFullPath(), true ) )
121 {
122 if( displayError )
123 OutputCopyError( src, fileCopy );
124 return false;
125 }
126 }
127
128 switch( aFT )
129 {
130 case SCHEMATIC_T: m_shCopy = fileCopy; break;
131
132 case PCB_T: m_pcbCopy = fileCopy; break;
133
134 default: break;
135 }
136
137 return true;
138}
139
141{
142 return CopyImportedFile( SCHEMATIC_T, displayError ) && CopyImportedFile( PCB_T, displayError );
143}
144
145void IMPORT_PROJ_HELPER::OutputCopyError( const wxFileName& aSrc, const wxFileName& aFileCopy )
146{
147 wxString msg;
148 msg.Printf( _( "Cannot copy file '%s'\n"
149 "to '%s'\n"
150 "The project cannot be imported." ),
151 aSrc.GetFullPath(), aFileCopy.GetFullPath() );
152
153 wxMessageDialog fileCopyErrorDlg( m_frame, msg, _( "Error" ), wxOK_DEFAULT | wxICON_ERROR );
154 fileCopyErrorDlg.ShowModal();
155}
156
157
158void IMPORT_PROJ_HELPER::AssociateFileWithProj( KICAD_T aFT, int aImportedFileType )
159{
160 wxFileName fileCopy, importedFile;
161 FRAME_T frame_type;
162 switch( aFT )
163 {
164 case SCHEMATIC_T:
165 importedFile = m_sch;
166 fileCopy = m_shCopy;
167 frame_type = FRAME_SCH;
168 break;
169
170 case PCB_T:
171 importedFile = m_pcb;
172 fileCopy = m_pcbCopy;
173 frame_type = FRAME_PCB_EDITOR;
174 break;
175
176 default: return;
177 }
178
179 if( fileCopy.FileExists() )
180 {
181 KIWAY_PLAYER* frame = m_frame->Kiway().Player( frame_type, true );
182
183 std::string packet =
184 StrPrintf( "%d\n%s", aImportedFileType, TO_UTF8( fileCopy.GetFullPath() ) );
185 frame->Kiway().ExpressMail( frame_type, MAIL_IMPORT_FILE, packet, m_frame );
186
187 if( !frame->IsShown() )
188 frame->Show( true );
189
190 // On Windows, Raise() does not bring the window on screen, when iconized
191 if( frame->IsIconized() )
192 frame->Iconize( false );
193
194 frame->Raise();
195
196 if( !fileCopy.SameAs( importedFile ) ) // Do not delete the original file!
197 wxRemoveFile( fileCopy.GetFullPath() );
198 }
199}
200
201
202void IMPORT_PROJ_HELPER::AssociateFilesWithProj( int aImportedSchFileType,
203 int aImportedPcbFileType )
204{
205 AssociateFileWithProj( SCHEMATIC_T, aImportedSchFileType );
206 AssociateFileWithProj( PCB_T, aImportedPcbFileType );
207}
void AssociateFilesWithProj(int aImportedSchFileType, int aImportedPcbFileType)
Converts imported files to kicad type files.
wxString GetProjFullPath()
Definition: import_proj.cpp:62
wxString GetProjPath()
Definition: import_proj.cpp:50
wxFileName m_pcb
Definition: import_proj.h:48
wxFileName m_shCopy
Definition: import_proj.h:47
const wxFileName & GetProj()
Definition: import_proj.cpp:44
bool CopyImportedFile(KICAD_T aKicad_T, bool displayError=true)
void SetProjPath(const wxString aPath)
Definition: import_proj.cpp:56
IMPORT_PROJ_HELPER(KICAD_MANAGER_FRAME *aframe, const wxString &aFile, const wxString &aSchFileExtension, const wxString &aPcbFileExtension)
Definition: import_proj.cpp:31
void CreateEmptyDirForProject()
Appends a new directory with the name of the project file Keep iterating until an empty directory is ...
Definition: import_proj.cpp:74
wxFileName m_pcbCopy
Definition: import_proj.h:49
KICAD_MANAGER_FRAME * m_frame
Definition: import_proj.h:45
void SetProjAbsolutePath()
Definition: import_proj.cpp:92
void OutputCopyError(const wxFileName &aSrc, const wxFileName &aFileCopy)
void AssociateFileWithProj(KICAD_T aKicad_T, int aImportedFileType)
wxFileName m_pro
Definition: import_proj.h:50
bool CopyImportedFiles(bool displayError=true)
Copies project files to the destination directory.
wxString GetProjName()
Definition: import_proj.cpp:68
wxFileName m_sch
Definition: import_proj.h:46
The main KiCad project manager frame.
KIWAY & Kiway() const
Return a reference to the KIWAY that this object has an opportunity to participate in.
Definition: kiway_holder.h:53
A wxFrame capable of the OpenProjectFiles function, meaning it can load a portion of a KiCad project.
Definition: kiway_player.h:66
virtual KIWAY_PLAYER * Player(FRAME_T aFrameType, bool doCreate=true, wxTopLevelWindow *aParent=nullptr)
Return the KIWAY_PLAYER* given a FRAME_T.
Definition: kiway.cpp:394
virtual void ExpressMail(FRAME_T aDestination, MAIL_T aCommand, std::string &aPayload, wxWindow *aSource=nullptr)
Send aPayload to aDestination from aSource.
Definition: kiway.cpp:488
#define _(s)
FRAME_T
The set of EDA_BASE_FRAME derivatives, typically stored in EDA_BASE_FRAME::m_Ident.
Definition: frame_type.h:33
@ FRAME_PCB_EDITOR
Definition: frame_type.h:40
@ FRAME_SCH
Definition: frame_type.h:34
const std::string ProjectFileExtension
This file contains miscellaneous commonly used macros and functions.
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: macros.h:96
@ MAIL_IMPORT_FILE
Definition: mail_type.h:48
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
int StrPrintf(std::string *result, const char *format,...)
This is like sprintf() but the output is appended to a std::string instead of to a character array.
Definition: richio.cpp:84
KICAD_T
The set of class identification values stored in EDA_ITEM::m_structType.
Definition: typeinfo.h:78
@ PCB_T
Definition: typeinfo.h:82
@ SCHEMATIC_T
Definition: typeinfo.h:188
Definition of file extensions used in Kicad.