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-2021 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 <advanced_config.h>
26 #include <base_units.h>
27 #include <board.h>
28 #include <boost/ptr_container/ptr_map.hpp>
29 #include <confirm.h>
30 #include <convert_basic_shapes_to_polygon.h> // for enum RECT_CHAMFER_POSITIONS definition
31 #include <core/arraydim.h>
32 #include <dimension.h>
33 #include <footprint.h>
34 #include <fp_shape.h>
35 #include <kicad_string.h>
36 #include <kiface_i.h>
37 #include <locale_io.h>
38 #include <macros.h>
39 #include <pcb_shape.h>
40 #include <pcb_target.h>
41 #include <pcb_text.h>
42 #include <pcbnew_settings.h>
45 #include <trace_helpers.h>
46 #include <track.h>
48 #include <wx/dir.h>
49 #include <wx_filename.h>
50 #include <zone.h>
51 #include <zones.h>
52 
53 using namespace PCB_KEYS_T;
54 
55 
65 {
67  std::unique_ptr<FOOTPRINT> m_footprint;
68 
69 public:
70  FP_CACHE_ITEM( FOOTPRINT* aFootprint, const WX_FILENAME& aFileName );
71 
72  const WX_FILENAME& GetFileName() const { return m_filename; }
73  const FOOTPRINT* GetFootprint() const { return m_footprint.get(); }
74 };
75 
76 
77 FP_CACHE_ITEM::FP_CACHE_ITEM( FOOTPRINT* aFootprint, const WX_FILENAME& aFileName ) :
78  m_filename( aFileName ),
79  m_footprint( aFootprint )
80 { }
81 
82 
83 typedef boost::ptr_map< wxString, FP_CACHE_ITEM > FOOTPRINT_MAP;
84 
85 
86 class FP_CACHE
87 {
88  PCB_IO* m_owner; // Plugin object that owns the cache.
89  wxFileName m_lib_path; // The path of the library.
90  wxString m_lib_raw_path; // For quick comparisons.
91  FOOTPRINT_MAP m_footprints; // Map of footprint filename to FOOTPRINT*.
92 
93  bool m_cache_dirty; // Stored separately because it's expensive to check
94  // m_cache_timestamp against all the files.
95  long long m_cache_timestamp; // A hash of the timestamps for all the footprint
96  // files.
97 
98 public:
99  FP_CACHE( PCB_IO* aOwner, const wxString& aLibraryPath );
100 
101  wxString GetPath() const { return m_lib_raw_path; }
102 
103  bool IsWritable() const { return m_lib_path.IsOk() && m_lib_path.IsDirWritable(); }
104 
105  bool Exists() const { return m_lib_path.IsOk() && m_lib_path.DirExists(); }
106 
108 
109  // Most all functions in this class throw IO_ERROR exceptions. There are no
110  // error codes nor user interface calls from here, nor in any PLUGIN.
111  // Catch these exceptions higher up please.
112 
118  void Save( FOOTPRINT* aFootprint = nullptr );
119 
120  void Load();
121 
122  void Remove( const wxString& aFootprintName );
123 
129  static long long GetTimestamp( const wxString& aLibPath );
130 
134  bool IsModified();
135 
147  bool IsPath( const wxString& aPath ) const;
148 };
149 
150 
151 FP_CACHE::FP_CACHE( PCB_IO* aOwner, const wxString& aLibraryPath )
152 {
153  m_owner = aOwner;
154  m_lib_raw_path = aLibraryPath;
155  m_lib_path.SetPath( aLibraryPath );
156  m_cache_timestamp = 0;
157  m_cache_dirty = true;
158 }
159 
160 
161 void FP_CACHE::Save( FOOTPRINT* aFootprint )
162 {
163  m_cache_timestamp = 0;
164 
165  if( !m_lib_path.DirExists() && !m_lib_path.Mkdir() )
166  {
167  THROW_IO_ERROR( wxString::Format( _( "Cannot create footprint library path \"%s\"" ),
168  m_lib_raw_path ) );
169  }
170 
171  if( !m_lib_path.IsDirWritable() )
172  {
173  THROW_IO_ERROR( wxString::Format( _( "Footprint library path \"%s\" is read only" ),
174  m_lib_raw_path ) );
175  }
176 
177  for( FOOTPRINT_MAP::iterator it = m_footprints.begin(); it != m_footprints.end(); ++it )
178  {
179  if( aFootprint && aFootprint != it->second->GetFootprint() )
180  continue;
181 
182  WX_FILENAME fn = it->second->GetFileName();
183 
184  wxString tempFileName =
185 #ifdef USE_TMP_FILE
186  wxFileName::CreateTempFileName( fn.GetPath() );
187 #else
188  fn.GetFullPath();
189 #endif
190  // Allow file output stream to go out of scope to close the file stream before
191  // renaming the file.
192  {
193  wxLogTrace( traceKicadPcbPlugin, wxT( "Creating temporary library file %s" ),
194  tempFileName );
195 
196  FILE_OUTPUTFORMATTER formatter( tempFileName );
197 
198  m_owner->SetOutputFormatter( &formatter );
199  m_owner->Format( (BOARD_ITEM*) it->second->GetFootprint() );
200  }
201 
202 #ifdef USE_TMP_FILE
203  wxRemove( fn.GetFullPath() ); // it is not an error if this does not exist
204 
205  // Even on Linux you can see an _intermittent_ error when calling wxRename(),
206  // and it is fully inexplicable. See if this dodges the error.
207  wxMilliSleep( 250L );
208 
209  if( !wxRenameFile( tempFileName, fn.GetFullPath() ) )
210  {
211  wxString msg = wxString::Format(
212  _( "Cannot rename temporary file \"%s\" to footprint library file \"%s\"" ),
213  tempFileName, fn.GetFullPath() );
214  THROW_IO_ERROR( msg );
215  }
216 #endif
218  }
219 
220  m_cache_timestamp += m_lib_path.GetModificationTime().GetValue().GetValue();
221 
222  // If we've saved the full cache, we clear the dirty flag.
223  if( !aFootprint )
224  m_cache_dirty = false;
225 }
226 
227 
229 {
230  m_cache_dirty = false;
231  m_cache_timestamp = 0;
232 
233  wxDir dir( m_lib_raw_path );
234 
235  if( !dir.IsOpened() )
236  {
237  wxString msg = wxString::Format( _( "Footprint library path '%s' does not exist "
238  "(or is not a directory)." ),
239  m_lib_raw_path );
240  THROW_IO_ERROR( msg );
241  }
242 
243  wxString fullName;
244  wxString fileSpec = wxT( "*." ) + KiCadFootprintFileExtension;
245 
246  // wxFileName construction is egregiously slow. Construct it once and just swap out
247  // the filename thereafter.
248  WX_FILENAME fn( m_lib_raw_path, wxT( "dummyName" ) );
249 
250  if( dir.GetFirst( &fullName, fileSpec ) )
251  {
252  wxString cacheError;
253 
254  do
255  {
256  fn.SetFullName( fullName );
257 
258  // Queue I/O errors so only files that fail to parse don't get loaded.
259  try
260  {
261  FILE_LINE_READER reader( fn.GetFullPath() );
262 
263  m_owner->m_parser->SetLineReader( &reader );
264 
265  FOOTPRINT* footprint = (FOOTPRINT*) m_owner->m_parser->Parse();
266  wxString fpName = fn.GetName();
267 
268  footprint->SetFPID( LIB_ID( wxEmptyString, fpName ) );
269  m_footprints.insert( fpName, new FP_CACHE_ITEM( footprint, fn ) );
270  }
271  catch( const IO_ERROR& ioe )
272  {
273  if( !cacheError.IsEmpty() )
274  cacheError += "\n\n";
275 
276  cacheError += ioe.What();
277  }
278  } while( dir.GetNext( &fullName ) );
279 
281 
282  if( !cacheError.IsEmpty() )
283  THROW_IO_ERROR( cacheError );
284  }
285 }
286 
287 
288 void FP_CACHE::Remove( const wxString& aFootprintName )
289 {
290  FOOTPRINT_MAP::const_iterator it = m_footprints.find( aFootprintName );
291 
292  if( it == m_footprints.end() )
293  {
294  wxString msg = wxString::Format( _( "library \"%s\" has no footprint \"%s\" to delete" ),
296  aFootprintName );
297  THROW_IO_ERROR( msg );
298  }
299 
300  // Remove the footprint from the cache and delete the footprint file from the library.
301  wxString fullPath = it->second->GetFileName().GetFullPath();
302  m_footprints.erase( aFootprintName );
303  wxRemoveFile( fullPath );
304 }
305 
306 
307 bool FP_CACHE::IsPath( const wxString& aPath ) const
308 {
309  return aPath == m_lib_raw_path;
310 }
311 
312 
314 {
316 
317  return m_cache_dirty;
318 }
319 
320 
321 long long FP_CACHE::GetTimestamp( const wxString& aLibPath )
322 {
323  wxString fileSpec = wxT( "*." ) + KiCadFootprintFileExtension;
324 
325  return TimestampDir( aLibPath, fileSpec );
326 }
327 
328 
329 void PCB_IO::Save( const wxString& aFileName, BOARD* aBoard, const PROPERTIES* aProperties )
330 {
331  LOCALE_IO toggle; // toggles on, then off, the C locale.
332 
333  wxString sanityResult = aBoard->GroupsSanityCheck();
334 
335  if( sanityResult != wxEmptyString )
336  {
337  KIDIALOG dlg( nullptr, wxString::Format(
338  _( "Please report this bug. Error validating group structure: %s"
339  "\n\nSave anyway?" ), sanityResult ),
340  _( "Internal group data structure corrupt" ),
341  wxOK | wxCANCEL | wxICON_ERROR );
342  dlg.SetOKLabel( _( "Save Anyway" ) );
343 
344  if( dlg.ShowModal() == wxID_CANCEL )
345  return;
346  }
347 
348  init( aProperties );
349 
350  m_board = aBoard; // after init()
351 
352  // Prepare net mapping that assures that net codes saved in a file are consecutive integers
353  m_mapping->SetBoard( aBoard );
354 
355  FILE_OUTPUTFORMATTER formatter( aFileName );
356 
357  m_out = &formatter; // no ownership
358 
359  m_out->Print( 0, "(kicad_pcb (version %d) (generator pcbnew)\n", SEXPR_BOARD_FILE_VERSION );
360 
361  Format( aBoard, 1 );
362 
363  m_out->Print( 0, ")\n" );
364 }
365 
366 
367 BOARD_ITEM* PCB_IO::Parse( const wxString& aClipboardSourceInput )
368 {
369  std::string input = TO_UTF8( aClipboardSourceInput );
370 
371  STRING_LINE_READER reader( input, wxT( "clipboard" ) );
372 
373  m_parser->SetLineReader( &reader );
374 
375  try
376  {
377  return m_parser->Parse();
378  }
379  catch( const PARSE_ERROR& parse_error )
380  {
381  if( m_parser->IsTooRecent() )
382  throw FUTURE_FORMAT_ERROR( parse_error, m_parser->GetRequiredVersion() );
383  else
384  throw;
385  }
386 }
387 
388 
389 void PCB_IO::Format( const BOARD_ITEM* aItem, int aNestLevel ) const
390 {
391  LOCALE_IO toggle; // public API function, perform anything convenient for caller
392 
393  switch( aItem->Type() )
394  {
395  case PCB_T:
396  format( static_cast<const BOARD*>( aItem ), aNestLevel );
397  break;
398 
399  case PCB_DIM_ALIGNED_T:
400  case PCB_DIM_CENTER_T:
402  case PCB_DIM_LEADER_T:
403  format( static_cast<const DIMENSION_BASE*>( aItem ), aNestLevel );
404  break;
405 
406  case PCB_SHAPE_T:
407  format( static_cast<const PCB_SHAPE*>( aItem ), aNestLevel );
408  break;
409 
410  case PCB_FP_SHAPE_T:
411  format( static_cast<const FP_SHAPE*>( aItem ), aNestLevel );
412  break;
413 
414  case PCB_TARGET_T:
415  format( static_cast<const PCB_TARGET*>( aItem ), aNestLevel );
416  break;
417 
418  case PCB_FOOTPRINT_T:
419  format( static_cast<const FOOTPRINT*>( aItem ), aNestLevel );
420  break;
421 
422  case PCB_PAD_T:
423  format( static_cast<const PAD*>( aItem ), aNestLevel );
424  break;
425 
426  case PCB_TEXT_T:
427  format( static_cast<const PCB_TEXT*>( aItem ), aNestLevel );
428  break;
429 
430  case PCB_FP_TEXT_T:
431  format( static_cast<const FP_TEXT*>( aItem ), aNestLevel );
432  break;
433 
434  case PCB_GROUP_T:
435  format( static_cast<const PCB_GROUP*>( aItem ), aNestLevel );
436  break;
437 
438  case PCB_TRACE_T:
439  case PCB_ARC_T:
440  case PCB_VIA_T:
441  format( static_cast<const TRACK*>( aItem ), aNestLevel );
442  break;
443 
444  case PCB_FP_ZONE_T:
445  case PCB_ZONE_T:
446  format( static_cast<const ZONE*>( aItem ), aNestLevel );
447  break;
448 
449  default:
450  wxFAIL_MSG( wxT( "Cannot format item " ) + aItem->GetClass() );
451  }
452 }
453 
454 
455 void PCB_IO::formatLayer( const BOARD_ITEM* aItem ) const
456 {
457  PCB_LAYER_ID layer = aItem->GetLayer();
458 
459  m_out->Print( 0, " (layer %s)", m_out->Quotew( LSET::Name( layer ) ).c_str() );
460 }
461 
462 
463 void PCB_IO::formatSetup( const BOARD* aBoard, int aNestLevel ) const
464 {
465  // Setup
466  m_out->Print( aNestLevel, "(setup\n" );
467 
468  // Save the board physical stackup structure
469  const BOARD_STACKUP& stackup = aBoard->GetDesignSettings().GetStackupDescriptor();
470 
471  if( aBoard->GetDesignSettings().m_HasStackup )
472  stackup.FormatBoardStackup( m_out, aBoard, aNestLevel+1 );
473 
474  BOARD_DESIGN_SETTINGS& dsnSettings = aBoard->GetDesignSettings();
475 
476  m_out->Print( aNestLevel+1, "(pad_to_mask_clearance %s)\n",
477  FormatInternalUnits( dsnSettings.m_SolderMaskMargin ).c_str() );
478 
479  if( dsnSettings.m_SolderMaskMinWidth )
480  m_out->Print( aNestLevel+1, "(solder_mask_min_width %s)\n",
481  FormatInternalUnits( dsnSettings.m_SolderMaskMinWidth ).c_str() );
482 
483  if( dsnSettings.m_SolderPasteMargin != 0 )
484  m_out->Print( aNestLevel+1, "(pad_to_paste_clearance %s)\n",
485  FormatInternalUnits( dsnSettings.m_SolderPasteMargin ).c_str() );
486 
487  if( dsnSettings.m_SolderPasteMarginRatio != 0 )
488  m_out->Print( aNestLevel+1, "(pad_to_paste_clearance_ratio %s)\n",
489  Double2Str( dsnSettings.m_SolderPasteMarginRatio ).c_str() );
490 
491  if( dsnSettings.m_AuxOrigin != wxPoint( 0, 0 ) )
492  m_out->Print( aNestLevel+1, "(aux_axis_origin %s %s)\n",
493  FormatInternalUnits( dsnSettings.m_AuxOrigin.x ).c_str(),
494  FormatInternalUnits( dsnSettings.m_AuxOrigin.y ).c_str() );
495 
496  if( dsnSettings.m_GridOrigin != wxPoint( 0, 0 ) )
497  m_out->Print( aNestLevel+1, "(grid_origin %s %s)\n",
498  FormatInternalUnits( dsnSettings.m_GridOrigin.x ).c_str(),
499  FormatInternalUnits( dsnSettings.m_GridOrigin.y ).c_str() );
500 
501  aBoard->GetPlotOptions().Format( m_out, aNestLevel+1 );
502 
503  m_out->Print( aNestLevel, ")\n\n" );
504 }
505 
506 
507 void PCB_IO::formatGeneral( const BOARD* aBoard, int aNestLevel ) const
508 {
509  const BOARD_DESIGN_SETTINGS& dsnSettings = aBoard->GetDesignSettings();
510 
511  m_out->Print( 0, "\n" );
512  m_out->Print( aNestLevel, "(general\n" );
513  m_out->Print( aNestLevel+1, "(thickness %s)\n",
514  FormatInternalUnits( dsnSettings.GetBoardThickness() ).c_str() );
515 
516  m_out->Print( aNestLevel, ")\n\n" );
517 
518  aBoard->GetPageSettings().Format( m_out, aNestLevel, m_ctl );
519  aBoard->GetTitleBlock().Format( m_out, aNestLevel, m_ctl );
520 }
521 
522 
523 void PCB_IO::formatBoardLayers( const BOARD* aBoard, int aNestLevel ) const
524 {
525  m_out->Print( aNestLevel, "(layers\n" );
526 
527  // Save only the used copper layers from front to back.
528 
529  for( LSEQ cu = aBoard->GetEnabledLayers().CuStack(); cu; ++cu )
530  {
531  PCB_LAYER_ID layer = *cu;
532 
533  m_out->Print( aNestLevel+1, "(%d %s %s", layer,
534  m_out->Quotew( LSET::Name( layer ) ).c_str(),
535  LAYER::ShowType( aBoard->GetLayerType( layer ) ) );
536 
537  if( LSET::Name( layer ) != m_board->GetLayerName( layer ) )
538  m_out->Print( 0, " %s", m_out->Quotew( m_board->GetLayerName( layer ) ).c_str() );
539 
540  m_out->Print( 0, ")\n" );
541  }
542 
543  // Save used non-copper layers in the order they are defined.
544  // desired sequence for non Cu BOARD layers.
545  static const PCB_LAYER_ID non_cu[] =
546  {
547  B_Adhes, // 32
548  F_Adhes,
549  B_Paste,
550  F_Paste,
551  B_SilkS,
552  F_SilkS,
553  B_Mask,
554  F_Mask,
555  Dwgs_User,
556  Cmts_User,
557  Eco1_User,
558  Eco2_User,
559  Edge_Cuts,
560  Margin,
561  B_CrtYd,
562  F_CrtYd,
563  B_Fab,
564  F_Fab,
565  User_1,
566  User_2,
567  User_3,
568  User_4,
569  User_5,
570  User_6,
571  User_7,
572  User_8,
573  User_9
574  };
575 
576  for( LSEQ seq = aBoard->GetEnabledLayers().Seq( non_cu, arrayDim( non_cu ) ); seq; ++seq )
577  {
578  PCB_LAYER_ID layer = *seq;
579 
580  m_out->Print( aNestLevel+1, "(%d %s user", layer,
581  m_out->Quotew( LSET::Name( layer ) ).c_str() );
582 
583  if( m_board->GetLayerName( layer ) != LSET::Name( layer ) )
584  m_out->Print( 0, " %s", m_out->Quotew( m_board->GetLayerName( layer ) ).c_str() );
585 
586  m_out->Print( 0, ")\n" );
587  }
588 
589  m_out->Print( aNestLevel, ")\n\n" );
590 }
591 
592 
593 void PCB_IO::formatNetInformation( const BOARD* aBoard, int aNestLevel ) const
594 {
595  for( NETINFO_ITEM* net : *m_mapping )
596  {
597  if( net == nullptr ) // Skip not actually existing nets (orphan nets)
598  continue;
599 
600  m_out->Print( aNestLevel, "(net %d %s)\n",
601  m_mapping->Translate( net->GetNetCode() ),
602  m_out->Quotew( net->GetNetname() ).c_str() );
603  }
604 
605  m_out->Print( 0, "\n" );
606 }
607 
608 
609 void PCB_IO::formatProperties( const BOARD* aBoard, int aNestLevel ) const
610 {
611  for( const std::pair<const wxString, wxString>& prop : aBoard->GetProperties() )
612  {
613  m_out->Print( aNestLevel, "(property %s %s)\n",
614  m_out->Quotew( prop.first ).c_str(),
615  m_out->Quotew( prop.second ).c_str() );
616  }
617 
618  if( !aBoard->GetProperties().empty() )
619  m_out->Print( 0, "\n" );
620 }
621 
622 
623 void PCB_IO::formatHeader( const BOARD* aBoard, int aNestLevel ) const
624 {
625  formatGeneral( aBoard, aNestLevel );
626 
627  // Layers list.
628  formatBoardLayers( aBoard, aNestLevel );
629 
630  // Setup
631  formatSetup( aBoard, aNestLevel );
632 
633  // Properties
634  formatProperties( aBoard, aNestLevel );
635 
636  // Save net codes and names
637  formatNetInformation( aBoard, aNestLevel );
638 }
639 
640 
641 void PCB_IO::format( const BOARD* aBoard, int aNestLevel ) const
642 {
643  std::set<BOARD_ITEM*, BOARD_ITEM::ptr_cmp> sorted_footprints( aBoard->Footprints().begin(),
644  aBoard->Footprints().end() );
645  std::set<BOARD_ITEM*, BOARD_ITEM::ptr_cmp> sorted_drawings( aBoard->Drawings().begin(),
646  aBoard->Drawings().end() );
647  std::set<TRACK*, TRACK::cmp_tracks> sorted_tracks( aBoard->Tracks().begin(),
648  aBoard->Tracks().end() );
649  std::set<BOARD_ITEM*, BOARD_ITEM::ptr_cmp> sorted_zones( aBoard->Zones().begin(),
650  aBoard->Zones().end() );
651  std::set<BOARD_ITEM*, BOARD_ITEM::ptr_cmp> sorted_groups( aBoard->Groups().begin(),
652  aBoard->Groups().end() );
653  formatHeader( aBoard, aNestLevel );
654 
655  // Save the footprints.
656  for( BOARD_ITEM* footprint : sorted_footprints )
657  {
658  Format( footprint, aNestLevel );
659  m_out->Print( 0, "\n" );
660  }
661 
662  // Save the graphical items on the board (not owned by a footprint)
663  for( BOARD_ITEM* item : sorted_drawings )
664  Format( item, aNestLevel );
665 
666  if( sorted_drawings.size() )
667  m_out->Print( 0, "\n" );
668 
669  // Do not save PCB_MARKERs, they can be regenerated easily.
670 
671  // Save the tracks and vias.
672  for( TRACK* track : sorted_tracks )
673  Format( track, aNestLevel );
674 
675  if( sorted_tracks.size() )
676  m_out->Print( 0, "\n" );
677 
678  // Save the polygon (which are the newer technology) zones.
679  for( auto zone : sorted_zones )
680  Format( zone, aNestLevel );
681 
682  // Save the groups
683  for( BOARD_ITEM* group : sorted_groups )
684  Format( group, aNestLevel );
685 }
686 
687 
688 void PCB_IO::format( const DIMENSION_BASE* aDimension, int aNestLevel ) const
689 {
690  const ALIGNED_DIMENSION* aligned = dynamic_cast<const ALIGNED_DIMENSION*>( aDimension );
691  const ORTHOGONAL_DIMENSION* ortho = dynamic_cast<const ORTHOGONAL_DIMENSION*>( aDimension );
692  const CENTER_DIMENSION* center = dynamic_cast<const CENTER_DIMENSION*>( aDimension );
693  const LEADER* leader = dynamic_cast<const LEADER*>( aDimension );
694 
695  m_out->Print( aNestLevel, "(dimension" );
696 
697  if( aDimension->IsLocked() )
698  m_out->Print( 0, " locked" );
699 
700  if( aDimension->Type() == PCB_DIM_ALIGNED_T )
701  m_out->Print( 0, " (type aligned)" );
702  else if( aDimension->Type() == PCB_DIM_LEADER_T )
703  m_out->Print( 0, " (type leader)" );
704  else if( aDimension->Type() == PCB_DIM_CENTER_T )
705  m_out->Print( 0, " (type center)" );
706  else if( aDimension->Type() == PCB_DIM_ORTHOGONAL_T )
707  m_out->Print( 0, " (type orthogonal)" );
708  else
709  wxFAIL_MSG( wxT( "Cannot format unknown dimension type!" ) );
710 
711  formatLayer( aDimension );
712 
713  m_out->Print( 0, " (tstamp %s)", TO_UTF8( aDimension->m_Uuid.AsString() ) );
714 
715  m_out->Print( 0, "\n" );
716 
717  m_out->Print( aNestLevel+1, "(pts (xy %s %s) (xy %s %s))\n",
718  FormatInternalUnits( aDimension->GetStart().x ).c_str(),
719  FormatInternalUnits( aDimension->GetStart().y ).c_str(),
720  FormatInternalUnits( aDimension->GetEnd().x ).c_str(),
721  FormatInternalUnits( aDimension->GetEnd().y ).c_str() );
722 
723  if( aligned )
724  m_out->Print( aNestLevel+1, "(height %s)\n",
725  FormatInternalUnits( aligned->GetHeight() ).c_str() );
726 
727  if( ortho )
728  m_out->Print( aNestLevel+1, "(orientation %d)\n",
729  static_cast<int>( ortho->GetOrientation() ) );
730 
731  if( !center )
732  {
733  Format( &aDimension->Text(), aNestLevel + 1 );
734 
735  m_out->Print( aNestLevel + 1, "(format" );
736 
737  if( !aDimension->GetPrefix().IsEmpty() )
738  m_out->Print( 0, " (prefix %s)", m_out->Quotew( aDimension->GetPrefix() ).c_str() );
739 
740  if( !aDimension->GetSuffix().IsEmpty() )
741  m_out->Print( 0, " (suffix %s)", m_out->Quotew( aDimension->GetSuffix() ).c_str() );
742 
743  m_out->Print( 0, " (units %d) (units_format %d) (precision %d)",
744  static_cast<int>( aDimension->GetUnitsMode() ),
745  static_cast<int>( aDimension->GetUnitsFormat() ), aDimension->GetPrecision() );
746 
747  if( aDimension->GetOverrideTextEnabled() )
748  m_out->Print( 0, " (override_value %s)",
749  m_out->Quotew( aDimension->GetOverrideText() ).c_str() );
750 
751  if( aDimension->GetSuppressZeroes() )
752  m_out->Print( 0, " suppress_zeroes" );
753 
754  m_out->Print( 0, ")\n" );
755  }
756 
757  m_out->Print( aNestLevel+1, "(style (thickness %s) (arrow_length %s) (text_position_mode %d)",
758  FormatInternalUnits( aDimension->GetLineThickness() ).c_str(),
759  FormatInternalUnits( aDimension->GetArrowLength() ).c_str(),
760  static_cast<int>( aDimension->GetTextPositionMode() ) );
761 
762  if( aligned )
763  {
764  m_out->Print( 0, " (extension_height %s)",
765  FormatInternalUnits( aligned->GetExtensionHeight() ).c_str() );
766  }
767 
768  if( leader )
769  m_out->Print( 0, " (text_frame %d)", static_cast<int>( leader->GetTextFrame() ) );
770 
771  m_out->Print( 0, " (extension_offset %s)",
772  FormatInternalUnits( aDimension->GetExtensionOffset() ).c_str() );
773 
774  if( aDimension->GetKeepTextAligned() )
775  m_out->Print( 0, " keep_text_aligned" );
776 
777  m_out->Print( 0, ")\n" );
778 
779  m_out->Print( aNestLevel, ")\n" );
780 }
781 
782 
783 void PCB_IO::format( const PCB_SHAPE* aShape, int aNestLevel ) const
784 {
785  std::string locked = aShape->IsLocked() ? " locked" : "";
786 
787  switch( aShape->GetShape() )
788  {
789  case PCB_SHAPE_TYPE::SEGMENT: // Line
790  m_out->Print( aNestLevel, "(gr_line%s (start %s) (end %s)",
791  locked.c_str(),
792  FormatInternalUnits( aShape->GetStart() ).c_str(),
793  FormatInternalUnits( aShape->GetEnd() ).c_str() );
794 
795  if( aShape->GetAngle() != 0.0 )
796  m_out->Print( 0, " (angle %s)", FormatAngle( aShape->GetAngle() ).c_str() );
797 
798  break;
799 
800  case PCB_SHAPE_TYPE::RECT: // Rectangle
801  m_out->Print( aNestLevel, "(gr_rect%s (start %s) (end %s)",
802  locked.c_str(),
803  FormatInternalUnits( aShape->GetStart() ).c_str(),
804  FormatInternalUnits( aShape->GetEnd() ).c_str() );
805  break;
806 
807  case PCB_SHAPE_TYPE::CIRCLE: // Circle
808  m_out->Print( aNestLevel, "(gr_circle%s (center %s) (end %s)",
809  locked.c_str(),
810  FormatInternalUnits( aShape->GetStart() ).c_str(),
811  FormatInternalUnits( aShape->GetEnd() ).c_str() );
812  break;
813 
814  case PCB_SHAPE_TYPE::ARC: // Arc
815  m_out->Print( aNestLevel, "(gr_arc%s (start %s) (end %s) (angle %s)",
816  locked.c_str(),
817  FormatInternalUnits( aShape->GetStart() ).c_str(),
818  FormatInternalUnits( aShape->GetEnd() ).c_str(),
819  FormatAngle( aShape->GetAngle() ).c_str() );
820  break;
821 
822  case PCB_SHAPE_TYPE::POLYGON: // Polygon
823  if( aShape->IsPolyShapeValid() )
824  {
825  const SHAPE_POLY_SET& poly = aShape->GetPolyShape();
826  const SHAPE_LINE_CHAIN& outline = poly.Outline( 0 );
827  const int pointsCount = outline.PointCount();
828 
829  m_out->Print( aNestLevel, "(gr_poly%s (pts\n",
830  locked.c_str() );
831 
832  for( int ii = 0; ii < pointsCount; ++ii )
833  {
834  int nestLevel = 0;
835 
836  if( ii && ( !( ii%4 ) || !ADVANCED_CFG::GetCfg().m_CompactSave ) )
837  {
838  // newline every 4 pts.
839  nestLevel = aNestLevel + 1;
840  m_out->Print( 0, "\n" );
841  }
842 
843  m_out->Print( nestLevel, "%s(xy %s)",
844  nestLevel ? "" : " ",
845  FormatInternalUnits( outline.CPoint( ii ) ).c_str() );
846  }
847 
848  m_out->Print( 0, ")" );
849  }
850  else
851  {
852  wxFAIL_MSG( wxT( "Cannot format invalid polygon." ) );
853  return;
854  }
855 
856  break;
857 
858  case PCB_SHAPE_TYPE::CURVE: // Bezier curve
859  m_out->Print( aNestLevel, "(gr_curve%s (pts (xy %s) (xy %s) (xy %s) (xy %s))",
860  locked.c_str(),
861  FormatInternalUnits( aShape->GetStart() ).c_str(),
862  FormatInternalUnits( aShape->GetBezControl1() ).c_str(),
863  FormatInternalUnits( aShape->GetBezControl2() ).c_str(),
864  FormatInternalUnits( aShape->GetEnd() ).c_str() );
865  break;
866 
867  default:
868  wxFAIL_MSG( "PCB_IO::format cannot format unknown PCB_SHAPE shape:"
869  + PCB_SHAPE_TYPE_T_asString( aShape->GetShape()) );
870  return;
871  };
872 
873  formatLayer( aShape );
874 
875  m_out->Print( 0, " (width %s)", FormatInternalUnits( aShape->GetWidth() ).c_str() );
876 
877  // The filled flag represents if a solid fill is present on circles, rectangles and polygons
878  if( ( aShape->GetShape() == PCB_SHAPE_TYPE::POLYGON )
879  || ( aShape->GetShape() == PCB_SHAPE_TYPE::RECT )
880  || ( aShape->GetShape() == PCB_SHAPE_TYPE::CIRCLE ) )
881  {
882  if( aShape->IsFilled() )
883  m_out->Print( 0, " (fill solid)" );
884  else
885  m_out->Print( 0, " (fill none)" );
886  }
887 
888  m_out->Print( 0, " (tstamp %s)", TO_UTF8( aShape->m_Uuid.AsString() ) );
889 
890  m_out->Print( 0, ")\n" );
891 }
892 
893 
894 void PCB_IO::format( const FP_SHAPE* aFPShape, int aNestLevel ) const
895 {
896  std::string locked = aFPShape->IsLocked() ? " locked" : "";
897 
898  switch( aFPShape->GetShape() )
899  {
900  case PCB_SHAPE_TYPE::SEGMENT: // Line
901  m_out->Print( aNestLevel, "(fp_line%s (start %s) (end %s)",
902  locked.c_str(),
903  FormatInternalUnits( aFPShape->GetStart0() ).c_str(),
904  FormatInternalUnits( aFPShape->GetEnd0() ).c_str() );
905  break;
906 
907  case PCB_SHAPE_TYPE::RECT: // Rectangle
908  m_out->Print( aNestLevel, "(fp_rect%s (start %s) (end %s)",
909  locked.c_str(),
910  FormatInternalUnits( aFPShape->GetStart0() ).c_str(),
911  FormatInternalUnits( aFPShape->GetEnd0() ).c_str() );
912  break;
913 
914  case PCB_SHAPE_TYPE::CIRCLE: // Circle
915  m_out->Print( aNestLevel, "(fp_circle%s (center %s) (end %s)",
916  locked.c_str(),
917  FormatInternalUnits( aFPShape->GetStart0() ).c_str(),
918  FormatInternalUnits( aFPShape->GetEnd0() ).c_str() );
919  break;
920 
921  case PCB_SHAPE_TYPE::ARC: // Arc
922  m_out->Print( aNestLevel, "(fp_arc%s (start %s) (end %s) (angle %s)",
923  locked.c_str(),
924  FormatInternalUnits( aFPShape->GetStart0() ).c_str(),
925  FormatInternalUnits( aFPShape->GetEnd0() ).c_str(),
926  FormatAngle( aFPShape->GetAngle() ).c_str() );
927  break;
928 
929  case PCB_SHAPE_TYPE::POLYGON: // Polygonal segment
930  if( aFPShape->IsPolyShapeValid() )
931  {
932  const SHAPE_POLY_SET& poly = aFPShape->GetPolyShape();
933  const SHAPE_LINE_CHAIN& outline = poly.Outline( 0 );
934  int pointsCount = outline.PointCount();
935 
936  m_out->Print( aNestLevel, "(fp_poly%s (pts",
937  locked.c_str() );
938 
939  for( int ii = 0; ii < pointsCount; ++ii )
940  {
941  int nestLevel = 0;
942 
943  if( ii && ( !( ii%4 ) || !ADVANCED_CFG::GetCfg().m_CompactSave ) )
944  {
945  // newline every 4 pts.
946  nestLevel = aNestLevel + 1;
947  m_out->Print( 0, "\n" );
948  }
949 
950  m_out->Print( nestLevel, "%s(xy %s)",
951  nestLevel ? "" : " ",
952  FormatInternalUnits( outline.CPoint( ii ) ).c_str() );
953  }
954 
955  m_out->Print( 0, ")" );
956  }
957  else
958  {
959  wxFAIL_MSG( wxT( "Cannot format invalid polygon." ) );
960  return;
961  }
962  break;
963 
964  case PCB_SHAPE_TYPE::CURVE: // Bezier curve
965  m_out->Print( aNestLevel, "(fp_curve%s (pts (xy %s) (xy %s) (xy %s) (xy %s))",
966  locked.c_str(),
967  FormatInternalUnits( aFPShape->GetStart0() ).c_str(),
968  FormatInternalUnits( aFPShape->GetBezier0_C1() ).c_str(),
969  FormatInternalUnits( aFPShape->GetBezier0_C2() ).c_str(),
970  FormatInternalUnits( aFPShape->GetEnd0() ).c_str() );
971  break;
972 
973  default:
974  wxFAIL_MSG( "PCB_IO::format cannot format unknown FP_SHAPE shape:"
975  + PCB_SHAPE_TYPE_T_asString( aFPShape->GetShape() ) );
976  return;
977  };
978 
979  formatLayer( aFPShape );
980 
981  m_out->Print( 0, " (width %s)", FormatInternalUnits( aFPShape->GetWidth() ).c_str() );
982 
983  // The filled flag represents if a solid fill is present on circles, rectangles and polygons
984  if( ( aFPShape->GetShape() == PCB_SHAPE_TYPE::POLYGON )
985  || ( aFPShape->GetShape() == PCB_SHAPE_TYPE::RECT )
986  || ( aFPShape->GetShape() == PCB_SHAPE_TYPE::CIRCLE ) )
987  {
988  if( aFPShape->IsFilled() )
989  m_out->Print( 0, " (fill solid)" );
990  else
991  m_out->Print( 0, " (fill none)" );
992  }
993 
994  m_out->Print( 0, " (tstamp %s)", TO_UTF8( aFPShape->m_Uuid.AsString() ) );
995 
996  m_out->Print( 0, ")\n" );
997 }
998 
999 
1000 void PCB_IO::format( const PCB_TARGET* aTarget, int aNestLevel ) const
1001 {
1002  m_out->Print( aNestLevel, "(target %s (at %s) (size %s)",
1003  ( aTarget->GetShape() ) ? "x" : "plus",
1004  FormatInternalUnits( aTarget->GetPosition() ).c_str(),
1005  FormatInternalUnits( aTarget->GetSize() ).c_str() );
1006 
1007  if( aTarget->GetWidth() != 0 )
1008  m_out->Print( 0, " (width %s)", FormatInternalUnits( aTarget->GetWidth() ).c_str() );
1009 
1010  formatLayer( aTarget );
1011 
1012  m_out->Print( 0, " (tstamp %s)", TO_UTF8( aTarget->m_Uuid.AsString() ) );
1013 
1014  m_out->Print( 0, ")\n" );
1015 }
1016 
1017 
1018 void PCB_IO::format( const FOOTPRINT* aFootprint, int aNestLevel ) const
1019 {
1020  if( !( m_ctl & CTL_OMIT_INITIAL_COMMENTS ) )
1021  {
1022  const wxArrayString* initial_comments = aFootprint->GetInitialComments();
1023 
1024  if( initial_comments )
1025  {
1026  for( unsigned i = 0; i < initial_comments->GetCount(); ++i )
1027  m_out->Print( aNestLevel, "%s\n", TO_UTF8( (*initial_comments)[i] ) );
1028 
1029  m_out->Print( 0, "\n" ); // improve readability?
1030  }
1031  }
1032 
1033  if( m_ctl & CTL_OMIT_LIBNAME )
1034  m_out->Print( aNestLevel, "(footprint %s",
1035  m_out->Quotes( aFootprint->GetFPID().GetLibItemNameAndRev() ).c_str() );
1036  else
1037  m_out->Print( aNestLevel, "(footprint %s",
1038  m_out->Quotes( aFootprint->GetFPID().Format() ).c_str() );
1039 
1040  if( !( m_ctl & CTL_OMIT_FOOTPRINT_VERSION ) )
1041  m_out->Print( 0, " (version %d) (generator pcbnew)", SEXPR_BOARD_FILE_VERSION );
1042 
1043  if( aFootprint->IsLocked() )
1044  m_out->Print( 0, " locked" );
1045 
1046  if( aFootprint->IsPlaced() )
1047  m_out->Print( 0, " placed" );
1048 
1049  formatLayer( aFootprint );
1050 
1051  m_out->Print( 0, "\n" );
1052  m_out->Print( aNestLevel+1, "(tedit %lX)", (unsigned long)aFootprint->GetLastEditTime() );
1053 
1054  if( !( m_ctl & CTL_OMIT_TSTAMPS ) )
1055  m_out->Print( 0, " (tstamp %s)", TO_UTF8( aFootprint->m_Uuid.AsString() ) );
1056 
1057  m_out->Print( 0, "\n" );
1058 
1059  if( !( m_ctl & CTL_OMIT_AT ) )
1060  {
1061  m_out->Print( aNestLevel+1, "(at %s",
1062  FormatInternalUnits( aFootprint->GetPosition() ).c_str() );
1063 
1064  if( aFootprint->GetOrientation() != 0.0 )
1065  m_out->Print( 0, " %s", FormatAngle( aFootprint->GetOrientation() ).c_str() );
1066 
1067  m_out->Print( 0, ")\n" );
1068  }
1069 
1070  if( !aFootprint->GetDescription().IsEmpty() )
1071  m_out->Print( aNestLevel+1, "(descr %s)\n",
1072  m_out->Quotew( aFootprint->GetDescription() ).c_str() );
1073 
1074  if( !aFootprint->GetKeywords().IsEmpty() )
1075  m_out->Print( aNestLevel+1, "(tags %s)\n",
1076  m_out->Quotew( aFootprint->GetKeywords() ).c_str() );
1077 
1078  const std::map<wxString, wxString>& props = aFootprint->GetProperties();
1079 
1080  for( const std::pair<const wxString, wxString>& prop : props )
1081  {
1082  m_out->Print( aNestLevel+1, "(property %s %s)\n",
1083  m_out->Quotew( prop.first ).c_str(),
1084  m_out->Quotew( prop.second ).c_str() );
1085  }
1086 
1087  if( !( m_ctl & CTL_OMIT_PATH ) && !aFootprint->GetPath().empty() )
1088  m_out->Print( aNestLevel+1, "(path %s)\n",
1089  m_out->Quotew( aFootprint->GetPath().AsString() ).c_str() );
1090 
1091  if( aFootprint->GetPlacementCost90() != 0 )
1092  m_out->Print( aNestLevel+1, "(autoplace_cost90 %d)\n", aFootprint->GetPlacementCost90() );
1093 
1094  if( aFootprint->GetPlacementCost180() != 0 )
1095  m_out->Print( aNestLevel+1, "(autoplace_cost180 %d)\n", aFootprint->GetPlacementCost180() );
1096 
1097  if( aFootprint->GetLocalSolderMaskMargin() != 0 )
1098  m_out->Print( aNestLevel+1, "(solder_mask_margin %s)\n",
1099  FormatInternalUnits( aFootprint->GetLocalSolderMaskMargin() ).c_str() );
1100 
1101  if( aFootprint->GetLocalSolderPasteMargin() != 0 )
1102  m_out->Print( aNestLevel+1, "(solder_paste_margin %s)\n",
1103  FormatInternalUnits( aFootprint->GetLocalSolderPasteMargin() ).c_str() );
1104 
1105  if( aFootprint->GetLocalSolderPasteMarginRatio() != 0 )
1106  m_out->Print( aNestLevel+1, "(solder_paste_ratio %s)\n",
1107  Double2Str( aFootprint->GetLocalSolderPasteMarginRatio() ).c_str() );
1108 
1109  if( aFootprint->GetLocalClearance() != 0 )
1110  m_out->Print( aNestLevel+1, "(clearance %s)\n",
1111  FormatInternalUnits( aFootprint->GetLocalClearance() ).c_str() );
1112 
1113  if( aFootprint->GetZoneConnection() != ZONE_CONNECTION::INHERITED )
1114  m_out->Print( aNestLevel+1, "(zone_connect %d)\n",
1115  static_cast<int>( aFootprint->GetZoneConnection() ) );
1116 
1117  if( aFootprint->GetThermalWidth() != 0 )
1118  m_out->Print( aNestLevel+1, "(thermal_width %s)\n",
1119  FormatInternalUnits( aFootprint->GetThermalWidth() ).c_str() );
1120 
1121  if( aFootprint->GetThermalGap() != 0 )
1122  m_out->Print( aNestLevel+1, "(thermal_gap %s)\n",
1123  FormatInternalUnits( aFootprint->GetThermalGap() ).c_str() );
1124 
1125  // Attributes
1126  if( aFootprint->GetAttributes() )
1127  {
1128  m_out->Print( aNestLevel+1, "(attr" );
1129 
1130  if( aFootprint->GetAttributes() & FP_SMD )
1131  m_out->Print( 0, " smd" );
1132 
1133  if( aFootprint->GetAttributes() & FP_THROUGH_HOLE )
1134  m_out->Print( 0, " through_hole" );
1135 
1136  if( aFootprint->GetAttributes() & FP_BOARD_ONLY )
1137  m_out->Print( 0, " board_only" );
1138 
1139  if( aFootprint->GetAttributes() & FP_EXCLUDE_FROM_POS_FILES )
1140  m_out->Print( 0, " exclude_from_pos_files" );
1141 
1142  if( aFootprint->GetAttributes() & FP_EXCLUDE_FROM_BOM )
1143  m_out->Print( 0, " exclude_from_bom" );
1144 
1145  m_out->Print( 0, ")\n" );
1146  }
1147 
1148  Format( (BOARD_ITEM*) &aFootprint->Reference(), aNestLevel + 1 );
1149  Format( (BOARD_ITEM*) &aFootprint->Value(), aNestLevel + 1 );
1150 
1151  std::set<PAD*, FOOTPRINT::cmp_pads> sorted_pads( aFootprint->Pads().begin(),
1152  aFootprint->Pads().end() );
1153  std::set<BOARD_ITEM*, FOOTPRINT::cmp_drawings> sorted_drawings(
1154  aFootprint->GraphicalItems().begin(),
1155  aFootprint->GraphicalItems().end() );
1156  std::set<BOARD_ITEM*, BOARD_ITEM::ptr_cmp> sorted_zones( aFootprint->Zones().begin(),
1157  aFootprint->Zones().end() );
1158  std::set<BOARD_ITEM*, PCB_GROUP::ptr_cmp> sorted_groups( aFootprint->Groups().begin(),
1159  aFootprint->Groups().end() );
1160 
1161  // Save drawing elements.
1162 
1163  for( BOARD_ITEM* gr : sorted_drawings )
1164  Format( gr, aNestLevel+1 );
1165 
1166  // Save pads.
1167  for( PAD* pad : sorted_pads )
1168  Format( pad, aNestLevel+1 );
1169 
1170  // Save zones.
1171  for( BOARD_ITEM* zone : sorted_zones )
1172  Format( zone, aNestLevel + 1 );
1173 
1174  // Save groups.
1175  for( BOARD_ITEM* group : sorted_groups )
1176  Format( group, aNestLevel + 1 );
1177 
1178  // Save 3D info.
1179  auto bs3D = aFootprint->Models().begin();
1180  auto es3D = aFootprint->Models().end();
1181 
1182  while( bs3D != es3D )
1183  {
1184  if( !bs3D->m_Filename.IsEmpty() )
1185  {
1186  m_out->Print( aNestLevel+1, "(model %s%s\n",
1187  m_out->Quotew( bs3D->m_Filename ).c_str(),
1188  bs3D->m_Show ? "" : " hide" );
1189 
1190  if( bs3D->m_Opacity != 1.0 )
1191  m_out->Print( aNestLevel+2, "(opacity %0.4f)", bs3D->m_Opacity );
1192 
1193  m_out->Print( aNestLevel+2, "(offset (xyz %s %s %s))\n",
1194  Double2Str( bs3D->m_Offset.x ).c_str(),
1195  Double2Str( bs3D->m_Offset.y ).c_str(),
1196  Double2Str( bs3D->m_Offset.z ).c_str() );
1197 
1198  m_out->Print( aNestLevel+2, "(scale (xyz %s %s %s))\n",
1199  Double2Str( bs3D->m_Scale.x ).c_str(),
1200  Double2Str( bs3D->m_Scale.y ).c_str(),
1201  Double2Str( bs3D->m_Scale.z ).c_str() );
1202 
1203  m_out->Print( aNestLevel+2, "(rotate (xyz %s %s %s))\n",
1204  Double2Str( bs3D->m_Rotation.x ).c_str(),
1205  Double2Str( bs3D->m_Rotation.y ).c_str(),
1206  Double2Str( bs3D->m_Rotation.z ).c_str() );
1207 
1208  m_out->Print( aNestLevel+1, ")\n" );
1209  }
1210 
1211  ++bs3D;
1212  }
1213 
1214  m_out->Print( aNestLevel, ")\n" );
1215 }
1216 
1217 
1218 void PCB_IO::formatLayers( LSET aLayerMask, int aNestLevel ) const
1219 {
1220  std::string output;
1221 
1222  if( aNestLevel == 0 )
1223  output += ' ';
1224 
1225  output += "(layers";
1226 
1227  static const LSET cu_all( LSET::AllCuMask() );
1228  static const LSET fr_bk( 2, B_Cu, F_Cu );
1229  static const LSET adhes( 2, B_Adhes, F_Adhes );
1230  static const LSET paste( 2, B_Paste, F_Paste );
1231  static const LSET silks( 2, B_SilkS, F_SilkS );
1232  static const LSET mask( 2, B_Mask, F_Mask );
1233  static const LSET crt_yd( 2, B_CrtYd, F_CrtYd );
1234  static const LSET fab( 2, B_Fab, F_Fab );
1235 
1236  LSET cu_mask = cu_all;
1237 
1238  // output copper layers first, then non copper
1239 
1240  if( ( aLayerMask & cu_mask ) == cu_mask )
1241  {
1242  output += " *.Cu";
1243  aLayerMask &= ~cu_all; // clear bits, so they are not output again below
1244  }
1245  else if( ( aLayerMask & cu_mask ) == fr_bk )
1246  {
1247  output += " F&B.Cu";
1248  aLayerMask &= ~fr_bk;
1249  }
1250 
1251  if( ( aLayerMask & adhes ) == adhes )
1252  {
1253  output += " *.Adhes";
1254  aLayerMask &= ~adhes;
1255  }
1256 
1257  if( ( aLayerMask & paste ) == paste )
1258  {
1259  output += " *.Paste";
1260  aLayerMask &= ~paste;
1261  }
1262 
1263  if( ( aLayerMask & silks ) == silks )
1264  {
1265  output += " *.SilkS";
1266  aLayerMask &= ~silks;
1267  }
1268 
1269  if( ( aLayerMask & mask ) == mask )
1270  {
1271  output += " *.Mask";
1272  aLayerMask &= ~mask;
1273  }
1274 
1275  if( ( aLayerMask & crt_yd ) == crt_yd )
1276  {
1277  output += " *.CrtYd";
1278  aLayerMask &= ~crt_yd;
1279  }
1280 
1281  if( ( aLayerMask & fab ) == fab )
1282  {
1283  output += " *.Fab";
1284  aLayerMask &= ~fab;
1285  }
1286 
1287  // output any individual layers not handled in wildcard combos above
1288  wxString layerName;
1289 
1290  for( LAYER_NUM layer = 0; layer < PCB_LAYER_ID_COUNT; ++layer )
1291  {
1292  if( aLayerMask[layer] )
1293  {
1294  layerName = LSET::Name( PCB_LAYER_ID( layer ) );
1295  output += ' ';
1296  output += m_out->Quotew( layerName );
1297  }
1298  }
1299 
1300  m_out->Print( aNestLevel, "%s)", output.c_str() );
1301 }
1302 
1303 
1304 void PCB_IO::format( const PAD* aPad, int aNestLevel ) const
1305 {
1306  const char* shape;
1307 
1308  switch( aPad->GetShape() )
1309  {
1310  case PAD_SHAPE::CIRCLE: shape = "circle"; break;
1311  case PAD_SHAPE::RECT: shape = "rect"; break;
1312  case PAD_SHAPE::OVAL: shape = "oval"; break;
1313  case PAD_SHAPE::TRAPEZOID: shape = "trapezoid"; break;
1315  case PAD_SHAPE::ROUNDRECT: shape = "roundrect"; break;
1316  case PAD_SHAPE::CUSTOM: shape = "custom"; break;
1317 
1318  default:
1319  THROW_IO_ERROR( wxString::Format( _( "unknown pad type: %d"), aPad->GetShape() ) );
1320  }
1321 
1322  const char* type;
1323 
1324  switch( aPad->GetAttribute() )
1325  {
1326  case PAD_ATTRIB::PTH: type = "thru_hole"; break;
1327  case PAD_ATTRIB::SMD: type = "smd"; break;
1328  case PAD_ATTRIB::CONN: type = "connect"; break;
1329  case PAD_ATTRIB::NPTH: type = "np_thru_hole"; break;
1330 
1331  default:
1332  THROW_IO_ERROR( wxString::Format( "unknown pad attribute: %d", aPad->GetAttribute() ) );
1333  }
1334 
1335  const char* property = nullptr;
1336 
1337  switch( aPad->GetProperty() )
1338  {
1339  case PAD_PROP::NONE: break; // could be "none"
1340  case PAD_PROP::BGA: property = "PAD_PROP::BGA"; break;
1341  case PAD_PROP::FIDUCIAL_GLBL: property = "pad_prop_fiducial_glob"; break;
1342  case PAD_PROP::FIDUCIAL_LOCAL: property = "pad_prop_fiducial_loc"; break;
1343  case PAD_PROP::TESTPOINT: property = "PAD_PROP::TESTPOINT"; break;
1344  case PAD_PROP::HEATSINK: property = "PAD_PROP::HEATSINK"; break;
1345  case PAD_PROP::CASTELLATED: property = "PAD_PROP::CASTELLATED"; break;
1346 
1347  default:
1348  THROW_IO_ERROR( wxString::Format( "unknown pad property: %d", aPad->GetProperty() ) );
1349  }
1350 
1351  m_out->Print( aNestLevel, "(pad %s %s %s",
1352  m_out->Quotew( aPad->GetName() ).c_str(),
1353  type,
1354  shape );
1355 
1356  if( aPad->IsLocked() )
1357  m_out->Print( 0, " locked" );
1358 
1359  m_out->Print( 0, " (at %s", FormatInternalUnits( aPad->GetPos0() ).c_str() );
1360 
1361  if( aPad->GetOrientation() != 0.0 )
1362  m_out->Print( 0, " %s", FormatAngle( aPad->GetOrientation() ).c_str() );
1363 
1364  m_out->Print( 0, ")" );
1365 
1366  m_out->Print( 0, " (size %s)", FormatInternalUnits( aPad->GetSize() ).c_str() );
1367 
1368  if( (aPad->GetDelta().GetWidth()) != 0 || (aPad->GetDelta().GetHeight() != 0 ) )
1369  m_out->Print( 0, " (rect_delta %s)", FormatInternalUnits( aPad->GetDelta() ).c_str() );
1370 
1371  wxSize sz = aPad->GetDrillSize();
1372  wxPoint shapeoffset = aPad->GetOffset();
1373 
1374  if( (sz.GetWidth() > 0) || (sz.GetHeight() > 0) ||
1375  (shapeoffset.x != 0) || (shapeoffset.y != 0) )
1376  {
1377  m_out->Print( 0, " (drill" );
1378 
1379  if( aPad->GetDrillShape() == PAD_DRILL_SHAPE_OBLONG )
1380  m_out->Print( 0, " oval" );
1381 
1382  if( sz.GetWidth() > 0 )
1383  m_out->Print( 0, " %s", FormatInternalUnits( sz.GetWidth() ).c_str() );
1384 
1385  if( sz.GetHeight() > 0 && sz.GetWidth() != sz.GetHeight() )
1386  m_out->Print( 0, " %s", FormatInternalUnits( sz.GetHeight() ).c_str() );
1387 
1388  if( (shapeoffset.x != 0) || (shapeoffset.y != 0) )
1389  m_out->Print( 0, " (offset %s)", FormatInternalUnits( aPad->GetOffset() ).c_str() );
1390 
1391  m_out->Print( 0, ")" );
1392  }
1393 
1394  // Add pad property, if exists.
1395  if( property )
1396  m_out->Print( 0, " (property %s)", property );
1397 
1398  formatLayers( aPad->GetLayerSet() );
1399 
1400  if( aPad->GetAttribute() == PAD_ATTRIB::PTH )
1401  {
1402  if( aPad->GetRemoveUnconnected() )
1403  {
1404  m_out->Print( 0, " (remove_unused_layers)" );
1405 
1406  if( aPad->GetKeepTopBottom() )
1407  m_out->Print( 0, " (keep_end_layers)" );
1408  }
1409  }
1410 
1411  // Output the radius ratio for rounded and chamfered rect pads
1412  if( aPad->GetShape() == PAD_SHAPE::ROUNDRECT || aPad->GetShape() == PAD_SHAPE::CHAMFERED_RECT)
1413  {
1414  m_out->Print( 0, " (roundrect_rratio %s)",
1415  Double2Str( aPad->GetRoundRectRadiusRatio() ).c_str() );
1416  }
1417 
1418  // Output the chamfer corners for chamfered rect pads
1419  if( aPad->GetShape() == PAD_SHAPE::CHAMFERED_RECT)
1420  {
1421  m_out->Print( 0, "\n" );
1422 
1423  m_out->Print( aNestLevel+1, "(chamfer_ratio %s)",
1424  Double2Str( aPad->GetChamferRectRatio() ).c_str() );
1425 
1426  m_out->Print( 0, " (chamfer" );
1427 
1428  if( ( aPad->GetChamferPositions() & RECT_CHAMFER_TOP_LEFT ) )
1429  m_out->Print( 0, " top_left" );
1430 
1431  if( ( aPad->GetChamferPositions() & RECT_CHAMFER_TOP_RIGHT ) )
1432  m_out->Print( 0, " top_right" );
1433 
1434  if( ( aPad->GetChamferPositions() & RECT_CHAMFER_BOTTOM_LEFT ) )
1435  m_out->Print( 0, " bottom_left" );
1436 
1438  m_out->Print( 0, " bottom_right" );
1439 
1440  m_out->Print( 0, ")" );
1441  }
1442 
1443  std::string output;
1444 
1445  // Unconnected pad is default net so don't save it.
1446  if( !( m_ctl & CTL_OMIT_PAD_NETS ) && aPad->GetNetCode() != NETINFO_LIST::UNCONNECTED )
1447  {
1448  StrPrintf( &output, " (net %d %s)", m_mapping->Translate( aPad->GetNetCode() ),
1449  m_out->Quotew( aPad->GetNetname() ).c_str() );
1450  }
1451 
1452  // Pin functions and types are closely related to nets, so if CTL_OMIT_NETS is set, omit
1453  // them as well (for instance when saved from library editor).
1454  if( !( m_ctl & CTL_OMIT_PAD_NETS ) )
1455  {
1456  if( !aPad->GetPinFunction().IsEmpty() )
1457  {
1458  StrPrintf( &output, " (pinfunction %s)",
1459  m_out->Quotew( aPad->GetPinFunction() ).c_str() );
1460  }
1461 
1462  if( !aPad->GetPinType().IsEmpty() )
1463  {
1464  StrPrintf( &output, " (pintype %s)",
1465  m_out->Quotew( aPad->GetPinType() ).c_str() );
1466  }
1467  }
1468 
1469  if( aPad->GetPadToDieLength() != 0 )
1470  {
1471  StrPrintf( &output, " (die_length %s)",
1472  FormatInternalUnits( aPad->GetPadToDieLength() ).c_str() );
1473  }
1474 
1475  if( aPad->GetLocalSolderMaskMargin() != 0 )
1476  {
1477  StrPrintf( &output, " (solder_mask_margin %s)",
1478  FormatInternalUnits( aPad->GetLocalSolderMaskMargin() ).c_str() );
1479  }
1480 
1481  if( aPad->GetLocalSolderPasteMargin() != 0 )
1482  {
1483  StrPrintf( &output, " (solder_paste_margin %s)",
1484  FormatInternalUnits( aPad->GetLocalSolderPasteMargin() ).c_str() );
1485  }
1486 
1487  if( aPad->GetLocalSolderPasteMarginRatio() != 0 )
1488  {
1489  StrPrintf( &output, " (solder_paste_margin_ratio %s)",
1490  Double2Str( aPad->GetLocalSolderPasteMarginRatio() ).c_str() );
1491  }
1492 
1493  if( aPad->GetLocalClearance() != 0 )
1494  {
1495  StrPrintf( &output, " (clearance %s)",
1496  FormatInternalUnits( aPad->GetLocalClearance() ).c_str() );
1497  }
1498 
1500  {
1501  StrPrintf( &output, " (zone_connect %d)",
1502  static_cast<int>( aPad->GetEffectiveZoneConnection() ) );
1503  }
1504 
1505  if( aPad->GetThermalSpokeWidth() != 0 )
1506  {
1507  StrPrintf( &output, " (thermal_width %s)",
1508  FormatInternalUnits( aPad->GetThermalSpokeWidth() ).c_str() );
1509  }
1510 
1511  if( aPad->GetThermalGap() != 0 )
1512  {
1513  StrPrintf( &output, " (thermal_gap %s)",
1514  FormatInternalUnits( aPad->GetThermalGap() ).c_str() );
1515  }
1516 
1517  if( output.size() )
1518  {
1519  m_out->Print( 0, "\n" );
1520  m_out->Print( aNestLevel+1, "%s", output.c_str()+1 ); // +1 skips 1st space on 1st element
1521  }
1522 
1523  if( aPad->GetShape() == PAD_SHAPE::CUSTOM )
1524  {
1525  m_out->Print( 0, "\n");
1526  m_out->Print( aNestLevel+1, "(options" );
1527 
1529  m_out->Print( 0, " (clearance convexhull)" );
1530  #if 1 // Set to 1 to output the default option
1531  else
1532  m_out->Print( 0, " (clearance outline)" );
1533  #endif
1534 
1535  // Output the anchor pad shape (circle/rect)
1536  if( aPad->GetAnchorPadShape() == PAD_SHAPE::RECT )
1537  shape = "rect";
1538  else
1539  shape = "circle";
1540 
1541  m_out->Print( 0, " (anchor %s)", shape );
1542 
1543  m_out->Print( 0, ")"); // end of (options ...
1544 
1545  // Output graphic primitive of the pad shape
1546  m_out->Print( 0, "\n");
1547  m_out->Print( aNestLevel+1, "(primitives" );
1548 
1549  int nested_level = aNestLevel+2;
1550 
1551  // Output all basic shapes
1552  for( const std::shared_ptr<PCB_SHAPE>& primitive : aPad->GetPrimitives() )
1553  {
1554  m_out->Print( 0, "\n");
1555 
1556  switch( primitive->GetShape() )
1557  {
1558  case PCB_SHAPE_TYPE::SEGMENT: // usual segment : line with rounded ends
1559  m_out->Print( nested_level, "(gr_line (start %s) (end %s)",
1560  FormatInternalUnits( primitive->GetStart() ).c_str(),
1561  FormatInternalUnits( primitive->GetEnd() ).c_str() );
1562  break;
1563 
1564  case PCB_SHAPE_TYPE::RECT:
1565  m_out->Print( nested_level, "(gr_rect (start %s) (end %s)",
1566  FormatInternalUnits( primitive->GetStart() ).c_str(),
1567  FormatInternalUnits( primitive->GetEnd() ).c_str() );
1568  break;
1569 
1570  case PCB_SHAPE_TYPE::ARC: // Arc with rounded ends
1571  m_out->Print( nested_level, "(gr_arc (start %s) (end %s) (angle %s)",
1572  FormatInternalUnits( primitive->GetStart() ).c_str(),
1573  FormatInternalUnits( primitive->GetEnd() ).c_str(),
1574  FormatAngle( primitive->GetAngle() ).c_str() );
1575  break;
1576 
1577  case PCB_SHAPE_TYPE::CIRCLE: // ring or circle (circle if width == 0
1578  m_out->Print( nested_level, "(gr_circle (center %s) (end %s)",
1579  FormatInternalUnits( primitive->GetStart() ).c_str(),
1580  FormatInternalUnits( primitive->GetEnd() ).c_str() );
1581  break;
1582 
1583  case PCB_SHAPE_TYPE::CURVE: // Bezier Curve
1584  m_out->Print( nested_level, "(gr_curve (pts (xy %s) (xy %s) (xy %s) (xy %s))",
1585  FormatInternalUnits( primitive->GetStart() ).c_str(),
1586  FormatInternalUnits( primitive->GetBezControl1() ).c_str(),
1587  FormatInternalUnits( primitive->GetBezControl2() ).c_str(),
1588  FormatInternalUnits( primitive->GetEnd() ).c_str() );
1589  break;
1590 
1591  case PCB_SHAPE_TYPE::POLYGON: // polygon
1592  if( primitive->GetPolyShape().COutline( 0 ).CPoints().size() < 2 )
1593  break; // Malformed polygon.
1594 
1595  {
1596  m_out->Print( nested_level, "(gr_poly (pts\n");
1597 
1598  // Write the polygon corners coordinates:
1599  int newLine = 0;
1600 
1601  for( const VECTOR2I &pt : primitive->GetPolyShape().COutline( 0 ).CPoints() )
1602  {
1603  if( newLine == 0 )
1604  m_out->Print( nested_level+1, "(xy %s)",
1605  FormatInternalUnits( (wxPoint) pt ).c_str() );
1606  else
1607  m_out->Print( 0, " (xy %s)",
1608  FormatInternalUnits( (wxPoint) pt ).c_str() );
1609 
1610  if( ++newLine > 4 || !ADVANCED_CFG::GetCfg().m_CompactSave )
1611  {
1612  newLine = 0;
1613  m_out->Print( 0, "\n" );
1614  }
1615  }
1616 
1617  m_out->Print( newLine ? 0 : nested_level, ")" );
1618  }
1619  break;
1620 
1621  default:
1622  break;
1623  }
1624 
1625  m_out->Print( 0, " (width %s)",
1626  FormatInternalUnits( primitive->GetWidth() ).c_str() );
1627 
1628  if( primitive->IsFilled() )
1629  m_out->Print( 0, " (fill yes)" );
1630 
1631  m_out->Print( 0, ")" );
1632  }
1633 
1634  m_out->Print( 0, "\n");
1635  m_out->Print( aNestLevel+1, ")" ); // end of (basic_shapes
1636  }
1637 
1638  m_out->Print( 0, " (tstamp %s)", TO_UTF8( aPad->m_Uuid.AsString() ) );
1639 
1640  m_out->Print( 0, ")\n" );
1641 }
1642 
1643 
1644 void PCB_IO::format( const PCB_TEXT* aText, int aNestLevel ) const
1645 {
1646  m_out->Print( aNestLevel, "(gr_text %s (at %s",
1647  m_out->Quotew( aText->GetText() ).c_str(),
1648  FormatInternalUnits( aText->GetTextPos() ).c_str() );
1649 
1650  if( aText->GetTextAngle() != 0.0 )
1651  m_out->Print( 0, " %s", FormatAngle( aText->GetTextAngle() ).c_str() );
1652 
1653  m_out->Print( 0, ")" );
1654 
1655  formatLayer( aText );
1656 
1657  m_out->Print( 0, " (tstamp %s)", TO_UTF8( aText->m_Uuid.AsString() ) );
1658 
1659  m_out->Print( 0, "\n" );
1660 
1661  aText->EDA_TEXT::Format( m_out, aNestLevel, m_ctl );
1662 
1663  m_out->Print( aNestLevel, ")\n" );
1664 }
1665 
1666 
1667 void PCB_IO::format( const PCB_GROUP* aGroup, int aNestLevel ) const
1668 {
1669  // Don't write empty groups
1670  if( aGroup->GetItems().empty() )
1671  return;
1672 
1673  m_out->Print( aNestLevel, "(group %s (id %s)\n",
1674  m_out->Quotew( aGroup->GetName() ).c_str(),
1675  TO_UTF8( aGroup->m_Uuid.AsString() ) );
1676 
1677  m_out->Print( aNestLevel + 1, "(members\n" );
1678 
1679  wxArrayString memberIds;
1680 
1681  for( BOARD_ITEM* member : aGroup->GetItems() )
1682  memberIds.Add( member->m_Uuid.AsString() );
1683 
1684  memberIds.Sort();
1685 
1686  for( const wxString& memberId : memberIds )
1687  m_out->Print( aNestLevel + 2, "%s\n", TO_UTF8( memberId ) );
1688 
1689  m_out->Print( aNestLevel + 1, ")\n" ); // Close `members` token.
1690  m_out->Print( aNestLevel, ")\n" ); // Close `group` token.
1691 }
1692 
1693 
1694 void PCB_IO::format( const FP_TEXT* aText, int aNestLevel ) const
1695 {
1696  std::string type;
1697 
1698  switch( aText->GetType() )
1699  {
1700  case FP_TEXT::TEXT_is_REFERENCE: type = "reference"; break;
1701  case FP_TEXT::TEXT_is_VALUE: type = "value"; break;
1702  case FP_TEXT::TEXT_is_DIVERS: type = "user";
1703  }
1704 
1705  m_out->Print( aNestLevel, "(fp_text %s %s (at %s",
1706  type.c_str(),
1707  m_out->Quotew( aText->GetText() ).c_str(),
1708  FormatInternalUnits( aText->GetPos0() ).c_str() );
1709 
1710  // Due to Pcbnew history, fp_text angle is saved as an absolute on screen angle,
1711  // but internally the angle is held relative to its parent footprint. parent
1712  // may be NULL when saving a footprint outside a BOARD.
1713  double orient = aText->GetTextAngle();
1714  FOOTPRINT* parent = (FOOTPRINT*) aText->GetParent();
1715 
1716  if( parent )
1717  {
1718  // GetTextAngle() is always in -360..+360 range because of
1719  // FP_TEXT::SetTextAngle(), but summing that angle with an
1720  // additional board angle could kick sum up >= 360 or <= -360, so to have
1721  // consistent results, normalize again for the BOARD save. A footprint
1722  // save does not use this code path since parent is NULL.
1723 #if 0
1724  // This one could be considered reasonable if you like positive angles
1725  // in your board text.
1726  orient = NormalizeAnglePos( orient + parent->GetOrientation() );
1727 #else
1728  // Choose compatibility for now, even though this is only a 720 degree clamp
1729  // with two possible values for every angle.
1730  orient = NormalizeAngle360Min( orient + parent->GetOrientation() );
1731 #endif
1732  }
1733 
1734  if( orient != 0.0 )
1735  m_out->Print( 0, " %s", FormatAngle( orient ).c_str() );
1736 
1737  if( !aText->IsKeepUpright() )
1738  m_out->Print( 0, " unlocked" );
1739 
1740  m_out->Print( 0, ")" );
1741  formatLayer( aText );
1742 
1743  if( !aText->IsVisible() )
1744  m_out->Print( 0, " hide" );
1745 
1746  m_out->Print( 0, "\n" );
1747 
1748  aText->EDA_TEXT::Format( m_out, aNestLevel, m_ctl | CTL_OMIT_HIDE );
1749 
1750  m_out->Print( aNestLevel + 1, "(tstamp %s)\n", TO_UTF8( aText->m_Uuid.AsString() ) );
1751 
1752  m_out->Print( aNestLevel, ")\n" );
1753 }
1754 
1755 
1756 void PCB_IO::format( const TRACK* aTrack, int aNestLevel ) const
1757 {
1758  if( aTrack->Type() == PCB_VIA_T )
1759  {
1760  PCB_LAYER_ID layer1, layer2;
1761 
1762  const VIA* via = static_cast<const VIA*>( aTrack );
1763  BOARD* board = (BOARD*) via->GetParent();
1764 
1765  wxCHECK_RET( board != nullptr, wxT( "Via " ) +
1766  via->GetSelectMenuText( EDA_UNITS::MILLIMETRES ) + wxT( " has no parent." ) );
1767 
1768  m_out->Print( aNestLevel, "(via" );
1769 
1770  via->LayerPair( &layer1, &layer2 );
1771 
1772  switch( via->GetViaType() )
1773  {
1774  case VIATYPE::THROUGH: // Default shape not saved.
1775  break;
1776 
1777  case VIATYPE::BLIND_BURIED:
1778  m_out->Print( 0, " blind" );
1779  break;
1780 
1781  case VIATYPE::MICROVIA:
1782  m_out->Print( 0, " micro" );
1783  break;
1784 
1785  default:
1786  THROW_IO_ERROR( wxString::Format( _( "unknown via type %d" ), via->GetViaType() ) );
1787  }
1788 
1789  if( via->IsLocked() )
1790  m_out->Print( 0, " locked" );
1791 
1792  m_out->Print( 0, " (at %s) (size %s)",
1793  FormatInternalUnits( aTrack->GetStart() ).c_str(),
1794  FormatInternalUnits( aTrack->GetWidth() ).c_str() );
1795 
1796  // Old boards were using UNDEFINED_DRILL_DIAMETER value in file for via drill when
1797  // via drill was the netclass value.
1798  // recent boards always set the via drill to the actual value, but now we need to
1799  // always store the drill value, because netclass value is not stored in the board file.
1800  // Otherwise the drill value of some (old) vias can be unknown
1801  if( via->GetDrill() != UNDEFINED_DRILL_DIAMETER )
1802  m_out->Print( 0, " (drill %s)", FormatInternalUnits( via->GetDrill() ).c_str() );
1803  else // Probably old board!
1804  m_out->Print( 0, " (drill %s)", FormatInternalUnits( via->GetDrillValue() ).c_str() );
1805 
1806  m_out->Print( 0, " (layers %s %s)",
1807  m_out->Quotew( LSET::Name( layer1 ) ).c_str(),
1808  m_out->Quotew( LSET::Name( layer2 ) ).c_str() );
1809 
1810  if( via->GetRemoveUnconnected() )
1811  {
1812  m_out->Print( 0, " (remove_unused_layers)" );
1813 
1814  if( via->GetKeepTopBottom() )
1815  m_out->Print( 0, " (keep_end_layers)" );
1816  }
1817 
1818  if( via->GetIsFree() )
1819  m_out->Print( 0, " (free)" );
1820  }
1821  else if( aTrack->Type() == PCB_ARC_T )
1822  {
1823  const ARC* arc = static_cast<const ARC*>( aTrack );
1824  std::string locked = arc->IsLocked() ? " locked" : "";
1825 
1826  m_out->Print( aNestLevel, "(arc%s (start %s) (mid %s) (end %s) (width %s)",
1827  locked.c_str(),
1828  FormatInternalUnits( arc->GetStart() ).c_str(),
1829  FormatInternalUnits( arc->GetMid() ).c_str(),
1830  FormatInternalUnits( arc->GetEnd() ).c_str(),
1831  FormatInternalUnits( arc->GetWidth() ).c_str() );
1832 
1833  m_out->Print( 0, " (layer %s)", m_out->Quotew( LSET::Name( arc->GetLayer() ) ).c_str() );
1834  }
1835  else
1836  {
1837  std::string locked = aTrack->IsLocked() ? " locked" : "";
1838 
1839  m_out->Print( aNestLevel, "(segment%s (start %s) (end %s) (width %s)",
1840  locked.c_str(),
1841  FormatInternalUnits( aTrack->GetStart() ).c_str(),
1842  FormatInternalUnits( aTrack->GetEnd() ).c_str(),
1843  FormatInternalUnits( aTrack->GetWidth() ).c_str() );
1844 
1845  m_out->Print( 0, " (layer %s)", m_out->Quotew( LSET::Name( aTrack->GetLayer() ) ).c_str() );
1846  }
1847 
1848  m_out->Print( 0, " (net %d)", m_mapping->Translate( aTrack->GetNetCode() ) );
1849 
1850  m_out->Print( 0, " (tstamp %s)", TO_UTF8( aTrack->m_Uuid.AsString() ) );
1851 
1852  m_out->Print( 0, ")\n" );
1853 }
1854 
1855 
1856 void PCB_IO::format( const ZONE* aZone, int aNestLevel ) const
1857 {
1858  std::string locked = aZone->IsLocked() ? " locked" : "";
1859 
1860  // Save the NET info; For keepout zones, net code and net name are irrelevant
1861  // so be sure a dummy value is stored, just for ZONE compatibility
1862  // (perhaps netcode and netname should be not stored)
1863  m_out->Print( aNestLevel, "(zone%s (net %d) (net_name %s)",
1864  locked.c_str(),
1865  aZone->GetIsRuleArea() ? 0 : m_mapping->Translate( aZone->GetNetCode() ),
1866  m_out->Quotew( aZone->GetIsRuleArea() ? wxT("") : aZone->GetNetname() ).c_str() );
1867 
1868  // If a zone exists on multiple layers, format accordingly
1869  if( aZone->GetLayerSet().count() > 1 )
1870  {
1871  formatLayers( aZone->GetLayerSet() );
1872  }
1873  else
1874  {
1875  formatLayer( aZone );
1876  }
1877 
1878  m_out->Print( 0, " (tstamp %s)", TO_UTF8( aZone->m_Uuid.AsString() ) );
1879 
1880  if( !aZone->GetZoneName().empty() )
1881  m_out->Print( 0, " (name %s)", m_out->Quotew( aZone->GetZoneName() ).c_str() );
1882 
1883  // Save the outline aux info
1884  std::string hatch;
1885 
1886  switch( aZone->GetHatchStyle() )
1887  {
1888  default:
1889  case ZONE_BORDER_DISPLAY_STYLE::NO_HATCH: hatch = "none"; break;
1890  case ZONE_BORDER_DISPLAY_STYLE::DIAGONAL_EDGE: hatch = "edge"; break;
1891  case ZONE_BORDER_DISPLAY_STYLE::DIAGONAL_FULL: hatch = "full"; break;
1892  }
1893 
1894  m_out->Print( 0, " (hatch %s %s)\n", hatch.c_str(),
1895  FormatInternalUnits( aZone->GetBorderHatchPitch() ).c_str() );
1896 
1897  if( aZone->GetPriority() > 0 )
1898  m_out->Print( aNestLevel+1, "(priority %d)\n", aZone->GetPriority() );
1899 
1900  m_out->Print( aNestLevel+1, "(connect_pads" );
1901 
1902  switch( aZone->GetPadConnection() )
1903  {
1904  default:
1905  case ZONE_CONNECTION::THERMAL: // Default option not saved or loaded.
1906  break;
1907 
1909  m_out->Print( 0, " thru_hole_only" );
1910  break;
1911 
1912  case ZONE_CONNECTION::FULL:
1913  m_out->Print( 0, " yes" );
1914  break;
1915 
1916  case ZONE_CONNECTION::NONE:
1917  m_out->Print( 0, " no" );
1918  break;
1919  }
1920 
1921  m_out->Print( 0, " (clearance %s))\n",
1922  FormatInternalUnits( aZone->GetLocalClearance() ).c_str() );
1923 
1924  m_out->Print( aNestLevel+1, "(min_thickness %s)",
1925  FormatInternalUnits( aZone->GetMinThickness() ).c_str() );
1926 
1927  // write it only if V 6.O version option is used (i.e. do not write if the "legacy"
1928  // algorithm is used)
1929  if( !aZone->GetFilledPolysUseThickness() )
1930  m_out->Print( 0, " (filled_areas_thickness no)" );
1931 
1932  m_out->Print( 0, "\n" );
1933 
1934  if( aZone->GetIsRuleArea() )
1935  {
1936  m_out->Print( aNestLevel + 1,
1937  "(keepout (tracks %s) (vias %s) (pads %s ) (copperpour %s) "
1938  "(footprints %s))\n",
1939  aZone->GetDoNotAllowTracks() ? "not_allowed" : "allowed",
1940  aZone->GetDoNotAllowVias() ? "not_allowed" : "allowed",
1941  aZone->GetDoNotAllowPads() ? "not_allowed" : "allowed",
1942  aZone->GetDoNotAllowCopperPour() ? "not_allowed" : "allowed",
1943  aZone->GetDoNotAllowFootprints() ? "not_allowed" : "allowed" );
1944  }
1945 
1946  m_out->Print( aNestLevel + 1, "(fill" );
1947 
1948  // Default is not filled.
1949  if( aZone->IsFilled() )
1950  m_out->Print( 0, " yes" );
1951 
1952  // Default is polygon filled.
1953  if( aZone->GetFillMode() == ZONE_FILL_MODE::HATCH_PATTERN )
1954  m_out->Print( 0, " (mode hatch)" );
1955 
1956  m_out->Print( 0, " (thermal_gap %s) (thermal_bridge_width %s)",
1957  FormatInternalUnits( aZone->GetThermalReliefGap() ).c_str(),
1958  FormatInternalUnits( aZone->GetThermalReliefSpokeWidth() ).c_str() );
1959 
1961  {
1962  m_out->Print( 0, " (smoothing" );
1963 
1964  switch( aZone->GetCornerSmoothingType() )
1965  {
1967  m_out->Print( 0, " chamfer" );
1968  break;
1969 
1971  m_out->Print( 0, " fillet" );
1972  break;
1973 
1974  default:
1975  THROW_IO_ERROR( wxString::Format( _( "unknown zone corner smoothing type %d" ),
1976  aZone->GetCornerSmoothingType() ) );
1977  }
1978  m_out->Print( 0, ")" );
1979 
1980  if( aZone->GetCornerRadius() != 0 )
1981  m_out->Print( 0, " (radius %s)",
1982  FormatInternalUnits( aZone->GetCornerRadius() ).c_str() );
1983  }
1984 
1986  {
1987  m_out->Print( 0, " (island_removal_mode %d) (island_area_min %s)",
1988  static_cast<int>( aZone->GetIslandRemovalMode() ),
1989  FormatInternalUnits( aZone->GetMinIslandArea() / IU_PER_MM ).c_str() );
1990  }
1991 
1992  if( aZone->GetFillMode() == ZONE_FILL_MODE::HATCH_PATTERN )
1993  {
1994  m_out->Print( 0, "\n" );
1995  m_out->Print( aNestLevel+2, "(hatch_thickness %s) (hatch_gap %s) (hatch_orientation %s)",
1996  FormatInternalUnits( aZone->GetHatchThickness() ).c_str(),
1997  FormatInternalUnits( aZone->GetHatchGap() ).c_str(),
1998  Double2Str( aZone->GetHatchOrientation() ).c_str() );
1999 
2000  if( aZone->GetHatchSmoothingLevel() > 0 )
2001  {
2002  m_out->Print( 0, "\n" );
2003  m_out->Print( aNestLevel+2, "(hatch_smoothing_level %d) (hatch_smoothing_value %s)",
2004  aZone->GetHatchSmoothingLevel(),
2005  Double2Str( aZone->GetHatchSmoothingValue() ).c_str() );
2006  }
2007 
2008  m_out->Print( 0, "\n" );
2009  m_out->Print( aNestLevel+2, "(hatch_border_algorithm %s) (hatch_min_hole_area %s)",
2010  aZone->GetHatchBorderAlgorithm() ? "hatch_thickness" : "min_thickness",
2011  Double2Str( aZone->GetHatchHoleMinArea() ).c_str() );
2012  }
2013 
2014  m_out->Print( 0, ")\n" );
2015 
2016  int newLine = 0;
2017 
2018  if( aZone->GetNumCorners() )
2019  {
2020  bool new_polygon = true;
2021  bool is_closed = false;
2022 
2023  for( auto iterator = aZone->CIterateWithHoles(); iterator; ++iterator )
2024  {
2025  if( new_polygon )
2026  {
2027  newLine = 0;
2028  m_out->Print( aNestLevel + 1, "(polygon\n" );
2029  m_out->Print( aNestLevel + 2, "(pts\n" );
2030  new_polygon = false;
2031  is_closed = false;
2032  }
2033 
2034  if( newLine == 0 )
2035  m_out->Print( aNestLevel + 3, "(xy %s %s)",
2036  FormatInternalUnits( iterator->x ).c_str(),
2037  FormatInternalUnits( iterator->y ).c_str() );
2038  else
2039  m_out->Print( 0, " (xy %s %s)",
2040  FormatInternalUnits( iterator->x ).c_str(),
2041  FormatInternalUnits( iterator->y ).c_str() );
2042 
2043  if( newLine < 4 && ADVANCED_CFG::GetCfg().m_CompactSave )
2044  {
2045  newLine += 1;
2046  }
2047  else
2048  {
2049  newLine = 0;
2050  m_out->Print( 0, "\n" );
2051  }
2052 
2053  if( iterator.IsEndContour() )
2054  {
2055  is_closed = true;
2056 
2057  if( newLine != 0 )
2058  m_out->Print( 0, "\n" );
2059 
2060  m_out->Print( aNestLevel + 2, ")\n" );
2061  m_out->Print( aNestLevel + 1, ")\n" );
2062  new_polygon = true;
2063  }
2064  }
2065 
2066  if( !is_closed ) // Should not happen, but...
2067  {
2068  if( newLine != 0 )
2069  m_out->Print( 0, "\n" );
2070 
2071  m_out->Print( aNestLevel + 2, ")\n" );
2072  m_out->Print( aNestLevel + 1, ")\n" );
2073  }
2074  }
2075 
2076  // Save the PolysList (filled areas)
2077  for( PCB_LAYER_ID layer : aZone->GetLayerSet().Seq() )
2078  {
2079  const SHAPE_POLY_SET& fv = aZone->GetFilledPolysList( layer );
2080  newLine = 0;
2081 
2082  if( !fv.IsEmpty() )
2083  {
2084  int poly_index = 0;
2085  bool new_polygon = true;
2086  bool is_closed = false;
2087 
2088  for( auto it = fv.CIterate(); it; ++it )
2089  {
2090  if( new_polygon )
2091  {
2092  newLine = 0;
2093  m_out->Print( aNestLevel + 1, "(filled_polygon\n" );
2094  m_out->Print( aNestLevel + 2, "(layer %s)\n",
2095  m_out->Quotew( LSET::Name( layer ) ).c_str() );
2096 
2097  if( aZone->IsIsland( layer, poly_index ) )
2098  m_out->Print( aNestLevel + 2, "(island)\n" );
2099 
2100  m_out->Print( aNestLevel + 2, "(pts\n" );
2101  new_polygon = false;
2102  is_closed = false;
2103  poly_index++;
2104  }
2105 
2106  if( newLine == 0 )
2107  m_out->Print( aNestLevel + 3, "(xy %s %s)",
2108  FormatInternalUnits( it->x ).c_str(),
2109  FormatInternalUnits( it->y ).c_str() );
2110  else
2111  m_out->Print( 0, " (xy %s %s)", FormatInternalUnits( it->x ).c_str(),
2112  FormatInternalUnits( it->y ).c_str() );
2113 
2114  if( newLine < 4 && ADVANCED_CFG::GetCfg().m_CompactSave )
2115  {
2116  newLine += 1;
2117  }
2118  else
2119  {
2120  newLine = 0;
2121  m_out->Print( 0, "\n" );
2122  }
2123 
2124  if( it.IsEndContour() )
2125  {
2126  is_closed = true;
2127 
2128  if( newLine != 0 )
2129  m_out->Print( 0, "\n" );
2130 
2131  m_out->Print( aNestLevel + 2, ")\n" );
2132  m_out->Print( aNestLevel + 1, ")\n" );
2133  new_polygon = true;
2134  }
2135  }
2136 
2137  if( !is_closed ) // Should not happen, but...
2138  m_out->Print( aNestLevel + 1, ")\n" );
2139  }
2140 
2141  // Save the filling segments list
2142  const auto& segs = aZone->FillSegments( layer );
2143 
2144  if( segs.size() )
2145  {
2146  m_out->Print( aNestLevel + 1, "(fill_segments\n" );
2147  m_out->Print( aNestLevel + 2, "(layer %s)\n",
2148  TO_UTF8( BOARD::GetStandardLayerName( layer ) ) );
2149 
2150  for( ZONE_SEGMENT_FILL::const_iterator it = segs.begin(); it != segs.end(); ++it )
2151  {
2152  m_out->Print( aNestLevel + 2, "(pts (xy %s) (xy %s))\n",
2153  FormatInternalUnits( wxPoint( it->A ) ).c_str(),
2154  FormatInternalUnits( wxPoint( it->B ) ).c_str() );
2155  }
2156 
2157  m_out->Print( aNestLevel + 1, ")\n" );
2158  }
2159  }
2160 
2161  m_out->Print( aNestLevel, ")\n" );
2162 }
2163 
2164 
2165 PCB_IO::PCB_IO( int aControlFlags ) :
2166  m_cache( nullptr ),
2167  m_ctl( aControlFlags ),
2168  m_parser( new PCB_PARSER() ),
2169  m_mapping( new NETINFO_MAPPING() )
2170 {
2171  init( nullptr );
2172  m_out = &m_sf;
2173 }
2174 
2175 
2177 {
2178  delete m_cache;
2179  delete m_parser;
2180  delete m_mapping;
2181 }
2182 
2183 
2184 BOARD* PCB_IO::Load( const wxString& aFileName, BOARD* aAppendToMe, const PROPERTIES* aProperties,
2185  PROJECT* aProject )
2186 {
2187  FILE_LINE_READER reader( aFileName );
2188 
2189  BOARD* board = DoLoad( reader, aAppendToMe, aProperties );
2190 
2191  // Give the filename to the board if it's new
2192  if( !aAppendToMe )
2193  board->SetFileName( aFileName );
2194 
2195  return board;
2196 }
2197 
2198 
2199 BOARD* PCB_IO::DoLoad( LINE_READER& aReader, BOARD* aAppendToMe, const PROPERTIES* aProperties )
2200 {
2201  init( aProperties );
2202 
2203  m_parser->SetLineReader( &aReader );
2204  m_parser->SetBoard( aAppendToMe );
2205 
2206  BOARD* board;
2207 
2208  try
2209  {
2210  board = dynamic_cast<BOARD*>( m_parser->Parse() );
2211  }
2212  catch( const FUTURE_FORMAT_ERROR& )
2213  {
2214  // Don't wrap a FUTURE_FORMAT_ERROR in another
2215  throw;
2216  }
2217  catch( const PARSE_ERROR& parse_error )
2218  {
2219  if( m_parser->IsTooRecent() )
2220  throw FUTURE_FORMAT_ERROR( parse_error, m_parser->GetRequiredVersion() );
2221  else
2222  throw;
2223  }
2224 
2225  if( !board )
2226  {
2227  // The parser loaded something that was valid, but wasn't a board.
2228  THROW_PARSE_ERROR( _( "this file does not contain a PCB" ),
2229  m_parser->CurSource(), m_parser->CurLine(),
2230  m_parser->CurLineNumber(), m_parser->CurOffset() );
2231  }
2232 
2233  return board;
2234 }
2235 
2236 
2237 void PCB_IO::init( const PROPERTIES* aProperties )
2238 {
2239  m_board = nullptr;
2240  m_reader = nullptr;
2241  m_props = aProperties;
2242 }
2243 
2244 
2245 void PCB_IO::validateCache( const wxString& aLibraryPath, bool checkModified )
2246 {
2247  if( !m_cache || !m_cache->IsPath( aLibraryPath ) || ( checkModified && m_cache->IsModified() ) )
2248  {
2249  // a spectacular episode in memory management:
2250  delete m_cache;
2251  m_cache = new FP_CACHE( this, aLibraryPath );
2252  m_cache->Load();
2253  }
2254 }
2255 
2256 
2257 void PCB_IO::FootprintEnumerate( wxArrayString& aFootprintNames, const wxString& aLibPath,
2258  bool aBestEfforts, const PROPERTIES* aProperties )
2259 {
2260  LOCALE_IO toggle; // toggles on, then off, the C locale.
2261  wxDir dir( aLibPath );
2262  wxString errorMsg;
2263 
2264  init( aProperties );
2265 
2266  try
2267  {
2268  validateCache( aLibPath );
2269  }
2270  catch( const IO_ERROR& ioe )
2271  {
2272  errorMsg = ioe.What();
2273  }
2274 
2275  // Some of the files may have been parsed correctly so we want to add the valid files to
2276  // the library.
2277 
2278  for( const auto& footprint : m_cache->GetFootprints() )
2279  aFootprintNames.Add( footprint.first );
2280 
2281  if( !errorMsg.IsEmpty() && !aBestEfforts )
2282  THROW_IO_ERROR( errorMsg );
2283 }
2284 
2285 
2286 const FOOTPRINT* PCB_IO::getFootprint( const wxString& aLibraryPath,
2287  const wxString& aFootprintName,
2288  const PROPERTIES* aProperties,
2289  bool checkModified )
2290 {
2291  LOCALE_IO toggle; // toggles on, then off, the C locale.
2292 
2293  init( aProperties );
2294 
2295  try
2296  {
2297  validateCache( aLibraryPath, checkModified );
2298  }
2299  catch( const IO_ERROR& )
2300  {
2301  // do nothing with the error
2302  }
2303 
2304  FOOTPRINT_MAP& footprints = m_cache->GetFootprints();
2305  FOOTPRINT_MAP::const_iterator it = footprints.find( aFootprintName );
2306 
2307  if( it == footprints.end() )
2308  return nullptr;
2309 
2310  return it->second->GetFootprint();
2311 }
2312 
2313 
2314 const FOOTPRINT* PCB_IO::GetEnumeratedFootprint( const wxString& aLibraryPath,
2315  const wxString& aFootprintName,
2316  const PROPERTIES* aProperties )
2317 {
2318  return getFootprint( aLibraryPath, aFootprintName, aProperties, false );
2319 }
2320 
2321 
2322 bool PCB_IO::FootprintExists( const wxString& aLibraryPath, const wxString& aFootprintName,
2323  const PROPERTIES* aProperties )
2324 {
2325  // Note: checking the cache sounds like a good idea, but won't catch files which differ
2326  // only in case.
2327  //
2328  // Since this goes out to the native filesystem, we get platform differences (ie: MSW's
2329  // case-insensitive filesystem) handled "for free".
2330  // Warning: footprint names frequently contain a point. So be careful when initializing
2331  // wxFileName, and use a CTOR with extension specified
2332  wxFileName footprintFile( aLibraryPath, aFootprintName, KiCadFootprintFileExtension );
2333 
2334  return footprintFile.Exists();
2335 }
2336 
2337 
2338 FOOTPRINT* PCB_IO::FootprintLoad( const wxString& aLibraryPath,
2339  const wxString& aFootprintName,
2340  bool aKeepUUID,
2341  const PROPERTIES* aProperties )
2342 {
2343  const FOOTPRINT* footprint = getFootprint( aLibraryPath, aFootprintName, aProperties, true );
2344 
2345  if( footprint )
2346  {
2347  FOOTPRINT* copy;
2348 
2349  if( aKeepUUID )
2350  copy = static_cast<FOOTPRINT*>( footprint->Clone() );
2351  else
2352  copy = static_cast<FOOTPRINT*>( footprint->Duplicate() );
2353 
2354  copy->SetParent( nullptr );
2355  return copy;
2356  }
2357 
2358  return nullptr;
2359 }
2360 
2361 
2362 void PCB_IO::FootprintSave( const wxString& aLibraryPath, const FOOTPRINT* aFootprint,
2363  const PROPERTIES* aProperties )
2364 {
2365  LOCALE_IO toggle; // toggles on, then off, the C locale.
2366 
2367  init( aProperties );
2368 
2369  // In this public PLUGIN API function, we can safely assume it was
2370  // called for saving into a library path.
2372 
2373  validateCache( aLibraryPath );
2374 
2375  if( !m_cache->IsWritable() )
2376  {
2377  if( !m_cache->Exists() )
2378  {
2379  const wxString msg = wxString::Format( _( "Library \"%s\" does not exist.\n"
2380  "Would you like to create it?"),
2381  aLibraryPath );
2382 
2383  if( wxMessageBox( msg, _( "Library Not Found"), wxYES_NO | wxICON_QUESTION ) != wxYES )
2384  return;
2385 
2386  // Save throws its own IO_ERROR on failure, so no need to recreate here
2387  m_cache->Save( nullptr );
2388  }
2389  else
2390  {
2391  wxString msg = wxString::Format( _( "Library \"%s\" is read only" ), aLibraryPath );
2392  THROW_IO_ERROR( msg );
2393  }
2394  }
2395 
2396  wxString footprintName = aFootprint->GetFPID().GetLibItemName();
2397 
2398  FOOTPRINT_MAP& footprints = m_cache->GetFootprints();
2399 
2400  // Quietly overwrite footprint and delete footprint file from path for any by same name.
2401  wxFileName fn( aLibraryPath, aFootprint->GetFPID().GetLibItemName(),
2403 
2404 #ifndef __WINDOWS__
2405  // Write through symlinks, don't replace them
2406  if( fn.Exists( wxFILE_EXISTS_SYMLINK ) )
2407  {
2408  char buffer[ PATH_MAX + 1 ];
2409  ssize_t pathLen = readlink( TO_UTF8( fn.GetFullPath() ), buffer, PATH_MAX );
2410 
2411  if( pathLen > 0 )
2412  {
2413  buffer[ pathLen ] = '\0';
2414  fn.Assign( fn.GetPath() + wxT( "/" ) + wxString::FromUTF8( buffer ) );
2415  fn.Normalize();
2416  }
2417  }
2418 #endif
2419 
2420  if( !fn.IsOk() )
2421  {
2422  THROW_IO_ERROR( wxString::Format( _( "Footprint file name \"%s\" is not valid." ),
2423  fn.GetFullPath() ) );
2424  }
2425 
2426  if( fn.FileExists() && !fn.IsFileWritable() )
2427  {
2428  THROW_IO_ERROR( wxString::Format( _( "No write permissions to delete file \"%s\"" ),
2429  fn.GetFullPath() ) );
2430  }
2431 
2432  wxString fullPath = fn.GetFullPath();
2433  wxString fullName = fn.GetFullName();
2434  FOOTPRINT_MAP::const_iterator it = footprints.find( footprintName );
2435 
2436  if( it != footprints.end() )
2437  {
2438  wxLogTrace( traceKicadPcbPlugin, wxT( "Removing footprint file '%s'." ), fullPath );
2439  footprints.erase( footprintName );
2440  wxRemoveFile( fullPath );
2441  }
2442 
2443  // I need my own copy for the cache
2444  FOOTPRINT* footprint = static_cast<FOOTPRINT*>( aFootprint->Clone() );
2445 
2446  // It's orientation should be zero and it should be on the front layer.
2447  footprint->SetOrientation( 0 );
2448 
2449  if( footprint->GetLayer() != F_Cu )
2450  {
2451  PCBNEW_SETTINGS* cfg = dynamic_cast<PCBNEW_SETTINGS*>( Kiface().KifaceSettings() );
2452 
2453  if( cfg )
2454  footprint->Flip( footprint->GetPosition(), cfg->m_FlipLeftRight );
2455  else
2456  footprint->Flip( footprint->GetPosition(), false );
2457  }
2458 
2459  // Detach it from the board
2460  footprint->SetParent( nullptr );
2461 
2462  wxLogTrace( traceKicadPcbPlugin, wxT( "Creating s-expr footprint file '%s'." ), fullPath );
2463  footprints.insert( footprintName,
2464  new FP_CACHE_ITEM( footprint, WX_FILENAME( fn.GetPath(), fullName ) ) );
2465  m_cache->Save( footprint );
2466 }
2467 
2468 
2469 void PCB_IO::FootprintDelete( const wxString& aLibraryPath, const wxString& aFootprintName,
2470  const PROPERTIES* aProperties )
2471 {
2472  LOCALE_IO toggle; // toggles on, then off, the C locale.
2473 
2474  init( aProperties );
2475 
2476  validateCache( aLibraryPath );
2477 
2478  if( !m_cache->IsWritable() )
2479  {
2480  THROW_IO_ERROR( wxString::Format( _( "Library \"%s\" is read only." ),
2481  aLibraryPath.GetData() ) );
2482  }
2483 
2484  m_cache->Remove( aFootprintName );
2485 }
2486 
2487 
2488 
2489 long long PCB_IO::GetLibraryTimestamp( const wxString& aLibraryPath ) const
2490 {
2491  return FP_CACHE::GetTimestamp( aLibraryPath );
2492 }
2493 
2494 
2495 void PCB_IO::FootprintLibCreate( const wxString& aLibraryPath, const PROPERTIES* aProperties )
2496 {
2497  if( wxDir::Exists( aLibraryPath ) )
2498  {
2499  THROW_IO_ERROR( wxString::Format( _( "Cannot overwrite library path \"%s\"." ),
2500  aLibraryPath.GetData() ) );
2501  }
2502 
2503  LOCALE_IO toggle;
2504 
2505  init( aProperties );
2506 
2507  delete m_cache;
2508  m_cache = new FP_CACHE( this, aLibraryPath );
2509  m_cache->Save();
2510 }
2511 
2512 
2513 bool PCB_IO::FootprintLibDelete( const wxString& aLibraryPath, const PROPERTIES* aProperties )
2514 {
2515  wxFileName fn;
2516  fn.SetPath( aLibraryPath );
2517 
2518  // Return if there is no library path to delete.
2519  if( !fn.DirExists() )
2520  return false;
2521 
2522  if( !fn.IsDirWritable() )
2523  {
2524  THROW_IO_ERROR( wxString::Format( _( "User does not have permission to delete directory "
2525  "\"%s\"." ),
2526  aLibraryPath.GetData() ) );
2527  }
2528 
2529  wxDir dir( aLibraryPath );
2530 
2531  if( dir.HasSubDirs() )
2532  {
2533  THROW_IO_ERROR( wxString::Format( _( "Library directory \"%s\" has unexpected "
2534  "sub-directories." ),
2535  aLibraryPath.GetData() ) );
2536  }
2537 
2538  // All the footprint files must be deleted before the directory can be deleted.
2539  if( dir.HasFiles() )
2540  {
2541  unsigned i;
2542  wxFileName tmp;
2543  wxArrayString files;
2544 
2545  wxDir::GetAllFiles( aLibraryPath, &files );
2546 
2547  for( i = 0; i < files.GetCount(); i++ )
2548  {
2549  tmp = files[i];
2550 
2551  if( tmp.GetExt() != KiCadFootprintFileExtension )
2552  {
2553  THROW_IO_ERROR( wxString::Format( _( "Unexpected file \"%s\" was found in "
2554  "library path \"%s\"." ),
2555  files[i].GetData(), aLibraryPath.GetData() ) );
2556  }
2557  }
2558 
2559  for( i = 0; i < files.GetCount(); i++ )
2560  wxRemoveFile( files[i] );
2561  }
2562 
2563  wxLogTrace( traceKicadPcbPlugin, wxT( "Removing footprint library \"%s\"." ),
2564  aLibraryPath.GetData() );
2565 
2566  // Some of the more elaborate wxRemoveFile() crap puts up its own wxLog dialog
2567  // we don't want that. we want bare metal portability with no UI here.
2568  if( !wxRmdir( aLibraryPath ) )
2569  {
2570  THROW_IO_ERROR( wxString::Format( _( "Footprint library \"%s\" cannot be deleted." ),
2571  aLibraryPath.GetData() ) );
2572  }
2573 
2574  // For some reason removing a directory in Windows is not immediately updated. This delay
2575  // prevents an error when attempting to immediately recreate the same directory when over
2576  // writing an existing library.
2577 #ifdef __WINDOWS__
2578  wxMilliSleep( 250L );
2579 #endif
2580 
2581  if( m_cache && !m_cache->IsPath( aLibraryPath ) )
2582  {
2583  delete m_cache;
2584  m_cache = nullptr;
2585  }
2586 
2587  return true;
2588 }
2589 
2590 
2591 bool PCB_IO::IsFootprintLibWritable( const wxString& aLibraryPath )
2592 {
2593  LOCALE_IO toggle;
2594 
2595  init( nullptr );
2596 
2597  validateCache( aLibraryPath );
2598 
2599  return m_cache->IsWritable();
2600 }
bool GetSuppressZeroes() const
Definition: dimension.h:172
static LSET AllCuMask(int aCuLayerCount=MAX_CU_LAYERS)
Return a mask holding the requested number of Cu PCB_LAYER_IDs.
Definition: lset.cpp:750
FOOTPRINT_MAP & GetFootprints()
bool IsPolyShapeValid() const
Definition: pcb_shape.cpp:1215
int GetNumCorners(void) const
Access to m_Poly parameters.
Definition: zone.h:520
void format(const BOARD *aBoard, int aNestLevel=0) const
bool IsLocked() const override
Definition: footprint.h:282
int GetLocalSolderMaskMargin() const
Definition: pad.h:386
int GetHatchGap() const
Definition: zone.h:253
Definition: track.h:343
OUTPUTFORMATTER * m_out
output any Format()s to this, no ownership
const wxString & GetDescription() const
Definition: footprint.h:193
LSEQ CuStack() const
Return a sequence of copper layers in starting from the front/top and extending to the back/bottom.
Definition: lset.cpp:170
bool GetOverrideTextEnabled() const
Definition: dimension.h:130
An abstract class from which implementation specific LINE_READERs may be derived to read single lines...
Definition: richio.h:80
bool FootprintExists(const wxString &aLibraryPath, const wxString &aFootprintName, const PROPERTIES *aProperties=nullptr) override
Check for the existence of a footprint.
bool IsFilled() const
Definition: pcb_shape.h:96
LINE_READER * m_reader
no ownership here.
const UTF8 & GetLibItemName() const
Definition: lib_id.h:106
const PAGE_INFO & GetPageSettings() const
Definition: board.h:608
bool IsModified()
Return true if the cache is not up-to-date.
bool m_CompactSave
Save files in compact display mode When is is not specified, points are written one per line.
class ALIGNED_DIMENSION, a linear dimension (graphic item)
Definition: typeinfo.h:100
const PCB_PLOT_PARAMS & GetPlotOptions() const
Definition: board.h:611
class LEADER, a leader dimension (graphic item)
Definition: typeinfo.h:101
Definition: typeinfo.h:84
BOARD_ITEM * Parse(const wxString &aClipboardSourceInput)
A PLUGIN derivation for saving and loading Pcbnew s-expression formatted files.
class FP_TEXT, text in a footprint
Definition: typeinfo.h:92
wxPoint m_GridOrigin
origin for grid offsets
bool GetDoNotAllowFootprints() const
Definition: zone.h:735
Container for project specific data.
Definition: project.h:62
void formatBoardLayers(const BOARD *aBoard, int aNestLevel=0) const
formats the board layer information
std::list< FP_3DMODEL > & Models()
Definition: footprint.h:178
const wxString GetLayerName(PCB_LAYER_ID aLayer) const
Return the name of a aLayer.
Definition: board.cpp:342
void formatLayer(const BOARD_ITEM *aItem) const
ZONES & Zones()
Definition: board.h:311
#define CTL_OMIT_AT
Omit position and rotation.
int GetLocalSolderPasteMargin() const
Definition: pad.h:393
DIM_UNITS_FORMAT GetUnitsFormat() const
Definition: dimension.h:166
const FOOTPRINT * getFootprint(const wxString &aLibraryPath, const wxString &aFootprintName, const PROPERTIES *aProperties, bool checkModified)
double GetRoundRectRadiusRatio() const
Definition: pad.h:523
BOARD * Load(const wxString &aFileName, BOARD *aAppendToMe, const PROPERTIES *aProperties=nullptr, PROJECT *aProject=nullptr) override
Load information from some input file format that this PLUGIN implementation knows about into either ...
SHAPE_POLY_SET & GetPolyShape()
Definition: pcb_shape.h:268
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition: locale_io.h:40
wxFileName m_lib_path
int GetHatchThickness() const
Definition: zone.h:250
const KIID_PATH & GetPath() const
Definition: footprint.h:199
Helper class to create more flexible dialogs, including 'do not show again' checkbox handling.
Definition: confirm.h:45
unsigned GetPriority() const
Definition: zone.h:122
const wxPoint & GetEnd() const
Function GetEnd returns the ending point of the graphic.
Definition: pcb_shape.h:156
Manage layers needed to make a physical board.
bool GetIsRuleArea() const
Accessors to parameters used in Rule Area zones:
Definition: zone.h:730
Implementation of conversion functions that require both schematic and board internal units.
This file is part of the common library.
bool IsKeepUpright() const
Definition: fp_text.h:110
SHAPE_POLY_SET::CONST_ITERATOR CIterateWithHoles() const
Return an iterator to visit all points of the zone's main outline with holes.
Definition: zone.h:550
void Save(FOOTPRINT *aFootprint=nullptr)
Save the footprint cache or a single footprint from it to disk.
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:82
int GetPlacementCost180() const
Definition: footprint.h:534
#define CTL_OMIT_TSTAMPS
Omit component time stamp (useless in library)
class PCB_GROUP, a set of BOARD_ITEMs
Definition: typeinfo.h:108
const std::string KiCadFootprintFileExtension
DIM_UNITS_MODE GetUnitsMode() const
Definition: dimension.cpp:150
DIM_TEXT_FRAME GetTextFrame() const
Definition: dimension.h:502
virtual ~PCB_IO()
FP_CACHE * m_cache
Footprint library cache.
boost::ptr_map< wxString, FP_CACHE_ITEM > FOOTPRINT_MAP
PAD_PROP GetProperty() const
Definition: pad.h:374
const wxPoint & GetStart() const
Definition: track.h:116
static constexpr double IU_PER_MM
Mock up a conversion function.
GROUPS & Groups()
The groups must maintain the following invariants.
Definition: board.h:325
A set of BOARD_ITEMs (i.e., without duplicates).
Definition: pcb_group.h:50
Read a Pcbnew s-expression formatted LINE_READER object and returns the appropriate BOARD_ITEM object...
Definition: pcb_parser.h:68
int GetSize() const
Definition: pcb_target.h:67
int GetBorderHatchPitch() const
HatchBorder related methods.
Definition: zone.cpp:890
const PROPERTIES * m_props
passed via Save() or Load(), no ownership, may be NULL.
PCB_SHAPE_TYPE GetShape() const
Definition: pcb_shape.h:130
void Format(OUTPUTFORMATTER *aFormatter, int aNestLevel, int aControl=0) const
bool IsVisible() const
Definition: eda_text.h:193
class CENTER_DIMENSION, a center point marking (graphic item)
Definition: typeinfo.h:102
void init(const PROPERTIES *aProperties)
bool GetDoNotAllowVias() const
Definition: zone.h:732
bool IsEmpty() const
Like smd, does not appear on the solder paste layer (default)
int GetWidth() const
Definition: pcb_shape.h:118
Smd pad, appears on the solder paste layer (default)
virtual const wxPoint & GetStart() const
The dimension's origin is the first feature point for the dimension.
Definition: dimension.h:121
const WX_FILENAME & GetFileName() const
virtual LSET GetLayerSet() const override
Return a std::bitset of all layers on which the item physically resides.
Definition: zone.cpp:289
wxString m_lib_raw_path
double GetHatchSmoothingValue() const
Definition: zone.h:262
bool IsLocked() const override
Definition: pad.cpp:138
double GetOrientation() const
Definition: footprint.h:186
DIM_TEXT_POSITION GetTextPositionMode() const
Definition: dimension.h:179
#define CTL_OMIT_INITIAL_COMMENTS
omit FOOTPRINT initial comments
double GetTextAngle() const
Definition: eda_text.h:181
class PCB_TEXT, text on a layer
Definition: typeinfo.h:91
wxString GetName() const
Definition: wx_filename.cpp:39
class ARC, an arc track segment on a copper layer
Definition: typeinfo.h:97
const wxPoint & GetStart0() const
Definition: fp_shape.h:112
void formatProperties(const BOARD *aBoard, int aNestLevel=0) const
formats the Nets and Netclasses
wxString GetNetname() const
wxString AsString() const
Definition: kiid.cpp:218
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Definition: board.h:593
bool IsFootprintLibWritable(const wxString &aLibraryPath) override
Return true if the library at aLibraryPath is writable.
class FP_SHAPE, a footprint edge
Definition: typeinfo.h:93
class PAD, a pad in a footprint
Definition: typeinfo.h:89
const SHAPE_POLY_SET & GetFilledPolysList(PCB_LAYER_ID aLayer) const
Definition: zone.h:632
wxString GetOverrideText() const
Definition: dimension.h:133
LSET GetEnabledLayers() const
A proxy function that calls the corresponding function in m_BoardSettings Returns a bit-mask of all t...
Definition: board.cpp:447
virtual std::string Quotes(const std::string &aWrapee) const
Check aWrapee input string for a need to be quoted (e.g.
Definition: richio.cpp:437
bool IsFilled() const
Definition: zone.h:231
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:51
void Format(OUTPUTFORMATTER *aFormatter, int aNestLevel, int aControlBits) const
Output the page class to aFormatter in s-expression form.
Definition: page_info.cpp:271
void SetFullName(const wxString &aFileNameAndExtension)
Definition: wx_filename.cpp:33
A name/value tuple with unique names and optional values.
Definition: properties.h:33
long long GetLibraryTimestamp(const wxString &aLibraryPath) const override
Generate a timestamp representing all the files in the library (including the library directory).
#define CTL_FOR_LIBRARY
Format output for a footprint library instead of clipboard or BOARD.
a test point pad
int PointCount() const
Function PointCount()
LSEQ Seq(const PCB_LAYER_ID *aWishListSequence, unsigned aCount) const
Return an LSEQ from the union of this LSET and a desired sequence.
Definition: lset.cpp:411
int GetThermalGap() const
Definition: pad.h:499
int GetPrecision() const
Definition: dimension.h:169
FOOTPRINT_MAP m_footprints
std::unordered_set< BOARD_ITEM * > & GetItems()
Definition: pcb_group.h:68
std::unique_ptr< FOOTPRINT > m_footprint
bool GetKeepTopBottom() const
Definition: pad.h:563
ZONE_FILL_MODE GetFillMode() const
Definition: zone.h:181
timestamp_t GetLastEditTime() const
Definition: footprint.h:332
virtual void SetParent(EDA_ITEM *aParent)
Definition: eda_item.h:166
class TRACK, a track segment (segment on a copper layer)
Definition: typeinfo.h:95
PADS & Pads()
Definition: footprint.h:164
#define CTL_OMIT_FOOTPRINT_VERSION
Omit the version string from the (footprint)
Plated through hole pad.
This file contains miscellaneous commonly used macros and functions.
FP_TEXT & Value()
read/write accessors:
Definition: footprint.h:462
static const char * ShowType(LAYER_T aType)
Convert a LAYER_T enum to a string representation of the layer type.
Definition: board.cpp:407
int GetLineThickness() const
Definition: dimension.h:187
void SetOutputFormatter(OUTPUTFORMATTER *aFormatter)
void SetBoard(const BOARD *aBoard)
Set a BOARD object that is used to prepare the net code map.
Definition: netinfo.h:193
long long m_cache_timestamp
#define UNDEFINED_DRILL_DIAMETER
Definition: track.h:77
void FootprintEnumerate(wxArrayString &aFootprintNames, const wxString &aLibraryPath, bool aBestEfforts, const PROPERTIES *aProperties=nullptr) override
Return a list of footprint names contained within the library at aLibraryPath.
UTF8 GetLibItemNameAndRev() const
Definition: lib_id.cpp:255
polygon (not yet used for tracks, but could be in microwave apps)
FP_TEXT & Reference()
Definition: footprint.h:463
a pad used as heat sink, usually in SMD footprints
int GetThermalReliefGap() const
Definition: zone.h:190
int StrPrintf(std::string *result, const char *format,...)
This is like sprintf() but the output is appended to a std::string instead of to a character array.
Definition: richio.cpp:78
wxPoint GetPosition() const override
Definition: pcb_target.h:61
const std::map< wxString, wxString > & GetProperties() const
Definition: board.h:330
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: macros.h:96
double GetHatchHoleMinArea() const
Definition: zone.h:265
const VECTOR2I & CPoint(int aIndex) const
Function Point()
FP_ZONES & Zones()
Definition: footprint.h:170
bool GetDoNotAllowPads() const
Definition: zone.h:734
long long GetTimestamp()
Definition: wx_filename.cpp:73
const wxPoint & GetOffset() const
Definition: pad.h:249
void Remove(const wxString &aFootprintName)
int GetWidth() const
Definition: pcb_target.h:70
const wxSize & GetDrillSize() const
Definition: pad.h:242
like PAD_PTH, but not plated
std::string FormatAngle(double aAngle)
Function FormatAngle converts aAngle from board units to a string appropriate for writing to file.
Definition: base_units.cpp:506
BOARD_STACKUP & GetStackupDescriptor()
PCB_IO(int aControlFlags=CTL_FOR_BOARD)
PCB_LAYER_ID
A quick note on layer IDs:
A LINE_READER that reads from an open file.
Definition: richio.h:172
wxString GetRequiredVersion()
Return a string representing the version of KiCad required to open this file.
Definition: pcb_parser.cpp:184
bool GetDoNotAllowCopperPour() const
Definition: zone.h:731
wxString GetSuffix() const
Definition: dimension.h:155
LSET is a set of PCB_LAYER_IDs.
#define THROW_PARSE_ERROR(aProblem, aSource, aInputLine, aLineNumber, aByteIndex)
Definition: ki_exception.h:164
pads are covered by copper
FOOTPRINT * FootprintLoad(const wxString &aLibraryPath, const wxString &aFootprintName, bool aKeepUUID=false, const PROPERTIES *aProperties=nullptr) override
Load a footprint having aFootprintName from the aLibraryPath containing a library format that this PL...
int GetThermalSpokeWidth() const
Definition: pad.h:491
const wxPoint & GetMid() const
Definition: track.h:292
void SetBoard(BOARD *aBoard)
Definition: pcb_parser.h:94
int GetMinThickness() const
Definition: zone.h:241
wxString GetPrefix() const
Definition: dimension.h:152
double GetOrientation() const
Return the rotation angle of the pad in a variety of units (the basic call returns tenths of degrees)...
Definition: pad.h:349
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:29
#define CTL_OMIT_LIBNAME
Omit lib alias when saving (used for.
wxString GetName() const
Definition: pcb_group.h:65
int GetHatchSmoothingLevel() const
Definition: zone.h:259
static const wxChar * Name(PCB_LAYER_ID aLayerId)
Return the fixed name association with aLayerId.
Definition: lset.cpp:82
A leader is a dimension-like object pointing to a specific point.
Definition: dimension.h:478
TEXT_TYPE GetType() const
Definition: fp_text.h:141
void formatHeader(const BOARD *aBoard, int aNestLevel=0) const
writes everything that comes before the board_items, like settings and layers etc
bool IsPath(const wxString &aPath) const
Check if aPath is the same as the current cache path.
int GetExtensionHeight() const
Definition: dimension.h:381
Represent a set of closed polygons.
SHAPE_LINE_CHAIN & Outline(int aIndex)
int GetLocalClearance(wxString *aSource) const override
Return any local clearances set in the "classic" (ie: pre-rule) system.
Definition: zone.cpp:492
void formatGeneral(const BOARD *aBoard, int aNestLevel=0) const
formats the General section of the file
void validateCache(const wxString &aLibraryPath, bool checkModified=true)
void SetOrientation(double aNewAngle)
Definition: footprint.cpp:1557
segment with non rounded ends
FOOTPRINTS & Footprints()
Definition: board.h:305
KIFACE_I & Kiface()
Global KIFACE_I "get" accessor.
const wxSize & GetSize() const
Definition: pad.h:232
PCB_TEXT & Text()
Definition: dimension.h:209
void formatNetInformation(const BOARD *aBoard, int aNestLevel=0) const
formats the Nets and Netclasses
const wxPoint & GetStart() const
Function GetStart returns the starting point of the graphic.
Definition: pcb_shape.h:145
ZONE_CONNECTION GetEffectiveZoneConnection(wxString *aSource=nullptr) const
Return the zone connection in effect (either locally overridden or overridden in the parent footprint...
Definition: pad.cpp:791
void Flip(const wxPoint &aCentre, bool aFlipLeftRight) override
Flip this object, i.e.
Definition: footprint.cpp:1367
Smd pad, used in BGA footprints.
bool GetDoNotAllowTracks() const
Definition: zone.h:733
An orthogonal dimension is like an aligned dimension, but the extension lines are locked to the X or ...
Definition: dimension.h:414
const wxString & GetName() const
Definition: pad.h:133
Definition of file extensions used in Kicad.
const wxChar *const traceKicadPcbPlugin
Flag to enable GEDA PCB plugin debug output.
int GetLocalClearance() const
Definition: footprint.h:205
const wxString & GetPinType() const
Definition: pad.h:145
PAD_SHAPE GetShape() const
Definition: pad.h:169
a fiducial (usually a smd) local to the parent footprint
long long TimestampDir(const wxString &aDirPath, const wxString &aFilespec)
A copy of ConvertFileTimeToWx() because wxWidgets left it as a static function private to src/common/...
Definition: common.cpp:510
constexpr std::size_t arrayDim(T const (&)[N]) noexcept
Returns # of elements in an array.
Definition: arraydim.h:31
PCB_IO * m_owner
wxLogTrace helper definitions.
BOARD_ITEM * Duplicate() const override
Create a copy of this BOARD_ITEM.
Definition: footprint.cpp:1601
DRAWINGS & GraphicalItems()
Definition: footprint.h:167
bool IsPlaced() const
Definition: footprint.h:300
virtual void Format(OUTPUTFORMATTER *aFormatter, int aNestLevel, int aControlBits) const
Output the object to aFormatter in s-expression form.
Definition: title_block.cpp:29
const LIB_ID & GetFPID() const
Definition: footprint.h:190
Handle a list of polygons defining a copper zone.
Definition: zone.h:57
LSET GetLayerSet() const override
Return a std::bitset of all layers on which the item physically resides.
Definition: pad.h:368
class ZONE, a copper pour area
Definition: typeinfo.h:105
WX_FILENAME m_filename
void SetFileName(const wxString &aFileName)
Definition: board.h:298
ZONE_BORDER_DISPLAY_STYLE GetHatchStyle() const
Definition: zone.h:610
void formatSetup(const BOARD *aBoard, int aNestLevel=0) const
formats the board setup information
BOARD_ITEM * Parse()
Definition: pcb_parser.cpp:473
Helper class for creating a footprint library cache.
const wxString & GetKeywords() const
Definition: footprint.h:196
int GetThermalWidth() const
Definition: footprint.h:226
NETINFO_MAPPING * m_mapping
mapping for net codes, so only not empty net codes are stored with consecutive integers as net codes
CONST_ITERATOR CIterate(int aFirst, int aLast, bool aIterateHoles=false) const
int GetLocalClearance(wxString *aSource) const override
Return any local clearances set in the "classic" (ie: pre-rule) system.
Definition: pad.cpp:685
LSEQ is a sequence (and therefore also a set) of PCB_LAYER_IDs.
UTF8 Format() const
Definition: lib_id.cpp:233
void FormatBoardStackup(OUTPUTFORMATTER *aFormatter, const BOARD *aBoard, int aNestLevel) const
Write the stackup info on board file.
void FootprintLibCreate(const wxString &aLibraryPath, const PROPERTIES *aProperties=nullptr) override
Create a new empty footprint library at aLibraryPath empty.
LINE_READER * SetLineReader(LINE_READER *aReader)
Set aLineReader into the parser, and returns the previous one, if any.
Definition: pcb_parser.h:87
Thermal relief only for THT pads.
class PCB_TARGET, a target (graphic item)
Definition: typeinfo.h:104
int LAYER_NUM
This can be replaced with int and removed.
double GetLocalSolderPasteMarginRatio() const
Definition: pad.h:396
class FOOTPRINT, a footprint
Definition: typeinfo.h:88
wxString GetPath() const
Definition: wx_filename.cpp:52
const KIID m_Uuid
Definition: eda_item.h:525
int GetHeight() const
Definition: dimension.h:372
const wxSize & GetDelta() const
Definition: pad.h:239
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
const wxPoint & GetPos0() const
Definition: fp_text.h:166
const wxArrayString * GetInitialComments() const
Definition: footprint.h:644
double GetHatchOrientation() const
Definition: zone.h:256
LAYER_T GetLayerType(PCB_LAYER_ID aLayer) const
Return the type of the copper layer given by aLayer.
Definition: board.cpp:376
void Load()
FP_GROUPS & Groups()
Definition: footprint.h:173
Use thermal relief for pads.
unsigned int GetCornerRadius() const
Definition: zone.h:684
const wxPoint & GetPos0() const
Definition: pad.h:226
void SetFPID(const LIB_ID &aFPID)
Definition: footprint.h:191
wxString GetFullPath() const
Definition: wx_filename.cpp:58
int GetHatchBorderAlgorithm() const
Definition: zone.h:268
void Format(const BOARD_ITEM *aItem, int aNestLevel=0) const
Output aItem to aFormatter in s-expression format.
const ISLAND_REMOVAL_MODE GetIslandRemovalMode() const
Definition: zone.h:746
const wxPoint & GetBezier0_C2() const
Definition: fp_shape.h:124
Handle the data for a net.
Definition: netinfo.h:64
bool IsWritable() const
bool IsTooRecent()
Return whether a version number, if any was parsed, was too recent.
Definition: pcb_parser.h:116
static wxString PCB_SHAPE_TYPE_T_asString(PCB_SHAPE_TYPE a)
Definition: board_item.h:59
int GetLocalSolderMaskMargin() const
Definition: footprint.h:202
virtual bool IsLocked() const
Definition: board_item.h:249
A filename or source description, a problem input line, a line number, a byte offset,...
Definition: ki_exception.h:118
CUST_PAD_SHAPE_IN_ZONE GetCustomShapeInZoneOpt() const
Definition: pad.h:187
virtual wxString GetClass() const =0
Return the class name.
TITLE_BLOCK & GetTitleBlock()
Definition: board.h:614
const wxPoint & GetBezier0_C1() const
Definition: fp_shape.h:121
A wrapper around a wxFileName which is much more performant with a subset of the API.
Definition: wx_filename.h:36
int GetWidth() const
Definition: track.h:110
int GetAttributes() const
Definition: footprint.h:231
a fiducial (usually a smd) for the full board
double GetChamferRectRatio() const
Definition: pad.h:532
PAD_DRILL_SHAPE_T GetDrillShape() const
Definition: pad.h:354
Definition: track.h:262
no special fabrication property
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:190
#define SEXPR_BOARD_FILE_VERSION
Current s-expression file format version. 2 was the last legacy format version.
bool IsIsland(PCB_LAYER_ID aLayer, int aPolyIdx) const
Check if a given filled polygon is an insulated island.
Definition: zone.cpp:1125
#define _(s)
Definition: 3d_actions.cpp:33
bool FootprintLibDelete(const wxString &aLibraryPath, const PROPERTIES *aProperties=nullptr) override
Delete an existing footprint library and returns true, or if library does not exist returns false,...
SHAPE_LINE_CHAIN.
PAD_ATTRIB GetAttribute() const
Definition: pad.h:371
#define CTL_OMIT_PATH
Omit component sheet time stamp (useless in library)
std::map< wxString, FOOTPRINT * > FOOTPRINT_MAP
Definition: eagle_parser.h:50
const wxPoint & GetBezControl2() const
Definition: pcb_shape.h:136
T NormalizeAnglePos(T Angle)
Normalize angle to be in the 0.0 .
Definition: trigo.h:279
wxString AsString() const
Definition: kiid.cpp:277
class ZONE, managed by a footprint
Definition: typeinfo.h:94
#define CTL_OMIT_PAD_NETS
Omit pads net names (useless in library)
double GetAngle() const
Definition: pcb_shape.h:127
BOARD * m_board
which BOARD, no ownership here
int GetShape() const
Definition: pcb_target.h:64
usual segment : line with rounded ends
const wxPoint & GetEnd0() const
Definition: fp_shape.h:115
Pads are not covered.
wxPoint GetPosition() const override
Definition: footprint.h:182
double GetLocalSolderPasteMarginRatio() const
Definition: footprint.h:219
const wxPoint & GetEnd() const
Definition: track.h:113
Used for text file output.
Definition: richio.h:453
int ShowModal() override
Definition: confirm.cpp:99
const std::map< wxString, wxString > & GetProperties() const
Definition: footprint.h:469
int GetChamferPositions() const
Definition: pad.h:542
const wxPoint & GetTextPos() const
Definition: eda_text.h:254
wxString GroupsSanityCheck(bool repair=false)
Definition: board.cpp:2079
static const ADVANCED_CFG & GetCfg()
Get the singleton instance's config, which is shared by all consumers.
a pad with a castellated through hole
class ORTHOGONAL_DIMENSION, a linear dimension constrained to x/y
Definition: typeinfo.h:103
class VIA, a via (like a track segment on a copper layer)
Definition: typeinfo.h:96
ZONE_CONNECTION GetZoneConnection() const
Definition: footprint.h:223
int GetPlacementCost90() const
Definition: footprint.h:537
virtual void Save(const wxString &aFileName, BOARD *aBoard, const PROPERTIES *aProperties=nullptr) override
Write aBoard to a storage file in a format that this PLUGIN implementation knows about or it can be u...
Pcbnew s-expression file format parser definition.
long long int GetMinIslandArea() const
Definition: zone.h:750
ZONE_SEGMENT_FILL & FillSegments(PCB_LAYER_ID aLayer)
Definition: zone.h:305
int GetThermalReliefSpokeWidth() const
Definition: zone.h:200
Variant of PARSE_ERROR indicating that a syntax or related error was likely caused by a file generate...
Definition: ki_exception.h:174
FP_CACHE(PCB_IO *aOwner, const wxString &aLibraryPath)
bool m_cache_dirty
int GetLocalSolderPasteMargin() const
Definition: footprint.h:216
const FOOTPRINT * GetFootprint() const
void formatLayers(LSET aLayerMask, int aNestLevel=0) const
int PRINTF_FUNC Print(int nestLevel, const char *fmt,...)
Format and write text to the output stream.
Definition: richio.cpp:408
ZONE_CONNECTION GetPadConnection(PAD *aPad, wxString *aSource=nullptr) const
Definition: zone.cpp:774
void FootprintDelete(const wxString &aLibraryPath, const wxString &aFootprintName, const PROPERTIES *aProperties=nullptr) override
Delete aFootprintName from the library at aLibraryPath.
Definition: pad.h:60
For better understanding of the points that make a dimension:
Definition: dimension.h:334
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:254
wxString GetZoneName() const
Definition: zone.h:129
Arcs (with rounded ends)
EDA_ITEM * Clone() const override
Create a duplicate of this item with linked list members set to NULL.
Definition: footprint.cpp:1169
BOARD_ITEM_CONTAINER * GetParent() const
Definition: board_item.h:168
const FOOTPRINT * GetEnumeratedFootprint(const wxString &aLibraryPath, const wxString &aFootprintName, const PROPERTIES *aProperties=nullptr) override
A version of FootprintLoad() for use after FootprintEnumerate() for more efficient cache management.
FP_CACHE_ITEM(FOOTPRINT *aFootprint, const WX_FILENAME &aFileName)
Is a LINE_READER that reads from a multiline 8 bit wide std::string.
Definition: richio.h:237
#define CTL_OMIT_HIDE
Definition: eda_text.h:56
Abstract dimension API.
Definition: dimension.h:95
class PCB_SHAPE, a segment not on copper layers
Definition: typeinfo.h:90
bool GetKeepTextAligned() const
Definition: dimension.h:175
int GetPadToDieLength() const
Definition: pad.h:384
static long long GetTimestamp(const wxString &aLibPath)
Generate a timestamp representing all source files in the cache (including the parent directory).
bool Exists() const
virtual PCB_LAYER_ID GetLayer() const
Return the primary layer this item is on.
Definition: board_item.h:173
void FootprintSave(const wxString &aLibraryPath, const FOOTPRINT *aFootprint, const PROPERTIES *aProperties=nullptr) override
Write aFootprint to an existing library located at aLibraryPath.
virtual const wxPoint & GetEnd() const
Definition: dimension.h:124
const wxString & GetPinFunction() const
Definition: pad.h:139
static const int UNCONNECTED
Constant that forces initialization of a netinfo item to the NETINFO_ITEM ORPHANED (typically -1) whe...
Definition: netinfo.h:365
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:75
int GetArrowLength() const
Definition: dimension.h:181
#define THROW_IO_ERROR(msg)
Definition: ki_exception.h:38
const wxPoint & GetBezControl1() const
Definition: pcb_shape.h:133
bool GetRemoveUnconnected() const
Definition: pad.h:557
int GetThermalGap() const
Definition: footprint.h:229
DRAWINGS & Drawings()
Definition: board.h:308
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition: eda_text.h:133
wxPoint m_AuxOrigin
origin for plot exports
int GetCornerSmoothingType() const
Definition: zone.h:680
wxString GetPath() const
int GetExtensionOffset() const
Definition: dimension.h:185
TRACKS & Tracks()
Definition: board.h:302
const std::vector< std::shared_ptr< PCB_SHAPE > > & GetPrimitives() const
Accessor to the basic shape list for custom-shaped pads.
Definition: pad.h:301
PAD_SHAPE GetAnchorPadShape() const
Definition: pad.h:182
std::string FormatInternalUnits(int aValue)
Function FormatInternalUnits converts aValue from internal units to a string appropriate for writing ...
Definition: base_units.cpp:471
Definition: track.h:83
int Translate(int aNetCode) const
Translate net number according to the map prepared by Update() function.
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:163
std::string Quotew(const wxString &aWrapee) const
Definition: richio.cpp:476
static wxString GetStandardLayerName(PCB_LAYER_ID aLayerId)
Return an "English Standard" name of a PCB layer when given aLayerNumber.
Definition: board.h:676
bool GetFilledPolysUseThickness() const
Definition: zone.h:686
BOARD * DoLoad(LINE_READER &aReader, BOARD *aAppendToMe, const PROPERTIES *aProperties)
Container for design settings for a BOARD object.
std::string Double2Str(double aValue)
Prints a float number without using scientific notation and no trailing 0 We want to avoid scientific...
Definition: string.cpp:873
Marks the center of a circle or arc with a cross shape The size and orientation of the cross is adjus...
Definition: dimension.h:517