KiCad PCB EDA Suite
class_board_item.cpp
Go to the documentation of this file.
1 
6 /*
7  * This program source code file is part of KiCad, a free EDA CAD application.
8  *
9  * Copyright (C) 2012 Jean-Pierre Charras, jean-pierre.charras@ujf-grenoble.fr
10  * Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
11  * Copyright (C) 1992-2016 KiCad Developers, see AUTHORS.txt for contributors.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, you may find one here:
25  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
26  * or you may search the http://www.gnu.org website for the version 2 license,
27  * or you may write to the Free Software Foundation, Inc.,
28  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
29  */
30 
31 #include <fctsys.h>
32 #include <common.h>
33 #include <pcbnew.h>
34 
35 #include <class_board.h>
36 #include <string>
37 
38 wxString BOARD_ITEM::ShowShape( STROKE_T aShape )
39 {
40  switch( aShape )
41  {
42  case S_SEGMENT: return _( "Line" );
43  case S_RECT: return _( "Rect" );
44  case S_ARC: return _( "Arc" );
45  case S_CIRCLE: return _( "Circle" );
46  case S_CURVE: return _( "Bezier Curve" );
47  case S_POLYGON: return _( "Polygon" );
48  default: return wxT( "??" );
49  }
50 }
51 
52 
54 {
56  wxASSERT( list );
57 
58  if( list )
59  list->Remove( this );
60 }
61 
62 
64 {
65  if( Type() == PCB_T )
66  return (BOARD*) this;
67 
68  BOARD_ITEM* parent = GetParent();
69 
70  if( parent )
71  return parent->GetBoard();
72 
73  return NULL;
74 }
75 
76 
77 wxString BOARD_ITEM::GetLayerName() const
78 {
79  BOARD* board = GetBoard();
80 
81  if( board )
82  return board->GetLayerName( m_Layer );
83 
84  // If no parent, return standard name
86 }
87 
88 
89 std::string BOARD_ITEM::FormatInternalUnits( int aValue )
90 {
91 #if 1
92 
93  char buf[50];
94  int len;
95  double mm = aValue / IU_PER_MM;
96 
97  if( mm != 0.0 && fabs( mm ) <= 0.0001 )
98  {
99  len = sprintf( buf, "%.10f", mm );
100 
101  while( --len > 0 && buf[len] == '0' )
102  buf[len] = '\0';
103 
104  if( buf[len] == '.' )
105  buf[len] = '\0';
106  else
107  ++len;
108  }
109  else
110  {
111  len = sprintf( buf, "%.10g", mm );
112  }
113 
114  return std::string( buf, len );
115 
116 #else
117 
118  // Assume aValue is in nanometers, and that we want the result in millimeters,
119  // and that int is 32 bits wide. Then perform an alternative algorithm.
120  // Can be used to verify that the above algorithm is correctly generating text.
121  // Convert aValue into an integer string, then insert a decimal point manually.
122  // Results are the same as above general purpose algorithm.
123 
124  wxASSERT( sizeof(int) == 4 );
125 
126  if( aValue == 0 )
127  return std::string( 1, '0' );
128  else
129  {
130  char buf[50];
131  int len = sprintf( buf, aValue > 0 ? "%06d" : "%07d", aValue ); // optionally pad w/leading zeros
132 
133  std::string ret( buf, len );
134 
135  std::string::iterator it = ret.end() - 1; // last byte
136 
137  // insert '.' at 6 positions from end, dividing by 10e6 (a million), nm => mm
138  std::string::iterator decpoint = ret.end() - 6;
139 
140  // truncate trailing zeros, up to decimal point position
141  for( ; *it=='0' && it >= decpoint; --it )
142  ret.erase( it ); // does not invalidate iterators it or decpoint
143 
144  if( it >= decpoint )
145  {
146  ret.insert( decpoint, '.' );
147 
148  // decpoint is invalidated here, after insert()
149 
150 #if 1 // want a leading zero when decimal point is in first position?
151  if( ret[0] == '.' )
152  {
153  // insert leading zero ahead of decimal point.
154  ret.insert( ret.begin(), '0' );
155  }
156  else if( ret[0]=='-' && ret[1]=='.' )
157  {
158  ret.insert( ret.begin() + 1, '0' );
159  }
160 #endif
161  }
162 
163  return ret;
164  }
165 
166 #endif
167 }
168 
169 
170 std::string BOARD_ITEM::FormatAngle( double aAngle )
171 {
172  char temp[50];
173 
174  int len = snprintf( temp, sizeof(temp), "%.10g", aAngle / 10.0 );
175 
176  return std::string( temp, len );
177 }
178 
179 
180 std::string BOARD_ITEM::FormatInternalUnits( const wxPoint& aPoint )
181 {
182  return FormatInternalUnits( aPoint.x ) + " " + FormatInternalUnits( aPoint.y );
183 }
184 
185 
186 std::string BOARD_ITEM::FormatInternalUnits( const wxSize& aSize )
187 {
188  return FormatInternalUnits( aSize.GetWidth() ) + " " + FormatInternalUnits( aSize.GetHeight() );
189 }
190 
191 
192 void BOARD_ITEM::ViewGetLayers( int aLayers[], int& aCount ) const
193 {
194  // Basic fallback
195  aCount = 1;
196  aLayers[0] = m_Layer;
197 }
198 
199 
200 int BOARD_ITEM::getTrailingInt( wxString aStr )
201 {
202  int number = 0;
203  int base = 1;
204 
205  // Trim and extract the trailing numeric part
206  int index = aStr.Len() - 1;
207  while( index >= 0 )
208  {
209  const char chr = aStr.GetChar( index );
210 
211  if( chr < '0' || chr > '9' )
212  break;
213 
214  number += ( chr - '0' ) * base;
215  base *= 10;
216  index--;
217  }
218 
219  return number;
220 }
221 
222 
223 int BOARD_ITEM::getNextNumberInSequence( const std::set<int>& aSeq, bool aFillSequenceGaps)
224 {
225  if( aSeq.empty() )
226  return 1;
227 
228  // By default go to the end of the sequence
229  int candidate = *aSeq.rbegin();
230 
231  // Filling in gaps in pad numbering
232  if( aFillSequenceGaps )
233  {
234  // start at the beginning
235  candidate = *aSeq.begin();
236 
237  for( auto it : aSeq )
238  {
239  if( it - candidate > 1 )
240  break;
241 
242  candidate = it;
243  }
244  }
245 
246  ++candidate;
247 
248  return candidate;
249 }
250 
251 
253 {
254  auto parent = GetParent();
255 
256  if( parent && GetList() )
257  parent->Remove( this );
258 
259  delete this;
260 }
DHEAD * GetList() const
Definition: base_struct.h:209
KICAD_T Type() const
Function Type()
Definition: base_struct.h:198
BOARD_ITEM_CONTAINER * GetParent() const
static wxString ShowShape(STROKE_T aShape)
Function ShowShape converts the enum STROKE_T integer value to a wxString.
Definition: typeinfo.h:97
Class BOARD_ITEM is a base class for any item which can be embedded within the BOARD container class...
Class BOARD to handle a board.
polygon (not yet used for tracks, but could be in microwave apps)
virtual void ViewGetLayers(int aLayers[], int &aCount) const override
void DeleteStructure()
Function DeleteStructure deletes this object after UnLink()ing it from its owner if it has one...
usual segment : line with rounded ends
virtual void UnLink()
Function UnLink detaches this object from its owner.
const wxString GetLayerName(LAYER_ID aLayer) const
Function GetLayerName returns the name of a layer given by aLayer.
wxString GetLayerName() const
Function GetLayerName returns the name of the PCB layer on which the item resides.
static int getNextNumberInSequence(const std::set< int > &aSeq, bool aFillSequenceGaps)
static std::string FormatInternalUnits(int aValue)
Function FormatInternalUnits converts aValue from board internal units to a string appropriate for wr...
segment with non rounded ends
LAYER_ID m_Layer
static int getTrailingInt(wxString aStr)
static std::string FormatAngle(double aAngle)
Function FormatAngle converts aAngle from board units to a string appropriate for writing to file...
Arcs (with rounded ends)
STROKE_T
Enum STROKE_T is the set of shapes for segments (graphic segments and tracks) which are often in the ...
Bezier Curve.
Class BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:166
static wxString GetStandardLayerName(LAYER_ID aLayerId)
Function GetStandardLayerName returns an "English Standard" name of a PCB layer when given aLayerNumb...
Definition: class_board.h:646
The common library.
virtual BOARD * GetBoard() const
Function GetBoard returns the BOARD in which this BOARD_ITEM resides, or NULL if none.