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 <fctsys.h>
30 #include <gerbview.h>
31 #include <gerbview_frame.h>
32 #include <gestfich.h>
33 #include <kiface_i.h>
34 #include <nlohmann/json.hpp>
35 #include <pgm_base.h>
37 
39 
40 const wxChar* g_GerberPageSizeList[] =
41 {
42  wxT( "GERBER" ), // index 0: full size page selection
43  wxT( "A4" ),
44  wxT( "A3" ),
45  wxT( "A2" ),
46  wxT( "A" ),
47  wxT( "B" ),
48  wxT( "C" ),
49 };
50 
51 
52 namespace GERBV {
53 
54 static struct IFACE : public KIFACE_I
55 {
56  // Of course all are virtual overloads, implementations of the KIFACE.
57 
58  IFACE( const char* aName, KIWAY::FACE_T aType ) :
59  KIFACE_I( aName, aType )
60  {}
61 
62  bool OnKifaceStart( PGM_BASE* aProgram, int aCtlBits ) override;
63 
64  void OnKifaceEnd() override;
65 
66  wxWindow* CreateWindow( wxWindow* aParent, int aClassId, KIWAY* aKiway, int aCtlBits = 0 ) override
67  {
68  switch( aClassId )
69  {
70  case FRAME_GERBER:
71  {
72  GERBVIEW_FRAME* frame = new GERBVIEW_FRAME( aKiway, aParent );
73  return frame;
74  }
75  break;
76 
77  default:
78  ;
79  }
80 
81  return NULL;
82  }
83 
92  void* IfaceOrAddress( int aDataId ) override
93  {
94  return NULL;
95  }
96 
103  void SaveFileAs( const wxString& aProjectBasePath, const wxString& aProjectName,
104  const wxString& aNewProjectBasePath, const wxString& aNewProjectName,
105  const wxString& aSrcFilePath, wxString& aErrors ) override;
106 
107 } kiface( "gerbview", KIWAY::FACE_GERBVIEW );
108 
109 } // namespace
110 
111 using namespace GERBV;
112 
114 
115 KIFACE_I& Kiface() { return kiface; }
116 
117 
118 // KIFACE_GETTER's actual spelling is a substitution macro found in kiway.h.
119 // KIFACE_GETTER will not have name mangling due to declaration in kiway.h.
120 MY_API( KIFACE* ) KIFACE_GETTER( int* aKIFACEversion, int aKiwayVersion, PGM_BASE* aProgram )
121 {
122  process = aProgram;
123  return &kiface;
124 }
125 
126 
128 {
129  wxASSERT( process ); // KIFACE_GETTER has already been called.
130  return *process;
131 }
132 
133 
134 bool IFACE::OnKifaceStart( PGM_BASE* aProgram, int aCtlBits )
135 {
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.GetPath();
153  wxString ext = destFile.GetExt();
154 
155  if( destPath.StartsWith( aProjectBasePath ) )
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  CopyFile( 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  CopyFile( 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  wxFile destJobFile( destFile.GetFullPath(), wxFile::write );
212 
213  if( destJobFile.IsOpened() )
214  success = destJobFile.Write( js.dump( 0 ) );
215 
216  // wxFile 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  CopyFile( 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
Function OnKifaceStart is called just once shortly after the DSO is loaded.
Definition: gerbview.cpp:134
wxWindow * CreateWindow(wxWindow *aParent, int aClassId, KIWAY *aKiway, int aCtlBits=0) override
Function CreateWindow creates a wxWindow for the current project.
Definition: gerbview.cpp:66
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:90
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:141
KIFACE_I & Kiface()
Global KIFACE_I "get" accessor.
Definition: gerbview.cpp:115
PGM_BASE & Pgm()
The global Program "get" accessor.
Definition: gerbview.cpp:127
PGM_BASE keeps program (whole process) data for KiCad programs.
Definition: pgm_base.h:156
bool parse(std::istream &aStream, bool aVerbose)
Parse a PCB or footprint file from the given input stream.
void CopyFile(const wxString &aSrcPath, const wxString &aDestPath, wxString &aErrors)
Function CopyFile.
Definition: gestfich.cpp:363
nlohmann::json json
Definition: gerbview.cpp:38
bool IsProtelExtension(const wxString &ext)
int PGM_BASE * aProgram
Definition: gerbview.cpp:121
static PGM_BASE * process
Definition: gerbview.cpp:113
IFACE(const char *aName, KIWAY::FACE_T aType)
Definition: gerbview.cpp:58
#define TO_UTF8(wxstring)
Macro TO_UTF8 converts a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: macros.h:48
FILE_LINE_READER is a LINE_READER that reads from an open file.
Definition: richio.h:180
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:100
int aKiwayVersion
Definition: gerbview.cpp:120
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:274
#define KIFACE_GETTER
Definition: kiway.h:112
FACE_T
Known KIFACE implementations.
Definition: kiway.h:280
void * IfaceOrAddress(int aDataId) override
Function IfaceOrAddress return a pointer to the requested object.
Definition: gerbview.cpp:92
see class PGM_BASE
#define _(s)
Definition: 3d_actions.cpp:31
KIFACE is used by a participant in the KIWAY alchemy.
Definition: kiway.h:151
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
MY_API(KIFACE *) KIFACE_GETTER(int *aKIFACEversion
const wxChar * g_GerberPageSizeList[]
List of page sizes.
Definition: gerbview.cpp:40