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, see <https://www.gnu.org/licenses/>.
23 */
24
25#include <gerbview.h>
26#include <gerbview_frame.h>
27#include <gerbview_settings.h>
29#include <gestfich.h>
30#include <kiface_base.h>
31#include <macros.h>
32#include <json_common.h>
33#include <pgm_base.h>
34#include <richio.h>
36#include <string_utils.h>
42#include <wx/ffile.h>
43#include <reporter.h>
45
47#include <toolbars_gerber.h>
48
49using json = nlohmann::json;
50
51
52namespace GERBV
53{
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
65 bool OnKifaceStart( PGM_BASE* aProgram, int aCtlBits, KIWAY* aKiway ) override;
66
67 void OnKifaceEnd() override;
69 wxWindow* CreateKiWindow( wxWindow* aParent, int aClassId, KIWAY* aKiway, int aCtlBits = 0 ) override
70 {
71 switch( aClassId )
72 {
73 case FRAME_GERBER: return new GERBVIEW_FRAME( aKiway, aParent );
74
76
78
80 {
82 EDA_BASE_FRAME* frame = aKiway->Player( FRAME_GERBER, false );
83
84 if( frame )
85 SetUserUnits( frame->GetUserUnits() );
86
87 return new PANEL_GRID_SETTINGS( aParent, this, frame, cfg, FRAME_GERBER );
88 }
89
90 case PANEL_GBR_COLORS: return new PANEL_GERBVIEW_COLOR_SETTINGS( aParent );
91
93 {
94 GERBVIEW_SETTINGS* cfg = GetAppSettings<GERBVIEW_SETTINGS>( "gerbview" );
95 TOOLBAR_SETTINGS* tb = GetToolbarSettings<GERBVIEW_TOOLBAR_SETTINGS>( "gerbview-toolbars" );
96
97 std::vector<TOOL_ACTION*> actions;
98 std::vector<ACTION_TOOLBAR_CONTROL*> controls;
99
100 for( TOOL_ACTION* action : ACTION_MANAGER::GetActionList() )
101 actions.push_back( action );
102
103 for( ACTION_TOOLBAR_CONTROL* control : ACTION_TOOLBAR::GetCustomControlList( FRAME_GERBER ) )
104 controls.push_back( control );
105
106 return new PANEL_TOOLBAR_CUSTOMIZATION( aParent, cfg, tb, FRAME_GERBER, actions, controls );
107 }
108
109 default:;
110 }
111
112 return nullptr;
113 }
114
125 void* IfaceOrAddress( int aDataId ) override { return nullptr; }
126
132 void SaveFileAs( const wxString& aProjectBasePath, const wxString& aProjectName,
133 const wxString& aNewProjectBasePath, const wxString& aNewProjectName, const wxString& aSrcFilePath,
134 wxString& aErrors ) override;
135
136 int HandleJob( JOB* aJob, REPORTER* aReporter, PROGRESS_REPORTER* aProgressReporter ) override;
137
138 bool HandleJobConfig( JOB* aJob, wxWindow* aParent ) override;
139
140private:
141 std::unique_ptr<GERBVIEW_JOBS_HANDLER> m_jobHandler;
142
143} kiface( "gerbview", KIWAY::FACE_GERBVIEW );
144
145} // namespace GERBV
146
147
148using namespace GERBV;
149
150
152{
153 return kiface;
154}
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
171 m_jobHandler = std::make_unique<GERBVIEW_JOBS_HANDLER>( aKiway );
172
174 {
175 m_jobHandler->SetReporter( &CLI_REPORTER::GetInstance() );
176 m_jobHandler->SetProgressReporter( &CLI_PROGRESS_REPORTER::GetInstance() );
177 }
178
179 return true;
180}
181
182
184{
185 end_common();
186}
187
188
189int IFACE::HandleJob( JOB* aJob, REPORTER* aReporter, PROGRESS_REPORTER* aProgressReporter )
190{
191 return m_jobHandler->RunJob( aJob, aReporter, aProgressReporter );
192}
193
194
195bool IFACE::HandleJobConfig( JOB* aJob, wxWindow* aParent )
196{
197 return m_jobHandler->HandleJobConfig( aJob, aParent );
198}
199
200
201void IFACE::SaveFileAs( const wxString& aProjectBasePath, const wxString& aProjectName,
202 const wxString& aNewProjectBasePath, const wxString& aNewProjectName,
203 const wxString& aSrcFilePath, wxString& aErrors )
204{
205 wxFileName destFile( aSrcFilePath );
206 wxString destPath = destFile.GetPathWithSep();
207 wxUniChar pathSep = wxFileName::GetPathSeparator();
208 wxString ext = destFile.GetExt();
209
210 if( destPath.StartsWith( aProjectBasePath + pathSep ) )
211 {
212 destPath.Replace( aProjectBasePath, aNewProjectBasePath, false );
213 destFile.SetPath( destPath );
214 }
215
217 {
218 wxString destFileName = destFile.GetName();
219
220 if( destFileName.StartsWith( aProjectName + "-" ) )
221 {
222 destFileName.Replace( aProjectName, aNewProjectName, false );
223 destFile.SetName( destFileName );
224 }
225
226 KiCopyFile( aSrcFilePath, destFile.GetFullPath(), aErrors );
227 }
228 else if( ext == FILEEXT::GerberJobFileExtension )
229 {
230 if( destFile.GetName() == aProjectName + wxT( "-job" ) )
231 destFile.SetName( aNewProjectName + wxT( "-job" ) );
232
233 FILE_LINE_READER jobfileReader( aSrcFilePath );
234
235 char* line;
236 wxString data;
237
238 while( ( line = jobfileReader.ReadLine() ) != nullptr )
239 data << line << '\n';
240
241 // detect the file format: old (deprecated) gerber format or official JSON format
242 if( !data.Contains( wxT( "{" ) ) )
243 {
244 KiCopyFile( aSrcFilePath, destFile.GetFullPath(), aErrors );
245 return;
246 }
247
248 bool success = false;
249
250 try
251 {
252 // Will throw on parse error
253 json js = json::parse( TO_UTF8( data ) );
254
255 for( auto& entry : js["FilesAttributes"] )
256 {
257 wxString path = wxString( entry["Path"].get<std::string>() );
258
259 if( path.StartsWith( aProjectName + wxT( "-" ) ) )
260 {
261 path.Replace( aProjectName, aNewProjectName, false );
262 entry["Path"] = path.ToStdString();
263 }
264 }
265
266 wxFFile destJobFile( destFile.GetFullPath(), wxT( "wb" ) );
267
268 if( destJobFile.IsOpened() )
269 success = destJobFile.Write( js.dump( 0 ) );
270
271 // wxFFile dtor will close the file
272 }
273 catch( ... )
274 {
275 success = false;
276 }
277
278 if( !success )
279 {
280 wxString msg;
281
282 if( !aErrors.empty() )
283 aErrors += wxT( "\n" );
284
285 msg.Printf( _( "Cannot copy file '%s'." ), destFile.GetFullPath() );
286 aErrors += msg;
287 }
288 }
289 else if( ext == FILEEXT::DrillFileExtension )
290 {
291 wxString destFileName = destFile.GetName();
292
293 if( destFileName == aProjectName )
294 destFileName = aNewProjectName;
295 else if( destFileName.StartsWith( aProjectName + wxT( "-" ) ) )
296 destFileName.Replace( aProjectName, aNewProjectName, false );
297
298 destFile.SetName( destFileName );
299
300 KiCopyFile( aSrcFilePath, destFile.GetFullPath(), aErrors );
301 }
302 else
303 {
304 wxFAIL_MSG( wxT( "Unexpected filetype for GerbView::SaveFileAs()" ) );
305 }
306}
constexpr EDA_IU_SCALE gerbIUScale
Definition base_units.h:120
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:157
The base frame for deriving all KiCad main window classes.
A LINE_READER that reads from an open file.
Definition richio.h:154
char * ReadLine() override
Read a line of text into the buffer and increments the line number counter.
Definition richio.cpp:208
An simple container class that lets us dispatch output jobs to kifaces.
Definition job.h:184
A KIFACE implementation.
Definition kiface_base.h:35
KIFACE_BASE(const char *aKifaceName, KIWAY::FACE_T aId)
Definition kiface_base.h:63
void InitSettings(APP_SETTINGS_BASE *aSettings)
Definition kiface_base.h:93
void end_common()
Common things to do for a top program module, during OnKifaceEnd();.
APP_SETTINGS_BASE * KifaceSettings() const
Definition kiface_base.h:91
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:311
virtual KIWAY_PLAYER * Player(FRAME_T aFrameType, bool doCreate=true, wxTopLevelWindow *aParent=nullptr)
Return the KIWAY_PLAYER* given a FRAME_T.
Definition kiway.cpp:398
FACE_T
Known KIFACE implementations.
Definition kiway.h:317
@ FACE_GERBVIEW
Definition kiway.h:321
Container for data for KiCad programs.
Definition pgm_base.h:102
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition pgm_base.h:124
A progress reporter interface for use in multi-threaded environments.
A pure virtual class used to derive REPORTER objects from.
Definition reporter.h:71
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:44
@ PANEL_GBR_DISPLAY_OPTIONS
Definition frame_type.h:111
@ PANEL_GBR_GRIDS
Definition frame_type.h:114
@ FRAME_GERBER
Definition frame_type.h:53
@ PANEL_GBR_TOOLBARS
Definition frame_type.h:116
@ PANEL_GBR_COLORS
Definition frame_type.h:115
@ PANEL_GBR_EXCELLON_OPTIONS
Definition frame_type.h:113
nlohmann::json json
Definition gerbview.cpp:49
KIFACE_BASE & Kiface()
Global KIFACE_BASE "get" accessor.
Definition gerbview.cpp:151
void KiCopyFile(const wxString &aSrcPath, const wxString &aDestPath, wxString &aErrors)
Definition gestfich.cpp:307
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:161
#define KIFACE_GETTER
Definition kiway.h:109
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:165
std::unique_ptr< GERBVIEW_JOBS_HANDLER > m_jobHandler
Definition gerbview.cpp:141
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:201
int HandleJob(JOB *aJob, REPORTER *aReporter, PROGRESS_REPORTER *aProgressReporter) override
Definition gerbview.cpp:189
wxWindow * CreateKiWindow(wxWindow *aParent, int aClassId, KIWAY *aKiway, int aCtlBits=0) override
Create a wxWindow for the current project.
Definition gerbview.cpp:69
void * IfaceOrAddress(int aDataId) override
Return a pointer to the requested object.
Definition gerbview.cpp:125
bool HandleJobConfig(JOB *aJob, wxWindow *aParent) override
Definition gerbview.cpp:195
void OnKifaceEnd() override
Called just once just before the DSO is to be unloaded.
Definition gerbview.cpp:183
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
std::string path
static const long long MM
Definition of file extensions used in Kicad.