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