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/connectivity_data.h>
#include <stdio.h>

Go to the source code of this file.

Classes

class  DELETED_BOARD_ITEM
 A singleton item of this class is returned for a weak reference that no longer exists. More...
 

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, wxPoint *aErrorLocation=nullptr)
 

Variables

DELETED_BOARD_ITEM g_DeletedItem
 
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

◆ BuildBoardPolygonOutlines()

bool BuildBoardPolygonOutlines ( BOARD aBoard,
SHAPE_POLY_SET aOutlines,
wxString *  aErrorText,
unsigned int  aTolerance,
wxPoint *  aErrorLocation = nullptr 
)

Definition at line 712 of file convert_drawsegment_list_to_polygon.cpp.

714 {
715  PCB_TYPE_COLLECTOR items;
716 
717  // Get all the DRAWSEGMENTS and module graphics into 'items',
718  // then keep only those on layer == Edge_Cuts.
719  static const KICAD_T scan_graphics[] = { PCB_LINE_T, PCB_MODULE_EDGE_T, EOT };
720  items.Collect( aBoard, scan_graphics );
721 
722  // Make a working copy of aSegList, because the list is modified during calculations
723  std::vector< DRAWSEGMENT* > segList;
724 
725  for( int ii = 0; ii < items.GetCount(); ii++ )
726  {
727  if( items[ii]->GetLayer() == Edge_Cuts )
728  segList.push_back( static_cast< DRAWSEGMENT* >( items[ii] ) );
729  }
730 
731  bool success = ConvertOutlineToPolygon( segList, aOutlines, aErrorText, aTolerance, aErrorLocation );
732 
733  if( !success || !aOutlines.OutlineCount() )
734  {
735  // Creates a valid polygon outline is not possible.
736  // So uses the board edge cuts bounding box to create a
737  // rectangular outline
738  // When no edge cuts items, build a contour
739  // from global bounding box
740 
741  EDA_RECT bbbox = aBoard->GetBoardEdgesBoundingBox();
742 
743  // If null area, uses the global bounding box.
744  if( ( bbbox.GetWidth() ) == 0 || ( bbbox.GetHeight() == 0 ) )
745  bbbox = aBoard->ComputeBoundingBox();
746 
747  // Ensure non null area. If happen, gives a minimal size.
748  if( ( bbbox.GetWidth() ) == 0 || ( bbbox.GetHeight() == 0 ) )
749  bbbox.Inflate( Millimeter2iu( 1.0 ) );
750 
751  aOutlines.RemoveAllContours();
752  aOutlines.NewOutline();
753 
754  wxPoint corner;
755  aOutlines.Append( bbbox.GetOrigin() );
756 
757  corner.x = bbbox.GetOrigin().x;
758  corner.y = bbbox.GetEnd().y;
759  aOutlines.Append( corner );
760 
761  aOutlines.Append( bbbox.GetEnd() );
762 
763  corner.x = bbbox.GetEnd().x;
764  corner.y = bbbox.GetOrigin().y;
765  aOutlines.Append( corner );
766  }
767 
768  return success;
769 }
int OutlineCount() const
Returns the number of outlines in the set
const EDA_RECT GetBoardEdgesBoundingBox() const
Function GetBoardEdgesBoundingBox Returns the board bounding box calculated using exclusively the boa...
Definition: class_board.h:810
int GetWidth() const
Definition: eda_rect.h:117
bool ConvertOutlineToPolygon(std::vector< DRAWSEGMENT * > &aSegList, SHAPE_POLY_SET &aPolygons, wxString *aErrorText, unsigned int aTolerance, wxPoint *aErrorLocation)
Function ConvertOutlineToPolygon build a polygon (with holes) from a DRAWSEGMENT list,...
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
int GetCount() const
Function GetCount returns the number of objects in the list.
Definition: collector.h:114
const wxPoint GetEnd() const
Definition: eda_rect.h:114
const wxPoint GetOrigin() const
Definition: eda_rect.h:112
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's Inspector method, which does the collection.
Definition: collectors.cpp:522
int GetHeight() const
Definition: eda_rect.h:118
void RemoveAllContours()
Removes all outlines & holes (clears) the polygon set.
Class EDA_RECT handles the component boundary box.
Definition: eda_rect.h:44
static bool GetLayer(MODEL_VRML &aModel, LAYER_NUM layer, VRML_LAYER **vlayer)
EDA_RECT ComputeBoundingBox(bool aBoardEdgesOnly=false) const
Function ComputeBoundingBox calculates the bounding box containing all board items (or board edge seg...
Collect all BOARD_ITEM objects of a given set of KICAD_T type(s).
Definition: collectors.h:627
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)

