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