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 The KiCad Developers, see AUTHORS.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 <json_common.h>
36#include <pgm_base.h>
37#include <richio.h>
39#include <string_utils.h>
45#include <wx/ffile.h>
46
48#include <toolbars_gerber.h>
49
50using json = nlohmann::json;
51
52
53namespace GERBV {
54
55static struct IFACE : public KIFACE_BASE, public UNITS_PROVIDER
56{
57 // Of course all are virtual overloads, implementations of the KIFACE.
58
59 IFACE( const char* aName, KIWAY::FACE_T aType ) :
60 KIFACE_BASE( aName, aType ),
62 {}
63
64 bool OnKifaceStart( PGM_BASE* aProgram, int aCtlBits, KIWAY* aKiway ) override;
65
66 void OnKifaceEnd() override;
67
68 wxWindow* CreateKiWindow( wxWindow* aParent, int aClassId, KIWAY* aKiway,
69 int aCtlBits = 0 ) override
70 {
71 switch( aClassId )
72 {
74 return new GERBVIEW_FRAME( aKiway, aParent );
75
77 return new PANEL_GERBVIEW_DISPLAY_OPTIONS( aParent );
78
80 return new PANEL_GERBVIEW_EXCELLON_SETTINGS( aParent );
81
82 case PANEL_GBR_GRIDS:
83 {
84 GERBVIEW_SETTINGS* cfg = GetAppSettings<GERBVIEW_SETTINGS>( "gerbview" );
85 EDA_BASE_FRAME* frame = aKiway->Player( FRAME_GERBER, false );
86
87 if( frame )
88 SetUserUnits( frame->GetUserUnits() );
89
90 return new PANEL_GRID_SETTINGS( aParent, this, frame, cfg, FRAME_GERBER );
91 }
92
94 return new PANEL_GERBVIEW_COLOR_SETTINGS( aParent );
95
97 {
98 GERBVIEW_SETTINGS* cfg = GetAppSettings<GERBVIEW_SETTINGS>( "gerbview" );
99 TOOLBAR_SETTINGS* tb = GetToolbarSettings<GERBVIEW_TOOLBAR_SETTINGS>( "gerbview-toolbars" );
100
101 std::vector<TOOL_ACTION*> actions;
102 std::vector<ACTION_TOOLBAR_CONTROL*> controls;
103
105 actions.push_back( action );
106
108 controls.push_back( control );
109
110 return new PANEL_TOOLBAR_CUSTOMIZATION( aParent, cfg, tb, actions, controls );
111 }
112
113 default:
114 ;
115 }
116
117 return nullptr;
118 }
119
130 void* IfaceOrAddress( int aDataId ) override
131 {
132 return nullptr;
133 }
134
140 void SaveFileAs( const wxString& aProjectBasePath, const wxString& aProjectName,
141 const wxString& aNewProjectBasePath, const wxString& aNewProjectName,
142 const wxString& aSrcFilePath, wxString& aErrors ) override;
143
144} kiface( "gerbview", KIWAY::FACE_GERBVIEW );
145
146} // namespace
147
148
149using namespace GERBV;
150
151
153
154
155// KIFACE_GETTER's actual spelling is a substitution macro found in kiway.h.
156// KIFACE_GETTER will not have name mangling due to declaration in kiway.h.
157KIFACE_API KIFACE* KIFACE_GETTER( int* aKIFACEversion, int aKiwayVersion, PGM_BASE* aProgram )
158{
159 return &kiface;
160}
161
162
163bool IFACE::OnKifaceStart( PGM_BASE* aProgram, int aCtlBits, KIWAY* aKiway )
164{
167 start_common( aCtlBits );
168 return true;
169}
170
171
173{
174 end_common();
175}
176
177
178void IFACE::SaveFileAs( const wxString& aProjectBasePath, const wxString& aProjectName,
179 const wxString& aNewProjectBasePath, const wxString& aNewProjectName,
180 const wxString& aSrcFilePath, wxString& aErrors )
181{
182 wxFileName destFile( aSrcFilePath );
183 wxString destPath = destFile.GetPathWithSep();
184 wxUniChar pathSep = wxFileName::GetPathSeparator();
185 wxString ext = destFile.GetExt();
186
187 if( destPath.StartsWith( aProjectBasePath + pathSep ) )
188 {
189 destPath.Replace( aProjectBasePath, aNewProjectBasePath, false );
190 destFile.SetPath( destPath );
191 }
192
194 {
195 wxString destFileName = destFile.GetName();
196
197 if( destFileName.StartsWith( aProjectName + "-" ) )
198 {
199 destFileName.Replace( aProjectName, aNewProjectName, false );
200 destFile.SetName( destFileName );
201 }
202
203 KiCopyFile( aSrcFilePath, destFile.GetFullPath(), aErrors );
204 }
205 else if( ext == FILEEXT::GerberJobFileExtension )
206 {
207 if( destFile.GetName() == aProjectName + wxT( "-job" ) )
208 destFile.SetName( aNewProjectName + wxT( "-job" ) );
209
210 FILE_LINE_READER jobfileReader( aSrcFilePath );
211
212 char* line;
213 wxString data;
214
215 while( ( line = jobfileReader.ReadLine() ) != nullptr )
216 data << line << '\n';
217
218 // detect the file format: old (deprecated) gerber format or official JSON format
219 if( !data.Contains( wxT( "{" ) ) )
220 {
221 KiCopyFile( aSrcFilePath, destFile.GetFullPath(), aErrors );
222 return;
223 }
224
225 bool success = false;
226
227 try
228 {
229 // Will throw on parse error
230 json js = json::parse( TO_UTF8( data ) );
231
232 for( auto& entry : js["FilesAttributes"] )
233 {
234 wxString path = wxString( entry["Path"].get<std::string>() );
235
236 if( path.StartsWith( aProjectName + wxT( "-" ) ) )
237 {
238 path.Replace( aProjectName, aNewProjectName, false );
239 entry["Path"] = path.ToStdString();
240 }
241 }
242
243 wxFFile destJobFile( destFile.GetFullPath(), wxT( "wb" ) );
244
245 if( destJobFile.IsOpened() )
246 success = destJobFile.Write( js.dump( 0 ) );
247
248 // wxFFile dtor will close the file
249 }
250 catch( ... )
251 {
252 success = false;
253 }
254
255 if( !success )
256 {
257 wxString msg;
258
259 if( !aErrors.empty() )
260 aErrors += wxT( "\n" );
261
262 msg.Printf( _( "Cannot copy file '%s'." ), destFile.GetFullPath() );
263 aErrors += msg;
264 }
265 }
266 else if( ext == FILEEXT::DrillFileExtension )
267 {
268 wxString destFileName = destFile.GetName();
269
270 if( destFileName == aProjectName )
271 destFileName = aNewProjectName;
272 else if( destFileName.StartsWith( aProjectName + wxT( "-" ) ) )
273 destFileName.Replace( aProjectName, aNewProjectName, false );
274
275 destFile.SetName( destFileName );
276
277 KiCopyFile( aSrcFilePath, destFile.GetFullPath(), aErrors );
278 }
279 else
280 {
281 wxFAIL_MSG( wxT( "Unexpected filetype for GerbView::SaveFileAs()" ) );
282 }
283}
284
constexpr EDA_IU_SCALE gerbIUScale
Definition: base_units.h:111
static std::list< TOOL_ACTION * > & GetActionList()
Return list of TOOL_ACTIONs.
Class to hold basic information about controls that can be added to the toolbars.
static std::list< ACTION_TOOLBAR_CONTROL * > & GetCustomControlList()
Get the list of custom controls that could be used on toolbars.
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:249
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:286
virtual KIWAY_PLAYER * Player(FRAME_T aFrameType, bool doCreate=true, wxTopLevelWindow *aParent=nullptr)
Return the KIWAY_PLAYER* given a FRAME_T.
Definition: kiway.cpp:395
FACE_T
Known KIFACE implementations.
Definition: kiway.h:292
@ FACE_GERBVIEW
Definition: kiway.h:296
Container for data for KiCad programs.
Definition: pgm_base.h:103
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition: pgm_base.h:125
T * RegisterSettings(T *aSettings, bool aLoadNow=true)
Take ownership of the pointer passed in.
Represent a single user action.
Definition: tool_action.h:304
EDA_UNITS GetUserUnits() const
void SetUserUnits(EDA_UNITS aUnits)
#define _(s)
EDA_UNITS
Definition: eda_units.h:48
@ PANEL_GBR_DISPLAY_OPTIONS
Definition: frame_type.h:109
@ PANEL_GBR_GRIDS
Definition: frame_type.h:112
@ FRAME_GERBER
Definition: frame_type.h:57
@ PANEL_GBR_TOOLBARS
Definition: frame_type.h:114
@ PANEL_GBR_COLORS
Definition: frame_type.h:113
@ PANEL_GBR_EXCELLON_OPTIONS
Definition: frame_type.h:111
nlohmann::json json
Definition: gerbview.cpp:50
KIFACE_BASE & Kiface()
Global KIFACE_BASE "get" accessor.
Definition: gerbview.cpp:152
void KiCopyFile(const wxString &aSrcPath, const wxString &aDestPath, wxString &aErrors)
Definition: gestfich.cpp:290
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)
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:429
bool OnKifaceStart(PGM_BASE *aProgram, int aCtlBits, KIWAY *aKiway) override
Typically start_common() is called from here.
Definition: gerbview.cpp:163
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:178
wxWindow * CreateKiWindow(wxWindow *aParent, int aClassId, KIWAY *aKiway, int aCtlBits=0) override
Create a wxWindow for the current project.
Definition: gerbview.cpp:68
void * IfaceOrAddress(int aDataId) override
Return a pointer to the requested object.
Definition: gerbview.cpp:130
void OnKifaceEnd() override
Called just once just before the DSO is to be unloaded.
Definition: gerbview.cpp:172
IFACE(const char *aName, KIWAY::FACE_T aType)
Definition: gerbview.cpp:59
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:153
Definition of file extensions used in Kicad.