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