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.

1066 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
// Dick Hollenbeck's KiROUND R&D // This provides better project control over rounding to int from double // than wxRound() did. This scheme provides better logging in Debug builds // and it provides for compile time calculation of constants. #include <stdio.h> #include <assert.h> #include <limits.h> //-----<KiROUND KIT>------------------------------------------------------------ /** * KiROUND * rounds a floating point number to an int using * "round halfway cases away from zero". * In Debug build an assert fires if will not fit into an int. */ #if defined( DEBUG ) // DEBUG: a macro to capture line and file, then calls this inline static inline int KiRound( double v, int line, const char* filename ) { v = v < 0 ? v - 0.5 : v + 0.5; if( v > INT_MAX + 0.5 ) { printf( "%s: in file %s on line %d, val: %.16g too ' > 0 ' for int\n", __FUNCTION__, filename, line, v ); } else if( v < INT_MIN - 0.5 ) { printf( "%s: in file %s on line %d, val: %.16g too ' < 0 ' for int\n", __FUNCTION__, filename, line, v ); } return int( v ); } #define KiROUND( v ) KiRound( v, __LINE__, __FILE__ ) #else // RELEASE: a macro so compile can pre-compute constants. #define KiROUND( v ) int( (v) < 0 ? (v) - 0.5 : (v) + 0.5 ) #endif //-----</KiROUND KIT>----------------------------------------------------------- // Only a macro is compile time calculated, an inline function causes a static constructor // in a situation like this. // Therefore the Release build is best done with a MACRO not an inline function. int Computed = KiROUND( 14.3 * 8 ); int main( int argc, char** argv ) { for( double d = double(INT_MAX)-1; d < double(INT_MAX)+8; d += 2.0 ) { int i = KiROUND( d ); printf( "t: %d %.16g\n", i, d ); } return 0; }
14 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-2017 Kicad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. /**
  25. * @file sch_sheet.cpp
  26. * @brief Implementation of SCH_SHEET class.
  27. */
  28. #include <fctsys.h>
  29. #include <class_drawpanel.h>
  30. #include <draw_graphic_text.h>
  31. #include <trigo.h>
  32. #include <richio.h>
  33. #include <sch_edit_frame.h>
  34. #include <plotter.h>
  35. #include <kicad_string.h>
  36. #include <msgpanel.h>
  37. #include <sch_sheet.h>
  38. #include <sch_sheet_path.h>
  39. #include <sch_component.h>
  40. #include <netlist_object.h>
  41. #include <trace_helpers.h>
  42. SCH_SHEET::SCH_SHEET( const wxPoint& pos ) :
  43. SCH_ITEM( NULL, SCH_SHEET_T )
  44. {
  45. m_Layer = LAYER_SHEET;
  46. m_pos = pos;
  47. m_size = wxSize( MIN_SHEET_WIDTH, MIN_SHEET_HEIGHT );
  48. SetTimeStamp( GetNewTimeStamp() );
  49. m_sheetNameSize = GetDefaultTextSize();
  50. m_fileNameSize = GetDefaultTextSize();
  51. m_screen = NULL;
  52. m_name.Printf( wxT( "Sheet%8.8lX" ), (long) m_TimeStamp );
  53. m_fileName.Printf( wxT( "file%8.8lX.sch" ), (long) m_TimeStamp );
  54. }
  55. SCH_SHEET::SCH_SHEET( const SCH_SHEET& aSheet ) :
  56. SCH_ITEM( aSheet )
  57. {
  58. m_pos = aSheet.m_pos;
  59. m_size = aSheet.m_size;
  60. m_Layer = aSheet.m_Layer;
  61. SetTimeStamp( aSheet.m_TimeStamp );
  62. m_sheetNameSize = aSheet.m_sheetNameSize;
  63. m_fileNameSize = aSheet.m_fileNameSize;
  64. m_screen = aSheet.m_screen;
  65. m_name = aSheet.m_name;
  66. m_fileName = aSheet.m_fileName;
  67. m_pins = aSheet.m_pins;
  68. for( size_t i = 0; i < m_pins.size(); i++ )
  69. m_pins[i].SetParent( this );
  70. if( m_screen )
  71. m_screen->IncRefCount();
  72. }
  73. SCH_SHEET::~SCH_SHEET()
  74. {
  75. // also, look at the associated sheet & its reference count
  76. // perhaps it should be deleted also.
  77. if( m_screen )
  78. {
  79. m_screen->DecRefCount();
  80. if( m_screen->GetRefCount() == 0 )
  81. delete m_screen;
  82. }
  83. }
  84. EDA_ITEM* SCH_SHEET::Clone() const
  85. {
  86. return new SCH_SHEET( *this );
  87. }
  88. void SCH_SHEET::SetScreen( SCH_SCREEN* aScreen )
  89. {
  90. if( aScreen == m_screen )
  91. return;
  92. if( m_screen != NULL )
  93. {
  94. m_screen->DecRefCount();
  95. if( m_screen->GetRefCount() == 0 )
  96. {
  97. delete m_screen;
  98. m_screen = NULL;
  99. }
  100. }
  101. m_screen = aScreen;
  102. if( m_screen )
  103. m_screen->IncRefCount();
  104. }
  105. int SCH_SHEET::GetScreenCount() const
  106. {
  107. if( m_screen == NULL )
  108. return 0;
  109. return m_screen->GetRefCount();
  110. }
  111. SCH_SHEET* SCH_SHEET::GetRootSheet()
  112. {
  113. SCH_SHEET* sheet = dynamic_cast< SCH_SHEET* >( GetParent() );
  114. if( sheet == NULL )
  115. return this;
  116. // Recurse until a sheet is found with no parent which is the root sheet.
  117. return sheet->GetRootSheet();
  118. }
  119. void SCH_SHEET::SwapData( SCH_ITEM* aItem )
  120. {
  121. wxCHECK_RET( aItem->Type() == SCH_SHEET_T,
  122. wxString::Format( wxT( "SCH_SHEET object cannot swap data with %s object." ),
  123. GetChars( aItem->GetClass() ) ) );
  124. SCH_SHEET* sheet = ( SCH_SHEET* ) aItem;
  125. std::swap( m_pos, sheet->m_pos );
  126. std::swap( m_size, sheet->m_size );
  127. std::swap( m_name, sheet->m_name );
  128. std::swap( m_sheetNameSize, sheet->m_sheetNameSize );
  129. std::swap( m_fileNameSize, sheet->m_fileNameSize );
  130. m_pins.swap( sheet->m_pins );
  131. // Ensure sheet labels have their .m_Parent member pointing really on their
  132. // parent, after swapping.
  133. for( SCH_SHEET_PIN& sheetPin : m_pins )
  134. {
  135. sheetPin.SetParent( this );
  136. }
  137. for( SCH_SHEET_PIN& sheetPin : sheet->m_pins )
  138. {
  139. sheetPin.SetParent( sheet );
  140. }
  141. }
  142. void SCH_SHEET::AddPin( SCH_SHEET_PIN* aSheetPin )
  143. {
  144. wxASSERT( aSheetPin != NULL );
  145. wxASSERT( aSheetPin->Type() == SCH_SHEET_PIN_T );
  146. m_pins.push_back( aSheetPin );
  147. renumberPins();
  148. }
  149. void SCH_SHEET::RemovePin( SCH_SHEET_PIN* aSheetPin )
  150. {
  151. wxASSERT( aSheetPin != NULL );
  152. wxASSERT( aSheetPin->Type() == SCH_SHEET_PIN_T );
  153. SCH_SHEET_PINS::iterator i;
  154. for( i = m_pins.begin(); i < m_pins.end(); ++i )
  155. {
  156. if( *i == aSheetPin )
  157. {
  158. m_pins.erase( i );
  159. renumberPins();
  160. return;
  161. }
  162. }
  163. wxLogDebug( wxT( "Fix me: attempt to remove label %s which is not in sheet %s." ),
  164. GetChars( aSheetPin->GetShownText() ), GetChars( m_name ) );
  165. }
  166. bool SCH_SHEET::HasPin( const wxString& aName )
  167. {
  168. for( const SCH_SHEET_PIN& pin : m_pins )
  169. {
  170. if( pin.GetText().CmpNoCase( aName ) == 0 )
  171. return true;
  172. }
  173. return false;
  174. }
  175. bool SCH_SHEET::IsVerticalOrientation() const
  176. {
  177. for( const SCH_SHEET_PIN& pin : m_pins )
  178. {
  179. if( pin.GetEdge() > 1 )
  180. return true;
  181. }
  182. return false;
  183. }
  184. bool SCH_SHEET::HasUndefinedPins()
  185. {
  186. for( const SCH_SHEET_PIN& pin : m_pins )
  187. {
  188. /* Search the schematic for a hierarchical label corresponding to this sheet label. */
  189. EDA_ITEM* DrawStruct = m_screen->GetDrawItems();
  190. const SCH_HIERLABEL* HLabel = NULL;
  191. for( ; DrawStruct != NULL; DrawStruct = DrawStruct->Next() )
  192. {
  193. if( DrawStruct->Type() != SCH_HIERARCHICAL_LABEL_T )
  194. continue;
  195. HLabel = static_cast<SCH_HIERLABEL*>( DrawStruct );
  196. if( pin.GetText().CmpNoCase( HLabel->GetText() ) == 0 )
  197. break; // Found!
  198. HLabel = NULL;
  199. }
  200. if( HLabel == NULL ) // Corresponding hierarchical label not found.
  201. return true;
  202. }
  203. return false;
  204. }
  205. int SCH_SHEET::GetMinWidth() const
  206. {
  207. int width = MIN_SHEET_WIDTH;
  208. for( size_t i = 0; i < m_pins.size(); i++ )
  209. {
  210. int edge = m_pins[i].GetEdge();
  211. EDA_RECT pinRect = m_pins[i].GetBoundingBox();
  212. wxASSERT( edge != SCH_SHEET_PIN::SHEET_UNDEFINED_SIDE );
  213. if( edge == SCH_SHEET_PIN::SHEET_TOP_SIDE || edge == SCH_SHEET_PIN::SHEET_BOTTOM_SIDE )
  214. {
  215. if( width < pinRect.GetRight() - m_pos.x )
  216. width = pinRect.GetRight() - m_pos.x;
  217. }
  218. else
  219. {
  220. if( width < pinRect.GetWidth() )
  221. width = pinRect.GetWidth();
  222. for( size_t j = 0; j < m_pins.size(); j++ )
  223. {
  224. // Check for pin directly across from the current pin.
  225. if( (i == j) || (m_pins[i].GetPosition().y != m_pins[j].GetPosition().y) )
  226. continue;
  227. if( width < pinRect.GetWidth() + m_pins[j].GetBoundingBox().GetWidth() )
  228. {
  229. width = pinRect.GetWidth() + m_pins[j].GetBoundingBox().GetWidth();
  230. break;
  231. }
  232. }
  233. }
  234. }
  235. return width;
  236. }
  237. int SCH_SHEET::GetMinHeight() const
  238. {
  239. int height = MIN_SHEET_HEIGHT;
  240. for( size_t i = 0; i < m_pins.size(); i++ )
  241. {
  242. int edge = m_pins[i].GetEdge();
  243. EDA_RECT pinRect = m_pins[i].GetBoundingBox();
  244. // Make sure pin is on top or bottom side of sheet.
  245. if( edge == SCH_SHEET_PIN::SHEET_RIGHT_SIDE || edge == SCH_SHEET_PIN::SHEET_LEFT_SIDE )
  246. {
  247. if( height < pinRect.GetBottom() - m_pos.y )
  248. height = pinRect.GetBottom() - m_pos.y;
  249. }
  250. else
  251. {
  252. if( height < pinRect.GetHeight() )
  253. height = pinRect.GetHeight();
  254. for( size_t j = 0; j < m_pins.size(); j++ )
  255. {
  256. // Check for pin directly above or below the current pin.
  257. if( (i == j) || (m_pins[i].GetPosition().x != m_pins[j].GetPosition().x) )
  258. continue;
  259. if( height < pinRect.GetHeight() + m_pins[j].GetBoundingBox().GetHeight() )
  260. {
  261. height = pinRect.GetHeight() + m_pins[j].GetBoundingBox().GetHeight();
  262. break;
  263. }
  264. }
  265. }
  266. }
  267. return height;
  268. }
  269. void SCH_SHEET::CleanupSheet()
  270. {
  271. SCH_SHEET_PINS::iterator i = m_pins.begin();
  272. while( i != m_pins.end() )
  273. {
  274. /* Search the schematic for a hierarchical label corresponding to this sheet label. */
  275. EDA_ITEM* DrawStruct = m_screen->GetDrawItems();
  276. const SCH_HIERLABEL* HLabel = NULL;
  277. for( ; DrawStruct != NULL; DrawStruct = DrawStruct->Next() )
  278. {
  279. if( DrawStruct->Type() != SCH_HIERARCHICAL_LABEL_T )
  280. continue;
  281. HLabel = static_cast<SCH_HIERLABEL*>( DrawStruct );
  282. if( i->GetText().CmpNoCase( HLabel->GetText() ) == 0 )
  283. break; // Found!
  284. HLabel = NULL;
  285. }
  286. if( HLabel == NULL ) // Hlabel not found: delete sheet label.
  287. i = m_pins.erase( i );
  288. else
  289. ++i;
  290. }
  291. }
  292. SCH_SHEET_PIN* SCH_SHEET::GetPin( const wxPoint& aPosition )
  293. {
  294. for( SCH_SHEET_PIN& pin : m_pins )
  295. {
  296. if( pin.HitTest( aPosition, 0 ) )
  297. return &pin;
  298. }
  299. return NULL;
  300. }
  301. int SCH_SHEET::GetPenSize() const
  302. {
  303. return GetDefaultLineThickness();
  304. }
  305. wxPoint SCH_SHEET::GetSheetNamePosition()
  306. {
  307. wxPoint pos = m_pos;
  308. if( IsVerticalOrientation() )
  309. {
  310. pos.x -= 8;
  311. pos.y += m_size.y;
  312. }
  313. else
  314. {
  315. pos.y -= 8;
  316. }
  317. return pos;
  318. }
  319. wxPoint SCH_SHEET::GetFileNamePosition()
  320. {
  321. wxPoint pos = m_pos;
  322. int margin = GetPenSize() + 4;
  323. if( IsVerticalOrientation() )
  324. {
  325. pos.x += m_size.x + margin;
  326. pos.y += m_size.y;
  327. }
  328. else
  329. {
  330. pos.y += m_size.y + margin;
  331. }
  332. return pos;
  333. }
  334. void SCH_SHEET::Draw( EDA_DRAW_PANEL* aPanel, wxDC* aDC,
  335. const wxPoint& aOffset, GR_DRAWMODE aDrawMode, COLOR4D aColor )
  336. {
  337. COLOR4D txtcolor;
  338. wxString Text;
  339. COLOR4D color;
  340. int name_orientation;
  341. wxPoint pos_sheetname,pos_filename;
  342. wxPoint pos = m_pos + aOffset;
  343. int lineWidth = GetPenSize();
  344. EDA_RECT* clipbox = aPanel? aPanel->GetClipBox() : NULL;
  345. if( aColor != COLOR4D::UNSPECIFIED )
  346. color = aColor;
  347. else
  348. color = GetLayerColor( m_Layer );
  349. GRSetDrawMode( aDC, aDrawMode );
  350. GRRect( clipbox, aDC, pos.x, pos.y,
  351. pos.x + m_size.x, pos.y + m_size.y, lineWidth, color );
  352. pos_sheetname = GetSheetNamePosition() + aOffset;
  353. pos_filename = GetFileNamePosition() + aOffset;
  354. if( IsVerticalOrientation() )
  355. name_orientation = TEXT_ANGLE_VERT;
  356. else
  357. name_orientation = TEXT_ANGLE_HORIZ;
  358. /* Draw text : SheetName */
  359. if( aColor != COLOR4D::UNSPECIFIED )
  360. txtcolor = aColor;
  361. else
  362. txtcolor = GetLayerColor( LAYER_SHEETNAME );
  363. Text = wxT( "Sheet: " ) + m_name;
  364. DrawGraphicText( clipbox, aDC, pos_sheetname,
  365. txtcolor, Text, name_orientation,
  366. wxSize( m_sheetNameSize, m_sheetNameSize ),
  367. GR_TEXT_HJUSTIFY_LEFT, GR_TEXT_VJUSTIFY_BOTTOM, lineWidth,
  368. false, false );
  369. /* Draw text : FileName */
  370. if( aColor != COLOR4D::UNSPECIFIED )
  371. txtcolor = aColor;
  372. else
  373. txtcolor = GetLayerColor( LAYER_SHEETFILENAME );
  374. Text = wxT( "File: " ) + m_fileName;
  375. DrawGraphicText( clipbox, aDC, pos_filename,
  376. txtcolor, Text, name_orientation,
  377. wxSize( m_fileNameSize, m_fileNameSize ),
  378. GR_TEXT_HJUSTIFY_LEFT, GR_TEXT_VJUSTIFY_TOP, lineWidth,
  379. false, false );
  380. /* Draw text : SheetLabel */
  381. for( SCH_SHEET_PIN& sheetPin : m_pins )
  382. {
  383. if( !sheetPin.IsMoving() )
  384. sheetPin.Draw( aPanel, aDC, aOffset, aDrawMode, aColor );
  385. }
  386. #if 0
  387. // Only for testing purposes, draw the component bounding box
  388. EDA_RECT boundingBox = GetBoundingBox();
  389. GRRect( aPanel->GetClipBox(), aDC, boundingBox, 0, BROWN );
  390. GRFilledCircle( aPanel->GetClipBox(), aDC, m_pos.x, m_pos.y, 10, 0, color, color );
  391. #endif
  392. }
  393. const EDA_RECT SCH_SHEET::GetBoundingBox() const
  394. {
  395. wxPoint end;
  396. EDA_RECT box( m_pos, m_size );
  397. int lineWidth = GetPenSize();
  398. // Determine length of texts
  399. wxString text = wxT( "Sheet: " ) + m_name;
  400. int textlen = GraphicTextWidth( text, wxSize( m_sheetNameSize, m_sheetNameSize ),
  401. false, false );
  402. text = wxT( "File: " ) + m_fileName;
  403. int textlen2 = GraphicTextWidth( text, wxSize( m_fileNameSize, m_fileNameSize ),
  404. false, false );
  405. // Calculate bounding box X size:
  406. textlen = std::max( textlen, textlen2 );
  407. end.x = std::max( m_size.x, textlen );
  408. // Calculate bounding box pos:
  409. end.y = m_size.y;
  410. end += m_pos;
  411. // Move upper and lower limits to include texts:
  412. box.SetY( box.GetY() - ( KiROUND( m_sheetNameSize * 1.3 ) + 8 ) );
  413. end.y += KiROUND( m_fileNameSize * 1.3 ) + 8;
  414. box.SetEnd( end );
  415. box.Inflate( lineWidth / 2 );
  416. return box;
  417. }
  418. int SCH_SHEET::ComponentCount()
  419. {
  420. int n = 0;
  421. if( m_screen )
  422. {
  423. EDA_ITEM* bs;
  424. for( bs = m_screen->GetDrawItems(); bs != NULL; bs = bs->Next() )
  425. {
  426. if( bs->Type() == SCH_COMPONENT_T )
  427. {
  428. SCH_COMPONENT* Cmp = (SCH_COMPONENT*) bs;
  429. if( Cmp->GetField( VALUE )->GetText().GetChar( 0 ) != '#' )
  430. n++;
  431. }
  432. if( bs->Type() == SCH_SHEET_T )
  433. {
  434. SCH_SHEET* sheet = (SCH_SHEET*) bs;
  435. n += sheet->ComponentCount();
  436. }
  437. }
  438. }
  439. return n;
  440. }
  441. bool SCH_SHEET::SearchHierarchy( const wxString& aFilename, SCH_SCREEN** aScreen )
  442. {
  443. if( m_screen )
  444. {
  445. EDA_ITEM* item = m_screen->GetDrawItems();
  446. while( item )
  447. {
  448. if( item->Type() == SCH_SHEET_T )
  449. {
  450. SCH_SHEET* sheet = (SCH_SHEET*) item;
  451. if( sheet->m_screen
  452. && sheet->m_screen->GetFileName().CmpNoCase( aFilename ) == 0 )
  453. {
  454. *aScreen = sheet->m_screen;
  455. return true;
  456. }
  457. if( sheet->SearchHierarchy( aFilename, aScreen ) )
  458. return true;
  459. }
  460. item = item->Next();
  461. }
  462. }
  463. return false;
  464. }
  465. bool SCH_SHEET::LocatePathOfScreen( SCH_SCREEN* aScreen, SCH_SHEET_PATH* aList )
  466. {
  467. if( m_screen )
  468. {
  469. aList->push_back( this );
  470. if( m_screen == aScreen )
  471. return true;
  472. EDA_ITEM* strct = m_screen->GetDrawItems();
  473. while( strct )
  474. {
  475. if( strct->Type() == SCH_SHEET_T )
  476. {
  477. SCH_SHEET* ss = (SCH_SHEET*) strct;
  478. if( ss->LocatePathOfScreen( aScreen, aList ) )
  479. return true;
  480. }
  481. strct = strct->Next();
  482. }
  483. aList->pop_back();
  484. }
  485. return false;
  486. }
  487. int SCH_SHEET::CountSheets()
  488. {
  489. int count = 1; //1 = this!!
  490. if( m_screen )
  491. {
  492. EDA_ITEM* strct = m_screen->GetDrawItems();
  493. for( ; strct; strct = strct->Next() )
  494. {
  495. if( strct->Type() == SCH_SHEET_T )
  496. {
  497. SCH_SHEET* subsheet = (SCH_SHEET*) strct;
  498. count += subsheet->CountSheets();
  499. }
  500. }
  501. }
  502. return count;
  503. }
  504. wxString SCH_SHEET::GetFileName( void ) const
  505. {
  506. return m_fileName;
  507. }
  508. void SCH_SHEET::GetMsgPanelInfo( MSG_PANEL_ITEMS& aList )
  509. {
  510. aList.push_back( MSG_PANEL_ITEM( _( "Sheet Name" ), m_name, CYAN ) );
  511. aList.push_back( MSG_PANEL_ITEM( _( "File Name" ), m_fileName, BROWN ) );
  512. #if 0 // Set to 1 to display the sheet time stamp (mainly for test)
  513. wxString msg;
  514. msg.Printf( wxT( "%.8X" ), m_TimeStamp );
  515. aList.push_back( MSG_PANEL_ITEM( _( "Time Stamp" ), msg, BLUE ) );
  516. #endif
  517. }
  518. void SCH_SHEET::Rotate(wxPoint aPosition)
  519. {
  520. RotatePoint( &m_pos, aPosition, 900 );
  521. RotatePoint( &m_size.x, &m_size.y, 900 );
  522. if( m_size.x < 0 )
  523. {
  524. m_pos.x += m_size.x;
  525. m_size.x = -m_size.x;
  526. }
  527. if( m_size.y < 0 )
  528. {
  529. m_pos.y += m_size.y;
  530. m_size.y = -m_size.y;
  531. }
  532. for( SCH_SHEET_PIN& sheetPin : m_pins )
  533. {
  534. sheetPin.Rotate( aPosition );
  535. }
  536. }
  537. void SCH_SHEET::MirrorX( int aXaxis_position )
  538. {
  539. MIRROR( m_pos.y, aXaxis_position );
  540. m_pos.y -= m_size.y;
  541. for( SCH_SHEET_PIN& sheetPin : m_pins )
  542. {
  543. sheetPin.MirrorX( aXaxis_position );
  544. }
  545. }
  546. void SCH_SHEET::MirrorY( int aYaxis_position )
  547. {
  548. MIRROR( m_pos.x, aYaxis_position );
  549. m_pos.x -= m_size.x;
  550. for( SCH_SHEET_PIN& label : m_pins )
  551. {
  552. label.MirrorY( aYaxis_position );
  553. }
  554. }
  555. void SCH_SHEET::SetPosition( const wxPoint& aPosition )
  556. {
  557. // Remember the sheet and all pin sheet positions must be
  558. // modified. So use Move function to do that.
  559. Move( aPosition - m_pos );
  560. }
  561. void SCH_SHEET::Resize( const wxSize& aSize )
  562. {
  563. if( aSize == m_size )
  564. return;
  565. m_size = aSize;
  566. /* Move the sheet labels according to the new sheet size. */
  567. for( SCH_SHEET_PIN& label : m_pins )
  568. {
  569. label.ConstrainOnEdge( label.GetPosition() );
  570. }
  571. }
  572. bool SCH_SHEET::Matches( wxFindReplaceData& aSearchData, void* aAuxData, wxPoint* aFindLocation )
  573. {
  574. wxLogTrace( traceFindItem, wxT( " item " ) + GetSelectMenuText() );
  575. // Ignore the sheet file name if searching to replace.
  576. if( !(aSearchData.GetFlags() & FR_SEARCH_REPLACE)
  577. && SCH_ITEM::Matches( m_fileName, aSearchData ) )
  578. {
  579. if( aFindLocation )
  580. *aFindLocation = GetFileNamePosition();
  581. return true;
  582. }
  583. if( SCH_ITEM::Matches( m_name, aSearchData ) )
  584. {
  585. if( aFindLocation )
  586. *aFindLocation = GetSheetNamePosition();
  587. return true;
  588. }
  589. return false;
  590. }
  591. bool SCH_SHEET::Replace( wxFindReplaceData& aSearchData, void* aAuxData )
  592. {
  593. return EDA_ITEM::Replace( aSearchData, m_name );
  594. }
  595. void SCH_SHEET::renumberPins()
  596. {
  597. int id = 2;
  598. for( SCH_SHEET_PIN& pin : m_pins )
  599. {
  600. pin.SetNumber( id );
  601. id++;
  602. }
  603. }
  604. void SCH_SHEET::GetEndPoints( std::vector <DANGLING_END_ITEM>& aItemList )
  605. {
  606. for( unsigned ii = 0; ii < GetPins().size(); ii++ )
  607. {
  608. SCH_SHEET_PIN &pinsheet = GetPins()[ii];
  609. wxCHECK2_MSG( pinsheet.Type() == SCH_SHEET_PIN_T, continue,
  610. wxT( "Invalid item in schematic sheet pin list. Bad programmer!" ) );
  611. pinsheet.GetEndPoints( aItemList );
  612. }
  613. }
  614. bool SCH_SHEET::IsDanglingStateChanged( std::vector< DANGLING_END_ITEM >& aItemList )
  615. {
  616. bool currentState = IsDangling();
  617. for( SCH_SHEET_PIN& pinsheet : GetPins() )
  618. {
  619. pinsheet.IsDanglingStateChanged( aItemList );
  620. }
  621. return currentState != IsDangling();
  622. }
  623. bool SCH_SHEET::IsDangling() const
  624. {
  625. // If any hierarchical label in the sheet is dangling, then the sheet is dangling.
  626. for( size_t i = 0; i < GetPins().size(); i++ )
  627. {
  628. if( GetPins()[i].IsDangling() )
  629. return true;
  630. }
  631. return false;
  632. }
  633. bool SCH_SHEET::IsSelectStateChanged( const wxRect& aRect )
  634. {
  635. bool previousState = IsSelected();
  636. EDA_RECT boundingBox = GetBoundingBox();
  637. if( aRect.Intersects( boundingBox ) )
  638. SetFlags( SELECTED );
  639. else
  640. ClearFlags( SELECTED );
  641. return previousState != IsSelected();
  642. }
  643. void SCH_SHEET::GetConnectionPoints( std::vector< wxPoint >& aPoints ) const
  644. {
  645. for( size_t i = 0; i < GetPins().size(); i++ )
  646. aPoints.push_back( GetPins()[i].GetPosition() );
  647. }
  648. SEARCH_RESULT SCH_SHEET::Visit( INSPECTOR aInspector, void* testData, const KICAD_T aFilterTypes[] )
  649. {
  650. KICAD_T stype;
  651. for( const KICAD_T* p = aFilterTypes; (stype = *p) != EOT; ++p )
  652. {
  653. // If caller wants to inspect my type
  654. if( stype == Type() )
  655. {
  656. if( SEARCH_QUIT == aInspector( this, NULL ) )
  657. return SEARCH_QUIT;
  658. }
  659. else if( stype == SCH_SHEET_PIN_T )
  660. {
  661. // Test the sheet labels.
  662. for( size_t i = 0; i < m_pins.size(); i++ )
  663. {
  664. if( SEARCH_QUIT == aInspector( &m_pins[ i ], this ) )
  665. return SEARCH_QUIT;
  666. }
  667. }
  668. }
  669. return SEARCH_CONTINUE;
  670. }
  671. wxString SCH_SHEET::GetSelectMenuText() const
  672. {
  673. wxString tmp;
  674. tmp.Printf( _( "Hierarchical Sheet %s" ), GetChars( m_name ) );
  675. return tmp;
  676. }
  677. BITMAP_DEF SCH_SHEET::GetMenuImage() const
  678. {
  679. return add_hierarchical_subsheet_xpm;
  680. }
  681. bool SCH_SHEET::HitTest( const wxPoint& aPosition, int aAccuracy ) const
  682. {
  683. EDA_RECT rect = GetBoundingBox();
  684. rect.Inflate( aAccuracy );
  685. return rect.Contains( aPosition );
  686. }
  687. bool SCH_SHEET::HitTest( const EDA_RECT& aRect, bool aContained, int aAccuracy ) const
  688. {
  689. EDA_RECT rect = aRect;
  690. rect.Inflate( aAccuracy );
  691. if( aContained )
  692. return rect.Contains( GetBoundingBox() );
  693. return rect.Intersects( GetBoundingBox() );
  694. }
  695. wxPoint SCH_SHEET::GetResizePosition() const
  696. {
  697. return wxPoint( m_pos.x + m_size.GetWidth(), m_pos.y + m_size.GetHeight() );
  698. }
  699. void SCH_SHEET::GetNetListItem( NETLIST_OBJECT_LIST& aNetListItems,
  700. SCH_SHEET_PATH* aSheetPath )
  701. {
  702. SCH_SHEET_PATH sheetPath = *aSheetPath;
  703. sheetPath.push_back( this );
  704. for( size_t i = 0; i < m_pins.size(); i++ )
  705. {
  706. NETLIST_OBJECT* item = new NETLIST_OBJECT();
  707. item->m_SheetPathInclude = sheetPath;
  708. item->m_SheetPath = *aSheetPath;
  709. item->m_Comp = &m_pins[i];
  710. item->m_Link = this;
  711. item->m_Type = NET_SHEETLABEL;
  712. item->m_Label = m_pins[i].GetText();
  713. item->m_Start = item->m_End = m_pins[i].GetPosition();
  714. aNetListItems.push_back( item );
  715. if( IsBusLabel( m_pins[i].GetText() ) )
  716. item->ConvertBusToNetListItems( aNetListItems );
  717. }
  718. }
  719. void SCH_SHEET::Plot( PLOTTER* aPlotter )
  720. {
  721. COLOR4D txtcolor = COLOR4D::UNSPECIFIED;
  722. wxSize size;
  723. wxString Text;
  724. int name_orientation;
  725. wxPoint pos_sheetname, pos_filename;
  726. wxPoint pos;
  727. aPlotter->SetColor( GetLayerColor( GetLayer() ) );
  728. int thickness = GetPenSize();
  729. aPlotter->SetCurrentLineWidth( thickness );
  730. aPlotter->MoveTo( m_pos );
  731. pos = m_pos;
  732. pos.x += m_size.x;
  733. aPlotter->LineTo( pos );
  734. pos.y += m_size.y;
  735. aPlotter->LineTo( pos );
  736. pos = m_pos;
  737. pos.y += m_size.y;
  738. aPlotter->LineTo( pos );
  739. aPlotter->FinishTo( m_pos );
  740. if( IsVerticalOrientation() )
  741. {
  742. pos_sheetname = wxPoint( m_pos.x - 8, m_pos.y + m_size.y );
  743. pos_filename = wxPoint( m_pos.x + m_size.x + 4, m_pos.y + m_size.y );
  744. name_orientation = TEXT_ANGLE_VERT;
  745. }
  746. else
  747. {
  748. pos_sheetname = wxPoint( m_pos.x, m_pos.y - 4 );
  749. pos_filename = wxPoint( m_pos.x, m_pos.y + m_size.y + 4 );
  750. name_orientation = TEXT_ANGLE_HORIZ;
  751. }
  752. /* Draw texts: SheetName */
  753. Text = m_name;
  754. size = wxSize( m_sheetNameSize, m_sheetNameSize );
  755. //pos = m_pos; pos.y -= 4;
  756. thickness = GetDefaultLineThickness();
  757. thickness = Clamp_Text_PenSize( thickness, size, false );
  758. aPlotter->SetColor( GetLayerColor( LAYER_SHEETNAME ) );
  759. bool italic = false;
  760. aPlotter->Text( pos_sheetname, txtcolor, Text, name_orientation, size,
  761. GR_TEXT_HJUSTIFY_LEFT, GR_TEXT_VJUSTIFY_BOTTOM,
  762. thickness, italic, false );
  763. /*Draw texts : FileName */
  764. Text = GetFileName();
  765. size = wxSize( m_fileNameSize, m_fileNameSize );
  766. thickness = GetDefaultLineThickness();
  767. thickness = Clamp_Text_PenSize( thickness, size, false );
  768. aPlotter->SetColor( GetLayerColor( LAYER_SHEETFILENAME ) );
  769. aPlotter->Text( pos_filename, txtcolor, Text, name_orientation, size,
  770. GR_TEXT_HJUSTIFY_LEFT, GR_TEXT_VJUSTIFY_TOP,
  771. thickness, italic, false );
  772. aPlotter->SetColor( GetLayerColor( GetLayer() ) );
  773. /* Draw texts : SheetLabel */
  774. for( size_t i = 0; i < m_pins.size(); i++ )
  775. {
  776. m_pins[i].Plot( aPlotter );
  777. }
  778. }
  779. SCH_ITEM& SCH_SHEET::operator=( const SCH_ITEM& aItem )
  780. {
  781. wxLogDebug( wxT( "Sheet assignment operator." ) );
  782. wxCHECK_MSG( Type() == aItem.Type(), *this,
  783. wxT( "Cannot assign object type " ) + aItem.GetClass() + wxT( " to type " ) +
  784. GetClass() );
  785. if( &aItem != this )
  786. {
  787. SCH_ITEM::operator=( aItem );
  788. SCH_SHEET* sheet = (SCH_SHEET*) &aItem;
  789. m_pos = sheet->m_pos;
  790. m_size = sheet->m_size;
  791. m_name = sheet->m_name;
  792. m_sheetNameSize = sheet->m_sheetNameSize;
  793. m_fileNameSize = sheet->m_fileNameSize;
  794. m_pins = sheet->m_pins;
  795. // Ensure sheet labels have their #m_Parent member pointing really on their
  796. // parent, after assigning.
  797. for( SCH_SHEET_PIN& sheetPin : m_pins )
  798. {
  799. sheetPin.SetParent( this );
  800. }
  801. }
  802. return *this;
  803. }
  804. #if defined(DEBUG)
  805. void SCH_SHEET::Show( int nestLevel, std::ostream& os ) const
  806. {
  807. // XML output:
  808. wxString s = GetClass();
  809. NestedSpace( nestLevel, os ) << '<' << s.Lower().mb_str() << ">" << " sheet_name=\""
  810. << TO_UTF8( m_name ) << '"' << ">\n";
  811. // show all the pins, and check the linked list integrity
  812. for( const SCH_SHEET_PIN& label : m_pins )
  813. {
  814. label.Show( nestLevel + 1, os );
  815. }
  816. NestedSpace( nestLevel, os ) << "</" << s.Lower().mb_str() << ">\n" << std::flush;
  817. }
  818. #endif