Referenced by BOARD::GetBoardPolygonOutlines().

◆ checkConnectedTo()

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 318 of file class_board.cpp.

320 {
321  TRACKS in_net = aTracksInNet; // copy source list so the copy can be modified
322  wxPoint next;
323 
324  otherEnd( *aFirstTrack, aStart, &next );
325 
326  aList->push_back( aFirstTrack );
327  removeTrack( &in_net, aFirstTrack );
328 
329  LSET lset( aFirstTrack->GetLayer() );
330 
331  while( in_net.size() )
332  {
333  if( next == aGoal )
334  return; // success
335 
336  // Want an exact match on the position of next, i.e. pad at next,
337  // not a forgiving HitTest() with tolerance type of match, otherwise the overall
338  // algorithm will not work. GetPadFast() is an exact match as I write this.
339  if( aBoard->GetPadFast( next, lset ) )
340  {
341  std::string m = StrPrintf(
342  "intervening pad at:(xy %s) between start:(xy %s) and goal:(xy %s)",
343  FormatInternalUnits( next ).c_str(),
344  FormatInternalUnits( aStart ).c_str(),
345  FormatInternalUnits( aGoal ).c_str()
346  );
347  THROW_IO_ERROR( m );
348  }
349 
350  int track_count = find_vias_and_tracks_at( *aList, in_net, lset, next );
351 
352  if( track_count != 1 )
353  {
354  std::string m = StrPrintf(
355  "found %d tracks intersecting at (xy %s), exactly 2 would be acceptable.",
356  track_count + aList->size() == 1 ? 1 : 0,
357  FormatInternalUnits( next ).c_str()
358  );
359  THROW_IO_ERROR( m );
360  }
361 
362  // reduce lset down to the layer that the last track at 'next' is on.
363  lset = aList->back()->GetLayerSet();
364 
365  otherEnd( *aList->back(), next, &next );
366  }
367 
368  std::string m = StrPrintf(
369  "not enough tracks connecting start:(xy %s) and goal:(xy %s).",
370  FormatInternalUnits( aStart ).c_str(),
371  FormatInternalUnits( aGoal ).c_str()
372  );
373  THROW_IO_ERROR( m );
374 }
CITER next(CITER it)
Definition: ptree.cpp:130
int StrPrintf(std::string *aResult, const char *aFormat,...)
Function StrPrintf is like sprintf() but the output is appended to a std::string instead of to a char...
Definition: richio.cpp:74
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)
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...
Class LSET is a set of PCB_LAYER_IDs.
#define THROW_IO_ERROR(msg)
virtual PCB_LAYER_ID GetLayer() const
Function GetLayer returns the primary layer this item is on.
std::string FormatInternalUnits(int aValue)
Function FormatInternalUnits converts aValue from internal units to a string appropriate for writing ...
Definition: base_units.cpp:457

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

Referenced by BOARD::TracksInNetBetweenPoints().

◆ find_vias_and_tracks_at()

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 266 of file class_board.cpp.

