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.

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