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