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 #include <eeschema_id.h> // for MAX_UNIT_COUNT_PER_PACKAGE definition
59 
60 #define NULL_STRING "_NONAME_"
61 
67 std::string toUTFTildaText( const wxString& txt )
68 {
69  std::string ret = TO_UTF8( txt );
70 
71  for( std::string::iterator it = ret.begin(); it!=ret.end(); ++it )
72  {
73  if( (unsigned char) *it <= ' ' )
74  *it = '~';
75  }
76  return ret;
77 }
78 
79 
93 static LIB_PART* dummy()
94 {
95  static LIB_PART* part;
96 
97  if( !part )
98  {
99  part = new LIB_PART( wxEmptyString );
100 
101  LIB_RECTANGLE* square = new LIB_RECTANGLE( part );
102 
103  square->Move( wxPoint( -200, 200 ) );
104  square->SetEndPosition( wxPoint( 200, -200 ) );
105 
106  LIB_TEXT* text = new LIB_TEXT( part );
107 
108  text->SetTextSize( wxSize( 150, 150 ) );
109  text->SetText( wxString( wxT( "??" ) ) );
110 
111  part->AddDrawItem( square );
112  part->AddDrawItem( text );
113  }
114 
115  return part;
116 }
117 
118 
120  SCH_ITEM( aParent, SCH_COMPONENT_T )
121 {
122  Init( aPos );
123  m_currentSheetPath = NULL;
125 }
126 
127 
129  int convert, const wxPoint& pos, bool setNewItemFlag ) :
130  SCH_ITEM( NULL, SCH_COMPONENT_T )
131 {
132  Init( pos );
133 
134  m_unit = unit;
135  m_convert = convert;
136  m_lib_id.SetLibItemName( TO_UTF8( aPart.GetName() ), false );
137  m_part = aPart.SharedPtr();
138  m_currentSheetPath = NULL;
140 
142 
143  if( setNewItemFlag )
144  m_Flags = IS_NEW | IS_MOVED;
145 
146  // Import user defined fields from the library component:
147  LIB_FIELDS libFields;
148 
149  aPart.GetFields( libFields );
150 
151  for( LIB_FIELDS::iterator it = libFields.begin(); it!=libFields.end(); ++it )
152  {
153  // Can no longer insert an empty name, since names are now keys. The
154  // field index is not used beyond the first MANDATORY_FIELDS
155  if( it->GetName().IsEmpty() )
156  continue;
157 
158  // See if field already exists (mandatory fields always exist).
159  // for mandatory fields, the name and field id are fixed, so we use the
160  // known and fixed id to get them (more reliable than names, which can be translated)
161  // for other fields (custom fields), locate the field by same name
162  // (field id has no known meaning for custom fields)
163  int idx = it->GetId();
164  SCH_FIELD* schField;
165 
166  if( idx < MANDATORY_FIELDS )
167  schField = GetField( idx );
168  else
169  schField = FindField( it->GetName() );
170 
171  if( !schField )
172  {
173  SCH_FIELD fld( wxPoint( 0, 0 ), GetFieldCount(), this, it->GetName() );
174  schField = AddField( fld );
175  }
176 
177  schField->ImportValues( *it );
178  schField->SetText( it->GetText() );
179 
180  // Now the field is initialized, place it to the right position:
181  schField->SetTextPos( m_Pos + it->GetTextPos() );
182  }
183 
184  wxString msg = aPart.GetReferenceField().GetText();
185 
186  if( msg.IsEmpty() )
187  msg = wxT( "U" );
188 
189  m_prefix = msg;
190 
191  // update the reference -- just the prefix for now.
192  msg += wxT( "?" );
193  SetRef( sheet, msg );
194 
195  // Use the schematic component name instead of the library value field name.
196  GetField( VALUE )->SetText( FROM_UTF8( GetLibId().GetLibItemName() ) );
197 }
198 
199 
201  SCH_ITEM( aComponent )
202 {
203  m_currentSheetPath = NULL;
204  m_Parent = aComponent.m_Parent;
205  m_Pos = aComponent.m_Pos;
206  m_unit = aComponent.m_unit;
207  m_convert = aComponent.m_convert;
208  m_lib_id = aComponent.m_lib_id;
209  m_part = aComponent.m_part;
210 
211  SetTimeStamp( aComponent.m_TimeStamp );
212 
213  m_transform = aComponent.m_transform;
214  m_prefix = aComponent.m_prefix;
216  m_Fields = aComponent.m_Fields;
217 
218  // Re-parent the fields, which before this had aComponent as parent
219  for( int i = 0; i<GetFieldCount(); ++i )
220  {
221  GetField( i )->SetParent( this );
222  }
223 
224  m_isDangling = aComponent.m_isDangling;
226 }
227 
228 
229 void SCH_COMPONENT::Init( const wxPoint& pos )
230 {
231  m_Pos = pos;
232  m_unit = 0; // In multi unit chip - which unit to draw.
233  m_convert = 0; // De Morgan Handling
234 
235  // The rotation/mirror transformation matrix. pos normal
237 
238  // construct only the mandatory fields, which are the first 4 only.
239  for( int i = 0; i < MANDATORY_FIELDS; ++i )
240  {
241  SCH_FIELD field( pos, i, this, TEMPLATE_FIELDNAME::GetDefaultFieldName( i ) );
242 
243  if( i == REFERENCE )
244  field.SetLayer( LAYER_REFERENCEPART );
245  else if( i == VALUE )
246  field.SetLayer( LAYER_VALUEPART );
247 
248  // else keep LAYER_FIELDS from SCH_FIELD constructor
249 
250  // SCH_FIELD's implicitly created copy constructor is called in here
251  AddField( field );
252  }
253 
254  m_prefix = wxString( wxT( "U" ) );
255 }
256 
257 
259 {
260  return new SCH_COMPONENT( *this );
261 }
262 
263 
264 void SCH_COMPONENT::SetLibId( const LIB_ID& aLibId, PART_LIBS* aLibs )
265 {
266  if( m_lib_id != aLibId )
267  {
268  m_lib_id = aLibId;
269  SetModified();
270 
271  if( aLibs )
272  Resolve( aLibs );
273  else
274  m_part.reset();
275  }
276 }
277 
278 
279 void SCH_COMPONENT::SetLibId( const LIB_ID& aLibId, SYMBOL_LIB_TABLE* aSymLibTable )
280 {
281  if( m_lib_id != aLibId )
282  {
283  wxCHECK_RET( aSymLibTable, "No symbol library table provided." );
284 
285  m_lib_id = aLibId;
286  SetModified();
287 
288  LIB_ALIAS* alias = aSymLibTable->LoadSymbol( m_lib_id.GetLibNickname(),
290 
291  if( alias && alias->GetPart() )
292  m_part = alias->GetPart()->SharedPtr();
293  else
294  m_part.reset();
295  }
296 }
297 
298 
300 {
301  if( PART_SPTR part = m_part.lock() )
302  {
303  LIB_ALIAS* alias = part->GetAlias( GetLibId().GetLibItemName() );
304 
305  if( !alias )
306  return wxEmptyString;
307 
308  return alias->GetDescription();
309  }
310 
311  return wxEmptyString;
312 }
313 
314 
316 {
317  if( PART_SPTR part = m_part.lock() )
318  {
319  LIB_ALIAS* alias = part->GetAlias( GetLibId().GetLibItemName() );
320 
321  if( !alias )
322  return wxEmptyString;
323 
324  return alias->GetDocFileName();
325  }
326 
327  return wxEmptyString;
328 }
329 
330 
332 {
333  // I've never been happy that the actual individual PART_LIB is left up to
334  // flimsy search path ordering. None-the-less find a part based on that design:
335  if( LIB_PART* part = aLibs->FindLibPart( m_lib_id ) )
336  {
337  m_part = part->SharedPtr();
338  return true;
339  }
340 
341  return false;
342 }
343 
344 
346 {
347  LIB_ALIAS* alias = aLibTable.LoadSymbol( m_lib_id );
348 
349  if( alias && alias->GetPart() )
350  {
351  m_part = alias->GetPart()->SharedPtr();
352  return true;
353  }
354 
355  return false;
356 }
357 
358 
359 // Helper sort function, used in SCH_COMPONENT::ResolveAll, to sort
360 // sch component by lib_id
361 static bool sort_by_libid( const SCH_COMPONENT* ref, SCH_COMPONENT* cmp )
362 {
363  return ref->GetLibId() < cmp->GetLibId();
364 }
365 
366 
367 void SCH_COMPONENT::ResolveAll( const SCH_COLLECTOR& aComponents, PART_LIBS* aLibs )
368 {
369  // Usually, many components use the same part lib.
370  // to avoid too long calculation time the list of components is grouped
371  // and once the lib part is found for one member of a group, it is also
372  // set for all other members of this group
373  std::vector<SCH_COMPONENT*> cmp_list;
374 
375  // build the cmp list.
376  for( int i = 0; i < aComponents.GetCount(); ++i )
377  {
378  SCH_COMPONENT* cmp = dynamic_cast<SCH_COMPONENT*>( aComponents[i] );
379  wxASSERT( cmp );
380 
381  if( cmp ) // cmp == NULL should not occur.
382  cmp_list.push_back( cmp );
383  }
384 
385  // sort it by lib part. Cmp will be grouped by same lib part.
386  std::sort( cmp_list.begin(), cmp_list.end(), sort_by_libid );
387 
388  LIB_ID curr_libid;
389 
390  for( unsigned ii = 0; ii < cmp_list.size (); ++ii )
391  {
392  SCH_COMPONENT* cmp = cmp_list[ii];
393  curr_libid = cmp->m_lib_id;
394  cmp->Resolve( aLibs );
395 
396  // Propagate the m_part pointer to other members using the same lib_id
397  for( unsigned jj = ii+1; jj < cmp_list.size (); ++jj )
398  {
399  SCH_COMPONENT* next_cmp = cmp_list[jj];
400 
401  if( curr_libid != next_cmp->m_lib_id )
402  break;
403 
404  next_cmp->m_part = cmp->m_part;
405  ii = jj;
406  }
407  }
408 }
409 
410 
411 void SCH_COMPONENT::ResolveAll( const SCH_COLLECTOR& aComponents, SYMBOL_LIB_TABLE& aLibTable )
412 {
413  std::vector<SCH_COMPONENT*> cmp_list;
414 
415  for( int i = 0; i < aComponents.GetCount(); ++i )
416  {
417  SCH_COMPONENT* cmp = dynamic_cast<SCH_COMPONENT*>( aComponents[i] );
418 
419  wxCHECK2_MSG( cmp, continue, "Invalid SCH_COMPONENT pointer in list." );
420 
421  cmp_list.push_back( cmp );
422  }
423 
424  // sort it by lib part. Cmp will be grouped by same lib part.
425  std::sort( cmp_list.begin(), cmp_list.end(), sort_by_libid );
426 
427  LIB_ID curr_libid;
428 
429  for( unsigned ii = 0; ii < cmp_list.size (); ++ii )
430  {
431  SCH_COMPONENT* cmp = cmp_list[ii];
432  cmp->Resolve( aLibTable );
433 
434  // Propagate the m_part pointer to other members using the same lib_id
435  for( unsigned jj = ii+1; jj < cmp_list.size (); ++jj )
436  {
437  SCH_COMPONENT* next_cmp = cmp_list[jj];
438 
439  if( curr_libid != next_cmp->m_lib_id )
440  break;
441 
442  next_cmp->m_part = cmp->m_part;
443  ii = jj;
444  }
445  }
446 }
447 
448 
449 void SCH_COMPONENT::SetUnit( int aUnit )
450 {
451  if( m_unit != aUnit )
452  {
453  m_unit = aUnit;
454  SetModified();
455  }
456 }
457 
458 
459 void SCH_COMPONENT::UpdateUnit( int aUnit )
460 {
461  m_unit = aUnit;
462 }
463 
464 
465 void SCH_COMPONENT::SetConvert( int aConvert )
466 {
467  if( m_convert != aConvert )
468  {
469  m_convert = aConvert;
470  SetModified();
471  }
472 }
473 
474 
475 void SCH_COMPONENT::SetTransform( const TRANSFORM& aTransform )
476 {
477  if( m_transform != aTransform )
478  {
479  m_transform = aTransform;
480  SetModified();
481  }
482 }
483 
484 
486 {
487  if( PART_SPTR part = m_part.lock() )
488  {
489  return part->GetUnitCount();
490  }
491 
492  return 0;
493 }
494 
495 
496 void SCH_COMPONENT::Draw( EDA_DRAW_PANEL* aPanel, wxDC* aDC, const wxPoint& aOffset,
497  GR_DRAWMODE aDrawMode, COLOR4D aColor,
498  bool aDrawPinText )
499 {
500  auto opts = PART_DRAW_OPTIONS::Default();
501  opts.draw_mode = aDrawMode;
502  opts.color = aColor;
503  opts.transform = m_transform;
504  opts.show_pin_text = aDrawPinText;
505  opts.draw_visible_fields = false;
506  opts.draw_hidden_fields = false;
507 
508  if( PART_SPTR part = m_part.lock() )
509  {
510  // Draw pin targets if part is being dragged
511  bool dragging = aPanel->GetScreen()->GetCurItem() == this && aPanel->IsMouseCaptured();
512 
513  if( !dragging )
514  {
515  opts.dangling = m_isDangling;
516  }
517 
518  part->Draw( aPanel, aDC, m_Pos + aOffset, m_unit, m_convert, opts );
519  }
520  else // Use dummy() part if the actual cannot be found.
521  {
522  dummy()->Draw( aPanel, aDC, m_Pos + aOffset, 0, 0, opts );
523  }
524 
525  SCH_FIELD* field = GetField( REFERENCE );
526 
527  if( field->IsVisible() && !field->IsMoving() )
528  {
529  field->Draw( aPanel, aDC, aOffset, aDrawMode );
530  }
531 
532  for( int ii = VALUE; ii < GetFieldCount(); ii++ )
533  {
534  field = GetField( ii );
535 
536  if( field->IsMoving() )
537  continue;
538 
539  field->Draw( aPanel, aDC, aOffset, aDrawMode );
540  }
541 
542 #if 0
543  // Only for testing purposes, draw the component bounding box
544  {
546  GRRect( aPanel->GetClipBox(), aDC, boundingBox, 0, BROWN );
547 #if 1
548  if( GetField( REFERENCE )->IsVisible() )
549  {
550  boundingBox = GetField( REFERENCE )->GetBoundingBox();
551  GRRect( aPanel->GetClipBox(), aDC, boundingBox, 0, BROWN );
552  }
553 
554  if( GetField( VALUE )->IsVisible() )
555  {
556  boundingBox = GetField( VALUE )->GetBoundingBox();
557  GRRect( aPanel->GetClipBox(), aDC, boundingBox, 0, BROWN );
558  }
559 #endif
560  }
561 #endif
562 }
563 
564 
565 void SCH_COMPONENT::AddHierarchicalReference( const wxString& aPath,
566  const wxString& aRef,
567  int aMulti )
568 {
569  wxString h_path, h_ref;
570  wxStringTokenizer tokenizer;
571  wxString separators( wxT( " " ) );
572 
573  // Search for an existing path and remove it if found (should not occur)
574  for( unsigned ii = 0; ii < m_PathsAndReferences.GetCount(); ii++ )
575  {
576  tokenizer.SetString( m_PathsAndReferences[ii], separators );
577  h_path = tokenizer.GetNextToken();
578 
579  if( h_path.Cmp( aPath ) == 0 )
580  {
581  m_PathsAndReferences.RemoveAt( ii );
582  ii--;
583  }
584  }
585 
586  h_ref = aPath + wxT( " " ) + aRef;
587  h_ref << wxT( " " ) << aMulti;
588  m_PathsAndReferences.Add( h_ref );
589 }
590 
591 
592 wxString SCH_COMPONENT::GetPath( const SCH_SHEET_PATH* sheet ) const
593 {
594  wxCHECK_MSG( sheet != NULL, wxEmptyString,
595  wxT( "Cannot get component path with invalid sheet object." ) );
596 
597  wxString str;
598 
599  str.Printf( wxT( "%8.8lX" ), (long unsigned) m_TimeStamp );
600  return sheet->Path() + str;
601 }
602 
603 
604 const wxString SCH_COMPONENT::GetRef( const SCH_SHEET_PATH* sheet )
605 {
606  wxString path = GetPath( sheet );
607  wxString h_path, h_ref;
608  wxStringTokenizer tokenizer;
609  wxString separators( wxT( " " ) );
610 
611  for( unsigned ii = 0; ii < m_PathsAndReferences.GetCount(); ii++ )
612  {
613  tokenizer.SetString( m_PathsAndReferences[ii], separators );
614  h_path = tokenizer.GetNextToken();
615 
616  if( h_path.Cmp( path ) == 0 )
617  {
618  h_ref = tokenizer.GetNextToken();
619 
620  /* printf( "GetRef hpath: %s\n",
621  * TO_UTF8( m_PathsAndReferences[ii] ) ); */
622  return h_ref;
623  }
624  }
625 
626  // if it was not found in m_Paths array, then see if it is in
627  // m_Field[REFERENCE] -- if so, use this as a default for this path.
628  // this will happen if we load a version 1 schematic file.
629  // it will also mean that multiple instances of the same sheet by default
630  // all have the same component references, but perhaps this is best.
631  if( !GetField( REFERENCE )->GetText().IsEmpty() )
632  {
633  SetRef( sheet, GetField( REFERENCE )->GetText() );
634  return GetField( REFERENCE )->GetText();
635  }
636 
637  return m_prefix;
638 }
639 
640 
641 bool SCH_COMPONENT::IsReferenceStringValid( const wxString& aReferenceString )
642 {
643  wxString text = aReferenceString;
644  bool ok = true;
645 
646  // Try to unannotate this reference
647  while( !text.IsEmpty() && ( text.Last() == '?' || isdigit( text.Last() ) ) )
648  text.RemoveLast();
649 
650  if( text.IsEmpty() )
651  ok = false;
652 
653  // Add here other constraints
654  // Currently:no other constraint
655 
656  return ok;
657 }
658 
659 
660 void SCH_COMPONENT::SetRef( const SCH_SHEET_PATH* sheet, const wxString& ref )
661 {
662  wxString path = GetPath( sheet );
663 
664  bool notInArray = true;
665 
666  wxString h_path, h_ref;
667  wxStringTokenizer tokenizer;
668  wxString separators( wxT( " " ) );
669 
670  // check to see if it is already there before inserting it
671  for( unsigned ii = 0; ii < m_PathsAndReferences.GetCount(); ii++ )
672  {
673  tokenizer.SetString( m_PathsAndReferences[ii], separators );
674  h_path = tokenizer.GetNextToken();
675 
676  if( h_path.Cmp( path ) == 0 )
677  {
678  // just update the reference text, not the timestamp.
679  h_ref = h_path + wxT( " " ) + ref;
680  h_ref += wxT( " " );
681  tokenizer.GetNextToken(); // Skip old reference
682  h_ref += tokenizer.GetNextToken(); // Add part selection
683 
684  // Add the part selection
685  m_PathsAndReferences[ii] = h_ref;
686  notInArray = false;
687  }
688  }
689 
690  if( notInArray )
691  AddHierarchicalReference( path, ref, m_unit );
692 
693  SCH_FIELD* rf = GetField( REFERENCE );
694 
695  if( rf->GetText().IsEmpty()
696  || ( abs( rf->GetTextPos().x - m_Pos.x ) +
697  abs( rf->GetTextPos().y - m_Pos.y ) > 10000 ) )
698  {
699  // move it to a reasonable position
700  rf->SetTextPos( m_Pos + wxPoint( 50, 50 ) );
701  }
702 
703  rf->SetText( ref ); // for drawing.
704 
705  // Reinit the m_prefix member if needed
706  wxString prefix = ref;
707 
708  if( IsReferenceStringValid( prefix ) )
709  {
710  while( prefix.Last() == '?' || isdigit( prefix.Last() ) )
711  prefix.RemoveLast();
712  }
713  else
714  {
715  prefix = wxT( "U" ); // Set to default ref prefix
716  }
717 
718  if( m_prefix != prefix )
719  m_prefix = prefix;
720 }
721 
722 
723 void SCH_COMPONENT::SetTimeStamp( time_t aNewTimeStamp )
724 {
725  wxString string_timestamp, string_oldtimestamp;
726 
727  string_timestamp.Printf( wxT( "%08lX" ), (long unsigned) aNewTimeStamp );
728  string_oldtimestamp.Printf( wxT( "%08lX" ), (long unsigned) m_TimeStamp );
729  EDA_ITEM::SetTimeStamp( aNewTimeStamp );
730 
731  for( unsigned ii = 0; ii < m_PathsAndReferences.GetCount(); ii++ )
732  {
733  m_PathsAndReferences[ii].Replace( string_oldtimestamp.GetData(),
734  string_timestamp.GetData() );
735  }
736 }
737 
738 
740 {
741  wxString path = GetPath( aSheet );
742  wxString h_path, h_multi;
743  wxStringTokenizer tokenizer;
744  wxString separators( wxT( " " ) );
745 
746  for( unsigned ii = 0; ii < m_PathsAndReferences.GetCount(); ii++ )
747  {
748  tokenizer.SetString( m_PathsAndReferences[ii], separators );
749  h_path = tokenizer.GetNextToken();
750 
751  if( h_path.Cmp( path ) == 0 )
752  {
753  tokenizer.GetNextToken(); // Skip reference
754  h_multi = tokenizer.GetNextToken();
755  long imulti = 1;
756  h_multi.ToLong( &imulti );
757  return imulti;
758  }
759  }
760 
761  // if it was not found in m_Paths array, then use m_unit.
762  // this will happen if we load a version 1 schematic file.
763  return m_unit;
764 }
765 
766 
767 void SCH_COMPONENT::SetUnitSelection( SCH_SHEET_PATH* aSheet, int aUnitSelection )
768 {
769  wxString path = GetPath( aSheet );
770 
771  bool notInArray = true;
772 
773  wxString h_path, h_ref;
774  wxStringTokenizer tokenizer;
775  wxString separators( wxT( " " ) );
776 
777  //check to see if it is already there before inserting it
778  for( unsigned ii = 0; ii < m_PathsAndReferences.GetCount(); ii++ )
779  {
780  tokenizer.SetString( m_PathsAndReferences[ii], separators );
781  h_path = tokenizer.GetNextToken();
782 
783  if( h_path.Cmp( path ) == 0 )
784  {
785  //just update the unit selection.
786  h_ref = h_path + wxT( " " );
787  h_ref += tokenizer.GetNextToken(); // Add reference
788  h_ref += wxT( " " );
789  h_ref << aUnitSelection; // Add part selection
790 
791  // Ann the part selection
792  m_PathsAndReferences[ii] = h_ref;
793  notInArray = false;
794  }
795  }
796 
797  if( notInArray )
798  AddHierarchicalReference( path, m_prefix, aUnitSelection );
799 }
800 
801 
802 SCH_FIELD* SCH_COMPONENT::GetField( int aFieldNdx ) const
803 {
804  const SCH_FIELD* field;
805 
806  if( (unsigned) aFieldNdx < m_Fields.size() )
807  field = &m_Fields[aFieldNdx];
808  else
809  field = NULL;
810 
811  wxASSERT( field );
812 
813  // use cast to remove const-ness
814  return (SCH_FIELD*) field;
815 }
816 
817 
818 wxString SCH_COMPONENT::GetFieldText( wxString aFieldName, bool aIncludeDefaultFields ) const
819 {
820  // Field name for comparison
821  wxString cmpFieldName;
822 
823  if( aIncludeDefaultFields )
824  {
825 
826  // Default field names
827  for ( unsigned int i=0; i<MANDATORY_FIELDS; i++)
828  {
829  cmpFieldName = TEMPLATE_FIELDNAME::GetDefaultFieldName( i );
830 
831  if( cmpFieldName.Cmp( aFieldName ) == 0 )
832  {
833  return m_Fields[i].GetText();
834  }
835  }
836  }
837 
838  // Search custom fields
839  for( unsigned int ii=MANDATORY_FIELDS; ii<m_Fields.size(); ii++ )
840  {
841  cmpFieldName = m_Fields[ii].GetName();
842 
843  if( cmpFieldName.Cmp( aFieldName ) == 0 )
844  {
845  return m_Fields[ii].GetText();
846  }
847  }
848 
849  return wxEmptyString;
850 }
851 
852 
853 void SCH_COMPONENT::GetFields( std::vector<SCH_FIELD*>& aVector, bool aVisibleOnly )
854 {
855  for( SCH_FIELD& each_field : m_Fields )
856  {
857  if( !aVisibleOnly || ( each_field.IsVisible() && !each_field.IsVoid() ) )
858  aVector.push_back( &each_field );
859  }
860 }
861 
862 
864 {
865  int newNdx = m_Fields.size();
866 
867  m_Fields.push_back( aField );
868  return &m_Fields[newNdx];
869 }
870 
871 
872 SCH_FIELD* SCH_COMPONENT::FindField( const wxString& aFieldName, bool aIncludeDefaultFields )
873 {
874  unsigned start = aIncludeDefaultFields ? 0 : MANDATORY_FIELDS;
875 
876  for( unsigned i = start; i<m_Fields.size(); ++i )
877  {
878  if( aFieldName == m_Fields[i].GetName( false ) )
879  {
880  return &m_Fields[i];
881  }
882  }
883 
884  return NULL;
885 }
886 
887 
888 LIB_PIN* SCH_COMPONENT::GetPin( const wxString& number )
889 {
890  if( PART_SPTR part = m_part.lock() )
891  {
892  return part->GetPin( number, m_unit, m_convert );
893  }
894  return NULL;
895 }
896 
897 
898 void SCH_COMPONENT::GetPins( std::vector<LIB_PIN*>& aPinsList )
899 {
900  if( PART_SPTR part = m_part.lock() )
901  {
902  part->GetPins( aPinsList, m_unit, m_convert );
903  }
904  else
905  wxFAIL_MSG( "Could not obtain PART_SPTR lock" );
906 }
907 
908 
910 {
911  wxCHECK_RET( (aItem != NULL) && (aItem->Type() == SCH_COMPONENT_T),
912  wxT( "Cannot swap data with invalid component." ) );
913 
914  SCH_COMPONENT* component = (SCH_COMPONENT*) aItem;
915 
916  std::swap( m_lib_id, component->m_lib_id );
917  std::swap( m_part, component->m_part );
918  std::swap( m_Pos, component->m_Pos );
919  std::swap( m_unit, component->m_unit );
920  std::swap( m_convert, component->m_convert );
921 
922  TRANSFORM tmp = m_transform;
923 
924  m_transform = component->m_transform;
925  component->m_transform = tmp;
926 
927  m_Fields.swap( component->m_Fields ); // std::vector's swap()
928 
929  // Reparent items after copying data
930  // (after swap(), m_Parent member does not point to the right parent):
931  for( int ii = 0; ii < component->GetFieldCount(); ++ii )
932  {
933  component->GetField( ii )->SetParent( component );
934  }
935 
936  for( int ii = 0; ii < GetFieldCount(); ++ii )
937  {
938  GetField( ii )->SetParent( this );
939  }
940 
941  std::swap( m_PathsAndReferences, component->m_PathsAndReferences );
942 }
943 
944 
946 {
947  bool keepMulti = false;
948  wxArrayString reference_fields;
949 
950  static const wxChar separators[] = wxT( " " );
951 
952  PART_SPTR part = m_part.lock();
953 
954  if( part && part->UnitsLocked() )
955  keepMulti = true;
956 
957  // Build a reference with no annotation,
958  // i.e. a reference ended by only one '?'
959  wxString defRef = m_prefix;
960 
961  if( IsReferenceStringValid( defRef ) )
962  {
963  while( defRef.Last() == '?' )
964  defRef.RemoveLast();
965  }
966  else
967  { // This is a malformed reference: reinit this reference
968  m_prefix = defRef = wxT("U"); // Set to default ref prefix
969  }
970 
971  defRef.Append( wxT( "?" ) );
972 
973  wxString multi = wxT( "1" );
974 
975  // For components with units locked,
976  // we cannot remove all annotations: part selection must be kept
977  // For all components: if aSheetPath is not NULL,
978  // remove annotation only for the given path
979  if( keepMulti || aSheetPath )
980  {
981  wxString NewHref;
982  wxString path;
983 
984  if( aSheetPath )
985  path = GetPath( aSheetPath );
986 
987  for( unsigned int ii = 0; ii < m_PathsAndReferences.GetCount(); ii++ )
988  {
989  // Break hierarchical reference in path, ref and multi selection:
990  reference_fields = wxStringTokenize( m_PathsAndReferences[ii], separators );
991 
992  if( aSheetPath == NULL || reference_fields[0].Cmp( path ) == 0 )
993  {
994  if( keepMulti ) // Get and keep part selection
995  multi = reference_fields[2];
996 
997  NewHref = reference_fields[0];
998  NewHref << wxT( " " ) << defRef << wxT( " " ) << multi;
999  m_PathsAndReferences[ii] = NewHref;
1000  }
1001  }
1002  }
1003  else
1004  {
1005  // Clear reference strings, but does not free memory because a new annotation
1006  // will reuse it
1007  m_PathsAndReferences.Empty();
1008  m_unit = 1;
1009  }
1010 
1011  // These 2 changes do not work in complex hierarchy.
1012  // When a clear annotation is made, the calling function must call a
1013  // UpdateAllScreenReferences for the active sheet.
1014  // But this call cannot made here.
1015  m_Fields[REFERENCE].SetText( defRef ); //for drawing.
1016 
1017  SetModified();
1018 }
1019 
1020 
1021 void SCH_COMPONENT::SetOrientation( int aOrientation )
1022 {
1023  TRANSFORM temp = TRANSFORM();
1024  bool transform = false;
1025 
1026  switch( aOrientation )
1027  {
1028  case CMP_ORIENT_0:
1029  case CMP_NORMAL: // default transform matrix
1030  m_transform.x1 = 1;
1031  m_transform.y2 = -1;
1032  m_transform.x2 = m_transform.y1 = 0;
1033  break;
1034 
1035  case CMP_ROTATE_COUNTERCLOCKWISE: // Rotate + (incremental rotation)
1036  temp.x1 = temp.y2 = 0;
1037  temp.y1 = 1;
1038  temp.x2 = -1;
1039  transform = true;
1040  break;
1041 
1042  case CMP_ROTATE_CLOCKWISE: // Rotate - (incremental rotation)
1043  temp.x1 = temp.y2 = 0;
1044  temp.y1 = -1;
1045  temp.x2 = 1;
1046  transform = true;
1047  break;
1048 
1049  case CMP_MIRROR_Y: // Mirror Y (incremental rotation)
1050  temp.x1 = -1;
1051  temp.y2 = 1;
1052  temp.y1 = temp.x2 = 0;
1053  transform = true;
1054  break;
1055 
1056  case CMP_MIRROR_X: // Mirror X (incremental rotation)
1057  temp.x1 = 1;
1058  temp.y2 = -1;
1059  temp.y1 = temp.x2 = 0;
1060  transform = true;
1061  break;
1062 
1063  case CMP_ORIENT_90:
1066  break;
1067 
1068  case CMP_ORIENT_180:
1072  break;
1073 
1074  case CMP_ORIENT_270:
1077  break;
1078 
1079  case ( CMP_ORIENT_0 + CMP_MIRROR_X ):
1082  break;
1083 
1084  case ( CMP_ORIENT_0 + CMP_MIRROR_Y ):
1087  break;
1088 
1089  case ( CMP_ORIENT_90 + CMP_MIRROR_X ):
1092  break;
1093 
1094  case ( CMP_ORIENT_90 + CMP_MIRROR_Y ):
1097  break;
1098 
1099  case ( CMP_ORIENT_180 + CMP_MIRROR_X ):
1102  break;
1103 
1104  case ( CMP_ORIENT_180 + CMP_MIRROR_Y ):
1107  break;
1108 
1109  case ( CMP_ORIENT_270 + CMP_MIRROR_X ):
1112  break;
1113 
1114  case ( CMP_ORIENT_270 + CMP_MIRROR_Y ):
1117  break;
1118 
1119  default:
1120  transform = false;
1121  wxMessageBox( wxT( "SetRotateMiroir() error: ill value" ) );
1122  break;
1123  }
1124 
1125  if( transform )
1126  {
1127  /* The new matrix transform is the old matrix transform modified by the
1128  * requested transformation, which is the temp transform (rot,
1129  * mirror ..) in order to have (in term of matrix transform):
1130  * transform coord = new_m_transform * coord
1131  * where transform coord is the coord modified by new_m_transform from
1132  * the initial value coord.
1133  * new_m_transform is computed (from old_m_transform and temp) to
1134  * have:
1135  * transform coord = old_m_transform * temp
1136  */
1137  TRANSFORM newTransform;
1138 
1139  newTransform.x1 = m_transform.x1 * temp.x1 + m_transform.x2 * temp.y1;
1140  newTransform.y1 = m_transform.y1 * temp.x1 + m_transform.y2 * temp.y1;
1141  newTransform.x2 = m_transform.x1 * temp.x2 + m_transform.x2 * temp.y2;
1142  newTransform.y2 = m_transform.y1 * temp.x2 + m_transform.y2 * temp.y2;
1143  m_transform = newTransform;
1144  }
1145 }
1146 
1147 
1149 {
1150  int type_rotate = CMP_ORIENT_0;
1151  TRANSFORM transform;
1152  int ii;
1153 
1154  #define ROTATE_VALUES_COUNT 12
1155 
1156  // list of all possibilities, but only the first 8 are actually used
1157  int rotate_value[ROTATE_VALUES_COUNT] =
1158  {
1164  CMP_MIRROR_Y + CMP_ORIENT_180, CMP_MIRROR_Y + CMP_ORIENT_270
1165  };
1166 
1167  // Try to find the current transform option:
1168  transform = m_transform;
1169 
1170  for( ii = 0; ii < ROTATE_VALUES_COUNT; ii++ )
1171  {
1172  type_rotate = rotate_value[ii];
1173  SetOrientation( type_rotate );
1174 
1175  if( transform == m_transform )
1176  return type_rotate;
1177  }
1178 
1179  // Error: orientation not found in list (should not happen)
1180  wxMessageBox( wxT( "Component orientation matrix internal error" ) );
1181  m_transform = transform;
1182 
1183  return CMP_NORMAL;
1184 }
1185 
1186 
1188 {
1189  return m_transform.TransformCoordinate( aPoint );
1190 }
1191 
1192 
1193 #if defined(DEBUG)
1194 
1195 void SCH_COMPONENT::Show( int nestLevel, std::ostream& os ) const
1196 {
1197  // for now, make it look like XML:
1198  NestedSpace( nestLevel, os ) << '<' << GetClass().Lower().mb_str()
1199  << " ref=\"" << TO_UTF8( GetField( 0 )->GetName() )
1200  << '"' << " chipName=\""
1201  << GetLibId().Format() << '"' << m_Pos
1202  << " layer=\"" << m_Layer
1203  << '"' << ">\n";
1204 
1205  // skip the reference, it's been output already.
1206  for( int i = 1; i < GetFieldCount(); ++i )
1207  {
1208  wxString value = GetField( i )->GetText();
1209 
1210  if( !value.IsEmpty() )
1211  {
1212  NestedSpace( nestLevel + 1, os ) << "<field" << " name=\""
1213  << TO_UTF8( GetField( i )->GetName() )
1214  << '"' << " value=\""
1215  << TO_UTF8( value ) << "\"/>\n";
1216  }
1217  }
1218 
1219  NestedSpace( nestLevel, os ) << "</" << TO_UTF8( GetClass().Lower() ) << ">\n";
1220 }
1221 
1222 #endif
1223 
1224 
1225 bool SCH_COMPONENT::Save( FILE* f ) const
1226 {
1227  std::string name1;
1228  std::string name2;
1229  wxArrayString reference_fields;
1230 
1231  static wxString delimiters( wxT( " " ) );
1232 
1233  // this is redundant with the AR entries below, but it makes the
1234  // files backwards-compatible.
1235  if( m_PathsAndReferences.GetCount() > 0 )
1236  {
1237  reference_fields = wxStringTokenize( m_PathsAndReferences[0], delimiters );
1238 
1239  name1 = toUTFTildaText( reference_fields[1] );
1240  }
1241  else
1242  {
1243  if( GetField( REFERENCE )->GetText().IsEmpty() )
1244  name1 = toUTFTildaText( m_prefix );
1245  else
1246  name1 = toUTFTildaText( GetField( REFERENCE )->GetText() );
1247  }
1248 
1249  wxString part_name = FROM_UTF8( GetLibId().GetLibItemName() );
1250 
1251  if( part_name.size() )
1252  {
1253  name2 = toUTFTildaText( part_name );
1254  }
1255  else
1256  {
1257  name2 = NULL_STRING;
1258  }
1259 
1260  if( fprintf( f, "$Comp\n" ) == EOF )
1261  return false;
1262 
1263  if( fprintf( f, "L %s %s\n", name2.c_str(), name1.c_str() ) == EOF )
1264  return false;
1265 
1266  // Generate unit number, convert and time stamp
1267  if( fprintf( f, "U %d %d %8.8lX\n", m_unit, m_convert, (unsigned long)m_TimeStamp ) == EOF )
1268  return false;
1269 
1270  // Save the position
1271  if( fprintf( f, "P %d %d\n", m_Pos.x, m_Pos.y ) == EOF )
1272  return false;
1273 
1274  /* If this is a complex hierarchy; save hierarchical references.
1275  * but for simple hierarchies it is not necessary.
1276  * the reference inf is already saved
1277  * this is useful for old Eeschema version compatibility
1278  */
1279  if( m_PathsAndReferences.GetCount() > 1 )
1280  {
1281  for( unsigned int ii = 0; ii < m_PathsAndReferences.GetCount(); ii++ )
1282  {
1283  /*format:
1284  * AR Path="/140/2" Ref="C99" Part="1"
1285  * where 140 is the uid of the containing sheet
1286  * and 2 is the timestamp of this component.
1287  * (timestamps are actually 8 hex chars)
1288  * Ref is the conventional component reference for this 'path'
1289  * Part is the conventional component part selection for this 'path'
1290  */
1291  reference_fields = wxStringTokenize( m_PathsAndReferences[ii], delimiters );
1292 
1293  if( fprintf( f, "AR Path=\"%s\" Ref=\"%s\" Part=\"%s\" \n",
1294  TO_UTF8( reference_fields[0] ),
1295  TO_UTF8( reference_fields[1] ),
1296  TO_UTF8( reference_fields[2] ) ) == EOF )
1297  return false;
1298  }
1299  }
1300 
1301  // update the ugly field index, which I would like to see go away someday soon.
1302  for( unsigned i = 0; i<m_Fields.size(); ++i )
1303  {
1304  SCH_FIELD* fld = GetField( i );
1305  fld->SetId( i ); // we don't need field Ids, please be gone.
1306  }
1307 
1308  // Fixed fields:
1309  // Save mandatory fields even if they are blank,
1310  // because the visibility, size and orientation are set from libary editor.
1311  for( unsigned i = 0; i<MANDATORY_FIELDS; ++i )
1312  {
1313  SCH_FIELD* fld = GetField( i );
1314  if( !fld->Save( f ) )
1315  return false;
1316  }
1317 
1318  // User defined fields:
1319  // The *policy* about which user defined fields are part of a symbol is now
1320  // only in the dialog editors. No policy should be enforced here, simply
1321  // save all the user defined fields, they are present because a dialog editor
1322  // thought they should be. If you disagree, go fix the dialog editors.
1323  for( unsigned i = MANDATORY_FIELDS; i<m_Fields.size(); ++i )
1324  {
1325  SCH_FIELD* fld = GetField( i );
1326 
1327  if( !fld->Save( f ) )
1328  return false;
1329  }
1330 
1331  // Unit number, position, box ( old standard )
1332  if( fprintf( f, "\t%-4d %-4d %-4d\n", m_unit, m_Pos.x, m_Pos.y ) == EOF )
1333  return false;
1334 
1335  if( fprintf( f, "\t%-4d %-4d %-4d %-4d\n",
1337  return false;
1338 
1339  if( fprintf( f, "$EndComp\n" ) == EOF )
1340  return false;
1341 
1342  return true;
1343 }
1344 
1345 
1346 bool SCH_COMPONENT::Load( LINE_READER& aLine, wxString& aErrorMsg )
1347 {
1348  // Remark: avoid using sscanf to read texts entered by user
1349  // which are UTF8 encoded, because sscanf does not work well on Windows
1350  // with some UTF8 values.
1351  char name1[256], char1[256], char2[256], char3[256];
1352  int newfmt = 0;
1353  char* ptcar;
1354  wxString fieldName;
1355  char* line = aLine.Line();
1356  unsigned long timeStamp;
1357 
1358  m_convert = 1;
1359 
1360  if( line[0] == '$' )
1361  {
1362  newfmt = 1;
1363 
1364  if( !(line = aLine.ReadLine()) )
1365  return true;
1366  }
1367 
1368  // Parse the first line of description:
1369  // like "L partname ref" (for instance "L 74LS00 U4"
1370  // They are UTF8 texts, so do not use sscanf
1371 
1372  line += 1;
1373 
1374  if( *line == ' ' )
1375  line++;
1376 
1377  // line points the first parameter
1378  wxString buffer( FROM_UTF8( line ) );
1379  wxStringTokenizer tokenizer( buffer, wxT( " \r\n" ) );
1380 
1381  if( tokenizer.CountTokens() < 2 )
1382  {
1383  aErrorMsg.Printf( wxT( "Eeschema component description error at line %d, aborted" ),
1384  aLine.LineNumber() );
1385  aErrorMsg << wxT( "\n" ) << FROM_UTF8( line );
1386  return false;
1387  }
1388 
1389  wxString partname = tokenizer.NextToken();
1390  partname.Replace( wxT("~"), wxT(" ") ); // all spaces were replaced by ~ in files.
1391 
1392  if( partname != NULL_STRING )
1393  {
1394  m_lib_id.SetLibItemName( TO_UTF8( partname ), false );
1395 
1396  if( !newfmt )
1397  GetField( VALUE )->SetText( partname );
1398  }
1399  else
1400  {
1401  m_lib_id.clear();
1402  GetField( VALUE )->Empty();
1404  GetField( VALUE )->SetVisible( false );
1405  }
1406 
1407  wxString reference = tokenizer.NextToken();
1408  reference.Replace( wxT("~"), wxT(" ") ); // all spaces were replaced by ~ in files.
1409  reference.Trim( true );
1410  reference.Trim( false );
1411 
1412  if( reference != NULL_STRING )
1413  {
1414  wxString prefix = reference;
1415  // Build reference prefix from the actual reference by removing trailing digits
1416  // (Perhaps outdated code, only for very old schematic files)
1417  while( prefix.Length() )
1418  {
1419  if( ( prefix.Last() < '0' || prefix.Last() > '9') && prefix.Last() != '?' )
1420  break;
1421 
1422  prefix.RemoveLast();
1423  }
1424 
1425  // Avoid a prefix containing trailing/leading spaces
1426  prefix.Trim( true );
1427  prefix.Trim( false );
1428 
1429  if( prefix.IsEmpty() )
1430  m_prefix = wxT( "U" );
1431  else
1432  m_prefix = prefix;
1433 
1434  if( !newfmt )
1435  GetField( REFERENCE )->SetText( reference );
1436  }
1437  else
1438  {
1439  GetField( REFERENCE )->SetVisible( false );
1440  }
1441 
1442  /* Parse component description
1443  * These lines begin with:
1444  * "P" = Position
1445  * U = Num Unit and Conversion
1446  * "Fn" = Fields (0 .. n = = number of field)
1447  * "Ar" = Alternate reference in the case of multiple sheets referring to
1448  * one schematic file.
1449  */
1450  for( ; ; )
1451  {
1452  if( !(line = aLine.ReadLine()) )
1453  return false;
1454 
1455  if( line[0] == 'U' )
1456  {
1457  sscanf( line + 1, "%d %d %lX", &m_unit, &m_convert, &timeStamp );
1458  m_TimeStamp = (time_t)timeStamp;
1459  }
1460  else if( line[0] == 'P' )
1461  {
1462  sscanf( line + 1, "%d %d", &m_Pos.x, &m_Pos.y );
1463 
1464  // Set fields position to a default position (that is the
1465  // component position. For existing fields, the real position
1466  // will be set later
1467  for( int i = 0; i<GetFieldCount(); i++ )
1468  {
1469  if( GetField( i )->GetText().IsEmpty() )
1470  GetField( i )->SetTextPos( m_Pos );
1471  }
1472  }
1473  else if( line[0] == 'A' && line[1] == 'R' )
1474  {
1475  /* format:
1476  * AR Path="/9086AF6E/67452AA0" Ref="C99" Part="1"
1477  * where 9086AF6E is the unique timestamp of the containing sheet
1478  * and 67452AA0 is the timestamp of this component.
1479  * C99 is the reference given this path.
1480  */
1481  int ii;
1482  ptcar = line + 2;
1483 
1484  //copy the path.
1485  ii = ReadDelimitedText( name1, ptcar, 255 );
1486  ptcar += ii + 1;
1487  wxString path = FROM_UTF8( name1 );
1488 
1489  // copy the reference
1490  ii = ReadDelimitedText( name1, ptcar, 255 );
1491  ptcar += ii + 1;
1492  wxString ref = FROM_UTF8( name1 );
1493 
1494  // copy the multi, if exists
1495  ii = ReadDelimitedText( name1, ptcar, 255 );
1496 
1497  if( name1[0] == 0 ) // Nothing read, put a default value
1498  sprintf( name1, "%d", m_unit );
1499 
1500  int multi = atoi( name1 );
1501 
1502  // Avoid out of range multi id:
1503  if( multi < 0 || multi > MAX_UNIT_COUNT_PER_PACKAGE )
1504  multi = 1;
1505 
1506  AddHierarchicalReference( path, ref, multi );
1507  GetField( REFERENCE )->SetText( ref );
1508  }
1509  else if( line[0] == 'F' )
1510  {
1511  int fieldNdx;
1512 
1513  wxString fieldText;
1516 
1517  ptcar = (char*) aLine;
1518 
1519  while( *ptcar && (*ptcar != '"') )
1520  ptcar++;
1521 
1522  if( *ptcar != '"' )
1523  {
1524  aErrorMsg.Printf( wxT( "Eeschema file library field F at line %d, aborted" ),
1525  aLine.LineNumber() );
1526  return false;
1527  }
1528 
1529  ptcar += ReadDelimitedText( &fieldText, ptcar );
1530 
1531  if( *ptcar == 0 )
1532  {
1533  aErrorMsg.Printf( wxT( "Component field F at line %d, aborted" ),
1534  aLine.LineNumber() );
1535  return false;
1536  }
1537 
1538  fieldNdx = atoi( line + 2 );
1539 
1540  ReadDelimitedText( &fieldName, ptcar );
1541 
1542  if( fieldName.IsEmpty() )
1543  fieldName = TEMPLATE_FIELDNAME::GetDefaultFieldName( fieldNdx );
1544 
1545  if( fieldNdx >= GetFieldCount() )
1546  {
1547  // The first MANDATOR_FIELDS _must_ be constructed within
1548  // the SCH_COMPONENT constructor. This assert is simply here
1549  // to guard against a change in that constructor.
1550  wxASSERT( GetFieldCount() >= MANDATORY_FIELDS );
1551 
1552  // Ignore the _supplied_ fieldNdx. It is not important anymore
1553  // if within the user defined fields region (i.e. >= MANDATORY_FIELDS).
1554  // We freely renumber the index to fit the next available field slot.
1555 
1556  fieldNdx = GetFieldCount(); // new has this index after insertion
1557 
1558  SCH_FIELD field( wxPoint( 0, 0 ),
1559  -1, // field id is not relavant for user defined fields
1560  this, fieldName );
1561 
1562  AddField( field );
1563  }
1564  else
1565  {
1566  GetField( fieldNdx )->SetName( fieldName );
1567  }
1568 
1569  GetField( fieldNdx )->SetText( fieldText );
1570  memset( char3, 0, sizeof(char3) );
1571  int ii, x, y, w, attr;
1572 
1573  if( ( ii = sscanf( ptcar, "%255s %d %d %d %X %255s %255s", char1, &x, &y, &w, &attr,
1574  char2, char3 ) ) < 4 )
1575  {
1576  aErrorMsg.Printf( wxT( "Component Field error line %d, aborted" ),
1577  aLine.LineNumber() );
1578  continue;
1579  }
1580 
1581  GetField( fieldNdx )->SetTextPos( wxPoint( x, y ) );
1582  GetField( fieldNdx )->SetVisible( !attr );
1583 
1584  if( (w == 0 ) || (ii == 4) )
1585  w = GetDefaultTextSize();
1586 
1587  GetField( fieldNdx )->SetTextSize( wxSize( w, w ) );
1588  GetField( fieldNdx )->SetTextAngle( TEXT_ANGLE_HORIZ );
1589 
1590  if( char1[0] == 'V' )
1591  GetField( fieldNdx )->SetTextAngle( TEXT_ANGLE_VERT );
1592 
1593  if( ii >= 7 )
1594  {
1595  if( *char2 == 'L' )
1596  hjustify = GR_TEXT_HJUSTIFY_LEFT;
1597  else if( *char2 == 'R' )
1598  hjustify = GR_TEXT_HJUSTIFY_RIGHT;
1599 
1600  if( char3[0] == 'B' )
1601  vjustify = GR_TEXT_VJUSTIFY_BOTTOM;
1602  else if( char3[0] == 'T' )
1603  vjustify = GR_TEXT_VJUSTIFY_TOP;
1604 
1605  GetField( fieldNdx )->SetItalic( char3[1] == 'I' );
1606  GetField( fieldNdx )->SetBold( char3[2] == 'B' );
1607  GetField( fieldNdx )->SetHorizJustify( hjustify );
1608  GetField( fieldNdx )->SetVertJustify( vjustify );
1609  }
1610 
1611  if( fieldNdx == REFERENCE )
1612  if( GetField( fieldNdx )->GetText()[0] == '#' )
1613  GetField( fieldNdx )->SetVisible( false );
1614  }
1615  else
1616  {
1617  break;
1618  }
1619  }
1620 
1621  if( sscanf( line, "%d %d %d", &m_unit, &m_Pos.x, &m_Pos.y ) != 3 )
1622  {
1623  aErrorMsg.Printf( wxT( "Component unit & pos error at line %d, aborted" ),
1624  aLine.LineNumber() );
1625  return false;
1626  }
1627 
1628  if( !(line = aLine.ReadLine()) ||
1629  sscanf( line, "%d %d %d %d",
1630  &m_transform.x1,
1631  &m_transform.y1,
1632  &m_transform.x2,
1633  &m_transform.y2 ) != 4 )
1634  {
1635  aErrorMsg.Printf( wxT( "Component orient error at line %d, aborted" ),
1636  aLine.LineNumber() );
1637  return false;
1638  }
1639 
1640  if( newfmt )
1641  {
1642  if( !(line = aLine.ReadLine()) )
1643  return false;
1644 
1645  if( strncasecmp( "$End", line, 4 ) != 0 )
1646  {
1647  aErrorMsg.Printf( wxT( "Component End expected at line %d, aborted" ),
1648  aLine.LineNumber() );
1649  return false;
1650  }
1651  }
1652 
1653  // ensure flags (mainly used in edit) are cleared.
1654  // some changes have set the modified flag
1655  m_Flags = 0;
1656 
1657  return true;
1658 }
1659 
1660 
1662 {
1663  EDA_RECT bBox;
1664 
1665  if( PART_SPTR part = m_part.lock() )
1666  {
1667  bBox = part->GetBodyBoundingBox( m_unit, m_convert );
1668  }
1669  else
1670  {
1671  bBox = dummy()->GetBodyBoundingBox( m_unit, m_convert );
1672  }
1673 
1674  int x0 = bBox.GetX();
1675  int xm = bBox.GetRight();
1676 
1677  // We must reverse Y values, because matrix orientation
1678  // suppose Y axis normal for the library items coordinates,
1679  // m_transform reverse Y values, but bBox is already reversed!
1680  int y0 = -bBox.GetY();
1681  int ym = -bBox.GetBottom();
1682 
1683  // Compute the real Boundary box (rotated, mirrored ...)
1684  int x1 = m_transform.x1 * x0 + m_transform.y1 * y0;
1685  int y1 = m_transform.x2 * x0 + m_transform.y2 * y0;
1686  int x2 = m_transform.x1 * xm + m_transform.y1 * ym;
1687  int y2 = m_transform.x2 * xm + m_transform.y2 * ym;
1688 
1689  // H and W must be > 0:
1690  if( x2 < x1 )
1691  std::swap( x2, x1 );
1692 
1693  if( y2 < y1 )
1694  std::swap( y2, y1 );
1695 
1696  bBox.SetX( x1 );
1697  bBox.SetY( y1 );
1698  bBox.SetWidth( x2 - x1 );
1699  bBox.SetHeight( y2 - y1 );
1700 
1701  bBox.Offset( m_Pos );
1702  return bBox;
1703 }
1704 
1705 
1707 {
1708  EDA_RECT bbox = GetBodyBoundingBox();
1709 
1710  for( size_t i = 0; i < m_Fields.size(); i++ )
1711  {
1712  bbox.Merge( m_Fields[i].GetBoundingBox() );
1713  }
1714 
1715  return bbox;
1716 }
1717 
1718 
1720 {
1721  // part and alias can differ if alias is not the root
1722  if( PART_SPTR part = m_part.lock() )
1723  {
1724  LIB_ALIAS* alias = part->GetAlias( FROM_UTF8( GetLibId().GetLibItemName() ) );
1725 
1726  if( !alias )
1727  return;
1728 
1729  if( m_currentSheetPath )
1730  aList.push_back( MSG_PANEL_ITEM( _( "Reference" ),
1732  DARKCYAN ) );
1733 
1734  wxString msg = part->IsPower() ? _( "Power symbol" ) : _( "Value" );
1735 
1736  aList.push_back( MSG_PANEL_ITEM( msg, GetField( VALUE )->GetShownText(), DARKCYAN ) );
1737 
1738  // Display component reference in library and library
1739  aList.push_back( MSG_PANEL_ITEM( _( "Component" ), FROM_UTF8( GetLibId().GetLibItemName() ),
1740  BROWN ) );
1741 
1742  if( alias->GetName() != part->GetName() )
1743  aList.push_back( MSG_PANEL_ITEM( _( "Alias of" ), part->GetName(), BROWN ) );
1744 
1745  aList.push_back( MSG_PANEL_ITEM( _( "Library" ), alias->GetLibraryName(), BROWN ) );
1746 
1747  // Display the current associated footprint, if exists.
1748  if( !GetField( FOOTPRINT )->IsVoid() )
1749  msg = GetField( FOOTPRINT )->GetShownText();
1750  else
1751  msg = _( "<Unknown>" );
1752 
1753  aList.push_back( MSG_PANEL_ITEM( _( "Footprint" ), msg, DARKRED ) );
1754 
1755  // Display description of the component, and keywords found in lib
1756  aList.push_back( MSG_PANEL_ITEM( _( "Description" ), alias->GetDescription(), DARKCYAN ) );
1757  aList.push_back( MSG_PANEL_ITEM( _( "Key Words" ), alias->GetKeyWords(), DARKCYAN ) );
1758  }
1759 }
1760 
1761 
1763 {
1764  return add_component_xpm;
1765 }
1766 
1767 
1768 void SCH_COMPONENT::MirrorY( int aYaxis_position )
1769 {
1770  int dx = m_Pos.x;
1771 
1773  MIRROR( m_Pos.x, aYaxis_position );
1774  dx -= m_Pos.x; // dx,0 is the move vector for this transform
1775 
1776  for( int ii = 0; ii < GetFieldCount(); ii++ )
1777  {
1778  // Move the fields to the new position because the component itself has moved.
1779  wxPoint pos = GetField( ii )->GetTextPos();
1780  pos.x -= dx;
1781  GetField( ii )->SetTextPos( pos );
1782  }
1783 }
1784 
1785 
1786 void SCH_COMPONENT::MirrorX( int aXaxis_position )
1787 {
1788  int dy = m_Pos.y;
1789 
1791  MIRROR( m_Pos.y, aXaxis_position );
1792  dy -= m_Pos.y; // dy,0 is the move vector for this transform
1793 
1794  for( int ii = 0; ii < GetFieldCount(); ii++ )
1795  {
1796  // Move the fields to the new position because the component itself has moved.
1797  wxPoint pos = GetField( ii )->GetTextPos();
1798  pos.y -= dy;
1799  GetField( ii )->SetTextPos( pos );
1800  }
1801 }
1802 
1803 
1805 {
1806  wxPoint prev = m_Pos;
1807 
1808  RotatePoint( &m_Pos, aPosition, 900 );
1809 
1811 
1812  for( int ii = 0; ii < GetFieldCount(); ii++ )
1813  {
1814  // Move the fields to the new position because the component itself has moved.
1815  wxPoint pos = GetField( ii )->GetTextPos();
1816  pos.x -= prev.x - m_Pos.x;
1817  pos.y -= prev.y - m_Pos.y;
1818  GetField( ii )->SetTextPos( pos );
1819  }
1820 }
1821 
1822 
1823 bool SCH_COMPONENT::Matches( wxFindReplaceData& aSearchData, void* aAuxData,
1824  wxPoint* aFindLocation )
1825 {
1826  wxLogTrace( traceFindItem, wxT( " item " ) + GetSelectMenuText() );
1827 
1828  // Components are searchable via the child field and pin item text.
1829  return false;
1830 }
1831 
1832 
1833 void SCH_COMPONENT::GetEndPoints( std::vector <DANGLING_END_ITEM>& aItemList )
1834 {
1835  if( PART_SPTR part = m_part.lock() )
1836  {
1837  for( LIB_PIN* pin = part->GetNextPin(); pin; pin = part->GetNextPin( pin ) )
1838  {
1839  wxASSERT( pin->Type() == LIB_PIN_T );
1840 
1841  if( pin->GetUnit() && m_unit && ( m_unit != pin->GetUnit() ) )
1842  continue;
1843 
1844  if( pin->GetConvert() && m_convert && ( m_convert != pin->GetConvert() ) )
1845  continue;
1846 
1847  DANGLING_END_ITEM item( PIN_END, pin, GetPinPhysicalPosition( pin ) );
1848  aItemList.push_back( item );
1849  }
1850  }
1851 }
1852 
1853 
1854 bool SCH_COMPONENT::IsPinDanglingStateChanged( std::vector<DANGLING_END_ITEM> &aItemList,
1855  LIB_PINS& aLibPins, unsigned aPin )
1856 {
1857  bool previousState;
1858 
1859  if( aPin < m_isDangling.size() )
1860  {
1861  previousState = m_isDangling[aPin];
1862  m_isDangling[aPin] = true;
1863  }
1864  else
1865  {
1866  previousState = true;
1867  m_isDangling.push_back( true );
1868  }
1869 
1870  wxPoint pin_position = GetPinPhysicalPosition( aLibPins[aPin] );
1871 
1872  for( DANGLING_END_ITEM& each_item : aItemList )
1873  {
1874  // Some people like to stack pins on top of each other in a symbol to indicate
1875  // internal connection. While technically connected, it is not particularly useful
1876  // to display them that way, so skip any pins that are in the same symbol as this
1877  // one.
1878  //
1879  // Do not make this exception for hidden pins, because those actually make internal
1880  // connections to a power net.
1881  const LIB_PIN* item_pin = dynamic_cast<const LIB_PIN*>( each_item.GetItem() );
1882 
1883  if( item_pin
1884  && ( !item_pin->IsPowerConnection() || !IsInNetlist() )
1885  && std::find( aLibPins.begin(), aLibPins.end(), item_pin) != aLibPins.end() )
1886  continue;
1887 
1888  switch( each_item.GetType() )
1889  {
1890  case PIN_END:
1891  case LABEL_END:
1892  case SHEET_LABEL_END:
1893  case WIRE_START_END:
1894  case WIRE_END_END:
1895  case NO_CONNECT_END:
1896  case JUNCTION_END:
1897  if( pin_position == each_item.GetPosition() )
1898  m_isDangling[aPin] = false;
1899  break;
1900  default:
1901  break;
1902  }
1903  if( !m_isDangling[aPin] )
1904  break;
1905  }
1906 
1907  return previousState != m_isDangling[aPin];
1908 }
1909 
1910 
1911 bool SCH_COMPONENT::IsDanglingStateChanged( std::vector<DANGLING_END_ITEM>& aItemList )
1912 {
1913  bool changed = false;
1914  LIB_PINS libPins;
1915  if( PART_SPTR part = m_part.lock() )
1916  part->GetPins( libPins, m_unit, m_convert );
1917  for( size_t i = 0; i < libPins.size(); ++i )
1918  {
1919  if( IsPinDanglingStateChanged( aItemList, libPins, i ) )
1920  changed = true;
1921  }
1922  return changed;
1923 }
1924 
1925 
1927 {
1928  for( bool each : m_isDangling )
1929  {
1930  if( each )
1931  return true;
1932  }
1933  return false;
1934 }
1935 
1936 
1938 {
1939  wxCHECK_MSG( Pin != NULL && Pin->Type() == LIB_PIN_T, wxPoint( 0, 0 ),
1940  wxT( "Cannot get physical position of pin." ) );
1941 
1942  return m_transform.TransformCoordinate( Pin->GetPosition() ) + m_Pos;
1943 }
1944 
1945 
1946 bool SCH_COMPONENT::IsSelectStateChanged( const wxRect& aRect )
1947 {
1948  bool previousState = IsSelected();
1949 
1951 
1952  if( aRect.Intersects( boundingBox ) )
1953  SetFlags( SELECTED );
1954  else
1955  ClearFlags( SELECTED );
1956 
1957  return previousState != IsSelected();
1958 }
1959 
1960 
1961 void SCH_COMPONENT::GetConnectionPoints( std::vector< wxPoint >& aPoints ) const
1962 {
1963  if( PART_SPTR part = m_part.lock() )
1964  {
1965  for( LIB_PIN* pin = part->GetNextPin(); pin; pin = part->GetNextPin( pin ) )
1966  {
1967  wxCHECK_RET( pin->Type() == LIB_PIN_T,
1968  wxT( "GetNextPin() did not return a pin object. Bad programmer!" ) );
1969 
1970  // Skip items not used for this part.
1971  if( m_unit && pin->GetUnit() && ( pin->GetUnit() != m_unit ) )
1972  continue;
1973 
1974  if( m_convert && pin->GetConvert() && ( pin->GetConvert() != m_convert ) )
1975  continue;
1976 
1977  // Calculate the pin position relative to the component position and orientation.
1978  aPoints.push_back( m_transform.TransformCoordinate( pin->GetPosition() ) + m_Pos );
1979  }
1980  }
1981  else
1982  {
1983  wxCHECK_RET( 0,
1984  wxT( "Cannot add connection points to list. Cannot find component <" ) +
1985  FROM_UTF8( GetLibId().GetLibItemName() ) +
1986  wxT( "> in any of the loaded libraries." ) );
1987  }
1988 }
1989 
1990 
1992 {
1993  if( PART_SPTR part = m_part.lock() )
1994  {
1995  // Calculate the position relative to the component.
1996  wxPoint libPosition = aPosition - m_Pos;
1997 
1998  return part->LocateDrawItem( m_unit, m_convert, aType, libPosition, m_transform );
1999  }
2000 
2001  return NULL;
2002 }
2003 
2004 
2006 {
2007  wxString tmp;
2008  tmp.Printf( _( "Component %s, %s" ),
2009  FROM_UTF8( GetLibId().GetLibItemName() ),
2010  GetChars( GetField( REFERENCE )->GetShownText() ) );
2011  return tmp;
2012 }
2013 
2014 
2015 SEARCH_RESULT SCH_COMPONENT::Visit( INSPECTOR aInspector, void* aTestData,
2016  const KICAD_T aFilterTypes[] )
2017 {
2018  KICAD_T stype;
2019 
2020  for( const KICAD_T* p = aFilterTypes; (stype = *p) != EOT; ++p )
2021  {
2022  // If caller wants to inspect component type or and component children types.
2023  if( stype == Type() )
2024  {
2025  if( SEARCH_QUIT == aInspector( this, aTestData ) )
2026  return SEARCH_QUIT;
2027  }
2028 
2029  switch( stype )
2030  {
2031  case SCH_FIELD_T:
2032  // Test the bounding boxes of fields if they are visible and not empty.
2033  for( int ii = 0; ii < GetFieldCount(); ii++ )
2034  {
2035  if( SEARCH_QUIT == aInspector( GetField( ii ), (void*) this ) )
2036  return SEARCH_QUIT;
2037  }
2038  break;
2039 
2041  if( SEARCH_QUIT == aInspector( GetField( REFERENCE ), (void*) this ) )
2042  return SEARCH_QUIT;
2043  break;
2044 
2046  if( SEARCH_QUIT == aInspector( GetField( VALUE ), (void*) this ) )
2047  return SEARCH_QUIT;
2048  break;
2049 
2051  if( SEARCH_QUIT == aInspector( GetField( FOOTPRINT ), (void*) this ) )
2052  return SEARCH_QUIT;
2053  break;
2054 
2055 
2056  case LIB_PIN_T:
2057  if( PART_SPTR part = m_part.lock() )
2058  {
2059  LIB_PINS pins;
2060 
2061  part->GetPins( pins, m_unit, m_convert );
2062 
2063  for( size_t i = 0; i < pins.size(); i++ )
2064  {
2065  if( SEARCH_QUIT == aInspector( pins[ i ], (void*) this ) )
2066  return SEARCH_QUIT;
2067  }
2068  }
2069  break;
2070 
2071  default:
2072  break;
2073  }
2074  }
2075 
2076  return SEARCH_CONTINUE;
2077 }
2078 
2079 
2081  SCH_SHEET_PATH* aSheetPath )
2082 {
2083  if( PART_SPTR part = m_part.lock() )
2084  {
2085  for( LIB_PIN* pin = part->GetNextPin(); pin; pin = part->GetNextPin( pin ) )
2086  {
2087  wxASSERT( pin->Type() == LIB_PIN_T );
2088 
2089  if( pin->GetUnit() && ( pin->GetUnit() != GetUnitSelection( aSheetPath ) ) )
2090  continue;
2091 
2092  if( pin->GetConvert() && ( pin->GetConvert() != GetConvert() ) )
2093  continue;
2094 
2095  wxPoint pos = GetTransform().TransformCoordinate( pin->GetPosition() ) + m_Pos;
2096 
2097  NETLIST_OBJECT* item = new NETLIST_OBJECT();
2098  item->m_SheetPathInclude = *aSheetPath;
2099  item->m_Comp = (SCH_ITEM*) pin;
2100  item->m_SheetPath = *aSheetPath;
2101  item->m_Type = NET_PIN;
2102  item->m_Link = (SCH_ITEM*) this;
2103  item->m_ElectricalPinType = pin->GetType();
2104  item->m_PinNum = pin->GetNumber();
2105  item->m_Label = pin->GetName();
2106  item->m_Start = item->m_End = pos;
2107 
2108  aNetListItems.push_back( item );
2109 
2110  if( pin->IsPowerConnection() )
2111  {
2112  // There is an associated PIN_LABEL.
2113  item = new NETLIST_OBJECT();
2114  item->m_SheetPathInclude = *aSheetPath;
2115  item->m_Comp = NULL;
2116  item->m_SheetPath = *aSheetPath;
2117  item->m_Type = NET_PINLABEL;
2118  item->m_Label = pin->GetName();
2119  item->m_Start = pos;
2120  item->m_End = item->m_Start;
2121 
2122  aNetListItems.push_back( item );
2123  }
2124  }
2125  }
2126 }
2127 
2128 
2129 bool SCH_COMPONENT::operator <( const SCH_ITEM& aItem ) const
2130 {
2131  if( Type() != aItem.Type() )
2132  return Type() < aItem.Type();
2133 
2134  SCH_COMPONENT* component = (SCH_COMPONENT*) &aItem;
2135 
2136  EDA_RECT rect = GetBodyBoundingBox();
2137 
2138  if( rect.GetArea() != component->GetBodyBoundingBox().GetArea() )
2139  return rect.GetArea() < component->GetBodyBoundingBox().GetArea();
2140 
2141  if( m_Pos.x != component->m_Pos.x )
2142  return m_Pos.x < component->m_Pos.x;
2143 
2144  if( m_Pos.y != component->m_Pos.y )
2145  return m_Pos.y < component->m_Pos.y;
2146 
2147  return false;
2148 }
2149 
2150 
2151 bool SCH_COMPONENT::operator==( const SCH_COMPONENT& aComponent ) const
2152 {
2153  if( GetFieldCount() != aComponent.GetFieldCount() )
2154  return false;
2155 
2156  for( int i = VALUE; i < GetFieldCount(); i++ )
2157  {
2158  if( GetField( i )->GetText().Cmp( aComponent.GetField( i )->GetText() ) != 0 )
2159  return false;
2160  }
2161 
2162  return true;
2163 }
2164 
2165 
2166 bool SCH_COMPONENT::operator!=( const SCH_COMPONENT& aComponent ) const
2167 {
2168  return !( *this == aComponent );
2169 }
2170 
2171 
2173 {
2174  wxCHECK_MSG( Type() == aItem.Type(), *this,
2175  wxT( "Cannot assign object type " ) + aItem.GetClass() + wxT( " to type " ) +
2176  GetClass() );
2177 
2178  if( &aItem != this )
2179  {
2180  SCH_ITEM::operator=( aItem );
2181 
2182  SCH_COMPONENT* c = (SCH_COMPONENT*) &aItem;
2183 
2184  m_lib_id = c->m_lib_id;
2185  m_part = c->m_part;
2186  m_Pos = c->m_Pos;
2187  m_unit = c->m_unit;
2188  m_convert = c->m_convert;
2189  m_transform = c->m_transform;
2190 
2192 
2193  m_Fields = c->m_Fields; // std::vector's assignment operator.
2194 
2195  // Reparent fields after assignment to new component.
2196  for( int ii = 0; ii < GetFieldCount(); ++ii )
2197  {
2198  GetField( ii )->SetParent( this );
2199  }
2200  }
2201 
2202  return *this;
2203 }
2204 
2205 
2206 bool SCH_COMPONENT::HitTest( const wxPoint& aPosition, int aAccuracy ) const
2207 {
2208  EDA_RECT bBox = GetBodyBoundingBox();
2209  bBox.Inflate( aAccuracy );
2210 
2211  if( bBox.Contains( aPosition ) )
2212  return true;
2213 
2214  return false;
2215 }
2216 
2217 
2218 bool SCH_COMPONENT::HitTest( const EDA_RECT& aRect, bool aContained, int aAccuracy ) const
2219 {
2221  return false;
2222 
2223  EDA_RECT rect = aRect;
2224 
2225  rect.Inflate( aAccuracy );
2226 
2227  if( aContained )
2228  return rect.Contains( GetBodyBoundingBox() );
2229 
2230  return rect.Intersects( GetBodyBoundingBox() );
2231 }
2232 
2233 
2234 bool SCH_COMPONENT::doIsConnected( const wxPoint& aPosition ) const
2235 {
2236  std::vector< wxPoint > pts;
2237 
2238  GetConnectionPoints( pts );
2239 
2240  for( size_t i = 0; i < pts.size(); i++ )
2241  {
2242  if( pts[i] == aPosition )
2243  return true;
2244  }
2245 
2246  return false;
2247 }
2248 
2249 
2251 {
2252  SCH_FIELD* rf = GetField( REFERENCE );
2253  return ! rf->GetText().StartsWith( wxT( "#" ) );
2254 }
2255 
2256 
2257 void SCH_COMPONENT::Plot( PLOTTER* aPlotter )
2258 {
2259  TRANSFORM temp;
2260 
2261  if( PART_SPTR part = m_part.lock() )
2262  {
2263  temp = GetTransform();
2264 
2265  part->Plot( aPlotter, GetUnit(), GetConvert(), m_Pos, temp );
2266 
2267  for( size_t i = 0; i < m_Fields.size(); i++ )
2268  {
2269  m_Fields[i].Plot( aPlotter );
2270  }
2271  }
2272 }
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)
Load a LIB_ALIAS having aAliasName from the library given by aNickname.
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.
SCH_FIELD * FindField(const wxString &aFieldName, bool aIncludeDefaultFields=true)
Search for a SCH_FIELD with aFieldName.
bool IsMoving() const
Definition: base_struct.h:218
EDA_ITEM * m_Parent
Linked list: Link (parent struct)
Definition: base_struct.h:168
wxString GetAliasDescription() const
Return information about the aliased parts.
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 number to aUnit without setting any internal flags.
wxString GetPath(const SCH_SHEET_PATH *sheet) const
const wxPoint & GetTextPos() const
Definition: eda_text.h:224
int GetOrientation()
Get the display symbol orientation.
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
unsigned long timeStamp(wxXmlNode *aTree)
Make a unique time stamp.
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 hie...
wxPoint GetScreenCoord(const wxPoint &aPoint)
Returns the coordinate points relative to the orientation of the symbol to aPoint.
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)
Assigns the current LIB_PART from aLibs which this symbol 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
LIB_PIN * GetPin(const wxString &number)
Find a symbol 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...
#define MAX_UNIT_COUNT_PER_PACKAGE
The maximum number of units per package.
Definition: eeschema_id.h:48
SCH_SHEET_PATH m_SheetPath
LIB_ITEM * GetDrawItem(const wxPoint &aPosition, KICAD_T aType=TYPE_NOT_INIT)
Return the component library item at aPosition that is part of this component.
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
Return the number of units per package of the symbol.
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
Return a bounding box for the symbol 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".
wxString GetFieldText(wxString aFieldName, bool aIncludeDefaultFields=true) const
Search for a field named aFieldName and returns text associated with this field.
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)
Populates a std::vector with SCH_FIELDs.
wxString GetAliasDocumentation() const
Return the documentation text for the given part alias.
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)
Change the time stamp to aNewTimeStamp and 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
Returns a field in this symbol.
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)
Add a field to the symbol.
bool IsDangling() const override
Return whether any pin in this symbol is dangling.
#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 number to aUnit.
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)
Clear exiting component annotation.
int GetRight() const
void SetWidth(int val)
const wxString GetRef(const SCH_SHEET_PATH *aSheet)
Return the reference for the given sheet path.
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:472
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))
void SetRef(const SCH_SHEET_PATH *aSheet, const wxString &aReference)
Set the reference for the given sheet path for this symbol.
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
wxString GetDocFileName() const
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)
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
Return the number of fields in this symbol.
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)
Add a full hierarchical reference to this symbol.
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 symbol'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
Function Draw Draw a schematic item.
#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:379
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)
Tests for an acceptable reference string.
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...
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)
Compute the new transform matrix based on aOrientation for the symbol which is applied to the current...
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