KiCad PCB EDA Suite
Loading...
Searching...
No Matches
sch_io_eagle.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) 2017 CERN
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * @author Alejandro GarcĂ­a Montoro <[email protected]>
8 * @author Maciej Suminski <[email protected]>
9 * @author Russell Oliver <[email protected]>
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 3
14 * of the License, or (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program. If not, see <http://www.gnu.org/licenses/>.
23 */
24
26
27#include <algorithm>
28#include <memory>
29#include <wx/filename.h>
30#include <wx/string.h>
31#include <wx/tokenzr.h>
32#include <wx/wfstream.h>
33#include <wx/txtstrm.h>
34#include <wx/mstream.h>
35#include <wx/xml/xml.h>
36
37#include <font/fontconfig.h>
38#include <reporter.h>
40#include <lib_id.h>
41#include <progress_reporter.h>
42#include <project.h>
44#include <project_sch.h>
45#include <sch_bus_entry.h>
46#include <sch_edit_frame.h>
48#include <sch_junction.h>
49#include <sch_label.h>
50#include <sch_marker.h>
51#include <sch_pin.h>
52#include <sch_screen.h>
53#include <sch_shape.h>
54#include <sch_sheet.h>
55#include <sch_sheet_path.h>
56#include <sch_sheet_pin.h>
57#include <sch_symbol.h>
58#include <schematic.h>
59#include <string_utils.h>
62
63
64// Eagle schematic axes are aligned with x increasing left to right and Y increasing bottom to top
65// KiCad schematic axes are aligned with x increasing left to right and Y increasing top to bottom.
66
67using namespace std;
68
72static const std::map<wxString, ELECTRICAL_PINTYPE> pinDirectionsMap = {
73 { wxT( "sup" ), ELECTRICAL_PINTYPE::PT_POWER_IN },
74 { wxT( "pas" ), ELECTRICAL_PINTYPE::PT_PASSIVE },
75 { wxT( "out" ), ELECTRICAL_PINTYPE::PT_OUTPUT },
76 { wxT( "in" ), ELECTRICAL_PINTYPE::PT_INPUT },
77 { wxT( "nc" ), ELECTRICAL_PINTYPE::PT_NC },
78 { wxT( "io" ), ELECTRICAL_PINTYPE::PT_BIDI },
80 { wxT( "hiz" ), ELECTRICAL_PINTYPE::PT_TRISTATE },
81 { wxT( "pwr" ), ELECTRICAL_PINTYPE::PT_POWER_IN },
82};
83
84
86static BOX2I getSheetBbox( SCH_SHEET* aSheet )
87{
88 BOX2I bbox;
89
90 for( SCH_ITEM* item : aSheet->GetScreen()->Items() )
91 bbox.Merge( item->GetBoundingBox() );
92
93 return bbox;
94}
95
96
98static inline wxString extractNetName( const wxString& aPinName )
99{
100 return aPinName.BeforeFirst( '@' );
101}
102
103
108
109
111{
112 SCH_SHEET* currentSheet = m_sheetPath.Last();
113 wxCHECK( currentSheet, nullptr );
114 return currentSheet->GetScreen();
115}
116
117
119{
120 if( m_libName.IsEmpty() )
121 {
122 // Try to come up with a meaningful name
123 m_libName = m_schematic->Project().GetProjectName();
124
125 if( m_libName.IsEmpty() )
126 {
127 wxFileName fn( m_rootSheet->GetFileName() );
128 m_libName = fn.GetName();
129 }
130
131 if( m_libName.IsEmpty() )
132 m_libName = wxT( "noname" );
133
134 m_libName += wxT( "-eagle-import" );
136 }
137
138 return m_libName;
139}
140
141
143{
144 wxFileName fn;
145
146 wxCHECK( m_schematic, fn );
147
148 fn.Assign( m_schematic->Project().GetProjectPath(), getLibName(),
150
151 return fn;
152}
153
154
155void SCH_IO_EAGLE::loadLayerDefs( const std::vector<std::unique_ptr<ELAYER>>& aLayers )
156{
157 // match layers based on their names
158 for( const std::unique_ptr<ELAYER>& elayer : aLayers )
159 {
176
177 switch ( elayer->number)
178 {
179 case 91:
180 m_layerMap[elayer->number] = LAYER_WIRE;
181 break;
182 case 92:
183 m_layerMap[elayer->number] = LAYER_BUS;
184 break;
185 case 97:
186 case 98:
187 m_layerMap[elayer->number] = LAYER_NOTES;
188 break;
189
190 default:
191 break;
192 }
193 }
194}
195
196
198{
199 auto it = m_layerMap.find( aEagleLayer );
200 return it == m_layerMap.end() ? LAYER_NOTES : it->second;
201}
202
203
204// Return the KiCad symbol orientation based on eagle rotation degrees.
206{
207 int roti = int( eagleDegrees );
208
209 switch( roti )
210 {
211 case 0: return SYM_ORIENT_0;
212 case 90: return SYM_ORIENT_90;
213 case 180: return SYM_ORIENT_180;
214 case 270: return SYM_ORIENT_270;
215
216 default:
217 wxASSERT_MSG( false, wxString::Format( wxT( "Unhandled orientation (%d degrees)" ),
218 roti ) );
219 return SYM_ORIENT_0;
220 }
221}
222
223
224// Calculate text alignment based on the given Eagle text alignment parameters.
225static void eagleToKicadAlignment( EDA_TEXT* aText, int aEagleAlignment, int aRelDegress,
226 bool aMirror, bool aSpin, int aAbsDegress )
227{
228 int align = aEagleAlignment;
229
230 if( aRelDegress == 90 )
231 {
233 }
234 else if( aRelDegress == 180 )
235 {
236 align = -align;
237 }
238 else if( aRelDegress == 270 )
239 {
241 align = -align;
242 }
243
244 if( aMirror == true )
245 {
246 if( aAbsDegress == 90 || aAbsDegress == 270 )
247 {
248 if( align == ETEXT::BOTTOM_RIGHT )
249 align = ETEXT::TOP_RIGHT;
250 else if( align == ETEXT::BOTTOM_LEFT )
251 align = ETEXT::TOP_LEFT;
252 else if( align == ETEXT::TOP_LEFT )
253 align = ETEXT::BOTTOM_LEFT;
254 else if( align == ETEXT::TOP_RIGHT )
255 align = ETEXT::BOTTOM_RIGHT;
256 }
257 else if( aAbsDegress == 0 || aAbsDegress == 180 )
258 {
259 if( align == ETEXT::BOTTOM_RIGHT )
260 align = ETEXT::BOTTOM_LEFT;
261 else if( align == ETEXT::BOTTOM_LEFT )
262 align = ETEXT::BOTTOM_RIGHT;
263 else if( align == ETEXT::TOP_LEFT )
264 align = ETEXT::TOP_RIGHT;
265 else if( align == ETEXT::TOP_RIGHT )
266 align = ETEXT::TOP_LEFT;
267 else if( align == ETEXT::CENTER_LEFT )
268 align = ETEXT::CENTER_RIGHT;
269 else if( align == ETEXT::CENTER_RIGHT )
270 align = ETEXT::CENTER_LEFT;
271 }
272 }
273
274 switch( align )
275 {
276 case ETEXT::CENTER:
279 break;
280
284 break;
285
289 break;
290
294 break;
295
296 case ETEXT::TOP_LEFT:
299 break;
300
301 case ETEXT::TOP_RIGHT:
304 break;
305
309 break;
310
314 break;
315
319 break;
320
321 default:
324 break;
325 }
326}
327
328
329SCH_IO_EAGLE::SCH_IO_EAGLE() : SCH_IO( wxS( "EAGLE" ) ),
330 m_rootSheet( nullptr ),
331 m_schematic( nullptr ),
332 m_sheetIndex( 1 )
333{
335}
336
337
341
342
344{
345 return 0;
346}
347
348
349SCH_SHEET* SCH_IO_EAGLE::LoadSchematicFile( const wxString& aFileName, SCHEMATIC* aSchematic,
350 SCH_SHEET* aAppendToMe,
351 const std::map<std::string, UTF8>* aProperties )
352{
353 wxASSERT( !aFileName || aSchematic != nullptr );
354
355 // Collect the font substitution warnings (RAII - automatically reset on scope exit)
357
358 m_filename = aFileName;
359 m_schematic = aSchematic;
360
362 {
363 m_progressReporter->Report( wxString::Format( _( "Loading %s..." ), aFileName ) );
364
365 if( !m_progressReporter->KeepRefreshing() )
366 THROW_IO_ERROR( ( "Open canceled by user." ) );
367 }
368
369 // Load the document
370 wxXmlDocument xmlDocument = loadXmlDocument( m_filename.GetFullPath() );
371
372 // Retrieve the root as current node
373 wxXmlNode* currentNode = xmlDocument.GetRoot();
374
376 m_progressReporter->SetNumPhases( static_cast<int>( GetNodeCount( currentNode ) ) );
377
378 wxFileName newFilename( m_filename );
379 newFilename.SetExt( FILEEXT::KiCadSchematicFileExtension );
380
381 // Owns the temporary VR for the non-append path so it is freed when this scope exits.
382 // The actual schematic VR will be created by SetTopLevelSheets() inside loadSchematic().
383 unique_ptr<SCH_SHEET> tempVROwner;
384
385 if( aAppendToMe )
386 {
387 wxCHECK_MSG( aSchematic->IsValid(), nullptr,
388 wxT( "Can't append to a schematic with no root!" ) );
389
390 m_rootSheet = &aSchematic->Root();
391
392 // We really should be passing the SCH_SHEET_PATH object to the aAppendToMe attribute
393 // instead of the SCH_SHEET. The full path is needed to properly generate instance
394 // data.
395 SCH_SHEET_LIST hierarchy( m_rootSheet );
396
397 for( const SCH_SHEET_PATH& sheetPath : hierarchy )
398 {
399 if( sheetPath.Last() == aAppendToMe )
400 {
401 m_sheetPath = sheetPath;
402 break;
403 }
404 }
405 }
406 else
407 {
408 // Create a temporary local VR used only to anchor m_sheetPath during loading.
409 // loadSchematic() will call SetTopLevelSheets() with the real Eagle pages, which
410 // creates the actual schematic VR and re-parents the pages to it.
411 tempVROwner = std::make_unique<SCH_SHEET>( aSchematic );
412 m_rootSheet = tempVROwner.get();
413 const_cast<KIID&>( m_rootSheet->m_Uuid ) = niluuid;
414 }
415
416 if( !m_rootSheet->GetScreen() )
417 {
418 SCH_SCREEN* screen = new SCH_SCREEN( m_schematic );
419 screen->SetFileName( newFilename.GetFullPath() );
420 m_rootSheet->SetScreen( screen );
421
422 // Virtual root sheet UUID must be nil since all Eagle pages are loaded as subsheets.
423 const_cast<KIID&>( m_rootSheet->m_Uuid ) = niluuid;
424
425 // There is always at least a root sheet.
426 m_sheetPath.push_back( m_rootSheet );
427 }
428
430 LIBRARY_TABLE* table = adapter->ProjectTable().value_or( nullptr );
431 wxCHECK_MSG( table, nullptr, "Could not load symbol lib table." );
432
433 m_pi.reset( SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_KICAD ) );
434
437 if( !table->HasRow( getLibName() ) )
438 {
439 // Create a new empty symbol library.
440 m_pi->CreateLibrary( getLibFileName().GetFullPath() );
441 wxString libTableUri = wxT( "${KIPRJMOD}/" ) + getLibFileName().GetFullName();
442
443 // Add the new library to the project symbol library table.
444 LIBRARY_TABLE_ROW& row = table->InsertRow();
445 row.SetNickname( getLibName() );
446 row.SetURI( libTableUri );
447 row.SetType( "KiCad" );
448
449 table->Save();
450
451 adapter->LoadOne( getLibName() );
452 }
453
454 m_eagleDoc = std::make_unique<EAGLE_DOC>( currentNode, this );
455
456 // If the attribute is found, store the Eagle version;
457 // otherwise, store the dummy "0.0" version.
458 m_version = ( m_eagleDoc->version.IsEmpty() ) ? wxString( wxS( "0.0" ) ) : m_eagleDoc->version;
459
460 // Load drawing
461 loadDrawing( m_eagleDoc->drawing );
462
463 if( !aAppendToMe )
464 m_rootSheet = &aSchematic->Root();
465
466 m_pi->SaveLibrary( getLibFileName().GetFullPath() );
467
468 SCH_SCREENS allSheets( m_rootSheet );
469 allSheets.UpdateSymbolLinks( &LOAD_INFO_REPORTER::GetInstance() ); // Update all symbol library links for all sheets.
470
471 return m_rootSheet;
472}
473
474
475void SCH_IO_EAGLE::EnumerateSymbolLib( wxArrayString& aSymbolNameList,
476 const wxString& aLibraryPath,
477 const std::map<std::string, UTF8>* aProperties )
478{
479 m_filename = aLibraryPath;
480 m_libName = m_filename.GetName();
481
482 ensureLoadedLibrary( aLibraryPath );
483
484 auto it = m_eagleLibs.find( m_libName );
485
486 if( it != m_eagleLibs.end() )
487 {
488 for( const auto& [symName, libSymbol] : it->second.KiCadSymbols )
489 aSymbolNameList.push_back( symName );
490 }
491}
492
493
494void SCH_IO_EAGLE::EnumerateSymbolLib( std::vector<LIB_SYMBOL*>& aSymbolList,
495 const wxString& aLibraryPath,
496 const std::map<std::string, UTF8>* aProperties )
497{
498 m_filename = aLibraryPath;
499 m_libName = m_filename.GetName();
500
501 ensureLoadedLibrary( aLibraryPath );
502
503 auto it = m_eagleLibs.find( m_libName );
504
505 if( it != m_eagleLibs.end() )
506 {
507 for( const auto& [symName, libSymbol] : it->second.KiCadSymbols )
508 aSymbolList.push_back( libSymbol.get() );
509 }
510}
511
512
513LIB_SYMBOL* SCH_IO_EAGLE::LoadSymbol( const wxString& aLibraryPath, const wxString& aAliasName,
514 const std::map<std::string, UTF8>* aProperties )
515{
516 m_filename = aLibraryPath;
517 m_libName = m_filename.GetName();
518
519 ensureLoadedLibrary( aLibraryPath );
520
521 auto it = m_eagleLibs.find( m_libName );
522
523 if( it != m_eagleLibs.end() )
524 {
525 auto it2 = it->second.KiCadSymbols.find( aAliasName );
526
527 if( it2 != it->second.KiCadSymbols.end() )
528 return it2->second.get();
529 }
530
531 return nullptr;
532}
533
534
535long long SCH_IO_EAGLE::getLibraryTimestamp( const wxString& aLibraryPath ) const
536{
537 wxFileName fn( aLibraryPath );
538
539 if( fn.IsFileReadable() && fn.GetModificationTime().IsValid() )
540 return fn.GetModificationTime().GetValue().GetValue();
541 else
542 return 0;
543}
544
545
546void SCH_IO_EAGLE::ensureLoadedLibrary( const wxString& aLibraryPath )
547{
548 // Suppress font substitution warnings (RAII - automatically restored on scope exit)
549 FONTCONFIG_REPORTER_SCOPE fontconfigScope( nullptr );
550
551 if( m_eagleLibs.find( m_libName ) != m_eagleLibs.end() )
552 {
553 wxCHECK( m_timestamps.count( m_libName ), /*void*/ );
554
555 if( m_timestamps.at( m_libName ) == getLibraryTimestamp( aLibraryPath ) )
556 return;
557 }
558
560 {
561 m_progressReporter->Report( wxString::Format( _( "Loading %s..." ), aLibraryPath ) );
562
563 if( !m_progressReporter->KeepRefreshing() )
564 THROW_IO_ERROR( ( "Open canceled by user." ) );
565 }
566
567 // Load the document
568 wxXmlDocument xmlDocument = loadXmlDocument( m_filename.GetFullPath() );
569
570 // Retrieve the root as current node
571 std::unique_ptr<EAGLE_DOC> doc = std::make_unique<EAGLE_DOC>( xmlDocument.GetRoot(), this );
572
573 // If the attribute is found, store the Eagle version;
574 // otherwise, store the dummy "0.0" version.
575 m_version = ( doc->version.IsEmpty() ) ? wxString( wxS( "0.0" ) ) : doc->version;
576
577 // Load drawing
578 loadDrawing( doc->drawing );
579
580 // Remember timestamp
581 m_timestamps[m_libName] = getLibraryTimestamp( aLibraryPath );
582}
583
584
585wxXmlDocument SCH_IO_EAGLE::loadXmlDocument( const wxString& aFileName )
586{
587 wxXmlDocument xmlDocument;
588 wxFFileInputStream stream( m_filename.GetFullPath() );
589
590 if( !stream.IsOk() )
591 {
593 wxString::Format( _( "Unable to read file '%s'." ), m_filename.GetFullPath() ) );
594 }
595
596 // read first line to check for Eagle XML format file
597 wxTextInputStream text( stream );
598 wxString line = text.ReadLine();
599
600 if( !line.StartsWith( wxT( "<?xml" ) ) && !line.StartsWith( wxT( "<!--" ) )
601 && !line.StartsWith( wxT( "<eagle " ) ) )
602 {
603 THROW_IO_ERROR( wxString::Format( _( "'%s' is an Eagle binary-format file; "
604 "only Eagle XML-format files can be imported." ),
605 m_filename.GetFullPath() ) );
606 }
607
608#if wxCHECK_VERSION( 3, 3, 0 )
609 wxXmlParseError err;
610
611 if( !xmlDocument.Load( stream, wxXMLDOC_NONE, &err ) )
612 {
613 if( err.message == wxS( "no element found" ) )
614 {
615 // Some files don't have the correct header, throwing off the xml parser
616 // So prepend the correct header
617 wxMemoryOutputStream memOutput;
618
619 wxString header;
620 header << "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
621 header << "<!DOCTYPE eagle SYSTEM \"eagle.dtd\">\n";
622
623 wxScopedCharBuffer headerBuf = header.utf8_str();
624 memOutput.Write( headerBuf.data(), headerBuf.length() );
625
626 wxFFileInputStream stream2( m_filename.GetFullPath() );
627 memOutput.Write( stream2 );
628
629 wxMemoryInputStream memInput( memOutput );
630
631 if( !xmlDocument.Load( memInput, wxXMLDOC_NONE, &err ) )
632 {
633 THROW_IO_ERROR( wxString::Format( _( "Unable to read file '%s'." ), m_filename.GetFullPath() ) );
634 }
635 }
636 else
637 {
638 THROW_IO_ERROR( wxString::Format( _( "Unable to read file '%s'.\n'%s' at line %d, column %d, offset %d" ),
639 m_filename.GetFullPath(), err.message, err.line, err.column,
640 err.offset ) );
641 }
642 }
643#else
644 if( !xmlDocument.Load( stream ) )
645 {
646 // Some files don't have the correct header, throwing off the xml parser
647 // So prepend the correct header
648 wxMemoryOutputStream memOutput;
649
650 wxString header;
651 header << "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
652 header << "<!DOCTYPE eagle SYSTEM \"eagle.dtd\">\n";
653
654 wxScopedCharBuffer headerBuf = header.utf8_str();
655 memOutput.Write( headerBuf.data(), headerBuf.length() );
656
657 wxFFileInputStream stream2( m_filename.GetFullPath() );
658 memOutput.Write( stream2 );
659
660 wxMemoryInputStream memInput( memOutput );
661
662 if( !xmlDocument.Load( memInput ) )
663 {
664 THROW_IO_ERROR( wxString::Format( _( "Unable to read file '%s'." ), m_filename.GetFullPath() ) );
665 }
666 }
667#endif
668
669 return xmlDocument;
670}
671
672
673void SCH_IO_EAGLE::loadDrawing( const std::unique_ptr<EDRAWING>& aDrawing )
674{
675 wxCHECK( aDrawing, /* void */ );
676
677 loadLayerDefs( aDrawing->layers );
678
679 if( aDrawing->library )
680 {
682 elib.name = m_libName;
683
684 loadLibrary( &aDrawing->library.value(), &elib );
685 }
686
687 if( aDrawing->schematic )
688 loadSchematic( *aDrawing->schematic );
689}
690
691
692void SCH_IO_EAGLE::countNets( const ESCHEMATIC& aSchematic )
693{
694 for( const std::unique_ptr<ESHEET>& esheet : aSchematic.sheets )
695 {
696 for( const std::unique_ptr<ENET>& enet : esheet->nets )
697 {
698 wxString netName = enet->netname;
699
700 if( m_netCounts.count( netName ) )
701 m_netCounts[netName] = m_netCounts[netName] + 1;
702 else
703 m_netCounts[netName] = 1;
704 }
705 }
706
707 for( const auto& [modname, emodule] : aSchematic.modules )
708 {
709 for( const std::unique_ptr<ESHEET>& esheet : emodule->sheets )
710 {
711 for( const std::unique_ptr<ENET>& enet : esheet->nets )
712 {
713 wxString netName = enet->netname;
714
715 if( m_netCounts.count( netName ) )
716 m_netCounts[netName] = m_netCounts[netName] + 1;
717 else
718 m_netCounts[netName] = 1;
719 }
720 }
721 }
722}
723
724
726{
727 // Map all children into a readable dictionary
728 if( aSchematic.sheets.empty() )
729 return;
730
731 // N.B. Eagle parts are case-insensitive in matching but we keep the display case
732 for( const auto& [name, epart] : aSchematic.parts )
733 m_partlist[name.Upper()] = epart.get();
734
735 for( const auto& [modName, emodule] : aSchematic.modules )
736 {
737 for( const auto& [partName, epart] : emodule->parts )
738 m_partlist[partName.Upper()] = epart.get();
739 }
740
741 if( !aSchematic.libraries.empty() )
742 {
743 for( const auto& [libName, elibrary] : aSchematic.libraries )
744 {
745 EAGLE_LIBRARY* elib = &m_eagleLibs[elibrary->GetName()];
746 elib->name = elibrary->GetName();
747
748 loadLibrary( elibrary.get(), &m_eagleLibs[elibrary->GetName()] );
749 }
750
751 m_pi->SaveLibrary( getLibFileName().GetFullPath() );
752 }
753
754 // find all nets and count how many sheets they appear on.
755 // local labels will be used for nets found only on that sheet.
756 countNets( aSchematic );
757
758 // Create all Eagle pages as top-level sheets (direct children of the virtual root).
759 // Collect them first so we can atomically replace any spurious default sheet created
760 // during schematic construction with exactly the set of real Eagle pages.
761 std::vector<SCH_SHEET*> eaglePages;
762 eaglePages.reserve( aSchematic.sheets.size() );
763
764 for( const std::unique_ptr<ESHEET>& esheet : aSchematic.sheets )
765 {
766 // Eagle schematics are never more than one sheet deep so the parent sheet is
767 // always the root sheet.
768 std::unique_ptr<SCH_SHEET> sheet = std::make_unique<SCH_SHEET>( m_rootSheet );
769 SCH_SCREEN* screen = new SCH_SCREEN( m_schematic );
770 sheet->SetScreen( screen );
771
772 wxCHECK2( sheet && screen, continue );
773
774 wxString pageNo = wxString::Format( wxT( "%d" ), m_sheetIndex );
775
776 m_sheetPath.push_back( sheet.get() );
777 loadSheet( esheet );
778
779 m_sheetPath.SetPageNumber( pageNo );
780 m_sheetPath.pop_back();
781
782 eaglePages.push_back( sheet.release() );
783
784 m_sheetIndex++;
785 }
786
787 if( !eaglePages.empty() )
788 {
789 // In the append path m_rootSheet is already the schematic's VR. Use
790 // AddTopLevelSheet to avoid discarding sheets already in the target.
791 // In the fresh-import path m_rootSheet is a temporary local VR, so we use
792 // SetTopLevelSheets to atomically replace any spurious default sheet with
793 // exactly the Eagle pages.
794 if( m_rootSheet == &m_schematic->Root() )
795 {
796 for( SCH_SHEET* page : eaglePages )
797 m_schematic->AddTopLevelSheet( page );
798 }
799 else
800 {
801 m_schematic->SetTopLevelSheets( eaglePages );
802 }
803 }
804
805 // Handle the missing symbol units that need to be instantiated
806 // to create the missing implicit connections
807
808 // Calculate the already placed items bounding box and the page size to determine
809 // placement for the new symbols
810 SCH_SHEET* schematicRoot = &m_schematic->Root();
811
812 struct MISSING_UNIT_PLACEMENT
813 {
814 VECTOR2I pageSizeIU;
815 BOX2I sheetBbox;
816 VECTOR2I newCmpPosition;
817 int maxY;
818 SCH_SHEET_PATH sheetpath;
819 SCH_SCREEN* screen;
820 };
821
822 std::map<SCH_SCREEN*, MISSING_UNIT_PLACEMENT> placements;
823
824 for( auto& cmp : m_missingCmps )
825 {
826 const SCH_SYMBOL* origSymbol = cmp.second.cmp;
827
828 for( auto& unitEntry : cmp.second.units )
829 {
830 if( unitEntry.second == false )
831 continue; // unit has been already processed
832
833 // Instantiate the missing symbol unit
834 int unit = unitEntry.first;
835 const wxString reference = origSymbol->GetField( FIELD_T::REFERENCE )->GetText();
836 std::unique_ptr<SCH_SYMBOL> symbol( (SCH_SYMBOL*) origSymbol->Duplicate( IGNORE_PARENT_GROUP ) );
837
838 SCH_SCREEN* targetScreen = cmp.second.screen;
839
840 if( !targetScreen )
841 {
842 SCH_SHEET* fallbackSheet = m_schematic->GetTopLevelSheet( 0 );
843
844 if( fallbackSheet )
845 targetScreen = fallbackSheet->GetScreen();
846 else
847 targetScreen = schematicRoot->GetScreen();
848 }
849
850 auto placementIt = placements.find( targetScreen );
851
852 if( placementIt == placements.end() )
853 {
854 MISSING_UNIT_PLACEMENT placement;
855 placement.screen = targetScreen;
856 placement.pageSizeIU = targetScreen->GetPageSettings().GetSizeIU( schIUScale.IU_PER_MILS );
857 schematicRoot->LocatePathOfScreen( targetScreen, &placement.sheetpath );
858
859 SCH_SHEET* targetSheet = placement.sheetpath.Last();
860
861 if( targetSheet )
862 placement.sheetBbox = getSheetBbox( targetSheet );
863
864 placement.newCmpPosition = VECTOR2I( placement.sheetBbox.GetLeft(),
865 placement.sheetBbox.GetBottom() );
866 placement.maxY = placement.sheetBbox.GetY();
867 placementIt = placements.emplace( targetScreen, placement ).first;
868 }
869
870 MISSING_UNIT_PLACEMENT& placement = placementIt->second;
871
872 symbol->SetUnitSelection( &placement.sheetpath, unit );
873 symbol->SetUnit( unit );
874 symbol->SetOrientation( 0 );
875 symbol->AddHierarchicalReference( placement.sheetpath.Path(), reference, unit );
876
877 // Calculate the placement position
878 BOX2I cmpBbox = symbol->GetBoundingBox();
879 int posY = placement.newCmpPosition.y + cmpBbox.GetHeight();
880 symbol->SetPosition( VECTOR2I( placement.newCmpPosition.x, posY ) );
881 placement.newCmpPosition.x += cmpBbox.GetWidth();
882 placement.maxY = std::max( placement.maxY, posY );
883
884 if( placement.newCmpPosition.x >= placement.pageSizeIU.x ) // reached the page boundary?
885 placement.newCmpPosition = VECTOR2I( placement.sheetBbox.GetLeft(),
886 placement.maxY ); // then start a new row
887
888 // Add the global net labels to recreate the implicit connections
889 addImplicitConnections( symbol.get(), placement.screen, false );
890 placement.screen->Append( symbol.release() );
891 }
892 }
893
894 m_missingCmps.clear();
895}
896
897
898void SCH_IO_EAGLE::loadSheet( const std::unique_ptr<ESHEET>& aSheet )
899{
900 SCH_SHEET* sheet = getCurrentSheet();
901 SCH_SCREEN* screen = getCurrentScreen();
902
903 wxCHECK( sheet && screen, /* void */ );
904
905 if( m_modules.empty() )
906 {
907 std::string filename;
908
909 filename = wxString::Format( wxT( "%s_%d" ), m_filename.GetName(), m_sheetIndex );
910
911 if( aSheet->description )
912 sheet->SetName( aSheet->description.value().text );
913 else
914 sheet->SetName( filename );
915
916 ReplaceIllegalFileNameChars( filename );
917 replace( filename.begin(), filename.end(), ' ', '_' );
918
919 // Use the project directory so saved pages land alongside the project file,
920 // not in the Eagle source directory.
921 wxFileName fn;
922 fn.SetPath( m_schematic->Project().GetProjectPath() );
923 fn.SetName( filename );
925
926 sheet->SetFileName( fn.GetFullName() );
927 screen->SetFileName( fn.GetFullPath() );
928 }
929
930 for( const auto& [name, moduleinst] : aSheet->moduleinsts )
931 loadModuleInstance( moduleinst );
932
933 sheet->AutoplaceFields( screen, AUTOPLACE_AUTO );
934
935 if( aSheet->plain )
936 {
937 for( const std::unique_ptr<EPOLYGON>& epoly : aSheet->plain->polygons )
938 screen->Append( loadPolyLine( epoly ) );
939
940 for( const std::unique_ptr<EWIRE>& ewire : aSheet->plain->wires )
941 {
942 SEG endpoints;
943 screen->Append( loadWire( ewire, endpoints ) );
944 }
945
946 for( const std::unique_ptr<ETEXT>& etext : aSheet->plain->texts )
947 screen->Append( loadPlainText( etext ) );
948
949 for( const std::unique_ptr<ECIRCLE>& ecircle : aSheet->plain->circles )
950 screen->Append( loadCircle( ecircle ) );
951
952 for( const std::unique_ptr<ERECT>& erectangle : aSheet->plain->rectangles )
953 screen->Append( loadRectangle( erectangle ) );
954
955 for( const std::unique_ptr<EFRAME>& eframe : aSheet->plain->frames )
956 {
957 std::vector<SCH_ITEM*> frameItems;
958
959 loadFrame( eframe, frameItems );
960
961 for( SCH_ITEM* item : frameItems )
962 screen->Append( item );
963 }
964
965 // Holes and splines currently not handled. Not sure hole has any meaning in scheamtics.
966 }
967
968 for( const std::unique_ptr<EINSTANCE>& einstance : aSheet->instances )
969 loadInstance( einstance, ( m_modules.size() ) ? m_modules.back()->parts
970 : m_eagleDoc->drawing->schematic->parts );
971
972 // Loop through all buses
973 // From the DTD: "Buses receive names which determine which signals they include.
974 // A bus is a drawing object. It does not create any electrical connections.
975 // These are always created by means of the nets and their names."
976 for( const std::unique_ptr<EBUS>& ebus : aSheet->busses )
977 {
978 // Get the bus name
979 wxString busName = translateEagleBusName( ebus->name );
980
981 // Load segments of this bus
982 loadSegments( ebus->segments, busName, wxString() );
983 }
984
985 for( const std::unique_ptr<ENET>& enet : aSheet->nets )
986 {
987 // Get the net name and class
988 wxString netName = enet->netname;
989 wxString netClass = wxString::Format( wxS( "%i" ), enet->netcode );
990
991 // Load segments of this net
992 loadSegments( enet->segments, netName, netClass );
993 }
994
995 adjustNetLabels(); // needs to be called before addBusEntries()
997
998 // Calculate the new sheet size.
999 BOX2I sheetBoundingBox = getSheetBbox( sheet );
1000 VECTOR2I targetSheetSize = sheetBoundingBox.GetSize();
1001 targetSheetSize += VECTOR2I( schIUScale.MilsToIU( 1500 ), schIUScale.MilsToIU( 1500 ) );
1002
1003 // Get current Eeschema sheet size.
1004 VECTOR2I pageSizeIU = screen->GetPageSettings().GetSizeIU( schIUScale.IU_PER_MILS );
1005 PAGE_INFO pageInfo = screen->GetPageSettings();
1006
1007 // Increase if necessary
1008 if( pageSizeIU.x < targetSheetSize.x )
1009 pageInfo.SetWidthMils( schIUScale.IUToMils( targetSheetSize.x ) );
1010
1011 if( pageSizeIU.y < targetSheetSize.y )
1012 pageInfo.SetHeightMils( schIUScale.IUToMils( targetSheetSize.y ) );
1013
1014 // Set the new sheet size.
1015 screen->SetPageSettings( pageInfo );
1016
1017 pageSizeIU = screen->GetPageSettings().GetSizeIU( schIUScale.IU_PER_MILS );
1018 VECTOR2I sheetcentre( pageSizeIU.x / 2, pageSizeIU.y / 2 );
1019 VECTOR2I itemsCentre = sheetBoundingBox.Centre();
1020
1021 // round the translation to nearest 100mil to place it on the grid.
1022 VECTOR2I translation = sheetcentre - itemsCentre;
1023 translation.x = translation.x - translation.x % schIUScale.MilsToIU( 100 );
1024 translation.y = translation.y - translation.y % schIUScale.MilsToIU( 100 );
1025
1026 // Add global net labels for the named power input pins in this sheet
1027 for( SCH_ITEM* item : screen->Items().OfType( SCH_SYMBOL_T ) )
1028 {
1029 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
1030 addImplicitConnections( symbol, screen, true );
1031 }
1032
1033 m_connPoints.clear();
1034
1035 // Translate the items.
1036 std::vector<SCH_ITEM*> allItems;
1037
1038 std::copy( screen->Items().begin(), screen->Items().end(), std::back_inserter( allItems ) );
1039
1040 for( SCH_ITEM* item : allItems )
1041 {
1042 item->SetPosition( item->GetPosition() + translation );
1043
1044 // We don't read positions of Eagle label fields (primarily intersheet refs), so we
1045 // need to autoplace them after applying the translation.
1046 if( SCH_LABEL_BASE* label = dynamic_cast<SCH_LABEL_BASE*>( item ) )
1047 label->AutoplaceFields( screen, AUTOPLACE_AUTO );
1048
1049 item->ClearFlags();
1050 screen->Update( item );
1051 }
1052}
1053
1054
1055void SCH_IO_EAGLE::loadModuleInstance( const std::unique_ptr<EMODULEINST>& aModuleInstance )
1056{
1057 SCH_SHEET* currentSheet = getCurrentSheet();
1058 SCH_SCREEN* currentScreen = getCurrentScreen();
1059
1060 wxCHECK( currentSheet &&currentScreen, /* void */ );
1061
1062 m_sheetIndex++;
1063
1064 // Eagle document has already be checked for drawing and schematic nodes so this
1065 // should not segfault.
1066 auto it = m_eagleDoc->drawing->schematic->modules.find( aModuleInstance->moduleinst );
1067
1068 // Find the module referenced by the module instance.
1069 if( it == m_eagleDoc->drawing->schematic->modules.end() )
1070 {
1071 THROW_IO_ERROR( wxString::Format( _( "No module instance '%s' found in schematic "
1072 "file:\n%s" ),
1073 aModuleInstance->name, m_filename.GetFullPath() ) );
1074 }
1075
1076 wxFileName fn = m_filename;
1077 fn.SetName( aModuleInstance->moduleinst );
1079
1080 VECTOR2I portExtWireEndpoint;
1081 VECTOR2I size( it->second->dx.ToSchUnits(), it->second->dy.ToSchUnits() );
1082
1083 int halfX = KiROUND( size.x / 2.0 );
1084 int halfY = KiROUND( size.y / 2.0 );
1085 int portExtWireLength = schIUScale.mmToIU( 5.08 );
1086 VECTOR2I pos( aModuleInstance->x.ToSchUnits() - halfX,
1087 -aModuleInstance->y.ToSchUnits() - halfY );
1088
1089 std::unique_ptr<SCH_SHEET> newSheet = std::make_unique<SCH_SHEET>( currentSheet, pos, size );
1090
1091 // The Eagle module for this instance (SCH_SCREEN in KiCad) may have already been loaded.
1092 SCH_SCREEN* newScreen = nullptr;
1093 SCH_SCREENS schFiles( m_rootSheet );
1094
1095 for( SCH_SCREEN* schFile = schFiles.GetFirst(); schFile; schFile = schFiles.GetNext() )
1096 {
1097 if( schFile->GetFileName() == fn.GetFullPath() )
1098 {
1099 newScreen = schFile;
1100 break;
1101 }
1102 }
1103
1104 bool isNewSchFile = ( newScreen == nullptr );
1105
1106 if( !newScreen )
1107 {
1108 newScreen = new SCH_SCREEN( m_schematic );
1109 newScreen->SetFileName( fn.GetFullPath() );
1110 }
1111
1112 wxCHECK( newSheet && newScreen, /* void */ );
1113
1114 newSheet->SetScreen( newScreen );
1115 newSheet->SetFileName( fn.GetFullName() );
1116 newSheet->SetName( aModuleInstance->name );
1117
1118 for( const auto& [portName, port] : it->second->ports )
1119 {
1120 VECTOR2I pinPos( 0, 0 );
1121 int pinOffset = port->coord.ToSchUnits();
1123
1124 if( port->side == "left" )
1125 {
1126 side = SHEET_SIDE::LEFT;
1127 pinPos.x = pos.x;
1128 pinPos.y = pos.y + halfY - pinOffset;
1129 portExtWireEndpoint = pinPos;
1130 portExtWireEndpoint.x -= portExtWireLength;
1131 }
1132 else if( port->side == "right" )
1133 {
1134 side = SHEET_SIDE::RIGHT;
1135 pinPos.x = pos.x + size.x;
1136 pinPos.y = pos.y + halfY - pinOffset;
1137 portExtWireEndpoint = pinPos;
1138 portExtWireEndpoint.x += portExtWireLength;
1139 }
1140 else if( port->side == "top" )
1141 {
1142 side = SHEET_SIDE::TOP;
1143 pinPos.x = pos.x + halfX + pinOffset;
1144 pinPos.y = pos.y;
1145 portExtWireEndpoint = pinPos;
1146 portExtWireEndpoint.y -= portExtWireLength;
1147 }
1148 else if( port->side == "bottom" )
1149 {
1150 side = SHEET_SIDE::BOTTOM;
1151 pinPos.x = pos.x + halfX + pinOffset;
1152 pinPos.y = pos.y + size.y;
1153 portExtWireEndpoint = pinPos;
1154 portExtWireEndpoint.y += portExtWireLength;
1155 }
1156
1157 SCH_LINE* portExtWire = new SCH_LINE( pinPos, LAYER_WIRE );
1158 portExtWire->SetEndPoint( portExtWireEndpoint );
1159 currentScreen->Append( portExtWire );
1160
1162
1163 if( port->direction )
1164 {
1165 if( *port->direction == "in" )
1166 pinType = LABEL_FLAG_SHAPE::L_INPUT;
1167 else if( *port->direction == "out" )
1169 else if( *port->direction == "io" )
1170 pinType = LABEL_FLAG_SHAPE::L_BIDI;
1171 else if( *port->direction == "hiz" )
1173 else
1175
1176 // KiCad does not support passive, power, open collector, or no-connect sheet
1177 // pins that Eagle ports support. They are set to unspecified to minimize
1178 // ERC issues.
1179 }
1180
1181 SCH_SHEET_PIN* sheetPin = new SCH_SHEET_PIN( newSheet.get(), VECTOR2I( 0, 0 ), portName );
1182
1183 sheetPin->SetShape( pinType );
1184 sheetPin->SetPosition( pinPos );
1185 sheetPin->SetSide( side );
1186 newSheet->AddPin( sheetPin );
1187 }
1188
1189 wxString pageNo = wxString::Format( wxT( "%d" ), m_sheetIndex );
1190
1191 newSheet->SetParent( currentSheet );
1192 m_sheetPath.push_back( newSheet.get() );
1193 m_sheetPath.SetPageNumber( pageNo );
1194 currentScreen->Append( newSheet.release() );
1195
1196 m_modules.push_back( it->second.get() );
1197 m_moduleInstances.push_back( aModuleInstance.get() );
1198
1199 // Do not reload shared modules that are already loaded.
1200 if( isNewSchFile )
1201 {
1202 for( const std::unique_ptr<ESHEET>& esheet : it->second->sheets )
1203 loadSheet( esheet );
1204 }
1205 else
1206 {
1207 // Add instances for shared schematics.
1208 wxString refPrefix;
1209
1210 for( const EMODULEINST* emoduleInst : m_moduleInstances )
1211 {
1212 wxCHECK2( emoduleInst, continue );
1213
1214 refPrefix += emoduleInst->name + wxS( ":" );
1215 }
1216
1217 SCH_SCREEN* sharedScreen = m_sheetPath.LastScreen();
1218
1219 if( sharedScreen )
1220 {
1221 for( SCH_ITEM* schItem : sharedScreen->Items().OfType( SCH_SYMBOL_T ) )
1222 {
1223 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( schItem );
1224
1225 wxCHECK2( symbol && !symbol->GetInstances().empty(), continue );
1226
1227 SCH_SYMBOL_INSTANCE inst = symbol->GetInstances().at( 0 );
1228 wxString newReference = refPrefix + inst.m_Reference.AfterLast( ':' );
1229
1230 symbol->AddHierarchicalReference( m_sheetPath.Path(), newReference, inst.m_Unit );
1231 }
1232 }
1233 }
1234
1235 m_moduleInstances.pop_back();
1236 m_modules.pop_back();
1237 m_sheetPath.pop_back();
1238}
1239
1240
1241void SCH_IO_EAGLE::loadFrame( const std::unique_ptr<EFRAME>& aFrame,
1242 std::vector<SCH_ITEM*>& aItems )
1243{
1244 int xMin = aFrame->x1.ToSchUnits();
1245 int xMax = aFrame->x2.ToSchUnits();
1246 int yMin = -aFrame->y1.ToSchUnits();
1247 int yMax = -aFrame->y2.ToSchUnits();
1248
1249 if( xMin > xMax )
1250 std::swap( xMin, xMax );
1251
1252 if( yMin > yMax )
1253 std::swap( yMin, yMax );
1254
1255 SCH_SHAPE* lines = new SCH_SHAPE( SHAPE_T::POLY );
1256 lines->AddPoint( VECTOR2I( xMin, yMin ) );
1257 lines->AddPoint( VECTOR2I( xMax, yMin ) );
1258 lines->AddPoint( VECTOR2I( xMax, yMax ) );
1259 lines->AddPoint( VECTOR2I( xMin, yMax ) );
1260 lines->AddPoint( VECTOR2I( xMin, yMin ) );
1261 aItems.push_back( lines );
1262
1263 if( !( aFrame->border_left == false ) )
1264 {
1265 lines = new SCH_SHAPE( SHAPE_T::POLY );
1266 lines->AddPoint( VECTOR2I( xMin + schIUScale.MilsToIU( 150 ),
1267 yMin + schIUScale.MilsToIU( 150 ) ) );
1268 lines->AddPoint( VECTOR2I( xMin + schIUScale.MilsToIU( 150 ),
1269 yMax - schIUScale.MilsToIU( 150 ) ) );
1270 aItems.push_back( lines );
1271
1272 int i;
1273 int height = yMax - yMin;
1274 int x1 = xMin;
1275 int x2 = x1 + schIUScale.MilsToIU( 150 );
1276 int legendPosX = xMin + schIUScale.MilsToIU( 75 );
1277 double rowSpacing = height / double( aFrame->rows );
1278 double legendPosY = yMin + ( rowSpacing / 2 );
1279
1280 for( i = 1; i < aFrame->rows; i++ )
1281 {
1282 int newY = KiROUND( yMin + ( rowSpacing * (double) i ) );
1283 lines = new SCH_SHAPE( SHAPE_T::POLY );
1284 lines->AddPoint( VECTOR2I( x1, newY ) );
1285 lines->AddPoint( VECTOR2I( x2, newY ) );
1286 aItems.push_back( lines );
1287 }
1288
1289 char legendChar = 'A';
1290
1291 for( i = 0; i < aFrame->rows; i++ )
1292 {
1293 SCH_TEXT* legendText = new SCH_TEXT();
1294 legendText->SetPosition( VECTOR2I( legendPosX, KiROUND( legendPosY ) ) );
1297 legendText->SetText( wxString( legendChar ) );
1298 legendText->SetTextSize( VECTOR2I( schIUScale.MilsToIU( 90 ),
1299 schIUScale.MilsToIU( 100 ) ) );
1300 aItems.push_back( legendText );
1301 legendChar++;
1302 legendPosY += rowSpacing;
1303 }
1304 }
1305
1306 if( !( aFrame->border_right == false ) )
1307 {
1308 lines = new SCH_SHAPE( SHAPE_T::POLY );
1309 lines->AddPoint( VECTOR2I( xMax - schIUScale.MilsToIU( 150 ),
1310 yMin + schIUScale.MilsToIU( 150 ) ) );
1311 lines->AddPoint( VECTOR2I( xMax - schIUScale.MilsToIU( 150 ),
1312 yMax - schIUScale.MilsToIU( 150 ) ) );
1313 aItems.push_back( lines );
1314
1315 int i;
1316 int height = yMax - yMin;
1317 int x1 = xMax - schIUScale.MilsToIU( 150 );
1318 int x2 = xMax;
1319 int legendPosX = xMax - schIUScale.MilsToIU( 75 );
1320 double rowSpacing = height / double( aFrame->rows );
1321 double legendPosY = yMin + ( rowSpacing / 2 );
1322
1323 for( i = 1; i < aFrame->rows; i++ )
1324 {
1325 int newY = KiROUND( yMin + ( rowSpacing * (double) i ) );
1326 lines = new SCH_SHAPE( SHAPE_T::POLY );
1327 lines->AddPoint( VECTOR2I( x1, newY ) );
1328 lines->AddPoint( VECTOR2I( x2, newY ) );
1329 aItems.push_back( lines );
1330 }
1331
1332 char legendChar = 'A';
1333
1334 for( i = 0; i < aFrame->rows; i++ )
1335 {
1336 SCH_TEXT* legendText = new SCH_TEXT();
1337 legendText->SetPosition( VECTOR2I( legendPosX, KiROUND( legendPosY ) ) );
1340 legendText->SetText( wxString( legendChar ) );
1341 legendText->SetTextSize( VECTOR2I( schIUScale.MilsToIU( 90 ),
1342 schIUScale.MilsToIU( 100 ) ) );
1343 aItems.push_back( legendText );
1344 legendChar++;
1345 legendPosY += rowSpacing;
1346 }
1347 }
1348
1349 if( !( aFrame->border_top == false ) )
1350 {
1351 lines = new SCH_SHAPE( SHAPE_T::POLY );
1352 lines->AddPoint( VECTOR2I( xMax - schIUScale.MilsToIU( 150 ),
1353 yMin + schIUScale.MilsToIU( 150 ) ) );
1354 lines->AddPoint( VECTOR2I( xMin + schIUScale.MilsToIU( 150 ),
1355 yMin + schIUScale.MilsToIU( 150 ) ) );
1356 aItems.push_back( lines );
1357
1358 int i;
1359 int width = xMax - xMin;
1360 int y1 = yMin;
1361 int y2 = yMin + schIUScale.MilsToIU( 150 );
1362 int legendPosY = yMin + schIUScale.MilsToIU( 75 );
1363 double columnSpacing = width / double( aFrame->columns );
1364 double legendPosX = xMin + ( columnSpacing / 2 );
1365
1366 for( i = 1; i < aFrame->columns; i++ )
1367 {
1368 int newX = KiROUND( xMin + ( columnSpacing * (double) i ) );
1369 lines = new SCH_SHAPE( SHAPE_T::POLY );
1370 lines->AddPoint( VECTOR2I( newX, y1 ) );
1371 lines->AddPoint( VECTOR2I( newX, y2 ) );
1372 aItems.push_back( lines );
1373 }
1374
1375 char legendChar = '1';
1376
1377 for( i = 0; i < aFrame->columns; i++ )
1378 {
1379 SCH_TEXT* legendText = new SCH_TEXT();
1380 legendText->SetPosition( VECTOR2I( KiROUND( legendPosX ), legendPosY ) );
1383 legendText->SetText( wxString( legendChar ) );
1384 legendText->SetTextSize( VECTOR2I( schIUScale.MilsToIU( 90 ),
1385 schIUScale.MilsToIU( 100 ) ) );
1386 aItems.push_back( legendText );
1387 legendChar++;
1388 legendPosX += columnSpacing;
1389 }
1390 }
1391
1392 if( !( aFrame->border_bottom == false ) )
1393 {
1394 lines = new SCH_SHAPE( SHAPE_T::POLY );
1395 lines->AddPoint( VECTOR2I( xMax - schIUScale.MilsToIU( 150 ),
1396 yMax - schIUScale.MilsToIU( 150 ) ) );
1397 lines->AddPoint( VECTOR2I( xMin + schIUScale.MilsToIU( 150 ),
1398 yMax - schIUScale.MilsToIU( 150 ) ) );
1399 aItems.push_back( lines );
1400
1401 int i;
1402 int width = xMax - xMin;
1403 int y1 = yMax - schIUScale.MilsToIU( 150 );
1404 int y2 = yMax;
1405 int legendPosY = yMax - schIUScale.MilsToIU( 75 );
1406 double columnSpacing = width / double( aFrame->columns );
1407 double legendPosX = xMin + ( columnSpacing / 2 );
1408
1409 for( i = 1; i < aFrame->columns; i++ )
1410 {
1411 int newX = KiROUND( xMin + ( columnSpacing * (double) i ) );
1412 lines = new SCH_SHAPE( SHAPE_T::POLY );
1413 lines->AddPoint( VECTOR2I( newX, y1 ) );
1414 lines->AddPoint( VECTOR2I( newX, y2 ) );
1415 aItems.push_back( lines );
1416 }
1417
1418 char legendChar = '1';
1419
1420 for( i = 0; i < aFrame->columns; i++ )
1421 {
1422 SCH_TEXT* legendText = new SCH_TEXT();
1423 legendText->SetPosition( VECTOR2I( KiROUND( legendPosX ), legendPosY ) );
1426 legendText->SetText( wxString( legendChar ) );
1427 legendText->SetTextSize( VECTOR2I( schIUScale.MilsToIU( 90 ),
1428 schIUScale.MilsToIU( 100 ) ) );
1429 aItems.push_back( legendText );
1430 legendChar++;
1431 legendPosX += columnSpacing;
1432 }
1433 }
1434}
1435
1436
1437void SCH_IO_EAGLE::loadSegments( const std::vector<std::unique_ptr<ESEGMENT>>& aSegments,
1438 const wxString& netName,
1439 const wxString& aNetClass )
1440{
1441 // Loop through all segments
1442 SCH_SCREEN* screen = getCurrentScreen();
1443
1444 wxCHECK( screen, /* void */ );
1445
1446 size_t segmentCount = aSegments.size();
1447
1448 for( const std::unique_ptr<ESEGMENT>& esegment : aSegments )
1449 {
1450 bool labelled = false; // has a label been added to this continuously connected segment
1451 bool firstWireFound = false;
1452 SEG firstWire;
1453
1454 m_segments.emplace_back();
1455 SEG_DESC& segDesc = m_segments.back();
1456
1457 for( const std::unique_ptr<EWIRE>& ewire : esegment->wires )
1458 {
1459 // TODO: Check how intersections used in adjustNetLabels should be
1460 // calculated - for now we pretend that all wires are line segments.
1461 SEG thisWire;
1462 SCH_ITEM* wire = loadWire( ewire, thisWire );
1463 m_connPoints[thisWire.A].emplace( wire );
1464 m_connPoints[thisWire.B].emplace( wire );
1465
1466 if( !firstWireFound )
1467 {
1468 firstWire = thisWire;
1469 firstWireFound = true;
1470 }
1471
1472 // Test for intersections with other wires
1473 for( SEG_DESC& desc : m_segments )
1474 {
1475 if( !desc.labels.empty() && desc.labels.front()->GetText() == netName )
1476 continue; // no point in saving intersections of the same net
1477
1478 for( const SEG& seg : desc.segs )
1479 {
1480 OPT_VECTOR2I intersection = thisWire.Intersect( seg, true );
1481
1482 if( intersection )
1483 m_wireIntersections.push_back( *intersection );
1484 }
1485 }
1486
1487 segDesc.segs.push_back( thisWire );
1488 screen->Append( wire );
1489 }
1490
1491 for( const std::unique_ptr<EJUNCTION>& ejunction : esegment->junctions )
1492 screen->Append( loadJunction( ejunction ) );
1493
1494 for( const std::unique_ptr<ELABEL>& elabel : esegment->labels )
1495 {
1496 SCH_TEXT* label = loadLabel( elabel, netName );
1497 screen->Append( label );
1498
1499 wxASSERT( segDesc.labels.empty()
1500 || segDesc.labels.front()->GetText() == label->GetText() );
1501
1502 segDesc.labels.push_back( label );
1503 labelled = true;
1504 }
1505
1506 for( const std::unique_ptr<EPINREF>& epinref : esegment->pinRefs )
1507 {
1508 wxString part = epinref->part;
1509 wxString pin = epinref->pin;
1510
1511 auto powerPort = m_powerPorts.find( wxT( "#" ) + part );
1512
1513 if( powerPort != m_powerPorts.end()
1514 && powerPort->second == EscapeString( pin, CTX_NETNAME ) )
1515 {
1516 labelled = true;
1517 }
1518 }
1519
1520 // Add a small label to the net segment if it hasn't been labeled already or is not
1521 // connect to a power symbol with a pin on the same net. This preserves the named net
1522 // feature of Eagle schematics.
1523 if( !labelled && firstWireFound )
1524 {
1525 std::unique_ptr<SCH_LABEL_BASE> label;
1526
1527 // Add a global label if the net appears on more than one Eagle sheet
1528 if( m_netCounts[netName.ToStdString()] > 1 )
1529 label.reset( new SCH_GLOBALLABEL );
1530 else if( segmentCount > 1 )
1531 label.reset( new SCH_LABEL );
1532
1533 if( label )
1534 {
1535 label->SetPosition( firstWire.A );
1536 label->SetText( escapeName( netName ) );
1537 label->SetTextSize( VECTOR2I( schIUScale.MilsToIU( 40 ),
1538 schIUScale.MilsToIU( 40 ) ) );
1539
1540 if( firstWire.B.x > firstWire.A.x )
1541 label->SetSpinStyle( SPIN_STYLE::LEFT );
1542 else
1543 label->SetSpinStyle( SPIN_STYLE::RIGHT );
1544
1545 screen->Append( label.release() );
1546 }
1547 }
1548 }
1549}
1550
1551
1552SCH_SHAPE* SCH_IO_EAGLE::loadPolyLine( const std::unique_ptr<EPOLYGON>& aPolygon )
1553{
1554 std::unique_ptr<SCH_SHAPE> poly = std::make_unique<SCH_SHAPE>( SHAPE_T::POLY );
1555 VECTOR2I pt, prev_pt;
1556 opt_double prev_curve;
1557
1558 for( const std::unique_ptr<EVERTEX>& evertex : aPolygon->vertices )
1559 {
1560 pt = VECTOR2I( evertex->x.ToSchUnits(), -evertex->y.ToSchUnits() );
1561
1562 if( prev_curve )
1563 {
1564 SHAPE_ARC arc;
1565 arc.ConstructFromStartEndAngle( prev_pt, pt, -EDA_ANGLE( *prev_curve, DEGREES_T ) );
1566 poly->GetPolyShape().Append( arc, -1, -1, ARC_ACCURACY );
1567 }
1568 else
1569 {
1570 poly->AddPoint( pt );
1571 }
1572
1573 prev_pt = pt;
1574 prev_curve = evertex->curve;
1575 }
1576
1577 poly->SetLayer( kiCadLayer( aPolygon->layer ) );
1578 poly->SetStroke( STROKE_PARAMS( aPolygon->width.ToSchUnits(), LINE_STYLE::SOLID ) );
1579 poly->SetFillMode( FILL_T::FILLED_SHAPE );
1580
1581 return poly.release();
1582}
1583
1584
1585SCH_ITEM* SCH_IO_EAGLE::loadWire( const std::unique_ptr<EWIRE>& aWire, SEG& endpoints )
1586{
1587 VECTOR2I start, end;
1588
1589 start.x = aWire->x1.ToSchUnits();
1590 start.y = -aWire->y1.ToSchUnits();
1591 end.x = aWire->x2.ToSchUnits();
1592 end.y = -aWire->y2.ToSchUnits();
1593
1594 // For segment wires.
1595 endpoints = SEG( start, end );
1596
1597 if( aWire->curve )
1598 {
1599 std::unique_ptr<SCH_SHAPE> arc = std::make_unique<SCH_SHAPE>( SHAPE_T::ARC );
1600
1601 VECTOR2I center = ConvertArcCenter( start, end, *aWire->curve );
1602 arc->SetCenter( center );
1603 arc->SetStart( start );
1604
1605 // KiCad rotates the other way.
1606 arc->SetArcAngleAndEnd( -EDA_ANGLE( *aWire->curve, DEGREES_T ), true );
1607 arc->SetLayer( kiCadLayer( aWire->layer ) );
1608 arc->SetStroke( STROKE_PARAMS( aWire->width.ToSchUnits(), LINE_STYLE::SOLID ) );
1609
1610 return arc.release();
1611 }
1612 else
1613 {
1614 std::unique_ptr<SCH_LINE> line = std::make_unique<SCH_LINE>();
1615
1616 line->SetStartPoint( start );
1617 line->SetEndPoint( end );
1618 line->SetLayer( kiCadLayer( aWire->layer ) );
1619 line->SetStroke( STROKE_PARAMS( aWire->width.ToSchUnits(), LINE_STYLE::SOLID ) );
1620
1621 return line.release();
1622 }
1623}
1624
1625
1626SCH_SHAPE* SCH_IO_EAGLE::loadCircle( const std::unique_ptr<ECIRCLE>& aCircle )
1627{
1628 std::unique_ptr<SCH_SHAPE> circle = std::make_unique<SCH_SHAPE>( SHAPE_T::CIRCLE );
1629 VECTOR2I center( aCircle->x.ToSchUnits(), -aCircle->y.ToSchUnits() );
1630
1631 circle->SetLayer( kiCadLayer( aCircle->layer ) );
1632 circle->SetPosition( center );
1633 circle->SetEnd( VECTOR2I( center.x + aCircle->radius.ToSchUnits(), center.y ) );
1634 circle->SetStroke( STROKE_PARAMS( aCircle->width.ToSchUnits(), LINE_STYLE::SOLID ) );
1635
1636 return circle.release();
1637}
1638
1639
1640SCH_SHAPE* SCH_IO_EAGLE::loadRectangle( const std::unique_ptr<ERECT>& aRectangle )
1641{
1642 std::unique_ptr<SCH_SHAPE> rectangle = std::make_unique<SCH_SHAPE>( SHAPE_T::RECTANGLE );
1643
1644 rectangle->SetLayer( kiCadLayer( aRectangle->layer ) );
1645 rectangle->SetPosition( VECTOR2I( aRectangle->x1.ToSchUnits(), -aRectangle->y1.ToSchUnits() ) );
1646 rectangle->SetEnd( VECTOR2I( aRectangle->x2.ToSchUnits(), -aRectangle->y2.ToSchUnits() ) );
1647
1648 if( aRectangle->rot )
1649 {
1650 VECTOR2I pos( rectangle->GetPosition() );
1651 VECTOR2I end( rectangle->GetEnd() );
1652 VECTOR2I center( rectangle->GetCenter() );
1653
1654 RotatePoint( pos, center, EDA_ANGLE( aRectangle->rot->degrees, DEGREES_T ) );
1655 RotatePoint( end, center, EDA_ANGLE( aRectangle->rot->degrees, DEGREES_T ) );
1656
1657 rectangle->SetPosition( pos );
1658 rectangle->SetEnd( end );
1659 }
1660
1661 // Eagle rectangles are filled by definition.
1662 rectangle->SetFillMode( FILL_T::FILLED_SHAPE );
1663
1664 return rectangle.release();
1665}
1666
1667
1668SCH_JUNCTION* SCH_IO_EAGLE::loadJunction( const std::unique_ptr<EJUNCTION>& aJunction )
1669{
1670 std::unique_ptr<SCH_JUNCTION> junction = std::make_unique<SCH_JUNCTION>();
1671
1672 VECTOR2I pos( aJunction->x.ToSchUnits(), -aJunction->y.ToSchUnits() );
1673
1674 junction->SetPosition( pos );
1675
1676 return junction.release();
1677}
1678
1679
1680SCH_TEXT* SCH_IO_EAGLE::loadLabel( const std::unique_ptr<ELABEL>& aLabel,
1681 const wxString& aNetName )
1682{
1683 VECTOR2I elabelpos( aLabel->x.ToSchUnits(), -aLabel->y.ToSchUnits() );
1684
1685 // Determine if the label is local or global depending on
1686 // the number of sheets the net appears in
1687 bool global = m_netCounts[aNetName] > 1;
1688 std::unique_ptr<SCH_LABEL_BASE> label;
1689
1690 VECTOR2I textSize = KiROUND( aLabel->size.ToSchUnits() * 0.7, aLabel->size.ToSchUnits() * 0.7 );
1691
1692 if( m_modules.size() )
1693 {
1694 if( m_modules.back()->ports.find( aNetName ) != m_modules.back()->ports.end() )
1695 {
1696 label = std::make_unique<SCH_HIERLABEL>();
1697 label->SetText( escapeName( aNetName ) );
1698
1699 const auto it = m_modules.back()->ports.find( aNetName );
1700
1701 LABEL_SHAPE type;
1702
1703 if( it->second->direction )
1704 {
1705 wxString direction = *it->second->direction;
1706
1707 if( direction == "in" )
1709 else if( direction == "out" )
1711 else if( direction == "io" )
1713 else if( direction == "hiz" )
1715 else
1717
1718 // KiCad does not support passive, power, open collector, or no-connect sheet
1719 // pins that Eagle ports support. They are set to unspecified to minimize
1720 // ERC issues.
1721 label->SetLabelShape( type );
1722 }
1723 }
1724 else
1725 {
1726 label = std::make_unique<SCH_LABEL>();
1727 label->SetText( escapeName( aNetName ) );
1728 }
1729 }
1730 else if( global )
1731 {
1732 label = std::make_unique<SCH_GLOBALLABEL>();
1733 label->SetText( escapeName( aNetName ) );
1734 }
1735 else
1736 {
1737 label = std::make_unique<SCH_LABEL>();
1738 label->SetText( escapeName( aNetName ) );
1739 }
1740
1741 label->SetPosition( elabelpos );
1742 label->SetTextSize( textSize );
1743 label->SetSpinStyle( SPIN_STYLE::RIGHT );
1744
1745 if( aLabel->rot )
1746 {
1747 for( int i = 0; i < KiROUND( aLabel->rot->degrees / 90.0 ) %4; ++i )
1748 label->Rotate90( false );
1749
1750 if( aLabel->rot->mirror )
1751 label->MirrorSpinStyle( false );
1752 }
1753
1754 return label.release();
1755}
1756
1757
1758std::pair<VECTOR2I, const SEG*>
1760 const std::vector<SEG>& aLines ) const
1761{
1762 VECTOR2I nearestPoint;
1763 const SEG* nearestLine = nullptr;
1764
1765 double d, mindistance = std::numeric_limits<double>::max();
1766
1767 // Find the nearest start, middle or end of a line from the list of lines.
1768 for( const SEG& line : aLines )
1769 {
1770 VECTOR2I testpoint = line.A;
1771 d = aPoint.Distance( testpoint );
1772
1773 if( d < mindistance )
1774 {
1775 mindistance = d;
1776 nearestPoint = testpoint;
1777 nearestLine = &line;
1778 }
1779
1780 testpoint = line.Center();
1781 d = aPoint.Distance( testpoint );
1782
1783 if( d < mindistance )
1784 {
1785 mindistance = d;
1786 nearestPoint = testpoint;
1787 nearestLine = &line;
1788 }
1789
1790 testpoint = line.B;
1791 d = aPoint.Distance( testpoint );
1792
1793 if( d < mindistance )
1794 {
1795 mindistance = d;
1796 nearestPoint = testpoint;
1797 nearestLine = &line;
1798 }
1799 }
1800
1801 return std::make_pair( nearestPoint, nearestLine );
1802}
1803
1804
1805void SCH_IO_EAGLE::loadInstance( const std::unique_ptr<EINSTANCE>& aInstance,
1806 const std::map<wxString, std::unique_ptr<EPART>>& aParts )
1807{
1808 wxCHECK( aInstance, /* void */ );
1809
1810 SCH_SCREEN* screen = getCurrentScreen();
1811
1812 wxCHECK( screen, /* void */ );
1813
1814 const auto partIt = aParts.find( aInstance->part );
1815
1816 if( partIt == aParts.end() )
1817 {
1818 Report( wxString::Format( _( "Error parsing Eagle file. Could not find '%s' "
1819 "instance but it is referenced in the schematic." ),
1820 aInstance->part ),
1822
1823 return;
1824 }
1825
1826 const std::unique_ptr<EPART>& epart = partIt->second;
1827
1828 wxString libName = epart->library;
1829
1830 // Correctly handle versioned libraries.
1831 if( epart->libraryUrn )
1832 libName += wxS( "_" ) + epart->libraryUrn->assetId;
1833
1834 wxString gatename = epart->deviceset + wxS( "_" ) + epart->device + wxS( "_" ) +
1835 aInstance->gate;
1836 wxString symbolname = wxString( epart->deviceset + epart->device );
1837 symbolname.Replace( wxT( "*" ), wxEmptyString );
1838 wxString kisymbolname = EscapeString( symbolname, CTX_LIBID );
1839
1840 // Eagle schematics can have multiple libraries containing symbols with duplicate symbol
1841 // names. Because this parser stores all of the symbols in a single library, the
1842 // loadSymbol() function, prefixed the original Eagle library name to the symbol name
1843 // in case of a name clash. Check for the prefixed symbol first. This ensures that
1844 // the correct library symbol gets mapped on load.
1845 wxString altSymbolName = libName + wxT( "_" ) + symbolname;
1846 altSymbolName = EscapeString( altSymbolName, CTX_LIBID );
1847
1848 wxString libIdSymbolName = altSymbolName;
1849
1850 const auto libIt = m_eagleLibs.find( libName );
1851
1852 if( libIt == m_eagleLibs.end() )
1853 {
1854 Report( wxString::Format( wxS( "Eagle library '%s' not found while looking up symbol for "
1855 "deviceset '%s', device '%s', and gate '%s." ),
1856 libName, epart->deviceset, epart->device, aInstance->gate ) );
1857 return;
1858 }
1859
1860 const auto gateIt = libIt->second.GateToUnitMap.find( gatename );
1861
1862 if( gateIt == libIt->second.GateToUnitMap.end() )
1863 {
1864 Report( wxString::Format( wxS( "Symbol not found for deviceset '%s', device '%s', and "
1865 "gate '%s in library '%s'." ),
1866 epart->deviceset, epart->device, aInstance->gate, libName ) );
1867 return;
1868 }
1869
1870 int unit = gateIt->second;
1871
1872 wxString package;
1873 EAGLE_LIBRARY* elib = &m_eagleLibs[libName];
1874
1875 auto p = elib->package.find( kisymbolname );
1876
1877 if( p != elib->package.end() )
1878 package = p->second;
1879
1880 // set properties to prevent save file on every symbol save
1881 std::map<std::string, UTF8> properties;
1882 properties.emplace( SCH_IO_KICAD_SEXPR::PropBuffering, wxEmptyString );
1883
1884 LIB_SYMBOL* part = m_pi->LoadSymbol( getLibFileName().GetFullPath(), altSymbolName, &properties );
1885
1886 if( !part )
1887 {
1888 part = m_pi->LoadSymbol( getLibFileName().GetFullPath(), kisymbolname, &properties );
1889 libIdSymbolName = kisymbolname;
1890 }
1891
1892 if( !part )
1893 {
1894 Report( wxString::Format( _( "Could not find '%s' in the imported library." ),
1895 UnescapeString( kisymbolname ) ),
1897 return;
1898 }
1899
1900 LIB_ID libId( getLibName(), libIdSymbolName );
1901 std::unique_ptr<SCH_SYMBOL> symbol = std::make_unique<SCH_SYMBOL>();
1902 symbol->SetLibId( libId );
1903 symbol->SetUnit( unit );
1904 symbol->SetPosition( VECTOR2I( aInstance->x.ToSchUnits(), -aInstance->y.ToSchUnits() ) );
1905
1906 // assume that footprint library is identical to project name
1907 if( !package.IsEmpty() )
1908 {
1909 wxString footprint = m_schematic->Project().GetProjectName() + wxT( ":" ) + package;
1910 symbol->GetField( FIELD_T::FOOTPRINT )->SetText( footprint );
1911 }
1912
1913 if( aInstance->rot )
1914 {
1915 symbol->SetOrientation( kiCadComponentRotation( aInstance->rot->degrees ) );
1916
1917 if( aInstance->rot->mirror )
1918 symbol->MirrorHorizontally( aInstance->x.ToSchUnits() );
1919 }
1920
1921 std::vector<SCH_FIELD*> partFields;
1922 part->GetFields( partFields );
1923
1924 for( const SCH_FIELD* partField : partFields )
1925 {
1926 SCH_FIELD* symbolField;
1927
1928 if( partField->IsMandatory() )
1929 symbolField = symbol->GetField( partField->GetId() );
1930 else
1931 symbolField = symbol->GetField( partField->GetName() );
1932
1933 wxCHECK2( symbolField, continue );
1934
1935 symbolField->ImportValues( *partField );
1936 symbolField->SetTextPos( symbol->GetPosition() + partField->GetTextPos() );
1937 }
1938
1939 // If there is no footprint assigned, then prepend the reference value
1940 // with a hash character to mute netlist updater complaints
1941 wxString reference = package.IsEmpty() ? '#' + aInstance->part : aInstance->part;
1942
1943 // reference must end with a number but EAGLE does not enforce this
1944 if( reference.find_last_not_of( wxT( "0123456789" ) ) == ( reference.Length()-1 ) )
1945 reference.Append( wxT( "0" ) );
1946
1947 // EAGLE allows references to be single digits. This breaks KiCad netlisting, which requires
1948 // parts to have non-digit + digit annotation. If the reference begins with a number,
1949 // we prepend 'UNK' (unknown) for the symbol designator
1950 if( reference.find_first_not_of( wxT( "0123456789" ) ) != 0 )
1951 reference.Prepend( wxT( "UNK" ) );
1952
1953 // EAGLE allows designator to start with # but that is used in KiCad
1954 // for symbols which do not have a footprint
1955 if( aInstance->part.find_first_not_of( wxT( "#" ) ) != 0 )
1956 reference.Prepend( wxT( "UNK" ) );
1957
1958 SCH_FIELD* referenceField = symbol->GetField( FIELD_T::REFERENCE );
1959 referenceField->SetText( reference );
1960
1961 SCH_FIELD* valueField = symbol->GetField( FIELD_T::VALUE );
1962 bool userValue = m_userValue.at( libIdSymbolName );
1963
1964 if( part->GetUnitCount() > 1 )
1965 {
1966 getEagleSymbolFieldAttributes( aInstance, wxS( ">NAME" ), referenceField );
1967 getEagleSymbolFieldAttributes( aInstance, wxS( ">VALUE" ), valueField );
1968 }
1969
1970 if( epart->value && !epart->value.CGet().IsEmpty() )
1971 {
1972 valueField->SetText( *epart->value );
1973 }
1974 else
1975 {
1976 valueField->SetText( kisymbolname );
1977
1978 if( userValue )
1979 valueField->SetVisible( false );
1980 }
1981
1982 for( const auto& [ attrName, attr ] : epart->attributes )
1983 {
1984 SCH_FIELD newField( symbol.get(), FIELD_T::USER );
1985
1986 newField.SetName( attrName );
1987
1988 if( !symbol->GetFields().empty() )
1989 newField.SetTextPos( symbol->GetFields().back().GetPosition() );
1990
1991 if( attr->value )
1992 newField.SetText( *attr->value );
1993
1994 newField.SetVisible( ( attr->display == EATTR::Off ) ? false : true );
1995
1996 symbol->AddField( newField );
1997 }
1998
1999 for( const auto& [variantName, variant] : epart->variants )
2000 {
2001 SCH_FIELD* field = symbol->AddField( *symbol->GetField( FIELD_T::VALUE ) );
2002 field->SetName( wxT( "VARIANT_" ) + variant->name );
2003
2004 if( variant->value )
2005 field->SetText( *variant->value );
2006
2007 field->SetVisible( false );
2008 }
2009
2010 bool valueAttributeFound = false;
2011 bool nameAttributeFound = false;
2012
2013 // Parse attributes for the instance
2014 for( auto& [name, eattr] : aInstance->attributes )
2015 {
2016 SCH_FIELD* field = nullptr;
2017
2018 if( eattr->name.Lower() == wxT( "name" ) )
2019 {
2020 field = symbol->GetField( FIELD_T::REFERENCE );
2021 nameAttributeFound = true;
2022 }
2023 else if( eattr->name.Lower() == wxT( "value" ) )
2024 {
2025 field = symbol->GetField( FIELD_T::VALUE );
2026 valueAttributeFound = true;
2027 }
2028 else
2029 {
2030 field = symbol->GetField( eattr->name );
2031
2032 if( field )
2033 field->SetVisible( false );
2034 }
2035
2036 if( field )
2037 {
2038 field->SetPosition( VECTOR2I( eattr->x->ToSchUnits(), -eattr->y->ToSchUnits() ) );
2039 int align = eattr->align ? *eattr->align : ETEXT::BOTTOM_LEFT;
2040 int absdegrees = eattr->rot ? eattr->rot->degrees : 0;
2041 bool mirror = eattr->rot ? eattr->rot->mirror : false;
2042
2043 if( aInstance->rot && aInstance->rot->mirror )
2044 mirror = !mirror;
2045
2046 bool spin = eattr->rot ? eattr->rot->spin : false;
2047
2048 if( eattr->display == EATTR::Off || eattr->display == EATTR::NAME )
2049 field->SetVisible( false );
2050
2051 int rotation = aInstance->rot ? aInstance->rot->degrees : 0;
2052 int reldegrees = ( absdegrees - rotation + 360.0 );
2053 reldegrees %= 360;
2054
2055 eagleToKicadAlignment( field, align, reldegrees, mirror, spin, absdegrees );
2056 }
2057 }
2058
2059 // Use the instance attribute to determine the reference and value field visibility.
2060 if( aInstance->smashed && aInstance->smashed.Get() )
2061 {
2062 symbol->GetField( FIELD_T::VALUE )->SetVisible( valueAttributeFound );
2063 symbol->GetField( FIELD_T::REFERENCE )->SetVisible( nameAttributeFound );
2064 }
2065
2066 // Eagle has a brain dead module reference scheme where the module names separated by colons
2067 // are prefixed to the symbol references. This will get blown away in KiCad the first time
2068 // any annotation is performed. It is required for the initial synchronization between the
2069 // schematic and the board.
2070 wxString refPrefix;
2071
2072 for( const EMODULEINST* emoduleInst : m_moduleInstances )
2073 {
2074 wxCHECK2( emoduleInst, continue );
2075
2076 refPrefix += emoduleInst->name + wxS( ":" );
2077 }
2078
2079 symbol->AddHierarchicalReference( m_sheetPath.Path(), refPrefix + reference, unit );
2080
2081 // Cache the lib symbol so pin positions are available for connection-point tracking.
2082 // Use the already-loaded `part` directly rather than re-fetching through the adapter,
2083 // because the .kicad_sym library is still buffered in m_pi and has not yet been saved to
2084 // disk at the time loadInstance runs.
2085 symbol->SetLibSymbol( new LIB_SYMBOL( *part ) );
2086
2087 for( const SCH_PIN* pin : symbol->GetLibPins() )
2088 m_connPoints[symbol->GetPinPhysicalPosition( pin )].emplace( pin );
2089
2090 if( part->IsGlobalPower() )
2091 m_powerPorts[ reference ] = symbol->GetField( FIELD_T::VALUE )->GetText();
2092
2093 symbol->ClearFlags();
2094
2095 screen->Append( symbol.release() );
2096}
2097
2098
2100{
2101 wxCHECK( aLibrary && aEagleLibrary, nullptr );
2102
2103 // Loop through the device sets and load each of them
2104 for( const auto& [name, edeviceset] : aLibrary->devicesets )
2105 {
2106 // Get Device set information
2107 wxString prefix = edeviceset->prefix ? edeviceset->prefix.Get() : wxString( wxT( "" ) );
2108 wxString deviceSetDescr;
2109
2110 if( edeviceset->description )
2111 deviceSetDescr = convertDescription( UnescapeHTML( edeviceset->description->text ) );
2112
2113 // For each device in the device set:
2114 for( const std::unique_ptr<EDEVICE>& edevice : edeviceset->devices )
2115 {
2116 // Create symbol name from deviceset and device names.
2117 wxString symbolName = edeviceset->name + edevice->name;
2118 symbolName.Replace( wxT( "*" ), wxEmptyString );
2119 wxASSERT( !symbolName.IsEmpty() );
2120 symbolName = EscapeString( symbolName, CTX_LIBID );
2121
2122 if( edevice->package )
2123 aEagleLibrary->package[symbolName] = edevice->package.Get();
2124
2125 // Create KiCad symbol.
2126 std::unique_ptr<LIB_SYMBOL> libSymbol = std::make_unique<LIB_SYMBOL>( symbolName );
2127
2128 // Process each gate in the deviceset for this device.
2129 int gate_count = static_cast<int>( edeviceset->gates.size() );
2130 libSymbol->SetUnitCount( gate_count, true );
2131 libSymbol->LockUnits( true );
2132
2133 SCH_FIELD* reference = libSymbol->GetField( FIELD_T::REFERENCE );
2134
2135 if( prefix.length() == 0 )
2136 {
2137 reference->SetVisible( false );
2138 }
2139 else
2140 {
2141 // If there is no footprint assigned, then prepend the reference value
2142 // with a hash character to mute netlist updater complaints
2143 reference->SetText( edevice->package ? prefix : '#' + prefix );
2144 }
2145
2146 libSymbol->GetValueField().SetVisible( true );
2147
2148 int gateindex = 1;
2149 bool ispower = false;
2150
2151 for( const auto& [gateName, egate] : edeviceset->gates )
2152 {
2153 const auto it = aLibrary->symbols.find( egate->symbol );
2154
2155 if( it == aLibrary->symbols.end() )
2156 {
2157 Report( wxString::Format( wxS( "Eagle symbol '%s' not found in library '%s'." ),
2158 egate->symbol, aLibrary->GetName() ) );
2159 continue;
2160 }
2161
2162 wxString gateMapName = edeviceset->name + wxS( "_" ) + edevice->name +
2163 wxS( "_" ) + egate->name;
2164 aEagleLibrary->GateToUnitMap[gateMapName] = gateindex;
2165 ispower = loadSymbol( it->second, libSymbol, edevice, gateindex, egate->name );
2166
2167 gateindex++;
2168 }
2169
2170 libSymbol->SetUnitCount( gate_count, true );
2171
2172 if( gate_count == 1 && ispower )
2173 libSymbol->SetGlobalPower();
2174
2175 // Don't set the footprint field if no package is defined in the Eagle schematic.
2176 if( edevice->package )
2177 {
2178 wxString libName;
2179
2180 if( m_schematic )
2181 {
2182 // assume that footprint library is identical to project name
2183 libName = m_schematic->Project().GetProjectName();
2184 }
2185 else
2186 {
2187 libName = m_libName;
2188 }
2189
2190 wxString packageString = libName + wxT( ":" ) + aEagleLibrary->package[symbolName];
2191
2192 libSymbol->GetFootprintField().SetText( packageString );
2193 }
2194
2195 wxString libName = libSymbol->GetName();
2196 libSymbol->SetName( libName );
2197 libSymbol->SetDescription( deviceSetDescr );
2198
2199 if( m_pi )
2200 {
2201 // If duplicate symbol names exist in multiple Eagle symbol libraries, prefix the
2202 // Eagle symbol library name to the symbol which should ensure that it is unique.
2203 try
2204 {
2205 if( m_pi->LoadSymbol( getLibFileName().GetFullPath(), libName ) )
2206 {
2207 libName = aEagleLibrary->name + wxT( "_" ) + libName;
2208 libName = EscapeString( libName, CTX_LIBID );
2209 libSymbol->SetName( libName );
2210 }
2211
2212 // set properties to prevent save file on every symbol save
2213 std::map<std::string, UTF8> properties;
2214 properties.emplace( SCH_IO_KICAD_SEXPR::PropBuffering, wxEmptyString );
2215
2216 m_pi->SaveSymbol( getLibFileName().GetFullPath(), new LIB_SYMBOL( *libSymbol.get() ),
2217 &properties );
2218 }
2219 catch(...)
2220 {
2221 // A library symbol cannot be loaded for some reason.
2222 // Just skip this symbol creating an issue.
2223 // The issue will be reported later by the Reporter
2224 }
2225 }
2226
2227 aEagleLibrary->KiCadSymbols[ libName ] = std::move( libSymbol );
2228
2229 // Store information on whether the value of FIELD_T::VALUE for a part should be
2230 // part/@value or part/@deviceset + part/@device.
2231 m_userValue.emplace( std::make_pair( libName, edeviceset->uservalue == true ) );
2232 }
2233 }
2234
2235 return aEagleLibrary;
2236}
2237
2238
2239bool SCH_IO_EAGLE::loadSymbol( const std::unique_ptr<ESYMBOL>& aEsymbol,
2240 std::unique_ptr<LIB_SYMBOL>& aSymbol,
2241 const std::unique_ptr<EDEVICE>& aDevice, int aGateNumber,
2242 const wxString& aGateName )
2243{
2244 wxCHECK( aEsymbol && aSymbol && aDevice, false );
2245
2246 std::vector<SCH_ITEM*> items;
2247
2248 bool showRefDes = false;
2249 bool showValue = false;
2250 bool ispower = false;
2251 int pincount = 0;
2252
2253 for( const std::unique_ptr<ECIRCLE>& ecircle : aEsymbol->circles )
2254 aSymbol->AddDrawItem( loadSymbolCircle( aSymbol, ecircle, aGateNumber ) );
2255
2256 for( const std::unique_ptr<EPIN>& epin : aEsymbol->pins )
2257 {
2258 std::unique_ptr<SCH_PIN> pin( loadPin( aSymbol, epin, aGateNumber ) );
2259 pincount++;
2260
2261 pin->SetType( ELECTRICAL_PINTYPE::PT_BIDI );
2262
2263 if( epin->direction )
2264 {
2265 for( const auto& pinDir : pinDirectionsMap )
2266 {
2267 if( epin->direction->Lower() == pinDir.first )
2268 {
2269 pin->SetType( pinDir.second );
2270
2271 if( pinDir.first == wxT( "sup" ) ) // power supply symbol
2272 ispower = true;
2273
2274 break;
2275 }
2276 }
2277
2278 }
2279
2280 if( aDevice->connects.size() != 0 )
2281 {
2282 for( const std::unique_ptr<ECONNECT>& connect : aDevice->connects )
2283 {
2284 if( connect->gate == aGateName && pin->GetName() == connect->pin )
2285 {
2286 wxArrayString pads = wxSplit( wxString( connect->pad ), ' ' );
2287
2288 pin->SetUnit( aGateNumber );
2289 pin->SetName( escapeName( pin->GetName() ) );
2290
2291 if( pads.GetCount() > 1 )
2292 {
2293 pin->SetNumberTextSize( 0 );
2294 }
2295
2296 for( unsigned i = 0; i < pads.GetCount(); i++ )
2297 {
2298 SCH_PIN* apin = new SCH_PIN( *pin );
2299
2300 wxString padname( pads[i] );
2301 apin->SetNumber( padname );
2302 aSymbol->AddDrawItem( apin );
2303 }
2304
2305 break;
2306 }
2307 }
2308 }
2309 else
2310 {
2311 pin->SetUnit( aGateNumber );
2312 pin->SetNumber( wxString::Format( wxT( "%i" ), pincount ) );
2313 aSymbol->AddDrawItem( pin.release() );
2314 }
2315 }
2316
2317 for( const std::unique_ptr<EPOLYGON>& epolygon : aEsymbol->polygons )
2318 aSymbol->AddDrawItem( loadSymbolPolyLine( aSymbol, epolygon, aGateNumber ) );
2319
2320 for( const std::unique_ptr<ERECT>& erectangle : aEsymbol->rectangles )
2321 aSymbol->AddDrawItem( loadSymbolRectangle( aSymbol, erectangle, aGateNumber ) );
2322
2323 for( const std::unique_ptr<ETEXT>& etext : aEsymbol->texts )
2324 {
2325 std::unique_ptr<SCH_TEXT> libtext( loadSymbolText( aSymbol, etext, aGateNumber ) );
2326
2327 if( libtext->GetText() == wxT( "${REFERENCE}" ) )
2328 {
2329 // Move text & attributes to Reference field and discard LIB_TEXT item
2330 loadFieldAttributes( &aSymbol->GetReferenceField(), libtext.get() );
2331
2332 // Show Reference field if Eagle reference was uppercase
2333 showRefDes = etext->text == wxT( ">NAME" );
2334 }
2335 else if( libtext->GetText() == wxT( "${VALUE}" ) )
2336 {
2337 // Move text & attributes to Value field and discard LIB_TEXT item
2338 loadFieldAttributes( &aSymbol->GetValueField(), libtext.get() );
2339
2340 // Show Value field if Eagle reference was uppercase
2341 showValue = etext->text == wxT( ">VALUE" );
2342 }
2343 else
2344 {
2345 aSymbol->AddDrawItem( libtext.release() );
2346 }
2347 }
2348
2349 for( const std::unique_ptr<EWIRE>& ewire : aEsymbol->wires )
2350 aSymbol->AddDrawItem( loadSymbolWire( aSymbol, ewire, aGateNumber ) );
2351
2352 for( const std::unique_ptr<EFRAME>& eframe : aEsymbol->frames )
2353 {
2354 std::vector<SCH_ITEM*> frameItems;
2355
2356 loadFrame( eframe, frameItems );
2357
2358 for( SCH_ITEM* item : frameItems )
2359 {
2360 item->SetParent( aSymbol.get() );
2361 item->SetUnit( aGateNumber );
2362 aSymbol->AddDrawItem( item );
2363 }
2364 }
2365
2366 aSymbol->GetReferenceField().SetVisible( showRefDes );
2367 aSymbol->GetValueField().SetVisible( showValue );
2368
2369 return pincount == 1 ? ispower : false;
2370}
2371
2372
2373SCH_SHAPE* SCH_IO_EAGLE::loadSymbolCircle( std::unique_ptr<LIB_SYMBOL>& aSymbol,
2374 const std::unique_ptr<ECIRCLE>& aCircle,
2375 int aGateNumber )
2376{
2377 wxCHECK( aSymbol && aCircle, nullptr );
2378
2379 // Parse the circle properties
2381 VECTOR2I center( aCircle->x.ToSchUnits(), -aCircle->y.ToSchUnits() );
2382
2383 circle->SetParent( aSymbol.get() );
2384 circle->SetPosition( center );
2385 circle->SetEnd( VECTOR2I( center.x + aCircle->radius.ToSchUnits(), center.y ) );
2386 circle->SetStroke( STROKE_PARAMS( aCircle->width.ToSchUnits(), LINE_STYLE::SOLID ) );
2387 circle->SetUnit( aGateNumber );
2388
2389 return circle;
2390}
2391
2392
2393SCH_SHAPE* SCH_IO_EAGLE::loadSymbolRectangle( std::unique_ptr<LIB_SYMBOL>& aSymbol,
2394 const std::unique_ptr<ERECT>& aRectangle,
2395 int aGateNumber )
2396{
2397 wxCHECK( aSymbol && aRectangle, nullptr );
2398
2399 SCH_SHAPE* rectangle = new SCH_SHAPE( SHAPE_T::RECTANGLE );
2400
2401 rectangle->SetParent( aSymbol.get() );
2402 rectangle->SetPosition( VECTOR2I( aRectangle->x1.ToSchUnits(), -aRectangle->y1.ToSchUnits() ) );
2403 rectangle->SetEnd( VECTOR2I( aRectangle->x2.ToSchUnits(), -aRectangle->y2.ToSchUnits() ) );
2404
2405 if( aRectangle->rot )
2406 {
2407 VECTOR2I pos( rectangle->GetPosition() );
2408 VECTOR2I end( rectangle->GetEnd() );
2409 VECTOR2I center( rectangle->GetCenter() );
2410
2411 RotatePoint( pos, center, EDA_ANGLE( aRectangle->rot->degrees, DEGREES_T ) );
2412 RotatePoint( end, center, EDA_ANGLE( aRectangle->rot->degrees, DEGREES_T ) );
2413
2414 rectangle->SetPosition( pos );
2415 rectangle->SetEnd( end );
2416 }
2417
2418 rectangle->SetUnit( aGateNumber );
2419
2420 // Eagle rectangles are filled by definition.
2421 rectangle->SetFillMode( FILL_T::FILLED_SHAPE );
2422
2423 return rectangle;
2424}
2425
2426
2427SCH_ITEM* SCH_IO_EAGLE::loadSymbolWire( std::unique_ptr<LIB_SYMBOL>& aSymbol,
2428 const std::unique_ptr<EWIRE>& aWire, int aGateNumber )
2429{
2430 wxCHECK( aSymbol && aWire, nullptr );
2431
2432 VECTOR2I begin, end;
2433
2434 begin.x = aWire->x1.ToSchUnits();
2435 begin.y = -aWire->y1.ToSchUnits();
2436 end.x = aWire->x2.ToSchUnits();
2437 end.y = -aWire->y2.ToSchUnits();
2438
2439 if( begin == end )
2440 return nullptr;
2441
2442 // if the wire is an arc
2443 if( aWire->curve )
2444 {
2446 VECTOR2I center = ConvertArcCenter( begin, end, *aWire->curve );
2447 double radius = sqrt( ( ( center.x - begin.x ) * ( center.x - begin.x ) ) +
2448 ( ( center.y - begin.y ) * ( center.y - begin.y ) ) );
2449
2450 arc->SetParent( aSymbol.get() );
2451
2452 // this emulates the filled semicircles created by a thick arc with flat ends caps.
2453 if( aWire->cap == EWIRE::FLAT && aWire->width.ToSchUnits() >= 2 * radius )
2454 {
2455 VECTOR2I centerStartVector = ( begin - center ) *
2456 ( aWire->width.ToSchUnits() / radius );
2457 begin = center + centerStartVector;
2458
2461 }
2462 else
2463 {
2464 arc->SetStroke( STROKE_PARAMS( aWire->width.ToSchUnits(), LINE_STYLE::SOLID ) );
2465 }
2466
2467 arc->SetCenter( center );
2468 arc->SetStart( begin );
2469
2470 // KiCad rotates the other way.
2471 arc->SetArcAngleAndEnd( -EDA_ANGLE( *aWire->curve, DEGREES_T ), true );
2472 arc->SetUnit( aGateNumber );
2473
2474 return arc;
2475 }
2476 else
2477 {
2479
2480 poly->AddPoint( begin );
2481 poly->AddPoint( end );
2482 poly->SetUnit( aGateNumber );
2483 poly->SetStroke( STROKE_PARAMS( aWire->width.ToSchUnits(), LINE_STYLE::SOLID ) );
2484
2485 return poly;
2486 }
2487}
2488
2489
2490SCH_SHAPE* SCH_IO_EAGLE::loadSymbolPolyLine( std::unique_ptr<LIB_SYMBOL>& aSymbol,
2491 const std::unique_ptr<EPOLYGON>& aPolygon,
2492 int aGateNumber )
2493{
2494 wxCHECK( aSymbol && aPolygon, nullptr );
2495
2496 SCH_SHAPE* poly = new SCH_SHAPE( SHAPE_T::POLY );
2497 VECTOR2I pt, prev_pt;
2498 opt_double prev_curve;
2499
2500 poly->SetParent( aSymbol.get() );
2501
2502 for( const std::unique_ptr<EVERTEX>& evertex : aPolygon->vertices )
2503 {
2504 pt = VECTOR2I( evertex->x.ToSchUnits(), evertex->y.ToSchUnits() );
2505
2506 if( prev_curve )
2507 {
2508 SHAPE_ARC arc;
2509 arc.ConstructFromStartEndAngle( prev_pt, pt, -EDA_ANGLE( *prev_curve, DEGREES_T ) );
2510 poly->GetPolyShape().Append( arc, -1, -1, ARC_ACCURACY );
2511 }
2512 else
2513 {
2514 poly->AddPoint( pt );
2515 }
2516
2517 prev_pt = pt;
2518 prev_curve = evertex->curve;
2519 }
2520
2521 poly->SetStroke( STROKE_PARAMS( aPolygon->width.ToSchUnits(), LINE_STYLE::SOLID ) );
2523 poly->SetUnit( aGateNumber );
2524
2525 return poly;
2526}
2527
2528
2529SCH_PIN* SCH_IO_EAGLE::loadPin( std::unique_ptr<LIB_SYMBOL>& aSymbol,
2530 const std::unique_ptr<EPIN>& aPin, int aGateNumber )
2531{
2532 wxCHECK( aSymbol && aPin, nullptr );
2533
2534 std::unique_ptr<SCH_PIN> pin = std::make_unique<SCH_PIN>( aSymbol.get() );
2535 pin->SetPosition( VECTOR2I( aPin->x.ToSchUnits(), -aPin->y.ToSchUnits() ) );
2536 pin->SetName( aPin->name );
2537 pin->SetUnit( aGateNumber );
2538
2539 int roti = aPin->rot ? aPin->rot->degrees : 0;
2540
2541 switch( roti )
2542 {
2543 case 0: pin->SetOrientation( PIN_ORIENTATION::PIN_RIGHT ); break;
2544 case 90: pin->SetOrientation( PIN_ORIENTATION::PIN_UP ); break;
2545 case 180: pin->SetOrientation( PIN_ORIENTATION::PIN_LEFT ); break;
2546 case 270: pin->SetOrientation( PIN_ORIENTATION::PIN_DOWN ); break;
2547 default: wxFAIL_MSG( wxString::Format( wxT( "Unhandled orientation (%d degrees)." ), roti ) );
2548 }
2549
2550 pin->SetLength( schIUScale.MilsToIU( 300 ) ); // Default pin length when not defined.
2551
2552 if( aPin->length )
2553 {
2554 wxString length = aPin->length.Get();
2555
2556 if( length == wxT( "short" ) )
2557 pin->SetLength( schIUScale.MilsToIU( 100 ) );
2558 else if( length == wxT( "middle" ) )
2559 pin->SetLength( schIUScale.MilsToIU( 200 ) );
2560 else if( length == wxT( "long" ) )
2561 pin->SetLength( schIUScale.MilsToIU( 300 ) );
2562 else if( length == wxT( "point" ) )
2563 pin->SetLength( schIUScale.MilsToIU( 0 ) );
2564 }
2565
2566 // Pin names and numbers are fixed size in Eagle.
2567 pin->SetNumberTextSize( schIUScale.MilsToIU( 60 ) );
2568 pin->SetNameTextSize( schIUScale.MilsToIU( 60 ) );
2569
2570 // emulate the visibility of pin elements
2571 if( aPin->visible )
2572 {
2573 wxString visible = aPin->visible.Get();
2574
2575 if( visible == wxT( "off" ) )
2576 {
2577 pin->SetNameTextSize( 0 );
2578 pin->SetNumberTextSize( 0 );
2579 }
2580 else if( visible == wxT( "pad" ) )
2581 {
2582 pin->SetNameTextSize( 0 );
2583 }
2584 else if( visible == wxT( "pin" ) )
2585 {
2586 pin->SetNumberTextSize( 0 );
2587 }
2588
2589 /*
2590 * else if( visible == wxT( "both" ) )
2591 * {
2592 * }
2593 */
2594 }
2595
2596 if( aPin->function )
2597 {
2598 wxString function = aPin->function.Get();
2599
2600 if( function == wxT( "dot" ) )
2601 pin->SetShape( GRAPHIC_PINSHAPE::INVERTED );
2602 else if( function == wxT( "clk" ) )
2603 pin->SetShape( GRAPHIC_PINSHAPE::CLOCK );
2604 else if( function == wxT( "dotclk" ) )
2606 }
2607
2608 return pin.release();
2609}
2610
2611
2612SCH_TEXT* SCH_IO_EAGLE::loadSymbolText( std::unique_ptr<LIB_SYMBOL>& aSymbol,
2613 const std::unique_ptr<ETEXT>& aText, int aGateNumber )
2614{
2615 wxCHECK( aSymbol && aText, nullptr );
2616
2617 std::unique_ptr<SCH_TEXT> libtext = std::make_unique<SCH_TEXT>();
2618
2619 libtext->SetParent( aSymbol.get() );
2620 libtext->SetUnit( aGateNumber );
2621 libtext->SetPosition( VECTOR2I( aText->x.ToSchUnits(), -aText->y.ToSchUnits() ) );
2622
2623 const wxString& eagleText = aText->text;
2624 wxString adjustedText;
2625 wxStringTokenizer tokenizer( eagleText, "\r\n" );
2626
2627 // Strip the whitespace from both ends of each line.
2628 while( tokenizer.HasMoreTokens() )
2629 {
2630 wxString tmp = interpretText( tokenizer.GetNextToken().Trim( true ).Trim( false ) );
2631
2632 if( tokenizer.HasMoreTokens() )
2633 tmp += wxT( "\n" );
2634
2635 adjustedText += tmp;
2636 }
2637
2638 libtext->SetText( adjustedText.IsEmpty() ? wxString( wxS( "~" ) ) : adjustedText );
2639
2640 loadTextAttributes( libtext.get(), aText );
2641
2642 return libtext.release();
2643}
2644
2645
2646SCH_TEXT* SCH_IO_EAGLE::loadPlainText( const std::unique_ptr<ETEXT>& aText )
2647{
2648 wxCHECK( aText, nullptr );
2649
2650 std::unique_ptr<SCH_TEXT> schtext = std::make_unique<SCH_TEXT>();
2651
2652 const wxString& eagleText = aText->text;
2653 wxString adjustedText;
2654 wxStringTokenizer tokenizer( eagleText, "\r\n" );
2655
2656 // Strip the whitespace from both ends of each line.
2657 while( tokenizer.HasMoreTokens() )
2658 {
2659 wxString tmp = interpretText( tokenizer.GetNextToken().Trim( true ).Trim( false ) );
2660
2661 if( tokenizer.HasMoreTokens() )
2662 tmp += wxT( "\n" );
2663
2664 adjustedText += tmp;
2665 }
2666
2667 schtext->SetText( adjustedText.IsEmpty() ? wxString( wxS( "\" \"" ) )
2668 : escapeName( adjustedText ) );
2669
2670 schtext->SetPosition( VECTOR2I( aText->x.ToSchUnits(), -aText->y.ToSchUnits() ) );
2671 loadTextAttributes( schtext.get(), aText );
2672 schtext->SetItalic( false );
2673
2674 return schtext.release();
2675}
2676
2677
2679 const std::unique_ptr<ETEXT>& aAttributes ) const
2680{
2681 wxCHECK( aText && aAttributes, /* void */ );
2682
2683 aText->SetTextSize( aAttributes->ConvertSize() );
2684
2685 // Must come after SetTextSize()
2686 if( aAttributes->ratio && aAttributes->ratio.CGet() > 12 )
2687 aText->SetBold( true );
2688
2689 int align = aAttributes->align ? *aAttributes->align : ETEXT::BOTTOM_LEFT;
2690 int degrees = aAttributes->rot ? aAttributes->rot->degrees : 0;
2691 bool mirror = aAttributes->rot ? aAttributes->rot->mirror : false;
2692 bool spin = aAttributes->rot ? aAttributes->rot->spin : false;
2693
2694 eagleToKicadAlignment( aText, align, degrees, mirror, spin, 0 );
2695}
2696
2697
2698void SCH_IO_EAGLE::loadFieldAttributes( SCH_FIELD* aField, const SCH_TEXT* aText ) const
2699{
2700 wxCHECK( aField && aText, /* void */ );
2701
2702 aField->SetTextPos( aText->GetPosition() );
2703 aField->SetTextSize( aText->GetTextSize() );
2704 aField->SetTextAngle( aText->GetTextAngle() );
2705
2706 // Must come after SetTextSize()
2707 aField->SetBold( aText->IsBold() );
2708 aField->SetItalic( false );
2709
2710 aField->SetVertJustify( aText->GetVertJustify() );
2711 aField->SetHorizJustify( aText->GetHorizJustify() );
2712}
2713
2714
2716{
2717 // Eagle supports detached labels, so a label does not need to be placed on a wire
2718 // to be associated with it. KiCad needs to move them, so the labels actually touch the
2719 // corresponding wires.
2720
2721 // Sort the intersection points to speed up the search process
2722 std::sort( m_wireIntersections.begin(), m_wireIntersections.end() );
2723
2724 auto onIntersection =
2725 [&]( const VECTOR2I& aPos )
2726 {
2727 return std::binary_search( m_wireIntersections.begin(),
2728 m_wireIntersections.end(), aPos );
2729 };
2730
2731 for( SEG_DESC& segDesc : m_segments )
2732 {
2733 for( SCH_TEXT* label : segDesc.labels )
2734 {
2735 VECTOR2I labelPos( label->GetPosition() );
2736 const SEG* segAttached = segDesc.LabelAttached( label );
2737
2738 if( segAttached && !onIntersection( labelPos ) )
2739 continue; // label is placed correctly
2740
2741 // Move the label to the nearest wire
2742 if( !segAttached )
2743 {
2744 std::tie( labelPos, segAttached ) = findNearestLinePoint( label->GetPosition(),
2745 segDesc.segs );
2746
2747 if( !segAttached ) // we cannot do anything
2748 continue;
2749 }
2750
2751 // Create a vector pointing in the direction of the wire, 50 mils long
2752 VECTOR2I wireDirection( segAttached->B - segAttached->A );
2753 wireDirection = wireDirection.Resize( schIUScale.MilsToIU( 50 ) );
2754 const VECTOR2I origPos( labelPos );
2755
2756 // Flags determining the search direction
2757 bool checkPositive = true, checkNegative = true, move = false;
2758 int trial = 0;
2759
2760 // Be sure the label is not placed on a wire intersection
2761 while( ( !move || onIntersection( labelPos ) ) && ( checkPositive || checkNegative ) )
2762 {
2763 move = false;
2764
2765 // Move along the attached wire to find the new label position
2766 if( trial % 2 == 1 )
2767 {
2768 labelPos = VECTOR2I( origPos + wireDirection * trial / 2 );
2769 move = checkPositive = segAttached->Contains( labelPos );
2770 }
2771 else
2772 {
2773 labelPos = VECTOR2I( origPos - wireDirection * trial / 2 );
2774 move = checkNegative = segAttached->Contains( labelPos );
2775 }
2776
2777 ++trial;
2778 }
2779
2780 if( move )
2781 label->SetPosition( VECTOR2I( labelPos ) );
2782 }
2783 }
2784
2785 m_segments.clear();
2786 m_wireIntersections.clear();
2787}
2788
2789
2790bool SCH_IO_EAGLE::CanReadSchematicFile( const wxString& aFileName ) const
2791{
2792 if( !SCH_IO::CanReadSchematicFile( aFileName ) )
2793 return false;
2794
2795 return checkHeader( aFileName );
2796}
2797
2798
2799bool SCH_IO_EAGLE::CanReadLibrary( const wxString& aFileName ) const
2800{
2801 if( !SCH_IO::CanReadLibrary( aFileName ) )
2802 return false;
2803
2804 return checkHeader( aFileName );
2805}
2806
2807
2808bool SCH_IO_EAGLE::checkHeader( const wxString& aFileName ) const
2809{
2810 wxFileInputStream input( aFileName );
2811
2812 if( !input.IsOk() )
2813 return false;
2814
2815 wxTextInputStream text( input );
2816
2817 for( int i = 0; i < 8; i++ )
2818 {
2819 if( input.Eof() )
2820 return false;
2821
2822 if( text.ReadLine().Contains( wxS( "<eagle" ) ) )
2823 return true;
2824 }
2825
2826 return false;
2827}
2828
2829
2830void SCH_IO_EAGLE::moveLabels( SCH_LINE* aWire, const VECTOR2I& aNewEndPoint )
2831{
2832 wxCHECK( aWire, /* void */ );
2833
2834 SCH_SCREEN* screen = getCurrentScreen();
2835
2836 wxCHECK( screen, /* void */ );
2837
2838 for( SCH_ITEM* item : screen->Items().Overlapping( aWire->GetBoundingBox() ) )
2839 {
2840 if( !item->IsType( { SCH_LABEL_LOCATE_ANY_T } ) )
2841 continue;
2842
2843 if( TestSegmentHit( item->GetPosition(), aWire->GetStartPoint(), aWire->GetEndPoint(), 0 ) )
2844 item->SetPosition( aNewEndPoint );
2845 }
2846}
2847
2848
2850{
2851 // Add bus entry symbols
2852 // TODO: Cleanup this function and break into pieces
2853
2854 // for each wire segment, compare each end with all busses.
2855 // If the wire end is found to end on a bus segment, place a bus entry symbol.
2856
2857 std::vector<SCH_LINE*> buses;
2858 std::vector<SCH_LINE*> wires;
2859
2860 SCH_SCREEN* screen = getCurrentScreen();
2861
2862 wxCHECK( screen, /* void */ );
2863
2864 for( SCH_ITEM* ii : screen->Items().OfType( SCH_LINE_T ) )
2865 {
2866 SCH_LINE* line = static_cast<SCH_LINE*>( ii );
2867
2868 if( line->IsBus() )
2869 buses.push_back( line );
2870 else if( line->IsWire() )
2871 wires.push_back( line );
2872 }
2873
2874 for( SCH_LINE* wire : wires )
2875 {
2876 VECTOR2I wireStart = wire->GetStartPoint();
2877 VECTOR2I wireEnd = wire->GetEndPoint();
2878
2879 for( SCH_LINE* bus : buses )
2880 {
2881 VECTOR2I busStart = bus->GetStartPoint();
2882 VECTOR2I busEnd = bus->GetEndPoint();
2883
2884 auto entrySize =
2885 []( int signX, int signY ) -> VECTOR2I
2886 {
2887 return VECTOR2I( schIUScale.MilsToIU( DEFAULT_SCH_ENTRY_SIZE ) * signX,
2888 schIUScale.MilsToIU( DEFAULT_SCH_ENTRY_SIZE ) * signY );
2889 };
2890
2891 auto testBusHit =
2892 [&]( const VECTOR2I& aPt ) -> bool
2893 {
2894 return TestSegmentHit( aPt, busStart, busEnd, 0 );
2895 };
2896
2897 if( wireStart.y == wireEnd.y && busStart.x == busEnd.x )
2898 {
2899 // Horizontal wire and vertical bus
2900
2901 if( testBusHit( wireStart ) )
2902 {
2903 // Wire start is on the vertical bus
2904
2905 if( wireEnd.x < busStart.x )
2906 {
2907 /* the end of the wire is to the left of the bus
2908 * ⎥⎢
2909 * ——————⎥⎢
2910 * ⎥⎢
2911 */
2912 VECTOR2I p = wireStart + entrySize( -1, 0 );
2913
2914 if( testBusHit( wireStart + entrySize( 0, -1 ) ) )
2915 {
2916 /* there is room above the wire for the bus entry
2917 * ⎥⎢
2918 * _____/⎥⎢
2919 * ⎥⎢
2920 */
2921 SCH_BUS_WIRE_ENTRY* busEntry = new SCH_BUS_WIRE_ENTRY( p, 1 );
2922 busEntry->SetFlags( IS_NEW );
2923 screen->Append( busEntry );
2924 moveLabels( wire, p );
2925 wire->SetStartPoint( p );
2926 }
2927 else if( testBusHit( wireStart + entrySize( 0, 1 ) ) )
2928 {
2929 /* there is room below the wire for the bus entry
2930 * _____ ⎥⎢
2931 * \⎥⎢
2932 * ⎥⎢
2933 */
2934 SCH_BUS_WIRE_ENTRY* busEntry = new SCH_BUS_WIRE_ENTRY( p, 2 );
2935 busEntry->SetFlags( IS_NEW );
2936 screen->Append( busEntry );
2937 moveLabels( wire, p );
2938 wire->SetStartPoint( p );
2939 }
2940 else
2941 {
2942 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_BUS_ENTRY_NEEDED );
2943 screen->Append( new SCH_MARKER( std::move( ercItem ), wireStart ) );
2944 }
2945 }
2946 else
2947 {
2948 /* the wire end is to the right of the bus
2949 * ⎥⎢
2950 * ⎥⎢——————
2951 * ⎥⎢
2952 */
2953 VECTOR2I p = wireStart + entrySize( 1, 0 );
2954
2955 if( testBusHit( wireStart + entrySize( 0, -1 ) ) )
2956 {
2957 /* There is room above the wire for the bus entry
2958 * ⎥⎢
2959 * ⎥⎢\_____
2960 * ⎥⎢
2961 */
2962 SCH_BUS_WIRE_ENTRY* busEntry = new SCH_BUS_WIRE_ENTRY( p , 4 );
2963 busEntry->SetFlags( IS_NEW );
2964 screen->Append( busEntry );
2965 moveLabels( wire, p );
2966 wire->SetStartPoint( p );
2967 }
2968 else if( testBusHit( wireStart + entrySize( 0, 1 ) ) )
2969 {
2970 /* There is room below the wire for the bus entry
2971 * ⎥⎢ _____
2972 * ⎥⎢/
2973 * ⎥⎢
2974 */
2975 SCH_BUS_WIRE_ENTRY* busEntry = new SCH_BUS_WIRE_ENTRY( p, 3 );
2976 busEntry->SetFlags( IS_NEW );
2977 screen->Append( busEntry );
2978 moveLabels( wire, p );
2979 wire->SetStartPoint( p );
2980 }
2981 else
2982 {
2983 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_BUS_ENTRY_NEEDED );
2984 screen->Append( new SCH_MARKER( std::move( ercItem ), wireStart ) );
2985 }
2986 }
2987
2988 break;
2989 }
2990 else if( testBusHit( wireEnd ) )
2991 {
2992 // Wire end is on the vertical bus
2993
2994 if( wireStart.x < busStart.x )
2995 {
2996 /* start of the wire is to the left of the bus
2997 * ⎥⎢
2998 * ——————⎥⎢
2999 * ⎥⎢
3000 */
3001 VECTOR2I p = wireEnd + entrySize( -1, 0 );
3002
3003 if( testBusHit( wireEnd + entrySize( 0, -1 ) ) )
3004 {
3005 /* there is room above the wire for the bus entry
3006 * ⎥⎢
3007 * _____/⎥⎢
3008 * ⎥⎢
3009 */
3010 SCH_BUS_WIRE_ENTRY* busEntry = new SCH_BUS_WIRE_ENTRY( p, 1 );
3011 busEntry->SetFlags( IS_NEW );
3012 screen->Append( busEntry );
3013 moveLabels( wire, p );
3014 wire->SetEndPoint( p );
3015 }
3016 else if( testBusHit( wireEnd + entrySize( 0, -1 ) ) )
3017 {
3018 /* there is room below the wire for the bus entry
3019 * _____ ⎥⎢
3020 * \⎥⎢
3021 * ⎥⎢
3022 */
3023 SCH_BUS_WIRE_ENTRY* busEntry = new SCH_BUS_WIRE_ENTRY( p, 2 );
3024 busEntry->SetFlags( IS_NEW );
3025 screen->Append( busEntry );
3026 moveLabels( wire, wireEnd + entrySize( -1, 0 ) );
3027 wire->SetEndPoint( wireEnd + entrySize( -1, 0 ) );
3028 }
3029 else
3030 {
3031 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_BUS_ENTRY_NEEDED );
3032 screen->Append( new SCH_MARKER( std::move( ercItem ), wireEnd ) );
3033 }
3034 }
3035 else
3036 {
3037 /* the start of the wire is to the right of the bus
3038 * ⎥⎢
3039 * ⎥⎢——————
3040 * ⎥⎢
3041 */
3042 VECTOR2I p = wireEnd + entrySize( 1, 0 );
3043
3044 if( testBusHit( wireEnd + entrySize( 0, -1 ) ) )
3045 {
3046 /* There is room above the wire for the bus entry
3047 * ⎥⎢
3048 * ⎥⎢\_____
3049 * ⎥⎢
3050 */
3051 SCH_BUS_WIRE_ENTRY* busEntry = new SCH_BUS_WIRE_ENTRY( p, 4 );
3052 busEntry->SetFlags( IS_NEW );
3053 screen->Append( busEntry );
3054 moveLabels( wire, p );
3055 wire->SetEndPoint( p );
3056 }
3057 else if( testBusHit( wireEnd + entrySize( 0, 1 ) ) )
3058 {
3059 /* There is room below the wire for the bus entry
3060 * ⎥⎢ _____
3061 * ⎥⎢/
3062 * ⎥⎢
3063 */
3064 SCH_BUS_WIRE_ENTRY* busEntry = new SCH_BUS_WIRE_ENTRY( p, 3 );
3065 busEntry->SetFlags( IS_NEW );
3066 screen->Append( busEntry );
3067 moveLabels( wire, p );
3068 wire->SetEndPoint( p );
3069 }
3070 else
3071 {
3072 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_BUS_ENTRY_NEEDED );
3073 screen->Append( new SCH_MARKER( std::move( ercItem ), wireEnd ) );
3074 }
3075 }
3076
3077 break;
3078 }
3079 }
3080 else if( wireStart.x == wireEnd.x && busStart.y == busEnd.y )
3081 {
3082 // Vertical wire and horizontal bus
3083
3084 if( testBusHit( wireStart ) )
3085 {
3086 // Wire start is on the bus
3087
3088 if( wireEnd.y < busStart.y )
3089 {
3090 /* the end of the wire is above the bus
3091 * |
3092 * |
3093 * |
3094 * =======
3095 */
3096 VECTOR2I p = wireStart + entrySize( 0, -1 );
3097
3098 if( testBusHit( wireStart + entrySize( -1, 0 ) ) )
3099 {
3100 /* there is room to the left of the wire for the bus entry
3101 * |
3102 * |
3103 * /
3104 * =======
3105 */
3106 SCH_BUS_WIRE_ENTRY* busEntry = new SCH_BUS_WIRE_ENTRY( p, 3 );
3107 busEntry->SetFlags( IS_NEW );
3108 screen->Append( busEntry );
3109 moveLabels( wire, p );
3110 wire->SetStartPoint( p );
3111 }
3112 else if( testBusHit( wireStart + entrySize( 1, 0 ) ) )
3113 {
3114 /* there is room to the right of the wire for the bus entry
3115 * |
3116 * |
3117 * \
3118 * =======
3119 */
3120 SCH_BUS_WIRE_ENTRY* busEntry = new SCH_BUS_WIRE_ENTRY( p, 2 );
3121 busEntry->SetFlags( IS_NEW );
3122 screen->Append( busEntry );
3123 moveLabels( wire, p );
3124 wire->SetStartPoint( p );
3125 }
3126 else
3127 {
3128 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_BUS_ENTRY_NEEDED );
3129 screen->Append( new SCH_MARKER( std::move( ercItem ), wireStart ) );
3130 }
3131 }
3132 else
3133 {
3134 /* wire end is below the bus
3135 * =======
3136 * |
3137 * |
3138 * |
3139 */
3140 VECTOR2I p = wireStart + entrySize( 0, 1 );
3141
3142 if( testBusHit( wireStart + entrySize( -1, 0 ) ) )
3143 {
3144 /* there is room to the left of the wire for the bus entry
3145 * =======
3146 * \
3147 * |
3148 * |
3149 */
3150 SCH_BUS_WIRE_ENTRY* busEntry = new SCH_BUS_WIRE_ENTRY( p, 4 );
3151 busEntry->SetFlags( IS_NEW );
3152 screen->Append( busEntry );
3153 moveLabels( wire, p );
3154 wire->SetStartPoint( p );
3155 }
3156 else if( testBusHit( wireStart + entrySize( 1, 0 ) ) )
3157 {
3158 /* there is room to the right of the wire for the bus entry
3159 * =======
3160 * /
3161 * |
3162 * |
3163 */
3164 SCH_BUS_WIRE_ENTRY* busEntry = new SCH_BUS_WIRE_ENTRY( p, 1 );
3165 busEntry->SetFlags( IS_NEW );
3166 screen->Append( busEntry );
3167 moveLabels( wire, p );
3168 wire->SetStartPoint( p );
3169 }
3170 else
3171 {
3172 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_BUS_ENTRY_NEEDED );
3173 screen->Append( new SCH_MARKER( std::move( ercItem ), wireStart ) );
3174 }
3175 }
3176
3177 break;
3178 }
3179 else if( testBusHit( wireEnd ) )
3180 {
3181 // Wire end is on the bus
3182
3183 if( wireStart.y < busStart.y )
3184 {
3185 /* the start of the wire is above the bus
3186 * |
3187 * |
3188 * |
3189 * =======
3190 */
3191 VECTOR2I p = wireEnd + entrySize( 0, -1 );
3192
3193 if( testBusHit( wireEnd + entrySize( -1, 0 ) ) )
3194 {
3195 /* there is room to the left of the wire for the bus entry
3196 * |
3197 * |
3198 * /
3199 * =======
3200 */
3201 SCH_BUS_WIRE_ENTRY* busEntry = new SCH_BUS_WIRE_ENTRY( p, 3 );
3202 busEntry->SetFlags( IS_NEW );
3203 screen->Append( busEntry );
3204 moveLabels( wire, p );
3205 wire->SetEndPoint( p );
3206 }
3207 else if( testBusHit( wireEnd + entrySize( 1, 0 ) ) )
3208 {
3209 /* there is room to the right of the wire for the bus entry
3210 * |
3211 * |
3212 * \
3213 * =======
3214 */
3215 SCH_BUS_WIRE_ENTRY* busEntry = new SCH_BUS_WIRE_ENTRY( p, 2 );
3216 busEntry->SetFlags( IS_NEW );
3217 screen->Append( busEntry );
3218 moveLabels( wire, p );
3219 wire->SetEndPoint( p );
3220 }
3221 else
3222 {
3223 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_BUS_ENTRY_NEEDED );
3224 screen->Append( new SCH_MARKER( std::move( ercItem ), wireEnd ) );
3225 }
3226 }
3227 else
3228 {
3229 /* wire start is below the bus
3230 * =======
3231 * |
3232 * |
3233 * |
3234 */
3235 VECTOR2I p = wireEnd + entrySize( 0, 1 );
3236
3237 if( testBusHit( wireEnd + entrySize( -1, 0 ) ) )
3238 {
3239 /* there is room to the left of the wire for the bus entry
3240 * =======
3241 * \
3242 * |
3243 * |
3244 */
3245 SCH_BUS_WIRE_ENTRY* busEntry = new SCH_BUS_WIRE_ENTRY( p, 4 );
3246 busEntry->SetFlags( IS_NEW );
3247 screen->Append( busEntry );
3248 moveLabels( wire, p );
3249 wire->SetEndPoint( p );
3250 }
3251 else if( testBusHit( wireEnd + entrySize( 1, 0 ) ) )
3252 {
3253 /* there is room to the right of the wire for the bus entry
3254 * =======
3255 * /
3256 * |
3257 * |
3258 */
3259 SCH_BUS_WIRE_ENTRY* busEntry = new SCH_BUS_WIRE_ENTRY( p, 1 );
3260 busEntry->SetFlags( IS_NEW );
3261 screen->Append( busEntry );
3262 moveLabels( wire, p );
3263 wire->SetEndPoint( p );
3264 }
3265 else
3266 {
3267 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_BUS_ENTRY_NEEDED );
3268 screen->Append( new SCH_MARKER( std::move( ercItem ), wireEnd ) );
3269 }
3270 }
3271
3272 break;
3273 }
3274 }
3275 else
3276 {
3277 // Wire isn't horizontal or vertical
3278
3279 if( testBusHit( wireStart ) )
3280 {
3281 VECTOR2I wirevector = wireStart - wireEnd;
3282
3283 if( wirevector.x > 0 )
3284 {
3285 if( wirevector.y > 0 )
3286 {
3287 VECTOR2I p = wireStart + entrySize( -1, -1 );
3288 SCH_BUS_WIRE_ENTRY* busEntry = new SCH_BUS_WIRE_ENTRY( p, 2 );
3289 busEntry->SetFlags( IS_NEW );
3290 screen->Append( busEntry );
3291
3292 moveLabels( wire, p );
3293 wire->SetStartPoint( p );
3294 }
3295 else
3296 {
3297 VECTOR2I p = wireStart + entrySize( -1, 1 );
3298 SCH_BUS_WIRE_ENTRY* busEntry = new SCH_BUS_WIRE_ENTRY( p, 1 );
3299 busEntry->SetFlags( IS_NEW );
3300 screen->Append( busEntry );
3301
3302 moveLabels( wire, p );
3303 wire->SetStartPoint( p );
3304 }
3305 }
3306 else
3307 {
3308 if( wirevector.y > 0 )
3309 {
3310 VECTOR2I p = wireStart + entrySize( 1, -1 );
3311 SCH_BUS_WIRE_ENTRY* busEntry = new SCH_BUS_WIRE_ENTRY( p, 3 );
3312 busEntry->SetFlags( IS_NEW );
3313 screen->Append( busEntry );
3314
3315 moveLabels( wire, p );
3316 wire->SetStartPoint( p );
3317 }
3318 else
3319 {
3320 VECTOR2I p = wireStart + entrySize( 1, 1 );
3321 SCH_BUS_WIRE_ENTRY* busEntry = new SCH_BUS_WIRE_ENTRY( p, 4 );
3322 busEntry->SetFlags( IS_NEW );
3323 screen->Append( busEntry );
3324
3325 moveLabels( wire, p );
3326 wire->SetStartPoint( p );
3327 }
3328 }
3329
3330 break;
3331 }
3332 else if( testBusHit( wireEnd ) )
3333 {
3334 VECTOR2I wirevector = wireStart - wireEnd;
3335
3336 if( wirevector.x > 0 )
3337 {
3338 if( wirevector.y > 0 )
3339 {
3340 VECTOR2I p = wireEnd + entrySize( 1, 1 );
3341 SCH_BUS_WIRE_ENTRY* busEntry = new SCH_BUS_WIRE_ENTRY( p, 4 );
3342 busEntry->SetFlags( IS_NEW );
3343 screen->Append( busEntry );
3344
3345 moveLabels( wire, p );
3346 wire->SetEndPoint( p );
3347 }
3348 else
3349 {
3350 VECTOR2I p = wireEnd + entrySize( 1, -1 );
3351 SCH_BUS_WIRE_ENTRY* busEntry = new SCH_BUS_WIRE_ENTRY( p, 3 );
3352 busEntry->SetFlags( IS_NEW );
3353 screen->Append( busEntry );
3354
3355 moveLabels( wire, p );
3356 wire->SetEndPoint( p );
3357 }
3358 }
3359 else
3360 {
3361 if( wirevector.y > 0 )
3362 {
3363 VECTOR2I p = wireEnd + entrySize( -1, 1 );
3364 SCH_BUS_WIRE_ENTRY* busEntry = new SCH_BUS_WIRE_ENTRY( p, 1 );
3365 busEntry->SetFlags( IS_NEW );
3366 screen->Append( busEntry );
3367
3368 moveLabels( wire, p );
3369 wire->SetEndPoint( p );
3370 }
3371 else
3372 {
3373 VECTOR2I p = wireEnd + entrySize( -1, -1 );
3374 SCH_BUS_WIRE_ENTRY* busEntry = new SCH_BUS_WIRE_ENTRY( p, 2 );
3375 busEntry->SetFlags( IS_NEW );
3376 screen->Append( busEntry );
3377
3378 moveLabels( wire, p );
3379 wire->SetEndPoint( p );
3380 }
3381 }
3382
3383 break;
3384 }
3385 }
3386 }
3387 }
3388}
3389
3390
3392{
3393 wxCHECK( aLabel, nullptr );
3394
3395 VECTOR2I labelPos( aLabel->GetPosition() );
3396
3397 for( const SEG& seg : segs )
3398 {
3399 if( seg.Contains( labelPos ) )
3400 return &seg;
3401 }
3402
3403 return nullptr;
3404}
3405
3406
3407// TODO could be used to place junctions, instead of IsJunctionNeeded()
3408// (see SCH_EDIT_FRAME::importFile())
3409bool SCH_IO_EAGLE::checkConnections( const SCH_SYMBOL* aSymbol, const SCH_PIN* aPin ) const
3410{
3411 wxCHECK( aSymbol && aPin, false );
3412
3413 VECTOR2I pinPosition = aSymbol->GetPinPhysicalPosition( aPin );
3414 auto pointIt = m_connPoints.find( pinPosition );
3415
3416 if( pointIt == m_connPoints.end() )
3417 return false;
3418
3419 const auto& items = pointIt->second;
3420
3421 wxCHECK( items.find( aPin ) != items.end(), false );
3422
3423 return items.size() > 1;
3424}
3425
3426
3428 bool aUpdateSet )
3429{
3430 wxCHECK( aSymbol && aScreen && aSymbol->GetLibSymbolRef(), /*void*/ );
3431
3432 // Normally power parts also have power input pins,
3433 // but they already force net names on the attached wires
3434 if( aSymbol->GetLibSymbolRef()->IsGlobalPower() )
3435 return;
3436
3437 int unit = aSymbol->GetUnit();
3438 const wxString reference = aSymbol->GetField( FIELD_T::REFERENCE )->GetText();
3439 std::vector<SCH_PIN*> pins = aSymbol->GetLibSymbolRef()->GetGraphicalPins( 0, 0 );
3440 std::set<int> missingUnits;
3441
3442 // Search all units for pins creating implicit connections
3443 for( const SCH_PIN* pin : pins )
3444 {
3445 if( pin->GetType() == ELECTRICAL_PINTYPE::PT_POWER_IN )
3446 {
3447 bool pinInUnit = !unit || pin->GetUnit() == unit; // pin belongs to the tested unit
3448
3449 // Create a global net label only if there are no other wires/pins attached
3450 if( pinInUnit )
3451 {
3452 if( !checkConnections( aSymbol, pin ) )
3453 {
3454 // Create a net label to force the net name on the pin
3455 SCH_GLOBALLABEL* netLabel = new SCH_GLOBALLABEL;
3456 netLabel->SetPosition( aSymbol->GetPinPhysicalPosition( pin ) );
3457 netLabel->SetText( extractNetName( pin->GetName() ) );
3458 netLabel->SetTextSize( VECTOR2I( schIUScale.MilsToIU( 40 ),
3459 schIUScale.MilsToIU( 40 ) ) );
3460
3461 switch( pin->GetOrientation() )
3462 {
3463 default:
3465 netLabel->SetSpinStyle( SPIN_STYLE::LEFT );
3466 break;
3468 netLabel->SetSpinStyle( SPIN_STYLE::RIGHT );
3469 break;
3471 netLabel->SetSpinStyle( SPIN_STYLE::UP );
3472 break;
3474 netLabel->SetSpinStyle( SPIN_STYLE::BOTTOM );
3475 break;
3476 }
3477
3478 aScreen->Append( netLabel );
3479 }
3480 }
3481 else if( aUpdateSet )
3482 {
3483 // Found a pin creating implicit connection information in another unit.
3484 // Such units will be instantiated if they do not appear in another sheet and
3485 // processed later.
3486 wxASSERT( pin->GetUnit() );
3487 missingUnits.insert( pin->GetUnit() );
3488 }
3489 }
3490 }
3491
3492 if( aUpdateSet && aSymbol->GetLibSymbolRef()->GetUnitCount() > 1 )
3493 {
3494 auto cmpIt = m_missingCmps.find( reference );
3495
3496 // The first unit found has always already been processed.
3497 if( cmpIt == m_missingCmps.end() )
3498 {
3499 EAGLE_MISSING_CMP& entry = m_missingCmps[reference];
3500 entry.cmp = aSymbol;
3501 entry.screen = aScreen;
3502 entry.units.emplace( unit, false );
3503 }
3504 else
3505 {
3506 // Set the flag indicating this unit has been processed.
3507 cmpIt->second.units[unit] = false;
3508 }
3509
3510 if( !missingUnits.empty() ) // Save the units that need later processing
3511 {
3512 EAGLE_MISSING_CMP& entry = m_missingCmps[reference];
3513 entry.cmp = aSymbol;
3514 entry.screen = aScreen;
3515
3516 // Add units that haven't already been processed.
3517 for( int i : missingUnits )
3518 {
3519 if( entry.units.find( i ) != entry.units.end() )
3520 entry.units.emplace( i, true );
3521 }
3522 }
3523 }
3524}
3525
3526
3527wxString SCH_IO_EAGLE::translateEagleBusName( const wxString& aEagleName ) const
3528{
3529 if( NET_SETTINGS::ParseBusVector( aEagleName, nullptr, nullptr ) )
3530 return aEagleName;
3531
3532 wxString ret = wxT( "{" );
3533
3534 wxStringTokenizer tokenizer( aEagleName, "," );
3535
3536 while( tokenizer.HasMoreTokens() )
3537 {
3538 wxString member = tokenizer.GetNextToken();
3539
3540 // In Eagle, overbar text is automatically stopped at the end of the net name, even when
3541 // that net name is part of a bus definition. In KiCad, we don't (currently) do that, so
3542 // if there is an odd number of overbar markers in this net name, we need to append one
3543 // to close it out before appending the space.
3544
3545 if( member.Freq( '!' ) % 2 > 0 )
3546 member << wxT( "!" );
3547
3548 ret << member << wxS( " " );
3549 }
3550
3551 ret.Trim( true );
3552 ret << wxT( "}" );
3553
3554 return ret;
3555}
3556
3557
3558const ESYMBOL* SCH_IO_EAGLE::getEagleSymbol( const std::unique_ptr<EINSTANCE>& aInstance )
3559{
3560 wxCHECK( m_eagleDoc && m_eagleDoc->drawing && m_eagleDoc->drawing->schematic && aInstance,
3561 nullptr );
3562
3563 std::unique_ptr<EPART>& epart = m_eagleDoc->drawing->schematic->parts[aInstance->part];
3564
3565 if( !epart || epart->deviceset.IsEmpty() )
3566 return nullptr;
3567
3568 std::unique_ptr<ELIBRARY>& elibrary = m_eagleDoc->drawing->schematic->libraries[epart->library];
3569
3570 if( !elibrary )
3571 return nullptr;
3572
3573 std::unique_ptr<EDEVICE_SET>& edeviceset = elibrary->devicesets[epart->deviceset];
3574
3575 if( !edeviceset )
3576 return nullptr;
3577
3578 std::unique_ptr<EGATE>& egate = edeviceset->gates[aInstance->gate];
3579
3580 if( !egate )
3581 return nullptr;
3582
3583 std::unique_ptr<ESYMBOL>& esymbol = elibrary->symbols[egate->symbol];
3584
3585 if( esymbol )
3586 return esymbol.get();
3587
3588 return nullptr;
3589}
3590
3591
3592void SCH_IO_EAGLE::getEagleSymbolFieldAttributes( const std::unique_ptr<EINSTANCE>& aInstance,
3593 const wxString& aEagleFieldName,
3594 SCH_FIELD* aField )
3595{
3596 wxCHECK( aField && !aEagleFieldName.IsEmpty(), /* void */ );
3597
3598 const ESYMBOL* esymbol = getEagleSymbol( aInstance );
3599
3600 if( esymbol )
3601 {
3602 for( const std::unique_ptr<ETEXT>& text : esymbol->texts )
3603 {
3604 if( text->text == aEagleFieldName )
3605 {
3606 aField->SetVisible( true );
3607 VECTOR2I pos( text->x.ToSchUnits() + aInstance->x.ToSchUnits(),
3608 -text->y.ToSchUnits() - aInstance->y.ToSchUnits() );
3609
3610 bool mirror = text->rot ? text->rot->mirror : false;
3611
3612 if( aInstance->rot && aInstance->rot->mirror )
3613 mirror = !mirror;
3614
3615 if( mirror )
3616 pos.y = -aInstance->y.ToSchUnits() + text->y.ToSchUnits();
3617
3618 aField->SetPosition( pos );
3619 }
3620 }
3621 }
3622}
const char * name
constexpr EDA_IU_SCALE schIUScale
Definition base_units.h:114
BOX2< VECTOR2I > BOX2I
Definition box2.h:922
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:990
constexpr size_type GetWidth() const
Definition box2.h:214
constexpr Vec Centre() const
Definition box2.h:97
constexpr BOX2< Vec > & Merge(const BOX2< Vec > &aRect)
Modify the position and size of the rectangle in order to contain aRect.
Definition box2.h:658
constexpr size_type GetHeight() const
Definition box2.h:215
constexpr const SizeVec & GetSize() const
Definition box2.h:206
void SetFlags(EDA_ITEM_FLAGS aMask)
Definition eda_item.h:149
virtual void SetParent(EDA_ITEM *aParent)
Definition eda_item.cpp:93
void SetCenter(const VECTOR2I &aCenter)
SHAPE_POLY_SET & GetPolyShape()
const VECTOR2I & GetEnd() const
Return the ending point of the graphic.
Definition eda_shape.h:228
void SetStart(const VECTOR2I &aStart)
Definition eda_shape.h:190
void SetEnd(const VECTOR2I &aEnd)
Definition eda_shape.h:232
void SetArcAngleAndEnd(const EDA_ANGLE &aAngle, bool aCheckNegativeAngle=false)
Set the end point from the angle center and start.
void SetFillMode(FILL_T aFill)
A mix-in class (via multiple inheritance) that handles texts such as labels, parts,...
Definition eda_text.h:91
const EDA_ANGLE & GetTextAngle() const
Definition eda_text.h:158
void SetTextSize(VECTOR2I aNewSize, bool aEnforceMinTextSize=true)
Definition eda_text.cpp:516
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition eda_text.h:109
void SetTextPos(const VECTOR2I &aPoint)
Definition eda_text.cpp:560
void SetVertJustify(GR_TEXT_V_ALIGN_T aType)
Definition eda_text.cpp:401
GR_TEXT_H_ALIGN_T GetHorizJustify() const
Definition eda_text.h:211
virtual void SetVisible(bool aVisible)
Definition eda_text.cpp:370
void SetBold(bool aBold)
Set the text to be bold - this will also update the font if needed.
Definition eda_text.cpp:319
bool IsBold() const
Definition eda_text.h:195
GR_TEXT_V_ALIGN_T GetVertJustify() const
Definition eda_text.h:214
virtual void SetText(const wxString &aText)
Definition eda_text.cpp:254
virtual void SetTextAngle(const EDA_ANGLE &aAngle)
Definition eda_text.cpp:283
void SetItalic(bool aItalic)
Set the text to be italic - this will also update the font if needed.
Definition eda_text.cpp:291
VECTOR2I GetTextSize() const
Definition eda_text.h:272
void SetHorizJustify(GR_TEXT_H_ALIGN_T aType)
Definition eda_text.cpp:393
EE_TYPE Overlapping(const BOX2I &aRect) const
Definition sch_rtree.h:230
ee_rtree::Iterator begin() const
Return a read/write iterator that points to the first.
Definition sch_rtree.h:265
ee_rtree::Iterator end() const
Return a read/write iterator that points to one past the last element in the EE_RTREE.
Definition sch_rtree.h:273
EE_TYPE OfType(KICAD_T aType) const
Definition sch_rtree.h:225
static std::shared_ptr< ERC_ITEM > Create(int aErrorCode)
Constructs an ERC_ITEM for the given error code.
Definition erc_item.cpp:317
RAII class to set and restore the fontconfig reporter.
Definition reporter.h:334
REPORTER * m_reporter
Reporter to log errors/warnings to, may be nullptr.
Definition io_base.h:237
PROGRESS_REPORTER * m_progressReporter
Progress reporter to track the progress of the operation, may be nullptr.
Definition io_base.h:240
virtual bool CanReadLibrary(const wxString &aFileName) const
Checks if this IO object can read the specified library file/directory.
Definition io_base.cpp:71
virtual void Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)
Definition io_base.cpp:124
Definition kiid.h:48
std::optional< LIBRARY_TABLE * > ProjectTable() const
Retrieves the project library table for this adapter type, or nullopt if one doesn't exist.
void SetNickname(const wxString &aNickname)
void SetType(const wxString &aType)
void SetURI(const wxString &aUri)
A logical library item identifier and consists of various portions much like a URI.
Definition lib_id.h:49
static UTF8 FixIllegalChars(const UTF8 &aLibItemName, bool aLib)
Replace illegal LIB_ID item name characters with underscores '_'.
Definition lib_id.cpp:192
Define a library symbol object.
Definition lib_symbol.h:83
std::vector< const SCH_PIN * > GetGraphicalPins(int aUnit=0, int aBodyStyle=0) const
Graphical pins: Return schematic pin objects as drawn (unexpanded), filtered by unit/body.
void GetFields(std::vector< SCH_FIELD * > &aList, bool aVisibleOnly=false) const override
Populate a std::vector with SCH_FIELDs, sorted in ordinal order.
bool IsGlobalPower() const override
int GetUnitCount() const override
static LOAD_INFO_REPORTER & GetInstance()
Definition reporter.cpp:222
static bool ParseBusVector(const wxString &aBus, wxString *aName, std::vector< wxString > *aMemberList)
Parse a bus vector (e.g.
Describe the page size and margins of a paper page on which to eventually print or plot.
Definition page_info.h:79
void SetHeightMils(double aHeightInMils)
const VECTOR2D GetSizeIU(double aIUScale) const
Gets the page size in internal units.
Definition page_info.h:177
void SetWidthMils(double aWidthInMils)
static SYMBOL_LIBRARY_ADAPTER * SymbolLibAdapter(PROJECT *aProject)
Accessor for project symbol library manager adapter.
Holds all the data relating to one schematic.
Definition schematic.h:89
PROJECT & Project() const
Return a reference to the project this schematic is part of.
Definition schematic.h:104
bool IsValid() const
A simple test if the schematic is loaded, not a complete one.
Definition schematic.h:173
SCH_SHEET & Root() const
Definition schematic.h:133
Class for a wire to bus entry.
virtual const wxString & GetText() const override
Return the string associated with the text object.
Definition sch_field.h:123
void ImportValues(const SCH_FIELD &aSource)
Copy parameters from a SCH_FIELD source.
bool IsEmpty()
Return true if both the name and value of the field are empty.
Definition sch_field.h:166
void SetPosition(const VECTOR2I &aPosition) override
void SetName(const wxString &aName)
void SetText(const wxString &aText) override
void SetSpinStyle(SPIN_STYLE aSpinStyle) override
std::unique_ptr< EAGLE_DOC > m_eagleDoc
SCH_ITEM * loadWire(const std::unique_ptr< EWIRE > &aWire, SEG &endpoints)
void loadTextAttributes(EDA_TEXT *aText, const std::unique_ptr< ETEXT > &aAttributes) const
void loadModuleInstance(const std::unique_ptr< EMODULEINST > &aModuleInstance)
SCH_TEXT * loadLabel(const std::unique_ptr< ELABEL > &aLabel, const wxString &aNetName)
void ensureLoadedLibrary(const wxString &aLibraryPath)
void loadSchematic(const ESCHEMATIC &aSchematic)
SCH_TEXT * loadPlainText(const std::unique_ptr< ETEXT > &aSchText)
void loadSheet(const std::unique_ptr< ESHEET > &aSheet)
void loadLayerDefs(const std::vector< std::unique_ptr< ELAYER > > &aLayers)
const ESYMBOL * getEagleSymbol(const std::unique_ptr< EINSTANCE > &aInstance)
EAGLE_LIBRARY * loadLibrary(const ELIBRARY *aLibrary, EAGLE_LIBRARY *aEagleLib)
wxXmlDocument loadXmlDocument(const wxString &aFileName)
wxString translateEagleBusName(const wxString &aEagleName) const
Translate an Eagle-style bus name into one that is KiCad-compatible.
void loadFieldAttributes(SCH_FIELD *aField, const SCH_TEXT *aText) const
Move net labels that are detached from any wire to the nearest wire.
std::map< wxString, wxString > m_powerPorts
map from symbol reference to global label equivalent
SCH_SHEET_PATH m_sheetPath
The current sheet path of the schematic being loaded.
wxString m_libName
Library name to save symbols.
SCH_TEXT * loadSymbolText(std::unique_ptr< LIB_SYMBOL > &aSymbol, const std::unique_ptr< ETEXT > &aText, int aGateNumber)
std::pair< VECTOR2I, const SEG * > findNearestLinePoint(const VECTOR2I &aPoint, const std::vector< SEG > &aLines) const
std::map< wxString, long long > m_timestamps
void loadInstance(const std::unique_ptr< EINSTANCE > &aInstance, const std::map< wxString, std::unique_ptr< EPART > > &aParts)
LIB_SYMBOL * LoadSymbol(const wxString &aLibraryPath, const wxString &aAliasName, const std::map< std::string, UTF8 > *aProperties) override
Load a LIB_SYMBOL object having aPartName from the aLibraryPath containing a library format that this...
std::unordered_map< wxString, bool > m_userValue
deviceset/@uservalue for device.
int GetModifyHash() const override
Return the modification hash from the library cache.
std::map< wxString, int > m_netCounts
wxFileName m_filename
std::map< wxString, EAGLE_LIBRARY > m_eagleLibs
bool loadSymbol(const std::unique_ptr< ESYMBOL > &aEsymbol, std::unique_ptr< LIB_SYMBOL > &aSymbol, const std::unique_ptr< EDEVICE > &aDevice, int aGateNumber, const wxString &aGateName)
SCH_SHEET * getCurrentSheet()
void loadDrawing(const std::unique_ptr< EDRAWING > &aDrawing)
void EnumerateSymbolLib(wxArrayString &aSymbolNameList, const wxString &aLibraryPath, const std::map< std::string, UTF8 > *aProperties) override
Populate a list of LIB_SYMBOL alias names contained within the library aLibraryPath.
SCH_SHAPE * loadSymbolPolyLine(std::unique_ptr< LIB_SYMBOL > &aSymbol, const std::unique_ptr< EPOLYGON > &aPolygon, int aGateNumber)
std::vector< VECTOR2I > m_wireIntersections
Wires and labels of a single connection (segment in Eagle nomenclature)
std::map< VECTOR2I, std::set< const EDA_ITEM * > > m_connPoints
The fully parsed Eagle schematic file.
void loadSegments(const std::vector< std::unique_ptr< ESEGMENT > > &aSegments, const wxString &aNetName, const wxString &aNetClass)
bool checkConnections(const SCH_SYMBOL *aSymbol, const SCH_PIN *aPin) const
IO_RELEASER< SCH_IO > m_pi
PI to create KiCad symbol library.
SCH_SHAPE * loadRectangle(const std::unique_ptr< ERECT > &aRect)
void addBusEntries()
This function finds best way to place a bus entry symbol for when an Eagle wire segment ends on an Ea...
bool CanReadSchematicFile(const wxString &aFileName) const override
Checks if this SCH_IO can read the specified schematic file.
void addImplicitConnections(SCH_SYMBOL *aSymbol, SCH_SCREEN *aScreen, bool aUpdateSet)
Create net labels to emulate implicit connections in Eagle.
std::map< int, SCH_LAYER_ID > m_layerMap
SCH_LAYER_ID kiCadLayer(int aEagleLayer)
Return the matching layer or return LAYER_NOTES.
wxString getLibName()
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,...
SCH_PIN * loadPin(std::unique_ptr< LIB_SYMBOL > &aSymbol, const std::unique_ptr< EPIN > &aPin, int aGateNumber)
SCH_SHAPE * loadCircle(const std::unique_ptr< ECIRCLE > &aCircle)
wxFileName getLibFileName()
Checks if there are other wires or pins at the position of the tested pin.
SCH_SHAPE * loadSymbolRectangle(std::unique_ptr< LIB_SYMBOL > &aSymbol, const std::unique_ptr< ERECT > &aRectangle, int aGateNumber)
SCH_JUNCTION * loadJunction(const std::unique_ptr< EJUNCTION > &aJunction)
wxString m_version
Eagle file version.
void getEagleSymbolFieldAttributes(const std::unique_ptr< EINSTANCE > &aInstance, const wxString &aEagleFieldName, SCH_FIELD *aField)
std::map< wxString, const EPART * > m_partlist
void moveLabels(SCH_LINE *aWire, const VECTOR2I &aNewEndPoint)
Move any labels on the wire to the new end point of the wire.
bool checkHeader(const wxString &aFileName) const
SCHEMATIC * m_schematic
Passed to Load(), the schematic object being loaded.
void countNets(const ESCHEMATIC &aSchematic)
SCH_SHEET * m_rootSheet
The root sheet of the schematic being loaded.
SCH_SHAPE * loadPolyLine(const std::unique_ptr< EPOLYGON > &aPolygon)
SCH_SHAPE * loadSymbolCircle(std::unique_ptr< LIB_SYMBOL > &aSymbol, const std::unique_ptr< ECIRCLE > &aCircle, int aGateNumber)
SCH_SCREEN * getCurrentScreen()
bool CanReadLibrary(const wxString &aFileName) const override
Checks if this IO object can read the specified library file/directory.
std::map< wxString, EAGLE_MISSING_CMP > m_missingCmps
void loadFrame(const std::unique_ptr< EFRAME > &aFrame, std::vector< SCH_ITEM * > &aItems)
const int ARC_ACCURACY
std::vector< SEG_DESC > m_segments
Nets as defined in the <nets> sections of an Eagle schematic file.
std::vector< EMODULE * > m_modules
The current module stack being loaded.
std::vector< EMODULEINST * > m_moduleInstances
SCH_ITEM * loadSymbolWire(std::unique_ptr< LIB_SYMBOL > &aSymbol, const std::unique_ptr< EWIRE > &aWire, int aGateNumber)
long long getLibraryTimestamp(const wxString &aLibraryPath) const
static const char * PropBuffering
The property used internally by the plugin to enable cache buffering which prevents the library file ...
virtual bool CanReadSchematicFile(const wxString &aFileName) const
Checks if this SCH_IO can read the specified schematic file.
Definition sch_io.cpp:45
SCH_IO(const wxString &aName)
Definition sch_io.h:375
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:168
SCH_ITEM * Duplicate(bool addToParentGroup, SCH_COMMIT *aCommit=nullptr, bool doClone=false) const
Routine to create a new copy of given item.
Definition sch_item.cpp:161
int GetUnit() const
Definition sch_item.h:239
virtual void SetUnit(int aUnit)
Definition sch_item.h:238
void SetShape(LABEL_FLAG_SHAPE aShape)
Definition sch_label.h:181
void SetPosition(const VECTOR2I &aPosition) override
Segment description base class to describe items which have 2 end points (track, wire,...
Definition sch_line.h:42
bool IsWire() const
Return true if the line is a wire.
Definition sch_line.cpp:992
const BOX2I GetBoundingBox() const override
Return the orthogonal bounding box of this object for display purposes.
Definition sch_line.cpp:234
VECTOR2I GetEndPoint() const
Definition sch_line.h:148
VECTOR2I GetStartPoint() const
Definition sch_line.h:139
bool IsBus() const
Return true if the line is a bus.
Definition sch_line.cpp:998
void SetEndPoint(const VECTOR2I &aPosition)
Definition sch_line.h:149
void SetNumber(const wxString &aNumber)
Definition sch_pin.cpp:643
Container class that holds multiple SCH_SCREEN objects in a hierarchy.
Definition sch_screen.h:749
SCH_SCREEN * GetNext()
void UpdateSymbolLinks(REPORTER *aReporter=nullptr)
Initialize the LIB_SYMBOL reference for each SCH_SYMBOL found in the full schematic.
SCH_SCREEN * GetFirst()
const PAGE_INFO & GetPageSettings() const
Definition sch_screen.h:141
void Append(SCH_ITEM *aItem, bool aUpdateLibSymbol=true)
void SetPageSettings(const PAGE_INFO &aPageSettings)
Definition sch_screen.h:142
EE_RTREE & Items()
Get the full RTree, usually for iterating.
Definition sch_screen.h:119
void SetFileName(const wxString &aFileName)
Set the file name for this screen to aFileName.
void Update(SCH_ITEM *aItem, bool aUpdateLibSymbol=true)
Update aItem's bounding box in the tree.
void SetPosition(const VECTOR2I &aPos) override
Definition sch_shape.h:86
void SetStroke(const STROKE_PARAMS &aStroke) override
Definition sch_shape.cpp:63
VECTOR2I GetCenter() const
Definition sch_shape.h:88
void AddPoint(const VECTOR2I &aPosition)
VECTOR2I GetPosition() const override
Definition sch_shape.h:85
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
Define a sheet pin (label) used in sheets to create hierarchical schematics.
void SetPosition(const VECTOR2I &aPosition) override
void SetSide(SHEET_SIDE aEdge)
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition sch_sheet.h:48
void SetFileName(const wxString &aFilename)
Definition sch_sheet.h:377
bool LocatePathOfScreen(SCH_SCREEN *aScreen, SCH_SHEET_PATH *aList)
Search the existing hierarchy for an instance of screen loaded from aFileName.
void SetName(const wxString &aName)
Definition sch_sheet.h:138
SCH_SCREEN * GetScreen() const
Definition sch_sheet.h:140
void AutoplaceFields(SCH_SCREEN *aScreen, AUTOPLACE_ALGO aAlgo) override
Schematic symbol object.
Definition sch_symbol.h:76
const std::vector< SCH_SYMBOL_INSTANCE > & GetInstances() const
Definition sch_symbol.h:135
void AddHierarchicalReference(const KIID_PATH &aPath, const wxString &aRef, int aUnit)
Add a full hierarchical reference to this symbol.
std::unique_ptr< LIB_SYMBOL > & GetLibSymbolRef()
Definition sch_symbol.h:184
VECTOR2I GetPinPhysicalPosition(const SCH_PIN *Pin) const
SCH_FIELD * GetField(FIELD_T aFieldType)
Return a mandatory field in this symbol.
VECTOR2I GetPosition() const override
Definition sch_text.h:147
void SetPosition(const VECTOR2I &aPosition) override
Definition sch_text.h:148
Definition seg.h:42
VECTOR2I A
Definition seg.h:49
VECTOR2I B
Definition seg.h:50
OPT_VECTOR2I Intersect(const SEG &aSeg, bool aIgnoreEndpoints=false, bool aLines=false) const
Compute intersection point of segment (this) with segment aSeg.
Definition seg.cpp:446
bool Contains(const SEG &aSeg) const
Definition seg.h:324
SHAPE_ARC & ConstructFromStartEndAngle(const VECTOR2I &aStart, const VECTOR2I &aEnd, const EDA_ANGLE &aAngle, double aWidth=0)
Construct this arc from the given start, end and angle.
int Append(int x, int y, int aOutline=-1, int aHole=-1, bool aAllowDuplication=false)
Appends a vertex at the end of the given outline/hole (default: the last outline)
Simple container to manage line stroke parameters.
An interface to the global shared library manager that is schematic-specific and linked to one projec...
std::optional< LIB_STATUS > LoadOne(LIB_DATA *aLib) override
Loads or reloads the given library, if it exists.
wxString wx_str() const
Definition utf8.cpp:45
double Distance(const VECTOR2< extended_type > &aVector) const
Compute the distance between two vectors.
Definition vector2d.h:553
VECTOR2< T > Resize(T aNewLength) const
Return a vector of the same direction, but length specified in aNewLength.
Definition vector2d.h:385
static REPORTER & GetInstance()
Definition reporter.cpp:195
#define DEFAULT_SCH_ENTRY_SIZE
The default text size in mils. (can be changed in preference menu)
wxString escapeName(const wxString &aNetName)
Translates Eagle special characters to their counterparts in KiCad.
wxString interpretText(const wxString &aText)
Interprets special characters in Eagle text and converts them to KiCAD notation.
size_t GetNodeCount(const wxXmlNode *aNode)
Fetch the number of XML nodes within aNode.
VECTOR2I ConvertArcCenter(const VECTOR2I &aStart, const VECTOR2I &aEnd, double aAngle)
Convert an Eagle curve end to a KiCad center for S_ARC.
wxString convertDescription(wxString aDescr)
Converts Eagle's HTML description into KiCad description format.
OPTIONAL_XML_ATTRIBUTE< double > opt_double
#define _(s)
@ DEGREES_T
Definition eda_angle.h:31
static constexpr EDA_ANGLE ANGLE_VERTICAL
Definition eda_angle.h:408
#define IGNORE_PARENT_GROUP
Definition eda_item.h:57
#define IS_NEW
New item, just created.
@ RECTANGLE
Use RECTANGLE instead of RECT to avoid collision in a Windows header.
Definition eda_shape.h:48
@ FILLED_SHAPE
Fill with object color.
Definition eda_shape.h:60
@ ERCE_BUS_ENTRY_NEEDED
Importer failed to auto-place a bus entry.
static const std::string KiCadSchematicFileExtension
static const std::string KiCadSymbolLibFileExtension
#define THROW_IO_ERROR(msg)
macro which captures the "call site" values of FILE_, __FUNCTION & LINE
KIID niluuid(0)
SCH_LAYER_ID
Eeschema drawing layers.
Definition layer_ids.h:451
@ LAYER_DEVICE
Definition layer_ids.h:468
@ LAYER_WIRE
Definition layer_ids.h:454
@ LAYER_NOTES
Definition layer_ids.h:469
@ LAYER_BUS
Definition layer_ids.h:455
STL namespace.
@ PT_INPUT
usual pin input: must be connected
Definition pin_type.h:37
@ PT_NC
not connected (must be left open)
Definition pin_type.h:50
@ PT_OUTPUT
usual output
Definition pin_type.h:38
@ PT_TRISTATE
tri state bus pin
Definition pin_type.h:40
@ PT_BIDI
input or output (like port for a microprocessor)
Definition pin_type.h:39
@ PT_OPENCOLLECTOR
pin type open collector
Definition pin_type.h:48
@ PT_POWER_IN
power input (GND, VCC for ICs). Must be connected to a power output.
Definition pin_type.h:46
@ PT_PASSIVE
pin for passive symbols: must be connected, and can be connected to any pin.
Definition pin_type.h:43
@ PIN_UP
The pin extends upwards from the connection point: Probably on the bottom side of the symbol.
Definition pin_type.h:127
@ PIN_RIGHT
The pin extends rightwards from the connection point.
Definition pin_type.h:111
@ PIN_LEFT
The pin extends leftwards from the connection point: Probably on the right side of the symbol.
Definition pin_type.h:118
@ PIN_DOWN
The pin extends downwards from the connection: Probably on the top side of the symbol.
Definition pin_type.h:135
@ RPT_SEVERITY_ERROR
static wxString extractNetName(const wxString &aPinName)
static const std::map< wxString, ELECTRICAL_PINTYPE > pinDirectionsMap
Map of EAGLE pin type values to KiCad pin type values.
static SYMBOL_ORIENTATION_T kiCadComponentRotation(float eagleDegrees)
static void eagleToKicadAlignment(EDA_TEXT *aText, int aEagleAlignment, int aRelDegress, bool aMirror, bool aSpin, int aAbsDegress)
static BOX2I getSheetBbox(SCH_SHEET *aSheet)
Extract the net name part from a pin name (e.g. return 'GND' for pin named 'GND@2')
@ AUTOPLACE_AUTO
Definition sch_item.h:71
LABEL_FLAG_SHAPE
Definition sch_label.h:99
@ L_BIDI
Definition sch_label.h:102
@ L_TRISTATE
Definition sch_label.h:103
@ L_UNSPECIFIED
Definition sch_label.h:104
@ L_OUTPUT
Definition sch_label.h:101
@ L_INPUT
Definition sch_label.h:100
LABEL_SHAPE
Definition sch_label.h:117
@ LABEL_BIDI
Definition sch_label.h:120
@ LABEL_INPUT
Definition sch_label.h:118
@ LABEL_OUTPUT
Definition sch_label.h:119
@ LABEL_PASSIVE
Definition sch_label.h:122
@ LABEL_TRISTATE
Definition sch_label.h:121
Definition of the SCH_SHEET_PATH and SCH_SHEET_LIST classes for Eeschema.
SHEET_SIDE
Define the edge of the sheet that the sheet pin is positioned.
std::optional< VECTOR2I > OPT_VECTOR2I
Definition seg.h:39
wxString UnescapeString(const wxString &aSource)
bool ReplaceIllegalFileNameChars(std::string &aName, int aReplaceChar)
Checks aName for illegal file name characters.
wxString EscapeString(const wxString &aSource, ESCAPE_CONTEXT aContext)
The Escape/Unescape routines use HTML-entity-reference-style encoding to handle characters which are:...
wxString UnescapeHTML(const wxString &aString)
Return a new wxString unescaped from HTML format.
@ CTX_LIBID
@ CTX_NETNAME
std::map< wxString, std::unique_ptr< LIB_SYMBOL > > KiCadSymbols
std::unordered_map< wxString, wxString > package
wxString name
std::unordered_map< wxString, int > GateToUnitMap
Map Eagle gate unit number (which are strings) to KiCad library symbol unit number.
std::map< wxString, std::unique_ptr< EDEVICE_SET > > devicesets
wxString GetName() const
Fetch the fully unique library name.
std::map< wxString, std::unique_ptr< ESYMBOL > > symbols
std::map< wxString, std::unique_ptr< EMODULE > > modules
std::vector< std::unique_ptr< ESHEET > > sheets
std::map< wxString, std::unique_ptr< EPART > > parts
std::map< wxString, std::unique_ptr< ELIBRARY > > libraries
std::vector< std::unique_ptr< ETEXT > > texts
@ BOTTOM_CENTER
@ BOTTOM_RIGHT
@ CENTER_RIGHT
@ CENTER_LEFT
@ BOTTOM_LEFT
Map references to missing symbol units data.
const SCH_SYMBOL * cmp
Screen where the parent symbol is located.
std::map< int, bool > units
Segments representing wires for intersection checking.
std::vector< SEG > segs
std::vector< SCH_TEXT * > labels
const SEG * LabelAttached(const SCH_TEXT *aLabel) const
< Test if a particular label is attached to any of the stored segments
A simple container for schematic symbol instance information.
SYMBOL_ORIENTATION_T
enum used in RotationMiroir()
Definition symbol.h:35
@ SYM_ORIENT_270
Definition symbol.h:42
@ SYM_ORIENT_180
Definition symbol.h:41
@ SYM_ORIENT_90
Definition symbol.h:40
@ SYM_ORIENT_0
Definition symbol.h:39
@ USER
The field ID hasn't been set yet; field is invalid.
@ FOOTPRINT
Field Name Module PCB, i.e. "16DIP300".
@ REFERENCE
Field Reference of part, i.e. "IC21".
@ VALUE
Field Value of part, i.e. "3.3K".
KIBIS_PIN * pin
std::vector< std::string > header
std::vector< std::vector< std::string > > table
VECTOR2I center
int radius
VECTOR2I end
SHAPE_CIRCLE circle(c.m_circle_center, c.m_circle_radius)
@ GR_TEXT_H_ALIGN_CENTER
@ GR_TEXT_H_ALIGN_RIGHT
@ GR_TEXT_H_ALIGN_LEFT
@ GR_TEXT_V_ALIGN_BOTTOM
@ GR_TEXT_V_ALIGN_CENTER
@ GR_TEXT_V_ALIGN_TOP
bool TestSegmentHit(const VECTOR2I &aRefPoint, const VECTOR2I &aStart, const VECTOR2I &aEnd, int aDist)
Test if aRefPoint is with aDistance on the line defined by aStart and aEnd.
Definition trigo.cpp:175
void RotatePoint(int *pX, int *pY, const EDA_ANGLE &aAngle)
Calculate the new point of coord coord pX, pY, for a rotation center 0, 0.
Definition trigo.cpp:229
@ SCH_LINE_T
Definition typeinfo.h:164
@ SCH_SYMBOL_T
Definition typeinfo.h:173
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:687
Definition of file extensions used in Kicad.