KiCad PCB EDA Suite
Loading...
Searching...
No Matches
target3001_sch_builder.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 The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
21
22#include <algorithm>
23#include <cmath>
24#include <set>
25
26#include <wx/filename.h>
27#include <wx/image.h>
28#include <wx/mstream.h>
29
30#include <base_units.h>
31#include <geometry/seg.h>
32#include <lib_id.h>
33#include <lib_symbol.h>
34#include <page_info.h>
35#include <reporter.h>
36#include <bitmap_base.h>
37#include <sch_bitmap.h>
38#include <sch_junction.h>
39#include <sch_label.h>
40#include <sch_line.h>
41#include <sch_no_connect.h>
42#include <sch_pin.h>
43#include <sch_screen.h>
44#include <sch_shape.h>
45#include <sch_sheet.h>
46#include <sch_sheet_path.h>
47#include <sch_symbol.h>
48#include <sch_text.h>
49#include <schematic.h>
50#include <trigo.h>
52
57
58namespace
59{
60
61constexpr int PAGE_MARGIN_MM = 10;
62
63// Keeps imported content on the standard connection grid when it is moved onto the page
64constexpr int PLACEMENT_GRID = 12700;
65
66// Signal of schematic buses, which join their member wires by name rather than by touching
67constexpr int BUS_SIGNAL = 65502;
68
69// Gap that parts touching items of different signals; one mil stays on the imperial grid
70constexpr int SIGNAL_GAP = 254;
71
72// Diagonal steps tried for a free spot before the last one is taken anyway
73constexpr int MAX_DIAGONAL_STEPS = 8;
74
75// Ends of one signal closer than this are one connection drawn off grid
76constexpr int NEAR_MISS = 1000;
77
78// Farthest a signal name is drawn from its wire and still names it
79constexpr int LABEL_REACH = 50800;
80
81const wxString LIB_NAME = wxS( "target3001" );
82
83
84PIN_ORIENTATION pinOrientation( float aDirection )
85{
86 int quadrant = ( (int) std::lround( aDirection / 90.0f ) % 4 + 4 ) % 4;
87
88 switch( quadrant )
89 {
90 case 1: return PIN_ORIENTATION::PIN_UP;
91 case 2: return PIN_ORIENTATION::PIN_LEFT;
92 case 3: return PIN_ORIENTATION::PIN_DOWN;
93 default: return PIN_ORIENTATION::PIN_RIGHT;
94 }
95}
96
97
98ELECTRICAL_PINTYPE pinType( const ELEMENT& aPin )
99{
100 if( aPin.pin.supplyPin )
102
103 switch( aPin.pin.electricalType )
104 {
105 // An unconnected pin wired to a signal still joins it, which KiCad allows only when not internally connected
107 case 1: return ELECTRICAL_PINTYPE::PT_INPUT;
108 case 2: return ELECTRICAL_PINTYPE::PT_OUTPUT;
109 case 3: return ELECTRICAL_PINTYPE::PT_BIDI;
111 case 5: return ELECTRICAL_PINTYPE::PT_POWER_IN;
112 case 6: return ELECTRICAL_PINTYPE::PT_PASSIVE;
113 case 7: return ELECTRICAL_PINTYPE::PT_TRISTATE;
115 }
116}
117
118
119GRAPHIC_PINSHAPE pinShape( const TARGET3001::PIN& aPin )
120{
121 if( aPin.inverted && aPin.clock )
123
124 if( aPin.inverted )
126
127 if( aPin.clock )
129
131}
132
133
134LINE_STYLE lineStyle( uint8_t aDash )
135{
136 switch( aDash )
137 {
138 case 1: return LINE_STYLE::DASH;
139 case 2: return LINE_STYLE::DASHDOT;
140 case 3: return LINE_STYLE::DASHDOTDOT;
141 case 4: return LINE_STYLE::DOT;
142 default: return LINE_STYLE::SOLID;
143 }
144}
145
146
148int textQuadrant( float aDirection )
149{
150 return ( (int) std::lround( aDirection / 90.0f ) % 4 + 4 ) % 4;
151}
152
153
155bool isHiddenText( const ELEMENT& aElem )
156{
157 return ( aElem.base.flags & 1 ) || aElem.text.weight == 0;
158}
159
160
161const ELEMENT* pinNameText( const ELEMENT& aPin )
162{
163 for( const ELEMENT& child : aPin.children )
164 {
165 if( child.type == ELEMENT_TYPE::TEXT && child.text.function == uint8_t( TEXT_FUNCTION::PIN_NAME ) )
166 return &child;
167 }
168
169 return nullptr;
170}
171
172
174bool isUnplacedGate( const ELEMENT& aGate )
175{
176 return aGate.base.flags & 1;
177}
178
179
180VECTOR2I wireEnd( const SCH_LINE* aWire, int aEnd )
181{
182 return aEnd ? aWire->GetEndPoint() : aWire->GetStartPoint();
183}
184
185
186void setWireEnd( SCH_LINE* aWire, int aEnd, const VECTOR2I& aPos )
187{
188 if( aEnd )
189 aWire->SetEndPoint( aPos );
190 else
191 aWire->SetStartPoint( aPos );
192}
193
194
196VECTOR2I stepIn( const SCH_LINE* aWire, int aEnd )
197{
198 VECTOR2I end = wireEnd( aWire, aEnd );
199 VECTOR2I span = wireEnd( aWire, 1 - aEnd ) - end;
200
201 if( span.EuclideanNorm() <= 2 * SIGNAL_GAP )
202 return end;
203
204 return end + span.Resize( SIGNAL_GAP );
205}
206
207
208bool passesThrough( const SCH_LINE* aLine, const VECTOR2I& aPos )
209{
210 return !aLine->IsEndPoint( aPos ) && IsPointOnSegment( aLine->GetStartPoint(), aLine->GetEndPoint(), aPos );
211}
212
213
215SPIN_STYLE spinAlong( const VECTOR2I& aDir )
216{
217 if( std::abs( aDir.x ) >= std::abs( aDir.y ) )
218 return aDir.x < 0 ? SPIN_STYLE::LEFT : SPIN_STYLE::RIGHT;
219
220 return aDir.y < 0 ? SPIN_STYLE::UP : SPIN_STYLE::BOTTOM;
221}
222
223
225bool overlaps( const SCH_LINE* aFirst, const SCH_LINE* aSecond )
226{
227 VECTOR2I a = aFirst->GetStartPoint();
228 VECTOR2I b = aFirst->GetEndPoint();
229 VECTOR2I c = aSecond->GetStartPoint();
230 VECTOR2I d = aSecond->GetEndPoint();
231 VECTOR2I dir = b - a;
232
233 if( dir.Cross( c - a ) || dir.Cross( d - a ) )
234 return false;
235
236 auto along = [&]( const VECTOR2I& aPos )
237 {
238 return dir.Dot( aPos - a );
239 };
240
241 return std::min( std::max( along( a ), along( b ) ), std::max( along( c ), along( d ) ) )
242 > std::max( std::min( along( a ), along( b ) ), std::min( along( c ), along( d ) ) );
243}
244
245
252std::pair<int32_t, int32_t> configureText( EDA_TEXT* aText, const ELEMENT& aElem, TARGET3001::FONTS& aFonts,
253 bool aUseRightEnd = true )
254{
255 const TARGET3001::TEXT& src = aElem.text;
256
257 aFonts.Apply( aText, aElem, 100 );
258
259 int quadrant = textQuadrant( src.direction );
260 bool upsideDown = quadrant >= 2;
261 bool fromEnd = upsideDown != ( src.mirrored != 0 );
262
263 aText->SetTextAngle( quadrant % 2 ? ANGLE_VERTICAL : ANGLE_HORIZONTAL );
265
266 // A stored right end lies on the far side of the anchor, so the justification flips with it
267 std::optional<std::pair<int32_t, int32_t>> end = aElem.base.TextRightEnd();
268
269 if( end && aUseRightEnd )
270 {
272 return *end;
273 }
274
276 return { aElem.base.x, aElem.base.y };
277}
278
279} // namespace
280
281
283 const TARGET3001::PROJECT& aProject, const wxString& aFileName,
284 REPORTER* aReporter ) :
285 m_schematic( aSchematic ),
286 m_rootSheet( aRootSheet ),
287 m_project( aProject ),
288 m_fileName( aFileName ),
289 m_reporter( aReporter )
290{
291}
292
293
295{
296 return VECTOR2I( KiROUND( aX / 100.0 ), KiROUND( -aY / 100.0 ) );
297}
298
299
300int TARGET3001_SCH_BUILDER::toSch( int32_t aLength )
301{
302 return KiROUND( aLength / 100.0 );
303}
304
305
307{
308 int sheet = aElem.base.layer - TARGET3001::SCHEMATIC_LAYER_BASE;
309
310 if( sheet < 1 || sheet > TARGET3001::SCHEMATIC_SHEET_COUNT )
311 return 0;
312
313 return sheet;
314}
315
316
318{
319 // Create sheets in order so the lowest numbered one becomes the root sheet
320 std::set<int> used;
321
322 for( const ELEMENT& e : m_project.components )
323 {
324 if( e.type == ELEMENT_TYPE::COMPONENT && e.component )
325 {
326 for( const ELEMENT& s : e.component->symbols )
327 {
328 if( !isUnplacedGate( s ) )
329 used.insert( sheetOf( s ) );
330 }
331 }
332 }
333
334 for( const ELEMENT& e : m_project.signals )
335 {
336 if( e.type == ELEMENT_TYPE::SIGNAL && e.signal )
337 {
338 for( const ELEMENT& c : e.signal->connections )
339 used.insert( sheetOf( c ) );
340 }
341 }
342
343 for( const ELEMENT& e : m_project.schematicItems )
344 used.insert( sheetOf( e ) );
345
346 used.erase( 0 );
347
348 for( int sheet : used )
349 screen( sheet );
350
351 for( const ELEMENT& e : m_project.components )
352 {
353 if( e.type == ELEMENT_TYPE::COMPONENT && e.component )
354 addComponent( e );
355 }
356
357 for( const ELEMENT& e : m_project.signals )
358 {
359 if( e.type == ELEMENT_TYPE::SIGNAL && e.signal )
360 addConnections( e );
361 }
362
363 for( const ELEMENT& e : m_project.schematicItems )
364 addFreeItem( e );
365
366 for( const auto& [sheet, items] : m_netItems )
367 separateSignals( sheet );
368
369 // Parting signals can split one, so its pieces are joined on the final geometry
371
372 finishSheets();
373}
374
375
377{
378 if( !aItem )
379 return;
380
381 BOX2I bbox = aItem->GetBoundingBox();
382 auto it = m_extents.find( aSheet );
383
384 if( it == m_extents.end() )
385 m_extents[aSheet] = bbox;
386 else
387 it->second.Merge( bbox );
388
389 m_pending[aSheet].push_back( aItem );
390}
391
392
393SCH_ITEM* TARGET3001_SCH_BUILDER::makeGraphic( const ELEMENT& aElem, const VECTOR2I& aOrigin, bool aInSymbol ) const
394{
395 const TARGET3001::ELEMENT_BASE& b = aElem.base;
396 SCH_LAYER_ID layer = aInSymbol ? LAYER_DEVICE : LAYER_NOTES;
397 VECTOR2I p0 = toSch( b.x, b.y ) - aOrigin;
398
399 switch( aElem.type )
400 {
401 case ELEMENT_TYPE::LINE:
402 {
403 const TARGET3001::LINE& l = aElem.line;
404 VECTOR2I p1 = toSch( l.x2, l.y2 ) - aOrigin;
405
406 if( !aInSymbol )
407 {
408 SCH_LINE* line = new SCH_LINE( p0, LAYER_NOTES );
409 line->SetEndPoint( p1 );
410 line->SetStroke( STROKE_PARAMS( toSch( l.width ), lineStyle( l.dashStyle ) ) );
411 return line;
412 }
413
414 SCH_SHAPE* shape = new SCH_SHAPE( SHAPE_T::POLY, layer, toSch( l.width ) );
415 shape->AddPoint( p0 );
416 shape->AddPoint( p1 );
417 shape->SetStroke( STROKE_PARAMS( toSch( l.width ), lineStyle( l.dashStyle ) ) );
418 return shape;
419 }
420
421 case ELEMENT_TYPE::RECTANGLE:
422 {
423 if( !aInSymbol )
424 {
425 if( SCH_BITMAP* picture = makePicture( aElem ) )
426 return picture;
427 }
428
429 const TARGET3001::RECTANGLE& r = aElem.rectangle;
430 EDA_ANGLE rot( double( r.direction ), DEGREES_T );
431 SCH_SHAPE* shape = new SCH_SHAPE( SHAPE_T::POLY, layer, 0, FILL_T::FILLED_SHAPE );
432 VECTOR2D corners[4] = { { 0.0, 0.0 }, { double( r.width ), 0.0 },
433 { double( r.width ), double( r.height ) }, { 0.0, double( r.height ) } };
434
435 for( VECTOR2D c : corners )
436 {
437 RotatePoint( c, -rot );
438 shape->AddPoint( toSch( b.x + KiROUND( c.x ), b.y + KiROUND( c.y ) ) - aOrigin );
439 }
440
441 shape->AddPoint( p0 );
442 return shape;
443 }
444
445 case ELEMENT_TYPE::TRIANGLE:
446 {
447 const TARGET3001::TRIANGLE& t = aElem.triangle;
448 SCH_SHAPE* shape = new SCH_SHAPE( SHAPE_T::POLY, layer, 0, FILL_T::FILLED_SHAPE );
449 shape->AddPoint( p0 );
450 shape->AddPoint( toSch( t.x2, t.y2 ) - aOrigin );
451 shape->AddPoint( toSch( t.x3, t.y3 ) - aOrigin );
452 shape->AddPoint( p0 );
453 return shape;
454 }
455
456 case ELEMENT_TYPE::DISC:
457 {
458 SCH_SHAPE* shape = new SCH_SHAPE( SHAPE_T::CIRCLE, layer, 0, FILL_T::FILLED_SHAPE );
459 shape->SetCenter( p0 );
460 shape->SetEnd( p0 + VECTOR2I( toSch( aElem.disc.radius ), 0 ) );
461 return shape;
462 }
463
464 case ELEMENT_TYPE::ARC:
465 {
466 const TARGET3001::ARC& a = aElem.arc;
467 VECTOR2I center = toSch( a.centerX, a.centerY ) - aOrigin;
468 int radius = toSch( a.radius );
469 double sweep = a.Sweep();
470
471 if( sweep >= 360.0 - 1e-6 )
472 {
473 SCH_SHAPE* shape = new SCH_SHAPE( SHAPE_T::CIRCLE, layer, toSch( a.width ) );
474 shape->SetCenter( center );
475 shape->SetEnd( center + VECTOR2I( radius, 0 ) );
476 return shape;
477 }
478
479 auto at = [&]( double aDeg )
480 {
481 return center + VECTOR2I( KiROUND( radius * cos( DEG2RAD( aDeg ) ) ),
482 -KiROUND( radius * sin( DEG2RAD( aDeg ) ) ) );
483 };
484
485 SCH_SHAPE* shape = new SCH_SHAPE( SHAPE_T::ARC, layer, toSch( a.width ) );
486 shape->SetArcGeometry( at( a.startAngle ), at( a.startAngle + sweep / 2.0 ),
487 at( a.startAngle + sweep ) );
488 return shape;
489 }
490
491 case ELEMENT_TYPE::TEXT:
492 {
493 if( isHiddenText( aElem ) )
494 return nullptr;
495
496 wxString content = m_project.FunctionText( aElem.text, true ).value_or( ToWx( aElem.text.text ) );
497
498 SCH_TEXT* text = new SCH_TEXT( p0, content, layer );
499 std::pair<int32_t, int32_t> pos = configureText( text, aElem, m_fonts );
500 text->SetPosition( toSch( pos.first, pos.second ) - aOrigin + TARGET3001::FONTS::BaselineShift( *text ) );
501 return text;
502 }
503
504 default:
505 return nullptr;
506 }
507}
508
509
511{
512 if( aElem.type != ELEMENT_TYPE::RECTANGLE )
513 return nullptr;
514
515 const TARGET3001::RECTANGLE& r = aElem.rectangle;
516 std::vector<uint8_t> data = aElem.base.Picture();
517 VECTOR2I size( std::abs( toSch( r.width ) ), std::abs( toSch( r.height ) ) );
518 wxImage image;
519
520 if( data.empty() || size.x == 0 || size.y == 0 )
521 return nullptr;
522
523 wxMemoryInputStream stream( data.data(), data.size() );
524
525 if( !image.LoadFile( stream, wxBITMAP_TYPE_ANY ) )
526 return nullptr;
527
528 // The picture is stretched over its rectangle; KiCad only scales images uniformly
529 int w = image.GetWidth();
530 int h = image.GetHeight();
531 int stretchedW = std::max( w, KiROUND( double( h ) * size.x / size.y ) );
532 int stretchedH = std::max( h, KiROUND( double( w ) * size.y / size.x ) );
533
534 if( stretchedW > w + 1 || stretchedH > h + 1 )
535 image.Rescale( stretchedW, stretchedH, wxIMAGE_QUALITY_HIGH );
536
537 EDA_ANGLE rot( double( r.direction ), DEGREES_T );
538 VECTOR2D half( r.width / 2.0, r.height / 2.0 );
539 RotatePoint( half, -rot );
540
541 SCH_BITMAP* bitmap = new SCH_BITMAP();
542 REFERENCE_IMAGE& ref = bitmap->GetReferenceImage();
543
544 if( !ref.SetImage( image ) )
545 {
546 delete bitmap;
547 return nullptr;
548 }
549
550 ref.MutableImage().SetImageType( image.GetType() );
551 ref.SetWidth( size.x );
552
553 VECTOR2I center = toSch( aElem.base.x + KiROUND( half.x ), aElem.base.y + KiROUND( half.y ) );
554 ref.SetPosition( center );
555
556 if( !rot.IsZero() )
557 ref.Rotate( center, rot );
558
559 return bitmap;
560}
561
562
564{
565 const TARGET3001::COMPONENT& comp = *aComponent.component;
566 std::vector<const ELEMENT*> gates;
567
568 for( const ELEMENT& s : comp.symbols )
569 {
570 if( s.type == ELEMENT_TYPE::SYMBOL && s.symbol && sheetOf( s ) > 0 )
571 gates.push_back( &s );
572 }
573
574 if( gates.empty() )
575 return;
576
577 std::stable_sort( gates.begin(), gates.end(),
578 []( const ELEMENT* a, const ELEMENT* b )
579 {
580 return a->symbol->gateOrdinal < b->symbol->gateOrdinal;
581 } );
582
583 bool hasPackage = std::any_of( comp.packageItems.begin(), comp.packageItems.end(),
584 []( const ELEMENT& e ) { return e.type == ELEMENT_TYPE::PACKAGE; } );
585 bool allSupply = true;
586 int supplySignal = 0;
587
588 for( const ELEMENT* gate : gates )
589 {
590 for( const ELEMENT& child : gate->symbol->children )
591 {
592 if( child.type != ELEMENT_TYPE::PIN )
593 continue;
594
595 if( !child.pin.supplyPin )
596 allSupply = false;
597 else if( !supplySignal )
598 supplySignal = child.base.signal;
599 }
600 }
601
602 bool isPower = !hasPackage && allSupply && supplySignal;
603
604 wxString reference = ToWx( comp.prefix ).Trim().Trim( false );
605 uint16_t number = comp.referenceExtension.value_or( comp.referenceNumber );
606
607 if( isPower )
608 reference = wxString::Format( wxS( "#PWR%03d" ), ++m_powerCount );
609 else if( number )
610 reference << number;
611
612 wxString value = ToWx( comp.value );
613
614 const ELEMENT* sig = isPower ? m_project.FindSignal( supplySignal ) : nullptr;
615
616 if( sig && !sig->signal->name.empty() )
617 value = ToWx( sig->signal->name );
618
619 // Each placed component gets its own library symbol because gate geometry is stored as drawn
620 wxString symName = ToWx( comp.name );
621
622 if( symName.IsEmpty() )
623 symName = reference;
624
625 symName = wxString::Format( wxS( "%s_%d" ), symName, int( aComponent.base.component ) );
626 symName = LIB_ID::FixIllegalChars( symName, false ).wx_str();
627 symName.Replace( wxS( "/" ), wxS( "_" ) );
628
629 LIB_SYMBOL* lib = new LIB_SYMBOL( symName );
630 lib->SetLibId( LIB_ID( LIB_NAME, symName ) );
631 lib->SetUnitCount( int( gates.size() ), false );
632 lib->GetReferenceField().SetText( reference );
633 lib->GetValueField().SetText( value );
634
635 if( isPower )
636 lib->SetGlobalPower();
637
638 std::map<uint16_t, wxString> padNumbers;
639
640 for( const ELEMENT& item : comp.packageItems )
641 {
642 if( item.type == ELEMENT_TYPE::PAD )
643 padNumbers.emplace( item.pad.number, PadNumber( item ) );
644 }
645
646 addGateBodies( lib, gates, true, padNumbers );
647
648 for( size_t g = 0; g < gates.size(); g++ )
649 {
650 const ELEMENT& gate = *gates[g];
651
652 if( isUnplacedGate( gate ) )
653 continue;
654
656 path.push_back( m_sheets.at( sheetOf( gate ) ) );
657
658 SCH_SYMBOL* symbol = new SCH_SYMBOL( *lib, lib->GetLibId(), &path, int( g ) + 1, 0,
659 toSch( gate.base.x, gate.base.y ) );
660 symbol->SetLibSymbol( new LIB_SYMBOL( *lib ) );
661 symbol->SetRef( &path, reference );
662 symbol->GetField( FIELD_T::REFERENCE )->SetText( reference );
663 symbol->GetField( FIELD_T::VALUE )->SetText( value );
664
665 if( !comp.package.empty() )
666 symbol->SetFootprintFieldText( LIB_NAME + wxS( ":" ) + ToWx( comp.package ) );
667
668 bool refShown = false;
669 bool valueShown = false;
670
671 auto placeField = [&]( FIELD_T aId, const ELEMENT& aText )
672 {
673 SCH_FIELD* field = symbol->GetField( aId );
674 std::pair<int32_t, int32_t> pos = configureText( field, aText, m_fonts );
675 field->SetPosition( toSch( pos.first, pos.second ) + TARGET3001::FONTS::BaselineShift( *field ) );
676 field->SetVisible( !isHiddenText( aText ) );
677 ( aId == FIELD_T::REFERENCE ? refShown : valueShown ) = !isHiddenText( aText );
678 };
679
680 for( const ELEMENT& child : gate.symbol->children )
681 {
682 if( child.type != ELEMENT_TYPE::TEXT )
683 continue;
684
685 if( child.text.function == uint8_t( TEXT_FUNCTION::REFERENCE ) )
686 placeField( FIELD_T::REFERENCE, child );
687 else if( child.text.function == uint8_t( TEXT_FUNCTION::VALUE ) )
688 placeField( FIELD_T::VALUE, child );
689 }
690
691 // KiCad names a power net with the value, so the pin name TARGET draws becomes the value
692 if( isPower && !valueShown )
693 {
694 for( const ELEMENT& child : gate.symbol->children )
695 {
696 if( child.type != ELEMENT_TYPE::PIN )
697 continue;
698
699 if( const ELEMENT* name = pinNameText( child ); name && !isHiddenText( *name ) )
700 {
701 placeField( FIELD_T::VALUE, *name );
702 break;
703 }
704 }
705 }
706
707 if( !refShown )
708 symbol->GetField( FIELD_T::REFERENCE )->SetVisible( false );
709
710 if( !valueShown )
711 symbol->GetField( FIELD_T::VALUE )->SetVisible( false );
712
713 addToSheet( sheetOf( gate ), symbol );
714
715 if( isPower )
716 m_powerSheets[supplySignal].insert( sheetOf( gate ) );
717
718 for( const ELEMENT& child : gate.symbol->children )
719 {
720 if( SCH_BITMAP* picture = makePicture( child ) )
721 addToSheet( sheetOf( gate ), picture );
722
723 // An unconnected pin belongs to no signal, so it gets one of its own
724 if( child.type == ELEMENT_TYPE::PIN )
725 {
726 int signal = child.base.signal ? int( child.base.signal ) : -( ++m_unconnectedPins );
727 m_pinSignals[sheetOf( gate )].emplace_back( toSch( child.base.x, child.base.y ), signal );
728
729 // Open pins get a flag unless the NC pin type already draws a cross
730 if( !child.base.signal && pinType( child ) != ELECTRICAL_PINTYPE::PT_NC )
731 addToSheet( sheetOf( gate ), new SCH_NO_CONNECT( toSch( child.base.x, child.base.y ) ) );
732
733 if( child.base.signal )
734 {
735 m_pinEnds[signal].push_back(
736 { sheetOf( gate ), toSch( child.base.x, child.base.y ), child.pin.direction, isPower } );
737 }
738 }
739 }
740 }
741
742 delete lib;
743}
744
745
746void TARGET3001_SCH_BUILDER::addGateBodies( LIB_SYMBOL* aLib, const std::vector<const ELEMENT*>& aGates,
747 bool aFieldTexts, const std::map<uint16_t, wxString>& aPadNumbers ) const
748{
749 bool showNumbers = false;
750 bool showNames = false;
751 int alongVotes = 0;
752 int acrossVotes = 0;
753 std::vector<SCH_PIN*> hiddenNames;
754
755 for( size_t g = 0; g < aGates.size(); g++ )
756 {
757 const ELEMENT& gate = *aGates[g];
758 VECTOR2I origin = toSch( gate.base.x, gate.base.y );
759 int unit = int( g ) + 1;
760
761 for( const ELEMENT& child : gate.symbol->children )
762 {
763 if( child.type == ELEMENT_TYPE::PIN )
764 {
765 const TARGET3001::PIN& src = child.pin;
766 SCH_PIN* pin = new SCH_PIN( aLib );
767 const ELEMENT* nameText = pinNameText( child );
768 wxString name = nameText ? ToWx( nameText->text.text ) : wxString();
769
770 // A power symbol shows its name through the value field
771 bool nameShown = nameText && !isHiddenText( *nameText ) && !name.IsEmpty() && !aLib->IsPower();
772
773 pin->SetPosition( toSch( child.base.x, child.base.y ) - origin );
774 pin->SetOrientation( pinOrientation( src.direction ) );
775 pin->SetLength( toSch( src.length ) );
776
777 if( nameShown )
778 {
779 SCH_TEXT probe( VECTOR2I( 0, 0 ), name );
780 std::pair<int32_t, int32_t> pos = configureText( &probe, *nameText, m_fonts );
781 probe.SetPosition( toSch( pos.first, pos.second ) - origin );
782 pin->SetNameTextSize( probe.GetTextHeight() );
783
784 // A name centred short of the pin root sits beside the pin, outside the body
785 VECTOR2I pinVec = pin->GetPinRoot() - pin->GetPosition();
786 SEG::ecoord along = pinVec.Dot( probe.GetBoundingBox().Centre() - pin->GetPosition() );
787 bool parallel =
788 textQuadrant( nameText->text.direction ) % 2 == textQuadrant( src.direction ) % 2;
789
790 // KiCad turns a name written across its pin, so those vote only when no name runs along one
791 ( parallel ? alongVotes : acrossVotes ) += along < pinVec.SquaredEuclideanNorm() ? 1 : -1;
792 }
793
794 pin->SetType( pinType( child ) );
795 pin->SetShape( pinShape( src ) );
796 pin->SetName( name.IsEmpty() ? wxString( wxS( "~" ) ) : name );
797 pin->SetUnit( unit );
798
799 // The footprint pad decides the number so pins and pads connect; the pin's PADNAME is what it shows
800 auto pad = aPadNumbers.find( src.pinNumber );
801 const std::string* padName = child.base.FindProp( "PADNAME" );
802
803 if( pad != aPadNumbers.end() )
804 pin->SetNumber( pad->second );
805 else if( padName && !padName->empty() )
806 pin->SetNumber( ToWx( *padName ) );
807 else
808 pin->SetNumber( wxString::Format( wxS( "%u" ), src.pinNumber ) );
809
810 if( !nameShown && !name.IsEmpty() )
811 hiddenNames.push_back( pin );
812
813 // KiCad shows names and numbers per symbol, so one shown pin shows them all
814 showNumbers |= src.showNumber != 0;
815 showNames |= nameShown;
816 aLib->AddDrawItem( pin, false );
817 continue;
818 }
819
820 if( aFieldTexts && child.type == ELEMENT_TYPE::TEXT
821 && ( child.text.function == uint8_t( TEXT_FUNCTION::REFERENCE )
822 || child.text.function == uint8_t( TEXT_FUNCTION::VALUE ) ) )
823 {
824 continue;
825 }
826
827 // Library symbols cannot hold images; placed gates put the picture on their sheet
828 if( child.type == ELEMENT_TYPE::RECTANGLE && child.base.FindProp( "JPEG_PICTURE" ) )
829 continue;
830
831 if( SCH_ITEM* item = makeGraphic( child, origin, true ) )
832 {
833 item->SetUnit( unit );
834 aLib->AddDrawItem( item, false );
835 }
836 }
837 }
838
839 // Names are hidden per pin in the source but per symbol in KiCad
840 if( showNames )
841 {
842 for( SCH_PIN* pin : hiddenNames )
843 pin->SetNameTextSize( 0 );
844 }
845
846 aLib->GetDrawItems().sort();
847 aLib->SetShowPinNumbers( showNumbers );
848 aLib->SetShowPinNames( showNames );
849
850 // Only a zero offset puts KiCad's names beside the pin instead of inside the body
851 if( ( alongVotes ? alongVotes : acrossVotes ) > 0 )
852 aLib->SetPinNameOffset( 0 );
853}
854
855
857{
858 wxString name = ToWx( aSignal.signal->name );
859 bool isBus = aSignal.base.signal == BUS_SIGNAL;
860 bool global = !isBus && joinsGlobally( aSignal );
861
862 if( global )
863 m_globalSignals.insert( aSignal.base.signal );
864
865 for( const ELEMENT& e : aSignal.signal->connections )
866 {
867 int sheet = sheetOf( e );
868
869 if( sheet == 0 )
870 continue;
871
872 const TARGET3001::ELEMENT_BASE& b = e.base;
873
874 switch( e.type )
875 {
876 case ELEMENT_TYPE::LINE:
877 {
878 SCH_LINE* wire = new SCH_LINE( toSch( b.x, b.y ), isBus ? LAYER_BUS : LAYER_WIRE );
879 wire->SetEndPoint( toSch( e.line.x2, e.line.y2 ) );
880 addNetItem( sheet, wire, aSignal.base.signal );
881 break;
882 }
883
884 case ELEMENT_TYPE::DISC:
885 addNetItem( sheet, new SCH_JUNCTION( toSch( b.x, b.y ), toSch( 2 * e.disc.radius ) ),
886 aSignal.base.signal );
887 break;
888
889 case ELEMENT_TYPE::TEXT:
890 {
891 bool signalName = e.text.function == uint8_t( TEXT_FUNCTION::SIGNAL_NAME );
892
893 // Labels carry connectivity between wire groups, so a hidden signal name still becomes one
894 if( isHiddenText( e ) && !signalName )
895 break;
896
897 // The name is drawn beside its wire; a label joins only where it touches
898 std::optional<VECTOR2I> anchor;
899
900 if( signalName )
901 anchor = labelAnchor( aSignal, sheet, toSch( b.x, b.y ) );
902
903 if( anchor )
904 {
905 SCH_LABEL_BASE* label = nullptr;
906
907 if( global )
908 label = new SCH_GLOBALLABEL( *anchor, name );
909 else
910 label = new SCH_LABEL( *anchor, name );
911
912 configureText( label, e, m_fonts, false );
913 addNetItem( sheet, label, aSignal.base.signal );
914 }
915 else
916 {
917 addToSheet( sheet, makeGraphic( e, VECTOR2I( 0, 0 ), false ) );
918 }
919
920 break;
921 }
922
923 default:
924 addToSheet( sheet, makeGraphic( e, VECTOR2I( 0, 0 ), false ) );
925 break;
926 }
927 }
928}
929
930
932{
933 if( aSignal.signal->name.empty() )
934 return false;
935
936 // Supply symbols name their net globally, which a local label would not join
937 if( m_powerSheets.count( aSignal.base.signal ) )
938 return true;
939
940 std::set<int> sheets;
941
942 for( const ELEMENT& e : aSignal.signal->connections )
943 {
944 if( sheetOf( e ) )
945 sheets.insert( sheetOf( e ) );
946 }
947
948 if( auto it = m_pinEnds.find( aSignal.base.signal ); it != m_pinEnds.end() )
949 {
950 for( const PIN_END& pin : it->second )
951 sheets.insert( pin.sheet );
952 }
953
954 return sheets.size() > 1;
955}
956
957
958std::optional<VECTOR2I> TARGET3001_SCH_BUILDER::labelAnchor( const ELEMENT& aSignal, int aSheet,
959 const VECTOR2I& aPos ) const
960{
961 std::optional<VECTOR2I> best;
962
963 auto consider = [&]( const VECTOR2I& aCandidate )
964 {
965 SEG::ecoord dist = ( aCandidate - aPos ).SquaredEuclideanNorm();
966
967 if( dist <= SEG::Square( LABEL_REACH ) && ( !best || dist < ( *best - aPos ).SquaredEuclideanNorm() ) )
968 best = aCandidate;
969 };
970
971 for( const ELEMENT& e : aSignal.signal->connections )
972 {
973 if( e.type != ELEMENT_TYPE::LINE || sheetOf( e ) != aSheet )
974 continue;
975
976 SEG wire( toSch( e.base.x, e.base.y ), toSch( e.line.x2, e.line.y2 ) );
977 VECTOR2I nearest = wire.NearestPoint( aPos );
978
979 // Rounding can leave the point beside a slanted wire, so it falls back to the nearer end
980 if( ( nearest - wire.A ).Cross( wire.B - wire.A ) != 0 )
981 {
982 consider( wire.A );
983 consider( wire.B );
984 }
985 else
986 {
987 consider( nearest );
988 }
989 }
990
991 if( auto it = m_pinEnds.find( aSignal.base.signal ); it != m_pinEnds.end() )
992 {
993 for( const PIN_END& pin : it->second )
994 {
995 if( pin.sheet == aSheet )
996 consider( pin.pos );
997 }
998 }
999
1000 return best;
1001}
1002
1003
1005{
1006 std::map<int, std::map<int, std::vector<SCH_ITEM*>>> bySignal;
1007
1008 for( const auto& [sheet, items] : m_netItems )
1009 {
1010 for( const auto& [item, signal] : items )
1011 bySignal[signal][sheet].push_back( item );
1012 }
1013
1014 for( const auto& [signal, pins] : m_pinEnds )
1015 {
1016 if( signal != BUS_SIGNAL )
1017 joinPieces( signal, bySignal[signal] );
1018 }
1019}
1020
1021
1022void TARGET3001_SCH_BUILDER::joinPieces( int aSignal, const std::map<int, std::vector<SCH_ITEM*>>& aItems )
1023{
1024 const std::vector<PIN_END>& pins = m_pinEnds.at( aSignal );
1025 bool global = m_globalSignals.count( aSignal ) > 0;
1026 bool anyNamed = false;
1027 std::set<int> sheets;
1028
1029 // An unnamed piece takes its label on a free wire end, else on its pin
1030 struct PIECE
1031 {
1032 const PIN_END* pin;
1033 std::vector<std::pair<VECTOR2I, VECTOR2I>> ends;
1034 };
1035
1036 std::vector<PIECE> unnamed;
1037
1038 // Only pieces holding a pin matter to the netlist
1039 for( const PIN_END& pin : pins )
1040 sheets.insert( pin.sheet );
1041
1042 auto isFree = [&]( int aSheet, const VECTOR2I& aPos )
1043 {
1044 for( const auto& [pos, pinSignal] : m_pinSignals[aSheet] )
1045 {
1046 if( pos == aPos && pinSignal != aSignal )
1047 return false;
1048 }
1049
1050 for( const auto& [item, itemSignal] : m_netItems[aSheet] )
1051 {
1052 if( itemSignal == aSignal )
1053 continue;
1054
1055 if( item->Type() != SCH_LINE_T )
1056 {
1057 if( item->GetPosition() == aPos )
1058 return false;
1059
1060 continue;
1061 }
1062
1063 SCH_LINE* line = static_cast<SCH_LINE*>( item );
1064
1065 if( IsPointOnSegment( line->GetStartPoint(), line->GetEndPoint(), aPos ) )
1066 return false;
1067 }
1068
1069 return true;
1070 };
1071
1072 for( int sheet : sheets )
1073 {
1074 std::vector<SEG> wires;
1075 std::vector<VECTOR2I> points;
1076 std::set<VECTOR2I> names;
1077 std::set<VECTOR2I> pinPoints;
1078
1079 if( auto it = aItems.find( sheet ); it != aItems.end() )
1080 {
1081 for( SCH_ITEM* item : it->second )
1082 {
1083 if( item->Type() == SCH_LINE_T )
1084 {
1085 SCH_LINE* line = static_cast<SCH_LINE*>( item );
1086 wires.emplace_back( line->GetStartPoint(), line->GetEndPoint() );
1087 }
1088 else if( dynamic_cast<SCH_LABEL_BASE*>( item ) )
1089 {
1090 names.insert( item->GetPosition() );
1091 }
1092 else
1093 {
1094 points.push_back( item->GetPosition() );
1095 }
1096 }
1097 }
1098
1099 for( const PIN_END& pin : pins )
1100 {
1101 if( pin.sheet == sheet )
1102 pinPoints.insert( pin.pos );
1103 }
1104
1105 std::map<VECTOR2I, int> index;
1106 std::vector<int> parent;
1107
1108 auto node = [&]( const VECTOR2I& aPos )
1109 {
1110 auto [it, inserted] = index.emplace( aPos, int( parent.size() ) );
1111
1112 if( inserted )
1113 parent.push_back( it->second );
1114
1115 return it->second;
1116 };
1117
1118 auto find = [&]( int aNode )
1119 {
1120 while( parent[aNode] != aNode )
1121 aNode = parent[aNode] = parent[parent[aNode]];
1122
1123 return aNode;
1124 };
1125
1126 auto unite = [&]( int aFirst, int aSecond )
1127 {
1128 parent[find( aFirst )] = find( aSecond );
1129 };
1130
1131 std::vector<VECTOR2I> ends( pinPoints.begin(), pinPoints.end() );
1132
1133 for( const SEG& wire : wires )
1134 {
1135 int start = node( wire.A );
1136 unite( start, node( wire.B ) );
1137 ends.push_back( wire.A );
1138 ends.push_back( wire.B );
1139 }
1140
1141 for( const VECTOR2I& pos : points )
1142 node( pos );
1143
1144 for( const VECTOR2I& pos : names )
1145 node( pos );
1146
1147 for( const VECTOR2I& pos : pinPoints )
1148 node( pos );
1149
1150 // A point lying inside a wire joins it
1151 for( const auto& [pos, id] : index )
1152 {
1153 for( const SEG& wire : wires )
1154 {
1155 if( IsPointOnSegment( wire.A, wire.B, pos ) )
1156 unite( id, index.at( wire.A ) );
1157 }
1158 }
1159
1160 std::sort( ends.begin(), ends.end(),
1161 []( const VECTOR2I& a, const VECTOR2I& b )
1162 {
1163 return a.x < b.x || ( a.x == b.x && a.y < b.y );
1164 } );
1165
1166 ends.erase( std::unique( ends.begin(), ends.end() ), ends.end() );
1167
1168 for( size_t i = 0; i < ends.size(); i++ )
1169 {
1170 for( size_t j = i + 1; j < ends.size() && ends[j].x - ends[i].x < NEAR_MISS; j++ )
1171 {
1172 int first = index.at( ends[i] );
1173 int second = index.at( ends[j] );
1174
1175 if( ( ends[j] - ends[i] ).EuclideanNorm() >= NEAR_MISS || find( first ) == find( second ) )
1176 continue;
1177
1178 SCH_LINE* stub = new SCH_LINE( ends[i], LAYER_WIRE );
1179 stub->SetEndPoint( ends[j] );
1180 addNetItem( sheet, stub, aSignal );
1181 unite( first, second );
1182 }
1183 }
1184
1185 std::set<int> named;
1186
1187 for( const VECTOR2I& pos : names )
1188 named.insert( find( index.at( pos ) ) );
1189
1190 for( const PIN_END& pin : pins )
1191 {
1192 if( global && pin.power && pin.sheet == sheet )
1193 named.insert( find( index.at( pin.pos ) ) );
1194 }
1195
1196 std::map<VECTOR2I, int> endCount;
1197 std::map<VECTOR2I, VECTOR2I> outward;
1198
1199 for( const SEG& wire : wires )
1200 {
1201 endCount[wire.A]++;
1202 endCount[wire.B]++;
1203 outward[wire.A] = wire.A - wire.B;
1204 outward[wire.B] = wire.B - wire.A;
1205 }
1206
1207 std::set<int> seen;
1208
1209 for( const PIN_END& pin : pins )
1210 {
1211 if( pin.sheet != sheet )
1212 continue;
1213
1214 int root = find( index.at( pin.pos ) );
1215
1216 if( named.count( root ) )
1217 {
1218 anyNamed = true;
1219 continue;
1220 }
1221
1222 if( !seen.insert( root ).second )
1223 continue;
1224
1225 PIECE piece{ &pin, {} };
1226
1227 for( const auto& [pos, count] : endCount )
1228 {
1229 if( count == 1 && !pinPoints.count( pos ) && find( index.at( pos ) ) == root )
1230 piece.ends.emplace_back( pos, outward.at( pos ) );
1231 }
1232
1233 unnamed.push_back( std::move( piece ) );
1234 }
1235 }
1236
1237 const ELEMENT* sig = m_project.FindSignal( aSignal );
1238
1239 if( unnamed.size() + ( anyNamed ? 1 : 0 ) < 2 || !sig || sig->signal->name.empty() )
1240 return;
1241
1242 // A name another signal shares would join the two
1243 for( const ELEMENT& e : m_project.signals )
1244 {
1245 if( &e != sig && e.type == ELEMENT_TYPE::SIGNAL && e.signal && e.signal->name == sig->signal->name )
1246 return;
1247 }
1248
1249 wxString name = ToWx( sig->signal->name );
1250
1251 for( const PIECE& piece : unnamed )
1252 {
1253 int sheet = piece.pin->sheet;
1254 VECTOR2I pos = piece.pin->pos;
1256
1257 // Text on a pin runs away from the body
1258 switch( pinOrientation( piece.pin->direction ) )
1259 {
1260 case PIN_ORIENTATION::PIN_RIGHT: spin = SPIN_STYLE::LEFT; break;
1261 case PIN_ORIENTATION::PIN_UP: spin = SPIN_STYLE::BOTTOM; break;
1262 case PIN_ORIENTATION::PIN_DOWN: spin = SPIN_STYLE::UP; break;
1263 default: spin = SPIN_STYLE::RIGHT; break;
1264 }
1265
1266 for( const auto& [end, dir] : piece.ends )
1267 {
1268 if( isFree( sheet, end ) )
1269 {
1270 pos = end;
1271 spin = spinAlong( dir );
1272 break;
1273 }
1274 }
1275
1276 SCH_LABEL_BASE* label = nullptr;
1277
1278 if( global )
1279 label = new SCH_GLOBALLABEL( pos, name );
1280 else
1281 label = new SCH_LABEL( pos, name );
1282
1283 label->SetSpinStyle( spin );
1284 addNetItem( sheet, label, aSignal );
1285 }
1286}
1287
1288
1289void TARGET3001_SCH_BUILDER::addNetItem( int aSheet, SCH_ITEM* aItem, int aSignal )
1290{
1291 addToSheet( aSheet, aItem );
1292 m_netItems[aSheet].emplace_back( aItem, aSignal );
1293}
1294
1295
1297{
1298 std::vector<std::pair<SCH_LINE*, int>> wires;
1299 std::vector<SCH_LINE*> buses;
1300 std::vector<std::pair<SCH_ITEM*, int>> points;
1301
1302 for( const auto& [item, signal] : m_netItems[aSheet] )
1303 {
1304 if( item->Type() != SCH_LINE_T )
1305 points.emplace_back( item, signal );
1306 else if( signal == BUS_SIGNAL )
1307 buses.push_back( static_cast<SCH_LINE*>( item ) );
1308 else
1309 wires.emplace_back( static_cast<SCH_LINE*>( item ), signal );
1310 }
1311
1312 // A bus joins its members by name, so each wire leaving it needs a label of its signal
1313 for( const auto& [wire, signal] : wires )
1314 {
1315 for( int end : { 0, 1 } )
1316 {
1317 VECTOR2I pos = wireEnd( wire, end );
1318 VECTOR2I moved = stepIn( wire, end );
1319
1320 bool onBus = std::any_of( buses.begin(), buses.end(),
1321 [&]( SCH_LINE* bus )
1322 {
1323 return IsPointOnSegment( bus->GetStartPoint(), bus->GetEndPoint(), pos );
1324 } );
1325
1326 if( !onBus || moved == pos )
1327 continue;
1328
1329 setWireEnd( wire, end, moved );
1330 bool labeled = false;
1331
1332 for( const auto& [item, itemSignal] : points )
1333 {
1334 if( itemSignal != signal )
1335 continue;
1336
1337 if( item->GetPosition() == pos )
1338 item->SetPosition( moved );
1339
1340 labeled |= dynamic_cast<SCH_LABEL_BASE*>( item )
1341 && IsPointOnSegment( wire->GetStartPoint(), wire->GetEndPoint(), item->GetPosition() );
1342 }
1343
1344 const ELEMENT* sig = m_project.FindSignal( signal );
1345
1346 if( labeled || !sig || sig->signal->name.empty() )
1347 continue;
1348
1349 wxString name = ToWx( sig->signal->name );
1350 SCH_LABEL_BASE* label = nullptr;
1351
1352 if( m_globalSignals.count( signal ) )
1353 label = new SCH_GLOBALLABEL( moved, name );
1354 else
1355 label = new SCH_LABEL( moved, name );
1356
1357 // The text runs along the wire, away from the bus
1358 label->SetSpinStyle( spinAlong( wireEnd( wire, 1 - end ) - moved ) );
1359
1360 addNetItem( aSheet, label, signal );
1361 points.emplace_back( label, signal );
1362 }
1363 }
1364
1365 // A junction or label inside a wire joins a branch to it, which a shifted wire would lose
1366 auto hasBranch = [&]( const std::pair<SCH_LINE*, int>& aWire )
1367 {
1368 return std::any_of( points.begin(), points.end(),
1369 [&]( const std::pair<SCH_ITEM*, int>& aPoint )
1370 {
1371 return aPoint.second == aWire.second
1372 && passesThrough( aWire.first, aPoint.first->GetPosition() );
1373 } );
1374 };
1375
1376 // Overlapping wires of different signals would be merged into one, so one of them steps aside; ends left
1377 // inside the other wire are parted below with everything else
1378 size_t drawn = wires.size();
1379
1380 for( size_t i = 0; i < drawn; i++ )
1381 {
1382 for( size_t j = i + 1; j < drawn; j++ )
1383 {
1384 if( wires[i].second == wires[j].second || !overlaps( wires[i].first, wires[j].first ) )
1385 continue;
1386
1387 bool firstShorter = wires[i].first->GetLength() <= wires[j].first->GetLength();
1388 std::pair<SCH_LINE*, int> pick = firstShorter ? wires[i] : wires[j];
1389
1390 if( hasBranch( pick ) )
1391 pick = firstShorter ? wires[j] : wires[i];
1392
1393 SCH_LINE* wire = pick.first;
1394
1395 if( hasBranch( pick ) || wire->GetLength() <= 2 * SIGNAL_GAP )
1396 continue;
1397
1398 // Stubs join the shifted run back to its ends, which stay where they connect
1399 VECTOR2I start = wire->GetStartPoint();
1400 VECTOR2I end = wire->GetEndPoint();
1401 VECTOR2I side = VECTOR2I( start.y - end.y, end.x - start.x ).Resize( SIGNAL_GAP );
1402
1403 for( const auto& [from, to] : { std::pair( start, start + side ), std::pair( end + side, end ) } )
1404 {
1405 SCH_LINE* stub = static_cast<SCH_LINE*>( wire->Duplicate( false ) );
1406 stub->SetStartPoint( from );
1407 stub->SetEndPoint( to );
1408 addNetItem( aSheet, stub, pick.second );
1409 wires.emplace_back( stub, pick.second );
1410 }
1411
1412 wire->SetStartPoint( start + side );
1413 wire->SetEndPoint( end + side );
1414 }
1415 }
1416
1417 // What KiCad joins at each point: everything there and every line passing through. A fixed member is a pin,
1418 // a bus or a line passing through; only pins and buses have no item
1419 struct MEMBER
1420 {
1421 int signal;
1422 SCH_ITEM* item;
1423 int end;
1424 bool fixed;
1425 };
1426
1427 std::map<VECTOR2I, std::vector<MEMBER>> at;
1428 std::set<VECTOR2I> busEnds;
1429
1430 for( const auto& [wire, signal] : wires )
1431 {
1432 for( int end : { 0, 1 } )
1433 at[wireEnd( wire, end )].push_back( { signal, wire, end, false } );
1434 }
1435
1436 for( const auto& [pos, signal] : m_pinSignals[aSheet] )
1437 at[pos].push_back( { signal, nullptr, -1, true } );
1438
1439 for( const auto& [item, signal] : points )
1440 at[item->GetPosition()].push_back( { signal, item, -1, false } );
1441
1442 for( SCH_LINE* bus : buses )
1443 {
1444 busEnds.insert( bus->GetStartPoint() );
1445 busEnds.insert( bus->GetEndPoint() );
1446 }
1447
1448 for( auto& [pos, members] : at )
1449 {
1450 // Wires never join buses, but pins, junctions and labels join both
1451 bool joinsBus = std::any_of( members.begin(), members.end(),
1452 []( const MEMBER& m )
1453 {
1454 return m.end < 0;
1455 } );
1456
1457 if( joinsBus && busEnds.count( pos ) )
1458 members.push_back( { BUS_SIGNAL, nullptr, -1, true } );
1459
1460 for( const auto& [wire, signal] : wires )
1461 {
1462 if( passesThrough( wire, pos ) )
1463 members.push_back( { signal, wire, -1, true } );
1464 }
1465
1466 for( SCH_LINE* bus : buses )
1467 {
1468 if( joinsBus && passesThrough( bus, pos ) )
1469 members.push_back( { BUS_SIGNAL, bus, -1, true } );
1470 }
1471 }
1472
1473 // A side is free when nothing of any signal lies there
1474 auto isFree = [&]( const VECTOR2I& aPos )
1475 {
1476 auto onLine = [&]( SCH_LINE* aLine )
1477 {
1478 return IsPointOnSegment( aLine->GetStartPoint(), aLine->GetEndPoint(), aPos );
1479 };
1480
1481 return !at.count( aPos ) && std::none_of( buses.begin(), buses.end(), onLine )
1482 && std::none_of( wires.begin(), wires.end(),
1483 [&]( const std::pair<SCH_LINE*, int>& aWire )
1484 {
1485 return onLine( aWire.first );
1486 } );
1487 };
1488
1489 // A wire bent earlier may have handed this end to one of its new pieces
1490 auto endAt = [&]( const MEMBER* aMember, const VECTOR2I& aPos ) -> std::pair<SCH_LINE*, int>
1491 {
1492 SCH_LINE* line = static_cast<SCH_LINE*>( aMember->item );
1493
1494 if( wireEnd( line, aMember->end ) == aPos )
1495 return { line, aMember->end };
1496
1497 for( const auto& [wire, signal] : wires )
1498 {
1499 for( int end : { 0, 1 } )
1500 {
1501 if( signal == aMember->signal && wireEnd( wire, end ) == aPos )
1502 return { wire, end };
1503 }
1504 }
1505
1506 return { line, aMember->end };
1507 };
1508
1509 for( const auto& [pos, members] : at )
1510 {
1511 std::map<int, std::vector<const MEMBER*>> groups;
1512 std::set<int> fixed;
1513 std::set<int> pinned;
1514
1515 for( const MEMBER& m : members )
1516 {
1517 groups[m.signal].push_back( &m );
1518
1519 if( m.fixed )
1520 fixed.insert( m.signal );
1521
1522 if( m.fixed && !m.item )
1523 pinned.insert( m.signal );
1524 }
1525
1526 // Pins of two signals on one spot cannot be parted without moving a symbol
1527 if( groups.size() < 2 || pinned.size() > 1 )
1528 continue;
1529
1530 int keeper = groups.begin()->first;
1531
1532 for( const auto& [signal, group] : groups )
1533 {
1534 if( fixed.count( signal ) > fixed.count( keeper )
1535 || ( fixed.count( signal ) == fixed.count( keeper ) && group.size() > groups.at( keeper ).size() ) )
1536 {
1537 keeper = signal;
1538 }
1539 }
1540
1541 if( !pinned.empty() )
1542 keeper = *pinned.begin();
1543
1544 int step = 0;
1545
1546 for( const auto& [signal, group] : groups )
1547 {
1548 if( signal == keeper )
1549 continue;
1550
1551 VECTOR2I moved = pos;
1552
1553 if( fixed.count( signal ) )
1554 {
1555 // Lines of this signal passing through bend around the point, taking its other items along
1556 std::optional<VECTOR2I> side = bendAround( aSheet, signal, pos, wires, isFree );
1557
1558 if( !side )
1559 continue;
1560
1561 moved = pos + *side;
1562 }
1563 else
1564 {
1565 // A wire end steps back along its wire, taking the rest along; otherwise all shift diagonally
1566 auto end = std::find_if( group.begin(), group.end(),
1567 []( const MEMBER* m )
1568 {
1569 return m->end >= 0;
1570 } );
1571
1572 bool oneEnd = std::count_if( group.begin(), group.end(),
1573 []( const MEMBER* m )
1574 {
1575 return m->end >= 0;
1576 } )
1577 == 1;
1578
1579 if( oneEnd )
1580 {
1581 auto [line, lineEnd] = endAt( *end, pos );
1582 moved = stepIn( line, lineEnd );
1583 }
1584
1585 // The diagonal step skips spots another item already holds
1586 if( moved == pos )
1587 {
1588 do
1589 {
1590 ++step;
1591 moved = pos + VECTOR2I( step * SIGNAL_GAP, step * SIGNAL_GAP );
1592 } while( !isFree( moved ) && step < MAX_DIAGONAL_STEPS );
1593 }
1594 }
1595
1596 for( const MEMBER* m : group )
1597 {
1598 if( m->end >= 0 )
1599 {
1600 auto [line, lineEnd] = endAt( m, pos );
1601 setWireEnd( line, lineEnd, moved );
1602 }
1603 else if( !m->fixed )
1604 {
1605 m->item->SetPosition( moved );
1606 }
1607 }
1608 }
1609 }
1610}
1611
1612
1613std::optional<VECTOR2I> TARGET3001_SCH_BUILDER::bendAround( int aSheet, int aSignal, const VECTOR2I& aPos,
1614 std::vector<std::pair<SCH_LINE*, int>>& aWires,
1615 const std::function<bool( const VECTOR2I& )>& aIsFree )
1616{
1617 std::vector<SCH_LINE*> lines;
1618
1619 for( const auto& [wire, signal] : aWires )
1620 {
1621 if( signal == aSignal && passesThrough( wire, aPos ) )
1622 lines.push_back( wire );
1623 }
1624
1625 if( lines.empty() )
1626 return std::nullopt;
1627
1628 VECTOR2I dir = lines[0]->GetEndPoint() - lines[0]->GetStartPoint();
1629
1630 for( SCH_LINE* line : lines )
1631 {
1632 VECTOR2I span = line->GetEndPoint() - line->GetStartPoint();
1633
1634 // The bend needs room on both sides, and the point's other items must sit on every bent line
1635 if( span.Cross( dir ) || ( aPos - line->GetStartPoint() ).EuclideanNorm() <= 2 * SIGNAL_GAP
1636 || ( line->GetEndPoint() - aPos ).EuclideanNorm() <= 2 * SIGNAL_GAP )
1637 {
1638 return std::nullopt;
1639 }
1640 }
1641
1642 VECTOR2I normal = VECTOR2I( -dir.y, dir.x ).Resize( SIGNAL_GAP );
1643 VECTOR2I side = normal;
1644
1645 for( int k : { 1, -1, 2, -2, 3, -3 } )
1646 {
1647 if( aIsFree( aPos + normal * k ) )
1648 {
1649 side = normal * k;
1650 break;
1651 }
1652 }
1653
1654 for( SCH_LINE* line : lines )
1655 {
1656 VECTOR2I along = ( line->GetEndPoint() - line->GetStartPoint() ).Resize( SIGNAL_GAP );
1657 VECTOR2I before = aPos - along;
1658 VECTOR2I after = aPos + along;
1659 VECTOR2I end = line->GetEndPoint();
1660
1661 line->SetEndPoint( before );
1662
1663 for( const auto& [from, to] : { std::pair( before, before + side ), std::pair( before + side, after + side ),
1664 std::pair( after + side, after ), std::pair( after, end ) } )
1665 {
1666 SCH_LINE* piece = static_cast<SCH_LINE*>( line->Duplicate( false ) );
1667 piece->SetStartPoint( from );
1668 piece->SetEndPoint( to );
1669 addNetItem( aSheet, piece, aSignal );
1670 aWires.emplace_back( piece, aSignal );
1671 }
1672 }
1673
1674 return side;
1675}
1676
1677
1679{
1680 int sheet = sheetOf( aElem );
1681
1682 if( sheet == 0 )
1683 return;
1684
1685 if( aElem.type == ELEMENT_TYPE::SYMBOL && aElem.symbol )
1686 {
1687 addFreeSymbol( aElem );
1688 return;
1689 }
1690
1691 addToSheet( sheet, makeGraphic( aElem, VECTOR2I( 0, 0 ), false ) );
1692}
1693
1694
1696{
1697 // A symbol without a component draws its placeholder texts as stored and is not a part
1698 wxString reference = wxString::Format( wxS( "#SYM%d" ), ++m_freeSymbolCount );
1699 wxString symName = LIB_ID::FixIllegalChars( reference.Mid( 1 ), false ).wx_str();
1700
1701 LIB_SYMBOL lib( symName );
1702 lib.SetLibId( LIB_ID( LIB_NAME, symName ) );
1703 lib.GetReferenceField().SetText( reference );
1704 addGateBodies( &lib, { &aSymbol }, false );
1705
1707 path.push_back( m_sheets.at( sheetOf( aSymbol ) ) );
1708
1709 SCH_SYMBOL* symbol = new SCH_SYMBOL( lib, lib.GetLibId(), &path, 1, 0, toSch( aSymbol.base.x, aSymbol.base.y ) );
1710 symbol->SetLibSymbol( new LIB_SYMBOL( lib ) );
1711 symbol->SetRef( &path, reference );
1712 symbol->GetField( FIELD_T::REFERENCE )->SetVisible( false );
1713 symbol->GetField( FIELD_T::VALUE )->SetVisible( false );
1714 symbol->SetExcludedFromBOM( true );
1715 symbol->SetExcludedFromBoard( true );
1716 symbol->SetExcludedFromSim( true );
1717 addToSheet( sheetOf( aSymbol ), symbol );
1718
1719 for( const ELEMENT& child : aSymbol.symbol->children )
1720 {
1721 if( SCH_BITMAP* picture = makePicture( child ) )
1722 addToSheet( sheetOf( aSymbol ), picture );
1723 }
1724}
1725
1726
1728{
1729 auto it = m_sheets.find( aSheet );
1730
1731 if( it != m_sheets.end() )
1732 return it->second->GetScreen();
1733
1734 SCH_SHEET* sheet;
1735
1736 // The first sheet in use holds its content in the root sheet
1737 if( m_sheets.empty() )
1738 {
1739 sheet = m_rootSheet;
1740 }
1741 else
1742 {
1743 sheet = new SCH_SHEET( &m_schematic->Root() );
1744 SCH_SCREEN* newScreen = new SCH_SCREEN( m_schematic );
1745
1746 wxFileName fn( m_fileName );
1747 fn.SetName( fn.GetName() + wxString::Format( wxS( "_%d" ), aSheet ) );
1749
1750 newScreen->SetFileName( fn.GetFullPath() );
1751 sheet->SetScreen( newScreen );
1752 sheet->SetFileName( fn.GetFullName() );
1753 }
1754
1755 wxString name;
1756
1757 if( aSheet - 1 < int( m_project.settings.sheetNames.size() ) )
1758 name = ToWx( m_project.settings.sheetNames[aSheet - 1] );
1759
1760 if( name.IsEmpty() )
1761 name = wxString::Format( wxS( "Sheet %d" ), aSheet );
1762
1763 if( sheet != m_rootSheet )
1764 sheet->SetName( name );
1765
1766 m_sheets[aSheet] = sheet;
1767 return sheet->GetScreen();
1768}
1769
1770
1772{
1773 const int margin = schIUScale.mmToIU( PAGE_MARGIN_MM );
1774
1775 for( auto& [sheet, items] : m_pending )
1776 {
1777 SCH_SCREEN* scr = screen( sheet );
1778 BOX2I extent = m_extents[sheet];
1779
1780 // Target sheets are centred on their origin; KiCad pages start at the top left corner
1781 auto snap = []( int v )
1782 {
1783 return KiROUND( double( v ) / PLACEMENT_GRID ) * PLACEMENT_GRID;
1784 };
1785
1786 VECTOR2I offset( snap( margin - extent.GetLeft() ), snap( margin - extent.GetTop() ) );
1787
1789 page.SetWidthMM( std::max( 297.0, schIUScale.IUTomm( extent.GetWidth() + 2 * margin ) ) );
1790 page.SetHeightMM( std::max( 210.0, schIUScale.IUTomm( extent.GetHeight() + 2 * margin ) ) );
1791 scr->SetPageSettings( page );
1792
1793 for( SCH_ITEM* item : items )
1794 {
1795 item->Move( offset );
1796 scr->Append( item );
1797 }
1798 }
1799
1800 if( m_sheets.size() > 1 )
1801 {
1802 std::vector<SCH_SHEET*> sheets;
1803
1804 for( const auto& [number, sheet] : m_sheets )
1805 sheets.push_back( sheet );
1806
1807 m_schematic->SetTopLevelSheets( sheets );
1808 }
1809
1810 int page = 1;
1811
1812 for( const auto& [number, sheet] : m_sheets )
1813 sheet->GetScreen()->SetPageNumber( wxString::Format( wxS( "%d" ), page++ ) );
1814}
int index
const char * name
constexpr EDA_IU_SCALE schIUScale
Definition base_units.h:130
BOX2< VECTOR2I > BOX2I
Definition box2.h:917
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:985
void SetImageType(wxBitmapType aType)
Set the bitmap type (png, jpeg, etc.)
constexpr size_type GetWidth() const
Definition box2.h:217
constexpr Vec Centre() const
Definition box2.h:100
constexpr BOX2< Vec > & Merge(const BOX2< Vec > &aRect)
Modify the position and size of the rectangle in order to contain aRect.
Definition box2.h:597
constexpr size_type GetHeight() const
Definition box2.h:218
constexpr coord_type GetLeft() const
Definition box2.h:231
constexpr coord_type GetTop() const
Definition box2.h:232
bool IsZero() const
Definition eda_angle.h:135
virtual VECTOR2I GetPosition() const
Definition eda_item.h:348
virtual const BOX2I GetBoundingBox() const
Return the orthogonal bounding box of this object for display purposes.
Definition eda_item.cpp:280
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:110
virtual void SetEnd(const VECTOR2I &aEnd)
Definition eda_shape.h:338
virtual void SetCenter(const VECTOR2I &aCenter)
virtual void SetArcGeometry(const VECTOR2I &aStart, const VECTOR2I &aMid, const VECTOR2I &aEnd)
Set the three controlling points for an arc.
A mix-in class (via multiple inheritance) that handles texts such as labels, parts,...
Definition eda_text.h:94
virtual int GetTextHeight() const
Definition eda_text.h:307
void SetVertJustify(GR_TEXT_V_ALIGN_T aType)
Definition eda_text.cpp:379
virtual void SetVisible(bool aVisible)
Definition eda_text.cpp:348
virtual void SetTextAngle(const EDA_ANGLE &aAngle)
Definition eda_text.cpp:269
void SetHorizJustify(GR_TEXT_H_ALIGN_T aType)
Definition eda_text.cpp:371
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
const LIB_ID & GetLibId() const override
Definition lib_symbol.h:190
void SetGlobalPower()
bool IsPower() const override
LIB_ITEMS_CONTAINER & GetDrawItems()
Return a reference to the draw item list.
Definition lib_symbol.h:845
void SetUnitCount(int aCount, bool aDuplicateDrawItems)
Set the units per symbol count.
SCH_FIELD & GetValueField()
Definition lib_symbol.h:449
void SetLibId(const LIB_ID &aLibId)
void AddDrawItem(SCH_ITEM *aItem, bool aSort=true)
Add a new draw aItem to the draw object list and sort according to aSort.
SCH_FIELD & GetReferenceField()
Definition lib_symbol.h:453
Describe the page size and margins of a paper page on which to eventually print or plot.
Definition page_info.h:75
void SetWidthMM(double aWidthInMM)
Definition page_info.h:135
void SetHeightMM(double aHeightInMM)
Definition page_info.h:140
A REFERENCE_IMAGE is a wrapper around a BITMAP_IMAGE that is displayed in an editor as a reference fo...
BITMAP_BASE & MutableImage() const
Only use this if you really need to modify the underlying image.
void Rotate(const VECTOR2I &aCenter, const EDA_ANGLE &aAngle)
Rotate the image about aCenter.
bool SetImage(const wxImage &aImage)
Set the image from an existing wxImage.
void SetPosition(const VECTOR2I &aPos)
void SetWidth(int aWidth)
A pure virtual class used to derive REPORTER objects from.
Definition reporter.h:73
Holds all the data relating to one schematic.
Definition schematic.h:155
Object to handle a bitmap image that can be inserted in a schematic.
Definition sch_bitmap.h:36
REFERENCE_IMAGE & GetReferenceImage()
Definition sch_bitmap.h:50
void SetPosition(const VECTOR2I &aPosition) override
void SetText(const wxString &aText) override
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
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
void SetStartPoint(const VECTOR2I &aPosition)
Definition sch_line.h:137
VECTOR2I GetEndPoint() const
Definition sch_line.h:145
VECTOR2I GetStartPoint() const
Definition sch_line.h:136
virtual void SetStroke(const STROKE_PARAMS &aStroke) override
Definition sch_line.h:199
bool IsEndPoint(const VECTOR2I &aPoint) const override
Test if aPt is an end point of this schematic object.
Definition sch_line.h:88
double GetLength() const
Definition sch_line.cpp:343
void SetEndPoint(const VECTOR2I &aPosition)
Definition sch_line.h:146
void Append(SCH_ITEM *aItem, bool aUpdateLibSymbol=true)
void SetPageSettings(const PAGE_INFO &aPageSettings)
Definition sch_screen.h:145
void SetFileName(const wxString &aFileName)
Set the file name for this screen to aFileName.
void SetStroke(const STROKE_PARAMS &aStroke) override
void AddPoint(const VECTOR2I &aPosition)
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition sch_sheet.h:50
void SetFileName(const wxString &aFilename)
Definition sch_sheet.h:434
void SetName(const wxString &aName)
Definition sch_sheet.h:185
SCH_SCREEN * GetScreen() const
Definition sch_sheet.h:187
void SetScreen(SCH_SCREEN *aScreen)
Set the SCH_SCREEN associated with this sheet to aScreen.
Schematic symbol object.
Definition sch_symbol.h:76
void SetExcludedFromSim(bool aEnable, const SCH_SHEET_PATH *aInstance=nullptr, const wxString &aVariantName=wxEmptyString) override
Set or clear the exclude from simulation flag.
void SetRef(const SCH_SHEET_PATH *aSheet, const wxString &aReference)
Set the base reference for the given sheet path for this symbol.
void SetFootprintFieldText(const wxString &aFootprint)
void SetExcludedFromBOM(bool aEnable, const SCH_SHEET_PATH *aInstance=nullptr, const wxString &aVariantName=wxEmptyString) override
Set or clear the exclude from schematic bill of materials flag.
void SetLibSymbol(LIB_SYMBOL *aLibSymbol)
Set this schematic symbol library symbol reference to aLibSymbol.
void SetExcludedFromBoard(bool aEnable, const SCH_SHEET_PATH *aInstance=nullptr, const wxString &aVariantName=wxEmptyString) override
SCH_FIELD * GetField(FIELD_T aFieldType)
Return a mandatory field in this symbol.
void SetPosition(const VECTOR2I &aPosition) override
Definition sch_text.h:144
const BOX2I GetBoundingBox() const override
Return the orthogonal bounding box of this object for display purposes.
Definition sch_text.cpp:343
Definition seg.h:38
VECTOR2I A
Definition seg.h:45
VECTOR2I::extended_type ecoord
Definition seg.h:40
VECTOR2I B
Definition seg.h:46
const VECTOR2I NearestPoint(const VECTOR2I &aP) const
Compute a point on the segment (this) that is closest to point aP.
Definition seg.cpp:599
static SEG::ecoord Square(int a)
Definition seg.h:119
Spin style for labels of all kinds on schematics.
Definition sch_label.h:38
Simple container to manage line stroke parameters.
virtual void SetShowPinNumbers(bool aShow)
Set or clear the pin number visibility flag.
Definition symbol.h:174
virtual void SetShowPinNames(bool aShow)
Set or clear the pin name visibility flag.
Definition symbol.h:168
void SetPinNameOffset(int aOffset)
Set the offset in mils of the pin name text from the pin symbol.
Definition symbol.h:162
Maps TARGET 3001!
static VECTOR2I BaselineShift(const EDA_TEXT &aText)
Offset in text units that moves aText so the baseline-left point of its first glyph lies on its posit...
void Apply(EDA_TEXT *aText, const ELEMENT &aElem, int aUnit)
Give aText the font, style, size and stroke width of text aElem.
void addNetItem(int aSheet, SCH_ITEM *aItem, int aSignal)
Add a wire, junction or label of signal aSignal; separateSignals() keeps it apart from other signals.
const TARGET3001::PROJECT & m_project
TARGET3001_SCH_BUILDER(SCHEMATIC *aSchematic, SCH_SHEET *aRootSheet, const TARGET3001::PROJECT &aProject, const wxString &aFileName, REPORTER *aReporter)
void addToSheet(int aSheet, SCH_ITEM *aItem)
void addFreeItem(const TARGET3001::ELEMENT &aElem)
bool joinsGlobally(const TARGET3001::ELEMENT &aSignal) const
True when the signal's labels must be global: it reaches several sheets or meets supply symbols.
static VECTOR2I toSch(int32_t aX, int32_t aY)
Sheet position in KiCad units, before the per-sheet page offset is applied.
std::map< int, std::vector< PIN_END > > m_pinEnds
placed pins of each signal, by signal ID
SCH_BITMAP * makePicture(const TARGET3001::ELEMENT &aElem) const
Image a rectangle shows instead of its fill, or nullptr when it has none.
std::map< int, std::set< int > > m_powerSheets
sheets holding a power symbol, by signal ID
void joinSignalPieces()
Join the pieces of every signal as separateSignals() left them; see joinPieces()
SCH_ITEM * makeGraphic(const TARGET3001::ELEMENT &aElem, const VECTOR2I &aOrigin, bool aInSymbol) const
Add graphics shared by free items and symbol bodies; returns nullptr for unsupported items.
std::map< int, BOX2I > m_extents
drawn extent of each sheet
void addFreeSymbol(const TARGET3001::ELEMENT &aSymbol)
void joinPieces(int aSignal, const std::map< int, std::vector< SCH_ITEM * > > &aItems)
Join the pieces of signal aSignal that KiCad would see apart, since the file joins a signal's items b...
std::map< int, std::vector< std::pair< SCH_ITEM *, int > > > m_netItems
items and their signal, by sheet
std::map< int, std::vector< std::pair< VECTOR2I, int > > > m_pinSignals
pin points and signal, by sheet
TARGET3001::FONTS m_fonts
lookup cache, filled while drawing
std::optional< VECTOR2I > labelAnchor(const TARGET3001::ELEMENT &aSignal, int aSheet, const VECTOR2I &aPos) const
Point on the signal's own wires or pins nearest a name drawn at aPos, if it is close enough to name t...
std::optional< VECTOR2I > bendAround(int aSheet, int aSignal, const VECTOR2I &aPos, std::vector< std::pair< SCH_LINE *, int > > &aWires, const std::function< bool(const VECTOR2I &)> &aIsFree)
Bend the wires of aSignal that pass through aPos around it, one gap aside on a side aIsFree accepts,...
void addGateBodies(LIB_SYMBOL *aLib, const std::vector< const TARGET3001::ELEMENT * > &aGates, bool aFieldTexts, const std::map< uint16_t, wxString > &aPadNumbers={}) const
Draw each gate's body as one unit of aLib; aFieldTexts leaves reference and value texts to fields.
void addComponent(const TARGET3001::ELEMENT &aComponent)
void addConnections(const TARGET3001::ELEMENT &aSignal)
std::map< int, std::vector< SCH_ITEM * > > m_pending
std::set< int > m_globalSignals
signals joined by global labels
void separateSignals(int aSheet)
Move apart items of different signals that touch where KiCad would join them, since the file connects...
SCH_SCREEN * screen(int aSheet)
Screen for a 1-based sheet number, created on first use.
std::map< int, SCH_SHEET * > m_sheets
by 1-based sheet number
static int sheetOf(const TARGET3001::ELEMENT &aElem)
Sheet number of a schematic element, or 0 for board elements.
wxString wx_str() const
Definition utf8.cpp:41
constexpr extended_type Cross(const VECTOR2< T > &aVector) const
Compute cross product of self with aVector.
Definition vector2d.h:559
constexpr extended_type SquaredEuclideanNorm() const
Compute the squared euclidean norm of the vector, which is defined as (x ** 2 + y ** 2).
Definition vector2d.h:312
T EuclideanNorm() const
Compute the Euclidean norm of the vector, which is defined as sqrt(x ** 2 + y ** 2).
Definition vector2d.h:281
constexpr extended_type Dot(const VECTOR2< T > &aVector) const
Compute dot product of self with aVector.
Definition vector2d.h:567
VECTOR2< T > Resize(T aNewLength) const
Return a vector of the same direction, but length specified in aNewLength.
Definition vector2d.h:406
@ DEGREES_T
Definition eda_angle.h:30
static constexpr EDA_ANGLE ANGLE_VERTICAL
Definition eda_angle.h:469
static constexpr EDA_ANGLE ANGLE_HORIZONTAL
Definition eda_angle.h:468
@ FILLED_SHAPE
Fill with object color.
Definition eda_fill.h:31
static const std::string KiCadSchematicFileExtension
SCH_LAYER_ID
Eeschema drawing layers.
Definition layer_ids.h:480
@ LAYER_DEVICE
Definition layer_ids.h:497
@ LAYER_WIRE
Definition layer_ids.h:483
@ LAYER_NOTES
Definition layer_ids.h:498
@ LAYER_BUS
Definition layer_ids.h:484
constexpr double PAGE_MARGIN_MM
Margin added on every side of the drawn extents before choosing a page, in millimetres.
constexpr int SCHEMATIC_SHEET_COUNT
Sheets occupy layers 101 to 200.
wxString ToWx(const std::string &aRaw)
Convert a stored ANSI string to a wxString.
ELEMENT_TYPE
Element class tags as stored in the file.
constexpr int SCHEMATIC_LAYER_BASE
First layer number used for schematic sheets; sheet N (1-based) is layer 100 + N.
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
Definition eda_angle.h:461
ELECTRICAL_PINTYPE
The symbol library pin object electrical types used in ERC tests.
Definition pin_type.h:32
@ 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_NIC
not internally connected (may be connected to anything)
Definition pin_type.h:40
@ 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_UNSPECIFIED
unknown electrical properties: creates always a warning when connected
Definition pin_type.h:41
@ PT_PASSIVE
pin for passive symbols: must be connected, and can be connected to any pin.
Definition pin_type.h:39
PIN_ORIENTATION
The symbol library pin object orientations.
Definition pin_type.h:101
@ 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
GRAPHIC_PINSHAPE
Definition pin_type.h:80
Definition of the SCH_SHEET_PATH and SCH_SHEET_LIST classes for Eeschema.
LINE_STYLE
Dashed line types.
Stroked arc or circle. The base position is the start point.
double Sweep() const
Counter-clockwise sweep in (0, 360]; equal angles give a full circle.
float startAngle
degrees, counter-clockwise from +X
Fields shared by every drawable element.
std::vector< uint8_t > Picture() const
Encoded image a rectangle shows stretched over its area instead of its fill, empty for none.
uint16_t component
ID of the owning component (a component stores its own), 0 for none.
uint16_t signal
ID of the owning signal (a signal stores its own), 0 for none.
std::optional< std::pair< int32_t, int32_t > > TextRightEnd() const
Right end of right-aligned text from its ARXY property, in file coordinates.
const std::string * FindProp(const std::string &aKey) const
Return the value of the first property named aKey, if any.
One element of any class.
std::shared_ptr< COMPONENT > component
std::shared_ptr< SYMBOL > symbol
std::shared_ptr< SIGNAL > signal
std::vector< ELEMENT > children
Children of a pin (its number text).
uint8_t dashStyle
0 solid, 1 long dash, 2 dash-dot, 3 other, 4 short dash
Schematic pin. The base position is the connection point; the pin extends along direction.
Filled rectangle anchored at a corner (the base position); sizes are signed.
Filled triangle given by three absolute corners (the first is the base position).
wxString ToWx(const std::string &aRaw)
Convert a stored ANSI string to a wxString.
FIELD_T
The set of all field indices assuming an array like sequence that a SCH_COMPONENT or LIB_PART can hol...
@ REFERENCE
Field Reference of part, i.e. "IC21".
@ VALUE
Field Value of part, i.e. "3.3K".
bool moved
std::string path
KIBIS_COMPONENT * comp
KIBIS_PIN * pin
VECTOR2I center
int radius
VECTOR2I end
static double sweep(float aStart, float aEnd)
@ GR_TEXT_H_ALIGN_RIGHT
@ GR_TEXT_H_ALIGN_LEFT
@ GR_TEXT_V_ALIGN_BOTTOM
@ GR_TEXT_V_ALIGN_TOP
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
bool IsPointOnSegment(const VECTOR2I &aSegStart, const VECTOR2I &aSegEnd, const VECTOR2I &aTestPoint)
Test if aTestPoint is on line defined by aSegStart and aSegEnd.
Definition trigo.cpp:87
double DEG2RAD(double deg)
Definition trigo.h:178
@ SCH_LINE_T
Definition typeinfo.h:159
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708
VECTOR2< double > VECTOR2D
Definition vector2d.h:707
VIA_CROSS_SECTION_GEOMETRY::PIECE PIECE
Definition of file extensions used in Kicad.