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