KiCad PCB EDA Suite
Loading...
Searching...
No Matches
pcb_io_kicad_sexpr.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-2024 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// base64 code. Needed for PCB_REFERENCE_IMAGE
26#define wxUSE_BASE64 1
27#include <wx/base64.h>
28#include <wx/dir.h>
29#include <wx/ffile.h>
30#include <wx/log.h>
31#include <wx/msgdlg.h>
32#include <wx/mstream.h>
33
34#include <advanced_config.h>
35#include <board.h>
37#include <callback_gal.h>
38#include <confirm.h>
39#include <convert_basic_shapes_to_polygon.h> // for enum RECT_CHAMFER_POSITIONS definition
40#include <fmt/core.h>
41#include <font/fontconfig.h>
42#include <footprint.h>
44#include <kiface_base.h>
45#include <locale_io.h>
46#include <macros.h>
47#include <pad.h>
48#include <pcb_dimension.h>
49#include <pcb_generator.h>
50#include <pcb_group.h>
53#include <pcb_reference_image.h>
54#include <pcb_shape.h>
55#include <pcb_table.h>
56#include <pcb_tablecell.h>
57#include <pcb_target.h>
58#include <pcb_text.h>
59#include <pcb_textbox.h>
60#include <pcb_track.h>
61#include <pcbnew_settings.h>
62#include <pgm_base.h>
63#include <progress_reporter.h>
64#include <reporter.h>
65#include <string_utils.h>
66#include <trace_helpers.h>
68#include <zone.h>
69
70#include <build_version.h>
71#include <filter_reader.h>
72#include <ctl_flags.h>
73
74
75using namespace PCB_KEYS_T;
76
77
78FP_CACHE_ITEM::FP_CACHE_ITEM( FOOTPRINT* aFootprint, const WX_FILENAME& aFileName ) :
79 m_filename( aFileName ),
80 m_footprint( aFootprint )
81{ }
82
83
84FP_CACHE::FP_CACHE( PCB_IO_KICAD_SEXPR* aOwner, const wxString& aLibraryPath )
85{
86 m_owner = aOwner;
87 m_lib_raw_path = aLibraryPath;
88 m_lib_path.SetPath( aLibraryPath );
90 m_cache_dirty = true;
91}
92
93
94void FP_CACHE::Save( FOOTPRINT* aFootprint )
95{
97
98 if( !m_lib_path.DirExists() && !m_lib_path.Mkdir() )
99 {
100 THROW_IO_ERROR( wxString::Format( _( "Cannot create footprint library '%s'." ),
101 m_lib_raw_path ) );
102 }
103
104 if( !m_lib_path.IsDirWritable() )
105 {
106 THROW_IO_ERROR( wxString::Format( _( "Footprint library '%s' is read only." ),
107 m_lib_raw_path ) );
108 }
109
110 for( FP_CACHE_FOOTPRINT_MAP::iterator it = m_footprints.begin(); it != m_footprints.end(); ++it )
111 {
112 if( aFootprint && aFootprint != it->second->GetFootprint() )
113 continue;
114
115 // If we've requested to embed the fonts in the footprint, do so.
116 // Otherwise, clear the embedded fonts from the footprint. Embedded
117 // fonts will be used if available
118 if( aFootprint->GetAreFontsEmbedded() )
119 aFootprint->EmbedFonts();
120 else
121 aFootprint->GetEmbeddedFiles()->ClearEmbeddedFonts();
122
123 WX_FILENAME fn = it->second->GetFileName();
124
125 wxString tempFileName =
126#ifdef USE_TMP_FILE
127 wxFileName::CreateTempFileName( fn.GetPath() );
128#else
129 fn.GetFullPath();
130#endif
131 // Allow file output stream to go out of scope to close the file stream before
132 // renaming the file.
133 {
134 wxLogTrace( traceKicadPcbPlugin, wxT( "Creating temporary library file '%s'." ),
135 tempFileName );
136
137 PRETTIFIED_FILE_OUTPUTFORMATTER formatter( tempFileName );
138
139 m_owner->SetOutputFormatter( &formatter );
140 m_owner->Format( (BOARD_ITEM*) it->second->GetFootprint() );
141 }
142
143#ifdef USE_TMP_FILE
144 wxRemove( fn.GetFullPath() ); // it is not an error if this does not exist
145
146 // Even on Linux you can see an _intermittent_ error when calling wxRename(),
147 // and it is fully inexplicable. See if this dodges the error.
148 wxMilliSleep( 250L );
149
150 // Preserve the permissions of the current file
152
153 if( !wxRenameFile( tempFileName, fn.GetFullPath() ) )
154 {
155 wxString msg = wxString::Format( _( "Cannot rename temporary file '%s' to '%s'" ),
156 tempFileName,
157 fn.GetFullPath() );
158 THROW_IO_ERROR( msg );
159 }
160#endif
162 }
163
164 m_cache_timestamp += m_lib_path.GetModificationTime().GetValue().GetValue();
165
166 // If we've saved the full cache, we clear the dirty flag.
167 if( !aFootprint )
168 m_cache_dirty = false;
169}
170
171
173{
174 m_cache_dirty = false;
176
177 wxDir dir( m_lib_raw_path );
178
179 if( !dir.IsOpened() )
180 {
181 wxString msg = wxString::Format( _( "Footprint library '%s' not found." ),
183 THROW_IO_ERROR( msg );
184 }
185
186 wxString fullName;
187 wxString fileSpec = wxT( "*." ) + wxString( FILEEXT::KiCadFootprintFileExtension );
188
189 // wxFileName construction is egregiously slow. Construct it once and just swap out
190 // the filename thereafter.
191 WX_FILENAME fn( m_lib_raw_path, wxT( "dummyName" ) );
192
193 if( dir.GetFirst( &fullName, fileSpec ) )
194 {
195 wxString cacheError;
196
197 do
198 {
199 fn.SetFullName( fullName );
200
201 // Queue I/O errors so only files that fail to parse don't get loaded.
202 try
203 {
204 FILE_LINE_READER reader( fn.GetFullPath() );
205 PCB_IO_KICAD_SEXPR_PARSER parser( &reader, nullptr, nullptr );
206
207 FOOTPRINT* footprint = dynamic_cast<FOOTPRINT*>( parser.Parse() );
208 wxString fpName = fn.GetName();
209
210 if( !footprint )
211 THROW_IO_ERROR( wxEmptyString ); // caught locally, just below...
212
213 footprint->SetFPID( LIB_ID( wxEmptyString, fpName ) );
214 m_footprints.insert( fpName, new FP_CACHE_ITEM( footprint, fn ) );
215 }
216 catch( const IO_ERROR& ioe )
217 {
218 if( !cacheError.IsEmpty() )
219 cacheError += wxT( "\n\n" );
220
221 cacheError += wxString::Format( _( "Unable to read file '%s'" ) + '\n',
222 fn.GetFullPath() );
223 cacheError += ioe.What();
224 }
225 } while( dir.GetNext( &fullName ) );
226
228
229 if( !cacheError.IsEmpty() )
230 THROW_IO_ERROR( cacheError );
231 }
232}
233
234
235void FP_CACHE::Remove( const wxString& aFootprintName )
236{
237 FP_CACHE_FOOTPRINT_MAP::const_iterator it = m_footprints.find( aFootprintName );
238
239 if( it == m_footprints.end() )
240 {
241 wxString msg = wxString::Format( _( "Library '%s' has no footprint '%s'." ),
243 aFootprintName );
244 THROW_IO_ERROR( msg );
245 }
246
247 // Remove the footprint from the cache and delete the footprint file from the library.
248 wxString fullPath = it->second->GetFileName().GetFullPath();
249 m_footprints.erase( aFootprintName );
250 wxRemoveFile( fullPath );
251}
252
253
254bool FP_CACHE::IsPath( const wxString& aPath ) const
255{
256 return aPath == m_lib_raw_path;
257}
258
259
260void FP_CACHE::SetPath( const wxString& aPath )
261{
262 m_lib_raw_path = aPath;
263 m_lib_path.SetPath( aPath );
264
265
266 for( const auto& footprint : GetFootprints() )
267 {
268 footprint.second->SetFilePath( aPath );
269 }
270}
271
272
274{
276
277 return m_cache_dirty;
278}
279
280
281long long FP_CACHE::GetTimestamp( const wxString& aLibPath )
282{
283 wxString fileSpec = wxT( "*." ) + wxString( FILEEXT::KiCadFootprintFileExtension );
284
285 return TimestampDir( aLibPath, fileSpec );
286}
287
288
289bool PCB_IO_KICAD_SEXPR::CanReadBoard( const wxString& aFileName ) const
290{
291 if( !PCB_IO::CanReadBoard( aFileName ) )
292 return false;
293
294 try
295 {
296 FILE_LINE_READER reader( aFileName );
297 PCB_IO_KICAD_SEXPR_PARSER parser( &reader, nullptr, m_queryUserCallback );
298
299 return parser.IsValidBoardHeader();
300 }
301 catch( const IO_ERROR& )
302 {
303 }
304
305 return false;
306}
307
308
309void PCB_IO_KICAD_SEXPR::SaveBoard( const wxString& aFileName, BOARD* aBoard,
310 const STRING_UTF8_MAP* aProperties )
311{
312 LOCALE_IO toggle; // toggles on, then off, the C locale.
313
314 wxString sanityResult = aBoard->GroupsSanityCheck();
315
316 if( sanityResult != wxEmptyString && m_queryUserCallback )
317 {
319 _( "Internal Group Data Error" ), wxICON_ERROR,
320 wxString::Format( _( "Please report this bug. Error validating group "
321 "structure: %s\n\nSave anyway?" ), sanityResult ),
322 _( "Save Anyway" ) ) )
323 {
324 return;
325 }
326 }
327
328 init( aProperties );
329
330 m_board = aBoard; // after init()
331
332 // If the user wants fonts embedded, make sure that they are added to the board. Otherwise,
333 // remove any fonts that were previously embedded.
336 else
338
339 // Prepare net mapping that assures that net codes saved in a file are consecutive integers
340 m_mapping->SetBoard( aBoard );
341
342 PRETTIFIED_FILE_OUTPUTFORMATTER formatter( aFileName );
343
344 m_out = &formatter; // no ownership
345
346 m_out->Print( 0, "(kicad_pcb (version %d) (generator \"pcbnew\") (generator_version \"%s\")\n",
348
349 Format( aBoard, 1 );
350
351 m_out->Print( 0, ")\n" );
352 m_out->Finish();
353
354 m_out = nullptr;
355}
356
357
358BOARD_ITEM* PCB_IO_KICAD_SEXPR::Parse( const wxString& aClipboardSourceInput )
359{
360 std::string input = TO_UTF8( aClipboardSourceInput );
361
362 STRING_LINE_READER reader( input, wxT( "clipboard" ) );
363 PCB_IO_KICAD_SEXPR_PARSER parser( &reader, nullptr, m_queryUserCallback );
364
365 try
366 {
367 return parser.Parse();
368 }
369 catch( const PARSE_ERROR& parse_error )
370 {
371 if( parser.IsTooRecent() )
372 throw FUTURE_FORMAT_ERROR( parse_error, parser.GetRequiredVersion() );
373 else
374 throw;
375 }
376}
377
378
379void PCB_IO_KICAD_SEXPR::Format( const BOARD_ITEM* aItem, int aNestLevel ) const
380{
381 LOCALE_IO toggle; // public API function, perform anything convenient for caller
382
383 switch( aItem->Type() )
384 {
385 case PCB_T:
386 format( static_cast<const BOARD*>( aItem ), aNestLevel );
387 break;
388
390 case PCB_DIM_CENTER_T:
391 case PCB_DIM_RADIAL_T:
393 case PCB_DIM_LEADER_T:
394 format( static_cast<const PCB_DIMENSION_BASE*>( aItem ), aNestLevel );
395 break;
396
397 case PCB_SHAPE_T:
398 format( static_cast<const PCB_SHAPE*>( aItem ), aNestLevel );
399 break;
400
402 format( static_cast<const PCB_REFERENCE_IMAGE*>( aItem ), aNestLevel );
403 break;
404
405 case PCB_TARGET_T:
406 format( static_cast<const PCB_TARGET*>( aItem ), aNestLevel );
407 break;
408
409 case PCB_FOOTPRINT_T:
410 format( static_cast<const FOOTPRINT*>( aItem ), aNestLevel );
411 break;
412
413 case PCB_PAD_T:
414 format( static_cast<const PAD*>( aItem ), aNestLevel );
415 break;
416
417 case PCB_FIELD_T:
418 // Handled in the footprint formatter when properties are formatted
419 break;
420
421 case PCB_TEXT_T:
422 format( static_cast<const PCB_TEXT*>( aItem ), aNestLevel );
423 break;
424
425 case PCB_TEXTBOX_T:
426 format( static_cast<const PCB_TEXTBOX*>( aItem ), aNestLevel );
427 break;
428
429 case PCB_TABLE_T:
430 format( static_cast<const PCB_TABLE*>( aItem ), aNestLevel );
431 break;
432
433 case PCB_GROUP_T:
434 format( static_cast<const PCB_GROUP*>( aItem ), aNestLevel );
435 break;
436
437 case PCB_GENERATOR_T:
438 format( static_cast<const PCB_GENERATOR*>( aItem ), aNestLevel );
439 break;
440
441 case PCB_TRACE_T:
442 case PCB_ARC_T:
443 case PCB_VIA_T:
444 format( static_cast<const PCB_TRACK*>( aItem ), aNestLevel );
445 break;
446
447 case PCB_ZONE_T:
448 format( static_cast<const ZONE*>( aItem ), aNestLevel );
449 break;
450
451 default:
452 wxFAIL_MSG( wxT( "Cannot format item " ) + aItem->GetClass() );
453 }
454}
455
456
457std::string formatInternalUnits( int aValue )
458{
460}
461
462
463std::string formatInternalUnits( const VECTOR2I& aCoord )
464{
466}
467
468
469std::string formatInternalUnits( const VECTOR2I& aCoord, const FOOTPRINT* aParentFP )
470{
471 if( aParentFP )
472 {
473 VECTOR2I coord = aCoord - aParentFP->GetPosition();
474 RotatePoint( coord, -aParentFP->GetOrientation() );
475 return formatInternalUnits( coord );
476 }
477
478 return formatInternalUnits( aCoord );
479}
480
481
482void PCB_IO_KICAD_SEXPR::formatLayer( PCB_LAYER_ID aLayer, bool aIsKnockout ) const
483{
484 m_out->Print( 0, " (layer %s%s)",
485 m_out->Quotew( LSET::Name( aLayer ) ).c_str(),
486 aIsKnockout ? " knockout" : "" );
487}
488
489
490void PCB_IO_KICAD_SEXPR::formatPolyPts( const SHAPE_LINE_CHAIN& outline, int aNestLevel,
491 bool aCompact, const FOOTPRINT* aParentFP ) const
492{
493 m_out->Print( aNestLevel + 1, "(pts\n" );
494
495 bool needNewline = false;
496 int nestLevel = aNestLevel + 2;
497 int shapesAdded = 0;
498
499 for( int ii = 0; ii < outline.PointCount(); ++ii )
500 {
501 int ind = outline.ArcIndex( ii );
502
503 if( ind < 0 )
504 {
505 m_out->Print( nestLevel, "(xy %s)",
506 formatInternalUnits( outline.CPoint( ii ), aParentFP ).c_str() );
507 needNewline = true;
508 }
509 else
510 {
511 const SHAPE_ARC& arc = outline.Arc( ind );
512 m_out->Print( nestLevel, "(arc (start %s) (mid %s) (end %s))",
513 formatInternalUnits( arc.GetP0(), aParentFP ).c_str(),
514 formatInternalUnits( arc.GetArcMid(), aParentFP ).c_str(),
515 formatInternalUnits( arc.GetP1(), aParentFP ).c_str() );
516 needNewline = true;
517
518 do
519 {
520 ++ii;
521 } while( ii < outline.PointCount() && outline.ArcIndex( ii ) == ind );
522
523 --ii;
524 }
525
526 ++shapesAdded;
527
528 if( !( shapesAdded % 4 ) || !aCompact )
529 {
530 // newline every 4 shapes if compact save
531 m_out->Print( 0, "\n" );
532 needNewline = false;
533 }
534 }
535
536 if( needNewline )
537 m_out->Print( 0, "\n" );
538
539 m_out->Print( aNestLevel + 1, ")\n" );
540}
541
542
543void PCB_IO_KICAD_SEXPR::formatRenderCache( const EDA_TEXT* aText, int aNestLevel ) const
544{
545 wxString resolvedText( aText->GetShownText( true ) );
546 std::vector<std::unique_ptr<KIFONT::GLYPH>>* cache = aText->GetRenderCache( aText->GetFont(),
547 resolvedText );
548
549 m_out->Print( aNestLevel, "(render_cache %s %s\n",
550 m_out->Quotew( resolvedText ).c_str(),
551 EDA_UNIT_UTILS::FormatAngle( aText->GetDrawRotation() ).c_str() );
552
554
555 CALLBACK_GAL callback_gal( empty_opts,
556 // Polygon callback
557 [&]( const SHAPE_LINE_CHAIN& aPoly )
558 {
559 m_out->Print( aNestLevel + 1, "(polygon\n" );
560 formatPolyPts( aPoly, aNestLevel + 1, true );
561 m_out->Print( aNestLevel + 1, ")\n" );
562 } );
563
564 callback_gal.SetLineWidth( aText->GetTextThickness() );
565 callback_gal.DrawGlyphs( *cache );
566
567 m_out->Print( aNestLevel, ")\n" );
568}
569
570
571void PCB_IO_KICAD_SEXPR::formatSetup( const BOARD* aBoard, int aNestLevel ) const
572{
573 // Setup
574 m_out->Print( aNestLevel, "(setup\n" );
575
576 // Save the board physical stackup structure
577 const BOARD_STACKUP& stackup = aBoard->GetDesignSettings().GetStackupDescriptor();
578
579 if( aBoard->GetDesignSettings().m_HasStackup )
580 stackup.FormatBoardStackup( m_out, aBoard, aNestLevel+1 );
581
582 BOARD_DESIGN_SETTINGS& dsnSettings = aBoard->GetDesignSettings();
583
584 m_out->Print( aNestLevel+1, "(pad_to_mask_clearance %s)\n",
585 formatInternalUnits( dsnSettings.m_SolderMaskExpansion ).c_str() );
586
587 if( dsnSettings.m_SolderMaskMinWidth )
588 {
589 m_out->Print( aNestLevel+1, "(solder_mask_min_width %s)\n",
590 formatInternalUnits( dsnSettings.m_SolderMaskMinWidth ).c_str() );
591 }
592
593 if( dsnSettings.m_SolderPasteMargin != 0 )
594 {
595 m_out->Print( aNestLevel+1, "(pad_to_paste_clearance %s)\n",
596 formatInternalUnits( dsnSettings.m_SolderPasteMargin ).c_str() );
597 }
598
599 if( dsnSettings.m_SolderPasteMarginRatio != 0 )
600 {
601 m_out->Print( aNestLevel+1, "(pad_to_paste_clearance_ratio %s)\n",
602 FormatDouble2Str( dsnSettings.m_SolderPasteMarginRatio ).c_str() );
603 }
604
605 KICAD_FORMAT::FormatBool( m_out, aNestLevel + 1, "allow_soldermask_bridges_in_footprints",
606 dsnSettings.m_AllowSoldermaskBridgesInFPs );
607
608 m_out->Print( 0, " (tenting " );
609
610 if( dsnSettings.m_TentViasFront || dsnSettings.m_TentViasBack )
611 {
612 if( dsnSettings.m_TentViasFront )
613 m_out->Print( 0, "front " );
614 if( dsnSettings.m_TentViasBack )
615 m_out->Print( 0, "back " );
616
617 m_out->Print( 0, ")" );
618 }
619 else
620 {
621 m_out->Print( 0, " none)" );
622 }
623
624 VECTOR2I origin = dsnSettings.GetAuxOrigin();
625
626 if( origin != VECTOR2I( 0, 0 ) )
627 {
628 m_out->Print( aNestLevel+1, "(aux_axis_origin %s %s)\n",
629 formatInternalUnits( origin.x ).c_str(),
630 formatInternalUnits( origin.y ).c_str() );
631 }
632
633 origin = dsnSettings.GetGridOrigin();
634
635 if( origin != VECTOR2I( 0, 0 ) )
636 {
637 m_out->Print( aNestLevel+1, "(grid_origin %s %s)\n",
638 formatInternalUnits( origin.x ).c_str(),
639 formatInternalUnits( origin.y ).c_str() );
640 }
641
642 aBoard->GetPlotOptions().Format( m_out, aNestLevel+1 );
643
644 m_out->Print( aNestLevel, ")\n\n" );
645}
646
647
648void PCB_IO_KICAD_SEXPR::formatGeneral( const BOARD* aBoard, int aNestLevel ) const
649{
650 const BOARD_DESIGN_SETTINGS& dsnSettings = aBoard->GetDesignSettings();
651
652 m_out->Print( 0, "\n" );
653 m_out->Print( aNestLevel, "(general\n" );
654
655 m_out->Print( aNestLevel+1, "(thickness %s)\n",
656 formatInternalUnits( dsnSettings.GetBoardThickness() ).c_str() );
657
658 KICAD_FORMAT::FormatBool( m_out, aNestLevel + 1, "legacy_teardrops",
659 aBoard->LegacyTeardrops() );
660
661 m_out->Print( aNestLevel, ")\n\n" );
662
663 aBoard->GetPageSettings().Format( m_out, aNestLevel, m_ctl );
664 aBoard->GetTitleBlock().Format( m_out, aNestLevel, m_ctl );
665}
666
667
668void PCB_IO_KICAD_SEXPR::formatBoardLayers( const BOARD* aBoard, int aNestLevel ) const
669{
670 m_out->Print( aNestLevel, "(layers\n" );
671
672 // Save only the used copper layers from front to back.
673
674 for( PCB_LAYER_ID layer : aBoard->GetEnabledLayers().CuStack() )
675 {
676
677 m_out->Print( aNestLevel+1, "(%d %s %s",
678 layer,
679 m_out->Quotew( LSET::Name( layer ) ).c_str(),
680 LAYER::ShowType( aBoard->GetLayerType( layer ) ) );
681
682 if( LSET::Name( layer ) != m_board->GetLayerName( layer ) )
683 m_out->Print( 0, " %s", m_out->Quotew( m_board->GetLayerName( layer ) ).c_str() );
684
685 m_out->Print( 0, ")\n" );
686 }
687
688 // Save used non-copper layers in the order they are defined.
689 // desired sequence for non Cu BOARD layers.
690 static const PCB_LAYER_ID non_cu[] =
691 {
692 B_Adhes, // 32
693 F_Adhes,
694 B_Paste,
695 F_Paste,
696 B_SilkS,
697 F_SilkS,
698 B_Mask,
699 F_Mask,
700 Dwgs_User,
701 Cmts_User,
702 Eco1_User,
703 Eco2_User,
704 Edge_Cuts,
705 Margin,
706 B_CrtYd,
707 F_CrtYd,
708 B_Fab,
709 F_Fab,
710 User_1,
711 User_2,
712 User_3,
713 User_4,
714 User_5,
715 User_6,
716 User_7,
717 User_8,
718 User_9
719 };
720
721 for( PCB_LAYER_ID layer : aBoard->GetEnabledLayers().Seq( non_cu, arrayDim( non_cu ) ) )
722 {
723 m_out->Print( aNestLevel+1, "(%d %s",
724 layer,
725 m_out->Quotew( LSET::Name( layer ) ).c_str() );
726
727 if( layer >= User_1 && layer <= User_9 )
728 m_out->Print( 0, " %s", LAYER::ShowType( aBoard->GetLayerType( layer ) ) );
729 else
730 m_out->Print( 0, " user" );
731
732 if( m_board->GetLayerName( layer ) != LSET::Name( layer ) )
733 m_out->Print( 0, " %s", m_out->Quotew( m_board->GetLayerName( layer ) ).c_str() );
734
735 m_out->Print( 0, ")\n" );
736 }
737
738 m_out->Print( aNestLevel, ")\n\n" );
739}
740
741
742void PCB_IO_KICAD_SEXPR::formatNetInformation( const BOARD* aBoard, int aNestLevel ) const
743{
744 for( NETINFO_ITEM* net : *m_mapping )
745 {
746 if( net == nullptr ) // Skip not actually existing nets (orphan nets)
747 continue;
748
749 m_out->Print( aNestLevel, "(net %d %s)\n",
750 m_mapping->Translate( net->GetNetCode() ),
751 m_out->Quotew( net->GetNetname() ).c_str() );
752 }
753
754 m_out->Print( 0, "\n" );
755}
756
757
758void PCB_IO_KICAD_SEXPR::formatProperties( const BOARD* aBoard, int aNestLevel ) const
759{
760 for( const std::pair<const wxString, wxString>& prop : aBoard->GetProperties() )
761 {
762 m_out->Print( aNestLevel, "(property %s %s)\n",
763 m_out->Quotew( prop.first ).c_str(),
764 m_out->Quotew( prop.second ).c_str() );
765 }
766
767 if( !aBoard->GetProperties().empty() )
768 m_out->Print( 0, "\n" );
769}
770
771
772void PCB_IO_KICAD_SEXPR::formatHeader( const BOARD* aBoard, int aNestLevel ) const
773{
774 formatGeneral( aBoard, aNestLevel );
775
776 // Layers list.
777 formatBoardLayers( aBoard, aNestLevel );
778
779 // Setup
780 formatSetup( aBoard, aNestLevel );
781
782 // Properties
783 formatProperties( aBoard, aNestLevel );
784
785 // Save net codes and names
786 formatNetInformation( aBoard, aNestLevel );
787}
788
789
791{
792 static const TEARDROP_PARAMETERS defaults;
793
794 return tdParams.m_Enabled == defaults.m_Enabled
795 && tdParams.m_BestLengthRatio == defaults.m_BestLengthRatio
796 && tdParams.m_TdMaxLen == defaults.m_TdMaxLen
797 && tdParams.m_BestWidthRatio == defaults.m_BestWidthRatio
798 && tdParams.m_TdMaxWidth == defaults.m_TdMaxWidth
799 && tdParams.m_CurveSegCount == defaults.m_CurveSegCount
801 && tdParams.m_AllowUseTwoTracks == defaults.m_AllowUseTwoTracks
802 && tdParams.m_TdOnPadsInZones == defaults.m_TdOnPadsInZones;
803}
804
805
807 int aNestLevel ) const
808{
809 m_out->Print( aNestLevel, "(teardrops (best_length_ratio %s) (max_length %s) "
810 "(best_width_ratio %s) (max_width %s) (curve_points %d) "
811 "(filter_ratio %s)",
812 FormatDouble2Str( tdParams.m_BestLengthRatio ).c_str(),
813 formatInternalUnits( tdParams.m_TdMaxLen ).c_str(),
814 FormatDouble2Str( tdParams.m_BestWidthRatio ).c_str(),
815 formatInternalUnits( tdParams.m_TdMaxWidth ).c_str(),
816 tdParams.m_CurveSegCount,
817 FormatDouble2Str( tdParams.m_WidthtoSizeFilterRatio ).c_str() );
818
819 KICAD_FORMAT::FormatBool( m_out, aNestLevel, "enabled", tdParams.m_Enabled );
820 KICAD_FORMAT::FormatBool( m_out, aNestLevel, "allow_two_segments",
821 tdParams.m_AllowUseTwoTracks );
822 KICAD_FORMAT::FormatBool( m_out, aNestLevel, "prefer_zone_connections",
823 !tdParams.m_TdOnPadsInZones );
824 m_out->Print( aNestLevel, ")" );
825}
826
827
828void PCB_IO_KICAD_SEXPR::format( const BOARD* aBoard, int aNestLevel ) const
829{
830 std::set<BOARD_ITEM*, BOARD_ITEM::ptr_cmp> sorted_footprints( aBoard->Footprints().begin(),
831 aBoard->Footprints().end() );
832 std::set<BOARD_ITEM*, BOARD_ITEM::ptr_cmp> sorted_drawings( aBoard->Drawings().begin(),
833 aBoard->Drawings().end() );
834 std::set<PCB_TRACK*, PCB_TRACK::cmp_tracks> sorted_tracks( aBoard->Tracks().begin(),
835 aBoard->Tracks().end() );
836 std::set<BOARD_ITEM*, BOARD_ITEM::ptr_cmp> sorted_zones( aBoard->Zones().begin(),
837 aBoard->Zones().end() );
838 std::set<BOARD_ITEM*, BOARD_ITEM::ptr_cmp> sorted_groups( aBoard->Groups().begin(),
839 aBoard->Groups().end() );
840 std::set<BOARD_ITEM*, BOARD_ITEM::ptr_cmp> sorted_generators( aBoard->Generators().begin(),
841 aBoard->Generators().end() );
842 formatHeader( aBoard, aNestLevel );
843
844 // Save the footprints.
845 for( BOARD_ITEM* footprint : sorted_footprints )
846 {
847 Format( footprint, aNestLevel );
848 m_out->Print( 0, "\n" );
849 }
850
851 // Save the graphical items on the board (not owned by a footprint)
852 for( BOARD_ITEM* item : sorted_drawings )
853 Format( item, aNestLevel );
854
855 if( sorted_drawings.size() )
856 m_out->Print( 0, "\n" );
857
858 // Do not save PCB_MARKERs, they can be regenerated easily.
859
860 // Save the tracks and vias.
861 for( PCB_TRACK* track : sorted_tracks )
862 Format( track, aNestLevel );
863
864 if( sorted_tracks.size() )
865 m_out->Print( 0, "\n" );
866
867 // Save the polygon (which are the newer technology) zones.
868 for( auto zone : sorted_zones )
869 Format( zone, aNestLevel );
870
871 // Save the groups
872 for( BOARD_ITEM* group : sorted_groups )
873 Format( group, aNestLevel );
874
875 // Save the generators
876 for( BOARD_ITEM* gen : sorted_generators )
877 Format( gen, aNestLevel );
878
879 // Save any embedded files
880 // Consolidate the embedded models in footprints into a single map
881 // to avoid duplicating the same model in the board file.
882 EMBEDDED_FILES files_to_write;
883
884 for( auto& file : aBoard->GetEmbeddedFiles()->EmbeddedFileMap() )
885 files_to_write.AddFile( file.second );
886
887 for( BOARD_ITEM* item : sorted_footprints )
888 {
889 FOOTPRINT* fp = static_cast<FOOTPRINT*>( item );
890
891 for( auto& file : fp->GetEmbeddedFiles()->EmbeddedFileMap() )
892 files_to_write.AddFile( file.second );
893 }
894
895 m_out->Print( aNestLevel + 1, "(embedded_fonts %s)\n",
896 aBoard->GetEmbeddedFiles()->GetAreFontsEmbedded() ? "yes" : "no" );
897
898 if( !files_to_write.IsEmpty() )
899 files_to_write.WriteEmbeddedFiles( *m_out, aNestLevel + 1, ( m_ctl & CTL_FOR_BOARD ) );
900
901 // Remove the files so that they are not freed in the DTOR
902 files_to_write.ClearEmbeddedFiles( false );
903}
904
905
906void PCB_IO_KICAD_SEXPR::format( const PCB_DIMENSION_BASE* aDimension, int aNestLevel ) const
907{
908 const PCB_DIM_ALIGNED* aligned = dynamic_cast<const PCB_DIM_ALIGNED*>( aDimension );
909 const PCB_DIM_ORTHOGONAL* ortho = dynamic_cast<const PCB_DIM_ORTHOGONAL*>( aDimension );
910 const PCB_DIM_CENTER* center = dynamic_cast<const PCB_DIM_CENTER*>( aDimension );
911 const PCB_DIM_RADIAL* radial = dynamic_cast<const PCB_DIM_RADIAL*>( aDimension );
912 const PCB_DIM_LEADER* leader = dynamic_cast<const PCB_DIM_LEADER*>( aDimension );
913
914 m_out->Print( aNestLevel, "(dimension" );
915
916 if( ortho ) // must be tested before aligned, because ortho is derived from aligned
917 // and aligned is not null
918 m_out->Print( 0, " (type orthogonal)" );
919 else if( aligned )
920 m_out->Print( 0, " (type aligned)" );
921 else if( leader )
922 m_out->Print( 0, " (type leader)" );
923 else if( center )
924 m_out->Print( 0, " (type center)" );
925 else if( radial )
926 m_out->Print( 0, " (type radial)" );
927 else
928 wxFAIL_MSG( wxT( "Cannot format unknown dimension type!" ) );
929
930 if( aDimension->IsLocked() )
931 KICAD_FORMAT::FormatBool( m_out, aNestLevel + 1, "locked", aDimension->IsLocked() );
932
933 formatLayer( aDimension->GetLayer() );
934
935 KICAD_FORMAT::FormatUuid( m_out, aDimension->m_Uuid );
936
937 m_out->Print( aNestLevel+1, "(pts (xy %s %s) (xy %s %s))\n",
938 formatInternalUnits( aDimension->GetStart().x ).c_str(),
939 formatInternalUnits( aDimension->GetStart().y ).c_str(),
940 formatInternalUnits( aDimension->GetEnd().x ).c_str(),
941 formatInternalUnits( aDimension->GetEnd().y ).c_str() );
942
943 if( aligned )
944 {
945 m_out->Print( aNestLevel+1, "(height %s)\n",
946 formatInternalUnits( aligned->GetHeight() ).c_str() );
947 }
948
949 if( radial )
950 {
951 m_out->Print( aNestLevel+1, "(leader_length %s)\n",
952 formatInternalUnits( radial->GetLeaderLength() ).c_str() );
953 }
954
955 if( ortho )
956 {
957 m_out->Print( aNestLevel+1, "(orientation %d)\n",
958 static_cast<int>( ortho->GetOrientation() ) );
959 }
960
961 if( !center )
962 {
963 format( static_cast<const PCB_TEXT*>( aDimension ), aNestLevel + 1 );
964
965 m_out->Print( aNestLevel + 1, "(format (prefix %s) (suffix %s) (units %d) (units_format %d) (precision %d)",
966 m_out->Quotew( aDimension->GetPrefix() ).c_str(),
967 m_out->Quotew( aDimension->GetSuffix() ).c_str(),
968 static_cast<int>( aDimension->GetUnitsMode() ),
969 static_cast<int>( aDimension->GetUnitsFormat() ),
970 static_cast<int>( aDimension->GetPrecision() ) );
971
972 if( aDimension->GetOverrideTextEnabled() )
973 {
974 m_out->Print( 0, " (override_value %s)",
975 m_out->Quotew( aDimension->GetOverrideText() ).c_str() );
976 }
977
978 if( aDimension->GetSuppressZeroes() )
979 m_out->Print( 0, " suppress_zeroes" );
980
981 m_out->Print( 0, ")\n" );
982 }
983
984 m_out->Print( aNestLevel+1, "(style (thickness %s) (arrow_length %s) (text_position_mode %d)",
985 formatInternalUnits( aDimension->GetLineThickness() ).c_str(),
986 formatInternalUnits( aDimension->GetArrowLength() ).c_str(),
987 static_cast<int>( aDimension->GetTextPositionMode() ) );
988
989 if( aligned )
990 {
991 m_out->Print( 0, " (extension_height %s)",
992 formatInternalUnits( aligned->GetExtensionHeight() ).c_str() );
993 }
994
995 if( leader )
996 m_out->Print( 0, " (text_frame %d)", static_cast<int>( leader->GetTextBorder() ) );
997
998 m_out->Print( 0, " (extension_offset %s)",
999 formatInternalUnits( aDimension->GetExtensionOffset() ).c_str() );
1000
1001 if( aDimension->GetKeepTextAligned() )
1002 m_out->Print( 0, " keep_text_aligned" );
1003
1004 m_out->Print( 0, ")\n" );
1005
1006 m_out->Print( aNestLevel, ")\n" );
1007}
1008
1009
1010void PCB_IO_KICAD_SEXPR::format( const PCB_SHAPE* aShape, int aNestLevel ) const
1011{
1012 FOOTPRINT* parentFP = aShape->GetParentFootprint();
1013 std::string prefix = parentFP ? "fp" : "gr";
1014
1015 switch( aShape->GetShape() )
1016 {
1017 case SHAPE_T::SEGMENT:
1018 m_out->Print( aNestLevel, "(%s_line (start %s) (end %s)\n",
1019 prefix.c_str(),
1020 formatInternalUnits( aShape->GetStart(), parentFP ).c_str(),
1021 formatInternalUnits( aShape->GetEnd(), parentFP ).c_str() );
1022 break;
1023
1024 case SHAPE_T::RECTANGLE:
1025 m_out->Print( aNestLevel, "(%s_rect (start %s) (end %s)\n",
1026 prefix.c_str(),
1027 formatInternalUnits( aShape->GetStart(), parentFP ).c_str(),
1028 formatInternalUnits( aShape->GetEnd(), parentFP ).c_str() );
1029 break;
1030
1031 case SHAPE_T::CIRCLE:
1032 m_out->Print( aNestLevel, "(%s_circle (center %s) (end %s)\n",
1033 prefix.c_str(),
1034 formatInternalUnits( aShape->GetStart(), parentFP ).c_str(),
1035 formatInternalUnits( aShape->GetEnd(), parentFP ).c_str() );
1036 break;
1037
1038 case SHAPE_T::ARC:
1039 m_out->Print( aNestLevel, "(%s_arc (start %s) (mid %s) (end %s)\n",
1040 prefix.c_str(),
1041 formatInternalUnits( aShape->GetStart(), parentFP ).c_str(),
1042 formatInternalUnits( aShape->GetArcMid(), parentFP ).c_str(),
1043 formatInternalUnits( aShape->GetEnd(), parentFP ).c_str() );
1044 break;
1045
1046 case SHAPE_T::POLY:
1047 if( aShape->IsPolyShapeValid() )
1048 {
1049 const SHAPE_POLY_SET& poly = aShape->GetPolyShape();
1050 const SHAPE_LINE_CHAIN& outline = poly.Outline( 0 );
1051
1052 m_out->Print( aNestLevel, "(%s_poly\n", prefix.c_str() );
1053 formatPolyPts( outline, aNestLevel, ADVANCED_CFG::GetCfg().m_CompactSave, parentFP );
1054 }
1055 else
1056 {
1057 wxFAIL_MSG( wxT( "Cannot format invalid polygon." ) );
1058 return;
1059 }
1060
1061 break;
1062
1063 case SHAPE_T::BEZIER:
1064 m_out->Print( aNestLevel, "(%s_curve (pts (xy %s) (xy %s) (xy %s) (xy %s))\n",
1065 prefix.c_str(),
1066 formatInternalUnits( aShape->GetStart(), parentFP ).c_str(),
1067 formatInternalUnits( aShape->GetBezierC1(), parentFP ).c_str(),
1068 formatInternalUnits( aShape->GetBezierC2(), parentFP ).c_str(),
1069 formatInternalUnits( aShape->GetEnd(), parentFP ).c_str() );
1070 break;
1071
1072 default:
1074 return;
1075 };
1076
1077 if( aShape->IsLocked() )
1078 KICAD_FORMAT::FormatBool( m_out, aNestLevel + 1, "locked", aShape->IsLocked() );
1079
1080 aShape->GetStroke().Format( m_out, pcbIUScale, aNestLevel + 1 );
1081
1082 // The filled flag represents if a solid fill is present on circles, rectangles and polygons
1083 if( ( aShape->GetShape() == SHAPE_T::POLY )
1084 || ( aShape->GetShape() == SHAPE_T::RECTANGLE )
1085 || ( aShape->GetShape() == SHAPE_T::CIRCLE ) )
1086 {
1087 m_out->Print( 0, aShape->IsFilled() ? " (fill solid)" : " (fill none)" );
1088 }
1089
1090 formatLayer( aShape->GetLayer() );
1091
1092 if( aShape->GetNetCode() > 0 )
1093 m_out->Print( 0, " (net %d)", m_mapping->Translate( aShape->GetNetCode() ) );
1094
1095 KICAD_FORMAT::FormatUuid( m_out, aShape->m_Uuid, 0 );
1096
1097 m_out->Print( 0, ")\n" );
1098}
1099
1100
1101void PCB_IO_KICAD_SEXPR::format( const PCB_REFERENCE_IMAGE* aBitmap, int aNestLevel ) const
1102{
1103 wxCHECK_RET( aBitmap != nullptr && m_out != nullptr, "" );
1104
1105 const wxImage* image = aBitmap->GetImage()->GetImageData();
1106
1107 wxCHECK_RET( image != nullptr, "wxImage* is NULL" );
1108
1109 m_out->Print( aNestLevel, "(image (at %s %s)",
1110 formatInternalUnits( aBitmap->GetPosition().x ).c_str(),
1111 formatInternalUnits( aBitmap->GetPosition().y ).c_str() );
1112
1113 formatLayer( aBitmap->GetLayer() );
1114
1115 if( aBitmap->GetImage()->GetScale() != 1.0 )
1116 m_out->Print( 0, "(scale %g)", aBitmap->GetImage()->GetScale() );
1117
1118 if( const bool locked = aBitmap->IsLocked() )
1119 KICAD_FORMAT::FormatBool( m_out, 0, "locked", locked );
1120
1121 m_out->Print( aNestLevel + 1, "(data" );
1122
1123 wxString out = wxBase64Encode( aBitmap->GetImage()->GetImageDataBuffer() );
1124
1125 // Apparently the MIME standard character width for base64 encoding is 76 (unconfirmed)
1126 // so use it in a vain attempt to be standard like.
1127#define MIME_BASE64_LENGTH 76
1128
1129 size_t first = 0;
1130
1131 while( first < out.Length() )
1132 {
1133 m_out->Print( 0, "\n" );
1134 m_out->Print( aNestLevel + 2, "\"%s\"", TO_UTF8( out( first, MIME_BASE64_LENGTH ) ) );
1135 first += MIME_BASE64_LENGTH;
1136 }
1137
1138 m_out->Print( 0, "\n" );
1139 m_out->Print( aNestLevel + 1, ")\n" ); // Closes data token.
1140
1141 KICAD_FORMAT::FormatUuid( m_out, aBitmap->m_Uuid, 0 );
1142
1143 m_out->Print( aNestLevel, ")\n" ); // Closes image token.
1144}
1145
1146
1147void PCB_IO_KICAD_SEXPR::format( const PCB_TARGET* aTarget, int aNestLevel ) const
1148{
1149 m_out->Print( aNestLevel, "(target %s (at %s) (size %s)",
1150 ( aTarget->GetShape() ) ? "x" : "plus",
1151 formatInternalUnits( aTarget->GetPosition() ).c_str(),
1152 formatInternalUnits( aTarget->GetSize() ).c_str() );
1153
1154 if( aTarget->GetWidth() != 0 )
1155 m_out->Print( 0, " (width %s)", formatInternalUnits( aTarget->GetWidth() ).c_str() );
1156
1157 formatLayer( aTarget->GetLayer() );
1158
1159 KICAD_FORMAT::FormatUuid( m_out, aTarget->m_Uuid, 0 );
1160
1161 m_out->Print( 0, ")\n" );
1162}
1163
1164
1165void PCB_IO_KICAD_SEXPR::format( const FOOTPRINT* aFootprint, int aNestLevel ) const
1166{
1167 if( !( m_ctl & CTL_OMIT_INITIAL_COMMENTS ) )
1168 {
1169 const wxArrayString* initial_comments = aFootprint->GetInitialComments();
1170
1171 if( initial_comments )
1172 {
1173 for( unsigned i = 0; i < initial_comments->GetCount(); ++i )
1174 m_out->Print( aNestLevel, "%s\n", TO_UTF8( (*initial_comments)[i] ) );
1175
1176 m_out->Print( 0, "\n" ); // improve readability?
1177 }
1178 }
1179
1180 if( m_ctl & CTL_OMIT_LIBNAME )
1181 {
1182 m_out->Print( aNestLevel, "(footprint %s",
1183 m_out->Quotes( aFootprint->GetFPID().GetLibItemName() ).c_str() );
1184 }
1185 else
1186 {
1187 m_out->Print( aNestLevel, "(footprint %s",
1188 m_out->Quotes( aFootprint->GetFPID().Format() ).c_str() );
1189 }
1190
1192 m_out->Print( 0, " (version %d) (generator \"pcbnew\") (generator_version \"%s\")\n ",
1193 SEXPR_BOARD_FILE_VERSION, GetMajorMinorVersion().c_str().AsChar() );
1194
1195 if( const bool locked = aFootprint->IsLocked() )
1196 {
1197 KICAD_FORMAT::FormatBool( m_out, 0, "locked", locked );
1198 }
1199
1200 if( const bool placed = aFootprint->IsPlaced() )
1201 {
1202 KICAD_FORMAT::FormatBool( m_out, 0, "placed", placed );
1203 }
1204
1205 formatLayer( aFootprint->GetLayer() );
1206
1207 m_out->Print( 0, "\n" );
1208
1209 if( !( m_ctl & CTL_OMIT_UUIDS ) )
1210 KICAD_FORMAT::FormatUuid( m_out, aFootprint->m_Uuid );
1211
1212 if( !( m_ctl & CTL_OMIT_AT ) )
1213 {
1214 m_out->Print( aNestLevel+1, "(at %s", formatInternalUnits( aFootprint->GetPosition() ).c_str() );
1215
1216 if( !aFootprint->GetOrientation().IsZero() )
1217 m_out->Print( 0, " %s", EDA_UNIT_UTILS::FormatAngle( aFootprint->GetOrientation() ).c_str() );
1218
1219 m_out->Print( 0, ")\n" );
1220 }
1221
1222 if( !aFootprint->GetLibDescription().IsEmpty() )
1223 {
1224 m_out->Print( aNestLevel + 1, "(descr %s)\n",
1225 m_out->Quotew( aFootprint->GetLibDescription() ).c_str() );
1226 }
1227
1228 if( !aFootprint->GetKeywords().IsEmpty() )
1229 {
1230 m_out->Print( aNestLevel+1, "(tags %s)\n",
1231 m_out->Quotew( aFootprint->GetKeywords() ).c_str() );
1232 }
1233
1234 for( const PCB_FIELD* field : aFootprint->GetFields() )
1235 {
1236 m_out->Print( aNestLevel + 1, "(property %s %s",
1237 m_out->Quotew( field->GetCanonicalName() ).c_str(),
1238 m_out->Quotew( field->GetText() ).c_str() );
1239
1240 format( field, aNestLevel + 1 );
1241
1242 m_out->Print( aNestLevel + 1, ")\n" );
1243 }
1244
1245 if( !aFootprint->GetFilters().empty() )
1246 {
1247 m_out->Print( aNestLevel + 1, "(property ki_fp_filters %s)\n",
1248 m_out->Quotew( aFootprint->GetFilters() ).c_str() );
1249 }
1250
1251 if( !( m_ctl & CTL_OMIT_PATH ) && !aFootprint->GetPath().empty() )
1252 {
1253 m_out->Print( aNestLevel+1, "(path %s)\n",
1254 m_out->Quotew( aFootprint->GetPath().AsString() ).c_str() );
1255 }
1256
1257 if( !aFootprint->GetSheetname().empty() )
1258 {
1259 m_out->Print( aNestLevel + 1, "(sheetname %s)\n",
1260 m_out->Quotew( aFootprint->GetSheetname() ).c_str() );
1261 }
1262
1263 if( !aFootprint->GetSheetfile().empty() )
1264 {
1265 m_out->Print( aNestLevel + 1, "(sheetfile %s)\n",
1266 m_out->Quotew( aFootprint->GetSheetfile() ).c_str() );
1267 }
1268
1269 if( aFootprint->GetLocalSolderMaskMargin().has_value() )
1270 {
1271 m_out->Print( aNestLevel+1, "(solder_mask_margin %s)\n",
1272 formatInternalUnits( aFootprint->GetLocalSolderMaskMargin().value() ).c_str() );
1273 }
1274
1275 if( aFootprint->GetLocalSolderPasteMargin().has_value() )
1276 {
1277 m_out->Print( aNestLevel+1, "(solder_paste_margin %s)\n",
1278 formatInternalUnits( aFootprint->GetLocalSolderPasteMargin().value() ).c_str() );
1279 }
1280
1281 if( aFootprint->GetLocalSolderPasteMarginRatio().has_value() )
1282 {
1283 m_out->Print( aNestLevel+1, "(solder_paste_margin_ratio %s)\n",
1284 FormatDouble2Str( aFootprint->GetLocalSolderPasteMarginRatio().value() ).c_str() );
1285 }
1286
1287 if( aFootprint->GetLocalClearance().has_value() )
1288 {
1289 m_out->Print( aNestLevel+1, "(clearance %s)\n",
1290 formatInternalUnits( aFootprint->GetLocalClearance().value() ).c_str() );
1291 }
1292
1293 if( aFootprint->GetLocalZoneConnection() != ZONE_CONNECTION::INHERITED )
1294 {
1295 m_out->Print( aNestLevel+1, "(zone_connect %d)\n",
1296 static_cast<int>( aFootprint->GetLocalZoneConnection() ) );
1297 }
1298
1299 // Attributes
1300 if( aFootprint->GetAttributes() )
1301 {
1302 m_out->Print( aNestLevel+1, "(attr" );
1303
1304 if( aFootprint->GetAttributes() & FP_SMD )
1305 m_out->Print( 0, " smd" );
1306
1307 if( aFootprint->GetAttributes() & FP_THROUGH_HOLE )
1308 m_out->Print( 0, " through_hole" );
1309
1310 if( aFootprint->GetAttributes() & FP_BOARD_ONLY )
1311 m_out->Print( 0, " board_only" );
1312
1313 if( aFootprint->GetAttributes() & FP_EXCLUDE_FROM_POS_FILES )
1314 m_out->Print( 0, " exclude_from_pos_files" );
1315
1316 if( aFootprint->GetAttributes() & FP_EXCLUDE_FROM_BOM )
1317 m_out->Print( 0, " exclude_from_bom" );
1318
1319 if( aFootprint->GetAttributes() & FP_ALLOW_MISSING_COURTYARD )
1320 m_out->Print( 0, " allow_missing_courtyard" );
1321
1322 if( aFootprint->GetAttributes() & FP_DNP )
1323 m_out->Print( 0, " dnp" );
1324
1325 if( aFootprint->GetAttributes() & FP_ALLOW_SOLDERMASK_BRIDGES )
1326 m_out->Print( 0, " allow_soldermask_bridges" );
1327
1328 m_out->Print( 0, ")\n" );
1329 }
1330
1331 if( aFootprint->GetPrivateLayers().any() )
1332 {
1333 m_out->Print( aNestLevel+1, "(private_layers" );
1334
1335 for( PCB_LAYER_ID layer : aFootprint->GetPrivateLayers().Seq() )
1336 {
1337 wxString canonicalName( LSET::Name( layer ) );
1338 m_out->Print( 0, " \"%s\"", canonicalName.ToStdString().c_str() );
1339 }
1340
1341 m_out->Print( 0, ")\n" );
1342 }
1343
1344 if( aFootprint->IsNetTie() )
1345 {
1346 m_out->Print( aNestLevel+1, "(net_tie_pad_groups" );
1347
1348 for( const wxString& group : aFootprint->GetNetTiePadGroups() )
1349 m_out->Print( 0, " \"%s\"", EscapeString( group, CTX_QUOTED_STR ).ToStdString().c_str() );
1350
1351 m_out->Print( 0, ")\n" );
1352 }
1353
1354 Format( (BOARD_ITEM*) &aFootprint->Reference(), aNestLevel + 1 );
1355 Format( (BOARD_ITEM*) &aFootprint->Value(), aNestLevel + 1 );
1356
1357 std::set<PAD*, FOOTPRINT::cmp_pads> sorted_pads( aFootprint->Pads().begin(),
1358 aFootprint->Pads().end() );
1359 std::set<BOARD_ITEM*, FOOTPRINT::cmp_drawings> sorted_drawings(
1360 aFootprint->GraphicalItems().begin(),
1361 aFootprint->GraphicalItems().end() );
1362 std::set<ZONE*, FOOTPRINT::cmp_zones> sorted_zones( aFootprint->Zones().begin(),
1363 aFootprint->Zones().end() );
1364 std::set<BOARD_ITEM*, PCB_GROUP::ptr_cmp> sorted_groups( aFootprint->Groups().begin(),
1365 aFootprint->Groups().end() );
1366
1367 // Save drawing elements.
1368
1369 for( BOARD_ITEM* gr : sorted_drawings )
1370 Format( gr, aNestLevel+1 );
1371
1372 // Save pads.
1373 for( PAD* pad : sorted_pads )
1374 Format( pad, aNestLevel+1 );
1375
1376 // Save zones.
1377 for( BOARD_ITEM* zone : sorted_zones )
1378 Format( zone, aNestLevel + 1 );
1379
1380 // Save groups.
1381 for( BOARD_ITEM* group : sorted_groups )
1382 Format( group, aNestLevel + 1 );
1383
1384 m_out->Print( aNestLevel + 1, "(embedded_fonts %s)\n",
1385 aFootprint->GetEmbeddedFiles()->GetAreFontsEmbedded() ? "yes" : "no" );
1386
1387 if( !aFootprint->GetEmbeddedFiles()->IsEmpty() )
1388 {
1389 aFootprint->WriteEmbeddedFiles( *m_out, aNestLevel + 1, !( m_ctl & CTL_FOR_BOARD ) );
1390 }
1391
1392 // Save 3D info.
1393 auto bs3D = aFootprint->Models().begin();
1394 auto es3D = aFootprint->Models().end();
1395
1396 while( bs3D != es3D )
1397 {
1398 if( !bs3D->m_Filename.IsEmpty() )
1399 {
1400 m_out->Print( aNestLevel+1, "(model %s\n",
1401 m_out->Quotew( bs3D->m_Filename ).c_str() );
1402
1403 if( !bs3D->m_Show )
1404 KICAD_FORMAT::FormatBool( m_out, aNestLevel + 1, "hide", !bs3D->m_Show );
1405
1406 if( bs3D->m_Opacity != 1.0 )
1407 m_out->Print( aNestLevel+2, "(opacity %0.4f)", bs3D->m_Opacity );
1408
1409 m_out->Print( aNestLevel+2, "(offset (xyz %s %s %s))\n",
1410 FormatDouble2Str( bs3D->m_Offset.x ).c_str(),
1411 FormatDouble2Str( bs3D->m_Offset.y ).c_str(),
1412 FormatDouble2Str( bs3D->m_Offset.z ).c_str() );
1413
1414 m_out->Print( aNestLevel+2, "(scale (xyz %s %s %s))\n",
1415 FormatDouble2Str( bs3D->m_Scale.x ).c_str(),
1416 FormatDouble2Str( bs3D->m_Scale.y ).c_str(),
1417 FormatDouble2Str( bs3D->m_Scale.z ).c_str() );
1418
1419 m_out->Print( aNestLevel+2, "(rotate (xyz %s %s %s))\n",
1420 FormatDouble2Str( bs3D->m_Rotation.x ).c_str(),
1421 FormatDouble2Str( bs3D->m_Rotation.y ).c_str(),
1422 FormatDouble2Str( bs3D->m_Rotation.z ).c_str() );
1423
1424 m_out->Print( aNestLevel+1, ")\n" );
1425 }
1426
1427 ++bs3D;
1428 }
1429
1430 m_out->Print( aNestLevel, ")\n" );
1431}
1432
1433
1434void PCB_IO_KICAD_SEXPR::formatLayers( LSET aLayerMask, int aNestLevel ) const
1435{
1436 std::string output;
1437
1438 if( aNestLevel == 0 )
1439 output += ' ';
1440
1441 output += "(layers";
1442
1443 static const LSET cu_all( LSET::AllCuMask() );
1444 static const LSET fr_bk( { B_Cu, F_Cu } );
1445 static const LSET adhes( { B_Adhes, F_Adhes } );
1446 static const LSET paste( { B_Paste, F_Paste } );
1447 static const LSET silks( { B_SilkS, F_SilkS } );
1448 static const LSET mask( { B_Mask, F_Mask } );
1449 static const LSET crt_yd( { B_CrtYd, F_CrtYd } );
1450 static const LSET fab( { B_Fab, F_Fab } );
1451
1452 LSET cu_mask = cu_all;
1453
1454 // output copper layers first, then non copper
1455
1456 if( ( aLayerMask & cu_mask ) == cu_mask )
1457 {
1458 output += ' ' + m_out->Quotew( "*.Cu" );
1459 aLayerMask &= ~cu_all; // clear bits, so they are not output again below
1460 }
1461 else if( ( aLayerMask & cu_mask ) == fr_bk )
1462 {
1463 output += ' ' + m_out->Quotew( "F&B.Cu" );
1464 aLayerMask &= ~fr_bk;
1465 }
1466
1467 if( ( aLayerMask & adhes ) == adhes )
1468 {
1469 output += ' ' + m_out->Quotew( "*.Adhes" );
1470 aLayerMask &= ~adhes;
1471 }
1472
1473 if( ( aLayerMask & paste ) == paste )
1474 {
1475 output += ' ' + m_out->Quotew( "*.Paste" );
1476 aLayerMask &= ~paste;
1477 }
1478
1479 if( ( aLayerMask & silks ) == silks )
1480 {
1481 output += ' ' + m_out->Quotew( "*.SilkS" );
1482 aLayerMask &= ~silks;
1483 }
1484
1485 if( ( aLayerMask & mask ) == mask )
1486 {
1487 output += ' ' + m_out->Quotew( "*.Mask" );
1488 aLayerMask &= ~mask;
1489 }
1490
1491 if( ( aLayerMask & crt_yd ) == crt_yd )
1492 {
1493 output += ' ' + m_out->Quotew( "*.CrtYd" );
1494 aLayerMask &= ~crt_yd;
1495 }
1496
1497 if( ( aLayerMask & fab ) == fab )
1498 {
1499 output += ' ' + m_out->Quotew( "*.Fab" );
1500 aLayerMask &= ~fab;
1501 }
1502
1503 // output any individual layers not handled in wildcard combos above
1504 wxString layerName;
1505
1506 for( int layer = 0; layer < PCB_LAYER_ID_COUNT; ++layer )
1507 {
1508 if( aLayerMask[layer] )
1509 {
1510 layerName = LSET::Name( PCB_LAYER_ID( layer ) );
1511 output += ' ';
1512 output += m_out->Quotew( layerName );
1513 }
1514 }
1515
1516 m_out->Print( aNestLevel, "%s)", output.c_str() );
1517}
1518
1519
1520void PCB_IO_KICAD_SEXPR::format( const PAD* aPad, int aNestLevel ) const
1521{
1522 const BOARD* board = aPad->GetBoard();
1523 const char* shape;
1524
1525 switch( aPad->GetShape() )
1526 {
1527 case PAD_SHAPE::CIRCLE: shape = "circle"; break;
1528 case PAD_SHAPE::RECTANGLE: shape = "rect"; break;
1529 case PAD_SHAPE::OVAL: shape = "oval"; break;
1530 case PAD_SHAPE::TRAPEZOID: shape = "trapezoid"; break;
1531 case PAD_SHAPE::CHAMFERED_RECT:
1532 case PAD_SHAPE::ROUNDRECT: shape = "roundrect"; break;
1533 case PAD_SHAPE::CUSTOM: shape = "custom"; break;
1534
1535 default:
1536 THROW_IO_ERROR( wxString::Format( _( "unknown pad type: %d"), aPad->GetShape() ) );
1537 }
1538
1539 const char* type;
1540
1541 switch( aPad->GetAttribute() )
1542 {
1543 case PAD_ATTRIB::PTH: type = "thru_hole"; break;
1544 case PAD_ATTRIB::SMD: type = "smd"; break;
1545 case PAD_ATTRIB::CONN: type = "connect"; break;
1546 case PAD_ATTRIB::NPTH: type = "np_thru_hole"; break;
1547
1548 default:
1549 THROW_IO_ERROR( wxString::Format( wxT( "unknown pad attribute: %d" ),
1550 aPad->GetAttribute() ) );
1551 }
1552
1553 const char* property = nullptr;
1554
1555 switch( aPad->GetProperty() )
1556 {
1557 case PAD_PROP::NONE: break; // could be "none"
1558 case PAD_PROP::BGA: property = "pad_prop_bga"; break;
1559 case PAD_PROP::FIDUCIAL_GLBL: property = "pad_prop_fiducial_glob"; break;
1560 case PAD_PROP::FIDUCIAL_LOCAL: property = "pad_prop_fiducial_loc"; break;
1561 case PAD_PROP::TESTPOINT: property = "pad_prop_testpoint"; break;
1562 case PAD_PROP::HEATSINK: property = "pad_prop_heatsink"; break;
1563 case PAD_PROP::CASTELLATED: property = "pad_prop_castellated"; break;
1564 case PAD_PROP::MECHANICAL: property = "pad_prop_mechanical"; break;
1565
1566 default:
1567 THROW_IO_ERROR( wxString::Format( wxT( "unknown pad property: %d" ),
1568 aPad->GetProperty() ) );
1569 }
1570
1571 m_out->Print( aNestLevel, "(pad %s %s %s",
1572 m_out->Quotew( aPad->GetNumber() ).c_str(),
1573 type,
1574 shape );
1575
1576 m_out->Print( 0, " (at %s", formatInternalUnits( aPad->GetFPRelativePosition() ).c_str() );
1577
1578 if( !aPad->GetOrientation().IsZero() )
1579 m_out->Print( 0, " %s", EDA_UNIT_UTILS::FormatAngle( aPad->GetOrientation() ).c_str() );
1580
1581 m_out->Print( 0, ")" );
1582
1583 m_out->Print( 0, " (size %s)", formatInternalUnits( aPad->GetSize() ).c_str() );
1584
1585 if( (aPad->GetDelta().x) != 0 || (aPad->GetDelta().y != 0 ) )
1586 m_out->Print( 0, " (rect_delta %s)", formatInternalUnits( aPad->GetDelta() ).c_str() );
1587
1588 VECTOR2I sz = aPad->GetDrillSize();
1589 VECTOR2I shapeoffset = aPad->GetOffset();
1590
1591 if( (sz.x > 0) || (sz.y > 0) ||
1592 (shapeoffset.x != 0) || (shapeoffset.y != 0) )
1593 {
1594 m_out->Print( 0, " (drill" );
1595
1596 if( aPad->GetDrillShape() == PAD_DRILL_SHAPE::OBLONG )
1597 m_out->Print( 0, " oval" );
1598
1599 if( sz.x > 0 )
1600 m_out->Print( 0, " %s", formatInternalUnits( sz.x ).c_str() );
1601
1602 if( sz.y > 0 && sz.x != sz.y )
1603 m_out->Print( 0, " %s", formatInternalUnits( sz.y ).c_str() );
1604
1605 if( (shapeoffset.x != 0) || (shapeoffset.y != 0) )
1606 m_out->Print( 0, " (offset %s)", formatInternalUnits( aPad->GetOffset() ).c_str() );
1607
1608 m_out->Print( 0, ")" );
1609 }
1610
1611 // Add pad property, if exists.
1612 if( property )
1613 m_out->Print( 0, " (property %s)", property );
1614
1615 formatLayers( aPad->GetLayerSet() );
1616
1617 if( aPad->GetAttribute() == PAD_ATTRIB::PTH )
1618 {
1619 KICAD_FORMAT::FormatBool( m_out, 0, "remove_unused_layers", aPad->GetRemoveUnconnected() );
1620
1621 if( aPad->GetRemoveUnconnected() )
1622 {
1623 KICAD_FORMAT::FormatBool( m_out, 0, "keep_end_layers", aPad->GetKeepTopBottom() );
1624
1625 if( board ) // Will be nullptr in footprint library
1626 {
1627 m_out->Print( 0, " (zone_layer_connections" );
1628
1629 for( PCB_LAYER_ID layer : board->GetEnabledLayers().CuStack() )
1630 {
1631 if( aPad->GetZoneLayerOverride( layer ) == ZLO_FORCE_FLASHED )
1632 m_out->Print( 0, " %s", m_out->Quotew( LSET::Name( layer ) ).c_str() );
1633 }
1634
1635 m_out->Print( 0, ")" );
1636 }
1637 }
1638 }
1639
1640 // Output the radius ratio for rounded and chamfered rect pads
1641 if( aPad->GetShape() == PAD_SHAPE::ROUNDRECT || aPad->GetShape() == PAD_SHAPE::CHAMFERED_RECT)
1642 {
1643 m_out->Print( 0, " (roundrect_rratio %s)",
1644 FormatDouble2Str( aPad->GetRoundRectRadiusRatio() ).c_str() );
1645 }
1646
1647 // Output the chamfer corners for chamfered rect pads
1648 if( aPad->GetShape() == PAD_SHAPE::CHAMFERED_RECT)
1649 {
1650 m_out->Print( 0, "\n" );
1651
1652 m_out->Print( aNestLevel+1, "(chamfer_ratio %s)",
1653 FormatDouble2Str( aPad->GetChamferRectRatio() ).c_str() );
1654
1655 m_out->Print( 0, " (chamfer" );
1656
1657 if( ( aPad->GetChamferPositions() & RECT_CHAMFER_TOP_LEFT ) )
1658 m_out->Print( 0, " top_left" );
1659
1661 m_out->Print( 0, " top_right" );
1662
1664 m_out->Print( 0, " bottom_left" );
1665
1667 m_out->Print( 0, " bottom_right" );
1668
1669 m_out->Print( 0, ")" );
1670 }
1671
1672 std::string output;
1673
1674 // Unconnected pad is default net so don't save it.
1676 {
1677 StrPrintf( &output, " (net %d %s)", m_mapping->Translate( aPad->GetNetCode() ),
1678 m_out->Quotew( aPad->GetNetname() ).c_str() );
1679 }
1680
1681 // Pin functions and types are closely related to nets, so if CTL_OMIT_NETS is set, omit
1682 // them as well (for instance when saved from library editor).
1683 if( !( m_ctl & CTL_OMIT_PAD_NETS ) )
1684 {
1685 if( !aPad->GetPinFunction().IsEmpty() )
1686 {
1687 StrPrintf( &output, " (pinfunction %s)",
1688 m_out->Quotew( aPad->GetPinFunction() ).c_str() );
1689 }
1690
1691 if( !aPad->GetPinType().IsEmpty() )
1692 {
1693 StrPrintf( &output, " (pintype %s)",
1694 m_out->Quotew( aPad->GetPinType() ).c_str() );
1695 }
1696 }
1697
1698 if( aPad->GetPadToDieLength() != 0 )
1699 {
1700 StrPrintf( &output, " (die_length %s)",
1701 formatInternalUnits( aPad->GetPadToDieLength() ).c_str() );
1702 }
1703
1704 if( aPad->GetLocalSolderMaskMargin().has_value() )
1705 {
1706 StrPrintf( &output, " (solder_mask_margin %s)",
1707 formatInternalUnits( aPad->GetLocalSolderMaskMargin().value() ).c_str() );
1708 }
1709
1710 if( aPad->GetLocalSolderPasteMargin().has_value() )
1711 {
1712 StrPrintf( &output, " (solder_paste_margin %s)",
1713 formatInternalUnits( aPad->GetLocalSolderPasteMargin().value() ).c_str() );
1714 }
1715
1716 if( aPad->GetLocalSolderPasteMarginRatio().has_value() )
1717 {
1718 StrPrintf( &output, " (solder_paste_margin_ratio %s)",
1719 FormatDouble2Str( aPad->GetLocalSolderPasteMarginRatio().value() ).c_str() );
1720 }
1721
1722 if( aPad->GetLocalClearance().has_value() )
1723 {
1724 StrPrintf( &output, " (clearance %s)",
1725 formatInternalUnits( aPad->GetLocalClearance().value() ).c_str() );
1726 }
1727
1728 if( aPad->GetLocalZoneConnection() != ZONE_CONNECTION::INHERITED )
1729 {
1730 StrPrintf( &output, " (zone_connect %d)",
1731 static_cast<int>( aPad->GetLocalZoneConnection() ) );
1732 }
1733
1734 if( aPad->GetThermalSpokeWidth() != 0 )
1735 {
1736 StrPrintf( &output, " (thermal_bridge_width %s)",
1737 formatInternalUnits( aPad->GetThermalSpokeWidth() ).c_str() );
1738 }
1739
1740 EDA_ANGLE defaultThermalSpokeAngle = ANGLE_90;
1741
1742 if( aPad->GetShape() == PAD_SHAPE::CIRCLE ||
1743 ( aPad->GetShape() == PAD_SHAPE::CUSTOM && aPad->GetAnchorPadShape() == PAD_SHAPE::CIRCLE ) )
1744 {
1745 defaultThermalSpokeAngle = ANGLE_45;
1746 }
1747
1748 if( aPad->GetThermalSpokeAngle() != defaultThermalSpokeAngle )
1749 {
1750 StrPrintf( &output, " (thermal_bridge_angle %s)",
1752 }
1753
1754 if( aPad->GetThermalGap() != 0 )
1755 {
1756 StrPrintf( &output, " (thermal_gap %s)",
1757 formatInternalUnits( aPad->GetThermalGap() ).c_str() );
1758 }
1759
1760 if( output.size() )
1761 {
1762 m_out->Print( 0, "\n" );
1763 m_out->Print( aNestLevel+1, "%s", output.c_str()+1 ); // +1 skips 1st space on 1st element
1764 }
1765
1766 if( aPad->GetShape() == PAD_SHAPE::CUSTOM )
1767 {
1768 m_out->Print( 0, "\n");
1769 m_out->Print( aNestLevel+1, "(options" );
1770
1772 m_out->Print( 0, " (clearance convexhull)" );
1773 #if 1 // Set to 1 to output the default option
1774 else
1775 m_out->Print( 0, " (clearance outline)" );
1776 #endif
1777
1778 // Output the anchor pad shape (circle/rect)
1779 if( aPad->GetAnchorPadShape() == PAD_SHAPE::RECTANGLE )
1780 shape = "rect";
1781 else
1782 shape = "circle";
1783
1784 m_out->Print( 0, " (anchor %s)", shape );
1785
1786 m_out->Print( 0, ")"); // end of (options ...
1787
1788 // Output graphic primitive of the pad shape
1789 m_out->Print( 0, "\n");
1790 m_out->Print( aNestLevel+1, "(primitives" );
1791
1792 int nested_level = aNestLevel+2;
1793
1794 // Output all basic shapes
1795 for( const std::shared_ptr<PCB_SHAPE>& primitive : aPad->GetPrimitives() )
1796 {
1797 m_out->Print( 0, "\n");
1798
1799 switch( primitive->GetShape() )
1800 {
1801 case SHAPE_T::SEGMENT:
1802 if( primitive->IsProxyItem() )
1803 {
1804 m_out->Print( nested_level, "(gr_vector (start %s) (end %s)",
1805 formatInternalUnits( primitive->GetStart() ).c_str(),
1806 formatInternalUnits( primitive->GetEnd() ).c_str() );
1807 }
1808 else
1809 {
1810 m_out->Print( nested_level, "(gr_line (start %s) (end %s)",
1811 formatInternalUnits( primitive->GetStart() ).c_str(),
1812 formatInternalUnits( primitive->GetEnd() ).c_str() );
1813 }
1814 break;
1815
1816 case SHAPE_T::RECTANGLE:
1817 if( primitive->IsProxyItem() )
1818 {
1819 m_out->Print( nested_level, "(gr_bbox (start %s) (end %s)",
1820 formatInternalUnits( primitive->GetStart() ).c_str(),
1821 formatInternalUnits( primitive->GetEnd() ).c_str() );
1822 }
1823 else
1824 {
1825 m_out->Print( nested_level, "(gr_rect (start %s) (end %s)",
1826 formatInternalUnits( primitive->GetStart() ).c_str(),
1827 formatInternalUnits( primitive->GetEnd() ).c_str() );
1828 }
1829 break;
1830
1831 case SHAPE_T::ARC:
1832 m_out->Print( nested_level, "(gr_arc (start %s) (mid %s) (end %s)",
1833 formatInternalUnits( primitive->GetStart() ).c_str(),
1834 formatInternalUnits( primitive->GetArcMid() ).c_str(),
1835 formatInternalUnits( primitive->GetEnd() ).c_str() );
1836 break;
1837
1838 case SHAPE_T::CIRCLE:
1839 m_out->Print( nested_level, "(gr_circle (center %s) (end %s)",
1840 formatInternalUnits( primitive->GetStart() ).c_str(),
1841 formatInternalUnits( primitive->GetEnd() ).c_str() );
1842 break;
1843
1844 case SHAPE_T::BEZIER:
1845 m_out->Print( nested_level, "(gr_curve (pts (xy %s) (xy %s) (xy %s) (xy %s))",
1846 formatInternalUnits( primitive->GetStart() ).c_str(),
1847 formatInternalUnits( primitive->GetBezierC1() ).c_str(),
1848 formatInternalUnits( primitive->GetBezierC2() ).c_str(),
1849 formatInternalUnits( primitive->GetEnd() ).c_str() );
1850 break;
1851
1852 case SHAPE_T::POLY:
1853 if( primitive->IsPolyShapeValid() )
1854 {
1855 const SHAPE_POLY_SET& poly = primitive->GetPolyShape();
1856 const SHAPE_LINE_CHAIN& outline = poly.Outline( 0 );
1857
1858 m_out->Print( nested_level, "(gr_poly\n" );
1859 formatPolyPts( outline, nested_level, ADVANCED_CFG::GetCfg().m_CompactSave );
1860
1861 // Align the next info at the right place.
1862 m_out->Print( nested_level, " " );
1863 }
1864 break;
1865
1866 default:
1867 break;
1868 }
1869
1870 if( !primitive->IsProxyItem() )
1871 m_out->Print( 0, " (width %s)", formatInternalUnits( primitive->GetWidth() ).c_str() );
1872
1873 // The filled flag represents if a solid fill is present on circles,
1874 // rectangles and polygons
1875 if( ( primitive->GetShape() == SHAPE_T::POLY )
1876 || ( primitive->GetShape() == SHAPE_T::RECTANGLE )
1877 || ( primitive->GetShape() == SHAPE_T::CIRCLE ) )
1878 {
1879 KICAD_FORMAT::FormatBool( m_out, 0, "fill", primitive->IsFilled() );
1880 }
1881
1882 m_out->Print( 0, ")" );
1883 }
1884
1885 m_out->Print( 0, "\n" );
1886 m_out->Print( aNestLevel+1, ")" ); // end of (primitives
1887 }
1888
1890 {
1891 m_out->Print( 0, "\n" );
1892 formatTeardropParameters( aPad->GetTeardropParams(), aNestLevel+1 );
1893 }
1894
1895 m_out->Print( 0, "\n" );
1897 m_out->Print( aNestLevel, ")\n" );
1898}
1899
1900
1901void PCB_IO_KICAD_SEXPR::format( const PCB_TEXT* aText, int aNestLevel ) const
1902{
1903 FOOTPRINT* parentFP = aText->GetParentFootprint();
1904 std::string prefix;
1905 std::string type;
1906 VECTOR2I pos = aText->GetTextPos();
1907 bool isField = dynamic_cast<const PCB_FIELD*>( aText ) != nullptr;
1908
1909 // Always format dimension text as gr_text
1910 if( dynamic_cast<const PCB_DIMENSION_BASE*>( aText ) )
1911 parentFP = nullptr;
1912
1913 if( parentFP )
1914 {
1915 prefix = "fp";
1916 type = " user";
1917
1918 pos -= parentFP->GetPosition();
1919 RotatePoint( pos, -parentFP->GetOrientation() );
1920 }
1921 else
1922 {
1923 prefix = "gr";
1924 }
1925
1926 if( !isField )
1927 {
1928 m_out->Print( aNestLevel, "(%s_text%s %s", prefix.c_str(), type.c_str(),
1929 m_out->Quotew( aText->GetText() ).c_str() );
1930
1931 if( aText->IsLocked() )
1932 KICAD_FORMAT::FormatBool( m_out, 0, "locked", aText->IsLocked() );
1933 }
1934
1935 m_out->Print( 0, " (at %s", formatInternalUnits( pos ).c_str() );
1936
1937 // Due to Pcbnew history, fp_text angle is saved as an absolute on screen angle.
1938 // To avoid issues in the future, always save the angle, even if it is 0
1939 m_out->Print( 0, " %s", EDA_UNIT_UTILS::FormatAngle( aText->GetTextAngle() ).c_str() );
1940
1941 m_out->Print( 0, ")" );
1942
1943 if( parentFP && !aText->IsKeepUpright() )
1944 KICAD_FORMAT::FormatBool( m_out, 0, "unlocked", !aText->IsKeepUpright() );
1945
1946 formatLayer( aText->GetLayer(), aText->IsKnockout() );
1947
1948 if( parentFP && !aText->IsVisible() )
1949 KICAD_FORMAT::FormatBool( m_out, 0, "hide", !aText->IsVisible() );
1950
1952
1953 int ctl_flags = m_ctl | CTL_OMIT_HIDE;
1954
1955 // Currently, texts have no specific color and no hyperlink.
1956 // so ensure they are never written in kicad_pcb file
1957 ctl_flags |= CTL_OMIT_COLOR | CTL_OMIT_HYPERLINK;
1958
1959 aText->EDA_TEXT::Format( m_out, aNestLevel, ctl_flags );
1960
1961 if( aText->GetFont() && aText->GetFont()->IsOutline() )
1962 formatRenderCache( aText, aNestLevel + 1 );
1963
1964 if( !isField )
1965 m_out->Print( aNestLevel, ")\n" );
1966}
1967
1968
1969void PCB_IO_KICAD_SEXPR::format( const PCB_TEXTBOX* aTextBox, int aNestLevel ) const
1970{
1971 FOOTPRINT* parentFP = aTextBox->GetParentFootprint();
1972
1973 m_out->Print( aNestLevel, "(%s %s\n",
1974 aTextBox->Type() == PCB_TABLECELL_T ? "table_cell"
1975 : parentFP ? "fp_text_box"
1976 : "gr_text_box",
1977 m_out->Quotew( aTextBox->GetText() ).c_str() );
1978
1979 if( aTextBox->IsLocked() )
1980 KICAD_FORMAT::FormatBool( m_out, aNestLevel, "locked", aTextBox->IsLocked() );
1981
1982 if( aTextBox->GetShape() == SHAPE_T::RECTANGLE )
1983 {
1984 m_out->Print( aNestLevel + 1, "(start %s) (end %s)",
1985 formatInternalUnits( aTextBox->GetStart(), parentFP ).c_str(),
1986 formatInternalUnits( aTextBox->GetEnd(), parentFP ).c_str() );
1987 }
1988 else if( aTextBox->GetShape() == SHAPE_T::POLY )
1989 {
1990 const SHAPE_POLY_SET& poly = aTextBox->GetPolyShape();
1991 const SHAPE_LINE_CHAIN& outline = poly.Outline( 0 );
1992
1993 formatPolyPts( outline, aNestLevel, true, parentFP );
1994 }
1995 else
1996 {
1997 UNIMPLEMENTED_FOR( aTextBox->SHAPE_T_asString() );
1998 }
1999
2000 m_out->Print( 0, " (margins %s %s %s %s)",
2001 formatInternalUnits( aTextBox->GetMarginLeft() ).c_str(),
2002 formatInternalUnits( aTextBox->GetMarginTop() ).c_str(),
2003 formatInternalUnits( aTextBox->GetMarginRight() ).c_str(),
2004 formatInternalUnits( aTextBox->GetMarginBottom() ).c_str() );
2005
2006 if( const PCB_TABLECELL* cell = dynamic_cast<const PCB_TABLECELL*>( aTextBox ) )
2007 m_out->Print( 0, " (span %d %d)", cell->GetColSpan(), cell->GetRowSpan() );
2008
2009 EDA_ANGLE angle = aTextBox->GetTextAngle();
2010
2011 if( parentFP )
2012 {
2013 angle -= parentFP->GetOrientation();
2014 angle.Normalize720();
2015 }
2016
2017 if( !angle.IsZero() )
2018 m_out->Print( aNestLevel + 1, "(angle %s)", EDA_UNIT_UTILS::FormatAngle( angle ).c_str() );
2019
2020 formatLayer( aTextBox->GetLayer() );
2021 m_out->Print( 0, "\n" );
2022
2024
2025 // PCB_TEXTBOXes are never hidden, so always omit "hide" attribute
2026 aTextBox->EDA_TEXT::Format( m_out, aNestLevel, m_ctl | CTL_OMIT_HIDE );
2027
2028 if( aTextBox->Type() != PCB_TABLECELL_T )
2029 {
2030 KICAD_FORMAT::FormatBool( m_out, aNestLevel + 1, "border", aTextBox->IsBorderEnabled() );
2031
2032 aTextBox->GetStroke().Format( m_out, pcbIUScale, aNestLevel + 1 );
2033 }
2034
2035 if( aTextBox->GetFont() && aTextBox->GetFont()->IsOutline() )
2036 formatRenderCache( aTextBox, aNestLevel + 1 );
2037
2038 m_out->Print( aNestLevel, ")\n" );
2039}
2040
2041
2042void PCB_IO_KICAD_SEXPR::format( const PCB_TABLE* aTable, int aNestLevel ) const
2043{
2044 wxCHECK_RET( aTable != nullptr && m_out != nullptr, "" );
2045
2046 m_out->Print( aNestLevel, "(table (column_count %d)",
2047 aTable->GetColCount() );
2048
2049 if( aTable->IsLocked() )
2050 KICAD_FORMAT::FormatBool( m_out, 0, "locked", aTable->IsLocked() );
2051
2052 EDA_ANGLE angle = aTable->GetOrientation();
2053
2054 if( FOOTPRINT* parentFP = aTable->GetParentFootprint() )
2055 {
2056 angle -= parentFP->GetOrientation();
2057 angle.Normalize720();
2058 }
2059
2060 if( !angle.IsZero() )
2061 m_out->Print( 0, " (angle %s)", EDA_UNIT_UTILS::FormatAngle( angle ).c_str() );
2062
2063 formatLayer( aTable->GetLayer() );
2064
2065 m_out->Print( 0, "\n" );
2066
2067 m_out->Print( aNestLevel + 1, "(border (external %s) (header %s)",
2068 aTable->StrokeExternal() ? "yes" : "no",
2069 aTable->StrokeHeader() ? "yes" : "no" );
2070
2071 if( aTable->StrokeExternal() || aTable->StrokeHeader() )
2072 {
2073 m_out->Print( 0, " " );
2074 aTable->GetBorderStroke().Format( m_out, pcbIUScale, 0 );
2075 }
2076
2077 m_out->Print( 0, ")\n" );
2078
2079 m_out->Print( aNestLevel + 1, "(separators (rows %s) (cols %s)",
2080 aTable->StrokeRows() ? "yes" : "no",
2081 aTable->StrokeColumns() ? "yes" : "no" );
2082
2083 if( aTable->StrokeRows() || aTable->StrokeColumns() )
2084 {
2085 m_out->Print( 0, " " );
2086 aTable->GetSeparatorsStroke().Format( m_out, pcbIUScale, 0 );
2087 }
2088
2089 m_out->Print( 0, ")\n" ); // Close `separators` token.
2090
2091 m_out->Print( aNestLevel + 1, "(column_widths" );
2092
2093 for( int col = 0; col < aTable->GetColCount(); ++col )
2094 m_out->Print( 0, " %s", formatInternalUnits( aTable->GetColWidth( col ) ).c_str() );
2095
2096 m_out->Print( 0, ")\n" );
2097
2098 m_out->Print( aNestLevel + 1, "(row_heights" );
2099
2100 for( int row = 0; row < aTable->GetRowCount(); ++row )
2101 m_out->Print( 0, " %s", formatInternalUnits( aTable->GetRowHeight( row ) ).c_str() );
2102
2103 m_out->Print( 0, ")\n" );
2104
2105 m_out->Print( aNestLevel + 1, "(cells\n" );
2106
2107 for( PCB_TABLECELL* cell : aTable->GetCells() )
2108 format( static_cast<PCB_TEXTBOX*>( cell ), aNestLevel + 2 );
2109
2110 m_out->Print( aNestLevel + 1, ")\n" ); // Close `cells` token.
2111 m_out->Print( aNestLevel, ")\n" ); // Close `table` token.
2112}
2113
2114
2115void PCB_IO_KICAD_SEXPR::format( const PCB_GROUP* aGroup, int aNestLevel ) const
2116{
2117 // Don't write empty groups
2118 if( aGroup->GetItems().empty() )
2119 return;
2120
2121 m_out->Print( aNestLevel, "(group %s\n", m_out->Quotew( aGroup->GetName() ).c_str() );
2122
2124
2125 if( aGroup->IsLocked() )
2126 KICAD_FORMAT::FormatBool( m_out, aNestLevel + 1, "locked", aGroup->IsLocked() );
2127
2128 m_out->Print( aNestLevel + 1, "(members\n" );
2129
2130 wxArrayString memberIds;
2131
2132 for( BOARD_ITEM* member : aGroup->GetItems() )
2133 memberIds.Add( member->m_Uuid.AsString() );
2134
2135 memberIds.Sort();
2136
2137 for( const wxString& memberId : memberIds )
2138 m_out->Print( aNestLevel + 2, "\"%s\"\n", TO_UTF8( memberId ) );
2139
2140 m_out->Print( aNestLevel + 1, ")\n" ); // Close `members` token.
2141 m_out->Print( aNestLevel, ")\n" ); // Close `group` token.
2142}
2143
2144
2145void PCB_IO_KICAD_SEXPR::format( const PCB_GENERATOR* aGenerator, int aNestLevel ) const
2146{
2147 m_out->Print( aNestLevel, "(generated\n" );
2148
2149 KICAD_FORMAT::FormatUuid( m_out, aGenerator->m_Uuid );
2150
2151 m_out->Print( aNestLevel + 1, "(type %s) (name %s)\n",
2152 TO_UTF8( aGenerator->GetGeneratorType() ),
2153 m_out->Quotew( aGenerator->GetName() ).c_str() );
2154
2155 m_out->Print( aNestLevel + 1, "(layer %s)",
2156 m_out->Quotew( LSET::Name( aGenerator->GetLayer() ) ).c_str() );
2157
2158 if( const bool locked = aGenerator->IsLocked() ) {
2159 KICAD_FORMAT::FormatBool( m_out, 0, "locked", locked );
2160 }
2161
2162 for( const auto& [key, value] : aGenerator->GetProperties() )
2163 {
2164 if( value.CheckType<double>() || value.CheckType<int>() || value.CheckType<long>()
2165 || value.CheckType<long long>() )
2166 {
2167 double val;
2168
2169 if( !value.GetAs( &val ) )
2170 continue;
2171
2172 std::string buf = fmt::format( "{:.10g}", val );
2173
2174 // Don't quote numbers
2175 m_out->Print( aNestLevel + 1, "(%s %s)\n", key.c_str(), buf.c_str() );
2176 }
2177 else if( value.CheckType<bool>() )
2178 {
2179 bool val;
2180 value.GetAs( &val );
2181
2182 m_out->Print( aNestLevel + 1, "(%s %s)\n", key.c_str(), val ? "yes" : "no" );
2183 }
2184 else if( value.CheckType<VECTOR2I>() )
2185 {
2186 VECTOR2I val;
2187 value.GetAs( &val );
2188
2189 m_out->Print( aNestLevel + 1, "(%s (xy %s))\n", key.c_str(),
2190 formatInternalUnits( val ).c_str() );
2191 }
2192 else if( value.CheckType<SHAPE_LINE_CHAIN>() )
2193 {
2194 SHAPE_LINE_CHAIN val;
2195 value.GetAs( &val );
2196
2197 m_out->Print( aNestLevel + 1, "(%s (pts\n", key.c_str() );
2198
2199 for( const VECTOR2I& pt : val.CPoints() )
2200 m_out->Print( aNestLevel + 2, "(xy %s)\n", formatInternalUnits( pt ).c_str() );
2201
2202 m_out->Print( aNestLevel + 1, "))\n" );
2203 }
2204 else
2205 {
2206 wxString val;
2207
2208 if( value.CheckType<wxString>() )
2209 {
2210 value.GetAs( &val );
2211 }
2212 else if( value.CheckType<std::string>() )
2213 {
2214 std::string str;
2215 value.GetAs( &str );
2216
2217 val = wxString::FromUTF8( str );
2218 }
2219
2220 m_out->Print( aNestLevel + 1, "(%s %s)\n", key.c_str(), m_out->Quotew( val ).c_str() );
2221 }
2222 }
2223
2224 m_out->Print( aNestLevel + 1, "(members\n" );
2225
2226 wxArrayString memberIds;
2227
2228 for( BOARD_ITEM* member : aGenerator->GetItems() )
2229 memberIds.Add( member->m_Uuid.AsString() );
2230
2231 memberIds.Sort();
2232
2233 for( const wxString& memberId : memberIds )
2234 m_out->Print( aNestLevel + 2, "%s\n", TO_UTF8( memberId ) );
2235
2236 m_out->Print( aNestLevel + 1, ")\n" ); // Close `members` token.
2237
2238 m_out->Print( aNestLevel, ")\n" ); // Close `generator` token.
2239}
2240
2241
2242void PCB_IO_KICAD_SEXPR::format( const PCB_TRACK* aTrack, int aNestLevel ) const
2243{
2244 if( aTrack->Type() == PCB_VIA_T )
2245 {
2246 PCB_LAYER_ID layer1, layer2;
2247
2248 const PCB_VIA* via = static_cast<const PCB_VIA*>( aTrack );
2249 const BOARD* board = via->GetBoard();
2250
2251 wxCHECK_RET( board != nullptr, wxT( "Via has no parent." ) );
2252
2253 m_out->Print( aNestLevel, "(via" );
2254
2255 via->LayerPair( &layer1, &layer2 );
2256
2257 switch( via->GetViaType() )
2258 {
2259 case VIATYPE::THROUGH: // Default shape not saved.
2260 break;
2261
2262 case VIATYPE::BLIND_BURIED:
2263 m_out->Print( 0, " blind" );
2264 break;
2265
2266 case VIATYPE::MICROVIA:
2267 m_out->Print( 0, " micro" );
2268 break;
2269
2270 default:
2271 THROW_IO_ERROR( wxString::Format( _( "unknown via type %d" ), via->GetViaType() ) );
2272 }
2273
2274 m_out->Print( 0, " (at %s) (size %s)",
2275 formatInternalUnits( aTrack->GetStart() ).c_str(),
2276 formatInternalUnits( aTrack->GetWidth() ).c_str() );
2277
2278 // Old boards were using UNDEFINED_DRILL_DIAMETER value in file for via drill when
2279 // via drill was the netclass value.
2280 // recent boards always set the via drill to the actual value, but now we need to
2281 // always store the drill value, because netclass value is not stored in the board file.
2282 // Otherwise the drill value of some (old) vias can be unknown
2283 if( via->GetDrill() != UNDEFINED_DRILL_DIAMETER )
2284 m_out->Print( 0, " (drill %s)", formatInternalUnits( via->GetDrill() ).c_str() );
2285 else
2286 m_out->Print( 0, " (drill %s)", formatInternalUnits( via->GetDrillValue() ).c_str() );
2287
2288 m_out->Print( 0, " (layers %s %s)",
2289 m_out->Quotew( LSET::Name( layer1 ) ).c_str(),
2290 m_out->Quotew( LSET::Name( layer2 ) ).c_str() );
2291
2292 switch( via->Padstack().UnconnectedLayerMode() )
2293 {
2295 m_out->Print( 0, "(remove_unused_layers yes)" );
2296 m_out->Print( 0, "(keep_end_layers no)" );
2297 break;
2298
2300 m_out->Print( 0, "(remove_unused_layers yes)" );
2301 m_out->Print( 0, "(keep_end_layers yes)" );
2302 break;
2303
2305 break;
2306 }
2307
2308 if( via->IsLocked() )
2309 KICAD_FORMAT::FormatBool( m_out, 0, "locked", via->IsLocked() );
2310
2311 if( via->GetIsFree() )
2312 KICAD_FORMAT::FormatBool( m_out, 0, "free", via->GetIsFree() );
2313
2314 if( via->GetRemoveUnconnected() )
2315 {
2316 m_out->Print( 0, " (zone_layer_connections" );
2317
2318 for( PCB_LAYER_ID layer : board->GetEnabledLayers().CuStack() )
2319 {
2320 if( via->GetZoneLayerOverride( layer ) == ZLO_FORCE_FLASHED )
2321 m_out->Print( 0, " %s", m_out->Quotew( LSET::Name( layer ) ).c_str() );
2322 }
2323
2324 m_out->Print( 0, ")" );
2325 }
2326
2327 std::optional<bool> front = via->Padstack().FrontOuterLayers().has_solder_mask;
2328 std::optional<bool> back = via->Padstack().BackOuterLayers().has_solder_mask;
2329
2330 if( front.has_value() || back.has_value() )
2331 {
2332 if( front.value_or( false ) || back.value_or( false ) )
2333 {
2334 m_out->Print( 0, " (tenting " );
2335
2336 if( front.value_or( false ) )
2337 m_out->Print( 0, " front" );
2338 if( back.value_or( false ) )
2339 m_out->Print( 0, " back" );
2340
2341 m_out->Print( 0, ")" );
2342 }
2343 else
2344 {
2345 m_out->Print( 0, " (tenting none)" );
2346 }
2347 }
2348
2349 if( !isDefaultTeardropParameters( via->GetTeardropParams() ) )
2350 {
2351 m_out->Print( 0, "\n" );
2352 formatTeardropParameters( via->GetTeardropParams(), aNestLevel+1 );
2353 }
2354 }
2355 else if( aTrack->Type() == PCB_ARC_T )
2356 {
2357 const PCB_ARC* arc = static_cast<const PCB_ARC*>( aTrack );
2358
2359 m_out->Print( aNestLevel, "(arc (start %s) (mid %s) (end %s) (width %s)",
2360 formatInternalUnits( arc->GetStart() ).c_str(),
2361 formatInternalUnits( arc->GetMid() ).c_str(),
2362 formatInternalUnits( arc->GetEnd() ).c_str(),
2363 formatInternalUnits( arc->GetWidth() ).c_str() );
2364
2365 if( arc->IsLocked() )
2366 KICAD_FORMAT::FormatBool( m_out, 0, "locked", arc->IsLocked() );
2367
2368 m_out->Print( 0, " (layer %s)", m_out->Quotew( LSET::Name( arc->GetLayer() ) ).c_str() );
2369 }
2370 else
2371 {
2372 m_out->Print( aNestLevel, "(segment (start %s) (end %s) (width %s)",
2373 formatInternalUnits( aTrack->GetStart() ).c_str(),
2374 formatInternalUnits( aTrack->GetEnd() ).c_str(),
2375 formatInternalUnits( aTrack->GetWidth() ).c_str() );
2376
2377 if( aTrack->IsLocked() )
2378 KICAD_FORMAT::FormatBool( m_out, 0, "locked", aTrack->IsLocked() );
2379
2380 m_out->Print( 0, " (layer %s)", m_out->Quotew( LSET::Name( aTrack->GetLayer() ) ).c_str() );
2381 }
2382
2383 m_out->Print( 0, " (net %d)", m_mapping->Translate( aTrack->GetNetCode() ) );
2384
2386
2387 m_out->Print( 0, ")\n" );
2388}
2389
2390
2391void PCB_IO_KICAD_SEXPR::format( const ZONE* aZone, int aNestLevel ) const
2392{
2393 // Save the NET info.
2394 // For keepout and non copper zones, net code and net name are irrelevant
2395 // so be sure a dummy value is stored, just for ZONE compatibility
2396 // (perhaps netcode and netname should be not stored)
2397 bool has_no_net = aZone->GetIsRuleArea() || !aZone->IsOnCopperLayer();
2398
2399 m_out->Print( aNestLevel, "(zone (net %d) (net_name %s)",
2400 has_no_net ? 0 : m_mapping->Translate( aZone->GetNetCode() ),
2401 m_out->Quotew( has_no_net ? wxString( wxT("") ) : aZone->GetNetname() ).c_str() );
2402
2403 if( aZone->IsLocked() )
2404 KICAD_FORMAT::FormatBool( m_out, 0, "locked", aZone->IsLocked() );
2405
2406 // If a zone exists on multiple layers, format accordingly
2407 LSET layers = aZone->GetLayerSet();
2408
2409 if( aZone->GetBoard() )
2410 layers &= aZone->GetBoard()->GetEnabledLayers();
2411
2412 if( layers.count() > 1 )
2413 {
2414 formatLayers( layers );
2415 }
2416 else
2417 {
2418 formatLayer( aZone->GetFirstLayer() );
2419 }
2420
2422
2423 if( !aZone->GetZoneName().empty() )
2424 m_out->Print( 0, " (name %s)", m_out->Quotew( aZone->GetZoneName() ).c_str() );
2425
2426 // Save the outline aux info
2427 std::string hatch;
2428
2429 switch( aZone->GetHatchStyle() )
2430 {
2431 default:
2432 case ZONE_BORDER_DISPLAY_STYLE::NO_HATCH: hatch = "none"; break;
2433 case ZONE_BORDER_DISPLAY_STYLE::DIAGONAL_EDGE: hatch = "edge"; break;
2434 case ZONE_BORDER_DISPLAY_STYLE::DIAGONAL_FULL: hatch = "full"; break;
2435 }
2436
2437 m_out->Print( 0, " (hatch %s %s)\n", hatch.c_str(),
2438 formatInternalUnits( aZone->GetBorderHatchPitch() ).c_str() );
2439
2440 if( aZone->GetAssignedPriority() > 0 )
2441 m_out->Print( aNestLevel+1, "(priority %d)\n", aZone->GetAssignedPriority() );
2442
2443 // Add teardrop keywords in file: (attr (teardrop (type xxx)))where xxx is the teardrop type
2444 if( aZone->IsTeardropArea() )
2445 {
2446 const char* td_type;
2447
2448 switch( aZone->GetTeardropAreaType() )
2449 {
2450 case TEARDROP_TYPE::TD_VIAPAD: // a teardrop on a via or pad
2451 td_type = "padvia";
2452 break;
2453
2454 default:
2455 case TEARDROP_TYPE::TD_TRACKEND: // a teardrop on a track end
2456 td_type = "track_end";
2457 break;
2458 }
2459
2460 m_out->Print( aNestLevel+1, "(attr (teardrop (type %s)))\n", td_type );
2461 }
2462
2463 m_out->Print( aNestLevel+1, "(connect_pads" );
2464
2465 switch( aZone->GetPadConnection() )
2466 {
2467 default:
2468 case ZONE_CONNECTION::THERMAL: // Default option not saved or loaded.
2469 break;
2470
2471 case ZONE_CONNECTION::THT_THERMAL:
2472 m_out->Print( 0, " thru_hole_only" );
2473 break;
2474
2475 case ZONE_CONNECTION::FULL:
2476 m_out->Print( 0, " yes" );
2477 break;
2478
2479 case ZONE_CONNECTION::NONE:
2480 m_out->Print( 0, " no" );
2481 break;
2482 }
2483
2484 m_out->Print( 0, " (clearance %s))\n",
2485 formatInternalUnits( aZone->GetLocalClearance().value() ).c_str() );
2486
2487 m_out->Print( aNestLevel+1, "(min_thickness %s)",
2488 formatInternalUnits( aZone->GetMinThickness() ).c_str() );
2489
2490 // We continue to write this for 3rd-party parsers, but we no longer read it (as of V7).
2491 m_out->Print( 0, " (filled_areas_thickness no)" );
2492
2493 m_out->Print( 0, "\n" );
2494
2495 if( aZone->GetIsRuleArea() )
2496 {
2497 m_out->Print( aNestLevel + 1,
2498 "(keepout (tracks %s) (vias %s) (pads %s) (copperpour %s) "
2499 "(footprints %s))\n",
2500 aZone->GetDoNotAllowTracks() ? "not_allowed" : "allowed",
2501 aZone->GetDoNotAllowVias() ? "not_allowed" : "allowed",
2502 aZone->GetDoNotAllowPads() ? "not_allowed" : "allowed",
2503 aZone->GetDoNotAllowCopperPour() ? "not_allowed" : "allowed",
2504 aZone->GetDoNotAllowFootprints() ? "not_allowed" : "allowed" );
2505 }
2506
2507 m_out->Print( aNestLevel + 1, "(fill" );
2508
2509 // Default is not filled.
2510 if( aZone->IsFilled() )
2511 m_out->Print( 0, " yes" );
2512
2513 // Default is polygon filled.
2514 if( aZone->GetFillMode() == ZONE_FILL_MODE::HATCH_PATTERN )
2515 m_out->Print( 0, " (mode hatch)" );
2516
2517 m_out->Print( 0, " (thermal_gap %s) (thermal_bridge_width %s)",
2518 formatInternalUnits( aZone->GetThermalReliefGap() ).c_str(),
2519 formatInternalUnits( aZone->GetThermalReliefSpokeWidth() ).c_str() );
2520
2522 {
2523 m_out->Print( 0, " (smoothing" );
2524
2525 switch( aZone->GetCornerSmoothingType() )
2526 {
2528 m_out->Print( 0, " chamfer" );
2529 break;
2530
2532 m_out->Print( 0, " fillet" );
2533 break;
2534
2535 default:
2536 THROW_IO_ERROR( wxString::Format( _( "unknown zone corner smoothing type %d" ),
2537 aZone->GetCornerSmoothingType() ) );
2538 }
2539 m_out->Print( 0, ")" );
2540
2541 if( aZone->GetCornerRadius() != 0 )
2542 m_out->Print( 0, " (radius %s)", formatInternalUnits( aZone->GetCornerRadius() ).c_str() );
2543 }
2544
2545 if( aZone->GetIslandRemovalMode() != ISLAND_REMOVAL_MODE::ALWAYS )
2546 {
2547 m_out->Print( 0, " (island_removal_mode %d) (island_area_min %s)",
2548 static_cast<int>( aZone->GetIslandRemovalMode() ),
2550 }
2551
2552 if( aZone->GetFillMode() == ZONE_FILL_MODE::HATCH_PATTERN )
2553 {
2554 m_out->Print( 0, "\n" );
2555 m_out->Print( aNestLevel+2, "(hatch_thickness %s) (hatch_gap %s) (hatch_orientation %s)",
2556 formatInternalUnits( aZone->GetHatchThickness() ).c_str(),
2557 formatInternalUnits( aZone->GetHatchGap() ).c_str(),
2558 FormatDouble2Str( aZone->GetHatchOrientation().AsDegrees() ).c_str() );
2559
2560 if( aZone->GetHatchSmoothingLevel() > 0 )
2561 {
2562 m_out->Print( 0, "\n" );
2563 m_out->Print( aNestLevel+2, "(hatch_smoothing_level %d) (hatch_smoothing_value %s)",
2564 aZone->GetHatchSmoothingLevel(),
2565 FormatDouble2Str( aZone->GetHatchSmoothingValue() ).c_str() );
2566 }
2567
2568 m_out->Print( 0, "\n" );
2569 m_out->Print( aNestLevel+2, "(hatch_border_algorithm %s) (hatch_min_hole_area %s)",
2570 aZone->GetHatchBorderAlgorithm() ? "hatch_thickness" : "min_thickness",
2571 FormatDouble2Str( aZone->GetHatchHoleMinArea() ).c_str() );
2572 }
2573
2574 m_out->Print( 0, ")\n" );
2575
2576 if( aZone->GetNumCorners() )
2577 {
2578 SHAPE_POLY_SET::POLYGON poly = aZone->Outline()->Polygon(0);
2579
2580 for( auto& chain : poly )
2581 {
2582 m_out->Print( aNestLevel + 1, "(polygon\n" );
2583 formatPolyPts( chain, aNestLevel + 1, ADVANCED_CFG::GetCfg().m_CompactSave );
2584 m_out->Print( aNestLevel + 1, ")\n" );
2585 }
2586 }
2587
2588 // Save the PolysList (filled areas)
2589 for( PCB_LAYER_ID layer : aZone->GetLayerSet().Seq() )
2590 {
2591 const std::shared_ptr<SHAPE_POLY_SET>& fv = aZone->GetFilledPolysList( layer );
2592
2593 for( int ii = 0; ii < fv->OutlineCount(); ++ii )
2594 {
2595 m_out->Print( aNestLevel + 1, "(filled_polygon\n" );
2596 m_out->Print( aNestLevel + 2, "(layer %s)\n",
2597 m_out->Quotew( LSET::Name( layer ) ).c_str() );
2598
2599 if( aZone->IsIsland( layer, ii ) )
2600 m_out->Print( aNestLevel + 2, "(island)\n" );
2601
2602 const SHAPE_LINE_CHAIN& chain = fv->COutline( ii );
2603
2604 formatPolyPts( chain, aNestLevel + 1, ADVANCED_CFG::GetCfg().m_CompactSave );
2605 m_out->Print( aNestLevel + 1, ")\n" );
2606 }
2607 }
2608
2609 m_out->Print( aNestLevel, ")\n" );
2610}
2611
2612
2613PCB_IO_KICAD_SEXPR::PCB_IO_KICAD_SEXPR( int aControlFlags ) : PCB_IO( wxS( "KiCad" ) ),
2614 m_cache( nullptr ),
2615 m_ctl( aControlFlags ),
2616 m_mapping( new NETINFO_MAPPING() )
2617{
2618 init( nullptr );
2619 m_out = &m_sf;
2620}
2621
2622
2624{
2625 delete m_cache;
2626 delete m_mapping;
2627}
2628
2629
2630BOARD* PCB_IO_KICAD_SEXPR::LoadBoard( const wxString& aFileName, BOARD* aAppendToMe,
2631 const STRING_UTF8_MAP* aProperties, PROJECT* aProject )
2632{
2633 FILE_LINE_READER reader( aFileName );
2634
2635 unsigned lineCount = 0;
2636
2638
2639 if( m_progressReporter )
2640 {
2641 m_progressReporter->Report( wxString::Format( _( "Loading %s..." ), aFileName ) );
2642
2644 THROW_IO_ERROR( _( "Open cancelled by user." ) );
2645
2646 while( reader.ReadLine() )
2647 lineCount++;
2648
2649 reader.Rewind();
2650 }
2651
2652 BOARD* board = DoLoad( reader, aAppendToMe, aProperties, m_progressReporter, lineCount );
2653
2654 // Give the filename to the board if it's new
2655 if( !aAppendToMe )
2656 board->SetFileName( aFileName );
2657
2658 return board;
2659}
2660
2661
2662BOARD* PCB_IO_KICAD_SEXPR::DoLoad( LINE_READER& aReader, BOARD* aAppendToMe, const STRING_UTF8_MAP* aProperties,
2663 PROGRESS_REPORTER* aProgressReporter, unsigned aLineCount)
2664{
2665 init( aProperties );
2666
2667 PCB_IO_KICAD_SEXPR_PARSER parser( &aReader, aAppendToMe, m_queryUserCallback, aProgressReporter, aLineCount );
2668 BOARD* board;
2669
2670 try
2671 {
2672 board = dynamic_cast<BOARD*>( parser.Parse() );
2673 }
2674 catch( const FUTURE_FORMAT_ERROR& )
2675 {
2676 // Don't wrap a FUTURE_FORMAT_ERROR in another
2677 throw;
2678 }
2679 catch( const PARSE_ERROR& parse_error )
2680 {
2681 if( parser.IsTooRecent() )
2682 throw FUTURE_FORMAT_ERROR( parse_error, parser.GetRequiredVersion() );
2683 else
2684 throw;
2685 }
2686
2687 if( !board )
2688 {
2689 // The parser loaded something that was valid, but wasn't a board.
2690 THROW_PARSE_ERROR( _( "This file does not contain a PCB." ), parser.CurSource(),
2691 parser.CurLine(), parser.CurLineNumber(), parser.CurOffset() );
2692 }
2693
2694 return board;
2695}
2696
2697
2699{
2700 m_board = nullptr;
2701 m_reader = nullptr;
2702 m_props = aProperties;
2703}
2704
2705
2706void PCB_IO_KICAD_SEXPR::validateCache( const wxString& aLibraryPath, bool checkModified )
2707{
2709
2710 if( !m_cache || !m_cache->IsPath( aLibraryPath ) || ( checkModified && m_cache->IsModified() ) )
2711 {
2712 // a spectacular episode in memory management:
2713 delete m_cache;
2714 m_cache = new FP_CACHE( this, aLibraryPath );
2715 m_cache->Load();
2716 }
2717}
2718
2719
2720void PCB_IO_KICAD_SEXPR::FootprintEnumerate( wxArrayString& aFootprintNames, const wxString& aLibPath,
2721 bool aBestEfforts, const STRING_UTF8_MAP* aProperties )
2722{
2723 LOCALE_IO toggle; // toggles on, then off, the C locale.
2724 wxDir dir( aLibPath );
2725 wxString errorMsg;
2726
2727 init( aProperties );
2728
2729 try
2730 {
2731 validateCache( aLibPath );
2732 }
2733 catch( const IO_ERROR& ioe )
2734 {
2735 errorMsg = ioe.What();
2736 }
2737
2738 // Some of the files may have been parsed correctly so we want to add the valid files to
2739 // the library.
2740
2741 for( const auto& footprint : m_cache->GetFootprints() )
2742 aFootprintNames.Add( footprint.first );
2743
2744 if( !errorMsg.IsEmpty() && !aBestEfforts )
2745 THROW_IO_ERROR( errorMsg );
2746}
2747
2748
2749const FOOTPRINT* PCB_IO_KICAD_SEXPR::getFootprint( const wxString& aLibraryPath,
2750 const wxString& aFootprintName,
2751 const STRING_UTF8_MAP* aProperties,
2752 bool checkModified )
2753{
2754 LOCALE_IO toggle; // toggles on, then off, the C locale.
2755
2756 init( aProperties );
2757
2758 try
2759 {
2760 validateCache( aLibraryPath, checkModified );
2761 }
2762 catch( const IO_ERROR& )
2763 {
2764 // do nothing with the error
2765 }
2766
2768 FP_CACHE_FOOTPRINT_MAP::const_iterator it = footprints.find( aFootprintName );
2769
2770 if( it == footprints.end() )
2771 return nullptr;
2772
2773 return it->second->GetFootprint();
2774}
2775
2776
2777const FOOTPRINT* PCB_IO_KICAD_SEXPR::GetEnumeratedFootprint( const wxString& aLibraryPath,
2778 const wxString& aFootprintName,
2779 const STRING_UTF8_MAP* aProperties )
2780{
2781 return getFootprint( aLibraryPath, aFootprintName, aProperties, false );
2782}
2783
2784
2785bool PCB_IO_KICAD_SEXPR::FootprintExists( const wxString& aLibraryPath, const wxString& aFootprintName,
2786 const STRING_UTF8_MAP* aProperties )
2787{
2788 // Note: checking the cache sounds like a good idea, but won't catch files which differ
2789 // only in case.
2790 //
2791 // Since this goes out to the native filesystem, we get platform differences (ie: MSW's
2792 // case-insensitive filesystem) handled "for free".
2793 // Warning: footprint names frequently contain a point. So be careful when initializing
2794 // wxFileName, and use a CTOR with extension specified
2795 wxFileName footprintFile( aLibraryPath, aFootprintName, FILEEXT::KiCadFootprintFileExtension );
2796
2797 return footprintFile.Exists();
2798}
2799
2800
2801FOOTPRINT* PCB_IO_KICAD_SEXPR::ImportFootprint( const wxString& aFootprintPath, wxString& aFootprintNameOut,
2802 const STRING_UTF8_MAP* aProperties )
2803{
2804 wxString fcontents;
2805 wxFFile f( aFootprintPath );
2806
2808
2809 if( !f.IsOpened() )
2810 return nullptr;
2811
2812 f.ReadAll( &fcontents );
2813
2814 aFootprintNameOut = wxFileName( aFootprintPath ).GetName();
2815
2816 return dynamic_cast<FOOTPRINT*>( Parse( fcontents ) );
2817}
2818
2819
2820FOOTPRINT* PCB_IO_KICAD_SEXPR::FootprintLoad( const wxString& aLibraryPath,
2821 const wxString& aFootprintName,
2822 bool aKeepUUID,
2823 const STRING_UTF8_MAP* aProperties )
2824{
2826
2827 const FOOTPRINT* footprint = getFootprint( aLibraryPath, aFootprintName, aProperties, true );
2828
2829 if( footprint )
2830 {
2831 FOOTPRINT* copy;
2832
2833 if( aKeepUUID )
2834 copy = static_cast<FOOTPRINT*>( footprint->Clone() );
2835 else
2836 copy = static_cast<FOOTPRINT*>( footprint->Duplicate() );
2837
2838 copy->SetParent( nullptr );
2839 return copy;
2840 }
2841
2842 return nullptr;
2843}
2844
2845
2846void PCB_IO_KICAD_SEXPR::FootprintSave( const wxString& aLibraryPath, const FOOTPRINT* aFootprint,
2847 const STRING_UTF8_MAP* aProperties )
2848{
2849 LOCALE_IO toggle; // toggles on, then off, the C locale.
2850
2851 init( aProperties );
2852
2853 // In this public PLUGIN API function, we can safely assume it was
2854 // called for saving into a library path.
2856
2857 validateCache( aLibraryPath );
2858
2859 if( !m_cache->IsWritable() )
2860 {
2861 if( !m_cache->Exists() )
2862 {
2863 const wxString msg = wxString::Format( _( "Library '%s' does not exist.\n"
2864 "Would you like to create it?"),
2865 aLibraryPath );
2866
2867 if( !Pgm().IsGUI()
2868 || wxMessageBox( msg, _( "Library Not Found" ), wxYES_NO | wxICON_QUESTION )
2869 != wxYES )
2870 return;
2871
2872 // Save throws its own IO_ERROR on failure, so no need to recreate here
2873 m_cache->Save( nullptr );
2874 }
2875 else
2876 {
2877 wxString msg = wxString::Format( _( "Library '%s' is read only." ), aLibraryPath );
2878 THROW_IO_ERROR( msg );
2879 }
2880 }
2881
2882 wxString footprintName = aFootprint->GetFPID().GetLibItemName();
2883
2885 wxString fpName = aFootprint->GetFPID().GetLibItemName().wx_str();
2886 ReplaceIllegalFileNameChars( fpName, '_' );
2887
2888 // Quietly overwrite footprint and delete footprint file from path for any by same name.
2889 wxFileName fn( aLibraryPath, fpName, FILEEXT::KiCadFootprintFileExtension );
2890
2891 // Write through symlinks, don't replace them
2893
2894 if( !fn.IsOk() )
2895 {
2896 THROW_IO_ERROR( wxString::Format( _( "Footprint file name '%s' is not valid." ),
2897 fn.GetFullPath() ) );
2898 }
2899
2900 if( fn.FileExists() && !fn.IsFileWritable() )
2901 {
2902 THROW_IO_ERROR( wxString::Format( _( "Insufficient permissions to delete '%s'." ),
2903 fn.GetFullPath() ) );
2904 }
2905
2906 wxString fullPath = fn.GetFullPath();
2907 wxString fullName = fn.GetFullName();
2908 FP_CACHE_FOOTPRINT_MAP::const_iterator it = footprints.find( footprintName );
2909
2910 if( it != footprints.end() )
2911 {
2912 wxLogTrace( traceKicadPcbPlugin, wxT( "Removing footprint file '%s'." ), fullPath );
2913 footprints.erase( footprintName );
2914 wxRemoveFile( fullPath );
2915 }
2916
2917 // I need my own copy for the cache
2918 FOOTPRINT* footprint = static_cast<FOOTPRINT*>( aFootprint->Clone() );
2919
2920 // It's orientation should be zero and it should be on the front layer.
2921 footprint->SetOrientation( ANGLE_0 );
2922
2923 if( footprint->GetLayer() != F_Cu )
2924 {
2925 PCBNEW_SETTINGS* cfg = dynamic_cast<PCBNEW_SETTINGS*>( Kiface().KifaceSettings() );
2926
2927 if( cfg )
2928 footprint->Flip( footprint->GetPosition(), cfg->m_FlipLeftRight );
2929 else
2930 footprint->Flip( footprint->GetPosition(), false );
2931 }
2932
2933 // Detach it from the board and its group
2934 footprint->SetParent( nullptr );
2935 footprint->SetParentGroup( nullptr );
2936
2937 wxLogTrace( traceKicadPcbPlugin, wxT( "Creating s-expr footprint file '%s'." ), fullPath );
2938 footprints.insert( footprintName,
2939 new FP_CACHE_ITEM( footprint, WX_FILENAME( fn.GetPath(), fullName ) ) );
2940 m_cache->Save( footprint );
2941}
2942
2943
2944void PCB_IO_KICAD_SEXPR::FootprintDelete( const wxString& aLibraryPath, const wxString& aFootprintName,
2945 const STRING_UTF8_MAP* aProperties )
2946{
2947 LOCALE_IO toggle; // toggles on, then off, the C locale.
2948
2949 init( aProperties );
2950
2951 validateCache( aLibraryPath );
2952
2953 if( !m_cache->IsWritable() )
2954 {
2955 THROW_IO_ERROR( wxString::Format( _( "Library '%s' is read only." ),
2956 aLibraryPath.GetData() ) );
2957 }
2958
2959 m_cache->Remove( aFootprintName );
2960}
2961
2962
2963
2964long long PCB_IO_KICAD_SEXPR::GetLibraryTimestamp( const wxString& aLibraryPath ) const
2965{
2966 return FP_CACHE::GetTimestamp( aLibraryPath );
2967}
2968
2969
2970void PCB_IO_KICAD_SEXPR::CreateLibrary( const wxString& aLibraryPath, const STRING_UTF8_MAP* aProperties )
2971{
2972 if( wxDir::Exists( aLibraryPath ) )
2973 {
2974 THROW_IO_ERROR( wxString::Format( _( "Cannot overwrite library path '%s'." ),
2975 aLibraryPath.GetData() ) );
2976 }
2977
2978 LOCALE_IO toggle;
2979
2980 init( aProperties );
2981
2982 delete m_cache;
2983 m_cache = new FP_CACHE( this, aLibraryPath );
2984 m_cache->Save();
2985}
2986
2987
2988bool PCB_IO_KICAD_SEXPR::DeleteLibrary( const wxString& aLibraryPath, const STRING_UTF8_MAP* aProperties )
2989{
2990 wxFileName fn;
2991 fn.SetPath( aLibraryPath );
2992
2993 // Return if there is no library path to delete.
2994 if( !fn.DirExists() )
2995 return false;
2996
2997 if( !fn.IsDirWritable() )
2998 {
2999 THROW_IO_ERROR( wxString::Format( _( "Insufficient permissions to delete folder '%s'." ),
3000 aLibraryPath.GetData() ) );
3001 }
3002
3003 wxDir dir( aLibraryPath );
3004
3005 if( dir.HasSubDirs() )
3006 {
3007 THROW_IO_ERROR( wxString::Format( _( "Library folder '%s' has unexpected sub-folders." ),
3008 aLibraryPath.GetData() ) );
3009 }
3010
3011 // All the footprint files must be deleted before the directory can be deleted.
3012 if( dir.HasFiles() )
3013 {
3014 unsigned i;
3015 wxFileName tmp;
3016 wxArrayString files;
3017
3018 wxDir::GetAllFiles( aLibraryPath, &files );
3019
3020 for( i = 0; i < files.GetCount(); i++ )
3021 {
3022 tmp = files[i];
3023
3024 if( tmp.GetExt() != FILEEXT::KiCadFootprintFileExtension )
3025 {
3026 THROW_IO_ERROR( wxString::Format( _( "Unexpected file '%s' found in library "
3027 "path '%s'." ),
3028 files[i].GetData(),
3029 aLibraryPath.GetData() ) );
3030 }
3031 }
3032
3033 for( i = 0; i < files.GetCount(); i++ )
3034 wxRemoveFile( files[i] );
3035 }
3036
3037 wxLogTrace( traceKicadPcbPlugin, wxT( "Removing footprint library '%s'." ),
3038 aLibraryPath.GetData() );
3039
3040 // Some of the more elaborate wxRemoveFile() crap puts up its own wxLog dialog
3041 // we don't want that. we want bare metal portability with no UI here.
3042 if( !wxRmdir( aLibraryPath ) )
3043 {
3044 THROW_IO_ERROR( wxString::Format( _( "Footprint library '%s' cannot be deleted." ),
3045 aLibraryPath.GetData() ) );
3046 }
3047
3048 // For some reason removing a directory in Windows is not immediately updated. This delay
3049 // prevents an error when attempting to immediately recreate the same directory when over
3050 // writing an existing library.
3051#ifdef __WINDOWS__
3052 wxMilliSleep( 250L );
3053#endif
3054
3055 if( m_cache && !m_cache->IsPath( aLibraryPath ) )
3056 {
3057 delete m_cache;
3058 m_cache = nullptr;
3059 }
3060
3061 return true;
3062}
3063
3064
3065bool PCB_IO_KICAD_SEXPR::IsLibraryWritable( const wxString& aLibraryPath )
3066{
3067 LOCALE_IO toggle;
3068
3069 init( nullptr );
3070
3071 validateCache( aLibraryPath );
3072
3073 return m_cache->IsWritable();
3074}
constexpr std::size_t arrayDim(T const (&)[N]) noexcept
Returns # of elements in an array.
Definition: arraydim.h:31
constexpr EDA_IU_SCALE pcbIUScale
Definition: base_units.h:108
KIFACE_BASE & Kiface()
Global KIFACE_BASE "get" accessor.
@ ZLO_FORCE_FLASHED
Definition: board_item.h:68
wxString GetMajorMinorVersion()
Get only the major and minor version in a string major.minor.
static const ADVANCED_CFG & GetCfg()
Get the singleton instance's config, which is shared by all consumers.
bool any() const
Definition: base_set.h:55
size_t count() const
Definition: base_set.h:106
double GetScale() const
Definition: bitmap_base.h:72
const wxMemoryBuffer & GetImageDataBuffer() const
Definition: bitmap_base.h:240
wxImage * GetImageData()
Definition: bitmap_base.h:67
TEARDROP_PARAMETERS & GetTeardropParams()
Container for design settings for a BOARD object.
int GetBoardThickness() const
The full thickness of the board including copper and masks.
const VECTOR2I & GetGridOrigin()
const VECTOR2I & GetAuxOrigin()
BOARD_STACKUP & GetStackupDescriptor()
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:79
virtual PCB_LAYER_ID GetLayer() const
Return the primary layer this item is on.
Definition: board_item.h:240
void SetParentGroup(PCB_GROUP *aGroup)
Definition: board_item.h:92
virtual bool IsKnockout() const
Definition: board_item.h:312
virtual const BOARD * GetBoard() const
Return the BOARD in which this BOARD_ITEM resides, or NULL if none.
Definition: board_item.cpp:47
FOOTPRINT * GetParentFootprint() const
Definition: board_item.cpp:264
VECTOR2I GetFPRelativePosition() const
Definition: board_item.cpp:293
virtual bool IsLocked() const
Definition: board_item.cpp:75
Manage layers needed to make a physical board.
void FormatBoardStackup(OUTPUTFORMATTER *aFormatter, const BOARD *aBoard, int aNestLevel) const
Write the stackup info on board file.
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:289
LSET GetEnabledLayers() const
A proxy function that calls the corresponding function in m_BoardSettings.
Definition: board.cpp:757
EMBEDDED_FILES * GetEmbeddedFiles() override
Definition: board.cpp:2523
const GENERATORS & Generators() const
Definition: board.h:336
void SetFileName(const wxString &aFileName)
Definition: board.h:324
const PAGE_INFO & GetPageSettings() const
Definition: board.h:682
const ZONES & Zones() const
Definition: board.h:334
const GROUPS & Groups() const
The groups must maintain the following invariants.
Definition: board.h:357
LAYER_T GetLayerType(PCB_LAYER_ID aLayer) const
Return the type of the copper layer given by aLayer.
Definition: board.cpp:610
TITLE_BLOCK & GetTitleBlock()
Definition: board.h:688
const std::map< wxString, wxString > & GetProperties() const
Definition: board.h:361
const FOOTPRINTS & Footprints() const
Definition: board.h:330
const TRACKS & Tracks() const
Definition: board.h:328
const PCB_PLOT_PARAMS & GetPlotOptions() const
Definition: board.h:685
const wxString GetLayerName(PCB_LAYER_ID aLayer) const
Return the name of a aLayer.
Definition: board.cpp:575
bool LegacyTeardrops() const
Definition: board.h:1254
wxString GroupsSanityCheck(bool repair=false)
Consistency check of internal m_groups structure.
Definition: board.cpp:2726
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Definition: board.cpp:874
void EmbedFonts() override
Finds all fonts used in the board and embeds them in the file if permissions allow.
Definition: board.cpp:2541
const DRAWINGS & Drawings() const
Definition: board.h:332
double AsDegrees() const
Definition: eda_angle.h:113
bool IsZero() const
Definition: eda_angle.h:133
EDA_ANGLE Normalize720()
Definition: eda_angle.h:271
const KIID m_Uuid
Definition: eda_item.h:489
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:101
virtual void SetParent(EDA_ITEM *aParent)
Definition: eda_item.h:104
virtual wxString GetClass() const =0
Return the class name.
const VECTOR2I & GetBezierC2() const
Definition: eda_shape.h:206
SHAPE_POLY_SET & GetPolyShape()
Definition: eda_shape.h:279
bool IsFilled() const
Definition: eda_shape.h:91
SHAPE_T GetShape() const
Definition: eda_shape.h:125
const VECTOR2I & GetEnd() const
Return the ending point of the graphic.
Definition: eda_shape.h:167
const VECTOR2I & GetStart() const
Return the starting point of the graphic.
Definition: eda_shape.h:130
wxString SHAPE_T_asString() const
Definition: eda_shape.cpp:89
const VECTOR2I & GetBezierC1() const
Definition: eda_shape.h:203
bool IsPolyShapeValid() const
Definition: eda_shape.cpp:1356
VECTOR2I GetArcMid() const
Definition: eda_shape.cpp:582
A mix-in class (via multiple inheritance) that handles texts such as labels, parts,...
Definition: eda_text.h:79
const VECTOR2I & GetTextPos() const
Definition: eda_text.h:230
const EDA_ANGLE & GetTextAngle() const
Definition: eda_text.h:130
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition: eda_text.h:94
bool IsKeepUpright() const
Definition: eda_text.h:166
virtual bool IsVisible() const
Definition: eda_text.h:147
KIFONT::FONT * GetFont() const
Definition: eda_text.h:207
std::vector< std::unique_ptr< KIFONT::GLYPH > > * GetRenderCache(const KIFONT::FONT *aFont, const wxString &forResolvedText, const VECTOR2I &aOffset={ 0, 0 }) const
Definition: eda_text.cpp:511
virtual EDA_ANGLE GetDrawRotation() const
Definition: eda_text.h:336
virtual wxString GetShownText(bool aAllowExtraText, int aDepth=0) const
Return the string actually shown after processing of the base text.
Definition: eda_text.h:105
int GetTextThickness() const
Definition: eda_text.h:122
bool IsEmpty() const
void WriteEmbeddedFiles(OUTPUTFORMATTER &aOut, int aNestLevel, bool aWriteData) const
Output formatter for the embedded files.
void ClearEmbeddedFiles(bool aDeleteFiles=true)
void ClearEmbeddedFonts()
Removes all embedded fonts from the collection.
EMBEDDED_FILE * AddFile(const wxFileName &aName, bool aOverwrite)
Loads a file from disk and adds it to the collection.
const std::map< wxString, EMBEDDED_FILE * > & EmbeddedFileMap() const
bool GetAreFontsEmbedded() const
A LINE_READER that reads from an open file.
Definition: richio.h:185
void Rewind()
Rewind the file and resets the line number back to zero.
Definition: richio.h:234
char * ReadLine() override
Read a line of text into the buffer and increments the line number counter.
Definition: richio.cpp:244
void EmbedFonts() override
Definition: footprint.cpp:3714
void SetFPID(const LIB_ID &aFPID)
Definition: footprint.h:238
wxString GetLibDescription() const
Definition: footprint.h:246
ZONE_CONNECTION GetLocalZoneConnection() const
Definition: footprint.h:277
EDA_ANGLE GetOrientation() const
Definition: footprint.h:216
ZONES & Zones()
Definition: footprint.h:201
void SetOrientation(const EDA_ANGLE &aNewAngle)
Definition: footprint.cpp:2415
wxString GetSheetname() const
Definition: footprint.h:255
std::optional< int > GetLocalSolderPasteMargin() const
Definition: footprint.h:270
EDA_ITEM * Clone() const override
Invoke a function on all children.
Definition: footprint.cpp:2032
PCB_FIELD & Value()
read/write accessors:
Definition: footprint.h:627
std::optional< int > GetLocalClearance() const
Definition: footprint.h:264
BOARD_ITEM * Duplicate() const override
Create a copy of this BOARD_ITEM.
Definition: footprint.cpp:2442
int GetAttributes() const
Definition: footprint.h:279
PCB_LAYER_ID GetLayer() const override
Return the primary layer this item is on.
Definition: footprint.h:225
LSET GetPrivateLayers() const
Definition: footprint.h:130
wxString GetSheetfile() const
Definition: footprint.h:258
PADS & Pads()
Definition: footprint.h:195
const std::vector< wxString > & GetNetTiePadGroups() const
Definition: footprint.h:328
const LIB_ID & GetFPID() const
Definition: footprint.h:237
bool IsLocked() const override
Definition: footprint.h:400
void GetFields(std::vector< PCB_FIELD * > &aVector, bool aVisibleOnly)
Populate a std::vector with PCB_TEXTs.
Definition: footprint.cpp:575
PCB_FIELD & Reference()
Definition: footprint.h:628
bool IsNetTie() const
Definition: footprint.h:286
std::optional< double > GetLocalSolderPasteMarginRatio() const
Definition: footprint.h:273
GROUPS & Groups()
Definition: footprint.h:204
wxString GetFilters() const
Definition: footprint.h:261
const wxArrayString * GetInitialComments() const
Return the initial comments block or NULL if none, without transfer of ownership.
Definition: footprint.h:944
std::vector< FP_3DMODEL > & Models()
Definition: footprint.h:209
void Flip(const VECTOR2I &aCentre, bool aFlipLeftRight) override
Flip this object, i.e.
Definition: footprint.cpp:2284
const KIID_PATH & GetPath() const
Definition: footprint.h:252
std::optional< int > GetLocalSolderMaskMargin() const
Definition: footprint.h:267
wxString GetKeywords() const
Definition: footprint.h:249
EMBEDDED_FILES * GetEmbeddedFiles() override
Definition: footprint.h:972
bool IsPlaced() const
Definition: footprint.h:423
VECTOR2I GetPosition() const override
Definition: footprint.h:213
DRAWINGS & GraphicalItems()
Definition: footprint.h:198
Helper class for creating a footprint library cache.
FP_CACHE_ITEM(FOOTPRINT *aFootprint, const WX_FILENAME &aFileName)
static long long GetTimestamp(const wxString &aLibPath)
Generate a timestamp representing all source files in the cache (including the parent directory).
FP_CACHE_FOOTPRINT_MAP & GetFootprints()
bool Exists() const
void Save(FOOTPRINT *aFootprint=nullptr)
Save the footprint cache or a single footprint from it to disk.
PCB_IO_KICAD_SEXPR * m_owner
bool IsModified()
Return true if the cache is not up-to-date.
long long m_cache_timestamp
wxString m_lib_raw_path
void SetPath(const wxString &aPath)
wxFileName m_lib_path
bool IsPath(const wxString &aPath) const
Check if aPath is the same as the current cache path.
FP_CACHE_FOOTPRINT_MAP m_footprints
FP_CACHE(PCB_IO_KICAD_SEXPR *aOwner, const wxString &aLibraryPath)
bool IsWritable() const
void Remove(const wxString &aFootprintName)
PROGRESS_REPORTER * m_progressReporter
Progress reporter to track the progress of the operation, may be nullptr.
Definition: io_base.h:218
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:77
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:30
APP_SETTINGS_BASE * KifaceSettings() const
Definition: kiface_base.h:95
virtual bool IsOutline() const
Definition: font.h:139
virtual void SetLineWidth(float aLineWidth)
Set the line width.
wxString AsString() const
Definition: kiid.cpp:356
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:49
UTF8 Format() const
Definition: lib_id.cpp:118
const UTF8 & GetLibItemName() const
Definition: lib_id.h:102
An abstract class from which implementation specific LINE_READERs may be derived to read single lines...
Definition: richio.h:93
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition: locale_io.h:49
LSET is a set of PCB_LAYER_IDs.
Definition: lset.h:35
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:392
LSEQ CuStack() const
Return a sequence of copper layers in starting from the front/top and extending to the back/bottom.
Definition: lset.cpp:151
static LSET AllCuMask(int aCuLayerCount=MAX_CU_LAYERS)
Return a mask holding the requested number of Cu PCB_LAYER_IDs.
Definition: lset.cpp:732
static const wxChar * Name(PCB_LAYER_ID aLayerId)
Return the fixed name association with aLayerId.
Definition: lset.cpp:63
Handle the data for a net.
Definition: netinfo.h:56
static const int UNCONNECTED
Constant that holds the "unconnected net" number (typically 0) all items "connected" to this net are ...
Definition: netinfo.h:381
void SetBoard(const BOARD *aBoard)
Set a BOARD object that is used to prepare the net code map.
Definition: netinfo.h:223
int Translate(int aNetCode) const
Translate net number according to the map prepared by Update() function.
virtual bool Finish()
Performs any cleanup needed at the end of a write.
Definition: richio.h:415
std::string Quotew(const wxString &aWrapee) const
Definition: richio.cpp:526
int PRINTF_FUNC Print(int nestLevel, const char *fmt,...)
Format and write text to the output stream.
Definition: richio.cpp:458
virtual std::string Quotes(const std::string &aWrapee) const
Check aWrapee input string for a need to be quoted (e.g.
Definition: richio.cpp:487
Definition: pad.h:54
PAD_PROP GetProperty() const
Definition: pad.h:398
bool GetRemoveUnconnected() const
Definition: pad.h:644
LSET GetLayerSet() const override
Return a std::bitset of all layers on which the item physically resides.
Definition: pad.h:392
const ZONE_LAYER_OVERRIDE & GetZoneLayerOverride(PCB_LAYER_ID aLayer) const
Definition: pad.h:817
std::optional< double > GetLocalSolderPasteMarginRatio() const
Definition: pad.h:425
const wxString & GetPinType() const
Definition: pad.h:151
const VECTOR2I & GetDrillSize() const
Definition: pad.h:267
PAD_ATTRIB GetAttribute() const
Definition: pad.h:395
const wxString & GetPinFunction() const
Definition: pad.h:145
const wxString & GetNumber() const
Definition: pad.h:134
const std::vector< std::shared_ptr< PCB_SHAPE > > & GetPrimitives() const
Accessor to the basic shape list for custom-shaped pads.
Definition: pad.h:318
EDA_ANGLE GetThermalSpokeAngle() const
Definition: pad.h:568
const VECTOR2I & GetOffset() const
Definition: pad.h:274
bool GetKeepTopBottom() const
Definition: pad.h:660
std::optional< int > GetLocalClearance() const override
Return any local clearances set in the "classic" (ie: pre-rule) system.
Definition: pad.h:410
const VECTOR2I & GetDelta() const
Definition: pad.h:264
PAD_SHAPE GetShape() const
Definition: pad.h:193
EDA_ANGLE GetOrientation() const
Return the rotation angle of the pad.
Definition: pad.h:359
PADSTACK::CUSTOM_SHAPE_ZONE_MODE GetCustomShapeInZoneOpt() const
Definition: pad.h:214
PAD_DRILL_SHAPE GetDrillShape() const
Definition: pad.h:377
std::optional< int > GetLocalSolderPasteMargin() const
Definition: pad.h:419
std::optional< int > GetLocalSolderMaskMargin() const
Definition: pad.h:413
int GetThermalSpokeWidth() const
Definition: pad.h:555
int GetChamferPositions() const
Definition: pad.h:624
ZONE_CONNECTION GetLocalZoneConnection() const
Definition: pad.h:435
double GetRoundRectRadiusRatio() const
Definition: pad.h:605
int GetThermalGap() const
Definition: pad.h:584
const VECTOR2I & GetSize() const
Definition: pad.h:256
PAD_SHAPE GetAnchorPadShape() const
Definition: pad.h:206
double GetChamferRectRatio() const
Definition: pad.h:614
int GetPadToDieLength() const
Definition: pad.h:408
void Format(OUTPUTFORMATTER *aFormatter, int aNestLevel, int aControlBits) const
Output the page class to aFormatter in s-expression form.
Definition: page_info.cpp:275
const VECTOR2I & GetMid() const
Definition: pcb_track.h:281
Abstract dimension API.
wxString GetOverrideText() const
wxString GetSuffix() const
int GetLineThickness() const
DIM_TEXT_POSITION GetTextPositionMode() const
bool GetKeepTextAligned() const
DIM_PRECISION GetPrecision() const
wxString GetPrefix() const
DIM_UNITS_MODE GetUnitsMode() const
DIM_UNITS_FORMAT GetUnitsFormat() const
virtual const VECTOR2I & GetStart() const
The dimension's origin is the first feature point for the dimension.
bool GetSuppressZeroes() const
int GetExtensionOffset() const
int GetArrowLength() const
bool GetOverrideTextEnabled() const
virtual const VECTOR2I & GetEnd() const
For better understanding of the points that make a dimension:
int GetHeight() const
int GetExtensionHeight() const
Mark the center of a circle or arc with a cross shape.
A leader is a dimension-like object pointing to a specific point.
DIM_TEXT_BORDER GetTextBorder() const
An orthogonal dimension is like an aligned dimension, but the extension lines are locked to the X or ...
A radial dimension indicates either the radius or diameter of an arc or circle.
int GetLeaderLength() const
virtual const STRING_ANY_MAP GetProperties() const
virtual wxString GetGeneratorType() const
A set of BOARD_ITEMs (i.e., without duplicates).
Definition: pcb_group.h:52
std::unordered_set< BOARD_ITEM * > & GetItems()
Definition: pcb_group.h:69
wxString GetName() const
Definition: pcb_group.h:66
Read a Pcbnew s-expression formatted LINE_READER object and returns the appropriate BOARD_ITEM object...
bool IsTooRecent()
Return whether a version number, if any was parsed, was too recent.
bool IsValidBoardHeader()
Partially parse the input and check if it matches expected header.
wxString GetRequiredVersion()
Return a string representing the version of KiCad required to open this file.
A #PLUGIN derivation for saving and loading Pcbnew s-expression formatted files.
bool CanReadBoard(const wxString &aFileName) const override
Checks if this PCB_IO can read the specified board file.
void formatRenderCache(const EDA_TEXT *aText, int aNestLevel) const
long long GetLibraryTimestamp(const wxString &aLibraryPath) const override
Generate a timestamp representing all the files in the library (including the library directory).
bool IsLibraryWritable(const wxString &aLibraryPath) override
Return true if the library at aLibraryPath is writable.
void SaveBoard(const wxString &aFileName, BOARD *aBoard, const STRING_UTF8_MAP *aProperties=nullptr) override
Write aBoard to a storage file in a format that this PCB_IO implementation knows about or it can be u...
NETINFO_MAPPING * m_mapping
mapping for net codes, so only not empty net codes are stored with consecutive integers as net codes
const FOOTPRINT * GetEnumeratedFootprint(const wxString &aLibraryPath, const wxString &aFootprintName, const STRING_UTF8_MAP *aProperties=nullptr) override
A version of FootprintLoad() for use after FootprintEnumerate() for more efficient cache management.
void init(const STRING_UTF8_MAP *aProperties)
void formatLayers(LSET aLayerMask, int aNestLevel=0) const
FOOTPRINT * FootprintLoad(const wxString &aLibraryPath, const wxString &aFootprintName, bool aKeepUUID=false, const STRING_UTF8_MAP *aProperties=nullptr) override
Load a footprint having aFootprintName from the aLibraryPath containing a library format that this PC...
const FOOTPRINT * getFootprint(const wxString &aLibraryPath, const wxString &aFootprintName, const STRING_UTF8_MAP *aProperties, bool checkModified)
BOARD * LoadBoard(const wxString &aFileName, BOARD *aAppendToMe, const STRING_UTF8_MAP *aProperties=nullptr, PROJECT *aProject=nullptr) override
Load information from some input file format that this PCB_IO implementation knows about into either ...
void formatSetup(const BOARD *aBoard, int aNestLevel=0) const
formats the board setup information
void FootprintEnumerate(wxArrayString &aFootprintNames, const wxString &aLibraryPath, bool aBestEfforts, const STRING_UTF8_MAP *aProperties=nullptr) override
Return a list of footprint names contained within the library at aLibraryPath.
FP_CACHE * m_cache
Footprint library cache.
void formatPolyPts(const SHAPE_LINE_CHAIN &outline, int aNestLevel, bool aCompact, const FOOTPRINT *aParentFP=nullptr) const
void formatHeader(const BOARD *aBoard, int aNestLevel=0) const
writes everything that comes before the board_items, like settings and layers etc
void formatNetInformation(const BOARD *aBoard, int aNestLevel=0) const
formats the Nets and Netclasses
void formatProperties(const BOARD *aBoard, int aNestLevel=0) const
formats the Nets and Netclasses
void FootprintSave(const wxString &aLibraryPath, const FOOTPRINT *aFootprint, const STRING_UTF8_MAP *aProperties=nullptr) override
Write aFootprint to an existing library located at aLibraryPath.
void formatGeneral(const BOARD *aBoard, int aNestLevel=0) const
formats the General section of the file
FOOTPRINT * ImportFootprint(const wxString &aFootprintPath, wxString &aFootprintNameOut, const STRING_UTF8_MAP *aProperties=nullptr) override
Load a single footprint from aFootprintPath and put its name in aFootprintNameOut.
void Format(const BOARD_ITEM *aItem, int aNestLevel=0) const
Output aItem to aFormatter in s-expression format.
BOARD * DoLoad(LINE_READER &aReader, BOARD *aAppendToMe, const STRING_UTF8_MAP *aProperties, PROGRESS_REPORTER *aProgressReporter, unsigned aLineCount)
bool FootprintExists(const wxString &aLibraryPath, const wxString &aFootprintName, const STRING_UTF8_MAP *aProperties=nullptr) override
Check for the existence of a footprint.
void format(const BOARD *aBoard, int aNestLevel=0) const
std::function< bool(wxString aTitle, int aIcon, wxString aMsg, wxString aAction)> m_queryUserCallback
BOARD_ITEM * Parse(const wxString &aClipboardSourceInput)
void formatBoardLayers(const BOARD *aBoard, int aNestLevel=0) const
formats the board layer information
void FootprintDelete(const wxString &aLibraryPath, const wxString &aFootprintName, const STRING_UTF8_MAP *aProperties=nullptr) override
Delete aFootprintName from the library at aLibraryPath.
STRING_FORMATTER m_sf
void formatTeardropParameters(const TEARDROP_PARAMETERS &tdParams, int aNestLevel=0) const
void formatLayer(PCB_LAYER_ID aLayer, bool aIsKnockout=false) const
void SetOutputFormatter(OUTPUTFORMATTER *aFormatter)
PCB_IO_KICAD_SEXPR(int aControlFlags=CTL_FOR_BOARD)
OUTPUTFORMATTER * m_out
output any Format()s to this, no ownership
void validateCache(const wxString &aLibraryPath, bool checkModified=true)
LINE_READER * m_reader
no ownership
void CreateLibrary(const wxString &aLibraryPath, const STRING_UTF8_MAP *aProperties=nullptr) override
Create a new empty library at aLibraryPath empty.
bool DeleteLibrary(const wxString &aLibraryPath, const STRING_UTF8_MAP *aProperties=nullptr) override
Delete an existing library and returns true, or if library does not exist returns false,...
A base class that BOARD loading and saving plugins should derive from.
Definition: pcb_io.h:72
BOARD * m_board
The board BOARD being worked on, no ownership here.
Definition: pcb_io.h:343
const STRING_UTF8_MAP * m_props
Properties passed via Save() or Load(), no ownership, may be NULL.
Definition: pcb_io.h:346
virtual bool CanReadBoard(const wxString &aFileName) const
Checks if this PCB_IO can read the specified board file.
Definition: pcb_io.cpp:43
void Format(OUTPUTFORMATTER *aFormatter, int aNestLevel, int aControl=0) const
Object to handle a bitmap image that can be inserted in a PCB.
VECTOR2I GetPosition() const override
const BITMAP_BASE * GetImage() const
STROKE_PARAMS GetStroke() const override
Definition: pcb_shape.h:85
PCB_LAYER_ID GetLayer() const override
Return the primary layer this item is on.
Definition: pcb_shape.h:70
bool StrokeRows() const
Definition: pcb_table.h:86
int GetRowCount() const
Definition: pcb_table.h:106
bool StrokeColumns() const
Definition: pcb_table.h:83
bool StrokeExternal() const
Definition: pcb_table.h:53
bool StrokeHeader() const
Definition: pcb_table.h:56
std::vector< PCB_TABLECELL * > GetCells() const
Definition: pcb_table.h:141
int GetColCount() const
Definition: pcb_table.h:104
const STROKE_PARAMS & GetSeparatorsStroke() const
Definition: pcb_table.h:71
const STROKE_PARAMS & GetBorderStroke() const
Definition: pcb_table.h:59
int GetColWidth(int aCol) const
Definition: pcb_table.h:113
EDA_ANGLE GetOrientation() const
Definition: pcb_table.h:101
int GetRowHeight(int aRow) const
Definition: pcb_table.h:123
int GetShape() const
Definition: pcb_target.h:58
int GetWidth() const
Definition: pcb_target.h:64
int GetSize() const
Definition: pcb_target.h:61
VECTOR2I GetPosition() const override
Definition: pcb_target.h:55
bool IsBorderEnabled() const
Disables the border, this is done by changing the stroke internally.
int GetMarginBottom() const
Definition: pcb_textbox.h:87
int GetMarginLeft() const
Definition: pcb_textbox.h:84
int GetMarginRight() const
Definition: pcb_textbox.h:86
int GetMarginTop() const
Definition: pcb_textbox.h:85
int GetWidth() const
Definition: pcb_track.h:116
const VECTOR2I & GetStart() const
Definition: pcb_track.h:122
const VECTOR2I & GetEnd() const
Definition: pcb_track.h:119
A progress reporter interface for use in multi-threaded environments.
virtual bool KeepRefreshing(bool aWait=false)=0
Update the UI (if any).
virtual void Report(const wxString &aMessage)=0
Display aMessage in the progress bar dialog.
Container for project specific data.
Definition: project.h:62
const VECTOR2I & GetArcMid() const
Definition: shape_arc.h:115
const VECTOR2I & GetP1() const
Definition: shape_arc.h:114
const VECTOR2I & GetP0() const
Definition: shape_arc.h:113
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
const SHAPE_ARC & Arc(size_t aArc) const
int PointCount() const
Return the number of points (vertices) in this line chain.
ssize_t ArcIndex(size_t aSegment) const
Return the arc index for the given segment index.
const VECTOR2I & CPoint(int aIndex) const
Return a reference to a given point in the line chain.
const std::vector< VECTOR2I > & CPoints() const
Represent a set of closed polygons.
POLYGON & Polygon(int aIndex)
Return the aIndex-th subpolygon in the set.
std::vector< SHAPE_LINE_CHAIN > POLYGON
represents a single polygon outline with holes.
SHAPE_LINE_CHAIN & Outline(int aIndex)
Return the reference to aIndex-th outline in the set.
Is a LINE_READER that reads from a multiline 8 bit wide std::string.
Definition: richio.h:253
A name/value tuple with unique names and optional values.
void Format(OUTPUTFORMATTER *out, const EDA_IU_SCALE &aIuScale, int nestLevel) const
TEARDROP_PARAMETARS is a helper class to handle parameters needed to build teardrops for a board thes...
int m_CurveSegCount
True if the teardrop should be curved.
double m_BestWidthRatio
The height of a teardrop as ratio between height and size of pad/via.
int m_TdMaxLen
max allowed length for teardrops in IU. <= 0 to disable
bool m_AllowUseTwoTracks
True to create teardrops using 2 track segments if the first in too small.
int m_TdMaxWidth
max allowed height for teardrops in IU. <= 0 to disable
double m_BestLengthRatio
The length of a teardrop as ratio between length and size of pad/via.
double m_WidthtoSizeFilterRatio
The ratio (H/D) between the via/pad size and the track width max value to create a teardrop 1....
bool m_TdOnPadsInZones
A filter to exclude pads inside zone fills.
bool m_Enabled
Flag to enable teardrops.
virtual void Format(OUTPUTFORMATTER *aFormatter, int aNestLevel, int aControlBits) const
Output the object to aFormatter in s-expression form.
Definition: title_block.cpp:30
wxString wx_str() const
Definition: utf8.cpp:45
static REPORTER & GetInstance()
Definition: reporter.cpp:212
A wrapper around a wxFileName which is much more performant with a subset of the API.
Definition: wx_filename.h:50
void SetFullName(const wxString &aFileNameAndExtension)
Definition: wx_filename.cpp:34
static void ResolvePossibleSymlinks(wxFileName &aFilename)
Definition: wx_filename.cpp:92
wxString GetPath() const
Definition: wx_filename.cpp:60
wxString GetName() const
Definition: wx_filename.cpp:47
wxString GetFullPath() const
Definition: wx_filename.cpp:66
long long GetTimestamp()
Definition: wx_filename.cpp:81
Handle a list of polygons defining a copper zone.
Definition: zone.h:73
int GetHatchBorderAlgorithm() const
Definition: zone.h:303
bool GetIsRuleArea() const
Accessors to parameters used in Rule Area zones:
Definition: zone.h:711
std::optional< int > GetLocalClearance() const override
Definition: zone.cpp:493
bool GetDoNotAllowVias() const
Definition: zone.h:713
const std::shared_ptr< SHAPE_POLY_SET > & GetFilledPolysList(PCB_LAYER_ID aLayer) const
Definition: zone.h:616
bool GetDoNotAllowPads() const
Definition: zone.h:715
bool GetDoNotAllowTracks() const
Definition: zone.h:714
bool IsFilled() const
Definition: zone.h:261
ISLAND_REMOVAL_MODE GetIslandRemovalMode() const
Definition: zone.h:725
SHAPE_POLY_SET * Outline()
Definition: zone.h:337
bool IsIsland(PCB_LAYER_ID aLayer, int aPolyIdx) const
Check if a given filled polygon is an insulated island.
Definition: zone.cpp:1091
long long int GetMinIslandArea() const
Definition: zone.h:728
const wxString & GetZoneName() const
Definition: zone.h:132
int GetMinThickness() const
Definition: zone.h:270
ZONE_CONNECTION GetPadConnection() const
Definition: zone.h:267
int GetHatchThickness() const
Definition: zone.h:285
double GetHatchHoleMinArea() const
Definition: zone.h:300
bool IsTeardropArea() const
Definition: zone.h:695
int GetThermalReliefSpokeWidth() const
Definition: zone.h:214
int GetBorderHatchPitch() const
HatchBorder related methods.
Definition: zone.cpp:872
ZONE_BORDER_DISPLAY_STYLE GetHatchStyle() const
Definition: zone.h:605
EDA_ANGLE GetHatchOrientation() const
Definition: zone.h:291
bool GetDoNotAllowFootprints() const
Definition: zone.h:716
ZONE_FILL_MODE GetFillMode() const
Definition: zone.h:193
virtual LSET GetLayerSet() const override
Return a std::bitset of all layers on which the item physically resides.
Definition: zone.h:130
bool GetDoNotAllowCopperPour() const
Definition: zone.h:712
int GetHatchGap() const
Definition: zone.h:288
TEARDROP_TYPE GetTeardropAreaType() const
Definition: zone.h:706
double GetHatchSmoothingValue() const
Definition: zone.h:297
int GetHatchSmoothingLevel() const
Definition: zone.h:294
unsigned int GetCornerRadius() const
Definition: zone.h:666
int GetCornerSmoothingType() const
Definition: zone.h:662
bool IsOnCopperLayer() const override
Definition: zone.cpp:257
PCB_LAYER_ID GetFirstLayer() const
Definition: zone.cpp:248
int GetThermalReliefGap() const
Definition: zone.h:203
unsigned GetAssignedPriority() const
Definition: zone.h:120
int GetNumCorners(void) const
Access to m_Poly parameters.
Definition: zone.h:515
static void SetReporter(REPORTER *aReporter)
Set the reporter to use for reporting font substitution warnings.
Definition: fontconfig.cpp:64
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:599
This file is part of the common library.
#define CTL_OMIT_HYPERLINK
Omit the hyperlink attribute in .kicad_xxx files.
Definition: ctl_flags.h:47
#define CTL_OMIT_UUIDS
Omit component unique ids (useless in library)
Definition: ctl_flags.h:30
#define CTL_OMIT_FOOTPRINT_VERSION
Omit the version string from the (footprint) sexpr group.
Definition: ctl_flags.h:41
#define CTL_OMIT_INITIAL_COMMENTS
omit FOOTPRINT initial comments
Definition: ctl_flags.h:44
#define CTL_OMIT_LIBNAME
Omit lib alias when saving (used for board/not library).
Definition: ctl_flags.h:39
#define CTL_OMIT_PATH
Omit component sheet time stamp (useless in library)
Definition: ctl_flags.h:32
#define CTL_OMIT_HIDE
Omit the hide attribute in .kicad_xxx files.
Definition: ctl_flags.h:37
#define CTL_OMIT_AT
Omit position and rotation.
Definition: ctl_flags.h:33
#define CTL_OMIT_PAD_NETS
Omit pads net names (useless in library)
Definition: ctl_flags.h:29
#define CTL_OMIT_COLOR
Omit the color attribute in .kicad_xxx files.
Definition: ctl_flags.h:46
#define MIME_BASE64_LENGTH
#define _(s)
static constexpr EDA_ANGLE ANGLE_0
Definition: eda_angle.h:401
static constexpr EDA_ANGLE ANGLE_90
Definition: eda_angle.h:403
static constexpr EDA_ANGLE ANGLE_45
Definition: eda_angle.h:402
@ FP_SMD
Definition: footprint.h:75
@ FP_DNP
Definition: footprint.h:82
@ FP_ALLOW_MISSING_COURTYARD
Definition: footprint.h:81
@ FP_EXCLUDE_FROM_POS_FILES
Definition: footprint.h:76
@ FP_BOARD_ONLY
Definition: footprint.h:78
@ FP_EXCLUDE_FROM_BOM
Definition: footprint.h:77
@ FP_THROUGH_HOLE
Definition: footprint.h:74
@ FP_ALLOW_SOLDERMASK_BRIDGES
Definition: footprint.h:80
static const std::string KiCadFootprintFileExtension
const wxChar *const traceKicadPcbPlugin
Flag to enable GEDA PCB plugin debug output.
#define THROW_IO_ERROR(msg)
Definition: ki_exception.h:39
#define THROW_PARSE_ERROR(aProblem, aSource, aInputLine, aLineNumber, aByteIndex)
Definition: ki_exception.h:165
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:60
@ User_8
Definition: layer_ids.h:130
@ F_CrtYd
Definition: layer_ids.h:117
@ B_Adhes
Definition: layer_ids.h:97
@ Edge_Cuts
Definition: layer_ids.h:113
@ Dwgs_User
Definition: layer_ids.h:109
@ F_Paste
Definition: layer_ids.h:101
@ Cmts_User
Definition: layer_ids.h:110
@ User_6
Definition: layer_ids.h:128
@ User_7
Definition: layer_ids.h:129
@ F_Adhes
Definition: layer_ids.h:98
@ B_Mask
Definition: layer_ids.h:106
@ B_Cu
Definition: layer_ids.h:95
@ User_5
Definition: layer_ids.h:127
@ Eco1_User
Definition: layer_ids.h:111
@ F_Mask
Definition: layer_ids.h:107
@ B_Paste
Definition: layer_ids.h:100
@ User_9
Definition: layer_ids.h:131
@ F_Fab
Definition: layer_ids.h:120
@ Margin
Definition: layer_ids.h:114
@ F_SilkS
Definition: layer_ids.h:104
@ B_CrtYd
Definition: layer_ids.h:116
@ Eco2_User
Definition: layer_ids.h:112
@ User_3
Definition: layer_ids.h:125
@ User_1
Definition: layer_ids.h:123
@ B_SilkS
Definition: layer_ids.h:103
@ User_4
Definition: layer_ids.h:126
@ PCB_LAYER_ID_COUNT
Definition: layer_ids.h:137
@ User_2
Definition: layer_ids.h:124
@ F_Cu
Definition: layer_ids.h:64
@ B_Fab
Definition: layer_ids.h:119
This file contains miscellaneous commonly used macros and functions.
#define UNIMPLEMENTED_FOR(type)
Definition: macros.h:96
KICOMMON_API std::string FormatInternalUnits(const EDA_IU_SCALE &aIuScale, int aValue)
Converts aValue from internal units to a string appropriate for writing to file.
Definition: eda_units.cpp:169
KICOMMON_API std::string FormatAngle(const EDA_ANGLE &aAngle)
Converts aAngle from board units to a string appropriate for writing to file.
Definition: eda_units.cpp:161
void FormatBool(OUTPUTFORMATTER *aOut, int aNestLevel, const wxString &aKey, bool aValue, char aSuffix)
Writes a boolean to the formatter, in the style (aKey [yes|no])
void FormatUuid(OUTPUTFORMATTER *aOut, const KIID &aUuid, char aSuffix)
bool DuplicatePermissions(const wxString &aSrc, const wxString &aDest)
Duplicates the file security data from one file to another ensuring that they are the same between bo...
Definition: unix/io.cpp:40
Class to handle a set of BOARD_ITEMs.
bool isDefaultTeardropParameters(const TEARDROP_PARAMETERS &tdParams)
std::string formatInternalUnits(int aValue)
#define SEXPR_BOARD_FILE_VERSION
Current s-expression file format version. 2 was the last legacy format version.
#define CTL_FOR_BOARD
The zero arg constructor when PCB_PLUGIN is used for PLUGIN::Load() and PLUGIN::Save()ing a BOARD fil...
boost::ptr_map< wxString, FP_CACHE_ITEM > FP_CACHE_FOOTPRINT_MAP
#define CTL_FOR_LIBRARY
Format output for a footprint library instead of clipboard or BOARD.
Pcbnew s-expression file format parser definition.
#define UNDEFINED_DRILL_DIAMETER
Definition: pcb_track.h:81
PGM_BASE & Pgm()
The global Program "get" accessor.
Definition: pgm_base.cpp:1059
see class PGM_BASE
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:68
bool ReplaceIllegalFileNameChars(std::string *aName, int aReplaceChar)
Checks aName for illegal file name characters.
std::string FormatDouble2Str(double aValue)
Print a float number without using scientific notation and no trailing 0 This function is intended in...
wxString EscapeString(const wxString &aSource, ESCAPE_CONTEXT aContext)
The Escape/Unescape routines use HTML-entity-reference-style encoding to handle characters which are:...
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: string_utils.h:391
@ CTX_QUOTED_STR
Definition: string_utils.h:57
const double IU_PER_MM
Definition: base_units.h:76
Variant of PARSE_ERROR indicating that a syntax or related error was likely caused by a file generate...
Definition: ki_exception.h:176
static const char * ShowType(LAYER_T aType)
Convert a LAYER_T enum to a string representation of the layer type.
Definition: board.cpp:637
A filename or source description, a problem input line, a line number, a byte offset,...
Definition: ki_exception.h:120
wxLogTrace helper definitions.
void RotatePoint(int *pX, int *pY, const EDA_ANGLE &aAngle)
Calculate the new point of coord coord pX, pY, for a rotation center 0, 0.
Definition: trigo.cpp:228
@ PCB_T
Definition: typeinfo.h:82
@ PCB_SHAPE_T
class PCB_SHAPE, a segment not on copper layers
Definition: typeinfo.h:88
@ PCB_DIM_ORTHOGONAL_T
class PCB_DIM_ORTHOGONAL, a linear dimension constrained to x/y
Definition: typeinfo.h:105
@ PCB_DIM_LEADER_T
class PCB_DIM_LEADER, a leader dimension (graphic item)
Definition: typeinfo.h:102
@ PCB_GENERATOR_T
class PCB_GENERATOR, generator on a layer
Definition: typeinfo.h:91
@ PCB_VIA_T
class PCB_VIA, a via (like a track segment on a copper layer)
Definition: typeinfo.h:97
@ PCB_DIM_CENTER_T
class PCB_DIM_CENTER, a center point marking (graphic item)
Definition: typeinfo.h:103
@ PCB_GROUP_T
class PCB_GROUP, a set of BOARD_ITEMs
Definition: typeinfo.h:110
@ PCB_TEXTBOX_T
class PCB_TEXTBOX, wrapped text on a layer
Definition: typeinfo.h:93
@ PCB_ZONE_T
class ZONE, a copper pour area
Definition: typeinfo.h:107
@ PCB_TEXT_T
class PCB_TEXT, text on a layer
Definition: typeinfo.h:92
@ PCB_REFERENCE_IMAGE_T
class PCB_REFERENCE_IMAGE, bitmap on a layer
Definition: typeinfo.h:89
@ PCB_FIELD_T
class PCB_FIELD, text associated with a footprint property
Definition: typeinfo.h:90
@ PCB_TARGET_T
class PCB_TARGET, a target (graphic item)
Definition: typeinfo.h:106
@ PCB_TABLECELL_T
class PCB_TABLECELL, PCB_TEXTBOX for use in tables
Definition: typeinfo.h:95
@ PCB_FOOTPRINT_T
class FOOTPRINT, a footprint
Definition: typeinfo.h:86
@ PCB_DIM_ALIGNED_T
class PCB_DIM_ALIGNED, a linear dimension (graphic item)
Definition: typeinfo.h:101
@ PCB_PAD_T
class PAD, a pad in a footprint
Definition: typeinfo.h:87
@ PCB_ARC_T
class PCB_ARC, an arc track segment on a copper layer
Definition: typeinfo.h:98
@ PCB_TABLE_T
class PCB_TABLE, table of PCB_TABLECELLs
Definition: typeinfo.h:94
@ PCB_TRACE_T
class PCB_TRACK, a track segment (segment on a copper layer)
Definition: typeinfo.h:96
@ PCB_DIM_RADIAL_T
class PCB_DIM_RADIAL, a radius or diameter dimension
Definition: typeinfo.h:104
VECTOR2< int32_t > VECTOR2I
Definition: vector2d.h:673
Definition of file extensions used in Kicad.