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>
45#include <wx/ffile.h>
46
47using json = nlohmann::json;
48
49
50namespace GERBV {
51
52static struct IFACE : public KIFACE_BASE, public UNITS_PROVIDER
53{
54 // Of course all are virtual overloads, implementations of the KIFACE.
55
56 IFACE( const char* aName, KIWAY::FACE_T aType ) :
57 KIFACE_BASE( aName, aType ),
59 {}
60
61 bool OnKifaceStart( PGM_BASE* aProgram, int aCtlBits, KIWAY* aKiway ) override;
62
63 void OnKifaceEnd() override;
64
65 wxWindow* CreateKiWindow( wxWindow* aParent, int aClassId, KIWAY* aKiway,
66 int aCtlBits = 0 ) override
67 {
68 switch( aClassId )
69 {
70 case FRAME_GERBER:
71 return new GERBVIEW_FRAME( aKiway, aParent );
72
74 return new PANEL_GERBVIEW_DISPLAY_OPTIONS( aParent );
75
77 return new PANEL_GERBVIEW_EXCELLON_SETTINGS( aParent );
78
79 case PANEL_GBR_GRIDS:
80 {
83 EDA_BASE_FRAME* frame = aKiway->Player( FRAME_GERBER, false );
84
85 if( frame )
86 SetUserUnits( frame->GetUserUnits() );
87
88 return new PANEL_GRID_SETTINGS( aParent, this, frame, cfg, FRAME_GERBER );
89 }
90
92 return new PANEL_GERBVIEW_COLOR_SETTINGS( aParent );
93
94 default:
95 ;
96 }
97
98 return nullptr;
99 }
100
111 void* IfaceOrAddress( int aDataId ) override
112 {
113 return nullptr;
114 }
115
121 void SaveFileAs( const wxString& aProjectBasePath, const wxString& aProjectName,
122 const wxString& aNewProjectBasePath, const wxString& aNewProjectName,
123 const wxString& aSrcFilePath, wxString& aErrors ) override;
124
125} kiface( "gerbview", KIWAY::FACE_GERBVIEW );
126
127} // namespace
128
129
130using namespace GERBV;
131
132
134
135
136// KIFACE_GETTER's actual spelling is a substitution macro found in kiway.h.
137// KIFACE_GETTER will not have name mangling due to declaration in kiway.h.
138KIFACE_API KIFACE* KIFACE_GETTER( int* aKIFACEversion, int aKiwayVersion, PGM_BASE* aProgram )
139{
140 return &kiface;
141}
142
143
144bool IFACE::OnKifaceStart( PGM_BASE* aProgram, int aCtlBits, KIWAY* aKiway )
145{
148 start_common( aCtlBits );
149 return true;
150}
151
152
154{
155 end_common();
156}
157
158
159void IFACE::SaveFileAs( const wxString& aProjectBasePath, const wxString& aProjectName,
160 const wxString& aNewProjectBasePath, const wxString& aNewProjectName,
161 const wxString& aSrcFilePath, wxString& aErrors )
162{
163 wxFileName destFile( aSrcFilePath );
164 wxString destPath = destFile.GetPathWithSep();
165 wxUniChar pathSep = wxFileName::GetPathSeparator();
166 wxString ext = destFile.GetExt();
167
168 if( destPath.StartsWith( aProjectBasePath + pathSep ) )
169 {
170 destPath.Replace( aProjectBasePath, aNewProjectBasePath, false );
171 destFile.SetPath( destPath );
172 }
173
175 {
176 wxString destFileName = destFile.GetName();
177
178 if( destFileName.StartsWith( aProjectName + "-" ) )
179 {
180 destFileName.Replace( aProjectName, aNewProjectName, false );
181 destFile.SetName( destFileName );
182 }
183
184 KiCopyFile( aSrcFilePath, destFile.GetFullPath(), aErrors );
185 }
186 else if( ext == FILEEXT::GerberJobFileExtension )
187 {
188 if( destFile.GetName() == aProjectName + wxT( "-job" ) )
189 destFile.SetName( aNewProjectName + wxT( "-job" ) );
190
191 FILE_LINE_READER jobfileReader( aSrcFilePath );
192
193 char* line;
194 wxString data;
195
196 while( ( line = jobfileReader.ReadLine() ) != nullptr )
197 data << line << '\n';
198
199 // detect the file format: old (deprecated) gerber format or official JSON format
200 if( !data.Contains( wxT( "{" ) ) )
201 {
202 KiCopyFile( aSrcFilePath, destFile.GetFullPath(), aErrors );
203 return;
204 }
205
206 bool success = false;
207
208 try
209 {
210 // Will throw on parse error
211 json js = json::parse( TO_UTF8( data ) );
212
213 for( auto& entry : js["FilesAttributes"] )
214 {
215 wxString path = wxString( entry["Path"].get<std::string>() );
216
217 if( path.StartsWith( aProjectName + wxT( "-" ) ) )
218 {
219 path.Replace( aProjectName, aNewProjectName, false );
220 entry["Path"] = path.ToStdString();
221 }
222 }
223
224 wxFFile destJobFile( destFile.GetFullPath(), wxT( "wb" ) );
225
226 if( destJobFile.IsOpened() )
227 success = destJobFile.Write( js.dump( 0 ) );
228
229 // wxFFile dtor will close the file
230 }
231 catch( ... )
232 {
233 success = false;
234 }
235
236 if( !success )
237 {
238 wxString msg;
239
240 if( !aErrors.empty() )
241 aErrors += wxT( "\n" );
242
243 msg.Printf( _( "Cannot copy file '%s'." ), destFile.GetFullPath() );
244 aErrors += msg;
245 }
246 }
247 else if( ext == FILEEXT::DrillFileExtension )
248 {
249 wxString destFileName = destFile.GetName();
250
251 if( destFileName == aProjectName )
252 destFileName = aNewProjectName;
253 else if( destFileName.StartsWith( aProjectName + wxT( "-" ) ) )
254 destFileName.Replace( aProjectName, aNewProjectName, false );
255
256 destFile.SetName( destFileName );
257
258 KiCopyFile( aSrcFilePath, destFile.GetFullPath(), aErrors );
259 }
260 else
261 {
262 wxFAIL_MSG( wxT( "Unexpected filetype for GerbView::SaveFileAs()" ) );
263 }
264}
265
constexpr EDA_IU_SCALE gerbIUScale
Definition: base_units.h:107
APP_SETTINGS_BASE is a settings class that should be derived for each standalone KiCad application.
Definition: app_settings.h:92
The base frame for deriving all KiCad main window classes.
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:284
virtual KIWAY_PLAYER * Player(FRAME_T aFrameType, bool doCreate=true, wxTopLevelWindow *aParent=nullptr)
Return the KIWAY_PLAYER* given a FRAME_T.
Definition: kiway.cpp:406
FACE_T
Known KIFACE implementations.
Definition: kiway.h:290
@ FACE_GERBVIEW
Definition: kiway.h:294
Container for data for KiCad programs.
Definition: pgm_base.h:102
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition: pgm_base.h:142
T * GetAppSettings()
Returns a handle to the a given settings by type If the settings have already been loaded,...
T * RegisterSettings(T *aSettings, bool aLoadNow=true)
Takes ownership of the pointer passed in.
void SetUserUnits(EDA_UNITS aUnits)
#define _(s)
EDA_UNITS
Definition: eda_units.h:46
@ PANEL_GBR_DISPLAY_OPTIONS
Definition: frame_type.h:103
@ PANEL_GBR_GRIDS
Definition: frame_type.h:106
@ FRAME_GERBER
Definition: frame_type.h:57
@ PANEL_GBR_COLORS
Definition: frame_type.h:107
@ PANEL_GBR_EXCELLON_OPTIONS
Definition: frame_type.h:105
nlohmann::json json
Definition: gerbview.cpp:47
KIFACE_BASE & Kiface()
Global KIFACE_BASE "get" accessor.
Definition: gerbview.cpp:133
void KiCopyFile(const wxString &aSrcPath, const wxString &aDestPath, wxString &aErrors)
Definition: gestfich.cpp:309
static const std::string GerberJobFileExtension
static const std::string DrillFileExtension
static bool IsGerberFileExtension(const wxString &ext)
#define KIFACE_API
Definition: import_export.h:61
#define KIFACE_GETTER
Definition: kiway.h:110
This file contains miscellaneous commonly used macros and functions.
GERBV::IFACE KIFACE_BASE, UNITS_PROVIDER kiface("gerbview", KIWAY::FACE_GERBVIEW)
PGM_BASE & Pgm()
The global Program "get" accessor.
Definition: pgm_base.cpp:1060
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:398
bool OnKifaceStart(PGM_BASE *aProgram, int aCtlBits, KIWAY *aKiway) override
Typically start_common() is called from here.
Definition: gerbview.cpp:144
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:159
wxWindow * CreateKiWindow(wxWindow *aParent, int aClassId, KIWAY *aKiway, int aCtlBits=0) override
Create a wxWindow for the current project.
Definition: gerbview.cpp:65
void * IfaceOrAddress(int aDataId) override
Return a pointer to the requested object.
Definition: gerbview.cpp:111
void OnKifaceEnd() override
Called just once just before the DSO is to be unloaded.
Definition: gerbview.cpp:153
IFACE(const char *aName, KIWAY::FACE_T aType)
Definition: gerbview.cpp:56
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.