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.

2013 lines
56 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
  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-2018 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 <sch_draw_panel.h>
  31. #include <gr_basic.h>
  32. #include <kicad_string.h>
  33. #include <richio.h>
  34. #include <sch_edit_frame.h>
  35. #include <plotter.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 <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 <cctype>
  53. #include <eeschema_id.h> // for MAX_UNIT_COUNT_PER_PACKAGE definition
  54. #include <trace_helpers.h>
  55. #define NULL_STRING "_NONAME_"
  56. /**
  57. * Function toUTFTildaText
  58. * convert a wxString to UTF8 and replace any control characters with a ~,
  59. * where a control character is one of the first ASCII values up to ' ' 32d.
  60. */
  61. std::string toUTFTildaText( const wxString& txt )
  62. {
  63. std::string ret = TO_UTF8( txt );
  64. for( std::string::iterator it = ret.begin(); it!=ret.end(); ++it )
  65. {
  66. if( (unsigned char) *it <= ' ' )
  67. *it = '~';
  68. }
  69. return ret;
  70. }
  71. /**
  72. * Used when a LIB_PART is not found in library
  73. * to draw a dummy shape
  74. * This component is a 400 mils square with the text ??
  75. * DEF DUMMY U 0 40 Y Y 1 0 N
  76. * F0 "U" 0 -350 60 H V
  77. * F1 "DUMMY" 0 350 60 H V
  78. * DRAW
  79. * T 0 0 0 150 0 0 0 ??
  80. * S -200 200 200 -200 0 1 0
  81. * ENDDRAW
  82. * ENDDEF
  83. */
  84. static LIB_PART* dummy()
  85. {
  86. static LIB_PART* part;
  87. if( !part )
  88. {
  89. part = new LIB_PART( wxEmptyString );
  90. LIB_RECTANGLE* square = new LIB_RECTANGLE( part );
  91. square->Move( wxPoint( -200, 200 ) );
  92. square->SetEndPosition( wxPoint( 200, -200 ) );
  93. LIB_TEXT* text = new LIB_TEXT( part );
  94. text->SetTextSize( wxSize( 150, 150 ) );
  95. text->SetText( wxString( wxT( "??" ) ) );
  96. part->AddDrawItem( square );
  97. part->AddDrawItem( text );
  98. }
  99. return part;
  100. }
  101. SCH_COMPONENT::SCH_COMPONENT( const wxPoint& aPos, SCH_ITEM* aParent ) :
  102. SCH_ITEM( aParent, SCH_COMPONENT_T )
  103. {
  104. Init( aPos );
  105. m_currentSheetPath = NULL;
  106. m_fieldsAutoplaced = AUTOPLACED_NO;
  107. }
  108. SCH_COMPONENT::SCH_COMPONENT( LIB_PART& aPart, LIB_ID aLibId, SCH_SHEET_PATH* sheet,
  109. int unit, int convert, const wxPoint& pos, bool setNewItemFlag ) :
  110. SCH_ITEM( NULL, SCH_COMPONENT_T )
  111. {
  112. Init( pos );
  113. m_unit = unit;
  114. m_convert = convert;
  115. m_lib_id = aLibId;
  116. m_part = aPart.SharedPtr();
  117. m_currentSheetPath = NULL;
  118. m_fieldsAutoplaced = AUTOPLACED_NO;
  119. SetTimeStamp( GetNewTimeStamp() );
  120. if( setNewItemFlag )
  121. m_Flags = IS_NEW | IS_MOVED;
  122. // Copy fields from the library component
  123. UpdateFields( true, true );
  124. // Update the pin locations
  125. UpdatePinCache();
  126. // Update the reference -- just the prefix for now.
  127. if( sheet )
  128. SetRef( sheet, aPart.GetReferenceField().GetText() + wxT( "?" ) );
  129. else
  130. m_prefix = aPart.GetReferenceField().GetText() + wxT( "?" );
  131. }
  132. SCH_COMPONENT::SCH_COMPONENT( const SCH_COMPONENT& aComponent ) :
  133. SCH_ITEM( aComponent )
  134. {
  135. m_currentSheetPath = NULL;
  136. m_Parent = aComponent.m_Parent;
  137. m_Pos = aComponent.m_Pos;
  138. m_unit = aComponent.m_unit;
  139. m_convert = aComponent.m_convert;
  140. m_lib_id = aComponent.m_lib_id;
  141. m_part = aComponent.m_part;
  142. m_Pins = aComponent.m_Pins;
  143. SetTimeStamp( aComponent.m_TimeStamp );
  144. m_transform = aComponent.m_transform;
  145. m_prefix = aComponent.m_prefix;
  146. m_PathsAndReferences = aComponent.m_PathsAndReferences;
  147. m_Fields = aComponent.m_Fields;
  148. // Re-parent the fields, which before this had aComponent as parent
  149. for( int i = 0; i<GetFieldCount(); ++i )
  150. {
  151. GetField( i )->SetParent( this );
  152. }
  153. m_isDangling = aComponent.m_isDangling;
  154. m_fieldsAutoplaced = aComponent.m_fieldsAutoplaced;
  155. }
  156. void SCH_COMPONENT::Init( const wxPoint& pos )
  157. {
  158. m_Pos = pos;
  159. m_unit = 1; // In multi unit chip - which unit to draw.
  160. m_convert = LIB_ITEM::LIB_CONVERT::BASE; // De Morgan Handling
  161. // The rotation/mirror transformation matrix. pos normal
  162. m_transform = TRANSFORM();
  163. // construct only the mandatory fields, which are the first 4 only.
  164. for( int i = 0; i < MANDATORY_FIELDS; ++i )
  165. {
  166. SCH_FIELD field( pos, i, this, TEMPLATE_FIELDNAME::GetDefaultFieldName( i ) );
  167. if( i == REFERENCE )
  168. field.SetLayer( LAYER_REFERENCEPART );
  169. else if( i == VALUE )
  170. field.SetLayer( LAYER_VALUEPART );
  171. // else keep LAYER_FIELDS from SCH_FIELD constructor
  172. // SCH_FIELD's implicitly created copy constructor is called in here
  173. AddField( field );
  174. }
  175. m_prefix = wxString( wxT( "U" ) );
  176. }
  177. EDA_ITEM* SCH_COMPONENT::Clone() const
  178. {
  179. return new SCH_COMPONENT( *this );
  180. }
  181. void SCH_COMPONENT::ViewGetLayers( int aLayers[], int& aCount ) const
  182. {
  183. aCount = 2;
  184. aLayers[0] = LAYER_DEVICE;
  185. aLayers[1] = LAYER_DEVICE_BACKGROUND;
  186. }
  187. void SCH_COMPONENT::SetLibId( const LIB_ID& aLibId, PART_LIBS* aLibs )
  188. {
  189. if( m_lib_id != aLibId )
  190. {
  191. m_lib_id = aLibId;
  192. SetModified();
  193. if( aLibs )
  194. Resolve( aLibs );
  195. else
  196. m_part.reset();
  197. }
  198. }
  199. void SCH_COMPONENT::SetLibId( const LIB_ID& aLibId, SYMBOL_LIB_TABLE* aSymLibTable,
  200. PART_LIB* aCacheLib )
  201. {
  202. if( m_lib_id == aLibId )
  203. return;
  204. m_lib_id = aLibId;
  205. SetModified();
  206. LIB_ALIAS* alias = nullptr;
  207. if( aSymLibTable && aSymLibTable->HasLibrary( m_lib_id.GetLibNickname() ) )
  208. alias = aSymLibTable->LoadSymbol( m_lib_id.GetLibNickname(), m_lib_id.GetLibItemName() );
  209. if( !alias && aCacheLib )
  210. alias = aCacheLib->FindAlias( m_lib_id.Format().wx_str() );
  211. if( alias && alias->GetPart() )
  212. m_part = alias->GetPart()->SharedPtr();
  213. else
  214. m_part.reset();
  215. }
  216. wxString SCH_COMPONENT::GetDescription() const
  217. {
  218. if( PART_SPTR part = m_part.lock() )
  219. {
  220. LIB_ALIAS* alias = part->GetAlias( GetLibId().GetLibItemName() );
  221. if( !alias )
  222. return wxEmptyString;
  223. return alias->GetDescription();
  224. }
  225. return wxEmptyString;
  226. }
  227. wxString SCH_COMPONENT::GetDatasheet() const
  228. {
  229. if( PART_SPTR part = m_part.lock() )
  230. {
  231. LIB_ALIAS* alias = part->GetAlias( GetLibId().GetLibItemName() );
  232. if( !alias )
  233. return wxEmptyString;
  234. return alias->GetDocFileName();
  235. }
  236. return wxEmptyString;
  237. }
  238. bool SCH_COMPONENT::Resolve( PART_LIBS* aLibs )
  239. {
  240. // I've never been happy that the actual individual PART_LIB is left up to
  241. // flimsy search path ordering. None-the-less find a part based on that design:
  242. if( LIB_PART* part = aLibs->FindLibPart( m_lib_id ) )
  243. {
  244. m_part = part->SharedPtr();
  245. return true;
  246. }
  247. return false;
  248. }
  249. bool SCH_COMPONENT::Resolve( SYMBOL_LIB_TABLE& aLibTable, PART_LIB* aCacheLib )
  250. {
  251. LIB_ALIAS* alias = nullptr;
  252. try
  253. {
  254. // LIB_TABLE_BASE::LoadSymbol() throws an IO_ERROR if the the library nickname
  255. // is not found in the table so check if the library still exists in the table
  256. // before attempting to load the symbol.
  257. if( m_lib_id.IsValid() && aLibTable.HasLibrary( m_lib_id.GetLibNickname() ) )
  258. alias = aLibTable.LoadSymbol( m_lib_id );
  259. // Fall back to cache library. This is temporary until the new schematic file
  260. // format is implemented.
  261. if( !alias && aCacheLib )
  262. alias = aCacheLib->FindAlias( m_lib_id.Format().wx_str() );
  263. if( alias && alias->GetPart() )
  264. {
  265. m_part = alias->GetPart()->SharedPtr();
  266. return true;
  267. }
  268. }
  269. catch( const IO_ERROR& )
  270. {
  271. wxLogDebug( "Cannot resolve library symbol %s", m_lib_id.Format().wx_str() );
  272. }
  273. return false;
  274. }
  275. // Helper sort function, used in SCH_COMPONENT::ResolveAll, to sort
  276. // sch component by lib_id
  277. static bool sort_by_libid( const SCH_COMPONENT* ref, SCH_COMPONENT* cmp )
  278. {
  279. if( ref->GetLibId() == cmp->GetLibId() )
  280. {
  281. if( ref->GetUnit() == cmp->GetUnit() )
  282. return ref->GetConvert() < cmp->GetConvert();
  283. return ref->GetUnit() < cmp->GetUnit();
  284. }
  285. return ref->GetLibId() < cmp->GetLibId();
  286. }
  287. void SCH_COMPONENT::ResolveAll( const SCH_COLLECTOR& aComponents, PART_LIBS* aLibs )
  288. {
  289. // Usually, many components use the same part lib.
  290. // to avoid too long calculation time the list of components is grouped
  291. // and once the lib part is found for one member of a group, it is also
  292. // set for all other members of this group
  293. std::vector<SCH_COMPONENT*> cmp_list;
  294. // build the cmp list.
  295. for( int i = 0; i < aComponents.GetCount(); ++i )
  296. {
  297. SCH_COMPONENT* cmp = dynamic_cast<SCH_COMPONENT*>( aComponents[i] );
  298. wxASSERT( cmp );
  299. if( cmp ) // cmp == NULL should not occur.
  300. cmp_list.push_back( cmp );
  301. }
  302. // sort it by lib part. Cmp will be grouped by same lib part.
  303. std::sort( cmp_list.begin(), cmp_list.end(), sort_by_libid );
  304. LIB_ID curr_libid;
  305. for( unsigned ii = 0; ii < cmp_list.size (); ++ii )
  306. {
  307. SCH_COMPONENT* cmp = cmp_list[ii];
  308. curr_libid = cmp->m_lib_id;
  309. cmp->Resolve( aLibs );
  310. cmp->UpdatePinCache();
  311. // Propagate the m_part pointer to other members using the same lib_id
  312. for( unsigned jj = ii+1; jj < cmp_list.size (); ++jj )
  313. {
  314. SCH_COMPONENT* next_cmp = cmp_list[jj];
  315. if( curr_libid != next_cmp->m_lib_id )
  316. break;
  317. next_cmp->m_part = cmp->m_part;
  318. if( ( cmp->m_unit == next_cmp->m_unit ) && ( cmp->m_convert == next_cmp->m_convert ) )
  319. // Propagate the pin cache vector as well
  320. next_cmp->m_Pins = cmp->m_Pins;
  321. else
  322. next_cmp->UpdatePinCache();
  323. ii = jj;
  324. }
  325. }
  326. }
  327. void SCH_COMPONENT::ResolveAll( const SCH_COLLECTOR& aComponents, SYMBOL_LIB_TABLE& aLibTable,
  328. PART_LIB* aCacheLib )
  329. {
  330. std::vector<SCH_COMPONENT*> cmp_list;
  331. for( int i = 0; i < aComponents.GetCount(); ++i )
  332. {
  333. SCH_COMPONENT* cmp = dynamic_cast<SCH_COMPONENT*>( aComponents[i] );
  334. wxCHECK2_MSG( cmp, continue, "Invalid SCH_COMPONENT pointer in list." );
  335. cmp_list.push_back( cmp );
  336. }
  337. // sort it by lib part. Cmp will be grouped by same lib part.
  338. std::sort( cmp_list.begin(), cmp_list.end(), sort_by_libid );
  339. LIB_ID curr_libid;
  340. for( unsigned ii = 0; ii < cmp_list.size (); ++ii )
  341. {
  342. SCH_COMPONENT* cmp = cmp_list[ii];
  343. curr_libid = cmp->m_lib_id;
  344. cmp->Resolve( aLibTable, aCacheLib );
  345. cmp->UpdatePinCache();
  346. // Propagate the m_part pointer to other members using the same lib_id
  347. for( unsigned jj = ii+1; jj < cmp_list.size (); ++jj )
  348. {
  349. SCH_COMPONENT* next_cmp = cmp_list[jj];
  350. if( curr_libid != next_cmp->m_lib_id )
  351. break;
  352. next_cmp->m_part = cmp->m_part;
  353. if( ( cmp->m_unit == next_cmp->m_unit ) && ( cmp->m_convert == next_cmp->m_convert ) )
  354. // Propagate the pin cache vector as well
  355. next_cmp->m_Pins = cmp->m_Pins;
  356. else
  357. next_cmp->UpdatePinCache();
  358. ii = jj;
  359. }
  360. }
  361. }
  362. void SCH_COMPONENT::UpdatePinCache()
  363. {
  364. if( PART_SPTR part = m_part.lock() )
  365. {
  366. m_Pins.clear();
  367. for( LIB_PIN* pin = part->GetNextPin(); pin; pin = part->GetNextPin( pin ) )
  368. {
  369. wxASSERT( pin->Type() == LIB_PIN_T );
  370. if( pin->GetUnit() && m_unit && ( m_unit != pin->GetUnit() ) )
  371. continue;
  372. if( pin->GetConvert() && m_convert && ( m_convert != pin->GetConvert() ) )
  373. continue;
  374. m_Pins.push_back( pin->GetPosition() );
  375. }
  376. }
  377. }
  378. void SCH_COMPONENT::UpdateAllPinCaches( const SCH_COLLECTOR& aComponents )
  379. {
  380. // Usually, many components use the same part lib.
  381. // to avoid too long calculation time the list of components is grouped
  382. // and once the lib part is found for one member of a group, it is also
  383. // set for all other members of this group
  384. std::vector<SCH_COMPONENT*> cmp_list;
  385. // build the cmp list.
  386. for( int i = 0; i < aComponents.GetCount(); ++i )
  387. {
  388. SCH_COMPONENT* cmp = dynamic_cast<SCH_COMPONENT*>( aComponents[i] );
  389. wxASSERT( cmp );
  390. if( cmp ) // cmp == NULL should not occur.
  391. cmp_list.push_back( cmp );
  392. }
  393. // sort it by lib part. Cmp will be grouped by same lib part.
  394. std::sort( cmp_list.begin(), cmp_list.end(), sort_by_libid );
  395. LIB_ID curr_libid;
  396. for( unsigned ii = 0; ii < cmp_list.size (); ++ii )
  397. {
  398. SCH_COMPONENT* cmp = cmp_list[ii];
  399. curr_libid = cmp->m_lib_id;
  400. cmp->UpdatePinCache();
  401. // Propagate the m_Pins vector to other members using the same lib_id
  402. for( unsigned jj = ii+1; jj < cmp_list.size (); ++jj )
  403. {
  404. SCH_COMPONENT* next_cmp = cmp_list[jj];
  405. if( ( curr_libid != next_cmp->m_lib_id )
  406. || ( cmp->m_unit != next_cmp->m_unit )
  407. || ( cmp->m_convert != next_cmp->m_convert ) )
  408. break;
  409. // Propagate the pin cache vector as well
  410. next_cmp->m_Pins = cmp->m_Pins;
  411. ii = jj;
  412. }
  413. }
  414. }
  415. void SCH_COMPONENT::SetUnit( int aUnit )
  416. {
  417. if( m_unit != aUnit )
  418. {
  419. m_unit = aUnit;
  420. SetModified();
  421. }
  422. }
  423. void SCH_COMPONENT::UpdateUnit( int aUnit )
  424. {
  425. m_unit = aUnit;
  426. }
  427. void SCH_COMPONENT::SetConvert( int aConvert )
  428. {
  429. if( m_convert != aConvert )
  430. {
  431. m_convert = aConvert;
  432. SetModified();
  433. }
  434. }
  435. void SCH_COMPONENT::SetTransform( const TRANSFORM& aTransform )
  436. {
  437. if( m_transform != aTransform )
  438. {
  439. m_transform = aTransform;
  440. SetModified();
  441. }
  442. }
  443. int SCH_COMPONENT::GetUnitCount() const
  444. {
  445. if( PART_SPTR part = m_part.lock() )
  446. {
  447. return part->GetUnitCount();
  448. }
  449. return 0;
  450. }
  451. void SCH_COMPONENT::Draw( EDA_DRAW_PANEL* aPanel, wxDC* aDC, const wxPoint& aOffset,
  452. GR_DRAWMODE aDrawMode, COLOR4D aColor,
  453. bool aDrawPinText )
  454. {
  455. auto opts = PART_DRAW_OPTIONS::Default();
  456. opts.draw_mode = aDrawMode;
  457. opts.color = aColor;
  458. opts.transform = m_transform;
  459. opts.show_pin_text = aDrawPinText;
  460. opts.draw_visible_fields = false;
  461. opts.draw_hidden_fields = false;
  462. if( PART_SPTR part = m_part.lock() )
  463. {
  464. // Draw pin targets if part is being dragged
  465. bool dragging = aPanel->GetScreen()->GetCurItem() == this && aPanel->IsMouseCaptured();
  466. if( !dragging )
  467. {
  468. opts.dangling = m_isDangling;
  469. }
  470. part->Draw( aPanel, aDC, m_Pos + aOffset, m_unit, m_convert, opts );
  471. }
  472. else // Use dummy() part if the actual cannot be found.
  473. {
  474. dummy()->Draw( aPanel, aDC, m_Pos + aOffset, 0, 0, opts );
  475. }
  476. SCH_FIELD* field = GetField( REFERENCE );
  477. if( field->IsVisible() && !field->IsMoving() )
  478. {
  479. field->Draw( aPanel, aDC, aOffset, aDrawMode );
  480. }
  481. for( int ii = VALUE; ii < GetFieldCount(); ii++ )
  482. {
  483. field = GetField( ii );
  484. if( field->IsMoving() )
  485. continue;
  486. field->Draw( aPanel, aDC, aOffset, aDrawMode );
  487. }
  488. #if 0
  489. // Only for testing purposes, draw the component bounding box
  490. {
  491. EDA_RECT boundingBox = GetBoundingBox();
  492. GRRect( aPanel->GetClipBox(), aDC, boundingBox, 0, BROWN );
  493. #if 1
  494. if( GetField( REFERENCE )->IsVisible() )
  495. {
  496. boundingBox = GetField( REFERENCE )->GetBoundingBox();
  497. GRRect( aPanel->GetClipBox(), aDC, boundingBox, 0, BROWN );
  498. }
  499. if( GetField( VALUE )->IsVisible() )
  500. {
  501. boundingBox = GetField( VALUE )->GetBoundingBox();
  502. GRRect( aPanel->GetClipBox(), aDC, boundingBox, 0, BROWN );
  503. }
  504. #endif
  505. }
  506. #endif
  507. }
  508. void SCH_COMPONENT::AddHierarchicalReference( const wxString& aPath,
  509. const wxString& aRef,
  510. int aMulti )
  511. {
  512. wxString h_path, h_ref;
  513. wxStringTokenizer tokenizer;
  514. wxString separators( wxT( " " ) );
  515. // Search for an existing path and remove it if found (should not occur)
  516. for( unsigned ii = 0; ii < m_PathsAndReferences.GetCount(); ii++ )
  517. {
  518. tokenizer.SetString( m_PathsAndReferences[ii], separators );
  519. h_path = tokenizer.GetNextToken();
  520. if( h_path.Cmp( aPath ) == 0 )
  521. {
  522. m_PathsAndReferences.RemoveAt( ii );
  523. ii--;
  524. }
  525. }
  526. h_ref = aPath + wxT( " " ) + aRef;
  527. h_ref << wxT( " " ) << aMulti;
  528. m_PathsAndReferences.Add( h_ref );
  529. }
  530. wxString SCH_COMPONENT::GetPath( const SCH_SHEET_PATH* sheet ) const
  531. {
  532. wxCHECK_MSG( sheet != NULL, wxEmptyString,
  533. wxT( "Cannot get component path with invalid sheet object." ) );
  534. wxString str;
  535. str.Printf( wxT( "%8.8lX" ), (long unsigned) m_TimeStamp );
  536. return sheet->Path() + str;
  537. }
  538. const wxString SCH_COMPONENT::GetRef( const SCH_SHEET_PATH* sheet )
  539. {
  540. wxString path = GetPath( sheet );
  541. wxString h_path, h_ref;
  542. wxStringTokenizer tokenizer;
  543. wxString separators( wxT( " " ) );
  544. for( unsigned ii = 0; ii < m_PathsAndReferences.GetCount(); ii++ )
  545. {
  546. tokenizer.SetString( m_PathsAndReferences[ii], separators );
  547. h_path = tokenizer.GetNextToken();
  548. if( h_path.Cmp( path ) == 0 )
  549. {
  550. h_ref = tokenizer.GetNextToken();
  551. /* printf( "GetRef hpath: %s\n",
  552. * TO_UTF8( m_PathsAndReferences[ii] ) ); */
  553. return h_ref;
  554. }
  555. }
  556. // if it was not found in m_Paths array, then see if it is in
  557. // m_Field[REFERENCE] -- if so, use this as a default for this path.
  558. // this will happen if we load a version 1 schematic file.
  559. // it will also mean that multiple instances of the same sheet by default
  560. // all have the same component references, but perhaps this is best.
  561. if( !GetField( REFERENCE )->GetText().IsEmpty() )
  562. {
  563. SetRef( sheet, GetField( REFERENCE )->GetText() );
  564. return GetField( REFERENCE )->GetText();
  565. }
  566. return m_prefix;
  567. }
  568. bool SCH_COMPONENT::IsReferenceStringValid( const wxString& aReferenceString )
  569. {
  570. wxString text = aReferenceString;
  571. bool ok = true;
  572. // Try to unannotate this reference
  573. while( !text.IsEmpty() && ( text.Last() == '?' || wxIsdigit( text.Last() ) ) )
  574. text.RemoveLast();
  575. if( text.IsEmpty() )
  576. ok = false;
  577. // Add here other constraints
  578. // Currently:no other constraint
  579. return ok;
  580. }
  581. void SCH_COMPONENT::SetRef( const SCH_SHEET_PATH* sheet, const wxString& ref )
  582. {
  583. wxString path = GetPath( sheet );
  584. bool notInArray = true;
  585. wxString h_path, h_ref;
  586. wxStringTokenizer tokenizer;
  587. wxString separators( wxT( " " ) );
  588. // check to see if it is already there before inserting it
  589. for( unsigned ii = 0; ii < m_PathsAndReferences.GetCount(); ii++ )
  590. {
  591. tokenizer.SetString( m_PathsAndReferences[ii], separators );
  592. h_path = tokenizer.GetNextToken();
  593. if( h_path.Cmp( path ) == 0 )
  594. {
  595. // just update the reference text, not the timestamp.
  596. h_ref = h_path + wxT( " " ) + ref;
  597. h_ref += wxT( " " );
  598. tokenizer.GetNextToken(); // Skip old reference
  599. h_ref += tokenizer.GetNextToken(); // Add part selection
  600. // Add the part selection
  601. m_PathsAndReferences[ii] = h_ref;
  602. notInArray = false;
  603. }
  604. }
  605. if( notInArray )
  606. AddHierarchicalReference( path, ref, m_unit );
  607. SCH_FIELD* rf = GetField( REFERENCE );
  608. if( rf->GetText().IsEmpty()
  609. || ( abs( rf->GetTextPos().x - m_Pos.x ) +
  610. abs( rf->GetTextPos().y - m_Pos.y ) > 10000 ) )
  611. {
  612. // move it to a reasonable position
  613. rf->SetTextPos( m_Pos + wxPoint( 50, 50 ) );
  614. }
  615. rf->SetText( ref ); // for drawing.
  616. // Reinit the m_prefix member if needed
  617. wxString prefix = ref;
  618. if( IsReferenceStringValid( prefix ) )
  619. {
  620. while( prefix.Last() == '?' || wxIsdigit( prefix.Last() ) )
  621. prefix.RemoveLast();
  622. }
  623. else
  624. {
  625. prefix = wxT( "U" ); // Set to default ref prefix
  626. }
  627. if( m_prefix != prefix )
  628. m_prefix = prefix;
  629. }
  630. void SCH_COMPONENT::SetTimeStamp( timestamp_t aNewTimeStamp )
  631. {
  632. wxString string_timestamp, string_oldtimestamp;
  633. string_timestamp.Printf( wxT( "%08lX" ), (long unsigned) aNewTimeStamp );
  634. string_oldtimestamp.Printf( wxT( "%08lX" ), (long unsigned) m_TimeStamp );
  635. EDA_ITEM::SetTimeStamp( aNewTimeStamp );
  636. for( unsigned ii = 0; ii < m_PathsAndReferences.GetCount(); ii++ )
  637. {
  638. m_PathsAndReferences[ii].Replace( string_oldtimestamp.GetData(),
  639. string_timestamp.GetData() );
  640. }
  641. }
  642. int SCH_COMPONENT::GetUnitSelection( SCH_SHEET_PATH* aSheet )
  643. {
  644. wxString path = GetPath( aSheet );
  645. wxString h_path, h_multi;
  646. wxStringTokenizer tokenizer;
  647. wxString separators( wxT( " " ) );
  648. for( unsigned ii = 0; ii < m_PathsAndReferences.GetCount(); ii++ )
  649. {
  650. tokenizer.SetString( m_PathsAndReferences[ii], separators );
  651. h_path = tokenizer.GetNextToken();
  652. if( h_path.Cmp( path ) == 0 )
  653. {
  654. tokenizer.GetNextToken(); // Skip reference
  655. h_multi = tokenizer.GetNextToken();
  656. long imulti = 1;
  657. h_multi.ToLong( &imulti );
  658. return imulti;
  659. }
  660. }
  661. // if it was not found in m_Paths array, then use m_unit.
  662. // this will happen if we load a version 1 schematic file.
  663. return m_unit;
  664. }
  665. void SCH_COMPONENT::SetUnitSelection( SCH_SHEET_PATH* aSheet, int aUnitSelection )
  666. {
  667. wxString path = GetPath( aSheet );
  668. bool notInArray = true;
  669. wxString h_path, h_ref;
  670. wxStringTokenizer tokenizer;
  671. wxString separators( wxT( " " ) );
  672. //check to see if it is already there before inserting it
  673. for( unsigned ii = 0; ii < m_PathsAndReferences.GetCount(); ii++ )
  674. {
  675. tokenizer.SetString( m_PathsAndReferences[ii], separators );
  676. h_path = tokenizer.GetNextToken();
  677. if( h_path.Cmp( path ) == 0 )
  678. {
  679. //just update the unit selection.
  680. h_ref = h_path + wxT( " " );
  681. h_ref += tokenizer.GetNextToken(); // Add reference
  682. h_ref += wxT( " " );
  683. h_ref << aUnitSelection; // Add part selection
  684. // Ann the part selection
  685. m_PathsAndReferences[ii] = h_ref;
  686. notInArray = false;
  687. }
  688. }
  689. if( notInArray )
  690. AddHierarchicalReference( path, m_prefix, aUnitSelection );
  691. }
  692. SCH_FIELD* SCH_COMPONENT::GetField( int aFieldNdx ) const
  693. {
  694. const SCH_FIELD* field;
  695. if( (unsigned) aFieldNdx < m_Fields.size() )
  696. field = &m_Fields[aFieldNdx];
  697. else
  698. field = NULL;
  699. wxASSERT( field );
  700. // use cast to remove const-ness
  701. return (SCH_FIELD*) field;
  702. }
  703. wxString SCH_COMPONENT::GetFieldText( const wxString& aFieldName, SCH_EDIT_FRAME* aFrame ) const
  704. {
  705. for( unsigned int ii = 0; ii < m_Fields.size(); ii++ )
  706. {
  707. if( aFieldName == m_Fields[ii].GetName() )
  708. return m_Fields[ii].GetText();
  709. }
  710. return wxEmptyString;
  711. }
  712. void SCH_COMPONENT::GetFields( std::vector<SCH_FIELD*>& aVector, bool aVisibleOnly )
  713. {
  714. for( SCH_FIELD& each_field : m_Fields )
  715. {
  716. if( !aVisibleOnly || ( each_field.IsVisible() && !each_field.IsVoid() ) )
  717. aVector.push_back( &each_field );
  718. }
  719. }
  720. SCH_FIELD* SCH_COMPONENT::AddField( const SCH_FIELD& aField )
  721. {
  722. int newNdx = m_Fields.size();
  723. m_Fields.push_back( aField );
  724. return &m_Fields[newNdx];
  725. }
  726. void SCH_COMPONENT::RemoveField( const wxString& aFieldName )
  727. {
  728. for( unsigned i = MANDATORY_FIELDS; i < m_Fields.size(); ++i )
  729. {
  730. if( aFieldName == m_Fields[i].GetName( false ) )
  731. {
  732. m_Fields.erase( m_Fields.begin() + i );
  733. return;
  734. }
  735. }
  736. }
  737. SCH_FIELD* SCH_COMPONENT::FindField( const wxString& aFieldName, bool aIncludeDefaultFields )
  738. {
  739. unsigned start = aIncludeDefaultFields ? 0 : MANDATORY_FIELDS;
  740. for( unsigned i = start; i<m_Fields.size(); ++i )
  741. {
  742. if( aFieldName == m_Fields[i].GetName( false ) )
  743. {
  744. return &m_Fields[i];
  745. }
  746. }
  747. return NULL;
  748. }
  749. void SCH_COMPONENT::UpdateFields( bool aResetStyle, bool aResetRef )
  750. {
  751. if( PART_SPTR part = m_part.lock() )
  752. {
  753. LIB_FIELDS fields;
  754. part->GetFields( fields );
  755. for( const LIB_FIELD& field : fields )
  756. {
  757. // Can no longer insert an empty name, since names are now keys. The
  758. // field index is not used beyond the first MANDATORY_FIELDS
  759. if( field.GetName().IsEmpty() )
  760. continue;
  761. // See if field already exists (mandatory fields always exist).
  762. // for mandatory fields, the name and field id are fixed, so we use the
  763. // known and fixed id to get them (more reliable than names, which can be translated)
  764. // for other fields (custom fields), locate the field by same name
  765. // (field id has no known meaning for custom fields)
  766. int idx = field.GetId();
  767. SCH_FIELD* schField;
  768. if( idx == REFERENCE && !aResetRef )
  769. continue;
  770. if( (unsigned) idx < MANDATORY_FIELDS )
  771. schField = GetField( idx );
  772. else
  773. schField = FindField( field.GetName() );
  774. if( !schField )
  775. {
  776. SCH_FIELD newField( wxPoint( 0, 0 ), GetFieldCount(), this, field.GetName() );
  777. schField = AddField( newField );
  778. }
  779. if( aResetStyle )
  780. {
  781. schField->ImportValues( field );
  782. schField->SetTextPos( m_Pos + field.GetTextPos() );
  783. }
  784. if( idx == VALUE )
  785. schField->SetText( m_lib_id.GetLibItemName() ); // fetch alias-specific value
  786. else if( idx == DATASHEET )
  787. schField->SetText( GetDatasheet() ); // fetch alias-specific value
  788. else
  789. schField->SetText( field.GetText() );
  790. }
  791. }
  792. }
  793. LIB_PIN* SCH_COMPONENT::GetPin( const wxString& number )
  794. {
  795. if( PART_SPTR part = m_part.lock() )
  796. {
  797. return part->GetPin( number, m_unit, m_convert );
  798. }
  799. return NULL;
  800. }
  801. void SCH_COMPONENT::GetPins( std::vector<LIB_PIN*>& aPinsList )
  802. {
  803. if( m_part.expired() )
  804. {
  805. // no pins; nothing to get
  806. }
  807. else if( PART_SPTR part = m_part.lock() )
  808. {
  809. part->GetPins( aPinsList, m_unit, m_convert );
  810. }
  811. else
  812. wxFAIL_MSG( "Could not obtain PART_SPTR lock" );
  813. }
  814. void SCH_COMPONENT::SwapData( SCH_ITEM* aItem )
  815. {
  816. wxCHECK_RET( (aItem != NULL) && (aItem->Type() == SCH_COMPONENT_T),
  817. wxT( "Cannot swap data with invalid component." ) );
  818. SCH_COMPONENT* component = (SCH_COMPONENT*) aItem;
  819. std::swap( m_lib_id, component->m_lib_id );
  820. std::swap( m_part, component->m_part );
  821. std::swap( m_Pos, component->m_Pos );
  822. std::swap( m_unit, component->m_unit );
  823. std::swap( m_convert, component->m_convert );
  824. std::swap( m_Pins, component->m_Pins );
  825. std::swap( m_isDangling, component->m_isDangling );
  826. TRANSFORM tmp = m_transform;
  827. m_transform = component->m_transform;
  828. component->m_transform = tmp;
  829. m_Fields.swap( component->m_Fields ); // std::vector's swap()
  830. // Reparent items after copying data
  831. // (after swap(), m_Parent member does not point to the right parent):
  832. for( int ii = 0; ii < component->GetFieldCount(); ++ii )
  833. {
  834. component->GetField( ii )->SetParent( component );
  835. }
  836. for( int ii = 0; ii < GetFieldCount(); ++ii )
  837. {
  838. GetField( ii )->SetParent( this );
  839. }
  840. std::swap( m_PathsAndReferences, component->m_PathsAndReferences );
  841. }
  842. void SCH_COMPONENT::ClearAnnotation( SCH_SHEET_PATH* aSheetPath )
  843. {
  844. wxArrayString reference_fields;
  845. static const wxChar separators[] = wxT( " " );
  846. PART_SPTR part = m_part.lock();
  847. // Build a reference with no annotation,
  848. // i.e. a reference ended by only one '?'
  849. wxString defRef = m_prefix;
  850. if( !IsReferenceStringValid( defRef ) )
  851. { // This is a malformed reference: reinit this reference
  852. m_prefix = defRef = wxT("U"); // Set to default ref prefix
  853. }
  854. while( defRef.Last() == '?' )
  855. defRef.RemoveLast();
  856. defRef.Append( wxT( "?" ) );
  857. wxString path;
  858. if( aSheetPath )
  859. path = GetPath( aSheetPath );
  860. for( unsigned int ii = 0; ii < m_PathsAndReferences.GetCount(); ii++ )
  861. {
  862. // Break hierarchical reference in path, ref and multi selection:
  863. reference_fields = wxStringTokenize( m_PathsAndReferences[ii], separators );
  864. // For all components: if aSheetPath is not NULL,
  865. // remove annotation only for the given path
  866. if( aSheetPath == NULL || reference_fields[0].Cmp( path ) == 0 )
  867. {
  868. wxString NewHref = reference_fields[0];
  869. NewHref << wxT( " " ) << defRef << wxT( " " ) << reference_fields[2];
  870. m_PathsAndReferences[ii] = NewHref;
  871. }
  872. }
  873. // These 2 changes do not work in complex hierarchy.
  874. // When a clear annotation is made, the calling function must call a
  875. // UpdateAllScreenReferences for the active sheet.
  876. // But this call cannot made here.
  877. m_Fields[REFERENCE].SetText( defRef ); //for drawing.
  878. SetModified();
  879. }
  880. bool SCH_COMPONENT::AddSheetPathReferenceEntryIfMissing( const wxString& aSheetPathName )
  881. {
  882. // a empty sheet path is illegal:
  883. wxCHECK( !aSheetPathName.IsEmpty(), false );
  884. wxString reference_path;
  885. // The full component reference path is aSheetPathName + the component time stamp itself
  886. // full_AR_path is the alternate reference path to search
  887. wxString full_AR_path = aSheetPathName
  888. + wxString::Format( "%8.8lX", (unsigned long) GetTimeStamp() );
  889. for( unsigned int ii = 0; ii < m_PathsAndReferences.GetCount(); ii++ )
  890. {
  891. // Break hierarchical reference in path, ref and multi selection:
  892. reference_path = m_PathsAndReferences[ii].BeforeFirst( ' ' );
  893. // if aSheetPath is found, nothing to do:
  894. if( reference_path.Cmp( full_AR_path ) == 0 )
  895. return false;
  896. }
  897. // This entry does not exist: add it, with a (temporary?) reference (last ref used for display)
  898. AddHierarchicalReference( full_AR_path, m_Fields[REFERENCE].GetText(), m_unit );
  899. return true;
  900. }
  901. void SCH_COMPONENT::SetOrientation( int aOrientation )
  902. {
  903. TRANSFORM temp = TRANSFORM();
  904. bool transform = false;
  905. switch( aOrientation )
  906. {
  907. case CMP_ORIENT_0:
  908. case CMP_NORMAL: // default transform matrix
  909. m_transform.x1 = 1;
  910. m_transform.y2 = -1;
  911. m_transform.x2 = m_transform.y1 = 0;
  912. break;
  913. case CMP_ROTATE_COUNTERCLOCKWISE: // Rotate + (incremental rotation)
  914. temp.x1 = temp.y2 = 0;
  915. temp.y1 = 1;
  916. temp.x2 = -1;
  917. transform = true;
  918. break;
  919. case CMP_ROTATE_CLOCKWISE: // Rotate - (incremental rotation)
  920. temp.x1 = temp.y2 = 0;
  921. temp.y1 = -1;
  922. temp.x2 = 1;
  923. transform = true;
  924. break;
  925. case CMP_MIRROR_Y: // Mirror Y (incremental rotation)
  926. temp.x1 = -1;
  927. temp.y2 = 1;
  928. temp.y1 = temp.x2 = 0;
  929. transform = true;
  930. break;
  931. case CMP_MIRROR_X: // Mirror X (incremental rotation)
  932. temp.x1 = 1;
  933. temp.y2 = -1;
  934. temp.y1 = temp.x2 = 0;
  935. transform = true;
  936. break;
  937. case CMP_ORIENT_90:
  938. SetOrientation( CMP_ORIENT_0 );
  939. SetOrientation( CMP_ROTATE_COUNTERCLOCKWISE );
  940. break;
  941. case CMP_ORIENT_180:
  942. SetOrientation( CMP_ORIENT_0 );
  943. SetOrientation( CMP_ROTATE_COUNTERCLOCKWISE );
  944. SetOrientation( CMP_ROTATE_COUNTERCLOCKWISE );
  945. break;
  946. case CMP_ORIENT_270:
  947. SetOrientation( CMP_ORIENT_0 );
  948. SetOrientation( CMP_ROTATE_CLOCKWISE );
  949. break;
  950. case ( CMP_ORIENT_0 + CMP_MIRROR_X ):
  951. SetOrientation( CMP_ORIENT_0 );
  952. SetOrientation( CMP_MIRROR_X );
  953. break;
  954. case ( CMP_ORIENT_0 + CMP_MIRROR_Y ):
  955. SetOrientation( CMP_ORIENT_0 );
  956. SetOrientation( CMP_MIRROR_Y );
  957. break;
  958. case ( CMP_ORIENT_90 + CMP_MIRROR_X ):
  959. SetOrientation( CMP_ORIENT_90 );
  960. SetOrientation( CMP_MIRROR_X );
  961. break;
  962. case ( CMP_ORIENT_90 + CMP_MIRROR_Y ):
  963. SetOrientation( CMP_ORIENT_90 );
  964. SetOrientation( CMP_MIRROR_Y );
  965. break;
  966. case ( CMP_ORIENT_180 + CMP_MIRROR_X ):
  967. SetOrientation( CMP_ORIENT_180 );
  968. SetOrientation( CMP_MIRROR_X );
  969. break;
  970. case ( CMP_ORIENT_180 + CMP_MIRROR_Y ):
  971. SetOrientation( CMP_ORIENT_180 );
  972. SetOrientation( CMP_MIRROR_Y );
  973. break;
  974. case ( CMP_ORIENT_270 + CMP_MIRROR_X ):
  975. SetOrientation( CMP_ORIENT_270 );
  976. SetOrientation( CMP_MIRROR_X );
  977. break;
  978. case ( CMP_ORIENT_270 + CMP_MIRROR_Y ):
  979. SetOrientation( CMP_ORIENT_270 );
  980. SetOrientation( CMP_MIRROR_Y );
  981. break;
  982. default:
  983. transform = false;
  984. wxMessageBox( wxT( "SetRotateMiroir() error: ill value" ) );
  985. break;
  986. }
  987. if( transform )
  988. {
  989. /* The new matrix transform is the old matrix transform modified by the
  990. * requested transformation, which is the temp transform (rot,
  991. * mirror ..) in order to have (in term of matrix transform):
  992. * transform coord = new_m_transform * coord
  993. * where transform coord is the coord modified by new_m_transform from
  994. * the initial value coord.
  995. * new_m_transform is computed (from old_m_transform and temp) to
  996. * have:
  997. * transform coord = old_m_transform * temp
  998. */
  999. TRANSFORM newTransform;
  1000. newTransform.x1 = m_transform.x1 * temp.x1 + m_transform.x2 * temp.y1;
  1001. newTransform.y1 = m_transform.y1 * temp.x1 + m_transform.y2 * temp.y1;
  1002. newTransform.x2 = m_transform.x1 * temp.x2 + m_transform.x2 * temp.y2;
  1003. newTransform.y2 = m_transform.y1 * temp.x2 + m_transform.y2 * temp.y2;
  1004. m_transform = newTransform;
  1005. }
  1006. }
  1007. int SCH_COMPONENT::GetOrientation()
  1008. {
  1009. int rotate_values[] =
  1010. {
  1011. CMP_ORIENT_0,
  1012. CMP_ORIENT_90,
  1013. CMP_ORIENT_180,
  1014. CMP_ORIENT_270,
  1015. CMP_MIRROR_X + CMP_ORIENT_0,
  1016. CMP_MIRROR_X + CMP_ORIENT_90,
  1017. CMP_MIRROR_X + CMP_ORIENT_270,
  1018. CMP_MIRROR_Y,
  1019. CMP_MIRROR_Y + CMP_ORIENT_0,
  1020. CMP_MIRROR_Y + CMP_ORIENT_90,
  1021. CMP_MIRROR_Y + CMP_ORIENT_180,
  1022. CMP_MIRROR_Y + CMP_ORIENT_270
  1023. };
  1024. // Try to find the current transform option:
  1025. TRANSFORM transform = m_transform;
  1026. for( int type_rotate : rotate_values )
  1027. {
  1028. SetOrientation( type_rotate );
  1029. if( transform == m_transform )
  1030. return type_rotate;
  1031. }
  1032. // Error: orientation not found in list (should not happen)
  1033. wxMessageBox( wxT( "Component orientation matrix internal error" ) );
  1034. m_transform = transform;
  1035. return CMP_NORMAL;
  1036. }
  1037. wxPoint SCH_COMPONENT::GetScreenCoord( const wxPoint& aPoint )
  1038. {
  1039. return m_transform.TransformCoordinate( aPoint );
  1040. }
  1041. #if defined(DEBUG)
  1042. void SCH_COMPONENT::Show( int nestLevel, std::ostream& os ) const
  1043. {
  1044. // for now, make it look like XML:
  1045. NestedSpace( nestLevel, os ) << '<' << GetClass().Lower().mb_str()
  1046. << " ref=\"" << TO_UTF8( GetField( 0 )->GetName() )
  1047. << '"' << " chipName=\""
  1048. << GetLibId().Format() << '"' << m_Pos
  1049. << " layer=\"" << m_Layer
  1050. << '"' << ">\n";
  1051. // skip the reference, it's been output already.
  1052. for( int i = 1; i < GetFieldCount(); ++i )
  1053. {
  1054. wxString value = GetField( i )->GetText();
  1055. if( !value.IsEmpty() )
  1056. {
  1057. NestedSpace( nestLevel + 1, os ) << "<field" << " name=\""
  1058. << TO_UTF8( GetField( i )->GetName() )
  1059. << '"' << " value=\""
  1060. << TO_UTF8( value ) << "\"/>\n";
  1061. }
  1062. }
  1063. NestedSpace( nestLevel, os ) << "</" << TO_UTF8( GetClass().Lower() ) << ">\n";
  1064. }
  1065. #endif
  1066. EDA_RECT SCH_COMPONENT::GetBodyBoundingBox() const
  1067. {
  1068. EDA_RECT bBox;
  1069. if( PART_SPTR part = m_part.lock() )
  1070. {
  1071. bBox = part->GetBodyBoundingBox( m_unit, m_convert );
  1072. }
  1073. else
  1074. {
  1075. bBox = dummy()->GetBodyBoundingBox( m_unit, m_convert );
  1076. }
  1077. int x0 = bBox.GetX();
  1078. int xm = bBox.GetRight();
  1079. // We must reverse Y values, because matrix orientation
  1080. // suppose Y axis normal for the library items coordinates,
  1081. // m_transform reverse Y values, but bBox is already reversed!
  1082. int y0 = -bBox.GetY();
  1083. int ym = -bBox.GetBottom();
  1084. // Compute the real Boundary box (rotated, mirrored ...)
  1085. int x1 = m_transform.x1 * x0 + m_transform.y1 * y0;
  1086. int y1 = m_transform.x2 * x0 + m_transform.y2 * y0;
  1087. int x2 = m_transform.x1 * xm + m_transform.y1 * ym;
  1088. int y2 = m_transform.x2 * xm + m_transform.y2 * ym;
  1089. // H and W must be > 0:
  1090. if( x2 < x1 )
  1091. std::swap( x2, x1 );
  1092. if( y2 < y1 )
  1093. std::swap( y2, y1 );
  1094. bBox.SetX( x1 );
  1095. bBox.SetY( y1 );
  1096. bBox.SetWidth( x2 - x1 );
  1097. bBox.SetHeight( y2 - y1 );
  1098. bBox.Offset( m_Pos );
  1099. return bBox;
  1100. }
  1101. const EDA_RECT SCH_COMPONENT::GetBoundingBox() const
  1102. {
  1103. EDA_RECT bbox = GetBodyBoundingBox();
  1104. for( size_t i = 0; i < m_Fields.size(); i++ )
  1105. {
  1106. bbox.Merge( m_Fields[i].GetBoundingBox() );
  1107. }
  1108. return bbox;
  1109. }
  1110. void SCH_COMPONENT::GetMsgPanelInfo( EDA_UNITS_T aUnits, MSG_PANEL_ITEMS& aList )
  1111. {
  1112. wxString msg;
  1113. // part and alias can differ if alias is not the root
  1114. if( PART_SPTR part = m_part.lock() )
  1115. {
  1116. if( part.get() != dummy() )
  1117. {
  1118. LIB_ALIAS* alias = nullptr;
  1119. if( part->GetLib() && part->GetLib()->IsCache() )
  1120. alias = part->GetAlias( GetLibId().Format() );
  1121. else
  1122. alias = part->GetAlias( GetLibId().GetLibItemName() );
  1123. if( !alias )
  1124. return;
  1125. if( m_currentSheetPath )
  1126. aList.push_back( MSG_PANEL_ITEM( _( "Reference" ),
  1127. GetRef( m_currentSheetPath ),
  1128. DARKCYAN ) );
  1129. msg = part->IsPower() ? _( "Power symbol" ) : _( "Value" );
  1130. aList.push_back( MSG_PANEL_ITEM( msg, GetField( VALUE )->GetShownText(), DARKCYAN ) );
  1131. // Display component reference in library and library
  1132. aList.push_back( MSG_PANEL_ITEM( _( "Name" ), GetLibId().GetLibItemName(),
  1133. BROWN ) );
  1134. if( alias->GetName() != part->GetName() )
  1135. aList.push_back( MSG_PANEL_ITEM( _( "Alias of" ), part->GetName(), BROWN ) );
  1136. if( alias->GetLib() && alias->GetLib()->IsCache() )
  1137. aList.push_back( MSG_PANEL_ITEM( _( "Library" ), alias->GetLibNickname(), RED ) );
  1138. else if( !m_lib_id.GetLibNickname().empty() )
  1139. aList.push_back( MSG_PANEL_ITEM( _( "Library" ), m_lib_id.GetLibNickname(),
  1140. BROWN ) );
  1141. else
  1142. aList.push_back( MSG_PANEL_ITEM( _( "Library" ), _( "Undefined!!!" ), RED ) );
  1143. // Display the current associated footprint, if exists.
  1144. if( !GetField( FOOTPRINT )->IsVoid() )
  1145. msg = GetField( FOOTPRINT )->GetShownText();
  1146. else
  1147. msg = _( "<Unknown>" );
  1148. aList.push_back( MSG_PANEL_ITEM( _( "Footprint" ), msg, DARKRED ) );
  1149. // Display description of the component, and keywords found in lib
  1150. aList.push_back( MSG_PANEL_ITEM( _( "Description" ), alias->GetDescription(),
  1151. DARKCYAN ) );
  1152. aList.push_back( MSG_PANEL_ITEM( _( "Key words" ), alias->GetKeyWords(), DARKCYAN ) );
  1153. }
  1154. }
  1155. else
  1156. {
  1157. if( m_currentSheetPath )
  1158. aList.push_back( MSG_PANEL_ITEM( _( "Reference" ), GetRef( m_currentSheetPath ),
  1159. DARKCYAN ) );
  1160. aList.push_back( MSG_PANEL_ITEM( _( "Value" ), GetField( VALUE )->GetShownText(),
  1161. DARKCYAN ) );
  1162. aList.push_back( MSG_PANEL_ITEM( _( "Name" ), GetLibId().GetLibItemName(), BROWN ) );
  1163. wxString libNickname = GetLibId().GetLibNickname();
  1164. if( libNickname.empty() )
  1165. {
  1166. aList.push_back( MSG_PANEL_ITEM( _( "Library" ),
  1167. _( "No library defined!!!" ), RED ) );
  1168. }
  1169. else
  1170. {
  1171. msg.Printf( _( "Symbol not found in %s!!!" ), libNickname );
  1172. aList.push_back( MSG_PANEL_ITEM( _( "Library" ), msg , RED ) );
  1173. }
  1174. }
  1175. }
  1176. BITMAP_DEF SCH_COMPONENT::GetMenuImage() const
  1177. {
  1178. return add_component_xpm;
  1179. }
  1180. void SCH_COMPONENT::MirrorY( int aYaxis_position )
  1181. {
  1182. int dx = m_Pos.x;
  1183. SetOrientation( CMP_MIRROR_Y );
  1184. MIRROR( m_Pos.x, aYaxis_position );
  1185. dx -= m_Pos.x; // dx,0 is the move vector for this transform
  1186. for( int ii = 0; ii < GetFieldCount(); ii++ )
  1187. {
  1188. // Move the fields to the new position because the component itself has moved.
  1189. wxPoint pos = GetField( ii )->GetTextPos();
  1190. pos.x -= dx;
  1191. GetField( ii )->SetTextPos( pos );
  1192. }
  1193. }
  1194. void SCH_COMPONENT::MirrorX( int aXaxis_position )
  1195. {
  1196. int dy = m_Pos.y;
  1197. SetOrientation( CMP_MIRROR_X );
  1198. MIRROR( m_Pos.y, aXaxis_position );
  1199. dy -= m_Pos.y; // dy,0 is the move vector for this transform
  1200. for( int ii = 0; ii < GetFieldCount(); ii++ )
  1201. {
  1202. // Move the fields to the new position because the component itself has moved.
  1203. wxPoint pos = GetField( ii )->GetTextPos();
  1204. pos.y -= dy;
  1205. GetField( ii )->SetTextPos( pos );
  1206. }
  1207. }
  1208. void SCH_COMPONENT::Rotate( wxPoint aPosition )
  1209. {
  1210. wxPoint prev = m_Pos;
  1211. RotatePoint( &m_Pos, aPosition, 900 );
  1212. SetOrientation( CMP_ROTATE_COUNTERCLOCKWISE );
  1213. for( int ii = 0; ii < GetFieldCount(); ii++ )
  1214. {
  1215. // Move the fields to the new position because the component itself has moved.
  1216. wxPoint pos = GetField( ii )->GetTextPos();
  1217. pos.x -= prev.x - m_Pos.x;
  1218. pos.y -= prev.y - m_Pos.y;
  1219. GetField( ii )->SetTextPos( pos );
  1220. }
  1221. }
  1222. bool SCH_COMPONENT::Matches( wxFindReplaceData& aSearchData, void* aAuxData,
  1223. wxPoint* aFindLocation )
  1224. {
  1225. wxLogTrace( traceFindItem, wxT( " item " ) + GetSelectMenuText( MILLIMETRES ) );
  1226. // Components are searchable via the child field and pin item text.
  1227. return false;
  1228. }
  1229. void SCH_COMPONENT::GetEndPoints( std::vector <DANGLING_END_ITEM>& aItemList )
  1230. {
  1231. if( PART_SPTR part = m_part.lock() )
  1232. {
  1233. for( LIB_PIN* pin = part->GetNextPin(); pin; pin = part->GetNextPin( pin ) )
  1234. {
  1235. wxASSERT( pin->Type() == LIB_PIN_T );
  1236. if( pin->GetUnit() && m_unit && ( m_unit != pin->GetUnit() ) )
  1237. continue;
  1238. if( pin->GetConvert() && m_convert && ( m_convert != pin->GetConvert() ) )
  1239. continue;
  1240. DANGLING_END_ITEM item( PIN_END, pin, GetPinPhysicalPosition( pin ), this );
  1241. aItemList.push_back( item );
  1242. }
  1243. }
  1244. }
  1245. bool SCH_COMPONENT::UpdateDanglingState( std::vector<DANGLING_END_ITEM>& aItemList )
  1246. {
  1247. bool changed = false;
  1248. for( size_t i = 0; i < m_Pins.size(); ++i )
  1249. {
  1250. bool previousState;
  1251. wxPoint pos = m_transform.TransformCoordinate( m_Pins[ i ] ) + m_Pos;
  1252. if( i < m_isDangling.size() )
  1253. {
  1254. previousState = m_isDangling[ i ];
  1255. m_isDangling[ i ] = true;
  1256. }
  1257. else
  1258. {
  1259. previousState = true;
  1260. m_isDangling.push_back( true );
  1261. }
  1262. for( DANGLING_END_ITEM& each_item : aItemList )
  1263. {
  1264. // Some people like to stack pins on top of each other in a symbol to indicate
  1265. // internal connection. While technically connected, it is not particularly useful
  1266. // to display them that way, so skip any pins that are in the same symbol as this
  1267. // one.
  1268. if( each_item.GetParent() == this )
  1269. continue;
  1270. switch( each_item.GetType() )
  1271. {
  1272. case PIN_END:
  1273. case LABEL_END:
  1274. case SHEET_LABEL_END:
  1275. case WIRE_START_END:
  1276. case WIRE_END_END:
  1277. case NO_CONNECT_END:
  1278. case JUNCTION_END:
  1279. if( pos == each_item.GetPosition() )
  1280. m_isDangling[ i ] = false;
  1281. break;
  1282. default:
  1283. break;
  1284. }
  1285. if( !m_isDangling[ i ] )
  1286. break;
  1287. }
  1288. changed = ( changed || ( previousState != m_isDangling[ i ] ) );
  1289. }
  1290. while( m_isDangling.size() > m_Pins.size() )
  1291. m_isDangling.pop_back();
  1292. return changed;
  1293. }
  1294. wxPoint SCH_COMPONENT::GetPinPhysicalPosition( const LIB_PIN* Pin ) const
  1295. {
  1296. wxCHECK_MSG( Pin != NULL && Pin->Type() == LIB_PIN_T, wxPoint( 0, 0 ),
  1297. wxT( "Cannot get physical position of pin." ) );
  1298. return m_transform.TransformCoordinate( Pin->GetPosition() ) + m_Pos;
  1299. }
  1300. bool SCH_COMPONENT::IsSelectStateChanged( const wxRect& aRect )
  1301. {
  1302. bool previousState = IsSelected();
  1303. EDA_RECT boundingBox = GetBoundingBox();
  1304. if( aRect.Intersects( boundingBox ) )
  1305. SetFlags( SELECTED );
  1306. else
  1307. ClearFlags( SELECTED );
  1308. return previousState != IsSelected();
  1309. }
  1310. void SCH_COMPONENT::GetConnectionPoints( std::vector< wxPoint >& aPoints ) const
  1311. {
  1312. for( auto pin : m_Pins )
  1313. aPoints.push_back( m_transform.TransformCoordinate( pin ) + m_Pos );
  1314. }
  1315. LIB_ITEM* SCH_COMPONENT::GetDrawItem( const wxPoint& aPosition, KICAD_T aType )
  1316. {
  1317. if( PART_SPTR part = m_part.lock() )
  1318. {
  1319. m_Pins.clear();
  1320. for( LIB_PIN* pin = part->GetNextPin(); pin; pin = part->GetNextPin( pin ) )
  1321. {
  1322. wxASSERT( pin->Type() == LIB_PIN_T );
  1323. if( pin->GetUnit() && m_unit && ( m_unit != pin->GetUnit() ) )
  1324. continue;
  1325. if( pin->GetConvert() && m_convert && ( m_convert != pin->GetConvert() ) )
  1326. continue;
  1327. m_Pins.push_back( pin->GetPosition() );
  1328. }
  1329. // Calculate the position relative to the component.
  1330. wxPoint libPosition = aPosition - m_Pos;
  1331. return part->LocateDrawItem( m_unit, m_convert, aType, libPosition, m_transform );
  1332. }
  1333. return NULL;
  1334. }
  1335. wxString SCH_COMPONENT::GetSelectMenuText( EDA_UNITS_T aUnits ) const
  1336. {
  1337. return wxString::Format( _( "Symbol %s, %s" ),
  1338. GetLibId().GetLibItemName().wx_str(),
  1339. GetField( REFERENCE )->GetShownText() );
  1340. }
  1341. SEARCH_RESULT SCH_COMPONENT::Visit( INSPECTOR aInspector, void* aTestData,
  1342. const KICAD_T aFilterTypes[] )
  1343. {
  1344. KICAD_T stype;
  1345. for( const KICAD_T* p = aFilterTypes; (stype = *p) != EOT; ++p )
  1346. {
  1347. // If caller wants to inspect component type or and component children types.
  1348. if( stype == Type() )
  1349. {
  1350. if( SEARCH_QUIT == aInspector( this, aTestData ) )
  1351. return SEARCH_QUIT;
  1352. }
  1353. switch( stype )
  1354. {
  1355. case SCH_FIELD_T:
  1356. // Test the bounding boxes of fields if they are visible and not empty.
  1357. for( int ii = 0; ii < GetFieldCount(); ii++ )
  1358. {
  1359. if( SEARCH_QUIT == aInspector( GetField( ii ), (void*) this ) )
  1360. return SEARCH_QUIT;
  1361. }
  1362. break;
  1363. case SCH_FIELD_LOCATE_REFERENCE_T:
  1364. if( SEARCH_QUIT == aInspector( GetField( REFERENCE ), (void*) this ) )
  1365. return SEARCH_QUIT;
  1366. break;
  1367. case SCH_FIELD_LOCATE_VALUE_T:
  1368. if( SEARCH_QUIT == aInspector( GetField( VALUE ), (void*) this ) )
  1369. return SEARCH_QUIT;
  1370. break;
  1371. case SCH_FIELD_LOCATE_FOOTPRINT_T:
  1372. if( SEARCH_QUIT == aInspector( GetField( FOOTPRINT ), (void*) this ) )
  1373. return SEARCH_QUIT;
  1374. break;
  1375. case SCH_FIELD_LOCATE_DATASHEET_T:
  1376. if( SEARCH_QUIT == aInspector( GetField( DATASHEET ), (void*) this ) )
  1377. return SEARCH_QUIT;
  1378. break;
  1379. case LIB_PIN_T:
  1380. if( PART_SPTR part = m_part.lock() )
  1381. {
  1382. LIB_PINS pins;
  1383. part->GetPins( pins, m_unit, m_convert );
  1384. for( size_t i = 0; i < pins.size(); i++ )
  1385. {
  1386. if( SEARCH_QUIT == aInspector( pins[ i ], (void*) this ) )
  1387. return SEARCH_QUIT;
  1388. }
  1389. }
  1390. break;
  1391. default:
  1392. break;
  1393. }
  1394. }
  1395. return SEARCH_CONTINUE;
  1396. }
  1397. void SCH_COMPONENT::GetNetListItem( NETLIST_OBJECT_LIST& aNetListItems,
  1398. SCH_SHEET_PATH* aSheetPath )
  1399. {
  1400. if( PART_SPTR part = m_part.lock() )
  1401. {
  1402. for( LIB_PIN* pin = part->GetNextPin(); pin; pin = part->GetNextPin( pin ) )
  1403. {
  1404. wxASSERT( pin->Type() == LIB_PIN_T );
  1405. if( pin->GetUnit() && ( pin->GetUnit() != GetUnitSelection( aSheetPath ) ) )
  1406. continue;
  1407. if( pin->GetConvert() && ( pin->GetConvert() != GetConvert() ) )
  1408. continue;
  1409. wxPoint pos = GetTransform().TransformCoordinate( pin->GetPosition() ) + m_Pos;
  1410. NETLIST_OBJECT* item = new NETLIST_OBJECT();
  1411. item->m_SheetPathInclude = *aSheetPath;
  1412. item->m_Comp = (SCH_ITEM*) pin;
  1413. item->m_SheetPath = *aSheetPath;
  1414. item->m_Type = NET_PIN;
  1415. item->m_Link = (SCH_ITEM*) this;
  1416. item->m_ElectricalPinType = pin->GetType();
  1417. item->m_PinNum = pin->GetNumber();
  1418. item->m_Label = pin->GetName();
  1419. item->m_Start = item->m_End = pos;
  1420. aNetListItems.push_back( item );
  1421. if( pin->IsPowerConnection() )
  1422. {
  1423. // There is an associated PIN_LABEL.
  1424. item = new NETLIST_OBJECT();
  1425. item->m_SheetPathInclude = *aSheetPath;
  1426. item->m_Comp = NULL;
  1427. item->m_SheetPath = *aSheetPath;
  1428. item->m_Type = NET_PINLABEL;
  1429. item->m_Label = pin->GetName();
  1430. item->m_Start = pos;
  1431. item->m_End = item->m_Start;
  1432. aNetListItems.push_back( item );
  1433. }
  1434. }
  1435. }
  1436. }
  1437. bool SCH_COMPONENT::operator <( const SCH_ITEM& aItem ) const
  1438. {
  1439. if( Type() != aItem.Type() )
  1440. return Type() < aItem.Type();
  1441. SCH_COMPONENT* component = (SCH_COMPONENT*) &aItem;
  1442. EDA_RECT rect = GetBodyBoundingBox();
  1443. if( rect.GetArea() != component->GetBodyBoundingBox().GetArea() )
  1444. return rect.GetArea() < component->GetBodyBoundingBox().GetArea();
  1445. if( m_Pos.x != component->m_Pos.x )
  1446. return m_Pos.x < component->m_Pos.x;
  1447. if( m_Pos.y != component->m_Pos.y )
  1448. return m_Pos.y < component->m_Pos.y;
  1449. return false;
  1450. }
  1451. bool SCH_COMPONENT::operator==( const SCH_COMPONENT& aComponent ) const
  1452. {
  1453. if( GetFieldCount() != aComponent.GetFieldCount() )
  1454. return false;
  1455. for( int i = VALUE; i < GetFieldCount(); i++ )
  1456. {
  1457. if( GetField( i )->GetText().Cmp( aComponent.GetField( i )->GetText() ) != 0 )
  1458. return false;
  1459. }
  1460. return true;
  1461. }
  1462. bool SCH_COMPONENT::operator!=( const SCH_COMPONENT& aComponent ) const
  1463. {
  1464. return !( *this == aComponent );
  1465. }
  1466. SCH_ITEM& SCH_COMPONENT::operator=( const SCH_ITEM& aItem )
  1467. {
  1468. wxCHECK_MSG( Type() == aItem.Type(), *this,
  1469. wxT( "Cannot assign object type " ) + aItem.GetClass() + wxT( " to type " ) +
  1470. GetClass() );
  1471. if( &aItem != this )
  1472. {
  1473. SCH_ITEM::operator=( aItem );
  1474. SCH_COMPONENT* c = (SCH_COMPONENT*) &aItem;
  1475. m_lib_id = c->m_lib_id;
  1476. m_part = c->m_part;
  1477. m_Pos = c->m_Pos;
  1478. m_unit = c->m_unit;
  1479. m_convert = c->m_convert;
  1480. m_transform = c->m_transform;
  1481. m_Pins = c->m_Pins;
  1482. m_PathsAndReferences = c->m_PathsAndReferences;
  1483. m_Fields = c->m_Fields; // std::vector's assignment operator.
  1484. // Reparent fields after assignment to new component.
  1485. for( int ii = 0; ii < GetFieldCount(); ++ii )
  1486. {
  1487. GetField( ii )->SetParent( this );
  1488. }
  1489. }
  1490. return *this;
  1491. }
  1492. bool SCH_COMPONENT::HitTest( const wxPoint& aPosition, int aAccuracy ) const
  1493. {
  1494. EDA_RECT bBox = GetBodyBoundingBox();
  1495. bBox.Inflate( aAccuracy );
  1496. if( bBox.Contains( aPosition ) )
  1497. return true;
  1498. return false;
  1499. }
  1500. bool SCH_COMPONENT::HitTest( const EDA_RECT& aRect, bool aContained, int aAccuracy ) const
  1501. {
  1502. if( m_Flags & STRUCT_DELETED || m_Flags & SKIP_STRUCT )
  1503. return false;
  1504. EDA_RECT rect = aRect;
  1505. rect.Inflate( aAccuracy );
  1506. if( aContained )
  1507. return rect.Contains( GetBodyBoundingBox() );
  1508. return rect.Intersects( GetBodyBoundingBox() );
  1509. }
  1510. bool SCH_COMPONENT::doIsConnected( const wxPoint& aPosition ) const
  1511. {
  1512. wxPoint new_pos = m_transform.InverseTransform().TransformCoordinate( aPosition - m_Pos );
  1513. return std::find( m_Pins.begin(), m_Pins.end(), new_pos ) != m_Pins.end();
  1514. }
  1515. bool SCH_COMPONENT::IsInNetlist() const
  1516. {
  1517. SCH_FIELD* rf = GetField( REFERENCE );
  1518. return ! rf->GetText().StartsWith( wxT( "#" ) );
  1519. }
  1520. void SCH_COMPONENT::Plot( PLOTTER* aPlotter )
  1521. {
  1522. TRANSFORM temp;
  1523. if( PART_SPTR part = m_part.lock() )
  1524. {
  1525. temp = GetTransform();
  1526. aPlotter->StartBlock( nullptr );
  1527. part->Plot( aPlotter, GetUnit(), GetConvert(), m_Pos, temp );
  1528. for( size_t i = 0; i < m_Fields.size(); i++ )
  1529. {
  1530. m_Fields[i].Plot( aPlotter );
  1531. }
  1532. aPlotter->EndBlock( nullptr );
  1533. }
  1534. }
  1535. void SCH_COMPONENT::ClearHighlightedPins()
  1536. {
  1537. m_highlightedPins.clear();
  1538. }
  1539. void SCH_COMPONENT::HighlightPin( LIB_PIN* aPin )
  1540. {
  1541. m_highlightedPins.insert( aPin->GetNumber() );
  1542. }
  1543. bool SCH_COMPONENT::IsPinHighlighted( const LIB_PIN* aPin )
  1544. {
  1545. return m_highlightedPins.find( aPin->GetNumber() ) != m_highlightedPins.end();
  1546. }
  1547. void SCH_COMPONENT::ClearAllHighlightFlags()
  1548. {
  1549. ClearFlags( HIGHLIGHTED );
  1550. // Clear the HIGHLIGHTED flag of pins
  1551. ClearHighlightedPins();
  1552. // Clear the HIGHLIGHTED flag of other items, currently only fields
  1553. for( SCH_FIELD& each_field : m_Fields )
  1554. each_field.ClearFlags( HIGHLIGHTED );
  1555. }