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 += wxT( "\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( wxT( "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( wxT( "unknown pad attribute: %d" ),
1405  aPad->GetAttribute() ) );
1406  }
1407 
1408  const char* property = nullptr;
1409 
1410  switch( aPad->GetProperty() )
1411  {
1412  case PAD_PROP::NONE: break; // could be "none"
1413  case PAD_PROP::BGA: property = "pad_prop_bga"; break;
1414  case PAD_PROP::FIDUCIAL_GLBL: property = "pad_prop_fiducial_glob"; break;
1415  case PAD_PROP::FIDUCIAL_LOCAL: property = "pad_prop_fiducial_loc"; break;
1416  case PAD_PROP::TESTPOINT: property = "pad_prop_testpoint"; break;
1417  case PAD_PROP::HEATSINK: property = "pad_prop_heatsink"; break;
1418  case PAD_PROP::CASTELLATED: property = "pad_prop_castellated"; break;
1419 
1420  default:
1421  THROW_IO_ERROR( wxString::Format( wxT( "unknown pad property: %d" ),
1422  aPad->GetProperty() ) );
1423  }
1424 
1425  m_out->Print( aNestLevel, "(pad %s %s %s",
1426  m_out->Quotew( aPad->GetNumber() ).c_str(),
1427  type,
1428  shape );
1429 
1430  if( aPad->IsLocked() )
1431  m_out->Print( 0, " locked" );
1432 
1433  m_out->Print( 0, " (at %s", FormatInternalUnits( aPad->GetPos0() ).c_str() );
1434 
1435  if( aPad->GetOrientation() != 0.0 )
1436  m_out->Print( 0, " %s", FormatAngle( aPad->GetOrientation() ).c_str() );
1437 
1438  m_out->Print( 0, ")" );
1439 
1440  m_out->Print( 0, " (size %s)", FormatInternalUnits( aPad->GetSize() ).c_str() );
1441 
1442  if( (aPad->GetDelta().GetWidth()) != 0 || (aPad->GetDelta().GetHeight() != 0 ) )
1443  m_out->Print( 0, " (rect_delta %s)", FormatInternalUnits( aPad->GetDelta() ).c_str() );
1444 
1445  wxSize sz = aPad->GetDrillSize();
1446  wxPoint shapeoffset = aPad->GetOffset();
1447 
1448  if( (sz.GetWidth() > 0) || (sz.GetHeight() > 0) ||
1449  (shapeoffset.x != 0) || (shapeoffset.y != 0) )
1450  {
1451  m_out->Print( 0, " (drill" );
1452 
1453  if( aPad->GetDrillShape() == PAD_DRILL_SHAPE_OBLONG )
1454  m_out->Print( 0, " oval" );
1455 
1456  if( sz.GetWidth() > 0 )
1457  m_out->Print( 0, " %s", FormatInternalUnits( sz.GetWidth() ).c_str() );
1458 
1459  if( sz.GetHeight() > 0 && sz.GetWidth() != sz.GetHeight() )
1460  m_out->Print( 0, " %s", FormatInternalUnits( sz.GetHeight() ).c_str() );
1461 
1462  if( (shapeoffset.x != 0) || (shapeoffset.y != 0) )
1463  m_out->Print( 0, " (offset %s)", FormatInternalUnits( aPad->GetOffset() ).c_str() );
1464 
1465  m_out->Print( 0, ")" );
1466  }
1467 
1468  // Add pad property, if exists.
1469  if( property )
1470  m_out->Print( 0, " (property %s)", property );
1471 
1472  formatLayers( aPad->GetLayerSet() );
1473 
1474  if( aPad->GetAttribute() == PAD_ATTRIB::PTH )
1475  {
1476  if( aPad->GetRemoveUnconnected() )
1477  {
1478  m_out->Print( 0, " (remove_unused_layers)" );
1479 
1480  if( aPad->GetKeepTopBottom() )
1481  m_out->Print( 0, " (keep_end_layers)" );
1482  }
1483  }
1484 
1485  // Output the radius ratio for rounded and chamfered rect pads
1486  if( aPad->GetShape() == PAD_SHAPE::ROUNDRECT || aPad->GetShape() == PAD_SHAPE::CHAMFERED_RECT)
1487  {
1488  m_out->Print( 0, " (roundrect_rratio %s)",
1489  Double2Str( aPad->GetRoundRectRadiusRatio() ).c_str() );
1490  }
1491 
1492  // Output the chamfer corners for chamfered rect pads
1493  if( aPad->GetShape() == PAD_SHAPE::CHAMFERED_RECT)
1494  {
1495  m_out->Print( 0, "\n" );
1496 
1497  m_out->Print( aNestLevel+1, "(chamfer_ratio %s)",
1498  Double2Str( aPad->GetChamferRectRatio() ).c_str() );
1499 
1500  m_out->Print( 0, " (chamfer" );
1501 
1502  if( ( aPad->GetChamferPositions() & RECT_CHAMFER_TOP_LEFT ) )
1503  m_out->Print( 0, " top_left" );
1504 
1505  if( ( aPad->GetChamferPositions() & RECT_CHAMFER_TOP_RIGHT ) )
1506  m_out->Print( 0, " top_right" );
1507 
1508  if( ( aPad->GetChamferPositions() & RECT_CHAMFER_BOTTOM_LEFT ) )
1509  m_out->Print( 0, " bottom_left" );
1510 
1512  m_out->Print( 0, " bottom_right" );
1513 
1514  m_out->Print( 0, ")" );
1515  }
1516 
1517  std::string output;
1518 
1519  // Unconnected pad is default net so don't save it.
1520  if( !( m_ctl & CTL_OMIT_PAD_NETS ) && aPad->GetNetCode() != NETINFO_LIST::UNCONNECTED )
1521  {
1522  StrPrintf( &output, " (net %d %s)", m_mapping->Translate( aPad->GetNetCode() ),
1523  m_out->Quotew( aPad->GetNetname() ).c_str() );
1524  }
1525 
1526  // Pin functions and types are closely related to nets, so if CTL_OMIT_NETS is set, omit
1527  // them as well (for instance when saved from library editor).
1528  if( !( m_ctl & CTL_OMIT_PAD_NETS ) )
1529  {
1530  if( !aPad->GetPinFunction().IsEmpty() )
1531  {
1532  StrPrintf( &output, " (pinfunction %s)",
1533  m_out->Quotew( aPad->GetPinFunction() ).c_str() );
1534  }
1535 
1536  if( !aPad->GetPinType().IsEmpty() )
1537  {
1538  StrPrintf( &output, " (pintype %s)",
1539  m_out->Quotew( aPad->GetPinType() ).c_str() );
1540  }
1541  }
1542 
1543  if( aPad->GetPadToDieLength() != 0 )
1544  {
1545  StrPrintf( &output, " (die_length %s)",
1546  FormatInternalUnits( aPad->GetPadToDieLength() ).c_str() );
1547  }
1548 
1549  if( aPad->GetLocalSolderMaskMargin() != 0 )
1550  {
1551  StrPrintf( &output, " (solder_mask_margin %s)",
1552  FormatInternalUnits( aPad->GetLocalSolderMaskMargin() ).c_str() );
1553  }
1554 
1555  if( aPad->GetLocalSolderPasteMargin() != 0 )
1556  {
1557  StrPrintf( &output, " (solder_paste_margin %s)",
1558  FormatInternalUnits( aPad->GetLocalSolderPasteMargin() ).c_str() );
1559  }
1560 
1561  if( aPad->GetLocalSolderPasteMarginRatio() != 0 )
1562  {
1563  StrPrintf( &output, " (solder_paste_margin_ratio %s)",
1564  Double2Str( aPad->GetLocalSolderPasteMarginRatio() ).c_str() );
1565  }
1566 
1567  if( aPad->GetLocalClearance() != 0 )
1568  {
1569  StrPrintf( &output, " (clearance %s)",
1570  FormatInternalUnits( aPad->GetLocalClearance() ).c_str() );
1571  }
1572 
1574  {
1575  StrPrintf( &output, " (zone_connect %d)",
1576  static_cast<int>( aPad->GetZoneConnection() ) );
1577  }
1578 
1579  if( aPad->GetThermalSpokeWidth() != 0 )
1580  {
1581  StrPrintf( &output, " (thermal_width %s)",
1582  FormatInternalUnits( aPad->GetThermalSpokeWidth() ).c_str() );
1583  }
1584 
1585  if( aPad->GetThermalGap() != 0 )
1586  {
1587  StrPrintf( &output, " (thermal_gap %s)",
1588  FormatInternalUnits( aPad->GetThermalGap() ).c_str() );
1589  }
1590 
1591  if( output.size() )
1592  {
1593  m_out->Print( 0, "\n" );
1594  m_out->Print( aNestLevel+1, "%s", output.c_str()+1 ); // +1 skips 1st space on 1st element
1595  }
1596 
1597  if( aPad->GetShape() == PAD_SHAPE::CUSTOM )
1598  {
1599  m_out->Print( 0, "\n");
1600  m_out->Print( aNestLevel+1, "(options" );
1601 
1603  m_out->Print( 0, " (clearance convexhull)" );
1604  #if 1 // Set to 1 to output the default option
1605  else
1606  m_out->Print( 0, " (clearance outline)" );
1607  #endif
1608 
1609  // Output the anchor pad shape (circle/rect)
1610  if( aPad->GetAnchorPadShape() == PAD_SHAPE::RECT )
1611  shape = "rect";
1612  else
1613  shape = "circle";
1614 
1615  m_out->Print( 0, " (anchor %s)", shape );
1616 
1617  m_out->Print( 0, ")"); // end of (options ...
1618 
1619  // Output graphic primitive of the pad shape
1620  m_out->Print( 0, "\n");
1621  m_out->Print( aNestLevel+1, "(primitives" );
1622 
1623  int nested_level = aNestLevel+2;
1624 
1625  // Output all basic shapes
1626  for( const std::shared_ptr<PCB_SHAPE>& primitive : aPad->GetPrimitives() )
1627  {
1628  m_out->Print( 0, "\n");
1629 
1630  switch( primitive->GetShape() )
1631  {
1632  case SHAPE_T::SEGMENT:
1633  m_out->Print( nested_level, "(gr_line (start %s) (end %s)",
1634  FormatInternalUnits( primitive->GetStart() ).c_str(),
1635  FormatInternalUnits( primitive->GetEnd() ).c_str() );
1636  break;
1637 
1638  case SHAPE_T::RECT:
1639  m_out->Print( nested_level, "(gr_rect (start %s) (end %s)",
1640  FormatInternalUnits( primitive->GetStart() ).c_str(),
1641  FormatInternalUnits( primitive->GetEnd() ).c_str() );
1642  break;
1643 
1644  case SHAPE_T::ARC:
1645  m_out->Print( aNestLevel, "(gr_arc (start %s) (mid %s) (end %s)",
1646  FormatInternalUnits( primitive->GetStart() ).c_str(),
1647  FormatInternalUnits( primitive->GetArcMid() ).c_str(),
1648  FormatInternalUnits( primitive->GetEnd() ).c_str() );
1649  break;
1650 
1651  case SHAPE_T::CIRCLE:
1652  m_out->Print( nested_level, "(gr_circle (center %s) (end %s)",
1653  FormatInternalUnits( primitive->GetStart() ).c_str(),
1654  FormatInternalUnits( primitive->GetEnd() ).c_str() );
1655  break;
1656 
1657  case SHAPE_T::BEZIER:
1658  m_out->Print( nested_level, "(gr_curve (pts (xy %s) (xy %s) (xy %s) (xy %s))",
1659  FormatInternalUnits( primitive->GetStart() ).c_str(),
1660  FormatInternalUnits( primitive->GetBezierC1() ).c_str(),
1661  FormatInternalUnits( primitive->GetBezierC2() ).c_str(),
1662  FormatInternalUnits( primitive->GetEnd() ).c_str() );
1663  break;
1664 
1665  case SHAPE_T::POLY:
1666  if( primitive->IsPolyShapeValid() )
1667  {
1668  const SHAPE_POLY_SET& poly = primitive->GetPolyShape();
1669  const SHAPE_LINE_CHAIN& outline = poly.Outline( 0 );
1670 
1671  m_out->Print( nested_level, "(gr_poly (pts" );
1672 
1673  bool need_newline = false;
1674 
1675  for( int ii = 0; ii < outline.PointCount(); ++ii )
1676  {
1677  nested_level = 0;
1678 
1679  if( !( ii % 4 ) || !ADVANCED_CFG::GetCfg().m_CompactSave )
1680  {
1681  // newline every 4 pts.
1682  m_out->Print( 0, "\n" );
1683  need_newline = false;
1684  nested_level = aNestLevel + 4;
1685  }
1686 
1687  int ind = outline.ArcIndex( ii );
1688 
1689  if( ind < 0 )
1690  {
1691  m_out->Print( nested_level, "%s(xy %s)",
1692  nested_level ? "" : " ",
1693  FormatInternalUnits( outline.CPoint( ii ) ).c_str() );
1694  need_newline = true;
1695  }
1696  else
1697  {
1698  const SHAPE_ARC& arc = outline.Arc( ind );
1699  m_out->Print( nested_level, "%s(arc (start %s) (mid %s) (end %s))",
1700  nested_level ? "" : " ",
1701  FormatInternalUnits( arc.GetP0() ).c_str(),
1702  FormatInternalUnits( arc.GetArcMid() ).c_str(),
1703  FormatInternalUnits( arc.GetP1() ).c_str() );
1704  need_newline = true;
1705 
1706  do
1707  {
1708  ++ii;
1709  } while( ii < outline.PointCount() && outline.ArcIndex( ii ) == ind );
1710 
1711  --ii;
1712  }
1713  }
1714 
1715  if( need_newline )
1716  m_out->Print( 0, "\n" );
1717 
1718  m_out->Print( aNestLevel + 3, ")" );
1719  }
1720  break;
1721 
1722  default:
1723  break;
1724  }
1725 
1726  m_out->Print( 0, " (width %s)",
1727  FormatInternalUnits( primitive->GetWidth() ).c_str() );
1728 
1729  if( primitive->IsFilled() )
1730  m_out->Print( 0, " (fill yes)" );
1731 
1732  m_out->Print( 0, ")" );
1733  }
1734 
1735  m_out->Print( 0, "\n");
1736  m_out->Print( aNestLevel+1, ")" ); // end of (basic_shapes
1737  }
1738 
1739  m_out->Print( 0, " (tstamp %s)", TO_UTF8( aPad->m_Uuid.AsString() ) );
1740 
1741  m_out->Print( 0, ")\n" );
1742 }
1743 
1744 
1745 void PCB_PLUGIN::format( const PCB_TEXT* aText, int aNestLevel ) const
1746 {
1747  m_out->Print( aNestLevel, "(gr_text %s (at %s",
1748  m_out->Quotew( aText->GetText() ).c_str(),
1749  FormatInternalUnits( aText->GetTextPos() ).c_str() );
1750 
1751  if( aText->GetTextAngle() != 0.0 )
1752  m_out->Print( 0, " %s", FormatAngle( aText->GetTextAngle() ).c_str() );
1753 
1754  m_out->Print( 0, ")" );
1755 
1756  formatLayer( aText );
1757 
1758  m_out->Print( 0, " (tstamp %s)", TO_UTF8( aText->m_Uuid.AsString() ) );
1759 
1760  m_out->Print( 0, "\n" );
1761 
1762  // PCB_TEXTS are never hidden, so always omit "hide" attribute
1763  aText->EDA_TEXT::Format( m_out, aNestLevel, m_ctl | CTL_OMIT_HIDE );
1764 
1765  m_out->Print( aNestLevel, ")\n" );
1766 }
1767 
1768 
1769 void PCB_PLUGIN::format( const PCB_GROUP* aGroup, int aNestLevel ) const
1770 {
1771  // Don't write empty groups
1772  if( aGroup->GetItems().empty() )
1773  return;
1774 
1775  m_out->Print( aNestLevel, "(group %s%s (id %s)\n",
1776  m_out->Quotew( aGroup->GetName() ).c_str(),
1777  aGroup->IsLocked() ? " locked" : "",
1778  TO_UTF8( aGroup->m_Uuid.AsString() ) );
1779 
1780  m_out->Print( aNestLevel + 1, "(members\n" );
1781 
1782  wxArrayString memberIds;
1783 
1784  for( BOARD_ITEM* member : aGroup->GetItems() )
1785  memberIds.Add( member->m_Uuid.AsString() );
1786 
1787  memberIds.Sort();
1788 
1789  for( const wxString& memberId : memberIds )
1790  m_out->Print( aNestLevel + 2, "%s\n", TO_UTF8( memberId ) );
1791 
1792  m_out->Print( aNestLevel + 1, ")\n" ); // Close `members` token.
1793  m_out->Print( aNestLevel, ")\n" ); // Close `group` token.
1794 }
1795 
1796 
1797 void PCB_PLUGIN::format( const FP_TEXT* aText, int aNestLevel ) const
1798 {
1799  std::string type;
1800 
1801  switch( aText->GetType() )
1802  {
1803  case FP_TEXT::TEXT_is_REFERENCE: type = "reference"; break;
1804  case FP_TEXT::TEXT_is_VALUE: type = "value"; break;
1805  case FP_TEXT::TEXT_is_DIVERS: type = "user";
1806  }
1807 
1808  std::string locked = aText->IsLocked() ? " locked" : "";
1809 
1810  m_out->Print( aNestLevel, "(fp_text %s%s %s (at %s",
1811  type.c_str(),
1812  locked.c_str(),
1813  m_out->Quotew( aText->GetText() ).c_str(),
1814  FormatInternalUnits( aText->GetPos0() ).c_str() );
1815 
1816  // Due to Pcbnew history, fp_text angle is saved as an absolute on screen angle,
1817  // but internally the angle is held relative to its parent footprint. parent
1818  // may be NULL when saving a footprint outside a BOARD.
1819  double orient = aText->GetTextAngle();
1820  FOOTPRINT* parent = (FOOTPRINT*) aText->GetParent();
1821 
1822  if( parent )
1823  {
1824  // GetTextAngle() is always in -360..+360 range because of
1825  // FP_TEXT::SetTextAngle(), but summing that angle with an
1826  // additional board angle could kick sum up >= 360 or <= -360, so to have
1827  // consistent results, normalize again for the BOARD save. A footprint
1828  // save does not use this code path since parent is NULL.
1829 #if 0
1830  // This one could be considered reasonable if you like positive angles
1831  // in your board text.
1832  orient = NormalizeAnglePos( orient + parent->GetOrientation() );
1833 #else
1834  // Choose compatibility for now, even though this is only a 720 degree clamp
1835  // with two possible values for every angle.
1836  orient = NormalizeAngle360Min( orient + parent->GetOrientation() );
1837 #endif
1838  }
1839 
1840  if( orient != 0.0 )
1841  m_out->Print( 0, " %s", FormatAngle( orient ).c_str() );
1842 
1843  if( !aText->IsKeepUpright() )
1844  m_out->Print( 0, " unlocked" );
1845 
1846  m_out->Print( 0, ")" );
1847  formatLayer( aText );
1848 
1849  if( !aText->IsVisible() )
1850  m_out->Print( 0, " hide" );
1851 
1852  m_out->Print( 0, "\n" );
1853 
1854  aText->EDA_TEXT::Format( m_out, aNestLevel, m_ctl | CTL_OMIT_HIDE );
1855 
1856  m_out->Print( aNestLevel + 1, "(tstamp %s)\n", TO_UTF8( aText->m_Uuid.AsString() ) );
1857 
1858  m_out->Print( aNestLevel, ")\n" );
1859 }
1860 
1861 
1862 void PCB_PLUGIN::format( const PCB_TRACK* aTrack, int aNestLevel ) const
1863 {
1864  if( aTrack->Type() == PCB_VIA_T )
1865  {
1866  PCB_LAYER_ID layer1, layer2;
1867 
1868  const PCB_VIA* via = static_cast<const PCB_VIA*>( aTrack );
1869  BOARD* board = (BOARD*) via->GetParent();
1870 
1871  wxCHECK_RET( board != nullptr, wxT( "Via " ) +
1872  via->GetSelectMenuText( EDA_UNITS::MILLIMETRES ) + wxT( " has no parent." ) );
1873 
1874  m_out->Print( aNestLevel, "(via" );
1875 
1876  via->LayerPair( &layer1, &layer2 );
1877 
1878  switch( via->GetViaType() )
1879  {
1880  case VIATYPE::THROUGH: // Default shape not saved.
1881  break;
1882 
1883  case VIATYPE::BLIND_BURIED:
1884  m_out->Print( 0, " blind" );
1885  break;
1886 
1887  case VIATYPE::MICROVIA:
1888  m_out->Print( 0, " micro" );
1889  break;
1890 
1891  default:
1892  THROW_IO_ERROR( wxString::Format( _( "unknown via type %d" ), via->GetViaType() ) );
1893  }
1894 
1895  if( via->IsLocked() )
1896  m_out->Print( 0, " locked" );
1897 
1898  m_out->Print( 0, " (at %s) (size %s)",
1899  FormatInternalUnits( aTrack->GetStart() ).c_str(),
1900  FormatInternalUnits( aTrack->GetWidth() ).c_str() );
1901 
1902  // Old boards were using UNDEFINED_DRILL_DIAMETER value in file for via drill when
1903  // via drill was the netclass value.
1904  // recent boards always set the via drill to the actual value, but now we need to
1905  // always store the drill value, because netclass value is not stored in the board file.
1906  // Otherwise the drill value of some (old) vias can be unknown
1907  if( via->GetDrill() != UNDEFINED_DRILL_DIAMETER )
1908  m_out->Print( 0, " (drill %s)", FormatInternalUnits( via->GetDrill() ).c_str() );
1909  else // Probably old board!
1910  m_out->Print( 0, " (drill %s)", FormatInternalUnits( via->GetDrillValue() ).c_str() );
1911 
1912  m_out->Print( 0, " (layers %s %s)",
1913  m_out->Quotew( LSET::Name( layer1 ) ).c_str(),
1914  m_out->Quotew( LSET::Name( layer2 ) ).c_str() );
1915 
1916  if( via->GetRemoveUnconnected() )
1917  {
1918  m_out->Print( 0, " (remove_unused_layers)" );
1919 
1920  if( via->GetKeepTopBottom() )
1921  m_out->Print( 0, " (keep_end_layers)" );
1922  }
1923 
1924  if( via->GetIsFree() )
1925  m_out->Print( 0, " (free)" );
1926  }
1927  else if( aTrack->Type() == PCB_ARC_T )
1928  {
1929  const PCB_ARC* arc = static_cast<const PCB_ARC*>( aTrack );
1930  std::string locked = arc->IsLocked() ? " locked" : "";
1931 
1932  m_out->Print( aNestLevel, "(arc%s (start %s) (mid %s) (end %s) (width %s)",
1933  locked.c_str(),
1934  FormatInternalUnits( arc->GetStart() ).c_str(),
1935  FormatInternalUnits( arc->GetMid() ).c_str(),
1936  FormatInternalUnits( arc->GetEnd() ).c_str(),
1937  FormatInternalUnits( arc->GetWidth() ).c_str() );
1938 
1939  m_out->Print( 0, " (layer %s)", m_out->Quotew( LSET::Name( arc->GetLayer() ) ).c_str() );
1940  }
1941  else
1942  {
1943  std::string locked = aTrack->IsLocked() ? " locked" : "";
1944 
1945  m_out->Print( aNestLevel, "(segment%s (start %s) (end %s) (width %s)",
1946  locked.c_str(),
1947  FormatInternalUnits( aTrack->GetStart() ).c_str(),
1948  FormatInternalUnits( aTrack->GetEnd() ).c_str(),
1949  FormatInternalUnits( aTrack->GetWidth() ).c_str() );
1950 
1951  m_out->Print( 0, " (layer %s)", m_out->Quotew( LSET::Name( aTrack->GetLayer() ) ).c_str() );
1952  }
1953 
1954  m_out->Print( 0, " (net %d)", m_mapping->Translate( aTrack->GetNetCode() ) );
1955 
1956  m_out->Print( 0, " (tstamp %s)", TO_UTF8( aTrack->m_Uuid.AsString() ) );
1957 
1958  m_out->Print( 0, ")\n" );
1959 }
1960 
1961 
1962 void PCB_PLUGIN::format( const ZONE* aZone, int aNestLevel ) const
1963 {
1964  std::string locked = aZone->IsLocked() ? " locked" : "";
1965 
1966  // Save the NET info; For keepout zones, net code and net name are irrelevant
1967  // so be sure a dummy value is stored, just for ZONE compatibility
1968  // (perhaps netcode and netname should be not stored)
1969  m_out->Print( aNestLevel, "(zone%s (net %d) (net_name %s)",
1970  locked.c_str(),
1971  aZone->GetIsRuleArea() ? 0 : m_mapping->Translate( aZone->GetNetCode() ),
1972  m_out->Quotew( aZone->GetIsRuleArea() ? wxT("") : aZone->GetNetname() ).c_str() );
1973 
1974  // If a zone exists on multiple layers, format accordingly
1975  if( aZone->GetLayerSet().count() > 1 )
1976  {
1977  formatLayers( aZone->GetLayerSet() );
1978  }
1979  else
1980  {
1981  formatLayer( aZone );
1982  }
1983 
1984  m_out->Print( 0, " (tstamp %s)", TO_UTF8( aZone->m_Uuid.AsString() ) );
1985 
1986  if( !aZone->GetZoneName().empty() )
1987  m_out->Print( 0, " (name %s)", m_out->Quotew( aZone->GetZoneName() ).c_str() );
1988 
1989  // Save the outline aux info
1990  std::string hatch;
1991 
1992  switch( aZone->GetHatchStyle() )
1993  {
1994  default:
1995  case ZONE_BORDER_DISPLAY_STYLE::NO_HATCH: hatch = "none"; break;
1996  case ZONE_BORDER_DISPLAY_STYLE::DIAGONAL_EDGE: hatch = "edge"; break;
1997  case ZONE_BORDER_DISPLAY_STYLE::DIAGONAL_FULL: hatch = "full"; break;
1998  }
1999 
2000  m_out->Print( 0, " (hatch %s %s)\n", hatch.c_str(),
2001  FormatInternalUnits( aZone->GetBorderHatchPitch() ).c_str() );
2002 
2003  if( aZone->GetPriority() > 0 )
2004  m_out->Print( aNestLevel+1, "(priority %d)\n", aZone->GetPriority() );
2005 
2006  m_out->Print( aNestLevel+1, "(connect_pads" );
2007 
2008  switch( aZone->GetPadConnection() )
2009  {
2010  default:
2011  case ZONE_CONNECTION::THERMAL: // Default option not saved or loaded.
2012  break;
2013 
2015  m_out->Print( 0, " thru_hole_only" );
2016  break;
2017 
2018  case ZONE_CONNECTION::FULL:
2019  m_out->Print( 0, " yes" );
2020  break;
2021 
2022  case ZONE_CONNECTION::NONE:
2023  m_out->Print( 0, " no" );
2024  break;
2025  }
2026 
2027  m_out->Print( 0, " (clearance %s))\n",
2028  FormatInternalUnits( aZone->GetLocalClearance() ).c_str() );
2029 
2030  m_out->Print( aNestLevel+1, "(min_thickness %s)",
2031  FormatInternalUnits( aZone->GetMinThickness() ).c_str() );
2032 
2033  // write it only if V 6.O version option is used (i.e. do not write if the "legacy"
2034  // algorithm is used)
2035  if( !aZone->GetFilledPolysUseThickness() )
2036  m_out->Print( 0, " (filled_areas_thickness no)" );
2037 
2038  m_out->Print( 0, "\n" );
2039 
2040  if( aZone->GetIsRuleArea() )
2041  {
2042  m_out->Print( aNestLevel + 1,
2043  "(keepout (tracks %s) (vias %s) (pads %s ) (copperpour %s) "
2044  "(footprints %s))\n",
2045  aZone->GetDoNotAllowTracks() ? "not_allowed" : "allowed",
2046  aZone->GetDoNotAllowVias() ? "not_allowed" : "allowed",
2047  aZone->GetDoNotAllowPads() ? "not_allowed" : "allowed",
2048  aZone->GetDoNotAllowCopperPour() ? "not_allowed" : "allowed",
2049  aZone->GetDoNotAllowFootprints() ? "not_allowed" : "allowed" );
2050  }
2051 
2052  m_out->Print( aNestLevel + 1, "(fill" );
2053 
2054  // Default is not filled.
2055  if( aZone->IsFilled() )
2056  m_out->Print( 0, " yes" );
2057 
2058  // Default is polygon filled.
2059  if( aZone->GetFillMode() == ZONE_FILL_MODE::HATCH_PATTERN )
2060  m_out->Print( 0, " (mode hatch)" );
2061 
2062  m_out->Print( 0, " (thermal_gap %s) (thermal_bridge_width %s)",
2063  FormatInternalUnits( aZone->GetThermalReliefGap() ).c_str(),
2064  FormatInternalUnits( aZone->GetThermalReliefSpokeWidth() ).c_str() );
2065 
2067  {
2068  m_out->Print( 0, " (smoothing" );
2069 
2070  switch( aZone->GetCornerSmoothingType() )
2071  {
2073  m_out->Print( 0, " chamfer" );
2074  break;
2075 
2077  m_out->Print( 0, " fillet" );
2078  break;
2079 
2080  default:
2081  THROW_IO_ERROR( wxString::Format( _( "unknown zone corner smoothing type %d" ),
2082  aZone->GetCornerSmoothingType() ) );
2083  }
2084  m_out->Print( 0, ")" );
2085 
2086  if( aZone->GetCornerRadius() != 0 )
2087  m_out->Print( 0, " (radius %s)",
2088  FormatInternalUnits( aZone->GetCornerRadius() ).c_str() );
2089  }
2090 
2092  {
2093  m_out->Print( 0, " (island_removal_mode %d) (island_area_min %s)",
2094  static_cast<int>( aZone->GetIslandRemovalMode() ),
2095  FormatInternalUnits( aZone->GetMinIslandArea() / IU_PER_MM ).c_str() );
2096  }
2097 
2098  if( aZone->GetFillMode() == ZONE_FILL_MODE::HATCH_PATTERN )
2099  {
2100  m_out->Print( 0, "\n" );
2101  m_out->Print( aNestLevel+2, "(hatch_thickness %s) (hatch_gap %s) (hatch_orientation %s)",
2102  FormatInternalUnits( aZone->GetHatchThickness() ).c_str(),
2103  FormatInternalUnits( aZone->GetHatchGap() ).c_str(),
2104  Double2Str( aZone->GetHatchOrientation() ).c_str() );
2105 
2106  if( aZone->GetHatchSmoothingLevel() > 0 )
2107  {
2108  m_out->Print( 0, "\n" );
2109  m_out->Print( aNestLevel+2, "(hatch_smoothing_level %d) (hatch_smoothing_value %s)",
2110  aZone->GetHatchSmoothingLevel(),
2111  Double2Str( aZone->GetHatchSmoothingValue() ).c_str() );
2112  }
2113 
2114  m_out->Print( 0, "\n" );
2115  m_out->Print( aNestLevel+2, "(hatch_border_algorithm %s) (hatch_min_hole_area %s)",
2116  aZone->GetHatchBorderAlgorithm() ? "hatch_thickness" : "min_thickness",
2117  Double2Str( aZone->GetHatchHoleMinArea() ).c_str() );
2118  }
2119 
2120  m_out->Print( 0, ")\n" );
2121 
2122  if( aZone->GetNumCorners() )
2123  {
2124  SHAPE_POLY_SET::POLYGON poly = aZone->Outline()->Polygon(0);
2125 
2126  for( auto& chain : poly )
2127  {
2128  m_out->Print( aNestLevel + 1, "(polygon\n" );
2129  m_out->Print( aNestLevel + 2, "(pts" );
2130 
2131  bool need_newline = true;
2132 
2133  for( int ii = 0; ii < chain.PointCount(); ++ii )
2134  {
2135  int nestLevel = 0;
2136 
2137  if( !( ii % 4 ) || !ADVANCED_CFG::GetCfg().m_CompactSave ) // newline every 4 pts
2138  {
2139  m_out->Print( 0, "\n" );
2140  need_newline = false;
2141  nestLevel = aNestLevel + 3;
2142  }
2143 
2144  int ind = chain.ArcIndex( ii );
2145 
2146  if( ind < 0 )
2147  {
2148  m_out->Print( nestLevel, "%s(xy %s)",
2149  nestLevel ? "" : " ",
2150  FormatInternalUnits( chain.CPoint( ii ) ).c_str() );
2151  need_newline = true;
2152  }
2153  else
2154  {
2155  auto& arc = chain.Arc( ind );
2156  m_out->Print( nestLevel, "%s(arc (start %s) (mid %s) (end %s))",
2157  nestLevel ? "" : " ",
2158  FormatInternalUnits( arc.GetP0() ).c_str(),
2159  FormatInternalUnits( arc.GetArcMid() ).c_str(),
2160  FormatInternalUnits( arc.GetP1() ).c_str() );
2161  need_newline = true;
2162 
2163  do
2164  {
2165  ++ii;
2166  } while( ii < chain.PointCount() && chain.ArcIndex( ii ) == ind );
2167 
2168  --ii;
2169  }
2170  }
2171 
2172  if( need_newline )
2173  m_out->Print( 0, "\n" );
2174 
2175  m_out->Print( aNestLevel + 2, ")\n" );
2176  m_out->Print( aNestLevel + 1, ")\n" );
2177 
2178  }
2179  }
2180 
2181  // Save the PolysList (filled areas)
2182  for( PCB_LAYER_ID layer : aZone->GetLayerSet().Seq() )
2183  {
2184  const SHAPE_POLY_SET& fv = aZone->GetFilledPolysList( layer );
2185 
2186  for( int ii = 0; ii < fv.OutlineCount(); ++ii )
2187  {
2188  m_out->Print( aNestLevel + 1, "(filled_polygon\n" );
2189  m_out->Print( aNestLevel + 2, "(layer %s)\n",
2190  m_out->Quotew( LSET::Name( layer ) ).c_str() );
2191 
2192  if( aZone->IsIsland( layer, ii ) )
2193  m_out->Print( aNestLevel + 2, "(island)\n" );
2194 
2195  m_out->Print( aNestLevel + 2, "(pts" );
2196 
2197  const SHAPE_LINE_CHAIN& chain = fv.COutline( ii );
2198 
2199  bool need_newline = true;
2200 
2201  for( int jj = 0; jj < chain.PointCount(); ++jj )
2202  {
2203  int nestLevel = 0;
2204 
2205  if( !( jj%4 ) || !ADVANCED_CFG::GetCfg().m_CompactSave ) // newline every 4 pts
2206  {
2207  m_out->Print( 0, "\n" );
2208  need_newline = false;
2209  nestLevel = aNestLevel + 3;
2210  }
2211 
2212  int ind = chain.ArcIndex( jj );
2213 
2214  if( ind < 0 )
2215  {
2216  m_out->Print( nestLevel, "%s(xy %s)",
2217  nestLevel ? "" : " ",
2218  FormatInternalUnits( chain.CPoint( jj ) ).c_str() );
2219  need_newline = true;
2220  }
2221  else
2222  {
2223  auto& arc = chain.Arc( ind );
2224  m_out->Print( nestLevel, "%s(arc (start %s) (mid %s) (end %s))",
2225  nestLevel ? "" : " ",
2226  FormatInternalUnits( arc.GetP0() ).c_str(),
2227  FormatInternalUnits( arc.GetArcMid() ).c_str(),
2228  FormatInternalUnits( arc.GetP1() ).c_str() );
2229  need_newline = true;
2230 
2231  do
2232  {
2233  ++jj;
2234  } while( jj < chain.PointCount() && chain.ArcIndex( jj ) == ind );
2235 
2236  --jj;
2237  }
2238  }
2239 
2240  if( need_newline )
2241  m_out->Print( 0, "\n" );
2242 
2243  m_out->Print( aNestLevel+2, ")\n" );
2244  m_out->Print( aNestLevel+1, ")\n" );
2245  }
2246 
2247  // Save the filling segments list
2248  const std::vector<SEG>& segs = aZone->FillSegments( layer );
2249 
2250  if( segs.size() )
2251  {
2252  m_out->Print( aNestLevel + 1, "(fill_segments\n" );
2253  m_out->Print( aNestLevel + 2, "(layer %s)\n",
2254  TO_UTF8( BOARD::GetStandardLayerName( layer ) ) );
2255 
2256  for( const SEG& seg : segs )
2257  {
2258  m_out->Print( aNestLevel + 2, "(pts (xy %s) (xy %s))\n",
2259  FormatInternalUnits( wxPoint( seg.A ) ).c_str(),
2260  FormatInternalUnits( wxPoint( seg.B ) ).c_str() );
2261  }
2262 
2263  m_out->Print( aNestLevel + 1, ")\n" );
2264  }
2265  }
2266 
2267  m_out->Print( aNestLevel, ")\n" );
2268 }
2269 
2270 
2271 PCB_PLUGIN::PCB_PLUGIN( int aControlFlags ) :
2272  m_cache( nullptr ),
2273  m_ctl( aControlFlags ),
2274  m_parser( new PCB_PARSER() ),
2275  m_mapping( new NETINFO_MAPPING() )
2276 {
2277  init( nullptr );
2278  m_out = &m_sf;
2279 }
2280 
2281 
2283 {
2284  delete m_cache;
2285  delete m_parser;
2286  delete m_mapping;
2287 }
2288 
2289 
2290 BOARD* PCB_PLUGIN::Load( const wxString& aFileName, BOARD* aAppendToMe, const PROPERTIES* aProperties,
2291  PROJECT* aProject, PROGRESS_REPORTER* aProgressReporter )
2292 {
2293  FILE_LINE_READER reader( aFileName );
2294 
2295  unsigned lineCount = 0;
2296 
2297  if( aProgressReporter )
2298  {
2299  aProgressReporter->Report( wxString::Format( _( "Loading %s..." ), aFileName ) );
2300 
2301  if( !aProgressReporter->KeepRefreshing() )
2302  THROW_IO_ERROR( _( "Open cancelled by user." ) );
2303 
2304  while( reader.ReadLine() )
2305  lineCount++;
2306 
2307  reader.Rewind();
2308  }
2309 
2310  BOARD* board = DoLoad( reader, aAppendToMe, aProperties, aProgressReporter, lineCount );
2311 
2312  // Give the filename to the board if it's new
2313  if( !aAppendToMe )
2314  board->SetFileName( aFileName );
2315 
2316  return board;
2317 }
2318 
2319 
2320 BOARD* PCB_PLUGIN::DoLoad( LINE_READER& aReader, BOARD* aAppendToMe, const PROPERTIES* aProperties,
2321  PROGRESS_REPORTER* aProgressReporter, unsigned aLineCount)
2322 {
2323  init( aProperties );
2324 
2325  m_parser->SetLineReader( &aReader );
2326  m_parser->SetBoard( aAppendToMe );
2327  m_parser->SetProgressReporter( aProgressReporter, &aReader, aLineCount );
2328 
2329  BOARD* board;
2330 
2331  try
2332  {
2333  board = dynamic_cast<BOARD*>( m_parser->Parse() );
2334  }
2335  catch( const FUTURE_FORMAT_ERROR& )
2336  {
2337  // Don't wrap a FUTURE_FORMAT_ERROR in another
2338  throw;
2339  }
2340  catch( const PARSE_ERROR& parse_error )
2341  {
2342  if( m_parser->IsTooRecent() )
2343  throw FUTURE_FORMAT_ERROR( parse_error, m_parser->GetRequiredVersion() );
2344  else
2345  throw;
2346  }
2347 
2348  if( !board )
2349  {
2350  // The parser loaded something that was valid, but wasn't a board.
2351  THROW_PARSE_ERROR( _( "This file does not contain a PCB." ), m_parser->CurSource(),
2352  m_parser->CurLine(), m_parser->CurLineNumber(), m_parser->CurOffset() );
2353  }
2354 
2355  return board;
2356 }
2357 
2358 
2359 void PCB_PLUGIN::init( const PROPERTIES* aProperties )
2360 {
2361  m_board = nullptr;
2362  m_reader = nullptr;
2363  m_props = aProperties;
2364 }
2365 
2366 
2367 void PCB_PLUGIN::validateCache( const wxString& aLibraryPath, bool checkModified )
2368 {
2369  if( !m_cache || !m_cache->IsPath( aLibraryPath ) || ( checkModified && m_cache->IsModified() ) )
2370  {
2371  // a spectacular episode in memory management:
2372  delete m_cache;
2373  m_cache = new FP_CACHE( this, aLibraryPath );
2374  m_cache->Load();
2375  }
2376 }
2377 
2378 
2379 void PCB_PLUGIN::FootprintEnumerate( wxArrayString& aFootprintNames, const wxString& aLibPath,
2380  bool aBestEfforts, const PROPERTIES* aProperties )
2381 {
2382  LOCALE_IO toggle; // toggles on, then off, the C locale.
2383  wxDir dir( aLibPath );
2384  wxString errorMsg;
2385 
2386  init( aProperties );
2387 
2388  try
2389  {
2390  validateCache( aLibPath );
2391  }
2392  catch( const IO_ERROR& ioe )
2393  {
2394  errorMsg = ioe.What();
2395  }
2396 
2397  // Some of the files may have been parsed correctly so we want to add the valid files to
2398  // the library.
2399 
2400  for( const auto& footprint : m_cache->GetFootprints() )
2401  aFootprintNames.Add( footprint.first );
2402 
2403  if( !errorMsg.IsEmpty() && !aBestEfforts )
2404  THROW_IO_ERROR( errorMsg );
2405 }
2406 
2407 
2408 const FOOTPRINT* PCB_PLUGIN::getFootprint( const wxString& aLibraryPath,
2409  const wxString& aFootprintName,
2410  const PROPERTIES* aProperties,
2411  bool checkModified )
2412 {
2413  LOCALE_IO toggle; // toggles on, then off, the C locale.
2414 
2415  init( aProperties );
2416 
2417  try
2418  {
2419  validateCache( aLibraryPath, checkModified );
2420  }
2421  catch( const IO_ERROR& )
2422  {
2423  // do nothing with the error
2424  }
2425 
2426  FOOTPRINT_MAP& footprints = m_cache->GetFootprints();
2427  FOOTPRINT_MAP::const_iterator it = footprints.find( aFootprintName );
2428 
2429  if( it == footprints.end() )
2430  return nullptr;
2431 
2432  return it->second->GetFootprint();
2433 }
2434 
2435 
2436 const FOOTPRINT* PCB_PLUGIN::GetEnumeratedFootprint( const wxString& aLibraryPath,
2437  const wxString& aFootprintName,
2438  const PROPERTIES* aProperties )
2439 {
2440  return getFootprint( aLibraryPath, aFootprintName, aProperties, false );
2441 }
2442 
2443 
2444 bool PCB_PLUGIN::FootprintExists( const wxString& aLibraryPath, const wxString& aFootprintName,
2445  const PROPERTIES* aProperties )
2446 {
2447  // Note: checking the cache sounds like a good idea, but won't catch files which differ
2448  // only in case.
2449  //
2450  // Since this goes out to the native filesystem, we get platform differences (ie: MSW's
2451  // case-insensitive filesystem) handled "for free".
2452  // Warning: footprint names frequently contain a point. So be careful when initializing
2453  // wxFileName, and use a CTOR with extension specified
2454  wxFileName footprintFile( aLibraryPath, aFootprintName, KiCadFootprintFileExtension );
2455 
2456  return footprintFile.Exists();
2457 }
2458 
2459 
2460 FOOTPRINT* PCB_PLUGIN::FootprintLoad( const wxString& aLibraryPath,
2461  const wxString& aFootprintName,
2462  bool aKeepUUID,
2463  const PROPERTIES* aProperties )
2464 {
2465  const FOOTPRINT* footprint = getFootprint( aLibraryPath, aFootprintName, aProperties, true );
2466 
2467  if( footprint )
2468  {
2469  FOOTPRINT* copy;
2470 
2471  if( aKeepUUID )
2472  copy = static_cast<FOOTPRINT*>( footprint->Clone() );
2473  else
2474  copy = static_cast<FOOTPRINT*>( footprint->Duplicate() );
2475 
2476  copy->SetParent( nullptr );
2477  return copy;
2478  }
2479 
2480  return nullptr;
2481 }
2482 
2483 
2484 void PCB_PLUGIN::FootprintSave( const wxString& aLibraryPath, const FOOTPRINT* aFootprint,
2485  const PROPERTIES* aProperties )
2486 {
2487  LOCALE_IO toggle; // toggles on, then off, the C locale.
2488 
2489  init( aProperties );
2490 
2491  // In this public PLUGIN API function, we can safely assume it was
2492  // called for saving into a library path.
2494 
2495  validateCache( aLibraryPath );
2496 
2497  if( !m_cache->IsWritable() )
2498  {
2499  if( !m_cache->Exists() )
2500  {
2501  const wxString msg = wxString::Format( _( "Library '%s' does not exist.\n"
2502  "Would you like to create it?"),
2503  aLibraryPath );
2504 
2505  if( wxMessageBox( msg, _( "Library Not Found"), wxYES_NO | wxICON_QUESTION ) != wxYES )
2506  return;
2507 
2508  // Save throws its own IO_ERROR on failure, so no need to recreate here
2509  m_cache->Save( nullptr );
2510  }
2511  else
2512  {
2513  wxString msg = wxString::Format( _( "Library '%s' is read only." ), aLibraryPath );
2514  THROW_IO_ERROR( msg );
2515  }
2516  }
2517 
2518  wxString footprintName = aFootprint->GetFPID().GetLibItemName();
2519 
2520  FOOTPRINT_MAP& footprints = m_cache->GetFootprints();
2521 
2522  // Quietly overwrite footprint and delete footprint file from path for any by same name.
2523  wxFileName fn( aLibraryPath, aFootprint->GetFPID().GetLibItemName(),
2525 
2526  // Write through symlinks, don't replace them
2528 
2529  if( !fn.IsOk() )
2530  {
2531  THROW_IO_ERROR( wxString::Format( _( "Footprint file name '%s' is not valid." ),
2532  fn.GetFullPath() ) );
2533  }
2534 
2535  if( fn.FileExists() && !fn.IsFileWritable() )
2536  {
2537  THROW_IO_ERROR( wxString::Format( _( "Insufficient permissions to delete '%s'." ),
2538  fn.GetFullPath() ) );
2539  }
2540 
2541  wxString fullPath = fn.GetFullPath();
2542  wxString fullName = fn.GetFullName();
2543  FOOTPRINT_MAP::const_iterator it = footprints.find( footprintName );
2544 
2545  if( it != footprints.end() )
2546  {
2547  wxLogTrace( traceKicadPcbPlugin, wxT( "Removing footprint file '%s'." ), fullPath );
2548  footprints.erase( footprintName );
2549  wxRemoveFile( fullPath );
2550  }
2551 
2552  // I need my own copy for the cache
2553  FOOTPRINT* footprint = static_cast<FOOTPRINT*>( aFootprint->Clone() );
2554 
2555  // It's orientation should be zero and it should be on the front layer.
2556  footprint->SetOrientation( 0 );
2557 
2558  if( footprint->GetLayer() != F_Cu )
2559  {
2560  PCBNEW_SETTINGS* cfg = dynamic_cast<PCBNEW_SETTINGS*>( Kiface().KifaceSettings() );
2561 
2562  if( cfg )
2563  footprint->Flip( footprint->GetPosition(), cfg->m_FlipLeftRight );
2564  else
2565  footprint->Flip( footprint->GetPosition(), false );
2566  }
2567 
2568  // Detach it from the board
2569  footprint->SetParent( nullptr );
2570 
2571  wxLogTrace( traceKicadPcbPlugin, wxT( "Creating s-expr footprint file '%s'." ), fullPath );
2572  footprints.insert( footprintName,
2573  new FP_CACHE_ITEM( footprint, WX_FILENAME( fn.GetPath(), fullName ) ) );
2574  m_cache->Save( footprint );
2575 }
2576 
2577 
2578 void PCB_PLUGIN::FootprintDelete( const wxString& aLibraryPath, const wxString& aFootprintName,
2579  const PROPERTIES* aProperties )
2580 {
2581  LOCALE_IO toggle; // toggles on, then off, the C locale.
2582 
2583  init( aProperties );
2584 
2585  validateCache( aLibraryPath );
2586 
2587  if( !m_cache->IsWritable() )
2588  {
2589  THROW_IO_ERROR( wxString::Format( _( "Library '%s' is read only." ),
2590  aLibraryPath.GetData() ) );
2591  }
2592 
2593  m_cache->Remove( aFootprintName );
2594 }
2595 
2596 
2597 
2598 long long PCB_PLUGIN::GetLibraryTimestamp( const wxString& aLibraryPath ) const
2599 {
2600  return FP_CACHE::GetTimestamp( aLibraryPath );
2601 }
2602 
2603 
2604 void PCB_PLUGIN::FootprintLibCreate( const wxString& aLibraryPath, const PROPERTIES* aProperties )
2605 {
2606  if( wxDir::Exists( aLibraryPath ) )
2607  {
2608  THROW_IO_ERROR( wxString::Format( _( "Cannot overwrite library path '%s'." ),
2609  aLibraryPath.GetData() ) );
2610  }
2611 
2612  LOCALE_IO toggle;
2613 
2614  init( aProperties );
2615 
2616  delete m_cache;
2617  m_cache = new FP_CACHE( this, aLibraryPath );
2618  m_cache->Save();
2619 }
2620 
2621 
2622 bool PCB_PLUGIN::FootprintLibDelete( const wxString& aLibraryPath, const PROPERTIES* aProperties )
2623 {
2624  wxFileName fn;
2625  fn.SetPath( aLibraryPath );
2626 
2627  // Return if there is no library path to delete.
2628  if( !fn.DirExists() )
2629  return false;
2630 
2631  if( !fn.IsDirWritable() )
2632  {
2633  THROW_IO_ERROR( wxString::Format( _( "Insufficient permissions to delete folder '%s'." ),
2634  aLibraryPath.GetData() ) );
2635  }
2636 
2637  wxDir dir( aLibraryPath );
2638 
2639  if( dir.HasSubDirs() )
2640  {
2641  THROW_IO_ERROR( wxString::Format( _( "Library folder '%s' has unexpected sub-folders." ),
2642  aLibraryPath.GetData() ) );
2643  }
2644 
2645  // All the footprint files must be deleted before the directory can be deleted.
2646  if( dir.HasFiles() )
2647  {
2648  unsigned i;
2649  wxFileName tmp;
2650  wxArrayString files;
2651 
2652  wxDir::GetAllFiles( aLibraryPath, &files );
2653 
2654  for( i = 0; i < files.GetCount(); i++ )
2655  {
2656  tmp = files[i];
2657 
2658  if( tmp.GetExt() != KiCadFootprintFileExtension )
2659  {
2660  THROW_IO_ERROR( wxString::Format( _( "Unexpected file '%s' found in library "
2661  "path '%s'." ),
2662  files[i].GetData(),
2663  aLibraryPath.GetData() ) );
2664  }
2665  }
2666 
2667  for( i = 0; i < files.GetCount(); i++ )
2668  wxRemoveFile( files[i] );
2669  }
2670 
2671  wxLogTrace( traceKicadPcbPlugin, wxT( "Removing footprint library '%s'." ),
2672  aLibraryPath.GetData() );
2673 
2674  // Some of the more elaborate wxRemoveFile() crap puts up its own wxLog dialog
2675  // we don't want that. we want bare metal portability with no UI here.
2676  if( !wxRmdir( aLibraryPath ) )
2677  {
2678  THROW_IO_ERROR( wxString::Format( _( "Footprint library '%s' cannot be deleted." ),
2679  aLibraryPath.GetData() ) );
2680  }
2681 
2682  // For some reason removing a directory in Windows is not immediately updated. This delay
2683  // prevents an error when attempting to immediately recreate the same directory when over
2684  // writing an existing library.
2685 #ifdef __WINDOWS__
2686  wxMilliSleep( 250L );
2687 #endif
2688 
2689  if( m_cache && !m_cache->IsPath( aLibraryPath ) )
2690  {
2691  delete m_cache;
2692  m_cache = nullptr;
2693  }
2694 
2695  return true;
2696 }
2697 
2698 
2699 bool PCB_PLUGIN::IsFootprintLibWritable( const wxString& aLibraryPath )
2700 {
2701  LOCALE_IO toggle;
2702 
2703  init( nullptr );
2704 
2705  validateCache( aLibraryPath );
2706 
2707  return m_cache->IsWritable();
2708 }
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:525
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:297
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:201
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:98
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:740
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:524
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:207
#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:735
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:581
const wxPoint & GetStart() const
Return the starting point of the graphic.
Definition: eda_shape.h:106
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:869
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:737
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:289
wxString m_lib_raw_path
Definition: pcb_plugin.cpp:95
double GetHatchSmoothingValue() const
Definition: zone.h:265
bool IsLocked() const override
Definition: pad.cpp:150
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:236
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:637
#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:1210
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:500
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:564
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:346
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:502
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:90
FP_TEXT & Reference()
Definition: footprint.h:503
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:154
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:477
#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:739
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:515
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:131
wxString GetRequiredVersion()
Return a string representing the version of KiCad required to open this file.
Definition: pcb_parser.cpp:247
bool GetDoNotAllowCopperPour() const
Definition: zone.h:736
int GetExtensionHeight() const
LSET is a set of PCB_LAYER_IDs.
Definition: layer_ids.h:516
#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:492
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:492
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:1681
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:1491
Mark the center of a circle or arc with a cross shape.
Smd pad, used in BGA footprints.
bool GetDoNotAllowTracks() const
Definition: zone.h:738
static void ResolvePossibleSymlinks(wxFileName &aFilename)
Definition: wx_filename.cpp:85
const wxPoint & GetBezierC2() const
Definition: eda_shape.h:157
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:213
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:1725
DRAWINGS & GraphicalItems()
Definition: footprint.h:172
bool IsPlaced() const
Definition: footprint.h:315
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:615
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:627
Helper class for creating a footprint library cache.
Definition: pcb_plugin.cpp:69
const wxString & GetKeywords() const
Definition: footprint.h:204
int GetThermalWidth() const
Definition: footprint.h:234
int GetLocalClearance(wxString *aSource) const override
Return any local clearances set in the "classic" (ie: pre-rule) system.
Definition: pad.cpp:735
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:477
SHAPE_POLY_SET & GetPolyShape()
Definition: eda_shape.h:227
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:691
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:689
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:751
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:210
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:239
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:533
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:1075
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:316
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:227
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:509
int GetChamferPositions() const
Definition: pad.h:543
const wxPoint & GetTextPos() const
Definition: eda_text.h:268
wxString GroupsSanityCheck(bool repair=false)
Consistency check of internal m_groups structure.
Definition: board.cpp:1978
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:231
int GetPlacementCost90() const
Definition: footprint.h:584
virtual const wxPoint & GetEnd() const
Pcbnew s-expression file format parser definition.
long long int GetMinIslandArea() const
Definition: zone.h:754
int GetThermalReliefSpokeWidth() const
Definition: zone.h:202
SHAPE_T GetShape() const
Definition: eda_shape.h:101
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:224
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:753
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:1293
BOARD_ITEM_CONTAINER * GetParent() const
Definition: board_item.h:136
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:143
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:558
const wxPoint & GetMid() const
Definition: pcb_track.h:272
int GetThermalGap() const
Definition: footprint.h:237
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:685
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:480
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:691
Container for design settings for a BOARD object.