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