KiCad PCB EDA Suite
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 (C) 1992-2012 KiCad Developers, see change_log.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_i.h>
34 #include <macros.h>
35 #include <nlohmann/json.hpp>
36 #include <pgm_base.h>
39 #include <wx/ffile.h>
40 
42 
43 
44 namespace GERBV {
45 
46 static struct IFACE : public KIFACE_I
47 {
48  // Of course all are virtual overloads, implementations of the KIFACE.
49 
50  IFACE( const char* aName, KIWAY::FACE_T aType ) :
51  KIFACE_I( aName, aType )
52  {}
53 
54  bool OnKifaceStart( PGM_BASE* aProgram, int aCtlBits ) override;
55 
56  void OnKifaceEnd() override;
57 
58  wxWindow* CreateWindow( wxWindow* aParent, int aClassId, KIWAY* aKiway, int aCtlBits = 0 ) override
59  {
60  switch( aClassId )
61  {
62  case FRAME_GERBER:
63  {
64  GERBVIEW_FRAME* frame = new GERBVIEW_FRAME( aKiway, aParent );
65  return frame;
66  }
67  break;
68 
69  default:
70  ;
71  }
72 
73  return NULL;
74  }
75 
84  void* IfaceOrAddress( int aDataId ) override
85  {
86  return NULL;
87  }
88 
95  void SaveFileAs( const wxString& aProjectBasePath, const wxString& aProjectName,
96  const wxString& aNewProjectBasePath, const wxString& aNewProjectName,
97  const wxString& aSrcFilePath, wxString& aErrors ) override;
98 
99 } kiface( "gerbview", KIWAY::FACE_GERBVIEW );
100 
101 } // namespace
102 
103 using namespace GERBV;
104 
106 
107 KIFACE_I& Kiface() { return kiface; }
108 
109 
110 // KIFACE_GETTER's actual spelling is a substitution macro found in kiway.h.
111 // KIFACE_GETTER will not have name mangling due to declaration in kiway.h.
112 MY_API( KIFACE* ) KIFACE_GETTER( int* aKIFACEversion, int aKiwayVersion, PGM_BASE* aProgram )
113 {
114  process = aProgram;
115  return &kiface;
116 }
117 
118 
120 {
121  wxASSERT( process ); // KIFACE_GETTER has already been called.
122  return *process;
123 }
124 
125 
126 // Similar to PGM_BASE& Pgm(), but return nullptr when a *.ki_face is run from a python script.
128 {
129  return process;
130 }
131 
132 bool IFACE::OnKifaceStart( PGM_BASE* aProgram, int aCtlBits )
133 {
136  start_common( aCtlBits );
137  return true;
138 }
139 
140 
142 {
143  end_common();
144 }
145 
146 
147 void IFACE::SaveFileAs( const wxString& aProjectBasePath, const wxString& aProjectName,
148  const wxString& aNewProjectBasePath, const wxString& aNewProjectName,
149  const wxString& aSrcFilePath, wxString& aErrors )
150 {
151  wxFileName destFile( aSrcFilePath );
152  wxString destPath = destFile.GetPathWithSep();
153  wxUniChar pathSep = wxFileName::GetPathSeparator();
154  wxString ext = destFile.GetExt();
155 
156  if( destPath.StartsWith( aProjectBasePath + pathSep ) )
157  {
158  destPath.Replace( aProjectBasePath, aNewProjectBasePath, false );
159  destFile.SetPath( destPath );
160  }
161 
162  if( ext == "gbr" || IsProtelExtension( ext ) )
163  {
164  wxString destFileName = destFile.GetName();
165 
166  if( destFileName.StartsWith( aProjectName + "-" ) )
167  {
168  destFileName.Replace( aProjectName, aNewProjectName, false );
169  destFile.SetName( destFileName );
170  }
171 
172  KiCopyFile( aSrcFilePath, destFile.GetFullPath(), aErrors );
173  }
174  else if( ext == "gbrjob" )
175  {
176  if( destFile.GetName() == aProjectName + "-job" )
177  destFile.SetName( aNewProjectName + "-job" );
178 
179  FILE_LINE_READER jobfileReader( aSrcFilePath );
180 
181  char* line;
182  wxString data;
183 
184  while( ( line = jobfileReader.ReadLine() ) )
185  data << line << '\n';
186 
187  // detect the file format: old (deprecated) gerber format or official JSON format
188  if( !data.Contains( "{" ) )
189  {
190  KiCopyFile( aSrcFilePath, destFile.GetFullPath(), aErrors );
191  return;
192  }
193 
194  bool success = false;
195 
196  try
197  {
198  // Will throw on parse error
199  json js = json::parse( TO_UTF8( data ) );
200 
201  for( auto& entry : js["FilesAttributes"] )
202  {
203  wxString path = wxString( entry["Path"].get<std::string>() );
204 
205  if( path.StartsWith( aProjectName + "-" ) )
206  {
207  path.Replace( aProjectName, aNewProjectName, false );
208  entry["Path"] = path.ToStdString();
209  }
210  }
211 
212  wxFFile destJobFile( destFile.GetFullPath(), "wb" );
213 
214  if( destJobFile.IsOpened() )
215  success = destJobFile.Write( js.dump( 0 ) );
216 
217  // wxFFile dtor will close the file
218  }
219  catch( ... )
220  {
221  success = false;
222  }
223 
224  if( !success )
225  {
226  wxString msg;
227 
228  if( !aErrors.empty() )
229  aErrors += "\n";
230 
231  msg.Printf( _( "Cannot copy file '%s'." ), destFile.GetFullPath() );
232  aErrors += msg;
233  }
234  }
235  else if( ext == "drl" )
236  {
237  wxString destFileName = destFile.GetName();
238 
239  if( destFileName == aProjectName )
240  destFileName = aNewProjectName;
241  else if( destFileName.StartsWith( aProjectName + "-" ) )
242  destFileName.Replace( aProjectName, aNewProjectName, false );
243 
244  destFile.SetName( destFileName );
245 
246  KiCopyFile( aSrcFilePath, destFile.GetFullPath(), aErrors );
247  }
248  else
249  {
250  wxFAIL_MSG( "Unexpected filetype for GerbView::SaveFileAs()" );
251  }
252 }
253 
bool OnKifaceStart(PGM_BASE *aProgram, int aCtlBits) override
Typically start_common() is called from here.
Definition: gerbview.cpp:132
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition: pgm_base.h:129
wxWindow * CreateWindow(wxWindow *aParent, int aClassId, KIWAY *aKiway, int aCtlBits=0) override
Create a wxWindow for the current project.
Definition: gerbview.cpp:58
A KIFACE (I)mplementation.
Definition: kiface_i.h:37
bool start_common(int aCtlBits)
Common things to do for a top program module, during OnKifaceStart().
Definition: kiface_i.cpp:84
This file is part of the common library TODO brief description.
void OnKifaceEnd() override
Called just once just before the DSO is to be unloaded.
Definition: gerbview.cpp:141
KIFACE_I & Kiface()
Global KIFACE_I "get" accessor.
Definition: gerbview.cpp:107
PGM_BASE & Pgm()
The global Program "get" accessor.
Definition: gerbview.cpp:119
Container for data for KiCad programs.
Definition: pgm_base.h:93
bool parse(std::istream &aStream, bool aVerbose)
Parse a PCB or footprint file from the given input stream.
void KiCopyFile(const wxString &aSrcPath, const wxString &aDestPath, wxString &aErrors)
Definition: gestfich.cpp:363
APP_SETTINGS_BASE * KifaceSettings() const
Definition: kiface_i.h:92
nlohmann::json json
Definition: gerbview.cpp:41
bool IsProtelExtension(const wxString &ext)
int PGM_BASE * aProgram
Definition: gerbview.cpp:113
This file contains miscellaneous commonly used macros and functions.
static PGM_BASE * process
Definition: gerbview.cpp:105
IFACE(const char *aName, KIWAY::FACE_T aType)
Definition: gerbview.cpp:50
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: macros.h:96
A LINE_READER that reads from an open file.
Definition: richio.h:172
GERBV::IFACE KIFACE_I kiface("gerbview", KIWAY::FACE_GERBVIEW)
#define NULL
void end_common()
Common things to do for a top program module, during OnKifaceEnd();.
Definition: kiface_i.cpp:94
int aKiwayVersion
Definition: gerbview.cpp:112
Definition of file extensions used in Kicad.
A minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the same KiCad...
Definition: kiway.h:258
#define _(s)
#define KIFACE_GETTER
Definition: kiway.h:108
FACE_T
Known KIFACE implementations.
Definition: kiway.h:264
void * IfaceOrAddress(int aDataId) override
Function IfaceOrAddress return a pointer to the requested object.
Definition: gerbview.cpp:84
JSON_SETTINGS * RegisterSettings(JSON_SETTINGS *aSettings, bool aLoadNow=true)
Takes ownership of the pointer passed in.
see class PGM_BASE
Implement a participant in the KIWAY alchemy.
Definition: kiway.h:146
void SaveFileAs(const wxString &aProjectBasePath, const wxString &aProjectName, const wxString &aNewProjectBasePath, const wxString &aNewProjectName, const wxString &aSrcFilePath, wxString &aErrors) override
Function SaveFileAs Saving a file under a different name is delegated to the various KIFACEs because ...
Definition: gerbview.cpp:147
void InitSettings(APP_SETTINGS_BASE *aSettings)
Definition: kiface_i.h:94
MY_API(KIFACE *) KIFACE_GETTER(int *aKIFACEversion
PGM_BASE * PgmOrNull()
similar to PGM_BASE& Pgm(), but return a reference that can be nullptr when running a shared lib from...
Definition: gerbview.cpp:127