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