KiCad PCB EDA Suite
sch_component.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) 2015 Jean-Pierre Charras, jp.charras at wanadoo.fr
5  * Copyright (C) 1992-2017 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
30 #include <fctsys.h>
31 #include <pgm_base.h>
32 #include <class_drawpanel.h>
33 #include <gr_basic.h>
34 #include <kicad_string.h>
35 #include <richio.h>
36 #include <schframe.h>
37 #include <plot_common.h>
38 #include <msgpanel.h>
39 #include <bitmaps.h>
40 
41 #include <general.h>
42 #include <class_library.h>
43 #include <lib_rectangle.h>
44 #include <lib_pin.h>
45 #include <lib_text.h>
46 #include <sch_component.h>
47 #include <sch_sheet.h>
48 #include <sch_sheet_path.h>
49 #include <class_netlist_object.h>
50 #include <lib_draw_item.h>
51 #include <symbol_lib_table.h>
52 
54 
55 #include <wx/tokenzr.h>
56 #include <iostream>
57 
58 #define NULL_STRING "_NONAME_"
59 
65 std::string toUTFTildaText( const wxString& txt )
66 {
67  std::string ret = TO_UTF8( txt );
68 
69  for( std::string::iterator it = ret.begin(); it!=ret.end(); ++it )
70  {
71  if( (unsigned char) *it <= ' ' )
72  *it = '~';
73  }
74  return ret;
75 }
76 
77 
91 static LIB_PART* dummy()
92 {
93  static LIB_PART* part;
94 
95  if( !part )
96  {
97  part = new LIB_PART( wxEmptyString );
98 
99  LIB_RECTANGLE* square = new LIB_RECTANGLE( part );
100 
101  square->Move( wxPoint( -200, 200 ) );
102  square->SetEndPosition( wxPoint( 200, -200 ) );
103 
104  LIB_TEXT* text = new LIB_TEXT( part );
105 
106  text->SetTextSize( wxSize( 150, 150 ) );
107  text->SetText( wxString( wxT( "??" ) ) );
108 
109  part->AddDrawItem( square );
110  part->AddDrawItem( text );
111  }
112 
113  return part;
114 }
115 
116 
118  SCH_ITEM( aParent, SCH_COMPONENT_T )
119 {
120  Init( aPos );
121  m_currentSheetPath = NULL;
123 }
124 
125 
127  int convert, const wxPoint& pos, bool setNewItemFlag ) :
128  SCH_ITEM( NULL, SCH_COMPONENT_T )
129 {
130  Init( pos );
131 
132  m_unit = unit;
133  m_convert = convert;
134  m_lib_id.SetLibItemName( TO_UTF8( aPart.GetName() ), false );
135  m_part = aPart.SharedPtr();
136  m_currentSheetPath = NULL;
138 
140 
141  if( setNewItemFlag )
142  m_Flags = IS_NEW | IS_MOVED;
143 
144  // Import user defined fields from the library component:
145  LIB_FIELDS libFields;
146 
147  aPart.GetFields( libFields );
148 
149  for( LIB_FIELDS::iterator it = libFields.begin(); it!=libFields.end(); ++it )
150  {
151  // Can no longer insert an empty name, since names are now keys. The
152  // field index is not used beyond the first MANDATORY_FIELDS
153  if( it->GetName().IsEmpty() )
154  continue;
155 
156  // See if field already exists (mandatory fields always exist).
157  // for mandatory fields, the name and field id are fixed, so we use the
158  // known and fixed id to get them (more reliable than names, which can be translated)
159  // for other fields (custom fields), locate the field by same name
160  // (field id has no known meaning for custom fields)
161  int idx = it->GetId();
162  SCH_FIELD* schField;
163 
164  if( idx < MANDATORY_FIELDS )
165  schField = GetField( idx );
166  else
167  schField = FindField( it->GetName() );
168 
169  if( !schField )
170  {
171  SCH_FIELD fld( wxPoint( 0, 0 ), GetFieldCount(), this, it->GetName() );
172  schField = AddField( fld );
173  }
174 
175  schField->ImportValues( *it );
176  schField->SetText( it->GetText() );
177 
178  // Now the field is initialized, place it to the right position:
179  schField->SetTextPos( m_Pos + it->GetTextPos() );
180  }
181 
182  wxString msg = aPart.GetReferenceField().GetText();
183 
184  if( msg.IsEmpty() )
185  msg = wxT( "U" );
186 
187  m_prefix = msg;
188 
189  // update the reference -- just the prefix for now.
190  msg += wxT( "?" );
191  SetRef( sheet, msg );
192 
193  // Use the schematic component name instead of the library value field name.
194  GetField( VALUE )->SetText( FROM_UTF8( GetLibId().GetLibItemName() ) );
195 }
196 
197 
199  SCH_ITEM( aComponent )
200 {
201  m_currentSheetPath = NULL;
202  m_Parent = aComponent.m_Parent;
203  m_Pos = aComponent.m_Pos;
204  m_unit = aComponent.m_unit;
205  m_convert = aComponent.m_convert;
206  m_lib_id = aComponent.m_lib_id;
207  m_part = aComponent.m_part;
208 
209  SetTimeStamp( aComponent.m_TimeStamp );
210 
211  m_transform = aComponent.m_transform;
212  m_prefix = aComponent.m_prefix;
214  m_Fields = aComponent.m_Fields;
215 
216  // Re-parent the fields, which before this had aComponent as parent
217  for( int i = 0; i<GetFieldCount(); ++i )
218  {
219  GetField( i )->SetParent( this );
220  }
221 
222  m_isDangling = aComponent.m_isDangling;
224 }
225 
226 
227 void SCH_COMPONENT::Init( const wxPoint& pos )
228 {
229  m_Pos = pos;
230  m_unit = 0; // In multi unit chip - which unit to draw.
231  m_convert = 0; // De Morgan Handling
232 
233  // The rotation/mirror transformation matrix. pos normal
235 
236  // construct only the mandatory fields, which are the first 4 only.
237  for( int i = 0; i < MANDATORY_FIELDS; ++i )
238  {
239  SCH_FIELD field( pos, i, this, TEMPLATE_FIELDNAME::GetDefaultFieldName( i ) );
240 
241  if( i == REFERENCE )
242  field.SetLayer( LAYER_REFERENCEPART );
243  else if( i == VALUE )
244  field.SetLayer( LAYER_VALUEPART );
245 
246  // else keep LAYER_FIELDS from SCH_FIELD constructor
247 
248  // SCH_FIELD's implicitly created copy constructor is called in here
249  AddField( field );
250  }
251 
252  m_prefix = wxString( wxT( "U" ) );
253 }
254 
255 
257 {
258  return new SCH_COMPONENT( *this );
259 }
260 
261 
262 void SCH_COMPONENT::SetLibId( const LIB_ID& aLibId, PART_LIBS* aLibs )
263 {
264  if( m_lib_id != aLibId )
265  {
266  m_lib_id = aLibId;
267  SetModified();
268 
269  if( aLibs )
270  Resolve( aLibs );
271  else
272  m_part.reset();
273  }
274 }
275 
276 
277 void SCH_COMPONENT::SetLibId( const LIB_ID& aLibId, SYMBOL_LIB_TABLE* aSymLibTable )
278 {
279  if( m_lib_id != aLibId )
280  {
281  wxCHECK_RET( aSymLibTable, "No symbol library table provided." );
282 
283  m_lib_id = aLibId;
284  SetModified();
285 
286  LIB_ALIAS* alias = aSymLibTable->LoadSymbol( m_lib_id.GetLibNickname(),
288 
289  if( alias && alias->GetPart() )
290  m_part = alias->GetPart()->SharedPtr();
291  else
292  m_part.reset();
293  }
294 }
295 
296 
298 {
299  // I've never been happy that the actual individual PART_LIB is left up to
300  // flimsy search path ordering. None-the-less find a part based on that design:
301  if( LIB_PART* part = aLibs->FindLibPart( m_lib_id ) )
302  {
303  m_part = part->SharedPtr();
304  return true;
305  }
306 
307  return false;
308 }
309 
310 // Helper sort function, used in SCH_COMPONENT::ResolveAll, to sort
311 // sch component by lib_id
312 static bool sort_by_libid( const SCH_COMPONENT* ref, SCH_COMPONENT* cmp )
313 {
314  return ref->GetLibId() < cmp->GetLibId();
315 }
316 
318  const SCH_COLLECTOR& aComponents, PART_LIBS* aLibs )
319 {
320  // Usually, many components use the same part lib.
321  // to avoid too long calculation time the list of components is grouped
322  // and once the lib part is found for one member of a group, it is also
323  // set for all other members of this group
324  std::vector<SCH_COMPONENT*> cmp_list;
325 
326  // build the cmp list.
327  for( int i = 0; i < aComponents.GetCount(); ++i )
328  {
329  SCH_COMPONENT* cmp = dynamic_cast<SCH_COMPONENT*>( aComponents[i] );
330  wxASSERT( cmp );
331 
332  if( cmp ) // cmp == NULL should not occur.
333  cmp_list.push_back( cmp );
334  }
335 
336  // sort it by lib part. Cmp will be grouped by same lib part.
337  std::sort( cmp_list.begin(), cmp_list.end(), sort_by_libid );
338 
339  LIB_ID curr_libid;
340 
341  for( unsigned ii = 0; ii < cmp_list.size (); ++ii )
342  {
343  SCH_COMPONENT* cmp = cmp_list[ii];
344  curr_libid = cmp->m_lib_id;
345  cmp->Resolve( aLibs );
346 
347  // Propagate the m_part pointer to other members using the same lib_id
348  for( unsigned jj = ii+1; jj < cmp_list.size (); ++jj )
349  {
350  SCH_COMPONENT* next_cmp = cmp_list[jj];
351 
352  if( curr_libid != next_cmp->m_lib_id )
353  break;
354 
355  next_cmp->m_part = cmp->m_part;
356  ii = jj;
357  }
358  }
359 }
360 
361 
362 void SCH_COMPONENT::SetUnit( int aUnit )
363 {
364  if( m_unit != aUnit )
365  {
366  m_unit = aUnit;
367  SetModified();
368  }
369 }
370 
371 void SCH_COMPONENT::UpdateUnit( int aUnit )
372 {
373  m_unit = aUnit;
374 }
375 
376 
377 void SCH_COMPONENT::SetConvert( int aConvert )
378 {
379  if( m_convert != aConvert )
380  {
381  m_convert = aConvert;
382  SetModified();
383  }
384 }
385 
386 
387 void SCH_COMPONENT::SetTransform( const TRANSFORM& aTransform )
388 {
389  if( m_transform != aTransform )
390  {
391  m_transform = aTransform;
392  SetModified();
393  }
394 }
395 
396 
398 {
399  if( PART_SPTR part = m_part.lock() )
400  {
401  return part->GetUnitCount();
402  }
403 
404  return 0;
405 }
406 
407 
408 void SCH_COMPONENT::Draw( EDA_DRAW_PANEL* aPanel, wxDC* aDC, const wxPoint& aOffset,
409  GR_DRAWMODE aDrawMode, COLOR4D aColor,
410  bool aDrawPinText )
411 {
412  auto opts = PART_DRAW_OPTIONS::Default();
413  opts.draw_mode = aDrawMode;
414  opts.color = aColor;
415  opts.transform = m_transform;
416  opts.show_pin_text = aDrawPinText;
417  opts.draw_visible_fields = false;
418  opts.draw_hidden_fields = false;
419 
420  if( PART_SPTR part = m_part.lock() )
421  {
422  // Draw pin targets if part is being dragged
423  bool dragging = aPanel->GetScreen()->GetCurItem() == this && aPanel->IsMouseCaptured();
424 
425  if( !dragging )
426  {
427  opts.dangling = m_isDangling;
428  }
429 
430  part->Draw( aPanel, aDC, m_Pos + aOffset, m_unit, m_convert, opts );
431  }
432  else // Use dummy() part if the actual cannot be found.
433  {
434  dummy()->Draw( aPanel, aDC, m_Pos + aOffset, 0, 0, opts );
435  }
436 
437  SCH_FIELD* field = GetField( REFERENCE );
438 
439  if( field->IsVisible() && !field->IsMoving() )
440  {
441  field->Draw( aPanel, aDC, aOffset, aDrawMode );
442  }
443 
444  for( int ii = VALUE; ii < GetFieldCount(); ii++ )
445  {
446  field = GetField( ii );
447 
448  if( field->IsMoving() )
449  continue;
450 
451  field->Draw( aPanel, aDC, aOffset, aDrawMode );
452  }
453 
454 #if 0
455  // Only for testing purposes, draw the component bounding box
456  {
458  GRRect( aPanel->GetClipBox(), aDC, boundingBox, 0, BROWN );
459 #if 1
460  if( GetField( REFERENCE )->IsVisible() )
461  {
462  boundingBox = GetField( REFERENCE )->GetBoundingBox();
463  GRRect( aPanel->GetClipBox(), aDC, boundingBox, 0, BROWN );
464  }
465 
466  if( GetField( VALUE )->IsVisible() )
467  {
468  boundingBox = GetField( VALUE )->GetBoundingBox();
469  GRRect( aPanel->GetClipBox(), aDC, boundingBox, 0, BROWN );
470  }
471 #endif
472  }
473 #endif
474 }
475 
476 
477 void SCH_COMPONENT::AddHierarchicalReference( const wxString& aPath,
478  const wxString& aRef,
479  int aMulti )
480 {
481  wxString h_path, h_ref;
482  wxStringTokenizer tokenizer;
483  wxString separators( wxT( " " ) );
484 
485  // Search for an existing path and remove it if found (should not occur)
486  for( unsigned ii = 0; ii < m_PathsAndReferences.GetCount(); ii++ )
487  {
488  tokenizer.SetString( m_PathsAndReferences[ii], separators );
489  h_path = tokenizer.GetNextToken();
490 
491  if( h_path.Cmp( aPath ) == 0 )
492  {
493  m_PathsAndReferences.RemoveAt( ii );
494  ii--;
495  }
496  }
497 
498  h_ref = aPath + wxT( " " ) + aRef;
499  h_ref << wxT( " " ) << aMulti;
500  m_PathsAndReferences.Add( h_ref );
501 }
502 
503 
504 wxString SCH_COMPONENT::GetPath( const SCH_SHEET_PATH* sheet ) const
505 {
506  wxCHECK_MSG( sheet != NULL, wxEmptyString,
507  wxT( "Cannot get component path with invalid sheet object." ) );
508 
509  wxString str;
510 
511  str.Printf( wxT( "%8.8lX" ), (long unsigned) m_TimeStamp );
512  return sheet->Path() + str;
513 }
514 
515 
516 const wxString SCH_COMPONENT::GetRef( const SCH_SHEET_PATH* sheet )
517 {
518  wxString path = GetPath( sheet );
519  wxString h_path, h_ref;
520  wxStringTokenizer tokenizer;
521  wxString separators( wxT( " " ) );
522 
523  for( unsigned ii = 0; ii < m_PathsAndReferences.GetCount(); ii++ )
524  {
525  tokenizer.SetString( m_PathsAndReferences[ii], separators );
526  h_path = tokenizer.GetNextToken();
527 
528  if( h_path.Cmp( path ) == 0 )
529  {
530  h_ref = tokenizer.GetNextToken();
531 
532  /* printf( "GetRef hpath: %s\n",
533  * TO_UTF8( m_PathsAndReferences[ii] ) ); */
534  return h_ref;
535  }
536  }
537 
538  // if it was not found in m_Paths array, then see if it is in
539  // m_Field[REFERENCE] -- if so, use this as a default for this path.
540  // this will happen if we load a version 1 schematic file.
541  // it will also mean that multiple instances of the same sheet by default
542  // all have the same component references, but perhaps this is best.
543  if( !GetField( REFERENCE )->GetText().IsEmpty() )
544  {
545  SetRef( sheet, GetField( REFERENCE )->GetText() );
546  return GetField( REFERENCE )->GetText();
547  }
548 
549  return m_prefix;
550 }
551 
552 
553 /* Function IsReferenceStringValid (static function)
554  * Tests for an acceptable reference string
555  * An acceptable reference string must support unannotation
556  * i.e starts by letter
557  * returns true if OK
558  */
559 bool SCH_COMPONENT::IsReferenceStringValid( const wxString& aReferenceString )
560 {
561  wxString text = aReferenceString;
562  bool ok = true;
563 
564  // Try to unannotate this reference
565  while( !text.IsEmpty() && ( text.Last() == '?' || isdigit( text.Last() ) ) )
566  text.RemoveLast();
567 
568  if( text.IsEmpty() )
569  ok = false;
570 
571  // Add here other constraints
572  // Currently:no other constraint
573 
574  return ok;
575 }
576 
577 
578 void SCH_COMPONENT::SetRef( const SCH_SHEET_PATH* sheet, const wxString& ref )
579 {
580  wxString path = GetPath( sheet );
581 
582  bool notInArray = true;
583 
584  wxString h_path, h_ref;
585  wxStringTokenizer tokenizer;
586  wxString separators( wxT( " " ) );
587 
588  // check to see if it is already there before inserting it
589  for( unsigned ii = 0; ii < m_PathsAndReferences.GetCount(); ii++ )
590  {
591  tokenizer.SetString( m_PathsAndReferences[ii], separators );
592  h_path = tokenizer.GetNextToken();
593 
594  if( h_path.Cmp( path ) == 0 )
595  {
596  // just update the reference text, not the timestamp.
597  h_ref = h_path + wxT( " " ) + ref;
598  h_ref += wxT( " " );
599  tokenizer.GetNextToken(); // Skip old reference
600  h_ref += tokenizer.GetNextToken(); // Add part selection
601 
602  // Add the part selection
603  m_PathsAndReferences[ii] = h_ref;
604  notInArray = false;
605  }
606  }
607 
608  if( notInArray )
609  AddHierarchicalReference( path, ref, m_unit );
610 
611  SCH_FIELD* rf = GetField( REFERENCE );
612 
613  if( rf->GetText().IsEmpty()
614  || ( abs( rf->GetTextPos().x - m_Pos.x ) +
615  abs( rf->GetTextPos().y - m_Pos.y ) > 10000 ) )
616  {
617  // move it to a reasonable position
618  rf->SetTextPos( m_Pos + wxPoint( 50, 50 ) );
619  }
620 
621  rf->SetText( ref ); // for drawing.
622 
623  // Reinit the m_prefix member if needed
624  wxString prefix = ref;
625 
626  if( IsReferenceStringValid( prefix ) )
627  {
628  while( prefix.Last() == '?' || isdigit( prefix.Last() ) )
629  prefix.RemoveLast();
630  }
631  else
632  {
633  prefix = wxT( "U" ); // Set to default ref prefix
634  }
635 
636  if( m_prefix != prefix )
637  m_prefix = prefix;
638 }
639 
640 
641 void SCH_COMPONENT::SetTimeStamp( time_t aNewTimeStamp )
642 {
643  wxString string_timestamp, string_oldtimestamp;
644 
645  string_timestamp.Printf( wxT( "%08lX" ), (long unsigned) aNewTimeStamp );
646  string_oldtimestamp.Printf( wxT( "%08lX" ), (long unsigned) m_TimeStamp );
647  EDA_ITEM::SetTimeStamp( aNewTimeStamp );
648 
649  for( unsigned ii = 0; ii < m_PathsAndReferences.GetCount(); ii++ )
650  {
651  m_PathsAndReferences[ii].Replace( string_oldtimestamp.GetData(),
652  string_timestamp.GetData() );
653  }
654 }
655 
656 
658 {
659  wxString path = GetPath( aSheet );
660  wxString h_path, h_multi;
661  wxStringTokenizer tokenizer;
662  wxString separators( wxT( " " ) );
663 
664  for( unsigned ii = 0; ii < m_PathsAndReferences.GetCount(); ii++ )
665  {
666  tokenizer.SetString( m_PathsAndReferences[ii], separators );
667  h_path = tokenizer.GetNextToken();
668 
669  if( h_path.Cmp( path ) == 0 )
670  {
671  tokenizer.GetNextToken(); // Skip reference
672  h_multi = tokenizer.GetNextToken();
673  long imulti = 1;
674  h_multi.ToLong( &imulti );
675  return imulti;
676  }
677  }
678 
679  // if it was not found in m_Paths array, then use m_unit.
680  // this will happen if we load a version 1 schematic file.
681  return m_unit;
682 }
683 
684 
685 void SCH_COMPONENT::SetUnitSelection( SCH_SHEET_PATH* aSheet, int aUnitSelection )
686 {
687  wxString path = GetPath( aSheet );
688 
689  bool notInArray = true;
690 
691  wxString h_path, h_ref;
692  wxStringTokenizer tokenizer;
693  wxString separators( wxT( " " ) );
694 
695  //check to see if it is already there before inserting it
696  for( unsigned ii = 0; ii < m_PathsAndReferences.GetCount(); ii++ )
697  {
698  tokenizer.SetString( m_PathsAndReferences[ii], separators );
699  h_path = tokenizer.GetNextToken();
700 
701  if( h_path.Cmp( path ) == 0 )
702  {
703  //just update the unit selection.
704  h_ref = h_path + wxT( " " );
705  h_ref += tokenizer.GetNextToken(); // Add reference
706  h_ref += wxT( " " );
707  h_ref << aUnitSelection; // Add part selection
708 
709  // Ann the part selection
710  m_PathsAndReferences[ii] = h_ref;
711  notInArray = false;
712  }
713  }
714 
715  if( notInArray )
716  AddHierarchicalReference( path, m_prefix, aUnitSelection );
717 }
718 
719 
720 SCH_FIELD* SCH_COMPONENT::GetField( int aFieldNdx ) const
721 {
722  const SCH_FIELD* field;
723 
724  if( (unsigned) aFieldNdx < m_Fields.size() )
725  field = &m_Fields[aFieldNdx];
726  else
727  field = NULL;
728 
729  wxASSERT( field );
730 
731  // use cast to remove const-ness
732  return (SCH_FIELD*) field;
733 }
734 
735 
736 void SCH_COMPONENT::GetFields( std::vector<SCH_FIELD*>& aVector, bool aVisibleOnly )
737 {
738  for( SCH_FIELD& each_field : m_Fields )
739  {
740  if( !aVisibleOnly || ( each_field.IsVisible() && !each_field.IsVoid() ) )
741  aVector.push_back( &each_field );
742  }
743 }
744 
745 
747 {
748  int newNdx = m_Fields.size();
749 
750  m_Fields.push_back( aField );
751  return &m_Fields[newNdx];
752 }
753 
754 
755 SCH_FIELD* SCH_COMPONENT::FindField( const wxString& aFieldName )
756 {
757  for( unsigned i = 0; i<m_Fields.size(); ++i )
758  {
759  if( aFieldName == m_Fields[i].GetName( false ) )
760  return &m_Fields[i];
761  }
762 
763  return NULL;
764 }
765 
766 
767 LIB_PIN* SCH_COMPONENT::GetPin( const wxString& number )
768 {
769  if( PART_SPTR part = m_part.lock() )
770  {
771  return part->GetPin( number, m_unit, m_convert );
772  }
773  return NULL;
774 }
775 
776 
777 void SCH_COMPONENT::GetPins( std::vector<LIB_PIN*>& aPinsList )
778 {
779  if( PART_SPTR part = m_part.lock() )
780  {
781  part->GetPins( aPinsList, m_unit, m_convert );
782  }
783  else
784  wxFAIL_MSG( "Could not obtain PART_SPTR lock" );
785 }
786 
787 
789 {
790  wxCHECK_RET( (aItem != NULL) && (aItem->Type() == SCH_COMPONENT_T),
791  wxT( "Cannot swap data with invalid component." ) );
792 
793  SCH_COMPONENT* component = (SCH_COMPONENT*) aItem;
794 
795  std::swap( m_lib_id, component->m_lib_id );
796  std::swap( m_part, component->m_part );
797  std::swap( m_Pos, component->m_Pos );
798  std::swap( m_unit, component->m_unit );
799  std::swap( m_convert, component->m_convert );
800 
801  TRANSFORM tmp = m_transform;
802 
803  m_transform = component->m_transform;
804  component->m_transform = tmp;
805 
806  m_Fields.swap( component->m_Fields ); // std::vector's swap()
807 
808  // Reparent items after copying data
809  // (after swap(), m_Parent member does not point to the right parent):
810  for( int ii = 0; ii < component->GetFieldCount(); ++ii )
811  {
812  component->GetField( ii )->SetParent( component );
813  }
814 
815  for( int ii = 0; ii < GetFieldCount(); ++ii )
816  {
817  GetField( ii )->SetParent( this );
818  }
819 
820  std::swap( m_PathsAndReferences, component->m_PathsAndReferences );
821 }
822 
823 
825 {
826  bool keepMulti = false;
827  wxArrayString reference_fields;
828 
829  static const wxChar separators[] = wxT( " " );
830 
831  PART_SPTR part = m_part.lock();
832 
833  if( part && part->UnitsLocked() )
834  keepMulti = true;
835 
836  // Build a reference with no annotation,
837  // i.e. a reference ended by only one '?'
838  wxString defRef = m_prefix;
839 
840  if( IsReferenceStringValid( defRef ) )
841  {
842  while( defRef.Last() == '?' )
843  defRef.RemoveLast();
844  }
845  else
846  { // This is a malformed reference: reinit this reference
847  m_prefix = defRef = wxT("U"); // Set to default ref prefix
848  }
849 
850  defRef.Append( wxT( "?" ) );
851 
852  wxString multi = wxT( "1" );
853 
854  // For components with units locked,
855  // we cannot remove all annotations: part selection must be kept
856  // For all components: if aSheetPath is not NULL,
857  // remove annotation only for the given path
858  if( keepMulti || aSheetPath )
859  {
860  wxString NewHref;
861  wxString path;
862 
863  if( aSheetPath )
864  path = GetPath( aSheetPath );
865 
866  for( unsigned int ii = 0; ii < m_PathsAndReferences.GetCount(); ii++ )
867  {
868  // Break hierarchical reference in path, ref and multi selection:
869  reference_fields = wxStringTokenize( m_PathsAndReferences[ii], separators );
870 
871  if( aSheetPath == NULL || reference_fields[0].Cmp( path ) == 0 )
872  {
873  if( keepMulti ) // Get and keep part selection
874  multi = reference_fields[2];
875 
876  NewHref = reference_fields[0];
877  NewHref << wxT( " " ) << defRef << wxT( " " ) << multi;
878  m_PathsAndReferences[ii] = NewHref;
879  }
880  }
881  }
882  else
883  {
884  // Clear reference strings, but does not free memory because a new annotation
885  // will reuse it
886  m_PathsAndReferences.Empty();
887  m_unit = 1;
888  }
889 
890  // These 2 changes do not work in complex hierarchy.
891  // When a clear annotation is made, the calling function must call a
892  // UpdateAllScreenReferences for the active sheet.
893  // But this call cannot made here.
894  m_Fields[REFERENCE].SetText( defRef ); //for drawing.
895 
896  SetModified();
897 }
898 
899 
900 void SCH_COMPONENT::SetOrientation( int aOrientation )
901 {
902  TRANSFORM temp = TRANSFORM();
903  bool transform = false;
904 
905  switch( aOrientation )
906  {
907  case CMP_ORIENT_0:
908  case CMP_NORMAL: // default transform matrix
909  m_transform.x1 = 1;
910  m_transform.y2 = -1;
912  break;
913 
914  case CMP_ROTATE_COUNTERCLOCKWISE: // Rotate + (incremental rotation)
915  temp.x1 = temp.y2 = 0;
916  temp.y1 = 1;
917  temp.x2 = -1;
918  transform = true;
919  break;
920 
921  case CMP_ROTATE_CLOCKWISE: // Rotate - (incremental rotation)
922  temp.x1 = temp.y2 = 0;
923  temp.y1 = -1;
924  temp.x2 = 1;
925  transform = true;
926  break;
927 
928  case CMP_MIRROR_Y: // Mirror Y (incremental rotation)
929  temp.x1 = -1;
930  temp.y2 = 1;
931  temp.y1 = temp.x2 = 0;
932  transform = true;
933  break;
934 
935  case CMP_MIRROR_X: // Mirror X (incremental rotation)
936  temp.x1 = 1;
937  temp.y2 = -1;
938  temp.y1 = temp.x2 = 0;
939  transform = true;
940  break;
941 
942  case CMP_ORIENT_90:
945  break;
946 
947  case CMP_ORIENT_180:
951  break;
952 
953  case CMP_ORIENT_270:
956  break;
957 
958  case ( CMP_ORIENT_0 + CMP_MIRROR_X ):
961  break;
962 
963  case ( CMP_ORIENT_0 + CMP_MIRROR_Y ):
966  break;
967 
968  case ( CMP_ORIENT_90 + CMP_MIRROR_X ):
971  break;
972 
973  case ( CMP_ORIENT_90 + CMP_MIRROR_Y ):
976  break;
977 
978  case ( CMP_ORIENT_180 + CMP_MIRROR_X ):
981  break;
982 
983  case ( CMP_ORIENT_180 + CMP_MIRROR_Y ):
986  break;
987 
988  case ( CMP_ORIENT_270 + CMP_MIRROR_X ):
991  break;
992 
993  case ( CMP_ORIENT_270 + CMP_MIRROR_Y ):
996  break;
997 
998  default:
999  transform = false;
1000  wxMessageBox( wxT( "SetRotateMiroir() error: ill value" ) );
1001  break;
1002  }
1003 
1004  if( transform )
1005  {
1006  /* The new matrix transform is the old matrix transform modified by the
1007  * requested transformation, which is the temp transform (rot,
1008  * mirror ..) in order to have (in term of matrix transform):
1009  * transform coord = new_m_transform * coord
1010  * where transform coord is the coord modified by new_m_transform from
1011  * the initial value coord.
1012  * new_m_transform is computed (from old_m_transform and temp) to
1013  * have:
1014  * transform coord = old_m_transform * temp
1015  */
1016  TRANSFORM newTransform;
1017 
1018  newTransform.x1 = m_transform.x1 * temp.x1 + m_transform.x2 * temp.y1;
1019  newTransform.y1 = m_transform.y1 * temp.x1 + m_transform.y2 * temp.y1;
1020  newTransform.x2 = m_transform.x1 * temp.x2 + m_transform.x2 * temp.y2;
1021  newTransform.y2 = m_transform.y1 * temp.x2 + m_transform.y2 * temp.y2;
1022  m_transform = newTransform;
1023  }
1024 }
1025 
1026 
1028 {
1029  int type_rotate = CMP_ORIENT_0;
1030  TRANSFORM transform;
1031  int ii;
1032 
1033  #define ROTATE_VALUES_COUNT 12
1034 
1035  // list of all possibilities, but only the first 8 are actually used
1036  int rotate_value[ROTATE_VALUES_COUNT] =
1037  {
1043  CMP_MIRROR_Y + CMP_ORIENT_180, CMP_MIRROR_Y + CMP_ORIENT_270
1044  };
1045 
1046  // Try to find the current transform option:
1047  transform = m_transform;
1048 
1049  for( ii = 0; ii < ROTATE_VALUES_COUNT; ii++ )
1050  {
1051  type_rotate = rotate_value[ii];
1052  SetOrientation( type_rotate );
1053 
1054  if( transform == m_transform )
1055  return type_rotate;
1056  }
1057 
1058  // Error: orientation not found in list (should not happen)
1059  wxMessageBox( wxT( "Component orientation matrix internal error" ) );
1060  m_transform = transform;
1061 
1062  return CMP_NORMAL;
1063 }
1064 
1065 
1067 {
1068  return m_transform.TransformCoordinate( aPoint );
1069 }
1070 
1071 
1072 #if defined(DEBUG)
1073 
1074 void SCH_COMPONENT::Show( int nestLevel, std::ostream& os ) const
1075 {
1076  // for now, make it look like XML:
1077  NestedSpace( nestLevel, os ) << '<' << GetClass().Lower().mb_str()
1078  << " ref=\"" << TO_UTF8( GetField( 0 )->GetName() )
1079  << '"' << " chipName=\""
1080  << GetLibId().Format() << '"' << m_Pos
1081  << " layer=\"" << m_Layer
1082  << '"' << ">\n";
1083 
1084  // skip the reference, it's been output already.
1085  for( int i = 1; i < GetFieldCount(); ++i )
1086  {
1087  wxString value = GetField( i )->GetText();
1088 
1089  if( !value.IsEmpty() )
1090  {
1091  NestedSpace( nestLevel + 1, os ) << "<field" << " name=\""
1092  << TO_UTF8( GetField( i )->GetName() )
1093  << '"' << " value=\""
1094  << TO_UTF8( value ) << "\"/>\n";
1095  }
1096  }
1097 
1098  NestedSpace( nestLevel, os ) << "</" << TO_UTF8( GetClass().Lower() ) << ">\n";
1099 }
1100 
1101 #endif
1102 
1103 
1104 bool SCH_COMPONENT::Save( FILE* f ) const
1105 {
1106  std::string name1;
1107  std::string name2;
1108  wxArrayString reference_fields;
1109 
1110  static wxString delimiters( wxT( " " ) );
1111 
1112  // this is redundant with the AR entries below, but it makes the
1113  // files backwards-compatible.
1114  if( m_PathsAndReferences.GetCount() > 0 )
1115  {
1116  reference_fields = wxStringTokenize( m_PathsAndReferences[0], delimiters );
1117 
1118  name1 = toUTFTildaText( reference_fields[1] );
1119  }
1120  else
1121  {
1122  if( GetField( REFERENCE )->GetText().IsEmpty() )
1123  name1 = toUTFTildaText( m_prefix );
1124  else
1125  name1 = toUTFTildaText( GetField( REFERENCE )->GetText() );
1126  }
1127 
1128  wxString part_name = FROM_UTF8( GetLibId().GetLibItemName() );
1129 
1130  if( part_name.size() )
1131  {
1132  name2 = toUTFTildaText( part_name );
1133  }
1134  else
1135  {
1136  name2 = NULL_STRING;
1137  }
1138 
1139  if( fprintf( f, "$Comp\n" ) == EOF )
1140  return false;
1141 
1142  if( fprintf( f, "L %s %s\n", name2.c_str(), name1.c_str() ) == EOF )
1143  return false;
1144 
1145  // Generate unit number, convert and time stamp
1146  if( fprintf( f, "U %d %d %8.8lX\n", m_unit, m_convert, (unsigned long)m_TimeStamp ) == EOF )
1147  return false;
1148 
1149  // Save the position
1150  if( fprintf( f, "P %d %d\n", m_Pos.x, m_Pos.y ) == EOF )
1151  return false;
1152 
1153  /* If this is a complex hierarchy; save hierarchical references.
1154  * but for simple hierarchies it is not necessary.
1155  * the reference inf is already saved
1156  * this is useful for old Eeschema version compatibility
1157  */
1158  if( m_PathsAndReferences.GetCount() > 1 )
1159  {
1160  for( unsigned int ii = 0; ii < m_PathsAndReferences.GetCount(); ii++ )
1161  {
1162  /*format:
1163  * AR Path="/140/2" Ref="C99" Part="1"
1164  * where 140 is the uid of the containing sheet
1165  * and 2 is the timestamp of this component.
1166  * (timestamps are actually 8 hex chars)
1167  * Ref is the conventional component reference for this 'path'
1168  * Part is the conventional component part selection for this 'path'
1169  */
1170  reference_fields = wxStringTokenize( m_PathsAndReferences[ii], delimiters );
1171 
1172  if( fprintf( f, "AR Path=\"%s\" Ref=\"%s\" Part=\"%s\" \n",
1173  TO_UTF8( reference_fields[0] ),
1174  TO_UTF8( reference_fields[1] ),
1175  TO_UTF8( reference_fields[2] ) ) == EOF )
1176  return false;
1177  }
1178  }
1179 
1180  // update the ugly field index, which I would like to see go away someday soon.
1181  for( unsigned i = 0; i<m_Fields.size(); ++i )
1182  {
1183  SCH_FIELD* fld = GetField( i );
1184  fld->SetId( i ); // we don't need field Ids, please be gone.
1185  }
1186 
1187  // Fixed fields:
1188  // Save mandatory fields even if they are blank,
1189  // because the visibility, size and orientation are set from libary editor.
1190  for( unsigned i = 0; i<MANDATORY_FIELDS; ++i )
1191  {
1192  SCH_FIELD* fld = GetField( i );
1193  if( !fld->Save( f ) )
1194  return false;
1195  }
1196 
1197  // User defined fields:
1198  // The *policy* about which user defined fields are part of a symbol is now
1199  // only in the dialog editors. No policy should be enforced here, simply
1200  // save all the user defined fields, they are present because a dialog editor
1201  // thought they should be. If you disagree, go fix the dialog editors.
1202  for( unsigned i = MANDATORY_FIELDS; i<m_Fields.size(); ++i )
1203  {
1204  SCH_FIELD* fld = GetField( i );
1205 
1206  if( !fld->Save( f ) )
1207  return false;
1208  }
1209 
1210  // Unit number, position, box ( old standard )
1211  if( fprintf( f, "\t%-4d %-4d %-4d\n", m_unit, m_Pos.x, m_Pos.y ) == EOF )
1212  return false;
1213 
1214  if( fprintf( f, "\t%-4d %-4d %-4d %-4d\n",
1216  return false;
1217 
1218  if( fprintf( f, "$EndComp\n" ) == EOF )
1219  return false;
1220 
1221  return true;
1222 }
1223 
1224 
1225 bool SCH_COMPONENT::Load( LINE_READER& aLine, wxString& aErrorMsg )
1226 {
1227  // Remark: avoid using sscanf to read texts entered by user
1228  // which are UTF8 encoded, because sscanf does not work well on Windows
1229  // with some UTF8 values.
1230  char name1[256], char1[256], char2[256], char3[256];
1231  int newfmt = 0;
1232  char* ptcar;
1233  wxString fieldName;
1234  char* line = aLine.Line();
1235  unsigned long timeStamp;
1236 
1237  m_convert = 1;
1238 
1239  if( line[0] == '$' )
1240  {
1241  newfmt = 1;
1242 
1243  if( !(line = aLine.ReadLine()) )
1244  return true;
1245  }
1246 
1247  // Parse the first line of description:
1248  // like "L partname ref" (for instance "L 74LS00 U4"
1249  // They are UTF8 texts, so do not use sscanf
1250 
1251  line += 1;
1252 
1253  if( *line == ' ' )
1254  line++;
1255 
1256  // line points the first parameter
1257  wxString buffer( FROM_UTF8( line ) );
1258  wxStringTokenizer tokenizer( buffer, wxT( " \r\n" ) );
1259 
1260  if( tokenizer.CountTokens() < 2 )
1261  {
1262  aErrorMsg.Printf( wxT( "Eeschema component description error at line %d, aborted" ),
1263  aLine.LineNumber() );
1264  aErrorMsg << wxT( "\n" ) << FROM_UTF8( line );
1265  return false;
1266  }
1267 
1268  wxString partname = tokenizer.NextToken();
1269  partname.Replace( wxT("~"), wxT(" ") ); // all spaces were replaced by ~ in files.
1270 
1271  if( partname != NULL_STRING )
1272  {
1273  m_lib_id.SetLibItemName( TO_UTF8( partname ), false );
1274 
1275  if( !newfmt )
1276  GetField( VALUE )->SetText( partname );
1277  }
1278  else
1279  {
1280  m_lib_id.clear();
1281  GetField( VALUE )->Empty();
1283  GetField( VALUE )->SetVisible( false );
1284  }
1285 
1286  wxString reference = tokenizer.NextToken();
1287  reference.Replace( wxT("~"), wxT(" ") ); // all spaces were replaced by ~ in files.
1288  reference.Trim( true );
1289  reference.Trim( false );
1290 
1291  if( reference != NULL_STRING )
1292  {
1293  wxString prefix = reference;
1294  // Build reference prefix from the actual reference by removing trailing digits
1295  // (Perhaps outdated code, only for very old schematic files)
1296  while( prefix.Length() )
1297  {
1298  if( ( prefix.Last() < '0' || prefix.Last() > '9') && prefix.Last() != '?' )
1299  break;
1300 
1301  prefix.RemoveLast();
1302  }
1303 
1304  // Avoid a prefix containing trailing/leading spaces
1305  prefix.Trim( true );
1306  prefix.Trim( false );
1307 
1308  if( prefix.IsEmpty() )
1309  m_prefix = wxT( "U" );
1310  else
1311  m_prefix = prefix;
1312 
1313  if( !newfmt )
1314  GetField( REFERENCE )->SetText( reference );
1315  }
1316  else
1317  {
1318  GetField( REFERENCE )->SetVisible( false );
1319  }
1320 
1321  /* Parse component description
1322  * These lines begin with:
1323  * "P" = Position
1324  * U = Num Unit and Conversion
1325  * "Fn" = Fields (0 .. n = = number of field)
1326  * "Ar" = Alternate reference in the case of multiple sheets referring to
1327  * one schematic file.
1328  */
1329  for( ; ; )
1330  {
1331  if( !(line = aLine.ReadLine()) )
1332  return false;
1333 
1334  if( line[0] == 'U' )
1335  {
1336  sscanf( line + 1, "%d %d %lX", &m_unit, &m_convert, &timeStamp );
1337  m_TimeStamp = (time_t)timeStamp;
1338  }
1339  else if( line[0] == 'P' )
1340  {
1341  sscanf( line + 1, "%d %d", &m_Pos.x, &m_Pos.y );
1342 
1343  // Set fields position to a default position (that is the
1344  // component position. For existing fields, the real position
1345  // will be set later
1346  for( int i = 0; i<GetFieldCount(); i++ )
1347  {
1348  if( GetField( i )->GetText().IsEmpty() )
1349  GetField( i )->SetTextPos( m_Pos );
1350  }
1351  }
1352  else if( line[0] == 'A' && line[1] == 'R' )
1353  {
1354  /* format:
1355  * AR Path="/9086AF6E/67452AA0" Ref="C99" Part="1"
1356  * where 9086AF6E is the unique timestamp of the containing sheet
1357  * and 67452AA0 is the timestamp of this component.
1358  * C99 is the reference given this path.
1359  */
1360  int ii;
1361  ptcar = line + 2;
1362 
1363  //copy the path.
1364  ii = ReadDelimitedText( name1, ptcar, 255 );
1365  ptcar += ii + 1;
1366  wxString path = FROM_UTF8( name1 );
1367 
1368  // copy the reference
1369  ii = ReadDelimitedText( name1, ptcar, 255 );
1370  ptcar += ii + 1;
1371  wxString ref = FROM_UTF8( name1 );
1372 
1373  // copy the multi, if exists
1374  ii = ReadDelimitedText( name1, ptcar, 255 );
1375 
1376  if( name1[0] == 0 ) // Nothing read, put a default value
1377  sprintf( name1, "%d", m_unit );
1378 
1379  int multi = atoi( name1 );
1380 
1381  if( multi < 0 || multi > 26 )
1382  multi = 1;
1383 
1384  AddHierarchicalReference( path, ref, multi );
1385  GetField( REFERENCE )->SetText( ref );
1386  }
1387  else if( line[0] == 'F' )
1388  {
1389  int fieldNdx;
1390 
1391  wxString fieldText;
1394 
1395  ptcar = (char*) aLine;
1396 
1397  while( *ptcar && (*ptcar != '"') )
1398  ptcar++;
1399 
1400  if( *ptcar != '"' )
1401  {
1402  aErrorMsg.Printf( wxT( "Eeschema file library field F at line %d, aborted" ),
1403  aLine.LineNumber() );
1404  return false;
1405  }
1406 
1407  ptcar += ReadDelimitedText( &fieldText, ptcar );
1408 
1409  if( *ptcar == 0 )
1410  {
1411  aErrorMsg.Printf( wxT( "Component field F at line %d, aborted" ),
1412  aLine.LineNumber() );
1413  return false;
1414  }
1415 
1416  fieldNdx = atoi( line + 2 );
1417 
1418  ReadDelimitedText( &fieldName, ptcar );
1419 
1420  if( fieldName.IsEmpty() )
1421  fieldName = TEMPLATE_FIELDNAME::GetDefaultFieldName( fieldNdx );
1422 
1423  if( fieldNdx >= GetFieldCount() )
1424  {
1425  // The first MANDATOR_FIELDS _must_ be constructed within
1426  // the SCH_COMPONENT constructor. This assert is simply here
1427  // to guard against a change in that constructor.
1428  wxASSERT( GetFieldCount() >= MANDATORY_FIELDS );
1429 
1430  // Ignore the _supplied_ fieldNdx. It is not important anymore
1431  // if within the user defined fields region (i.e. >= MANDATORY_FIELDS).
1432  // We freely renumber the index to fit the next available field slot.
1433 
1434  fieldNdx = GetFieldCount(); // new has this index after insertion
1435 
1436  SCH_FIELD field( wxPoint( 0, 0 ),
1437  -1, // field id is not relavant for user defined fields
1438  this, fieldName );
1439 
1440  AddField( field );
1441  }
1442  else
1443  {
1444  GetField( fieldNdx )->SetName( fieldName );
1445  }
1446 
1447  GetField( fieldNdx )->SetText( fieldText );
1448  memset( char3, 0, sizeof(char3) );
1449  int ii, x, y, w, attr;
1450 
1451  if( ( ii = sscanf( ptcar, "%255s %d %d %d %X %255s %255s", char1, &x, &y, &w, &attr,
1452  char2, char3 ) ) < 4 )
1453  {
1454  aErrorMsg.Printf( wxT( "Component Field error line %d, aborted" ),
1455  aLine.LineNumber() );
1456  continue;
1457  }
1458 
1459  GetField( fieldNdx )->SetTextPos( wxPoint( x, y ) );
1460  GetField( fieldNdx )->SetVisible( !attr );
1461 
1462  if( (w == 0 ) || (ii == 4) )
1463  w = GetDefaultTextSize();
1464 
1465  GetField( fieldNdx )->SetTextSize( wxSize( w, w ) );
1466  GetField( fieldNdx )->SetTextAngle( TEXT_ANGLE_HORIZ );
1467 
1468  if( char1[0] == 'V' )
1469  GetField( fieldNdx )->SetTextAngle( TEXT_ANGLE_VERT );
1470 
1471  if( ii >= 7 )
1472  {
1473  if( *char2 == 'L' )
1474  hjustify = GR_TEXT_HJUSTIFY_LEFT;
1475  else if( *char2 == 'R' )
1476  hjustify = GR_TEXT_HJUSTIFY_RIGHT;
1477 
1478  if( char3[0] == 'B' )
1479  vjustify = GR_TEXT_VJUSTIFY_BOTTOM;
1480  else if( char3[0] == 'T' )
1481  vjustify = GR_TEXT_VJUSTIFY_TOP;
1482 
1483  GetField( fieldNdx )->SetItalic( char3[1] == 'I' );
1484  GetField( fieldNdx )->SetBold( char3[2] == 'B' );
1485  GetField( fieldNdx )->SetHorizJustify( hjustify );
1486  GetField( fieldNdx )->SetVertJustify( vjustify );
1487  }
1488 
1489  if( fieldNdx == REFERENCE )
1490  if( GetField( fieldNdx )->GetText()[0] == '#' )
1491  GetField( fieldNdx )->SetVisible( false );
1492  }
1493  else
1494  {
1495  break;
1496  }
1497  }
1498 
1499  if( sscanf( line, "%d %d %d", &m_unit, &m_Pos.x, &m_Pos.y ) != 3 )
1500  {
1501  aErrorMsg.Printf( wxT( "Component unit & pos error at line %d, aborted" ),
1502  aLine.LineNumber() );
1503  return false;
1504  }
1505 
1506  if( !(line = aLine.ReadLine()) ||
1507  sscanf( line, "%d %d %d %d",
1508  &m_transform.x1,
1509  &m_transform.y1,
1510  &m_transform.x2,
1511  &m_transform.y2 ) != 4 )
1512  {
1513  aErrorMsg.Printf( wxT( "Component orient error at line %d, aborted" ),
1514  aLine.LineNumber() );
1515  return false;
1516  }
1517 
1518  if( newfmt )
1519  {
1520  if( !(line = aLine.ReadLine()) )
1521  return false;
1522 
1523  if( strncasecmp( "$End", line, 4 ) != 0 )
1524  {
1525  aErrorMsg.Printf( wxT( "Component End expected at line %d, aborted" ),
1526  aLine.LineNumber() );
1527  return false;
1528  }
1529  }
1530 
1531  // ensure flags (mainly used in edit) are cleared.
1532  // some changes have set the modified flag
1533  m_Flags = 0;
1534 
1535  return true;
1536 }
1537 
1538 
1540 {
1541  EDA_RECT bBox;
1542 
1543  if( PART_SPTR part = m_part.lock() )
1544  {
1545  bBox = part->GetBodyBoundingBox( m_unit, m_convert );
1546  }
1547  else
1548  {
1549  bBox = dummy()->GetBodyBoundingBox( m_unit, m_convert );
1550  }
1551 
1552  int x0 = bBox.GetX();
1553  int xm = bBox.GetRight();
1554 
1555  // We must reverse Y values, because matrix orientation
1556  // suppose Y axis normal for the library items coordinates,
1557  // m_transform reverse Y values, but bBox is already reversed!
1558  int y0 = -bBox.GetY();
1559  int ym = -bBox.GetBottom();
1560 
1561  // Compute the real Boundary box (rotated, mirrored ...)
1562  int x1 = m_transform.x1 * x0 + m_transform.y1 * y0;
1563  int y1 = m_transform.x2 * x0 + m_transform.y2 * y0;
1564  int x2 = m_transform.x1 * xm + m_transform.y1 * ym;
1565  int y2 = m_transform.x2 * xm + m_transform.y2 * ym;
1566 
1567  // H and W must be > 0:
1568  if( x2 < x1 )
1569  std::swap( x2, x1 );
1570 
1571  if( y2 < y1 )
1572  std::swap( y2, y1 );
1573 
1574  bBox.SetX( x1 );
1575  bBox.SetY( y1 );
1576  bBox.SetWidth( x2 - x1 );
1577  bBox.SetHeight( y2 - y1 );
1578 
1579  bBox.Offset( m_Pos );
1580  return bBox;
1581 }
1582 
1583 
1585 {
1586  EDA_RECT bbox = GetBodyBoundingBox();
1587 
1588  for( size_t i = 0; i < m_Fields.size(); i++ )
1589  {
1590  bbox.Merge( m_Fields[i].GetBoundingBox() );
1591  }
1592 
1593  return bbox;
1594 }
1595 
1596 
1598 {
1599  // part and alias can differ if alias is not the root
1600  if( PART_SPTR part = m_part.lock() )
1601  {
1602  LIB_ALIAS* alias = part->GetAlias( FROM_UTF8( GetLibId().GetLibItemName() ) );
1603 
1604  if( !alias )
1605  return;
1606 
1607  if( m_currentSheetPath )
1608  aList.push_back( MSG_PANEL_ITEM( _( "Reference" ),
1610  DARKCYAN ) );
1611 
1612  wxString msg = part->IsPower() ? _( "Power symbol" ) : _( "Value" );
1613 
1614  aList.push_back( MSG_PANEL_ITEM( msg, GetField( VALUE )->GetShownText(), DARKCYAN ) );
1615 
1616  // Display component reference in library and library
1617  aList.push_back( MSG_PANEL_ITEM( _( "Component" ), FROM_UTF8( GetLibId().GetLibItemName() ),
1618  BROWN ) );
1619 
1620  if( alias->GetName() != part->GetName() )
1621  aList.push_back( MSG_PANEL_ITEM( _( "Alias of" ), part->GetName(), BROWN ) );
1622 
1623  aList.push_back( MSG_PANEL_ITEM( _( "Library" ), alias->GetLibraryName(), BROWN ) );
1624 
1625  // Display the current associated footprint, if exists.
1626  if( !GetField( FOOTPRINT )->IsVoid() )
1627  msg = GetField( FOOTPRINT )->GetShownText();
1628  else
1629  msg = _( "<Unknown>" );
1630 
1631  aList.push_back( MSG_PANEL_ITEM( _( "Footprint" ), msg, DARKRED ) );
1632 
1633  // Display description of the component, and keywords found in lib
1634  aList.push_back( MSG_PANEL_ITEM( _( "Description" ), alias->GetDescription(), DARKCYAN ) );
1635  aList.push_back( MSG_PANEL_ITEM( _( "Key Words" ), alias->GetKeyWords(), DARKCYAN ) );
1636  }
1637 }
1638 
1639 
1641 {
1642  return add_component_xpm;
1643 }
1644 
1645 
1646 void SCH_COMPONENT::MirrorY( int aYaxis_position )
1647 {
1648  int dx = m_Pos.x;
1649 
1651  MIRROR( m_Pos.x, aYaxis_position );
1652  dx -= m_Pos.x; // dx,0 is the move vector for this transform
1653 
1654  for( int ii = 0; ii < GetFieldCount(); ii++ )
1655  {
1656  // Move the fields to the new position because the component itself has moved.
1657  wxPoint pos = GetField( ii )->GetTextPos();
1658  pos.x -= dx;
1659  GetField( ii )->SetTextPos( pos );
1660  }
1661 }
1662 
1663 
1664 void SCH_COMPONENT::MirrorX( int aXaxis_position )
1665 {
1666  int dy = m_Pos.y;
1667 
1669  MIRROR( m_Pos.y, aXaxis_position );
1670  dy -= m_Pos.y; // dy,0 is the move vector for this transform
1671 
1672  for( int ii = 0; ii < GetFieldCount(); ii++ )
1673  {
1674  // Move the fields to the new position because the component itself has moved.
1675  wxPoint pos = GetField( ii )->GetTextPos();
1676  pos.y -= dy;
1677  GetField( ii )->SetTextPos( pos );
1678  }
1679 }
1680 
1681 
1683 {
1684  wxPoint prev = m_Pos;
1685 
1686  RotatePoint( &m_Pos, aPosition, 900 );
1687 
1689 
1690  for( int ii = 0; ii < GetFieldCount(); ii++ )
1691  {
1692  // Move the fields to the new position because the component itself has moved.
1693  wxPoint pos = GetField( ii )->GetTextPos();
1694  pos.x -= prev.x - m_Pos.x;
1695  pos.y -= prev.y - m_Pos.y;
1696  GetField( ii )->SetTextPos( pos );
1697  }
1698 }
1699 
1700 
1701 bool SCH_COMPONENT::Matches( wxFindReplaceData& aSearchData, void* aAuxData,
1702  wxPoint* aFindLocation )
1703 {
1704  wxLogTrace( traceFindItem, wxT( " item " ) + GetSelectMenuText() );
1705 
1706  // Components are searchable via the child field and pin item text.
1707  return false;
1708 }
1709 
1710 
1711 void SCH_COMPONENT::GetEndPoints( std::vector <DANGLING_END_ITEM>& aItemList )
1712 {
1713  if( PART_SPTR part = m_part.lock() )
1714  {
1715  for( LIB_PIN* pin = part->GetNextPin(); pin; pin = part->GetNextPin( pin ) )
1716  {
1717  wxASSERT( pin->Type() == LIB_PIN_T );
1718 
1719  if( pin->GetUnit() && m_unit && ( m_unit != pin->GetUnit() ) )
1720  continue;
1721 
1722  if( pin->GetConvert() && m_convert && ( m_convert != pin->GetConvert() ) )
1723  continue;
1724 
1725  DANGLING_END_ITEM item( PIN_END, pin, GetPinPhysicalPosition( pin ) );
1726  aItemList.push_back( item );
1727  }
1728  }
1729 }
1730 
1731 
1732 bool SCH_COMPONENT::IsPinDanglingStateChanged( std::vector<DANGLING_END_ITEM> &aItemList,
1733  LIB_PINS& aLibPins, unsigned aPin )
1734 {
1735  bool previousState;
1736 
1737  if( aPin < m_isDangling.size() )
1738  {
1739  previousState = m_isDangling[aPin];
1740  m_isDangling[aPin] = true;
1741  }
1742  else
1743  {
1744  previousState = true;
1745  m_isDangling.push_back( true );
1746  }
1747 
1748  wxPoint pin_position = GetPinPhysicalPosition( aLibPins[aPin] );
1749 
1750  for( DANGLING_END_ITEM& each_item : aItemList )
1751  {
1752  // Some people like to stack pins on top of each other in a symbol to indicate
1753  // internal connection. While technically connected, it is not particularly useful
1754  // to display them that way, so skip any pins that are in the same symbol as this
1755  // one.
1756  //
1757  // Do not make this exception for hidden pins, because those actually make internal
1758  // connections to a power net.
1759  const LIB_PIN* item_pin = dynamic_cast<const LIB_PIN*>( each_item.GetItem() );
1760 
1761  if( item_pin
1762  && ( !item_pin->IsPowerConnection() || !IsInNetlist() )
1763  && std::find( aLibPins.begin(), aLibPins.end(), item_pin) != aLibPins.end() )
1764  continue;
1765 
1766  switch( each_item.GetType() )
1767  {
1768  case PIN_END:
1769  case LABEL_END:
1770  case SHEET_LABEL_END:
1771  case WIRE_START_END:
1772  case WIRE_END_END:
1773  case NO_CONNECT_END:
1774  case JUNCTION_END:
1775  if( pin_position == each_item.GetPosition() )
1776  m_isDangling[aPin] = false;
1777  break;
1778  default:
1779  break;
1780  }
1781  if( !m_isDangling[aPin] )
1782  break;
1783  }
1784 
1785  return previousState != m_isDangling[aPin];
1786 }
1787 
1788 
1789 bool SCH_COMPONENT::IsDanglingStateChanged( std::vector<DANGLING_END_ITEM>& aItemList )
1790 {
1791  bool changed = false;
1792  LIB_PINS libPins;
1793  if( PART_SPTR part = m_part.lock() )
1794  part->GetPins( libPins, m_unit, m_convert );
1795  for( size_t i = 0; i < libPins.size(); ++i )
1796  {
1797  if( IsPinDanglingStateChanged( aItemList, libPins, i ) )
1798  changed = true;
1799  }
1800  return changed;
1801 }
1802 
1803 
1805 {
1806  for( bool each : m_isDangling )
1807  {
1808  if( each )
1809  return true;
1810  }
1811  return false;
1812 }
1813 
1814 
1816 {
1817  wxCHECK_MSG( Pin != NULL && Pin->Type() == LIB_PIN_T, wxPoint( 0, 0 ),
1818  wxT( "Cannot get physical position of pin." ) );
1819 
1820  return m_transform.TransformCoordinate( Pin->GetPosition() ) + m_Pos;
1821 }
1822 
1823 
1824 bool SCH_COMPONENT::IsSelectStateChanged( const wxRect& aRect )
1825 {
1826  bool previousState = IsSelected();
1827 
1829 
1830  if( aRect.Intersects( boundingBox ) )
1831  SetFlags( SELECTED );
1832  else
1833  ClearFlags( SELECTED );
1834 
1835  return previousState != IsSelected();
1836 }
1837 
1838 
1839 void SCH_COMPONENT::GetConnectionPoints( std::vector< wxPoint >& aPoints ) const
1840 {
1841  if( PART_SPTR part = m_part.lock() )
1842  {
1843  for( LIB_PIN* pin = part->GetNextPin(); pin; pin = part->GetNextPin( pin ) )
1844  {
1845  wxCHECK_RET( pin->Type() == LIB_PIN_T,
1846  wxT( "GetNextPin() did not return a pin object. Bad programmer!" ) );
1847 
1848  // Skip items not used for this part.
1849  if( m_unit && pin->GetUnit() && ( pin->GetUnit() != m_unit ) )
1850  continue;
1851 
1852  if( m_convert && pin->GetConvert() && ( pin->GetConvert() != m_convert ) )
1853  continue;
1854 
1855  // Calculate the pin position relative to the component position and orientation.
1856  aPoints.push_back( m_transform.TransformCoordinate( pin->GetPosition() ) + m_Pos );
1857  }
1858  }
1859  else
1860  {
1861  wxCHECK_RET( 0,
1862  wxT( "Cannot add connection points to list. Cannot find component <" ) +
1863  FROM_UTF8( GetLibId().GetLibItemName() ) +
1864  wxT( "> in any of the loaded libraries." ) );
1865  }
1866 }
1867 
1868 
1870 {
1871  if( PART_SPTR part = m_part.lock() )
1872  {
1873  // Calculate the position relative to the component.
1874  wxPoint libPosition = aPosition - m_Pos;
1875 
1876  return part->LocateDrawItem( m_unit, m_convert, aType, libPosition, m_transform );
1877  }
1878 
1879  return NULL;
1880 }
1881 
1882 
1884 {
1885  wxString tmp;
1886  tmp.Printf( _( "Component %s, %s" ),
1887  FROM_UTF8( GetLibId().GetLibItemName() ),
1888  GetChars( GetField( REFERENCE )->GetShownText() ) );
1889  return tmp;
1890 }
1891 
1892 
1893 SEARCH_RESULT SCH_COMPONENT::Visit( INSPECTOR aInspector, void* aTestData,
1894  const KICAD_T aFilterTypes[] )
1895 {
1896  KICAD_T stype;
1897 
1898  for( const KICAD_T* p = aFilterTypes; (stype = *p) != EOT; ++p )
1899  {
1900  // If caller wants to inspect component type or and component children types.
1901  if( stype == Type() )
1902  {
1903  if( SEARCH_QUIT == aInspector( this, aTestData ) )
1904  return SEARCH_QUIT;
1905  }
1906 
1907  switch( stype )
1908  {
1909  case SCH_FIELD_T:
1910  // Test the bounding boxes of fields if they are visible and not empty.
1911  for( int ii = 0; ii < GetFieldCount(); ii++ )
1912  {
1913  if( SEARCH_QUIT == aInspector( GetField( ii ), (void*) this ) )
1914  return SEARCH_QUIT;
1915  }
1916  break;
1917 
1919  if( SEARCH_QUIT == aInspector( GetField( REFERENCE ), (void*) this ) )
1920  return SEARCH_QUIT;
1921  break;
1922 
1924  if( SEARCH_QUIT == aInspector( GetField( VALUE ), (void*) this ) )
1925  return SEARCH_QUIT;
1926  break;
1927 
1929  if( SEARCH_QUIT == aInspector( GetField( FOOTPRINT ), (void*) this ) )
1930  return SEARCH_QUIT;
1931  break;
1932 
1933 
1934  case LIB_PIN_T:
1935  if( PART_SPTR part = m_part.lock() )
1936  {
1937  LIB_PINS pins;
1938 
1939  part->GetPins( pins, m_unit, m_convert );
1940 
1941  for( size_t i = 0; i < pins.size(); i++ )
1942  {
1943  if( SEARCH_QUIT == aInspector( pins[ i ], (void*) this ) )
1944  return SEARCH_QUIT;
1945  }
1946  }
1947  break;
1948 
1949  default:
1950  break;
1951  }
1952  }
1953 
1954  return SEARCH_CONTINUE;
1955 }
1956 
1957 
1959  SCH_SHEET_PATH* aSheetPath )
1960 {
1961  if( PART_SPTR part = m_part.lock() )
1962  {
1963  for( LIB_PIN* pin = part->GetNextPin(); pin; pin = part->GetNextPin( pin ) )
1964  {
1965  wxASSERT( pin->Type() == LIB_PIN_T );
1966 
1967  if( pin->GetUnit() && ( pin->GetUnit() != GetUnitSelection( aSheetPath ) ) )
1968  continue;
1969 
1970  if( pin->GetConvert() && ( pin->GetConvert() != GetConvert() ) )
1971  continue;
1972 
1973  wxPoint pos = GetTransform().TransformCoordinate( pin->GetPosition() ) + m_Pos;
1974 
1975  NETLIST_OBJECT* item = new NETLIST_OBJECT();
1976  item->m_SheetPathInclude = *aSheetPath;
1977  item->m_Comp = (SCH_ITEM*) pin;
1978  item->m_SheetPath = *aSheetPath;
1979  item->m_Type = NET_PIN;
1980  item->m_Link = (SCH_ITEM*) this;
1981  item->m_ElectricalPinType = pin->GetType();
1982  item->m_PinNum = pin->GetNumber();
1983  item->m_Label = pin->GetName();
1984  item->m_Start = item->m_End = pos;
1985 
1986  aNetListItems.push_back( item );
1987 
1988  if( pin->IsPowerConnection() )
1989  {
1990  // There is an associated PIN_LABEL.
1991  item = new NETLIST_OBJECT();
1992  item->m_SheetPathInclude = *aSheetPath;
1993  item->m_Comp = NULL;
1994  item->m_SheetPath = *aSheetPath;
1995  item->m_Type = NET_PINLABEL;
1996  item->m_Label = pin->GetName();
1997  item->m_Start = pos;
1998  item->m_End = item->m_Start;
1999 
2000  aNetListItems.push_back( item );
2001  }
2002  }
2003  }
2004 }
2005 
2006 
2007 bool SCH_COMPONENT::operator <( const SCH_ITEM& aItem ) const
2008 {
2009  if( Type() != aItem.Type() )
2010  return Type() < aItem.Type();
2011 
2012  SCH_COMPONENT* component = (SCH_COMPONENT*) &aItem;
2013 
2014  EDA_RECT rect = GetBodyBoundingBox();
2015 
2016  if( rect.GetArea() != component->GetBodyBoundingBox().GetArea() )
2017  return rect.GetArea() < component->GetBodyBoundingBox().GetArea();
2018 
2019  if( m_Pos.x != component->m_Pos.x )
2020  return m_Pos.x < component->m_Pos.x;
2021 
2022  if( m_Pos.y != component->m_Pos.y )
2023  return m_Pos.y < component->m_Pos.y;
2024 
2025  return false;
2026 }
2027 
2028 
2029 bool SCH_COMPONENT::operator==( const SCH_COMPONENT& aComponent ) const
2030 {
2031  if( GetFieldCount() != aComponent.GetFieldCount() )
2032  return false;
2033 
2034  for( int i = VALUE; i < GetFieldCount(); i++ )
2035  {
2036  if( GetField( i )->GetText().Cmp( aComponent.GetField( i )->GetText() ) != 0 )
2037  return false;
2038  }
2039 
2040  return true;
2041 }
2042 
2043 
2044 bool SCH_COMPONENT::operator!=( const SCH_COMPONENT& aComponent ) const
2045 {
2046  return !( *this == aComponent );
2047 }
2048 
2049 
2051 {
2052  wxCHECK_MSG( Type() == aItem.Type(), *this,
2053  wxT( "Cannot assign object type " ) + aItem.GetClass() + wxT( " to type " ) +
2054  GetClass() );
2055 
2056  if( &aItem != this )
2057  {
2058  SCH_ITEM::operator=( aItem );
2059 
2060  SCH_COMPONENT* c = (SCH_COMPONENT*) &aItem;
2061 
2062  m_lib_id = c->m_lib_id;
2063  m_part = c->m_part;
2064  m_Pos = c->m_Pos;
2065  m_unit = c->m_unit;
2066  m_convert = c->m_convert;
2067  m_transform = c->m_transform;
2068 
2070 
2071  m_Fields = c->m_Fields; // std::vector's assignment operator.
2072 
2073  // Reparent fields after assignment to new component.
2074  for( int ii = 0; ii < GetFieldCount(); ++ii )
2075  {
2076  GetField( ii )->SetParent( this );
2077  }
2078  }
2079 
2080  return *this;
2081 }
2082 
2083 
2084 bool SCH_COMPONENT::HitTest( const wxPoint& aPosition, int aAccuracy ) const
2085 {
2086  EDA_RECT bBox = GetBodyBoundingBox();
2087  bBox.Inflate( aAccuracy );
2088 
2089  if( bBox.Contains( aPosition ) )
2090  return true;
2091 
2092  return false;
2093 }
2094 
2095 
2096 bool SCH_COMPONENT::HitTest( const EDA_RECT& aRect, bool aContained, int aAccuracy ) const
2097 {
2099  return false;
2100 
2101  EDA_RECT rect = aRect;
2102 
2103  rect.Inflate( aAccuracy );
2104 
2105  if( aContained )
2106  return rect.Contains( GetBodyBoundingBox() );
2107 
2108  return rect.Intersects( GetBodyBoundingBox() );
2109 }
2110 
2111 
2112 bool SCH_COMPONENT::doIsConnected( const wxPoint& aPosition ) const
2113 {
2114  std::vector< wxPoint > pts;
2115 
2116  GetConnectionPoints( pts );
2117 
2118  for( size_t i = 0; i < pts.size(); i++ )
2119  {
2120  if( pts[i] == aPosition )
2121  return true;
2122  }
2123 
2124  return false;
2125 }
2126 
2127 
2129 {
2130  SCH_FIELD* rf = GetField( REFERENCE );
2131  return ! rf->GetText().StartsWith( wxT( "#" ) );
2132 }
2133 
2134 
2135 void SCH_COMPONENT::Plot( PLOTTER* aPlotter )
2136 {
2137  TRANSFORM temp;
2138 
2139  if( PART_SPTR part = m_part.lock() )
2140  {
2141  temp = GetTransform();
2142 
2143  part->Plot( aPlotter, GetUnit(), GetConvert(), m_Pos, temp );
2144 
2145  for( size_t i = 0; i < m_Fields.size(); i++ )
2146  {
2147  m_Fields[i].Plot( aPlotter );
2148  }
2149  }
2150 }
void SetTextAngle(double aAngle)
Definition: eda_text.h:156
Definition of the SCH_SHEET class for Eeschema.
LIB_ALIAS * LoadSymbol(const wxString &aNickname, const wxString &aAliasName)
Function LoadSymbol.
bool HitTest(const wxPoint &aPosition, int aAccuracy) const override
Function HitTest tests if aPosition is contained within or on the bounding box of an item...
int GetCount() const
Function GetCount returns the number of objects in the list.
KICAD_T Type() const
Function Type()
Definition: base_struct.h:198
#define TEXT_ANGLE_HORIZ
Frequent text rotations, used with {Set,Get}TextAngle(), in 0.1 degrees for now, hoping to migrate to...
Definition: common.h:91
void Offset(int dx, int dy)
Class SCH_FIELD instances are attached to a component and provide a place for the component's value...
Definition: sch_field.h:56
SCH_LAYER_ID m_Layer
void SetModified()
void GetMsgPanelInfo(std::vector< MSG_PANEL_ITEM > &aList) override
Function GetMsgPanelInfo populates aList of MSG_PANEL_ITEM objects with it's internal state for displ...
void Rotate(wxPoint aPosition) override
Function Rotate rotates the item around aPosition 90 degrees in the clockwise direction.
const wxString traceFindItem
Flag to enable find item tracing using the WXTRACE environment variable.
Class LINE_READER is an abstract class from which implementation specific LINE_READERs may be derived...
Definition: richio.h:81
void Draw(EDA_DRAW_PANEL *aPanel, wxDC *aDc, const wxPoint &aOffset, int aMulti, int aConvert, const PART_DRAW_OPTIONS &aOpts)
Draw part.
Part library alias object definition.
bool IsMoving() const
Definition: base_struct.h:218
EDA_ITEM * m_Parent
Linked list: Link (parent struct)
Definition: base_struct.h:168
EDA_TEXT_HJUSTIFY_T
Definition: eda_text.h:47
void Plot(PLOTTER *aPlotter) override
Function Plot plots the schematic item to aPlotter.
TRANSFORM & GetTransform() const
virtual unsigned LineNumber() const
Function Line Number returns the line number of the last line read from this LINE_READER.
Definition: richio.h:159
void Merge(const EDA_RECT &aRect)
Function Merge modifies the position and size of the rectangle in order to contain aRect...
void SetTransform(const TRANSFORM &aTransform)
PNG memory record (file in memory).
Definition: bitmap_types.h:38
static wxString FROM_UTF8(const char *cstring)
function FROM_UTF8 converts a UTF8 encoded C string to a wxString for all wxWidgets build modes...
Definition: macros.h:53
void UpdateUnit(int aUnit)
change the unit id to aUnit has maening only for multiple parts per package Do not change the modifie...
wxString GetPath(const SCH_SHEET_PATH *sheet) const
const wxPoint & GetTextPos() const
Definition: eda_text.h:224
int GetOrientation()
Function GetOrientation Used to display component orientation (in dialog editor or info) ...
int y2
Definition: transform.h:51
EDA_ITEM * GetCurItem() const
SCH_SHEET_PATH m_SheetPathInclude
Class LIB_TEXT defines a component library graphical text item.
Definition: lib_text.h:45
time_t m_TimeStamp
Time stamp used for logical links.
Definition: base_struct.h:169
bool Contains(const wxPoint &aPoint) const
Function Contains.
SCH_FIELDS m_Fields
Variable length list of fields.
Definition: sch_component.h:91
const SCH_SHEET_PATH * m_currentSheetPath
A temporary sheet path is required to generate the correct reference designator string in complex hei...
wxPoint GetScreenCoord(const wxPoint &aPoint)
Function GetScreenCoord Returns the coordinated point relative to the orientation of the component of...
void ImportValues(const LIB_FIELD &aSource)
Function ImportValues copy parameters from a LIB_FIELD source.
Definition: sch_field.cpp:221
bool Resolve(PART_LIBS *aLibs)
Function Resolve [re-]assigns the current LIB_PART from aLibs which this component is based on...
static bool sort_by_libid(const SCH_COMPONENT *ref, SCH_COMPONENT *cmp)
const wxString GetLibraryName()
wxArrayString m_PathsAndReferences
Defines the hierarchical path and reference of the component.
#define SKIP_STRUCT
flag indicating that the structure should be ignored
Definition: base_struct.h:124
wxPoint m_Pos
Name and library where symbol was loaded from, i.e. "74xx:74LS00".
Definition: sch_component.h:77
void SetItalic(bool isItalic)
Definition: eda_text.h:169
The first 4 are mandatory, and must be instantiated in SCH_COMPONENT and LIB_PART constructors...
void GetConnectionPoints(std::vector< wxPoint > &aPoints) const override
Function GetConnectionPoints add all the connection points for this item to aPoints.
bool Save(FILE *aFile) const override
Function Save writes the data structures for this object out to a FILE in "*.sch" format...
int GetUnitSelection(SCH_SHEET_PATH *aSheet)
const EDA_RECT GetBodyBoundingBox(int aUnit, int aConvert) const
Function GetBodyBoundingBox.
void GetEndPoints(std::vector< DANGLING_END_ITEM > &aItemList) override
Function GetEndPoints adds the schematic item end points to aItemList if the item has end points...
void Draw(EDA_DRAW_PANEL *aPanel, wxDC *aDC, const wxPoint &aOffset, GR_DRAWMODE aDrawMode, COLOR4D aColor=COLOR4D::UNSPECIFIED) override
Function Draw Draw a schematic item.
Definition: sch_field.cpp:118
void SetTextPos(const wxPoint &aPoint)
Definition: eda_text.h:223
void SetVisible(bool aVisible)
Definition: eda_text.h:175
time_t GetNewTimeStamp()
Definition: common.cpp:166
int x2
Definition: transform.h:50
void MirrorX(int aXaxis_position) override
Function MirrorX mirrors item relative to the X axis about aXaxis_position.
bool IsSelected() const
Definition: base_struct.h:221
bool IsVoid() const
Function IsVoid returns true if the field is either empty or holds "~".
Definition: sch_field.h:116
bool Save(FILE *aFile) const override
Function Save writes the data structures for this object out to a FILE in "*.sch" format...
Definition: sch_field.cpp:314
SCH_FIELD * FindField(const wxString &aFieldName)
Function FindField searches for SCH_FIELD with aFieldName and returns it if found, else NULL.
LIB_PIN * GetPin(const wxString &number)
Function GetPin finds a component pin by number.
int x1
Definition: transform.h:48
static const wxString GetDefaultFieldName(int aFieldNdx)
Function GetDefaultFieldName returns a default symbol field name for field aFieldNdx for all componen...
void SetRef(const SCH_SHEET_PATH *sheet, const wxString &ref)
Set the reference, for the given sheet path.
SCH_SHEET_PATH m_SheetPath
LIB_ITEM * GetDrawItem(const wxPoint &aPosition, KICAD_T aType=TYPE_NOT_INIT)
Function GetDrawItem().
ELECTRICAL_PINTYPE m_ElectricalPinType
void SetUnitSelection(SCH_SHEET_PATH *aSheet, int aUnitSelection)
void SwapData(SCH_ITEM *aItem) override
Function SwapData swap the internal data structures aItem with the schematic item.
void SetTextSize(const wxSize &aNewSize)
Definition: eda_text.h:214
int GetUnitCount() const
Function GetUnitCount returns the number of parts per package of the component.
std::vector< bool > m_isDangling
One isDangling per pin.
Definition: sch_component.h:95
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:317
EDA_RECT GetBodyBoundingBox() const
Function GetBodyBoundingBox Return a bounding box for the component body but not the fields...
Class LIB_ID.
Definition: lib_id.h:56
NETLIST_ITEM_T m_Type
#define abs(a)
Definition: auxiliary.h:84
#define TEXT_ANGLE_VERT
Definition: common.h:92
Field Name Module PCB, i.e. "16DIP300".
Field Reference of part, i.e. "IC21".
search types array terminator (End Of Types)
Definition: typeinfo.h:94
KICAD_T
Enum KICAD_T is the set of class identification values, stored in EDA_ITEM::m_StructType.
Definition: typeinfo.h:90
Definition: colors.h:54
void SetTimeStamp(time_t aNewTimeStamp)
Definition: base_struct.h:203
Class LIB_ITEM is the base class for drawable items used by schematic library components.
Definition: lib_draw_item.h:77
void GetFields(std::vector< SCH_FIELD * > &aVector, bool aVisibleOnly)
Function GetFields populates a std::vector with SCH_FIELDs.
const EDA_RECT GetBoundingBox() const override
Function GetBoundingBox returns the orthogonal, bounding box of this object for display purposes...
Class NETLIST_OBJECT_LIST is a container holding and owning NETLIST_OBJECTs, which are connected item...
wxString Path() const
Function Path the path uses the time stamps which do not changes even when editing sheet parameters a...
void SetConvert(int aConvert)
void AddDrawItem(LIB_ITEM *aItem)
Add a new draw aItem to the draw object list.
virtual wxString GetShownText() const
Returns the string actually shown after processing of the base text.
Definition: eda_text.h:135
bool IsSelectStateChanged(const wxRect &aRect) override
Function IsSelectStateChanged checks if the selection state of an item inside aRect has changed...
BITMAP_DEF GetMenuImage() const override
Function GetMenuImage returns a pointer to an image to be used in menus.
#define IS_NEW
New item, just created.
Definition: base_struct.h:113
wxPoint TransformCoordinate(const wxPoint &aPoint) const
Calculate a new coordinate according to the mirror/rotation transform.
Definition: transform.cpp:41
const INSPECTOR_FUNC & INSPECTOR
Definition: base_struct.h:104
Class SCH_COLLECTOR.
#define TO_UTF8(wxstring)
Macro TO_UTF8 converts a wxString to a UTF8 encoded C string for all wxWidgets build modes...
Definition: macros.h:47
BOX2I boundingBox(T aObject)
boundingBox template method
Definition: shape_index.h:59
int GetConvert() const
int y1
Definition: transform.h:49
void MIRROR(T &aPoint, const T &aMirrorRef)
Definition: macros.h:111
#define SELECTED
Definition: base_struct.h:120
void SetLibId(const LIB_ID &aName, PART_LIBS *aLibs=NULL)
void GRRect(EDA_RECT *aClipBox, wxDC *aDC, int x1, int y1, int x2, int y2, COLOR4D aColor)
Definition: gr_basic.cpp:1077
void SetTimeStamp(time_t aNewTimeStamp)
Function SetTimeStamp changes the time stamp to aNewTimeStamp updates the reference path...
double GetArea() const
Function GetArea returns the area of the rectangle.
Class for tranforming drawing coordinates for a wxDC device context.
Definition: transform.h:45
PART_REF m_part
points into the PROJECT's libraries to the LIB_PART for this component
Definition: sch_component.h:93
LIB_FIELD & GetReferenceField()
Return reference to the reference designator field.
void SetFlags(STATUS_FLAGS aMask)
Definition: base_struct.h:253
SCH_FIELD * GetField(int aFieldNdx) const
Function GetField returns a field.
void SetHeight(int val)
void SetParent(EDA_ITEM *aParent)
Definition: base_struct.h:213
static void ResolveAll(const SCH_COLLECTOR &aComponents, PART_LIBS *aLibs)
GR_DRAWMODE
Drawmode. Compositing mode plus a flag or two.
Definition: gr_basic.h:41
bool IsMouseCaptured() const
const wxString & GetName() const
const UTF8 & GetLibItemName() const
Function GetLibItemName.
Definition: lib_id.h:129
char * Line() const
Function Line returns a pointer to the last line that was read in.
Definition: richio.h:139
Subclass of DIALOG_SCH_FIND_BASE, which is generated by wxFormBuilder.
const wxString & GetText() const
Function GetText returns the string associated with the text object.
Definition: eda_text.h:130
wxPoint GetPinPhysicalPosition(LIB_PIN *Pin)
void SetVertJustify(EDA_TEXT_VJUSTIFY_T aType)
Definition: eda_text.h:194
std::shared_ptr< LIB_PART > PART_SPTR
shared pointer to LIB_PART
void SetLayer(SCH_LAYER_ID aLayer)
Function SetLayer sets the layer this item is on.
bool operator<(const SCH_ITEM &aItem) const override
EDA_ITEM * Clone() const override
Function Clone creates a duplicate of this item with linked list members set to NULL.
wxString GetDescription() const
SCH_COMPONENT(const wxPoint &pos=wxPoint(0, 0), SCH_ITEM *aParent=NULL)
PART_SPTR SharedPtr()
std::string toUTFTildaText(const wxString &txt)
Function toUTFTildaText convert a wxString to UTF8 and replace any control characters with a ~...
void Empty()
Definition: eda_text.h:231
Class LIB_PART defines a library part object.
SCH_FIELD * AddField(const SCH_FIELD &aField)
Function AddField adds a field to the component.
bool IsDangling() const override
Return whether any pin has dangling status.
#define STRUCT_DELETED
flag indication structures to be erased
Definition: base_struct.h:122
EDA_RECT * GetClipBox()
void GetNetListItem(NETLIST_OBJECT_LIST &aNetListItems, SCH_SHEET_PATH *aSheetPath) override
Function GetNetListItem creates a new NETLIST_OBJECT for the schematic object and adds it to aNetList...
void SetX(int val)
Common plot library Plot settings, and plotting engines (Postscript, Gerber, HPGL and DXF) ...
LIB_PART * FindLibPart(const LIB_ID &aLibId, const wxString &aLibraryName=wxEmptyString)
Function FindLibPart searches all libraries in the list for a part.
wxString GetText(GRAPHIC_PINSHAPE shape)
Definition: pin_shape.cpp:33
void SetUnit(int aUnit)
change the unit id to aUnit has maening only for multiple parts per package Also set the modified fla...
bool Matches(wxFindReplaceData &aSearchData, void *aAuxData, wxPoint *aFindLocation) override
Function Matches compares the item against the search criteria in aSearchData.
int GetBottom() const
int GetUnit() const
Class SCH_SHEET_PATH.
bool IsInNetlist() const
static PART_DRAW_OPTIONS Default()
void clear()
Function clear.
Definition: lib_id.cpp:114
void ClearAnnotation(SCH_SHEET_PATH *aSheetPath)
Function ClearAnnotation clears exiting component annotation ( i.i IC23 changed to IC...
int GetRight() const
void SetWidth(int val)
EDA_ITEM & operator=(const EDA_ITEM &aItem)
Operator assignment is used to assign the members of aItem to another object.
bool Load(LINE_READER &aLine, wxString &aErrorMsg) override
Function Load reads a schematic item from aLine in a .sch file.
int SetLibItemName(const UTF8 &aLibItemName, bool aTestForRev=true)
Function SetLibItemName.
Definition: lib_id.cpp:232
void SetEndPosition(const wxPoint &aPosition)
Definition: lib_rectangle.h:62
void SetY(int val)
bool operator==(const SCH_COMPONENT &aComponent) const
LIB_PART * GetPart() const
Function GetPart gets the shared LIB_PART.
SCH_ITEM & operator=(const SCH_ITEM &aItem)
EDA_TEXT_VJUSTIFY_T
Definition: eda_text.h:54
wxPoint GetPosition() const override
Function GetPosition returns the current draw object position.
Definition: lib_pin.h:471
void Move(const wxPoint &aPosition) override
Function Move moves a draw object to aPosition.
Class PART_LIBS is a collection of PART_LIBs.
Definition of the NETLIST_OBJECT class.
int m_unit
The unit for multiple part per package components.
Definition: sch_component.h:82
Field Value of part, i.e. "3.3K".
void Init(const wxPoint &pos=wxPoint(0, 0))
bool IsDanglingStateChanged(std::vector< DANGLING_END_ITEM > &aItemList) override
Test if the component's dangling state has changed for all pins.
Base plotter engine class.
Definition: plot_common.h:86
const wxString GetRef(const SCH_SHEET_PATH *sheet)
Function GetRef returns the reference, for the given sheet path.
bool Intersects(const EDA_RECT &aRect) const
Function Intersects tests for a common area between rectangles.
Definition the SCH_COMPONENT class for Eeschema.
#define NULL_STRING
std::vector< LIB_PIN * > LIB_PINS
Helper for defining a list of pin object pointers.
Definition: lib_draw_item.h:70
void GetPins(std::vector< LIB_PIN * > &aPinsList)
Function GetPins populate a vector with all the pins.
virtual char * ReadLine()=0
Function ReadLine reads a line of text into the buffer and increments the line number counter...
static const wxChar * GetChars(const wxString &s)
Function GetChars returns a wxChar* to the actual wxChar* data within a wxString, and is helpful for ...
Definition: macros.h:92
see class PGM_BASE
const wxString & GetName() const
int GetFieldCount() const
Function GetFieldCount returns the number of fields in this component.
const EDA_RECT GetBoundingBox() const override
Function GetBoundingBox returns the orthogonal, bounding box of this object for display purposes...
Definition: sch_field.cpp:251
static LIB_PART * dummy()
Used when a LIB_PART is not found in library to draw a dummy shape This component is a 400 mils squar...
static double square(int x)
Definition: trigo.cpp:138
void SetHorizJustify(EDA_TEXT_HJUSTIFY_T aType)
Definition: eda_text.h:193
void AddHierarchicalReference(const wxString &aPath, const wxString &aRef, int aMulti)
Function AddHierarchicalReference adds a full hierarchical reference (path + local reference) ...
bool IsVisible() const
Definition: eda_text.h:176
wxString GetSelectMenuText() const override
Function GetSelectMenuText returns the text to display to be used in the selection clarification cont...
void MirrorY(int aYaxis_position) override
Function MirrorY mirrors item relative to the Y axis about aYaxis_position.
bool IsPinDanglingStateChanged(std::vector< DANGLING_END_ITEM > &aItemList, LIB_PINS &aLibPins, unsigned aPin)
Test if the component's dangling state has changed for one given pin index.
void Draw(EDA_DRAW_PANEL *aPanel, wxDC *aDC, const wxPoint &aOffset, GR_DRAWMODE aDrawMode, COLOR4D aColor=COLOR4D::UNSPECIFIED) override
Virtual function, from the base class SCH_ITEM::Draw.
#define ROTATE_VALUES_COUNT
void SetText(const wxString &aText) override
Sets the text item string to aText.
Definition: lib_text.cpp:488
std::vector< LIB_FIELD > LIB_FIELDS
Definition: lib_field.h:261
STATUS_FLAGS m_Flags
Flag bits for editing and other uses.
Definition: base_struct.h:175
Class EDA_RECT handles the component boundary box.
void SetName(const wxString &aName)
Definition: sch_field.h:85
Class SCH_COMPONENT describes a real schematic component.
Definition: sch_component.h:69
int GetX() const
const LIB_ID & GetLibId() const
BASE_SCREEN * GetScreen()
Definition: draw_panel.cpp:188
Class EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boa...
Definition: base_struct.h:151
std::vector< MSG_PANEL_ITEM > MSG_PANEL_ITEMS
Definition: msgpanel.h:102
SEARCH_RESULT Visit(INSPECTOR inspector, void *testData, const KICAD_T scanTypes[]) override
Function Visit may be re-implemented for each derived class in order to handle all the types given by...
UTF8 Format() const
Function Format.
Definition: lib_id.cpp:263
int GetY() const
virtual wxString GetClass() const override
Function GetClass returns the class name.
void ClearFlags(STATUS_FLAGS aMask=EDA_ITEM_ALL_FLAGS)
Definition: base_struct.h:254
bool IsPowerConnection() const
Return whether this pin forms an implicit power connection: i.e., is hidden and of type POWER_IN...
Definition: lib_pin.h:378
bool operator!=(const SCH_COMPONENT &aComponent) const
int m_convert
The alternate body style for components that have more than one body style defined.
Definition: sch_component.h:83
Class DANGLING_END_ITEM is a helper class used to store the state of schematic items that can be conn...
Definition for part library class.
Class EDA_MSG_ITEM is used EDA_MSG_PANEL as the item type for displaying messages.
Definition: msgpanel.h:53
void SetId(int aId)
Definition: sch_field.h:89
int ReadDelimitedText(wxString *aDest, const char *aSource)
Function ReadDelimitedText copies bytes from aSource delimited string segment to aDest wxString...
Definition: string.cpp:43
SEARCH_RESULT
Definition: base_struct.h:68
static bool IsReferenceStringValid(const wxString &aReferenceString)
Function IsReferenceStringValid (static) Tests for an acceptable reference string An acceptable refer...
Definition of the SCH_SHEET_PATH and SCH_SHEET_LIST classes for Eeschema.
const UTF8 & GetLibNickname() const
Function GetLibNickname.
Definition: lib_id.h:108
wxString GetClass() const override
Function GetClass returns the class name.
Message panel definition file.
AUTOPLACED m_fieldsAutoplaced
indicates status of field autoplacement
Definition: sch_component.h:97
wxString m_prefix
C, R, U, Q etc - the first character which typically indicates what the component is...
Definition: sch_component.h:86
void SetBold(bool aBold)
Definition: eda_text.h:172
wxString GetKeyWords() const
TRANSFORM m_transform
The rotation/mirror transformation matrix.
Definition: sch_component.h:90
Class SCH_ITEM is a base class for any item which can be embedded within the SCHEMATIC container clas...
static unsigned long timeStamp(CPTREE &aTree)
Make a unique time stamp.
bool doIsConnected(const wxPoint &aPosition) const override
Function doIsConnected provides the object specific test to see if it is connected to aPosition...
EDA_RECT & Inflate(wxCoord dx, wxCoord dy)
Function Inflate inflates the rectangle horizontally by dx and vertically by dy.
void GetFields(LIB_FIELDS &aList)
Function GetFields returns a list of fields withing this part.
int GetDefaultTextSize()
Default size for text in general.
virtual void SetText(const wxString &aText)
Definition: eda_text.h:141
void SetOrientation(int aOrientation)
Function SetOrientation computes the new transform matrix based on aOrientation for the component whi...
Class COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:39
#define IS_MOVED
Item being moved.
Definition: base_struct.h:112
Definition: colors.h:62