You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2286 lines
64 KiB

* KIWAY Milestone A): Make major modules into DLL/DSOs. ! The initial testing of this commit should be done using a Debug build so that all the wxASSERT()s are enabled. Also, be sure and keep enabled the USE_KIWAY_DLLs option. The tree won't likely build without it. Turning it off is senseless anyways. If you want stable code, go back to a prior version, the one tagged with "stable". * Relocate all functionality out of the wxApp derivative into more finely targeted purposes: a) DLL/DSO specific b) PROJECT specific c) EXE or process specific d) configuration file specific data e) configuration file manipulations functions. All of this functionality was blended into an extremely large wxApp derivative and that was incompatible with the desire to support multiple concurrently loaded DLL/DSO's ("KIFACE")s and multiple concurrently open projects. An amazing amount of organization come from simply sorting each bit of functionality into the proper box. * Switch to wxConfigBase from wxConfig everywhere except instantiation. * Add classes KIWAY, KIFACE, KIFACE_I, SEARCH_STACK, PGM_BASE, PGM_KICAD, PGM_SINGLE_TOP, * Remove "Return" prefix on many function names. * Remove obvious comments from CMakeLists.txt files, and from else() and endif()s. * Fix building boost for use in a DSO on linux. * Remove some of the assumptions in the CMakeLists.txt files that windows had to be the host platform when building windows binaries. * Reduce the number of wxStrings being constructed at program load time via static construction. * Pass wxConfigBase* to all SaveSettings() and LoadSettings() functions so that these functions are useful even when the wxConfigBase comes from another source, as is the case in the KICAD_MANAGER_FRAME. * Move the setting of the KIPRJMOD environment variable into class PROJECT, so that it can be moved into a project variable soon, and out of FP_LIB_TABLE. * Add the KIWAY_PLAYER which is associated with a particular PROJECT, and all its child wxFrames and wxDialogs now have a Kiway() member function which returns a KIWAY& that that window tree branch is in support of. This is like wxWindows DNA in that child windows get this member with proper value at time of construction. * Anticipate some of the needs for milestones B) and C) and make code adjustments now in an effort to reduce work in those milestones. * No testing has been done for python scripting, since milestone C) has that being largely reworked and re-thought-out.
12 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
18 years ago
18 years ago
  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. /**
  25. * @file sch_component.cpp
  26. * @brief Implementation of the class SCH_COMPONENT.
  27. */
  28. #include <fctsys.h>
  29. #include <pgm_base.h>
  30. #include <class_drawpanel.h>
  31. #include <gr_basic.h>
  32. #include <kicad_string.h>
  33. #include <richio.h>
  34. #include <schframe.h>
  35. #include <plot_common.h>
  36. #include <msgpanel.h>
  37. #include <bitmaps.h>
  38. #include <general.h>
  39. #include <class_library.h>
  40. #include <lib_rectangle.h>
  41. #include <lib_pin.h>
  42. #include <lib_text.h>
  43. #include <sch_component.h>
  44. #include <sch_sheet.h>
  45. #include <sch_sheet_path.h>
  46. #include <class_netlist_object.h>
  47. #include <lib_draw_item.h>
  48. #include <symbol_lib_table.h>
  49. #include <dialogs/dialog_schematic_find.h>
  50. #include <wx/tokenzr.h>
  51. #include <iostream>
  52. #include <eeschema_id.h> // for MAX_UNIT_COUNT_PER_PACKAGE definition
  53. #define NULL_STRING "_NONAME_"
  54. /**
  55. * Function toUTFTildaText
  56. * convert a wxString to UTF8 and replace any control characters with a ~,
  57. * where a control character is one of the first ASCII values up to ' ' 32d.
  58. */
  59. std::string toUTFTildaText( const wxString& txt )
  60. {
  61. std::string ret = TO_UTF8( txt );
  62. for( std::string::iterator it = ret.begin(); it!=ret.end(); ++it )
  63. {
  64. if( (unsigned char) *it <= ' ' )
  65. *it = '~';
  66. }
  67. return ret;
  68. }
  69. /**
  70. * Used when a LIB_PART is not found in library
  71. * to draw a dummy shape
  72. * This component is a 400 mils square with the text ??
  73. * DEF DUMMY U 0 40 Y Y 1 0 N
  74. * F0 "U" 0 -350 60 H V
  75. * F1 "DUMMY" 0 350 60 H V
  76. * DRAW
  77. * T 0 0 0 150 0 0 0 ??
  78. * S -200 200 200 -200 0 1 0
  79. * ENDDRAW
  80. * ENDDEF
  81. */
  82. static LIB_PART* dummy()
  83. {
  84. static LIB_PART* part;
  85. if( !part )
  86. {
  87. part = new LIB_PART( wxEmptyString );
  88. LIB_RECTANGLE* square = new LIB_RECTANGLE( part );
  89. square->Move( wxPoint( -200, 200 ) );
  90. square->SetEndPosition( wxPoint( 200, -200 ) );
  91. LIB_TEXT* text = new LIB_TEXT( part );
  92. text->SetTextSize( wxSize( 150, 150 ) );
  93. text->SetText( wxString( wxT( "??" ) ) );
  94. part->AddDrawItem( square );
  95. part->AddDrawItem( text );
  96. }
  97. return part;
  98. }
  99. SCH_COMPONENT::SCH_COMPONENT( const wxPoint& aPos, SCH_ITEM* aParent ) :
  100. SCH_ITEM( aParent, SCH_COMPONENT_T )
  101. {
  102. Init( aPos );
  103. m_currentSheetPath = NULL;
  104. m_fieldsAutoplaced = AUTOPLACED_NO;
  105. }
  106. SCH_COMPONENT::SCH_COMPONENT( LIB_PART& aPart, SCH_SHEET_PATH* sheet, int unit,
  107. int convert, const wxPoint& pos, bool setNewItemFlag ) :
  108. SCH_ITEM( NULL, SCH_COMPONENT_T )
  109. {
  110. Init( pos );
  111. m_unit = unit;
  112. m_convert = convert;
  113. m_lib_id.SetLibItemName( aPart.GetName(), false );
  114. m_part = aPart.SharedPtr();
  115. m_currentSheetPath = NULL;
  116. m_fieldsAutoplaced = AUTOPLACED_NO;
  117. SetTimeStamp( GetNewTimeStamp() );
  118. if( setNewItemFlag )
  119. m_Flags = IS_NEW | IS_MOVED;
  120. // Import user defined fields from the library component:
  121. LIB_FIELDS libFields;
  122. aPart.GetFields( libFields );
  123. for( LIB_FIELDS::iterator it = libFields.begin(); it!=libFields.end(); ++it )
  124. {
  125. // Can no longer insert an empty name, since names are now keys. The
  126. // field index is not used beyond the first MANDATORY_FIELDS
  127. if( it->GetName().IsEmpty() )
  128. continue;
  129. // See if field already exists (mandatory fields always exist).
  130. // for mandatory fields, the name and field id are fixed, so we use the
  131. // known and fixed id to get them (more reliable than names, which can be translated)
  132. // for other fields (custom fields), locate the field by same name
  133. // (field id has no known meaning for custom fields)
  134. int idx = it->GetId();
  135. SCH_FIELD* schField;
  136. if( idx < MANDATORY_FIELDS )
  137. schField = GetField( idx );
  138. else
  139. schField = FindField( it->GetName() );
  140. if( !schField )
  141. {
  142. SCH_FIELD fld( wxPoint( 0, 0 ), GetFieldCount(), this, it->GetName() );
  143. schField = AddField( fld );
  144. }
  145. schField->ImportValues( *it );
  146. schField->SetText( it->GetText() );
  147. // Now the field is initialized, place it to the right position:
  148. schField->SetTextPos( m_Pos + it->GetTextPos() );
  149. }
  150. wxString msg = aPart.GetReferenceField().GetText();
  151. if( msg.IsEmpty() )
  152. msg = wxT( "U" );
  153. m_prefix = msg;
  154. // update the reference -- just the prefix for now.
  155. msg += wxT( "?" );
  156. SetRef( sheet, msg );
  157. // Use the schematic component name instead of the library value field name.
  158. GetField( VALUE )->SetText( GetLibId().GetLibItemName() );
  159. }
  160. SCH_COMPONENT::SCH_COMPONENT( const SCH_COMPONENT& aComponent ) :
  161. SCH_ITEM( aComponent )
  162. {
  163. m_currentSheetPath = NULL;
  164. m_Parent = aComponent.m_Parent;
  165. m_Pos = aComponent.m_Pos;
  166. m_unit = aComponent.m_unit;
  167. m_convert = aComponent.m_convert;
  168. m_lib_id = aComponent.m_lib_id;
  169. m_part = aComponent.m_part;
  170. SetTimeStamp( aComponent.m_TimeStamp );
  171. m_transform = aComponent.m_transform;
  172. m_prefix = aComponent.m_prefix;
  173. m_PathsAndReferences = aComponent.m_PathsAndReferences;
  174. m_Fields = aComponent.m_Fields;
  175. // Re-parent the fields, which before this had aComponent as parent
  176. for( int i = 0; i<GetFieldCount(); ++i )
  177. {
  178. GetField( i )->SetParent( this );
  179. }
  180. m_isDangling = aComponent.m_isDangling;
  181. m_fieldsAutoplaced = aComponent.m_fieldsAutoplaced;
  182. }
  183. void SCH_COMPONENT::Init( const wxPoint& pos )
  184. {
  185. m_Pos = pos;
  186. m_unit = 0; // In multi unit chip - which unit to draw.
  187. m_convert = 0; // De Morgan Handling
  188. // The rotation/mirror transformation matrix. pos normal
  189. m_transform = TRANSFORM();
  190. // construct only the mandatory fields, which are the first 4 only.
  191. for( int i = 0; i < MANDATORY_FIELDS; ++i )
  192. {
  193. SCH_FIELD field( pos, i, this, TEMPLATE_FIELDNAME::GetDefaultFieldName( i ) );
  194. if( i == REFERENCE )
  195. field.SetLayer( LAYER_REFERENCEPART );
  196. else if( i == VALUE )
  197. field.SetLayer( LAYER_VALUEPART );
  198. // else keep LAYER_FIELDS from SCH_FIELD constructor
  199. // SCH_FIELD's implicitly created copy constructor is called in here
  200. AddField( field );
  201. }
  202. m_prefix = wxString( wxT( "U" ) );
  203. }
  204. EDA_ITEM* SCH_COMPONENT::Clone() const
  205. {
  206. return new SCH_COMPONENT( *this );
  207. }
  208. void SCH_COMPONENT::SetLibId( const LIB_ID& aLibId, PART_LIBS* aLibs )
  209. {
  210. if( m_lib_id != aLibId )
  211. {
  212. m_lib_id = aLibId;
  213. SetModified();
  214. if( aLibs )
  215. Resolve( aLibs );
  216. else
  217. m_part.reset();
  218. }
  219. }
  220. void SCH_COMPONENT::SetLibId( const LIB_ID& aLibId, SYMBOL_LIB_TABLE* aSymLibTable )
  221. {
  222. if( m_lib_id != aLibId )
  223. {
  224. wxCHECK_RET( aSymLibTable, "No symbol library table provided." );
  225. m_lib_id = aLibId;
  226. SetModified();
  227. LIB_ALIAS* alias = aSymLibTable->LoadSymbol( m_lib_id.GetLibNickname(),
  228. m_lib_id.GetLibItemName() );
  229. if( alias && alias->GetPart() )
  230. m_part = alias->GetPart()->SharedPtr();
  231. else
  232. m_part.reset();
  233. }
  234. }
  235. wxString SCH_COMPONENT::GetAliasDescription() const
  236. {
  237. if( PART_SPTR part = m_part.lock() )
  238. {
  239. LIB_ALIAS* alias = part->GetAlias( GetLibId().GetLibItemName() );
  240. if( !alias )
  241. return wxEmptyString;
  242. return alias->GetDescription();
  243. }
  244. return wxEmptyString;
  245. }
  246. wxString SCH_COMPONENT::GetAliasDocumentation() const
  247. {
  248. if( PART_SPTR part = m_part.lock() )
  249. {
  250. LIB_ALIAS* alias = part->GetAlias( GetLibId().GetLibItemName() );
  251. if( !alias )
  252. return wxEmptyString;
  253. return alias->GetDocFileName();
  254. }
  255. return wxEmptyString;
  256. }
  257. bool SCH_COMPONENT::Resolve( PART_LIBS* aLibs )
  258. {
  259. // I've never been happy that the actual individual PART_LIB is left up to
  260. // flimsy search path ordering. None-the-less find a part based on that design:
  261. if( LIB_PART* part = aLibs->FindLibPart( m_lib_id ) )
  262. {
  263. m_part = part->SharedPtr();
  264. return true;
  265. }
  266. return false;
  267. }
  268. bool SCH_COMPONENT::Resolve( SYMBOL_LIB_TABLE& aLibTable )
  269. {
  270. LIB_ALIAS* alias = aLibTable.LoadSymbol( m_lib_id );
  271. if( alias && alias->GetPart() )
  272. {
  273. m_part = alias->GetPart()->SharedPtr();
  274. return true;
  275. }
  276. return false;
  277. }
  278. // Helper sort function, used in SCH_COMPONENT::ResolveAll, to sort
  279. // sch component by lib_id
  280. static bool sort_by_libid( const SCH_COMPONENT* ref, SCH_COMPONENT* cmp )
  281. {
  282. return ref->GetLibId() < cmp->GetLibId();
  283. }
  284. void SCH_COMPONENT::ResolveAll( const SCH_COLLECTOR& aComponents, PART_LIBS* aLibs )
  285. {
  286. // Usually, many components use the same part lib.
  287. // to avoid too long calculation time the list of components is grouped
  288. // and once the lib part is found for one member of a group, it is also
  289. // set for all other members of this group
  290. std::vector<SCH_COMPONENT*> cmp_list;
  291. // build the cmp list.
  292. for( int i = 0; i < aComponents.GetCount(); ++i )
  293. {
  294. SCH_COMPONENT* cmp = dynamic_cast<SCH_COMPONENT*>( aComponents[i] );
  295. wxASSERT( cmp );
  296. if( cmp ) // cmp == NULL should not occur.
  297. cmp_list.push_back( cmp );
  298. }
  299. // sort it by lib part. Cmp will be grouped by same lib part.
  300. std::sort( cmp_list.begin(), cmp_list.end(), sort_by_libid );
  301. LIB_ID curr_libid;
  302. for( unsigned ii = 0; ii < cmp_list.size (); ++ii )
  303. {
  304. SCH_COMPONENT* cmp = cmp_list[ii];
  305. curr_libid = cmp->m_lib_id;
  306. cmp->Resolve( aLibs );
  307. // Propagate the m_part pointer to other members using the same lib_id
  308. for( unsigned jj = ii+1; jj < cmp_list.size (); ++jj )
  309. {
  310. SCH_COMPONENT* next_cmp = cmp_list[jj];
  311. if( curr_libid != next_cmp->m_lib_id )
  312. break;
  313. next_cmp->m_part = cmp->m_part;
  314. ii = jj;
  315. }
  316. }
  317. }
  318. void SCH_COMPONENT::ResolveAll( const SCH_COLLECTOR& aComponents, SYMBOL_LIB_TABLE& aLibTable )
  319. {
  320. std::vector<SCH_COMPONENT*> cmp_list;
  321. for( int i = 0; i < aComponents.GetCount(); ++i )
  322. {
  323. SCH_COMPONENT* cmp = dynamic_cast<SCH_COMPONENT*>( aComponents[i] );
  324. wxCHECK2_MSG( cmp, continue, "Invalid SCH_COMPONENT pointer in list." );
  325. cmp_list.push_back( cmp );
  326. }
  327. // sort it by lib part. Cmp will be grouped by same lib part.
  328. std::sort( cmp_list.begin(), cmp_list.end(), sort_by_libid );
  329. LIB_ID curr_libid;
  330. for( unsigned ii = 0; ii < cmp_list.size (); ++ii )
  331. {
  332. SCH_COMPONENT* cmp = cmp_list[ii];
  333. cmp->Resolve( aLibTable );
  334. // Propagate the m_part pointer to other members using the same lib_id
  335. for( unsigned jj = ii+1; jj < cmp_list.size (); ++jj )
  336. {
  337. SCH_COMPONENT* next_cmp = cmp_list[jj];
  338. if( curr_libid != next_cmp->m_lib_id )
  339. break;
  340. next_cmp->m_part = cmp->m_part;
  341. ii = jj;
  342. }
  343. }
  344. }
  345. void SCH_COMPONENT::SetUnit( int aUnit )
  346. {
  347. if( m_unit != aUnit )
  348. {
  349. m_unit = aUnit;
  350. SetModified();
  351. }
  352. }
  353. void SCH_COMPONENT::UpdateUnit( int aUnit )
  354. {
  355. m_unit = aUnit;
  356. }
  357. void SCH_COMPONENT::SetConvert( int aConvert )
  358. {
  359. if( m_convert != aConvert )
  360. {
  361. m_convert = aConvert;
  362. SetModified();
  363. }
  364. }
  365. void SCH_COMPONENT::SetTransform( const TRANSFORM& aTransform )
  366. {
  367. if( m_transform != aTransform )
  368. {
  369. m_transform = aTransform;
  370. SetModified();
  371. }
  372. }
  373. int SCH_COMPONENT::GetUnitCount() const
  374. {
  375. if( PART_SPTR part = m_part.lock() )
  376. {
  377. return part->GetUnitCount();
  378. }
  379. return 0;
  380. }
  381. void SCH_COMPONENT::Draw( EDA_DRAW_PANEL* aPanel, wxDC* aDC, const wxPoint& aOffset,
  382. GR_DRAWMODE aDrawMode, COLOR4D aColor,
  383. bool aDrawPinText )
  384. {
  385. auto opts = PART_DRAW_OPTIONS::Default();
  386. opts.draw_mode = aDrawMode;
  387. opts.color = aColor;
  388. opts.transform = m_transform;
  389. opts.show_pin_text = aDrawPinText;
  390. opts.draw_visible_fields = false;
  391. opts.draw_hidden_fields = false;
  392. if( PART_SPTR part = m_part.lock() )
  393. {
  394. // Draw pin targets if part is being dragged
  395. bool dragging = aPanel->GetScreen()->GetCurItem() == this && aPanel->IsMouseCaptured();
  396. if( !dragging )
  397. {
  398. opts.dangling = m_isDangling;
  399. }
  400. part->Draw( aPanel, aDC, m_Pos + aOffset, m_unit, m_convert, opts );
  401. }
  402. else // Use dummy() part if the actual cannot be found.
  403. {
  404. dummy()->Draw( aPanel, aDC, m_Pos + aOffset, 0, 0, opts );
  405. }
  406. SCH_FIELD* field = GetField( REFERENCE );
  407. if( field->IsVisible() && !field->IsMoving() )
  408. {
  409. field->Draw( aPanel, aDC, aOffset, aDrawMode );
  410. }
  411. for( int ii = VALUE; ii < GetFieldCount(); ii++ )
  412. {
  413. field = GetField( ii );
  414. if( field->IsMoving() )
  415. continue;
  416. field->Draw( aPanel, aDC, aOffset, aDrawMode );
  417. }
  418. #if 0
  419. // Only for testing purposes, draw the component bounding box
  420. {
  421. EDA_RECT boundingBox = GetBoundingBox();
  422. GRRect( aPanel->GetClipBox(), aDC, boundingBox, 0, BROWN );
  423. #if 1
  424. if( GetField( REFERENCE )->IsVisible() )
  425. {
  426. boundingBox = GetField( REFERENCE )->GetBoundingBox();
  427. GRRect( aPanel->GetClipBox(), aDC, boundingBox, 0, BROWN );
  428. }
  429. if( GetField( VALUE )->IsVisible() )
  430. {
  431. boundingBox = GetField( VALUE )->GetBoundingBox();
  432. GRRect( aPanel->GetClipBox(), aDC, boundingBox, 0, BROWN );
  433. }
  434. #endif
  435. }
  436. #endif
  437. }
  438. void SCH_COMPONENT::AddHierarchicalReference( const wxString& aPath,
  439. const wxString& aRef,
  440. int aMulti )
  441. {
  442. wxString h_path, h_ref;
  443. wxStringTokenizer tokenizer;
  444. wxString separators( wxT( " " ) );
  445. // Search for an existing path and remove it if found (should not occur)
  446. for( unsigned ii = 0; ii < m_PathsAndReferences.GetCount(); ii++ )
  447. {
  448. tokenizer.SetString( m_PathsAndReferences[ii], separators );
  449. h_path = tokenizer.GetNextToken();
  450. if( h_path.Cmp( aPath ) == 0 )
  451. {
  452. m_PathsAndReferences.RemoveAt( ii );
  453. ii--;
  454. }
  455. }
  456. h_ref = aPath + wxT( " " ) + aRef;
  457. h_ref << wxT( " " ) << aMulti;
  458. m_PathsAndReferences.Add( h_ref );
  459. }
  460. wxString SCH_COMPONENT::GetPath( const SCH_SHEET_PATH* sheet ) const
  461. {
  462. wxCHECK_MSG( sheet != NULL, wxEmptyString,
  463. wxT( "Cannot get component path with invalid sheet object." ) );
  464. wxString str;
  465. str.Printf( wxT( "%8.8lX" ), (long unsigned) m_TimeStamp );
  466. return sheet->Path() + str;
  467. }
  468. const wxString SCH_COMPONENT::GetRef( const SCH_SHEET_PATH* sheet )
  469. {
  470. wxString path = GetPath( sheet );
  471. wxString h_path, h_ref;
  472. wxStringTokenizer tokenizer;
  473. wxString separators( wxT( " " ) );
  474. for( unsigned ii = 0; ii < m_PathsAndReferences.GetCount(); ii++ )
  475. {
  476. tokenizer.SetString( m_PathsAndReferences[ii], separators );
  477. h_path = tokenizer.GetNextToken();
  478. if( h_path.Cmp( path ) == 0 )
  479. {
  480. h_ref = tokenizer.GetNextToken();
  481. /* printf( "GetRef hpath: %s\n",
  482. * TO_UTF8( m_PathsAndReferences[ii] ) ); */
  483. return h_ref;
  484. }
  485. }
  486. // if it was not found in m_Paths array, then see if it is in
  487. // m_Field[REFERENCE] -- if so, use this as a default for this path.
  488. // this will happen if we load a version 1 schematic file.
  489. // it will also mean that multiple instances of the same sheet by default
  490. // all have the same component references, but perhaps this is best.
  491. if( !GetField( REFERENCE )->GetText().IsEmpty() )
  492. {
  493. SetRef( sheet, GetField( REFERENCE )->GetText() );
  494. return GetField( REFERENCE )->GetText();
  495. }
  496. return m_prefix;
  497. }
  498. bool SCH_COMPONENT::IsReferenceStringValid( const wxString& aReferenceString )
  499. {
  500. wxString text = aReferenceString;
  501. bool ok = true;
  502. // Try to unannotate this reference
  503. while( !text.IsEmpty() && ( text.Last() == '?' || isdigit( text.Last() ) ) )
  504. text.RemoveLast();
  505. if( text.IsEmpty() )
  506. ok = false;
  507. // Add here other constraints
  508. // Currently:no other constraint
  509. return ok;
  510. }
  511. void SCH_COMPONENT::SetRef( const SCH_SHEET_PATH* sheet, const wxString& ref )
  512. {
  513. wxString path = GetPath( sheet );
  514. bool notInArray = true;
  515. wxString h_path, h_ref;
  516. wxStringTokenizer tokenizer;
  517. wxString separators( wxT( " " ) );
  518. // check to see if it is already there before inserting it
  519. for( unsigned ii = 0; ii < m_PathsAndReferences.GetCount(); ii++ )
  520. {
  521. tokenizer.SetString( m_PathsAndReferences[ii], separators );
  522. h_path = tokenizer.GetNextToken();
  523. if( h_path.Cmp( path ) == 0 )
  524. {
  525. // just update the reference text, not the timestamp.
  526. h_ref = h_path + wxT( " " ) + ref;
  527. h_ref += wxT( " " );
  528. tokenizer.GetNextToken(); // Skip old reference
  529. h_ref += tokenizer.GetNextToken(); // Add part selection
  530. // Add the part selection
  531. m_PathsAndReferences[ii] = h_ref;
  532. notInArray = false;
  533. }
  534. }
  535. if( notInArray )
  536. AddHierarchicalReference( path, ref, m_unit );
  537. SCH_FIELD* rf = GetField( REFERENCE );
  538. if( rf->GetText().IsEmpty()
  539. || ( abs( rf->GetTextPos().x - m_Pos.x ) +
  540. abs( rf->GetTextPos().y - m_Pos.y ) > 10000 ) )
  541. {
  542. // move it to a reasonable position
  543. rf->SetTextPos( m_Pos + wxPoint( 50, 50 ) );
  544. }
  545. rf->SetText( ref ); // for drawing.
  546. // Reinit the m_prefix member if needed
  547. wxString prefix = ref;
  548. if( IsReferenceStringValid( prefix ) )
  549. {
  550. while( prefix.Last() == '?' || isdigit( prefix.Last() ) )
  551. prefix.RemoveLast();
  552. }
  553. else
  554. {
  555. prefix = wxT( "U" ); // Set to default ref prefix
  556. }
  557. if( m_prefix != prefix )
  558. m_prefix = prefix;
  559. }
  560. void SCH_COMPONENT::SetTimeStamp( time_t aNewTimeStamp )
  561. {
  562. wxString string_timestamp, string_oldtimestamp;
  563. string_timestamp.Printf( wxT( "%08lX" ), (long unsigned) aNewTimeStamp );
  564. string_oldtimestamp.Printf( wxT( "%08lX" ), (long unsigned) m_TimeStamp );
  565. EDA_ITEM::SetTimeStamp( aNewTimeStamp );
  566. for( unsigned ii = 0; ii < m_PathsAndReferences.GetCount(); ii++ )
  567. {
  568. m_PathsAndReferences[ii].Replace( string_oldtimestamp.GetData(),
  569. string_timestamp.GetData() );
  570. }
  571. }
  572. int SCH_COMPONENT::GetUnitSelection( SCH_SHEET_PATH* aSheet )
  573. {
  574. wxString path = GetPath( aSheet );
  575. wxString h_path, h_multi;
  576. wxStringTokenizer tokenizer;
  577. wxString separators( wxT( " " ) );
  578. for( unsigned ii = 0; ii < m_PathsAndReferences.GetCount(); ii++ )
  579. {
  580. tokenizer.SetString( m_PathsAndReferences[ii], separators );
  581. h_path = tokenizer.GetNextToken();
  582. if( h_path.Cmp( path ) == 0 )
  583. {
  584. tokenizer.GetNextToken(); // Skip reference
  585. h_multi = tokenizer.GetNextToken();
  586. long imulti = 1;
  587. h_multi.ToLong( &imulti );
  588. return imulti;
  589. }
  590. }
  591. // if it was not found in m_Paths array, then use m_unit.
  592. // this will happen if we load a version 1 schematic file.
  593. return m_unit;
  594. }
  595. void SCH_COMPONENT::SetUnitSelection( SCH_SHEET_PATH* aSheet, int aUnitSelection )
  596. {
  597. wxString path = GetPath( aSheet );
  598. bool notInArray = true;
  599. wxString h_path, h_ref;
  600. wxStringTokenizer tokenizer;
  601. wxString separators( wxT( " " ) );
  602. //check to see if it is already there before inserting it
  603. for( unsigned ii = 0; ii < m_PathsAndReferences.GetCount(); ii++ )
  604. {
  605. tokenizer.SetString( m_PathsAndReferences[ii], separators );
  606. h_path = tokenizer.GetNextToken();
  607. if( h_path.Cmp( path ) == 0 )
  608. {
  609. //just update the unit selection.
  610. h_ref = h_path + wxT( " " );
  611. h_ref += tokenizer.GetNextToken(); // Add reference
  612. h_ref += wxT( " " );
  613. h_ref << aUnitSelection; // Add part selection
  614. // Ann the part selection
  615. m_PathsAndReferences[ii] = h_ref;
  616. notInArray = false;
  617. }
  618. }
  619. if( notInArray )
  620. AddHierarchicalReference( path, m_prefix, aUnitSelection );
  621. }
  622. SCH_FIELD* SCH_COMPONENT::GetField( int aFieldNdx ) const
  623. {
  624. const SCH_FIELD* field;
  625. if( (unsigned) aFieldNdx < m_Fields.size() )
  626. field = &m_Fields[aFieldNdx];
  627. else
  628. field = NULL;
  629. wxASSERT( field );
  630. // use cast to remove const-ness
  631. return (SCH_FIELD*) field;
  632. }
  633. wxString SCH_COMPONENT::GetFieldText( wxString aFieldName, bool aIncludeDefaultFields ) const
  634. {
  635. // Field name for comparison
  636. wxString cmpFieldName;
  637. if( aIncludeDefaultFields )
  638. {
  639. // Default field names
  640. for ( unsigned int i=0; i<MANDATORY_FIELDS; i++)
  641. {
  642. cmpFieldName = TEMPLATE_FIELDNAME::GetDefaultFieldName( i );
  643. if( cmpFieldName.Cmp( aFieldName ) == 0 )
  644. {
  645. return m_Fields[i].GetText();
  646. }
  647. }
  648. }
  649. // Search custom fields
  650. for( unsigned int ii=MANDATORY_FIELDS; ii<m_Fields.size(); ii++ )
  651. {
  652. cmpFieldName = m_Fields[ii].GetName();
  653. if( cmpFieldName.Cmp( aFieldName ) == 0 )
  654. {
  655. return m_Fields[ii].GetText();
  656. }
  657. }
  658. return wxEmptyString;
  659. }
  660. void SCH_COMPONENT::GetFields( std::vector<SCH_FIELD*>& aVector, bool aVisibleOnly )
  661. {
  662. for( SCH_FIELD& each_field : m_Fields )
  663. {
  664. if( !aVisibleOnly || ( each_field.IsVisible() && !each_field.IsVoid() ) )
  665. aVector.push_back( &each_field );
  666. }
  667. }
  668. SCH_FIELD* SCH_COMPONENT::AddField( const SCH_FIELD& aField )
  669. {
  670. int newNdx = m_Fields.size();
  671. m_Fields.push_back( aField );
  672. return &m_Fields[newNdx];
  673. }
  674. SCH_FIELD* SCH_COMPONENT::FindField( const wxString& aFieldName, bool aIncludeDefaultFields )
  675. {
  676. unsigned start = aIncludeDefaultFields ? 0 : MANDATORY_FIELDS;
  677. for( unsigned i = start; i<m_Fields.size(); ++i )
  678. {
  679. if( aFieldName == m_Fields[i].GetName( false ) )
  680. {
  681. return &m_Fields[i];
  682. }
  683. }
  684. return NULL;
  685. }
  686. LIB_PIN* SCH_COMPONENT::GetPin( const wxString& number )
  687. {
  688. if( PART_SPTR part = m_part.lock() )
  689. {
  690. return part->GetPin( number, m_unit, m_convert );
  691. }
  692. return NULL;
  693. }
  694. void SCH_COMPONENT::GetPins( std::vector<LIB_PIN*>& aPinsList )
  695. {
  696. if( PART_SPTR part = m_part.lock() )
  697. {
  698. part->GetPins( aPinsList, m_unit, m_convert );
  699. }
  700. else
  701. wxFAIL_MSG( "Could not obtain PART_SPTR lock" );
  702. }
  703. void SCH_COMPONENT::SwapData( SCH_ITEM* aItem )
  704. {
  705. wxCHECK_RET( (aItem != NULL) && (aItem->Type() == SCH_COMPONENT_T),
  706. wxT( "Cannot swap data with invalid component." ) );
  707. SCH_COMPONENT* component = (SCH_COMPONENT*) aItem;
  708. std::swap( m_lib_id, component->m_lib_id );
  709. std::swap( m_part, component->m_part );
  710. std::swap( m_Pos, component->m_Pos );
  711. std::swap( m_unit, component->m_unit );
  712. std::swap( m_convert, component->m_convert );
  713. TRANSFORM tmp = m_transform;
  714. m_transform = component->m_transform;
  715. component->m_transform = tmp;
  716. m_Fields.swap( component->m_Fields ); // std::vector's swap()
  717. // Reparent items after copying data
  718. // (after swap(), m_Parent member does not point to the right parent):
  719. for( int ii = 0; ii < component->GetFieldCount(); ++ii )
  720. {
  721. component->GetField( ii )->SetParent( component );
  722. }
  723. for( int ii = 0; ii < GetFieldCount(); ++ii )
  724. {
  725. GetField( ii )->SetParent( this );
  726. }
  727. std::swap( m_PathsAndReferences, component->m_PathsAndReferences );
  728. }
  729. void SCH_COMPONENT::ClearAnnotation( SCH_SHEET_PATH* aSheetPath )
  730. {
  731. bool keepMulti = false;
  732. wxArrayString reference_fields;
  733. static const wxChar separators[] = wxT( " " );
  734. PART_SPTR part = m_part.lock();
  735. if( part && part->UnitsLocked() )
  736. keepMulti = true;
  737. // Build a reference with no annotation,
  738. // i.e. a reference ended by only one '?'
  739. wxString defRef = m_prefix;
  740. if( IsReferenceStringValid( defRef ) )
  741. {
  742. while( defRef.Last() == '?' )
  743. defRef.RemoveLast();
  744. }
  745. else
  746. { // This is a malformed reference: reinit this reference
  747. m_prefix = defRef = wxT("U"); // Set to default ref prefix
  748. }
  749. defRef.Append( wxT( "?" ) );
  750. wxString multi = wxT( "1" );
  751. // For components with units locked,
  752. // we cannot remove all annotations: part selection must be kept
  753. // For all components: if aSheetPath is not NULL,
  754. // remove annotation only for the given path
  755. if( keepMulti || aSheetPath )
  756. {
  757. wxString NewHref;
  758. wxString path;
  759. if( aSheetPath )
  760. path = GetPath( aSheetPath );
  761. for( unsigned int ii = 0; ii < m_PathsAndReferences.GetCount(); ii++ )
  762. {
  763. // Break hierarchical reference in path, ref and multi selection:
  764. reference_fields = wxStringTokenize( m_PathsAndReferences[ii], separators );
  765. if( aSheetPath == NULL || reference_fields[0].Cmp( path ) == 0 )
  766. {
  767. if( keepMulti ) // Get and keep part selection
  768. multi = reference_fields[2];
  769. NewHref = reference_fields[0];
  770. NewHref << wxT( " " ) << defRef << wxT( " " ) << multi;
  771. m_PathsAndReferences[ii] = NewHref;
  772. }
  773. }
  774. }
  775. else
  776. {
  777. // Clear reference strings, but does not free memory because a new annotation
  778. // will reuse it
  779. m_PathsAndReferences.Empty();
  780. m_unit = 1;
  781. }
  782. // These 2 changes do not work in complex hierarchy.
  783. // When a clear annotation is made, the calling function must call a
  784. // UpdateAllScreenReferences for the active sheet.
  785. // But this call cannot made here.
  786. m_Fields[REFERENCE].SetText( defRef ); //for drawing.
  787. SetModified();
  788. }
  789. void SCH_COMPONENT::SetOrientation( int aOrientation )
  790. {
  791. TRANSFORM temp = TRANSFORM();
  792. bool transform = false;
  793. switch( aOrientation )
  794. {
  795. case CMP_ORIENT_0:
  796. case CMP_NORMAL: // default transform matrix
  797. m_transform.x1 = 1;
  798. m_transform.y2 = -1;
  799. m_transform.x2 = m_transform.y1 = 0;
  800. break;
  801. case CMP_ROTATE_COUNTERCLOCKWISE: // Rotate + (incremental rotation)
  802. temp.x1 = temp.y2 = 0;
  803. temp.y1 = 1;
  804. temp.x2 = -1;
  805. transform = true;
  806. break;
  807. case CMP_ROTATE_CLOCKWISE: // Rotate - (incremental rotation)
  808. temp.x1 = temp.y2 = 0;
  809. temp.y1 = -1;
  810. temp.x2 = 1;
  811. transform = true;
  812. break;
  813. case CMP_MIRROR_Y: // Mirror Y (incremental rotation)
  814. temp.x1 = -1;
  815. temp.y2 = 1;
  816. temp.y1 = temp.x2 = 0;
  817. transform = true;
  818. break;
  819. case CMP_MIRROR_X: // Mirror X (incremental rotation)
  820. temp.x1 = 1;
  821. temp.y2 = -1;
  822. temp.y1 = temp.x2 = 0;
  823. transform = true;
  824. break;
  825. case CMP_ORIENT_90:
  826. SetOrientation( CMP_ORIENT_0 );
  827. SetOrientation( CMP_ROTATE_COUNTERCLOCKWISE );
  828. break;
  829. case CMP_ORIENT_180:
  830. SetOrientation( CMP_ORIENT_0 );
  831. SetOrientation( CMP_ROTATE_COUNTERCLOCKWISE );
  832. SetOrientation( CMP_ROTATE_COUNTERCLOCKWISE );
  833. break;
  834. case CMP_ORIENT_270:
  835. SetOrientation( CMP_ORIENT_0 );
  836. SetOrientation( CMP_ROTATE_CLOCKWISE );
  837. break;
  838. case ( CMP_ORIENT_0 + CMP_MIRROR_X ):
  839. SetOrientation( CMP_ORIENT_0 );
  840. SetOrientation( CMP_MIRROR_X );
  841. break;
  842. case ( CMP_ORIENT_0 + CMP_MIRROR_Y ):
  843. SetOrientation( CMP_ORIENT_0 );
  844. SetOrientation( CMP_MIRROR_Y );
  845. break;
  846. case ( CMP_ORIENT_90 + CMP_MIRROR_X ):
  847. SetOrientation( CMP_ORIENT_90 );
  848. SetOrientation( CMP_MIRROR_X );
  849. break;
  850. case ( CMP_ORIENT_90 + CMP_MIRROR_Y ):
  851. SetOrientation( CMP_ORIENT_90 );
  852. SetOrientation( CMP_MIRROR_Y );
  853. break;
  854. case ( CMP_ORIENT_180 + CMP_MIRROR_X ):
  855. SetOrientation( CMP_ORIENT_180 );
  856. SetOrientation( CMP_MIRROR_X );
  857. break;
  858. case ( CMP_ORIENT_180 + CMP_MIRROR_Y ):
  859. SetOrientation( CMP_ORIENT_180 );
  860. SetOrientation( CMP_MIRROR_Y );
  861. break;
  862. case ( CMP_ORIENT_270 + CMP_MIRROR_X ):
  863. SetOrientation( CMP_ORIENT_270 );
  864. SetOrientation( CMP_MIRROR_X );
  865. break;
  866. case ( CMP_ORIENT_270 + CMP_MIRROR_Y ):
  867. SetOrientation( CMP_ORIENT_270 );
  868. SetOrientation( CMP_MIRROR_Y );
  869. break;
  870. default:
  871. transform = false;
  872. wxMessageBox( wxT( "SetRotateMiroir() error: ill value" ) );
  873. break;
  874. }
  875. if( transform )
  876. {
  877. /* The new matrix transform is the old matrix transform modified by the
  878. * requested transformation, which is the temp transform (rot,
  879. * mirror ..) in order to have (in term of matrix transform):
  880. * transform coord = new_m_transform * coord
  881. * where transform coord is the coord modified by new_m_transform from
  882. * the initial value coord.
  883. * new_m_transform is computed (from old_m_transform and temp) to
  884. * have:
  885. * transform coord = old_m_transform * temp
  886. */
  887. TRANSFORM newTransform;
  888. newTransform.x1 = m_transform.x1 * temp.x1 + m_transform.x2 * temp.y1;
  889. newTransform.y1 = m_transform.y1 * temp.x1 + m_transform.y2 * temp.y1;
  890. newTransform.x2 = m_transform.x1 * temp.x2 + m_transform.x2 * temp.y2;
  891. newTransform.y2 = m_transform.y1 * temp.x2 + m_transform.y2 * temp.y2;
  892. m_transform = newTransform;
  893. }
  894. }
  895. int SCH_COMPONENT::GetOrientation()
  896. {
  897. int type_rotate = CMP_ORIENT_0;
  898. TRANSFORM transform;
  899. int ii;
  900. #define ROTATE_VALUES_COUNT 12
  901. // list of all possibilities, but only the first 8 are actually used
  902. int rotate_value[ROTATE_VALUES_COUNT] =
  903. {
  904. CMP_ORIENT_0, CMP_ORIENT_90, CMP_ORIENT_180,
  905. CMP_ORIENT_270,
  906. CMP_MIRROR_X + CMP_ORIENT_0, CMP_MIRROR_X + CMP_ORIENT_90,
  907. CMP_MIRROR_X + CMP_ORIENT_180, CMP_MIRROR_X + CMP_ORIENT_270,
  908. CMP_MIRROR_Y + CMP_ORIENT_0, CMP_MIRROR_Y + CMP_ORIENT_90,
  909. CMP_MIRROR_Y + CMP_ORIENT_180, CMP_MIRROR_Y + CMP_ORIENT_270
  910. };
  911. // Try to find the current transform option:
  912. transform = m_transform;
  913. for( ii = 0; ii < ROTATE_VALUES_COUNT; ii++ )
  914. {
  915. type_rotate = rotate_value[ii];
  916. SetOrientation( type_rotate );
  917. if( transform == m_transform )
  918. return type_rotate;
  919. }
  920. // Error: orientation not found in list (should not happen)
  921. wxMessageBox( wxT( "Component orientation matrix internal error" ) );
  922. m_transform = transform;
  923. return CMP_NORMAL;
  924. }
  925. wxPoint SCH_COMPONENT::GetScreenCoord( const wxPoint& aPoint )
  926. {
  927. return m_transform.TransformCoordinate( aPoint );
  928. }
  929. #if defined(DEBUG)
  930. void SCH_COMPONENT::Show( int nestLevel, std::ostream& os ) const
  931. {
  932. // for now, make it look like XML:
  933. NestedSpace( nestLevel, os ) << '<' << GetClass().Lower().mb_str()
  934. << " ref=\"" << TO_UTF8( GetField( 0 )->GetName() )
  935. << '"' << " chipName=\""
  936. << GetLibId().Format() << '"' << m_Pos
  937. << " layer=\"" << m_Layer
  938. << '"' << ">\n";
  939. // skip the reference, it's been output already.
  940. for( int i = 1; i < GetFieldCount(); ++i )
  941. {
  942. wxString value = GetField( i )->GetText();
  943. if( !value.IsEmpty() )
  944. {
  945. NestedSpace( nestLevel + 1, os ) << "<field" << " name=\""
  946. << TO_UTF8( GetField( i )->GetName() )
  947. << '"' << " value=\""
  948. << TO_UTF8( value ) << "\"/>\n";
  949. }
  950. }
  951. NestedSpace( nestLevel, os ) << "</" << TO_UTF8( GetClass().Lower() ) << ">\n";
  952. }
  953. #endif
  954. bool SCH_COMPONENT::Save( FILE* f ) const
  955. {
  956. std::string name1;
  957. std::string name2;
  958. wxArrayString reference_fields;
  959. static wxString delimiters( wxT( " " ) );
  960. // this is redundant with the AR entries below, but it makes the
  961. // files backwards-compatible.
  962. if( m_PathsAndReferences.GetCount() > 0 )
  963. {
  964. reference_fields = wxStringTokenize( m_PathsAndReferences[0], delimiters );
  965. name1 = toUTFTildaText( reference_fields[1] );
  966. }
  967. else
  968. {
  969. if( GetField( REFERENCE )->GetText().IsEmpty() )
  970. name1 = toUTFTildaText( m_prefix );
  971. else
  972. name1 = toUTFTildaText( GetField( REFERENCE )->GetText() );
  973. }
  974. wxString part_name = GetLibId().GetLibItemName();
  975. if( part_name.size() )
  976. {
  977. name2 = toUTFTildaText( part_name );
  978. }
  979. else
  980. {
  981. name2 = NULL_STRING;
  982. }
  983. if( fprintf( f, "$Comp\n" ) == EOF )
  984. return false;
  985. if( fprintf( f, "L %s %s\n", name2.c_str(), name1.c_str() ) == EOF )
  986. return false;
  987. // Generate unit number, convert and time stamp
  988. if( fprintf( f, "U %d %d %8.8lX\n", m_unit, m_convert, (unsigned long)m_TimeStamp ) == EOF )
  989. return false;
  990. // Save the position
  991. if( fprintf( f, "P %d %d\n", m_Pos.x, m_Pos.y ) == EOF )
  992. return false;
  993. /* If this is a complex hierarchy; save hierarchical references.
  994. * but for simple hierarchies it is not necessary.
  995. * the reference inf is already saved
  996. * this is useful for old Eeschema version compatibility
  997. */
  998. if( m_PathsAndReferences.GetCount() > 1 )
  999. {
  1000. for( unsigned int ii = 0; ii < m_PathsAndReferences.GetCount(); ii++ )
  1001. {
  1002. /*format:
  1003. * AR Path="/140/2" Ref="C99" Part="1"
  1004. * where 140 is the uid of the containing sheet
  1005. * and 2 is the timestamp of this component.
  1006. * (timestamps are actually 8 hex chars)
  1007. * Ref is the conventional component reference for this 'path'
  1008. * Part is the conventional component part selection for this 'path'
  1009. */
  1010. reference_fields = wxStringTokenize( m_PathsAndReferences[ii], delimiters );
  1011. if( fprintf( f, "AR Path=\"%s\" Ref=\"%s\" Part=\"%s\" \n",
  1012. TO_UTF8( reference_fields[0] ),
  1013. TO_UTF8( reference_fields[1] ),
  1014. TO_UTF8( reference_fields[2] ) ) == EOF )
  1015. return false;
  1016. }
  1017. }
  1018. // update the ugly field index, which I would like to see go away someday soon.
  1019. for( unsigned i = 0; i<m_Fields.size(); ++i )
  1020. {
  1021. SCH_FIELD* fld = GetField( i );
  1022. fld->SetId( i ); // we don't need field Ids, please be gone.
  1023. }
  1024. // Fixed fields:
  1025. // Save mandatory fields even if they are blank,
  1026. // because the visibility, size and orientation are set from libary editor.
  1027. for( unsigned i = 0; i<MANDATORY_FIELDS; ++i )
  1028. {
  1029. SCH_FIELD* fld = GetField( i );
  1030. if( !fld->Save( f ) )
  1031. return false;
  1032. }
  1033. // User defined fields:
  1034. // The *policy* about which user defined fields are part of a symbol is now
  1035. // only in the dialog editors. No policy should be enforced here, simply
  1036. // save all the user defined fields, they are present because a dialog editor
  1037. // thought they should be. If you disagree, go fix the dialog editors.
  1038. for( unsigned i = MANDATORY_FIELDS; i<m_Fields.size(); ++i )
  1039. {
  1040. SCH_FIELD* fld = GetField( i );
  1041. if( !fld->Save( f ) )
  1042. return false;
  1043. }
  1044. // Unit number, position, box ( old standard )
  1045. if( fprintf( f, "\t%-4d %-4d %-4d\n", m_unit, m_Pos.x, m_Pos.y ) == EOF )
  1046. return false;
  1047. if( fprintf( f, "\t%-4d %-4d %-4d %-4d\n",
  1048. m_transform.x1, m_transform.y1, m_transform.x2, m_transform.y2 ) == EOF )
  1049. return false;
  1050. if( fprintf( f, "$EndComp\n" ) == EOF )
  1051. return false;
  1052. return true;
  1053. }
  1054. bool SCH_COMPONENT::Load( LINE_READER& aLine, wxString& aErrorMsg )
  1055. {
  1056. // Remark: avoid using sscanf to read texts entered by user
  1057. // which are UTF8 encoded, because sscanf does not work well on Windows
  1058. // with some UTF8 values.
  1059. char name1[256], char1[256], char2[256], char3[256];
  1060. int newfmt = 0;
  1061. char* ptcar;
  1062. wxString fieldName;
  1063. char* line = aLine.Line();
  1064. unsigned long timeStamp;
  1065. m_convert = 1;
  1066. if( line[0] == '$' )
  1067. {
  1068. newfmt = 1;
  1069. if( !(line = aLine.ReadLine()) )
  1070. return true;
  1071. }
  1072. // Parse the first line of description:
  1073. // like "L partname ref" (for instance "L 74LS00 U4"
  1074. // They are UTF8 texts, so do not use sscanf
  1075. line += 1;
  1076. if( *line == ' ' )
  1077. line++;
  1078. // line points the first parameter
  1079. wxString buffer( FROM_UTF8( line ) );
  1080. wxStringTokenizer tokenizer( buffer, wxT( " \r\n" ) );
  1081. if( tokenizer.CountTokens() < 2 )
  1082. {
  1083. aErrorMsg.Printf( wxT( "Eeschema component description error at line %d, aborted" ),
  1084. aLine.LineNumber() );
  1085. aErrorMsg << wxT( "\n" ) << FROM_UTF8( line );
  1086. return false;
  1087. }
  1088. wxString partname = tokenizer.NextToken();
  1089. partname.Replace( wxT("~"), wxT(" ") ); // all spaces were replaced by ~ in files.
  1090. if( partname != NULL_STRING )
  1091. {
  1092. m_lib_id.SetLibItemName( partname, false );
  1093. if( !newfmt )
  1094. GetField( VALUE )->SetText( partname );
  1095. }
  1096. else
  1097. {
  1098. m_lib_id.clear();
  1099. GetField( VALUE )->Empty();
  1100. GetField( VALUE )->SetTextAngle( TEXT_ANGLE_HORIZ );
  1101. GetField( VALUE )->SetVisible( false );
  1102. }
  1103. wxString reference = tokenizer.NextToken();
  1104. reference.Replace( wxT("~"), wxT(" ") ); // all spaces were replaced by ~ in files.
  1105. reference.Trim( true );
  1106. reference.Trim( false );
  1107. if( reference != NULL_STRING )
  1108. {
  1109. wxString prefix = reference;
  1110. // Build reference prefix from the actual reference by removing trailing digits
  1111. // (Perhaps outdated code, only for very old schematic files)
  1112. while( prefix.Length() )
  1113. {
  1114. if( ( prefix.Last() < '0' || prefix.Last() > '9') && prefix.Last() != '?' )
  1115. break;
  1116. prefix.RemoveLast();
  1117. }
  1118. // Avoid a prefix containing trailing/leading spaces
  1119. prefix.Trim( true );
  1120. prefix.Trim( false );
  1121. if( prefix.IsEmpty() )
  1122. m_prefix = wxT( "U" );
  1123. else
  1124. m_prefix = prefix;
  1125. if( !newfmt )
  1126. GetField( REFERENCE )->SetText( reference );
  1127. }
  1128. else
  1129. {
  1130. GetField( REFERENCE )->SetVisible( false );
  1131. }
  1132. /* Parse component description
  1133. * These lines begin with:
  1134. * "P" = Position
  1135. * U = Num Unit and Conversion
  1136. * "Fn" = Fields (0 .. n = = number of field)
  1137. * "Ar" = Alternate reference in the case of multiple sheets referring to
  1138. * one schematic file.
  1139. */
  1140. for( ; ; )
  1141. {
  1142. if( !(line = aLine.ReadLine()) )
  1143. return false;
  1144. if( line[0] == 'U' )
  1145. {
  1146. sscanf( line + 1, "%d %d %lX", &m_unit, &m_convert, &timeStamp );
  1147. m_TimeStamp = (time_t)timeStamp;
  1148. }
  1149. else if( line[0] == 'P' )
  1150. {
  1151. sscanf( line + 1, "%d %d", &m_Pos.x, &m_Pos.y );
  1152. // Set fields position to a default position (that is the
  1153. // component position. For existing fields, the real position
  1154. // will be set later
  1155. for( int i = 0; i<GetFieldCount(); i++ )
  1156. {
  1157. if( GetField( i )->GetText().IsEmpty() )
  1158. GetField( i )->SetTextPos( m_Pos );
  1159. }
  1160. }
  1161. else if( line[0] == 'A' && line[1] == 'R' )
  1162. {
  1163. /* format:
  1164. * AR Path="/9086AF6E/67452AA0" Ref="C99" Part="1"
  1165. * where 9086AF6E is the unique timestamp of the containing sheet
  1166. * and 67452AA0 is the timestamp of this component.
  1167. * C99 is the reference given this path.
  1168. */
  1169. int ii;
  1170. ptcar = line + 2;
  1171. //copy the path.
  1172. ii = ReadDelimitedText( name1, ptcar, 255 );
  1173. ptcar += ii + 1;
  1174. wxString path = FROM_UTF8( name1 );
  1175. // copy the reference
  1176. ii = ReadDelimitedText( name1, ptcar, 255 );
  1177. ptcar += ii + 1;
  1178. wxString ref = FROM_UTF8( name1 );
  1179. // copy the multi, if exists
  1180. ii = ReadDelimitedText( name1, ptcar, 255 );
  1181. if( name1[0] == 0 ) // Nothing read, put a default value
  1182. sprintf( name1, "%d", m_unit );
  1183. int multi = atoi( name1 );
  1184. // Avoid out of range multi id:
  1185. if( multi < 0 || multi > MAX_UNIT_COUNT_PER_PACKAGE )
  1186. multi = 1;
  1187. AddHierarchicalReference( path, ref, multi );
  1188. GetField( REFERENCE )->SetText( ref );
  1189. }
  1190. else if( line[0] == 'F' )
  1191. {
  1192. int fieldNdx;
  1193. wxString fieldText;
  1194. EDA_TEXT_HJUSTIFY_T hjustify = GR_TEXT_HJUSTIFY_CENTER;
  1195. EDA_TEXT_VJUSTIFY_T vjustify = GR_TEXT_VJUSTIFY_CENTER;
  1196. ptcar = (char*) aLine;
  1197. while( *ptcar && (*ptcar != '"') )
  1198. ptcar++;
  1199. if( *ptcar != '"' )
  1200. {
  1201. aErrorMsg.Printf( wxT( "Eeschema file library field F at line %d, aborted" ),
  1202. aLine.LineNumber() );
  1203. return false;
  1204. }
  1205. ptcar += ReadDelimitedText( &fieldText, ptcar );
  1206. if( *ptcar == 0 )
  1207. {
  1208. aErrorMsg.Printf( wxT( "Component field F at line %d, aborted" ),
  1209. aLine.LineNumber() );
  1210. return false;
  1211. }
  1212. fieldNdx = atoi( line + 2 );
  1213. ReadDelimitedText( &fieldName, ptcar );
  1214. if( fieldName.IsEmpty() )
  1215. fieldName = TEMPLATE_FIELDNAME::GetDefaultFieldName( fieldNdx );
  1216. if( fieldNdx >= GetFieldCount() )
  1217. {
  1218. // The first MANDATOR_FIELDS _must_ be constructed within
  1219. // the SCH_COMPONENT constructor. This assert is simply here
  1220. // to guard against a change in that constructor.
  1221. wxASSERT( GetFieldCount() >= MANDATORY_FIELDS );
  1222. // Ignore the _supplied_ fieldNdx. It is not important anymore
  1223. // if within the user defined fields region (i.e. >= MANDATORY_FIELDS).
  1224. // We freely renumber the index to fit the next available field slot.
  1225. fieldNdx = GetFieldCount(); // new has this index after insertion
  1226. SCH_FIELD field( wxPoint( 0, 0 ),
  1227. -1, // field id is not relavant for user defined fields
  1228. this, fieldName );
  1229. AddField( field );
  1230. }
  1231. else
  1232. {
  1233. GetField( fieldNdx )->SetName( fieldName );
  1234. }
  1235. GetField( fieldNdx )->SetText( fieldText );
  1236. memset( char3, 0, sizeof(char3) );
  1237. int ii, x, y, w, attr;
  1238. if( ( ii = sscanf( ptcar, "%255s %d %d %d %X %255s %255s", char1, &x, &y, &w, &attr,
  1239. char2, char3 ) ) < 4 )
  1240. {
  1241. aErrorMsg.Printf( wxT( "Component Field error line %d, aborted" ),
  1242. aLine.LineNumber() );
  1243. continue;
  1244. }
  1245. GetField( fieldNdx )->SetTextPos( wxPoint( x, y ) );
  1246. GetField( fieldNdx )->SetVisible( !attr );
  1247. if( (w == 0 ) || (ii == 4) )
  1248. w = GetDefaultTextSize();
  1249. GetField( fieldNdx )->SetTextSize( wxSize( w, w ) );
  1250. GetField( fieldNdx )->SetTextAngle( TEXT_ANGLE_HORIZ );
  1251. if( char1[0] == 'V' )
  1252. GetField( fieldNdx )->SetTextAngle( TEXT_ANGLE_VERT );
  1253. if( ii >= 7 )
  1254. {
  1255. if( *char2 == 'L' )
  1256. hjustify = GR_TEXT_HJUSTIFY_LEFT;
  1257. else if( *char2 == 'R' )
  1258. hjustify = GR_TEXT_HJUSTIFY_RIGHT;
  1259. if( char3[0] == 'B' )
  1260. vjustify = GR_TEXT_VJUSTIFY_BOTTOM;
  1261. else if( char3[0] == 'T' )
  1262. vjustify = GR_TEXT_VJUSTIFY_TOP;
  1263. GetField( fieldNdx )->SetItalic( char3[1] == 'I' );
  1264. GetField( fieldNdx )->SetBold( char3[2] == 'B' );
  1265. GetField( fieldNdx )->SetHorizJustify( hjustify );
  1266. GetField( fieldNdx )->SetVertJustify( vjustify );
  1267. }
  1268. if( fieldNdx == REFERENCE )
  1269. if( GetField( fieldNdx )->GetText()[0] == '#' )
  1270. GetField( fieldNdx )->SetVisible( false );
  1271. }
  1272. else
  1273. {
  1274. break;
  1275. }
  1276. }
  1277. if( sscanf( line, "%d %d %d", &m_unit, &m_Pos.x, &m_Pos.y ) != 3 )
  1278. {
  1279. aErrorMsg.Printf( wxT( "Component unit & pos error at line %d, aborted" ),
  1280. aLine.LineNumber() );
  1281. return false;
  1282. }
  1283. if( !(line = aLine.ReadLine()) ||
  1284. sscanf( line, "%d %d %d %d",
  1285. &m_transform.x1,
  1286. &m_transform.y1,
  1287. &m_transform.x2,
  1288. &m_transform.y2 ) != 4 )
  1289. {
  1290. aErrorMsg.Printf( wxT( "Component orient error at line %d, aborted" ),
  1291. aLine.LineNumber() );
  1292. return false;
  1293. }
  1294. if( newfmt )
  1295. {
  1296. if( !(line = aLine.ReadLine()) )
  1297. return false;
  1298. if( strncasecmp( "$End", line, 4 ) != 0 )
  1299. {
  1300. aErrorMsg.Printf( wxT( "Component End expected at line %d, aborted" ),
  1301. aLine.LineNumber() );
  1302. return false;
  1303. }
  1304. }
  1305. // ensure flags (mainly used in edit) are cleared.
  1306. // some changes have set the modified flag
  1307. m_Flags = 0;
  1308. return true;
  1309. }
  1310. EDA_RECT SCH_COMPONENT::GetBodyBoundingBox() const
  1311. {
  1312. EDA_RECT bBox;
  1313. if( PART_SPTR part = m_part.lock() )
  1314. {
  1315. bBox = part->GetBodyBoundingBox( m_unit, m_convert );
  1316. }
  1317. else
  1318. {
  1319. bBox = dummy()->GetBodyBoundingBox( m_unit, m_convert );
  1320. }
  1321. int x0 = bBox.GetX();
  1322. int xm = bBox.GetRight();
  1323. // We must reverse Y values, because matrix orientation
  1324. // suppose Y axis normal for the library items coordinates,
  1325. // m_transform reverse Y values, but bBox is already reversed!
  1326. int y0 = -bBox.GetY();
  1327. int ym = -bBox.GetBottom();
  1328. // Compute the real Boundary box (rotated, mirrored ...)
  1329. int x1 = m_transform.x1 * x0 + m_transform.y1 * y0;
  1330. int y1 = m_transform.x2 * x0 + m_transform.y2 * y0;
  1331. int x2 = m_transform.x1 * xm + m_transform.y1 * ym;
  1332. int y2 = m_transform.x2 * xm + m_transform.y2 * ym;
  1333. // H and W must be > 0:
  1334. if( x2 < x1 )
  1335. std::swap( x2, x1 );
  1336. if( y2 < y1 )
  1337. std::swap( y2, y1 );
  1338. bBox.SetX( x1 );
  1339. bBox.SetY( y1 );
  1340. bBox.SetWidth( x2 - x1 );
  1341. bBox.SetHeight( y2 - y1 );
  1342. bBox.Offset( m_Pos );
  1343. return bBox;
  1344. }
  1345. const EDA_RECT SCH_COMPONENT::GetBoundingBox() const
  1346. {
  1347. EDA_RECT bbox = GetBodyBoundingBox();
  1348. for( size_t i = 0; i < m_Fields.size(); i++ )
  1349. {
  1350. bbox.Merge( m_Fields[i].GetBoundingBox() );
  1351. }
  1352. return bbox;
  1353. }
  1354. void SCH_COMPONENT::GetMsgPanelInfo( MSG_PANEL_ITEMS& aList )
  1355. {
  1356. // part and alias can differ if alias is not the root
  1357. if( PART_SPTR part = m_part.lock() )
  1358. {
  1359. if( part.get() != dummy() )
  1360. {
  1361. LIB_ALIAS* alias = part->GetAlias( GetLibId().GetLibItemName() );
  1362. if( !alias )
  1363. return;
  1364. if( m_currentSheetPath )
  1365. aList.push_back( MSG_PANEL_ITEM( _( "Reference" ),
  1366. GetRef( m_currentSheetPath ),
  1367. DARKCYAN ) );
  1368. wxString msg = part->IsPower() ? _( "Power symbol" ) : _( "Value" );
  1369. aList.push_back( MSG_PANEL_ITEM( msg, GetField( VALUE )->GetShownText(), DARKCYAN ) );
  1370. // Display component reference in library and library
  1371. aList.push_back( MSG_PANEL_ITEM( _( "Component" ), GetLibId().GetLibItemName(),
  1372. BROWN ) );
  1373. if( alias->GetName() != part->GetName() )
  1374. aList.push_back( MSG_PANEL_ITEM( _( "Alias of" ), part->GetName(), BROWN ) );
  1375. aList.push_back( MSG_PANEL_ITEM( _( "Library" ), alias->GetLibraryName(), BROWN ) );
  1376. // Display the current associated footprint, if exists.
  1377. if( !GetField( FOOTPRINT )->IsVoid() )
  1378. msg = GetField( FOOTPRINT )->GetShownText();
  1379. else
  1380. msg = _( "<Unknown>" );
  1381. aList.push_back( MSG_PANEL_ITEM( _( "Footprint" ), msg, DARKRED ) );
  1382. // Display description of the component, and keywords found in lib
  1383. aList.push_back( MSG_PANEL_ITEM( _( "Description" ), alias->GetDescription(),
  1384. DARKCYAN ) );
  1385. aList.push_back( MSG_PANEL_ITEM( _( "Key Words" ), alias->GetKeyWords(), DARKCYAN ) );
  1386. }
  1387. }
  1388. else
  1389. {
  1390. if( m_currentSheetPath )
  1391. aList.push_back( MSG_PANEL_ITEM( _( "Reference" ), GetRef( m_currentSheetPath ),
  1392. DARKCYAN ) );
  1393. aList.push_back( MSG_PANEL_ITEM( _( "Value" ), GetField( VALUE )->GetShownText(),
  1394. DARKCYAN ) );
  1395. aList.push_back( MSG_PANEL_ITEM( _( "Component" ), GetLibId().GetLibItemName(), BROWN ) );
  1396. aList.push_back( MSG_PANEL_ITEM( _( "Library" ), _( "Error: symbol not found!!!" ), RED ) );
  1397. }
  1398. }
  1399. BITMAP_DEF SCH_COMPONENT::GetMenuImage() const
  1400. {
  1401. return add_component_xpm;
  1402. }
  1403. void SCH_COMPONENT::MirrorY( int aYaxis_position )
  1404. {
  1405. int dx = m_Pos.x;
  1406. SetOrientation( CMP_MIRROR_Y );
  1407. MIRROR( m_Pos.x, aYaxis_position );
  1408. dx -= m_Pos.x; // dx,0 is the move vector for this transform
  1409. for( int ii = 0; ii < GetFieldCount(); ii++ )
  1410. {
  1411. // Move the fields to the new position because the component itself has moved.
  1412. wxPoint pos = GetField( ii )->GetTextPos();
  1413. pos.x -= dx;
  1414. GetField( ii )->SetTextPos( pos );
  1415. }
  1416. }
  1417. void SCH_COMPONENT::MirrorX( int aXaxis_position )
  1418. {
  1419. int dy = m_Pos.y;
  1420. SetOrientation( CMP_MIRROR_X );
  1421. MIRROR( m_Pos.y, aXaxis_position );
  1422. dy -= m_Pos.y; // dy,0 is the move vector for this transform
  1423. for( int ii = 0; ii < GetFieldCount(); ii++ )
  1424. {
  1425. // Move the fields to the new position because the component itself has moved.
  1426. wxPoint pos = GetField( ii )->GetTextPos();
  1427. pos.y -= dy;
  1428. GetField( ii )->SetTextPos( pos );
  1429. }
  1430. }
  1431. void SCH_COMPONENT::Rotate( wxPoint aPosition )
  1432. {
  1433. wxPoint prev = m_Pos;
  1434. RotatePoint( &m_Pos, aPosition, 900 );
  1435. SetOrientation( CMP_ROTATE_COUNTERCLOCKWISE );
  1436. for( int ii = 0; ii < GetFieldCount(); ii++ )
  1437. {
  1438. // Move the fields to the new position because the component itself has moved.
  1439. wxPoint pos = GetField( ii )->GetTextPos();
  1440. pos.x -= prev.x - m_Pos.x;
  1441. pos.y -= prev.y - m_Pos.y;
  1442. GetField( ii )->SetTextPos( pos );
  1443. }
  1444. }
  1445. bool SCH_COMPONENT::Matches( wxFindReplaceData& aSearchData, void* aAuxData,
  1446. wxPoint* aFindLocation )
  1447. {
  1448. wxLogTrace( traceFindItem, wxT( " item " ) + GetSelectMenuText() );
  1449. // Components are searchable via the child field and pin item text.
  1450. return false;
  1451. }
  1452. void SCH_COMPONENT::GetEndPoints( std::vector <DANGLING_END_ITEM>& aItemList )
  1453. {
  1454. if( PART_SPTR part = m_part.lock() )
  1455. {
  1456. for( LIB_PIN* pin = part->GetNextPin(); pin; pin = part->GetNextPin( pin ) )
  1457. {
  1458. wxASSERT( pin->Type() == LIB_PIN_T );
  1459. if( pin->GetUnit() && m_unit && ( m_unit != pin->GetUnit() ) )
  1460. continue;
  1461. if( pin->GetConvert() && m_convert && ( m_convert != pin->GetConvert() ) )
  1462. continue;
  1463. DANGLING_END_ITEM item( PIN_END, pin, GetPinPhysicalPosition( pin ) );
  1464. aItemList.push_back( item );
  1465. }
  1466. }
  1467. }
  1468. bool SCH_COMPONENT::IsPinDanglingStateChanged( std::vector<DANGLING_END_ITEM> &aItemList,
  1469. LIB_PINS& aLibPins, unsigned aPin )
  1470. {
  1471. bool previousState;
  1472. if( aPin < m_isDangling.size() )
  1473. {
  1474. previousState = m_isDangling[aPin];
  1475. m_isDangling[aPin] = true;
  1476. }
  1477. else
  1478. {
  1479. previousState = true;
  1480. m_isDangling.push_back( true );
  1481. }
  1482. wxPoint pin_position = GetPinPhysicalPosition( aLibPins[aPin] );
  1483. for( DANGLING_END_ITEM& each_item : aItemList )
  1484. {
  1485. // Some people like to stack pins on top of each other in a symbol to indicate
  1486. // internal connection. While technically connected, it is not particularly useful
  1487. // to display them that way, so skip any pins that are in the same symbol as this
  1488. // one.
  1489. //
  1490. // Do not make this exception for hidden pins, because those actually make internal
  1491. // connections to a power net.
  1492. const LIB_PIN* item_pin = dynamic_cast<const LIB_PIN*>( each_item.GetItem() );
  1493. if( item_pin
  1494. && ( !item_pin->IsPowerConnection() || !IsInNetlist() )
  1495. && std::find( aLibPins.begin(), aLibPins.end(), item_pin) != aLibPins.end() )
  1496. continue;
  1497. switch( each_item.GetType() )
  1498. {
  1499. case PIN_END:
  1500. case LABEL_END:
  1501. case SHEET_LABEL_END:
  1502. case WIRE_START_END:
  1503. case WIRE_END_END:
  1504. case NO_CONNECT_END:
  1505. case JUNCTION_END:
  1506. if( pin_position == each_item.GetPosition() )
  1507. m_isDangling[aPin] = false;
  1508. break;
  1509. default:
  1510. break;
  1511. }
  1512. if( !m_isDangling[aPin] )
  1513. break;
  1514. }
  1515. return previousState != m_isDangling[aPin];
  1516. }
  1517. bool SCH_COMPONENT::IsDanglingStateChanged( std::vector<DANGLING_END_ITEM>& aItemList )
  1518. {
  1519. bool changed = false;
  1520. LIB_PINS libPins;
  1521. if( PART_SPTR part = m_part.lock() )
  1522. part->GetPins( libPins, m_unit, m_convert );
  1523. for( size_t i = 0; i < libPins.size(); ++i )
  1524. {
  1525. if( IsPinDanglingStateChanged( aItemList, libPins, i ) )
  1526. changed = true;
  1527. }
  1528. return changed;
  1529. }
  1530. bool SCH_COMPONENT::IsDangling() const
  1531. {
  1532. for( bool each : m_isDangling )
  1533. {
  1534. if( each )
  1535. return true;
  1536. }
  1537. return false;
  1538. }
  1539. wxPoint SCH_COMPONENT::GetPinPhysicalPosition( LIB_PIN* Pin )
  1540. {
  1541. wxCHECK_MSG( Pin != NULL && Pin->Type() == LIB_PIN_T, wxPoint( 0, 0 ),
  1542. wxT( "Cannot get physical position of pin." ) );
  1543. return m_transform.TransformCoordinate( Pin->GetPosition() ) + m_Pos;
  1544. }
  1545. bool SCH_COMPONENT::IsSelectStateChanged( const wxRect& aRect )
  1546. {
  1547. bool previousState = IsSelected();
  1548. EDA_RECT boundingBox = GetBoundingBox();
  1549. if( aRect.Intersects( boundingBox ) )
  1550. SetFlags( SELECTED );
  1551. else
  1552. ClearFlags( SELECTED );
  1553. return previousState != IsSelected();
  1554. }
  1555. void SCH_COMPONENT::GetConnectionPoints( std::vector< wxPoint >& aPoints ) const
  1556. {
  1557. if( PART_SPTR part = m_part.lock() )
  1558. {
  1559. for( LIB_PIN* pin = part->GetNextPin(); pin; pin = part->GetNextPin( pin ) )
  1560. {
  1561. wxCHECK_RET( pin->Type() == LIB_PIN_T,
  1562. wxT( "GetNextPin() did not return a pin object. Bad programmer!" ) );
  1563. // Skip items not used for this part.
  1564. if( m_unit && pin->GetUnit() && ( pin->GetUnit() != m_unit ) )
  1565. continue;
  1566. if( m_convert && pin->GetConvert() && ( pin->GetConvert() != m_convert ) )
  1567. continue;
  1568. // Calculate the pin position relative to the component position and orientation.
  1569. aPoints.push_back( m_transform.TransformCoordinate( pin->GetPosition() ) + m_Pos );
  1570. }
  1571. }
  1572. else
  1573. {
  1574. wxCHECK_RET( 0,
  1575. wxT( "Cannot add connection points to list. Cannot find component <" ) +
  1576. GetLibId().GetLibItemName() + wxT( "> in any of the loaded libraries." ) );
  1577. }
  1578. }
  1579. LIB_ITEM* SCH_COMPONENT::GetDrawItem( const wxPoint& aPosition, KICAD_T aType )
  1580. {
  1581. if( PART_SPTR part = m_part.lock() )
  1582. {
  1583. // Calculate the position relative to the component.
  1584. wxPoint libPosition = aPosition - m_Pos;
  1585. return part->LocateDrawItem( m_unit, m_convert, aType, libPosition, m_transform );
  1586. }
  1587. return NULL;
  1588. }
  1589. wxString SCH_COMPONENT::GetSelectMenuText() const
  1590. {
  1591. wxString tmp;
  1592. tmp.Printf( _( "Component %s, %s" ),
  1593. GetChars( GetLibId().GetLibItemName() ),
  1594. GetChars( GetField( REFERENCE )->GetShownText() ) );
  1595. return tmp;
  1596. }
  1597. SEARCH_RESULT SCH_COMPONENT::Visit( INSPECTOR aInspector, void* aTestData,
  1598. const KICAD_T aFilterTypes[] )
  1599. {
  1600. KICAD_T stype;
  1601. for( const KICAD_T* p = aFilterTypes; (stype = *p) != EOT; ++p )
  1602. {
  1603. // If caller wants to inspect component type or and component children types.
  1604. if( stype == Type() )
  1605. {
  1606. if( SEARCH_QUIT == aInspector( this, aTestData ) )
  1607. return SEARCH_QUIT;
  1608. }
  1609. switch( stype )
  1610. {
  1611. case SCH_FIELD_T:
  1612. // Test the bounding boxes of fields if they are visible and not empty.
  1613. for( int ii = 0; ii < GetFieldCount(); ii++ )
  1614. {
  1615. if( SEARCH_QUIT == aInspector( GetField( ii ), (void*) this ) )
  1616. return SEARCH_QUIT;
  1617. }
  1618. break;
  1619. case SCH_FIELD_LOCATE_REFERENCE_T:
  1620. if( SEARCH_QUIT == aInspector( GetField( REFERENCE ), (void*) this ) )
  1621. return SEARCH_QUIT;
  1622. break;
  1623. case SCH_FIELD_LOCATE_VALUE_T:
  1624. if( SEARCH_QUIT == aInspector( GetField( VALUE ), (void*) this ) )
  1625. return SEARCH_QUIT;
  1626. break;
  1627. case SCH_FIELD_LOCATE_FOOTPRINT_T:
  1628. if( SEARCH_QUIT == aInspector( GetField( FOOTPRINT ), (void*) this ) )
  1629. return SEARCH_QUIT;
  1630. break;
  1631. case LIB_PIN_T:
  1632. if( PART_SPTR part = m_part.lock() )
  1633. {
  1634. LIB_PINS pins;
  1635. part->GetPins( pins, m_unit, m_convert );
  1636. for( size_t i = 0; i < pins.size(); i++ )
  1637. {
  1638. if( SEARCH_QUIT == aInspector( pins[ i ], (void*) this ) )
  1639. return SEARCH_QUIT;
  1640. }
  1641. }
  1642. break;
  1643. default:
  1644. break;
  1645. }
  1646. }
  1647. return SEARCH_CONTINUE;
  1648. }
  1649. void SCH_COMPONENT::GetNetListItem( NETLIST_OBJECT_LIST& aNetListItems,
  1650. SCH_SHEET_PATH* aSheetPath )
  1651. {
  1652. if( PART_SPTR part = m_part.lock() )
  1653. {
  1654. for( LIB_PIN* pin = part->GetNextPin(); pin; pin = part->GetNextPin( pin ) )
  1655. {
  1656. wxASSERT( pin->Type() == LIB_PIN_T );
  1657. if( pin->GetUnit() && ( pin->GetUnit() != GetUnitSelection( aSheetPath ) ) )
  1658. continue;
  1659. if( pin->GetConvert() && ( pin->GetConvert() != GetConvert() ) )
  1660. continue;
  1661. wxPoint pos = GetTransform().TransformCoordinate( pin->GetPosition() ) + m_Pos;
  1662. NETLIST_OBJECT* item = new NETLIST_OBJECT();
  1663. item->m_SheetPathInclude = *aSheetPath;
  1664. item->m_Comp = (SCH_ITEM*) pin;
  1665. item->m_SheetPath = *aSheetPath;
  1666. item->m_Type = NET_PIN;
  1667. item->m_Link = (SCH_ITEM*) this;
  1668. item->m_ElectricalPinType = pin->GetType();
  1669. item->m_PinNum = pin->GetNumber();
  1670. item->m_Label = pin->GetName();
  1671. item->m_Start = item->m_End = pos;
  1672. aNetListItems.push_back( item );
  1673. if( pin->IsPowerConnection() )
  1674. {
  1675. // There is an associated PIN_LABEL.
  1676. item = new NETLIST_OBJECT();
  1677. item->m_SheetPathInclude = *aSheetPath;
  1678. item->m_Comp = NULL;
  1679. item->m_SheetPath = *aSheetPath;
  1680. item->m_Type = NET_PINLABEL;
  1681. item->m_Label = pin->GetName();
  1682. item->m_Start = pos;
  1683. item->m_End = item->m_Start;
  1684. aNetListItems.push_back( item );
  1685. }
  1686. }
  1687. }
  1688. }
  1689. bool SCH_COMPONENT::operator <( const SCH_ITEM& aItem ) const
  1690. {
  1691. if( Type() != aItem.Type() )
  1692. return Type() < aItem.Type();
  1693. SCH_COMPONENT* component = (SCH_COMPONENT*) &aItem;
  1694. EDA_RECT rect = GetBodyBoundingBox();
  1695. if( rect.GetArea() != component->GetBodyBoundingBox().GetArea() )
  1696. return rect.GetArea() < component->GetBodyBoundingBox().GetArea();
  1697. if( m_Pos.x != component->m_Pos.x )
  1698. return m_Pos.x < component->m_Pos.x;
  1699. if( m_Pos.y != component->m_Pos.y )
  1700. return m_Pos.y < component->m_Pos.y;
  1701. return false;
  1702. }
  1703. bool SCH_COMPONENT::operator==( const SCH_COMPONENT& aComponent ) const
  1704. {
  1705. if( GetFieldCount() != aComponent.GetFieldCount() )
  1706. return false;
  1707. for( int i = VALUE; i < GetFieldCount(); i++ )
  1708. {
  1709. if( GetField( i )->GetText().Cmp( aComponent.GetField( i )->GetText() ) != 0 )
  1710. return false;
  1711. }
  1712. return true;
  1713. }
  1714. bool SCH_COMPONENT::operator!=( const SCH_COMPONENT& aComponent ) const
  1715. {
  1716. return !( *this == aComponent );
  1717. }
  1718. SCH_ITEM& SCH_COMPONENT::operator=( const SCH_ITEM& aItem )
  1719. {
  1720. wxCHECK_MSG( Type() == aItem.Type(), *this,
  1721. wxT( "Cannot assign object type " ) + aItem.GetClass() + wxT( " to type " ) +
  1722. GetClass() );
  1723. if( &aItem != this )
  1724. {
  1725. SCH_ITEM::operator=( aItem );
  1726. SCH_COMPONENT* c = (SCH_COMPONENT*) &aItem;
  1727. m_lib_id = c->m_lib_id;
  1728. m_part = c->m_part;
  1729. m_Pos = c->m_Pos;
  1730. m_unit = c->m_unit;
  1731. m_convert = c->m_convert;
  1732. m_transform = c->m_transform;
  1733. m_PathsAndReferences = c->m_PathsAndReferences;
  1734. m_Fields = c->m_Fields; // std::vector's assignment operator.
  1735. // Reparent fields after assignment to new component.
  1736. for( int ii = 0; ii < GetFieldCount(); ++ii )
  1737. {
  1738. GetField( ii )->SetParent( this );
  1739. }
  1740. }
  1741. return *this;
  1742. }
  1743. bool SCH_COMPONENT::HitTest( const wxPoint& aPosition, int aAccuracy ) const
  1744. {
  1745. EDA_RECT bBox = GetBodyBoundingBox();
  1746. bBox.Inflate( aAccuracy );
  1747. if( bBox.Contains( aPosition ) )
  1748. return true;
  1749. return false;
  1750. }
  1751. bool SCH_COMPONENT::HitTest( const EDA_RECT& aRect, bool aContained, int aAccuracy ) const
  1752. {
  1753. if( m_Flags & STRUCT_DELETED || m_Flags & SKIP_STRUCT )
  1754. return false;
  1755. EDA_RECT rect = aRect;
  1756. rect.Inflate( aAccuracy );
  1757. if( aContained )
  1758. return rect.Contains( GetBodyBoundingBox() );
  1759. return rect.Intersects( GetBodyBoundingBox() );
  1760. }
  1761. bool SCH_COMPONENT::doIsConnected( const wxPoint& aPosition ) const
  1762. {
  1763. std::vector< wxPoint > pts;
  1764. GetConnectionPoints( pts );
  1765. for( size_t i = 0; i < pts.size(); i++ )
  1766. {
  1767. if( pts[i] == aPosition )
  1768. return true;
  1769. }
  1770. return false;
  1771. }
  1772. bool SCH_COMPONENT::IsInNetlist() const
  1773. {
  1774. SCH_FIELD* rf = GetField( REFERENCE );
  1775. return ! rf->GetText().StartsWith( wxT( "#" ) );
  1776. }
  1777. void SCH_COMPONENT::Plot( PLOTTER* aPlotter )
  1778. {
  1779. TRANSFORM temp;
  1780. if( PART_SPTR part = m_part.lock() )
  1781. {
  1782. temp = GetTransform();
  1783. part->Plot( aPlotter, GetUnit(), GetConvert(), m_Pos, temp );
  1784. for( size_t i = 0; i < m_Fields.size(); i++ )
  1785. {
  1786. m_Fields[i].Plot( aPlotter );
  1787. }
  1788. }
  1789. }