KiCad PCB EDA Suite
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
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 <nlohmann/json.hpp>
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 {
73 case FRAME_GERBER:
74 return new GERBVIEW_FRAME( aKiway, aParent );
77 return new PANEL_GERBVIEW_DISPLAY_OPTIONS( aParent );
78
80 return new PANEL_GERBVIEW_EXCELLON_SETTINGS( aParent );
81
82 case PANEL_GBR_GRIDS:
83 {
86 EDA_BASE_FRAME* frame = aKiway->Player( FRAME_GERBER, false );
87
88 if( frame )
89 SetUserUnits( frame->GetUserUnits() );
90
91 return new PANEL_GRID_SETTINGS( aParent, this, frame, cfg, FRAME_GERBER );
92 }
93
95 return new PANEL_GERBVIEW_COLOR_SETTINGS( aParent );
96
98 {
100 GERBVIEW_SETTINGS* cfg = mgr.GetAppSettings<GERBVIEW_SETTINGS>( "gerbview" );
101 TOOLBAR_SETTINGS* tb = mgr.GetToolbarSettings<GERBVIEW_TOOLBAR_SETTINGS>( "gerbview-toolbars" );
102
103 std::vector<TOOL_ACTION*> actions;
104 std::vector<ACTION_TOOLBAR_CONTROL*> controls;
105
107 actions.push_back( action );
108
110 controls.push_back( control );
111
112 return new PANEL_TOOLBAR_CUSTOMIZATION( aParent, cfg, tb, actions, controls );
113 }
114
115 default:
116 ;
117 }
118
119 return nullptr;
120 }
121
132 void* IfaceOrAddress( int aDataId ) override
133 {
134 return nullptr;
135 }
136
142 void SaveFileAs( const wxString& aProjectBasePath, const wxString& aProjectName,
143 const wxString& aNewProjectBasePath, const wxString& aNewProjectName,
144 const wxString& aSrcFilePath, wxString& aErrors ) override;
145
146} kiface( "gerbview", KIWAY::FACE_GERBVIEW );
147
148} // namespace
149
150
151using namespace GERBV;
152
153
155
156
157// KIFACE_GETTER's actual spelling is a substitution macro found in kiway.h.
158// KIFACE_GETTER will not have name mangling due to declaration in kiway.h.
159KIFACE_API KIFACE* KIFACE_GETTER( int* aKIFACEversion, int aKiwayVersion, PGM_BASE* aProgram )
160{
161 return &kiface;
162}
163
164
165bool IFACE::OnKifaceStart( PGM_BASE* aProgram, int aCtlBits, KIWAY* aKiway )
166{
169 start_common( aCtlBits );
170 return true;
171}
172
173
175{
176 end_common();
177}
178
179
180void IFACE::SaveFileAs( const wxString& aProjectBasePath, const wxString& aProjectName,
181 const wxString& aNewProjectBasePath, const wxString& aNewProjectName,
182 const wxString& aSrcFilePath, wxString& aErrors )
183{
184 wxFileName destFile( aSrcFilePath );
185 wxString destPath = destFile.GetPathWithSep();
186 wxUniChar pathSep = wxFileName::GetPathSeparator();
187 wxString ext = destFile.GetExt();
188
189 if( destPath.StartsWith( aProjectBasePath + pathSep ) )
190 {
191 destPath.Replace( aProjectBasePath, aNewProjectBasePath, false );
192 destFile.SetPath( destPath );
193 }
194
196 {
197 wxString destFileName = destFile.GetName();
198
199 if( destFileName.StartsWith( aProjectName + "-" ) )
200 {
201 destFileName.Replace( aProjectName, aNewProjectName, false );
202 destFile.SetName( destFileName );
203 }
204
205 KiCopyFile( aSrcFilePath, destFile.GetFullPath(), aErrors );
206 }
207 else if( ext == FILEEXT::GerberJobFileExtension )
208 {
209 if( destFile.GetName() == aProjectName + wxT( "-job" ) )
210 destFile.SetName( aNewProjectName + wxT( "-job" ) );
211
212 FILE_LINE_READER jobfileReader( aSrcFilePath );
213
214 char* line;
215 wxString data;
216
217 while( ( line = jobfileReader.ReadLine() ) != nullptr )
218 data << line << '\n';
219
220 // detect the file format: old (deprecated) gerber format or official JSON format
221 if( !data.Contains( wxT( "{" ) ) )
222 {
223 KiCopyFile( aSrcFilePath, destFile.GetFullPath(), aErrors );
224 return;
225 }
226
227 bool success = false;
228
229 try
230 {
231 // Will throw on parse error
232 json js = json::parse( TO_UTF8( data ) );
233
234 for( auto& entry : js["FilesAttributes"] )
235 {
236 wxString path = wxString( entry["Path"].get<std::string>() );
237
238 if( path.StartsWith( aProjectName + wxT( "-" ) ) )
239 {
240 path.Replace( aProjectName, aNewProjectName, false );
241 entry["Path"] = path.ToStdString();
242 }
243 }
244
245 wxFFile destJobFile( destFile.GetFullPath(), wxT( "wb" ) );
246
247 if( destJobFile.IsOpened() )
248 success = destJobFile.Write( js.dump( 0 ) );
249
250 // wxFFile dtor will close the file
251 }
252 catch( ... )
253 {
254 success = false;
255 }
256
257 if( !success )
258 {
259 wxString msg;
260
261 if( !aErrors.empty() )
262 aErrors += wxT( "\n" );
263
264 msg.Printf( _( "Cannot copy file '%s'." ), destFile.GetFullPath() );
265 aErrors += msg;
266 }
267 }
268 else if( ext == FILEEXT::DrillFileExtension )
269 {
270 wxString destFileName = destFile.GetName();
271
272 if( destFileName == aProjectName )
273 destFileName = aNewProjectName;
274 else if( destFileName.StartsWith( aProjectName + wxT( "-" ) ) )
275 destFileName.Replace( aProjectName, aNewProjectName, false );
276
277 destFile.SetName( destFileName );
278
279 KiCopyFile( aSrcFilePath, destFile.GetFullPath(), aErrors );
280 }
281 else
282 {
283 wxFAIL_MSG( wxT( "Unexpected filetype for GerbView::SaveFileAs()" ) );
284 }
285}
286
constexpr EDA_IU_SCALE gerbIUScale
Definition: base_units.h:107
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:246
Toolbar configuration for gerbview.
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:285
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:291
@ FACE_GERBVIEW
Definition: kiway.h:295
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.
T * GetToolbarSettings(const wxString &aFilename)
Return a handle to the given toolbar settings.
T * GetAppSettings(const wxString &aFilename)
Return a handle to the a given settings by type.
Represent a single user action.
Definition: tool_action.h:304
void SetUserUnits(EDA_UNITS aUnits)
#define _(s)
EDA_UNITS
Definition: eda_units.h:46
@ 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:154
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)
PGM_BASE & Pgm()
The global program "get" accessor.
Definition: pgm_base.cpp:1071
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:403
bool OnKifaceStart(PGM_BASE *aProgram, int aCtlBits, KIWAY *aKiway) override
Typically start_common() is called from here.
Definition: gerbview.cpp:165
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:180
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:132
void OnKifaceEnd() override
Called just once just before the DSO is to be unloaded.
Definition: gerbview.cpp:174
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:152
Definition of file extensions used in Kicad.