KiCad PCB EDA Suite
Loading...
Searching...
No Matches
sch_io_kicad_legacy.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) 2016 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#include <boost/algorithm/string/join.hpp>
25#include <cctype>
26#include <mutex>
27#include <set>
28
29#include <wx/mstream.h>
30#include <wx/filename.h>
31#include <wx/log.h>
32#include <wx/textfile.h>
33#include <wx/tokenzr.h>
34#include <wx_filename.h> // For ::ResolvePossibleSymlinks()
35
36#include <bitmap_base.h>
37#include <fmt/format.h>
38#include <kiway.h>
39#include <string_utils.h>
40#include <richio.h>
41#include <trace_helpers.h>
42#include <trigo.h>
43#include <progress_reporter.h>
44#include <general.h>
45#include <gr_text.h>
46#include <sch_bitmap.h>
47#include <sch_bus_entry.h>
48#include <sch_symbol.h>
49#include <sch_junction.h>
50#include <sch_line.h>
51#include <sch_marker.h>
52#include <sch_no_connect.h>
53#include <sch_text.h>
54#include <sch_sheet.h>
55#include <sch_sheet_pin.h>
56#include <bus_alias.h>
57#include <io/io_utils.h>
61#include <sch_screen.h>
62#include <schematic.h>
63#include <symbol_library.h>
64#include <symbol_lib_table.h>
65#include <eeschema_id.h> // for MAX_UNIT_COUNT_PER_PACKAGE definition
66#include <tool/selection.h>
68
69
70// Tokens to read/save graphic lines style
71#define T_STYLE "style"
72#define T_COLOR "rgb" // cannot be modified (used by wxWidgets)
73#define T_COLORA "rgba" // cannot be modified (used by wxWidgets)
74#define T_WIDTH "width"
75
76
77SCH_IO_KICAD_LEGACY::SCH_IO_KICAD_LEGACY() : SCH_IO( wxS( "Eeschema legacy" ) ),
78 m_appending( false ),
79 m_lineReader( nullptr ),
80 m_lastProgressLine( 0 ),
81 m_lineCount( 0 )
82{
83 init( nullptr );
84}
85
86
88{
89 delete m_cache;
90}
91
92
93void SCH_IO_KICAD_LEGACY::init( SCHEMATIC* aSchematic, const std::map<std::string, UTF8>* aProperties )
94{
95 m_version = 0;
96 m_rootSheet = nullptr;
97 m_currentSheet = nullptr;
98 m_schematic = aSchematic;
99 m_cache = nullptr;
100 m_out = nullptr;
101}
102
103
105{
106 const unsigned PROGRESS_DELTA = 250;
107
109 {
110 unsigned curLine = m_lineReader->LineNumber();
111
112 if( curLine > m_lastProgressLine + PROGRESS_DELTA )
113 {
114 m_progressReporter->SetCurrentProgress( ( (double) curLine )
115 / std::max( 1U, m_lineCount ) );
116
118 THROW_IO_ERROR( _( "Open canceled by user." ) );
119
120 m_lastProgressLine = curLine;
121 }
122 }
123}
124
125
126SCH_SHEET* SCH_IO_KICAD_LEGACY::LoadSchematicFile( const wxString& aFileName, SCHEMATIC* aSchematic,
127 SCH_SHEET* aAppendToMe,
128 const std::map<std::string, UTF8>* aProperties )
129{
130 wxASSERT( !aFileName || aSchematic != nullptr );
131
132 SCH_SHEET* sheet;
133 wxFileName fn = aFileName;
134
135 // Unfortunately child sheet file names the legacy schematic file format are not fully
136 // qualified and are always appended to the project path. The aFileName attribute must
137 // always be an absolute path so the project path can be used for load child sheet files.
138 wxASSERT( fn.IsAbsolute() );
139
140 if( aAppendToMe )
141 {
142 wxLogTrace( traceSchLegacyPlugin, "Append \"%s\" to sheet \"%s\".",
143 aFileName, aAppendToMe->GetFileName() );
144
145 wxFileName normedFn = aAppendToMe->GetFileName();
146
147 if( !normedFn.IsAbsolute() )
148 {
149 if( aFileName.Right( normedFn.GetFullPath().Length() ) == normedFn.GetFullPath() )
150 m_path = aFileName.Left( aFileName.Length() - normedFn.GetFullPath().Length() );
151 }
152
153 if( m_path.IsEmpty() )
154 m_path = aSchematic->Project().GetProjectPath();
155
156 wxLogTrace( traceSchLegacyPlugin, "Normalized append path \"%s\".", m_path );
157 }
158 else
159 {
160 m_path = aSchematic->Project().GetProjectPath();
161 }
162
163 m_currentPath.push( m_path );
164 init( aSchematic, aProperties );
165
166 if( aAppendToMe == nullptr )
167 {
168 // Clean up any allocated memory if an exception occurs loading the schematic.
169 std::unique_ptr<SCH_SHEET> newSheet = std::make_unique<SCH_SHEET>( aSchematic );
170 newSheet->SetFileName( aFileName );
171 m_rootSheet = newSheet.get();
172 loadHierarchy( newSheet.get() );
173
174 // If we got here, the schematic loaded successfully.
175 sheet = newSheet.release();
176 m_rootSheet = nullptr; // Quiet Coverity warning.
177 }
178 else
179 {
180 m_appending = true;
181 wxCHECK_MSG( aSchematic->IsValid(), nullptr, "Can't append to a schematic with no root!" );
182 m_rootSheet = &aSchematic->Root();
183 sheet = aAppendToMe;
184 loadHierarchy( sheet );
185 }
186
187 wxASSERT( m_currentPath.size() == 1 ); // only the project path should remain
188
189 return sheet;
190}
191
192
193// Everything below this comment is recursive. Modify with care.
194
196{
197 SCH_SCREEN* screen = nullptr;
198
199 m_currentSheet = aSheet;
200
201 if( !aSheet->GetScreen() )
202 {
203 // SCH_SCREEN objects store the full path and file name where the SCH_SHEET object only
204 // stores the file name and extension. Add the project path to the file name and
205 // extension to compare when calling SCH_SHEET::SearchHierarchy().
206 wxFileName fileName = aSheet->GetFileName();
207 fileName.SetExt( "sch" );
208
209 if( !fileName.IsAbsolute() )
210 fileName.MakeAbsolute( m_currentPath.top() );
211
212 // Save the current path so that it gets restored when descending and ascending the
213 // sheet hierarchy which allows for sheet schematic files to be nested in folders
214 // relative to the last path a schematic was loaded from.
215 wxLogTrace( traceSchLegacyPlugin, "Saving path '%s'", m_currentPath.top() );
216 m_currentPath.push( fileName.GetPath() );
217 wxLogTrace( traceSchLegacyPlugin, "Current path '%s'", m_currentPath.top() );
218 wxLogTrace( traceSchLegacyPlugin, "Loading '%s'", fileName.GetFullPath() );
219
220 m_rootSheet->SearchHierarchy( fileName.GetFullPath(), &screen );
221
222 if( screen )
223 {
224 aSheet->SetScreen( screen );
225 screen->SetParent( m_schematic );
226 // Do not need to load the sub-sheets - this has already been done.
227 }
228 else
229 {
230 aSheet->SetScreen( new SCH_SCREEN( m_schematic ) );
231 aSheet->GetScreen()->SetFileName( fileName.GetFullPath() );
232
233 if( aSheet == m_rootSheet )
234 const_cast<KIID&>( aSheet->m_Uuid ) = aSheet->GetScreen()->GetUuid();
235
236 try
237 {
238 loadFile( fileName.GetFullPath(), aSheet->GetScreen() );
239 }
240 catch( const IO_ERROR& ioe )
241 {
242 // If there is a problem loading the root sheet, there is no recovery.
243 if( aSheet == m_rootSheet )
244 throw( ioe );
245
246 // For all subsheets, queue up the error message for the caller.
247 if( !m_error.IsEmpty() )
248 m_error += "\n";
249
250 m_error += ioe.What();
251 }
252
253 aSheet->GetScreen()->SetFileReadOnly( !fileName.IsFileWritable() );
254 aSheet->GetScreen()->SetFileExists( true );
255
256 for( SCH_ITEM* aItem : aSheet->GetScreen()->Items().OfType( SCH_SHEET_T ) )
257 {
258 wxCHECK2( aItem->Type() == SCH_SHEET_T, continue );
259 auto sheet = static_cast<SCH_SHEET*>( aItem );
260
261 // Set the parent to aSheet. This effectively creates a method to find
262 // the root sheet from any sheet so a pointer to the root sheet does not
263 // need to be stored globally. Note: this is not the same as a hierarchy.
264 // Complex hierarchies can have multiple copies of a sheet. This only
265 // provides a simple tree to find the root sheet.
266 sheet->SetParent( aSheet );
267
268 // Recursion starts here.
269 loadHierarchy( sheet );
270 }
271 }
272
273 m_currentPath.pop();
274 wxLogTrace( traceSchLegacyPlugin, "Restoring path \"%s\"", m_currentPath.top() );
275 }
276}
277
278
279void SCH_IO_KICAD_LEGACY::loadFile( const wxString& aFileName, SCH_SCREEN* aScreen )
280{
281 FILE_LINE_READER reader( aFileName );
282
284 {
285 m_progressReporter->Report( wxString::Format( _( "Loading %s..." ), aFileName ) );
286
288 THROW_IO_ERROR( _( "Open canceled by user." ) );
289
290 m_lineReader = &reader;
291 m_lineCount = 0;
292
293 while( reader.ReadLine() )
294 m_lineCount++;
295
296 reader.Rewind();
297 }
298
299 loadHeader( reader, aScreen );
300
301 LoadContent( reader, aScreen, m_version );
302
303 // Unfortunately schematic files prior to version 2 are not terminated with $EndSCHEMATC
304 // so checking for its existance will fail so just exit here and take our chances. :(
305 if( m_version > 1 )
306 {
307 char* line = reader.Line();
308
309 while( *line == ' ' )
310 line++;
311
312 if( !strCompare( "$EndSCHEMATC", line ) )
313 THROW_IO_ERROR( "'$EndSCHEMATC' not found" );
314 }
315}
316
317
318void SCH_IO_KICAD_LEGACY::LoadContent( LINE_READER& aReader, SCH_SCREEN* aScreen, int version )
319{
320 m_version = version;
321
322 // We cannot safely load content without a set root level.
323 wxCHECK_RET( m_rootSheet,
324 "Cannot call SCH_IO_KICAD_LEGACY::LoadContent() without setting root sheet." );
325
326 while( aReader.ReadLine() )
327 {
328 checkpoint();
329
330 char* line = aReader.Line();
331
332 while( *line == ' ' )
333 line++;
334
335 // Either an object will be loaded properly or the file load will fail and raise
336 // an exception.
337 if( strCompare( "$Descr", line ) )
338 loadPageSettings( aReader, aScreen );
339 else if( strCompare( "$Comp", line ) )
340 aScreen->Append( loadSymbol( aReader ) );
341 else if( strCompare( "$Sheet", line ) )
342 aScreen->Append( loadSheet( aReader ) );
343 else if( strCompare( "$Bitmap", line ) )
344 aScreen->Append( loadBitmap( aReader ) );
345 else if( strCompare( "Connection", line ) )
346 aScreen->Append( loadJunction( aReader ) );
347 else if( strCompare( "NoConn", line ) )
348 aScreen->Append( loadNoConnect( aReader ) );
349 else if( strCompare( "Wire", line ) )
350 aScreen->Append( loadWire( aReader ) );
351 else if( strCompare( "Entry", line ) )
352 aScreen->Append( loadBusEntry( aReader ) );
353 else if( strCompare( "Text", line ) )
354 aScreen->Append( loadText( aReader ) );
355 else if( strCompare( "BusAlias", line ) )
356 aScreen->AddBusAlias( loadBusAlias( aReader, aScreen ) );
357 else if( strCompare( "Kmarq", line ) )
358 continue; // Ignore legacy (until 2009) ERC marker entry
359 else if( strCompare( "$EndSCHEMATC", line ) )
360 return;
361 else
362 SCH_PARSE_ERROR( "unrecognized token", aReader, line );
363 }
364}
365
366
368{
369 const char* line = aReader.ReadLine();
370
371 if( !line || !strCompare( "Eeschema Schematic File Version", line, &line ) )
372 {
373 m_error.Printf( _( "'%s' does not appear to be an Eeschema file." ),
374 aScreen->GetFileName() );
376 }
377
378 // get the file version here.
379 m_version = parseInt( aReader, line, &line );
380
381 // The next lines are the lib list section, and are mainly comments, like:
382 // LIBS:power
383 // the lib list is not used, but is in schematic file just in case.
384 // It is usually not empty, but we accept empty list.
385 // If empty, there is a legacy section, not used
386 // EELAYER i j
387 // and the last line is
388 // EELAYER END
389 // Skip all lines until the end of header "EELAYER END" is found
390 while( aReader.ReadLine() )
391 {
392 checkpoint();
393
394 line = aReader.Line();
395
396 while( *line == ' ' )
397 line++;
398
399 if( strCompare( "EELAYER END", line ) )
400 return;
401 }
402
403 THROW_IO_ERROR( _( "Missing 'EELAYER END'" ) );
404}
405
406
408{
409 wxASSERT( aScreen != nullptr );
410
411 wxString buf;
412 const char* line = aReader.Line();
413
414 PAGE_INFO pageInfo;
415 TITLE_BLOCK tb;
416
417 wxCHECK_RET( strCompare( "$Descr", line, &line ), "Invalid sheet description" );
418
419 parseUnquotedString( buf, aReader, line, &line );
420
421 if( !pageInfo.SetType( buf ) )
422 SCH_PARSE_ERROR( "invalid page size", aReader, line );
423
424 int pagew = parseInt( aReader, line, &line );
425 int pageh = parseInt( aReader, line, &line );
426
427 if( buf == PAGE_INFO::Custom )
428 {
429 pageInfo.SetWidthMils( pagew );
430 pageInfo.SetHeightMils( pageh );
431 }
432 else
433 {
434 wxString orientation;
435
436 // Non custom size, set portrait if its present. Can be empty string which defaults
437 // to landscape.
438 parseUnquotedString( orientation, aReader, line, &line, true );
439
440 if( orientation == "portrait" )
441 pageInfo.SetPortrait( true );
442 }
443
444 aScreen->SetPageSettings( pageInfo );
445
446 while( line != nullptr )
447 {
448 buf.clear();
449
450 if( !aReader.ReadLine() )
451 SCH_PARSE_ERROR( _( "unexpected end of file" ), aReader, line );
452
453 line = aReader.Line();
454
455 if( strCompare( "Sheet", line, &line ) )
456 {
457 aScreen->SetVirtualPageNumber( parseInt( aReader, line, &line ) );
458 aScreen->SetPageCount( parseInt( aReader, line, &line ) );
459 }
460 else if( strCompare( "Title", line, &line ) )
461 {
462 parseQuotedString( buf, aReader, line, &line, true );
463 tb.SetTitle( buf );
464 }
465 else if( strCompare( "Date", line, &line ) )
466 {
467 parseQuotedString( buf, aReader, line, &line, true );
468 tb.SetDate( buf );
469 }
470 else if( strCompare( "Rev", line, &line ) )
471 {
472 parseQuotedString( buf, aReader, line, &line, true );
473 tb.SetRevision( buf );
474 }
475 else if( strCompare( "Comp", line, &line ) )
476 {
477 parseQuotedString( buf, aReader, line, &line, true );
478 tb.SetCompany( buf );
479 }
480 else if( strCompare( "Comment1", line, &line ) )
481 {
482 parseQuotedString( buf, aReader, line, &line, true );
483 tb.SetComment( 0, buf );
484 }
485 else if( strCompare( "Comment2", line, &line ) )
486 {
487 parseQuotedString( buf, aReader, line, &line, true );
488 tb.SetComment( 1, buf );
489 }
490 else if( strCompare( "Comment3", line, &line ) )
491 {
492 parseQuotedString( buf, aReader, line, &line, true );
493 tb.SetComment( 2, buf );
494 }
495 else if( strCompare( "Comment4", line, &line ) )
496 {
497 parseQuotedString( buf, aReader, line, &line, true );
498 tb.SetComment( 3, buf );
499 }
500 else if( strCompare( "Comment5", line, &line ) )
501 {
502 parseQuotedString( buf, aReader, line, &line, true );
503 tb.SetComment( 4, buf );
504 }
505 else if( strCompare( "Comment6", line, &line ) )
506 {
507 parseQuotedString( buf, aReader, line, &line, true );
508 tb.SetComment( 5, buf );
509 }
510 else if( strCompare( "Comment7", line, &line ) )
511 {
512 parseQuotedString( buf, aReader, line, &line, true );
513 tb.SetComment( 6, buf );
514 }
515 else if( strCompare( "Comment8", line, &line ) )
516 {
517 parseQuotedString( buf, aReader, line, &line, true );
518 tb.SetComment( 7, buf );
519 }
520 else if( strCompare( "Comment9", line, &line ) )
521 {
522 parseQuotedString( buf, aReader, line, &line, true );
523 tb.SetComment( 8, buf );
524 }
525 else if( strCompare( "$EndDescr", line ) )
526 {
527 aScreen->SetTitleBlock( tb );
528 return;
529 }
530 }
531
532 SCH_PARSE_ERROR( "missing 'EndDescr'", aReader, line );
533}
534
535
537{
538 std::unique_ptr<SCH_SHEET> sheet = std::make_unique<SCH_SHEET>();
539
540 const char* line = aReader.ReadLine();
541
542 while( line != nullptr )
543 {
544 if( strCompare( "S", line, &line ) ) // Sheet dimensions.
545 {
546 VECTOR2I position;
547
548 position.x = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
549 position.y = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
550 sheet->SetPosition( position );
551
552 VECTOR2I size;
553
554 size.x = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
555 size.y = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
556 sheet->SetSize( size );
557 }
558 else if( strCompare( "U", line, &line ) ) // Sheet UUID.
559 {
560 wxString text;
561 parseUnquotedString( text, aReader, line );
562
563 if( text != "00000000" )
564 const_cast<KIID&>( sheet->m_Uuid ) = KIID( text );
565 }
566 else if( *line == 'F' ) // Sheet field.
567 {
568 line++;
569
570 wxString text;
571 int size;
572 int legacy_field_id = parseInt( aReader, line, &line );
573
574 if( legacy_field_id == 0 || legacy_field_id == 1 ) // Sheet name and file name.
575 {
576 parseQuotedString( text, aReader, line, &line );
577 size = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
578
579 SCH_FIELD* field = sheet->GetField( legacy_field_id == 0 ? FIELD_T::SHEET_NAME
580 : FIELD_T::SHEET_FILENAME );
581 field->SetText( text );
582 field->SetTextSize( VECTOR2I( size, size ) );
583 }
584 else // Sheet pin.
585 {
586 // Use a unique_ptr so that we clean up in the case of a throw
587 std::unique_ptr<SCH_SHEET_PIN> sheetPin = std::make_unique<SCH_SHEET_PIN>( sheet.get() );
588
589 sheetPin->SetNumber( legacy_field_id );
590
591 // Can be empty fields.
592 parseQuotedString( text, aReader, line, &line, true );
593
594 sheetPin->SetText( ConvertToNewOverbarNotation( text ) );
595
596 if( line == nullptr )
597 THROW_IO_ERROR( _( "unexpected end of line" ) );
598
599 switch( parseChar( aReader, line, &line ) )
600 {
601 case 'I': sheetPin->SetShape( LABEL_FLAG_SHAPE::L_INPUT ); break;
602 case 'O': sheetPin->SetShape( LABEL_FLAG_SHAPE::L_OUTPUT ); break;
603 case 'B': sheetPin->SetShape( LABEL_FLAG_SHAPE::L_BIDI ); break;
604 case 'T': sheetPin->SetShape( LABEL_FLAG_SHAPE::L_TRISTATE ); break;
605 case 'U': sheetPin->SetShape( LABEL_FLAG_SHAPE::L_UNSPECIFIED ); break;
606 default: SCH_PARSE_ERROR( "invalid sheet pin type", aReader, line );
607 }
608
609 switch( parseChar( aReader, line, &line ) )
610 {
611 case 'R': sheetPin->SetSide( SHEET_SIDE::RIGHT ); break;
612 case 'T': sheetPin->SetSide( SHEET_SIDE::TOP ); break;
613 case 'B': sheetPin->SetSide( SHEET_SIDE::BOTTOM ); break;
614 case 'L': sheetPin->SetSide( SHEET_SIDE::LEFT ); break;
615 default:
616 SCH_PARSE_ERROR( "invalid sheet pin side", aReader, line );
617 }
618
619 VECTOR2I position;
620
621 position.x = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
622 position.y = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
623 sheetPin->SetPosition( position );
624
625 size = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
626
627 sheetPin->SetTextSize( VECTOR2I( size, size ) );
628
629 sheet->AddPin( sheetPin.release() );
630 }
631 }
632 else if( strCompare( "$EndSheet", line ) )
633 {
634 sheet->AutoplaceFields( nullptr, AUTOPLACE_AUTO );
635 return sheet.release();
636 }
637
638 line = aReader.ReadLine();
639 }
640
641 SCH_PARSE_ERROR( "missing '$EndSheet`", aReader, line );
642
643 return nullptr; // Prevents compiler warning. Should never get here.
644}
645
646
648{
649 std::unique_ptr<SCH_BITMAP> bitmap = std::make_unique<SCH_BITMAP>();
650 REFERENCE_IMAGE& refImage = bitmap->GetReferenceImage();
651
652 const char* line = aReader.Line();
653
654 wxCHECK( strCompare( "$Bitmap", line, &line ), nullptr );
655
656 line = aReader.ReadLine();
657
658 while( line != nullptr )
659 {
660 if( strCompare( "Pos", line, &line ) )
661 {
662 VECTOR2I position;
663
664 position.x = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
665 position.y = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
666 bitmap->SetPosition( position );
667 }
668 else if( strCompare( "Scale", line, &line ) )
669 {
670 auto scalefactor = parseDouble( aReader, line, &line );
671
672 // Prevent scalefactor values that cannot be displayed.
673 // In the case of a bad value, we accept that the image might be mis-scaled
674 // rather than removing the full image. Users can then edit the scale factor in
675 // Eeschema to the appropriate value
676 if( !std::isnormal( scalefactor ) )
677 scalefactor = 1.0;
678
679 refImage.SetImageScale( scalefactor );
680 }
681 else if( strCompare( "Data", line, &line ) )
682 {
683 wxMemoryBuffer buffer;
684
685 while( line )
686 {
687 if( !aReader.ReadLine() )
688 SCH_PARSE_ERROR( _( "Unexpected end of file" ), aReader, line );
689
690 line = aReader.Line();
691
692 if( strCompare( "EndData", line ) )
693 {
694 // all the PNG date is read.
695 refImage.ReadImageFile( buffer );
696
697 // Legacy file formats assumed 300 image PPI at load.
698 const BITMAP_BASE& bitmapImage = refImage.GetImage();
699 refImage.SetImageScale( refImage.GetImageScale() * bitmapImage.GetPPI()
700 / 300.0 );
701 break;
702 }
703
704 // Read PNG data, stored in hexadecimal,
705 // each byte = 2 hexadecimal digits and a space between 2 bytes
706 // and put it in memory stream buffer
707 // Note:
708 // Some old files created bu the V4 schematic versions have a extra
709 // "$EndBitmap" at the end of the hexadecimal data. (Probably due to
710 // a bug). So discard it
711 int len = strlen( line );
712
713 for( ; len > 0 && !isspace( *line ) && '$' != *line; len -= 3, line += 3 )
714 {
715 int value = 0;
716
717 if( sscanf( line, "%X", &value ) == 1 )
718 buffer.AppendByte( (char) value );
719 else
720 THROW_IO_ERROR( "invalid PNG data" );
721 }
722 }
723
724 if( line == nullptr )
725 THROW_IO_ERROR( _( "unexpected end of file" ) );
726 }
727 else if( strCompare( "$EndBitmap", line ) )
728 {
729 return bitmap.release();
730 }
731
732 line = aReader.ReadLine();
733 }
734
735 THROW_IO_ERROR( _( "unexpected end of file" ) );
736}
737
738
740{
741 std::unique_ptr<SCH_JUNCTION> junction = std::make_unique<SCH_JUNCTION>();
742
743 const char* line = aReader.Line();
744
745 wxCHECK( strCompare( "Connection", line, &line ), nullptr );
746
747 wxString name;
748
749 parseUnquotedString( name, aReader, line, &line );
750
751 VECTOR2I position;
752
753 position.x = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
754 position.y = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
755 junction->SetPosition( position );
756
757 return junction.release();
758}
759
760
762{
763 std::unique_ptr<SCH_NO_CONNECT> no_connect = std::make_unique<SCH_NO_CONNECT>();
764
765 const char* line = aReader.Line();
766
767 wxCHECK( strCompare( "NoConn", line, &line ), nullptr );
768
769 wxString name;
770
771 parseUnquotedString( name, aReader, line, &line );
772
773 VECTOR2I position;
774
775 position.x = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
776 position.y = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
777 no_connect->SetPosition( position );
778
779 return no_connect.release();
780}
781
782
784{
785 std::unique_ptr<SCH_LINE> wire = std::make_unique<SCH_LINE>();
786
787 const char* line = aReader.Line();
788
789 wxCHECK( strCompare( "Wire", line, &line ), nullptr );
790
791 if( strCompare( "Wire", line, &line ) )
792 wire->SetLayer( LAYER_WIRE );
793 else if( strCompare( "Bus", line, &line ) )
794 wire->SetLayer( LAYER_BUS );
795 else if( strCompare( "Notes", line, &line ) )
796 wire->SetLayer( LAYER_NOTES );
797 else
798 SCH_PARSE_ERROR( "invalid line type", aReader, line );
799
800 if( !strCompare( "Line", line, &line ) )
801 SCH_PARSE_ERROR( "invalid wire definition", aReader, line );
802
803 // The default graphical line style was Dashed.
804 if( wire->GetLayer() == LAYER_NOTES )
805 wire->SetLineStyle( LINE_STYLE::DASH );
806
807 // Since Sept 15, 2017, a line style is alloved (width, style, color)
808 // Only non default values are stored
809 while( !is_eol( *line ) )
810 {
811 wxString buf;
812
813 parseUnquotedString( buf, aReader, line, &line );
814
815 if( buf == ")" )
816 continue;
817
818 else if( buf == T_WIDTH )
819 {
820 int size = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
821 wire->SetLineWidth( size );
822 }
823 else if( buf == T_STYLE )
824 {
825 parseUnquotedString( buf, aReader, line, &line );
826
827 if( buf == wxT( "solid" ) )
828 wire->SetLineStyle( LINE_STYLE::SOLID );
829 else if( buf == wxT( "dashed" ) )
830 wire->SetLineStyle( LINE_STYLE::DASH );
831 else if( buf == wxT( "dash_dot" ) )
832 wire->SetLineStyle( LINE_STYLE::DASHDOT );
833 else if( buf == wxT( "dotted" ) )
834 wire->SetLineStyle( LINE_STYLE::DOT );
835 }
836 else // should be the color parameter.
837 {
838 // The color param is something like rgb(150, 40, 191)
839 // and because there is no space between ( and 150
840 // the first param is inside buf.
841 // So break keyword and the first param into 2 separate strings.
842 wxString prm, keyword;
843 keyword = buf.BeforeLast( '(', &prm );
844
845 if( ( keyword == T_COLOR ) || ( keyword == T_COLORA ) )
846 {
847 long color[4] = { 0 };
848
849 int ii = 0;
850
851 if( !prm.IsEmpty() )
852 {
853 prm.ToLong( &color[ii] );
854 ii++;
855 }
856
857 int prm_count = ( keyword == T_COLORA ) ? 4 : 3;
858
859 // fix opacity to 1.0 or 255, when not exists in file
860 color[3] = 255;
861
862 for(; ii < prm_count && !is_eol( *line ); ii++ )
863 {
864 color[ii] = parseInt( aReader, line, &line );
865
866 // Skip the separator between values
867 if( *line == ',' || *line == ' ')
868 line++;
869 }
870
871 wire->SetLineColor( color[0]/255.0, color[1]/255.0, color[2]/255.0,color[3]/255.0 );
872 }
873 }
874 }
875
876 // Read the segment en points coordinates:
877 line = aReader.ReadLine();
878
879 VECTOR2I begin, end;
880
881 begin.x = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
882 begin.y = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
883 end.x = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
884 end.y = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
885
886 wire->SetStartPoint( begin );
887 wire->SetEndPoint( end );
888
889 return wire.release();
890}
891
892
894{
895 const char* line = aReader.Line();
896
897 wxCHECK( strCompare( "Entry", line, &line ), nullptr );
898
899 std::unique_ptr<SCH_BUS_ENTRY_BASE> busEntry;
900
901 if( strCompare( "Wire", line, &line ) )
902 {
903 busEntry = std::make_unique<SCH_BUS_WIRE_ENTRY>();
904
905 if( !strCompare( "Line", line, &line ) )
906 SCH_PARSE_ERROR( "invalid bus entry definition expected 'Line'", aReader, line );
907 }
908 else if( strCompare( "Bus", line, &line ) )
909 {
910 busEntry = std::make_unique<SCH_BUS_BUS_ENTRY>();
911
912 if( !strCompare( "Bus", line, &line ) )
913 SCH_PARSE_ERROR( "invalid bus entry definition expected 'Bus'", aReader, line );
914 }
915 else
916 {
917 SCH_PARSE_ERROR( "invalid bus entry type", aReader, line );
918 }
919
920 line = aReader.ReadLine();
921
922 VECTOR2I pos;
923 VECTOR2I size;
924
925 pos.x = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
926 pos.y = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
927 size.x = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
928 size.y = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
929
930 size.x -= pos.x;
931 size.y -= pos.y;
932
933 busEntry->SetPosition( pos );
934 busEntry->SetSize( size );
935
936 return busEntry.release();
937}
938
939
940// clang-format off
941const std::map<LABEL_FLAG_SHAPE, const char*> sheetLabelNames
942{
943 { LABEL_FLAG_SHAPE::L_INPUT, "Input" },
944 { LABEL_FLAG_SHAPE::L_OUTPUT, "Output" },
945 { LABEL_FLAG_SHAPE::L_BIDI, "BiDi" },
946 { LABEL_FLAG_SHAPE::L_TRISTATE, "3State" },
948};
949// clang-format on
950
951
953{
954 const char* line = aReader.Line();
955 KICAD_T textType = TYPE_NOT_INIT;
956
957 wxCHECK( strCompare( "Text", line, &line ), nullptr );
958
959 if( strCompare( "Notes", line, &line ) )
960 {
961 textType = SCH_TEXT_T;
962 }
963 else if( strCompare( "Label", line, &line ) )
964 {
965 textType = SCH_LABEL_T;
966 }
967 else if( strCompare( "HLabel", line, &line ) )
968 {
969 textType = SCH_HIER_LABEL_T;
970 }
971 else if( strCompare( "GLabel", line, &line ) )
972 {
973 // Prior to version 2, the SCH_GLOBALLABEL object did not exist.
974 if( m_version == 1 )
975 textType = SCH_HIER_LABEL_T;
976 else
977 textType = SCH_GLOBAL_LABEL_T;
978 }
979 else
980 {
981 SCH_PARSE_ERROR( "unknown Text type", aReader, line );
982 }
983
984 VECTOR2I position;
985
986 position.x = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
987 position.y = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
988
989 std::unique_ptr<SCH_TEXT> text;
990
991 switch( textType )
992 {
993 case SCH_TEXT_T: text.reset( new SCH_TEXT( position ) ); break;
994 case SCH_LABEL_T: text.reset( new SCH_LABEL( position ) ); break;
995 case SCH_HIER_LABEL_T: text.reset( new SCH_HIERLABEL( position ) ); break;
996 case SCH_GLOBAL_LABEL_T: text.reset( new SCH_GLOBALLABEL( position ) ); break;
997 default: break;
998 }
999
1000 int spinStyle = parseInt( aReader, line, &line );
1001
1002 // Sadly we store the orientation of hierarchical and global labels using a different
1003 // int encoding than that for local labels:
1004 // Global Local
1005 // Left justified 0 2
1006 // Up 1 3
1007 // Right justified 2 0
1008 // Down 3 1
1009 // So we must flip it as the enum is setup with the "global" numbering
1010 if( textType != SCH_GLOBAL_LABEL_T && textType != SCH_HIER_LABEL_T )
1011 {
1012 if( spinStyle == 0 )
1013 spinStyle = 2;
1014 else if( spinStyle == 2 )
1015 spinStyle = 0;
1016 }
1017
1018 int size = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
1019
1020 text->SetTextSize( VECTOR2I( size, size ) );
1021
1022 if( textType == SCH_LABEL_T || textType == SCH_HIER_LABEL_T || textType == SCH_GLOBAL_LABEL_T )
1023 {
1024 SCH_LABEL_BASE* label = static_cast<SCH_LABEL_BASE*>( text.get() );
1025
1026 label->SetSpinStyle( static_cast<SPIN_STYLE::SPIN>( spinStyle ) );
1027
1028 // Parse the global and hierarchical label type.
1029 if( textType == SCH_HIER_LABEL_T || textType == SCH_GLOBAL_LABEL_T )
1030 {
1031 auto resultIt = std::find_if( sheetLabelNames.begin(), sheetLabelNames.end(),
1032 [ &line ]( const auto& it )
1033 {
1034 return strCompare( it.second, line, &line );
1035 } );
1036
1037 if( resultIt != sheetLabelNames.end() )
1038 label->SetShape( resultIt->first );
1039 else
1040 SCH_PARSE_ERROR( "invalid label type", aReader, line );
1041 }
1042 }
1043 else if( textType == SCH_TEXT_T )
1044 {
1045 switch( spinStyle )
1046 {
1047 case SPIN_STYLE::RIGHT: // Horiz Normal Orientation
1048 text->SetTextAngle( ANGLE_HORIZONTAL );
1049 text->SetHorizJustify( GR_TEXT_H_ALIGN_LEFT );
1050 break;
1051
1052 case SPIN_STYLE::UP: // Vert Orientation UP
1053 text->SetTextAngle( ANGLE_VERTICAL );
1054 text->SetHorizJustify( GR_TEXT_H_ALIGN_LEFT );
1055 break;
1056
1057 case SPIN_STYLE::LEFT: // Horiz Orientation - Right justified
1058 text->SetTextAngle( ANGLE_HORIZONTAL );
1059 text->SetHorizJustify( GR_TEXT_H_ALIGN_RIGHT );
1060 break;
1061
1062 case SPIN_STYLE::BOTTOM: // Vert Orientation BOTTOM
1063 text->SetTextAngle( ANGLE_VERTICAL );
1064 text->SetHorizJustify( GR_TEXT_H_ALIGN_RIGHT );
1065 break;
1066 }
1067
1068 text->SetVertJustify( GR_TEXT_V_ALIGN_BOTTOM );
1069 }
1070
1071 int penWidth = 0;
1072
1073 // The following tokens do not exist in version 1 schematic files,
1074 // and not always in version 2 for HLabels and GLabels
1075 if( m_version > 1 )
1076 {
1077 if( m_version > 2 || *line >= ' ' )
1078 {
1079 if( strCompare( "Italic", line, &line ) )
1080 text->SetItalicFlag( true );
1081 else if( !strCompare( "~", line, &line ) )
1082 SCH_PARSE_ERROR( _( "expected 'Italics' or '~'" ), aReader, line );
1083 }
1084
1085 // The penWidth token does not exist in older versions of the schematic file format
1086 // so calling parseInt will be made only if the EOL is not reached.
1087 if( *line >= ' ' )
1088 penWidth = parseInt( aReader, line, &line );
1089 }
1090
1091 text->SetBoldFlag( penWidth != 0 );
1092 text->SetTextThickness( penWidth != 0 ? GetPenSizeForBold( size ) : 0 );
1093
1094 // Read the text string for the text.
1095 char* tmp = aReader.ReadLine();
1096
1097 tmp = strtok( tmp, "\r\n" );
1098 wxString val = From_UTF8( tmp );
1099
1100 for( ; ; )
1101 {
1102 size_t i = val.find( wxT( "\\n" ) );
1103
1104 if( i == wxString::npos )
1105 break;
1106
1107 val.erase( i, 2 );
1108 val.insert( i, wxT( "\n" ) );
1109 }
1110
1111 text->SetText( ConvertToNewOverbarNotation( val ) );
1112
1113 return text.release();
1114}
1115
1116
1118{
1119 const char* line = aReader.Line();
1120
1121 wxCHECK( strCompare( "$Comp", line, &line ), nullptr );
1122
1123 std::unique_ptr<SCH_SYMBOL> symbol = std::make_unique<SCH_SYMBOL>();
1124
1125 line = aReader.ReadLine();
1126
1127 while( line != nullptr )
1128 {
1129 if( strCompare( "L", line, &line ) )
1130 {
1131 wxString libName;
1132 size_t pos = 2; // "X" plus ' ' space character.
1133 wxString utf8Line = wxString::FromUTF8( line );
1134 wxStringTokenizer tokens( utf8Line, " \r\n\t" );
1135
1136 if( tokens.CountTokens() < 2 )
1137 THROW_PARSE_ERROR( "invalid symbol library definition", aReader.GetSource(),
1138 aReader.Line(), aReader.LineNumber(), pos );
1139
1140 libName = tokens.GetNextToken();
1141 libName.Replace( "~", " " );
1142
1143 LIB_ID libId;
1144
1145 // Prior to schematic version 4, library IDs did not have a library nickname so
1146 // parsing the symbol name with LIB_ID::Parse() would break symbol library links
1147 // that contained '/' and ':' characters.
1148 if( m_version > 3 )
1149 libId.Parse( libName, true );
1150 else
1151 libId.SetLibItemName( libName );
1152
1153 symbol->SetLibId( libId );
1154
1155 wxString refDesignator = tokens.GetNextToken();
1156
1157 refDesignator.Replace( "~", " " );
1158
1159 wxString prefix = refDesignator;
1160
1161 while( prefix.Length() )
1162 {
1163 if( ( prefix.Last() < '0' || prefix.Last() > '9') && prefix.Last() != '?' )
1164 break;
1165
1166 prefix.RemoveLast();
1167 }
1168
1169 // Avoid a prefix containing trailing/leading spaces
1170 prefix.Trim( true );
1171 prefix.Trim( false );
1172
1173 if( prefix.IsEmpty() )
1174 symbol->SetPrefix( wxString( "U" ) );
1175 else
1176 symbol->SetPrefix( prefix );
1177 }
1178 else if( strCompare( "U", line, &line ) )
1179 {
1180 // This fixes a potentially buggy files caused by unit being set to zero which
1181 // causes netlist issues. See https://bugs.launchpad.net/kicad/+bug/1677282.
1182 int unit = parseInt( aReader, line, &line );
1183
1184 if( unit == 0 )
1185 {
1186 unit = 1;
1187
1188 // Set the file as modified so the user can be warned.
1189 if( m_rootSheet->GetScreen() )
1191 }
1192
1193 symbol->SetUnit( unit );
1194
1195 // Same can also happen with the body style ("convert") parameter
1196 int bodyStyle = parseInt( aReader, line, &line );
1197
1198 if( bodyStyle == 0 )
1199 {
1200 bodyStyle = 1;
1201
1202 // Set the file as modified so the user can be warned.
1203 if( m_rootSheet->GetScreen() )
1205 }
1206
1207 symbol->SetBodyStyle( bodyStyle );
1208
1209 wxString text;
1210 parseUnquotedString( text, aReader, line, &line );
1211
1212 if( text != "00000000" )
1213 const_cast<KIID&>( symbol->m_Uuid ) = KIID( text );
1214 }
1215 else if( strCompare( "P", line, &line ) )
1216 {
1217 VECTOR2I pos;
1218
1219 pos.x = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
1220 pos.y = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
1221 symbol->SetPosition( pos );
1222 }
1223 else if( strCompare( "AR", line, &line ) )
1224 {
1225 const char* strCompare = "Path=";
1226 int len = strlen( strCompare );
1227
1228 if( strncasecmp( strCompare, line, len ) != 0 )
1229 SCH_PARSE_ERROR( "missing 'Path=' token", aReader, line );
1230
1231 line += len;
1232 wxString pathStr, reference, unit;
1233
1234 parseQuotedString( pathStr, aReader, line, &line );
1235
1236 // Note: AR path excludes root sheet, but includes symbol. Drop the symbol ID
1237 // since it's already defined in the symbol itself.
1238 KIID_PATH path( pathStr );
1239
1240 if( path.size() > 0 )
1241 path.pop_back();
1242
1243 // In the new file format, the root schematic UUID is used as the virtual SCH_SHEET
1244 // UUID so we need to prefix it to the symbol path so the symbol instance paths
1245 // get saved with the root schematic UUID.
1246 if( !m_appending )
1247 path.insert( path.begin(), m_rootSheet->GetScreen()->GetUuid() );
1248
1249 strCompare = "Ref=";
1250 len = strlen( strCompare );
1251
1252 if( strncasecmp( strCompare, line, len ) != 0 )
1253 SCH_PARSE_ERROR( "missing 'Ref=' token", aReader, line );
1254
1255 line+= len;
1256 parseQuotedString( reference, aReader, line, &line );
1257
1258 strCompare = "Part=";
1259 len = strlen( strCompare );
1260
1261 if( strncasecmp( strCompare, line, len ) != 0 )
1262 SCH_PARSE_ERROR( "missing 'Part=' token", aReader, line );
1263
1264 line+= len;
1265 parseQuotedString( unit, aReader, line, &line );
1266
1267 long tmp;
1268
1269 if( !unit.ToLong( &tmp, 10 ) )
1270 SCH_PARSE_ERROR( "expected integer value", aReader, line );
1271
1272 if( tmp < 0 || tmp > MAX_UNIT_COUNT_PER_PACKAGE )
1273 SCH_PARSE_ERROR( "unit value out of range", aReader, line );
1274
1275 symbol->AddHierarchicalReference( path, reference, (int)tmp );
1276 symbol->GetField( FIELD_T::REFERENCE )->SetText( reference );
1277 }
1278 else if( strCompare( "F", line, &line ) )
1279 {
1280 int legacy_field_id = parseInt( aReader, line, &line );
1281
1282 wxString text, name;
1283
1284 parseQuotedString( text, aReader, line, &line, true );
1285
1286 char orientation = parseChar( aReader, line, &line );
1287 VECTOR2I pos;
1288 pos.x = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
1289 pos.y = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
1290
1291 // Y got inverted in symbol coordinates
1292 pos.y = -( pos.y - symbol->GetY() ) + symbol->GetY();
1293
1294 int size = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
1295 int attributes = parseHex( aReader, line, &line );
1296
1297 SCH_FIELD* field;
1298
1299 // Map fixed legacy IDs
1300 switch( legacy_field_id )
1301 {
1302 case 0: field = symbol->GetField( FIELD_T::REFERENCE ); break;
1303 case 1: field = symbol->GetField( FIELD_T::VALUE ); break;
1304 case 2: field = symbol->GetField( FIELD_T::FOOTPRINT ); break;
1305 case 3: field = symbol->GetField( FIELD_T::DATASHEET ); break;
1306
1307 default:
1308 field = symbol->AddField( SCH_FIELD( symbol.get(), FIELD_T::USER ) );
1309 break;
1310 }
1311
1312 // Prior to version 2 of the schematic file format, none of the following existed.
1313 if( m_version > 1 )
1314 {
1315 wxString textAttrs;
1316 char hjustify = parseChar( aReader, line, &line );
1317
1318 parseUnquotedString( textAttrs, aReader, line, &line );
1319
1320 // The name of the field is optional.
1321 parseQuotedString( name, aReader, line, &line, true );
1322
1323 if( hjustify == 'L' )
1325 else if( hjustify == 'R' )
1327 else if( hjustify != 'C' )
1328 SCH_PARSE_ERROR( "symbol field text horizontal justification must be "
1329 "L, R, or C", aReader, line );
1330
1331 // We are guaranteed to have a least one character here for older file formats
1332 // otherwise an exception would have been raised..
1333 if( textAttrs[0] == 'T' )
1335 else if( textAttrs[0] == 'B' )
1337 else if( textAttrs[0] != 'C' )
1338 SCH_PARSE_ERROR( "symbol field text vertical justification must be "
1339 "B, T, or C", aReader, line );
1340
1341 // Newer file formats include the bold and italics text attribute.
1342 if( textAttrs.Length() > 1 )
1343 {
1344 if( textAttrs.Length() != 3 )
1345 {
1346 SCH_PARSE_ERROR( _( "symbol field text attributes must be 3 characters wide" ),
1347 aReader, line );
1348 }
1349
1350 if( textAttrs[1] == 'I' )
1351 {
1352 field->SetItalicFlag( true );
1353 }
1354 else if( textAttrs[1] != 'N' )
1355 {
1356 SCH_PARSE_ERROR( "symbol field text italics indicator must be I or N",
1357 aReader, line );
1358 }
1359
1360 if( textAttrs[2] == 'B' )
1361 {
1362 field->SetBoldFlag( true );
1363 }
1364 else if( textAttrs[2] != 'N' )
1365 {
1366 SCH_PARSE_ERROR( "symbol field text bold indicator must be B or N",
1367 aReader, line );
1368 }
1369 }
1370 }
1371
1372 field->SetText( text );
1373 field->SetTextPos( pos );
1374 field->SetVisible( !attributes );
1375 field->SetTextSize( VECTOR2I( size, size ) );
1376
1377 if( orientation == 'H' )
1379 else if( orientation == 'V' )
1380 field->SetTextAngle( ANGLE_VERTICAL );
1381 else
1382 SCH_PARSE_ERROR( "symbol field orientation must be H or V", aReader, line );
1383
1384 if( name.IsEmpty() )
1385 {
1386 if( field->IsMandatory() )
1387 name = GetCanonicalFieldName( field->GetId() );
1388 else
1389 name = GetUserFieldName( legacy_field_id, !DO_TRANSLATE );
1390 }
1391
1392 field->SetName( name );
1393 }
1394 else if( strCompare( "$EndComp", line ) )
1395 {
1396 if( !m_appending )
1397 {
1399 {
1401 path.push_back( m_rootSheet->GetScreen()->GetUuid() );
1402
1403 SCH_SYMBOL_INSTANCE instance;
1404 instance.m_Path = path;
1405 instance.m_Reference = symbol->GetField( FIELD_T::REFERENCE )->GetText();
1406 instance.m_Unit = symbol->GetUnit();
1407 symbol->AddHierarchicalReference( instance );
1408 }
1409 else
1410 {
1411 for( const SCH_SYMBOL_INSTANCE& instance : symbol->GetInstances() )
1412 {
1413 SCH_SYMBOL_INSTANCE tmpInstance = instance;
1414 symbol->AddHierarchicalReference( tmpInstance );
1415 }
1416 }
1417 }
1418
1419 // Ensure all flags (some are set by previous initializations) are reset:
1420 symbol->ClearFlags();
1421 return symbol.release();
1422 }
1423 else
1424 {
1425 // There are two lines that begin with a tab or spaces that includes a line with the
1426 // redundant position information and the transform matrix settings.
1427
1428 // Parse the redundant position information just the same to check for formatting
1429 // errors.
1430 parseInt( aReader, line, &line ); // Always 1.
1431 parseInt( aReader, line, &line ); // The X coordinate.
1432 parseInt( aReader, line, &line ); // The Y coordinate.
1433
1434 line = aReader.ReadLine();
1435
1436 TRANSFORM transform;
1437
1438 transform.x1 = parseInt( aReader, line, &line );
1439
1440 if( transform.x1 < -1 || transform.x1 > 1 )
1441 SCH_PARSE_ERROR( "invalid symbol X1 transform value", aReader, line );
1442
1443 transform.y1 = -parseInt( aReader, line, &line );
1444
1445 if( transform.y1 < -1 || transform.y1 > 1 )
1446 SCH_PARSE_ERROR( "invalid symbol Y1 transform value", aReader, line );
1447
1448 transform.x2 = parseInt( aReader, line, &line );
1449
1450 if( transform.x2 < -1 || transform.x2 > 1 )
1451 SCH_PARSE_ERROR( "invalid symbol X2 transform value", aReader, line );
1452
1453 transform.y2 = -parseInt( aReader, line, &line );
1454
1455 if( transform.y2 < -1 || transform.y2 > 1 )
1456 SCH_PARSE_ERROR( "invalid symbol Y2 transform value", aReader, line );
1457
1458 symbol->SetTransform( transform );
1459 }
1460
1461 line = aReader.ReadLine();
1462 }
1463
1464 SCH_PARSE_ERROR( "invalid symbol line", aReader, line );
1465
1466 return nullptr; // Prevents compiler warning. Should never get here.
1467}
1468
1469
1470std::shared_ptr<BUS_ALIAS> SCH_IO_KICAD_LEGACY::loadBusAlias( LINE_READER& aReader,
1471 SCH_SCREEN* aScreen )
1472{
1473 auto busAlias = std::make_shared<BUS_ALIAS>( aScreen );
1474 const char* line = aReader.Line();
1475
1476 wxCHECK( strCompare( "BusAlias", line, &line ), nullptr );
1477
1478 wxString buf;
1479 parseUnquotedString( buf, aReader, line, &line );
1480 busAlias->SetName( buf );
1481
1482 while( *line != '\0' )
1483 {
1484 buf.clear();
1485 parseUnquotedString( buf, aReader, line, &line, true );
1486
1487 if( !buf.IsEmpty() )
1488 busAlias->Members().emplace_back( buf );
1489 }
1490
1491 return busAlias;
1492}
1493
1494
1495void SCH_IO_KICAD_LEGACY::SaveSchematicFile( const wxString& aFileName, SCH_SHEET* aSheet,
1496 SCHEMATIC* aSchematic,
1497 const std::map<std::string, UTF8>* aProperties )
1498{
1499 wxCHECK_RET( aSheet != nullptr, "NULL SCH_SHEET object." );
1500 wxCHECK_RET( !aFileName.IsEmpty(), "No schematic file name defined." );
1501
1502 init( aSchematic, aProperties );
1503
1504 wxFileName fn = aFileName;
1505
1506 // File names should be absolute. Don't assume everything relative to the project path
1507 // works properly.
1508 wxASSERT( fn.IsAbsolute() );
1509
1510 FILE_OUTPUTFORMATTER formatter( fn.GetFullPath() );
1511
1512 m_out = &formatter; // no ownership
1513
1514 Format( aSheet );
1515
1516 aSheet->GetScreen()->SetFileExists( true );
1517}
1518
1519
1521{
1522 wxCHECK_RET( aSheet != nullptr, "NULL SCH_SHEET* object." );
1523 wxCHECK_RET( m_schematic != nullptr, "NULL SCHEMATIC* object." );
1524
1525 SCH_SCREEN* screen = aSheet->GetScreen();
1526
1527 wxCHECK( screen, /* void */ );
1528
1529 // Write the header
1530 m_out->Print( 0, "%s %s %d\n", "EESchema", SCHEMATIC_HEAD_STRING, EESCHEMA_VERSION );
1531
1532 // This section is not used, but written for file compatibility
1533 m_out->Print( 0, "EELAYER %d %d\n", SCH_LAYER_ID_COUNT, 0 );
1534 m_out->Print( 0, "EELAYER END\n" );
1535
1536 /* Write page info, ScreenNumber and NumberOfScreen; not very meaningful for
1537 * SheetNumber and Sheet Count in a complex hierarchy, but useful in
1538 * simple hierarchy and flat hierarchy. Used also to search the root
1539 * sheet ( ScreenNumber = 1 ) within the files
1540 */
1541 const TITLE_BLOCK& tb = screen->GetTitleBlock();
1542 const PAGE_INFO& page = screen->GetPageSettings();
1543
1544 m_out->Print( 0, "$Descr %s %d %d%s\n", TO_UTF8( page.GetType() ),
1545 (int)page.GetWidthMils(),
1546 (int)page.GetHeightMils(),
1547 !page.IsCustom() && page.IsPortrait() ? " portrait" : "" );
1548 m_out->Print( 0, "encoding utf-8\n" );
1549 m_out->Print( 0, "Sheet %d %d\n", screen->GetVirtualPageNumber(), screen->GetPageCount() );
1550 m_out->Print( 0, "Title %s\n", EscapedUTF8( tb.GetTitle() ).c_str() );
1551 m_out->Print( 0, "Date %s\n", EscapedUTF8( tb.GetDate() ).c_str() );
1552 m_out->Print( 0, "Rev %s\n", EscapedUTF8( tb.GetRevision() ).c_str() );
1553 m_out->Print( 0, "Comp %s\n", EscapedUTF8( tb.GetCompany() ).c_str() );
1554 m_out->Print( 0, "Comment1 %s\n", EscapedUTF8( tb.GetComment( 0 ) ).c_str() );
1555 m_out->Print( 0, "Comment2 %s\n", EscapedUTF8( tb.GetComment( 1 ) ).c_str() );
1556 m_out->Print( 0, "Comment3 %s\n", EscapedUTF8( tb.GetComment( 2 ) ).c_str() );
1557 m_out->Print( 0, "Comment4 %s\n", EscapedUTF8( tb.GetComment( 3 ) ).c_str() );
1558 m_out->Print( 0, "Comment5 %s\n", EscapedUTF8( tb.GetComment( 4 ) ).c_str() );
1559 m_out->Print( 0, "Comment6 %s\n", EscapedUTF8( tb.GetComment( 5 ) ).c_str() );
1560 m_out->Print( 0, "Comment7 %s\n", EscapedUTF8( tb.GetComment( 6 ) ).c_str() );
1561 m_out->Print( 0, "Comment8 %s\n", EscapedUTF8( tb.GetComment( 7 ) ).c_str() );
1562 m_out->Print( 0, "Comment9 %s\n", EscapedUTF8( tb.GetComment( 8 ) ).c_str() );
1563 m_out->Print( 0, "$EndDescr\n" );
1564
1565 for( const std::shared_ptr<BUS_ALIAS>& alias : screen->GetBusAliases() )
1566 saveBusAlias( alias );
1567
1568 // Enforce item ordering
1569 auto cmp = []( const SCH_ITEM* a, const SCH_ITEM* b ) { return *a < *b; };
1570 std::multiset<SCH_ITEM*, decltype( cmp )> save_map( cmp );
1571
1572 for( SCH_ITEM* item : screen->Items() )
1573 save_map.insert( item );
1574
1575
1576 for( auto& item : save_map )
1577 {
1578 switch( item->Type() )
1579 {
1580 case SCH_SYMBOL_T:
1581 saveSymbol( static_cast<SCH_SYMBOL*>( item ) );
1582 break;
1583 case SCH_BITMAP_T:
1584 saveBitmap( static_cast<const SCH_BITMAP&>( *item ) );
1585 break;
1586 case SCH_SHEET_T:
1587 saveSheet( static_cast<SCH_SHEET*>( item ) );
1588 break;
1589 case SCH_JUNCTION_T:
1590 saveJunction( static_cast<SCH_JUNCTION*>( item ) );
1591 break;
1592 case SCH_NO_CONNECT_T:
1593 saveNoConnect( static_cast<SCH_NO_CONNECT*>( item ) );
1594 break;
1597 saveBusEntry( static_cast<SCH_BUS_ENTRY_BASE*>( item ) );
1598 break;
1599 case SCH_LINE_T:
1600 saveLine( static_cast<SCH_LINE*>( item ) );
1601 break;
1602 case SCH_TEXT_T:
1603 case SCH_LABEL_T:
1604 case SCH_GLOBAL_LABEL_T:
1605 case SCH_HIER_LABEL_T:
1606 saveText( static_cast<SCH_TEXT*>( item ) );
1607 break;
1608 default:
1609 wxASSERT( "Unexpected schematic object type in SCH_IO_KICAD_LEGACY::Format()" );
1610 }
1611 }
1612
1613 m_out->Print( 0, "$EndSCHEMATC\n" );
1614}
1615
1616
1618{
1619 m_out = aFormatter;
1620
1621 for( unsigned i = 0; i < aSelection->GetSize(); ++i )
1622 {
1623 SCH_ITEM* item = (SCH_ITEM*) aSelection->GetItem( i );
1624
1625 switch( item->Type() )
1626 {
1627 case SCH_SYMBOL_T:
1628 saveSymbol( static_cast< SCH_SYMBOL* >( item ) );
1629 break;
1630 case SCH_BITMAP_T:
1631 saveBitmap( static_cast< const SCH_BITMAP& >( *item ) );
1632 break;
1633 case SCH_SHEET_T:
1634 saveSheet( static_cast< SCH_SHEET* >( item ) );
1635 break;
1636 case SCH_JUNCTION_T:
1637 saveJunction( static_cast< SCH_JUNCTION* >( item ) );
1638 break;
1639 case SCH_NO_CONNECT_T:
1640 saveNoConnect( static_cast< SCH_NO_CONNECT* >( item ) );
1641 break;
1644 saveBusEntry( static_cast< SCH_BUS_ENTRY_BASE* >( item ) );
1645 break;
1646 case SCH_LINE_T:
1647 saveLine( static_cast< SCH_LINE* >( item ) );
1648 break;
1649 case SCH_TEXT_T:
1650 case SCH_LABEL_T:
1651 case SCH_GLOBAL_LABEL_T:
1652 case SCH_HIER_LABEL_T:
1653 saveText( static_cast< SCH_TEXT* >( item ) );
1654 break;
1655 default:
1656 wxASSERT( "Unexpected schematic object type in SCH_IO_KICAD_LEGACY::Format()" );
1657 }
1658 }
1659}
1660
1661
1663{
1664 std::string name1;
1665 std::string name2;
1666
1667 // This is redundant with the AR entries below, but it makes the files backwards-compatible.
1668 if( aSymbol->GetInstances().size() > 0 )
1669 {
1670 const SCH_SYMBOL_INSTANCE& instance = aSymbol->GetInstances()[0];
1671 name1 = toUTFTildaText( instance.m_Reference );
1672 }
1673 else
1674 {
1675 if( aSymbol->GetField( FIELD_T::REFERENCE )->GetText().IsEmpty() )
1676 name1 = toUTFTildaText( aSymbol->GetPrefix() );
1677 else
1678 name1 = toUTFTildaText( aSymbol->GetField( FIELD_T::REFERENCE )->GetText() );
1679 }
1680
1681 wxString symbol_name = aSymbol->GetLibId().Format();
1682
1683 if( symbol_name.size() )
1684 {
1685 name2 = toUTFTildaText( symbol_name );
1686 }
1687 else
1688 {
1689 name2 = "_NONAME_";
1690 }
1691
1692 m_out->Print( 0, "$Comp\n" );
1693 m_out->Print( 0, "L %s %s\n", name2.c_str(), name1.c_str() );
1694
1695 // Generate unit number, conversion and timestamp
1696 m_out->Print( 0, "U %d %d %8.8X\n",
1697 aSymbol->GetUnit(),
1698 aSymbol->GetBodyStyle(),
1699 aSymbol->m_Uuid.AsLegacyTimestamp() );
1700
1701 // Save the position
1702 m_out->Print( 0, "P %d %d\n",
1703 schIUScale.IUToMils( aSymbol->GetPosition().x ),
1704 schIUScale.IUToMils( aSymbol->GetPosition().y ) );
1705
1706 /* If this is a complex hierarchy; save hierarchical references.
1707 * but for simple hierarchies it is not necessary.
1708 * the reference inf is already saved
1709 * this is useful for old Eeschema version compatibility
1710 */
1711 if( aSymbol->GetInstances().size() > 1 )
1712 {
1713 for( const SCH_SYMBOL_INSTANCE& instance : aSymbol->GetInstances() )
1714 {
1715 /*format:
1716 * AR Path="/140/2" Ref="C99" Part="1"
1717 * where 140 is the uid of the containing sheet and 2 is the timestamp of this symbol.
1718 * (timestamps are actually 8 hex chars)
1719 * Ref is the conventional symbol reference designator for this 'path'
1720 * Part is the conventional symbol unit selection for this 'path'
1721 */
1722 wxString path = "/";
1723
1724 // Skip root sheet
1725 for( int i = 1; i < (int) instance.m_Path.size(); ++i )
1726 path += instance.m_Path[i].AsLegacyTimestampString() + "/";
1727
1728 m_out->Print( 0, "AR Path=\"%s\" Ref=\"%s\" Part=\"%d\" \n",
1730 TO_UTF8( instance.m_Reference ),
1731 instance.m_Unit );
1732 }
1733 }
1734
1735 // NB: FieldIDs in legacy libraries must be consecutive, and include user fields
1736 int legacy_field_id = 0;
1737
1738 for( SCH_FIELD& field : aSymbol->GetFields() )
1739 saveField( &field, legacy_field_id++ );
1740
1741 // Unit number, position, box ( old standard )
1742 m_out->Print( 0, "\t%-4d %-4d %-4d\n", aSymbol->GetUnit(),
1743 schIUScale.IUToMils( aSymbol->GetPosition().x ),
1744 schIUScale.IUToMils( aSymbol->GetPosition().y ) );
1745
1746 TRANSFORM transform = aSymbol->GetTransform();
1747
1748 m_out->Print( 0, "\t%-4d %-4d %-4d %-4d\n",
1749 transform.x1, transform.y1, transform.x2, transform.y2 );
1750 m_out->Print( 0, "$EndComp\n" );
1751}
1752
1753
1754void SCH_IO_KICAD_LEGACY::saveField( SCH_FIELD* aField, int aLegacyId )
1755{
1756 char hjustify = 'C';
1757
1758 if( aField->GetHorizJustify() == GR_TEXT_H_ALIGN_LEFT )
1759 hjustify = 'L';
1760 else if( aField->GetHorizJustify() == GR_TEXT_H_ALIGN_RIGHT )
1761 hjustify = 'R';
1762
1763 char vjustify = 'C';
1764
1765 if( aField->GetVertJustify() == GR_TEXT_V_ALIGN_BOTTOM )
1766 vjustify = 'B';
1767 else if( aField->GetVertJustify() == GR_TEXT_V_ALIGN_TOP )
1768 vjustify = 'T';
1769
1770 m_out->Print( 0, "F %d %s %c %-3d %-3d %-3d %4.4X %c %c%c%c",
1771 aLegacyId,
1772 EscapedUTF8( aField->GetText() ).c_str(), // wraps in quotes too
1773 aField->GetTextAngle().IsHorizontal() ? 'H' : 'V',
1774 schIUScale.IUToMils( aField->GetLibPosition().x ),
1775 schIUScale.IUToMils( aField->GetLibPosition().y ),
1776 schIUScale.IUToMils( aField->GetTextWidth() ),
1777 !aField->IsVisible(),
1778 hjustify, vjustify,
1779 aField->IsItalic() ? 'I' : 'N',
1780 aField->IsBold() ? 'B' : 'N' );
1781
1782 // Save field name, if the name is user definable
1783 if( !aField->IsMandatory() )
1784 m_out->Print( 0, " %s", EscapedUTF8( aField->GetName() ).c_str() );
1785
1786 m_out->Print( 0, "\n" );
1787}
1788
1789
1791{
1792 const REFERENCE_IMAGE& refImage = aBitmap.GetReferenceImage();
1793
1794 const wxImage* image = refImage.GetImage().GetImageData();
1795
1796 wxCHECK_RET( image != nullptr, "wxImage* is NULL" );
1797
1798 m_out->Print( 0, "$Bitmap\n" );
1799 m_out->Print( 0, "Pos %-4d %-4d\n",
1800 schIUScale.IUToMils( aBitmap.GetPosition().x ),
1801 schIUScale.IUToMils( aBitmap.GetPosition().y ) );
1802 m_out->Print( "%s", fmt::format("Scale {:g}\n", refImage.GetImageScale()).c_str() );
1803 m_out->Print( 0, "Data\n" );
1804
1805 wxMemoryOutputStream stream;
1806
1807 image->SaveFile( stream, wxBITMAP_TYPE_PNG );
1808
1809 // Write binary data in hexadecimal form (ASCII)
1810 wxStreamBuffer* buffer = stream.GetOutputStreamBuffer();
1811 char* begin = (char*) buffer->GetBufferStart();
1812
1813 for( int ii = 0; begin < buffer->GetBufferEnd(); begin++, ii++ )
1814 {
1815 if( ii >= 32 )
1816 {
1817 ii = 0;
1818
1819 m_out->Print( 0, "\n" );
1820 }
1821
1822 m_out->Print( 0, "%2.2X ", *begin & 0xFF );
1823 }
1824
1825 m_out->Print( 0, "\nEndData\n" );
1826 m_out->Print( 0, "$EndBitmap\n" );
1827}
1828
1829
1831{
1832 wxCHECK_RET( aSheet != nullptr, "SCH_SHEET* is NULL" );
1833
1834 m_out->Print( 0, "$Sheet\n" );
1835 m_out->Print( 0, "S %-4d %-4d %-4d %-4d\n",
1836 schIUScale.IUToMils( aSheet->GetPosition().x ),
1837 schIUScale.IUToMils( aSheet->GetPosition().y ),
1838 schIUScale.IUToMils( aSheet->GetSize().x ),
1839 schIUScale.IUToMils( aSheet->GetSize().y ) );
1840
1841 m_out->Print( 0, "U %8.8X\n", aSheet->m_Uuid.AsLegacyTimestamp() );
1842
1843 SCH_FIELD* sheetName = aSheet->GetField( FIELD_T::SHEET_NAME );
1844 SCH_FIELD* fileName = aSheet->GetField( FIELD_T::SHEET_FILENAME );
1845
1846 if( !sheetName->GetText().IsEmpty() )
1847 {
1848 m_out->Print( 0, "F0 %s %d\n",
1849 EscapedUTF8( sheetName->GetText() ).c_str(),
1850 schIUScale.IUToMils( sheetName->GetTextSize().x ) );
1851 }
1852
1853 if( !fileName->GetText().IsEmpty() )
1854 {
1855 m_out->Print( 0, "F1 %s %d\n",
1856 EscapedUTF8( fileName->GetText() ).c_str(),
1857 schIUScale.IUToMils( fileName->GetTextSize().x ) );
1858 }
1859
1860 for( const SCH_SHEET_PIN* pin : aSheet->GetPins() )
1861 {
1862 int type, side;
1863
1864 if( pin->GetText().IsEmpty() )
1865 break;
1866
1867 switch( pin->GetSide() )
1868 {
1869 default:
1870 case SHEET_SIDE::LEFT: side = 'L'; break;
1871 case SHEET_SIDE::RIGHT: side = 'R'; break;
1872 case SHEET_SIDE::TOP: side = 'T'; break;
1873 case SHEET_SIDE::BOTTOM: side = 'B'; break;
1874 }
1875
1876 switch( pin->GetShape() )
1877 {
1878 default:
1879 case LABEL_FLAG_SHAPE::L_UNSPECIFIED: type = 'U'; break;
1880 case LABEL_FLAG_SHAPE::L_INPUT: type = 'I'; break;
1881 case LABEL_FLAG_SHAPE::L_OUTPUT: type = 'O'; break;
1882 case LABEL_FLAG_SHAPE::L_BIDI: type = 'B'; break;
1883 case LABEL_FLAG_SHAPE::L_TRISTATE: type = 'T'; break;
1884 }
1885
1886 m_out->Print( 0, "F%d %s %c %c %-3d %-3d %-3d\n",
1887 pin->GetNumber(),
1888 EscapedUTF8( pin->GetText() ).c_str(), // supplies wrapping quotes
1889 type, side, schIUScale.IUToMils( pin->GetPosition().x ),
1890 schIUScale.IUToMils( pin->GetPosition().y ),
1891 schIUScale.IUToMils( pin->GetTextWidth() ) );
1892 }
1893
1894 m_out->Print( 0, "$EndSheet\n" );
1895}
1896
1897
1899{
1900 wxCHECK_RET( aJunction != nullptr, "SCH_JUNCTION* is NULL" );
1901
1902 m_out->Print( 0, "Connection ~ %-4d %-4d\n",
1903 schIUScale.IUToMils( aJunction->GetPosition().x ),
1904 schIUScale.IUToMils( aJunction->GetPosition().y ) );
1905}
1906
1907
1909{
1910 wxCHECK_RET( aNoConnect != nullptr, "SCH_NOCONNECT* is NULL" );
1911
1912 m_out->Print( 0, "NoConn ~ %-4d %-4d\n",
1913 schIUScale.IUToMils( aNoConnect->GetPosition().x ),
1914 schIUScale.IUToMils( aNoConnect->GetPosition().y ) );
1915}
1916
1917
1919{
1920 wxCHECK_RET( aBusEntry != nullptr, "SCH_BUS_ENTRY_BASE* is NULL" );
1921
1922 if( aBusEntry->GetLayer() == LAYER_WIRE )
1923 {
1924 m_out->Print( 0, "Entry Wire Line\n\t%-4d %-4d %-4d %-4d\n",
1925 schIUScale.IUToMils( aBusEntry->GetPosition().x ),
1926 schIUScale.IUToMils( aBusEntry->GetPosition().y ),
1927 schIUScale.IUToMils( aBusEntry->GetEnd().x ),
1928 schIUScale.IUToMils( aBusEntry->GetEnd().y ) );
1929 }
1930 else
1931 {
1932 m_out->Print( 0, "Entry Bus Bus\n\t%-4d %-4d %-4d %-4d\n",
1933 schIUScale.IUToMils( aBusEntry->GetPosition().x ),
1934 schIUScale.IUToMils( aBusEntry->GetPosition().y ),
1935 schIUScale.IUToMils( aBusEntry->GetEnd().x ),
1936 schIUScale.IUToMils( aBusEntry->GetEnd().y ) );
1937 }
1938}
1939
1940
1942{
1943 wxCHECK_RET( aLine != nullptr, "SCH_LINE* is NULL" );
1944
1945 const char* layer = "Notes";
1946 const char* width = "Line";
1947
1948 if( aLine->GetLayer() == LAYER_WIRE )
1949 layer = "Wire";
1950 else if( aLine->GetLayer() == LAYER_BUS )
1951 layer = "Bus";
1952
1953 m_out->Print( 0, "Wire %s %s", layer, width );
1954
1955 // Write line style (width, type, color) only for non default values
1956 if( aLine->IsGraphicLine() )
1957 {
1958 const STROKE_PARAMS& stroke = aLine->GetStroke();
1959
1960 if( stroke.GetWidth() != 0 )
1961 m_out->Print( 0, " %s %d", T_WIDTH, schIUScale.IUToMils( stroke.GetWidth() ) );
1962
1963 m_out->Print( 0, " %s %s",
1964 T_STYLE,
1966
1967 if( stroke.GetColor() != COLOR4D::UNSPECIFIED )
1968 m_out->Print( 0, " %s", TO_UTF8( stroke.GetColor().ToCSSString() ) );
1969 }
1970
1971 m_out->Print( 0, "\n" );
1972
1973 m_out->Print( 0, "\t%-4d %-4d %-4d %-4d",
1974 schIUScale.IUToMils( aLine->GetStartPoint().x ),
1975 schIUScale.IUToMils( aLine->GetStartPoint().y ),
1976 schIUScale.IUToMils( aLine->GetEndPoint().x ),
1977 schIUScale.IUToMils( aLine->GetEndPoint().y ) );
1978
1979 m_out->Print( 0, "\n");
1980}
1981
1982
1984{
1985 wxCHECK_RET( aText != nullptr, "SCH_TEXT* is NULL" );
1986
1987 const char* italics = "~";
1988 const char* textType = "Notes";
1989
1990 if( aText->IsItalic() )
1991 italics = "Italic";
1992
1993 wxString text = aText->GetText();
1994
1995 SCH_LAYER_ID layer = aText->GetLayer();
1996
1997 if( layer == LAYER_NOTES || layer == LAYER_LOCLABEL )
1998 {
1999 if( layer == LAYER_NOTES )
2000 {
2001 // For compatibility reasons, the text must be saved in only one text line
2002 // so replace all EOLs with \\n
2003 text.Replace( wxT( "\n" ), wxT( "\\n" ) );
2004
2005 // Here we should have no CR or LF character in line
2006 // This is not always the case if a multiline text was copied (using a copy/paste
2007 // function) from a text that uses E.O.L characters that differs from the current
2008 // EOL format. This is mainly the case under Linux using LF symbol when copying
2009 // a text from Windows (using CRLF symbol) so we must just remove the extra CR left
2010 // (or LF left under MacOSX)
2011 for( unsigned ii = 0; ii < text.Len(); )
2012 {
2013 if( text[ii] == 0x0A || text[ii] == 0x0D )
2014 text.erase( ii, 1 );
2015 else
2016 ii++;
2017 }
2018 }
2019 else
2020 {
2021 textType = "Label";
2022 }
2023
2024 int spinStyle = 0;
2025
2026 // Local labels must have their spin style inverted for left and right
2027 if( SCH_LABEL_BASE* label = dynamic_cast<SCH_LABEL_BASE*>( aText ) )
2028 {
2029 spinStyle = static_cast<int>( label->GetSpinStyle() );
2030
2031 if( spinStyle == 0 )
2032 spinStyle = 2;
2033 else if( spinStyle == 2 )
2034 spinStyle = 0;
2035 }
2036
2037 m_out->Print( 0, "Text %s %-4d %-4d %-4d %-4d %s %d\n%s\n", textType,
2038 schIUScale.IUToMils( aText->GetPosition().x ),
2039 schIUScale.IUToMils( aText->GetPosition().y ),
2040 spinStyle,
2041 schIUScale.IUToMils( aText->GetTextWidth() ),
2042 italics, schIUScale.IUToMils( aText->GetTextThickness() ), TO_UTF8( text ) );
2043 }
2044 else if( layer == LAYER_GLOBLABEL || layer == LAYER_HIERLABEL )
2045 {
2046 textType = ( layer == LAYER_GLOBLABEL ) ? "GLabel" : "HLabel";
2047
2048 SCH_LABEL_BASE* label = static_cast<SCH_LABEL_BASE*>( aText );
2049 auto shapeLabelIt = sheetLabelNames.find( label->GetShape() );
2050 wxCHECK_RET( shapeLabelIt != sheetLabelNames.end(), "Shape not found in names list" );
2051
2052 m_out->Print( 0, "Text %s %-4d %-4d %-4d %-4d %s %s %d\n%s\n", textType,
2053 schIUScale.IUToMils( aText->GetPosition().x ),
2054 schIUScale.IUToMils( aText->GetPosition().y ),
2055 static_cast<int>( label->GetSpinStyle() ),
2056 schIUScale.IUToMils( aText->GetTextWidth() ),
2057 shapeLabelIt->second,
2058 italics,
2060 }
2061}
2062
2063
2064void SCH_IO_KICAD_LEGACY::saveBusAlias( std::shared_ptr<BUS_ALIAS> aAlias )
2065{
2066 wxCHECK_RET( aAlias != nullptr, "BUS_ALIAS* is NULL" );
2067
2068 wxString members = boost::algorithm::join( aAlias->Members(), " " );
2069
2070 m_out->Print( 0, "BusAlias %s %s\n",
2071 TO_UTF8( aAlias->GetName() ), TO_UTF8( members ) );
2072}
2073
2074
2075void SCH_IO_KICAD_LEGACY::cacheLib( const wxString& aLibraryFileName,
2076 const std::map<std::string, UTF8>* aProperties )
2077{
2078 if( !m_cache || !m_cache->IsFile( aLibraryFileName ) || m_cache->IsFileChanged() )
2079 {
2080 // a spectacular episode in memory management:
2081 delete m_cache;
2082 m_cache = new SCH_IO_KICAD_LEGACY_LIB_CACHE( aLibraryFileName );
2083
2084 if( !isBuffering( aProperties ) )
2085 m_cache->Load();
2086 }
2087}
2088
2089
2090bool SCH_IO_KICAD_LEGACY::writeDocFile( const std::map<std::string, UTF8>* aProperties )
2091{
2092 std::string propName( SCH_IO_KICAD_LEGACY::PropNoDocFile );
2093
2094 if( aProperties && aProperties->find( propName ) != aProperties->end() )
2095 return false;
2096
2097 return true;
2098}
2099
2100
2101bool SCH_IO_KICAD_LEGACY::isBuffering( const std::map<std::string, UTF8>* aProperties )
2102{
2103 return ( aProperties && aProperties->contains( SCH_IO_KICAD_LEGACY::PropBuffering ) );
2104}
2105
2106
2108{
2109 if( m_cache )
2110 return m_cache->GetModifyHash();
2111
2112 // If the cache hasn't been loaded, it hasn't been modified.
2113 return 0;
2114}
2115
2116
2117void SCH_IO_KICAD_LEGACY::EnumerateSymbolLib( wxArrayString& aSymbolNameList,
2118 const wxString& aLibraryPath,
2119 const std::map<std::string, UTF8>* aProperties )
2120{
2121 bool powerSymbolsOnly = ( aProperties &&
2122 aProperties->find( SYMBOL_LIB_TABLE::PropPowerSymsOnly ) != aProperties->end() );
2123
2124 cacheLib( aLibraryPath, aProperties );
2125
2126 const LIB_SYMBOL_MAP& symbols = m_cache->m_symbols;
2127
2128 for( LIB_SYMBOL_MAP::const_iterator it = symbols.begin(); it != symbols.end(); ++it )
2129 {
2130 if( !powerSymbolsOnly || it->second->IsGlobalPower() )
2131 aSymbolNameList.Add( it->first );
2132 }
2133}
2134
2135
2136void SCH_IO_KICAD_LEGACY::EnumerateSymbolLib( std::vector<LIB_SYMBOL*>& aSymbolList,
2137 const wxString& aLibraryPath,
2138 const std::map<std::string, UTF8>* aProperties )
2139{
2140 bool powerSymbolsOnly = ( aProperties &&
2141 aProperties->find( SYMBOL_LIB_TABLE::PropPowerSymsOnly ) != aProperties->end() );
2142
2143 cacheLib( aLibraryPath, aProperties );
2144
2145 const LIB_SYMBOL_MAP& symbols = m_cache->m_symbols;
2146
2147 for( LIB_SYMBOL_MAP::const_iterator it = symbols.begin(); it != symbols.end(); ++it )
2148 {
2149 if( !powerSymbolsOnly || it->second->IsGlobalPower() )
2150 aSymbolList.push_back( it->second );
2151 }
2152}
2153
2154
2155LIB_SYMBOL* SCH_IO_KICAD_LEGACY::LoadSymbol( const wxString& aLibraryPath,
2156 const wxString& aSymbolName,
2157 const std::map<std::string, UTF8>* aProperties )
2158{
2159 cacheLib( aLibraryPath, aProperties );
2160
2161 LIB_SYMBOL_MAP::const_iterator it = m_cache->m_symbols.find( aSymbolName );
2162
2163 if( it == m_cache->m_symbols.end() )
2164 return nullptr;
2165
2166 return it->second;
2167}
2168
2169
2170void SCH_IO_KICAD_LEGACY::SaveSymbol( const wxString& aLibraryPath, const LIB_SYMBOL* aSymbol,
2171 const std::map<std::string, UTF8>* aProperties )
2172{
2173 cacheLib( aLibraryPath, aProperties );
2174
2175 m_cache->AddSymbol( aSymbol );
2176
2177 if( !isBuffering( aProperties ) )
2178 m_cache->Save( writeDocFile( aProperties ) );
2179}
2180
2181
2182void SCH_IO_KICAD_LEGACY::DeleteSymbol( const wxString& aLibraryPath, const wxString& aSymbolName,
2183 const std::map<std::string, UTF8>* aProperties )
2184{
2185 cacheLib( aLibraryPath, aProperties );
2186
2187 m_cache->DeleteSymbol( aSymbolName );
2188
2189 if( !isBuffering( aProperties ) )
2190 m_cache->Save( writeDocFile( aProperties ) );
2191}
2192
2193
2194void SCH_IO_KICAD_LEGACY::CreateLibrary( const wxString& aLibraryPath,
2195 const std::map<std::string, UTF8>* aProperties )
2196{
2197 if( wxFileExists( aLibraryPath ) )
2198 {
2199 THROW_IO_ERROR( wxString::Format( _( "Symbol library '%s' already exists." ),
2200 aLibraryPath.GetData() ) );
2201 }
2202
2203 delete m_cache;
2204 m_cache = new SCH_IO_KICAD_LEGACY_LIB_CACHE( aLibraryPath );
2206 m_cache->Save( writeDocFile( aProperties ) );
2207 m_cache->Load(); // update m_writable and m_timestamp
2208}
2209
2210
2211bool SCH_IO_KICAD_LEGACY::DeleteLibrary( const wxString& aLibraryPath,
2212 const std::map<std::string, UTF8>* aProperties )
2213{
2214 wxFileName fn = aLibraryPath;
2215
2216 if( !fn.FileExists() )
2217 return false;
2218
2219 // Some of the more elaborate wxRemoveFile() crap puts up its own wxLog dialog
2220 // we don't want that. we want bare metal portability with no UI here.
2221 if( wxRemove( aLibraryPath ) )
2222 {
2223 THROW_IO_ERROR( wxString::Format( _( "Symbol library '%s' cannot be deleted." ),
2224 aLibraryPath.GetData() ) );
2225 }
2226
2227 if( m_cache && m_cache->IsFile( aLibraryPath ) )
2228 {
2229 delete m_cache;
2230 m_cache = nullptr;
2231 }
2232
2233 return true;
2234}
2235
2236
2237void SCH_IO_KICAD_LEGACY::SaveLibrary( const wxString& aLibraryPath,
2238 const std::map<std::string, UTF8>* aProperties )
2239{
2240 if( !m_cache )
2241 m_cache = new SCH_IO_KICAD_LEGACY_LIB_CACHE( aLibraryPath );
2242
2243 wxString oldFileName = m_cache->GetFileName();
2244
2245 if( !m_cache->IsFile( aLibraryPath ) )
2246 {
2247 m_cache->SetFileName( aLibraryPath );
2248 }
2249
2250 // This is a forced save.
2252 m_cache->Save( writeDocFile( aProperties ) );
2253 m_cache->SetFileName( oldFileName );
2254}
2255
2256
2257bool SCH_IO_KICAD_LEGACY::CanReadSchematicFile( const wxString& aFileName ) const
2258{
2259 if( !SCH_IO::CanReadSchematicFile( aFileName ) )
2260 return false;
2261
2262 return IO_UTILS::fileStartsWithPrefix( aFileName, wxT( "EESchema" ), true );
2263}
2264
2265
2266bool SCH_IO_KICAD_LEGACY::CanReadLibrary( const wxString& aFileName ) const
2267{
2268 if( !SCH_IO::CanReadLibrary( aFileName ) )
2269 return false;
2270
2271 return IO_UTILS::fileStartsWithPrefix( aFileName, wxT( "EESchema" ), true );
2272}
2273
2274
2275bool SCH_IO_KICAD_LEGACY::IsLibraryWritable( const wxString& aLibraryPath )
2276{
2277 // Writing legacy symbol libraries is deprecated.
2278 return false;
2279}
2280
2281
2283 int aMinorVersion )
2284{
2285 return SCH_IO_KICAD_LEGACY_LIB_CACHE::LoadPart( reader, aMajorVersion, aMinorVersion );
2286}
2287
2288
2290{
2291 SCH_IO_KICAD_LEGACY_LIB_CACHE::SaveSymbol( symbol, formatter );
2292}
2293
2294
2295
2296const char* SCH_IO_KICAD_LEGACY::PropBuffering = "buffering";
2297const char* SCH_IO_KICAD_LEGACY::PropNoDocFile = "no_doc_file";
int color
Definition: DXF_plotter.cpp:63
const char * name
Definition: DXF_plotter.cpp:62
constexpr EDA_IU_SCALE schIUScale
Definition: base_units.h:114
void SetPageCount(int aPageCount)
Definition: base_screen.cpp:63
int GetPageCount() const
Definition: base_screen.h:72
int GetVirtualPageNumber() const
Definition: base_screen.h:75
void SetVirtualPageNumber(int aPageNumber)
Definition: base_screen.h:76
void SetContentModified(bool aModified=true)
Definition: base_screen.h:59
This class handle bitmap images in KiCad.
Definition: bitmap_base.h:49
int GetPPI() const
Definition: bitmap_base.h:118
wxImage * GetImageData()
Definition: bitmap_base.h:68
bool IsHorizontal() const
Definition: eda_angle.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
virtual void SetParent(EDA_ITEM *aParent)
Definition: eda_item.h:113
bool IsItalic() const
Definition: eda_text.h:166
const EDA_ANGLE & GetTextAngle() const
Definition: eda_text.h:144
void SetTextSize(VECTOR2I aNewSize, bool aEnforceMinTextSize=true)
Definition: eda_text.cpp:533
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
void SetTextPos(const VECTOR2I &aPoint)
Definition: eda_text.cpp:578
int GetTextWidth() const
Definition: eda_text.h:261
void SetVertJustify(GR_TEXT_V_ALIGN_T aType)
Definition: eda_text.cpp:417
GR_TEXT_H_ALIGN_T GetHorizJustify() const
Definition: eda_text.h:197
void SetBoldFlag(bool aBold)
Set only the bold flag, without changing the font.
Definition: eda_text.cpp:378
virtual void SetVisible(bool aVisible)
Definition: eda_text.cpp:386
void SetItalicFlag(bool aItalic)
Set only the italic flag, without changing the font.
Definition: eda_text.cpp:327
bool IsBold() const
Definition: eda_text.h:181
GR_TEXT_V_ALIGN_T GetVertJustify() const
Definition: eda_text.h:200
virtual void SetTextAngle(const EDA_ANGLE &aAngle)
Definition: eda_text.cpp:299
int GetTextThickness() const
Definition: eda_text.h:125
VECTOR2I GetTextSize() const
Definition: eda_text.h:258
void SetHorizJustify(GR_TEXT_H_ALIGN_T aType)
Definition: eda_text.cpp:409
EE_TYPE OfType(KICAD_T aType) const
Definition: sch_rtree.h:241
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
Used for text file output.
Definition: richio.h:491
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
wxString ToCSSString() const
Definition: color4d.cpp:147
Definition: kiid.h:49
wxString AsLegacyTimestampString() const
Definition: kiid.cpp:258
timestamp_t AsLegacyTimestamp() const
Definition: kiid.cpp:221
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:49
int Parse(const UTF8 &aId, bool aFix=false)
Parse LIB_ID with the information from aId.
Definition: lib_id.cpp:52
int SetLibItemName(const UTF8 &aLibItemName)
Override the library item name portion of the LIB_ID to aLibItemName.
Definition: lib_id.cpp:111
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
virtual char * ReadLine()=0
Read a line of text into the buffer and increments the line number counter.
virtual const wxString & GetSource() const
Returns the name of the source of the lines in an abstract sense.
Definition: richio.h:121
virtual unsigned LineNumber() const
Return the line number of the last line read from this LINE_READER.
Definition: richio.h:147
char * Line() const
Return a pointer to the last line that was read in.
Definition: richio.h:129
An interface used to output 8 bit text in a convenient way.
Definition: richio.h:322
int PRINTF_FUNC_N Print(int nestLevel, const char *fmt,...)
Format and write text to the output stream.
Definition: richio.cpp:463
Describe the page size and margins of a paper page on which to eventually print or plot.
Definition: page_info.h:59
void SetPortrait(bool aIsPortrait)
Rotate the paper page 90 degrees.
Definition: page_info.cpp:189
static const wxChar Custom[]
"User" defined page type
Definition: page_info.h:82
void SetHeightMils(double aHeightInMils)
Definition: page_info.cpp:262
double GetHeightMils() const
Definition: page_info.h:141
const wxString & GetType() const
Definition: page_info.h:99
double GetWidthMils() const
Definition: page_info.h:136
bool IsCustom() const
Definition: page_info.cpp:183
bool IsPortrait() const
Definition: page_info.h:122
void SetWidthMils(double aWidthInMils)
Definition: page_info.cpp:248
bool SetType(const wxString &aStandardPageDescriptionName, bool aIsPortrait=false)
Set the name of the page type and also the sizes and margins commonly associated with that type name.
Definition: page_info.cpp:122
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 void SetCurrentProgress(double aProgress)=0
Set the progress value to aProgress (0..1).
virtual const wxString GetProjectPath() const
Return the full path of the project.
Definition: project.cpp:149
A REFERENCE_IMAGE is a wrapper around a BITMAP_IMAGE that is displayed in an editor as a reference fo...
bool ReadImageFile(const wxString &aFullFilename)
Read and store an image file.
const BITMAP_BASE & GetImage() const
Get the underlying image.
double GetImageScale() const
void SetImageScale(double aScale)
Set the image "zoom" value.
Holds all the data relating to one schematic.
Definition: schematic.h:88
PROJECT & Project() const
Return a reference to the project this schematic is part of.
Definition: schematic.h:103
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
VECTOR2I GetPosition() const override
Definition: sch_bitmap.cpp:103
REFERENCE_IMAGE & GetReferenceImage()
Definition: sch_bitmap.h:51
Base class for a bus or wire entry.
Definition: sch_bus_entry.h:38
VECTOR2I GetPosition() const override
VECTOR2I GetEnd() const
bool IsMandatory() const
Definition: sch_field.cpp:1359
FIELD_T GetId() const
Definition: sch_field.h:116
VECTOR2I GetLibPosition() const
Definition: sch_field.h:260
wxString GetName(bool aUseDefaultName=true) const
Return the field name (not translated).
Definition: sch_field.cpp:1103
void SetName(const wxString &aName)
Definition: sch_field.cpp:1079
void SetText(const wxString &aText) override
Definition: sch_field.cpp:1089
A cache assistant for KiCad legacy symbol libraries.
static LIB_SYMBOL * LoadPart(LINE_READER &aReader, int aMajorVersion, int aMinorVersion, LIB_SYMBOL_MAP *aMap=nullptr)
void Save(const std::optional< bool > &aOpt) override
Save the entire library to file m_libFileName;.
void DeleteSymbol(const wxString &aName) override
static void SaveSymbol(LIB_SYMBOL *aSymbol, OUTPUTFORMATTER &aFormatter, LIB_SYMBOL_MAP *aMap=nullptr)
wxString m_error
For throwing exceptions or errors on partial schematic loads.
SCH_SHEET * m_currentSheet
The sheet currently being loaded.
void loadFile(const wxString &aFileName, SCH_SCREEN *aScreen)
void saveBusAlias(std::shared_ptr< BUS_ALIAS > aAlias)
void SaveSchematicFile(const wxString &aFileName, SCH_SHEET *aScreen, 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 saveText(SCH_TEXT *aText)
void saveJunction(SCH_JUNCTION *aJunction)
void loadPageSettings(LINE_READER &aReader, SCH_SCREEN *aScreen)
OUTPUTFORMATTER * m_out
The formatter for saving SCH_SCREEN objects.
void saveField(SCH_FIELD *aField, int aLegacyId)
bool isBuffering(const std::map< std::string, UTF8 > *aProperties)
void Format(SCH_SHEET *aSheet)
void loadHierarchy(SCH_SHEET *aSheet)
void saveBusEntry(SCH_BUS_ENTRY_BASE *aBusEntry)
SCH_SYMBOL * loadSymbol(LINE_READER &aReader)
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...
std::stack< wxString > m_currentPath
Stack to maintain nested sheet paths.
int GetModifyHash() const override
Return the modification hash from the library cache.
wxString m_path
Root project path for loading child sheets.
void LoadContent(LINE_READER &aReader, SCH_SCREEN *aScreen, int version=EESCHEMA_VERSION)
void loadHeader(LINE_READER &aReader, SCH_SCREEN *aScreen)
SCH_TEXT * loadText(LINE_READER &aReader)
void init(SCHEMATIC *aSchematic, const std::map< std::string, UTF8 > *aProperties=nullptr)
initialize PLUGIN like a constructor would.
static const char * PropBuffering
The property used internally by the plugin to enable cache buffering which prevents the library file ...
static void FormatPart(LIB_SYMBOL *aSymbol, OUTPUTFORMATTER &aFormatter)
SCH_NO_CONNECT * loadNoConnect(LINE_READER &aReader)
int m_version
Version of file being loaded.
bool CanReadSchematicFile(const wxString &aFileName) const override
Checks if this SCH_IO can read the specified schematic file.
void saveLine(SCH_LINE *aLine)
SCH_BUS_ENTRY_BASE * loadBusEntry(LINE_READER &aReader)
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,...
void saveSymbol(SCH_SYMBOL *aSymbol)
SCH_SHEET * loadSheet(LINE_READER &aReader)
SCH_LINE * loadWire(LINE_READER &aReader)
void saveNoConnect(SCH_NO_CONNECT *aNoConnect)
unsigned m_lineCount
for progress reporting
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 SaveLibrary(const wxString &aLibraryPath, const std::map< std::string, UTF8 > *aProperties=nullptr) override
std::shared_ptr< BUS_ALIAS > loadBusAlias(LINE_READER &aReader, SCH_SCREEN *aScreen)
SCH_IO_KICAD_LEGACY_LIB_CACHE * m_cache
SCH_BITMAP * loadBitmap(LINE_READER &aReader)
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.
static LIB_SYMBOL * ParsePart(LINE_READER &aReader, int majorVersion=0, int minorVersion=0)
void cacheLib(const wxString &aLibraryFileName, const std::map< std::string, UTF8 > *aProperties)
SCH_SHEET * m_rootSheet
The root sheet of the schematic being loaded.
static const char * PropNoDocFile
The property used internally by the plugin to disable writing the library documentation (....
SCH_JUNCTION * loadJunction(LINE_READER &aReader)
void saveBitmap(const SCH_BITMAP &aBitmap)
void saveSheet(SCH_SHEET *aSheet)
bool IsLibraryWritable(const wxString &aLibraryPath) override
Return true if the library at aLibraryPath is writable.
void CreateLibrary(const wxString &aLibraryPath, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Create a new empty library at aLibraryPath empty.
bool CanReadLibrary(const wxString &aFileName) const override
Checks if this IO object can read the specified library file/directory.
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 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.
bool writeDocFile(const std::map< std::string, UTF8 > *aProperties)
LINE_READER * m_lineReader
for progress reporting
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
virtual bool CanReadSchematicFile(const wxString &aFileName) const
Checks if this SCH_IO can read the specified schematic file.
Definition: sch_io.cpp:45
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
int GetUnit() const
Definition: sch_item.h:239
SCH_LAYER_ID GetLayer() const
Return the layer this item is on.
Definition: sch_item.h:313
VECTOR2I GetPosition() const override
Definition: sch_junction.h:107
SPIN_STYLE GetSpinStyle() const
Definition: sch_label.cpp:351
void SetShape(LABEL_FLAG_SHAPE aShape)
Definition: sch_label.h:177
LABEL_FLAG_SHAPE GetShape() const
Definition: sch_label.h:176
virtual void SetSpinStyle(SPIN_STYLE aSpinStyle)
Definition: sch_label.cpp:316
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
bool IsGraphicLine() const
Return if the line is a graphic (non electrical line)
Definition: sch_line.cpp:943
VECTOR2I GetPosition() const override
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
void SetTitleBlock(const TITLE_BLOCK &aTitleBlock)
Definition: sch_screen.h:165
void Append(SCH_ITEM *aItem, bool aUpdateLibSymbol=true)
Definition: sch_screen.cpp:160
void AddBusAlias(std::shared_ptr< BUS_ALIAS > aAlias)
Add a bus alias definition (and transfers ownership of the pointer).
void SetPageSettings(const PAGE_INFO &aPageSettings)
Definition: sch_screen.h:140
EE_RTREE & Items()
Get the full RTree, usually for iterating.
Definition: sch_screen.h:117
const wxString & GetFileName() const
Definition: sch_screen.h:152
const KIID & GetUuid() const
Definition: sch_screen.h:539
void SetFileName(const wxString &aFileName)
Set the file name for this screen to aFileName.
Definition: sch_screen.cpp:123
const TITLE_BLOCK & GetTitleBlock() const
Definition: sch_screen.h:163
void SetFileReadOnly(bool aIsReadOnly)
Definition: sch_screen.h:154
void SetFileExists(bool aFileExists)
Definition: sch_screen.h:157
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
wxString GetFileName() const
Return the filename corresponding to this sheet.
Definition: sch_sheet.h:321
SCH_FIELD * GetField(FIELD_T aFieldType)
Return a mandatory field in this sheet.
Definition: sch_sheet.cpp:367
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
std::vector< SCH_SHEET_PIN * > & GetPins()
Definition: sch_sheet.h:187
Schematic symbol object.
Definition: sch_symbol.h:75
const std::vector< SCH_SYMBOL_INSTANCE > & GetInstances() const
Definition: sch_symbol.h:134
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
wxString GetPrefix() const
Definition: sch_symbol.h:244
SCH_FIELD * GetField(FIELD_T aFieldType)
Return a mandatory field in this symbol.
Definition: sch_symbol.cpp:760
VECTOR2I GetPosition() const override
Definition: sch_text.h:141
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:105
Simple container to manage line stroke parameters.
Definition: stroke_params.h:94
int GetWidth() const
LINE_STYLE GetLineStyle() const
KIGFX::COLOR4D GetColor() const
static wxString GetLineStyleToken(LINE_STYLE aStyle)
static const char * PropPowerSymsOnly
const TRANSFORM & GetTransform() const
Definition: symbol.h:197
Hold the information shown in the lower right corner of a plot, printout, or editing view.
Definition: title_block.h:41
const wxString & GetCompany() const
Definition: title_block.h:96
void SetRevision(const wxString &aRevision)
Definition: title_block.h:81
void SetComment(int aIdx, const wxString &aComment)
Definition: title_block.h:101
const wxString & GetRevision() const
Definition: title_block.h:86
void SetTitle(const wxString &aTitle)
Definition: title_block.h:58
const wxString & GetDate() const
Definition: title_block.h:76
const wxString & GetComment(int aIdx) const
Definition: title_block.h:107
void SetCompany(const wxString &aCompany)
Definition: title_block.h:91
const wxString & GetTitle() const
Definition: title_block.h:63
void SetDate(const wxString &aDate)
Set the date field, and defaults to the current time and date.
Definition: title_block.h:71
for transforming drawing coordinates for a wxDC device context.
Definition: transform.h:46
int x2
Definition: transform.h:50
int y1
Definition: transform.h:49
int y2
Definition: transform.h:51
int x1
Definition: transform.h:48
#define _(s)
static constexpr EDA_ANGLE ANGLE_VERTICAL
Definition: eda_angle.h:408
static constexpr EDA_ANGLE ANGLE_HORIZONTAL
Definition: eda_angle.h:407
#define MAX_UNIT_COUNT_PER_PACKAGE
The maximum number of units per package.
Definition: eeschema_id.h:36
#define SCHEMATIC_HEAD_STRING
Definition: general.h:36
#define EESCHEMA_VERSION
Definition: general.h:35
int GetPenSizeForBold(int aTextSize)
Definition: gr_text.cpp:36
const wxChar *const traceSchLegacyPlugin
Flag to enable legacy schematic plugin debug output.
#define THROW_IO_ERROR(msg)
Definition: ki_exception.h:39
#define THROW_PARSE_ERROR(aProblem, aSource, aInputLine, aLineNumber, aByteIndex)
Definition: ki_exception.h:165
#define SCH_LAYER_ID_COUNT
Definition: layer_ids.h:496
SCH_LAYER_ID
Eeschema drawing layers.
Definition: layer_ids.h:439
@ LAYER_HIERLABEL
Definition: layer_ids.h:447
@ LAYER_GLOBLABEL
Definition: layer_ids.h:446
@ LAYER_WIRE
Definition: layer_ids.h:442
@ LAYER_NOTES
Definition: layer_ids.h:457
@ LAYER_BUS
Definition: layer_ids.h:443
@ LAYER_LOCLABEL
Definition: layer_ids.h:445
bool fileStartsWithPrefix(const wxString &aFilePath, const wxString &aPrefix, bool aIgnoreWhitespace)
Check if a file starts with a defined string.
Definition: io_utils.cpp:34
const std::map< LABEL_FLAG_SHAPE, const char * > sheetLabelNames
#define T_COLORA
#define T_WIDTH
#define T_COLOR
#define T_STYLE
int parseInt(LINE_READER &aReader, const char *aLine, const char **aOutput)
Parse an ASCII integer string with possible leading whitespace into an integer and updates the pointe...
bool strCompare(const char *aString, const char *aLine, const char **aOutput)
Compare aString to the string starting at aLine and advances the character point to the end of String...
void parseQuotedString(wxString &aString, LINE_READER &aReader, const char *aCurrentToken, const char **aNextToken, bool aCanBeEmpty)
Parse an quoted ASCII utf8 and updates the pointer at aOutput if it is not NULL.
void parseUnquotedString(wxString &aString, LINE_READER &aReader, const char *aCurrentToken, const char **aNextToken, bool aCanBeEmpty)
Parse an unquoted utf8 string and updates the pointer at aOutput if it is not NULL.
uint32_t parseHex(LINE_READER &aReader, const char *aLine, const char **aOutput)
Parse an ASCII hex integer string with possible leading whitespace into a long integer and updates th...
bool is_eol(char c)
char parseChar(LINE_READER &aReader, const char *aCurrentToken, const char **aNextToken)
Parse a single ASCII character and updates the pointer at aOutput if it is not NULL.
double parseDouble(LINE_READER &aReader, const char *aLine, const char **aOutput)
Parses an ASCII point string with possible leading whitespace into a double precision floating point ...
#define SCH_PARSE_ERROR(text, reader, pos)
@ AUTOPLACE_AUTO
Definition: sch_item.h:71
@ L_BIDI
Definition: sch_label.h:102
@ L_TRISTATE
Definition: sch_label.h:103
@ L_UNSPECIFIED
Definition: sch_label.h:104
@ L_OUTPUT
Definition: sch_label.h:101
@ L_INPUT
Definition: sch_label.h:100
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
wxString ConvertToNewOverbarNotation(const wxString &aOldStr)
Convert the old ~...~ overbar notation to the new ~{...} one.
wxString From_UTF8(const char *cstring)
std::string EscapedUTF8(const wxString &aString)
Return an 8 bit UTF8 string given aString in Unicode form.
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: string_utils.h:429
constexpr int IUToMils(int iu) const
Definition: base_units.h:103
constexpr int MilsToIU(int mils) const
Definition: base_units.h:97
A simple container for schematic symbol instance information.
Definition for symbol library class.
std::map< wxString, LIB_SYMBOL *, LibSymbolMapSort > LIB_SYMBOL_MAP
wxString GetUserFieldName(int aFieldNdx, bool aTranslateForHI)
#define DO_TRANSLATE
wxString GetCanonicalFieldName(FIELD_T aFieldType)
VECTOR2I end
@ GR_TEXT_H_ALIGN_RIGHT
@ GR_TEXT_H_ALIGN_LEFT
@ GR_TEXT_V_ALIGN_BOTTOM
@ GR_TEXT_V_ALIGN_TOP
wxLogTrace helper definitions.
KICAD_T
The set of class identification values stored in EDA_ITEM::m_structType.
Definition: typeinfo.h:78
@ SCH_LINE_T
Definition: typeinfo.h:164
@ SCH_NO_CONNECT_T
Definition: typeinfo.h:161
@ TYPE_NOT_INIT
Definition: typeinfo.h:81
@ SCH_SYMBOL_T
Definition: typeinfo.h:173
@ SCH_LABEL_T
Definition: typeinfo.h:168
@ SCH_SHEET_T
Definition: typeinfo.h:176
@ 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_GLOBAL_LABEL_T
Definition: typeinfo.h:169
@ SCH_JUNCTION_T
Definition: typeinfo.h:160
VECTOR2< int32_t > VECTOR2I
Definition: vector2d.h:695
Definition of file extensions used in Kicad.