KiCad PCB EDA Suite
class_board.cpp File Reference

BOARD class functions. More...

#include <limits.h>
#include <algorithm>
#include <iterator>
#include <fctsys.h>
#include <common.h>
#include <kicad_string.h>
#include <pcb_base_frame.h>
#include <msgpanel.h>
#include <pcb_netlist.h>
#include <reporter.h>
#include <base_units.h>
#include <ratsnest_data.h>
#include <ratsnest_viewitem.h>
#include <worksheet_viewitem.h>
#include <pcbnew.h>
#include <collectors.h>
#include <class_board.h>
#include <class_module.h>
#include <class_track.h>
#include <class_zone.h>
#include <class_marker_pcb.h>
#include <class_drawsegment.h>
#include <class_pcb_text.h>
#include <class_pcb_target.h>
#include <class_dimension.h>
#include <connectivity_data.h>
#include <stdio.h>

Go to the source code of this file.

Functions

static void removeTrack (TRACKS *aList, TRACK *aOneToRemove)
 Function removeTrack removes aOneToRemove from aList, which is a non-owning std::vector. More...
 
static void otherEnd (const TRACK &aTrack, const wxPoint &aNotThisEnd, wxPoint *aOtherEnd)
 
static int find_vias_and_tracks_at (TRACKS &at_next, TRACKS &in_net, LSET &lset, const wxPoint &next)
 Function find_vias_and_tracks_at collects TRACKs and VIAs at aPos and returns the track_count which excludes vias. More...
 
static void checkConnectedTo (BOARD *aBoard, TRACKS *aList, const TRACKS &aTracksInNet, const wxPoint &aGoal, const wxPoint &aStart, TRACK *aFirstTrack)
 Function checkConnectedTo returns if aTracksInNet contains a copper pathway to aGoal when starting with aFirstTrack. More...
 
static bool sortNetsByNodes (const NETINFO_ITEM *a, const NETINFO_ITEM *b)
 
static bool sortNetsByNames (const NETINFO_ITEM *a, const NETINFO_ITEM *b)
 
bool sortPadsByXthenYCoord (D_PAD *const &ref, D_PAD *const &comp)
 Function SortPadsByXCoord is used by GetSortedPadListByXCoord to Sort a pad list by x coordinate value. More...
 
bool BuildBoardPolygonOutlines (BOARD *aBoard, SHAPE_POLY_SET &aOutlines, wxString *aErrorText, unsigned int aTolerance)
 

Variables

static COLORS_DESIGN_SETTINGS dummyColorsSettings (FRAME_PCB)
 
static std::vector< int > padCountListByNet
 

Detailed Description

BOARD class functions.

Definition in file class_board.cpp.

Function Documentation

bool BuildBoardPolygonOutlines ( BOARD aBoard,
SHAPE_POLY_SET aOutlines,
wxString *  aErrorText,
unsigned int  aTolerance 
)

Definition at line 562 of file convert_drawsegment_list_to_polygon.cpp.

Referenced by BOARD::Duplicate(), and BOARD::GetBoardPolygonOutlines().

