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 for( PCB_LAYER_ID layer : LAYER_RANGE( F_Cu, B_Cu, board->GetCopperLayerCount() ) )
2053 {
2054 if( layer == F_Cu )
2055 continue;
2056
2057 m_out->Print( 0, "(layer %s", m_out->Quotew( LSET::Name( layer ) ).c_str() );
2058 formatPadLayer( layer );
2059 m_out->Print( 0, ")" );
2060 }
2061 }
2062
2063 m_out->Print( 0, ")" );
2064 }
2065
2067 m_out->Print( aNestLevel, ")\n" );
2068}
2069
2070
2071void PCB_IO_KICAD_SEXPR::formatTenting( const PADSTACK& aPadstack ) const
2072{
2073 std::optional<bool> front = aPadstack.FrontOuterLayers().has_solder_mask;
2074 std::optional<bool> back = aPadstack.BackOuterLayers().has_solder_mask;
2075
2076 if( front.has_value() || back.has_value() )
2077 {
2078 if( front.value_or( false ) || back.value_or( false ) )
2079 {
2080 m_out->Print( 0, " (tenting " );
2081
2082 if( front.value_or( false ) )
2083 m_out->Print( 0, " front" );
2084 if( back.value_or( false ) )
2085 m_out->Print( 0, " back" );
2086
2087 m_out->Print( 0, ")" );
2088 }
2089 else
2090 {
2091 m_out->Print( 0, " (tenting none)" );
2092 }
2093 }
2094}
2095
2096
2097void PCB_IO_KICAD_SEXPR::format( const PCB_TEXT* aText, int aNestLevel ) const
2098{
2099 FOOTPRINT* parentFP = aText->GetParentFootprint();
2100 std::string prefix;
2101 std::string type;
2102 VECTOR2I pos = aText->GetTextPos();
2103 bool isField = dynamic_cast<const PCB_FIELD*>( aText ) != nullptr;
2104
2105 // Always format dimension text as gr_text
2106 if( dynamic_cast<const PCB_DIMENSION_BASE*>( aText ) )
2107 parentFP = nullptr;
2108
2109 if( parentFP )
2110 {
2111 prefix = "fp";
2112 type = " user";
2113
2114 pos -= parentFP->GetPosition();
2115 RotatePoint( pos, -parentFP->GetOrientation() );
2116 }
2117 else
2118 {
2119 prefix = "gr";
2120 }
2121
2122 if( !isField )
2123 {
2124 m_out->Print( aNestLevel, "(%s_text%s %s", prefix.c_str(), type.c_str(),
2125 m_out->Quotew( aText->GetText() ).c_str() );
2126
2127 if( aText->IsLocked() )
2128 KICAD_FORMAT::FormatBool( m_out, 0, "locked", aText->IsLocked() );
2129 }
2130
2131 m_out->Print( 0, " (at %s", formatInternalUnits( pos ).c_str() );
2132
2133 // Due to Pcbnew history, fp_text angle is saved as an absolute on screen angle.
2134 // To avoid issues in the future, always save the angle, even if it is 0
2135 m_out->Print( 0, " %s", EDA_UNIT_UTILS::FormatAngle( aText->GetTextAngle() ).c_str() );
2136
2137 m_out->Print( 0, ")" );
2138
2139 if( parentFP && !aText->IsKeepUpright() )
2140 KICAD_FORMAT::FormatBool( m_out, 0, "unlocked", !aText->IsKeepUpright() );
2141
2142 formatLayer( aText->GetLayer(), aText->IsKnockout() );
2143
2144 if( parentFP && !aText->IsVisible() )
2145 KICAD_FORMAT::FormatBool( m_out, 0, "hide", !aText->IsVisible() );
2146
2148
2149 int ctl_flags = m_ctl | CTL_OMIT_HIDE;
2150
2151 // Currently, texts have no specific color and no hyperlink.
2152 // so ensure they are never written in kicad_pcb file
2153 ctl_flags |= CTL_OMIT_COLOR | CTL_OMIT_HYPERLINK;
2154
2155 aText->EDA_TEXT::Format( m_out, aNestLevel, ctl_flags );
2156
2157 if( aText->GetFont() && aText->GetFont()->IsOutline() )
2158 formatRenderCache( aText, aNestLevel + 1 );
2159
2160 if( !isField )
2161 m_out->Print( aNestLevel, ")\n" );
2162}
2163
2164
2165void PCB_IO_KICAD_SEXPR::format( const PCB_TEXTBOX* aTextBox, int aNestLevel ) const
2166{
2167 FOOTPRINT* parentFP = aTextBox->GetParentFootprint();
2168
2169 m_out->Print( aNestLevel, "(%s %s\n",
2170 aTextBox->Type() == PCB_TABLECELL_T ? "table_cell"
2171 : parentFP ? "fp_text_box"
2172 : "gr_text_box",
2173 m_out->Quotew( aTextBox->GetText() ).c_str() );
2174
2175 if( aTextBox->IsLocked() )
2176 KICAD_FORMAT::FormatBool( m_out, aNestLevel, "locked", aTextBox->IsLocked() );
2177
2178 if( aTextBox->GetShape() == SHAPE_T::RECTANGLE )
2179 {
2180 m_out->Print( aNestLevel + 1, "(start %s) (end %s)",
2181 formatInternalUnits( aTextBox->GetStart(), parentFP ).c_str(),
2182 formatInternalUnits( aTextBox->GetEnd(), parentFP ).c_str() );
2183 }
2184 else if( aTextBox->GetShape() == SHAPE_T::POLY )
2185 {
2186 const SHAPE_POLY_SET& poly = aTextBox->GetPolyShape();
2187 const SHAPE_LINE_CHAIN& outline = poly.Outline( 0 );
2188
2189 formatPolyPts( outline, aNestLevel, true, parentFP );
2190 }
2191 else
2192 {
2193 UNIMPLEMENTED_FOR( aTextBox->SHAPE_T_asString() );
2194 }
2195
2196 m_out->Print( 0, " (margins %s %s %s %s)",
2197 formatInternalUnits( aTextBox->GetMarginLeft() ).c_str(),
2198 formatInternalUnits( aTextBox->GetMarginTop() ).c_str(),
2199 formatInternalUnits( aTextBox->GetMarginRight() ).c_str(),
2200 formatInternalUnits( aTextBox->GetMarginBottom() ).c_str() );
2201
2202 if( const PCB_TABLECELL* cell = dynamic_cast<const PCB_TABLECELL*>( aTextBox ) )
2203 m_out->Print( 0, " (span %d %d)", cell->GetColSpan(), cell->GetRowSpan() );
2204
2205 EDA_ANGLE angle = aTextBox->GetTextAngle();
2206
2207 if( parentFP )
2208 {
2209 angle -= parentFP->GetOrientation();
2210 angle.Normalize720();
2211 }
2212
2213 if( !angle.IsZero() )
2214 m_out->Print( aNestLevel + 1, "(angle %s)", EDA_UNIT_UTILS::FormatAngle( angle ).c_str() );
2215
2216 formatLayer( aTextBox->GetLayer() );
2217 m_out->Print( 0, "\n" );
2218
2220
2221 // PCB_TEXTBOXes are never hidden, so always omit "hide" attribute
2222 aTextBox->EDA_TEXT::Format( m_out, aNestLevel, m_ctl | CTL_OMIT_HIDE );
2223
2224 if( aTextBox->Type() != PCB_TABLECELL_T )
2225 {
2226 KICAD_FORMAT::FormatBool( m_out, aNestLevel + 1, "border", aTextBox->IsBorderEnabled() );
2227
2228 aTextBox->GetStroke().Format( m_out, pcbIUScale, aNestLevel + 1 );
2229 }
2230
2231 if( aTextBox->GetFont() && aTextBox->GetFont()->IsOutline() )
2232 formatRenderCache( aTextBox, aNestLevel + 1 );
2233
2234 m_out->Print( aNestLevel, ")\n" );
2235}
2236
2237
2238void PCB_IO_KICAD_SEXPR::format( const PCB_TABLE* aTable, int aNestLevel ) const
2239{
2240 wxCHECK_RET( aTable != nullptr && m_out != nullptr, "" );
2241
2242 m_out->Print( aNestLevel, "(table (column_count %d)",
2243 aTable->GetColCount() );
2244
2245 if( aTable->IsLocked() )
2246 KICAD_FORMAT::FormatBool( m_out, 0, "locked", aTable->IsLocked() );
2247
2248 EDA_ANGLE angle = aTable->GetOrientation();
2249
2250 if( FOOTPRINT* parentFP = aTable->GetParentFootprint() )
2251 {
2252 angle -= parentFP->GetOrientation();
2253 angle.Normalize720();
2254 }
2255
2256 if( !angle.IsZero() )
2257 m_out->Print( 0, " (angle %s)", EDA_UNIT_UTILS::FormatAngle( angle ).c_str() );
2258
2259 formatLayer( aTable->GetLayer() );
2260
2261 m_out->Print( 0, "\n" );
2262
2263 m_out->Print( aNestLevel + 1, "(border (external %s) (header %s)",
2264 aTable->StrokeExternal() ? "yes" : "no",
2265 aTable->StrokeHeader() ? "yes" : "no" );
2266
2267 if( aTable->StrokeExternal() || aTable->StrokeHeader() )
2268 {
2269 m_out->Print( 0, " " );
2270 aTable->GetBorderStroke().Format( m_out, pcbIUScale, 0 );
2271 }
2272
2273 m_out->Print( 0, ")\n" );
2274
2275 m_out->Print( aNestLevel + 1, "(separators (rows %s) (cols %s)",
2276 aTable->StrokeRows() ? "yes" : "no",
2277 aTable->StrokeColumns() ? "yes" : "no" );
2278
2279 if( aTable->StrokeRows() || aTable->StrokeColumns() )
2280 {
2281 m_out->Print( 0, " " );
2282 aTable->GetSeparatorsStroke().Format( m_out, pcbIUScale, 0 );
2283 }
2284
2285 m_out->Print( 0, ")\n" ); // Close `separators` token.
2286
2287 m_out->Print( aNestLevel + 1, "(column_widths" );
2288
2289 for( int col = 0; col < aTable->GetColCount(); ++col )
2290 m_out->Print( 0, " %s", formatInternalUnits( aTable->GetColWidth( col ) ).c_str() );
2291
2292 m_out->Print( 0, ")\n" );
2293
2294 m_out->Print( aNestLevel + 1, "(row_heights" );
2295
2296 for( int row = 0; row < aTable->GetRowCount(); ++row )
2297 m_out->Print( 0, " %s", formatInternalUnits( aTable->GetRowHeight( row ) ).c_str() );
2298
2299 m_out->Print( 0, ")\n" );
2300
2301 m_out->Print( aNestLevel + 1, "(cells\n" );
2302
2303 for( PCB_TABLECELL* cell : aTable->GetCells() )
2304 format( static_cast<PCB_TEXTBOX*>( cell ), aNestLevel + 2 );
2305
2306 m_out->Print( aNestLevel + 1, ")\n" ); // Close `cells` token.
2307 m_out->Print( aNestLevel, ")\n" ); // Close `table` token.
2308}
2309
2310
2311void PCB_IO_KICAD_SEXPR::format( const PCB_GROUP* aGroup, int aNestLevel ) const
2312{
2313 // Don't write empty groups
2314 if( aGroup->GetItems().empty() )
2315 return;
2316
2317 m_out->Print( aNestLevel, "(group %s\n", m_out->Quotew( aGroup->GetName() ).c_str() );
2318
2320
2321 if( aGroup->IsLocked() )
2322 KICAD_FORMAT::FormatBool( m_out, aNestLevel + 1, "locked", aGroup->IsLocked() );
2323
2324 m_out->Print( aNestLevel + 1, "(members\n" );
2325
2326 wxArrayString memberIds;
2327
2328 for( BOARD_ITEM* member : aGroup->GetItems() )
2329 memberIds.Add( member->m_Uuid.AsString() );
2330
2331 memberIds.Sort();
2332
2333 for( const wxString& memberId : memberIds )
2334 m_out->Print( aNestLevel + 2, "\"%s\"\n", TO_UTF8( memberId ) );
2335
2336 m_out->Print( aNestLevel + 1, ")\n" ); // Close `members` token.
2337 m_out->Print( aNestLevel, ")\n" ); // Close `group` token.
2338}
2339
2340
2341void PCB_IO_KICAD_SEXPR::format( const PCB_GENERATOR* aGenerator, int aNestLevel ) const
2342{
2343 m_out->Print( aNestLevel, "(generated\n" );
2344
2345 KICAD_FORMAT::FormatUuid( m_out, aGenerator->m_Uuid );
2346
2347 m_out->Print( aNestLevel + 1, "(type %s) (name %s)\n",
2348 TO_UTF8( aGenerator->GetGeneratorType() ),
2349 m_out->Quotew( aGenerator->GetName() ).c_str() );
2350
2351 m_out->Print( aNestLevel + 1, "(layer %s)",
2352 m_out->Quotew( LSET::Name( aGenerator->GetLayer() ) ).c_str() );
2353
2354 if( const bool locked = aGenerator->IsLocked() ) {
2355 KICAD_FORMAT::FormatBool( m_out, 0, "locked", locked );
2356 }
2357
2358 for( const auto& [key, value] : aGenerator->GetProperties() )
2359 {
2360 if( value.CheckType<double>() || value.CheckType<int>() || value.CheckType<long>()
2361 || value.CheckType<long long>() )
2362 {
2363 double val;
2364
2365 if( !value.GetAs( &val ) )
2366 continue;
2367
2368 std::string buf = fmt::format( "{:.10g}", val );
2369
2370 // Don't quote numbers
2371 m_out->Print( aNestLevel + 1, "(%s %s)\n", key.c_str(), buf.c_str() );
2372 }
2373 else if( value.CheckType<bool>() )
2374 {
2375 bool val;
2376 value.GetAs( &val );
2377
2378 m_out->Print( aNestLevel + 1, "(%s %s)\n", key.c_str(), val ? "yes" : "no" );
2379 }
2380 else if( value.CheckType<VECTOR2I>() )
2381 {
2382 VECTOR2I val;
2383 value.GetAs( &val );
2384
2385 m_out->Print( aNestLevel + 1, "(%s (xy %s))\n", key.c_str(),
2386 formatInternalUnits( val ).c_str() );
2387 }
2388 else if( value.CheckType<SHAPE_LINE_CHAIN>() )
2389 {
2390 SHAPE_LINE_CHAIN val;
2391 value.GetAs( &val );
2392
2393 m_out->Print( aNestLevel + 1, "(%s (pts\n", key.c_str() );
2394
2395 for( const VECTOR2I& pt : val.CPoints() )
2396 m_out->Print( aNestLevel + 2, "(xy %s)\n", formatInternalUnits( pt ).c_str() );
2397
2398 m_out->Print( aNestLevel + 1, "))\n" );
2399 }
2400 else
2401 {
2402 wxString val;
2403
2404 if( value.CheckType<wxString>() )
2405 {
2406 value.GetAs( &val );
2407 }
2408 else if( value.CheckType<std::string>() )
2409 {
2410 std::string str;
2411 value.GetAs( &str );
2412
2413 val = wxString::FromUTF8( str );
2414 }
2415
2416 m_out->Print( aNestLevel + 1, "(%s %s)\n", key.c_str(), m_out->Quotew( val ).c_str() );
2417 }
2418 }
2419
2420 m_out->Print( aNestLevel + 1, "(members\n" );
2421
2422 wxArrayString memberIds;
2423
2424 for( BOARD_ITEM* member : aGenerator->GetItems() )
2425 memberIds.Add( member->m_Uuid.AsString() );
2426
2427 memberIds.Sort();
2428
2429 for( const wxString& memberId : memberIds )
2430 m_out->Print( aNestLevel + 2, "%s\n", TO_UTF8( memberId ) );
2431
2432 m_out->Print( aNestLevel + 1, ")\n" ); // Close `members` token.
2433
2434 m_out->Print( aNestLevel, ")\n" ); // Close `generator` token.
2435}
2436
2437
2438void PCB_IO_KICAD_SEXPR::format( const PCB_TRACK* aTrack, int aNestLevel ) const
2439{
2440 if( aTrack->Type() == PCB_VIA_T )
2441 {
2442 PCB_LAYER_ID layer1, layer2;
2443
2444 const PCB_VIA* via = static_cast<const PCB_VIA*>( aTrack );
2445 const BOARD* board = via->GetBoard();
2446
2447 wxCHECK_RET( board != nullptr, wxT( "Via has no parent." ) );
2448
2449 m_out->Print( aNestLevel, "(via" );
2450
2451 via->LayerPair( &layer1, &layer2 );
2452
2453 switch( via->GetViaType() )
2454 {
2455 case VIATYPE::THROUGH: // Default shape not saved.
2456 break;
2457
2458 case VIATYPE::BLIND_BURIED:
2459 m_out->Print( 0, " blind" );
2460 break;
2461
2462 case VIATYPE::MICROVIA:
2463 m_out->Print( 0, " micro" );
2464 break;
2465
2466 default:
2467 THROW_IO_ERROR( wxString::Format( _( "unknown via type %d" ), via->GetViaType() ) );
2468 }
2469
2470 m_out->Print( 0, " (at %s) (size %s)",
2471 formatInternalUnits( aTrack->GetStart() ).c_str(),
2472 formatInternalUnits( via->GetWidth( F_Cu ) ).c_str() );
2473
2474 // Old boards were using UNDEFINED_DRILL_DIAMETER value in file for via drill when
2475 // via drill was the netclass value.
2476 // recent boards always set the via drill to the actual value, but now we need to
2477 // always store the drill value, because netclass value is not stored in the board file.
2478 // Otherwise the drill value of some (old) vias can be unknown
2479 if( via->GetDrill() != UNDEFINED_DRILL_DIAMETER )
2480 m_out->Print( 0, " (drill %s)", formatInternalUnits( via->GetDrill() ).c_str() );
2481 else
2482 m_out->Print( 0, " (drill %s)", formatInternalUnits( via->GetDrillValue() ).c_str() );
2483
2484 m_out->Print( 0, " (layers %s %s)",
2485 m_out->Quotew( LSET::Name( layer1 ) ).c_str(),
2486 m_out->Quotew( LSET::Name( layer2 ) ).c_str() );
2487
2488 switch( via->Padstack().UnconnectedLayerMode() )
2489 {
2491 m_out->Print( 0, "(remove_unused_layers yes)" );
2492 m_out->Print( 0, "(keep_end_layers no)" );
2493 break;
2494
2496 m_out->Print( 0, "(remove_unused_layers yes)" );
2497 m_out->Print( 0, "(keep_end_layers yes)" );
2498 break;
2499
2501 break;
2502 }
2503
2504 if( via->IsLocked() )
2505 KICAD_FORMAT::FormatBool( m_out, 0, "locked", via->IsLocked() );
2506
2507 if( via->GetIsFree() )
2508 KICAD_FORMAT::FormatBool( m_out, 0, "free", via->GetIsFree() );
2509
2510 if( via->GetRemoveUnconnected() )
2511 {
2512 m_out->Print( 0, " (zone_layer_connections" );
2513
2514 for( PCB_LAYER_ID layer : board->GetEnabledLayers().CuStack() )
2515 {
2516 if( via->GetZoneLayerOverride( layer ) == ZLO_FORCE_FLASHED )
2517 m_out->Print( 0, " %s", m_out->Quotew( LSET::Name( layer ) ).c_str() );
2518 }
2519
2520 m_out->Print( 0, ")" );
2521 }
2522
2523 const PADSTACK& padstack = via->Padstack();
2524
2525 formatTenting( padstack );
2526
2527 if( padstack.Mode() != PADSTACK::MODE::NORMAL )
2528 {
2529 std::string mode =
2530 padstack.Mode() == PADSTACK::MODE::CUSTOM ? "custom" : "front_inner_back";
2531 m_out->Print( 0, "(padstack (mode %s)", mode.c_str() );
2532
2533 if( padstack.Mode() == PADSTACK::MODE::FRONT_INNER_BACK )
2534 {
2535 m_out->Print( 0, "(layer \"Inner\"" );
2536 m_out->Print( 0, " (size %s)",
2537 formatInternalUnits( padstack.Size( PADSTACK::INNER_LAYERS ).x ).c_str() );
2538 m_out->Print( 0, ")(layer \"B.Cu\"" );
2539 m_out->Print( 0, " (size %s)",
2540 formatInternalUnits( padstack.Size( B_Cu ).x ).c_str() );
2541 m_out->Print( 0, ")" );
2542 }
2543 else
2544 {
2545 for( PCB_LAYER_ID layer : LAYER_RANGE( F_Cu, B_Cu, board->GetCopperLayerCount() ) )
2546 {
2547 if( layer == F_Cu )
2548 continue;
2549
2550 m_out->Print( 0, "(layer %s", m_out->Quotew( LSET::Name( layer ) ).c_str() );
2551 m_out->Print( 0, " (size %s)",
2552 formatInternalUnits( padstack.Size( layer ).x ).c_str() );
2553 m_out->Print( 0, ")" );
2554 }
2555 }
2556
2557 m_out->Print( 0, ")" );
2558 }
2559
2560 if( !isDefaultTeardropParameters( via->GetTeardropParams() ) )
2561 {
2562 m_out->Print( 0, "\n" );
2563 formatTeardropParameters( via->GetTeardropParams(), aNestLevel+1 );
2564 }
2565 }
2566 else
2567 {
2568 if( aTrack->Type() == PCB_ARC_T )
2569 {
2570 const PCB_ARC* arc = static_cast<const PCB_ARC*>( aTrack );
2571
2572 m_out->Print( aNestLevel, "(arc (start %s) (mid %s) (end %s) (width %s)",
2573 formatInternalUnits( arc->GetStart() ).c_str(),
2574 formatInternalUnits( arc->GetMid() ).c_str(),
2575 formatInternalUnits( arc->GetEnd() ).c_str(),
2576 formatInternalUnits( arc->GetWidth() ).c_str() );
2577 }
2578 else
2579 {
2580 m_out->Print( aNestLevel, "(segment (start %s) (end %s) (width %s)",
2581 formatInternalUnits( aTrack->GetStart() ).c_str(),
2582 formatInternalUnits( aTrack->GetEnd() ).c_str(),
2583 formatInternalUnits( aTrack->GetWidth() ).c_str() );
2584 }
2585
2586 if( aTrack->IsLocked() )
2587 KICAD_FORMAT::FormatBool( m_out, 0, "locked", aTrack->IsLocked() );
2588
2589 if( aTrack->GetLayerSet().count() > 1 )
2590 formatLayers( aTrack->GetLayerSet() );
2591 else
2592 formatLayer( aTrack->GetLayer() );
2593
2594 if( aTrack->HasSolderMask()
2595 && aTrack->GetLocalSolderMaskMargin().has_value()
2596 && ( aTrack->IsOnLayer( F_Cu ) || aTrack->IsOnLayer( B_Cu ) ) )
2597 {
2598 m_out->Print( 0, " (solder_mask_margin %s)",
2599 formatInternalUnits( aTrack->GetLocalSolderMaskMargin().value() ).c_str() );
2600 }
2601 }
2602
2603 m_out->Print( 0, " (net %d)", m_mapping->Translate( aTrack->GetNetCode() ) );
2604
2606
2607 m_out->Print( 0, ")\n" );
2608}
2609
2610
2611void PCB_IO_KICAD_SEXPR::format( const ZONE* aZone, int aNestLevel ) const
2612{
2613 // Save the NET info.
2614 // For keepout and non copper zones, net code and net name are irrelevant
2615 // so be sure a dummy value is stored, just for ZONE compatibility
2616 // (perhaps netcode and netname should be not stored)
2617
2618 bool has_no_net = aZone->GetIsRuleArea() || !aZone->IsOnCopperLayer();
2619
2620 m_out->Print( aNestLevel, "(zone (net %d) (net_name %s)",
2621 has_no_net ? 0 : m_mapping->Translate( aZone->GetNetCode() ),
2622 m_out->Quotew( has_no_net ? wxString( wxT("") ) : aZone->GetNetname() ).c_str() );
2623
2624 if( aZone->IsLocked() )
2625 KICAD_FORMAT::FormatBool( m_out, 0, "locked", aZone->IsLocked() );
2626
2627 // If a zone exists on multiple layers, format accordingly
2628 LSET layers = aZone->GetLayerSet();
2629
2630 if( aZone->GetBoard() )
2631 layers &= aZone->GetBoard()->GetEnabledLayers();
2632
2633 if( layers.count() > 1 )
2634 {
2635 formatLayers( layers );
2636 }
2637 else
2638 {
2639 formatLayer( aZone->GetFirstLayer() );
2640 }
2641
2643
2644 if( !aZone->GetZoneName().empty() )
2645 m_out->Print( 0, " (name %s)", m_out->Quotew( aZone->GetZoneName() ).c_str() );
2646
2647 // Save the outline aux info
2648 std::string hatch;
2649
2650 switch( aZone->GetHatchStyle() )
2651 {
2652 default:
2653 case ZONE_BORDER_DISPLAY_STYLE::NO_HATCH: hatch = "none"; break;
2654 case ZONE_BORDER_DISPLAY_STYLE::DIAGONAL_EDGE: hatch = "edge"; break;
2655 case ZONE_BORDER_DISPLAY_STYLE::DIAGONAL_FULL: hatch = "full"; break;
2656 }
2657
2658 m_out->Print( 0, " (hatch %s %s)\n", hatch.c_str(),
2659 formatInternalUnits( aZone->GetBorderHatchPitch() ).c_str() );
2660
2661
2662
2663 if( aZone->GetAssignedPriority() > 0 )
2664 m_out->Print( aNestLevel+1, "(priority %d)\n", aZone->GetAssignedPriority() );
2665
2666 // Add teardrop keywords in file: (attr (teardrop (type xxx)))where xxx is the teardrop type
2667 if( aZone->IsTeardropArea() )
2668 {
2669 const char* td_type;
2670
2671 switch( aZone->GetTeardropAreaType() )
2672 {
2673 case TEARDROP_TYPE::TD_VIAPAD: // a teardrop on a via or pad
2674 td_type = "padvia";
2675 break;
2676
2677 default:
2678 case TEARDROP_TYPE::TD_TRACKEND: // a teardrop on a track end
2679 td_type = "track_end";
2680 break;
2681 }
2682
2683 m_out->Print( aNestLevel+1, "(attr (teardrop (type %s)))\n", td_type );
2684 }
2685
2686 m_out->Print( aNestLevel+1, "(connect_pads" );
2687
2688 switch( aZone->GetPadConnection() )
2689 {
2690 default:
2691 case ZONE_CONNECTION::THERMAL: // Default option not saved or loaded.
2692 break;
2693
2694 case ZONE_CONNECTION::THT_THERMAL:
2695 m_out->Print( 0, " thru_hole_only" );
2696 break;
2697
2698 case ZONE_CONNECTION::FULL:
2699 m_out->Print( 0, " yes" );
2700 break;
2701
2702 case ZONE_CONNECTION::NONE:
2703 m_out->Print( 0, " no" );
2704 break;
2705 }
2706
2707 m_out->Print( 0, " (clearance %s))\n",
2708 formatInternalUnits( aZone->GetLocalClearance().value() ).c_str() );
2709
2710 m_out->Print( aNestLevel+1, "(min_thickness %s)",
2711 formatInternalUnits( aZone->GetMinThickness() ).c_str() );
2712
2713 // We continue to write this for 3rd-party parsers, but we no longer read it (as of V7).
2714 m_out->Print( 0, " (filled_areas_thickness no)" );
2715
2716 m_out->Print( 0, "\n" );
2717
2718 if( aZone->GetIsRuleArea() )
2719 {
2720 // Keepout settings
2721 m_out->Print( aNestLevel + 1,
2722 "(keepout (tracks %s) (vias %s) (pads %s) (copperpour %s) "
2723 "(footprints %s))\n",
2724 aZone->GetDoNotAllowTracks() ? "not_allowed" : "allowed",
2725 aZone->GetDoNotAllowVias() ? "not_allowed" : "allowed",
2726 aZone->GetDoNotAllowPads() ? "not_allowed" : "allowed",
2727 aZone->GetDoNotAllowCopperPour() ? "not_allowed" : "allowed",
2728 aZone->GetDoNotAllowFootprints() ? "not_allowed" : "allowed" );
2729
2730 // Multichannel settings
2731 if( ADVANCED_CFG::GetCfg().m_EnableMultichannelTool )
2732 {
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
2757 m_out->Print( aNestLevel + 1, "(fill" );
2758
2759 // Default is not filled.
2760 if( aZone->IsFilled() )
2761 m_out->Print( 0, " yes" );
2762
2763 // Default is polygon filled.
2764 if( aZone->GetFillMode() == ZONE_FILL_MODE::HATCH_PATTERN )
2765 m_out->Print( 0, " (mode hatch)" );
2766
2767 m_out->Print( 0, " (thermal_gap %s) (thermal_bridge_width %s)",
2768 formatInternalUnits( aZone->GetThermalReliefGap() ).c_str(),
2769 formatInternalUnits( aZone->GetThermalReliefSpokeWidth() ).c_str() );
2770
2772 {
2773 m_out->Print( 0, " (smoothing" );
2774
2775 switch( aZone->GetCornerSmoothingType() )
2776 {
2778 m_out->Print( 0, " chamfer" );
2779 break;
2780
2782 m_out->Print( 0, " fillet" );
2783 break;
2784
2785 default:
2786 THROW_IO_ERROR( wxString::Format( _( "unknown zone corner smoothing type %d" ),
2787 aZone->GetCornerSmoothingType() ) );
2788 }
2789 m_out->Print( 0, ")" );
2790
2791 if( aZone->GetCornerRadius() != 0 )
2792 m_out->Print( 0, " (radius %s)", formatInternalUnits( aZone->GetCornerRadius() ).c_str() );
2793 }
2794
2795 if( aZone->GetIslandRemovalMode() != ISLAND_REMOVAL_MODE::ALWAYS )
2796 {
2797 m_out->Print( 0, " (island_removal_mode %d) (island_area_min %s)",
2798 static_cast<int>( aZone->GetIslandRemovalMode() ),
2800 }
2801
2802 if( aZone->GetFillMode() == ZONE_FILL_MODE::HATCH_PATTERN )
2803 {
2804 m_out->Print( 0, "\n" );
2805 m_out->Print( aNestLevel+2, "(hatch_thickness %s) (hatch_gap %s) (hatch_orientation %s)",
2806 formatInternalUnits( aZone->GetHatchThickness() ).c_str(),
2807 formatInternalUnits( aZone->GetHatchGap() ).c_str(),
2808 FormatDouble2Str( aZone->GetHatchOrientation().AsDegrees() ).c_str() );
2809
2810 if( aZone->GetHatchSmoothingLevel() > 0 )
2811 {
2812 m_out->Print( 0, "\n" );
2813 m_out->Print( aNestLevel+2, "(hatch_smoothing_level %d) (hatch_smoothing_value %s)",
2814 aZone->GetHatchSmoothingLevel(),
2815 FormatDouble2Str( aZone->GetHatchSmoothingValue() ).c_str() );
2816 }
2817
2818 m_out->Print( 0, "\n" );
2819 m_out->Print( aNestLevel+2, "(hatch_border_algorithm %s) (hatch_min_hole_area %s)",
2820 aZone->GetHatchBorderAlgorithm() ? "hatch_thickness" : "min_thickness",
2821 FormatDouble2Str( aZone->GetHatchHoleMinArea() ).c_str() );
2822 }
2823
2824 m_out->Print( 0, ")\n" );
2825
2826 if( aZone->GetNumCorners() )
2827 {
2828 SHAPE_POLY_SET::POLYGON poly = aZone->Outline()->Polygon(0);
2829
2830 for( auto& chain : poly )
2831 {
2832 m_out->Print( aNestLevel + 1, "(polygon\n" );
2833 formatPolyPts( chain, aNestLevel + 1, ADVANCED_CFG::GetCfg().m_CompactSave );
2834 m_out->Print( aNestLevel + 1, ")\n" );
2835 }
2836 }
2837
2838 // Save the PolysList (filled areas)
2839 for( PCB_LAYER_ID layer : aZone->GetLayerSet().Seq() )
2840 {
2841 const std::shared_ptr<SHAPE_POLY_SET>& fv = aZone->GetFilledPolysList( layer );
2842
2843 for( int ii = 0; ii < fv->OutlineCount(); ++ii )
2844 {
2845 m_out->Print( aNestLevel + 1, "(filled_polygon\n" );
2846 m_out->Print( aNestLevel + 2, "(layer %s)\n",
2847 m_out->Quotew( LSET::Name( layer ) ).c_str() );
2848
2849 if( aZone->IsIsland( layer, ii ) )
2850 m_out->Print( aNestLevel + 2, "(island)\n" );
2851
2852 const SHAPE_LINE_CHAIN& chain = fv->COutline( ii );
2853
2854 formatPolyPts( chain, aNestLevel + 1, ADVANCED_CFG::GetCfg().m_CompactSave );
2855 m_out->Print( aNestLevel + 1, ")\n" );
2856 }
2857 }
2858
2859 m_out->Print( aNestLevel, ")\n" );
2860}
2861
2862
2863PCB_IO_KICAD_SEXPR::PCB_IO_KICAD_SEXPR( int aControlFlags ) : PCB_IO( wxS( "KiCad" ) ),
2864 m_cache( nullptr ),
2865 m_ctl( aControlFlags ),
2866 m_mapping( new NETINFO_MAPPING() )
2867{
2868 init( nullptr );
2869 m_out = &m_sf;
2870}
2871
2872
2874{
2875 delete m_cache;
2876 delete m_mapping;
2877}
2878
2879
2880BOARD* PCB_IO_KICAD_SEXPR::LoadBoard( const wxString& aFileName, BOARD* aAppendToMe,
2881 const std::map<std::string, UTF8>* aProperties, PROJECT* aProject )
2882{
2883 FILE_LINE_READER reader( aFileName );
2884
2885 unsigned lineCount = 0;
2886
2888
2889 if( m_progressReporter )
2890 {
2891 m_progressReporter->Report( wxString::Format( _( "Loading %s..." ), aFileName ) );
2892
2894 THROW_IO_ERROR( _( "Open cancelled by user." ) );
2895
2896 while( reader.ReadLine() )
2897 lineCount++;
2898
2899 reader.Rewind();
2900 }
2901
2902 BOARD* board = DoLoad( reader, aAppendToMe, aProperties, m_progressReporter, lineCount );
2903
2904 // Give the filename to the board if it's new
2905 if( !aAppendToMe )
2906 board->SetFileName( aFileName );
2907
2908 return board;
2909}
2910
2911
2912BOARD* PCB_IO_KICAD_SEXPR::DoLoad( LINE_READER& aReader, BOARD* aAppendToMe, const std::map<std::string, UTF8>* aProperties,
2913 PROGRESS_REPORTER* aProgressReporter, unsigned aLineCount)
2914{
2915 init( aProperties );
2916
2917 PCB_IO_KICAD_SEXPR_PARSER parser( &aReader, aAppendToMe, m_queryUserCallback, aProgressReporter, aLineCount );
2918 BOARD* board;
2919
2920 try
2921 {
2922 board = dynamic_cast<BOARD*>( parser.Parse() );
2923 }
2924 catch( const FUTURE_FORMAT_ERROR& )
2925 {
2926 // Don't wrap a FUTURE_FORMAT_ERROR in another
2927 throw;
2928 }
2929 catch( const PARSE_ERROR& parse_error )
2930 {
2931 if( parser.IsTooRecent() )
2932 throw FUTURE_FORMAT_ERROR( parse_error, parser.GetRequiredVersion() );
2933 else
2934 throw;
2935 }
2936
2937 if( !board )
2938 {
2939 // The parser loaded something that was valid, but wasn't a board.
2940 THROW_PARSE_ERROR( _( "This file does not contain a PCB." ), parser.CurSource(),
2941 parser.CurLine(), parser.CurLineNumber(), parser.CurOffset() );
2942 }
2943
2944 return board;
2945}
2946
2947
2948void PCB_IO_KICAD_SEXPR::init( const std::map<std::string, UTF8>* aProperties )
2949{
2950 m_board = nullptr;
2951 m_reader = nullptr;
2952 m_props = aProperties;
2953}
2954
2955
2956void PCB_IO_KICAD_SEXPR::validateCache( const wxString& aLibraryPath, bool checkModified )
2957{
2959
2960 if( !m_cache || !m_cache->IsPath( aLibraryPath ) || ( checkModified && m_cache->IsModified() ) )
2961 {
2962 // a spectacular episode in memory management:
2963 delete m_cache;
2964 m_cache = new FP_CACHE( this, aLibraryPath );
2965 m_cache->Load();
2966 }
2967}
2968
2969
2970void PCB_IO_KICAD_SEXPR::FootprintEnumerate( wxArrayString& aFootprintNames, const wxString& aLibPath,
2971 bool aBestEfforts, const std::map<std::string, UTF8>* aProperties )
2972{
2973 LOCALE_IO toggle; // toggles on, then off, the C locale.
2974 wxDir dir( aLibPath );
2975 wxString errorMsg;
2976
2977 init( aProperties );
2978
2979 try
2980 {
2981 validateCache( aLibPath );
2982 }
2983 catch( const IO_ERROR& ioe )
2984 {
2985 errorMsg = ioe.What();
2986 }
2987
2988 // Some of the files may have been parsed correctly so we want to add the valid files to
2989 // the library.
2990
2991 for( const auto& footprint : m_cache->GetFootprints() )
2992 aFootprintNames.Add( footprint.first );
2993
2994 if( !errorMsg.IsEmpty() && !aBestEfforts )
2995 THROW_IO_ERROR( errorMsg );
2996}
2997
2998
2999const FOOTPRINT* PCB_IO_KICAD_SEXPR::getFootprint( const wxString& aLibraryPath,
3000 const wxString& aFootprintName,
3001 const std::map<std::string, UTF8>* aProperties,
3002 bool checkModified )
3003{
3004 LOCALE_IO toggle; // toggles on, then off, the C locale.
3005
3006 init( aProperties );
3007
3008 try
3009 {
3010 validateCache( aLibraryPath, checkModified );
3011 }
3012 catch( const IO_ERROR& )
3013 {
3014 // do nothing with the error
3015 }
3016
3018 FP_CACHE_FOOTPRINT_MAP::const_iterator it = footprints.find( aFootprintName );
3019
3020 if( it == footprints.end() )
3021 return nullptr;
3022
3023 return it->second->GetFootprint();
3024}
3025
3026
3027const FOOTPRINT* PCB_IO_KICAD_SEXPR::GetEnumeratedFootprint( const wxString& aLibraryPath,
3028 const wxString& aFootprintName,
3029 const std::map<std::string, UTF8>* aProperties )
3030{
3031 return getFootprint( aLibraryPath, aFootprintName, aProperties, false );
3032}
3033
3034
3035bool PCB_IO_KICAD_SEXPR::FootprintExists( const wxString& aLibraryPath, const wxString& aFootprintName,
3036 const std::map<std::string, UTF8>* aProperties )
3037{
3038 // Note: checking the cache sounds like a good idea, but won't catch files which differ
3039 // only in case.
3040 //
3041 // Since this goes out to the native filesystem, we get platform differences (ie: MSW's
3042 // case-insensitive filesystem) handled "for free".
3043 // Warning: footprint names frequently contain a point. So be careful when initializing
3044 // wxFileName, and use a CTOR with extension specified
3045 wxFileName footprintFile( aLibraryPath, aFootprintName, FILEEXT::KiCadFootprintFileExtension );
3046
3047 return footprintFile.Exists();
3048}
3049
3050
3051FOOTPRINT* PCB_IO_KICAD_SEXPR::ImportFootprint( const wxString& aFootprintPath, wxString& aFootprintNameOut,
3052 const std::map<std::string, UTF8>* aProperties )
3053{
3054 wxString fcontents;
3055 wxFFile f( aFootprintPath );
3056
3058
3059 if( !f.IsOpened() )
3060 return nullptr;
3061
3062 f.ReadAll( &fcontents );
3063
3064 aFootprintNameOut = wxFileName( aFootprintPath ).GetName();
3065
3066 return dynamic_cast<FOOTPRINT*>( Parse( fcontents ) );
3067}
3068
3069
3070FOOTPRINT* PCB_IO_KICAD_SEXPR::FootprintLoad( const wxString& aLibraryPath,
3071 const wxString& aFootprintName,
3072 bool aKeepUUID,
3073 const std::map<std::string, UTF8>* aProperties )
3074{
3076
3077 const FOOTPRINT* footprint = getFootprint( aLibraryPath, aFootprintName, aProperties, true );
3078
3079 if( footprint )
3080 {
3081 FOOTPRINT* copy;
3082
3083 if( aKeepUUID )
3084 copy = static_cast<FOOTPRINT*>( footprint->Clone() );
3085 else
3086 copy = static_cast<FOOTPRINT*>( footprint->Duplicate() );
3087
3088 copy->SetParent( nullptr );
3089 return copy;
3090 }
3091
3092 return nullptr;
3093}
3094
3095
3096void PCB_IO_KICAD_SEXPR::FootprintSave( const wxString& aLibraryPath, const FOOTPRINT* aFootprint,
3097 const std::map<std::string, UTF8>* aProperties )
3098{
3099 LOCALE_IO toggle; // toggles on, then off, the C locale.
3100
3101 init( aProperties );
3102
3103 // In this public PLUGIN API function, we can safely assume it was
3104 // called for saving into a library path.
3106
3107 validateCache( aLibraryPath, !aProperties || !aProperties->contains( "skip_cache_validation" ) );
3108
3109 if( !m_cache->IsWritable() )
3110 {
3111 if( !m_cache->Exists() )
3112 {
3113 const wxString msg = wxString::Format( _( "Library '%s' does not exist.\n"
3114 "Would you like to create it?"),
3115 aLibraryPath );
3116
3117 if( !Pgm().IsGUI()
3118 || wxMessageBox( msg, _( "Library Not Found" ), wxYES_NO | wxICON_QUESTION )
3119 != wxYES )
3120 return;
3121
3122 // Save throws its own IO_ERROR on failure, so no need to recreate here
3123 m_cache->Save( nullptr );
3124 }
3125 else
3126 {
3127 wxString msg = wxString::Format( _( "Library '%s' is read only." ), aLibraryPath );
3128 THROW_IO_ERROR( msg );
3129 }
3130 }
3131
3132 wxString footprintName = aFootprint->GetFPID().GetLibItemName();
3133
3135 wxString fpName = aFootprint->GetFPID().GetLibItemName().wx_str();
3136 ReplaceIllegalFileNameChars( fpName, '_' );
3137
3138 // Quietly overwrite footprint and delete footprint file from path for any by same name.
3139 wxFileName fn( aLibraryPath, fpName, FILEEXT::KiCadFootprintFileExtension );
3140
3141 // Write through symlinks, don't replace them
3143
3144 if( !fn.IsOk() )
3145 {
3146 THROW_IO_ERROR( wxString::Format( _( "Footprint file name '%s' is not valid." ),
3147 fn.GetFullPath() ) );
3148 }
3149
3150 if( fn.FileExists() && !fn.IsFileWritable() )
3151 {
3152 THROW_IO_ERROR( wxString::Format( _( "Insufficient permissions to delete '%s'." ),
3153 fn.GetFullPath() ) );
3154 }
3155
3156 wxString fullPath = fn.GetFullPath();
3157 wxString fullName = fn.GetFullName();
3158 FP_CACHE_FOOTPRINT_MAP::const_iterator it = footprints.find( footprintName );
3159
3160 if( it != footprints.end() )
3161 {
3162 wxLogTrace( traceKicadPcbPlugin, wxT( "Removing footprint file '%s'." ), fullPath );
3163 footprints.erase( footprintName );
3164 wxRemoveFile( fullPath );
3165 }
3166
3167 // I need my own copy for the cache
3168 FOOTPRINT* footprint = static_cast<FOOTPRINT*>( aFootprint->Clone() );
3169
3170 // It's orientation should be zero and it should be on the front layer.
3171 footprint->SetOrientation( ANGLE_0 );
3172
3173 if( footprint->GetLayer() != F_Cu )
3174 {
3175 PCBNEW_SETTINGS* cfg = dynamic_cast<PCBNEW_SETTINGS*>( Kiface().KifaceSettings() );
3176
3177 if( cfg )
3178 footprint->Flip( footprint->GetPosition(), cfg->m_FlipDirection );
3179 else
3180 footprint->Flip( footprint->GetPosition(), FLIP_DIRECTION::TOP_BOTTOM );
3181 }
3182
3183 // Detach it from the board and its group
3184 footprint->SetParent( nullptr );
3185 footprint->SetParentGroup( nullptr );
3186
3187 wxLogTrace( traceKicadPcbPlugin, wxT( "Creating s-expr footprint file '%s'." ), fullPath );
3188 footprints.insert( footprintName,
3189 new FP_CACHE_ITEM( footprint, WX_FILENAME( fn.GetPath(), fullName ) ) );
3190 m_cache->Save( footprint );
3191}
3192
3193
3194void PCB_IO_KICAD_SEXPR::FootprintDelete( const wxString& aLibraryPath, const wxString& aFootprintName,
3195 const std::map<std::string, UTF8>* aProperties )
3196{
3197 LOCALE_IO toggle; // toggles on, then off, the C locale.
3198
3199 init( aProperties );
3200
3201 validateCache( aLibraryPath );
3202
3203 if( !m_cache->IsWritable() )
3204 {
3205 THROW_IO_ERROR( wxString::Format( _( "Library '%s' is read only." ),
3206 aLibraryPath.GetData() ) );
3207 }
3208
3209 m_cache->Remove( aFootprintName );
3210}
3211
3212
3213
3214long long PCB_IO_KICAD_SEXPR::GetLibraryTimestamp( const wxString& aLibraryPath ) const
3215{
3216 return FP_CACHE::GetTimestamp( aLibraryPath );
3217}
3218
3219
3220void PCB_IO_KICAD_SEXPR::CreateLibrary( const wxString& aLibraryPath, const std::map<std::string, UTF8>* aProperties )
3221{
3222 if( wxDir::Exists( aLibraryPath ) )
3223 {
3224 THROW_IO_ERROR( wxString::Format( _( "Cannot overwrite library path '%s'." ),
3225 aLibraryPath.GetData() ) );
3226 }
3227
3228 LOCALE_IO toggle;
3229
3230 init( aProperties );
3231
3232 delete m_cache;
3233 m_cache = new FP_CACHE( this, aLibraryPath );
3234 m_cache->Save();
3235}
3236
3237
3238bool PCB_IO_KICAD_SEXPR::DeleteLibrary( const wxString& aLibraryPath, const std::map<std::string, UTF8>* aProperties )
3239{
3240 wxFileName fn;
3241 fn.SetPath( aLibraryPath );
3242
3243 // Return if there is no library path to delete.
3244 if( !fn.DirExists() )
3245 return false;
3246
3247 if( !fn.IsDirWritable() )
3248 {
3249 THROW_IO_ERROR( wxString::Format( _( "Insufficient permissions to delete folder '%s'." ),
3250 aLibraryPath.GetData() ) );
3251 }
3252
3253 wxDir dir( aLibraryPath );
3254
3255 if( dir.HasSubDirs() )
3256 {
3257 THROW_IO_ERROR( wxString::Format( _( "Library folder '%s' has unexpected sub-folders." ),
3258 aLibraryPath.GetData() ) );
3259 }
3260
3261 // All the footprint files must be deleted before the directory can be deleted.
3262 if( dir.HasFiles() )
3263 {
3264 unsigned i;
3265 wxFileName tmp;
3266 wxArrayString files;
3267
3268 wxDir::GetAllFiles( aLibraryPath, &files );
3269
3270 for( i = 0; i < files.GetCount(); i++ )
3271 {
3272 tmp = files[i];
3273
3274 if( tmp.GetExt() != FILEEXT::KiCadFootprintFileExtension )
3275 {
3276 THROW_IO_ERROR( wxString::Format( _( "Unexpected file '%s' found in library "
3277 "path '%s'." ),
3278 files[i].GetData(),
3279 aLibraryPath.GetData() ) );
3280 }
3281 }
3282
3283 for( i = 0; i < files.GetCount(); i++ )
3284 wxRemoveFile( files[i] );
3285 }
3286
3287 wxLogTrace( traceKicadPcbPlugin, wxT( "Removing footprint library '%s'." ),
3288 aLibraryPath.GetData() );
3289
3290 // Some of the more elaborate wxRemoveFile() crap puts up its own wxLog dialog
3291 // we don't want that. we want bare metal portability with no UI here.
3292 if( !wxRmdir( aLibraryPath ) )
3293 {
3294 THROW_IO_ERROR( wxString::Format( _( "Footprint library '%s' cannot be deleted." ),
3295 aLibraryPath.GetData() ) );
3296 }
3297
3298 // For some reason removing a directory in Windows is not immediately updated. This delay
3299 // prevents an error when attempting to immediately recreate the same directory when over
3300 // writing an existing library.
3301#ifdef __WINDOWS__
3302 wxMilliSleep( 250L );
3303#endif
3304
3305 if( m_cache && !m_cache->IsPath( aLibraryPath ) )
3306 {
3307 delete m_cache;
3308 m_cache = nullptr;
3309 }
3310
3311 return true;
3312}
3313
3314
3315bool PCB_IO_KICAD_SEXPR::IsLibraryWritable( const wxString& aLibraryPath )
3316{
3317 LOCALE_IO toggle;
3318
3319 init( nullptr );
3320
3321 validateCache( aLibraryPath );
3322
3323 return m_cache->IsWritable();
3324}
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:773
EMBEDDED_FILES * GetEmbeddedFiles() override
Definition: board.cpp:2544
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:613
TITLE_BLOCK & GetTitleBlock()
Definition: board.h:695
int GetCopperLayerCount() const
Definition: board.cpp:736
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:577
bool LegacyTeardrops() const
Definition: board.h:1261
wxString GroupsSanityCheck(bool repair=false)
Consistency check of internal m_groups structure.
Definition: board.cpp:2747
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Definition: board.cpp:890
void EmbedFonts() override
Finds all fonts used in the board and embeds them in the file if permissions allow.
Definition: board.cpp:2562
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:542
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:3758
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:118
std::optional< int > & Clearance(PCB_LAYER_ID aLayer=F_Cu)
Definition: padstack.cpp:1034
MASK_LAYER_PROPS & FrontOuterLayers()
Definition: padstack.h:306
std::optional< int > & ThermalSpokeWidth(PCB_LAYER_ID aLayer=F_Cu)
Definition: padstack.cpp:1099
EDA_ANGLE ThermalSpokeAngle(PCB_LAYER_ID aLayer=F_Cu) const
Definition: padstack.cpp:1134
std::optional< int > & ThermalGap(PCB_LAYER_ID aLayer=F_Cu)
Definition: padstack.cpp:1111
const VECTOR2I & Size(PCB_LAYER_ID aLayer) const
Definition: padstack.cpp:921
@ 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:281
MASK_LAYER_PROPS & BackOuterLayers()
Definition: padstack.h:309
static constexpr PCB_LAYER_ID ALL_LAYERS
! Temporary layer identifier to identify code that is not padstack-aware
Definition: padstack.h:138
static constexpr PCB_LAYER_ID INNER_LAYERS
! The layer identifier to use for "inner layers" on top/inner/bottom padstacks
Definition: padstack.h:141
std::optional< ZONE_CONNECTION > & ZoneConnection(PCB_LAYER_ID aLayer=F_Cu)
Definition: padstack.cpp:1087
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:199
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:1022
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:934
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
bool IsExternalCopperLayer(int aLayerId)
Tests whether a layer is an external (F_Cu or B_Cu) copper layer.
Definition: layer_ids.h:543
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:640
std::optional< bool > has_solder_mask
True if this outer layer has mask (is not tented)
Definition: padstack.h:225
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.