KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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-2023 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 <kicad_manager_frame.h>
23#include <kiway.h>
24#include <kiway_player.h>
26#include <macros.h>
27#include <string_utils.h>
28#include <richio.h>
29
30#include <wx/msgdlg.h>
31
32
34 const std::vector<wxString>& aSchFileExtensions,
35 const std::vector<wxString>& aPcbFileExtensions ) :
36 m_frame( aFrame ),
37 m_schExtenstions( aSchFileExtensions ), m_pcbExtenstions( aPcbFileExtensions )
38{
39}
40
41
43{
44 // Append a new directory with the same name of the project file
45 // Keep iterating until we find an empty directory
46 wxString newDir = m_TargetProj.GetName();
47 int attempt = 0;
48
49 m_TargetProj.AppendDir( newDir );
50
51 while( m_TargetProj.DirExists() )
52 {
53 m_TargetProj.RemoveLastDir();
54 wxString suffix = wxString::Format( "_%d", ++attempt );
55 m_TargetProj.AppendDir( newDir + suffix );
56 }
57}
58
59
60void IMPORT_PROJ_HELPER::OutputCopyError( const wxFileName& aSrc, const wxFileName& aFileCopy )
61{
62 wxString msg;
63 msg.Printf( _( "Cannot copy file '%s'\n"
64 "to '%s'\n"
65 "The project cannot be imported." ),
66 aSrc.GetFullPath(), aFileCopy.GetFullPath() );
67
68 wxMessageDialog fileCopyErrorDlg( m_frame, msg, _( "Error" ), wxOK_DEFAULT | wxICON_ERROR );
69 fileCopyErrorDlg.ShowModal();
70}
71
72
74{
75 wxString m_file;
76
77public:
78 SCOPED_FILE_REMOVER( const wxString& aFile ) : m_file( aFile ) {}
79 ~SCOPED_FILE_REMOVER() { wxRemoveFile( m_file ); }
80};
81
82
83void IMPORT_PROJ_HELPER::ImportIndividualFile( KICAD_T aFT, int aImportedFileType )
84{
85 FRAME_T frame_type;
86 wxString appImportFile;
87 std::vector<wxString> neededExts;
88
89 switch( aFT )
90 {
91 case SCHEMATIC_T:
92 neededExts = m_schExtenstions;
93 frame_type = FRAME_SCH;
94 break;
95
96 case PCB_T:
97 neededExts = m_pcbExtenstions;
98 frame_type = FRAME_PCB_EDITOR;
99 break;
100
101 default: return;
102 }
103
104 std::vector<SCOPED_FILE_REMOVER> copiedFiles;
105
106 for( wxString ext : neededExts )
107 {
108 if( ext == wxS( "INPUT" ) )
109 ext = m_InputFile.GetExt();
110
111 wxFileName candidate = m_InputFile;
112 candidate.SetExt( ext );
113
114 if( !candidate.FileExists() )
115 continue;
116
117 wxFileName targetFile( m_TargetProj.GetPath(), candidate.GetName(), candidate.GetExt() );
118
119 if( !targetFile.FileExists() )
120 {
121 bool copied = wxCopyFile( candidate.GetFullPath(), targetFile.GetFullPath(), false );
122
123 if( copied )
124 {
125 // Will be auto-removed
126 copiedFiles.emplace_back( targetFile.GetFullPath() );
127 }
128 }
129
130 // Pick the first file to pass to application
131 if( appImportFile.empty() && targetFile.FileExists() )
132 appImportFile = targetFile.GetFullPath();
133 }
134
135 if( appImportFile.empty() )
136 return;
137
138 KIWAY_PLAYER* frame = m_frame->Kiway().Player( frame_type, true );
139
140 std::string packet = StrPrintf( "%d\n%s", aImportedFileType, TO_UTF8( appImportFile ) );
141 frame->Kiway().ExpressMail( frame_type, MAIL_IMPORT_FILE, packet, m_frame );
142
143 if( !frame->IsShown() )
144 frame->Show( true );
145
146 // On Windows, Raise() does not bring the window on screen, when iconized
147 if( frame->IsIconized() )
148 frame->Iconize( false );
149
150 frame->Raise();
151}
152
153
154void IMPORT_PROJ_HELPER::ImportFiles( int aImportedSchFileType, int aImportedPcbFileType )
155{
156 ImportIndividualFile( SCHEMATIC_T, aImportedSchFileType );
157 ImportIndividualFile( PCB_T, aImportedPcbFileType );
158}
wxFileName m_TargetProj
Definition: import_proj.h:34
IMPORT_PROJ_HELPER(KICAD_MANAGER_FRAME *aframe, const std::vector< wxString > &aSchFileExtensions, const std::vector< wxString > &aPcbFileExtensions)
Definition: import_proj.cpp:33
void FindEmptyTargetDir()
Appends a new directory with the name of the project file Keep iterating until an empty directory is ...
Definition: import_proj.cpp:42
std::vector< wxString > m_pcbExtenstions
Definition: import_proj.h:43
KICAD_MANAGER_FRAME * m_frame
Definition: import_proj.h:37
void ImportFiles(int aImportedSchFileType, int aImportedPcbFileType)
Converts imported files to kicad type files.
void OutputCopyError(const wxFileName &aSrc, const wxFileName &aFileCopy)
Definition: import_proj.cpp:60
void ImportIndividualFile(KICAD_T aKicad_T, int aImportedFileType)
Definition: import_proj.cpp:83
wxFileName m_InputFile
Definition: import_proj.h:33
std::vector< wxString > m_schExtenstions
Definition: import_proj.h:42
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:432
virtual void ExpressMail(FRAME_T aDestination, MAIL_T aCommand, std::string &aPayload, wxWindow *aSource=nullptr)
Send aPayload to aDestination from aSource.
Definition: kiway.cpp:553
SCOPED_FILE_REMOVER(const wxString &aFile)
Definition: import_proj.cpp:78
#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
This file contains miscellaneous commonly used macros and functions.
@ MAIL_IMPORT_FILE
Definition: mail_type.h:48
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:85
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: string_utils.h:378
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:187
Definition of file extensions used in Kicad.