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