564 {
565  PCB_TYPE_COLLECTOR items;
566 
567  // Get all the DRAWSEGMENTS and module graphics into 'items',
568  // then keep only those on layer == Edge_Cuts.
569  static const KICAD_T scan_graphics[] = { PCB_LINE_T, PCB_MODULE_EDGE_T, EOT };
570  items.Collect( aBoard, scan_graphics );
571 
572  // Make a working copy of aSegList, because the list is modified during calculations
573  std::vector< DRAWSEGMENT* > segList;
574 
575  for( int ii = 0; ii < items.GetCount(); ii++ )
576  {
577  if( items[ii]->GetLayer() == Edge_Cuts )
578  segList.push_back( static_cast< DRAWSEGMENT* >( items[ii] ) );
579  }
580 
581  bool success = ConvertOutlineToPolygon( segList, aOutlines, aErrorText, aTolerance );
582 
583  if( !success || !aOutlines.OutlineCount() )
584  {
585  // Creates a valid polygon outline is not possible.
586  // So uses the board edge cuts bounding box to create a
587  // rectangular outline
588  // When no edge cuts items, build a contour
589  // from global bounding box
590 
591  EDA_RECT bbbox = aBoard->GetBoardEdgesBoundingBox();
592 
593  // If null area, uses the global bounding box.
594  if( ( bbbox.GetWidth() ) == 0 || ( bbbox.GetHeight() == 0 ) )
595  bbbox = aBoard->ComputeBoundingBox();
596 
597  // Ensure non null area. If happen, gives a minimal size.
598  if( ( bbbox.GetWidth() ) == 0 || ( bbbox.GetHeight() == 0 ) )
599  bbbox.Inflate( Millimeter2iu( 1.0 ) );
600 
601  aOutlines.RemoveAllContours();
602  aOutlines.NewOutline();
603 
604  wxPoint corner;
605  aOutlines.Append( bbbox.GetOrigin() );
606 
607  corner.x = bbbox.GetOrigin().x;
608  corner.y = bbbox.GetEnd().y;
609  aOutlines.Append( corner );
610 
611  aOutlines.Append( bbbox.GetEnd() );
612 
613  corner.x = bbbox.GetEnd().x;
614  corner.y = bbbox.GetOrigin().y;
615  aOutlines.Append( corner );
616  }
617 
618  return success;
619 }
int GetCount() const
Function GetCount returns the number of objects in the list.
Definition: collector.h:106
bool ConvertOutlineToPolygon(std::vector< DRAWSEGMENT * > &aSegList, SHAPE_POLY_SET &aPolygons, wxString *aErrorText, unsigned int aTolerance)
Function ConvertOutlineToPolygon build a polygon (with holes) from a DRAWSEGMENT list, which is expected to be a outline, therefore a closed main outline with perhaps closed inner outlines.
const wxPoint GetOrigin() const
Definition: eda_rect.h:112
EDA_RECT ComputeBoundingBox(bool aBoardEdgesOnly=false) const
Function ComputeBoundingBox calculates the bounding box containing all board items (or board edge seg...
int GetHeight() const
Definition: eda_rect.h:118
int OutlineCount() const
Returns the number of outlines in the set
class EDGE_MODULE, a footprint edge
Definition: typeinfo.h:94
search types array terminator (End Of Types)
Definition: typeinfo.h:82
KICAD_T
Enum KICAD_T is the set of class identification values, stored in EDA_ITEM::m_StructType.
Definition: typeinfo.h:78
const EDA_RECT GetBoardEdgesBoundingBox() const
Function GetBoardEdgesBoundingBox Returns the board bounding box calculated using exclusively the boa...
Definition: class_board.h:801
int NewOutline()
Creates a new empty polygon in the set and returns its index
void Collect(BOARD_ITEM *aBoard, const KICAD_T aScanList[])
Collect BOARD_ITEM objects using this class&#39;s Inspector method, which does the collection.
Definition: collectors.cpp:498
const wxPoint GetEnd() const
Definition: eda_rect.h:114
void RemoveAllContours()
Removes all outlines & holes (clears) the polygon set.
Class EDA_RECT handles the component boundary box.
Definition: eda_rect.h:44
int GetWidth() const
Definition: eda_rect.h:117
static bool GetLayer(MODEL_VRML &aModel, LAYER_NUM layer, VRML_LAYER **vlayer)
Collect all BOARD_ITEM objects of a given set of KICAD_T type(s).
Definition: collectors.h:590
class DRAWSEGMENT, a segment not on copper layers
Definition: typeinfo.h:91
EDA_RECT & Inflate(wxCoord dx, wxCoord dy)
Function Inflate inflates the rectangle horizontally by dx and vertically by dy.
int Append(int x, int y, int aOutline=-1, int aHole=-1, bool aAllowDuplication=false)
Appends a vertex at the end of the given outline/hole (default: the last outline) ...
static void checkConnectedTo ( BOARD aBoard,
TRACKS *  aList,
const TRACKS &  aTracksInNet,
const wxPoint aGoal,
const wxPoint aStart,
TRACK aFirstTrack 
)
static

Function checkConnectedTo returns if aTracksInNet contains a copper pathway to aGoal when starting with aFirstTrack.

aFirstTrack should have one end situated on aStart, and the traversal testing begins from the other end of aFirstTrack.

The function throws an exception instead of returning bool so that detailed information can be provided about a possible failure in the track layout.

Exceptions
IO_ERROR- if points are not connected, with text saying why.

Definition at line 286 of file class_board.cpp.

References find_vias_and_tracks_at(), BOARD_ITEM::FormatInternalUnits(), BOARD_ITEM::GetLayer(), BOARD::GetPadFast(), next(), otherEnd(), removeTrack(), StrPrintf(), and THROW_IO_ERROR.

Referenced by BOARD::TracksInNetBetweenPoints().

288 {
289  TRACKS in_net = aTracksInNet; // copy source list so the copy can be modified
290  wxPoint next;
291 
292  otherEnd( *aFirstTrack, aStart, &next );
293 
294  aList->push_back( aFirstTrack );
295  removeTrack( &in_net, aFirstTrack );
296 
297  LSET lset( aFirstTrack->GetLayer() );
298 
299  while( in_net.size() )
300  {
301  if( next == aGoal )
302  return; // success
303 
304  // Want an exact match on the position of next, i.e. pad at next,
305  // not a forgiving HitTest() with tolerance type of match, otherwise the overall
306  // algorithm will not work. GetPadFast() is an exact match as I write this.
307  if( aBoard->GetPadFast( next, lset ) )
308  {
309  std::string m = StrPrintf(
310  "intervening pad at:(xy %s) between start:(xy %s) and goal:(xy %s)",
311  BOARD_ITEM::FormatInternalUnits( next ).c_str(),
312  BOARD_ITEM::FormatInternalUnits( aStart ).c_str(),
313  BOARD_ITEM::FormatInternalUnits( aGoal ).c_str()
314  );
315  THROW_IO_ERROR( m );
316  }
317 
318  int track_count = find_vias_and_tracks_at( *aList, in_net, lset, next );
319 
320  if( track_count != 1 )
321  {
322  std::string m = StrPrintf(
323  "found %d tracks intersecting at (xy %s), exactly 2 would be acceptable.",
324  track_count + aList->size() == 1 ? 1 : 0,
325  BOARD_ITEM::FormatInternalUnits( next ).c_str()
326  );
327  THROW_IO_ERROR( m );
328  }
329 
330  // reduce lset down to the layer that the last track at 'next' is on.
331  lset = aList->back()->GetLayerSet();
332 
333  otherEnd( *aList->back(), next, &next );
334  }
335 
336  std::string m = StrPrintf(
337  "not enough tracks connecting start:(xy %s) and goal:(xy %s).",
338  BOARD_ITEM::FormatInternalUnits( aStart ).c_str(),
339  BOARD_ITEM::FormatInternalUnits( aGoal ).c_str()
340  );
341  THROW_IO_ERROR( m );
342 }
CITER next(CITER it)
Definition: ptree.cpp:130
static void removeTrack(TRACKS *aList, TRACK *aOneToRemove)
Function removeTrack removes aOneToRemove from aList, which is a non-owning std::vector.
D_PAD * GetPadFast(const wxPoint &aPosition, LSET aLayerMask)
Function GetPadFast return pad found at aPosition on aLayerMask using the fast search method...
static void otherEnd(const TRACK &aTrack, const wxPoint &aNotThisEnd, wxPoint *aOtherEnd)
virtual PCB_LAYER_ID GetLayer() const
Function GetLayer returns the primary layer this item is on.
static int find_vias_and_tracks_at(TRACKS &at_next, TRACKS &in_net, LSET &lset, const wxPoint &next)
Function find_vias_and_tracks_at collects TRACKs and VIAs at aPos and returns the track_count which e...
static std::string FormatInternalUnits(int aValue)
Function FormatInternalUnits converts aValue from board internal units to a string appropriate for wr...
int StrPrintf(std::string *result, const char *format,...)
Function StrPrintf is like sprintf() but the output is appended to a std::string instead of to a char...
Definition: richio.cpp:74
Class LSET is a set of PCB_LAYER_IDs.
#define THROW_IO_ERROR(msg)
Definition: ki_exception.h:38
static int find_vias_and_tracks_at ( TRACKS &  at_next,
TRACKS &  in_net,
LSET lset,
const wxPoint next 
)
static

Function find_vias_and_tracks_at collects TRACKs and VIAs at aPos and returns the track_count which excludes vias.

Definition at line 234 of file class_board.cpp.

References TRACK::GetEnd(), BOARD_ITEM::GetLayerSet(), TRACK::GetStart(), next(), PCB_VIA_T, and EDA_ITEM::Type().

Referenced by checkConnectedTo().

235 {
236  // first find all vias (in this net) at 'next' location, and expand LSET with each
237  for( TRACKS::iterator it = in_net.begin(); it != in_net.end(); )
238  {
239  TRACK* t = *it;
240 
241  if( t->Type() == PCB_VIA_T && (t->GetLayerSet() & lset).any() &&
242  ( t->GetStart() == next || t->GetEnd() == next ) )
243  {
244  lset |= t->GetLayerSet();
245  at_next.push_back( t );
246  it = in_net.erase( it );
247  }
248  else
249  ++it;
250  }
251 
252  int track_count = 0;
253 
254  // with expanded lset, find all tracks with an end on any of the layers in lset
255  for( TRACKS::iterator it = in_net.begin(); it != in_net.end(); /* iterates in the loop body */ )
256  {
257  TRACK* t = *it;
258 
259  if( ( t->GetLayerSet() & lset ).any() && ( t->GetStart() == next || t->GetEnd() == next ) )
260  {
261  at_next.push_back( t );
262  it = in_net.erase( it );
263  ++track_count;
264  }
265  else
266  {
267  ++it;
268  }
269  }
270 
271  return track_count;
272 }
CITER next(CITER it)
Definition: ptree.cpp:130
KICAD_T Type() const
Function Type()
Definition: base_struct.h:209
const wxPoint & GetEnd() const
Definition: class_track.h:119
const wxPoint & GetStart() const
Definition: class_track.h:122
virtual LSET GetLayerSet() const
Function GetLayerSet returns a "layer mask", which is a bitmap of all layers on which the TRACK segme...
class VIA, a via (like a track segment on a copper layer)
Definition: typeinfo.h:96
static void otherEnd ( const TRACK aTrack,
const wxPoint aNotThisEnd,
wxPoint aOtherEnd 
)
static

Definition at line 216 of file class_board.cpp.

References TRACK::GetEnd(), and TRACK::GetStart().

Referenced by checkConnectedTo().

217 {
218  if( aTrack.GetStart() == aNotThisEnd )
219  {
220  *aOtherEnd = aTrack.GetEnd();
221  }
222  else
223  {
224  wxASSERT( aTrack.GetEnd() == aNotThisEnd );
225  *aOtherEnd = aTrack.GetStart();
226  }
227 }
const wxPoint & GetEnd() const
Definition: class_track.h:119
const wxPoint & GetStart() const
Definition: class_track.h:122
static void removeTrack ( TRACKS *  aList,
TRACK aOneToRemove 
)
static

Function removeTrack removes aOneToRemove from aList, which is a non-owning std::vector.

Definition at line 210 of file class_board.cpp.

Referenced by checkConnectedTo().

211 {
212  aList->erase( std::remove( aList->begin(), aList->end(), aOneToRemove ), aList->end() );
213 }
static bool sortNetsByNames ( const NETINFO_ITEM a,
const NETINFO_ITEM b 
)
static

Definition at line 1421 of file class_board.cpp.

References NETINFO_ITEM::GetNetname().

Referenced by BOARD::SortedNetnamesList().

1422 {
1423  return a->GetNetname() < b->GetNetname();
1424 }
const wxString & GetNetname() const
Function GetNetname.
Definition: netinfo.h:235
static bool sortNetsByNodes ( const NETINFO_ITEM a,
const NETINFO_ITEM b 
)
static

Definition at line 1409 of file class_board.cpp.

References NETINFO_ITEM::GetNet(), NETINFO_ITEM::GetNetname(), and padCountListByNet.

Referenced by BOARD::SortedNetnamesList().

1410 {
1411  int countA = padCountListByNet[a->GetNet()];
1412  int countB = padCountListByNet[b->GetNet()];
1413 
1414  if( countA == countB )
1415  return a->GetNetname() < b->GetNetname();
1416  else
1417  return countB < countA;
1418 }
int GetNet() const
Function GetNet.
Definition: netinfo.h:227
const wxString & GetNetname() const
Function GetNetname.
Definition: netinfo.h:235
static std::vector< int > padCountListByNet
bool sortPadsByXthenYCoord ( D_PAD *const &  ref,
D_PAD *const &  comp 
)

Function SortPadsByXCoord is used by GetSortedPadListByXCoord to Sort a pad list by x coordinate value.

This function is used to build ordered pads lists

Definition at line 1751 of file class_board.cpp.

References D_PAD::GetPosition(), wxPoint::x, and wxPoint::y.

Referenced by DRAG_LIST::BuildDragListe(), BOARD::GetSortedPadListByXthenYCoord(), and DRAG_SEGM_PICKER::SetTrackEndsCoordinates().

1752 {
1753  if( ref->GetPosition().x == comp->GetPosition().x )
1754  return ref->GetPosition().y < comp->GetPosition().y;
1755  return ref->GetPosition().x < comp->GetPosition().x;
1756 }
const wxPoint GetPosition() const override
Definition: class_pad.h:220

Variable Documentation

COLORS_DESIGN_SETTINGS dummyColorsSettings(FRAME_PCB)
static

Referenced by BOARD::BOARD().

std::vector<int> padCountListByNet
static

Definition at line 1405 of file class_board.cpp.

Referenced by BOARD::SortedNetnamesList(), and sortNetsByNodes().