KiCad PCB EDA Suite
gerbview.cpp
Go to the documentation of this file.
1
6/*
7 * This program source code file is part of KiCad, a free EDA CAD application.
8 *
9 * Copyright (C) 1992-2021 KiCad Developers, see change_log.txt for contributors.
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, you may find one here:
23 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
24 * or you may search the http://www.gnu.org website for the version 2 license,
25 * or you may write to the Free Software Foundation, Inc.,
26 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
27 */
28
29#include <gerbview.h>
30#include <gerbview_frame.h>
31#include <gerbview_settings.h>
32#include <gestfich.h>
33#include <kiface_base.h>
34#include <macros.h>
35#include <nlohmann/json.hpp>
36#include <pgm_base.h>
42#include <wx/ffile.h>
43
45
46
47namespace GERBV {
48
49static struct IFACE : public KIFACE_BASE
50{
51 // Of course all are virtual overloads, implementations of the KIFACE.
52
53 IFACE( const char* aName, KIWAY::FACE_T aType ) :
54 KIFACE_BASE( aName, aType )
55 {}
56
57 bool OnKifaceStart( PGM_BASE* aProgram, int aCtlBits ) override;
58
59 void OnKifaceEnd() override;
60
61 wxWindow* CreateKiWindow( wxWindow* aParent, int aClassId, KIWAY* aKiway,
62 int aCtlBits = 0 ) override
63 {
64 switch( aClassId )
65 {
66 case FRAME_GERBER:
67 return new GERBVIEW_FRAME( aKiway, aParent );
68
70 return new PANEL_GERBVIEW_DISPLAY_OPTIONS( aParent );
71
73 return new PANEL_GERBVIEW_EXCELLON_SETTINGS( aParent );
74
76 return new PANEL_GERBVIEW_COLOR_SETTINGS( aParent );
77
78 default:
79 ;
80 }
81
82 return nullptr;
83 }
84
95 void* IfaceOrAddress( int aDataId ) override
96 {
97 return nullptr;
98 }
99
105 void SaveFileAs( const wxString& aProjectBasePath, const wxString& aProjectName,
106 const wxString& aNewProjectBasePath, const wxString& aNewProjectName,
107 const wxString& aSrcFilePath, wxString& aErrors ) override;
108
109} kiface( "gerbview", KIWAY::FACE_GERBVIEW );
110
111} // namespace
112
113
114using namespace GERBV;
115
116
118
119
121
122
123// KIFACE_GETTER's actual spelling is a substitution macro found in kiway.h.
124// KIFACE_GETTER will not have name mangling due to declaration in kiway.h.
125MY_API( KIFACE* ) KIFACE_GETTER( int* aKIFACEversion, int aKiwayVersion, PGM_BASE* aProgram )
126{
128 return &kiface;
129}
130
131
133{
134 wxASSERT( process ); // KIFACE_GETTER has already been called.
135 return *process;
136}
137
138
139// Similar to PGM_BASE& Pgm(), but return nullptr when a *.ki_face is run from a python script.
141{
142 return process;
143}
144
145
147{
150 start_common( aCtlBits );
151 return true;
152}
153
154
156{
157 end_common();
158}
159
160
161void IFACE::SaveFileAs( const wxString& aProjectBasePath, const wxString& aProjectName,
162 const wxString& aNewProjectBasePath, const wxString& aNewProjectName,
163 const wxString& aSrcFilePath, wxString& aErrors )
164{
165 wxFileName destFile( aSrcFilePath );
166 wxString destPath = destFile.GetPathWithSep();
167 wxUniChar pathSep = wxFileName::GetPathSeparator();
168 wxString ext = destFile.GetExt();
169
170 if( destPath.StartsWith( aProjectBasePath + pathSep ) )
171 {
172 destPath.Replace( aProjectBasePath, aNewProjectBasePath, false );
173 destFile.SetPath( destPath );
174 }
175
176 if( IsGerberFileExtension( ext ) )
177 {
178 wxString destFileName = destFile.GetName();
179
180 if( destFileName.StartsWith( aProjectName + "-" ) )
181 {
182 destFileName.Replace( aProjectName, aNewProjectName, false );
183 destFile.SetName( destFileName );
184 }
185
186 KiCopyFile( aSrcFilePath, destFile.GetFullPath(), aErrors );
187 }
188 else if( ext == GerberJobFileExtension )
189 {
190 if( destFile.GetName() == aProjectName + wxT( "-job" ) )
191 destFile.SetName( aNewProjectName + wxT( "-job" ) );
192
193 FILE_LINE_READER jobfileReader( aSrcFilePath );
194
195 char* line;
196 wxString data;
197
198 while( ( line = jobfileReader.ReadLine() ) )
199 data << line << '\n';
200
201 // detect the file format: old (deprecated) gerber format or official JSON format
202 if( !data.Contains( wxT( "{" ) ) )
203 {
204 KiCopyFile( aSrcFilePath, destFile.GetFullPath(), aErrors );
205 return;
206 }
207
208 bool success = false;
209
210 try
211 {
212 // Will throw on parse error
213 json js = json::parse( TO_UTF8( data ) );
214
215 for( auto& entry : js["FilesAttributes"] )
216 {
217 wxString path = wxString( entry["Path"].get<std::string>() );
218
219 if( path.StartsWith( aProjectName + wxT( "-" ) ) )
220 {
221 path.Replace( aProjectName, aNewProjectName, false );
222 entry["Path"] = path.ToStdString();
223 }
224 }
225
226 wxFFile destJobFile( destFile.GetFullPath(), wxT( "wb" ) );
227
228 if( destJobFile.IsOpened() )
229 success = destJobFile.Write( js.dump( 0 ) );
230
231 // wxFFile dtor will close the file
232 }
233 catch( ... )
234 {
235 success = false;
236 }
237
238 if( !success )
239 {
240 wxString msg;
241
242 if( !aErrors.empty() )
243 aErrors += wxT( "\n" );
244
245 msg.Printf( _( "Cannot copy file '%s'." ), destFile.GetFullPath() );
246 aErrors += msg;
247 }
248 }
249 else if( ext == DrillFileExtension )
250 {
251 wxString destFileName = destFile.GetName();
252
253 if( destFileName == aProjectName )
254 destFileName = aNewProjectName;
255 else if( destFileName.StartsWith( aProjectName + wxT( "-" ) ) )
256 destFileName.Replace( aProjectName, aNewProjectName, false );
257
258 destFile.SetName( destFileName );
259
260 KiCopyFile( aSrcFilePath, destFile.GetFullPath(), aErrors );
261 }
262 else
263 {
264 wxFAIL_MSG( wxT( "Unexpected filetype for GerbView::SaveFileAs()" ) );
265 }
266}
267
A LINE_READER that reads from an open file.
Definition: richio.h:173
char * ReadLine() override
Read a line of text into the buffer and increments the line number counter.
Definition: richio.cpp:219
A KIFACE implementation.
Definition: kiface_base.h:39
void InitSettings(APP_SETTINGS_BASE *aSettings)
Definition: kiface_base.h:95
void end_common()
Common things to do for a top program module, during OnKifaceEnd();.
APP_SETTINGS_BASE * KifaceSettings() const
Definition: kiface_base.h:93
bool start_common(int aCtlBits)
Common things to do for a top program module, during OnKifaceStart().
Definition: kiface_base.cpp:97
A minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the same KiCad...
Definition: kiway.h:273
FACE_T
Known KIFACE implementations.
Definition: kiway.h:279
@ FACE_GERBVIEW
Definition: kiway.h:283
Container for data for KiCad programs.
Definition: pgm_base.h:94
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition: pgm_base.h:134
T * RegisterSettings(T *aSettings, bool aLoadNow=true)
Takes ownership of the pointer passed in.
#define _(s)
@ PANEL_GBR_DISPLAY_OPTIONS
Definition: frame_type.h:94
@ FRAME_GERBER
Definition: frame_type.h:53
@ PANEL_GBR_COLORS
Definition: frame_type.h:97
@ PANEL_GBR_EXCELLON_OPTIONS
Definition: frame_type.h:96
return & kiface
Definition: gerbview.cpp:128
MY_API(KIFACE *) KIFACE_GETTER(int *aKIFACEversion
int aKiwayVersion
Definition: gerbview.cpp:125
static PGM_BASE * process
Definition: gerbview.cpp:117
nlohmann::json json
Definition: gerbview.cpp:44
int PGM_BASE * aProgram
Definition: gerbview.cpp:126
PGM_BASE & Pgm()
The global Program "get" accessor.
Definition: gerbview.cpp:132
KIFACE_BASE & Kiface()
Global KIFACE_BASE "get" accessor.
Definition: gerbview.cpp:120
PGM_BASE * PgmOrNull()
similar to PGM_BASE& Pgm(), but return a reference that can be nullptr when running a shared lib from...
Definition: gerbview.cpp:140
void KiCopyFile(const wxString &aSrcPath, const wxString &aDestPath, wxString &aErrors)
Definition: gestfich.cpp:214
const std::string GerberJobFileExtension
const std::string DrillFileExtension
bool IsGerberFileExtension(const wxString &ext)
#define KIFACE_GETTER
Definition: kiway.h:111
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
GERBV::IFACE KIFACE_BASE kiface("gerbview", KIWAY::FACE_GERBVIEW)
bool parse(std::istream &aStream, bool aVerbose)
Parse a PCB or footprint file from the given input stream.
see class PGM_BASE
void SaveFileAs(const wxString &aProjectBasePath, const wxString &aProjectName, const wxString &aNewProjectBasePath, const wxString &aNewProjectName, const wxString &aSrcFilePath, wxString &aErrors) override
Saving a file under a different name is delegated to the various KIFACEs because the project doesn't ...
Definition: gerbview.cpp:161
bool OnKifaceStart(PGM_BASE *aProgram, int aCtlBits) override
Typically start_common() is called from here.
Definition: gerbview.cpp:146
wxWindow * CreateKiWindow(wxWindow *aParent, int aClassId, KIWAY *aKiway, int aCtlBits=0) override
Create a wxWindow for the current project.
Definition: gerbview.cpp:61
void * IfaceOrAddress(int aDataId) override
Return a pointer to the requested object.
Definition: gerbview.cpp:95
void OnKifaceEnd() override
Called just once just before the DSO is to be unloaded.
Definition: gerbview.cpp:155
IFACE(const char *aName, KIWAY::FACE_T aType)
Definition: gerbview.cpp:53
bool OnKifaceStart(PGM_BASE *aProgram, int aCtlBits) override
Typically start_common() is called from here.
Implement a participant in the KIWAY alchemy.
Definition: kiway.h:151
Definition of file extensions used in Kicad.