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.

1071 lines
28 KiB

18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2016 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 <sch_draw_panel.h>
  26. #include <gr_text.h>
  27. #include <trigo.h>
  28. #include <sch_edit_frame.h>
  29. #include <plotter.h>
  30. #include <kicad_string.h>
  31. #include <msgpanel.h>
  32. #include <math/util.h> // for KiROUND
  33. #include <sch_sheet.h>
  34. #include <sch_sheet_path.h>
  35. #include <sch_component.h>
  36. #include <settings/color_settings.h>
  37. #include <netlist_object.h>
  38. #include <trace_helpers.h>
  39. #include <pgm_base.h>
  40. const wxString SCH_SHEET::GetDefaultFieldName( int aFieldNdx )
  41. {
  42. static void* locale = nullptr;
  43. static wxString sheetnameDefault;
  44. static wxString sheetfilenameDefault;
  45. static wxString fieldDefault;
  46. // Fetching translations can take a surprising amount of time when loading libraries,
  47. // so only do it when necessary.
  48. if( Pgm().GetLocale() != locale )
  49. {
  50. sheetnameDefault = _( "Sheet name" );
  51. sheetfilenameDefault = _( "Sheet file" );
  52. fieldDefault = _( "Field%d" );
  53. locale = Pgm().GetLocale();
  54. }
  55. // Fixed values for the mandatory fields
  56. switch( aFieldNdx )
  57. {
  58. case SHEETNAME: return sheetnameDefault;
  59. case SHEETFILENAME: return sheetfilenameDefault;
  60. default: return wxString::Format( fieldDefault, aFieldNdx );
  61. }
  62. }
  63. SCH_SHEET::SCH_SHEET( const wxPoint& pos ) :
  64. SCH_ITEM( NULL, SCH_SHEET_T )
  65. {
  66. m_Layer = LAYER_SHEET;
  67. m_pos = pos;
  68. m_size = wxSize( Mils2iu( MIN_SHEET_WIDTH ), Mils2iu( MIN_SHEET_HEIGHT ) );
  69. m_screen = NULL;
  70. for( int i = 0; i < SHEET_MANDATORY_FIELDS; ++i )
  71. {
  72. m_fields.emplace_back( pos, i, this, GetDefaultFieldName( i ) );
  73. m_fields.back().SetVisible( true );
  74. if( i == SHEETNAME )
  75. m_fields.back().SetLayer( LAYER_SHEETNAME );
  76. else if( i == SHEETFILENAME )
  77. m_fields.back().SetLayer( LAYER_SHEETFILENAME );
  78. else
  79. m_fields.back().SetLayer( LAYER_SHEETFIELDS );
  80. }
  81. m_fieldsAutoplaced = FIELDS_AUTOPLACED_AUTO;
  82. m_borderWidth = 0;
  83. m_borderColor = COLOR4D::UNSPECIFIED;
  84. m_backgroundColor = COLOR4D::UNSPECIFIED;
  85. }
  86. SCH_SHEET::SCH_SHEET( const SCH_SHEET& aSheet ) :
  87. SCH_ITEM( aSheet )
  88. {
  89. m_pos = aSheet.m_pos;
  90. m_size = aSheet.m_size;
  91. m_Layer = aSheet.m_Layer;
  92. const_cast<KIID&>( m_Uuid ) = aSheet.m_Uuid;
  93. m_fields = aSheet.m_fields;
  94. m_fieldsAutoplaced = aSheet.m_fieldsAutoplaced;
  95. m_screen = aSheet.m_screen;
  96. for( SCH_SHEET_PIN* pin : aSheet.m_pins )
  97. {
  98. m_pins.emplace_back( new SCH_SHEET_PIN( *pin ) );
  99. m_pins.back()->SetParent( this );
  100. }
  101. m_borderWidth = aSheet.m_borderWidth;
  102. m_borderColor = aSheet.m_borderColor;
  103. m_backgroundColor = aSheet.m_backgroundColor;
  104. if( m_screen )
  105. m_screen->IncRefCount();
  106. }
  107. SCH_SHEET::~SCH_SHEET()
  108. {
  109. // also, look at the associated sheet & its reference count
  110. // perhaps it should be deleted also.
  111. if( m_screen )
  112. {
  113. m_screen->DecRefCount();
  114. if( m_screen->GetRefCount() == 0 )
  115. delete m_screen;
  116. }
  117. // We own our pins; delete them
  118. for( SCH_SHEET_PIN* pin : m_pins )
  119. delete pin;
  120. }
  121. EDA_ITEM* SCH_SHEET::Clone() const
  122. {
  123. return new SCH_SHEET( *this );
  124. }
  125. void SCH_SHEET::SetParent( EDA_ITEM* aSheet )
  126. {
  127. m_Parent = nullptr;
  128. if( aSheet )
  129. {
  130. // Parent must be another SCH_SHEET object or nullptr
  131. wxCHECK( aSheet->Type() == SCH_SHEET_T, /* void */ );
  132. m_Parent = aSheet;
  133. }
  134. }
  135. void SCH_SHEET::SetScreen( SCH_SCREEN* aScreen )
  136. {
  137. if( aScreen == m_screen )
  138. return;
  139. if( m_screen != NULL )
  140. {
  141. m_screen->DecRefCount();
  142. if( m_screen->GetRefCount() == 0 )
  143. {
  144. delete m_screen;
  145. m_screen = NULL;
  146. }
  147. }
  148. m_screen = aScreen;
  149. if( m_screen )
  150. m_screen->IncRefCount();
  151. }
  152. int SCH_SHEET::GetScreenCount() const
  153. {
  154. if( m_screen == NULL )
  155. return 0;
  156. return m_screen->GetRefCount();
  157. }
  158. SCH_SHEET* SCH_SHEET::GetRootSheet()
  159. {
  160. EDA_ITEM* item = GetParent();
  161. if( item == nullptr )
  162. return this;
  163. SCH_SHEET* sheet = dynamic_cast< SCH_SHEET* >( item );
  164. // The parent must be a SCH_SHEET object.
  165. wxCHECK( sheet, nullptr );
  166. // Recurse until a sheet is found with no parent which is the root sheet.
  167. return sheet->GetRootSheet();
  168. }
  169. bool SCH_SHEET::ResolveTextVar( wxString* token, int aDepth ) const
  170. {
  171. for( int i = 0; i < SHEET_MANDATORY_FIELDS; ++i )
  172. {
  173. if( token->IsSameAs( m_fields[i].GetCanonicalName().Upper() ) )
  174. {
  175. *token = m_fields[i].GetShownText( aDepth + 1 );
  176. return true;
  177. }
  178. }
  179. for( size_t i = SHEET_MANDATORY_FIELDS; i < m_fields.size(); ++i )
  180. {
  181. if( token->IsSameAs( m_fields[i].GetName() ) )
  182. {
  183. *token = m_fields[i].GetShownText( aDepth + 1 );
  184. return true;
  185. }
  186. }
  187. if( token->IsSameAs( wxT( "#" ) ) )
  188. {
  189. SCH_SHEET_LIST sheetList( g_RootSheet );
  190. for( const SCH_SHEET_PATH& sheet : sheetList )
  191. {
  192. if( sheet.Last() == this ) // Current sheet path found
  193. {
  194. *token = wxString::Format( wxT( "%d" ), sheet.GetPageNumber() );
  195. return true;
  196. }
  197. }
  198. }
  199. else if( token->IsSameAs( wxT( "##" ) ) )
  200. {
  201. SCH_SHEET_LIST sheetList( g_RootSheet );
  202. *token = wxString::Format( wxT( "%d" ), (int) sheetList.size() );
  203. return true;
  204. }
  205. return false;
  206. }
  207. bool SCH_SHEET::UsesDefaultStroke() const
  208. {
  209. return m_borderWidth == 0 && m_borderColor == COLOR4D::UNSPECIFIED;
  210. }
  211. void SCH_SHEET::SwapData( SCH_ITEM* aItem )
  212. {
  213. wxCHECK_RET( aItem->Type() == SCH_SHEET_T,
  214. wxString::Format( wxT( "SCH_SHEET object cannot swap data with %s object." ),
  215. GetChars( aItem->GetClass() ) ) );
  216. SCH_SHEET* sheet = ( SCH_SHEET* ) aItem;
  217. std::swap( m_pos, sheet->m_pos );
  218. std::swap( m_size, sheet->m_size );
  219. m_fields.swap( sheet->m_fields );
  220. std::swap( m_fieldsAutoplaced, sheet->m_fieldsAutoplaced );
  221. m_pins.swap( sheet->m_pins );
  222. // Update parent pointers after swapping.
  223. for( SCH_SHEET_PIN* sheetPin : m_pins )
  224. sheetPin->SetParent( this );
  225. for( SCH_SHEET_PIN* sheetPin : sheet->m_pins )
  226. sheetPin->SetParent( sheet );
  227. std::swap( m_borderWidth, sheet->m_borderWidth );
  228. std::swap( m_borderColor, sheet->m_borderColor );
  229. std::swap( m_backgroundColor, sheet->m_backgroundColor );
  230. }
  231. void SCH_SHEET::AddPin( SCH_SHEET_PIN* aSheetPin )
  232. {
  233. wxASSERT( aSheetPin != NULL );
  234. wxASSERT( aSheetPin->Type() == SCH_SHEET_PIN_T );
  235. aSheetPin->SetParent( this );
  236. m_pins.push_back( aSheetPin );
  237. renumberPins();
  238. }
  239. void SCH_SHEET::RemovePin( SCH_SHEET_PIN* aSheetPin )
  240. {
  241. wxASSERT( aSheetPin != NULL );
  242. wxASSERT( aSheetPin->Type() == SCH_SHEET_PIN_T );
  243. for( auto i = m_pins.begin(); i < m_pins.end(); ++i )
  244. {
  245. if( *i == aSheetPin )
  246. {
  247. m_pins.erase( i );
  248. renumberPins();
  249. return;
  250. }
  251. }
  252. wxLogDebug( wxT( "Fix me: attempt to remove label %s which is not in sheet %s." ),
  253. aSheetPin->GetShownText(), m_fields[ SHEETNAME ].GetText() );
  254. }
  255. bool SCH_SHEET::HasPin( const wxString& aName )
  256. {
  257. for( SCH_SHEET_PIN* pin : m_pins )
  258. {
  259. if( pin->GetText().CmpNoCase( aName ) == 0 )
  260. return true;
  261. }
  262. return false;
  263. }
  264. bool SCH_SHEET::doIsConnected( const wxPoint& aPosition ) const
  265. {
  266. for( SCH_SHEET_PIN* sheetPin : m_pins )
  267. {
  268. if( sheetPin->GetPosition() == aPosition )
  269. return true;
  270. }
  271. return false;
  272. }
  273. bool SCH_SHEET::IsVerticalOrientation() const
  274. {
  275. int leftRight = 0;
  276. int topBottom = 0;
  277. for( SCH_SHEET_PIN* pin : m_pins )
  278. {
  279. switch( pin->GetEdge() )
  280. {
  281. case SHEET_LEFT_SIDE: leftRight++; break;
  282. case SHEET_RIGHT_SIDE: leftRight++; break;
  283. case SHEET_TOP_SIDE: topBottom++; break;
  284. case SHEET_BOTTOM_SIDE: topBottom++; break;
  285. default: break;
  286. }
  287. }
  288. return topBottom > 0 && leftRight == 0;
  289. }
  290. bool SCH_SHEET::HasUndefinedPins()
  291. {
  292. for( SCH_SHEET_PIN* pin : m_pins )
  293. {
  294. /* Search the schematic for a hierarchical label corresponding to this sheet label. */
  295. const SCH_HIERLABEL* HLabel = nullptr;
  296. for( auto aItem : m_screen->Items().OfType( SCH_HIER_LABEL_T ) )
  297. {
  298. if( !pin->GetText().CmpNoCase( static_cast<SCH_HIERLABEL*>( aItem )->GetText() ) )
  299. {
  300. HLabel = static_cast<SCH_HIERLABEL*>( aItem );
  301. break;
  302. }
  303. }
  304. if( HLabel == nullptr ) // Corresponding hierarchical label not found.
  305. return true;
  306. }
  307. return false;
  308. }
  309. int SCH_SHEET::GetMinWidth() const
  310. {
  311. int width = Mils2iu( MIN_SHEET_WIDTH );
  312. for( size_t i = 0; i < m_pins.size(); i++ )
  313. {
  314. int edge = m_pins[i]->GetEdge();
  315. EDA_RECT pinRect = m_pins[i]->GetBoundingBox();
  316. wxASSERT( edge != SHEET_UNDEFINED_SIDE );
  317. if( edge == SHEET_TOP_SIDE || edge == SHEET_BOTTOM_SIDE )
  318. {
  319. if( width < pinRect.GetRight() - m_pos.x )
  320. width = pinRect.GetRight() - m_pos.x;
  321. }
  322. else
  323. {
  324. if( width < pinRect.GetWidth() )
  325. width = pinRect.GetWidth();
  326. for( size_t j = 0; j < m_pins.size(); j++ )
  327. {
  328. // Check for pin directly across from the current pin.
  329. if( (i == j) || (m_pins[i]->GetPosition().y != m_pins[j]->GetPosition().y) )
  330. continue;
  331. if( width < pinRect.GetWidth() + m_pins[j]->GetBoundingBox().GetWidth() )
  332. {
  333. width = pinRect.GetWidth() + m_pins[j]->GetBoundingBox().GetWidth();
  334. break;
  335. }
  336. }
  337. }
  338. }
  339. return width;
  340. }
  341. int SCH_SHEET::GetMinHeight() const
  342. {
  343. int height = Mils2iu( MIN_SHEET_HEIGHT );
  344. for( size_t i = 0; i < m_pins.size(); i++ )
  345. {
  346. int edge = m_pins[i]->GetEdge();
  347. EDA_RECT pinRect = m_pins[i]->GetBoundingBox();
  348. // Make sure pin is on top or bottom side of sheet.
  349. if( edge == SHEET_RIGHT_SIDE || edge == SHEET_LEFT_SIDE )
  350. {
  351. if( height < pinRect.GetBottom() - m_pos.y )
  352. height = pinRect.GetBottom() - m_pos.y;
  353. }
  354. else
  355. {
  356. if( height < pinRect.GetHeight() )
  357. height = pinRect.GetHeight();
  358. for( size_t j = 0; j < m_pins.size(); j++ )
  359. {
  360. // Check for pin directly above or below the current pin.
  361. if( (i == j) || (m_pins[i]->GetPosition().x != m_pins[j]->GetPosition().x) )
  362. continue;
  363. if( height < pinRect.GetHeight() + m_pins[j]->GetBoundingBox().GetHeight() )
  364. {
  365. height = pinRect.GetHeight() + m_pins[j]->GetBoundingBox().GetHeight();
  366. break;
  367. }
  368. }
  369. }
  370. }
  371. return height;
  372. }
  373. void SCH_SHEET::CleanupSheet()
  374. {
  375. auto i = m_pins.begin();
  376. while( i != m_pins.end() )
  377. {
  378. /* Search the schematic for a hierarchical label corresponding to this sheet label. */
  379. const SCH_HIERLABEL* HLabel = NULL;
  380. for( SCH_ITEM* aItem : m_screen->Items().OfType( SCH_HIER_LABEL_T ) )
  381. {
  382. if( (*i)->GetText().CmpNoCase( static_cast<SCH_HIERLABEL*>( aItem )->GetText() ) == 0 )
  383. {
  384. HLabel = static_cast<SCH_HIERLABEL*>( aItem );
  385. break;
  386. }
  387. }
  388. if( HLabel == NULL ) // Hlabel not found: delete sheet label.
  389. i = m_pins.erase( i );
  390. else
  391. ++i;
  392. }
  393. }
  394. SCH_SHEET_PIN* SCH_SHEET::GetPin( const wxPoint& aPosition )
  395. {
  396. for( SCH_SHEET_PIN* pin : m_pins )
  397. {
  398. if( pin->HitTest( aPosition ) )
  399. return pin;
  400. }
  401. return NULL;
  402. }
  403. int SCH_SHEET::GetPenWidth() const
  404. {
  405. return std::max( GetBorderWidth(), 1 );
  406. }
  407. void SCH_SHEET::AutoplaceFields( SCH_SCREEN* aScreen, bool aManual )
  408. {
  409. wxASSERT_MSG( !aManual, "manual autoplacement not currently supported for sheets" );
  410. wxSize textSize = m_fields[ SHEETNAME ].GetTextSize();
  411. int borderMargin = KiROUND( GetPenWidth() / 2.0 ) + 4;
  412. int margin = borderMargin + KiROUND( std::max( textSize.x, textSize.y ) * 0.5 );
  413. if( IsVerticalOrientation() )
  414. {
  415. m_fields[ SHEETNAME ].SetTextPos( m_pos + wxPoint( -margin, m_size.y ) );
  416. m_fields[ SHEETNAME ].SetHorizJustify( GR_TEXT_HJUSTIFY_LEFT );
  417. m_fields[ SHEETNAME ].SetVertJustify(GR_TEXT_VJUSTIFY_BOTTOM );
  418. m_fields[ SHEETNAME ].SetTextAngle( TEXT_ANGLE_VERT );
  419. }
  420. else
  421. {
  422. m_fields[ SHEETNAME ].SetTextPos( m_pos + wxPoint( 0, -margin ) );
  423. m_fields[ SHEETNAME ].SetHorizJustify( GR_TEXT_HJUSTIFY_LEFT );
  424. m_fields[ SHEETNAME ].SetVertJustify(GR_TEXT_VJUSTIFY_BOTTOM );
  425. m_fields[ SHEETNAME ].SetTextAngle( TEXT_ANGLE_HORIZ );
  426. }
  427. textSize = m_fields[ SHEETFILENAME ].GetTextSize();
  428. margin = borderMargin + KiROUND( std::max( textSize.x, textSize.y ) * 0.4 );
  429. if( IsVerticalOrientation() )
  430. {
  431. m_fields[ SHEETFILENAME ].SetTextPos( m_pos + wxPoint( m_size.x + margin, m_size.y ) );
  432. m_fields[ SHEETFILENAME ].SetHorizJustify( GR_TEXT_HJUSTIFY_LEFT );
  433. m_fields[ SHEETFILENAME ].SetVertJustify(GR_TEXT_VJUSTIFY_TOP );
  434. m_fields[ SHEETFILENAME ].SetTextAngle( TEXT_ANGLE_VERT );
  435. }
  436. else
  437. {
  438. m_fields[ SHEETFILENAME ].SetTextPos( m_pos + wxPoint( 0, m_size.y + margin ) );
  439. m_fields[ SHEETFILENAME ].SetHorizJustify( GR_TEXT_HJUSTIFY_LEFT );
  440. m_fields[ SHEETFILENAME ].SetVertJustify(GR_TEXT_VJUSTIFY_TOP );
  441. m_fields[ SHEETFILENAME ].SetTextAngle( TEXT_ANGLE_HORIZ );
  442. }
  443. m_fieldsAutoplaced = FIELDS_AUTOPLACED_AUTO;
  444. }
  445. void SCH_SHEET::ViewGetLayers( int aLayers[], int& aCount ) const
  446. {
  447. aCount = 4;
  448. aLayers[0] = LAYER_HIERLABEL;
  449. aLayers[1] = LAYER_SHEET;
  450. aLayers[2] = LAYER_SHEET_BACKGROUND;
  451. aLayers[3] = LAYER_SELECTION_SHADOWS;
  452. }
  453. void SCH_SHEET::Print( RENDER_SETTINGS* aSettings, const wxPoint& aOffset )
  454. {
  455. wxDC* DC = aSettings->GetPrintDC();
  456. wxPoint pos = m_pos + aOffset;
  457. int lineWidth = std::max( GetPenWidth(), aSettings->GetDefaultPenWidth() );
  458. COLOR4D color = GetBorderColor();
  459. GRRect( nullptr, DC, pos.x, pos.y, pos.x + m_size.x, pos.y + m_size.y, lineWidth, color );
  460. for( SCH_FIELD& field : m_fields )
  461. field.Print( aSettings, aOffset );
  462. /* Draw text : SheetLabel */
  463. for( SCH_SHEET_PIN* sheetPin : m_pins )
  464. sheetPin->Print( aSettings, aOffset );
  465. }
  466. const EDA_RECT SCH_SHEET::GetBodyBoundingBox() const
  467. {
  468. wxPoint end;
  469. EDA_RECT box( m_pos, m_size );
  470. int lineWidth = GetPenWidth();
  471. int textLength = 0;
  472. // Calculate bounding box X size:
  473. end.x = std::max( m_size.x, textLength );
  474. // Calculate bounding box pos:
  475. end.y = m_size.y;
  476. end += m_pos;
  477. box.SetEnd( end );
  478. box.Inflate( lineWidth / 2 );
  479. return box;
  480. }
  481. const EDA_RECT SCH_SHEET::GetBoundingBox() const
  482. {
  483. EDA_RECT box = GetBodyBoundingBox();
  484. for( const SCH_FIELD& field : m_fields )
  485. box.Merge( field.GetBoundingBox() );
  486. return box;
  487. }
  488. wxPoint SCH_SHEET::GetRotationCenter() const
  489. {
  490. EDA_RECT box( m_pos, m_size );
  491. return box.GetCenter();
  492. }
  493. int SCH_SHEET::ComponentCount() const
  494. {
  495. int n = 0;
  496. if( m_screen )
  497. {
  498. for( auto aItem : m_screen->Items().OfType( SCH_COMPONENT_T ) )
  499. {
  500. SCH_COMPONENT* Cmp = (SCH_COMPONENT*) aItem;
  501. if( Cmp->GetField( VALUE )->GetText().GetChar( 0 ) != '#' )
  502. n++;
  503. }
  504. for( auto aItem : m_screen->Items().OfType( SCH_SHEET_T ) )
  505. n += static_cast<const SCH_SHEET*>( aItem )->ComponentCount();
  506. }
  507. return n;
  508. }
  509. bool SCH_SHEET::SearchHierarchy( const wxString& aFilename, SCH_SCREEN** aScreen )
  510. {
  511. if( m_screen )
  512. {
  513. // Only check the root sheet once and don't recurse.
  514. if( !GetParent() )
  515. {
  516. if( m_screen && m_screen->GetFileName().Cmp( aFilename ) == 0 )
  517. {
  518. *aScreen = m_screen;
  519. return true;
  520. }
  521. }
  522. for( auto aItem : m_screen->Items().OfType( SCH_SHEET_T ) )
  523. {
  524. SCH_SHEET* sheet = static_cast<SCH_SHEET*>( aItem );
  525. SCH_SCREEN* screen = sheet->m_screen;
  526. // Must use the screen's path (which is always absolute) rather than the
  527. // sheet's (which could be relative).
  528. if( screen && screen->GetFileName().Cmp( aFilename ) == 0 )
  529. {
  530. *aScreen = screen;
  531. return true;
  532. }
  533. if( sheet->SearchHierarchy( aFilename, aScreen ) )
  534. return true;
  535. }
  536. }
  537. return false;
  538. }
  539. bool SCH_SHEET::LocatePathOfScreen( SCH_SCREEN* aScreen, SCH_SHEET_PATH* aList )
  540. {
  541. if( m_screen )
  542. {
  543. aList->push_back( this );
  544. if( m_screen == aScreen )
  545. return true;
  546. for( auto item : m_screen->Items().OfType( SCH_SHEET_T ) )
  547. {
  548. SCH_SHEET* sheet = static_cast<SCH_SHEET*>( item );
  549. if( sheet->LocatePathOfScreen( aScreen, aList ) )
  550. {
  551. return true;
  552. }
  553. }
  554. aList->pop_back();
  555. }
  556. return false;
  557. }
  558. int SCH_SHEET::CountSheets()
  559. {
  560. int count = 1; //1 = this!!
  561. if( m_screen )
  562. {
  563. for( auto aItem : m_screen->Items().OfType( SCH_SHEET_T ) )
  564. count += static_cast<SCH_SHEET*>( aItem )->CountSheets();
  565. }
  566. return count;
  567. }
  568. void SCH_SHEET::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, MSG_PANEL_ITEMS& aList )
  569. {
  570. aList.emplace_back( _( "Sheet Name" ), m_fields[ SHEETNAME ].GetText(), CYAN );
  571. aList.emplace_back( _( "File Name" ), m_fields[ SHEETFILENAME ].GetText(), BROWN );
  572. #if 1 // Set to 1 to display the sheet UUID and hierarchical path
  573. wxString msgU, msgL;
  574. msgU << _( "UUID" ) << ": " << m_Uuid.AsString();
  575. msgL << _( "Path" ) << ": " << g_CurrentSheet->PathHumanReadable();
  576. aList.push_back( MSG_PANEL_ITEM( msgU, msgL, BLUE ) );
  577. #endif
  578. }
  579. void SCH_SHEET::Rotate(wxPoint aPosition)
  580. {
  581. wxPoint prev = m_pos;
  582. RotatePoint( &m_pos, aPosition, 900 );
  583. RotatePoint( &m_size.x, &m_size.y, 900 );
  584. if( m_size.x < 0 )
  585. {
  586. m_pos.x += m_size.x;
  587. m_size.x = -m_size.x;
  588. }
  589. if( m_size.y < 0 )
  590. {
  591. m_pos.y += m_size.y;
  592. m_size.y = -m_size.y;
  593. }
  594. // Pins must be rotated first as that's how we determine vertical vs horizontal
  595. // orientation for auto-placement
  596. for( SCH_SHEET_PIN* sheetPin : m_pins )
  597. sheetPin->Rotate( aPosition );
  598. if( m_fieldsAutoplaced == FIELDS_AUTOPLACED_AUTO )
  599. {
  600. AutoplaceFields( /* aScreen */ NULL, /* aManual */ false );
  601. }
  602. else
  603. {
  604. // Move the fields to the new position because the component itself has moved.
  605. for( SCH_FIELD& field : m_fields )
  606. {
  607. wxPoint pos = field.GetTextPos();
  608. pos.x -= prev.x - m_pos.x;
  609. pos.y -= prev.y - m_pos.y;
  610. field.SetTextPos( pos );
  611. }
  612. }
  613. }
  614. void SCH_SHEET::MirrorX( int aXaxis_position )
  615. {
  616. MIRROR( m_pos.y, aXaxis_position );
  617. m_pos.y -= m_size.y;
  618. for( SCH_SHEET_PIN* sheetPin : m_pins )
  619. sheetPin->MirrorX( aXaxis_position );
  620. }
  621. void SCH_SHEET::MirrorY( int aYaxis_position )
  622. {
  623. MIRROR( m_pos.x, aYaxis_position );
  624. m_pos.x -= m_size.x;
  625. for( SCH_SHEET_PIN* sheetPin : m_pins )
  626. sheetPin->MirrorY( aYaxis_position );
  627. }
  628. void SCH_SHEET::SetPosition( const wxPoint& aPosition )
  629. {
  630. // Remember the sheet and all pin sheet positions must be
  631. // modified. So use Move function to do that.
  632. Move( aPosition - m_pos );
  633. }
  634. void SCH_SHEET::Resize( const wxSize& aSize )
  635. {
  636. if( aSize == m_size )
  637. return;
  638. m_size = aSize;
  639. // Move the fields if we're in autoplace mode
  640. if( m_fieldsAutoplaced == FIELDS_AUTOPLACED_AUTO )
  641. AutoplaceFields( /* aScreen */ NULL, /* aManual */ false );
  642. // Move the sheet labels according to the new sheet size.
  643. for( SCH_SHEET_PIN* sheetPin : m_pins )
  644. sheetPin->ConstrainOnEdge( sheetPin->GetPosition() );
  645. }
  646. bool SCH_SHEET::Matches( wxFindReplaceData& aSearchData, void* aAuxData )
  647. {
  648. wxLogTrace( traceFindItem, wxT( " item " ) + GetSelectMenuText( EDA_UNITS::MILLIMETRES ) );
  649. // Sheets are searchable via the child field and pin item text.
  650. return false;
  651. }
  652. void SCH_SHEET::renumberPins()
  653. {
  654. int id = 2;
  655. for( SCH_SHEET_PIN* pin : m_pins )
  656. {
  657. pin->SetNumber( id );
  658. id++;
  659. }
  660. }
  661. void SCH_SHEET::GetEndPoints( std::vector <DANGLING_END_ITEM>& aItemList )
  662. {
  663. for( SCH_SHEET_PIN* sheetPin : m_pins )
  664. {
  665. wxCHECK2_MSG( sheetPin->Type() == SCH_SHEET_PIN_T, continue,
  666. wxT( "Invalid item in schematic sheet pin list. Bad programmer!" ) );
  667. sheetPin->GetEndPoints( aItemList );
  668. }
  669. }
  670. bool SCH_SHEET::UpdateDanglingState( std::vector<DANGLING_END_ITEM>& aItemList,
  671. const SCH_SHEET_PATH* aPath )
  672. {
  673. bool changed = false;
  674. for( SCH_SHEET_PIN* sheetPin : m_pins )
  675. changed |= sheetPin->UpdateDanglingState( aItemList );
  676. return changed;
  677. }
  678. void SCH_SHEET::GetConnectionPoints( std::vector< wxPoint >& aPoints ) const
  679. {
  680. for( SCH_SHEET_PIN* sheetPin : m_pins )
  681. aPoints.push_back( sheetPin->GetPosition() );
  682. }
  683. SEARCH_RESULT SCH_SHEET::Visit( INSPECTOR aInspector, void* testData, const KICAD_T aFilterTypes[] )
  684. {
  685. KICAD_T stype;
  686. for( const KICAD_T* p = aFilterTypes; (stype = *p) != EOT; ++p )
  687. {
  688. // If caller wants to inspect my type
  689. if( stype == SCH_LOCATE_ANY_T || stype == Type() )
  690. {
  691. if( SEARCH_RESULT::QUIT == aInspector( this, NULL ) )
  692. return SEARCH_RESULT::QUIT;
  693. }
  694. if( stype == SCH_LOCATE_ANY_T || stype == SCH_FIELD_T )
  695. {
  696. // Test the sheet fields.
  697. for( SCH_FIELD& field : m_fields )
  698. {
  699. if( SEARCH_RESULT::QUIT == aInspector( &field, this ) )
  700. return SEARCH_RESULT::QUIT;
  701. }
  702. }
  703. if( stype == SCH_LOCATE_ANY_T || stype == SCH_SHEET_PIN_T )
  704. {
  705. // Test the sheet labels.
  706. for( SCH_SHEET_PIN* sheetPin : m_pins )
  707. {
  708. if( SEARCH_RESULT::QUIT == aInspector( sheetPin, this ) )
  709. return SEARCH_RESULT::QUIT;
  710. }
  711. }
  712. }
  713. return SEARCH_RESULT::CONTINUE;
  714. }
  715. wxString SCH_SHEET::GetSelectMenuText( EDA_UNITS aUnits ) const
  716. {
  717. return wxString::Format( _( "Hierarchical Sheet %s" ), m_fields[ SHEETNAME ].GetText() );
  718. }
  719. BITMAP_DEF SCH_SHEET::GetMenuImage() const
  720. {
  721. return add_hierarchical_subsheet_xpm;
  722. }
  723. bool SCH_SHEET::HitTest( const wxPoint& aPosition, int aAccuracy ) const
  724. {
  725. EDA_RECT rect = GetBodyBoundingBox();
  726. rect.Inflate( aAccuracy );
  727. return rect.Contains( aPosition );
  728. }
  729. bool SCH_SHEET::HitTest( const EDA_RECT& aRect, bool aContained, int aAccuracy ) const
  730. {
  731. EDA_RECT rect = aRect;
  732. rect.Inflate( aAccuracy );
  733. if( aContained )
  734. return rect.Contains( GetBodyBoundingBox() );
  735. return rect.Intersects( GetBodyBoundingBox() );
  736. }
  737. void SCH_SHEET::GetNetListItem( NETLIST_OBJECT_LIST& aNetListItems, SCH_SHEET_PATH* aSheetPath )
  738. {
  739. SCH_SHEET_PATH sheetPath = *aSheetPath;
  740. sheetPath.push_back( this );
  741. for( SCH_SHEET_PIN* sheetPin : m_pins )
  742. {
  743. NETLIST_OBJECT* item = new NETLIST_OBJECT();
  744. item->m_SheetPathInclude = sheetPath;
  745. item->m_SheetPath = *aSheetPath;
  746. item->m_Comp = sheetPin;
  747. item->m_Link = this;
  748. item->m_Type = NETLIST_ITEM::SHEETLABEL;
  749. item->m_Label = sheetPin->GetText();
  750. item->m_Start = item->m_End = sheetPin->GetPosition();
  751. aNetListItems.push_back( item );
  752. if( SCH_CONNECTION::IsBusLabel( sheetPin->GetShownText() ) )
  753. item->ConvertBusToNetListItems( aNetListItems );
  754. }
  755. }
  756. void SCH_SHEET::Plot( PLOTTER* aPlotter )
  757. {
  758. wxString Text;
  759. wxPoint pos;
  760. COLOR4D borderColor = GetBorderColor();
  761. if( borderColor == COLOR4D::UNSPECIFIED )
  762. borderColor = aPlotter->RenderSettings()->GetLayerColor( LAYER_SHEET );
  763. aPlotter->SetColor( borderColor );
  764. int penWidth = std::max( GetPenWidth(), aPlotter->RenderSettings()->GetDefaultPenWidth() );
  765. aPlotter->SetCurrentLineWidth( penWidth );
  766. aPlotter->MoveTo( m_pos );
  767. pos = m_pos;
  768. pos.x += m_size.x;
  769. aPlotter->LineTo( pos );
  770. pos.y += m_size.y;
  771. aPlotter->LineTo( pos );
  772. pos = m_pos;
  773. pos.y += m_size.y;
  774. aPlotter->LineTo( pos );
  775. aPlotter->FinishTo( m_pos );
  776. for( SCH_FIELD field : m_fields )
  777. field.Plot( aPlotter );
  778. /* Draw texts : SheetLabel */
  779. for( SCH_SHEET_PIN* sheetPin : m_pins )
  780. sheetPin->Plot( aPlotter );
  781. }
  782. SCH_SHEET& SCH_SHEET::operator=( const SCH_ITEM& aItem )
  783. {
  784. wxLogDebug( wxT( "Sheet assignment operator." ) );
  785. wxCHECK_MSG( Type() == aItem.Type(), *this,
  786. wxT( "Cannot assign object type " ) + aItem.GetClass() + wxT( " to type " ) +
  787. GetClass() );
  788. if( &aItem != this )
  789. {
  790. SCH_ITEM::operator=( aItem );
  791. SCH_SHEET* sheet = (SCH_SHEET*) &aItem;
  792. m_pos = sheet->m_pos;
  793. m_size = sheet->m_size;
  794. m_fields = sheet->m_fields;
  795. for( SCH_SHEET_PIN* pin : sheet->m_pins )
  796. {
  797. m_pins.emplace_back( new SCH_SHEET_PIN( *pin ) );
  798. m_pins.back()->SetParent( this );
  799. }
  800. }
  801. return *this;
  802. }
  803. bool SCH_SHEET::operator <( const SCH_ITEM& aItem ) const
  804. {
  805. if( Type() != aItem.Type() )
  806. return Type() < aItem.Type();
  807. auto sheet = static_cast<const SCH_SHEET*>( &aItem );
  808. if (m_fields[ SHEETNAME ].GetText() != sheet->m_fields[ SHEETNAME ].GetText())
  809. return m_fields[ SHEETNAME ].GetText() < sheet->m_fields[ SHEETNAME ].GetText();
  810. if (m_fields[ SHEETFILENAME ].GetText() != sheet->m_fields[ SHEETFILENAME ].GetText())
  811. return m_fields[ SHEETFILENAME ].GetText() < sheet->m_fields[ SHEETFILENAME ].GetText();
  812. return false;
  813. }
  814. #if defined(DEBUG)
  815. void SCH_SHEET::Show( int nestLevel, std::ostream& os ) const
  816. {
  817. // XML output:
  818. wxString s = GetClass();
  819. NestedSpace( nestLevel, os ) << '<' << s.Lower().mb_str() << ">" << " sheet_name=\""
  820. << TO_UTF8( m_fields[ SHEETNAME ].GetText() ) << '"' << ">\n";
  821. // show all the pins, and check the linked list integrity
  822. for( SCH_SHEET_PIN* sheetPin : m_pins )
  823. sheetPin->Show( nestLevel + 1, os );
  824. NestedSpace( nestLevel, os ) << "</" << s.Lower().mb_str() << ">\n" << std::flush;
  825. }
  826. #endif