-
-
Notifications
You must be signed in to change notification settings - Fork 487
/
gerbview.cpp
265 lines (209 loc) · 8.12 KB
/
gerbview.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/**
* @file gerbview.cpp
* @brief GERBVIEW main file.
*/
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 1992-2021 KiCad Developers, see change_log.txt for contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <gerbview.h>
#include <gerbview_frame.h>
#include <gerbview_settings.h>
#include <gestfich.h>
#include <kiface_base.h>
#include <macros.h>
#include <nlohmann/json.hpp>
#include <pgm_base.h>
#include <richio.h>
#include <settings/settings_manager.h>
#include <string_utils.h>
#include <dialogs/panel_gerbview_display_options.h>
#include <dialogs/panel_gerbview_excellon_settings.h>
#include <dialogs/panel_grid_settings.h>
#include <dialogs/panel_gerbview_color_settings.h>
#include <wildcards_and_files_ext.h>
#include <wx/ffile.h>
using json = nlohmann::json;
namespace GERBV {
static struct IFACE : public KIFACE_BASE, public UNITS_PROVIDER
{
// Of course all are virtual overloads, implementations of the KIFACE.
IFACE( const char* aName, KIWAY::FACE_T aType ) :
KIFACE_BASE( aName, aType ),
UNITS_PROVIDER( gerbIUScale, EDA_UNITS::MILLIMETRES )
{}
bool OnKifaceStart( PGM_BASE* aProgram, int aCtlBits, KIWAY* aKiway ) override;
void OnKifaceEnd() override;
wxWindow* CreateKiWindow( wxWindow* aParent, int aClassId, KIWAY* aKiway,
int aCtlBits = 0 ) override
{
switch( aClassId )
{
case FRAME_GERBER:
return new GERBVIEW_FRAME( aKiway, aParent );
case PANEL_GBR_DISPLAY_OPTIONS:
return new PANEL_GERBVIEW_DISPLAY_OPTIONS( aParent );
case PANEL_GBR_EXCELLON_OPTIONS:
return new PANEL_GERBVIEW_EXCELLON_SETTINGS( aParent );
case PANEL_GBR_GRIDS:
{
SETTINGS_MANAGER& mgr = Pgm().GetSettingsManager();
APP_SETTINGS_BASE* cfg = mgr.GetAppSettings<GERBVIEW_SETTINGS>();
EDA_BASE_FRAME* frame = aKiway->Player( FRAME_GERBER, false );
if( frame )
SetUserUnits( frame->GetUserUnits() );
return new PANEL_GRID_SETTINGS( aParent, this, frame, cfg, FRAME_GERBER );
}
case PANEL_GBR_COLORS:
return new PANEL_GERBVIEW_COLOR_SETTINGS( aParent );
default:
;
}
return nullptr;
}
/**
* Return a pointer to the requested object.
*
* The safest way to use this is to retrieve a pointer to a static instance of an interface,
* similar to how the KIFACE interface is exported. But if you know what you are doing use
* it to retrieve anything you want.
*
* @param aDataId identifies which object you want the address of.
* @return the object requested and must be cast into the know type.
*/
void* IfaceOrAddress( int aDataId ) override
{
return nullptr;
}
/**
* Saving a file under a different name is delegated to the various KIFACEs because
* the project doesn't know the internal format of the various files (which may have
* paths in them that need updating).
*/
void SaveFileAs( const wxString& aProjectBasePath, const wxString& aProjectName,
const wxString& aNewProjectBasePath, const wxString& aNewProjectName,
const wxString& aSrcFilePath, wxString& aErrors ) override;
} kiface( "gerbview", KIWAY::FACE_GERBVIEW );
} // namespace
using namespace GERBV;
KIFACE_BASE& Kiface() { return kiface; }
// KIFACE_GETTER's actual spelling is a substitution macro found in kiway.h.
// KIFACE_GETTER will not have name mangling due to declaration in kiway.h.
KIFACE_API KIFACE* KIFACE_GETTER( int* aKIFACEversion, int aKiwayVersion, PGM_BASE* aProgram )
{
return &kiface;
}
bool IFACE::OnKifaceStart( PGM_BASE* aProgram, int aCtlBits, KIWAY* aKiway )
{
InitSettings( new GERBVIEW_SETTINGS );
aProgram->GetSettingsManager().RegisterSettings( KifaceSettings() );
start_common( aCtlBits );
return true;
}
void IFACE::OnKifaceEnd()
{
end_common();
}
void IFACE::SaveFileAs( const wxString& aProjectBasePath, const wxString& aProjectName,
const wxString& aNewProjectBasePath, const wxString& aNewProjectName,
const wxString& aSrcFilePath, wxString& aErrors )
{
wxFileName destFile( aSrcFilePath );
wxString destPath = destFile.GetPathWithSep();
wxUniChar pathSep = wxFileName::GetPathSeparator();
wxString ext = destFile.GetExt();
if( destPath.StartsWith( aProjectBasePath + pathSep ) )
{
destPath.Replace( aProjectBasePath, aNewProjectBasePath, false );
destFile.SetPath( destPath );
}
if( FILEEXT::IsGerberFileExtension( ext ) )
{
wxString destFileName = destFile.GetName();
if( destFileName.StartsWith( aProjectName + "-" ) )
{
destFileName.Replace( aProjectName, aNewProjectName, false );
destFile.SetName( destFileName );
}
KiCopyFile( aSrcFilePath, destFile.GetFullPath(), aErrors );
}
else if( ext == FILEEXT::GerberJobFileExtension )
{
if( destFile.GetName() == aProjectName + wxT( "-job" ) )
destFile.SetName( aNewProjectName + wxT( "-job" ) );
FILE_LINE_READER jobfileReader( aSrcFilePath );
char* line;
wxString data;
while( ( line = jobfileReader.ReadLine() ) != nullptr )
data << line << '\n';
// detect the file format: old (deprecated) gerber format or official JSON format
if( !data.Contains( wxT( "{" ) ) )
{
KiCopyFile( aSrcFilePath, destFile.GetFullPath(), aErrors );
return;
}
bool success = false;
try
{
// Will throw on parse error
json js = json::parse( TO_UTF8( data ) );
for( auto& entry : js["FilesAttributes"] )
{
wxString path = wxString( entry["Path"].get<std::string>() );
if( path.StartsWith( aProjectName + wxT( "-" ) ) )
{
path.Replace( aProjectName, aNewProjectName, false );
entry["Path"] = path.ToStdString();
}
}
wxFFile destJobFile( destFile.GetFullPath(), wxT( "wb" ) );
if( destJobFile.IsOpened() )
success = destJobFile.Write( js.dump( 0 ) );
// wxFFile dtor will close the file
}
catch( ... )
{
success = false;
}
if( !success )
{
wxString msg;
if( !aErrors.empty() )
aErrors += wxT( "\n" );
msg.Printf( _( "Cannot copy file '%s'." ), destFile.GetFullPath() );
aErrors += msg;
}
}
else if( ext == FILEEXT::DrillFileExtension )
{
wxString destFileName = destFile.GetName();
if( destFileName == aProjectName )
destFileName = aNewProjectName;
else if( destFileName.StartsWith( aProjectName + wxT( "-" ) ) )
destFileName.Replace( aProjectName, aNewProjectName, false );
destFile.SetName( destFileName );
KiCopyFile( aSrcFilePath, destFile.GetFullPath(), aErrors );
}
else
{
wxFAIL_MSG( wxT( "Unexpected filetype for GerbView::SaveFileAs()" ) );
}
}