KiCad PCB EDA Suite
export_gencad.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) 2016 Jean-Pierre Charras, jean-pierre.charras@ujf-grenoble.fr
5  * Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
6  * Copyright (C) 2012 Wayne Stambaugh <stambaughw@verizon.net>
7  * Copyright (C) 1992-2020 KiCad Developers, see AUTHORS.txt for contributors.
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, you may find one here:
21  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
22  * or you may search the http://www.gnu.org website for the version 2 license,
23  * or you may write to the Free Software Foundation, Inc.,
24  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25  */
26 
32 #include <build_version.h>
33 #include <board.h>
34 #include <board_design_settings.h>
36 #include <fp_shape.h>
37 #include <footprint.h>
38 #include <pad.h>
39 #include <pcb_track.h>
40 #include <confirm.h>
41 #include <core/arraydim.h>
43 #include <locale_io.h>
44 #include <macros.h>
45 #include <hash_eda.h>
46 #include <pcb_edit_frame.h>
47 #include <pcbnew_settings.h>
48 #include <pgm_base.h>
49 #include <project/project_file.h> // LAST_PATH_TYPE
50 
51 #include <wx/app.h>
52 #include <wx/filedlg.h>
53 
54 static bool CreateHeaderInfoData( FILE* aFile, PCB_EDIT_FRAME* frame );
55 static void CreateArtworksSection( FILE* aFile );
56 static void CreateTracksInfoData( FILE* aFile, BOARD* aPcb );
57 static void CreateBoardSection( FILE* aFile, BOARD* aPcb );
58 static void CreateComponentsSection( FILE* aFile, BOARD* aPcb );
59 static void CreateDevicesSection( FILE* aFile, BOARD* aPcb );
60 static void CreateRoutesSection( FILE* aFile, BOARD* aPcb );
61 static void CreateSignalsSection( FILE* aFile, BOARD* aPcb );
62 static void CreateShapesSection( FILE* aFile, BOARD* aPcb );
63 static void CreatePadsShapesSection( FILE* aFile, BOARD* aPcb );
64 static void FootprintWriteShape( FILE* File, FOOTPRINT* aFootprint, const wxString& aShapeName );
65 
66 // layer names for Gencad export
67 
68 static std::string GenCADLayerName( int aCuCount, PCB_LAYER_ID aId )
69 {
70  if( IsCopperLayer( aId ) )
71  {
72  if( aId == F_Cu )
73  return "TOP";
74  else if( aId == B_Cu )
75  return "BOTTOM";
76  else if( aId <= 14 )
77  return StrPrintf( "INNER%d", aCuCount - aId - 1 );
78  else
79  return StrPrintf( "LAYER%d", aId );
80  }
81 
82  else
83  {
84  const char* txt;
85 
86  // using a switch to clearly show mapping & catch out of bounds index.
87  switch( aId )
88  {
89  // Technicals
90  case B_Adhes: txt = "B.Adhes"; break;
91  case F_Adhes: txt = "F.Adhes"; break;
92  case B_Paste: txt = "SOLDERPASTE_BOTTOM"; break;
93  case F_Paste: txt = "SOLDERPASTE_TOP"; break;
94  case B_SilkS: txt = "SILKSCREEN_BOTTOM"; break;
95  case F_SilkS: txt = "SILKSCREEN_TOP"; break;
96  case B_Mask: txt = "SOLDERMASK_BOTTOM"; break;
97  case F_Mask: txt = "SOLDERMASK_TOP"; break;
98 
99  // Users
100  case Dwgs_User: txt = "Dwgs.User"; break;
101  case Cmts_User: txt = "Cmts.User"; break;
102  case Eco1_User: txt = "Eco1.User"; break;
103  case Eco2_User: txt = "Eco2.User"; break;
104  case Edge_Cuts: txt = "Edge.Cuts"; break;
105  case Margin: txt = "Margin"; break;
106 
107  // Footprint
108  case F_CrtYd: txt = "F_CrtYd"; break;
109  case B_CrtYd: txt = "B_CrtYd"; break;
110  case F_Fab: txt = "F_Fab"; break;
111  case B_Fab: txt = "B_Fab"; break;
112 
113  default:
114  wxASSERT_MSG( 0, wxT( "aId UNEXPECTED" ) );
115  txt = "BAD-INDEX!"; break;
116  }
117 
118  return txt;
119  }
120 }
121 
122 
123 static const PCB_LAYER_ID gc_seq[] = {
124  B_Cu,
125  In30_Cu,
126  In29_Cu,
127  In28_Cu,
128  In27_Cu,
129  In26_Cu,
130  In25_Cu,
131  In24_Cu,
132  In23_Cu,
133  In22_Cu,
134  In21_Cu,
135  In20_Cu,
136  In19_Cu,
137  In18_Cu,
138  In17_Cu,
139  In16_Cu,
140  In15_Cu,
141  In14_Cu,
142  In13_Cu,
143  In12_Cu,
144  In11_Cu,
145  In10_Cu,
146  In9_Cu,
147  In8_Cu,
148  In7_Cu,
149  In6_Cu,
150  In5_Cu,
151  In4_Cu,
152  In3_Cu,
153  In2_Cu,
154  In1_Cu,
155  F_Cu,
156 };
157 
158 
159 // flipped layer name for Gencad export (to make CAM350 imports correct)
160 static std::string GenCADLayerNameFlipped( int aCuCount, PCB_LAYER_ID aId )
161 {
162  if( 1<= aId && aId <= 14 )
163  return StrPrintf( "INNER%d", 14 - aId );
164 
165  return GenCADLayerName( aCuCount, aId );
166 }
167 
168 
169 static wxString escapeString( const wxString& aString )
170 {
171  wxString copy( aString );
172  copy.Replace( "\"", "\\\"" );
173  return copy;
174 }
175 
176 
177 static std::string fmt_mask( LSET aSet )
178 {
179  return StrPrintf( "%08x", (unsigned) ( aSet & LSET::AllCuMask() ).to_ulong() );
180 }
181 
182 // Export options
183 static bool flipBottomPads;
184 static bool uniquePins;
185 static bool individualShapes;
186 static bool storeOriginCoords;
187 
188 // These are the export origin (the auxiliary axis)
190 
191 // Association between shape names (using shapeName index) and components
192 static std::map<FOOTPRINT*, int> componentShapes;
193 static std::map<int, wxString> shapeNames;
194 
195 static const wxString getShapeName( FOOTPRINT* aFootprint )
196 {
197  static const wxString invalid( "invalid" );
198 
199  if( individualShapes )
200  return aFootprint->GetReference();
201 
202  auto itShape = componentShapes.find( aFootprint );
203  wxCHECK( itShape != componentShapes.end(), invalid );
204 
205  auto itName = shapeNames.find( itShape->second );
206  wxCHECK( itName != shapeNames.end(), invalid );
207 
208  return itName->second;
209 }
210 
211 // GerbTool chokes on units different than INCH so this is the conversion factor
212 const static double SCALE_FACTOR = 1000.0 * IU_PER_MILS;
213 
214 /* Two helper functions to calculate coordinates of footprints in gencad values
215  * (GenCAD Y axis from bottom to top)
216  */
217 static double MapXTo( int aX )
218 {
219  return (aX - GencadOffsetX) / SCALE_FACTOR;
220 }
221 
222 
223 static double MapYTo( int aY )
224 {
225  return (GencadOffsetY - aY) / SCALE_FACTOR;
226 }
227 
228 
229 /* Driver function: processing starts here */
230 void PCB_EDIT_FRAME::ExportToGenCAD( wxCommandEvent& aEvent )
231 {
232  // Build default output file name
233  wxString path = GetLastPath( LAST_PATH_GENCAD );
234 
235  if( path.IsEmpty() )
236  {
237  wxFileName brdFile = GetBoard()->GetFileName();
238  brdFile.SetExt( "cad" );
239  path = brdFile.GetFullPath();
240  }
241 
242  DIALOG_GENCAD_EXPORT_OPTIONS optionsDialog( this, path );
243 
244  if( optionsDialog.ShowModal() == wxID_CANCEL )
245  return;
246 
247  path = optionsDialog.GetFileName();
249  FILE* file = wxFopen( path, "wt" );
250 
251  if( !file )
252  {
253  DisplayError( this, wxString::Format( _( "Failed to create file '%s'." ),
254  optionsDialog.GetFileName() ) );
255  return;
256  }
257 
258  // Get options
259  flipBottomPads = optionsDialog.GetOption( FLIP_BOTTOM_PADS );
260  uniquePins = optionsDialog.GetOption( UNIQUE_PIN_NAMES );
261  individualShapes = optionsDialog.GetOption( INDIVIDUAL_SHAPES );
263 
264  // Switch the locale to standard C (needed to print floating point numbers)
265  LOCALE_IO toggle;
266 
267  // Update some board data, to ensure a reliable gencad export
269 
270  // Save the auxiliary origin for the rest of the footprint
271  wxPoint auxOrigin = m_pcb->GetDesignSettings().m_AuxOrigin;
272  GencadOffsetX = optionsDialog.GetOption( USE_AUX_ORIGIN ) ? auxOrigin.x : 0;
273  GencadOffsetY = optionsDialog.GetOption( USE_AUX_ORIGIN ) ? auxOrigin.y : 0;
274 
275  // No idea on *why* this should be needed... maybe to fix net names?
276  Compile_Ratsnest( true );
277 
278  /* Temporary modification of footprints that are flipped (i.e. on bottom
279  * layer) to convert them to non flipped footprints.
280  * This is necessary to easily export shapes to GenCAD,
281  * that are given as normal orientation (non flipped, rotation = 0))
282  * these changes will be undone later
283  */
284  BOARD* pcb = GetBoard();
285 
286  for( FOOTPRINT* footprint : pcb->Footprints() )
287  {
288  footprint->SetFlag( 0 );
289 
290  if( footprint->GetLayer() == B_Cu )
291  {
292  footprint->Flip( footprint->GetPosition(), false );
293  footprint->SetFlag( 1 );
294  }
295  }
296 
297  /* Gencad has some mandatory and some optional sections: some importer
298  * need the padstack section (which is optional) anyway. Also the
299  * order of the section *is* important */
300 
301  CreateHeaderInfoData( file, this ); // Gencad header
302  CreateBoardSection( file, pcb ); // Board perimeter
303 
304  CreatePadsShapesSection( file, pcb ); // Pads and padstacks
305  CreateArtworksSection( file ); // Empty but mandatory
306 
307  /* Gencad splits a component info in shape, component and device.
308  * We don't do any sharing (it would be difficult since each module is
309  * customizable after placement) */
310  CreateShapesSection( file, pcb );
311  CreateComponentsSection( file, pcb );
312  CreateDevicesSection( file, pcb );
313 
314  // In a similar way the netlist is split in net, track and route
315  CreateSignalsSection( file, pcb );
316  CreateTracksInfoData( file, pcb );
317  CreateRoutesSection( file, pcb );
318 
319  fclose( file );
320 
321  // Undo the footprints modifications (flipped footprints)
322  for( FOOTPRINT* footprint : pcb->Footprints() )
323  {
324  if( footprint->GetFlag() )
325  {
326  footprint->Flip( footprint->GetPosition(), false );
327  footprint->SetFlag( 0 );
328  }
329  }
330 
331  componentShapes.clear();
332  shapeNames.clear();
333 }
334 
335 
336 // Sort vias for uniqueness
337 static bool ViaSort( const PCB_VIA* aPadref, const PCB_VIA* aPadcmp )
338 {
339  if( aPadref->GetWidth() != aPadcmp->GetWidth() )
340  return aPadref->GetWidth() < aPadcmp->GetWidth();
341 
342  if( aPadref->GetDrillValue() != aPadcmp->GetDrillValue() )
343  return aPadref->GetDrillValue() < aPadcmp->GetDrillValue();
344 
345  if( aPadref->GetLayerSet() != aPadcmp->GetLayerSet() )
346  return aPadref->GetLayerSet().FmtBin().compare( aPadcmp->GetLayerSet().FmtBin() ) < 0;
347 
348  return false;
349 }
350 
351 
352 // The ARTWORKS section is empty but (officially) mandatory
353 static void CreateArtworksSection( FILE* aFile )
354 {
355  /* The artworks section is empty */
356  fputs( "$ARTWORKS\n", aFile );
357  fputs( "$ENDARTWORKS\n\n", aFile );
358 }
359 
360 
361 // Emit PADS and PADSTACKS. They are sorted and emitted uniquely.
362 // Via name is synthesized from their attributes, pads are numbered
363 static void CreatePadsShapesSection( FILE* aFile, BOARD* aPcb )
364 {
365  std::vector<PAD*> padstacks;
366  std::vector<PCB_VIA*> vias;
367  std::vector<PCB_VIA*> viastacks;
368 
369  padstacks.resize( 1 ); // We count pads from 1
370 
371  // The master layermask (i.e. the enabled layers) for padstack generation
372  LSET master_layermask = aPcb->GetDesignSettings().GetEnabledLayers();
373  int cu_count = aPcb->GetCopperLayerCount();
374 
375  fputs( "$PADS\n", aFile );
376 
377  // Enumerate and sort the pads
378 
379  std::vector<PAD*> pads = aPcb->GetPads();
380  std::sort( pads.begin(), pads.end(), []( const PAD* a, const PAD* b )
381  {
382  return PAD::Compare( a, b ) < 0;
383  } );
384 
385 
386  // The same for vias
387  for( PCB_TRACK* track : aPcb->Tracks() )
388  {
389  if( PCB_VIA* via = dyn_cast<PCB_VIA*>( track ) )
390  vias.push_back( via );
391  }
392 
393  std::sort( vias.begin(), vias.end(), ViaSort );
394  vias.erase( std::unique( vias.begin(), vias.end(), []( const PCB_VIA* a, const PCB_VIA* b )
395  {
396  return ViaSort( a, b ) == false;
397  } ),
398  vias.end() );
399 
400  // Emit vias pads
401 
402  for( PCB_VIA* via : vias )
403  {
404  viastacks.push_back( via );
405  fprintf( aFile, "PAD V%d.%d.%s ROUND %g\nCIRCLE 0 0 %g\n",
406  via->GetWidth(), via->GetDrillValue(),
407  fmt_mask( via->GetLayerSet() & master_layermask ).c_str(),
408  via->GetDrillValue() / SCALE_FACTOR,
409  via->GetWidth() / (SCALE_FACTOR * 2) );
410  }
411 
412  // Emit component pads
413  PAD* old_pad = nullptr;
414  int pad_name_number = 0;
415 
416  for( unsigned i = 0; i<pads.size(); ++i )
417  {
418  PAD* pad = pads[i];
419  const wxPoint& off = pad->GetOffset();
420 
421  pad->SetSubRatsnest( pad_name_number );
422 
423  if( old_pad && 0 == PAD::Compare( old_pad, pad ) )
424  continue; // already created
425 
426  old_pad = pad;
427 
428  pad_name_number++;
429  pad->SetSubRatsnest( pad_name_number );
430 
431  fprintf( aFile, "PAD P%d", pad->GetSubRatsnest() );
432 
433  padstacks.push_back( pad ); // Will have its own padstack later
434  int dx = pad->GetSize().x / 2;
435  int dy = pad->GetSize().y / 2;
436 
437  switch( pad->GetShape() )
438  {
439  default:
440  wxASSERT_MSG( false, "Pad type not implemented" );
442 
443  case PAD_SHAPE::CIRCLE:
444  fprintf( aFile, " ROUND %g\n",
445  pad->GetDrillSize().x / SCALE_FACTOR );
446  /* Circle is center, radius */
447  fprintf( aFile, "CIRCLE %g %g %g\n",
448  off.x / SCALE_FACTOR,
449  -off.y / SCALE_FACTOR,
450  pad->GetSize().x / (SCALE_FACTOR * 2) );
451  break;
452 
453  case PAD_SHAPE::RECT:
454  fprintf( aFile, " RECTANGULAR %g\n",
455  pad->GetDrillSize().x / SCALE_FACTOR );
456 
457  // Rectangle is begin, size *not* begin, end!
458  fprintf( aFile, "RECTANGLE %g %g %g %g\n",
459  (-dx + off.x ) / SCALE_FACTOR,
460  (-dy - off.y ) / SCALE_FACTOR,
461  dx / (SCALE_FACTOR / 2), dy / (SCALE_FACTOR / 2) );
462  break;
463 
465  case PAD_SHAPE::OVAL:
466  {
467  const wxSize& size = pad->GetSize();
468  int radius = std::min( size.x, size.y ) / 2;
469 
470  if( pad->GetShape() == PAD_SHAPE::ROUNDRECT )
471  {
472  radius = pad->GetRoundRectCornerRadius();
473  }
474 
475  int lineX = size.x / 2 - radius;
476  int lineY = size.y / 2 - radius;
477 
478  fprintf( aFile, " POLYGON %g\n", pad->GetDrillSize().x / SCALE_FACTOR );
479 
480  // bottom left arc
481  fprintf( aFile, "ARC %g %g %g %g %g %g\n",
482  ( off.x - lineX - radius ) / SCALE_FACTOR,
483  ( -off.y - lineY ) / SCALE_FACTOR, ( off.x - lineX ) / SCALE_FACTOR,
484  ( -off.y - lineY - radius ) / SCALE_FACTOR,
485  ( off.x - lineX ) / SCALE_FACTOR, ( -off.y - lineY ) / SCALE_FACTOR );
486  // bottom line
487  if( lineX > 0 )
488  {
489  fprintf( aFile, "LINE %g %g %g %g\n",
490  ( off.x - lineX ) / SCALE_FACTOR,
491  ( -off.y - lineY - radius ) / SCALE_FACTOR,
492  ( off.x + lineX ) / SCALE_FACTOR,
493  ( -off.y - lineY - radius ) / SCALE_FACTOR );
494  }
495 
496  // bottom right arc
497  fprintf( aFile, "ARC %g %g %g %g %g %g\n",
498  ( off.x + lineX ) / SCALE_FACTOR,
499  ( -off.y - lineY - radius ) / SCALE_FACTOR,
500  ( off.x + lineX + radius ) / SCALE_FACTOR,
501  ( -off.y - lineY ) / SCALE_FACTOR, ( off.x + lineX ) / SCALE_FACTOR,
502  ( -off.y - lineY ) / SCALE_FACTOR );
503 
504  // right line
505  if( lineY > 0 )
506  {
507  fprintf( aFile, "LINE %g %g %g %g\n",
508  ( off.x + lineX + radius ) / SCALE_FACTOR,
509  ( -off.y + lineY ) / SCALE_FACTOR,
510  ( off.x + lineX + radius ) / SCALE_FACTOR,
511  ( -off.y - lineY ) / SCALE_FACTOR );
512  }
513 
514  // top right arc
515  fprintf( aFile, "ARC %g %g %g %g %g %g\n",
516  ( off.x + lineX + radius ) / SCALE_FACTOR,
517  ( -off.y + lineY ) / SCALE_FACTOR, ( off.x + lineX ) / SCALE_FACTOR,
518  ( -off.y + lineY + radius ) / SCALE_FACTOR,
519  ( off.x + lineX ) / SCALE_FACTOR, ( -off.y + lineY ) / SCALE_FACTOR );
520 
521  // top line
522  if( lineX > 0 )
523  {
524  fprintf( aFile, "LINE %g %g %g %g\n"
525  , ( off.x - lineX ) / SCALE_FACTOR,
526  ( -off.y + lineY + radius ) / SCALE_FACTOR,
527  ( off.x + lineX ) / SCALE_FACTOR,
528  ( -off.y + lineY + radius ) / SCALE_FACTOR );
529  }
530 
531  // top left arc
532  fprintf( aFile, "ARC %g %g %g %g %g %g\n",
533  ( off.x - lineX ) / SCALE_FACTOR,
534  ( -off.y + lineY + radius ) / SCALE_FACTOR,
535  ( off.x - lineX - radius ) / SCALE_FACTOR,
536  ( -off.y + lineY ) / SCALE_FACTOR, ( off.x - lineX ) / SCALE_FACTOR,
537  ( -off.y + lineY ) / SCALE_FACTOR );
538 
539  // left line
540  if( lineY > 0 )
541  {
542  fprintf( aFile, "LINE %g %g %g %g\n",
543  ( off.x - lineX - radius ) / SCALE_FACTOR,
544  ( -off.y - lineY ) / SCALE_FACTOR,
545  ( off.x - lineX - radius ) / SCALE_FACTOR,
546  ( -off.y + lineY ) / SCALE_FACTOR );
547  }
548  }
549  break;
550 
552  {
553  fprintf( aFile, " POLYGON %g\n", pad->GetDrillSize().x / SCALE_FACTOR );
554 
555  int ddx = pad->GetDelta().x / 2;
556  int ddy = pad->GetDelta().y / 2;
557 
558  wxPoint poly[4];
559  poly[0] = wxPoint( -dx + ddy, dy + ddx );
560  poly[1] = wxPoint( dx - ddy, dy - ddx );
561  poly[2] = wxPoint( dx + ddy, -dy + ddx );
562  poly[3] = wxPoint( -dx - ddy, -dy - ddx );
563 
564  for( int cur = 0; cur < 4; ++cur )
565  {
566  int next = ( cur + 1 ) % 4;
567  fprintf( aFile, "LINE %g %g %g %g\n",
568  ( off.x + poly[cur].x ) / SCALE_FACTOR,
569  ( -off.y - poly[cur].y ) / SCALE_FACTOR,
570  ( off.x + poly[next].x ) / SCALE_FACTOR,
571  ( -off.y - poly[next].y ) / SCALE_FACTOR );
572  }
573  }
574  break;
575 
577  {
578  fprintf( aFile, " POLYGON %g\n", pad->GetDrillSize().x / SCALE_FACTOR );
579 
580  SHAPE_POLY_SET outline;
581  int maxError = aPcb->GetDesignSettings().m_MaxError;
582 
583  TransformRoundChamferedRectToPolygon( outline, pad->GetPosition(), pad->GetSize(),
584  pad->GetOrientation(), pad->GetRoundRectCornerRadius(),
585  pad->GetChamferRectRatio(), pad->GetChamferPositions(), 0, maxError,
586  ERROR_INSIDE );
587 
588  for( int jj = 0; jj < outline.OutlineCount(); ++jj )
589  {
590  const SHAPE_LINE_CHAIN& poly = outline.COutline( jj );
591  int pointCount = poly.PointCount();
592 
593  for( int ii = 0; ii < pointCount; ii++ )
594  {
595  int next = ( ii + 1 ) % pointCount;
596  fprintf( aFile, "LINE %g %g %g %g\n",
597  ( off.x + poly.CPoint( ii ).x ) / SCALE_FACTOR,
598  ( -off.y - poly.CPoint( ii ).y ) / SCALE_FACTOR,
599  ( off.x + poly.CPoint( next ).x ) / SCALE_FACTOR,
600  ( -off.y - poly.CPoint( next ).y ) / SCALE_FACTOR );
601  }
602  }
603 
604  break;
605  }
606 
607  case PAD_SHAPE::CUSTOM:
608  {
609  fprintf( aFile, " POLYGON %g\n", pad->GetDrillSize().x / SCALE_FACTOR );
610 
611  SHAPE_POLY_SET outline;
612  pad->MergePrimitivesAsPolygon( &outline );
613 
614  for( int jj = 0; jj < outline.OutlineCount(); ++jj )
615  {
616  const SHAPE_LINE_CHAIN& poly = outline.COutline( jj );
617  int pointCount = poly.PointCount();
618 
619  for( int ii = 0; ii < pointCount; ii++ )
620  {
621  int next = ( ii + 1 ) % pointCount;
622  fprintf( aFile, "LINE %g %g %g %g\n",
623  ( off.x + poly.CPoint( ii ).x ) / SCALE_FACTOR,
624  ( -off.y - poly.CPoint( ii ).y ) / SCALE_FACTOR,
625  ( off.x + poly.CPoint( next ).x ) / SCALE_FACTOR,
626  ( -off.y - poly.CPoint( next ).y ) / SCALE_FACTOR );
627  }
628  }
629  }
630  break;
631  }
632  }
633 
634  fputs( "\n$ENDPADS\n\n", aFile );
635 
636  // Now emit the padstacks definitions, using the combined layer masks
637  fputs( "$PADSTACKS\n", aFile );
638 
639  // Via padstacks
640  for( unsigned i = 0; i < viastacks.size(); i++ )
641  {
642  PCB_VIA* via = viastacks[i];
643 
644  LSET mask = via->GetLayerSet() & master_layermask;
645 
646  fprintf( aFile, "PADSTACK VIA%d.%d.%s %g\n",
647  via->GetWidth(), via->GetDrillValue(),
648  fmt_mask( mask ).c_str(),
649  via->GetDrillValue() / SCALE_FACTOR );
650 
651  for( LSEQ seq = mask.Seq( gc_seq, arrayDim( gc_seq ) ); seq; ++seq )
652  {
653  PCB_LAYER_ID layer = *seq;
654 
655  fprintf( aFile, "PAD V%d.%d.%s %s 0 0\n",
656  via->GetWidth(), via->GetDrillValue(),
657  fmt_mask( mask ).c_str(),
658  GenCADLayerName( cu_count, layer ).c_str()
659  );
660  }
661  }
662 
663  /* Component padstacks
664  * Older versions of CAM350 don't apply correctly the FLIP semantics for
665  * padstacks, i.e. doesn't swap the top and bottom layers... so I need to
666  * define the shape as MIRRORX and define a separate 'flipped' padstack...
667  * until it appears yet another noncompliant importer */
668  for( unsigned i = 1; i < padstacks.size(); i++ )
669  {
670  PAD* pad = padstacks[i];
671 
672  // Straight padstack
673  fprintf( aFile, "PADSTACK PAD%u %g\n", i, pad->GetDrillSize().x / SCALE_FACTOR );
674 
675  LSET pad_set = pad->GetLayerSet() & master_layermask;
676 
677  // the special gc_seq
678  for( LSEQ seq = pad_set.Seq( gc_seq, arrayDim( gc_seq ) ); seq; ++seq )
679  {
680  PCB_LAYER_ID layer = *seq;
681 
682  fprintf( aFile, "PAD P%u %s 0 0\n", i, GenCADLayerName( cu_count, layer ).c_str() );
683  }
684 
685  // Flipped padstack
686  if( flipBottomPads )
687  {
688  fprintf( aFile, "PADSTACK PAD%uF %g\n", i, pad->GetDrillSize().x / SCALE_FACTOR );
689 
690  // the normal PCB_LAYER_ID sequence is inverted from gc_seq[]
691  for( LSEQ seq = pad_set.Seq(); seq; ++seq )
692  {
693  PCB_LAYER_ID layer = *seq;
694 
695  fprintf( aFile, "PAD P%u %s 0 0\n", i, GenCADLayerNameFlipped( cu_count, layer ).c_str() );
696  }
697  }
698  }
699 
700  fputs( "$ENDPADSTACKS\n\n", aFile );
701 }
702 
703 
705 static size_t hashFootprint( const FOOTPRINT* aFootprint )
706 {
707  size_t ret = 0x11223344;
708  constexpr int flags = HASH_FLAGS::HASH_POS | HASH_FLAGS::REL_COORD
710 
711  for( BOARD_ITEM* i : aFootprint->GraphicalItems() )
712  ret += hash_fp_item( i, flags );
713 
714  for( PAD* i : aFootprint->Pads() )
715  ret += hash_fp_item( i, flags );
716 
717  return ret;
718 }
719 
720 
721 /* Creates the footprint shape list.
722  * Since module shape is customizable after the placement we cannot share them;
723  * instead we opt for the one-module-one-shape-one-component-one-device approach
724  */
725 static void CreateShapesSection( FILE* aFile, BOARD* aPcb )
726 {
727  const char* layer;
728  wxString pinname;
729  const char* mirror = "0";
730  std::map<wxString, size_t> shapes;
731 
732  fputs( "$SHAPES\n", aFile );
733 
734  for( FOOTPRINT* footprint : aPcb->Footprints() )
735  {
736  if( !individualShapes )
737  {
738  // Check if such shape has been already generated, and if so - reuse it
739  // It is necessary to compute hash (i.e. check all children objects) as
740  // certain components instances might have been modified on the board.
741  // In such case the shape will be different despite the same LIB_ID.
742  wxString shapeName = footprint->GetFPID().Format();
743 
744  auto shapeIt = shapes.find( shapeName );
745  size_t modHash = hashFootprint( footprint );
746 
747  if( shapeIt != shapes.end() )
748  {
749  if( modHash != shapeIt->second )
750  {
751  // there is an entry for this footprint, but it has a modified shape,
752  // so we need to create a new entry
753  wxString newShapeName;
754  int suffix = 0;
755 
756  // find an unused name or matching entry
757  do
758  {
759  newShapeName = wxString::Format( "%s_%d", shapeName, suffix );
760  shapeIt = shapes.find( newShapeName );
761  ++suffix;
762  }
763  while( shapeIt != shapes.end() && shapeIt->second != modHash );
764 
765  shapeName = newShapeName;
766  }
767 
768  if( shapeIt != shapes.end() && modHash == shapeIt->second )
769  {
770  // shape found, so reuse it
771  componentShapes[footprint] = modHash;
772  continue;
773  }
774  }
775 
776  // new shape
777  componentShapes[footprint] = modHash;
778  shapeNames[modHash] = shapeName;
779  shapes[shapeName] = modHash;
780  FootprintWriteShape( aFile, footprint, shapeName );
781  }
782  else // individual shape for each component
783  {
784  FootprintWriteShape( aFile, footprint, footprint->GetReference() );
785  }
786 
787  // set of already emitted pins to check for duplicates
788  std::set<wxString> pins;
789 
790  for( PAD* pad : footprint->Pads() )
791  {
792  /* Padstacks are defined using the correct layers for the pads, therefore to
793  * all pads need to be marked as TOP to use the padstack information correctly.
794  */
795  layer = "TOP";
796  pinname = pad->GetNumber();
797 
798  if( pinname.IsEmpty() )
799  pinname = wxT( "none" );
800 
801  if( uniquePins )
802  {
803  int suffix = 0;
804  wxString origPinname( pinname );
805 
806  auto it = pins.find( pinname );
807 
808  while( it != pins.end() )
809  {
810  pinname = wxString::Format( "%s_%d", origPinname, suffix );
811  ++suffix;
812  it = pins.find( pinname );
813  }
814 
815  pins.insert( pinname );
816  }
817 
818  double orient = pad->GetOrientation() - footprint->GetOrientation();
819  NORMALIZE_ANGLE_POS( orient );
820 
821  // Bottom side footprints use the flipped padstack
822  fprintf( aFile, ( flipBottomPads && footprint->GetFlag() ) ?
823  "PIN \"%s\" PAD%dF %g %g %s %g %s\n" :
824  "PIN \"%s\" PAD%d %g %g %s %g %s\n",
825  TO_UTF8( escapeString( pinname ) ), pad->GetSubRatsnest(),
826  pad->GetPos0().x / SCALE_FACTOR,
827  -pad->GetPos0().y / SCALE_FACTOR,
828  layer, orient / 10.0, mirror );
829  }
830  }
831 
832  fputs( "$ENDSHAPES\n\n", aFile );
833 }
834 
835 
836 /* Creates the section $COMPONENTS (Footprints placement)
837  * Bottom side components are difficult to handle: shapes must be mirrored or
838  * flipped, silk layers need to be handled correctly and so on. Also it seems
839  * that *no one* follows the specs...
840  */
841 static void CreateComponentsSection( FILE* aFile, BOARD* aPcb )
842 {
843  fputs( "$COMPONENTS\n", aFile );
844 
845  int cu_count = aPcb->GetCopperLayerCount();
846 
847  for( FOOTPRINT* footprint : aPcb->Footprints() )
848  {
849  const char* mirror;
850  const char* flip;
851  double fp_orient = footprint->GetOrientation();
852 
853  if( footprint->GetFlag() )
854  {
855  mirror = "MIRRORX";
856  flip = "FLIP";
857  NEGATE_AND_NORMALIZE_ANGLE_POS( fp_orient );
858  }
859  else
860  {
861  mirror = "0";
862  flip = "0";
863  }
864 
865  fprintf( aFile, "\nCOMPONENT \"%s\"\n",
866  TO_UTF8( escapeString( footprint->GetReference() ) ) );
867  fprintf( aFile, "DEVICE \"DEV_%s\"\n",
868  TO_UTF8( escapeString( getShapeName( footprint ) ) ) );
869  fprintf( aFile, "PLACE %g %g\n",
870  MapXTo( footprint->GetPosition().x ),
871  MapYTo( footprint->GetPosition().y ) );
872  fprintf( aFile, "LAYER %s\n",
873  footprint->GetFlag() ? "BOTTOM" : "TOP" );
874  fprintf( aFile, "ROTATION %g\n",
875  fp_orient / 10.0 );
876  fprintf( aFile, "SHAPE \"%s\" %s %s\n",
877  TO_UTF8( escapeString( getShapeName( footprint ) ) ),
878  mirror, flip );
879 
880  // Text on silk layer: RefDes and value (are they actually useful?)
881  for( FP_TEXT* textItem : { &footprint->Reference(), &footprint->Value() } )
882  {
883  double txt_orient = textItem->GetTextAngle();
884  std::string layer = GenCADLayerName( cu_count, footprint->GetFlag() ? B_SilkS : F_SilkS );
885 
886  fprintf( aFile, "TEXT %g %g %g %g %s %s \"%s\"",
887  textItem->GetPos0().x / SCALE_FACTOR,
888  -textItem->GetPos0().y / SCALE_FACTOR,
889  textItem->GetTextWidth() / SCALE_FACTOR,
890  txt_orient / 10.0,
891  mirror,
892  layer.c_str(),
893  TO_UTF8( escapeString( textItem->GetText() ) ) );
894 
895  // Please note, the width is approx
896  fprintf( aFile, " 0 0 %g %g\n",
897  ( textItem->GetTextWidth() * textItem->GetLength() ) / SCALE_FACTOR,
898  textItem->GetTextHeight() / SCALE_FACTOR );
899  }
900 
901  // The SHEET is a 'generic description' for referencing the component
902  fprintf( aFile, "SHEET \"RefDes: %s, Value: %s\"\n",
903  TO_UTF8( footprint->GetReference() ),
904  TO_UTF8( footprint->GetValue() ) );
905  }
906 
907  fputs( "$ENDCOMPONENTS\n\n", aFile );
908 }
909 
910 
911 /* Emit the netlist (which is actually the thing for which GenCAD is used these
912  * days!); tracks are handled later */
913 static void CreateSignalsSection( FILE* aFile, BOARD* aPcb )
914 {
915  wxString msg;
916  NETINFO_ITEM* net;
917  int NbNoConn = 1;
918 
919  fputs( "$SIGNALS\n", aFile );
920 
921  for( unsigned ii = 0; ii < aPcb->GetNetCount(); ii++ )
922  {
923  net = aPcb->FindNet( ii );
924 
925  if( net->GetNetname() == wxEmptyString ) // dummy netlist (no connection)
926  {
927  msg.Printf( "NoConnection%d", NbNoConn++ );
928  }
929 
930  if( net->GetNetCode() <= 0 ) // dummy netlist (no connection)
931  continue;
932 
933  msg = wxT( "SIGNAL \"" ) + escapeString( net->GetNetname() ) + "\"";
934 
935  fputs( TO_UTF8( msg ), aFile );
936  fputs( "\n", aFile );
937 
938  for( FOOTPRINT* footprint : aPcb->Footprints() )
939  {
940  for( PAD* pad : footprint->Pads() )
941  {
942  if( pad->GetNetCode() != net->GetNetCode() )
943  continue;
944 
945  msg.Printf( wxT( "NODE \"%s\" \"%s\"" ),
946  escapeString( footprint->GetReference() ),
947  escapeString( pad->GetNumber() ) );
948 
949  fputs( TO_UTF8( msg ), aFile );
950  fputs( "\n", aFile );
951  }
952  }
953  }
954 
955  fputs( "$ENDSIGNALS\n\n", aFile );
956 }
957 
958 
959 // Creates the header section
960 static bool CreateHeaderInfoData( FILE* aFile, PCB_EDIT_FRAME* aFrame )
961 {
962  wxString msg;
963  BOARD* board = aFrame->GetBoard();
964 
965  fputs( "$HEADER\n", aFile );
966  fputs( "GENCAD 1.4\n", aFile );
967 
968  // Please note: GenCAD syntax requires quoted strings if they can contain spaces
969  msg.Printf( wxT( "USER \"%s %s\"\n" ),
970  Pgm().App().GetAppName(),
971  GetBuildVersion() );
972  fputs( TO_UTF8( msg ), aFile );
973 
974  msg = wxT( "DRAWING \"" ) + board->GetFileName() + wxT( "\"\n" );
975  fputs( TO_UTF8( msg ), aFile );
976 
977  const TITLE_BLOCK& tb = aFrame->GetTitleBlock();
978 
979  msg = wxT( "REVISION \"" ) + tb.GetRevision() + wxT( " " ) + tb.GetDate() + wxT( "\"\n" );
980 
981  fputs( TO_UTF8( msg ), aFile );
982  fputs( "UNITS INCH\n", aFile );
983 
984  // giving 0 as the argument to Map{X,Y}To returns the scaled origin point
985  msg.Printf( wxT( "ORIGIN %g %g\n" ),
986  storeOriginCoords ? MapXTo( 0 ) : 0,
987  storeOriginCoords ? MapYTo( 0 ) : 0 );
988  fputs( TO_UTF8( msg ), aFile );
989 
990  fputs( "INTERTRACK 0\n", aFile );
991  fputs( "$ENDHEADER\n\n", aFile );
992 
993  return true;
994 }
995 
996 
997 /* Creates the section ROUTES
998  * that handles tracks, vias
999  * TODO: add zones
1000  * section:
1001  * $ROUTE
1002  * ...
1003  * $ENROUTE
1004  * Track segments must be sorted by nets
1005  */
1006 static void CreateRoutesSection( FILE* aFile, BOARD* aPcb )
1007 {
1008  int vianum = 1;
1009  int old_netcode, old_width, old_layer;
1010  LSET master_layermask = aPcb->GetDesignSettings().GetEnabledLayers();
1011 
1012  int cu_count = aPcb->GetCopperLayerCount();
1013 
1014  TRACKS tracks( aPcb->Tracks() );
1015  std::sort( tracks.begin(), tracks.end(),
1016  []( const PCB_TRACK* a, const PCB_TRACK* b )
1017  {
1018  if( a->GetNetCode() == b->GetNetCode() )
1019  {
1020  if( a->GetWidth() == b->GetWidth() )
1021  return ( a->GetLayer() < b->GetLayer() );
1022 
1023  return ( a->GetWidth() < b->GetWidth() );
1024  }
1025 
1026  return ( a->GetNetCode() < b->GetNetCode() );
1027  } );
1028 
1029  fputs( "$ROUTES\n", aFile );
1030 
1031  old_netcode = -1; old_width = -1; old_layer = -1;
1032 
1033  for( PCB_TRACK* track : tracks )
1034  {
1035  if( old_netcode != track->GetNetCode() )
1036  {
1037  old_netcode = track->GetNetCode();
1038  NETINFO_ITEM* net = track->GetNet();
1039  wxString netname;
1040 
1041  if( net && (net->GetNetname() != wxEmptyString) )
1042  netname = net->GetNetname();
1043  else
1044  netname = wxT( "_noname_" );
1045 
1046  fprintf( aFile, "ROUTE \"%s\"\n", TO_UTF8( escapeString( netname ) ) );
1047  }
1048 
1049  if( old_width != track->GetWidth() )
1050  {
1051  old_width = track->GetWidth();
1052  fprintf( aFile, "TRACK TRACK%d\n", track->GetWidth() );
1053  }
1054 
1055  if( track->Type() == PCB_TRACE_T )
1056  {
1057  if( old_layer != track->GetLayer() )
1058  {
1059  old_layer = track->GetLayer();
1060  fprintf( aFile, "LAYER %s\n",
1061  GenCADLayerName( cu_count, track->GetLayer() ).c_str() );
1062  }
1063 
1064  fprintf( aFile, "LINE %g %g %g %g\n",
1065  MapXTo( track->GetStart().x ), MapYTo( track->GetStart().y ),
1066  MapXTo( track->GetEnd().x ), MapYTo( track->GetEnd().y ) );
1067  }
1068 
1069  if( track->Type() == PCB_VIA_T )
1070  {
1071  const PCB_VIA* via = static_cast<const PCB_VIA*>(track);
1072 
1073  LSET vset = via->GetLayerSet() & master_layermask;
1074 
1075  fprintf( aFile, "VIA VIA%d.%d.%s %g %g ALL %g via%d\n",
1076  via->GetWidth(), via->GetDrillValue(),
1077  fmt_mask( vset ).c_str(),
1078  MapXTo( via->GetStart().x ), MapYTo( via->GetStart().y ),
1079  via->GetDrillValue() / SCALE_FACTOR, vianum++ );
1080  }
1081  }
1082 
1083  fputs( "$ENDROUTES\n\n", aFile );
1084 }
1085 
1086 
1087 /* Creates the section $DEVICES
1088  * This is a list of footprints properties
1089  * ( Shapes are in section $SHAPE )
1090  */
1091 static void CreateDevicesSection( FILE* aFile, BOARD* aPcb )
1092 {
1093  std::set<wxString> emitted;
1094  fputs( "$DEVICES\n", aFile );
1095 
1096  for( const auto& componentShape : componentShapes )
1097  {
1098  const wxString& shapeName = shapeNames[componentShape.second];
1099  bool newDevice;
1100  std::tie( std::ignore, newDevice ) = emitted.insert( shapeName );
1101 
1102  if( !newDevice ) // do not repeat device definitions
1103  continue;
1104 
1105  const FOOTPRINT* footprint = componentShape.first;
1106 
1107  fprintf( aFile, "\nDEVICE \"DEV_%s\"\n", TO_UTF8( escapeString( shapeName ) ) );
1108 
1109  fprintf( aFile, "PART \"%s\"\n",
1110  TO_UTF8( escapeString( footprint->GetValue() ) ) );
1111 
1112  fprintf( aFile, "PACKAGE \"%s\"\n",
1113  TO_UTF8( escapeString( footprint->GetFPID().Format() ) ) );
1114  }
1115 
1116  fputs( "$ENDDEVICES\n\n", aFile );
1117 }
1118 
1119 
1120 /* Creates the section $BOARD.
1121  * We output here only the board perimeter
1122  */
1123 static void CreateBoardSection( FILE* aFile, BOARD* aPcb )
1124 {
1125  fputs( "$BOARD\n", aFile );
1126 
1127  // Extract the board edges
1128  for( BOARD_ITEM* drawing : aPcb->Drawings() )
1129  {
1130  if( drawing->Type() == PCB_SHAPE_T )
1131  {
1132  PCB_SHAPE* drawseg = static_cast<PCB_SHAPE*>( drawing );
1133 
1134  if( drawseg->GetLayer() == Edge_Cuts )
1135  {
1136  // XXX GenCAD supports arc boundaries but I've seen nothing that reads them
1137  fprintf( aFile, "LINE %g %g %g %g\n",
1138  MapXTo( drawseg->GetStart().x ), MapYTo( drawseg->GetStart().y ),
1139  MapXTo( drawseg->GetEnd().x ), MapYTo( drawseg->GetEnd().y ) );
1140  }
1141  }
1142  }
1143 
1144  fputs( "$ENDBOARD\n\n", aFile );
1145 }
1146 
1147 
1148 /* Creates the section "$TRACKS"
1149  * This sections give the list of widths (tools) used in tracks and vias
1150  * format:
1151  * $TRACK
1152  * TRACK <name> <width>
1153  * $ENDTRACK
1154  *
1155  * Each tool name is build like this: "TRACK" + track width.
1156  * For instance for a width = 120 : name = "TRACK120".
1157  */
1158 static void CreateTracksInfoData( FILE* aFile, BOARD* aPcb )
1159 {
1160  // Find thickness used for traces
1161 
1162  std::set<int> trackinfo;
1163 
1164  for( PCB_TRACK* track : aPcb->Tracks() )
1165  trackinfo.insert( track->GetWidth() );
1166 
1167  // Write data
1168  fputs( "$TRACKS\n", aFile );
1169 
1170  for( int size : trackinfo )
1171  fprintf( aFile, "TRACK TRACK%d %g\n", size, size / SCALE_FACTOR );
1172 
1173  fputs( "$ENDTRACKS\n\n", aFile );
1174 }
1175 
1176 
1177 /* Creates the shape of a footprint (section SHAPE)
1178  * The shape is always given "normal" (Orient 0, not mirrored)
1179  * It's almost guaranteed that the silk layer will be imported wrong but
1180  * the shape also contains the pads!
1181  */
1182 static void FootprintWriteShape( FILE* aFile, FOOTPRINT* aFootprint, const wxString& aShapeName )
1183 {
1184  FP_SHAPE* shape;
1185 
1186  /* creates header: */
1187  fprintf( aFile, "\nSHAPE \"%s\"\n", TO_UTF8( escapeString( aShapeName ) ) );
1188 
1189  if( aFootprint->GetAttributes() & FP_THROUGH_HOLE )
1190  fprintf( aFile, "INSERT TH\n" );
1191  else
1192  fprintf( aFile, "INSERT SMD\n" );
1193 
1194  // Silk outline; wildly interpreted by various importers:
1195  // CAM350 read it right but only closed shapes
1196  // ProntoPlace double-flip it (at least the pads are correct)
1197  // GerberTool usually get it right...
1198  for( BOARD_ITEM* PtStruct : aFootprint->GraphicalItems() )
1199  {
1200  switch( PtStruct->Type() )
1201  {
1202  case PCB_FP_TEXT_T:
1203 
1204  // If we wanted to export text, this is not the correct section
1205  break;
1206 
1207  case PCB_FP_SHAPE_T:
1208  shape = (FP_SHAPE*) PtStruct;
1209  if( shape->GetLayer() == F_SilkS || shape->GetLayer() == B_SilkS )
1210  {
1211  switch( shape->GetShape() )
1212  {
1213  case SHAPE_T::SEGMENT:
1214  fprintf( aFile, "LINE %g %g %g %g\n",
1215  shape->GetStart0().x / SCALE_FACTOR,
1216  -shape->GetStart0().y / SCALE_FACTOR,
1217  shape->GetEnd0().x / SCALE_FACTOR,
1218  -shape->GetEnd0().y / SCALE_FACTOR );
1219  break;
1220 
1221  case SHAPE_T::RECT:
1222  {
1223  fprintf( aFile, "LINE %g %g %g %g\n",
1224  shape->GetStart0().x / SCALE_FACTOR,
1225  -shape->GetStart0().y / SCALE_FACTOR,
1226  shape->GetEnd0().x / SCALE_FACTOR,
1227  -shape->GetStart0().y / SCALE_FACTOR );
1228  fprintf( aFile, "LINE %g %g %g %g\n",
1229  shape->GetEnd0().x / SCALE_FACTOR,
1230  -shape->GetStart0().y / SCALE_FACTOR,
1231  shape->GetEnd0().x / SCALE_FACTOR,
1232  -shape->GetEnd0().y / SCALE_FACTOR );
1233  fprintf( aFile, "LINE %g %g %g %g\n",
1234  shape->GetEnd0().x / SCALE_FACTOR,
1235  -shape->GetEnd0().y / SCALE_FACTOR,
1236  shape->GetStart0().x / SCALE_FACTOR,
1237  -shape->GetEnd0().y / SCALE_FACTOR );
1238  fprintf( aFile, "LINE %g %g %g %g\n",
1239  shape->GetStart0().x / SCALE_FACTOR,
1240  -shape->GetEnd0().y / SCALE_FACTOR,
1241  shape->GetStart0().x / SCALE_FACTOR,
1242  -shape->GetStart0().y / SCALE_FACTOR );
1243  }
1244  break;
1245 
1246  case SHAPE_T::CIRCLE:
1247  {
1248  int radius = KiROUND( GetLineLength( shape->GetEnd0(), shape->GetStart0() ) );
1249 
1250  fprintf( aFile, "CIRCLE %g %g %g\n",
1251  shape->GetStart0().x / SCALE_FACTOR,
1252  -shape->GetStart0().y / SCALE_FACTOR,
1253  radius / SCALE_FACTOR );
1254  break;
1255  }
1256 
1257  case SHAPE_T::ARC:
1258  {
1259  int arcendx, arcendy;
1260  arcendx = shape->GetEnd0().x - shape->GetStart0().x;
1261  arcendy = shape->GetEnd0().y - shape->GetStart0().y;
1262  RotatePoint( &arcendx, &arcendy, -shape->GetAngle() );
1263  arcendx += shape->GetStart0().x;
1264  arcendy += shape->GetStart0().y;
1265 
1266  fprintf( aFile, "ARC %g %g %g %g %g %g\n",
1267  shape->GetEnd0().x / SCALE_FACTOR,
1268  -shape->GetEnd0().y / SCALE_FACTOR,
1269  arcendx / SCALE_FACTOR,
1270  -arcendy / SCALE_FACTOR,
1271  shape->GetStart0().x / SCALE_FACTOR,
1272  -shape->GetStart0().y / SCALE_FACTOR );
1273  break;
1274  }
1275 
1276  case SHAPE_T::POLY:
1277  // Not exported (TODO)
1278  break;
1279 
1280  default:
1281  wxFAIL_MSG( wxString::Format( "Type Edge Module %d invalid.",
1282  PtStruct->Type() ) );
1283  break;
1284  }
1285  }
1286  break;
1287 
1288  default:
1289  break;
1290  }
1291  }
1292 }
bool GetOption(GENCAD_EXPORT_OPT aOption) const
Return all export settings.
static const wxString getShapeName(FOOTPRINT *aFootprint)
static LSET AllCuMask(int aCuLayerCount=MAX_CU_LAYERS)
Return a mask holding the requested number of Cu PCB_LAYER_IDs.
Definition: lset.cpp:750
static void CreateBoardSection(FILE *aFile, BOARD *aPcb)
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:271
CITER next(CITER it)
Definition: ptree.cpp:126
NETINFO_ITEM * FindNet(int aNetcode) const
Search for a net with the given netcode.
Definition: board.cpp:1344
Arcs (with rounded ends)
static void CreateShapesSection(FILE *aFile, BOARD *aPcb)
double GetLineLength(const wxPoint &aPointA, const wxPoint &aPointB)
Return the length of a line segment defined by aPointA and aPointB.
Definition: trigo.h:222
class FP_TEXT, text in a footprint
Definition: typeinfo.h:92
polygon (not yet used for tracks, but could be in microwave apps)
const wxString & GetValue() const
Definition: footprint.h:452
static std::map< FOOTPRINT *, int > componentShapes
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition: locale_io.h:40
const wxPoint & GetEnd() const
Return the ending point of the graphic.
Definition: pcb_shape.h:134
void Compile_Ratsnest(bool aDisplayStatus)
Create the entire board ratsnest.
Definition: ratsnest.cpp:40
This file is part of the common library.
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:80
use coordinates relative to the parent object
Definition: hash_eda.h:43
SHAPE_T GetShape() const
Definition: pcb_shape.h:110
static void CreateRoutesSection(FILE *aFile, BOARD *aPcb)
static int Compare(const PAD *padref, const PAD *padcmp)
Compare two pads and return 0 if they are equal.
Definition: pad.cpp:1023
static void CreateSignalsSection(FILE *aFile, BOARD *aPcb)
static double MapYTo(int aY)
usual segment : line with rounded ends
static bool uniquePins
const wxPoint & GetStart0() const
Definition: fp_shape.h:112
virtual LSET GetLayerSet() const override
Return a std::bitset of all layers on which the item physically resides.
Definition: pcb_track.cpp:402
void ExportToGenCAD(wxCommandEvent &event)
Create a file in GenCAD 1.4 format from the current board.
class FP_SHAPE, a footprint edge
Definition: typeinfo.h:93
#define KI_FALLTHROUGH
The KI_FALLTHROUGH macro is to be used when switch statement cases should purposely fallthrough from ...
Definition: macros.h:83
int GetWidth() const
Definition: pcb_track.h:102
static wxString escapeString(const wxString &aString)
static int GencadOffsetY
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:229
void NORMALIZE_ANGLE_POS(T &Angle)
Definition: trigo.h:290
static bool flipBottomPads
int PointCount() const
Return the number of points (vertices) in this line chain.
LSEQ Seq(const PCB_LAYER_ID *aWishListSequence, unsigned aCount) const
Return an LSEQ from the union of this LSET and a desired sequence.
Definition: lset.cpp:411
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:106
const wxString & GetFileName() const
Definition: board.h:228
Hold the information shown in the lower right corner of a plot, printout, or editing view.
Definition: title_block.h:40
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Definition: board.cpp:589
static void CreateTracksInfoData(FILE *aFile, BOARD *aPcb)
wxString GetLastPath(LAST_PATH_TYPE aType)
Get the last path for a particular type.
static void CreateArtworksSection(FILE *aFile)
class PCB_TRACK, a track segment (segment on a copper layer)
Definition: typeinfo.h:95
PADS & Pads()
Definition: footprint.h:168
void vset(double *v, double x, double y, double z)
Definition: trackball.cpp:82
This file contains miscellaneous commonly used macros and functions.
void TransformRoundChamferedRectToPolygon(SHAPE_POLY_SET &aCornerBuffer, const wxPoint &aPosition, const wxSize &aSize, double aRotation, int aCornerRadius, double aChamferRatio, int aChamferCorners, int aInflate, int aError, ERROR_LOC aErrorLoc)
Convert a rectangle with rounded corners and/or chamfered corners to a polygon.
int StrPrintf(std::string *result, const char *format,...)
This is like sprintf() but the output is appended to a std::string instead of to a character array.
Definition: richio.cpp:78
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: macros.h:96
static size_t hashFootprint(const FOOTPRINT *aFootprint)
Compute hashes for footprints without taking into account their position, rotation or layer.
const VECTOR2I & CPoint(int aIndex) const
Return a reference to a given point in the line chain.
static std::string GenCADLayerNameFlipped(int aCuCount, PCB_LAYER_ID aId)
static void CreateComponentsSection(FILE *aFile, BOARD *aPcb)
static bool individualShapes
LSET is a set of PCB_LAYER_IDs.
Definition: layer_ids.h:502
unsigned GetNetCount() const
Definition: board.h:715
wxString GetBuildVersion()
Get the full KiCad version string.
const wxString & GetRevision() const
Definition: title_block.h:86
static void CreatePadsShapesSection(FILE *aFile, BOARD *aPcb)
Represent a set of closed polygons.
const std::vector< PAD * > GetPads() const
Return a reference to a list of all the pads.
Definition: board.cpp:1914
FOOTPRINTS & Footprints()
Definition: board.h:233
static const PCB_LAYER_ID gc_seq[]
const wxPoint & GetStart() const
Return the starting point of the graphic.
Definition: pcb_shape.h:124
size_t hash_fp_item(const EDA_ITEM *aItem, int aFlags)
Calculate hash of an EDA_ITEM.
Definition: hash_eda.cpp:49
const wxString & GetNetname() const
Definition: netinfo.h:119
const wxString & GetReference() const
Definition: footprint.h:430
#define _(s)
constexpr std::size_t arrayDim(T const (&)[N]) noexcept
Returns # of elements in an array.
Definition: arraydim.h:31
static void FootprintWriteShape(FILE *File, FOOTPRINT *aFootprint, const wxString &aShapeName)
DRAWINGS & GraphicalItems()
Definition: footprint.h:171
const LIB_ID & GetFPID() const
Definition: footprint.h:194
static bool storeOriginCoords
LSEQ is a sequence (and therefore also a set) of PCB_LAYER_IDs.
Definition: layer_ids.h:463
UTF8 Format() const
Definition: lib_id.cpp:116
bool IsCopperLayer(LAYER_NUM aLayerId)
Tests whether a layer is a copper layer.
Definition: layer_ids.h:787
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
static std::string GenCADLayerName(int aCuCount, PCB_LAYER_ID aId)
void NEGATE_AND_NORMALIZE_ANGLE_POS(T &Angle)
Definition: trigo.h:362
const wxString & GetDate() const
Definition: title_block.h:76
const TITLE_BLOCK & GetTitleBlock() const override
Handle the data for a net.
Definition: netinfo.h:64
static bool ViaSort(const PCB_VIA *aPadref, const PCB_VIA *aPadcmp)
see class PGM_BASE
int GetAttributes() const
Definition: footprint.h:235
std::string FmtBin() const
Return a binary string showing contents of this LSEQ.
Definition: lset.cpp:297
static bool CreateHeaderInfoData(FILE *aFile, PCB_EDIT_FRAME *frame)
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:190
static void CreateDevicesSection(FILE *aFile, BOARD *aPcb)
LSET GetEnabledLayers() const
Return a bit-mask of all the layers that are enabled.
Represent a polyline (an zero-thickness chain of connected line segments).
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:64
Definition: layer_ids.h:70
int GetCopperLayerCount() const
Definition: board.cpp:454
double GetAngle() const
Definition: pcb_shape.h:107
The main frame for Pcbnew.
const wxPoint & GetEnd0() const
Definition: fp_shape.h:115
#define IU_PER_MILS
Definition: plotter.cpp:136
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: util.h:73
static const double SCALE_FACTOR
EDA_RECT ComputeBoundingBox(bool aBoardEdgesOnly=false) const
Calculate the bounding box containing all board items (or board edge segments).
Definition: board.cpp:1105
static int GencadOffsetX
int GetDrillValue() const
Function GetDrillValue "calculates" the drill value for vias (m-Drill if > 0, or default drill value ...
Definition: pcb_track.cpp:193
class PCB_VIA, a via (like a track segment on a copper layer)
Definition: typeinfo.h:96
static std::string fmt_mask(LSET aSet)
BOARD * GetBoard() const
segment with non rounded ends
Definition: pad.h:57
static std::map< int, wxString > shapeNames
class PCB_SHAPE, a segment not on copper layers
Definition: typeinfo.h:90
virtual PCB_LAYER_ID GetLayer() const
Return the primary layer this item is on.
Definition: board_item.h:171
DRAWINGS & Drawings()
Definition: board.h:236
wxPoint m_AuxOrigin
origin for plot exports
TRACKS & Tracks()
Definition: board.h:230
void SetLastPath(LAST_PATH_TYPE aType, const wxString &aLastPath)
Set the path of the last file successfully read.
static double MapXTo(int aX)
int GetNetCode() const
Definition: netinfo.h:113