KiCad PCB EDA Suite
sch_sheet_pin.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2006 Jean-Pierre Charras, jaen-pierre.charras@gipsa-lab.inpg.com
5  * Copyright (C) 1992-2011 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 
30 #include <fctsys.h>
31 #include <gr_basic.h>
32 #include <class_drawpanel.h>
33 #include <drawtxt.h>
34 #include <plot_common.h>
35 #include <trigo.h>
36 #include <richio.h>
37 #include <schframe.h>
38 #include <bitmaps.h>
39 
40 #include <general.h>
41 #include <sch_sheet.h>
42 #include <kicad_string.h>
43 
44 
45 SCH_SHEET_PIN::SCH_SHEET_PIN( SCH_SHEET* parent, const wxPoint& pos, const wxString& text ) :
46  SCH_HIERLABEL( pos, text, SCH_SHEET_PIN_T )
47 {
48  SetParent( parent );
49  wxASSERT( parent );
51 
52  SetTextPos( pos );
53 
54  if( parent->IsVerticalOrientation() )
56  else
58 
60  m_isDangling = true;
61  m_number = 2;
62 }
63 
64 
66 {
67  return new SCH_SHEET_PIN( *this );
68 }
69 
70 
72  wxDC* aDC,
73  const wxPoint& aOffset,
74  GR_DRAWMODE aDraw_mode,
75  COLOR4D aColor )
76 {
77  // The icon selection is handle by the virtual method CreateGraphicShape
78  // called by ::Draw
79  SCH_HIERLABEL::Draw( aPanel, aDC, aOffset, aDraw_mode, aColor );
80 }
81 
82 
84 {
85  wxCHECK_RET( aItem->Type() == SCH_SHEET_PIN_T,
86  wxString::Format( wxT( "SCH_SHEET_PIN object cannot swap data with %s object." ),
87  GetChars( aItem->GetClass() ) ) );
88 
89  SCH_SHEET_PIN* pin = ( SCH_SHEET_PIN* ) aItem;
90  SCH_TEXT::SwapData( (SCH_TEXT*) pin );
91  int tmp = pin->GetNumber();
92  pin->SetNumber( GetNumber() );
93  SetNumber( tmp );
94  SHEET_SIDE stmp = pin->GetEdge();
95  pin->SetEdge( GetEdge() );
96  SetEdge( stmp );
97 }
98 
99 
100 bool SCH_SHEET_PIN::operator==( const SCH_SHEET_PIN* aPin ) const
101 {
102  return aPin == this;
103 }
104 
105 
107 {
108  return GetDefaultLineThickness();
109 }
110 
111 
112 void SCH_SHEET_PIN::SetNumber( int aNumber )
113 {
114  wxASSERT( aNumber >= 2 );
115 
116  m_number = aNumber;
117 }
118 
119 
121 {
122  SCH_SHEET* Sheet = GetParent();
123 
124  // use SHEET_UNDEFINED_SIDE to adjust text orientation without changing edge
125 
126  switch( aEdge )
127  {
128  case SHEET_LEFT_SIDE:
129  m_edge = aEdge;
130  SetTextX( Sheet->m_pos.x );
131  SetLabelSpinStyle( 2 ); // Orientation horiz inverse
132  break;
133 
134  case SHEET_RIGHT_SIDE:
135  m_edge = aEdge;
136  SetTextX( Sheet->m_pos.x + Sheet->m_size.x );
137  SetLabelSpinStyle( 0 ); // Orientation horiz normal
138  break;
139 
140  case SHEET_TOP_SIDE:
141  m_edge = aEdge;
142  SetTextY( Sheet->m_pos.y );
143  SetLabelSpinStyle( 3 ); // Orientation vert BOTTOM
144  break;
145 
146  case SHEET_BOTTOM_SIDE:
147  m_edge = aEdge;
148  SetTextY( Sheet->m_pos.y + Sheet->m_size.y );
149  SetLabelSpinStyle( 1 ); // Orientation vert UP
150  break;
151 
152  default:
153  break;
154  }
155 }
156 
157 
159 {
160  return m_edge;
161 }
162 
163 
165 {
166  SCH_SHEET* sheet = GetParent();
167 
168  if( sheet == NULL )
169  return;
170 
171  wxPoint center = sheet->m_pos + ( sheet->m_size / 2 );
172 
174  {
175  if( Pos.x > center.x )
176  {
178  }
179  else
180  {
182  }
183 
184  SetTextY( Pos.y );
185 
186  if( GetTextPos().y < sheet->m_pos.y )
187  SetTextY( sheet->m_pos.y );
188 
189  if( GetTextPos().y > (sheet->m_pos.y + sheet->m_size.y) )
190  SetTextY( sheet->m_pos.y + sheet->m_size.y );
191  }
192  else // vertical sheetpin
193  {
194  if( Pos.y > center.y )
195  {
196  SetEdge( SHEET_BOTTOM_SIDE ); //bottom
197  }
198  else
199  {
200  SetEdge( SHEET_TOP_SIDE ); //top
201  }
202 
203  SetTextX( Pos.x );
204 
205  if( GetTextPos().x < sheet->m_pos.x )
206  SetTextX( sheet->m_pos.x );
207 
208  if( GetTextPos().x > (sheet->m_pos.x + sheet->m_size.x) )
209  SetTextX( sheet->m_pos.x + sheet->m_size.x );
210  }
211 }
212 
213 
214 bool SCH_SHEET_PIN::Save( FILE* aFile ) const
215 {
216  int type = 'U', side = 'L';
217 
218  if( m_Text.IsEmpty() )
219  return true;
220 
221  switch( m_edge )
222  {
223  default:
224  case SHEET_LEFT_SIDE: //pin on left side
225  side = 'L';
226  break;
227 
228  case SHEET_RIGHT_SIDE: //pin on right side
229  side = 'R';
230  break;
231 
232  case SHEET_TOP_SIDE: //pin on top side
233  side = 'T';
234  break;
235 
236  case SHEET_BOTTOM_SIDE: //pin on bottom side
237  side = 'B';
238  break;
239  }
240 
241  switch( m_shape )
242  {
243  case NET_INPUT:
244  type = 'I'; break;
245 
246  case NET_OUTPUT:
247  type = 'O'; break;
248 
249  case NET_BIDI:
250  type = 'B'; break;
251 
252  case NET_TRISTATE:
253  type = 'T'; break;
254 
255  case NET_UNSPECIFIED:
256  type = 'U'; break;
257  }
258 
259  if( fprintf( aFile, "F%d %s %c %c %-3d %-3d %-3d\n", m_number,
260  EscapedUTF8( m_Text ).c_str(), // supplies wrapping quotes
261  type, side, GetTextPos().x, GetTextPos().y,
262  GetTextWidth() ) == EOF )
263  {
264  return false;
265  }
266 
267  return true;
268 }
269 
270 
271 bool SCH_SHEET_PIN::Load( LINE_READER& aLine, wxString& aErrorMsg )
272 {
273  int x, y, size;
274  char number[256];
275  char name[256];
276  char connectType[256];
277  char sheetSide[256];
278  char* line = aLine.Line();
279  char* cp;
280 
281  static const char delims[] = " \t";
282 
283  // Read coordinates.
284  // D( printf( "line: \"%s\"\n", line );)
285 
286  cp = strtok( line, delims );
287 
288  strncpy( number, cp, sizeof(number) );
289  number[sizeof(number)-1] = 0;
290 
291  cp += strlen( number ) + 1;
292 
293  cp += ReadDelimitedText( name, cp, sizeof(name) );
294 
295  cp = strtok( cp, delims );
296  strncpy( connectType, cp, sizeof(connectType) );
297  connectType[sizeof(connectType)-1] = 0;
298 
299  cp = strtok( NULL, delims );
300  strncpy( sheetSide, cp, sizeof(sheetSide) );
301  sheetSide[sizeof(sheetSide)-1] = 0;
302 
303  cp += strlen( sheetSide ) + 1;
304 
305  int r = sscanf( cp, "%d %d %d", &x, &y, &size );
306  if( r != 3 )
307  {
308  aErrorMsg.Printf( wxT( "Eeschema file sheet hierarchical label error at line %d.\n" ),
309  aLine.LineNumber() );
310 
311  aErrorMsg << FROM_UTF8( line );
312  return false;
313  }
314 
315  m_Text = FROM_UTF8( name );
316 
317  if( size == 0 )
318  size = GetDefaultTextSize();
319 
320  SetTextSize( wxSize( size, size ) );
321  SetTextPos( wxPoint( x, y ) );
322 
323  switch( connectType[0] )
324  {
325  case 'I':
326  m_shape = NET_INPUT;
327  break;
328 
329  case 'O':
331  break;
332 
333  case 'B':
334  m_shape = NET_BIDI;
335  break;
336 
337  case 'T':
339  break;
340 
341  case 'U':
343  break;
344  }
345 
346  switch( sheetSide[0] )
347  {
348  case 'R' : // pin on right side
350  break;
351 
352  case 'T' : // pin on top side
354  break;
355 
356  case 'B' : // pin on bottom side
358  break;
359 
360  case 'L' : // pin on left side
361  default :
363  break;
364  }
365 
366  return true;
367 }
368 
369 
370 bool SCH_SHEET_PIN::Matches( wxFindReplaceData& aSearchData,
371  void* aAuxData, wxPoint* aFindLocation )
372 {
373  wxCHECK_MSG( GetParent() != NULL, false,
374  wxT( "Sheet pin " ) + m_Text + wxT( " does not have a parent sheet!" ) );
375 
376  wxLogTrace( traceFindItem, wxT( " child item " ) + GetSelectMenuText() );
377 
378  if( SCH_ITEM::Matches( m_Text, aSearchData ) )
379  {
380  if( aFindLocation )
381  *aFindLocation = GetBoundingBox().Centre();
382 
383  return true;
384  }
385 
386  return false;
387 }
388 
389 
390 void SCH_SHEET_PIN::MirrorX( int aXaxis_position )
391 {
392  int p = GetTextPos().y - aXaxis_position;
393 
394  SetTextY( aXaxis_position - p );
395 
396  switch( m_edge )
397  {
398  case SHEET_TOP_SIDE:
400  break;
401 
402  case SHEET_BOTTOM_SIDE:
404  break;
405 
406  default:
407  break;
408  }
409 }
410 
411 
412 void SCH_SHEET_PIN::MirrorY( int aYaxis_position )
413 {
414  int p = GetTextPos().x - aYaxis_position;
415 
416  SetTextX( aYaxis_position - p );
417 
418  switch( m_edge )
419  {
420  case SHEET_LEFT_SIDE:
422  break;
423 
424  case SHEET_RIGHT_SIDE:
426  break;
427 
428  default:
429  break;
430  }
431 }
432 
433 
435 {
436  wxPoint pt = GetTextPos();
437  RotatePoint( &pt, aPosition, 900 );
438  SetTextPos( pt );
439 
440  switch( m_edge )
441  {
442  case SHEET_LEFT_SIDE: //pin on left side
444  break;
445 
446  case SHEET_RIGHT_SIDE: //pin on right side
448  break;
449 
450  case SHEET_TOP_SIDE: //pin on top side
452  break;
453 
454  case SHEET_BOTTOM_SIDE: //pin on bottom side
456  break;
457 
458  default:
459  break;
460  }
461 }
462 
463 
464 void SCH_SHEET_PIN::CreateGraphicShape( std::vector <wxPoint>& aPoints, const wxPoint& aPos )
465 {
466  /* This is the same icon shapes as SCH_HIERLABEL
467  * but the graphic icon is slightly different in 2 cases:
468  * for INPUT type the icon is the OUTPUT shape of SCH_HIERLABEL
469  * for OUTPUT type the icon is the INPUT shape of SCH_HIERLABEL
470  */
472 
473  switch( m_shape )
474  {
475  case NET_INPUT:
477  break;
478 
479  case NET_OUTPUT:
480  m_shape = NET_INPUT;
481  break;
482 
483  default:
484  break;
485  }
486 
487  SCH_HIERLABEL::CreateGraphicShape( aPoints, aPos );
488  m_shape = tmp;
489 }
490 
491 
492 void SCH_SHEET_PIN::GetEndPoints( std::vector <DANGLING_END_ITEM>& aItemList )
493 {
495  aItemList.push_back( item );
496 }
497 
498 
500 {
501  wxString tmp;
502  tmp.Printf( _( "Hierarchical Sheet Pin %s" ), GetChars( ShortenedShownText() ) );
503  return tmp;
504 }
505 
507 {
508  return add_hierar_pin_xpm;
509 }
510 
511 
512 bool SCH_SHEET_PIN::HitTest( const wxPoint& aPoint, int aAccuracy ) const
513 {
514  EDA_RECT rect = GetBoundingBox();
515 
516  rect.Inflate( aAccuracy );
517 
518  return rect.Contains( aPoint );
519 }
520 
521 
522 #if defined(DEBUG)
523 
524 void SCH_SHEET_PIN::Show( int nestLevel, std::ostream& os ) const
525 {
526  // XML output:
527  wxString s = GetClass();
528 
529  NestedSpace( nestLevel, os ) << '<' << s.Lower().mb_str() << ">"
530  << " pin_name=\"" << TO_UTF8( m_Text )
531  << '"' << "/>\n" << std::flush;
532 
533 // NestedSpace( nestLevel, os ) << "</" << s.Lower().mb_str() << ">\n";
534 }
535 
536 #endif
bool HitTest(const wxPoint &aPosition, int aAccuracy) const override
Function HitTest tests if aPosition is contained within or on the bounding box of an item...
Definition of the SCH_SHEET class for Eeschema.
KICAD_T Type() const
Function Type()
Definition: base_struct.h:198
SCH_LAYER_ID m_Layer
const wxString traceFindItem
Flag to enable find item tracing using the WXTRACE environment variable.
Class LINE_READER is an abstract class from which implementation specific LINE_READERs may be derived...
Definition: richio.h:81
SCH_SHEET * GetParent() const
Get the parent sheet object of this sheet pin.
Definition: sch_sheet.h:161
wxString m_Text
Definition: eda_text.h:346
BITMAP_DEF GetMenuImage() const override
Function GetMenuImage returns a pointer to an image to be used in menus.
virtual unsigned LineNumber() const
Function Line Number returns the line number of the last line read from this LINE_READER.
Definition: richio.h:159
wxString ShortenedShownText() const
Returns a shortened version (max 15 characters) of the shown text.
Definition: eda_text.cpp:92
PNG memory record (file in memory).
Definition: bitmap_types.h:38
static wxString FROM_UTF8(const char *cstring)
function FROM_UTF8 converts a UTF8 encoded C string to a wxString for all wxWidgets build modes...
Definition: macros.h:53
const wxPoint & GetTextPos() const
Definition: eda_text.h:224
EDA_ITEM * Clone() const override
Function Clone creates a duplicate of this item with linked list members set to NULL.
SCH_SHEET_PIN(SCH_SHEET *parent, const wxPoint &pos=wxPoint(0, 0), const wxString &text=wxEmptyString)
void MirrorY(int aYaxis_position) override
Function MirrorY mirrors item relative to the Y axis about aYaxis_position.
bool Contains(const wxPoint &aPoint) const
Function Contains.
bool m_isDangling
True if not connected to another object if the object derive from SCH_TEXT supports connections...
Definition: sch_text.h:65
virtual bool Matches(wxFindReplaceData &aSearchData, void *aAuxData, wxPoint *aFindLocation)
Function Matches compares the item against the search criteria in aSearchData.
Definition: base_struct.h:400
int GetTextWidth() const
Definition: eda_text.h:218
int GetDefaultLineThickness()
Default line thickness used to draw/plot items having a default thickness line value (i...
void SetTextPos(const wxPoint &aPoint)
Definition: eda_text.h:223
SHEET_SIDE GetEdge() const
wxSize m_size
The size of the sheet.
Definition: sch_sheet.h:245
wxString GetClass() const override
Function GetClass returns the class name.
Definition: sch_sheet.h:100
bool Save(FILE *aFile) const override
Function Save writes the data structures for this object out to a FILE in "*.sch" format...
void SetTextSize(const wxSize &aNewSize)
Definition: eda_text.h:214
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:317
bool operator==(const SCH_SHEET_PIN *aPin) const
void MirrorX(int aXaxis_position) override
Function MirrorX mirrors item relative to the X axis about aXaxis_position.
wxPoint m_pos
The position of the sheet.
Definition: sch_sheet.h:242
void CreateGraphicShape(std::vector< wxPoint > &aPoints, const wxPoint &aPos) override
Function CreateGraphicShape (virtual) Calculates the graphic shape (a polygon) associated to the text...
void SetTextX(int aX)
Definition: eda_text.h:226
#define TO_UTF8(wxstring)
Macro TO_UTF8 converts a wxString to a UTF8 encoded C string for all wxWidgets build modes...
Definition: macros.h:47
std::string EscapedUTF8(const wxString &aString)
Function EscapedUTF8 returns an 8 bit UTF8 string given aString in unicode form.
Definition: string.cpp:137
void SetParent(EDA_ITEM *aParent)
Definition: base_struct.h:213
GR_DRAWMODE
Drawmode. Compositing mode plus a flag or two.
Definition: gr_basic.h:41
char * Line() const
Function Line returns a pointer to the last line that was read in.
Definition: richio.h:139
void Draw(EDA_DRAW_PANEL *panel, wxDC *DC, const wxPoint &offset, GR_DRAWMODE draw_mode, COLOR4D Color=COLOR4D::UNSPECIFIED) override
Function Draw Draw a schematic item.
Definition: sch_text.cpp:1635
bool Load(LINE_READER &aLine, wxString &aErrorMsg) override
Function Load reads a schematic item from aLine in a .sch file.
void SetEdge(SHEET_SIDE aEdge)
int GetNumber() const
Get the sheet label number.
Definition: sch_sheet.h:135
Common plot library Plot settings, and plotting engines (Postscript, Gerber, HPGL and DXF) ...
const EDA_RECT GetBoundingBox() const override
Function GetBoundingBox returns the orthogonal, bounding box of this object for display purposes...
Definition: sch_text.cpp:1702
Class SCH_SHEET_PIN defines a sheet pin (label) used in sheets to create hierarchical schematics...
Definition: sch_sheet.h:62
wxPoint Centre() const
void ConstrainOnEdge(wxPoint Pos)
Function ConstrainOnEdge is used to adjust label position to edge based on proximity to vertical / ho...
const char * delims
Class SCH_SHEET is the sheet symbol placed in a schematic, and is the entry point for a sub schematic...
Definition: sch_sheet.h:216
wxString GetSelectMenuText() const override
Function GetSelectMenuText returns the text to display to be used in the selection clarification cont...
void Draw(EDA_DRAW_PANEL *aPanel, wxDC *aDC, const wxPoint &aOffset, GR_DRAWMODE aDrawMode, COLOR4D aColor=COLOR4D::UNSPECIFIED) override
Function Draw Draw a schematic item.
void CreateGraphicShape(std::vector< wxPoint > &aPoints, const wxPoint &Pos) override
Function CreateGraphicShape Calculates the graphic shape (a polygon) associated to the text...
Definition: sch_text.cpp:1679
void SetLabelSpinStyle(int aSpinStyle) override
Function SetLabelSpinStyle sets a spin or rotation angle, along with specific horizontal and vertical...
Definition: sch_text.cpp:1601
PINSHEETLABEL_SHAPE
Definition: sch_text.h:46
SHEET_SIDE
Defines the edge of the sheet that the sheet pin is positioned SHEET_LEFT_SIDE = 0: pin on left side ...
Definition: sch_sheet.h:74
static const wxChar * GetChars(const wxString &s)
Function GetChars returns a wxChar* to the actual wxChar* data within a wxString, and is helpful for ...
Definition: macros.h:92
void SetNumber(int aNumber)
Set the sheet label number.
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
bool Matches(wxFindReplaceData &aSearchData, void *aAuxData, wxPoint *aFindLocation) override
Function Matches compares the item against the search criteria in aSearchData.
void GetEndPoints(std::vector< DANGLING_END_ITEM > &aItemList) override
Function GetEndPoints adds the schematic item end points to aItemList if the item has end points...
int m_number
Label number use for saving sheet label to file.
Definition: sch_sheet.h:84
virtual void SwapData(SCH_ITEM *aItem) override
Function SwapData swap the internal data structures aItem with the schematic item.
Definition: sch_text.cpp:311
Class EDA_RECT handles the component boundary box.
Class EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boa...
Definition: base_struct.h:151
bool IsVerticalOrientation() const
Definition: sch_sheet.cpp:404
void SwapData(SCH_ITEM *aItem) override
Function SwapData swap the internal data structures aItem with the schematic item.
const char * name
virtual wxString GetClass() const override
Function GetClass returns the class name.
This file is part of the common libary.
void Rotate(wxPoint aPosition) override
Function Rotate rotates the item around aPosition 90 degrees in the clockwise direction.
PINSHEETLABEL_SHAPE m_shape
Definition: sch_text.h:61
Class DANGLING_END_ITEM is a helper class used to store the state of schematic items that can be conn...
int ReadDelimitedText(wxString *aDest, const char *aSource)
Function ReadDelimitedText copies bytes from aSource delimited string segment to aDest wxString...
Definition: string.cpp:43
Class SCH_ITEM is a base class for any item which can be embedded within the SCHEMATIC container clas...
int GetPenSize() const override
Function GetPenSize virtual pure.
SHEET_SIDE m_edge
Definition: sch_sheet.h:89
EDA_RECT & Inflate(wxCoord dx, wxCoord dy)
Function Inflate inflates the rectangle horizontally by dx and vertically by dy.
void SetTextY(int aY)
Definition: eda_text.h:227
int GetDefaultTextSize()
Default size for text in general.
Class COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:39