267 {
268  // first find all vias (in this net) at 'next' location, and expand LSET with each
269  for( TRACKS::iterator it = in_net.begin(); it != in_net.end(); )
270  {
271  TRACK* t = *it;
272 
273  if( t->Type() == PCB_VIA_T && (t->GetLayerSet() & lset).any() &&
274  ( t->GetStart() == next || t->GetEnd() == next ) )
275  {
276  lset |= t->GetLayerSet();
277  at_next.push_back( t );
278  it = in_net.erase( it );
279  }
280  else
281  ++it;
282  }
283 
284  int track_count = 0;
285 
286  // with expanded lset, find all tracks with an end on any of the layers in lset
287  for( TRACKS::iterator it = in_net.begin(); it != in_net.end(); /* iterates in the loop body */ )
288  {
289  TRACK* t = *it;
290 
291  if( ( t->GetLayerSet() & lset ).any() && ( t->GetStart() == next || t->GetEnd() == next ) )
292  {
293  at_next.push_back( t );
294  it = in_net.erase( it );
295  ++track_count;
296  }
297  else
298  {
299  ++it;
300  }
301  }
302 
303  return track_count;
304 }
CITER next(CITER it)
Definition: ptree.cpp:130
const wxPoint & GetStart() const
Definition: class_track.h:126
const wxPoint & GetEnd() const
Definition: class_track.h:123
class VIA, a via (like a track segment on a copper layer)
Definition: typeinfo.h:96
virtual LSET GetLayerSet() const
Function GetLayerSet returns a "layer mask", which is a bitmap of all layers on which the TRACK segme...
KICAD_T Type() const
Function Type()
Definition: base_struct.h:201

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

Referenced by checkConnectedTo().

◆ otherEnd()

static void otherEnd ( const TRACK aTrack,
const wxPoint &  aNotThisEnd,
wxPoint *  aOtherEnd 
)
static

Definition at line 248 of file class_board.cpp.

249 {
250  if( aTrack.GetStart() == aNotThisEnd )
251  {
252  *aOtherEnd = aTrack.GetEnd();
253  }
254  else
255  {
256  wxASSERT( aTrack.GetEnd() == aNotThisEnd );
257  *aOtherEnd = aTrack.GetStart();
258  }
259 }
const wxPoint & GetStart() const
Definition: class_track.h:126
const wxPoint & GetEnd() const
Definition: class_track.h:123

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

Referenced by checkConnectedTo().

◆ removeTrack()

static void removeTrack ( TRACKS *  aList,
TRACK aOneToRemove 
)
static

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

Definition at line 242 of file class_board.cpp.

243 {
244  aList->erase( std::remove( aList->begin(), aList->end(), aOneToRemove ), aList->end() );
245 }

Referenced by checkConnectedTo().

◆ sortNetsByNames()

static bool sortNetsByNames ( const NETINFO_ITEM a,
const NETINFO_ITEM b 
)
static

Definition at line 1495 of file class_board.cpp.

1496 {
1497  return a->GetNetname() < b->GetNetname();
1498 }
const wxString & GetNetname() const
Function GetNetname.
Definition: netinfo.h:239

References NETINFO_ITEM::GetNetname().

Referenced by BOARD::SortedNetnamesList().

◆ sortNetsByNodes()

static bool sortNetsByNodes ( const NETINFO_ITEM a,
const NETINFO_ITEM b 
)
static

Definition at line 1483 of file class_board.cpp.

1484 {
1485  int countA = padCountListByNet[a->GetNet()];
1486  int countB = padCountListByNet[b->GetNet()];
1487 
1488  if( countA == countB )
1489  return a->GetNetname() < b->GetNetname();
1490  else
1491  return countB < countA;
1492 }
const wxString & GetNetname() const
Function GetNetname.
Definition: netinfo.h:239
int GetNet() const
Function GetNet.
Definition: netinfo.h:231
static std::vector< int > padCountListByNet

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

Referenced by BOARD::SortedNetnamesList().

◆ sortPadsByXthenYCoord()

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 1822 of file class_board.cpp.

1823 {
1824  if( ref->GetPosition().x == comp->GetPosition().x )
1825  return ref->GetPosition().y < comp->GetPosition().y;
1826  return ref->GetPosition().x < comp->GetPosition().x;
1827 }
const wxPoint GetPosition() const override
Definition: class_pad.h:220

References D_PAD::GetPosition().

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

Variable Documentation

◆ dummyColorsSettings

COLORS_DESIGN_SETTINGS dummyColorsSettings(FRAME_PCB)
static

Referenced by BOARD::BOARD().

◆ g_DeletedItem

DELETED_BOARD_ITEM g_DeletedItem

Definition at line 95 of file class_board.cpp.

Referenced by BOARD::GetItem().

◆ padCountListByNet

std::vector<int> padCountListByNet
static

Definition at line 1479 of file class_board.cpp.

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