KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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>
37#include <richio.h>
39#include <string_utils.h>
44#include <wx/ffile.h>
45
46using json = nlohmann::json;
47
48
49namespace GERBV {
50
51static struct IFACE : public KIFACE_BASE
52{
53 // Of course all are virtual overloads, implementations of the KIFACE.
55 IFACE( const char* aName, KIWAY::FACE_T aType ) :
56 KIFACE_BASE( aName, aType )
57 {}
58
59 bool OnKifaceStart( PGM_BASE* aProgram, int aCtlBits, KIWAY* aKiway ) override;
60
61 void OnKifaceEnd() override;
62
63 wxWindow* CreateKiWindow( wxWindow* aParent, int aClassId, KIWAY* aKiway,
64 int aCtlBits = 0 ) override
65 {
66 switch( aClassId )
67 {
68 case FRAME_GERBER:
69 return new GERBVIEW_FRAME( aKiway, aParent );
70
72 return new PANEL_GERBVIEW_DISPLAY_OPTIONS( aParent );
73
75 return new PANEL_GERBVIEW_EXCELLON_SETTINGS( aParent );
76
78 return new PANEL_GERBVIEW_COLOR_SETTINGS( aParent );
79
80 default:
81 ;
82 }
83
84 return nullptr;
85 }
86
97 void* IfaceOrAddress( int aDataId ) override
98 {
99 return nullptr;
100 }
101
107 void SaveFileAs( const wxString& aProjectBasePath, const wxString& aProjectName,
108 const wxString& aNewProjectBasePath, const wxString& aNewProjectName,
109 const wxString& aSrcFilePath, wxString& aErrors ) override;
110
111} kiface( "gerbview", KIWAY::FACE_GERBVIEW );
112
113} // namespace
114
115
116using namespace GERBV;
117
118
120
121
123
124
125// KIFACE_GETTER's actual spelling is a substitution macro found in kiway.h.
126// KIFACE_GETTER will not have name mangling due to declaration in kiway.h.
127KIFACE_API KIFACE* KIFACE_GETTER( int* aKIFACEversion, int aKiwayVersion, PGM_BASE* aProgram )
128{
129 process = aProgram;
130 return &kiface;
131}
132
133
135{
136 wxASSERT( process ); // KIFACE_GETTER has already been called.
137 return *process;
138}
139
140
141// Similar to PGM_BASE& Pgm(), but return nullptr when a *.ki_face is run from a python script.
143{
144 return process;
145}
146
147
148bool IFACE::OnKifaceStart( PGM_BASE* aProgram, int aCtlBits, KIWAY* aKiway )
149{
152 start_common( aCtlBits );
153 return true;
154}
155
156
158{
159 end_common();
160}
161
162
163void IFACE::SaveFileAs( const wxString& aProjectBasePath, const wxString& aProjectName,
164 const wxString& aNewProjectBasePath, const wxString& aNewProjectName,
165 const wxString& aSrcFilePath, wxString& aErrors )
166{
167 wxFileName destFile( aSrcFilePath );
168 wxString destPath = destFile.GetPathWithSep();
169 wxUniChar pathSep = wxFileName::GetPathSeparator();
170 wxString ext = destFile.GetExt();
171
172 if( destPath.StartsWith( aProjectBasePath + pathSep ) )
173 {
174 destPath.Replace( aProjectBasePath, aNewProjectBasePath, false );
175 destFile.SetPath( destPath );
176 }
177
179 {
180 wxString destFileName = destFile.GetName();
181
182 if( destFileName.StartsWith( aProjectName + "-" ) )
183 {
184 destFileName.Replace( aProjectName, aNewProjectName, false );
185 destFile.SetName( destFileName );
186 }
187
188 KiCopyFile( aSrcFilePath, destFile.GetFullPath(), aErrors );
189 }
190 else if( ext == FILEEXT::GerberJobFileExtension )
191 {
192 if( destFile.GetName() == aProjectName + wxT( "-job" ) )
193 destFile.SetName( aNewProjectName + wxT( "-job" ) );
194
195 FILE_LINE_READER jobfileReader( aSrcFilePath );
196
197 char* line;
198 wxString data;
199
200 while( ( line = jobfileReader.ReadLine() ) != nullptr )
201 data << line << '\n';
202
203 // detect the file format: old (deprecated) gerber format or official JSON format
204 if( !data.Contains( wxT( "{" ) ) )
205 {
206 KiCopyFile( aSrcFilePath, destFile.GetFullPath(), aErrors );
207 return;
208 }
209
210 bool success = false;
211
212 try
213 {
214 // Will throw on parse error
215 json js = json::parse( TO_UTF8( data ) );
216
217 for( auto& entry : js["FilesAttributes"] )
218 {
219 wxString path = wxString( entry["Path"].get<std::string>() );
220
221 if( path.StartsWith( aProjectName + wxT( "-" ) ) )
222 {
223 path.Replace( aProjectName, aNewProjectName, false );
224 entry["Path"] = path.ToStdString();
225 }
226 }
227
228 wxFFile destJobFile( destFile.GetFullPath(), wxT( "wb" ) );
229
230 if( destJobFile.IsOpened() )
231 success = destJobFile.Write( js.dump( 0 ) );
232
233 // wxFFile dtor will close the file
234 }
235 catch( ... )
236 {
237 success = false;
238 }
239
240 if( !success )
241 {
242 wxString msg;
243
244 if( !aErrors.empty() )
245 aErrors += wxT( "\n" );
246
247 msg.Printf( _( "Cannot copy file '%s'." ), destFile.GetFullPath() );
248 aErrors += msg;
249 }
250 }
251 else if( ext == FILEEXT::DrillFileExtension )
252 {
253 wxString destFileName = destFile.GetName();
254
255 if( destFileName == aProjectName )
256 destFileName = aNewProjectName;
257 else if( destFileName.StartsWith( aProjectName + wxT( "-" ) ) )
258 destFileName.Replace( aProjectName, aNewProjectName, false );
259
260 destFile.SetName( destFileName );
261
262 KiCopyFile( aSrcFilePath, destFile.GetFullPath(), aErrors );
263 }
264 else
265 {
266 wxFAIL_MSG( wxT( "Unexpected filetype for GerbView::SaveFileAs()" ) );
267 }
268}
269
A LINE_READER that reads from an open file.
Definition: richio.h:185
char * ReadLine() override
Read a line of text into the buffer and increments the line number counter.
Definition: richio.cpp:244
A KIFACE implementation.
Definition: kiface_base.h:39
void InitSettings(APP_SETTINGS_BASE *aSettings)
Definition: kiface_base.h:97
void end_common()
Common things to do for a top program module, during OnKifaceEnd();.
Definition: kiface_base.cpp:42
APP_SETTINGS_BASE * KifaceSettings() const
Definition: kiface_base.h:95
bool start_common(int aCtlBits)
Common things to do for a top program module, during OnKifaceStart().
Definition: kiface_base.cpp:32
A minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the same KiCad...
Definition: kiway.h:279
FACE_T
Known KIFACE implementations.
Definition: kiway.h:285
@ FACE_GERBVIEW
Definition: kiway.h:289
Container for data for KiCad programs.
Definition: pgm_base.h:96
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition: pgm_base.h:136
T * RegisterSettings(T *aSettings, bool aLoadNow=true)
Takes ownership of the pointer passed in.
#define _(s)
@ PANEL_GBR_DISPLAY_OPTIONS
Definition: frame_type.h:102
@ FRAME_GERBER
Definition: frame_type.h:57
@ PANEL_GBR_COLORS
Definition: frame_type.h:105
@ PANEL_GBR_EXCELLON_OPTIONS
Definition: frame_type.h:104
static PGM_BASE * process
Definition: gerbview.cpp:119
nlohmann::json json
Definition: gerbview.cpp:46
PGM_BASE & Pgm()
The global Program "get" accessor.
Definition: gerbview.cpp:134
KIFACE_BASE & Kiface()
Global KIFACE_BASE "get" accessor.
Definition: gerbview.cpp:122
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:142
void KiCopyFile(const wxString &aSrcPath, const wxString &aDestPath, wxString &aErrors)
Definition: gestfich.cpp:297
static const std::string GerberJobFileExtension
static const std::string DrillFileExtension
static bool IsGerberFileExtension(const wxString &ext)
#define KIFACE_API
Definition: import_export.h:51
#define KIFACE_GETTER
Definition: kiway.h:111
This file contains miscellaneous commonly used macros and functions.
GERBV::IFACE KIFACE_BASE kiface("gerbview", KIWAY::FACE_GERBVIEW)
see class PGM_BASE
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: string_utils.h:391
bool OnKifaceStart(PGM_BASE *aProgram, int aCtlBits, KIWAY *aKiway) override
Typically start_common() is called from here.
Definition: gerbview.cpp:148
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:163
wxWindow * CreateKiWindow(wxWindow *aParent, int aClassId, KIWAY *aKiway, int aCtlBits=0) override
Create a wxWindow for the current project.
Definition: gerbview.cpp:63
void * IfaceOrAddress(int aDataId) override
Return a pointer to the requested object.
Definition: gerbview.cpp:97
void OnKifaceEnd() override
Called just once just before the DSO is to be unloaded.
Definition: gerbview.cpp:157
IFACE(const char *aName, KIWAY::FACE_T aType)
Definition: gerbview.cpp:55
bool OnKifaceStart(PGM_BASE *aProgram, int aCtlBits, KIWAY *aKiway) 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.