KiCad PCB EDA Suite
Loading...
Searching...
No Matches
gerbview.cpp
Go to the documentation of this file.
1
2 * @file gerbview.cpp
3 * @brief GERBVIEW main file.
4 */
5
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>
33#include <gestfich.h>
34#include <kiface_base.h>
35#include <macros.h>
36#include <json_common.h>
37#include <pgm_base.h>
38#include <richio.h>
40#include <string_utils.h>
46#include <wx/ffile.h>
47#include <reporter.h>
49
51#include <toolbars_gerber.h>
52
53using json = nlohmann::json;
54
55
56namespace GERBV
57{
58
59static struct IFACE : public KIFACE_BASE, public UNITS_PROVIDER
60{
61 // Of course all are virtual overloads, implementations of the KIFACE.
62
63 IFACE( const char* aName, KIWAY::FACE_T aType ) :
64 KIFACE_BASE( aName, aType ),
66 {
67 }
69 bool OnKifaceStart( PGM_BASE* aProgram, int aCtlBits, KIWAY* aKiway ) override;
70
71 void OnKifaceEnd() override;
72
73 wxWindow* CreateKiWindow( wxWindow* aParent, int aClassId, KIWAY* aKiway, int aCtlBits = 0 ) override
74 {
75 switch( aClassId )
76 {
77 case FRAME_GERBER: return new GERBVIEW_FRAME( aKiway, aParent );
78
80
82
83 case PANEL_GBR_GRIDS:
84 {
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
94 case PANEL_GBR_COLORS: 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
104 for( TOOL_ACTION* action : ACTION_MANAGER::GetActionList() )
105 actions.push_back( action );
106
107 for( ACTION_TOOLBAR_CONTROL* control : ACTION_TOOLBAR::GetCustomControlList( FRAME_GERBER ) )
108 controls.push_back( control );
109
110 return new PANEL_TOOLBAR_CUSTOMIZATION( aParent, cfg, tb, FRAME_GERBER, actions, controls );
111 }
112
113 default:;
114 }
115
116 return nullptr;
117 }
118
129 void* IfaceOrAddress( int aDataId ) override { return nullptr; }
130
136 void SaveFileAs( const wxString& aProjectBasePath, const wxString& aProjectName,
137 const wxString& aNewProjectBasePath, const wxString& aNewProjectName, const wxString& aSrcFilePath,
138 wxString& aErrors ) override;
139
140 int HandleJob( JOB* aJob, REPORTER* aReporter, PROGRESS_REPORTER* aProgressReporter ) override;
141
142 bool HandleJobConfig( JOB* aJob, wxWindow* aParent ) override;
143
144private:
145 std::unique_ptr<GERBVIEW_JOBS_HANDLER> m_jobHandler;
146
147} kiface( "gerbview", KIWAY::FACE_GERBVIEW );
148
149} // namespace GERBV
150
151
152using namespace GERBV;
153
154
156{
157 return kiface;
158}
159
160
161// KIFACE_GETTER's actual spelling is a substitution macro found in kiway.h.
162// KIFACE_GETTER will not have name mangling due to declaration in kiway.h.
163KIFACE_API KIFACE* KIFACE_GETTER( int* aKIFACEversion, int aKiwayVersion, PGM_BASE* aProgram )
164{
165 return &kiface;
166}
167
168
169bool IFACE::OnKifaceStart( PGM_BASE* aProgram, int aCtlBits, KIWAY* aKiway )
170{
173 start_common( aCtlBits );
174
175 m_jobHandler = std::make_unique<GERBVIEW_JOBS_HANDLER>( aKiway );
176
178 {
179 m_jobHandler->SetReporter( &CLI_REPORTER::GetInstance() );
180 m_jobHandler->SetProgressReporter( &CLI_PROGRESS_REPORTER::GetInstance() );
181 }
182
183 return true;
184}
185
186
188{
189 end_common();
190}
191
192
193int IFACE::HandleJob( JOB* aJob, REPORTER* aReporter, PROGRESS_REPORTER* aProgressReporter )
194{
195 return m_jobHandler->RunJob( aJob, aReporter, aProgressReporter );
196}
197
198
199bool IFACE::HandleJobConfig( JOB* aJob, wxWindow* aParent )
200{
201 return m_jobHandler->HandleJobConfig( aJob, aParent );
202}
203
204
205void IFACE::SaveFileAs( const wxString& aProjectBasePath, const wxString& aProjectName,
206 const wxString& aNewProjectBasePath, const wxString& aNewProjectName,
207 const wxString& aSrcFilePath, wxString& aErrors )
208{
209 wxFileName destFile( aSrcFilePath );
210 wxString destPath = destFile.GetPathWithSep();
211 wxUniChar pathSep = wxFileName::GetPathSeparator();
212 wxString ext = destFile.GetExt();
213
214 if( destPath.StartsWith( aProjectBasePath + pathSep ) )
215 {
216 destPath.Replace( aProjectBasePath, aNewProjectBasePath, false );
217 destFile.SetPath( destPath );
218 }
219
221 {
222 wxString destFileName = destFile.GetName();
223
224 if( destFileName.StartsWith( aProjectName + "-" ) )
225 {
226 destFileName.Replace( aProjectName, aNewProjectName, false );
227 destFile.SetName( destFileName );
228 }
229
230 KiCopyFile( aSrcFilePath, destFile.GetFullPath(), aErrors );
231 }
232 else if( ext == FILEEXT::GerberJobFileExtension )
233 {
234 if( destFile.GetName() == aProjectName + wxT( "-job" ) )
235 destFile.SetName( aNewProjectName + wxT( "-job" ) );
236
237 FILE_LINE_READER jobfileReader( aSrcFilePath );
238
239 char* line;
240 wxString data;
241
242 while( ( line = jobfileReader.ReadLine() ) != nullptr )
243 data << line << '\n';
244
245 // detect the file format: old (deprecated) gerber format or official JSON format
246 if( !data.Contains( wxT( "{" ) ) )
247 {
248 KiCopyFile( aSrcFilePath, destFile.GetFullPath(), aErrors );
249 return;
250 }
251
252 bool success = false;
253
254 try
255 {
256 // Will throw on parse error
257 json js = json::parse( TO_UTF8( data ) );
258
259 for( auto& entry : js["FilesAttributes"] )
260 {
261 wxString path = wxString( entry["Path"].get<std::string>() );
262
263 if( path.StartsWith( aProjectName + wxT( "-" ) ) )
264 {
265 path.Replace( aProjectName, aNewProjectName, false );
266 entry["Path"] = path.ToStdString();
267 }
268 }
269
270 wxFFile destJobFile( destFile.GetFullPath(), wxT( "wb" ) );
271
272 if( destJobFile.IsOpened() )
273 success = destJobFile.Write( js.dump( 0 ) );
274
275 // wxFFile dtor will close the file
276 }
277 catch( ... )
278 {
279 success = false;
280 }
281
282 if( !success )
283 {
284 wxString msg;
285
286 if( !aErrors.empty() )
287 aErrors += wxT( "\n" );
288
289 msg.Printf( _( "Cannot copy file '%s'." ), destFile.GetFullPath() );
290 aErrors += msg;
291 }
292 }
293 else if( ext == FILEEXT::DrillFileExtension )
294 {
295 wxString destFileName = destFile.GetName();
296
297 if( destFileName == aProjectName )
298 destFileName = aNewProjectName;
299 else if( destFileName.StartsWith( aProjectName + wxT( "-" ) ) )
300 destFileName.Replace( aProjectName, aNewProjectName, false );
301
302 destFile.SetName( destFileName );
303
304 KiCopyFile( aSrcFilePath, destFile.GetFullPath(), aErrors );
305 }
306 else
307 {
308 wxFAIL_MSG( wxT( "Unexpected filetype for GerbView::SaveFileAs()" ) );
309 }
310}
constexpr EDA_IU_SCALE gerbIUScale
Definition base_units.h:124
static std::list< TOOL_ACTION * > & GetActionList()
Return list of TOOL_ACTIONs.
static std::list< ACTION_TOOLBAR_CONTROL * > GetCustomControlList(FRAME_T aContext)
Get the list of custom controls that could be used on a particular frame type.
static CLI_PROGRESS_REPORTER & GetInstance()
static CLI_REPORTER & GetInstance()
Definition reporter.cpp:161
The base frame for deriving all KiCad main window classes.
A LINE_READER that reads from an open file.
Definition richio.h:158
char * ReadLine() override
Read a line of text into the buffer and increments the line number counter.
Definition richio.cpp:212
An simple container class that lets us dispatch output jobs to kifaces.
Definition job.h:184
A KIFACE implementation.
Definition kiface_base.h:39
KIFACE_BASE(const char *aKifaceName, KIWAY::FACE_T aId)
Definition kiface_base.h:67
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();.
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().
int m_start_flags
flags provided in OnKifaceStart()
A minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the same KiCad...
Definition kiway.h:315
virtual KIWAY_PLAYER * Player(FRAME_T aFrameType, bool doCreate=true, wxTopLevelWindow *aParent=nullptr)
Return the KIWAY_PLAYER* given a FRAME_T.
Definition kiway.cpp:402
FACE_T
Known KIFACE implementations.
Definition kiway.h:321
@ FACE_GERBVIEW
Definition kiway.h:325
Container for data for KiCad programs.
Definition pgm_base.h:108
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition pgm_base.h:130
A progress reporter interface for use in multi-threaded environments.
A pure virtual class used to derive REPORTER objects from.
Definition reporter.h:75
T * RegisterSettings(T *aSettings, bool aLoadNow=true)
Take ownership of the pointer passed in.
UNITS_PROVIDER(const EDA_IU_SCALE &aIuScale, EDA_UNITS aUnits)
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:110
@ PANEL_GBR_GRIDS
Definition frame_type.h:113
@ FRAME_GERBER
Definition frame_type.h:57
@ PANEL_GBR_TOOLBARS
Definition frame_type.h:115
@ PANEL_GBR_COLORS
Definition frame_type.h:114
@ PANEL_GBR_EXCELLON_OPTIONS
Definition frame_type.h:112
nlohmann::json json
Definition gerbview.cpp:53
KIFACE_BASE & Kiface()
Global KIFACE_BASE "get" accessor.
Definition gerbview.cpp:155
void KiCopyFile(const wxString &aSrcPath, const wxString &aDestPath, wxString &aErrors)
Definition gestfich.cpp:294
static const std::string GerberJobFileExtension
static const std::string DrillFileExtension
static bool IsGerberFileExtension(const wxString &ext)
#define KIFACE_API
#define KFCTL_CLI
Running as CLI app.
Definition kiway.h:165
#define KIFACE_GETTER
Definition kiway.h:113
This file contains miscellaneous commonly used macros and functions.
GERBV::IFACE KIFACE_BASE, UNITS_PROVIDER kiface("gerbview", KIWAY::FACE_GERBVIEW)
see class PGM_BASE
T * GetToolbarSettings(const wxString &aFilename)
T * GetAppSettings(const char *aFilename)
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
bool OnKifaceStart(PGM_BASE *aProgram, int aCtlBits, KIWAY *aKiway) override
Typically start_common() is called from here.
Definition gerbview.cpp:169
std::unique_ptr< GERBVIEW_JOBS_HANDLER > m_jobHandler
Definition gerbview.cpp:145
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:205
int HandleJob(JOB *aJob, REPORTER *aReporter, PROGRESS_REPORTER *aProgressReporter) override
Definition gerbview.cpp:193
wxWindow * CreateKiWindow(wxWindow *aParent, int aClassId, KIWAY *aKiway, int aCtlBits=0) override
Create a wxWindow for the current project.
Definition gerbview.cpp:73
void * IfaceOrAddress(int aDataId) override
Return a pointer to the requested object.
Definition gerbview.cpp:129
bool HandleJobConfig(JOB *aJob, wxWindow *aParent) override
Definition gerbview.cpp:199
void OnKifaceEnd() override
Called just once just before the DSO is to be unloaded.
Definition gerbview.cpp:187
IFACE(const char *aName, KIWAY::FACE_T aType)
Definition gerbview.cpp:63
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:156
std::string path
Definition of file extensions used in Kicad.