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 (C) 2016-2023 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 <kiway.h>
37#include <string_utils.h>
38#include <locale_io.h>
39#include <richio.h>
40#include <trace_helpers.h>
41#include <trigo.h>
42#include <progress_reporter.h>
43#include <general.h>
44#include <gr_text.h>
45#include <sch_bitmap.h>
46#include <sch_bus_entry.h>
47#include <sch_symbol.h>
48#include <sch_junction.h>
49#include <sch_line.h>
50#include <sch_marker.h>
51#include <sch_no_connect.h>
52#include <sch_text.h>
53#include <sch_sheet.h>
54#include <sch_sheet_pin.h>
55#include <bus_alias.h>
56#include <io/io_utils.h>
60#include <sch_screen.h>
61#include <schematic.h>
62#include <symbol_library.h>
63#include <eeschema_id.h> // for MAX_UNIT_COUNT_PER_PACKAGE definition
64#include <tool/selection.h>
66
67
68// Tokens to read/save graphic lines style
69#define T_STYLE "style"
70#define T_COLOR "rgb" // cannot be modified (used by wxWidgets)
71#define T_COLORA "rgba" // cannot be modified (used by wxWidgets)
72#define T_WIDTH "width"
73
74
75SCH_IO_KICAD_LEGACY::SCH_IO_KICAD_LEGACY() : SCH_IO( wxS( "Eeschema legacy" ) ),
76 m_appending( false ),
77 m_lineReader( nullptr ),
78 m_lastProgressLine( 0 ),
79 m_lineCount( 0 )
80{
81 init( nullptr );
82}
83
84
86{
87 delete m_cache;
88}
89
90
91void SCH_IO_KICAD_LEGACY::init( SCHEMATIC* aSchematic, const STRING_UTF8_MAP* aProperties )
92{
93 m_version = 0;
94 m_rootSheet = nullptr;
95 m_currentSheet = nullptr;
96 m_schematic = aSchematic;
97 m_cache = nullptr;
98 m_out = nullptr;
99}
100
101
103{
104 const unsigned PROGRESS_DELTA = 250;
105
107 {
108 unsigned curLine = m_lineReader->LineNumber();
109
110 if( curLine > m_lastProgressLine + PROGRESS_DELTA )
111 {
112 m_progressReporter->SetCurrentProgress( ( (double) curLine )
113 / std::max( 1U, m_lineCount ) );
114
116 THROW_IO_ERROR( _( "Open cancelled by user." ) );
117
118 m_lastProgressLine = curLine;
119 }
120 }
121}
122
123
124SCH_SHEET* SCH_IO_KICAD_LEGACY::LoadSchematicFile( const wxString& aFileName, SCHEMATIC* aSchematic,
125 SCH_SHEET* aAppendToMe,
126 const STRING_UTF8_MAP* aProperties )
127{
128 wxASSERT( !aFileName || aSchematic != nullptr );
129
130 LOCALE_IO toggle; // toggles on, then off, the C locale.
131 SCH_SHEET* sheet;
132
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->Prj().GetProjectPath();
155
156 wxLogTrace( traceSchLegacyPlugin, "Normalized append path \"%s\".", m_path );
157 }
158 else
159 {
160 m_path = aSchematic->Prj().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 cancelled 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 fieldId = parseInt( aReader, line, &line );
573
574 if( fieldId == 0 || fieldId == 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->GetFields()[ fieldId ];
580 field.SetText( text );
581 field.SetTextSize( VECTOR2I( size, size ) );
582 }
583 else // Sheet pin.
584 {
585 // Use a unique_ptr so that we clean up in the case of a throw
586 std::unique_ptr<SCH_SHEET_PIN> sheetPin = std::make_unique<SCH_SHEET_PIN>( sheet.get() );
587
588 sheetPin->SetNumber( fieldId );
589
590 // Can be empty fields.
591 parseQuotedString( text, aReader, line, &line, true );
592
593 sheetPin->SetText( ConvertToNewOverbarNotation( text ) );
594
595 if( line == nullptr )
596 THROW_IO_ERROR( _( "unexpected end of line" ) );
597
598 switch( parseChar( aReader, line, &line ) )
599 {
600 case 'I': sheetPin->SetShape( LABEL_FLAG_SHAPE::L_INPUT ); break;
601 case 'O': sheetPin->SetShape( LABEL_FLAG_SHAPE::L_OUTPUT ); break;
602 case 'B': sheetPin->SetShape( LABEL_FLAG_SHAPE::L_BIDI ); break;
603 case 'T': sheetPin->SetShape( LABEL_FLAG_SHAPE::L_TRISTATE ); break;
604 case 'U': sheetPin->SetShape( LABEL_FLAG_SHAPE::L_UNSPECIFIED ); break;
605 default: SCH_PARSE_ERROR( "invalid sheet pin type", aReader, line );
606 }
607
608 switch( parseChar( aReader, line, &line ) )
609 {
610 case 'R': sheetPin->SetSide( SHEET_SIDE::RIGHT ); break;
611 case 'T': sheetPin->SetSide( SHEET_SIDE::TOP ); break;
612 case 'B': sheetPin->SetSide( SHEET_SIDE::BOTTOM ); break;
613 case 'L': sheetPin->SetSide( SHEET_SIDE::LEFT ); break;
614 default:
615 SCH_PARSE_ERROR( "invalid sheet pin side", aReader, line );
616 }
617
618 VECTOR2I position;
619
620 position.x = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
621 position.y = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
622 sheetPin->SetPosition( position );
623
624 size = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
625
626 sheetPin->SetTextSize( VECTOR2I( size, size ) );
627
628 sheet->AddPin( sheetPin.release() );
629 }
630 }
631 else if( strCompare( "$EndSheet", line ) )
632 {
633 sheet->AutoplaceFields( /* aScreen */ nullptr, /* aManual */ false );
634 return sheet.release();
635 }
636
637 line = aReader.ReadLine();
638 }
639
640 SCH_PARSE_ERROR( "missing '$EndSheet`", aReader, line );
641
642 return nullptr; // Prevents compiler warning. Should never get here.
643}
644
645
647{
648 std::unique_ptr<SCH_BITMAP> bitmap = std::make_unique<SCH_BITMAP>();
649
650 const char* line = aReader.Line();
651
652 wxCHECK( strCompare( "$Bitmap", line, &line ), nullptr );
653
654 line = aReader.ReadLine();
655
656 while( line != nullptr )
657 {
658 if( strCompare( "Pos", line, &line ) )
659 {
660 VECTOR2I position;
661
662 position.x = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
663 position.y = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
664 bitmap->SetPosition( position );
665 }
666 else if( strCompare( "Scale", line, &line ) )
667 {
668 auto scalefactor = parseDouble( aReader, line, &line );
669
670 // Prevent scalefactor values that cannot be displayed.
671 // In the case of a bad value, we accept that the image might be mis-scaled
672 // rather than removing the full image. Users can then edit the scale factor in
673 // Eeschema to the appropriate value
674 if( !std::isnormal( scalefactor ) )
675 scalefactor = 1.0;
676
677 bitmap->GetImage()->SetScale( scalefactor );
678 }
679 else if( strCompare( "Data", line, &line ) )
680 {
681 wxMemoryBuffer buffer;
682
683 while( line )
684 {
685 if( !aReader.ReadLine() )
686 SCH_PARSE_ERROR( _( "Unexpected end of file" ), aReader, line );
687
688 line = aReader.Line();
689
690 if( strCompare( "EndData", line ) )
691 {
692 // all the PNG date is read.
693 bitmap->GetImage()->ReadImageFile( buffer );
694
695 // Legacy file formats assumed 300 image PPI at load.
696 BITMAP_BASE* bitmapImage = bitmap->GetImage();
697 bitmapImage->SetScale( bitmapImage->GetScale() * bitmapImage->GetPPI()
698 / 300.0 );
699 break;
700 }
701
702 // Read PNG data, stored in hexadecimal,
703 // each byte = 2 hexadecimal digits and a space between 2 bytes
704 // and put it in memory stream buffer
705 // Note:
706 // Some old files created bu the V4 schematic versions have a extra
707 // "$EndBitmap" at the end of the hexadecimal data. (Probably due to
708 // a bug). So discard it
709 int len = strlen( line );
710
711 for( ; len > 0 && !isspace( *line ) && '$' != *line; len -= 3, line += 3 )
712 {
713 int value = 0;
714
715 if( sscanf( line, "%X", &value ) == 1 )
716 buffer.AppendByte( (char) value );
717 else
718 THROW_IO_ERROR( "invalid PNG data" );
719 }
720 }
721
722 if( line == nullptr )
723 THROW_IO_ERROR( _( "unexpected end of file" ) );
724 }
725 else if( strCompare( "$EndBitmap", line ) )
726 {
727 return bitmap.release();
728 }
729
730 line = aReader.ReadLine();
731 }
732
733 THROW_IO_ERROR( _( "unexpected end of file" ) );
734}
735
736
738{
739 std::unique_ptr<SCH_JUNCTION> junction = std::make_unique<SCH_JUNCTION>();
740
741 const char* line = aReader.Line();
742
743 wxCHECK( strCompare( "Connection", line, &line ), nullptr );
744
745 wxString name;
746
747 parseUnquotedString( name, aReader, line, &line );
748
749 VECTOR2I position;
750
751 position.x = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
752 position.y = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
753 junction->SetPosition( position );
754
755 return junction.release();
756}
757
758
760{
761 std::unique_ptr<SCH_NO_CONNECT> no_connect = std::make_unique<SCH_NO_CONNECT>();
762
763 const char* line = aReader.Line();
764
765 wxCHECK( strCompare( "NoConn", line, &line ), nullptr );
766
767 wxString name;
768
769 parseUnquotedString( name, aReader, line, &line );
770
771 VECTOR2I position;
772
773 position.x = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
774 position.y = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
775 no_connect->SetPosition( position );
776
777 return no_connect.release();
778}
779
780
782{
783 std::unique_ptr<SCH_LINE> wire = std::make_unique<SCH_LINE>();
784
785 const char* line = aReader.Line();
786
787 wxCHECK( strCompare( "Wire", line, &line ), nullptr );
788
789 if( strCompare( "Wire", line, &line ) )
790 wire->SetLayer( LAYER_WIRE );
791 else if( strCompare( "Bus", line, &line ) )
792 wire->SetLayer( LAYER_BUS );
793 else if( strCompare( "Notes", line, &line ) )
794 wire->SetLayer( LAYER_NOTES );
795 else
796 SCH_PARSE_ERROR( "invalid line type", aReader, line );
797
798 if( !strCompare( "Line", line, &line ) )
799 SCH_PARSE_ERROR( "invalid wire definition", aReader, line );
800
801 // The default graphical line style was Dashed.
802 if( wire->GetLayer() == LAYER_NOTES )
803 wire->SetLineStyle( LINE_STYLE::DASH );
804
805 // Since Sept 15, 2017, a line style is alloved (width, style, color)
806 // Only non default values are stored
807 while( !is_eol( *line ) )
808 {
809 wxString buf;
810
811 parseUnquotedString( buf, aReader, line, &line );
812
813 if( buf == ")" )
814 continue;
815
816 else if( buf == T_WIDTH )
817 {
818 int size = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
819 wire->SetLineWidth( size );
820 }
821 else if( buf == T_STYLE )
822 {
823 parseUnquotedString( buf, aReader, line, &line );
824
825 if( buf == wxT( "solid" ) )
826 wire->SetLineStyle( LINE_STYLE::SOLID );
827 else if( buf == wxT( "dashed" ) )
828 wire->SetLineStyle( LINE_STYLE::DASH );
829 else if( buf == wxT( "dash_dot" ) )
830 wire->SetLineStyle( LINE_STYLE::DASHDOT );
831 else if( buf == wxT( "dotted" ) )
832 wire->SetLineStyle( LINE_STYLE::DOT );
833 }
834 else // should be the color parameter.
835 {
836 // The color param is something like rgb(150, 40, 191)
837 // and because there is no space between ( and 150
838 // the first param is inside buf.
839 // So break keyword and the first param into 2 separate strings.
840 wxString prm, keyword;
841 keyword = buf.BeforeLast( '(', &prm );
842
843 if( ( keyword == T_COLOR ) || ( keyword == T_COLORA ) )
844 {
845 long color[4] = { 0 };
846
847 int ii = 0;
848
849 if( !prm.IsEmpty() )
850 {
851 prm.ToLong( &color[ii] );
852 ii++;
853 }
854
855 int prm_count = ( keyword == T_COLORA ) ? 4 : 3;
856
857 // fix opacity to 1.0 or 255, when not exists in file
858 color[3] = 255;
859
860 for(; ii < prm_count && !is_eol( *line ); ii++ )
861 {
862 color[ii] = parseInt( aReader, line, &line );
863
864 // Skip the separator between values
865 if( *line == ',' || *line == ' ')
866 line++;
867 }
868
869 wire->SetLineColor( color[0]/255.0, color[1]/255.0, color[2]/255.0,color[3]/255.0 );
870 }
871 }
872 }
873
874 // Read the segment en points coordinates:
875 line = aReader.ReadLine();
876
877 VECTOR2I begin, end;
878
879 begin.x = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
880 begin.y = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
881 end.x = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
882 end.y = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
883
884 wire->SetStartPoint( begin );
885 wire->SetEndPoint( end );
886
887 return wire.release();
888}
889
890
892{
893 const char* line = aReader.Line();
894
895 wxCHECK( strCompare( "Entry", line, &line ), nullptr );
896
897 std::unique_ptr<SCH_BUS_ENTRY_BASE> busEntry;
898
899 if( strCompare( "Wire", line, &line ) )
900 {
901 busEntry = std::make_unique<SCH_BUS_WIRE_ENTRY>();
902
903 if( !strCompare( "Line", line, &line ) )
904 SCH_PARSE_ERROR( "invalid bus entry definition expected 'Line'", aReader, line );
905 }
906 else if( strCompare( "Bus", line, &line ) )
907 {
908 busEntry = std::make_unique<SCH_BUS_BUS_ENTRY>();
909
910 if( !strCompare( "Bus", line, &line ) )
911 SCH_PARSE_ERROR( "invalid bus entry definition expected 'Bus'", aReader, line );
912 }
913 else
914 {
915 SCH_PARSE_ERROR( "invalid bus entry type", aReader, line );
916 }
917
918 line = aReader.ReadLine();
919
920 VECTOR2I pos;
921 VECTOR2I size;
922
923 pos.x = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
924 pos.y = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
925 size.x = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
926 size.y = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
927
928 size.x -= pos.x;
929 size.y -= pos.y;
930
931 busEntry->SetPosition( pos );
932 busEntry->SetSize( size );
933
934 return busEntry.release();
935}
936
937
938// clang-format off
939const std::map<LABEL_FLAG_SHAPE, const char*> sheetLabelNames
940{
941 { LABEL_FLAG_SHAPE::L_INPUT, "Input" },
942 { LABEL_FLAG_SHAPE::L_OUTPUT, "Output" },
943 { LABEL_FLAG_SHAPE::L_BIDI, "BiDi" },
944 { LABEL_FLAG_SHAPE::L_TRISTATE, "3State" },
946};
947// clang-format on
948
949
951{
952 const char* line = aReader.Line();
953 KICAD_T textType = TYPE_NOT_INIT;
954
955 wxCHECK( strCompare( "Text", line, &line ), nullptr );
956
957 if( strCompare( "Notes", line, &line ) )
958 {
959 textType = SCH_TEXT_T;
960 }
961 else if( strCompare( "Label", line, &line ) )
962 {
963 textType = SCH_LABEL_T;
964 }
965 else if( strCompare( "HLabel", line, &line ) )
966 {
967 textType = SCH_HIER_LABEL_T;
968 }
969 else if( strCompare( "GLabel", line, &line ) )
970 {
971 // Prior to version 2, the SCH_GLOBALLABEL object did not exist.
972 if( m_version == 1 )
973 textType = SCH_HIER_LABEL_T;
974 else
975 textType = SCH_GLOBAL_LABEL_T;
976 }
977 else
978 {
979 SCH_PARSE_ERROR( "unknown Text type", aReader, line );
980 }
981
982 VECTOR2I position;
983
984 position.x = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
985 position.y = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
986
987 std::unique_ptr<SCH_TEXT> text;
988
989 switch( textType )
990 {
991 case SCH_TEXT_T: text.reset( new SCH_TEXT( position ) ); break;
992 case SCH_LABEL_T: text.reset( new SCH_LABEL( position ) ); break;
993 case SCH_HIER_LABEL_T: text.reset( new SCH_HIERLABEL( position ) ); break;
994 case SCH_GLOBAL_LABEL_T: text.reset( new SCH_GLOBALLABEL( position ) ); break;
995 default: break;
996 }
997
998 int spinStyle = parseInt( aReader, line, &line );
999
1000 // Sadly we store the orientation of hierarchical and global labels using a different
1001 // int encoding than that for local labels:
1002 // Global Local
1003 // Left justified 0 2
1004 // Up 1 3
1005 // Right justified 2 0
1006 // Down 3 1
1007 // So we must flip it as the enum is setup with the "global" numbering
1008 if( textType != SCH_GLOBAL_LABEL_T && textType != SCH_HIER_LABEL_T )
1009 {
1010 if( spinStyle == 0 )
1011 spinStyle = 2;
1012 else if( spinStyle == 2 )
1013 spinStyle = 0;
1014 }
1015
1016 int size = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
1017
1018 text->SetTextSize( VECTOR2I( size, size ) );
1019
1020 if( textType == SCH_LABEL_T || textType == SCH_HIER_LABEL_T || textType == SCH_GLOBAL_LABEL_T )
1021 {
1022 SCH_LABEL_BASE* label = static_cast<SCH_LABEL_BASE*>( text.get() );
1023
1024 label->SetSpinStyle( static_cast<SPIN_STYLE::SPIN>( spinStyle ) );
1025
1026 // Parse the global and hierarchical label type.
1027 if( textType == SCH_HIER_LABEL_T || textType == SCH_GLOBAL_LABEL_T )
1028 {
1029 auto resultIt = std::find_if( sheetLabelNames.begin(), sheetLabelNames.end(),
1030 [ &line ]( const auto& it )
1031 {
1032 return strCompare( it.second, line, &line );
1033 } );
1034
1035 if( resultIt != sheetLabelNames.end() )
1036 label->SetShape( resultIt->first );
1037 else
1038 SCH_PARSE_ERROR( "invalid label type", aReader, line );
1039 }
1040 }
1041 else if( textType == SCH_TEXT_T )
1042 {
1043 switch( spinStyle )
1044 {
1045 case SPIN_STYLE::RIGHT: // Horiz Normal Orientation
1046 text->SetTextAngle( ANGLE_HORIZONTAL );
1047 text->SetHorizJustify( GR_TEXT_H_ALIGN_LEFT );
1048 break;
1049
1050 case SPIN_STYLE::UP: // Vert Orientation UP
1051 text->SetTextAngle( ANGLE_VERTICAL );
1052 text->SetHorizJustify( GR_TEXT_H_ALIGN_LEFT );
1053 break;
1054
1055 case SPIN_STYLE::LEFT: // Horiz Orientation - Right justified
1056 text->SetTextAngle( ANGLE_HORIZONTAL );
1057 text->SetHorizJustify( GR_TEXT_H_ALIGN_RIGHT );
1058 break;
1059
1060 case SPIN_STYLE::BOTTOM: // Vert Orientation BOTTOM
1061 text->SetTextAngle( ANGLE_VERTICAL );
1062 text->SetHorizJustify( GR_TEXT_H_ALIGN_RIGHT );
1063 break;
1064 }
1065
1066 text->SetVertJustify( GR_TEXT_V_ALIGN_BOTTOM );
1067 }
1068
1069 int penWidth = 0;
1070
1071 // The following tokens do not exist in version 1 schematic files,
1072 // and not always in version 2 for HLabels and GLabels
1073 if( m_version > 1 )
1074 {
1075 if( m_version > 2 || *line >= ' ' )
1076 {
1077 if( strCompare( "Italic", line, &line ) )
1078 text->SetItalic( true );
1079 else if( !strCompare( "~", line, &line ) )
1080 SCH_PARSE_ERROR( _( "expected 'Italics' or '~'" ), aReader, line );
1081 }
1082
1083 // The penWidth token does not exist in older versions of the schematic file format
1084 // so calling parseInt will be made only if the EOL is not reached.
1085 if( *line >= ' ' )
1086 penWidth = parseInt( aReader, line, &line );
1087 }
1088
1089 text->SetBoldFlag( penWidth != 0 );
1090 text->SetTextThickness( penWidth != 0 ? GetPenSizeForBold( size ) : 0 );
1091
1092 // Read the text string for the text.
1093 char* tmp = aReader.ReadLine();
1094
1095 tmp = strtok( tmp, "\r\n" );
1096 wxString val = From_UTF8( tmp );
1097
1098 for( ; ; )
1099 {
1100 int i = val.find( wxT( "\\n" ) );
1101
1102 if( i == wxNOT_FOUND )
1103 break;
1104
1105 val.erase( i, 2 );
1106 val.insert( i, wxT( "\n" ) );
1107 }
1108
1109 text->SetText( ConvertToNewOverbarNotation( val ) );
1110
1111 return text.release();
1112}
1113
1114
1116{
1117 const char* line = aReader.Line();
1118
1119 wxCHECK( strCompare( "$Comp", line, &line ), nullptr );
1120
1121 std::unique_ptr<SCH_SYMBOL> symbol = std::make_unique<SCH_SYMBOL>();
1122
1123 line = aReader.ReadLine();
1124
1125 while( line != nullptr )
1126 {
1127 if( strCompare( "L", line, &line ) )
1128 {
1129 wxString libName;
1130 size_t pos = 2; // "X" plus ' ' space character.
1131 wxString utf8Line = wxString::FromUTF8( line );
1132 wxStringTokenizer tokens( utf8Line, " \r\n\t" );
1133
1134 if( tokens.CountTokens() < 2 )
1135 THROW_PARSE_ERROR( "invalid symbol library definition", aReader.GetSource(),
1136 aReader.Line(), aReader.LineNumber(), pos );
1137
1138 libName = tokens.GetNextToken();
1139 libName.Replace( "~", " " );
1140
1141 LIB_ID libId;
1142
1143 // Prior to schematic version 4, library IDs did not have a library nickname so
1144 // parsing the symbol name with LIB_ID::Parse() would break symbol library links
1145 // that contained '/' and ':' characters.
1146 if( m_version > 3 )
1147 libId.Parse( libName, true );
1148 else
1149 libId.SetLibItemName( libName );
1150
1151 symbol->SetLibId( libId );
1152
1153 wxString refDesignator = tokens.GetNextToken();
1154
1155 refDesignator.Replace( "~", " " );
1156
1157 wxString prefix = refDesignator;
1158
1159 while( prefix.Length() )
1160 {
1161 if( ( prefix.Last() < '0' || prefix.Last() > '9') && prefix.Last() != '?' )
1162 break;
1163
1164 prefix.RemoveLast();
1165 }
1166
1167 // Avoid a prefix containing trailing/leading spaces
1168 prefix.Trim( true );
1169 prefix.Trim( false );
1170
1171 if( prefix.IsEmpty() )
1172 symbol->SetPrefix( wxString( "U" ) );
1173 else
1174 symbol->SetPrefix( prefix );
1175 }
1176 else if( strCompare( "U", line, &line ) )
1177 {
1178 // This fixes a potentially buggy files caused by unit being set to zero which
1179 // causes netlist issues. See https://bugs.launchpad.net/kicad/+bug/1677282.
1180 int unit = parseInt( aReader, line, &line );
1181
1182 if( unit == 0 )
1183 {
1184 unit = 1;
1185
1186 // Set the file as modified so the user can be warned.
1187 if( m_rootSheet->GetScreen() )
1189 }
1190
1191 symbol->SetUnit( unit );
1192
1193 // Same can also happen with the body style ("convert") parameter
1194 int bodyStyle = parseInt( aReader, line, &line );
1195
1196 if( bodyStyle == 0 )
1197 {
1198 bodyStyle = 1;
1199
1200 // Set the file as modified so the user can be warned.
1201 if( m_rootSheet->GetScreen() )
1203 }
1204
1205 symbol->SetBodyStyle( bodyStyle );
1206
1207 wxString text;
1208 parseUnquotedString( text, aReader, line, &line );
1209
1210 if( text != "00000000" )
1211 const_cast<KIID&>( symbol->m_Uuid ) = KIID( text );
1212 }
1213 else if( strCompare( "P", line, &line ) )
1214 {
1215 VECTOR2I pos;
1216
1217 pos.x = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
1218 pos.y = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
1219 symbol->SetPosition( pos );
1220 }
1221 else if( strCompare( "AR", line, &line ) )
1222 {
1223 const char* strCompare = "Path=";
1224 int len = strlen( strCompare );
1225
1226 if( strncasecmp( strCompare, line, len ) != 0 )
1227 SCH_PARSE_ERROR( "missing 'Path=' token", aReader, line );
1228
1229 line += len;
1230 wxString pathStr, reference, unit;
1231
1232 parseQuotedString( pathStr, aReader, line, &line );
1233
1234 // Note: AR path excludes root sheet, but includes symbol. Drop the symbol ID
1235 // since it's already defined in the symbol itself.
1236 KIID_PATH path( pathStr );
1237
1238 if( path.size() > 0 )
1239 path.pop_back();
1240
1241 // In the new file format, the root schematic UUID is used as the virtual SCH_SHEET
1242 // UUID so we need to prefix it to the symbol path so the symbol instance paths
1243 // get saved with the root schematic UUID.
1244 if( !m_appending )
1245 path.insert( path.begin(), m_rootSheet->GetScreen()->GetUuid() );
1246
1247 strCompare = "Ref=";
1248 len = strlen( strCompare );
1249
1250 if( strncasecmp( strCompare, line, len ) != 0 )
1251 SCH_PARSE_ERROR( "missing 'Ref=' token", aReader, line );
1252
1253 line+= len;
1254 parseQuotedString( reference, aReader, line, &line );
1255
1256 strCompare = "Part=";
1257 len = strlen( strCompare );
1258
1259 if( strncasecmp( strCompare, line, len ) != 0 )
1260 SCH_PARSE_ERROR( "missing 'Part=' token", aReader, line );
1261
1262 line+= len;
1263 parseQuotedString( unit, aReader, line, &line );
1264
1265 long tmp;
1266
1267 if( !unit.ToLong( &tmp, 10 ) )
1268 SCH_PARSE_ERROR( "expected integer value", aReader, line );
1269
1270 if( tmp < 0 || tmp > MAX_UNIT_COUNT_PER_PACKAGE )
1271 SCH_PARSE_ERROR( "unit value out of range", aReader, line );
1272
1273 symbol->AddHierarchicalReference( path, reference, (int)tmp );
1274 symbol->GetField( REFERENCE_FIELD )->SetText( reference );
1275 }
1276 else if( strCompare( "F", line, &line ) )
1277 {
1278 int index = parseInt( aReader, line, &line );
1279
1280 wxString text, name;
1281
1282 parseQuotedString( text, aReader, line, &line, true );
1283
1284 char orientation = parseChar( aReader, line, &line );
1285 VECTOR2I pos;
1286 pos.x = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
1287 pos.y = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
1288 int size = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
1289 int attributes = parseHex( aReader, line, &line );
1290
1291 // Description was not mandatory until v8.0, so any fields with an index
1292 // past this point should be user fields
1293 if( index > DATASHEET_FIELD )
1294 {
1295 // The first MANDATOR_FIELDS _must_ be constructed within the SCH_SYMBOL
1296 // constructor. This assert is simply here to guard against a change in that
1297 // constructor.
1298 wxASSERT( symbol->GetFieldCount() >= MANDATORY_FIELDS );
1299
1300 // We need to check for an existing field by name that happens to have the same
1301 // name and index as any field that was made mandatory after this point, e.g. Description
1302 // is now mandatory but a user could have easily added their own user field named Description
1303 // after Datasheet before it was mandatory, and we don't want to add a second Description field.
1304 SCH_FIELD* existingField = symbol->GetFieldByName( name );
1305
1306 if( !existingField )
1307 {
1308 // Ignore the _supplied_ fieldNdx. It is not important anymore if within the
1309 // user defined fields region (i.e. >= MANDATORY_FIELDS).
1310 // We freely renumber the index to fit the next available field slot.
1311 index = symbol->GetFieldCount(); // new has this index after insertion
1312
1313 SCH_FIELD field( VECTOR2I( 0, 0 ), index, symbol.get(), name );
1314 symbol->AddField( field );
1315 }
1316 else
1317 index = existingField->GetId();
1318 }
1319
1320 SCH_FIELD& field = symbol->GetFields()[index];
1321
1322 // Prior to version 2 of the schematic file format, none of the following existed.
1323 if( m_version > 1 )
1324 {
1325 wxString textAttrs;
1326 char hjustify = parseChar( aReader, line, &line );
1327
1328 parseUnquotedString( textAttrs, aReader, line, &line );
1329
1330 // The name of the field is optional.
1331 parseQuotedString( name, aReader, line, &line, true );
1332
1333 if( hjustify == 'L' )
1335 else if( hjustify == 'R' )
1337 else if( hjustify != 'C' )
1338 SCH_PARSE_ERROR( "symbol field text horizontal justification must be "
1339 "L, R, or C", aReader, line );
1340
1341 // We are guaranteed to have a least one character here for older file formats
1342 // otherwise an exception would have been raised..
1343 if( textAttrs[0] == 'T' )
1345 else if( textAttrs[0] == 'B' )
1347 else if( textAttrs[0] != 'C' )
1348 SCH_PARSE_ERROR( "symbol field text vertical justification must be "
1349 "B, T, or C", aReader, line );
1350
1351 // Newer file formats include the bold and italics text attribute.
1352 if( textAttrs.Length() > 1 )
1353 {
1354 if( textAttrs.Length() != 3 )
1355 {
1356 SCH_PARSE_ERROR( _( "symbol field text attributes must be 3 characters wide" ),
1357 aReader, line );
1358 }
1359
1360 if( textAttrs[1] == 'I' )
1361 {
1362 field.SetItalic( true );
1363 }
1364 else if( textAttrs[1] != 'N' )
1365 {
1366 SCH_PARSE_ERROR( "symbol field text italics indicator must be I or N",
1367 aReader, line );
1368 }
1369
1370 if( textAttrs[2] == 'B' )
1371 {
1372 field.SetBoldFlag( true );
1373 }
1374 else if( textAttrs[2] != 'N' )
1375 {
1376 SCH_PARSE_ERROR( "symbol field text bold indicator must be B or N",
1377 aReader, line );
1378 }
1379 }
1380 }
1381
1382 field.SetText( text );
1383 field.SetTextPos( pos );
1384 field.SetVisible( !attributes );
1385 field.SetTextSize( VECTOR2I( size, size ) );
1386
1387 if( orientation == 'H' )
1389 else if( orientation == 'V' )
1391 else
1392 SCH_PARSE_ERROR( "symbol field orientation must be H or V", aReader, line );
1393
1394 if( name.IsEmpty() )
1396
1397 field.SetName( name );
1398 }
1399 else if( strCompare( "$EndComp", line ) )
1400 {
1401 if( !m_appending )
1402 {
1404 {
1406 path.push_back( m_rootSheet->GetScreen()->GetUuid() );
1407
1408 SCH_SYMBOL_INSTANCE instance;
1409 instance.m_Path = path;
1410 instance.m_Reference = symbol->GetField( REFERENCE_FIELD )->GetText();
1411 instance.m_Unit = symbol->GetUnit();
1412 symbol->AddHierarchicalReference( instance );
1413 }
1414 else
1415 {
1416 for( const SCH_SYMBOL_INSTANCE& instance : symbol->GetInstances() )
1417 {
1418 SCH_SYMBOL_INSTANCE tmpInstance = instance;
1419 symbol->AddHierarchicalReference( tmpInstance );
1420 }
1421 }
1422 }
1423
1424 // Ensure all flags (some are set by previous initializations) are reset:
1425 symbol->ClearFlags();
1426 return symbol.release();
1427 }
1428 else
1429 {
1430 // There are two lines that begin with a tab or spaces that includes a line with the
1431 // redundant position information and the transform matrix settings.
1432
1433 // Parse the redundant position information just the same to check for formatting
1434 // errors.
1435 parseInt( aReader, line, &line ); // Always 1.
1436 parseInt( aReader, line, &line ); // The X coordinate.
1437 parseInt( aReader, line, &line ); // The Y coordinate.
1438
1439 line = aReader.ReadLine();
1440
1441 TRANSFORM transform;
1442
1443 transform.x1 = parseInt( aReader, line, &line );
1444
1445 if( transform.x1 < -1 || transform.x1 > 1 )
1446 SCH_PARSE_ERROR( "invalid symbol X1 transform value", aReader, line );
1447
1448 transform.y1 = parseInt( aReader, line, &line );
1449
1450 if( transform.y1 < -1 || transform.y1 > 1 )
1451 SCH_PARSE_ERROR( "invalid symbol Y1 transform value", aReader, line );
1452
1453 transform.x2 = parseInt( aReader, line, &line );
1454
1455 if( transform.x2 < -1 || transform.x2 > 1 )
1456 SCH_PARSE_ERROR( "invalid symbol X2 transform value", aReader, line );
1457
1458 transform.y2 = parseInt( aReader, line, &line );
1459
1460 if( transform.y2 < -1 || transform.y2 > 1 )
1461 SCH_PARSE_ERROR( "invalid symbol Y2 transform value", aReader, line );
1462
1463 symbol->SetTransform( transform );
1464 }
1465
1466 line = aReader.ReadLine();
1467 }
1468
1469 SCH_PARSE_ERROR( "invalid symbol line", aReader, line );
1470
1471 return nullptr; // Prevents compiler warning. Should never get here.
1472}
1473
1474
1475std::shared_ptr<BUS_ALIAS> SCH_IO_KICAD_LEGACY::loadBusAlias( LINE_READER& aReader,
1476 SCH_SCREEN* aScreen )
1477{
1478 auto busAlias = std::make_shared<BUS_ALIAS>( aScreen );
1479 const char* line = aReader.Line();
1480
1481 wxCHECK( strCompare( "BusAlias", line, &line ), nullptr );
1482
1483 wxString buf;
1484 parseUnquotedString( buf, aReader, line, &line );
1485 busAlias->SetName( buf );
1486
1487 while( *line != '\0' )
1488 {
1489 buf.clear();
1490 parseUnquotedString( buf, aReader, line, &line, true );
1491
1492 if( !buf.IsEmpty() )
1493 busAlias->Members().emplace_back( buf );
1494 }
1495
1496 return busAlias;
1497}
1498
1499
1500void SCH_IO_KICAD_LEGACY::SaveSchematicFile( const wxString& aFileName, SCH_SHEET* aSheet,
1501 SCHEMATIC* aSchematic,
1502 const STRING_UTF8_MAP* aProperties )
1503{
1504 wxCHECK_RET( aSheet != nullptr, "NULL SCH_SHEET object." );
1505 wxCHECK_RET( !aFileName.IsEmpty(), "No schematic file name defined." );
1506
1507 LOCALE_IO toggle; // toggles on, then off, the C locale, to write floating point values.
1508
1509 init( aSchematic, aProperties );
1510
1511 wxFileName fn = aFileName;
1512
1513 // File names should be absolute. Don't assume everything relative to the project path
1514 // works properly.
1515 wxASSERT( fn.IsAbsolute() );
1516
1517 FILE_OUTPUTFORMATTER formatter( fn.GetFullPath() );
1518
1519 m_out = &formatter; // no ownership
1520
1521 Format( aSheet );
1522
1523 aSheet->GetScreen()->SetFileExists( true );
1524}
1525
1526
1528{
1529 wxCHECK_RET( aSheet != nullptr, "NULL SCH_SHEET* object." );
1530 wxCHECK_RET( m_schematic != nullptr, "NULL SCHEMATIC* object." );
1531
1532 SCH_SCREEN* screen = aSheet->GetScreen();
1533
1534 wxCHECK( screen, /* void */ );
1535
1536 // Write the header
1537 m_out->Print( 0, "%s %s %d\n", "EESchema", SCHEMATIC_HEAD_STRING, EESCHEMA_VERSION );
1538
1539 // This section is not used, but written for file compatibility
1540 m_out->Print( 0, "EELAYER %d %d\n", SCH_LAYER_ID_COUNT, 0 );
1541 m_out->Print( 0, "EELAYER END\n" );
1542
1543 /* Write page info, ScreenNumber and NumberOfScreen; not very meaningful for
1544 * SheetNumber and Sheet Count in a complex hierarchy, but useful in
1545 * simple hierarchy and flat hierarchy. Used also to search the root
1546 * sheet ( ScreenNumber = 1 ) within the files
1547 */
1548 const TITLE_BLOCK& tb = screen->GetTitleBlock();
1549 const PAGE_INFO& page = screen->GetPageSettings();
1550
1551 m_out->Print( 0, "$Descr %s %d %d%s\n", TO_UTF8( page.GetType() ),
1552 (int)page.GetWidthMils(),
1553 (int)page.GetHeightMils(),
1554 !page.IsCustom() && page.IsPortrait() ? " portrait" : "" );
1555 m_out->Print( 0, "encoding utf-8\n" );
1556 m_out->Print( 0, "Sheet %d %d\n", screen->GetVirtualPageNumber(), screen->GetPageCount() );
1557 m_out->Print( 0, "Title %s\n", EscapedUTF8( tb.GetTitle() ).c_str() );
1558 m_out->Print( 0, "Date %s\n", EscapedUTF8( tb.GetDate() ).c_str() );
1559 m_out->Print( 0, "Rev %s\n", EscapedUTF8( tb.GetRevision() ).c_str() );
1560 m_out->Print( 0, "Comp %s\n", EscapedUTF8( tb.GetCompany() ).c_str() );
1561 m_out->Print( 0, "Comment1 %s\n", EscapedUTF8( tb.GetComment( 0 ) ).c_str() );
1562 m_out->Print( 0, "Comment2 %s\n", EscapedUTF8( tb.GetComment( 1 ) ).c_str() );
1563 m_out->Print( 0, "Comment3 %s\n", EscapedUTF8( tb.GetComment( 2 ) ).c_str() );
1564 m_out->Print( 0, "Comment4 %s\n", EscapedUTF8( tb.GetComment( 3 ) ).c_str() );
1565 m_out->Print( 0, "Comment5 %s\n", EscapedUTF8( tb.GetComment( 4 ) ).c_str() );
1566 m_out->Print( 0, "Comment6 %s\n", EscapedUTF8( tb.GetComment( 5 ) ).c_str() );
1567 m_out->Print( 0, "Comment7 %s\n", EscapedUTF8( tb.GetComment( 6 ) ).c_str() );
1568 m_out->Print( 0, "Comment8 %s\n", EscapedUTF8( tb.GetComment( 7 ) ).c_str() );
1569 m_out->Print( 0, "Comment9 %s\n", EscapedUTF8( tb.GetComment( 8 ) ).c_str() );
1570 m_out->Print( 0, "$EndDescr\n" );
1571
1572 for( const std::shared_ptr<BUS_ALIAS>& alias : screen->GetBusAliases() )
1573 saveBusAlias( alias );
1574
1575 // Enforce item ordering
1576 auto cmp = []( const SCH_ITEM* a, const SCH_ITEM* b ) { return *a < *b; };
1577 std::multiset<SCH_ITEM*, decltype( cmp )> save_map( cmp );
1578
1579 for( SCH_ITEM* item : screen->Items() )
1580 save_map.insert( item );
1581
1582
1583 for( auto& item : save_map )
1584 {
1585 switch( item->Type() )
1586 {
1587 case SCH_SYMBOL_T:
1588 saveSymbol( static_cast<SCH_SYMBOL*>( item ) );
1589 break;
1590 case SCH_BITMAP_T:
1591 saveBitmap( static_cast<SCH_BITMAP*>( item ) );
1592 break;
1593 case SCH_SHEET_T:
1594 saveSheet( static_cast<SCH_SHEET*>( item ) );
1595 break;
1596 case SCH_JUNCTION_T:
1597 saveJunction( static_cast<SCH_JUNCTION*>( item ) );
1598 break;
1599 case SCH_NO_CONNECT_T:
1600 saveNoConnect( static_cast<SCH_NO_CONNECT*>( item ) );
1601 break;
1604 saveBusEntry( static_cast<SCH_BUS_ENTRY_BASE*>( item ) );
1605 break;
1606 case SCH_LINE_T:
1607 saveLine( static_cast<SCH_LINE*>( item ) );
1608 break;
1609 case SCH_TEXT_T:
1610 case SCH_LABEL_T:
1611 case SCH_GLOBAL_LABEL_T:
1612 case SCH_HIER_LABEL_T:
1613 saveText( static_cast<SCH_TEXT*>( item ) );
1614 break;
1615 default:
1616 wxASSERT( "Unexpected schematic object type in SCH_IO_KICAD_LEGACY::Format()" );
1617 }
1618 }
1619
1620 m_out->Print( 0, "$EndSCHEMATC\n" );
1621}
1622
1623
1625{
1626 m_out = aFormatter;
1627
1628 for( unsigned i = 0; i < aSelection->GetSize(); ++i )
1629 {
1630 SCH_ITEM* item = (SCH_ITEM*) aSelection->GetItem( i );
1631
1632 switch( item->Type() )
1633 {
1634 case SCH_SYMBOL_T:
1635 saveSymbol( static_cast< SCH_SYMBOL* >( item ) );
1636 break;
1637 case SCH_BITMAP_T:
1638 saveBitmap( static_cast< SCH_BITMAP* >( item ) );
1639 break;
1640 case SCH_SHEET_T:
1641 saveSheet( static_cast< SCH_SHEET* >( item ) );
1642 break;
1643 case SCH_JUNCTION_T:
1644 saveJunction( static_cast< SCH_JUNCTION* >( item ) );
1645 break;
1646 case SCH_NO_CONNECT_T:
1647 saveNoConnect( static_cast< SCH_NO_CONNECT* >( item ) );
1648 break;
1651 saveBusEntry( static_cast< SCH_BUS_ENTRY_BASE* >( item ) );
1652 break;
1653 case SCH_LINE_T:
1654 saveLine( static_cast< SCH_LINE* >( item ) );
1655 break;
1656 case SCH_TEXT_T:
1657 case SCH_LABEL_T:
1658 case SCH_GLOBAL_LABEL_T:
1659 case SCH_HIER_LABEL_T:
1660 saveText( static_cast< SCH_TEXT* >( item ) );
1661 break;
1662 default:
1663 wxASSERT( "Unexpected schematic object type in SCH_IO_KICAD_LEGACY::Format()" );
1664 }
1665 }
1666}
1667
1668
1670{
1671 std::string name1;
1672 std::string name2;
1673
1674 static wxString delimiters( wxT( " " ) );
1675
1676 // This is redundant with the AR entries below, but it makes the files backwards-compatible.
1677 if( aSymbol->GetInstances().size() > 0 )
1678 {
1679 const SCH_SYMBOL_INSTANCE& instance = aSymbol->GetInstances()[0];
1680 name1 = toUTFTildaText( instance.m_Reference );
1681 }
1682 else
1683 {
1684 if( aSymbol->GetField( REFERENCE_FIELD )->GetText().IsEmpty() )
1685 name1 = toUTFTildaText( aSymbol->GetPrefix() );
1686 else
1687 name1 = toUTFTildaText( aSymbol->GetField( REFERENCE_FIELD )->GetText() );
1688 }
1689
1690 wxString symbol_name = aSymbol->GetLibId().Format();
1691
1692 if( symbol_name.size() )
1693 {
1694 name2 = toUTFTildaText( symbol_name );
1695 }
1696 else
1697 {
1698 name2 = "_NONAME_";
1699 }
1700
1701 m_out->Print( 0, "$Comp\n" );
1702 m_out->Print( 0, "L %s %s\n", name2.c_str(), name1.c_str() );
1703
1704 // Generate unit number, conversion and timestamp
1705 m_out->Print( 0, "U %d %d %8.8X\n",
1706 aSymbol->GetUnit(),
1707 aSymbol->GetBodyStyle(),
1708 aSymbol->m_Uuid.AsLegacyTimestamp() );
1709
1710 // Save the position
1711 m_out->Print( 0, "P %d %d\n",
1712 schIUScale.IUToMils( aSymbol->GetPosition().x ),
1713 schIUScale.IUToMils( aSymbol->GetPosition().y ) );
1714
1715 /* If this is a complex hierarchy; save hierarchical references.
1716 * but for simple hierarchies it is not necessary.
1717 * the reference inf is already saved
1718 * this is useful for old Eeschema version compatibility
1719 */
1720 if( aSymbol->GetInstances().size() > 1 )
1721 {
1722 for( const SCH_SYMBOL_INSTANCE& instance : aSymbol->GetInstances() )
1723 {
1724 /*format:
1725 * AR Path="/140/2" Ref="C99" Part="1"
1726 * where 140 is the uid of the containing sheet and 2 is the timestamp of this symbol.
1727 * (timestamps are actually 8 hex chars)
1728 * Ref is the conventional symbol reference designator for this 'path'
1729 * Part is the conventional symbol unit selection for this 'path'
1730 */
1731 wxString path = "/";
1732
1733 // Skip root sheet
1734 for( int i = 1; i < (int) instance.m_Path.size(); ++i )
1735 path += instance.m_Path[i].AsLegacyTimestampString() + "/";
1736
1737 m_out->Print( 0, "AR Path=\"%s\" Ref=\"%s\" Part=\"%d\" \n",
1739 TO_UTF8( instance.m_Reference ),
1740 instance.m_Unit );
1741 }
1742 }
1743
1744 // update the ugly field id, which I would like to see go away someday soon.
1745 for( int i = 0; i < aSymbol->GetFieldCount(); ++i )
1746 aSymbol->GetFields()[i].SetId( i );
1747
1748 // Fixed fields:
1749 // Save mandatory fields even if they are blank,
1750 // because the visibility, size and orientation are set from library editor.
1751 for( unsigned i = 0; i < MANDATORY_FIELDS; ++i )
1752 saveField( &aSymbol->GetFields()[i] );
1753
1754 // User defined fields:
1755 // The *policy* about which user defined fields are symbol of a symbol is now
1756 // only in the dialog editors. No policy should be enforced here, simply
1757 // save all the user defined fields, they are present because a dialog editor
1758 // thought they should be. If you disagree, go fix the dialog editors.
1759 for( int i = MANDATORY_FIELDS; i < aSymbol->GetFieldCount(); ++i )
1760 saveField( &aSymbol->GetFields()[i] );
1761
1762 // Unit number, position, box ( old standard )
1763 m_out->Print( 0, "\t%-4d %-4d %-4d\n", aSymbol->GetUnit(),
1764 schIUScale.IUToMils( aSymbol->GetPosition().x ),
1765 schIUScale.IUToMils( aSymbol->GetPosition().y ) );
1766
1767 TRANSFORM transform = aSymbol->GetTransform();
1768
1769 m_out->Print( 0, "\t%-4d %-4d %-4d %-4d\n",
1770 transform.x1, transform.y1, transform.x2, transform.y2 );
1771 m_out->Print( 0, "$EndComp\n" );
1772}
1773
1774
1776{
1777 char hjustify = 'C';
1778
1779 if( aField->GetHorizJustify() == GR_TEXT_H_ALIGN_LEFT )
1780 hjustify = 'L';
1781 else if( aField->GetHorizJustify() == GR_TEXT_H_ALIGN_RIGHT )
1782 hjustify = 'R';
1783
1784 char vjustify = 'C';
1785
1786 if( aField->GetVertJustify() == GR_TEXT_V_ALIGN_BOTTOM )
1787 vjustify = 'B';
1788 else if( aField->GetVertJustify() == GR_TEXT_V_ALIGN_TOP )
1789 vjustify = 'T';
1790
1791 m_out->Print( 0, "F %d %s %c %-3d %-3d %-3d %4.4X %c %c%c%c",
1792 aField->GetId(),
1793 EscapedUTF8( aField->GetText() ).c_str(), // wraps in quotes too
1794 aField->GetTextAngle().IsHorizontal() ? 'H' : 'V',
1795 schIUScale.IUToMils( aField->GetLibPosition().x ),
1796 schIUScale.IUToMils( aField->GetLibPosition().y ),
1797 schIUScale.IUToMils( aField->GetTextWidth() ),
1798 !aField->IsVisible(),
1799 hjustify, vjustify,
1800 aField->IsItalic() ? 'I' : 'N',
1801 aField->IsBold() ? 'B' : 'N' );
1802
1803 // Save field name, if the name is user definable
1804 if( aField->GetId() >= MANDATORY_FIELDS )
1805 m_out->Print( 0, " %s", EscapedUTF8( aField->GetName() ).c_str() );
1806
1807 m_out->Print( 0, "\n" );
1808}
1809
1810
1812{
1813 wxCHECK_RET( aBitmap != nullptr, "SCH_BITMAP* is NULL" );
1814
1815 const wxImage* image = aBitmap->GetImage()->GetImageData();
1816
1817 wxCHECK_RET( image != nullptr, "wxImage* is NULL" );
1818
1819 m_out->Print( 0, "$Bitmap\n" );
1820 m_out->Print( 0, "Pos %-4d %-4d\n",
1821 schIUScale.IUToMils( aBitmap->GetPosition().x ),
1822 schIUScale.IUToMils( aBitmap->GetPosition().y ) );
1823 m_out->Print( 0, "Scale %f\n", aBitmap->GetImage()->GetScale() );
1824 m_out->Print( 0, "Data\n" );
1825
1826 wxMemoryOutputStream stream;
1827
1828 image->SaveFile( stream, wxBITMAP_TYPE_PNG );
1829
1830 // Write binary data in hexadecimal form (ASCII)
1831 wxStreamBuffer* buffer = stream.GetOutputStreamBuffer();
1832 char* begin = (char*) buffer->GetBufferStart();
1833
1834 for( int ii = 0; begin < buffer->GetBufferEnd(); begin++, ii++ )
1835 {
1836 if( ii >= 32 )
1837 {
1838 ii = 0;
1839
1840 m_out->Print( 0, "\n" );
1841 }
1842
1843 m_out->Print( 0, "%2.2X ", *begin & 0xFF );
1844 }
1845
1846 m_out->Print( 0, "\nEndData\n" );
1847 m_out->Print( 0, "$EndBitmap\n" );
1848}
1849
1850
1852{
1853 wxCHECK_RET( aSheet != nullptr, "SCH_SHEET* is NULL" );
1854
1855 m_out->Print( 0, "$Sheet\n" );
1856 m_out->Print( 0, "S %-4d %-4d %-4d %-4d\n",
1857 schIUScale.IUToMils( aSheet->GetPosition().x ),
1858 schIUScale.IUToMils( aSheet->GetPosition().y ),
1859 schIUScale.IUToMils( aSheet->GetSize().x ),
1860 schIUScale.IUToMils( aSheet->GetSize().y ) );
1861
1862 m_out->Print( 0, "U %8.8X\n", aSheet->m_Uuid.AsLegacyTimestamp() );
1863
1864 SCH_FIELD& sheetName = aSheet->GetFields()[SHEETNAME];
1865 SCH_FIELD& fileName = aSheet->GetFields()[SHEETFILENAME];
1866
1867 if( !sheetName.GetText().IsEmpty() )
1868 {
1869 m_out->Print( 0, "F0 %s %d\n",
1870 EscapedUTF8( sheetName.GetText() ).c_str(),
1871 schIUScale.IUToMils( sheetName.GetTextSize().x ) );
1872 }
1873
1874 if( !fileName.GetText().IsEmpty() )
1875 {
1876 m_out->Print( 0, "F1 %s %d\n",
1877 EscapedUTF8( fileName.GetText() ).c_str(),
1878 schIUScale.IUToMils( fileName.GetTextSize().x ) );
1879 }
1880
1881 for( const SCH_SHEET_PIN* pin : aSheet->GetPins() )
1882 {
1883 int type, side;
1884
1885 if( pin->GetText().IsEmpty() )
1886 break;
1887
1888 switch( pin->GetSide() )
1889 {
1890 default:
1891 case SHEET_SIDE::LEFT: side = 'L'; break;
1892 case SHEET_SIDE::RIGHT: side = 'R'; break;
1893 case SHEET_SIDE::TOP: side = 'T'; break;
1894 case SHEET_SIDE::BOTTOM: side = 'B'; break;
1895 }
1896
1897 switch( pin->GetShape() )
1898 {
1899 default:
1900 case LABEL_FLAG_SHAPE::L_UNSPECIFIED: type = 'U'; break;
1901 case LABEL_FLAG_SHAPE::L_INPUT: type = 'I'; break;
1902 case LABEL_FLAG_SHAPE::L_OUTPUT: type = 'O'; break;
1903 case LABEL_FLAG_SHAPE::L_BIDI: type = 'B'; break;
1904 case LABEL_FLAG_SHAPE::L_TRISTATE: type = 'T'; break;
1905 }
1906
1907 m_out->Print( 0, "F%d %s %c %c %-3d %-3d %-3d\n",
1908 pin->GetNumber(),
1909 EscapedUTF8( pin->GetText() ).c_str(), // supplies wrapping quotes
1910 type, side, schIUScale.IUToMils( pin->GetPosition().x ),
1911 schIUScale.IUToMils( pin->GetPosition().y ),
1912 schIUScale.IUToMils( pin->GetTextWidth() ) );
1913 }
1914
1915 m_out->Print( 0, "$EndSheet\n" );
1916}
1917
1918
1920{
1921 wxCHECK_RET( aJunction != nullptr, "SCH_JUNCTION* is NULL" );
1922
1923 m_out->Print( 0, "Connection ~ %-4d %-4d\n",
1924 schIUScale.IUToMils( aJunction->GetPosition().x ),
1925 schIUScale.IUToMils( aJunction->GetPosition().y ) );
1926}
1927
1928
1930{
1931 wxCHECK_RET( aNoConnect != nullptr, "SCH_NOCONNECT* is NULL" );
1932
1933 m_out->Print( 0, "NoConn ~ %-4d %-4d\n",
1934 schIUScale.IUToMils( aNoConnect->GetPosition().x ),
1935 schIUScale.IUToMils( aNoConnect->GetPosition().y ) );
1936}
1937
1938
1940{
1941 wxCHECK_RET( aBusEntry != nullptr, "SCH_BUS_ENTRY_BASE* is NULL" );
1942
1943 if( aBusEntry->GetLayer() == LAYER_WIRE )
1944 {
1945 m_out->Print( 0, "Entry Wire Line\n\t%-4d %-4d %-4d %-4d\n",
1946 schIUScale.IUToMils( aBusEntry->GetPosition().x ),
1947 schIUScale.IUToMils( aBusEntry->GetPosition().y ),
1948 schIUScale.IUToMils( aBusEntry->GetEnd().x ),
1949 schIUScale.IUToMils( aBusEntry->GetEnd().y ) );
1950 }
1951 else
1952 {
1953 m_out->Print( 0, "Entry Bus Bus\n\t%-4d %-4d %-4d %-4d\n",
1954 schIUScale.IUToMils( aBusEntry->GetPosition().x ),
1955 schIUScale.IUToMils( aBusEntry->GetPosition().y ),
1956 schIUScale.IUToMils( aBusEntry->GetEnd().x ),
1957 schIUScale.IUToMils( aBusEntry->GetEnd().y ) );
1958 }
1959}
1960
1961
1963{
1964 wxCHECK_RET( aLine != nullptr, "SCH_LINE* is NULL" );
1965
1966 const char* layer = "Notes";
1967 const char* width = "Line";
1968
1969 if( aLine->GetLayer() == LAYER_WIRE )
1970 layer = "Wire";
1971 else if( aLine->GetLayer() == LAYER_BUS )
1972 layer = "Bus";
1973
1974 m_out->Print( 0, "Wire %s %s", layer, width );
1975
1976 // Write line style (width, type, color) only for non default values
1977 if( aLine->IsGraphicLine() )
1978 {
1979 if( aLine->GetLineWidth() != 0 )
1980 m_out->Print( 0, " %s %d", T_WIDTH, schIUScale.IUToMils( aLine->GetLineWidth() ) );
1981
1982 m_out->Print( 0, " %s %s", T_STYLE,
1984
1985 if( aLine->GetLineColor() != COLOR4D::UNSPECIFIED )
1986 {
1987 m_out->Print( 0, " %s",
1988 TO_UTF8( aLine->GetLineColor().ToCSSString() ) );
1989 }
1990 }
1991
1992 m_out->Print( 0, "\n" );
1993
1994 m_out->Print( 0, "\t%-4d %-4d %-4d %-4d",
1995 schIUScale.IUToMils( aLine->GetStartPoint().x ),
1996 schIUScale.IUToMils( aLine->GetStartPoint().y ),
1997 schIUScale.IUToMils( aLine->GetEndPoint().x ),
1998 schIUScale.IUToMils( aLine->GetEndPoint().y ) );
1999
2000 m_out->Print( 0, "\n");
2001}
2002
2003
2005{
2006 wxCHECK_RET( aText != nullptr, "SCH_TEXT* is NULL" );
2007
2008 const char* italics = "~";
2009 const char* textType = "Notes";
2010
2011 if( aText->IsItalic() )
2012 italics = "Italic";
2013
2014 wxString text = aText->GetText();
2015
2016 SCH_LAYER_ID layer = aText->GetLayer();
2017
2018 if( layer == LAYER_NOTES || layer == LAYER_LOCLABEL )
2019 {
2020 if( layer == LAYER_NOTES )
2021 {
2022 // For compatibility reasons, the text must be saved in only one text line
2023 // so replace all EOLs with \\n
2024 text.Replace( wxT( "\n" ), wxT( "\\n" ) );
2025
2026 // Here we should have no CR or LF character in line
2027 // This is not always the case if a multiline text was copied (using a copy/paste
2028 // function) from a text that uses E.O.L characters that differs from the current
2029 // EOL format. This is mainly the case under Linux using LF symbol when copying
2030 // a text from Windows (using CRLF symbol) so we must just remove the extra CR left
2031 // (or LF left under MacOSX)
2032 for( unsigned ii = 0; ii < text.Len(); )
2033 {
2034 if( text[ii] == 0x0A || text[ii] == 0x0D )
2035 text.erase( ii, 1 );
2036 else
2037 ii++;
2038 }
2039 }
2040 else
2041 {
2042 textType = "Label";
2043 }
2044
2045 int spinStyle = 0;
2046
2047 // Local labels must have their spin style inverted for left and right
2048 if( SCH_LABEL_BASE* label = dynamic_cast<SCH_LABEL_BASE*>( aText ) )
2049 {
2050 spinStyle = static_cast<int>( label->GetSpinStyle() );
2051
2052 if( spinStyle == 0 )
2053 spinStyle = 2;
2054 else if( spinStyle == 2 )
2055 spinStyle = 0;
2056 }
2057
2058 m_out->Print( 0, "Text %s %-4d %-4d %-4d %-4d %s %d\n%s\n", textType,
2059 schIUScale.IUToMils( aText->GetPosition().x ),
2060 schIUScale.IUToMils( aText->GetPosition().y ),
2061 spinStyle,
2062 schIUScale.IUToMils( aText->GetTextWidth() ),
2063 italics, schIUScale.IUToMils( aText->GetTextThickness() ), TO_UTF8( text ) );
2064 }
2065 else if( layer == LAYER_GLOBLABEL || layer == LAYER_HIERLABEL )
2066 {
2067 textType = ( layer == LAYER_GLOBLABEL ) ? "GLabel" : "HLabel";
2068
2069 SCH_LABEL_BASE* label = static_cast<SCH_LABEL_BASE*>( aText );
2070 auto shapeLabelIt = sheetLabelNames.find( label->GetShape() );
2071 wxCHECK_RET( shapeLabelIt != sheetLabelNames.end(), "Shape not found in names list" );
2072
2073 m_out->Print( 0, "Text %s %-4d %-4d %-4d %-4d %s %s %d\n%s\n", textType,
2074 schIUScale.IUToMils( aText->GetPosition().x ),
2075 schIUScale.IUToMils( aText->GetPosition().y ),
2076 static_cast<int>( label->GetSpinStyle() ),
2077 schIUScale.IUToMils( aText->GetTextWidth() ),
2078 shapeLabelIt->second,
2079 italics,
2081 }
2082}
2083
2084
2085void SCH_IO_KICAD_LEGACY::saveBusAlias( std::shared_ptr<BUS_ALIAS> aAlias )
2086{
2087 wxCHECK_RET( aAlias != nullptr, "BUS_ALIAS* is NULL" );
2088
2089 wxString members = boost::algorithm::join( aAlias->Members(), " " );
2090
2091 m_out->Print( 0, "BusAlias %s %s\n",
2092 TO_UTF8( aAlias->GetName() ), TO_UTF8( members ) );
2093}
2094
2095
2096void SCH_IO_KICAD_LEGACY::cacheLib( const wxString& aLibraryFileName,
2097 const STRING_UTF8_MAP* aProperties )
2098{
2099 if( !m_cache || !m_cache->IsFile( aLibraryFileName ) || m_cache->IsFileChanged() )
2100 {
2101 // a spectacular episode in memory management:
2102 delete m_cache;
2103 m_cache = new SCH_IO_KICAD_LEGACY_LIB_CACHE( aLibraryFileName );
2104
2105 if( !isBuffering( aProperties ) )
2106 m_cache->Load();
2107 }
2108}
2109
2110
2112{
2113 std::string propName( SCH_IO_KICAD_LEGACY::PropNoDocFile );
2114
2115 if( aProperties && aProperties->find( propName ) != aProperties->end() )
2116 return false;
2117
2118 return true;
2119}
2120
2121
2123{
2124 return ( aProperties && aProperties->Exists( SCH_IO_KICAD_LEGACY::PropBuffering ) );
2125}
2126
2127
2129{
2130 if( m_cache )
2131 return m_cache->GetModifyHash();
2132
2133 // If the cache hasn't been loaded, it hasn't been modified.
2134 return 0;
2135}
2136
2137
2138void SCH_IO_KICAD_LEGACY::EnumerateSymbolLib( wxArrayString& aSymbolNameList,
2139 const wxString& aLibraryPath,
2140 const STRING_UTF8_MAP* aProperties )
2141{
2142 LOCALE_IO toggle; // toggles on, then off, the C locale.
2143
2144 bool powerSymbolsOnly = ( aProperties &&
2145 aProperties->find( SYMBOL_LIB_TABLE::PropPowerSymsOnly ) != aProperties->end() );
2146
2147 cacheLib( aLibraryPath, aProperties );
2148
2149 const LIB_SYMBOL_MAP& symbols = m_cache->m_symbols;
2150
2151 for( LIB_SYMBOL_MAP::const_iterator it = symbols.begin(); it != symbols.end(); ++it )
2152 {
2153 if( !powerSymbolsOnly || it->second->IsPower() )
2154 aSymbolNameList.Add( it->first );
2155 }
2156}
2157
2158
2159void SCH_IO_KICAD_LEGACY::EnumerateSymbolLib( std::vector<LIB_SYMBOL*>& aSymbolList,
2160 const wxString& aLibraryPath,
2161 const STRING_UTF8_MAP* aProperties )
2162{
2163 LOCALE_IO toggle; // toggles on, then off, the C locale.
2164
2165 bool powerSymbolsOnly = ( aProperties &&
2166 aProperties->find( SYMBOL_LIB_TABLE::PropPowerSymsOnly ) != aProperties->end() );
2167
2168 cacheLib( aLibraryPath, aProperties );
2169
2170 const LIB_SYMBOL_MAP& symbols = m_cache->m_symbols;
2171
2172 for( LIB_SYMBOL_MAP::const_iterator it = symbols.begin(); it != symbols.end(); ++it )
2173 {
2174 if( !powerSymbolsOnly || it->second->IsPower() )
2175 aSymbolList.push_back( it->second );
2176 }
2177}
2178
2179
2180LIB_SYMBOL* SCH_IO_KICAD_LEGACY::LoadSymbol( const wxString& aLibraryPath,
2181 const wxString& aSymbolName,
2182 const STRING_UTF8_MAP* aProperties )
2183{
2184 LOCALE_IO toggle; // toggles on, then off, the C locale.
2185
2186 cacheLib( aLibraryPath, aProperties );
2187
2188 LIB_SYMBOL_MAP::const_iterator it = m_cache->m_symbols.find( aSymbolName );
2189
2190 if( it == m_cache->m_symbols.end() )
2191 return nullptr;
2192
2193 return it->second;
2194}
2195
2196
2197void SCH_IO_KICAD_LEGACY::SaveSymbol( const wxString& aLibraryPath, const LIB_SYMBOL* aSymbol,
2198 const STRING_UTF8_MAP* aProperties )
2199{
2200 LOCALE_IO toggle; // toggles on, then off, the C locale.
2201
2202 cacheLib( aLibraryPath, aProperties );
2203
2204 m_cache->AddSymbol( aSymbol );
2205
2206 if( !isBuffering( aProperties ) )
2207 m_cache->Save( writeDocFile( aProperties ) );
2208}
2209
2210
2211void SCH_IO_KICAD_LEGACY::DeleteSymbol( const wxString& aLibraryPath, const wxString& aSymbolName,
2212 const STRING_UTF8_MAP* aProperties )
2213{
2214 LOCALE_IO toggle; // toggles on, then off, the C locale.
2215
2216 cacheLib( aLibraryPath, aProperties );
2217
2218 m_cache->DeleteSymbol( aSymbolName );
2219
2220 if( !isBuffering( aProperties ) )
2221 m_cache->Save( writeDocFile( aProperties ) );
2222}
2223
2224
2225void SCH_IO_KICAD_LEGACY::CreateLibrary( const wxString& aLibraryPath,
2226 const STRING_UTF8_MAP* aProperties )
2227{
2228 if( wxFileExists( aLibraryPath ) )
2229 {
2230 THROW_IO_ERROR( wxString::Format( _( "Symbol library '%s' already exists." ),
2231 aLibraryPath.GetData() ) );
2232 }
2233
2234 LOCALE_IO toggle;
2235
2236 delete m_cache;
2237 m_cache = new SCH_IO_KICAD_LEGACY_LIB_CACHE( aLibraryPath );
2239 m_cache->Save( writeDocFile( aProperties ) );
2240 m_cache->Load(); // update m_writable and m_mod_time
2241}
2242
2243
2244bool SCH_IO_KICAD_LEGACY::DeleteLibrary( const wxString& aLibraryPath,
2245 const STRING_UTF8_MAP* aProperties )
2246{
2247 wxFileName fn = aLibraryPath;
2248
2249 if( !fn.FileExists() )
2250 return false;
2251
2252 // Some of the more elaborate wxRemoveFile() crap puts up its own wxLog dialog
2253 // we don't want that. we want bare metal portability with no UI here.
2254 if( wxRemove( aLibraryPath ) )
2255 {
2256 THROW_IO_ERROR( wxString::Format( _( "Symbol library '%s' cannot be deleted." ),
2257 aLibraryPath.GetData() ) );
2258 }
2259
2260 if( m_cache && m_cache->IsFile( aLibraryPath ) )
2261 {
2262 delete m_cache;
2263 m_cache = nullptr;
2264 }
2265
2266 return true;
2267}
2268
2269
2270void SCH_IO_KICAD_LEGACY::SaveLibrary( const wxString& aLibraryPath,
2271 const STRING_UTF8_MAP* aProperties )
2272{
2273 if( !m_cache )
2274 m_cache = new SCH_IO_KICAD_LEGACY_LIB_CACHE( aLibraryPath );
2275
2276 wxString oldFileName = m_cache->GetFileName();
2277
2278 if( !m_cache->IsFile( aLibraryPath ) )
2279 {
2280 m_cache->SetFileName( aLibraryPath );
2281 }
2282
2283 // This is a forced save.
2285 m_cache->Save( writeDocFile( aProperties ) );
2286 m_cache->SetFileName( oldFileName );
2287}
2288
2289
2290bool SCH_IO_KICAD_LEGACY::CanReadSchematicFile( const wxString& aFileName ) const
2291{
2292 if( !SCH_IO::CanReadSchematicFile( aFileName ) )
2293 return false;
2294
2295 return IO_UTILS::fileStartsWithPrefix( aFileName, wxT( "EESchema" ), true );
2296}
2297
2298
2299bool SCH_IO_KICAD_LEGACY::CanReadLibrary( const wxString& aFileName ) const
2300{
2301 if( !SCH_IO::CanReadLibrary( aFileName ) )
2302 return false;
2303
2304 return IO_UTILS::fileStartsWithPrefix( aFileName, wxT( "EESchema" ), true );
2305}
2306
2307
2308bool SCH_IO_KICAD_LEGACY::IsLibraryWritable( const wxString& aLibraryPath )
2309{
2310 // Writing legacy symbol libraries is deprecated.
2311 return false;
2312}
2313
2314
2316 int aMinorVersion )
2317{
2318 return SCH_IO_KICAD_LEGACY_LIB_CACHE::LoadPart( reader, aMajorVersion, aMinorVersion );
2319}
2320
2321
2323{
2324 SCH_IO_KICAD_LEGACY_LIB_CACHE::SaveSymbol( symbol, formatter );
2325}
2326
2327
2328
2329const char* SCH_IO_KICAD_LEGACY::PropBuffering = "buffering";
2330const char* SCH_IO_KICAD_LEGACY::PropNoDocFile = "no_doc_file";
int color
Definition: DXF_plotter.cpp:58
const char * name
Definition: DXF_plotter.cpp:57
constexpr EDA_IU_SCALE schIUScale
Definition: base_units.h:110
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:48
double GetScale() const
Definition: bitmap_base.h:72
int GetPPI() const
Definition: bitmap_base.h:117
wxImage * GetImageData()
Definition: bitmap_base.h:67
void SetScale(double aScale)
Definition: bitmap_base.h:73
bool IsHorizontal() const
Definition: eda_angle.h:180
const KIID m_Uuid
Definition: eda_item.h:485
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:100
virtual void SetParent(EDA_ITEM *aParent)
Definition: eda_item.h:103
bool IsItalic() const
Definition: eda_text.h:144
const EDA_ANGLE & GetTextAngle() const
Definition: eda_text.h:134
void SetTextSize(VECTOR2I aNewSize, bool aEnforceMinTextSize=true)
Definition: eda_text.cpp:372
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition: eda_text.h:98
virtual bool IsVisible() const
Definition: eda_text.h:151
void SetTextPos(const VECTOR2I &aPoint)
Definition: eda_text.cpp:417
int GetTextWidth() const
Definition: eda_text.h:225
void SetVertJustify(GR_TEXT_V_ALIGN_T aType)
Definition: eda_text.cpp:274
GR_TEXT_H_ALIGN_T GetHorizJustify() const
Definition: eda_text.h:164
void SetBoldFlag(bool aBold)
Definition: eda_text.cpp:235
virtual void SetVisible(bool aVisible)
Definition: eda_text.cpp:243
bool IsBold() const
Definition: eda_text.h:148
GR_TEXT_V_ALIGN_T GetVertJustify() const
Definition: eda_text.h:167
virtual void SetTextAngle(const EDA_ANGLE &aAngle)
Definition: eda_text.cpp:203
int GetTextThickness() const
Definition: eda_text.h:126
void SetItalic(bool aItalic)
Definition: eda_text.cpp:211
VECTOR2I GetTextSize() const
Definition: eda_text.h:222
void SetHorizJustify(GR_TEXT_H_ALIGN_T aType)
Definition: eda_text.cpp:266
EE_TYPE OfType(KICAD_T aType) const
Definition: sch_rtree.h:238
A LINE_READER that reads from an open file.
Definition: richio.h:185
void Rewind()
Rewind the file and resets the line number back to zero.
Definition: richio.h:234
char * ReadLine() override
Read a line of text into the buffer and increments the line number counter.
Definition: richio.cpp:244
Used for text file output.
Definition: richio.h:475
PROGRESS_REPORTER * m_progressReporter
Progress reporter to track the progress of the operation, may be nullptr.
Definition: io_base.h:213
virtual bool CanReadLibrary(const wxString &aFileName) const
Checks if this IO object can read the specified library file/directory.
Definition: io_base.cpp:67
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:269
timestamp_t AsLegacyTimestamp() const
Definition: kiid.cpp:230
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:51
int SetLibItemName(const UTF8 &aLibItemName)
Override the library item name portion of the LIB_ID to aLibItemName.
Definition: lib_id.cpp:110
UTF8 Format() const
Definition: lib_id.cpp:118
Define a library symbol object.
Definition: lib_symbol.h:77
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
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition: locale_io.h:49
An interface used to output 8 bit text in a convenient way.
Definition: richio.h:322
int PRINTF_FUNC Print(int nestLevel, const char *fmt,...)
Format and write text to the output stream.
Definition: richio.cpp:458
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:261
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:247
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:135
Holds all the data relating to one schematic.
Definition: schematic.h:75
bool IsValid() const
A simple test if the schematic is loaded, not a complete one.
Definition: schematic.h:121
SCH_SHEET & Root() const
Definition: schematic.h:105
PROJECT & Prj() const override
Return a reference to the project this schematic is part of.
Definition: schematic.h:90
Object to handle a bitmap image that can be inserted in a schematic.
Definition: sch_bitmap.h:41
VECTOR2I GetPosition() const override
Definition: sch_bitmap.h:145
BITMAP_BASE * GetImage() const
Definition: sch_bitmap.h:54
Base class for a bus or wire entry.
Definition: sch_bus_entry.h:38
VECTOR2I GetPosition() const override
VECTOR2I GetEnd() const
Instances are attached to a symbol or sheet and provide a place for the symbol's value,...
Definition: sch_field.h:51
int GetId() const
Definition: sch_field.h:133
VECTOR2I GetLibPosition() const
Definition: sch_field.h:262
wxString GetName(bool aUseDefaultName=true) const
Return the field name (not translated).
Definition: sch_field.cpp:1118
void SetName(const wxString &aName)
Definition: sch_field.cpp:1097
void SetText(const wxString &aText) override
Definition: sch_field.cpp:1107
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 saveText(SCH_TEXT *aText)
void cacheLib(const wxString &aLibraryFileName, const STRING_UTF8_MAP *aProperties)
void saveJunction(SCH_JUNCTION *aJunction)
void loadPageSettings(LINE_READER &aReader, SCH_SCREEN *aScreen)
OUTPUTFORMATTER * m_out
The formatter for saving SCH_SCREEN objects.
void Format(SCH_SHEET *aSheet)
void loadHierarchy(SCH_SHEET *aSheet)
void saveBusEntry(SCH_BUS_ENTRY_BASE *aBusEntry)
void SaveSymbol(const wxString &aLibraryPath, const LIB_SYMBOL *aSymbol, const STRING_UTF8_MAP *aProperties=nullptr) override
Write aSymbol to an existing library located at aLibraryPath.
void EnumerateSymbolLib(wxArrayString &aSymbolNameList, const wxString &aLibraryPath, const STRING_UTF8_MAP *aProperties=nullptr) override
Populate a list of LIB_SYMBOL alias names contained within the library aLibraryPath.
bool DeleteLibrary(const wxString &aLibraryPath, const STRING_UTF8_MAP *aProperties=nullptr) override
Delete an existing library and returns true, or if library does not exist returns false,...
SCH_SYMBOL * loadSymbol(LINE_READER &aReader)
std::stack< wxString > m_currentPath
Stack to maintain nested sheet paths.
LIB_SYMBOL * LoadSymbol(const wxString &aLibraryPath, const wxString &aAliasName, const STRING_UTF8_MAP *aProperties=nullptr) override
Load a LIB_SYMBOL object having aPartName from the aLibraryPath containing a library format that this...
int GetModifyHash() const override
Return the modification hash from the library cache.
void init(SCHEMATIC *aSchematic, const STRING_UTF8_MAP *aProperties=nullptr)
initialize PLUGIN like a constructor would.
void DeleteSymbol(const wxString &aLibraryPath, const wxString &aSymbolName, const STRING_UTF8_MAP *aProperties=nullptr) override
Delete the entire LIB_SYMBOL associated with aAliasName from the library aLibraryPath.
void CreateLibrary(const wxString &aLibraryPath, const STRING_UTF8_MAP *aProperties=nullptr) override
Create a new empty library at aLibraryPath empty.
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)
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.
SCH_SHEET * LoadSchematicFile(const wxString &aFileName, SCHEMATIC *aSchematic, SCH_SHEET *aAppendToMe=nullptr, const STRING_UTF8_MAP *aProperties=nullptr) override
Load information from some input file format that this SCH_IO implementation knows about,...
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)
void saveSymbol(SCH_SYMBOL *aSymbol)
void saveField(SCH_FIELD *aField)
SCH_SHEET * loadSheet(LINE_READER &aReader)
SCH_LINE * loadWire(LINE_READER &aReader)
void saveNoConnect(SCH_NO_CONNECT *aNoConnect)
void SaveSchematicFile(const wxString &aFileName, SCH_SHEET *aScreen, SCHEMATIC *aSchematic, const STRING_UTF8_MAP *aProperties=nullptr) override
Write aSchematic to a storage file in a format that this SCH_IO implementation knows about,...
unsigned m_lineCount
for progress reporting
std::shared_ptr< BUS_ALIAS > loadBusAlias(LINE_READER &aReader, SCH_SCREEN *aScreen)
SCH_IO_KICAD_LEGACY_LIB_CACHE * m_cache
void SaveLibrary(const wxString &aLibraryPath, const STRING_UTF8_MAP *aProperties=nullptr) override
SCH_BITMAP * loadBitmap(LINE_READER &aReader)
static LIB_SYMBOL * ParsePart(LINE_READER &aReader, int majorVersion=0, int minorVersion=0)
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 saveSheet(SCH_SHEET *aSheet)
bool writeDocFile(const STRING_UTF8_MAP *aProperties)
bool IsLibraryWritable(const wxString &aLibraryPath) override
Return true if the library at aLibraryPath is writable.
bool CanReadLibrary(const wxString &aFileName) const override
Checks if this IO object can read the specified library file/directory.
bool isBuffering(const STRING_UTF8_MAP *aProperties)
LINE_READER * m_lineReader
for progress reporting
void saveBitmap(SCH_BITMAP *aBitmap)
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:46
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:174
int GetBodyStyle() const
Definition: sch_item.h:240
int GetUnit() const
Definition: sch_item.h:237
SCH_LAYER_ID GetLayer() const
Return the layer this item is on.
Definition: sch_item.h:289
VECTOR2I GetPosition() const override
Definition: sch_junction.h:107
SPIN_STYLE GetSpinStyle() const
Definition: sch_label.cpp:373
void SetShape(LABEL_FLAG_SHAPE aShape)
Definition: sch_label.h:173
LABEL_FLAG_SHAPE GetShape() const
Definition: sch_label.h:172
virtual void SetSpinStyle(SPIN_STYLE aSpinStyle)
Definition: sch_label.cpp:338
Segment description base class to describe items which have 2 end points (track, wire,...
Definition: sch_line.h:40
VECTOR2I GetEndPoint() const
Definition: sch_line.h:140
VECTOR2I GetStartPoint() const
Definition: sch_line.h:135
LINE_STYLE GetLineStyle() const
Definition: sch_line.cpp:299
bool IsGraphicLine() const
Return if the line is a graphic (non electrical line)
Definition: sch_line.cpp:1005
int GetLineWidth() const
Definition: sch_line.h:172
COLOR4D GetLineColor() const
Returns COLOR4D::UNSPECIFIED if a custom color hasn't been set for this line.
Definition: sch_line.cpp:273
VECTOR2I GetPosition() const override
const PAGE_INFO & GetPageSettings() const
Definition: sch_screen.h:131
auto & GetBusAliases() const
Return a set of bus aliases defined in this screen.
Definition: sch_screen.h:510
void SetTitleBlock(const TITLE_BLOCK &aTitleBlock)
Definition: sch_screen.h:157
void Append(SCH_ITEM *aItem, bool aUpdateLibSymbol=true)
Definition: sch_screen.cpp:150
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:132
EE_RTREE & Items()
Gets the full RTree, usually for iterating.
Definition: sch_screen.h:109
const wxString & GetFileName() const
Definition: sch_screen.h:144
const KIID & GetUuid() const
Definition: sch_screen.h:525
void SetFileName(const wxString &aFileName)
Set the file name for this screen to aFileName.
Definition: sch_screen.cpp:115
const TITLE_BLOCK & GetTitleBlock() const
Definition: sch_screen.h:155
void SetFileReadOnly(bool aIsReadOnly)
Definition: sch_screen.h:146
void SetFileExists(bool aFileExists)
Definition: sch_screen.h:149
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:57
wxString GetFileName() const
Return the filename corresponding to this sheet.
Definition: sch_sheet.h:306
std::vector< SCH_FIELD > & GetFields()
Definition: sch_sheet.h:93
bool SearchHierarchy(const wxString &aFilename, SCH_SCREEN **aScreen)
Search the existing hierarchy for an instance of screen loaded from aFileName.
Definition: sch_sheet.cpp:728
VECTOR2I GetSize() const
Definition: sch_sheet.h:112
SCH_SCREEN * GetScreen() const
Definition: sch_sheet.h:110
VECTOR2I GetPosition() const override
Definition: sch_sheet.h:376
void SetScreen(SCH_SCREEN *aScreen)
Set the SCH_SCREEN associated with this sheet to aScreen.
Definition: sch_sheet.cpp:162
std::vector< SCH_SHEET_PIN * > & GetPins()
Definition: sch_sheet.h:181
Schematic symbol object.
Definition: sch_symbol.h:105
const std::vector< SCH_SYMBOL_INSTANCE > & GetInstances() const
Definition: sch_symbol.h:164
int GetFieldCount() const
Return the number of fields in this symbol.
Definition: sch_symbol.h:568
SCH_FIELD * GetField(MANDATORY_FIELD_T aFieldType)
Return a mandatory field in this symbol.
Definition: sch_symbol.cpp:915
VECTOR2I GetPosition() const override
Definition: sch_symbol.h:779
const LIB_ID & GetLibId() const override
Definition: sch_symbol.h:194
TRANSFORM & GetTransform()
Definition: sch_symbol.h:282
void GetFields(std::vector< SCH_FIELD * > &aVector, bool aVisibleOnly)
Populate a std::vector with SCH_FIELDs.
Definition: sch_symbol.cpp:963
wxString GetPrefix() const
Definition: sch_symbol.h:272
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:100
A name/value tuple with unique names and optional values.
bool Exists(const std::string &aProperty) const
static wxString GetLineStyleToken(LINE_STYLE aStyle)
static const char * PropPowerSymsOnly
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:432
static constexpr EDA_ANGLE ANGLE_HORIZONTAL
Definition: eda_angle.h:431
#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:40
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:407
SCH_LAYER_ID
Eeschema drawing layers.
Definition: layer_ids.h:353
@ LAYER_HIERLABEL
Definition: layer_ids.h:361
@ LAYER_GLOBLABEL
Definition: layer_ids.h:360
@ LAYER_WIRE
Definition: layer_ids.h:356
@ LAYER_NOTES
Definition: layer_ids.h:371
@ LAYER_BUS
Definition: layer_ids.h:357
@ LAYER_LOCLABEL
Definition: layer_ids.h:359
bool fileStartsWithPrefix(const wxString &aFilePath, const wxString &aPrefix, bool aIgnoreWhitespace)
Check if a file starts with a defined string.
Definition: io_utils.cpp:32
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)
@ L_BIDI
Definition: sch_label.h:96
@ L_TRISTATE
Definition: sch_label.h:97
@ L_UNSPECIFIED
Definition: sch_label.h:98
@ L_OUTPUT
Definition: sch_label.h:95
@ L_INPUT
Definition: sch_label.h:94
@ SHEETNAME
Definition: sch_sheet.h:45
@ SHEETFILENAME
Definition: sch_sheet.h:46
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:391
constexpr int IUToMils(int iu) const
Definition: base_units.h:99
constexpr int MilsToIU(int mils) const
Definition: base_units.h:93
A simple container for schematic symbol instance information.
static const wxString GetDefaultFieldName(int aFieldNdx, bool aTranslateForHI=false)
Return a default symbol field name for field aFieldNdx for all components.
Definition for symbol library class.
std::map< wxString, LIB_SYMBOL *, LibSymbolMapSort > LIB_SYMBOL_MAP
@ DATASHEET_FIELD
name of datasheet
@ MANDATORY_FIELDS
The first 5 are mandatory, and must be instantiated in SCH_COMPONENT and LIB_PART constructors.
@ REFERENCE_FIELD
Field Reference of part, i.e. "IC21".
@ 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:163
@ SCH_NO_CONNECT_T
Definition: typeinfo.h:160
@ TYPE_NOT_INIT
Definition: typeinfo.h:81
@ SCH_SYMBOL_T
Definition: typeinfo.h:172
@ SCH_LABEL_T
Definition: typeinfo.h:167
@ SCH_SHEET_T
Definition: typeinfo.h:174
@ SCH_HIER_LABEL_T
Definition: typeinfo.h:169
@ SCH_BUS_BUS_ENTRY_T
Definition: typeinfo.h:162
@ SCH_TEXT_T
Definition: typeinfo.h:151
@ SCH_BUS_WIRE_ENTRY_T
Definition: typeinfo.h:161
@ SCH_BITMAP_T
Definition: typeinfo.h:164
@ SCH_GLOBAL_LABEL_T
Definition: typeinfo.h:168
@ SCH_JUNCTION_T
Definition: typeinfo.h:159
VECTOR2< int > VECTOR2I
Definition: vector2d.h:588
Definition of file extensions used in Kicad.