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