KiCad PCB EDA Suite
kicad_plugin.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) 2012 CERN
5  * Copyright (C) 1992-2018 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
25 #include <fctsys.h>
26 #include <kicad_string.h>
27 #include <common.h>
28 #include <build_version.h> // LEGACY_BOARD_FILE_VERSION
29 #include <macros.h>
31 #include <base_units.h>
32 #include <trace_helpers.h>
33 
34 #include <class_board.h>
35 #include <class_module.h>
36 #include <class_pcb_text.h>
37 #include <class_dimension.h>
38 #include <class_track.h>
39 #include <class_zone.h>
40 #include <class_drawsegment.h>
41 #include <class_pcb_target.h>
42 #include <class_edge_mod.h>
43 #include <pcb_plot_params.h>
44 #include <zones.h>
45 #include <kicad_plugin.h>
46 #include <pcb_parser.h>
47 
48 #include <wx/dir.h>
49 #include <wx/filename.h>
50 #include <wx/wfstream.h>
51 #include <boost/ptr_container/ptr_map.hpp>
52 #include <memory.h>
53 #include <connectivity_data.h>
54 
55 using namespace PCB_KEYS_T;
56 
57 #define FMT_IU BOARD_ITEM::FormatInternalUnits
58 #define FMT_ANGLE BOARD_ITEM::FormatAngle
59 
60 
62 void filterNetClass( const BOARD& aBoard, NETCLASS& aNetClass )
63 {
64  auto connectivity = aBoard.GetConnectivity();
65 
66  for( NETCLASS::iterator it = aNetClass.begin(); it != aNetClass.end(); )
67  {
68  NETINFO_ITEM* netinfo = aBoard.FindNet( *it );
69 
70  if( netinfo && connectivity->GetNodeCount( netinfo->GetNet() ) <= 0 ) // hopefully there are no nets with negative
71  aNetClass.Remove( it++ ); // node count, but you never know..
72  else
73  ++it;
74  }
75 }
76 
87 {
88  wxFileName m_file_name;
89  std::unique_ptr<MODULE> m_module;
90 
91 public:
92  FP_CACHE_ITEM( MODULE* aModule, const wxFileName& aFileName );
93 
94  wxString GetName() const { return m_file_name.GetDirs().Last(); }
95  wxFileName GetFileName() const { return m_file_name; }
96 
97  MODULE* GetModule() const { return m_module.get(); }
98 };
99 
100 
101 FP_CACHE_ITEM::FP_CACHE_ITEM( MODULE* aModule, const wxFileName& aFileName ) :
102  m_module( aModule )
103 {
104  m_file_name = aFileName;
105 }
106 
107 
108 typedef boost::ptr_map< wxString, FP_CACHE_ITEM > MODULE_MAP;
109 typedef MODULE_MAP::iterator MODULE_ITER;
110 typedef MODULE_MAP::const_iterator MODULE_CITER;
111 
112 
113 class FP_CACHE
114 {
116  wxFileName m_lib_path;
118 
119  bool m_cache_dirty; // Stored separately because it's expensive to check
120  // m_cache_timestamp against all the files.
121  long long m_cache_timestamp; // A hash of the timestamps for all the footprint
122  // files.
123 
124 public:
125  FP_CACHE( PCB_IO* aOwner, const wxString& aLibraryPath );
126 
127  wxString GetPath() const { return m_lib_path.GetPath(); }
128  bool IsWritable() const { return m_lib_path.IsOk() && m_lib_path.IsDirWritable(); }
129  MODULE_MAP& GetModules() { return m_modules; }
130 
131  // Most all functions in this class throw IO_ERROR exceptions. There are no
132  // error codes nor user interface calls from here, nor in any PLUGIN.
133  // Catch these exceptions higher up please.
134 
141  void Save( MODULE* aModule = NULL );
142 
143  void Load();
144 
145  void Remove( const wxString& aFootprintName );
146 
153  long long GetTimestamp();
154 
159  bool IsModified();
160 
173  bool IsPath( const wxString& aPath ) const;
174 };
175 
176 
177 FP_CACHE::FP_CACHE( PCB_IO* aOwner, const wxString& aLibraryPath )
178 {
179  m_owner = aOwner;
180  m_lib_path.SetPath( aLibraryPath );
181  m_cache_timestamp = 0;
182  m_cache_dirty = true;
183 }
184 
185 
186 void FP_CACHE::Save( MODULE* aModule )
187 {
188  m_cache_timestamp = 0;
189 
190  if( !m_lib_path.DirExists() && !m_lib_path.Mkdir() )
191  {
192  THROW_IO_ERROR( wxString::Format( _( "Cannot create footprint library path \"%s\"" ),
193  m_lib_path.GetPath().GetData() ) );
194  }
195 
196  if( !m_lib_path.IsDirWritable() )
197  {
198  THROW_IO_ERROR( wxString::Format( _( "Footprint library path \"%s\" is read only" ),
199  GetChars( m_lib_path.GetPath() ) ) );
200  }
201 
202  for( MODULE_ITER it = m_modules.begin(); it != m_modules.end(); ++it )
203  {
204  if( aModule && aModule != it->second->GetModule() )
205  continue;
206 
207  wxFileName fn = it->second->GetFileName();
208 
209  wxString tempFileName =
210 #ifdef USE_TMP_FILE
211  fn.CreateTempFileName( fn.GetPath() );
212 #else
213  fn.GetFullPath();
214 #endif
215  // Allow file output stream to go out of scope to close the file stream before
216  // renaming the file.
217  {
218  wxLogTrace( traceKicadPcbPlugin, wxT( "Creating temporary library file %s" ),
219  GetChars( tempFileName ) );
220 
221  FILE_OUTPUTFORMATTER formatter( tempFileName );
222 
223  m_owner->SetOutputFormatter( &formatter );
224  m_owner->Format( (BOARD_ITEM*) it->second->GetModule() );
225  }
226 
227 #ifdef USE_TMP_FILE
228  wxRemove( fn.GetFullPath() ); // it is not an error if this does not exist
229 
230  // Even on linux you can see an _intermittent_ error when calling wxRename(),
231  // and it is fully inexplicable. See if this dodges the error.
232  wxMilliSleep( 250L );
233 
234  if( !wxRenameFile( tempFileName, fn.GetFullPath() ) )
235  {
236  wxString msg = wxString::Format(
237  _( "Cannot rename temporary file \"%s\" to footprint library file \"%s\"" ),
238  GetChars( tempFileName ),
239  GetChars( fn.GetFullPath() )
240  );
241  THROW_IO_ERROR( msg );
242  }
243 #endif
244  m_cache_timestamp += fn.GetModificationTime().GetValue().GetValue();
245  }
246 
247  m_cache_timestamp += m_lib_path.GetModificationTime().GetValue().GetValue();
248 
249  // If we've saved the full cache, we clear the dirty flag.
250  if( !aModule )
251  m_cache_dirty = false;
252 }
253 
254 
256 {
257  wxDir dir( m_lib_path.GetPath() );
258 
259  if( !dir.IsOpened() )
260  {
261  m_cache_timestamp = 0;
262  m_cache_dirty = false;
263 
264  wxString msg = wxString::Format(
265  _( "Footprint library path \"%s\" does not exist" ),
266  GetChars( m_lib_path.GetPath() )
267  );
268 
269  THROW_IO_ERROR( msg );
270  }
271  else
272  {
273  m_cache_timestamp = m_lib_path.GetModificationTime().GetValue().GetValue();
274  m_cache_dirty = false;
275  }
276 
277  wxString fpFileName;
278  wxString wildcard = wxT( "*." ) + KiCadFootprintFileExtension;
279 
280  if( dir.GetFirst( &fpFileName, wildcard, wxDIR_FILES ) )
281  {
282  wxString cacheError;
283 
284  do
285  {
286  // prepend the libpath into fullPath
287  wxFileName fullPath( m_lib_path.GetPath(), fpFileName );
288 
289  // Queue I/O errors so only files that fail to parse don't get loaded.
290  try
291  {
292  FILE_LINE_READER reader( fullPath.GetFullPath() );
293 
294  m_owner->m_parser->SetLineReader( &reader );
295 
296  MODULE* footprint = (MODULE*) m_owner->m_parser->Parse();
297 
298  // The footprint name is the file name without the extension.
299  wxString fpName = fullPath.GetName();
300 
301  footprint->SetFPID( LIB_ID( fpName ) );
302  m_modules.insert( fpName, new FP_CACHE_ITEM( footprint, fullPath ) );
303 
304  m_cache_timestamp += fullPath.GetModificationTime().GetValue().GetValue();
305  }
306  catch( const IO_ERROR& ioe )
307  {
308  if( !cacheError.IsEmpty() )
309  cacheError += "\n\n";
310 
311  cacheError += ioe.What();
312  }
313  } while( dir.GetNext( &fpFileName ) );
314 
315  if( !cacheError.IsEmpty() )
316  THROW_IO_ERROR( cacheError );
317  }
318 }
319 
320 
321 void FP_CACHE::Remove( const wxString& aFootprintName )
322 {
323  MODULE_CITER it = m_modules.find( aFootprintName );
324 
325  if( it == m_modules.end() )
326  {
327  wxString msg = wxString::Format(
328  _( "library \"%s\" has no footprint \"%s\" to delete" ),
329  GetChars( m_lib_path.GetPath() ),
330  GetChars( aFootprintName )
331  );
332  THROW_IO_ERROR( msg );
333  }
334 
335  // Remove the module from the cache and delete the module file from the library.
336  wxString fullPath = it->second->GetFileName().GetFullPath();
337  m_modules.erase( aFootprintName );
338  wxRemoveFile( fullPath );
339 }
340 
341 
342 bool FP_CACHE::IsPath( const wxString& aPath ) const
343 {
344  // Converts path separators to native path separators
345  wxFileName newPath;
346  newPath.AssignDir( aPath );
347 
348  return m_lib_path == newPath;
349 }
350 
351 
353 {
354  if( m_cache_dirty )
355  return true;
356  else
357  return GetTimestamp() != m_cache_timestamp;
358 }
359 
360 
362 {
363  // Avoid expensive GetModificationTime checks if we already know we're dirty
364  if( m_cache_dirty )
365  return wxDateTime::Now().GetValue().GetValue();
366 
367  long long files_timestamp = 0;
368 
369  if( m_lib_path.DirExists() )
370  {
371  files_timestamp = m_lib_path.GetModificationTime().GetValue().GetValue();
372 
373  for( MODULE_CITER it = m_modules.begin(); it != m_modules.end(); ++it )
374  {
375  wxFileName moduleFile = it->second->GetFileName();
376  if( moduleFile.FileExists() )
377  files_timestamp += moduleFile.GetModificationTime().GetValue().GetValue();
378  }
379  }
380 
381  // If the new timestamp doesn't match the cache timestamp, then save ourselves the
382  // expensive calls next time
383  if( m_cache_timestamp != files_timestamp )
384  m_cache_dirty = true;
385 
386  return files_timestamp;
387 }
388 
389 
390 void PCB_IO::Save( const wxString& aFileName, BOARD* aBoard, const PROPERTIES* aProperties )
391 {
392  LOCALE_IO toggle; // toggles on, then off, the C locale.
393 
394  init( aProperties );
395 
396  m_board = aBoard; // after init()
397 
398  // Prepare net mapping that assures that net codes saved in a file are consecutive integers
399  m_mapping->SetBoard( aBoard );
400 
401  FILE_OUTPUTFORMATTER formatter( aFileName );
402 
403  m_out = &formatter; // no ownership
404 
405  m_out->Print( 0, "(kicad_pcb (version %d) (host pcbnew %s)\n", SEXPR_BOARD_FILE_VERSION,
406  formatter.Quotew( GetBuildVersion() ).c_str() );
407 
408  Format( aBoard, 1 );
409 
410  m_out->Print( 0, ")\n" );
411 }
412 
413 
414 BOARD_ITEM* PCB_IO::Parse( const wxString& aClipboardSourceInput )
415 {
416  std::string input = TO_UTF8( aClipboardSourceInput );
417 
418  STRING_LINE_READER reader( input, wxT( "clipboard" ) );
419 
420  m_parser->SetLineReader( &reader );
421 
422  try
423  {
424  return m_parser->Parse();
425  }
426  catch( const PARSE_ERROR& parse_error )
427  {
428  if( m_parser->IsTooRecent() )
429  throw FUTURE_FORMAT_ERROR( parse_error, m_parser->GetRequiredVersion() );
430  else
431  throw;
432  }
433 }
434 
435 
436 void PCB_IO::Format( BOARD_ITEM* aItem, int aNestLevel ) const
437 {
438  LOCALE_IO toggle; // public API function, perform anything convenient for caller
439 
440  switch( aItem->Type() )
441  {
442  case PCB_T:
443  format( static_cast<BOARD*>( aItem ), aNestLevel );
444  break;
445 
446  case PCB_DIMENSION_T:
447  format( static_cast<DIMENSION*>( aItem ), aNestLevel );
448  break;
449 
450  case PCB_LINE_T:
451  format( static_cast<DRAWSEGMENT*>( aItem ), aNestLevel );
452  break;
453 
454  case PCB_MODULE_EDGE_T:
455  format( static_cast<EDGE_MODULE*>( aItem ), aNestLevel );
456  break;
457 
458  case PCB_TARGET_T:
459  format( static_cast<PCB_TARGET*>( aItem ), aNestLevel );
460  break;
461 
462  case PCB_MODULE_T:
463  format( static_cast<MODULE*>( aItem ), aNestLevel );
464  break;
465 
466  case PCB_PAD_T:
467  format( static_cast<D_PAD*>( aItem ), aNestLevel );
468  break;
469 
470  case PCB_TEXT_T:
471  format( static_cast<TEXTE_PCB*>( aItem ), aNestLevel );
472  break;
473 
474  case PCB_MODULE_TEXT_T:
475  format( static_cast<TEXTE_MODULE*>( aItem ), aNestLevel );
476  break;
477 
478  case PCB_TRACE_T:
479  case PCB_VIA_T:
480  format( static_cast<TRACK*>( aItem ), aNestLevel );
481  break;
482 
483  case PCB_ZONE_AREA_T:
484  format( static_cast<ZONE_CONTAINER*>( aItem ), aNestLevel );
485  break;
486 
487  default:
488  wxFAIL_MSG( wxT( "Cannot format item " ) + aItem->GetClass() );
489  }
490 }
491 
492 
493 void PCB_IO::formatLayer( const BOARD_ITEM* aItem ) const
494 {
495  if( m_ctl & CTL_STD_LAYER_NAMES )
496  {
497  PCB_LAYER_ID layer = aItem->GetLayer();
498 
499  // English layer names should never need quoting.
500  m_out->Print( 0, " (layer %s)", TO_UTF8( BOARD::GetStandardLayerName( layer ) ) );
501  }
502  else
503  m_out->Print( 0, " (layer %s)", m_out->Quotew( aItem->GetLayerName() ).c_str() );
504 }
505 
506 void PCB_IO::formatSetup( BOARD* aBoard, int aNestLevel ) const
507 {
508  const BOARD_DESIGN_SETTINGS& dsnSettings = aBoard->GetDesignSettings();
509 
510  // Setup
511  m_out->Print( aNestLevel, "(setup\n" );
512 
513  // Save current default track width, for compatibility with older Pcbnew version;
514  m_out->Print( aNestLevel+1, "(last_trace_width %s)\n",
515  FMT_IU( dsnSettings.GetCurrentTrackWidth() ).c_str() );
516 
517  // Save custom tracks width list (the first is not saved here: this is the netclass value
518  for( unsigned ii = 1; ii < dsnSettings.m_TrackWidthList.size(); ii++ )
519  m_out->Print( aNestLevel+1, "(user_trace_width %s)\n",
520  FMT_IU( dsnSettings.m_TrackWidthList[ii] ).c_str() );
521 
522  m_out->Print( aNestLevel+1, "(trace_clearance %s)\n",
523  FMT_IU( dsnSettings.GetDefault()->GetClearance() ).c_str() );
524 
525  // ZONE_SETTINGS
526  m_out->Print( aNestLevel+1, "(zone_clearance %s)\n",
527  FMT_IU( aBoard->GetZoneSettings().m_ZoneClearance ).c_str() );
528  m_out->Print( aNestLevel+1, "(zone_45_only %s)\n",
529  aBoard->GetZoneSettings().m_Zone_45_Only ? "yes" : "no" );
530 
531  m_out->Print( aNestLevel+1, "(trace_min %s)\n",
532  FMT_IU( dsnSettings.m_TrackMinWidth ).c_str() );
533 
534  m_out->Print( aNestLevel+1, "(segment_width %s)\n",
535  FMT_IU( dsnSettings.m_DrawSegmentWidth ).c_str() );
536  m_out->Print( aNestLevel+1, "(edge_width %s)\n",
537  FMT_IU( dsnSettings.m_EdgeSegmentWidth ).c_str() );
538 
539  // Save current default via size, for compatibility with older Pcbnew version;
540  m_out->Print( aNestLevel+1, "(via_size %s)\n",
541  FMT_IU( dsnSettings.GetDefault()->GetViaDiameter() ).c_str() );
542  m_out->Print( aNestLevel+1, "(via_drill %s)\n",
543  FMT_IU( dsnSettings.GetDefault()->GetViaDrill() ).c_str() );
544  m_out->Print( aNestLevel+1, "(via_min_size %s)\n",
545  FMT_IU( dsnSettings.m_ViasMinSize ).c_str() );
546  m_out->Print( aNestLevel+1, "(via_min_drill %s)\n",
547  FMT_IU( dsnSettings.m_ViasMinDrill ).c_str() );
548 
549  // Save custom vias diameters list (the first is not saved here: this is
550  // the netclass value
551  for( unsigned ii = 1; ii < dsnSettings.m_ViasDimensionsList.size(); ii++ )
552  m_out->Print( aNestLevel+1, "(user_via %s %s)\n",
553  FMT_IU( dsnSettings.m_ViasDimensionsList[ii].m_Diameter ).c_str(),
554  FMT_IU( dsnSettings.m_ViasDimensionsList[ii].m_Drill ).c_str() );
555 
556  // for old versions compatibility:
557  if( dsnSettings.m_BlindBuriedViaAllowed )
558  m_out->Print( aNestLevel+1, "(blind_buried_vias_allowed yes)\n" );
559 
560  m_out->Print( aNestLevel+1, "(uvia_size %s)\n",
561  FMT_IU( dsnSettings.GetDefault()->GetuViaDiameter() ).c_str() );
562  m_out->Print( aNestLevel+1, "(uvia_drill %s)\n",
563  FMT_IU( dsnSettings.GetDefault()->GetuViaDrill() ).c_str() );
564  m_out->Print( aNestLevel+1, "(uvias_allowed %s)\n",
565  ( dsnSettings.m_MicroViasAllowed ) ? "yes" : "no" );
566  m_out->Print( aNestLevel+1, "(uvia_min_size %s)\n",
567  FMT_IU( dsnSettings.m_MicroViasMinSize ).c_str() );
568  m_out->Print( aNestLevel+1, "(uvia_min_drill %s)\n",
569  FMT_IU( dsnSettings.m_MicroViasMinDrill ).c_str() );
570 
571  m_out->Print( aNestLevel+1, "(pcb_text_width %s)\n",
572  FMT_IU( dsnSettings.m_PcbTextWidth ).c_str() );
573  m_out->Print( aNestLevel+1, "(pcb_text_size %s %s)\n",
574  FMT_IU( dsnSettings.m_PcbTextSize.x ).c_str(),
575  FMT_IU( dsnSettings.m_PcbTextSize.y ).c_str() );
576 
577  m_out->Print( aNestLevel+1, "(mod_edge_width %s)\n",
578  FMT_IU( dsnSettings.m_ModuleSegmentWidth ).c_str() );
579  m_out->Print( aNestLevel+1, "(mod_text_size %s %s)\n",
580  FMT_IU( dsnSettings.m_ModuleTextSize.x ).c_str(),
581  FMT_IU( dsnSettings.m_ModuleTextSize.y ).c_str() );
582  m_out->Print( aNestLevel+1, "(mod_text_width %s)\n",
583  FMT_IU( dsnSettings.m_ModuleTextWidth ).c_str() );
584 
585  m_out->Print( aNestLevel+1, "(pad_size %s %s)\n",
586  FMT_IU( dsnSettings.m_Pad_Master.GetSize().x ).c_str(),
587  FMT_IU( dsnSettings.m_Pad_Master.GetSize().y ).c_str() );
588  m_out->Print( aNestLevel+1, "(pad_drill %s)\n",
589  FMT_IU( dsnSettings.m_Pad_Master.GetDrillSize().x ).c_str() );
590 
591  m_out->Print( aNestLevel+1, "(pad_to_mask_clearance %s)\n",
592  FMT_IU( dsnSettings.m_SolderMaskMargin ).c_str() );
593 
594  if( dsnSettings.m_SolderMaskMinWidth )
595  m_out->Print( aNestLevel+1, "(solder_mask_min_width %s)\n",
596  FMT_IU( dsnSettings.m_SolderMaskMinWidth ).c_str() );
597 
598  if( dsnSettings.m_SolderPasteMargin != 0 )
599  m_out->Print( aNestLevel+1, "(pad_to_paste_clearance %s)\n",
600  FMT_IU( dsnSettings.m_SolderPasteMargin ).c_str() );
601 
602  if( dsnSettings.m_SolderPasteMarginRatio != 0 )
603  m_out->Print( aNestLevel+1, "(pad_to_paste_clearance_ratio %s)\n",
604  Double2Str( dsnSettings.m_SolderPasteMarginRatio ).c_str() );
605 
606  m_out->Print( aNestLevel+1, "(aux_axis_origin %s %s)\n",
607  FMT_IU( aBoard->GetAuxOrigin().x ).c_str(),
608  FMT_IU( aBoard->GetAuxOrigin().y ).c_str() );
609 
610  if( aBoard->GetGridOrigin().x || aBoard->GetGridOrigin().y )
611  m_out->Print( aNestLevel+1, "(grid_origin %s %s)\n",
612  FMT_IU( aBoard->GetGridOrigin().x ).c_str(),
613  FMT_IU( aBoard->GetGridOrigin().y ).c_str() );
614 
615  m_out->Print( aNestLevel+1, "(visible_elements %X)\n",
616  dsnSettings.GetVisibleElements() );
617 
618  aBoard->GetPlotOptions().Format( m_out, aNestLevel+1 );
619 
620  m_out->Print( aNestLevel, ")\n\n" );
621 }
622 
623 
624 void PCB_IO::formatGeneral( BOARD* aBoard, int aNestLevel ) const
625 {
626  const BOARD_DESIGN_SETTINGS& dsnSettings = aBoard->GetDesignSettings();
627 
628  m_out->Print( 0, "\n" );
629  m_out->Print( aNestLevel, "(general\n" );
630  // Write Bounding box info
631  m_out->Print( aNestLevel+1, "(thickness %s)\n",
632  FMT_IU( dsnSettings.GetBoardThickness() ).c_str() );
633 
634  m_out->Print( aNestLevel+1, "(drawings %d)\n", aBoard->Drawings().Size() );
635  m_out->Print( aNestLevel+1, "(tracks %d)\n", aBoard->GetNumSegmTrack() );
636  m_out->Print( aNestLevel+1, "(zones %d)\n", aBoard->GetNumSegmZone() );
637  m_out->Print( aNestLevel+1, "(modules %d)\n", aBoard->m_Modules.GetCount() );
638  m_out->Print( aNestLevel+1, "(nets %d)\n", m_mapping->GetSize() );
639  m_out->Print( aNestLevel, ")\n\n" );
640 
641  aBoard->GetPageSettings().Format( m_out, aNestLevel, m_ctl );
642  aBoard->GetTitleBlock().Format( m_out, aNestLevel, m_ctl );
643 }
644 
645 
646 void PCB_IO::formatBoardLayers( BOARD* aBoard, int aNestLevel ) const
647 {
648  m_out->Print( aNestLevel, "(layers\n" );
649 
650  // Save only the used copper layers from front to back.
651  LSET visible_layers = aBoard->GetVisibleLayers();
652 
653  for( LSEQ cu = aBoard->GetEnabledLayers().CuStack(); cu; ++cu )
654  {
655  PCB_LAYER_ID layer = *cu;
656 
657  m_out->Print( aNestLevel+1, "(%d %s %s", layer,
658  m_out->Quotew( aBoard->GetLayerName( layer ) ).c_str(),
659  LAYER::ShowType( aBoard->GetLayerType( layer ) ) );
660 
661  if( !visible_layers[layer] )
662  m_out->Print( 0, " hide" );
663 
664  m_out->Print( 0, ")\n" );
665  }
666 
667  // Save used non-copper layers in the order they are defined.
668  // desired sequence for non Cu BOARD layers.
669  static const PCB_LAYER_ID non_cu[] =
670  {
671  B_Adhes, // 32
672  F_Adhes,
673  B_Paste,
674  F_Paste,
675  B_SilkS,
676  F_SilkS,
677  B_Mask,
678  F_Mask,
679  Dwgs_User,
680  Cmts_User,
681  Eco1_User,
682  Eco2_User,
683  Edge_Cuts,
684  Margin,
685  B_CrtYd,
686  F_CrtYd,
687  B_Fab,
688  F_Fab
689  };
690 
691  for( LSEQ seq = aBoard->GetEnabledLayers().Seq( non_cu, DIM( non_cu ) ); seq; ++seq )
692  {
693  PCB_LAYER_ID layer = *seq;
694 
695  m_out->Print( aNestLevel+1, "(%d %s user", layer,
696  m_out->Quotew( aBoard->GetLayerName( layer ) ).c_str() );
697 
698  if( !visible_layers[layer] )
699  m_out->Print( 0, " hide" );
700 
701  m_out->Print( 0, ")\n" );
702  }
703 
704  m_out->Print( aNestLevel, ")\n\n" );
705 }
706 
707 
708 void PCB_IO::formatNetInformation( BOARD* aBoard, int aNestLevel ) const
709 {
710  const BOARD_DESIGN_SETTINGS& dsnSettings = aBoard->GetDesignSettings();
711  for( NETINFO_MAPPING::iterator net = m_mapping->begin(), netEnd = m_mapping->end();
712  net != netEnd; ++net )
713  {
714  m_out->Print( aNestLevel, "(net %d %s)\n",
715  m_mapping->Translate( net->GetNet() ),
716  m_out->Quotew( net->GetNetname() ).c_str() );
717  }
718 
719  m_out->Print( 0, "\n" );
720 
721  // Save the default net class first.
722  NETCLASS defaultNC = *dsnSettings.GetDefault();
723  filterNetClass( *aBoard, defaultNC ); // Remove empty nets (from a copy of a netclass)
724  defaultNC.Format( m_out, aNestLevel, m_ctl );
725 
726  // Save the rest of the net classes alphabetically.
727  for( NETCLASSES::const_iterator it = dsnSettings.m_NetClasses.begin();
728  it != dsnSettings.m_NetClasses.end();
729  ++it )
730  {
731  NETCLASS netclass = *it->second;
732  filterNetClass( *aBoard, netclass ); // Remove empty nets (from a copy of a netclass)
733  netclass.Format( m_out, aNestLevel, m_ctl );
734  }
735 }
736 
737 
738 void PCB_IO::formatHeader( BOARD* aBoard, int aNestLevel ) const
739 {
740  formatGeneral( aBoard, aNestLevel );
741  // Layers.
742  formatBoardLayers( aBoard, aNestLevel );
743  // Setup
744  formatSetup( aBoard, aNestLevel );
745  // Save net codes and names
746  formatNetInformation( aBoard, aNestLevel );
747 }
748 
749 void PCB_IO::format( BOARD* aBoard, int aNestLevel ) const
750 {
751  formatHeader( aBoard, aNestLevel );
752 
753  // Save the modules.
754  for( MODULE* module = aBoard->m_Modules; module; module = module->Next() )
755  {
756  Format( module, aNestLevel );
757  m_out->Print( 0, "\n" );
758  }
759 
760  // Save the graphical items on the board (not owned by a module)
761  for( auto item : aBoard->Drawings() )
762  Format( item, aNestLevel );
763 
764  if( aBoard->Drawings().Size() )
765  m_out->Print( 0, "\n" );
766 
767  // Do not save MARKER_PCBs, they can be regenerated easily.
768 
769  // Save the tracks and vias.
770  for( TRACK* track = aBoard->m_Track; track; track = track->Next() )
771  Format( track, aNestLevel );
772 
773  if( aBoard->m_Track.GetCount() )
774  m_out->Print( 0, "\n" );
775 
778 
779  // Save the polygon (which are the newer technology) zones.
780  for( int i = 0; i < aBoard->GetAreaCount(); ++i )
781  Format( aBoard->GetArea( i ), aNestLevel );
782 }
783 
784 
785 void PCB_IO::format( DIMENSION* aDimension, int aNestLevel ) const
786 {
787  m_out->Print( aNestLevel, "(dimension %s (width %s)",
788  FMT_IU( aDimension->GetValue() ).c_str(),
789  FMT_IU( aDimension->GetWidth() ).c_str() );
790 
791  formatLayer( aDimension );
792 
793  if( aDimension->GetTimeStamp() )
794  m_out->Print( 0, " (tstamp %lX)", (unsigned long)aDimension->GetTimeStamp() );
795 
796  m_out->Print( 0, "\n" );
797 
798  Format( &aDimension->Text(), aNestLevel+1 );
799 
800  m_out->Print( aNestLevel+1, "(feature1 (pts (xy %s %s) (xy %s %s)))\n",
801  FMT_IU( aDimension->m_featureLineDO.x ).c_str(),
802  FMT_IU( aDimension->m_featureLineDO.y ).c_str(),
803  FMT_IU( aDimension->m_featureLineDF.x ).c_str(),
804  FMT_IU( aDimension->m_featureLineDF.y ).c_str() );
805 
806  m_out->Print( aNestLevel+1, "(feature2 (pts (xy %s %s) (xy %s %s)))\n",
807  FMT_IU( aDimension->m_featureLineGO.x ).c_str(),
808  FMT_IU( aDimension->m_featureLineGO.y ).c_str(),
809  FMT_IU( aDimension->m_featureLineGF.x ).c_str(),
810  FMT_IU( aDimension->m_featureLineGF.y ).c_str() );
811 
812  m_out->Print( aNestLevel+1, "(crossbar (pts (xy %s %s) (xy %s %s)))\n",
813  FMT_IU( aDimension->m_crossBarO.x ).c_str(),
814  FMT_IU( aDimension->m_crossBarO.y ).c_str(),
815  FMT_IU( aDimension->m_crossBarF.x ).c_str(),
816  FMT_IU( aDimension->m_crossBarF.y ).c_str() );
817 
818  m_out->Print( aNestLevel+1, "(arrow1a (pts (xy %s %s) (xy %s %s)))\n",
819  FMT_IU( aDimension->m_crossBarF.x ).c_str(),
820  FMT_IU( aDimension->m_crossBarF.y ).c_str(),
821  FMT_IU( aDimension->m_arrowD1F.x ).c_str(),
822  FMT_IU( aDimension->m_arrowD1F.y ).c_str() );
823 
824  m_out->Print( aNestLevel+1, "(arrow1b (pts (xy %s %s) (xy %s %s)))\n",
825  FMT_IU( aDimension->m_crossBarF.x ).c_str(),
826  FMT_IU( aDimension->m_crossBarF.y ).c_str(),
827  FMT_IU( aDimension->m_arrowD2F.x ).c_str(),
828  FMT_IU( aDimension->m_arrowD2F.y ).c_str() );
829 
830  m_out->Print( aNestLevel+1, "(arrow2a (pts (xy %s %s) (xy %s %s)))\n",
831  FMT_IU( aDimension->m_crossBarO.x ).c_str(),
832  FMT_IU( aDimension->m_crossBarO.y ).c_str(),
833  FMT_IU( aDimension->m_arrowG1F.x ).c_str(),
834  FMT_IU( aDimension->m_arrowG1F.y ).c_str() );
835 
836  m_out->Print( aNestLevel+1, "(arrow2b (pts (xy %s %s) (xy %s %s)))\n",
837  FMT_IU( aDimension->m_crossBarO.x ).c_str(),
838  FMT_IU( aDimension->m_crossBarO.y ).c_str(),
839  FMT_IU( aDimension->m_arrowG2F.x ).c_str(),
840  FMT_IU( aDimension->m_arrowG2F.y ).c_str() );
841 
842  m_out->Print( aNestLevel, ")\n" );
843 }
844 
845 
846 void PCB_IO::format( DRAWSEGMENT* aSegment, int aNestLevel ) const
847 {
848  switch( aSegment->GetShape() )
849  {
850  case S_SEGMENT: // Line
851  m_out->Print( aNestLevel, "(gr_line (start %s) (end %s)",
852  FMT_IU( aSegment->GetStart() ).c_str(),
853  FMT_IU( aSegment->GetEnd() ).c_str() );
854 
855  if( aSegment->GetAngle() != 0.0 )
856  m_out->Print( 0, " (angle %s)", FMT_ANGLE( aSegment->GetAngle() ).c_str() );
857 
858  break;
859 
860  case S_CIRCLE: // Circle
861  m_out->Print( aNestLevel, "(gr_circle (center %s) (end %s)",
862  FMT_IU( aSegment->GetStart() ).c_str(),
863  FMT_IU( aSegment->GetEnd() ).c_str() );
864  break;
865 
866  case S_ARC: // Arc
867  m_out->Print( aNestLevel, "(gr_arc (start %s) (end %s) (angle %s)",
868  FMT_IU( aSegment->GetStart() ).c_str(),
869  FMT_IU( aSegment->GetEnd() ).c_str(),
870  FMT_ANGLE( aSegment->GetAngle() ).c_str() );
871  break;
872 
873  case S_POLYGON: // Polygon
874  if( aSegment->IsPolyShapeValid() )
875  {
876  SHAPE_POLY_SET& poly = aSegment->GetPolyShape();
877  SHAPE_LINE_CHAIN& outline = poly.Outline( 0 );
878  int pointsCount = outline.PointCount();
879 
880  m_out->Print( aNestLevel, "(gr_poly (pts" );
881 
882  for( int ii = 0; ii < pointsCount; ++ii )
883  {
884  m_out->Print( 0, " (xy %s)", FMT_IU( outline.CPoint( ii ) ).c_str() );
885  }
886 
887  m_out->Print( 0, ")" );
888  }
889  else
890  {
891  wxFAIL_MSG( wxT( "Cannot format invalid polygon." ) );
892  return;
893  }
894 
895  break;
896 
897  case S_CURVE: // Bezier curve
898  m_out->Print( aNestLevel, "(gr_curve (pts (xy %s) (xy %s) (xy %s) (xy %s))",
899  FMT_IU( aSegment->GetStart() ).c_str(),
900  FMT_IU( aSegment->GetBezControl1() ).c_str(),
901  FMT_IU( aSegment->GetBezControl2() ).c_str(),
902  FMT_IU( aSegment->GetEnd() ).c_str() );
903  break;
904 
905  default:
906  wxFAIL_MSG( wxT( "Cannot format invalid DRAWSEGMENT type." ) );
907  return;
908  };
909 
910  formatLayer( aSegment );
911 
912  m_out->Print( 0, " (width %s)", FMT_IU( aSegment->GetWidth() ).c_str() );
913 
914  if( aSegment->GetTimeStamp() )
915  m_out->Print( 0, " (tstamp %lX)", (unsigned long)aSegment->GetTimeStamp() );
916 
917  if( aSegment->GetStatus() )
918  m_out->Print( 0, " (status %X)", aSegment->GetStatus() );
919 
920  m_out->Print( 0, ")\n" );
921 }
922 
923 
924 void PCB_IO::format( EDGE_MODULE* aModuleDrawing, int aNestLevel ) const
925 {
926  switch( aModuleDrawing->GetShape() )
927  {
928  case S_SEGMENT: // Line
929  m_out->Print( aNestLevel, "(fp_line (start %s) (end %s)",
930  FMT_IU( aModuleDrawing->GetStart0() ).c_str(),
931  FMT_IU( aModuleDrawing->GetEnd0() ).c_str() );
932  break;
933 
934  case S_CIRCLE: // Circle
935  m_out->Print( aNestLevel, "(fp_circle (center %s) (end %s)",
936  FMT_IU( aModuleDrawing->GetStart0() ).c_str(),
937  FMT_IU( aModuleDrawing->GetEnd0() ).c_str() );
938  break;
939 
940  case S_ARC: // Arc
941  m_out->Print( aNestLevel, "(fp_arc (start %s) (end %s) (angle %s)",
942  FMT_IU( aModuleDrawing->GetStart0() ).c_str(),
943  FMT_IU( aModuleDrawing->GetEnd0() ).c_str(),
944  FMT_ANGLE( aModuleDrawing->GetAngle() ).c_str() );
945  break;
946 
947  case S_POLYGON: // Polygonal segment
948  if( aModuleDrawing->IsPolyShapeValid() )
949  {
950  SHAPE_POLY_SET& poly = aModuleDrawing->GetPolyShape();
951  SHAPE_LINE_CHAIN& outline = poly.Outline( 0 );
952  int pointsCount = outline.PointCount();
953 
954  m_out->Print( aNestLevel, "(fp_poly (pts" );
955 
956  for( int ii = 0; ii < pointsCount; ++ii )
957  {
958  int nestLevel = 0;
959 
960  if( ii && !( ii%4 ) ) // newline every 4 pts
961  {
962  nestLevel = aNestLevel + 1;
963  m_out->Print( 0, "\n" );
964  }
965 
966  m_out->Print( nestLevel, "%s(xy %s)",
967  nestLevel ? "" : " ", FMT_IU( outline.CPoint( ii ) ).c_str() );
968  }
969 
970  m_out->Print( 0, ")" );
971  }
972  else
973  {
974  wxFAIL_MSG( wxT( "Cannot format invalid polygon." ) );
975  return;
976  }
977  break;
978 
979  case S_CURVE: // Bezier curve
980  m_out->Print( aNestLevel, "(fp_curve (pts (xy %s) (xy %s) (xy %s) (xy %s))",
981  FMT_IU( aModuleDrawing->GetStart0() ).c_str(),
982  FMT_IU( aModuleDrawing->GetBezControl1() ).c_str(),
983  FMT_IU( aModuleDrawing->GetBezControl2() ).c_str(),
984  FMT_IU( aModuleDrawing->GetEnd0() ).c_str() );
985  break;
986 
987  default:
988  wxFAIL_MSG( wxT( "Cannot format invalid DRAWSEGMENT type." ) );
989  return;
990  };
991 
992  formatLayer( aModuleDrawing );
993 
994  m_out->Print( 0, " (width %s)", FMT_IU( aModuleDrawing->GetWidth() ).c_str() );
995 
996  m_out->Print( 0, ")\n" );
997 }
998 
999 
1000 void PCB_IO::format( PCB_TARGET* aTarget, int aNestLevel ) const
1001 {
1002  m_out->Print( aNestLevel, "(target %s (at %s) (size %s)",
1003  ( aTarget->GetShape() ) ? "x" : "plus",
1004  FMT_IU( aTarget->GetPosition() ).c_str(),
1005  FMT_IU( aTarget->GetSize() ).c_str() );
1006 
1007  if( aTarget->GetWidth() != 0 )
1008  m_out->Print( 0, " (width %s)", FMT_IU( aTarget->GetWidth() ).c_str() );
1009 
1010  formatLayer( aTarget );
1011 
1012  if( aTarget->GetTimeStamp() )
1013  m_out->Print( 0, " (tstamp %lX)", (unsigned long)aTarget->GetTimeStamp() );
1014 
1015  m_out->Print( 0, ")\n" );
1016 }
1017 
1018 
1019 void PCB_IO::format( MODULE* aModule, int aNestLevel ) const
1020 {
1021  if( !( m_ctl & CTL_OMIT_INITIAL_COMMENTS ) )
1022  {
1023  const wxArrayString* initial_comments = aModule->GetInitialComments();
1024 
1025  if( initial_comments )
1026  {
1027  for( unsigned i=0; i<initial_comments->GetCount(); ++i )
1028  m_out->Print( aNestLevel, "%s\n", TO_UTF8( (*initial_comments)[i] ) );
1029 
1030  m_out->Print( 0, "\n" ); // improve readability?
1031  }
1032  }
1033 
1034  m_out->Print( aNestLevel, "(module %s",
1035  m_out->Quotes( aModule->GetFPID().Format() ).c_str() );
1036 
1037  if( aModule->IsLocked() )
1038  m_out->Print( 0, " locked" );
1039 
1040  if( aModule->IsPlaced() )
1041  m_out->Print( 0, " placed" );
1042 
1043  formatLayer( aModule );
1044 
1045  m_out->Print( 0, " (tedit %lX)", (unsigned long)aModule->GetLastEditTime() );
1046 
1047  if( !( m_ctl & CTL_OMIT_TSTAMPS ) )
1048  {
1049  m_out->Print( 0, " (tstamp %lX)\n", (unsigned long)aModule->GetTimeStamp() );
1050  }
1051  else
1052  m_out->Print( 0, "\n" );
1053 
1054  if( !( m_ctl & CTL_OMIT_AT ) )
1055  {
1056  m_out->Print( aNestLevel+1, "(at %s", FMT_IU( aModule->GetPosition() ).c_str() );
1057 
1058  if( aModule->GetOrientation() != 0.0 )
1059  m_out->Print( 0, " %s", FMT_ANGLE( aModule->GetOrientation() ).c_str() );
1060 
1061  m_out->Print( 0, ")\n" );
1062  }
1063 
1064  if( !aModule->GetDescription().IsEmpty() )
1065  m_out->Print( aNestLevel+1, "(descr %s)\n",
1066  m_out->Quotew( aModule->GetDescription() ).c_str() );
1067 
1068  if( !aModule->GetKeywords().IsEmpty() )
1069  m_out->Print( aNestLevel+1, "(tags %s)\n",
1070  m_out->Quotew( aModule->GetKeywords() ).c_str() );
1071 
1072  if( !( m_ctl & CTL_OMIT_PATH ) && !!aModule->GetPath() )
1073  m_out->Print( aNestLevel+1, "(path %s)\n",
1074  m_out->Quotew( aModule->GetPath() ).c_str() );
1075 
1076  if( aModule->GetPlacementCost90() != 0 )
1077  m_out->Print( aNestLevel+1, "(autoplace_cost90 %d)\n", aModule->GetPlacementCost90() );
1078 
1079  if( aModule->GetPlacementCost180() != 0 )
1080  m_out->Print( aNestLevel+1, "(autoplace_cost180 %d)\n", aModule->GetPlacementCost180() );
1081 
1082  if( aModule->GetLocalSolderMaskMargin() != 0 )
1083  m_out->Print( aNestLevel+1, "(solder_mask_margin %s)\n",
1084  FMT_IU( aModule->GetLocalSolderMaskMargin() ).c_str() );
1085 
1086  if( aModule->GetLocalSolderPasteMargin() != 0 )
1087  m_out->Print( aNestLevel+1, "(solder_paste_margin %s)\n",
1088  FMT_IU( aModule->GetLocalSolderPasteMargin() ).c_str() );
1089 
1090  if( aModule->GetLocalSolderPasteMarginRatio() != 0 )
1091  m_out->Print( aNestLevel+1, "(solder_paste_ratio %s)\n",
1092  Double2Str( aModule->GetLocalSolderPasteMarginRatio() ).c_str() );
1093 
1094  if( aModule->GetLocalClearance() != 0 )
1095  m_out->Print( aNestLevel+1, "(clearance %s)\n",
1096  FMT_IU( aModule->GetLocalClearance() ).c_str() );
1097 
1098  if( aModule->GetZoneConnection() != PAD_ZONE_CONN_INHERITED )
1099  m_out->Print( aNestLevel+1, "(zone_connect %d)\n", aModule->GetZoneConnection() );
1100 
1101  if( aModule->GetThermalWidth() != 0 )
1102  m_out->Print( aNestLevel+1, "(thermal_width %s)\n",
1103  FMT_IU( aModule->GetThermalWidth() ).c_str() );
1104 
1105  if( aModule->GetThermalGap() != 0 )
1106  m_out->Print( aNestLevel+1, "(thermal_gap %s)\n",
1107  FMT_IU( aModule->GetThermalGap() ).c_str() );
1108 
1109  // Attributes
1110  if( aModule->GetAttributes() != MOD_DEFAULT )
1111  {
1112  m_out->Print( aNestLevel+1, "(attr" );
1113 
1114  if( aModule->GetAttributes() & MOD_CMS )
1115  m_out->Print( 0, " smd" );
1116 
1117  if( aModule->GetAttributes() & MOD_VIRTUAL )
1118  m_out->Print( 0, " virtual" );
1119 
1120  m_out->Print( 0, ")\n" );
1121  }
1122 
1123  Format( (BOARD_ITEM*) &aModule->Reference(), aNestLevel+1 );
1124  Format( (BOARD_ITEM*) &aModule->Value(), aNestLevel+1 );
1125 
1126  // Save drawing elements.
1127  for( BOARD_ITEM* gr = aModule->GraphicalItemsList(); gr; gr = gr->Next() )
1128  Format( gr, aNestLevel+1 );
1129 
1130  // Save pads.
1131  for( D_PAD* pad = aModule->PadsList(); pad; pad = pad->Next() )
1132  format( pad, aNestLevel+1 );
1133 
1134  // Save 3D info.
1135  auto bs3D = aModule->Models().begin();
1136  auto es3D = aModule->Models().end();
1137 
1138  while( bs3D != es3D )
1139  {
1140  if( !bs3D->m_Filename.IsEmpty() )
1141  {
1142  m_out->Print( aNestLevel+1, "(model %s\n",
1143  m_out->Quotew( bs3D->m_Filename ).c_str() );
1144 
1145  /* Write 3D model offset in mm
1146  * 4.0.x wrote "at" which was actually in inches
1147  * 5.0.x onwards, 3D model offset is written using "offset"
1148  *
1149  * If the offset is all zero, write "at" (fewer file changes)
1150  * Otherwise, write "offset"
1151  */
1152 
1153  wxString offsetTag = "offset";
1154 
1155  if( bs3D->m_Offset.x == 0 &&
1156  bs3D->m_Offset.y == 0 &&
1157  bs3D->m_Offset.z == 0 )
1158  {
1159  offsetTag = "at";
1160  }
1161 
1162  m_out->Print( aNestLevel+2, "(%s (xyz %s %s %s))\n",
1163  offsetTag.ToStdString().c_str(),
1164  Double2Str( bs3D->m_Offset.x ).c_str(),
1165  Double2Str( bs3D->m_Offset.y ).c_str(),
1166  Double2Str( bs3D->m_Offset.z ).c_str() );
1167 
1168  m_out->Print( aNestLevel+2, "(scale (xyz %s %s %s))\n",
1169  Double2Str( bs3D->m_Scale.x ).c_str(),
1170  Double2Str( bs3D->m_Scale.y ).c_str(),
1171  Double2Str( bs3D->m_Scale.z ).c_str() );
1172 
1173  m_out->Print( aNestLevel+2, "(rotate (xyz %s %s %s))\n",
1174  Double2Str( bs3D->m_Rotation.x ).c_str(),
1175  Double2Str( bs3D->m_Rotation.y ).c_str(),
1176  Double2Str( bs3D->m_Rotation.z ).c_str() );
1177 
1178  m_out->Print( aNestLevel+1, ")\n" );
1179  }
1180  ++bs3D;
1181  }
1182 
1183  m_out->Print( aNestLevel, ")\n" );
1184 }
1185 
1186 
1187 void PCB_IO::formatLayers( LSET aLayerMask, int aNestLevel ) const
1188 {
1189  std::string output;
1190 
1191  if( aNestLevel == 0 )
1192  output += ' ';
1193 
1194  output += "(layers";
1195 
1196  static const LSET cu_all( LSET::AllCuMask() );
1197  static const LSET fr_bk( 2, B_Cu, F_Cu );
1198  static const LSET adhes( 2, B_Adhes, F_Adhes );
1199  static const LSET paste( 2, B_Paste, F_Paste );
1200  static const LSET silks( 2, B_SilkS, F_SilkS );
1201  static const LSET mask( 2, B_Mask, F_Mask );
1202  static const LSET crt_yd(2, B_CrtYd, F_CrtYd );
1203  static const LSET fab( 2, B_Fab, F_Fab );
1204 
1205  LSET cu_mask = cu_all;
1206 
1207  // output copper layers first, then non copper
1208 
1209  if( ( aLayerMask & cu_mask ) == cu_mask )
1210  {
1211  output += " *.Cu";
1212  aLayerMask &= ~cu_all; // clear bits, so they are not output again below
1213  }
1214  else if( ( aLayerMask & cu_mask ) == fr_bk )
1215  {
1216  output += " F&B.Cu";
1217  aLayerMask &= ~fr_bk;
1218  }
1219 
1220  if( ( aLayerMask & adhes ) == adhes )
1221  {
1222  output += " *.Adhes";
1223  aLayerMask &= ~adhes;
1224  }
1225 
1226  if( ( aLayerMask & paste ) == paste )
1227  {
1228  output += " *.Paste";
1229  aLayerMask &= ~paste;
1230  }
1231 
1232  if( ( aLayerMask & silks ) == silks )
1233  {
1234  output += " *.SilkS";
1235  aLayerMask &= ~silks;
1236  }
1237 
1238  if( ( aLayerMask & mask ) == mask )
1239  {
1240  output += " *.Mask";
1241  aLayerMask &= ~mask;
1242  }
1243 
1244  if( ( aLayerMask & crt_yd ) == crt_yd )
1245  {
1246  output += " *.CrtYd";
1247  aLayerMask &= ~crt_yd;
1248  }
1249 
1250  if( ( aLayerMask & fab ) == fab )
1251  {
1252  output += " *.Fab";
1253  aLayerMask &= ~fab;
1254  }
1255 
1256  // output any individual layers not handled in wildcard combos above
1257 
1258  wxString layerName;
1259 
1260  for( LAYER_NUM layer = 0; layer < PCB_LAYER_ID_COUNT; ++layer )
1261  {
1262  if( aLayerMask[layer] )
1263  {
1264  if( m_board && !( m_ctl & CTL_STD_LAYER_NAMES ) )
1265  layerName = m_board->GetLayerName( PCB_LAYER_ID( layer ) );
1266 
1267  else // I am being called from FootprintSave()
1268  layerName = BOARD::GetStandardLayerName( PCB_LAYER_ID( layer ) );
1269 
1270  output += ' ';
1271  output += m_out->Quotew( layerName );
1272  }
1273  }
1274 
1275  m_out->Print( aNestLevel, "%s)", output.c_str() );
1276 }
1277 
1278 
1279 void PCB_IO::format( D_PAD* aPad, int aNestLevel ) const
1280 {
1281  const char* shape;
1282 
1283  switch( aPad->GetShape() )
1284  {
1285  case PAD_SHAPE_CIRCLE: shape = "circle"; break;
1286  case PAD_SHAPE_RECT: shape = "rect"; break;
1287  case PAD_SHAPE_OVAL: shape = "oval"; break;
1288  case PAD_SHAPE_TRAPEZOID: shape = "trapezoid"; break;
1289  case PAD_SHAPE_ROUNDRECT: shape = "roundrect"; break;
1290  case PAD_SHAPE_CUSTOM: shape = "custom"; break;
1291 
1292  default:
1293  THROW_IO_ERROR( wxString::Format( _( "unknown pad type: %d"), aPad->GetShape() ) );
1294  }
1295 
1296  const char* type;
1297 
1298  switch( aPad->GetAttribute() )
1299  {
1300  case PAD_ATTRIB_STANDARD: type = "thru_hole"; break;
1301  case PAD_ATTRIB_SMD: type = "smd"; break;
1302  case PAD_ATTRIB_CONN: type = "connect"; break;
1303  case PAD_ATTRIB_HOLE_NOT_PLATED: type = "np_thru_hole"; break;
1304 
1305  default:
1306  THROW_IO_ERROR( wxString::Format( _( "unknown pad attribute: %d" ),
1307  aPad->GetAttribute() ) );
1308  }
1309 
1310  m_out->Print( aNestLevel, "(pad %s %s %s",
1311  m_out->Quotew( aPad->GetName() ).c_str(),
1312  type, shape );
1313  m_out->Print( 0, " (at %s", FMT_IU( aPad->GetPos0() ).c_str() );
1314 
1315  if( aPad->GetOrientation() != 0.0 )
1316  m_out->Print( 0, " %s", FMT_ANGLE( aPad->GetOrientation() ).c_str() );
1317 
1318  m_out->Print( 0, ")" );
1319  m_out->Print( 0, " (size %s)", FMT_IU( aPad->GetSize() ).c_str() );
1320 
1321  if( (aPad->GetDelta().GetWidth()) != 0 || (aPad->GetDelta().GetHeight() != 0 ) )
1322  m_out->Print( 0, " (rect_delta %s )", FMT_IU( aPad->GetDelta() ).c_str() );
1323 
1324  wxSize sz = aPad->GetDrillSize();
1325  wxPoint shapeoffset = aPad->GetOffset();
1326 
1327  if( (sz.GetWidth() > 0) || (sz.GetHeight() > 0) ||
1328  (shapeoffset.x != 0) || (shapeoffset.y != 0) )
1329  {
1330  m_out->Print( 0, " (drill" );
1331 
1332  if( aPad->GetDrillShape() == PAD_DRILL_SHAPE_OBLONG )
1333  m_out->Print( 0, " oval" );
1334 
1335  if( sz.GetWidth() > 0 )
1336  m_out->Print( 0, " %s", FMT_IU( sz.GetWidth() ).c_str() );
1337 
1338  if( sz.GetHeight() > 0 && sz.GetWidth() != sz.GetHeight() )
1339  m_out->Print( 0, " %s", FMT_IU( sz.GetHeight() ).c_str() );
1340 
1341  if( (shapeoffset.x != 0) || (shapeoffset.y != 0) )
1342  m_out->Print( 0, " (offset %s)", FMT_IU( aPad->GetOffset() ).c_str() );
1343 
1344  m_out->Print( 0, ")" );
1345  }
1346 
1347  formatLayers( aPad->GetLayerSet() );
1348 
1349  // Output the radius ratio for rounded rect pads
1350  if( aPad->GetShape() == PAD_SHAPE_ROUNDRECT )
1351  {
1352  m_out->Print( 0, "(roundrect_rratio %s)",
1353  Double2Str( aPad->GetRoundRectRadiusRatio() ).c_str() );
1354  }
1355 
1356  std::string output;
1357 
1358  // Unconnected pad is default net so don't save it.
1359  if( !( m_ctl & CTL_OMIT_NETS ) && aPad->GetNetCode() != NETINFO_LIST::UNCONNECTED )
1360  StrPrintf( &output, " (net %d %s)", m_mapping->Translate( aPad->GetNetCode() ),
1361  m_out->Quotew( aPad->GetNetname() ).c_str() );
1362 
1363  if( aPad->GetPadToDieLength() != 0 )
1364  StrPrintf( &output, " (die_length %s)", FMT_IU( aPad->GetPadToDieLength() ).c_str() );
1365 
1366  if( aPad->GetLocalSolderMaskMargin() != 0 )
1367  StrPrintf( &output, " (solder_mask_margin %s)", FMT_IU( aPad->GetLocalSolderMaskMargin() ).c_str() );
1368 
1369  if( aPad->GetLocalSolderPasteMargin() != 0 )
1370  StrPrintf( &output, " (solder_paste_margin %s)", FMT_IU( aPad->GetLocalSolderPasteMargin() ).c_str() );
1371 
1372  if( aPad->GetLocalSolderPasteMarginRatio() != 0 )
1373  StrPrintf( &output, " (solder_paste_margin_ratio %s)",
1374  Double2Str( aPad->GetLocalSolderPasteMarginRatio() ).c_str() );
1375 
1376  if( aPad->GetLocalClearance() != 0 )
1377  StrPrintf( &output, " (clearance %s)", FMT_IU( aPad->GetLocalClearance() ).c_str() );
1378 
1380  StrPrintf( &output, " (zone_connect %d)", aPad->GetZoneConnection() );
1381 
1382  if( aPad->GetThermalWidth() != 0 )
1383  StrPrintf( &output, " (thermal_width %s)", FMT_IU( aPad->GetThermalWidth() ).c_str() );
1384 
1385  if( aPad->GetThermalGap() != 0 )
1386  StrPrintf( &output, " (thermal_gap %s)", FMT_IU( aPad->GetThermalGap() ).c_str() );
1387 
1388  if( output.size() )
1389  {
1390  m_out->Print( 0, "\n" );
1391  m_out->Print( aNestLevel+1, "%s", output.c_str()+1 ); // +1 skips 1st space on 1st element
1392  }
1393 
1394  if( aPad->GetShape() == PAD_SHAPE_CUSTOM )
1395  {
1396  m_out->Print( 0, "\n");
1397  m_out->Print( aNestLevel+1, "(options" );
1398 
1400  m_out->Print( 0, " (clearance convexhull)" );
1401  #if 1 // Set to 1 to output the default option
1402  else
1403  m_out->Print( 0, " (clearance outline)" );
1404  #endif
1405 
1406  // Output the anchor pad shape (circle/rect)
1407  if( aPad->GetAnchorPadShape() == PAD_SHAPE_RECT )
1408  shape = "rect";
1409  else
1410  shape = "circle";
1411 
1412  m_out->Print( 0, " (anchor %s)", shape );
1413 
1414  m_out->Print( 0, ")"); // end of (options ...
1415 
1416  // Output graphic primitive of the pad shape
1417  m_out->Print( 0, "\n");
1418  m_out->Print( aNestLevel+1, "(primitives" );
1419 
1420  int nested_level = aNestLevel+2;
1421 
1422  // Output all basic shapes
1423  for( unsigned icnt = 0; icnt < aPad->GetPrimitives().size(); ++icnt )
1424  {
1425  m_out->Print( 0, "\n");
1426 
1427  const PAD_CS_PRIMITIVE& primitive = aPad->GetPrimitives()[icnt];
1428 
1429  switch( primitive.m_Shape )
1430  {
1431  case S_SEGMENT: // usual segment : line with rounded ends
1432  m_out->Print( nested_level, "(gr_line (start %s) (end %s) (width %s))",
1433  FMT_IU( primitive.m_Start ).c_str(),
1434  FMT_IU( primitive.m_End ).c_str(),
1435  FMT_IU( primitive.m_Thickness ).c_str() );
1436  break;
1437 
1438  case S_ARC: // Arc with rounded ends
1439  m_out->Print( nested_level, "(gr_arc (start %s) (end %s) (angle %s) (width %s))",
1440  FMT_IU( primitive.m_Start ).c_str(),
1441  FMT_IU( primitive.m_End ).c_str(),
1442  FMT_ANGLE( primitive.m_ArcAngle ).c_str(),
1443  FMT_IU( primitive.m_Thickness ).c_str() );
1444  break;
1445 
1446  case S_CIRCLE: // ring or circle (circle if width == 0
1447  m_out->Print( nested_level, "(gr_circle (center %s) (end %s %s) (width %s))",
1448  FMT_IU( primitive.m_Start ).c_str(),
1449  FMT_IU( primitive.m_Start.x + primitive.m_Radius ).c_str(),
1450  FMT_IU( primitive.m_Start.y ).c_str(),
1451  FMT_IU( primitive.m_Thickness ).c_str() );
1452  break;
1453 
1454  case S_POLYGON: // polygon
1455  if( primitive.m_Poly.size() < 2 )
1456  break; // Malformed polygon.
1457 
1458  {
1459  m_out->Print( nested_level, "(gr_poly (pts\n");
1460 
1461  // Write the polygon corners coordinates:
1462  const std::vector< wxPoint>& poly = primitive.m_Poly;
1463  int newLine = 0;
1464 
1465  for( unsigned ii = 0; ii < poly.size(); ii++ )
1466  {
1467  if( newLine == 0 )
1468  m_out->Print( nested_level+1, " (xy %s)", FMT_IU( wxPoint( poly[ii].x, poly[ii].y ) ).c_str() );
1469  else
1470  m_out->Print( 0, " (xy %s)", FMT_IU( wxPoint( poly[ii].x, poly[ii].y ) ).c_str() );
1471 
1472  if( ++newLine > 4 )
1473  {
1474  newLine = 0;
1475  m_out->Print( 0, "\n" );
1476  }
1477  }
1478 
1479  m_out->Print( 0, ") (width %s))", FMT_IU( primitive.m_Thickness ).c_str() );
1480  }
1481  break;
1482 
1483  default:
1484  break;
1485  }
1486  }
1487 
1488  m_out->Print( 0, "\n");
1489  m_out->Print( aNestLevel+1, ")" ); // end of (basic_shapes
1490  }
1491 
1492  m_out->Print( 0, ")\n" );
1493 }
1494 
1495 
1496 void PCB_IO::format( TEXTE_PCB* aText, int aNestLevel ) const
1497 {
1498  m_out->Print( aNestLevel, "(gr_text %s (at %s",
1499  m_out->Quotew( aText->GetText() ).c_str(),
1500  FMT_IU( aText->GetTextPos() ).c_str() );
1501 
1502  if( aText->GetTextAngle() != 0.0 )
1503  m_out->Print( 0, " %s", FMT_ANGLE( aText->GetTextAngle() ).c_str() );
1504 
1505  m_out->Print( 0, ")" );
1506 
1507  formatLayer( aText );
1508 
1509  if( aText->GetTimeStamp() )
1510  m_out->Print( 0, " (tstamp %lX)", (unsigned long)aText->GetTimeStamp() );
1511 
1512  m_out->Print( 0, "\n" );
1513 
1514  aText->EDA_TEXT::Format( m_out, aNestLevel, m_ctl );
1515 
1516  m_out->Print( aNestLevel, ")\n" );
1517 }
1518 
1519 
1520 void PCB_IO::format( TEXTE_MODULE* aText, int aNestLevel ) const
1521 {
1522  wxString type;
1523 
1524  switch( aText->GetType() )
1525  {
1526  case TEXTE_MODULE::TEXT_is_REFERENCE: type = "reference"; break;
1527  case TEXTE_MODULE::TEXT_is_VALUE: type = "value"; break;
1528  case TEXTE_MODULE::TEXT_is_DIVERS: type = "user";
1529  }
1530 
1531  m_out->Print( aNestLevel, "(fp_text %s %s (at %s",
1532  m_out->Quotew( type ).c_str(),
1533  m_out->Quotew( aText->GetText() ).c_str(),
1534  FMT_IU( aText->GetPos0() ).c_str() );
1535 
1536  // Due to Pcbnew history, fp_text angle is saved as an absolute on screen angle,
1537  // but internally the angle is held relative to its parent footprint. parent
1538  // may be NULL when saving a footprint outside a BOARD.
1539  double orient = aText->GetTextAngle();
1540  MODULE* parent = (MODULE*) aText->GetParent();
1541 
1542  if( parent )
1543  {
1544  // GetTextAngle() is always in -360..+360 range because of
1545  // TEXTE_MODULE::SetTextAngle(), but summing that angle with an
1546  // additional board angle could kick sum up >= 360 or <= -360, so to have
1547  // consistent results, normalize again for the BOARD save. A footprint
1548  // save does not use this code path since parent is NULL.
1549 #if 0
1550  // This one could be considered reasonable if you like positive angles
1551  // in your board text.
1552  orient = NormalizeAnglePos( orient + parent->GetOrientation() );
1553 #else
1554  // Choose compatibility for now, even though this is only a 720 degree clamp
1555  // with two possible values for every angle.
1556  orient = NormalizeAngle360Min( orient + parent->GetOrientation() );
1557 #endif
1558  }
1559 
1560  if( orient != 0.0 )
1561  m_out->Print( 0, " %s", FMT_ANGLE( orient ).c_str() );
1562 
1563  if( aText->IsUnlocked() )
1564  m_out->Print( 0, " unlocked" );
1565 
1566  m_out->Print( 0, ")" );
1567  formatLayer( aText );
1568 
1569  if( !aText->IsVisible() )
1570  m_out->Print( 0, " hide" );
1571 
1572  m_out->Print( 0, "\n" );
1573 
1574  aText->EDA_TEXT::Format( m_out, aNestLevel, m_ctl | CTL_OMIT_HIDE );
1575 
1576  m_out->Print( aNestLevel, ")\n" );
1577 }
1578 
1579 
1580 void PCB_IO::format( TRACK* aTrack, int aNestLevel ) const
1581 {
1582  if( aTrack->Type() == PCB_VIA_T )
1583  {
1584  PCB_LAYER_ID layer1, layer2;
1585 
1586  const VIA* via = static_cast<const VIA*>( aTrack );
1587  BOARD* board = (BOARD*) via->GetParent();
1588 
1589  wxCHECK_RET( board != 0, wxT( "Via " ) + via->GetSelectMenuText() +
1590  wxT( " has no parent." ) );
1591 
1592  m_out->Print( aNestLevel, "(via" );
1593 
1594  via->LayerPair( &layer1, &layer2 );
1595 
1596  switch( via->GetViaType() )
1597  {
1598  case VIA_THROUGH: // Default shape not saved.
1599  break;
1600 
1601  case VIA_BLIND_BURIED:
1602  m_out->Print( 0, " blind" );
1603  break;
1604 
1605  case VIA_MICROVIA:
1606  m_out->Print( 0, " micro" );
1607  break;
1608 
1609  default:
1610  THROW_IO_ERROR( wxString::Format( _( "unknown via type %d" ), via->GetViaType() ) );
1611  }
1612 
1613  m_out->Print( 0, " (at %s) (size %s)",
1614  FMT_IU( aTrack->GetStart() ).c_str(),
1615  FMT_IU( aTrack->GetWidth() ).c_str() );
1616 
1617  if( via->GetDrill() != UNDEFINED_DRILL_DIAMETER )
1618  m_out->Print( 0, " (drill %s)", FMT_IU( via->GetDrill() ).c_str() );
1619 
1620  m_out->Print( 0, " (layers %s %s)",
1621  m_out->Quotew( m_board->GetLayerName( layer1 ) ).c_str(),
1622  m_out->Quotew( m_board->GetLayerName( layer2 ) ).c_str() );
1623  }
1624  else
1625  {
1626  m_out->Print( aNestLevel, "(segment (start %s) (end %s) (width %s)",
1627  FMT_IU( aTrack->GetStart() ).c_str(), FMT_IU( aTrack->GetEnd() ).c_str(),
1628  FMT_IU( aTrack->GetWidth() ).c_str() );
1629 
1630  m_out->Print( 0, " (layer %s)", m_out->Quotew( aTrack->GetLayerName() ).c_str() );
1631  }
1632 
1633  m_out->Print( 0, " (net %d)", m_mapping->Translate( aTrack->GetNetCode() ) );
1634 
1635  if( aTrack->GetTimeStamp() != 0 )
1636  m_out->Print( 0, " (tstamp %lX)", (unsigned long)aTrack->GetTimeStamp() );
1637 
1638  if( aTrack->GetStatus() != 0 )
1639  m_out->Print( 0, " (status %X)", aTrack->GetStatus() );
1640 
1641  m_out->Print( 0, ")\n" );
1642 }
1643 
1644 
1645 void PCB_IO::format( ZONE_CONTAINER* aZone, int aNestLevel ) const
1646 {
1647  // Save the NET info; For keepout zones, net code and net name are irrelevant
1648  // so be sure a dummy value is stored, just for ZONE_CONTAINER compatibility
1649  // (perhaps netcode and netname should be not stored)
1650  m_out->Print( aNestLevel, "(zone (net %d) (net_name %s)",
1651  aZone->GetIsKeepout() ? 0 : m_mapping->Translate( aZone->GetNetCode() ),
1652  m_out->Quotew( aZone->GetIsKeepout() ? wxT("") : aZone->GetNetname() ).c_str() );
1653 
1654  // If a zone exists on multiple layers, format accordingly
1655  if( aZone->GetLayerSet().count() > 1 )
1656  {
1657  formatLayers( aZone->GetLayerSet() );
1658  }
1659  else
1660  {
1661  formatLayer( aZone );
1662  }
1663 
1664  m_out->Print( 0, " (tstamp %lX)", (unsigned long) aZone->GetTimeStamp() );
1665 
1666  // Save the outline aux info
1667  std::string hatch;
1668 
1669  switch( aZone->GetHatchStyle() )
1670  {
1671  default:
1672  case ZONE_CONTAINER::NO_HATCH: hatch = "none"; break;
1673  case ZONE_CONTAINER::DIAGONAL_EDGE: hatch = "edge"; break;
1674  case ZONE_CONTAINER::DIAGONAL_FULL: hatch = "full"; break;
1675  }
1676 
1677  m_out->Print( 0, " (hatch %s %s)\n", hatch.c_str(),
1678  FMT_IU( aZone->GetHatchPitch() ).c_str() );
1679 
1680  if( aZone->GetPriority() > 0 )
1681  m_out->Print( aNestLevel+1, "(priority %d)\n", aZone->GetPriority() );
1682 
1683  m_out->Print( aNestLevel+1, "(connect_pads" );
1684 
1685  switch( aZone->GetPadConnection() )
1686  {
1687  default:
1688  case PAD_ZONE_CONN_THERMAL: // Default option not saved or loaded.
1689  break;
1690 
1692  m_out->Print( 0, " thru_hole_only" );
1693  break;
1694 
1695  case PAD_ZONE_CONN_FULL:
1696  m_out->Print( 0, " yes" );
1697  break;
1698 
1699  case PAD_ZONE_CONN_NONE:
1700  m_out->Print( 0, " no" );
1701  break;
1702  }
1703 
1704  m_out->Print( 0, " (clearance %s))\n",
1705  FMT_IU( aZone->GetZoneClearance() ).c_str() );
1706 
1707  m_out->Print( aNestLevel+1, "(min_thickness %s)\n",
1708  FMT_IU( aZone->GetMinThickness() ).c_str() );
1709 
1710  if( aZone->GetIsKeepout() )
1711  {
1712  m_out->Print( aNestLevel+1, "(keepout (tracks %s) (vias %s) (copperpour %s))\n",
1713  aZone->GetDoNotAllowTracks() ? "not_allowed" : "allowed",
1714  aZone->GetDoNotAllowVias() ? "not_allowed" : "allowed",
1715  aZone->GetDoNotAllowCopperPour() ? "not_allowed" : "allowed" );
1716  }
1717 
1718  m_out->Print( aNestLevel+1, "(fill" );
1719 
1720  // Default is not filled.
1721  if( aZone->IsFilled() )
1722  m_out->Print( 0, " yes" );
1723 
1724  // Default is polygon filled.
1725  if( aZone->GetFillMode() )
1726  m_out->Print( 0, " (mode segment)" );
1727 
1728  m_out->Print( 0, " (arc_segments %d) (thermal_gap %s) (thermal_bridge_width %s)",
1729  aZone->GetArcSegmentCount(),
1730  FMT_IU( aZone->GetThermalReliefGap() ).c_str(),
1731  FMT_IU( aZone->GetThermalReliefCopperBridge() ).c_str() );
1732 
1734  {
1735  m_out->Print( 0, " (smoothing" );
1736 
1737  switch( aZone->GetCornerSmoothingType() )
1738  {
1740  m_out->Print( 0, " chamfer" );
1741  break;
1742 
1744  m_out->Print( 0, " fillet" );
1745  break;
1746 
1747  default:
1748  THROW_IO_ERROR( wxString::Format( _( "unknown zone corner smoothing type %d" ),
1749  aZone->GetCornerSmoothingType() ) );
1750  }
1751  m_out->Print( 0, ")" );
1752 
1753  if( aZone->GetCornerRadius() != 0 )
1754  m_out->Print( 0, " (radius %s)",
1755  FMT_IU( aZone->GetCornerRadius() ).c_str() );
1756  }
1757 
1758  m_out->Print( 0, ")\n" );
1759 
1760  int newLine = 0;
1761 
1762  if( aZone->GetNumCorners() )
1763  {
1764  bool new_polygon = true;
1765  bool is_closed = false;
1766 
1767  for( auto iterator = aZone->IterateWithHoles(); iterator; iterator++ )
1768  {
1769  if( new_polygon )
1770  {
1771  newLine = 0;
1772  m_out->Print( aNestLevel+1, "(polygon\n" );
1773  m_out->Print( aNestLevel+2, "(pts\n" );
1774  new_polygon = false;
1775  is_closed = false;
1776  }
1777 
1778  if( newLine == 0 )
1779  m_out->Print( aNestLevel+3, "(xy %s %s)",
1780  FMT_IU( iterator->x ).c_str(), FMT_IU( iterator->y ).c_str() );
1781  else
1782  m_out->Print( 0, " (xy %s %s)",
1783  FMT_IU( iterator->x ).c_str(), FMT_IU( iterator->y ).c_str() );
1784 
1785  if( newLine < 4 )
1786  {
1787  newLine += 1;
1788  }
1789  else
1790  {
1791  newLine = 0;
1792  m_out->Print( 0, "\n" );
1793  }
1794 
1795  if( iterator.IsEndContour() )
1796  {
1797  is_closed = true;
1798 
1799  if( newLine != 0 )
1800  m_out->Print( 0, "\n" );
1801 
1802  m_out->Print( aNestLevel+2, ")\n" );
1803  m_out->Print( aNestLevel+1, ")\n" );
1804  new_polygon = true;
1805  }
1806  }
1807 
1808  if( !is_closed ) // Should not happen, but...
1809  m_out->Print( aNestLevel+1, ")\n" );
1810 
1811  }
1812 
1813  // Save the PolysList (filled areas)
1814  const SHAPE_POLY_SET& fv = aZone->GetFilledPolysList();
1815  newLine = 0;
1816 
1817  if( !fv.IsEmpty() )
1818  {
1819  bool new_polygon = true;
1820  bool is_closed = false;
1821 
1822  for( auto it = fv.CIterate(); it; ++it )
1823  {
1824  if( new_polygon )
1825  {
1826  newLine = 0;
1827  m_out->Print( aNestLevel+1, "(filled_polygon\n" );
1828  m_out->Print( aNestLevel+2, "(pts\n" );
1829  new_polygon = false;
1830  is_closed = false;
1831  }
1832 
1833  if( newLine == 0 )
1834  m_out->Print( aNestLevel+3, "(xy %s %s)",
1835  FMT_IU( it->x ).c_str(), FMT_IU( it->y ).c_str() );
1836  else
1837  m_out->Print( 0, " (xy %s %s)",
1838  FMT_IU( it->x ) .c_str(), FMT_IU( it->y ).c_str() );
1839 
1840  if( newLine < 4 )
1841  {
1842  newLine += 1;
1843  }
1844  else
1845  {
1846  newLine = 0;
1847  m_out->Print( 0, "\n" );
1848  }
1849 
1850  if( it.IsEndContour() )
1851  {
1852  is_closed = true;
1853 
1854  if( newLine != 0 )
1855  m_out->Print( 0, "\n" );
1856 
1857  m_out->Print( aNestLevel+2, ")\n" );
1858  m_out->Print( aNestLevel+1, ")\n" );
1859  new_polygon = true;
1860  }
1861  }
1862 
1863  if( !is_closed ) // Should not happen, but...
1864  m_out->Print( aNestLevel+1, ")\n" );
1865  }
1866 
1867  // Save the filling segments list
1868  const auto& segs = aZone->FillSegments();
1869 
1870  if( segs.size() )
1871  {
1872  m_out->Print( aNestLevel+1, "(fill_segments\n" );
1873 
1874  for( ZONE_SEGMENT_FILL::const_iterator it = segs.begin(); it != segs.end(); ++it )
1875  {
1876  m_out->Print( aNestLevel+2, "(pts (xy %s) (xy %s))\n",
1877  FMT_IU( wxPoint( it->A ) ).c_str(),
1878  FMT_IU( wxPoint( it->B ) ).c_str() );
1879  }
1880 
1881  m_out->Print( aNestLevel+1, ")\n" );
1882  }
1883 
1884  m_out->Print( aNestLevel, ")\n" );
1885 }
1886 
1887 
1888 PCB_IO::PCB_IO( int aControlFlags ) :
1889  m_cache( 0 ),
1890  m_ctl( aControlFlags ),
1891  m_parser( new PCB_PARSER() ),
1892  m_mapping( new NETINFO_MAPPING() )
1893 {
1894  init( 0 );
1895  m_out = &m_sf;
1896 }
1897 
1898 
1900 {
1901  delete m_cache;
1902  delete m_parser;
1903  delete m_mapping;
1904 }
1905 
1906 
1907 BOARD* PCB_IO::Load( const wxString& aFileName, BOARD* aAppendToMe, const PROPERTIES* aProperties )
1908 {
1909  FILE_LINE_READER reader( aFileName );
1910 
1911  init( aProperties );
1912 
1913  m_parser->SetLineReader( &reader );
1914  m_parser->SetBoard( aAppendToMe );
1915 
1916  BOARD* board;
1917 
1918  try
1919  {
1920  board = dynamic_cast<BOARD*>( m_parser->Parse() );
1921  }
1922  catch( const FUTURE_FORMAT_ERROR& )
1923  {
1924  // Don't wrap a FUTURE_FORMAT_ERROR in another
1925  throw;
1926  }
1927  catch( const PARSE_ERROR& parse_error )
1928  {
1929  if( m_parser->IsTooRecent() )
1930  throw FUTURE_FORMAT_ERROR( parse_error, m_parser->GetRequiredVersion() );
1931  else
1932  throw;
1933  }
1934 
1935  if( !board )
1936  {
1937  // The parser loaded something that was valid, but wasn't a board.
1938  THROW_PARSE_ERROR( _( "this file does not contain a PCB" ),
1939  m_parser->CurSource(), m_parser->CurLine(),
1940  m_parser->CurLineNumber(), m_parser->CurOffset() );
1941  }
1942 
1943  // Give the filename to the board if it's new
1944  if( !aAppendToMe )
1945  board->SetFileName( aFileName );
1946 
1947  return board;
1948 }
1949 
1950 
1951 void PCB_IO::init( const PROPERTIES* aProperties )
1952 {
1953  m_board = NULL;
1954  m_reader = NULL;
1956  m_props = aProperties;
1957 }
1958 
1959 
1960 void PCB_IO::validateCache( const wxString& aLibraryPath, bool checkModified )
1961 {
1962  if( !m_cache || !m_cache->IsPath( aLibraryPath ) || ( checkModified && m_cache->IsModified() ) )
1963  {
1964  // a spectacular episode in memory management:
1965  delete m_cache;
1966  m_cache = new FP_CACHE( this, aLibraryPath );
1967  m_cache->Load();
1968  }
1969 }
1970 
1971 
1972 void PCB_IO::FootprintEnumerate( wxArrayString& aFootprintNames,
1973  const wxString& aLibraryPath,
1974  const PROPERTIES* aProperties )
1975 {
1976  LOCALE_IO toggle; // toggles on, then off, the C locale.
1977  wxDir dir( aLibraryPath );
1978 
1979  init( aProperties );
1980 
1981  wxString errorMsg;
1982 
1983  try
1984  {
1985  validateCache( aLibraryPath );
1986  }
1987  catch( const IO_ERROR& ioe )
1988  {
1989  errorMsg = ioe.What();
1990  }
1991 
1992  // Some of the files may have been parsed correctly so we want to add the valid files to
1993  // the library.
1994 
1995  const MODULE_MAP& mods = m_cache->GetModules();
1996 
1997  for( MODULE_CITER it = mods.begin(); it != mods.end(); ++it )
1998  {
1999  aFootprintNames.Add( it->first );
2000  }
2001 
2002  if( !errorMsg.IsEmpty() )
2003  THROW_IO_ERROR( errorMsg );
2004 }
2005 
2006 
2007 MODULE* PCB_IO::doLoadFootprint( const wxString& aLibraryPath,
2008  const wxString& aFootprintName,
2009  const PROPERTIES* aProperties,
2010  bool checkModified )
2011 {
2012  LOCALE_IO toggle; // toggles on, then off, the C locale.
2013 
2014  init( aProperties );
2015 
2016  try
2017  {
2018  validateCache( aLibraryPath, checkModified );
2019  }
2020  catch( const IO_ERROR& )
2021  {
2022  // do nothing with the error
2023  }
2024 
2025  const MODULE_MAP& mods = m_cache->GetModules();
2026 
2027  MODULE_CITER it = mods.find( aFootprintName );
2028 
2029  if( it == mods.end() )
2030  {
2031  return NULL;
2032  }
2033 
2034  // copy constructor to clone the already loaded MODULE
2035  return new MODULE( *it->second->GetModule() );
2036 }
2037 
2038 
2039 MODULE* PCB_IO::LoadEnumeratedFootprint( const wxString& aLibraryPath,
2040  const wxString& aFootprintName,
2041  const PROPERTIES* aProperties )
2042 {
2043  return doLoadFootprint( aLibraryPath, aFootprintName, aProperties, false );
2044 }
2045 
2046 
2047 MODULE* PCB_IO::FootprintLoad( const wxString& aLibraryPath, const wxString& aFootprintName,
2048  const PROPERTIES* aProperties )
2049 {
2050  return doLoadFootprint( aLibraryPath, aFootprintName, aProperties, true );
2051 }
2052 
2053 
2054 void PCB_IO::FootprintSave( const wxString& aLibraryPath, const MODULE* aFootprint,
2055  const PROPERTIES* aProperties )
2056 {
2057  LOCALE_IO toggle; // toggles on, then off, the C locale.
2058 
2059  init( aProperties );
2060 
2061  // In this public PLUGIN API function, we can safely assume it was
2062  // called for saving into a library path.
2064 
2065  validateCache( aLibraryPath );
2066 
2067  if( !m_cache->IsWritable() )
2068  {
2069  wxString msg = wxString::Format(
2070  _( "Library \"%s\" is read only" ),
2071  GetChars( aLibraryPath )
2072  );
2073 
2074  THROW_IO_ERROR( msg );
2075  }
2076 
2077  wxString footprintName = aFootprint->GetFPID().GetLibItemName();
2078 
2079  MODULE_MAP& mods = m_cache->GetModules();
2080 
2081  // Quietly overwrite module and delete module file from path for any by same name.
2082  wxFileName fn( aLibraryPath, aFootprint->GetFPID().GetLibItemName(),
2084 
2085  if( !fn.IsOk() )
2086  {
2087  THROW_IO_ERROR( wxString::Format( _( "Footprint file name \"%s\" is not valid." ),
2088  GetChars( fn.GetFullPath() ) ) );
2089  }
2090 
2091  if( fn.FileExists() && !fn.IsFileWritable() )
2092  {
2093  THROW_IO_ERROR( wxString::Format( _( "user does not have write permission to delete file \"%s\" " ),
2094  GetChars( fn.GetFullPath() ) ) );
2095  }
2096 
2097  MODULE_CITER it = mods.find( footprintName );
2098 
2099  if( it != mods.end() )
2100  {
2101  wxLogTrace( traceKicadPcbPlugin, wxT( "Removing footprint library file '%s'." ),
2102  fn.GetFullPath().GetData() );
2103  mods.erase( footprintName );
2104  wxRemoveFile( fn.GetFullPath() );
2105  }
2106 
2107  // I need my own copy for the cache
2108  MODULE* module = new MODULE( *aFootprint );
2109 
2110  // and it's time stamp must be 0, it should have no parent, orientation should
2111  // be zero, and it should be on the front layer.
2112  module->SetTimeStamp( 0 );
2113  module->SetParent( 0 );
2114  module->SetOrientation( 0 );
2115 
2116  if( module->GetLayer() != F_Cu )
2117  module->Flip( module->GetPosition() );
2118 
2119  wxLogTrace( traceKicadPcbPlugin, wxT( "Creating s-expression footprint file: %s." ),
2120  fn.GetFullPath().GetData() );
2121  mods.insert( footprintName, new FP_CACHE_ITEM( module, fn ) );
2122  m_cache->Save( module );
2123 }
2124 
2125 
2126 void PCB_IO::FootprintDelete( const wxString& aLibraryPath, const wxString& aFootprintName, const PROPERTIES* aProperties )
2127 {
2128  LOCALE_IO toggle; // toggles on, then off, the C locale.
2129 
2130  init( aProperties );
2131 
2132  validateCache( aLibraryPath );
2133 
2134  if( !m_cache->IsWritable() )
2135  {
2136  THROW_IO_ERROR( wxString::Format( _( "Library \"%s\" is read only" ),
2137  aLibraryPath.GetData() ) );
2138  }
2139 
2140  m_cache->Remove( aFootprintName );
2141 }
2142 
2143 
2144 
2145 long long PCB_IO::GetLibraryTimestamp( const wxString& aLibraryPath ) const
2146 {
2147  // If we have no cache, return a number which won't match any stored timestamps
2148  if( !m_cache || !m_cache->IsPath( aLibraryPath ) )
2149  return wxDateTime::Now().GetValue().GetValue();
2150 
2151  return m_cache->GetTimestamp();
2152 }
2153 
2154 
2155 void PCB_IO::FootprintLibCreate( const wxString& aLibraryPath, const PROPERTIES* aProperties )
2156 {
2157  if( wxDir::Exists( aLibraryPath ) )
2158  {
2159  THROW_IO_ERROR( wxString::Format( _( "cannot overwrite library path \"%s\"" ),
2160  aLibraryPath.GetData() ) );
2161  }
2162 
2163  LOCALE_IO toggle;
2164 
2165  init( aProperties );
2166 
2167  delete m_cache;
2168  m_cache = new FP_CACHE( this, aLibraryPath );
2169  m_cache->Save();
2170 }
2171 
2172 
2173 bool PCB_IO::FootprintLibDelete( const wxString& aLibraryPath, const PROPERTIES* aProperties )
2174 {
2175  wxFileName fn;
2176  fn.SetPath( aLibraryPath );
2177 
2178  // Return if there is no library path to delete.
2179  if( !fn.DirExists() )
2180  return false;
2181 
2182  if( !fn.IsDirWritable() )
2183  {
2184  THROW_IO_ERROR( wxString::Format( _( "user does not have permission to delete directory \"%s\"" ),
2185  aLibraryPath.GetData() ) );
2186  }
2187 
2188  wxDir dir( aLibraryPath );
2189 
2190  if( dir.HasSubDirs() )
2191  {
2192  THROW_IO_ERROR( wxString::Format( _( "library directory \"%s\" has unexpected sub-directories" ),
2193  aLibraryPath.GetData() ) );
2194  }
2195 
2196  // All the footprint files must be deleted before the directory can be deleted.
2197  if( dir.HasFiles() )
2198  {
2199  unsigned i;
2200  wxFileName tmp;
2201  wxArrayString files;
2202 
2203  wxDir::GetAllFiles( aLibraryPath, &files );
2204 
2205  for( i = 0; i < files.GetCount(); i++ )
2206  {
2207  tmp = files[i];
2208 
2209  if( tmp.GetExt() != KiCadFootprintFileExtension )
2210  {
2211  THROW_IO_ERROR( wxString::Format( _( "unexpected file \"%s\" was found in library path \"%s\"" ),
2212  files[i].GetData(), aLibraryPath.GetData() ) );
2213  }
2214  }
2215 
2216  for( i = 0; i < files.GetCount(); i++ )
2217  {
2218  wxRemoveFile( files[i] );
2219  }
2220  }
2221 
2222  wxLogTrace( traceKicadPcbPlugin, wxT( "Removing footprint library \"%s\"" ),
2223  aLibraryPath.GetData() );
2224 
2225  // Some of the more elaborate wxRemoveFile() crap puts up its own wxLog dialog
2226  // we don't want that. we want bare metal portability with no UI here.
2227  if( !wxRmdir( aLibraryPath ) )
2228  {
2229  THROW_IO_ERROR( wxString::Format( _( "footprint library \"%s\" cannot be deleted" ),
2230  aLibraryPath.GetData() ) );
2231  }
2232 
2233  // For some reason removing a directory in Windows is not immediately updated. This delay
2234  // prevents an error when attempting to immediately recreate the same directory when over
2235  // writing an existing library.
2236 #ifdef __WINDOWS__
2237  wxMilliSleep( 250L );
2238 #endif
2239 
2240  if( m_cache && !m_cache->IsPath( aLibraryPath ) )
2241  {
2242  delete m_cache;
2243  m_cache = NULL;
2244  }
2245 
2246  return true;
2247 }
2248 
2249 
2250 bool PCB_IO::IsFootprintLibWritable( const wxString& aLibraryPath )
2251 {
2252  LOCALE_IO toggle;
2253 
2254  init( NULL );
2255 
2256  validateCache( aLibraryPath );
2257 
2258  return m_cache->IsWritable();
2259 }
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:673
friend class FP_CACHE
KICAD_T Type() const
Function Type()
Definition: base_struct.h:209
wxString GetPath() const
#define DIM(x)
of elements in an array
Definition: macros.h:98
MODULE_MAP::iterator MODULE_ITER
int m_SolderMaskMargin
Solder mask margin.
BOARD_ITEM_CONTAINER * GetParent() const
Class ZONE_CONTAINER handles a list of polygons defining a copper zone.
Definition: class_zone.h:60
OUTPUTFORMATTER * m_out
output any Format()s to this, no ownership
LINE_READER * m_reader
no ownership here.
PAD_SHAPE_T GetAnchorPadShape() const
Function GetAnchorPadShape.
Definition: class_pad.h:226
bool IsModified()
Function IsModified Return true if the cache is not up-to-date.
TEXTE_MODULE & Reference()
Definition: class_module.h:502
const std::vector< PAD_CS_PRIMITIVE > & GetPrimitives() const
Accessor to the basic shape list.
Definition: class_pad.h:336
std::unique_ptr< MODULE > m_module
const ZONE_SETTINGS & GetZoneSettings() const
Definition: class_board.h:556
int GetVisibleElements() const
Function GetVisibleElements returns a bit-mask of all the element categories that are visible...
Definition: typeinfo.h:85
#define CTL_OMIT_AT
Omit position and rotation.
int GetThermalGap() const
Definition: class_module.h:222
BOARD_ITEM * Parse(const wxString &aClipboardSourceInput)
const wxPoint & GetBezControl2() const
Class PCB_IO is a PLUGIN derivation for saving and loading Pcbnew s-expression formatted files...
BOARD * Load(const wxString &aFileName, BOARD *aAppendToMe, const PROPERTIES *aProperties=NULL) override
Function Load loads information from some input file format that this PLUGIN implementation knows abo...
NETCLASSPTR GetDefault() const
Function GetDefault.
int GetNumCorners(void) const
Access to m_Poly parameters.
Definition: class_zone.h:414
PAD_ATTR_T GetAttribute() const
Definition: class_pad.h:405
TEXTE_PCB class definition.
const wxPoint & GetTextPos() const
Definition: eda_text.h:222
const wxPoint & GetGridOrigin() const
Definition: class_board.h:350
like PAD_STANDARD, but not plated mechanical use only, no connection allowed
Definition: pad_shapes.h:65
int m_SolderPasteMargin
Solder paste margin absolute value.
Class LOCALE_IO is a class that can be instantiated within a scope in which you are expecting excepti...
Definition: common.h:166
wxFileName m_lib_path
Plugin object that owns the cache.
Implementation of conversion functions that require both schematic and board internal units...
int GetMinThickness() const
Definition: class_zone.h:198
wxPoint m_crossBarF
int GetWidth() const
int m_ModuleTextWidth
Default footprint texts thickness.
Class BOARD_ITEM is a base class for any item which can be embedded within the BOARD container class...
bool GetDoNotAllowCopperPour() const
Definition: class_zone.h:603
int GetLocalClearance() const
Definition: class_module.h:206
std::vector< int > m_TrackWidthList
Track width list.
void formatGeneral(BOARD *aBoard, int aNestLevel=0) const
formats the General section of the file
int m_ModuleSegmentWidth
Default width for all graphic lines.
wxPoint m_arrowD1F
TEXT_TYPE GetType() const
FP_CACHE * m_cache
Footprint library cache.
virtual PCB_LAYER_ID GetLayer() const
Function GetLayer returns the primary layer this item is on.
int PointCount() const
Function PointCount()
int GetArcSegmentCount() const
Definition: class_zone.h:187
const wxPoint & GetPos0() const
Class PCB_PARSER reads a Pcbnew s-expression formatted LINE_READER object and returns the appropriate...
Definition: pcb_parser.h:65
int m_PcbTextWidth
current Pcb (not module) Text width
const PROPERTIES * m_props
passed via Save() or Load(), no ownership, may be NULL.
int GetWidth() const
LSEQ CuStack() const
Function CuStack returns a sequence of copper layers in starting from the front/top and extending to ...
Definition: lset.cpp:147
int GetPadToDieLength() const
Definition: class_pad.h:408
Class BOARD to handle a board.
void init(const PROPERTIES *aProperties)
bool IsFilled() const
Definition: class_zone.h:189
wxPoint m_Start
angle of an arc, from its starting point, in 0.1 deg
Definition: class_pad.h:100
const wxString KiCadFootprintFileExtension
polygon (not yet used for tracks, but could be in microwave apps)
int GetLocalSolderMaskMargin() const
Definition: class_pad.h:410
MODULE * Next() const
Definition: class_module.h:121
const wxPoint & GetEnd0() const
Smd pad, appears on the solder paste layer (default)
Definition: pad_shapes.h:61
CONST_ITERATOR CIterate(int aFirst, int aLast, bool aIterateHoles=false) const
class ZONE_CONTAINER, a zone area
Definition: typeinfo.h:102
long long GetTimestamp()
Function GetTimestamp Generate a timestamp representing all source files in the cache (including the ...
ZONE_SEGMENT_FILL & FillSegments()
Definition: class_zone.h:233
void formatLayer(const BOARD_ITEM *aItem) const
Set for modules listed in the automatic insertion list (usually SMD footprints)
Definition: class_module.h:77
#define SEXPR_BOARD_FILE_VERSION
Current s-expression file format version. 2 was the last legacy format version.
class TEXTE_PCB, text on a layer
Definition: typeinfo.h:92
void FootprintEnumerate(wxArrayString &aFootprintNames, const wxString &aLibraryPath, const PROPERTIES *aProperties=NULL) override
Return a list of footprint names contained within the library at aLibraryPath.
wxString GetName() const
Classes to handle copper zones.
const wxPoint & GetPos0() const
Definition: class_pad.h:263
int GetCornerSmoothingType() const
Definition: class_zone.h:570
PAD_DRILL_SHAPE_T GetDrillShape() const
Definition: class_pad.h:388
bool IsFootprintLibWritable(const wxString &aLibraryPath) override
Function IsFootprintLibWritable returns true iff the library at aLibraryPath is writable.
usual segment : line with rounded ends
class D_PAD, a pad in a footprint
Definition: typeinfo.h:90
iterator end()
Definition: netclass.h:256
std::string Double2Str(double aValue)
Helper function Double2Str to print a float number without using scientific notation and no trailing ...
Definition: base_units.cpp:61
timestamp_t GetLastEditTime() const
Definition: class_module.h:311
wxPoint m_featureLineDF
ZoneConnection GetZoneConnection() const
Definition: class_module.h:216
wxFileName GetFileName() const
bool IsPolyShapeValid() const
const wxSize & GetDrillSize() const
Definition: class_pad.h:275
int GetPlacementCost90() const
Definition: class_module.h:572
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:51
STATUS_FLAGS GetStatus() const
Definition: base_struct.h:261
wxSize m_ModuleTextSize
Default footprint texts size.
Class PROPERTIES is a name/value tuple with unique names and optional values.
Definition: properties.h:34
long long GetLibraryTimestamp(const wxString &aLibraryPath) const override
Generate a timestamp representing all the files in the library (including the library directory)...
#define cu(a)
Definition: auxiliary.h:88
int GetThermalReliefCopperBridge(D_PAD *aPad=NULL) const
Definition: class_zone.cpp:637
PAD_SHAPE_T GetShape() const
Function GetShape.
Definition: class_pad.h:216
int GetDrill() const
Function GetDrill returns the local drill setting for this VIA.
Definition: class_track.h:470
LSET GetEnabledLayers() const
Function GetEnabledLayers is a proxy function that calls the corresponding function in m_BoardSetting...
unsigned int GetCornerRadius() const
Definition: class_zone.h:574
wxString GetLayerName() const
Function GetLayerName returns the name of the PCB layer on which the item resides.
class EDGE_MODULE, a footprint edge
Definition: typeinfo.h:94
double GetLocalSolderPasteMarginRatio() const
Definition: class_module.h:212
NETCLASS_MAP::const_iterator const_iterator
Definition: netclass.h:258
FP_CACHE_ITEM(MODULE *aModule, const wxFileName &aFileName)
const wxPoint & GetEnd() const
Definition: class_track.h:119
iterator begin()
Definition: netclass.h:142
double GetTextAngle() const
Definition: eda_text.h:162
Functions relatives to tracks, vias and segments used to fill zones.
class TRACK, a track segment (segment on a copper layer)
Definition: typeinfo.h:95
void Format(OUTPUTFORMATTER *aFormatter, int aNestLevel, int aControlBits) const
Function Format outputs the net class to aFormatter in s-expression form.
Definition: netclass.cpp:253
This file contains miscellaneous commonly used macros and functions.
static const char * ShowType(LAYER_T aType)
Function ShowType converts a LAYER_T enum to a const char*.
long long m_cache_timestamp
Pads are not covered.
Definition: zones.h:57
wxPoint m_featureLineGO
bool GetIsKeepout() const
Accessors to parameters used in Keepout zones:
Definition: class_zone.h:602
static struct PcbQueue * Save
Definition: queue.cpp:56
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:364
int StrPrintf(std::string *result, const char *format,...)
Function StrPrintf is like sprintf() but the output is appended to a std::string instead of to a char...
Definition: richio.cpp:74
BOARD_ITEM * Next() const
virtual LSET GetLayerSet() const override
Function GetLayerSet returns a "layer mask", which is a bitmap of all layers on which the TRACK segme...
Definition: class_zone.cpp:230
void formatBoardLayers(BOARD *aBoard, int aNestLevel=0) const
formats the board layer information
MODULE * GetModule() const
const LIB_ID & GetFPID() const
Definition: class_module.h:191
#define TO_UTF8(wxstring)
Macro TO_UTF8 converts a wxString to a UTF8 encoded C string for all wxWidgets build modes...
Definition: macros.h:47
DIMENSION class definition.
MODULE_MAP m_modules
The path of the library.
class MODULE, a footprint
Definition: typeinfo.h:89
void Remove(const wxString &aFootprintName)
MODULE_MAP::const_iterator MODULE_CITER
#define FMT_IU
PCB_IO(int aControlFlags=CTL_FOR_BOARD)
boost::ptr_map< wxString, FP_CACHE_ITEM > MODULE_MAP
STRINGSET::iterator iterator
Definition: netclass.h:141
PCB_LAYER_ID
A quick note on layer IDs:
Class FILE_LINE_READER is a LINE_READER that reads from an open file.
Definition: richio.h:180
STROKE_T GetShape() const
wxString GetRequiredVersion()
Return a string representing the version of kicad required to open this file.
Definition: pcb_parser.cpp:182
MODULE * LoadEnumeratedFootprint(const wxString &aLibraryPath, const wxString &aFootprintName, const PROPERTIES *aProperties=NULL) override
Function LoadEnumeratedFootprint a version of FootprintLoad() for use after FootprintEnumerate() for ...
int GetShape() const
int m_Radius
thickness of segment or outline For filled S_CIRCLE shape, thickness = 0.
Definition: class_pad.h:98
void FootprintLibCreate(const wxString &aLibraryPath, const PROPERTIES *aProperties=NULL) override
Function FootprintLibCreate creates a new empty footprint library at aLibraryPath empty...
Class LSET is a set of PCB_LAYER_IDs.
iterator begin()
Definition: netclass.h:255
#define THROW_PARSE_ERROR(aProblem, aSource, aInputLine, aLineNumber, aByteIndex)
Definition: ki_exception.h:133
ZoneConnection GetPadConnection(D_PAD *aPad=NULL) const
Definition: class_zone.cpp:997
void SetBoard(BOARD *aBoard)
Definition: pcb_parser.h:296
const wxPoint & GetEnd() const
Function GetEnd returns the ending point of the graphic.
bool IsUnlocked()
std::vector< wxPoint > m_Poly
is also the start point of the arc
Definition: class_pad.h:102
VIATYPE_T GetViaType() const
Definition: class_track.h:455
void FootprintSave(const wxString &aLibraryPath, const MODULE *aFootprint, const PROPERTIES *aProperties=NULL) override
Function FootprintSave will write aModule to an existing library located at aLibraryPath.
iterator end()
Definition: netclass.h:143
wxString GetBuildVersion()
Function GetBuildVersion Return the build version string.
bool IsPath(const wxString &aPath) const
Function IsPath checks if aPath is the same as the current cache path.
wxFileName m_file_name
The the full file name and path of the footprint to cache.
double GetOrientation() const
Definition: class_module.h:187
Class SHAPE_POLY_SET.
TEXTE_MODULE & Value()
read/write accessors:
Definition: class_module.h:501
SHAPE_LINE_CHAIN & Outline(int aIndex)
Returns the reference to aIndex-th outline in the set
wxSize m_PcbTextSize
current Pcb (not module) Text size
MODULE * FootprintLoad(const wxString &aLibraryPath, const wxString &aFootprintName, const PROPERTIES *aProperties=NULL) override
Function FootprintLoad loads a footprint having aFootprintName from the aLibraryPath containing a lib...
const UTF8 & GetLibItemName() const
Definition: lib_id.h:118
void validateCache(const wxString &aLibraryPath, bool checkModified=true)
void formatNetInformation(BOARD *aBoard, int aNestLevel=0) const
formats the Nets and Netclasses
const wxPoint & GetStart() const
Definition: class_track.h:122
int m_TrackMinWidth
track min value for width ((min copper size value
const wxString & GetText() const
Function GetText returns the string associated with the text object.
Definition: eda_text.h:128
Arcs (with rounded ends)
int m_ViasMinSize
vias (not micro vias) min diameter
int m_loading_format_version
which SEXPR_BOARD_FILE_VERSION should be Load()ed?
Class NETCLASS handles a collection of nets and the parameters used to route or test these nets...
Definition: netclass.h:55
Helper class to handle a primitive (basic shape: polygon, segment, circle or arc) to build a custom p...
Definition: class_pad.h:91
ZoneConnection GetZoneConnection() const
Definition: class_pad.cpp:650
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:402
wxPoint m_arrowG1F
int GetThermalReliefGap(D_PAD *aPad=NULL) const
Definition: class_zone.cpp:628
const wxString GetLayerName(PCB_LAYER_ID aLayer) const
Function GetLayerName returns the name of a layer given by aLayer.
int m_DrawSegmentWidth
current graphic line width (not EDGE layer)
wxPoint m_arrowD2F
wxPoint m_arrowG2F
int GetThermalWidth() const
Definition: class_pad.cpp:661
int m_ViasMinDrill
vias (not micro vias) min drill diameter
D_PAD * Next() const
Definition: class_pad.h:160
ZONE_FILL_MODE GetFillMode() const
Definition: class_zone.h:175
int GetThermalGap() const
Definition: class_pad.cpp:672
The common library.
const wxChar *const traceKicadPcbPlugin
Flag to enable GEDA PCB plugin debug output.
const PCB_PLOT_PARAMS & GetPlotOptions() const
Definition: class_board.h:550
const wxSize & GetSize() const
Definition: class_pad.h:269
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Function GetDesignSettings.
Definition: class_board.h:532
int m_ZoneClearance
Clearance value.
Definition: zone_settings.h:63
int GetAttributes() const
Definition: class_module.h:224
virtual void Format(OUTPUTFORMATTER *aFormatter, int aNestLevel, int aControlBits) const
Function Format outputs the object to aFormatter in s-expression form.
Definition: worksheet.cpp:246
const wxString & GetName() const
Definition: class_pad.h:190
PCB_IO * m_owner
wxLogTrace helper definitions.
const wxString & GetKeywords() const
Definition: class_module.h:197
const wxPoint & GetBezControl1() const
bool m_BlindBuriedViaAllowed
true to allow blind/buried vias
int GetLocalSolderPasteMargin() const
Definition: class_pad.h:416
std::string Quotew(const wxString &aWrapee)
Definition: richio.cpp:482
int GetAreaCount() const
Function GetAreaCount.
Definition: class_board.h:1011
SHAPE_POLY_SET & GetPolyShape()
std::map< wxString, MODULE * > MODULE_MAP
Definition: eagle_plugin.h:35
void SetFileName(const wxString &aFileName)
Definition: class_board.h:232
Bezier Curve.
#define UNDEFINED_DRILL_DIAMETER
Definition: class_track.h:59
PCB_PARSER * m_parser
int GetNet() const
Function GetNet.
Definition: netinfo.h:227
void formatSetup(BOARD *aBoard, int aNestLevel=0) const
formats the board setup information
int GetLocalSolderMaskMargin() const
Definition: class_module.h:203
wxPoint m_End
is also the center of the circle and arc
Definition: class_pad.h:101
#define CTL_OMIT_NETS
Omit pads net names (useless in library)
Wrapper class, so you can iterate through NETINFO_ITEM*s, not std::pair<int/wxString, NETINFO_ITEM*>
Definition: netinfo.h:305
BOARD_ITEM * Parse()
Definition: pcb_parser.cpp:442
#define CTL_OMIT_PATH
Omit component sheet time stamp (useless in library)
Class FP_CACHE_ITEM is helper class for creating a footprint library cache.
int m_MicroViasMinSize
micro vias (not vias) min diameter
void Format(BOARD_ITEM *aItem, int aNestLevel=0) const
Function Format outputs aItem to aFormatter in s-expression format.
Like smd, does not appear on the solder paste layer (default) note also has a special attribute in Ge...
Definition: pad_shapes.h:62
class DIMENSION, a dimension (graphic item)
Definition: typeinfo.h:100
void Save(MODULE *aModule=NULL)
Function Save Save the footprint cache or a single module from it to disk.
bool IsLocked() const override
Function IsLocked.
Definition: class_module.h:263
NETINFO_MAPPING * m_mapping
mapping for net codes, so only not empty net codes are stored with consecutive integers as net codes ...
STROKE_T m_Shape
Definition: class_pad.h:94
bool IsWritable() const
const SHAPE_POLY_SET & GetFilledPolysList() const
Function GetFilledPolysList returns a reference to the list of filled polygons.
Definition: class_zone.h:534
int GetNumSegmTrack() const
Functions to get some items count.
Thermal relief only for THT pads.
Definition: zones.h:60
Class LSEQ is a sequence (and therefore also a set) of PCB_LAYER_IDs.
const PAGE_INFO & GetPageSettings() const
Definition: class_board.h:547
int GetLocalClearance() const
Definition: class_pad.h:413
LINE_READER * SetLineReader(LINE_READER *aReader)
Function SetLineReader sets aLineReader into the parser, and returns the previous one...
Definition: pcb_parser.h:289
class PCB_TARGET, a target (graphic item)
Definition: typeinfo.h:101
void LayerPair(PCB_LAYER_ID *top_layer, PCB_LAYER_ID *bottom_layer) const
Function LayerPair Return the 2 layers used by the via (the via actually uses all layers between thes...
int LAYER_NUM
Type LAYER_NUM can be replaced with int and removed.
int GetLocalSolderPasteMargin() const
Definition: class_module.h:209
double GetLocalSolderPasteMarginRatio() const
Definition: class_pad.h:419
MODULE * doLoadFootprint(const wxString &aLibraryPath, const wxString &aFootprintName, const PROPERTIES *aProperties, bool checkModified)
void FootprintDelete(const wxString &aLibraryPath, const wxString &aFootprintName, const PROPERTIES *aProperties=NULL) override
Function FootprintDelete deletes aFootprintName from the library at aLibraryPath. ...
default
Definition: class_module.h:76
void formatHeader(BOARD *aBoard, int aNestLevel=0) const
writes everything that comes before the board_items, like settings and layers etc ...
int GetNetCode() const
Function GetNetCode.
int m_Thickness
S_SEGMENT, S_ARC, S_CIRCLE, S_POLYGON only (same as DRAWSEGMENT)
Definition: class_pad.h:95
class TEXTE_MODULE, text in a footprint
Definition: typeinfo.h:93
const wxPoint & GetAuxOrigin() const
Definition: class_board.h:343
wxString GetSelectMenuText() const override
Function GetSelectMenuText returns the text to display to be used in the selection clarification cont...
void Load()
Class NETINFO_ITEM handles the data for a net.
Definition: netinfo.h:69
bool IsTooRecent()
Return whether a version number, if any was parsed, was too recent.
Definition: pcb_parser.h:314
void Format(OUTPUTFORMATTER *aFormatter, int aNestLevel, int aControlBits) const
Function GetStandardSizes returns the standard page types, such as "A4", "A3", etc.
Definition: page_info.cpp:267
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:33
MODULE_MAP::const_iterator MODULE_CITER
std::list< MODULE_3D_SETTINGS > & Models()
Definition: class_module.h:177
void Remove(iterator aName)
Function Remove will remove NET name aName from the collection of members.
Definition: netclass.h:153
Struct PARSE_ERROR contains a filename or source description, a problem input line, a line number, a byte offset, and an error message which contains the the caller&#39;s report and his call site information: CPP source file, function, and line number.
Definition: ki_exception.h:94
bool GetDoNotAllowTracks() const
Definition: class_zone.h:605
STRING_FORMATTER m_sf
#define CTL_FOR_LIBRARY
Format output for a footprint library instead of clipboard or BOARD.
TRACK * Next() const
Definition: class_track.h:99
static const wxChar * GetChars(const wxString &s)
Function GetChars returns a wxChar* to the actual wxChar* data within a wxString, and is helpful for ...
Definition: macros.h:92
double GetAngle() const
virtual wxString GetClass() const =0
Function GetClass returns the class name.
TITLE_BLOCK & GetTitleBlock()
Definition: class_board.h:553
const wxString & GetNetname() const
Function GetNetname.
ZONE_CONTAINER * GetArea(int index) const
Function GetArea returns the Area (Zone Container) at a given index.
Definition: class_board.h:982
double GetRoundRectRadiusRatio() const
has meaning only for rounded rect pads
Definition: class_pad.h:615
Class to handle a graphic segment.
const wxPoint & GetStart0() const
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
const wxString & GetDescription() const
Definition: class_module.h:194
bool GetDoNotAllowVias() const
Definition: class_zone.h:604
Class BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:169
CUST_PAD_SHAPE_IN_ZONE GetCustomShapeInZoneOpt() const
Definition: class_pad.h:232
DLIST< MODULE > m_Modules
Definition: class_board.h:245
Class SHAPE_LINE_CHAIN.
bool IsVisible() const
Definition: eda_text.h:174
int GetWidth() const
Definition: class_track.h:116
SHAPE_POLY_SET::ITERATOR IterateWithHoles()
Function IterateWithHoles returns an iterator to visit all points of the zone&#39;s main outline with hol...
Definition: class_zone.h:434
Virtual component: when created by copper shapes on board (Like edge card connectors, mounting hole...)
Definition: class_module.h:79
#define CTL_OMIT_TSTAMPS
Omit component time stamp (useless in library)
T NormalizeAnglePos(T Angle)
Normalize angle to be in the 0.0 .
Definition: trigo.h:233
TEXTE_PCB & Text()
LSET GetVisibleLayers() const
Function GetVisibleLayers is a proxy function that calls the correspondent function in m_BoardSetting...
#define CTL_STD_LAYER_NAMES
Use English Standard layer names.
size_t i
Definition: json11.cpp:597
double GetOrientation() const
Function GetOrientation returns the rotation angle of the pad in tenths of degrees, but soon degrees.
Definition: class_pad.h:382
D_PAD m_Pad_Master
A dummy pad to store all default parameters.
int GetNumSegmZone() const
Calculate the zone segment count.
void format(BOARD *aBoard, int aNestLevel=0) const
BOARD * m_board
which BOARD, no ownership here
const wxSize & GetDelta() const
Definition: class_pad.h:272
Usual pad.
Definition: pad_shapes.h:60
HATCH_STYLE GetHatchStyle() const
Definition: class_zone.h:498
NETINFO_ITEM * FindNet(int aNetcode) const
Function FindNet searches for a net with the given netcode.
The common library.
int GetWidth() const
std::vector< VIA_DIMENSION > m_ViasDimensionsList
Vias size and drill list.
UTF8 Format() const
Definition: lib_id.cpp:263
Class FILE_OUTPUTFORMATTER may be used for text file output.
Definition: richio.h:492
int GetSize() const
unsigned GetCount() const
Function GetCount returns the number of elements in the list.
Definition: dlist.h:126
void formatLayers(LSET aLayerMask, int aNestLevel=0) const
wxPoint m_crossBarO
KICAD_PLUGIN_EXPORT SCENEGRAPH * Load(char const *aFileName)
reads a model file and creates a generic display structure
PCB_TARGET class definition.
DLIST< BOARD_ITEM > & GraphicalItemsList()
Definition: class_module.h:164
class VIA, a via (like a track segment on a copper layer)
Definition: typeinfo.h:96
Pcbnew s-expression file format parser definition.
DLIST< D_PAD > & PadsList()
Definition: class_module.h:161
DLIST< TRACK > m_Track
Definition: class_board.h:246
int GetHatchPitch() const
Hatch related methods.
void filterNetClass(const BOARD &aBoard, NETCLASS &aNetClass)
Removes empty nets (i.e. with node count equal zero) from net classes
Struct FUTURE_FORMAT_ERROR variant of PARSE_ERROR indicating that a syntax or related error was likel...
Definition: ki_exception.h:143
FP_CACHE(PCB_IO *aOwner, const wxString &aLibraryPath)
Module description (excepted pads)
bool IsEmpty() const
Returns true if the set is empty (no polygons at all)
bool m_MicroViasAllowed
true to allow micro vias
int GetValue() const
bool m_cache_dirty
Map of footprint file name per MODULE*.
int m_MicroViasMinDrill
micro vias (not vias) min drill diameter
void Format(OUTPUTFORMATTER *aFormatter, int aNestLevel, int aControl=0) const
int PRINTF_FUNC Print(int nestLevel, const char *fmt,...)
Function Print formats and writes text to the output stream.
Definition: richio.cpp:404
#define CTL_OMIT_INITIAL_COMMENTS
omit MODULE initial comments
MODULE_MAP::iterator MODULE_ITER
const wxString & GetPath() const
Definition: class_module.h:200
int m_EdgeSegmentWidth
current graphic line width (EDGE layer only)
double m_SolderPasteMarginRatio
Solder pask margin ratio value of pad size The final margin is the sum of these 2 values...
unsigned GetPriority() const
Function GetPriority.
Definition: class_zone.h:101
int GetCurrentTrackWidth() const
Function GetCurrentTrackWidth.
EDGE_MODULE class definition.
class DRAWSEGMENT, a segment not on copper layers
Definition: typeinfo.h:91
T NormalizeAngle360Min(T Angle)
Normalize angle to be > -360.0 and < 360.0 Angle equal to -360 or +360 are set to 0...
Definition: trigo.h:222
bool FootprintLibDelete(const wxString &aLibraryPath, const PROPERTIES *aProperties=NULL) override
Function FootprintLibDelete deletes an existing footprint library and returns true, or if library does not exist returns false, or throws an exception if library exists but is read only or cannot be deleted for some other reason.
const wxArrayString * GetInitialComments() const
Return the initial comments block or NULL if none, without transfer of ownership. ...
Definition: class_module.h:696
const wxPoint & GetStart() const
Function GetStart returns the starting point of the graphic.
Class STRING_LINE_READER is a LINE_READER that reads from a multiline 8 bit wide std::string.
Definition: richio.h:254
#define CTL_OMIT_HIDE
Definition: eda_text.h:42
const VECTOR2I & CPoint(int aIndex) const
Function CPoint()
bool IsPlaced() const
Definition: class_module.h:281
wxPoint m_featureLineDO
static const int UNCONNECTED
Constant that holds the "unconnected net" number (typically 0) all items "connected" to this net are ...
Definition: netinfo.h:461
Struct IO_ERROR is a class used to hold an error message and may be used when throwing exceptions con...
Definition: ki_exception.h:47
int GetPlacementCost180() const
Definition: class_module.h:569
#define THROW_IO_ERROR(msg)
Definition: ki_exception.h:38
const wxPoint GetPosition() const override
Definition: class_module.h:182
LAYER_T GetLayerType(PCB_LAYER_ID aLayer) const
Function GetLayerType returns the type of the copper layer given by aLayer.
void SetFPID(const LIB_ID &aFPID)
Definition: class_module.h:192
Use thermal relief for pads.
Definition: zones.h:58
std::shared_ptr< CONNECTIVITY_DATA > GetConnectivity() const
Function GetConnectivity() returns list of missing connections between components/tracks.
Definition: class_board.h:290
#define FMT_ANGLE
Class DIMENSION.
int GetZoneClearance() const
Definition: class_zone.h:192
const wxPoint & GetOffset() const
Definition: class_pad.h:278
double m_ArcAngle
radius of a circle
Definition: class_pad.h:99
int GetThermalWidth() const
Definition: class_module.h:219
MODULE_MAP & GetModules()
DLIST_ITERATOR_WRAPPER< BOARD_ITEM > Drawings()
Definition: class_board.h:251
virtual void Save(const wxString &aFileName, BOARD *aBoard, const PROPERTIES *aProperties=NULL) override
Function Save will write aBoard to a storage file in a format that this PLUGIN implementation knows a...
const wxPoint GetPosition() const override
NETCLASSES m_NetClasses
List of current netclasses. There is always the default netclass.
timestamp_t GetTimeStamp() const
Definition: base_struct.h:215
int m_SolderMaskMinWidth
Solder mask min width.
static wxString GetStandardLayerName(PCB_LAYER_ID aLayerId)
Function GetStandardLayerName returns an "English Standard" name of a PCB layer when given aLayerNumb...
Definition: class_board.h:647
pads are covered by copper
Definition: zones.h:59
Class BOARD_DESIGN_SETTINGS contains design settings for a BOARD object.
wxPoint m_featureLineGF