KiCad PCB EDA Suite
Loading...
Searching...
No Matches
sch_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) 2020 CERN
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * @author Wayne Stambaugh <[email protected]>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23#include <algorithm>
24
25#include <fmt/format.h>
26
27#include <wx/log.h>
28#include <wx/mstream.h>
29
30#include <base_units.h>
31#include <bitmap_base.h>
32#include <build_version.h>
33#include <sch_selection.h>
34#include <font/fontconfig.h>
36#include <progress_reporter.h>
37#include <schematic.h>
38#include <schematic_lexer.h>
39#include <sch_bitmap.h>
40#include <sch_bus_entry.h>
41#include <sch_edit_frame.h> // SYMBOL_ORIENTATION_T
42#include <sch_group.h>
47#include <sch_junction.h>
48#include <sch_line.h>
49#include <sch_no_connect.h>
50#include <sch_pin.h>
51#include <sch_rule_area.h>
52#include <sch_screen.h>
53#include <sch_shape.h>
54#include <sch_sheet.h>
55#include <sch_sheet_pin.h>
56#include <sch_symbol.h>
57#include <sch_table.h>
58#include <sch_tablecell.h>
59#include <sch_text.h>
60#include <sch_textbox.h>
61#include <string_utils.h>
62#include <symbol_lib_table.h> // for PropPowerSymsOnly definition.
63#include <trace_helpers.h>
64
65using namespace TSCHEMATIC_T;
66
67
68#define SCH_PARSE_ERROR( text, reader, pos ) \
69 THROW_PARSE_ERROR( text, reader.GetSource(), reader.Line(), \
70 reader.LineNumber(), pos - reader.Line() )
71
72
73SCH_IO_KICAD_SEXPR::SCH_IO_KICAD_SEXPR() : SCH_IO( wxS( "Eeschema s-expression" ) )
74{
75 init( nullptr );
76}
77
78
80{
81 delete m_cache;
82}
83
84
86 const std::map<std::string, UTF8>* aProperties )
87{
88 m_version = 0;
89 m_appending = false;
90 m_rootSheet = nullptr;
91 m_schematic = aSchematic;
92 m_cache = nullptr;
93 m_out = nullptr;
94}
95
96
97SCH_SHEET* SCH_IO_KICAD_SEXPR::LoadSchematicFile( const wxString& aFileName, SCHEMATIC* aSchematic,
98 SCH_SHEET* aAppendToMe,
99 const std::map<std::string, UTF8>* aProperties )
100{
101 wxASSERT( !aFileName || aSchematic != nullptr );
102
103 SCH_SHEET* sheet;
104
105 wxFileName fn = aFileName;
106
107 // Show the font substitution warnings
109
110 // Unfortunately child sheet file names the legacy schematic file format are not fully
111 // qualified and are always appended to the project path. The aFileName attribute must
112 // always be an absolute path so the project path can be used for load child sheet files.
113 wxASSERT( fn.IsAbsolute() );
114
115 if( aAppendToMe )
116 {
117 m_appending = true;
118 wxLogTrace( traceSchPlugin, "Append \"%s\" to sheet \"%s\".",
119 aFileName, aAppendToMe->GetFileName() );
120
121 wxFileName normedFn = aAppendToMe->GetFileName();
122
123 if( !normedFn.IsAbsolute() )
124 {
125 if( aFileName.Right( normedFn.GetFullPath().Length() ) == normedFn.GetFullPath() )
126 m_path = aFileName.Left( aFileName.Length() - normedFn.GetFullPath().Length() );
127 }
128
129 if( m_path.IsEmpty() )
130 m_path = aSchematic->Project().GetProjectPath();
131
132 wxLogTrace( traceSchPlugin, "Normalized append path \"%s\".", m_path );
133 }
134 else
135 {
136 m_path = aSchematic->Project().GetProjectPath();
137 }
138
139 m_currentPath.push( m_path );
140 init( aSchematic, aProperties );
141
142 if( aAppendToMe == nullptr )
143 {
144 // Clean up any allocated memory if an exception occurs loading the schematic.
145 std::unique_ptr<SCH_SHEET> newSheet = std::make_unique<SCH_SHEET>( aSchematic );
146
147 wxFileName relPath( aFileName );
148
149 // Do not use wxPATH_UNIX as option in MakeRelativeTo(). It can create incorrect
150 // relative paths on Windows, because paths have a disk identifier (C:, D: ...)
151 relPath.MakeRelativeTo( aSchematic->Project().GetProjectPath() );
152
153 newSheet->SetFileName( relPath.GetFullPath() );
154 m_rootSheet = newSheet.get();
155 loadHierarchy( SCH_SHEET_PATH(), newSheet.get() );
156
157 // If we got here, the schematic loaded successfully.
158 sheet = newSheet.release();
159 m_rootSheet = nullptr; // Quiet Coverity warning.
160 }
161 else
162 {
163 wxCHECK_MSG( aSchematic->IsValid(), nullptr, "Can't append to a schematic with no root!" );
164 m_rootSheet = &aSchematic->Root();
165 sheet = aAppendToMe;
166 loadHierarchy( SCH_SHEET_PATH(), sheet );
167 }
168
169 wxASSERT( m_currentPath.size() == 1 ); // only the project path should remain
170
171 m_currentPath.pop(); // Clear the path stack for next call to Load
172
173 return sheet;
174}
175
176
177// Everything below this comment is recursive. Modify with care.
178
179void SCH_IO_KICAD_SEXPR::loadHierarchy( const SCH_SHEET_PATH& aParentSheetPath, SCH_SHEET* aSheet )
180{
182
183 SCH_SCREEN* screen = nullptr;
184
185 if( !aSheet->GetScreen() )
186 {
187 // SCH_SCREEN objects store the full path and file name where the SCH_SHEET object only
188 // stores the file name and extension. Add the project path to the file name and
189 // extension to compare when calling SCH_SHEET::SearchHierarchy().
190 wxFileName fileName = aSheet->GetFileName();
191
192 if( !fileName.IsAbsolute() )
193 fileName.MakeAbsolute( m_currentPath.top() );
194
195 // Save the current path so that it gets restored when descending and ascending the
196 // sheet hierarchy which allows for sheet schematic files to be nested in folders
197 // relative to the last path a schematic was loaded from.
198 wxLogTrace( traceSchPlugin, "Saving path '%s'", m_currentPath.top() );
199 m_currentPath.push( fileName.GetPath() );
200 wxLogTrace( traceSchPlugin, "Current path '%s'", m_currentPath.top() );
201 wxLogTrace( traceSchPlugin, "Loading '%s'", fileName.GetFullPath() );
202
203 SCH_SHEET_PATH ancestorSheetPath = aParentSheetPath;
204
205 while( !ancestorSheetPath.empty() )
206 {
207 if( ancestorSheetPath.LastScreen()->GetFileName() == fileName.GetFullPath() )
208 {
209 if( !m_error.IsEmpty() )
210 m_error += "\n";
211
212 m_error += wxString::Format( _( "Could not load sheet '%s' because it already "
213 "appears as a direct ancestor in the schematic "
214 "hierarchy." ),
215 fileName.GetFullPath() );
216
217 fileName = wxEmptyString;
218
219 break;
220 }
221
222 ancestorSheetPath.pop_back();
223 }
224
225 if( ancestorSheetPath.empty() )
226 {
227 // Existing schematics could be either in the root sheet path or the current sheet
228 // load path so we have to check both.
229 if( !m_rootSheet->SearchHierarchy( fileName.GetFullPath(), &screen ) )
230 m_currentSheetPath.at( 0 )->SearchHierarchy( fileName.GetFullPath(), &screen );
231 }
232
233 if( screen )
234 {
235 aSheet->SetScreen( screen );
236 aSheet->GetScreen()->SetParent( m_schematic );
237 // Do not need to load the sub-sheets - this has already been done.
238 }
239 else
240 {
241 aSheet->SetScreen( new SCH_SCREEN( m_schematic ) );
242 aSheet->GetScreen()->SetFileName( fileName.GetFullPath() );
243
244 try
245 {
246 loadFile( fileName.GetFullPath(), aSheet );
247 }
248 catch( const IO_ERROR& ioe )
249 {
250 // If there is a problem loading the root sheet, there is no recovery.
251 if( aSheet == m_rootSheet )
252 throw;
253
254 // For all subsheets, queue up the error message for the caller.
255 if( !m_error.IsEmpty() )
256 m_error += "\n";
257
258 m_error += ioe.What();
259 }
260
261 if( fileName.FileExists() )
262 {
263 aSheet->GetScreen()->SetFileReadOnly( !fileName.IsFileWritable() );
264 aSheet->GetScreen()->SetFileExists( true );
265 }
266 else
267 {
268 aSheet->GetScreen()->SetFileReadOnly( !fileName.IsDirWritable() );
269 aSheet->GetScreen()->SetFileExists( false );
270 }
271
272 SCH_SHEET_PATH currentSheetPath = aParentSheetPath;
273 currentSheetPath.push_back( aSheet );
274
275 // This was moved out of the try{} block so that any sheet definitions that
276 // the plugin fully parsed before the exception was raised will be loaded.
277 for( SCH_ITEM* aItem : aSheet->GetScreen()->Items().OfType( SCH_SHEET_T ) )
278 {
279 wxCHECK2( aItem->Type() == SCH_SHEET_T, /* do nothing */ );
280 SCH_SHEET* sheet = static_cast<SCH_SHEET*>( aItem );
281
282 // Recursion starts here.
283 loadHierarchy( currentSheetPath, sheet );
284 }
285 }
286
287 m_currentPath.pop();
288 wxLogTrace( traceSchPlugin, "Restoring path \"%s\"", m_currentPath.top() );
289 }
290
292}
293
294
295void SCH_IO_KICAD_SEXPR::loadFile( const wxString& aFileName, SCH_SHEET* aSheet )
296{
297 FILE_LINE_READER reader( aFileName );
298
299 size_t lineCount = 0;
300
302 {
303 m_progressReporter->Report( wxString::Format( _( "Loading %s..." ), aFileName ) );
304
306 THROW_IO_ERROR( _( "Open canceled by user." ) );
307
308 while( reader.ReadLine() )
309 lineCount++;
310
311 reader.Rewind();
312 }
313
314 SCH_IO_KICAD_SEXPR_PARSER parser( &reader, m_progressReporter, lineCount, m_rootSheet,
315 m_appending );
316
317 parser.ParseSchematic( aSheet );
318}
319
320
321void SCH_IO_KICAD_SEXPR::LoadContent( LINE_READER& aReader, SCH_SHEET* aSheet, int aFileVersion )
322{
323 wxCHECK( aSheet, /* void */ );
324
325 SCH_IO_KICAD_SEXPR_PARSER parser( &aReader );
326
327 parser.ParseSchematic( aSheet, true, aFileVersion );
328}
329
330
331void SCH_IO_KICAD_SEXPR::SaveSchematicFile( const wxString& aFileName, SCH_SHEET* aSheet,
332 SCHEMATIC* aSchematic,
333 const std::map<std::string, UTF8>* aProperties )
334{
335 wxCHECK_RET( aSheet != nullptr, "NULL SCH_SHEET object." );
336 wxCHECK_RET( !aFileName.IsEmpty(), "No schematic file name defined." );
337
338 wxString sanityResult = aSheet->GetScreen()->GroupsSanityCheck();
339
340 if( sanityResult != wxEmptyString && m_queryUserCallback )
341 {
342 if( !m_queryUserCallback( _( "Internal Group Data Error" ), wxICON_ERROR,
343 wxString::Format( _( "Please report this bug. Error validating group "
344 "structure: %s\n\nSave anyway?" ),
345 sanityResult ),
346 _( "Save Anyway" ) ) )
347 {
348 return;
349 }
350 }
351
352 init( aSchematic, aProperties );
353
354 wxFileName fn = aFileName;
355
356 // File names should be absolute. Don't assume everything relative to the project path
357 // works properly.
358 wxASSERT( fn.IsAbsolute() );
359
360 PRETTIFIED_FILE_OUTPUTFORMATTER formatter( fn.GetFullPath() );
361
362 m_out = &formatter; // no ownership
363
364 Format( aSheet );
365
366 if( aSheet->GetScreen() )
367 aSheet->GetScreen()->SetFileExists( true );
368}
369
370
372{
373 wxCHECK_RET( aSheet != nullptr, "NULL SCH_SHEET* object." );
374 wxCHECK_RET( m_schematic != nullptr, "NULL SCHEMATIC* object." );
375
377 SCH_SCREEN* screen = aSheet->GetScreen();
378
379 wxCHECK( screen, /* void */ );
380
381 // If we've requested to embed the fonts in the schematic, do so.
382 // Otherwise, clear the embedded fonts from the schematic. Embedded
383 // fonts will be used if available
386 else
388
389 m_out->Print( "(kicad_sch (version %d) (generator \"eeschema\") (generator_version %s)",
391 m_out->Quotew( GetMajorMinorVersion() ).c_str() );
392
394
395 screen->GetPageSettings().Format( m_out );
396 screen->GetTitleBlock().Format( m_out );
397
398 // Save cache library.
399 m_out->Print( "(lib_symbols" );
400
401 for( const auto& [ libItemName, libSymbol ] : screen->GetLibSymbols() )
402 SCH_IO_KICAD_SEXPR_LIB_CACHE::SaveSymbol( libSymbol, *m_out, libItemName );
403
404 m_out->Print( ")" );
405
406 for( const std::shared_ptr<BUS_ALIAS>& alias : screen->GetBusAliases() )
407 saveBusAlias( alias );
408
409 // Enforce item ordering
410 auto cmp =
411 []( const SCH_ITEM* a, const SCH_ITEM* b )
412 {
413 if( a->Type() != b->Type() )
414 return a->Type() < b->Type();
415
416 return a->m_Uuid < b->m_Uuid;
417 };
418
419 std::multiset<SCH_ITEM*, decltype( cmp )> save_map( cmp );
420
421 for( SCH_ITEM* item : screen->Items() )
422 {
423 // Markers are not saved, so keep them from being considered below
424 if( item->Type() != SCH_MARKER_T )
425 save_map.insert( item );
426 }
427
428 for( SCH_ITEM* item : save_map )
429 {
430 switch( item->Type() )
431 {
432 case SCH_SYMBOL_T:
433 saveSymbol( static_cast<SCH_SYMBOL*>( item ), *m_schematic, sheets, false );
434 break;
435
436 case SCH_BITMAP_T:
437 saveBitmap( static_cast<SCH_BITMAP&>( *item ) );
438 break;
439
440 case SCH_SHEET_T:
441 saveSheet( static_cast<SCH_SHEET*>( item ), sheets );
442 break;
443
444 case SCH_JUNCTION_T:
445 saveJunction( static_cast<SCH_JUNCTION*>( item ) );
446 break;
447
448 case SCH_NO_CONNECT_T:
449 saveNoConnect( static_cast<SCH_NO_CONNECT*>( item ) );
450 break;
451
454 saveBusEntry( static_cast<SCH_BUS_ENTRY_BASE*>( item ) );
455 break;
456
457 case SCH_LINE_T:
458 saveLine( static_cast<SCH_LINE*>( item ) );
459 break;
460
461 case SCH_SHAPE_T:
462 saveShape( static_cast<SCH_SHAPE*>( item ) );
463 break;
464
465 case SCH_RULE_AREA_T:
466 saveRuleArea( static_cast<SCH_RULE_AREA*>( item ) );
467 break;
468
469 case SCH_TEXT_T:
470 case SCH_LABEL_T:
472 case SCH_HIER_LABEL_T:
474 saveText( static_cast<SCH_TEXT*>( item ) );
475 break;
476
477 case SCH_TEXTBOX_T:
478 saveTextBox( static_cast<SCH_TEXTBOX*>( item ) );
479 break;
480
481 case SCH_TABLE_T:
482 saveTable( static_cast<SCH_TABLE*>( item ) );
483 break;
484
485 case SCH_GROUP_T:
486 saveGroup( static_cast<SCH_GROUP*>( item ) );
487 break;
488
489 default:
490 wxASSERT( "Unexpected schematic object type in SCH_IO_KICAD_SEXPR::Format()" );
491 }
492 }
493
494 if( aSheet->HasRootInstance() )
495 {
496 std::vector< SCH_SHEET_INSTANCE> instances;
497
498 instances.emplace_back( aSheet->GetRootInstance() );
499 saveInstances( instances );
500
502
503 // Save any embedded files
506 }
507
508 m_out->Print( ")" );
509}
510
511
512void SCH_IO_KICAD_SEXPR::Format( SCH_SELECTION* aSelection, SCH_SHEET_PATH* aSelectionPath,
513 SCHEMATIC& aSchematic, OUTPUTFORMATTER* aFormatter,
514 bool aForClipboard )
515{
516 wxCHECK( aSelection && aSelectionPath && aFormatter, /* void */ );
517
518 SCH_SHEET_LIST sheets = aSchematic.Hierarchy();
519
520 m_schematic = &aSchematic;
521 m_out = aFormatter;
522
523 std::map<wxString, LIB_SYMBOL*> libSymbols;
524 SCH_SCREEN* screen = aSelection->GetScreen();
525 std::set<SCH_TABLE*> promotedTables;
526
527 for( EDA_ITEM* item : *aSelection )
528 {
529 if( item->Type() != SCH_SYMBOL_T )
530 continue;
531
532 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
533
534 wxString libSymbolLookup = symbol->GetLibId().Format().wx_str();
535
536 if( !symbol->UseLibIdLookup() )
537 libSymbolLookup = symbol->GetSchSymbolLibraryName();
538
539 auto it = screen->GetLibSymbols().find( libSymbolLookup );
540
541 if( it != screen->GetLibSymbols().end() )
542 libSymbols[ libSymbolLookup ] = it->second;
543 }
544
545 if( !libSymbols.empty() )
546 {
547 m_out->Print( "(lib_symbols" );
548
549 for( const auto& [name, libSymbol] : libSymbols )
551
552 m_out->Print( ")" );
553 }
554
555 for( EDA_ITEM* edaItem : *aSelection )
556 {
557 if( !edaItem->IsSCH_ITEM() )
558 continue;
559
560 SCH_ITEM* item = static_cast<SCH_ITEM*>( edaItem );
561
562 switch( item->Type() )
563 {
564 case SCH_SYMBOL_T:
565 saveSymbol( static_cast<SCH_SYMBOL*>( item ), aSchematic, sheets, aForClipboard, aSelectionPath );
566 break;
567
568 case SCH_BITMAP_T:
569 saveBitmap( static_cast<SCH_BITMAP&>( *item ) );
570 break;
571
572 case SCH_SHEET_T:
573 saveSheet( static_cast<SCH_SHEET*>( item ), sheets );
574 break;
575
576 case SCH_JUNCTION_T:
577 saveJunction( static_cast<SCH_JUNCTION*>( item ) );
578 break;
579
580 case SCH_NO_CONNECT_T:
581 saveNoConnect( static_cast<SCH_NO_CONNECT*>( item ) );
582 break;
583
586 saveBusEntry( static_cast<SCH_BUS_ENTRY_BASE*>( item ) );
587 break;
588
589 case SCH_LINE_T:
590 saveLine( static_cast<SCH_LINE*>( item ) );
591 break;
592
593 case SCH_SHAPE_T:
594 saveShape( static_cast<SCH_SHAPE*>( item ) );
595 break;
596
597 case SCH_RULE_AREA_T:
598 saveRuleArea( static_cast<SCH_RULE_AREA*>( item ) );
599 break;
600
601 case SCH_TEXT_T:
602 case SCH_LABEL_T:
604 case SCH_HIER_LABEL_T:
606 saveText( static_cast<SCH_TEXT*>( item ) );
607 break;
608
609 case SCH_TEXTBOX_T:
610 saveTextBox( static_cast<SCH_TEXTBOX*>( item ) );
611 break;
612
613 case SCH_TABLECELL_T:
614 {
615 SCH_TABLE* table = static_cast<SCH_TABLE*>( item->GetParent() );
616
617 if( promotedTables.count( table ) )
618 break;
619
620 table->SetFlags( SKIP_STRUCT );
621 saveTable( table );
622 table->ClearFlags( SKIP_STRUCT );
623 promotedTables.insert( table );
624 break;
625 }
626
627 case SCH_TABLE_T:
628 item->ClearFlags( SKIP_STRUCT );
629 saveTable( static_cast<SCH_TABLE*>( item ) );
630 break;
631
632 case SCH_GROUP_T:
633 saveGroup( static_cast<SCH_GROUP*>( item ) );
634 break;
635
636 default:
637 wxASSERT( "Unexpected schematic object type in SCH_IO_KICAD_SEXPR::Format()" );
638 }
639 }
640}
641
642
643void SCH_IO_KICAD_SEXPR::saveSymbol( SCH_SYMBOL* aSymbol, const SCHEMATIC& aSchematic,
644 const SCH_SHEET_LIST& aSheetList, bool aForClipboard,
645 const SCH_SHEET_PATH* aRelativePath )
646{
647 wxCHECK_RET( aSymbol != nullptr && m_out != nullptr, "" );
648
649 std::string libName;
650
651 wxString symbol_name = aSymbol->GetLibId().Format();
652
653 if( symbol_name.size() )
654 {
655 libName = toUTFTildaText( symbol_name );
656 }
657 else
658 {
659 libName = "_NONAME_";
660 }
661
662 EDA_ANGLE angle;
663 int orientation = aSymbol->GetOrientation() & ~( SYM_MIRROR_X | SYM_MIRROR_Y );
664
665 if( orientation == SYM_ORIENT_90 )
666 angle = ANGLE_90;
667 else if( orientation == SYM_ORIENT_180 )
668 angle = ANGLE_180;
669 else if( orientation == SYM_ORIENT_270 )
670 angle = ANGLE_270;
671 else
672 angle = ANGLE_0;
673
674 m_out->Print( "(symbol" );
675
676 if( !aSymbol->UseLibIdLookup() )
677 {
678 m_out->Print( "(lib_name %s)",
679 m_out->Quotew( aSymbol->GetSchSymbolLibraryName() ).c_str() );
680 }
681
682 m_out->Print( "(lib_id %s) (at %s %s %s)",
683 m_out->Quotew( aSymbol->GetLibId().Format().wx_str() ).c_str(),
685 aSymbol->GetPosition().x ).c_str(),
687 aSymbol->GetPosition().y ).c_str(),
688 EDA_UNIT_UTILS::FormatAngle( angle ).c_str() );
689
690 bool mirrorX = aSymbol->GetOrientation() & SYM_MIRROR_X;
691 bool mirrorY = aSymbol->GetOrientation() & SYM_MIRROR_Y;
692
693 if( mirrorX || mirrorY )
694 {
695 m_out->Print( "(mirror %s %s)",
696 mirrorX ? "x" : "",
697 mirrorY ? "y" : "" );
698 }
699
700 // The symbol unit is always set to the ordianal instance regardless of the current sheet
701 // instance to prevent file churn.
702 SCH_SYMBOL_INSTANCE ordinalInstance;
703
704 ordinalInstance.m_Reference = aSymbol->GetPrefix();
705
706 const SCH_SCREEN* parentScreen = static_cast<const SCH_SCREEN*>( aSymbol->GetParent() );
707
708 wxASSERT( parentScreen );
709
710 if( parentScreen && m_schematic )
711 {
712 std::optional<SCH_SHEET_PATH> ordinalPath =
713 m_schematic->Hierarchy().GetOrdinalPath( parentScreen );
714
715 // Design blocks are saved from a temporary sheet & screen which will not be found in
716 // the schematic, and will therefore have no ordinal path.
717 // wxASSERT( ordinalPath );
718
719 if( ordinalPath )
720 aSymbol->GetInstance( ordinalInstance, ordinalPath->Path() );
721 else if( aSymbol->GetInstances().size() )
722 ordinalInstance = aSymbol->GetInstances()[0];
723 }
724
725 int unit = ordinalInstance.m_Unit;
726
727 if( aForClipboard && aRelativePath )
728 {
729 SCH_SYMBOL_INSTANCE unitInstance;
730
731 if( aSymbol->GetInstance( unitInstance, aRelativePath->Path() ) )
732 unit = unitInstance.m_Unit;
733 }
734
735 m_out->Print( "(unit %d)", unit );
736
737 if( aSymbol->GetBodyStyle() == BODY_STYLE::DEMORGAN )
738 m_out->Print( "(convert %d)", aSymbol->GetBodyStyle() );
739
740 KICAD_FORMAT::FormatBool( m_out, "exclude_from_sim", aSymbol->GetExcludedFromSim() );
741 KICAD_FORMAT::FormatBool( m_out, "in_bom", !aSymbol->GetExcludedFromBOM() );
742 KICAD_FORMAT::FormatBool( m_out, "on_board", !aSymbol->GetExcludedFromBoard() );
743 KICAD_FORMAT::FormatBool( m_out, "dnp", aSymbol->GetDNP() );
744
745 AUTOPLACE_ALGO fieldsAutoplaced = aSymbol->GetFieldsAutoplaced();
746
747 if( fieldsAutoplaced == AUTOPLACE_AUTO || fieldsAutoplaced == AUTOPLACE_MANUAL )
748 KICAD_FORMAT::FormatBool( m_out, "fields_autoplaced", true );
749
751
752 std::vector<SCH_FIELD*> orderedFields;
753 aSymbol->GetFields( orderedFields, false );
754
755 for( SCH_FIELD* field : orderedFields )
756 {
757 FIELD_T id = field->GetId();
758 wxString value = field->GetText();
759
760 if( !aForClipboard && aSymbol->GetInstances().size() )
761 {
762 // The instance fields are always set to the default instance regardless of the
763 // sheet instance to prevent file churn.
764 if( id == FIELD_T::REFERENCE )
765 field->SetText( ordinalInstance.m_Reference );
766 }
767 else if( aForClipboard && aSymbol->GetInstances().size() && aRelativePath
768 && ( id == FIELD_T::REFERENCE ) )
769 {
770 SCH_SYMBOL_INSTANCE instance;
771
772 if( aSymbol->GetInstance( instance, aRelativePath->Path() ) )
773 field->SetText( instance.m_Reference );
774 }
775
776 try
777 {
778 saveField( field );
779 }
780 catch( ... )
781 {
782 // Restore the changed field text on write error.
783 if( id == FIELD_T::REFERENCE )
784 field->SetText( value );
785
786 throw;
787 }
788
789 if( id == FIELD_T::REFERENCE )
790 field->SetText( value );
791 }
792
793 for( const std::unique_ptr<SCH_PIN>& pin : aSymbol->GetRawPins() )
794 {
795 // There was a bug introduced somewhere in the original alternated pin code that would
796 // set the alternate pin to the default pin name which caused a number of library symbol
797 // comparison issues. Clearing the alternate pin resolves this issue.
798 if( pin->GetAlt().IsEmpty() || ( pin->GetAlt() == pin->GetBaseName() ) )
799 {
800 m_out->Print( "(pin %s", m_out->Quotew( pin->GetNumber() ).c_str() );
802 m_out->Print( ")" );
803 }
804 else
805 {
806 m_out->Print( "(pin %s", m_out->Quotew( pin->GetNumber() ).c_str() );
808 m_out->Print( "(alternate %s))", m_out->Quotew( pin->GetAlt() ).c_str() );
809 }
810 }
811
812 if( !aSymbol->GetInstances().empty() )
813 {
814 std::map<KIID, std::vector<SCH_SYMBOL_INSTANCE>> projectInstances;
815
816 m_out->Print( "(instances" );
817
818 wxString projectName;
819 KIID rootSheetUuid = aSchematic.Root().m_Uuid;
820
821 for( const SCH_SYMBOL_INSTANCE& inst : aSymbol->GetInstances() )
822 {
823 // Zero length KIID_PATH objects are not valid and will cause a crash below.
824 wxCHECK2( inst.m_Path.size(), continue );
825
826 // If the instance data is part of this design but no longer has an associated sheet
827 // path, don't save it. This prevents large amounts of orphaned instance data for the
828 // current project from accumulating in the schematic files.
829 bool isOrphaned = ( inst.m_Path[0] == rootSheetUuid )
830 && !aSheetList.GetSheetPathByKIIDPath( inst.m_Path );
831
832 // Keep all instance data when copying to the clipboard. They may be needed on paste.
833 if( !aForClipboard && isOrphaned )
834 continue;
835
836 auto it = projectInstances.find( inst.m_Path[0] );
837
838 if( it == projectInstances.end() )
839 projectInstances[ inst.m_Path[0] ] = { inst };
840 else
841 it->second.emplace_back( inst );
842 }
843
844 for( auto& [uuid, instances] : projectInstances )
845 {
846 wxCHECK2( instances.size(), continue );
847
848 // Sort project instances by KIID_PATH.
849 std::sort( instances.begin(), instances.end(),
851 {
852 return aLhs.m_Path < aRhs.m_Path;
853 } );
854
855 projectName = instances[0].m_ProjectName;
856
857 m_out->Print( "(project %s", m_out->Quotew( projectName ).c_str() );
858
859 for( const SCH_SYMBOL_INSTANCE& instance : instances )
860 {
861 wxString path;
862 KIID_PATH tmp = instance.m_Path;
863
864 if( aForClipboard && aRelativePath )
865 tmp.MakeRelativeTo( aRelativePath->Path() );
866
867 path = tmp.AsString();
868
869 m_out->Print( "(path %s (reference %s) (unit %d))",
870 m_out->Quotew( path ).c_str(),
871 m_out->Quotew( instance.m_Reference ).c_str(),
872 instance.m_Unit );
873 }
874
875 m_out->Print( ")" ); // Closes `project`.
876 }
877
878 m_out->Print( ")" ); // Closes `instances`.
879 }
880
881 m_out->Print( ")" ); // Closes `symbol`.
882}
883
884
886{
887 wxCHECK_RET( aField != nullptr && m_out != nullptr, "" );
888
889 wxString fieldName;
890
891 if( aField->IsMandatory() )
892 fieldName = aField->GetCanonicalName();
893 else
894 fieldName = aField->GetName();
895
896 m_out->Print( "(property %s %s %s (at %s %s %s)",
897 aField->IsPrivate() ? "private" : "",
898 m_out->Quotew( fieldName ).c_str(),
899 m_out->Quotew( aField->GetText() ).c_str(),
901 aField->GetPosition().x ).c_str(),
903 aField->GetPosition().y ).c_str(),
904 EDA_UNIT_UTILS::FormatAngle( aField->GetTextAngle() ).c_str() );
905
906 if( !aField->IsVisible() )
907 KICAD_FORMAT::FormatBool( m_out, "hide", true );
908
909 if( aField->IsNameShown() )
910 KICAD_FORMAT::FormatBool( m_out, "show_name", true );
911
912 if( !aField->CanAutoplace() )
913 KICAD_FORMAT::FormatBool( m_out, "do_not_autoplace", true );
914
915 if( !aField->IsDefaultFormatting()
916 || ( aField->GetTextHeight() != schIUScale.MilsToIU( DEFAULT_SIZE_TEXT ) ) )
917 {
918 aField->Format( m_out, 0 );
919 }
920
921 m_out->Print( ")" ); // Closes `property` token
922}
923
924
926{
927 wxCHECK_RET( m_out != nullptr, "" );
928
929 const REFERENCE_IMAGE& refImage = aBitmap.GetReferenceImage();
930 const BITMAP_BASE& bitmapBase = refImage.GetImage();
931
932 const wxImage* image = bitmapBase.GetImageData();
933
934 wxCHECK_RET( image != nullptr, "wxImage* is NULL" );
935
936 m_out->Print( "(image (at %s %s)",
938 refImage.GetPosition().x ).c_str(),
940 refImage.GetPosition().y ).c_str() );
941
942 double scale = refImage.GetImageScale();
943
944 // 20230121 or older file format versions assumed 300 image PPI at load/save.
945 // Let's keep compatibility by changing image scale.
946 if( SEXPR_SCHEMATIC_FILE_VERSION <= 20230121 )
947 scale = scale * 300.0 / bitmapBase.GetPPI();
948
949 if( scale != 1.0 )
950 m_out->Print( "%s", fmt::format("(scale {:g})", refImage.GetImageScale()).c_str() );
951
953
954 wxMemoryOutputStream stream;
955 bitmapBase.SaveImageData( stream );
956
957 KICAD_FORMAT::FormatStreamData( *m_out, *stream.GetOutputStreamBuffer() );
958
959 m_out->Print( ")" ); // Closes image token.
960}
961
962
964{
965 wxCHECK_RET( aSheet != nullptr && m_out != nullptr, "" );
966
967 m_out->Print( "(sheet (at %s %s) (size %s %s)",
969 aSheet->GetPosition().x ).c_str(),
971 aSheet->GetPosition().y ).c_str(),
973 aSheet->GetSize().x ).c_str(),
975 aSheet->GetSize().y ).c_str() );
976
977 KICAD_FORMAT::FormatBool( m_out, "exclude_from_sim", aSheet->GetExcludedFromSim() );
978 KICAD_FORMAT::FormatBool( m_out, "in_bom", !aSheet->GetExcludedFromBOM() );
979 KICAD_FORMAT::FormatBool( m_out, "on_board", !aSheet->GetExcludedFromBoard() );
980 KICAD_FORMAT::FormatBool( m_out, "dnp", aSheet->GetDNP() );
981
982 AUTOPLACE_ALGO fieldsAutoplaced = aSheet->GetFieldsAutoplaced();
983
984 if( fieldsAutoplaced == AUTOPLACE_AUTO || fieldsAutoplaced == AUTOPLACE_MANUAL )
985 KICAD_FORMAT::FormatBool( m_out, "fields_autoplaced", true );
986
987 STROKE_PARAMS stroke( aSheet->GetBorderWidth(), LINE_STYLE::SOLID, aSheet->GetBorderColor() );
988
989 stroke.SetWidth( aSheet->GetBorderWidth() );
990 stroke.Format( m_out, schIUScale );
991
992 m_out->Print( "(fill (color %d %d %d %s))",
993 KiROUND( aSheet->GetBackgroundColor().r * 255.0 ),
994 KiROUND( aSheet->GetBackgroundColor().g * 255.0 ),
995 KiROUND( aSheet->GetBackgroundColor().b * 255.0 ),
996 FormatDouble2Str( aSheet->GetBackgroundColor().a ).c_str() );
997
999
1000 for( SCH_FIELD& field : aSheet->GetFields() )
1001 saveField( &field );
1002
1003 for( const SCH_SHEET_PIN* pin : aSheet->GetPins() )
1004 {
1005 m_out->Print( "(pin %s %s (at %s %s %s)",
1006 EscapedUTF8( pin->GetText() ).c_str(),
1007 getSheetPinShapeToken( pin->GetShape() ),
1009 pin->GetPosition().x ).c_str(),
1011 pin->GetPosition().y ).c_str(),
1012 EDA_UNIT_UTILS::FormatAngle( getSheetPinAngle( pin->GetSide() ) ).c_str() );
1013
1015
1016 pin->Format( m_out, 0 );
1017
1018 m_out->Print( ")" ); // Closes pin token.
1019 }
1020
1021 // Save all sheet instances here except the root sheet instance.
1022 std::vector< SCH_SHEET_INSTANCE > sheetInstances = aSheet->GetInstances();
1023
1024 auto it = sheetInstances.begin();
1025
1026 while( it != sheetInstances.end() )
1027 {
1028 if( it->m_Path.size() == 0 )
1029 it = sheetInstances.erase( it );
1030 else
1031 it++;
1032 }
1033
1034 if( !sheetInstances.empty() )
1035 {
1036 m_out->Print( "(instances" );
1037
1038 KIID lastProjectUuid;
1039 KIID rootSheetUuid = m_schematic->Root().m_Uuid;
1040 bool inProjectClause = false;
1041
1042 for( size_t i = 0; i < sheetInstances.size(); i++ )
1043 {
1044 // If the instance data is part of this design but no longer has an associated sheet
1045 // path, don't save it. This prevents large amounts of orphaned instance data for the
1046 // current project from accumulating in the schematic files.
1047 //
1048 // Keep all instance data when copying to the clipboard. It may be needed on paste.
1049 if( ( sheetInstances[i].m_Path[0] == rootSheetUuid )
1050 && !aSheetList.GetSheetPathByKIIDPath( sheetInstances[i].m_Path, false ) )
1051 {
1052 if( inProjectClause && ( ( i + 1 == sheetInstances.size() )
1053 || lastProjectUuid != sheetInstances[i+1].m_Path[0] ) )
1054 {
1055 m_out->Print( ")" ); // Closes `project` token.
1056 inProjectClause = false;
1057 }
1058
1059 continue;
1060 }
1061
1062 if( lastProjectUuid != sheetInstances[i].m_Path[0] )
1063 {
1064 wxString projectName;
1065
1066 if( sheetInstances[i].m_Path[0] == rootSheetUuid )
1067 projectName = m_schematic->Project().GetProjectName();
1068 else
1069 projectName = sheetInstances[i].m_ProjectName;
1070
1071 lastProjectUuid = sheetInstances[i].m_Path[0];
1072 m_out->Print( "(project %s", m_out->Quotew( projectName ).c_str() );
1073 inProjectClause = true;
1074 }
1075
1076 wxString path = sheetInstances[i].m_Path.AsString();
1077
1078 m_out->Print( "(path %s (page %s))",
1079 m_out->Quotew( path ).c_str(),
1080 m_out->Quotew( sheetInstances[i].m_PageNumber ).c_str() );
1081
1082 if( inProjectClause && ( ( i + 1 == sheetInstances.size() )
1083 || lastProjectUuid != sheetInstances[i+1].m_Path[0] ) )
1084 {
1085 m_out->Print( ")" ); // Closes `project` token.
1086 inProjectClause = false;
1087 }
1088 }
1089
1090 m_out->Print( ")" ); // Closes `instances` token.
1091 }
1092
1093 m_out->Print( ")" ); // Closes sheet token.
1094}
1095
1096
1098{
1099 wxCHECK_RET( aJunction != nullptr && m_out != nullptr, "" );
1100
1101 m_out->Print( "(junction (at %s %s) (diameter %s) (color %d %d %d %s)",
1103 aJunction->GetPosition().x ).c_str(),
1105 aJunction->GetPosition().y ).c_str(),
1107 aJunction->GetDiameter() ).c_str(),
1108 KiROUND( aJunction->GetColor().r * 255.0 ),
1109 KiROUND( aJunction->GetColor().g * 255.0 ),
1110 KiROUND( aJunction->GetColor().b * 255.0 ),
1111 FormatDouble2Str( aJunction->GetColor().a ).c_str() );
1112
1113 KICAD_FORMAT::FormatUuid( m_out, aJunction->m_Uuid );
1114 m_out->Print( ")" );
1115}
1116
1117
1119{
1120 wxCHECK_RET( aNoConnect != nullptr && m_out != nullptr, "" );
1121
1122 m_out->Print( "(no_connect (at %s %s)",
1124 aNoConnect->GetPosition().x ).c_str(),
1126 aNoConnect->GetPosition().y ).c_str() );
1127
1128 KICAD_FORMAT::FormatUuid( m_out, aNoConnect->m_Uuid );
1129 m_out->Print( ")" );
1130}
1131
1132
1134{
1135 wxCHECK_RET( aBusEntry != nullptr && m_out != nullptr, "" );
1136
1137 // Bus to bus entries are converted to bus line segments.
1138 if( aBusEntry->GetClass() == "SCH_BUS_BUS_ENTRY" )
1139 {
1140 SCH_LINE busEntryLine( aBusEntry->GetPosition(), LAYER_BUS );
1141
1142 busEntryLine.SetEndPoint( aBusEntry->GetEnd() );
1143 saveLine( &busEntryLine );
1144 return;
1145 }
1146
1147 m_out->Print( "(bus_entry (at %s %s) (size %s %s)",
1149 aBusEntry->GetPosition().x ).c_str(),
1151 aBusEntry->GetPosition().y ).c_str(),
1153 aBusEntry->GetSize().x ).c_str(),
1155 aBusEntry->GetSize().y ).c_str() );
1156
1157 aBusEntry->GetStroke().Format( m_out, schIUScale );
1158 KICAD_FORMAT::FormatUuid( m_out, aBusEntry->m_Uuid );
1159 m_out->Print( ")" );
1160}
1161
1162
1164{
1165 wxCHECK_RET( aShape != nullptr && m_out != nullptr, "" );
1166
1167 switch( aShape->GetShape() )
1168 {
1169 case SHAPE_T::ARC:
1170 formatArc( m_out, aShape, false, aShape->GetStroke(), aShape->GetFillMode(),
1171 aShape->GetFillColor(), false, aShape->m_Uuid );
1172 break;
1173
1174 case SHAPE_T::CIRCLE:
1175 formatCircle( m_out, aShape, false, aShape->GetStroke(), aShape->GetFillMode(),
1176 aShape->GetFillColor(), false, aShape->m_Uuid );
1177 break;
1178
1179 case SHAPE_T::RECTANGLE:
1180 formatRect( m_out, aShape, false, aShape->GetStroke(), aShape->GetFillMode(),
1181 aShape->GetFillColor(), false, aShape->m_Uuid );
1182 break;
1183
1184 case SHAPE_T::BEZIER:
1185 formatBezier( m_out, aShape, false, aShape->GetStroke(), aShape->GetFillMode(),
1186 aShape->GetFillColor(), false, aShape->m_Uuid );
1187 break;
1188
1189 case SHAPE_T::POLY:
1190 formatPoly( m_out, aShape, false, aShape->GetStroke(), aShape->GetFillMode(),
1191 aShape->GetFillColor(), false, aShape->m_Uuid );
1192 break;
1193
1194 default:
1196 }
1197}
1198
1199
1201{
1202 wxCHECK_RET( aRuleArea != nullptr && m_out != nullptr, "" );
1203
1204 m_out->Print( "(rule_area " );
1205
1206 KICAD_FORMAT::FormatBool( m_out, "exclude_from_sim", aRuleArea->GetExcludedFromSim() );
1207 KICAD_FORMAT::FormatBool( m_out, "in_bom", !aRuleArea->GetExcludedFromBOM() );
1208 KICAD_FORMAT::FormatBool( m_out, "on_board", !aRuleArea->GetExcludedFromBoard() );
1209 KICAD_FORMAT::FormatBool( m_out, "dnp", aRuleArea->GetDNP() );
1210
1211 saveShape( aRuleArea );
1212
1213 m_out->Print( ")" );
1214}
1215
1216
1218{
1219 wxCHECK_RET( aLine != nullptr && m_out != nullptr, "" );
1220
1221 wxString lineType;
1222
1223 STROKE_PARAMS line_stroke = aLine->GetStroke();
1224
1225 switch( aLine->GetLayer() )
1226 {
1227 case LAYER_BUS: lineType = "bus"; break;
1228 case LAYER_WIRE: lineType = "wire"; break;
1229 case LAYER_NOTES: lineType = "polyline"; break;
1230 default:
1231 UNIMPLEMENTED_FOR( LayerName( aLine->GetLayer() ) );
1232 }
1233
1234 m_out->Print( "(%s (pts (xy %s %s) (xy %s %s))",
1235 TO_UTF8( lineType ),
1237 aLine->GetStartPoint().x ).c_str(),
1239 aLine->GetStartPoint().y ).c_str(),
1241 aLine->GetEndPoint().x ).c_str(),
1243 aLine->GetEndPoint().y ).c_str() );
1244
1245 line_stroke.Format( m_out, schIUScale );
1247 m_out->Print( ")" );
1248}
1249
1250
1252{
1253 wxCHECK_RET( aText != nullptr && m_out != nullptr, "" );
1254
1255 // Note: label is nullptr SCH_TEXT, but not for SCH_LABEL_XXX,
1256 SCH_LABEL_BASE* label = dynamic_cast<SCH_LABEL_BASE*>( aText );
1257
1258 m_out->Print( "(%s %s",
1259 getTextTypeToken( aText->Type() ),
1260 m_out->Quotew( aText->GetText() ).c_str() );
1261
1262 if( aText->Type() == SCH_TEXT_T )
1263 KICAD_FORMAT::FormatBool( m_out, "exclude_from_sim", aText->GetExcludedFromSim() );
1264
1265 if( aText->Type() == SCH_DIRECTIVE_LABEL_T )
1266 {
1267 SCH_DIRECTIVE_LABEL* flag = static_cast<SCH_DIRECTIVE_LABEL*>( aText );
1268
1269 m_out->Print( "(length %s)",
1271 flag->GetPinLength() ).c_str() );
1272 }
1273
1274 EDA_ANGLE angle = aText->GetTextAngle();
1275
1276 if( label )
1277 {
1278 if( label->Type() == SCH_GLOBAL_LABEL_T
1279 || label->Type() == SCH_HIER_LABEL_T
1280 || label->Type() == SCH_DIRECTIVE_LABEL_T )
1281 {
1282 m_out->Print( "(shape %s)", getSheetPinShapeToken( label->GetShape() ) );
1283 }
1284
1285 // The angle of the text is always 0 or 90 degrees for readibility reasons,
1286 // but the item itself can have more rotation (-90 and 180 deg)
1287 switch( label->GetSpinStyle() )
1288 {
1289 default:
1290 case SPIN_STYLE::LEFT: angle += ANGLE_180; break;
1291 case SPIN_STYLE::UP: break;
1292 case SPIN_STYLE::RIGHT: break;
1293 case SPIN_STYLE::BOTTOM: angle += ANGLE_180; break;
1294 }
1295 }
1296
1297 m_out->Print( "(at %s %s %s)",
1299 aText->GetPosition().x ).c_str(),
1301 aText->GetPosition().y ).c_str(),
1302 EDA_UNIT_UTILS::FormatAngle( angle ).c_str() );
1303
1304 if( label && !label->GetFields().empty() )
1305 {
1306 AUTOPLACE_ALGO fieldsAutoplaced = label->GetFieldsAutoplaced();
1307
1308 if( fieldsAutoplaced == AUTOPLACE_AUTO || fieldsAutoplaced == AUTOPLACE_MANUAL )
1309 KICAD_FORMAT::FormatBool( m_out, "fields_autoplaced", true );
1310 }
1311
1312 aText->EDA_TEXT::Format( m_out, 0 );
1314
1315 if( label )
1316 {
1317 for( SCH_FIELD& field : label->GetFields() )
1318 saveField( &field );
1319 }
1320
1321 m_out->Print( ")" ); // Closes text token.
1322}
1323
1324
1326{
1327 wxCHECK_RET( aTextBox != nullptr && m_out != nullptr, "" );
1328
1329 m_out->Print( "(%s %s",
1330 aTextBox->Type() == SCH_TABLECELL_T ? "table_cell" : "text_box",
1331 m_out->Quotew( aTextBox->GetText() ).c_str() );
1332
1333 KICAD_FORMAT::FormatBool( m_out, "exclude_from_sim", aTextBox->GetExcludedFromSim() );
1334
1335 VECTOR2I pos = aTextBox->GetStart();
1336 VECTOR2I size = aTextBox->GetEnd() - pos;
1337
1338 m_out->Print( "(at %s %s %s) (size %s %s) (margins %s %s %s %s)",
1341 EDA_UNIT_UTILS::FormatAngle( aTextBox->GetTextAngle() ).c_str(),
1348
1349 if( SCH_TABLECELL* cell = dynamic_cast<SCH_TABLECELL*>( aTextBox ) )
1350 m_out->Print( "(span %d %d)", cell->GetColSpan(), cell->GetRowSpan() );
1351
1352 if( aTextBox->Type() != SCH_TABLECELL_T )
1353 aTextBox->GetStroke().Format( m_out, schIUScale );
1354
1355 formatFill( m_out, aTextBox->GetFillMode(), aTextBox->GetFillColor() );
1356 aTextBox->EDA_TEXT::Format( m_out, 0 );
1358 m_out->Print( ")" );
1359}
1360
1361
1363{
1364 if( aTable->GetFlags() & SKIP_STRUCT )
1365 {
1366 aTable = static_cast<SCH_TABLE*>( aTable->Clone() );
1367
1368 int minCol = aTable->GetColCount();
1369 int maxCol = -1;
1370 int minRow = aTable->GetRowCount();
1371 int maxRow = -1;
1372
1373 for( int row = 0; row < aTable->GetRowCount(); ++row )
1374 {
1375 for( int col = 0; col < aTable->GetColCount(); ++col )
1376 {
1377 SCH_TABLECELL* cell = aTable->GetCell( row, col );
1378
1379 if( cell->IsSelected() )
1380 {
1381 minRow = std::min( minRow, row );
1382 maxRow = std::max( maxRow, row );
1383 minCol = std::min( minCol, col );
1384 maxCol = std::max( maxCol, col );
1385 }
1386 else
1387 {
1388 cell->SetFlags( STRUCT_DELETED );
1389 }
1390 }
1391 }
1392
1393 wxCHECK_MSG( maxCol >= minCol && maxRow >= minRow, /*void*/, wxT( "No selected cells!" ) );
1394
1395 int destRow = 0;
1396
1397 for( int row = minRow; row <= maxRow; row++ )
1398 aTable->SetRowHeight( destRow++, aTable->GetRowHeight( row ) );
1399
1400 int destCol = 0;
1401
1402 for( int col = minCol; col <= maxCol; col++ )
1403 aTable->SetColWidth( destCol++, aTable->GetColWidth( col ) );
1404
1405 aTable->DeleteMarkedCells();
1406 aTable->SetColCount( ( maxCol - minCol ) + 1 );
1407 }
1408
1409 wxCHECK_RET( aTable != nullptr && m_out != nullptr, "" );
1410
1411 m_out->Print( "(table (column_count %d)", aTable->GetColCount() );
1412
1413 m_out->Print( "(border" );
1414 KICAD_FORMAT::FormatBool( m_out, "external", aTable->StrokeExternal() );
1416
1417 if( aTable->StrokeExternal() || aTable->StrokeHeaderSeparator() )
1418 aTable->GetBorderStroke().Format( m_out, schIUScale );
1419
1420 m_out->Print( ")" ); // Close `border` token.
1421
1422 m_out->Print( "(separators" );
1423 KICAD_FORMAT::FormatBool( m_out, "rows", aTable->StrokeRows() );
1424 KICAD_FORMAT::FormatBool( m_out, "cols", aTable->StrokeColumns() );
1425
1426 if( aTable->StrokeRows() || aTable->StrokeColumns() )
1428
1429 m_out->Print( ")" ); // Close `separators` token.
1430
1431 m_out->Print( "(column_widths" );
1432
1433 for( int col = 0; col < aTable->GetColCount(); ++col )
1434 {
1435 m_out->Print( " %s",
1436 EDA_UNIT_UTILS::FormatInternalUnits( schIUScale, aTable->GetColWidth( col ) ).c_str() );
1437 }
1438
1439 m_out->Print( ")" );
1440
1441 m_out->Print( "(row_heights" );
1442
1443 for( int row = 0; row < aTable->GetRowCount(); ++row )
1444 {
1445 m_out->Print( " %s",
1446 EDA_UNIT_UTILS::FormatInternalUnits( schIUScale, aTable->GetRowHeight( row ) ).c_str() );
1447 }
1448
1449 m_out->Print( ")" );
1450
1452
1453 m_out->Print( "(cells" );
1454
1455 for( SCH_TABLECELL* cell : aTable->GetCells() )
1456 saveTextBox( cell );
1457
1458 m_out->Print( ")" ); // Close `cells` token.
1459 m_out->Print( ")" ); // Close `table` token.
1460
1461 if( aTable->GetFlags() & SKIP_STRUCT )
1462 delete aTable;
1463}
1464
1465
1467{
1468 // Don't write empty groups
1469 if( aGroup->GetItems().empty() )
1470 return;
1471
1472 m_out->Print( "(group %s", m_out->Quotew( aGroup->GetName() ).c_str() );
1473
1475
1476 if( aGroup->IsLocked() )
1477 KICAD_FORMAT::FormatBool( m_out, "locked", true );
1478
1479 if( aGroup->HasDesignBlockLink() )
1480 m_out->Print( "(lib_id \"%s\")", aGroup->GetDesignBlockLibId().Format().c_str() );
1481
1482 wxArrayString memberIds;
1483
1484 for( EDA_ITEM* member : aGroup->GetItems() )
1485 memberIds.Add( member->m_Uuid.AsString() );
1486
1487 memberIds.Sort();
1488
1489 m_out->Print( "(members" );
1490
1491 for( const wxString& memberId : memberIds )
1492 m_out->Print( " %s", m_out->Quotew( memberId ).c_str() );
1493
1494 m_out->Print( ")" ); // Close `members` token.
1495 m_out->Print( ")" ); // Close `group` token.
1496}
1497
1498
1499void SCH_IO_KICAD_SEXPR::saveBusAlias( std::shared_ptr<BUS_ALIAS> aAlias )
1500{
1501 wxCHECK_RET( aAlias != nullptr, "BUS_ALIAS* is NULL" );
1502
1503 wxString members;
1504
1505 for( const wxString& member : aAlias->Members() )
1506 {
1507 if( !members.IsEmpty() )
1508 members += wxS( " " );
1509
1510 members += m_out->Quotew( member );
1511 }
1512
1513 m_out->Print( "(bus_alias %s (members %s))",
1514 m_out->Quotew( aAlias->GetName() ).c_str(),
1515 TO_UTF8( members ) );
1516}
1517
1518
1519void SCH_IO_KICAD_SEXPR::saveInstances( const std::vector<SCH_SHEET_INSTANCE>& aInstances )
1520{
1521 if( aInstances.size() )
1522 {
1523 m_out->Print( "(sheet_instances" );
1524
1525 for( const SCH_SHEET_INSTANCE& instance : aInstances )
1526 {
1527 wxString path = instance.m_Path.AsString();
1528
1529 if( path.IsEmpty() )
1530 path = wxT( "/" ); // Root path
1531
1532 m_out->Print( "(path %s (page %s))",
1533 m_out->Quotew( path ).c_str(),
1534 m_out->Quotew( instance.m_PageNumber ).c_str() );
1535 }
1536
1537 m_out->Print( ")" ); // Close sheet instances token.
1538 }
1539}
1540
1541
1542void SCH_IO_KICAD_SEXPR::cacheLib( const wxString& aLibraryFileName,
1543 const std::map<std::string, UTF8>* aProperties )
1544{
1545 // Suppress font substitution warnings
1547
1548 if( !m_cache || !m_cache->IsFile( aLibraryFileName ) || m_cache->IsFileChanged() )
1549 {
1550 // a spectacular episode in memory management:
1551 delete m_cache;
1552 m_cache = new SCH_IO_KICAD_SEXPR_LIB_CACHE( aLibraryFileName );
1553
1554 if( !isBuffering( aProperties ) )
1555 m_cache->Load();
1556 }
1557}
1558
1559
1560bool SCH_IO_KICAD_SEXPR::isBuffering( const std::map<std::string, UTF8>* aProperties )
1561{
1562 return ( aProperties && aProperties->contains( SCH_IO_KICAD_SEXPR::PropBuffering ) );
1563}
1564
1565
1567{
1568 if( m_cache )
1569 return m_cache->GetModifyHash();
1570
1571 // If the cache hasn't been loaded, it hasn't been modified.
1572 return 0;
1573}
1574
1575
1576void SCH_IO_KICAD_SEXPR::EnumerateSymbolLib( wxArrayString& aSymbolNameList,
1577 const wxString& aLibraryPath,
1578 const std::map<std::string, UTF8>* aProperties )
1579{
1580 bool powerSymbolsOnly = ( aProperties &&
1581 aProperties->find( SYMBOL_LIB_TABLE::PropPowerSymsOnly ) != aProperties->end() );
1582
1583 cacheLib( aLibraryPath, aProperties );
1584
1585 const LIB_SYMBOL_MAP& symbols = m_cache->m_symbols;
1586
1587 for( LIB_SYMBOL_MAP::const_iterator it = symbols.begin(); it != symbols.end(); ++it )
1588 {
1589 if( !powerSymbolsOnly || it->second->IsPower() )
1590 aSymbolNameList.Add( it->first );
1591 }
1592}
1593
1594
1595void SCH_IO_KICAD_SEXPR::EnumerateSymbolLib( std::vector<LIB_SYMBOL*>& aSymbolList,
1596 const wxString& aLibraryPath,
1597 const std::map<std::string, UTF8>* aProperties )
1598{
1599 bool powerSymbolsOnly = ( aProperties &&
1600 aProperties->find( SYMBOL_LIB_TABLE::PropPowerSymsOnly ) != aProperties->end() );
1601
1602 cacheLib( aLibraryPath, aProperties );
1603
1604 const LIB_SYMBOL_MAP& symbols = m_cache->m_symbols;
1605
1606 for( LIB_SYMBOL_MAP::const_iterator it = symbols.begin(); it != symbols.end(); ++it )
1607 {
1608 if( !powerSymbolsOnly || it->second->IsPower() )
1609 aSymbolList.push_back( it->second );
1610 }
1611}
1612
1613
1614LIB_SYMBOL* SCH_IO_KICAD_SEXPR::LoadSymbol( const wxString& aLibraryPath,
1615 const wxString& aSymbolName,
1616 const std::map<std::string, UTF8>* aProperties )
1617{
1618 cacheLib( aLibraryPath, aProperties );
1619
1620 LIB_SYMBOL_MAP::const_iterator it = m_cache->m_symbols.find( aSymbolName );
1621
1622 // We no longer escape '/' in symbol names, but we used to.
1623 if( it == m_cache->m_symbols.end() && aSymbolName.Contains( '/' ) )
1624 it = m_cache->m_symbols.find( EscapeString( aSymbolName, CTX_LEGACY_LIBID ) );
1625
1626 if( it == m_cache->m_symbols.end() && aSymbolName.Contains( wxT( "{slash}" ) ) )
1627 {
1628 wxString unescaped = aSymbolName;
1629 unescaped.Replace( wxT( "{slash}" ), wxT( "/" ) );
1630 it = m_cache->m_symbols.find( unescaped );
1631 }
1632
1633 if( it == m_cache->m_symbols.end() )
1634 return nullptr;
1635
1636 return it->second;
1637}
1638
1639
1640void SCH_IO_KICAD_SEXPR::SaveSymbol( const wxString& aLibraryPath, const LIB_SYMBOL* aSymbol,
1641 const std::map<std::string, UTF8>* aProperties )
1642{
1643 cacheLib( aLibraryPath, aProperties );
1644
1645 m_cache->AddSymbol( aSymbol );
1646
1647 if( !isBuffering( aProperties ) )
1648 m_cache->Save();
1649}
1650
1651
1652void SCH_IO_KICAD_SEXPR::DeleteSymbol( const wxString& aLibraryPath, const wxString& aSymbolName,
1653 const std::map<std::string, UTF8>* aProperties )
1654{
1655 cacheLib( aLibraryPath, aProperties );
1656
1657 m_cache->DeleteSymbol( aSymbolName );
1658
1659 if( !isBuffering( aProperties ) )
1660 m_cache->Save();
1661}
1662
1663
1664void SCH_IO_KICAD_SEXPR::CreateLibrary( const wxString& aLibraryPath,
1665 const std::map<std::string, UTF8>* aProperties )
1666{
1667 if( wxFileExists( aLibraryPath ) )
1668 {
1669 THROW_IO_ERROR( wxString::Format( _( "Symbol library '%s' already exists." ),
1670 aLibraryPath.GetData() ) );
1671 }
1672
1673 delete m_cache;
1674 m_cache = new SCH_IO_KICAD_SEXPR_LIB_CACHE( aLibraryPath );
1676 m_cache->Save();
1677 m_cache->Load(); // update m_writable and m_timestamp
1678}
1679
1680
1681bool SCH_IO_KICAD_SEXPR::DeleteLibrary( const wxString& aLibraryPath,
1682 const std::map<std::string, UTF8>* aProperties )
1683{
1684 wxFileName fn = aLibraryPath;
1685
1686 if( !fn.FileExists() )
1687 return false;
1688
1689 // Some of the more elaborate wxRemoveFile() crap puts up its own wxLog dialog
1690 // we don't want that. we want bare metal portability with no UI here.
1691 if( wxRemove( aLibraryPath ) )
1692 {
1693 THROW_IO_ERROR( wxString::Format( _( "Symbol library '%s' cannot be deleted." ),
1694 aLibraryPath.GetData() ) );
1695 }
1696
1697 if( m_cache && m_cache->IsFile( aLibraryPath ) )
1698 {
1699 delete m_cache;
1700 m_cache = nullptr;
1701 }
1702
1703 return true;
1704}
1705
1706
1707void SCH_IO_KICAD_SEXPR::SaveLibrary( const wxString& aLibraryPath,
1708 const std::map<std::string, UTF8>* aProperties )
1709{
1710 if( !m_cache )
1711 m_cache = new SCH_IO_KICAD_SEXPR_LIB_CACHE( aLibraryPath );
1712
1713 wxString oldFileName = m_cache->GetFileName();
1714
1715 if( !m_cache->IsFile( aLibraryPath ) )
1716 m_cache->SetFileName( aLibraryPath );
1717
1718 // This is a forced save.
1720 m_cache->Save();
1721 m_cache->SetFileName( oldFileName );
1722}
1723
1724
1725bool SCH_IO_KICAD_SEXPR::CanReadLibrary( const wxString& aLibraryPath ) const
1726{
1727 if( !SCH_IO::CanReadLibrary( aLibraryPath ) )
1728 return false;
1729
1730 // Above just checks for proper extension; now check that it actually exists
1731
1732 wxFileName fn( aLibraryPath );
1733 return fn.IsOk() && fn.FileExists();
1734}
1735
1736
1737bool SCH_IO_KICAD_SEXPR::IsLibraryWritable( const wxString& aLibraryPath )
1738{
1739 wxFileName fn( aLibraryPath );
1740
1741 if( fn.FileExists() )
1742 return fn.IsFileWritable();
1743
1744 return fn.IsDirWritable();
1745}
1746
1747
1748void SCH_IO_KICAD_SEXPR::GetAvailableSymbolFields( std::vector<wxString>& aNames )
1749{
1750 if( !m_cache )
1751 return;
1752
1753 const LIB_SYMBOL_MAP& symbols = m_cache->m_symbols;
1754
1755 std::set<wxString> fieldNames;
1756
1757 for( LIB_SYMBOL_MAP::const_iterator it = symbols.begin(); it != symbols.end(); ++it )
1758 {
1759 std::vector<SCH_FIELD*> fields;
1760 it->second->GetFields( fields );
1761
1762 for( SCH_FIELD* field : fields )
1763 {
1764 if( field->IsMandatory() )
1765 continue;
1766
1767 // TODO(JE): enable configurability of this outside database libraries?
1768 // if( field->ShowInChooser() )
1769 fieldNames.insert( field->GetName() );
1770 }
1771 }
1772
1773 std::copy( fieldNames.begin(), fieldNames.end(), std::back_inserter( aNames ) );
1774}
1775
1776
1777void SCH_IO_KICAD_SEXPR::GetDefaultSymbolFields( std::vector<wxString>& aNames )
1778{
1779 GetAvailableSymbolFields( aNames );
1780}
1781
1782
1783std::vector<LIB_SYMBOL*> SCH_IO_KICAD_SEXPR::ParseLibSymbols( std::string& aSymbolText,
1784 std::string aSource,
1785 int aFileVersion )
1786{
1787 LIB_SYMBOL* newSymbol = nullptr;
1788 LIB_SYMBOL_MAP map;
1789
1790 std::vector<LIB_SYMBOL*> newSymbols;
1791 std::unique_ptr<STRING_LINE_READER> reader = std::make_unique<STRING_LINE_READER>( aSymbolText,
1792 aSource );
1793
1794 do
1795 {
1796 SCH_IO_KICAD_SEXPR_PARSER parser( reader.get() );
1797
1798 newSymbol = parser.ParseSymbol( map, aFileVersion );
1799
1800 if( newSymbol )
1801 newSymbols.emplace_back( newSymbol );
1802
1803 reader.reset( new STRING_LINE_READER( *reader ) );
1804 }
1805 while( newSymbol );
1806
1807 return newSymbols;
1808}
1809
1810
1812{
1813 SCH_IO_KICAD_SEXPR_LIB_CACHE::SaveSymbol( symbol, formatter );
1814}
1815
1816
1817const char* SCH_IO_KICAD_SEXPR::PropBuffering = "buffering";
const char * name
Definition: DXF_plotter.cpp:62
constexpr EDA_IU_SCALE schIUScale
Definition: base_units.h:114
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition: box2.h:990
wxString GetMajorMinorVersion()
Get only the major and minor version in a string major.minor.
This class handle bitmap images in KiCad.
Definition: bitmap_base.h:49
bool SaveImageData(wxOutputStream &aOutStream) const
Write the bitmap data to aOutStream.
int GetPPI() const
Definition: bitmap_base.h:118
wxImage * GetImageData()
Definition: bitmap_base.h:68
const LIB_ID & GetDesignBlockLibId() const
Definition: eda_group.h:73
std::unordered_set< EDA_ITEM * > & GetItems()
Definition: eda_group.h:54
wxString GetName() const
Definition: eda_group.h:51
bool HasDesignBlockLink() const
Definition: eda_group.h:70
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:98
void SetFlags(EDA_ITEM_FLAGS aMask)
Definition: eda_item.h:142
const KIID m_Uuid
Definition: eda_item.h:516
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:110
void ClearFlags(EDA_ITEM_FLAGS aMask=EDA_ITEM_ALL_FLAGS)
Definition: eda_item.h:144
bool IsSelected() const
Definition: eda_item.h:127
virtual void SetParent(EDA_ITEM *aParent)
Definition: eda_item.h:113
EDA_ITEM * GetParent() const
Definition: eda_item.h:112
virtual bool IsLocked() const
Definition: eda_item.h:120
EDA_ITEM_FLAGS GetFlags() const
Definition: eda_item.h:145
FILL_T GetFillMode() const
Definition: eda_shape.h:142
SHAPE_T GetShape() const
Definition: eda_shape.h:168
const VECTOR2I & GetEnd() const
Return the ending point of the graphic.
Definition: eda_shape.h:215
const VECTOR2I & GetStart() const
Return the starting point of the graphic.
Definition: eda_shape.h:173
COLOR4D GetFillColor() const
Definition: eda_shape.h:152
wxString SHAPE_T_asString() const
Definition: eda_shape.cpp:343
int GetTextHeight() const
Definition: eda_text.h:264
bool IsDefaultFormatting() const
Definition: eda_text.cpp:1027
const EDA_ANGLE & GetTextAngle() const
Definition: eda_text.h:144
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition: eda_text.h:97
virtual bool IsVisible() const
Definition: eda_text.h:184
virtual void Format(OUTPUTFORMATTER *aFormatter, int aControlBits) const
Output the object to aFormatter in s-expression form.
Definition: eda_text.cpp:1041
EE_TYPE OfType(KICAD_T aType) const
Definition: sch_rtree.h:241
bool IsEmpty() const
void WriteEmbeddedFiles(OUTPUTFORMATTER &aOut, bool aWriteData) const
Output formatter for the embedded files.
void ClearEmbeddedFonts()
Remove all embedded fonts from the collection.
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:249
PROGRESS_REPORTER * m_progressReporter
Progress reporter to track the progress of the operation, may be nullptr.
Definition: io_base.h:223
virtual bool CanReadLibrary(const wxString &aFileName) const
Checks if this IO object can read the specified library file/directory.
Definition: io_base.cpp:71
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
double r
Red component.
Definition: color4d.h:392
double g
Green component.
Definition: color4d.h:393
double a
Alpha component.
Definition: color4d.h:395
double b
Blue component.
Definition: color4d.h:394
bool MakeRelativeTo(const KIID_PATH &aPath)
Definition: kiid.cpp:311
wxString AsString() const
Definition: kiid.cpp:356
Definition: kiid.h:49
UTF8 Format() const
Definition: lib_id.cpp:119
Define a library symbol object.
Definition: lib_symbol.h:85
An abstract class from which implementation specific LINE_READERs may be derived to read single lines...
Definition: richio.h:93
An interface used to output 8 bit text in a convenient way.
Definition: richio.h:322
std::string Quotew(const wxString &aWrapee) const
Definition: richio.cpp:548
int PRINTF_FUNC_N Print(int nestLevel, const char *fmt,...)
Format and write text to the output stream.
Definition: richio.cpp:463
void Format(OUTPUTFORMATTER *aFormatter) const
Output the page class to aFormatter in s-expression form.
Definition: page_info.cpp:276
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.
virtual const wxString GetProjectPath() const
Return the full path of the project.
Definition: project.cpp:149
virtual const wxString GetProjectName() const
Return the short name of the project.
Definition: project.cpp:161
A REFERENCE_IMAGE is a wrapper around a BITMAP_IMAGE that is displayed in an editor as a reference fo...
VECTOR2I GetPosition() const
const BITMAP_BASE & GetImage() const
Get the underlying image.
double GetImageScale() const
Holds all the data relating to one schematic.
Definition: schematic.h:88
void EmbedFonts() override
Embed fonts in the schematic.
Definition: schematic.cpp:984
SCH_SHEET_LIST Hierarchy() const
Return the full schematic flattened hierarchical sheet list.
Definition: schematic.cpp:258
PROJECT & Project() const
Return a reference to the project this schematic is part of.
Definition: schematic.h:103
EMBEDDED_FILES * GetEmbeddedFiles() override
Definition: schematic.cpp:927
bool IsValid() const
A simple test if the schematic is loaded, not a complete one.
Definition: schematic.h:156
SCH_SHEET & Root() const
Definition: schematic.h:140
Object to handle a bitmap image that can be inserted in a schematic.
Definition: sch_bitmap.h:40
REFERENCE_IMAGE & GetReferenceImage()
Definition: sch_bitmap.h:51
Base class for a bus or wire entry.
Definition: sch_bus_entry.h:38
VECTOR2I GetSize() const
Definition: sch_bus_entry.h:73
VECTOR2I GetPosition() const override
virtual STROKE_PARAMS GetStroke() const override
Definition: sch_bus_entry.h:81
VECTOR2I GetEnd() const
bool IsMandatory() const
Definition: sch_field.cpp:1359
VECTOR2I GetPosition() const override
Definition: sch_field.cpp:1337
bool IsNameShown() const
Definition: sch_field.h:202
wxString GetCanonicalName() const
Get a non-language-specific name for a field which can be used for storage, variable look-up,...
Definition: sch_field.cpp:1117
wxString GetName(bool aUseDefaultName=true) const
Return the field name (not translated).
Definition: sch_field.cpp:1103
bool CanAutoplace() const
Definition: sch_field.h:213
A set of SCH_ITEMs (i.e., without duplicates).
Definition: sch_group.h:52
A cache assistant for the KiCad s-expression symbol libraries.
static void SaveSymbol(LIB_SYMBOL *aSymbol, OUTPUTFORMATTER &aFormatter, const wxString &aLibName=wxEmptyString, bool aIncludeData=true)
void DeleteSymbol(const wxString &aName) override
void Save(const std::optional< bool > &aOpt=std::nullopt) override
Save the entire library to file m_libFileName;.
Object to parser s-expression symbol library and schematic file formats.
void ParseSchematic(SCH_SHEET *aSheet, bool aIsCopyablyOnly=false, int aFileVersion=SEXPR_SCHEMATIC_FILE_VERSION)
Parse the internal LINE_READER object into aSheet.
LIB_SYMBOL * ParseSymbol(LIB_SYMBOL_MAP &aSymbolLibMap, int aFileVersion=SEXPR_SYMBOL_LIB_FILE_VERSION)
Parse internal LINE_READER object into symbols and return all found.
wxString m_path
Root project path for loading child sheets.
void GetDefaultSymbolFields(std::vector< wxString > &aNames) override
Retrieves a list of (custom) field names that should be shown by default for this library in the symb...
void saveShape(SCH_SHAPE *aShape)
void SaveSchematicFile(const wxString &aFileName, SCH_SHEET *aSheet, SCHEMATIC *aSchematic, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Write aSchematic to a storage file in a format that this SCH_IO implementation knows about,...
void SaveLibrary(const wxString &aLibraryPath, const std::map< std::string, UTF8 > *aProperties=nullptr) override
SCH_SHEET_PATH m_currentSheetPath
void saveGroup(SCH_GROUP *aGroup)
void LoadContent(LINE_READER &aReader, SCH_SHEET *aSheet, int aVersion=SEXPR_SCHEMATIC_FILE_VERSION)
void EnumerateSymbolLib(wxArrayString &aSymbolNameList, const wxString &aLibraryPath, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Populate a list of LIB_SYMBOL alias names contained within the library aLibraryPath.
bool m_appending
Schematic load append status.
int m_version
Version of file being loaded.
void loadFile(const wxString &aFileName, SCH_SHEET *aSheet)
static void FormatLibSymbol(LIB_SYMBOL *aPart, OUTPUTFORMATTER &aFormatter)
void DeleteSymbol(const wxString &aLibraryPath, const wxString &aSymbolName, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Delete the entire LIB_SYMBOL associated with aAliasName from the library aLibraryPath.
void loadHierarchy(const SCH_SHEET_PATH &aParentSheetPath, SCH_SHEET *aSheet)
void SaveSymbol(const wxString &aLibraryPath, const LIB_SYMBOL *aSymbol, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Write aSymbol to an existing library located at aLibraryPath.
static std::vector< LIB_SYMBOL * > ParseLibSymbols(std::string &aSymbolText, std::string aSource, int aFileVersion=SEXPR_SCHEMATIC_FILE_VERSION)
void saveBusAlias(std::shared_ptr< BUS_ALIAS > aAlias)
OUTPUTFORMATTER * m_out
The formatter for saving SCH_SCREEN objects.
SCH_SHEET * LoadSchematicFile(const wxString &aFileName, SCHEMATIC *aSchematic, SCH_SHEET *aAppendToMe=nullptr, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Load information from some input file format that this SCH_IO implementation knows about,...
LIB_SYMBOL * LoadSymbol(const wxString &aLibraryPath, const wxString &aAliasName, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Load a LIB_SYMBOL object having aPartName from the aLibraryPath containing a library format that this...
wxString m_error
For throwing exceptions or errors on partial loads.
void saveInstances(const std::vector< SCH_SHEET_INSTANCE > &aSheets)
bool isBuffering(const std::map< std::string, UTF8 > *aProperties)
static const char * PropBuffering
The property used internally by the plugin to enable cache buffering which prevents the library file ...
SCH_SHEET * m_rootSheet
The root sheet of the schematic being loaded.
void cacheLib(const wxString &aLibraryFileName, const std::map< std::string, UTF8 > *aProperties)
void saveRuleArea(SCH_RULE_AREA *aRuleArea)
void saveField(SCH_FIELD *aField)
SCH_IO_KICAD_SEXPR_LIB_CACHE * m_cache
void Format(SCH_SHEET *aSheet)
bool IsLibraryWritable(const wxString &aLibraryPath) override
Return true if the library at aLibraryPath is writable.
void init(SCHEMATIC *aSchematic, const std::map< std::string, UTF8 > *aProperties=nullptr)
initialize PLUGIN like a constructor would.
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,...
void saveBitmap(const SCH_BITMAP &aBitmap)
void saveText(SCH_TEXT *aText)
void saveSheet(SCH_SHEET *aSheet, const SCH_SHEET_LIST &aSheetList)
int GetModifyHash() const override
Return the modification hash from the library cache.
bool CanReadLibrary(const wxString &aLibraryPath) const override
Checks if this IO object can read the specified library file/directory.
void saveLine(SCH_LINE *aLine)
void saveNoConnect(SCH_NO_CONNECT *aNoConnect)
void saveTable(SCH_TABLE *aTable)
std::stack< wxString > m_currentPath
Stack to maintain nested sheet paths.
void CreateLibrary(const wxString &aLibraryPath, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Create a new empty library at aLibraryPath empty.
void saveJunction(SCH_JUNCTION *aJunction)
std::function< bool(wxString aTitle, int aIcon, wxString aMsg, wxString aAction)> m_queryUserCallback
void saveTextBox(SCH_TEXTBOX *aText)
void saveSymbol(SCH_SYMBOL *aSymbol, const SCHEMATIC &aSchematic, const SCH_SHEET_LIST &aSheetList, bool aForClipboard, const SCH_SHEET_PATH *aRelativePath=nullptr)
void saveBusEntry(SCH_BUS_ENTRY_BASE *aBusEntry)
void GetAvailableSymbolFields(std::vector< wxString > &aNames) override
Retrieves a list of (custom) field names that are present on symbols in this library.
bool IsFile(const wxString &aFullPathAndFileName) const
void SetFileName(const wxString &aFileName)
virtual void AddSymbol(const LIB_SYMBOL *aSymbol)
LIB_SYMBOL_MAP m_symbols
bool IsFileChanged() const
wxString GetFileName() const
void SetModified(bool aModified=true)
Base class that schematic file and library loading and saving plugins should derive from.
Definition: sch_io.h:57
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:168
int GetBodyStyle() const
Definition: sch_item.h:248
bool IsPrivate() const
Definition: sch_item.h:254
SCH_LAYER_ID GetLayer() const
Return the layer this item is on.
Definition: sch_item.h:313
AUTOPLACE_ALGO GetFieldsAutoplaced() const
Return whether the fields have been automatically placed.
Definition: sch_item.h:597
wxString GetClass() const override
Return the class name.
Definition: sch_item.h:178
COLOR4D GetColor() const
Definition: sch_junction.h:119
int GetDiameter() const
Definition: sch_junction.h:114
VECTOR2I GetPosition() const override
Definition: sch_junction.h:107
SPIN_STYLE GetSpinStyle() const
Definition: sch_label.cpp:351
LABEL_FLAG_SHAPE GetShape() const
Definition: sch_label.h:176
std::vector< SCH_FIELD > & GetFields()
Definition: sch_label.h:205
Segment description base class to describe items which have 2 end points (track, wire,...
Definition: sch_line.h:42
virtual STROKE_PARAMS GetStroke() const override
Definition: sch_line.h:193
VECTOR2I GetEndPoint() const
Definition: sch_line.h:144
VECTOR2I GetStartPoint() const
Definition: sch_line.h:139
void SetEndPoint(const VECTOR2I &aPosition)
Definition: sch_line.h:145
VECTOR2I GetPosition() const override
bool GetExcludedFromSim() const override
Definition: sch_rule_area.h:65
bool GetExcludedFromBoard() const override
Definition: sch_rule_area.h:77
bool GetDNP() const override
Set or clear the 'Do Not Populate' flag.
Definition: sch_rule_area.h:82
bool GetExcludedFromBOM() const override
Definition: sch_rule_area.h:71
const PAGE_INFO & GetPageSettings() const
Definition: sch_screen.h:139
auto & GetBusAliases() const
Return a set of bus aliases defined in this screen.
Definition: sch_screen.h:524
const std::map< wxString, LIB_SYMBOL * > & GetLibSymbols() const
Fetch a list of unique LIB_SYMBOL object pointers required to properly render each SCH_SYMBOL in this...
Definition: sch_screen.h:488
EE_RTREE & Items()
Get the full RTree, usually for iterating.
Definition: sch_screen.h:117
const wxString & GetFileName() const
Definition: sch_screen.h:152
void SetFileName(const wxString &aFileName)
Set the file name for this screen to aFileName.
Definition: sch_screen.cpp:123
wxString GroupsSanityCheck(bool repair=false)
Consistency check of internal m_groups structure.
const TITLE_BLOCK & GetTitleBlock() const
Definition: sch_screen.h:163
KIID m_uuid
A unique identifier for each schematic file.
Definition: sch_screen.h:744
void SetFileReadOnly(bool aIsReadOnly)
Definition: sch_screen.h:154
void SetFileExists(bool aFileExists)
Definition: sch_screen.h:157
SCH_SCREEN * GetScreen()
Definition: sch_selection.h:52
STROKE_PARAMS GetStroke() const override
Definition: sch_shape.h:57
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
std::optional< SCH_SHEET_PATH > GetSheetPathByKIIDPath(const KIID_PATH &aPath, bool aIncludeLastSheet=true) const
Finds a SCH_SHEET_PATH that matches the provided KIID_PATH.
std::optional< SCH_SHEET_PATH > GetOrdinalPath(const SCH_SCREEN *aScreen) const
Return the ordinal sheet path of aScreen.
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
bool empty() const
Forwarded method from std::vector.
KIID_PATH Path() const
Get the sheet path as an KIID_PATH.
SCH_SCREEN * LastScreen()
SCH_SHEET * at(size_t aIndex) const
Forwarded method from std::vector.
void push_back(SCH_SHEET *aSheet)
Forwarded method from std::vector.
void pop_back()
Forwarded method from std::vector.
Define a sheet pin (label) used in sheets to create hierarchical schematics.
Definition: sch_sheet_pin.h:66
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition: sch_sheet.h:47
bool GetExcludedFromBoard() const override
Definition: sch_sheet.h:397
wxString GetFileName() const
Return the filename corresponding to this sheet.
Definition: sch_sheet.h:321
bool HasRootInstance() const
Check to see if this sheet has a root sheet instance.
Definition: sch_sheet.cpp:1417
std::vector< SCH_FIELD > & GetFields()
Return a reference to the vector holding the sheet's fields.
Definition: sch_sheet.h:87
bool GetExcludedFromBOM() const override
Definition: sch_sheet.h:391
bool SearchHierarchy(const wxString &aFilename, SCH_SCREEN **aScreen)
Search the existing hierarchy for an instance of screen loaded from aFileName.
Definition: sch_sheet.cpp:751
VECTOR2I GetSize() const
Definition: sch_sheet.h:118
SCH_SCREEN * GetScreen() const
Definition: sch_sheet.h:116
VECTOR2I GetPosition() const override
Definition: sch_sheet.h:415
void SetScreen(SCH_SCREEN *aScreen)
Set the SCH_SCREEN associated with this sheet to aScreen.
Definition: sch_sheet.cpp:137
const SCH_SHEET_INSTANCE & GetRootInstance() const
Return the root sheet instance data.
Definition: sch_sheet.cpp:1429
KIGFX::COLOR4D GetBorderColor() const
Definition: sch_sheet.h:124
bool GetExcludedFromSim() const override
Definition: sch_sheet.h:385
int GetBorderWidth() const
Definition: sch_sheet.h:121
std::vector< SCH_SHEET_PIN * > & GetPins()
Definition: sch_sheet.h:187
bool GetDNP() const override
Set or clear the 'Do Not Populate' flags.
Definition: sch_sheet.h:402
const std::vector< SCH_SHEET_INSTANCE > & GetInstances() const
Definition: sch_sheet.h:429
KIGFX::COLOR4D GetBackgroundColor() const
Definition: sch_sheet.h:127
Schematic symbol object.
Definition: sch_symbol.h:75
std::vector< std::unique_ptr< SCH_PIN > > & GetRawPins()
Definition: sch_symbol.h:633
const std::vector< SCH_SYMBOL_INSTANCE > & GetInstances() const
Definition: sch_symbol.h:134
bool UseLibIdLookup() const
Definition: sch_symbol.h:181
wxString GetSchSymbolLibraryName() const
Definition: sch_symbol.cpp:243
void GetFields(std::vector< SCH_FIELD * > &aVector, bool aVisibleOnly) const override
Populate a std::vector with SCH_FIELDs, sorted in ordinal order.
Definition: sch_symbol.cpp:788
VECTOR2I GetPosition() const override
Definition: sch_symbol.h:767
const LIB_ID & GetLibId() const override
Definition: sch_symbol.h:164
bool GetInstance(SCH_SYMBOL_INSTANCE &aInstance, const KIID_PATH &aSheetPath, bool aTestFromEnd=false) const
Definition: sch_symbol.cpp:468
int GetOrientation() const override
Get the display symbol orientation.
wxString GetPrefix() const
Definition: sch_symbol.h:244
void SetRowHeight(int aRow, int aHeight)
Definition: sch_table.h:137
const STROKE_PARAMS & GetSeparatorsStroke() const
Definition: sch_table.h:77
void SetColCount(int aCount)
Definition: sch_table.h:119
bool StrokeExternal() const
Definition: sch_table.h:53
int GetRowHeight(int aRow) const
Definition: sch_table.h:139
void SetColWidth(int aCol, int aWidth)
Definition: sch_table.h:127
std::vector< SCH_TABLECELL * > GetCells() const
Definition: sch_table.h:157
int GetColWidth(int aCol) const
Definition: sch_table.h:129
const STROKE_PARAMS & GetBorderStroke() const
Definition: sch_table.h:59
int GetColCount() const
Definition: sch_table.h:120
bool StrokeHeaderSeparator() const
Definition: sch_table.h:56
void DeleteMarkedCells()
Definition: sch_table.h:182
SCH_TABLECELL * GetCell(int aRow, int aCol) const
Definition: sch_table.h:147
bool StrokeColumns() const
Definition: sch_table.h:99
EDA_ITEM * Clone() const override
Create a duplicate of this item with linked list members set to NULL.
Definition: sch_table.h:226
bool StrokeRows() const
Definition: sch_table.h:102
int GetRowCount() const
Definition: sch_table.h:122
int GetMarginBottom() const
Definition: sch_textbox.h:66
int GetMarginLeft() const
Definition: sch_textbox.h:63
bool GetExcludedFromSim() const override
Definition: sch_textbox.h:92
int GetMarginRight() const
Definition: sch_textbox.h:65
int GetMarginTop() const
Definition: sch_textbox.h:64
VECTOR2I GetPosition() const override
Definition: sch_text.h:141
bool GetExcludedFromSim() const override
Definition: sch_text.h:88
Is a LINE_READER that reads from a multiline 8 bit wide std::string.
Definition: richio.h:253
Simple container to manage line stroke parameters.
Definition: stroke_params.h:94
void SetWidth(int aWidth)
void Format(OUTPUTFORMATTER *out, const EDA_IU_SCALE &aIuScale) const
static const char * PropPowerSymsOnly
bool GetExcludedFromBoard() const override
Definition: symbol.h:187
bool GetDNP() const override
Set or clear the 'Do Not Populate' flag.
Definition: symbol.h:192
bool GetExcludedFromBOM() const override
Definition: symbol.h:181
bool GetExcludedFromSim() const override
Definition: symbol.h:175
virtual void Format(OUTPUTFORMATTER *aFormatter) const
Output the object to aFormatter in s-expression form.
Definition: title_block.cpp:31
const char * c_str() const
Definition: utf8.h:109
wxString wx_str() const
Definition: utf8.cpp:45
static REPORTER & GetInstance()
Definition: reporter.cpp:202
static void SetReporter(REPORTER *aReporter)
Set the reporter to use for reporting font substitution warnings.
Definition: fontconfig.cpp:64
#define _(s)
static constexpr EDA_ANGLE ANGLE_0
Definition: eda_angle.h:411
static constexpr EDA_ANGLE ANGLE_90
Definition: eda_angle.h:413
static constexpr EDA_ANGLE ANGLE_270
Definition: eda_angle.h:416
static constexpr EDA_ANGLE ANGLE_180
Definition: eda_angle.h:415
#define STRUCT_DELETED
flag indication structures to be erased
#define SKIP_STRUCT
flag indicating that the structure should be ignored
#define DEFAULT_SIZE_TEXT
This is the "default-of-the-default" hardcoded text size; individual application define their own def...
Definition: eda_text.h:69
const wxChar *const traceSchPlugin
Flag to enable legacy schematic plugin debug output.
#define THROW_IO_ERROR(msg)
Definition: ki_exception.h:39
wxString LayerName(int aLayer)
Returns the default display name for a given layer.
Definition: layer_id.cpp:31
@ LAYER_WIRE
Definition: layer_ids.h:442
@ LAYER_NOTES
Definition: layer_ids.h:457
@ LAYER_BUS
Definition: layer_ids.h:443
#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:194
KICOMMON_API std::string FormatAngle(const EDA_ANGLE &aAngle)
Convert aAngle from board units to a string appropriate for writing to file.
Definition: eda_units.cpp:186
void FormatUuid(OUTPUTFORMATTER *aOut, const KIID &aUuid)
void FormatStreamData(OUTPUTFORMATTER &aOut, const wxStreamBuffer &aStream)
Write binary data to the formatter as base 64 encoded string.
void FormatBool(OUTPUTFORMATTER *aOut, const wxString &aKey, bool aValue)
Writes a boolean to the formatter, in the style (aKey [yes|no])
#define SEXPR_SCHEMATIC_FILE_VERSION
Schematic file version.
Class to handle a set of SCH_ITEMs.
void formatArc(OUTPUTFORMATTER *aFormatter, EDA_SHAPE *aArc, bool aIsPrivate, const STROKE_PARAMS &aStroke, FILL_T aFillMode, const COLOR4D &aFillColor, bool aInvertY, const KIID &aUuid)
const char * getSheetPinShapeToken(LABEL_FLAG_SHAPE aShape)
void formatCircle(OUTPUTFORMATTER *aFormatter, EDA_SHAPE *aCircle, bool aIsPrivate, const STROKE_PARAMS &aStroke, FILL_T aFillMode, const COLOR4D &aFillColor, bool aInvertY, const KIID &aUuid)
const char * getTextTypeToken(KICAD_T aType)
void formatBezier(OUTPUTFORMATTER *aFormatter, EDA_SHAPE *aBezier, bool aIsPrivate, const STROKE_PARAMS &aStroke, FILL_T aFillMode, const COLOR4D &aFillColor, bool aInvertY, const KIID &aUuid)
void formatRect(OUTPUTFORMATTER *aFormatter, EDA_SHAPE *aRect, bool aIsPrivate, const STROKE_PARAMS &aStroke, FILL_T aFillMode, const COLOR4D &aFillColor, bool aInvertY, const KIID &aUuid)
void formatPoly(OUTPUTFORMATTER *aFormatter, EDA_SHAPE *aPolyLine, bool aIsPrivate, const STROKE_PARAMS &aStroke, FILL_T aFillMode, const COLOR4D &aFillColor, bool aInvertY, const KIID &aUuid)
EDA_ANGLE getSheetPinAngle(SHEET_SIDE aSide)
void formatFill(OUTPUTFORMATTER *aFormatter, FILL_T aFillMode, const COLOR4D &aFillColor)
Fill token formatting helper.
AUTOPLACE_ALGO
Definition: sch_item.h:69
@ AUTOPLACE_MANUAL
Definition: sch_item.h:72
@ AUTOPLACE_AUTO
Definition: sch_item.h:71
std::string toUTFTildaText(const wxString &txt)
Convert a wxString to UTF8 and replace any control characters with a ~, where a control character is ...
Definition: sch_symbol.cpp:56
const int scale
std::string FormatDouble2Str(double aValue)
Print a float number without using scientific notation and no trailing 0 This function is intended in...
std::string EscapedUTF8(const wxString &aString)
Return an 8 bit UTF8 string given aString in Unicode form.
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:429
@ CTX_LEGACY_LIBID
Definition: string_utils.h:55
constexpr int MilsToIU(int mils) const
Definition: base_units.h:97
A simple container for sheet instance information.
A simple container for schematic symbol instance information.
@ SYM_ORIENT_270
Definition: symbol.h:42
@ SYM_MIRROR_Y
Definition: symbol.h:44
@ SYM_ORIENT_180
Definition: symbol.h:41
@ SYM_MIRROR_X
Definition: symbol.h:43
@ SYM_ORIENT_90
Definition: symbol.h:40
std::map< wxString, LIB_SYMBOL *, LibSymbolMapSort > LIB_SYMBOL_MAP
FIELD_T
The set of all field indices assuming an array like sequence that a SCH_COMPONENT or LIB_PART can hol...
wxLogTrace helper definitions.
@ SCH_GROUP_T
Definition: typeinfo.h:174
@ SCH_TABLE_T
Definition: typeinfo.h:166
@ SCH_LINE_T
Definition: typeinfo.h:164
@ SCH_NO_CONNECT_T
Definition: typeinfo.h:161
@ SCH_SYMBOL_T
Definition: typeinfo.h:173
@ SCH_TABLECELL_T
Definition: typeinfo.h:167
@ SCH_DIRECTIVE_LABEL_T
Definition: typeinfo.h:172
@ SCH_LABEL_T
Definition: typeinfo.h:168
@ SCH_SHEET_T
Definition: typeinfo.h:176
@ SCH_MARKER_T
Definition: typeinfo.h:159
@ SCH_SHAPE_T
Definition: typeinfo.h:150
@ SCH_RULE_AREA_T
Definition: typeinfo.h:171
@ SCH_HIER_LABEL_T
Definition: typeinfo.h:170
@ SCH_BUS_BUS_ENTRY_T
Definition: typeinfo.h:163
@ SCH_TEXT_T
Definition: typeinfo.h:152
@ SCH_BUS_WIRE_ENTRY_T
Definition: typeinfo.h:162
@ SCH_BITMAP_T
Definition: typeinfo.h:165
@ SCH_TEXTBOX_T
Definition: typeinfo.h:153
@ SCH_GLOBAL_LABEL_T
Definition: typeinfo.h:169
@ SCH_JUNCTION_T
Definition: typeinfo.h:160