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.

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