KiCad PCB EDA Suite
export_to_pcbnew.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2007-2014 Jean-Pierre Charras jp.charras at wanadoo.fr
5  * Copyright (C) 1992-2017 KiCad Developers, see change_log.txt for contributors.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
30 #include <vector>
31 
32 #include <fctsys.h>
33 #include <common.h>
34 #include <confirm.h>
35 #include <macros.h>
36 #include <trigo.h>
37 #include <gerbview.h>
38 #include <gerbview_frame.h>
41 #include <select_layers_to_pcb.h>
42 #include <build_version.h>
44 
45 
46 // Imported function
47 extern const wxString GetPCBDefaultLayerName( LAYER_NUM aLayerNumber );
48 
49 #define TO_PCB_UNIT( x ) ( x / IU_PER_MM)
50 
51 /* A helper class to export a Gerber set of files to Pcbnew
52  */
54 {
55 private:
56  GERBVIEW_FRAME* m_gerbview_frame; // the main gerber frame
57  wxString m_pcb_file_name; // BOARD file to write to
58  FILE* m_fp; // the board file
60  std::vector<wxPoint> m_vias_coordinates; // list of already generated vias,
61  // used to export only once a via
62  // having a given coordinate
63 public:
64  GBR_TO_PCB_EXPORTER( GERBVIEW_FRAME* aFrame, const wxString& aFileName );
66 
71  bool ExportPcb( LAYER_NUM* aLayerLookUpTable, int aCopperLayers );
72 
73 private:
80  void export_non_copper_item( GERBER_DRAW_ITEM* aGbrItem, LAYER_NUM aLayer );
81 
88  void export_copper_item( GERBER_DRAW_ITEM* aGbrItem, LAYER_NUM aLayer );
89 
96 
103  void export_segline_copper_item( GERBER_DRAW_ITEM* aGbrItem, LAYER_NUM aLayer );
104 
112  void export_segarc_copper_item( GERBER_DRAW_ITEM* aGbrItem, LAYER_NUM aLayer );
113 
119  void writePcbLineItem( bool aIsArc, wxPoint& aStart, wxPoint& aEnd,
120  int aWidth, LAYER_NUM aLayer, double aAngle = 0 );
121 
127  void writeCopperLineItem( wxPoint& aStart, wxPoint& aEnd,
128  int aWidth, LAYER_NUM aLayer );
129 
134  void writePcbHeader( LAYER_NUM* aLayerLookUpTable );
135 };
136 
137 
138 GBR_TO_PCB_EXPORTER::GBR_TO_PCB_EXPORTER( GERBVIEW_FRAME* aFrame, const wxString& aFileName )
139 {
140  m_gerbview_frame = aFrame;
141  m_pcb_file_name = aFileName;
142  m_fp = NULL;
144 }
145 
146 
148 {
149 }
150 
151 
152 /* Export data in Pcbnew format
153  * remember Pcbnew uses a Y reversed axis, so we must negate all Y coordinates
154  */
155 void GERBVIEW_FRAME::ExportDataInPcbnewFormat( wxCommandEvent& event )
156 {
157  int layercount = 0;
158 
160 
161  // Count the Gerber layers which are actually currently used
162  for( LAYER_NUM ii = 0; ii < (LAYER_NUM)images->ImagesMaxCount(); ++ii )
163  {
164  if( images->GetGbrImage( ii ) )
165  layercount++;
166  }
167 
168  if( layercount == 0 )
169  {
170  DisplayInfoMessage( this,
171  _( "None of the Gerber layers contain any data" ) );
172  return;
173  }
174 
175  wxString fileName;
176  wxString path = m_mruPath;
177 
178  wxFileDialog filedlg( this, _( "Board File Name" ),
179  path, fileName, PcbFileWildcard(),
180  wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
181 
182  if( filedlg.ShowModal() == wxID_CANCEL )
183  return;
184 
185  fileName = filedlg.GetPath();
186 
187  /* Install a dialog frame to choose the mapping
188  * between gerber layers and Pcbnew layers
189  */
190  LAYERS_MAP_DIALOG* layerdlg = new LAYERS_MAP_DIALOG( this );
191  int ok = layerdlg->ShowModal();
192  layerdlg->Destroy();
193 
194  if( ok != wxID_OK )
195  return;
196 
197  m_mruPath = wxFileName( fileName ).GetPath();
198 
199  GBR_TO_PCB_EXPORTER gbr_exporter( this, fileName );
200 
201  gbr_exporter.ExportPcb( layerdlg->GetLayersLookUpTable(), layerdlg->GetCopperLayersCount() );
202 }
203 
204 
205 bool GBR_TO_PCB_EXPORTER::ExportPcb( LAYER_NUM* aLayerLookUpTable, int aCopperLayers )
206 {
207  LOCALE_IO toggle; // toggles on, then off, the C locale.
208 
209  m_fp = wxFopen( m_pcb_file_name, wxT( "wt" ) );
210 
211  if( m_fp == NULL )
212  {
213  wxString msg;
214  msg.Printf( _( "Cannot create file '%s'" ), GetChars( m_pcb_file_name ) );
216  return false;
217  }
218 
219  m_pcbCopperLayersCount = aCopperLayers;
220 
221  writePcbHeader( aLayerLookUpTable );
222 
223  // create an image of gerber data
224  // First: non copper layers:
225  const int pcbCopperLayerMax = 31;
227 
228  for( unsigned layer = 0; layer < images->ImagesMaxCount(); ++layer )
229  {
230  GERBER_FILE_IMAGE* gerber = images->GetGbrImage( layer );
231 
232  if( gerber == NULL ) // Graphic layer not yet used
233  continue;
234 
235  LAYER_NUM pcb_layer_number = aLayerLookUpTable[layer];
236 
237  if( !IsPcbLayer( pcb_layer_number ) )
238  continue;
239 
240  if( pcb_layer_number <= pcbCopperLayerMax ) // copper layer
241  continue;
242 
243  GERBER_DRAW_ITEM* gerb_item = gerber->GetItemsList();
244 
245  for( ; gerb_item; gerb_item = gerb_item->Next() )
246  export_non_copper_item( gerb_item, pcb_layer_number );
247  }
248 
249  // Copper layers
250  for( unsigned layer = 0; layer < images->ImagesMaxCount(); ++layer )
251  {
252  GERBER_FILE_IMAGE* gerber = images->GetGbrImage( layer );
253 
254  if( gerber == NULL ) // Graphic layer not yet used
255  continue;
256 
257  LAYER_NUM pcb_layer_number = aLayerLookUpTable[layer];
258 
259  if( pcb_layer_number < 0 || pcb_layer_number > pcbCopperLayerMax )
260  continue;
261 
262  GERBER_DRAW_ITEM* gerb_item = gerber->GetItemsList();
263 
264  for( ; gerb_item; gerb_item = gerb_item->Next() )
265  export_copper_item( gerb_item, pcb_layer_number );
266  }
267 
268  fprintf( m_fp, ")\n" );
269 
270  fclose( m_fp );
271  m_fp = NULL;
272  return true;
273 }
274 
275 
277 {
278  bool isArc = false;
279 
280  double angle = 0;
281  wxPoint seg_start = aGbrItem->m_Start;
282  wxPoint seg_end = aGbrItem->m_End;
283 
284  if( aGbrItem->m_Shape == GBR_ARC )
285  {
286  double a = atan2( (double) ( aGbrItem->m_Start.y - aGbrItem->m_ArcCentre.y),
287  (double) ( aGbrItem->m_Start.x - aGbrItem->m_ArcCentre.x ) );
288  double b = atan2( (double) ( aGbrItem->m_End.y - aGbrItem->m_ArcCentre.y ),
289  (double) ( aGbrItem->m_End.x - aGbrItem->m_ArcCentre.x ) );
290 
291  isArc = true;
292  angle = RAD2DEG(b - a);
293  seg_start = aGbrItem->m_ArcCentre;
294 
295  // Ensure arc orientation is CCW
296  if( angle < 0 )
297  angle += 360.0;
298  }
299 
300  // Reverse Y axis:
301  seg_start.y = -seg_start.y;
302  seg_end.y = -seg_end.y;
303  writePcbLineItem( isArc, seg_start, seg_end, aGbrItem->m_Size.x, aLayer, angle );
304 }
305 
306 
308 {
309  switch( aGbrItem->m_Shape )
310  {
311  case GBR_SPOT_CIRCLE:
312  case GBR_SPOT_RECT:
313  case GBR_SPOT_OVAL:
314  // replace spots with vias when possible
315  export_flashed_copper_item( aGbrItem );
316  break;
317 
318  case GBR_ARC:
319  export_segarc_copper_item( aGbrItem, aLayer );
320  break;
321 
322  default:
323  export_segline_copper_item( aGbrItem, aLayer );
324  break;
325  }
326 }
327 
328 
330 {
331  wxPoint seg_start, seg_end;
332 
333  seg_start = aGbrItem->m_Start;
334  seg_end = aGbrItem->m_End;
335 
336  // Reverse Y axis:
337  seg_start.y = -seg_start.y;
338  seg_end.y = -seg_end.y;
339 
340  writeCopperLineItem( seg_start, seg_end, aGbrItem->m_Size.x, aLayer );
341 }
342 
343 
345  int aWidth, LAYER_NUM aLayer )
346 {
347  fprintf( m_fp, "(segment (start %s %s) (end %s %s) (width %s) (layer %s) (net 0))\n",
348  Double2Str( TO_PCB_UNIT(aStart.x) ).c_str(),
349  Double2Str( TO_PCB_UNIT(aStart.y) ).c_str(),
350  Double2Str( TO_PCB_UNIT(aEnd.x) ).c_str(),
351  Double2Str( TO_PCB_UNIT(aEnd.y) ).c_str(),
352  Double2Str( TO_PCB_UNIT( aWidth ) ).c_str(),
353  TO_UTF8( GetPCBDefaultLayerName( aLayer ) ) );
354 }
355 
356 
358 {
359  double a = atan2( (double) ( aGbrItem->m_Start.y - aGbrItem->m_ArcCentre.y ),
360  (double) ( aGbrItem->m_Start.x - aGbrItem->m_ArcCentre.x ) );
361  double b = atan2( (double) ( aGbrItem->m_End.y - aGbrItem->m_ArcCentre.y ),
362  (double) ( aGbrItem->m_End.x - aGbrItem->m_ArcCentre.x ) );
363 
364  wxPoint start = aGbrItem->m_Start;
365  wxPoint end = aGbrItem->m_End;
366 
367  /* Because Pcbnew does not know arcs in tracks,
368  * approximate arc by segments (SEG_COUNT__CIRCLE segment per 360 deg)
369  * The arc is drawn anticlockwise from the start point to the end point.
370  */
371  #define SEG_COUNT_CIRCLE 16
372  #define DELTA_ANGLE 2 * M_PI / SEG_COUNT_CIRCLE
373 
374  // calculate the number of segments from a to b.
375  // we want CNT_PER_360 segments fo a circle
376  if( a > b )
377  b += 2 * M_PI;
378 
379  wxPoint curr_start = start;
380  wxPoint seg_start, seg_end;
381 
382  int ii = 1;
383 
384  for( double rot = a; rot < (b - DELTA_ANGLE); rot += DELTA_ANGLE, ii++ )
385  {
386  seg_start = curr_start;
387  wxPoint curr_end = start;
388  RotatePoint( &curr_end, aGbrItem->m_ArcCentre,
389  -RAD2DECIDEG( DELTA_ANGLE * ii ) );
390  seg_end = curr_end;
391  // Reverse Y axis:
392  seg_start.y = -seg_start.y;
393  seg_end.y = -seg_end.y;
394  writeCopperLineItem( seg_start, seg_end, aGbrItem->m_Size.x, aLayer );
395  curr_start = curr_end;
396  }
397 
398  if( end != curr_start )
399  {
400  seg_start = curr_start;
401  seg_end = end;
402  // Reverse Y axis:
403  seg_start.y = -seg_start.y;
404  seg_end.y = -seg_end.y;
405  writeCopperLineItem( seg_start, seg_end, aGbrItem->m_Size.x, aLayer );
406  }
407 }
408 
409 
410 /*
411  * creates a via from a flashed gerber item.
412  * Flashed items are usually pads or vias, so we try to export all of them
413  * using vias
414  */
416 {
417  // First, explore already created vias, before creating a new via
418  for( unsigned ii = 0; ii < m_vias_coordinates.size(); ii++ )
419  {
420  if( m_vias_coordinates[ii] == aGbrItem->m_Start ) // Already created
421  return;
422  }
423 
424  m_vias_coordinates.push_back( aGbrItem->m_Start );
425 
426  wxPoint via_pos = aGbrItem->m_Start;
427  int width = (aGbrItem->m_Size.x + aGbrItem->m_Size.y) / 2;
428  // Reverse Y axis:
429  via_pos.y = -via_pos.y;
430 
431  // Layers are Front to Back
432  fprintf( m_fp, " (via (at %s %s) (size %s)",
433  Double2Str( TO_PCB_UNIT(via_pos.x) ).c_str(),
434  Double2Str( TO_PCB_UNIT(via_pos.y) ).c_str(),
435  Double2Str( TO_PCB_UNIT( width ) ).c_str() );
436 
437  fprintf( m_fp, " (layers %s %s))\n",
440 }
441 
443 {
444  fprintf( m_fp, "(kicad_pcb (version 4) (host Gerbview \"%s\")\n\n",
445  TO_UTF8( GetBuildVersion() ) );
446 
447  // Write layers section
448  fprintf( m_fp, " (layers \n" );
449 
450  for( int ii = 0; ii < m_pcbCopperLayersCount; ii++ )
451  {
452  int id = ii;
453 
454  if( ii == m_pcbCopperLayersCount-1)
455  id = B_Cu;
456 
457  fprintf( m_fp, " (%d %s signal)\n", id, TO_UTF8( GetPCBDefaultLayerName( id ) ) );
458  }
459 
460  for( int ii = B_Adhes; ii < PCB_LAYER_ID_COUNT; ii++ )
461  {
462  fprintf( m_fp, " (%d %s user)\n", ii, TO_UTF8( GetPCBDefaultLayerName( ii ) ) );
463  }
464 
465  fprintf( m_fp, " )\n\n" );
466 }
467 
468 
469 void GBR_TO_PCB_EXPORTER::writePcbLineItem( bool aIsArc, wxPoint& aStart, wxPoint& aEnd,
470  int aWidth, LAYER_NUM aLayer, double aAngle )
471 {
472  if( aIsArc && ( aAngle == 360.0 || aAngle == 0 ) )
473  {
474  fprintf( m_fp, "(gr_circle (center %s %s) (end %s %s)(layer %s) (width %s))\n",
475  Double2Str( TO_PCB_UNIT(aStart.x) ).c_str(),
476  Double2Str( TO_PCB_UNIT(aStart.y) ).c_str(),
477  Double2Str( TO_PCB_UNIT(aEnd.x) ).c_str(),
478  Double2Str( TO_PCB_UNIT(aEnd.y) ).c_str(),
479  TO_UTF8( GetPCBDefaultLayerName( aLayer ) ),
480  Double2Str( TO_PCB_UNIT( aWidth ) ).c_str()
481  );
482  }
483  else if( aIsArc )
484  {
485  fprintf( m_fp, "(gr_arc (start %s %s) (end %s %s) (angle %s)(layer %s) (width %s))\n",
486  Double2Str( TO_PCB_UNIT(aStart.x) ).c_str(),
487  Double2Str( TO_PCB_UNIT(aStart.y) ).c_str(),
488  Double2Str( TO_PCB_UNIT(aEnd.x) ).c_str(),
489  Double2Str( TO_PCB_UNIT(aEnd.y) ).c_str(),
490  Double2Str( aAngle ).c_str(),
491  TO_UTF8( GetPCBDefaultLayerName( aLayer ) ),
492  Double2Str( TO_PCB_UNIT( aWidth ) ).c_str()
493  );
494  }
495  else
496  {
497  fprintf( m_fp, "(gr_line (start %s %s) (end %s %s)(layer %s) (width %s))\n",
498  Double2Str( TO_PCB_UNIT(aStart.x) ).c_str(),
499  Double2Str( TO_PCB_UNIT(aStart.y) ).c_str(),
500  Double2Str( TO_PCB_UNIT(aEnd.x) ).c_str(),
501  Double2Str( TO_PCB_UNIT(aEnd.y) ).c_str(),
502  TO_UTF8( GetPCBDefaultLayerName( aLayer ) ),
503  Double2Str( TO_PCB_UNIT( aWidth ) ).c_str()
504  );
505  }
506 }
void writePcbLineItem(bool aIsArc, wxPoint &aStart, wxPoint &aEnd, int aWidth, LAYER_NUM aLayer, double aAngle=0)
function writePcbLineItem basic write function to write a DRAWSEGMENT item or a TRACK item to the boa...
wxString m_mruPath
Most recently used path.
Definition: wxstruct.h:165
#define TO_PCB_UNIT(x)
Class LOCALE_IO is a class that can be instantiated within a scope in which you are expecting excepti...
Definition: common.h:165
This file is part of the common library.
GBR_TO_PCB_EXPORTER(GERBVIEW_FRAME *aFrame, const wxString &aFileName)
double RAD2DEG(double rad)
Definition: trigo.h:200
Class GERBER_FILE_IMAGE holds the Image data and parameters for one gerber file and layer parameters ...
LAYER_NUM * GetLayersLookUpTable()
void export_segline_copper_item(GERBER_DRAW_ITEM *aGbrItem, LAYER_NUM aLayer)
Function export_segline_copper_item write a track (not via) to the board file.
double RAD2DECIDEG(double rad)
Definition: trigo.h:204
void export_non_copper_item(GERBER_DRAW_ITEM *aGbrItem, LAYER_NUM aLayer)
Function export_non_copper_item write a non copper line or arc to the board file. ...
std::string Double2Str(double aValue)
Helper function Double2Str to print a float number without using scientific notation and no trailing ...
Definition: base_units.cpp:60
#define DELTA_ANGLE
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:317
GERBER_FILE_IMAGE_LIST is a helper class to handle a list of GERBER_FILE_IMAGE files which are loaded...
static int GetCopperLayersCount()
This file contains miscellaneous commonly used macros and functions.
#define TO_UTF8(wxstring)
Macro TO_UTF8 converts a wxString to a UTF8 encoded C string for all wxWidgets build modes...
Definition: macros.h:47
GERBER_FILE_IMAGE * GetGbrImage(int aIdx)
wxString GetBuildVersion()
Function GetBuildVersion Return the build version string.
bool ExportPcb(LAYER_NUM *aLayerLookUpTable, int aCopperLayers)
Function ExportPcb saves a board from a set of Gerber images.
void export_flashed_copper_item(GERBER_DRAW_ITEM *aGbrItem)
Function export_flashed_copper_item write a via to the board file (always uses a via through)...
std::vector< wxPoint > m_vias_coordinates
GERBER_DRAW_ITEM * Next() const
GERBVIEW_FRAME * m_gerbview_frame
The common library.
void writeCopperLineItem(wxPoint &aStart, wxPoint &aEnd, int aWidth, LAYER_NUM aLayer)
function writeCopperLineItem basic write function to write a a TRACK item to the board file...
GBR_LAYOUT * GetGerberLayout() const
void DisplayInfoMessage(wxWindow *aParent, const wxString &aMessage, const wxString aExtraInfo)
Function DisplayInfoMessage displays an informational message box with aMessage.
Definition: confirm.cpp:104
int LAYER_NUM
Type LAYER_NUM can be replaced with int and removed.
void export_copper_item(GERBER_DRAW_ITEM *aGbrItem, LAYER_NUM aLayer)
Function export_copper_item write a track or via) to the board file.
void ExportDataInPcbnewFormat(wxCommandEvent &event)
static const wxChar * GetChars(const wxString &s)
Function GetChars returns a wxChar* to the actual wxChar* data within a wxString, and is helpful for ...
Definition: macros.h:92
GERBER_DRAW_ITEM * GetItemsList()
Function GetItemsList.
GERBER_FILE_IMAGE_LIST * GetImagesList() const
void export_segarc_copper_item(GERBER_DRAW_ITEM *aGbrItem, LAYER_NUM aLayer)
Function export_segarc_copper_item write a set of tracks (arcs are approximated by track segments) to...
static DIRECTION_45::AngleType angle(const VECTOR2I &a, const VECTOR2I &b)
const wxString GetPCBDefaultLayerName(LAYER_NUM aLayerNumber)
wxString PcbFileWildcard()
The common library.
bool IsPcbLayer(LAYER_NUM aLayer)
Function IsPcbLayer tests whether a layer is a valid layer for pcbnew.
void DisplayError(wxWindow *parent, const wxString &text, int displaytime)
Function DisplayError displays an error or warning message box with aMessage.
Definition: confirm.cpp:73
void writePcbHeader(LAYER_NUM *aLayerLookUpTable)
function writePcbHeader Write a very basic header to the board file